linux-2.6.20.4-openswan-2.4.7.kernel-2.6-klips.patch 1.8 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334
  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,341 @@
  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.9 2006/07/29 05:00:40 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. +#ifdef NET_21
  3907. +# include <linux/in6.h>
  3908. +#else
  3909. + /* old kernel in.h has some IPv6 stuff, but not quite enough */
  3910. +# define s6_addr16 s6_addr
  3911. +# define AF_INET6 10
  3912. +# define uint8_t __u8
  3913. +# define uint16_t __u16
  3914. +# define uint32_t __u32
  3915. +# define uint64_t __u64
  3916. +#endif
  3917. +
  3918. +#ifdef NET_21
  3919. +# define ipsec_kfree_skb(a) kfree_skb(a)
  3920. +#else /* NET_21 */
  3921. +# define ipsec_kfree_skb(a) kfree_skb(a, FREE_WRITE)
  3922. +#endif /* NET_21 */
  3923. +
  3924. +#ifdef NETDEV_23
  3925. +#if 0
  3926. +#ifndef NETDEV_25
  3927. +#define device net_device
  3928. +#endif
  3929. +#endif
  3930. +# define ipsec_dev_get dev_get_by_name
  3931. +# define __ipsec_dev_get __dev_get_by_name
  3932. +# define ipsec_dev_put(x) dev_put(x)
  3933. +# define __ipsec_dev_put(x) __dev_put(x)
  3934. +# define ipsec_dev_hold(x) dev_hold(x)
  3935. +#else /* NETDEV_23 */
  3936. +# define ipsec_dev_get dev_get
  3937. +# define __ipsec_dev_put(x)
  3938. +# define ipsec_dev_put(x)
  3939. +# define ipsec_dev_hold(x)
  3940. +#endif /* NETDEV_23 */
  3941. +
  3942. +#ifndef SPINLOCK
  3943. +# include <linux/bios32.h>
  3944. + /* simulate spin locks and read/write locks */
  3945. + typedef struct {
  3946. + volatile char lock;
  3947. + } spinlock_t;
  3948. +
  3949. + typedef struct {
  3950. + volatile unsigned int lock;
  3951. + } rwlock_t;
  3952. +
  3953. +# define spin_lock_init(x) { (x)->lock = 0;}
  3954. +# define rw_lock_init(x) { (x)->lock = 0; }
  3955. +
  3956. +# define spin_lock(x) { while ((x)->lock) barrier(); (x)->lock=1;}
  3957. +# define spin_lock_irq(x) { cli(); spin_lock(x);}
  3958. +# define spin_lock_irqsave(x,flags) { save_flags(flags); spin_lock_irq(x);}
  3959. +
  3960. +# define spin_unlock(x) { (x)->lock=0;}
  3961. +# define spin_unlock_irq(x) { spin_unlock(x); sti();}
  3962. +# define spin_unlock_irqrestore(x,flags) { spin_unlock(x); restore_flags(flags);}
  3963. +
  3964. +# define read_lock(x) spin_lock(x)
  3965. +# define read_lock_irq(x) spin_lock_irq(x)
  3966. +# define read_lock_irqsave(x,flags) spin_lock_irqsave(x,flags)
  3967. +
  3968. +# define read_unlock(x) spin_unlock(x)
  3969. +# define read_unlock_irq(x) spin_unlock_irq(x)
  3970. +# define read_unlock_irqrestore(x,flags) spin_unlock_irqrestore(x,flags)
  3971. +
  3972. +# define write_lock(x) spin_lock(x)
  3973. +# define write_lock_irq(x) spin_lock_irq(x)
  3974. +# define write_lock_irqsave(x,flags) spin_lock_irqsave(x,flags)
  3975. +
  3976. +# define write_unlock(x) spin_unlock(x)
  3977. +# define write_unlock_irq(x) spin_unlock_irq(x)
  3978. +# define write_unlock_irqrestore(x,flags) spin_unlock_irqrestore(x,flags)
  3979. +#endif /* !SPINLOCK */
  3980. +
  3981. +#ifndef SPINLOCK_23
  3982. +# define spin_lock_bh(x) spin_lock_irq(x)
  3983. +# define spin_unlock_bh(x) spin_unlock_irq(x)
  3984. +
  3985. +# define read_lock_bh(x) read_lock_irq(x)
  3986. +# define read_unlock_bh(x) read_unlock_irq(x)
  3987. +
  3988. +# define write_lock_bh(x) write_lock_irq(x)
  3989. +# define write_unlock_bh(x) write_unlock_irq(x)
  3990. +#endif /* !SPINLOCK_23 */
  3991. +
  3992. +#ifndef HAVE_NETDEV_PRINTK
  3993. +#define netdev_printk(sevlevel, netdev, msglevel, format, arg...) \
  3994. + printk(sevlevel "%s: " format , netdev->name , ## arg)
  3995. +#endif
  3996. +
  3997. +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
  3998. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  3999. +#include "openswan/ipsec_kern24.h"
  4000. +#else
  4001. +#error "kernels before 2.4 are not supported at this time"
  4002. +#endif
  4003. +#endif
  4004. +
  4005. +
  4006. +#endif /* _OPENSWAN_KVERSIONS_H */
  4007. +
  4008. +/*
  4009. + * $Log: ipsec_kversion.h,v $
  4010. + * Revision 1.15.2.9 2006/07/29 05:00:40 paul
  4011. + * Added HAVE_NEW_SKB_LINEARIZE for 2.6.18+ kernels where skb_linearize
  4012. + * only takes 1 argument.
  4013. + *
  4014. + * Revision 1.15.2.8 2006/05/01 14:31:52 mcr
  4015. + * FREESWAN->OPENSWAN in #ifdef.
  4016. + *
  4017. + * Revision 1.15.2.7 2006/01/11 02:02:59 mcr
  4018. + * updated patches and DEFAULT_TTL code to work
  4019. + *
  4020. + * Revision 1.15.2.6 2006/01/03 19:25:02 ken
  4021. + * Remove duplicated #ifdef for TTL fix - bad patch
  4022. + *
  4023. + * Revision 1.15.2.5 2006/01/03 18:06:33 ken
  4024. + * Fix for missing sysctl default ttl
  4025. + *
  4026. + * Revision 1.15.2.4 2005/11/27 21:40:14 paul
  4027. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"
  4028. + * in for klips as module.
  4029. + *
  4030. + * Revision 1.15.2.3 2005/11/22 04:11:52 ken
  4031. + * Backport fixes for 2.6.14 kernels from HEAD
  4032. + *
  4033. + * Revision 1.15.2.2 2005/09/01 01:57:19 paul
  4034. + * michael's fixes for 2.6.13 from head
  4035. + *
  4036. + * Revision 1.15.2.1 2005/08/27 23:13:48 paul
  4037. + * Fix for:
  4038. + * 7 weeks ago: [NET]: Remove unused security member in sk_buff
  4039. + * changeset 4280: 328ea53f5fee
  4040. + * parent 4279: beb0afb0e3f8
  4041. + * author: Thomas Graf <tgraf@suug.ch>
  4042. + * date: Tue Jul 5 21:12:44 2005
  4043. + * 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
  4044. + *
  4045. + * This should fix compilation on 2.6.13(rc) kernels
  4046. + *
  4047. + * Revision 1.15 2005/07/19 20:02:15 mcr
  4048. + * sk_alloc() interface change.
  4049. + *
  4050. + * Revision 1.14 2005/07/08 16:20:05 mcr
  4051. + * fix for 2.6.12 disapperance of sk_zapped field -> sock_flags.
  4052. + *
  4053. + * Revision 1.13 2005/05/20 03:19:18 mcr
  4054. + * modifications for use on 2.4.30 kernel, with backported
  4055. + * printk_ratelimit(). all warnings removed.
  4056. + *
  4057. + * Revision 1.12 2005/04/13 22:46:21 mcr
  4058. + * note that KLIPS does not work on Linux 2.0.
  4059. + *
  4060. + * Revision 1.11 2004/09/13 02:22:26 mcr
  4061. + * #define inet_protocol if necessary.
  4062. + *
  4063. + * Revision 1.10 2004/08/03 18:17:15 mcr
  4064. + * in 2.6, use "net_device" instead of #define device->net_device.
  4065. + * this probably breaks 2.0 compiles.
  4066. + *
  4067. + * Revision 1.9 2004/04/05 19:55:05 mcr
  4068. + * Moved from linux/include/freeswan/ipsec_kversion.h,v
  4069. + *
  4070. + * Revision 1.8 2003/12/13 19:10:16 mcr
  4071. + * refactored rcv and xmit code - same as FS 2.05.
  4072. + *
  4073. + * Revision 1.7 2003/07/31 22:48:08 mcr
  4074. + * derive NET25-ness from presence of NETLINK_XFRM macro.
  4075. + *
  4076. + * Revision 1.6 2003/06/24 20:22:32 mcr
  4077. + * added new global: ipsecdevices[] so that we can keep track of
  4078. + * the ipsecX devices. They will be referenced with dev_hold(),
  4079. + * so 2.2 may need this as well.
  4080. + *
  4081. + * Revision 1.5 2003/04/03 17:38:09 rgb
  4082. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  4083. + *
  4084. + * Revision 1.4 2002/04/24 07:36:46 mcr
  4085. + * Moved from ./klips/net/ipsec/ipsec_kversion.h,v
  4086. + *
  4087. + * Revision 1.3 2002/04/12 03:21:17 mcr
  4088. + * three parameter version of ip_select_ident appears first
  4089. + * in 2.4.2 (RH7.1) not 2.4.4.
  4090. + *
  4091. + * Revision 1.2 2002/03/08 21:35:22 rgb
  4092. + * Defined LINUX_KERNEL_HAS_SNPRINTF to shut up compiler warnings after
  4093. + * 2.4.9. (Andreas Piesk).
  4094. + *
  4095. + * Revision 1.1 2002/01/29 02:11:42 mcr
  4096. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  4097. + * updating of IPv6 structures to match latest in6.h version.
  4098. + * removed dead code from freeswan.h that also duplicated kversions.h
  4099. + * code.
  4100. + *
  4101. + *
  4102. + */
  4103. --- /dev/null Tue Mar 11 13:02:56 2003
  4104. +++ linux/include/openswan/ipsec_life.h Mon Feb 9 13:51:03 2004
  4105. @@ -0,0 +1,112 @@
  4106. +/*
  4107. + * Definitions relevant to IPSEC lifetimes
  4108. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4109. + * and Michael Richardson <mcr@freeswan.org>
  4110. + *
  4111. + * This program is free software; you can redistribute it and/or modify it
  4112. + * under the terms of the GNU General Public License as published by the
  4113. + * Free Software Foundation; either version 2 of the License, or (at your
  4114. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4115. + *
  4116. + * This program is distributed in the hope that it will be useful, but
  4117. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4118. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4119. + * for more details.
  4120. + *
  4121. + * RCSID $Id: ipsec_life.h,v 1.4 2004/04/05 19:55:05 mcr Exp $
  4122. + *
  4123. + * This file derived from ipsec_xform.h on 2001/9/18 by mcr.
  4124. + *
  4125. + */
  4126. +
  4127. +/*
  4128. + * This file describes the book keeping fields for the
  4129. + * IPsec Security Association Structure. ("ipsec_sa")
  4130. + *
  4131. + * This structure is never allocated directly by kernel code,
  4132. + * (it is always a static/auto or is part of a structure)
  4133. + * so it does not have a reference count.
  4134. + *
  4135. + */
  4136. +
  4137. +#ifndef _IPSEC_LIFE_H_
  4138. +
  4139. +/*
  4140. + * _count is total count.
  4141. + * _hard is hard limit (kill SA after this number)
  4142. + * _soft is soft limit (try to renew SA after this number)
  4143. + * _last is used in some special cases.
  4144. + *
  4145. + */
  4146. +
  4147. +struct ipsec_lifetime64
  4148. +{
  4149. + __u64 ipl_count;
  4150. + __u64 ipl_soft;
  4151. + __u64 ipl_hard;
  4152. + __u64 ipl_last;
  4153. +};
  4154. +
  4155. +struct ipsec_lifetimes
  4156. +{
  4157. + /* number of bytes processed */
  4158. + struct ipsec_lifetime64 ipl_bytes;
  4159. +
  4160. + /* number of packets processed */
  4161. + struct ipsec_lifetime64 ipl_packets;
  4162. +
  4163. + /* time since SA was added */
  4164. + struct ipsec_lifetime64 ipl_addtime;
  4165. +
  4166. + /* time since SA was first used */
  4167. + struct ipsec_lifetime64 ipl_usetime;
  4168. +
  4169. + /* from rfc2367:
  4170. + * For CURRENT, the number of different connections,
  4171. + * endpoints, or flows that the association has been
  4172. + * allocated towards. For HARD and SOFT, the number of
  4173. + * these the association may be allocated towards
  4174. + * before it expires. The concept of a connection,
  4175. + * flow, or endpoint is system specific.
  4176. + *
  4177. + * mcr(2001-9-18) it is unclear what purpose these serve for FreeSWAN.
  4178. + * They are maintained for PF_KEY compatibility.
  4179. + */
  4180. + struct ipsec_lifetime64 ipl_allocations;
  4181. +};
  4182. +
  4183. +enum ipsec_life_alive {
  4184. + ipsec_life_harddied = -1,
  4185. + ipsec_life_softdied = 0,
  4186. + ipsec_life_okay = 1
  4187. +};
  4188. +
  4189. +enum ipsec_life_type {
  4190. + ipsec_life_timebased = 1,
  4191. + ipsec_life_countbased= 0
  4192. +};
  4193. +
  4194. +#define _IPSEC_LIFE_H_
  4195. +#endif /* _IPSEC_LIFE_H_ */
  4196. +
  4197. +
  4198. +/*
  4199. + * $Log: ipsec_life.h,v $
  4200. + * Revision 1.4 2004/04/05 19:55:05 mcr
  4201. + * Moved from linux/include/freeswan/ipsec_life.h,v
  4202. + *
  4203. + * Revision 1.3 2002/04/24 07:36:46 mcr
  4204. + * Moved from ./klips/net/ipsec/ipsec_life.h,v
  4205. + *
  4206. + * Revision 1.2 2001/11/26 09:16:14 rgb
  4207. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  4208. + *
  4209. + * Revision 1.1.2.1 2001/09/25 02:25:58 mcr
  4210. + * lifetime structure created and common functions created.
  4211. + *
  4212. + *
  4213. + * Local variables:
  4214. + * c-file-style: "linux"
  4215. + * End:
  4216. + *
  4217. + */
  4218. --- /dev/null Tue Mar 11 13:02:56 2003
  4219. +++ linux/include/openswan/ipsec_md5h.h Mon Feb 9 13:51:03 2004
  4220. @@ -0,0 +1,143 @@
  4221. +/*
  4222. + * RCSID $Id: ipsec_md5h.h,v 1.10 2004/09/08 17:21:35 ken Exp $
  4223. + */
  4224. +
  4225. +/*
  4226. + * The rest of this file is Copyright RSA DSI. See the following comments
  4227. + * for the full Copyright notice.
  4228. + */
  4229. +
  4230. +#ifndef _IPSEC_MD5H_H_
  4231. +#define _IPSEC_MD5H_H_
  4232. +
  4233. +/* GLOBAL.H - RSAREF types and constants
  4234. + */
  4235. +
  4236. +/* PROTOTYPES should be set to one if and only if the compiler supports
  4237. + function argument prototyping.
  4238. + The following makes PROTOTYPES default to 0 if it has not already
  4239. + been defined with C compiler flags.
  4240. + */
  4241. +#ifndef PROTOTYPES
  4242. +#define PROTOTYPES 1
  4243. +#endif /* !PROTOTYPES */
  4244. +
  4245. +/* POINTER defines a generic pointer type */
  4246. +typedef __u8 *POINTER;
  4247. +
  4248. +/* UINT2 defines a two byte word */
  4249. +typedef __u16 UINT2;
  4250. +
  4251. +/* UINT4 defines a four byte word */
  4252. +typedef __u32 UINT4;
  4253. +
  4254. +/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  4255. + If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  4256. + returns an empty list.
  4257. + */
  4258. +
  4259. +#if PROTOTYPES
  4260. +#define PROTO_LIST(list) list
  4261. +#else /* PROTOTYPES */
  4262. +#define PROTO_LIST(list) ()
  4263. +#endif /* PROTOTYPES */
  4264. +
  4265. +
  4266. +/* MD5.H - header file for MD5C.C
  4267. + */
  4268. +
  4269. +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  4270. +rights reserved.
  4271. +
  4272. +License to copy and use this software is granted provided that it
  4273. +is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  4274. +Algorithm" in all material mentioning or referencing this software
  4275. +or this function.
  4276. +
  4277. +License is also granted to make and use derivative works provided
  4278. +that such works are identified as "derived from the RSA Data
  4279. +Security, Inc. MD5 Message-Digest Algorithm" in all material
  4280. +mentioning or referencing the derived work.
  4281. +
  4282. +RSA Data Security, Inc. makes no representations concerning either
  4283. +the merchantability of this software or the suitability of this
  4284. +software for any particular purpose. It is provided "as is"
  4285. +without express or implied warranty of any kind.
  4286. +
  4287. +These notices must be retained in any copies of any part of this
  4288. +documentation and/or software.
  4289. + */
  4290. +
  4291. +/* MD5 context. */
  4292. +typedef struct {
  4293. + UINT4 state[4]; /* state (ABCD) */
  4294. + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  4295. + unsigned char buffer[64]; /* input buffer */
  4296. +} MD5_CTX;
  4297. +
  4298. +void osMD5Init PROTO_LIST ((void *));
  4299. +void osMD5Update PROTO_LIST
  4300. + ((void *, unsigned char *, __u32));
  4301. +void osMD5Final PROTO_LIST ((unsigned char [16], void *));
  4302. +
  4303. +#endif /* _IPSEC_MD5H_H_ */
  4304. +
  4305. +/*
  4306. + * $Log: ipsec_md5h.h,v $
  4307. + * Revision 1.10 2004/09/08 17:21:35 ken
  4308. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  4309. + *
  4310. + * Revision 1.9 2004/04/05 19:55:05 mcr
  4311. + * Moved from linux/include/freeswan/ipsec_md5h.h,v
  4312. + *
  4313. + * Revision 1.8 2002/09/10 01:45:09 mcr
  4314. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  4315. + * the function prototypes would match, and could be placed
  4316. + * into a pointer to a function.
  4317. + *
  4318. + * Revision 1.7 2002/04/24 07:36:46 mcr
  4319. + * Moved from ./klips/net/ipsec/ipsec_md5h.h,v
  4320. + *
  4321. + * Revision 1.6 1999/12/13 13:59:13 rgb
  4322. + * Quick fix to argument size to Update bugs.
  4323. + *
  4324. + * Revision 1.5 1999/12/07 18:16:23 rgb
  4325. + * Fixed comments at end of #endif lines.
  4326. + *
  4327. + * Revision 1.4 1999/04/06 04:54:26 rgb
  4328. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  4329. + * patch shell fixes.
  4330. + *
  4331. + * Revision 1.3 1999/01/22 06:19:58 rgb
  4332. + * 64-bit clean-up.
  4333. + *
  4334. + * Revision 1.2 1998/11/30 13:22:54 rgb
  4335. + * Rationalised all the klips kernel file headers. They are much shorter
  4336. + * now and won't conflict under RH5.2.
  4337. + *
  4338. + * Revision 1.1 1998/06/18 21:27:48 henry
  4339. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  4340. + * kernel-build scripts happier in the presence of symlinks
  4341. + *
  4342. + * Revision 1.2 1998/04/23 20:54:03 rgb
  4343. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  4344. + * verified.
  4345. + *
  4346. + * Revision 1.1 1998/04/09 03:04:21 henry
  4347. + * sources moved up from linux/net/ipsec
  4348. + * these two include files modified not to include others except in kernel
  4349. + *
  4350. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  4351. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  4352. + *
  4353. + * Revision 0.4 1997/01/15 01:28:15 ji
  4354. + * No changes.
  4355. + *
  4356. + * Revision 0.3 1996/11/20 14:48:53 ji
  4357. + * Release update only.
  4358. + *
  4359. + * Revision 0.2 1996/11/02 00:18:33 ji
  4360. + * First limited release.
  4361. + *
  4362. + *
  4363. + */
  4364. --- /dev/null Tue Mar 11 13:02:56 2003
  4365. +++ linux/include/openswan/ipsec_param.h Mon Feb 9 13:51:03 2004
  4366. @@ -0,0 +1,387 @@
  4367. +/*
  4368. + * @(#) Openswan tunable paramaters
  4369. + *
  4370. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4371. + * and Michael Richardson <mcr@freeswan.org>
  4372. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  4373. + *
  4374. + * This program is free software; you can redistribute it and/or modify it
  4375. + * under the terms of the GNU General Public License as published by the
  4376. + * Free Software Foundation; either version 2 of the License, or (at your
  4377. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4378. + *
  4379. + * This program is distributed in the hope that it will be useful, but
  4380. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4381. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4382. + * for more details.
  4383. + *
  4384. + * RCSID $Id: ipsec_param.h,v 1.29.6.3 2006/05/01 14:32:31 mcr Exp $
  4385. + *
  4386. + */
  4387. +
  4388. +/*
  4389. + * This file provides a set of #define's which may be tuned by various
  4390. + * people/configurations. It keeps all compile-time tunables in one place.
  4391. + *
  4392. + * This file should be included before all other IPsec kernel-only files.
  4393. + *
  4394. + */
  4395. +
  4396. +#ifndef _IPSEC_PARAM_H_
  4397. +
  4398. +#ifdef __KERNEL__
  4399. +#include "ipsec_kversion.h"
  4400. +
  4401. +/* Set number of ipsecX virtual devices here. */
  4402. +/* This must be < exp(field width of IPSEC_DEV_FORMAT) */
  4403. +/* It must also be reasonable so as not to overload the memory and CPU */
  4404. +/* constraints of the host. */
  4405. +#define IPSEC_NUM_IF 4
  4406. +/* The field width must be < IF_NAM_SIZ - strlen("ipsec") - 1. */
  4407. +/* With "ipsec" being 5 characters, that means 10 is the max field width */
  4408. +/* but machine memory and CPU constraints are not likely to tollerate */
  4409. +/* more than 3 digits. The default is one digit. */
  4410. +/* Update: userland scripts get upset if they can't find "ipsec0", so */
  4411. +/* for now, no "0"-padding should be used (which would have been helpful */
  4412. +/* to make text-searches work */
  4413. +#define IPSEC_DEV_FORMAT "ipsec%d"
  4414. +/* For, say, 500 virtual ipsec devices, I would recommend: */
  4415. +/* #define IPSEC_NUM_IF 500 */
  4416. +/* #define IPSEC_DEV_FORMAT "ipsec%03d" */
  4417. +/* Note that the "interfaces=" line in /etc/ipsec.conf would be, um, challenging. */
  4418. +
  4419. +/* use dynamic ipsecX device allocation */
  4420. +#ifndef CONFIG_KLIPS_DYNDEV
  4421. +#define CONFIG_KLIPS_DYNDEV 1
  4422. +#endif /* CONFIG_KLIPS_DYNDEV */
  4423. +
  4424. +
  4425. +#ifdef CONFIG_KLIPS_BIGGATE
  4426. +# define SADB_HASHMOD 8069
  4427. +#else /* CONFIG_KLIPS_BIGGATE */
  4428. +# define SADB_HASHMOD 257
  4429. +#endif /* CONFIG_KLIPS_BIGGATE */
  4430. +#endif /* __KERNEL__ */
  4431. +
  4432. +/*
  4433. + * This is for the SA reference table. This number is related to the
  4434. + * maximum number of SAs that KLIPS can concurrently deal with, plus enough
  4435. + * space for keeping expired SAs around.
  4436. + *
  4437. + * TABLE_MAX_WIDTH is the number of bits that we will use.
  4438. + * MAIN_TABLE_WIDTH is the number of bits used for the primary index table.
  4439. + *
  4440. + */
  4441. +#ifndef IPSEC_SA_REF_TABLE_IDX_WIDTH
  4442. +# define IPSEC_SA_REF_TABLE_IDX_WIDTH 16
  4443. +#endif
  4444. +
  4445. +#ifndef IPSEC_SA_REF_MAINTABLE_IDX_WIDTH
  4446. +# define IPSEC_SA_REF_MAINTABLE_IDX_WIDTH 4
  4447. +#endif
  4448. +
  4449. +#ifndef IPSEC_SA_REF_FREELIST_NUM_ENTRIES
  4450. +# define IPSEC_SA_REF_FREELIST_NUM_ENTRIES 256
  4451. +#endif
  4452. +
  4453. +#ifndef IPSEC_SA_REF_CODE
  4454. +# define IPSEC_SA_REF_CODE 1
  4455. +#endif
  4456. +
  4457. +#ifdef __KERNEL__
  4458. +/* This is defined for 2.4, but not 2.2.... */
  4459. +#ifndef ARPHRD_VOID
  4460. +# define ARPHRD_VOID 0xFFFF
  4461. +#endif
  4462. +
  4463. +/* always turn on IPIP mode */
  4464. +#ifndef CONFIG_KLIPS_IPIP
  4465. +#define CONFIG_KLIPS_IPIP 1
  4466. +#endif
  4467. +
  4468. +/*
  4469. + * Worry about PROC_FS stuff
  4470. + */
  4471. +#if defined(PROC_FS_2325)
  4472. +/* kernel 2.4 */
  4473. +# define IPSEC_PROC_LAST_ARG ,int *eof,void *data
  4474. +# define IPSEC_PROCFS_DEBUG_NO_STATIC
  4475. +# define IPSEC_PROC_SUBDIRS
  4476. +#else
  4477. +/* kernel <2.4 */
  4478. +# define IPSEC_PROCFS_DEBUG_NO_STATIC DEBUG_NO_STATIC
  4479. +
  4480. +# ifndef PROC_NO_DUMMY
  4481. +# define IPSEC_PROC_LAST_ARG , int dummy
  4482. +# else
  4483. +# define IPSEC_PROC_LAST_ARG
  4484. +# endif /* !PROC_NO_DUMMY */
  4485. +#endif /* PROC_FS_2325 */
  4486. +
  4487. +#if !defined(LINUX_KERNEL_HAS_SNPRINTF)
  4488. +/* GNU CPP specific! */
  4489. +# define snprintf(buf, len, fmt...) sprintf(buf, ##fmt)
  4490. +#endif /* !LINUX_KERNEL_HAS_SNPRINTF */
  4491. +
  4492. +#ifdef SPINLOCK
  4493. +# ifdef SPINLOCK_23
  4494. +# include <linux/spinlock.h> /* *lock* */
  4495. +# else /* SPINLOCK_23 */
  4496. +# include <asm/spinlock.h> /* *lock* */
  4497. +# endif /* SPINLOCK_23 */
  4498. +#endif /* SPINLOCK */
  4499. +
  4500. +#ifndef KLIPS_FIXES_DES_PARITY
  4501. +# define KLIPS_FIXES_DES_PARITY 1
  4502. +#endif /* !KLIPS_FIXES_DES_PARITY */
  4503. +
  4504. +/* we don't really want to print these unless there are really big problems */
  4505. +#ifndef KLIPS_DIVULGE_CYPHER_KEY
  4506. +# define KLIPS_DIVULGE_CYPHER_KEY 0
  4507. +#endif /* !KLIPS_DIVULGE_CYPHER_KEY */
  4508. +
  4509. +#ifndef KLIPS_DIVULGE_HMAC_KEY
  4510. +# define KLIPS_DIVULGE_HMAC_KEY 0
  4511. +#endif /* !KLIPS_DIVULGE_HMAC_KEY */
  4512. +
  4513. +#ifndef IPSEC_DISALLOW_IPOPTIONS
  4514. +# define IPSEC_DISALLOW_IPOPTIONS 1
  4515. +#endif /* !KLIPS_DIVULGE_HMAC_KEY */
  4516. +
  4517. +/* extra toggles for regression testing */
  4518. +#ifdef CONFIG_KLIPS_REGRESS
  4519. +
  4520. +/*
  4521. + * should pfkey_acquire() become 100% lossy?
  4522. + *
  4523. + */
  4524. +extern int sysctl_ipsec_regress_pfkey_lossage;
  4525. +#ifndef KLIPS_PFKEY_ACQUIRE_LOSSAGE
  4526. +# ifdef CONFIG_KLIPS_PFKEY_ACQUIRE_LOSSAGE
  4527. +# define KLIPS_PFKEY_ACQUIRE_LOSSAGE 100
  4528. +# endif /* CONFIG_KLIPS_PFKEY_ACQUIRE_LOSSAGE */
  4529. +#else
  4530. +#define KLIPS_PFKEY_ACQUIRE_LOSSAGE 0
  4531. +#endif /* KLIPS_PFKEY_ACQUIRE_LOSSAGE */
  4532. +
  4533. +#else /* CONFIG_KLIPS_REGRESS */
  4534. +#define KLIPS_PFKEY_ACQUIRE_LOSSAGE 0
  4535. +
  4536. +#endif /* CONFIG_KLIPS_REGRESS */
  4537. +
  4538. +
  4539. +/*
  4540. + * debugging routines.
  4541. + */
  4542. +#define KLIPS_ERROR(flag, format, args...) if(printk_ratelimit() || flag) printk(KERN_ERR "KLIPS " format, ## args)
  4543. +#ifdef CONFIG_KLIPS_DEBUG
  4544. +extern void ipsec_print_ip(struct iphdr *ip);
  4545. +
  4546. + #define KLIPS_PRINT(flag, format, args...) \
  4547. + ((flag) ? printk(KERN_INFO format , ## args) : 0)
  4548. + #define KLIPS_PRINTMORE(flag, format, args...) \
  4549. + ((flag) ? printk(format , ## args) : 0)
  4550. + #define KLIPS_IP_PRINT(flag, ip) \
  4551. + ((flag) ? ipsec_print_ip(ip) : 0)
  4552. +#else /* CONFIG_KLIPS_DEBUG */
  4553. + #define KLIPS_PRINT(flag, format, args...) do ; while(0)
  4554. + #define KLIPS_PRINTMORE(flag, format, args...) do ; while(0)
  4555. + #define KLIPS_IP_PRINT(flag, ip) do ; while(0)
  4556. +#endif /* CONFIG_KLIPS_DEBUG */
  4557. +
  4558. +
  4559. +/*
  4560. + * Stupid kernel API differences in APIs. Not only do some
  4561. + * kernels not have ip_select_ident, but some have differing APIs,
  4562. + * and SuSE has one with one parameter, but no way of checking to
  4563. + * see what is really what.
  4564. + */
  4565. +
  4566. +#ifdef SUSE_LINUX_2_4_19_IS_STUPID
  4567. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph)
  4568. +#else
  4569. +
  4570. +/* simplest case, nothing */
  4571. +#if !defined(IP_SELECT_IDENT)
  4572. +#define KLIPS_IP_SELECT_IDENT(iph, skb) do { iph->id = htons(ip_id_count++); } while(0)
  4573. +#endif
  4574. +
  4575. +/* kernels > 2.3.37-ish */
  4576. +#if defined(IP_SELECT_IDENT) && !defined(IP_SELECT_IDENT_NEW)
  4577. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst)
  4578. +#endif
  4579. +
  4580. +/* kernels > 2.4.2 */
  4581. +#if defined(IP_SELECT_IDENT) && defined(IP_SELECT_IDENT_NEW)
  4582. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst, NULL)
  4583. +#endif
  4584. +
  4585. +#endif /* SUSE_LINUX_2_4_19_IS_STUPID */
  4586. +
  4587. +/*
  4588. + * make klips fail test:east-espiv-01.
  4589. + * exploit is at testing/attacks/espiv
  4590. + *
  4591. + */
  4592. +#define KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK 0
  4593. +
  4594. +
  4595. +/* IP_FRAGMENT_LINEARIZE is set in freeswan.h if Kernel > 2.4.4 */
  4596. +#ifndef IP_FRAGMENT_LINEARIZE
  4597. +# define IP_FRAGMENT_LINEARIZE 0
  4598. +#endif /* IP_FRAGMENT_LINEARIZE */
  4599. +#endif /* __KERNEL__ */
  4600. +
  4601. +#ifdef NEED_INET_PROTOCOL
  4602. +#define inet_protocol net_protocol
  4603. +#endif
  4604. +
  4605. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && CONFIG_IPSEC_NAT_TRAVERSAL
  4606. +#define NAT_TRAVERSAL 1
  4607. +#else
  4608. +/* let people either #undef, or #define = 0 it */
  4609. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  4610. +#undef CONFIG_IPSEC_NAT_TRAVERSAL
  4611. +#endif
  4612. +#endif
  4613. +
  4614. +#ifndef IPSEC_DEFAULT_TTL
  4615. +#define IPSEC_DEFAULT_TTL 64
  4616. +#endif
  4617. +
  4618. +#define _IPSEC_PARAM_H_
  4619. +#endif /* _IPSEC_PARAM_H_ */
  4620. +
  4621. +/*
  4622. + * $Log: ipsec_param.h,v $
  4623. + * Revision 1.29.6.3 2006/05/01 14:32:31 mcr
  4624. + * added KLIPS_ERROR and make sure that things work without CONFIG_KLIPS_REGRESS.
  4625. + *
  4626. + * Revision 1.29.6.2 2005/11/27 21:40:14 paul
  4627. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"
  4628. + * in for klips as module.
  4629. + *
  4630. + * Revision 1.29.6.1 2005/08/12 16:24:18 ken
  4631. + * Pull in NAT-T compile logic from HEAD
  4632. + *
  4633. + * Revision 1.29 2005/01/26 00:50:35 mcr
  4634. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  4635. + * and make sure that NAT_TRAVERSAL is set as well to match
  4636. + * userspace compiles of code.
  4637. + *
  4638. + * Revision 1.28 2004/09/13 15:50:15 mcr
  4639. + * spell NEED_INET properly, not NET_INET.
  4640. + *
  4641. + * Revision 1.27 2004/09/13 02:21:45 mcr
  4642. + * always turn on IPIP mode.
  4643. + * #define inet_protocol if necessary.
  4644. + *
  4645. + * Revision 1.26 2004/08/17 03:25:43 mcr
  4646. + * freeswan->openswan.
  4647. + *
  4648. + * Revision 1.25 2004/07/10 19:08:41 mcr
  4649. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  4650. + *
  4651. + * Revision 1.24 2004/04/05 19:55:06 mcr
  4652. + * Moved from linux/include/freeswan/ipsec_param.h,v
  4653. + *
  4654. + * Revision 1.23 2003/12/13 19:10:16 mcr
  4655. + * refactored rcv and xmit code - same as FS 2.05.
  4656. + *
  4657. + * Revision 1.22 2003/10/31 02:27:05 mcr
  4658. + * pulled up port-selector patches and sa_id elimination.
  4659. + *
  4660. + * Revision 1.21.4.1 2003/10/29 01:10:19 mcr
  4661. + * elimited "struct sa_id"
  4662. + *
  4663. + * Revision 1.21 2003/04/03 17:38:18 rgb
  4664. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  4665. + * Change indentation for readability.
  4666. + *
  4667. + * Revision 1.20 2003/03/14 08:09:26 rgb
  4668. + * Fixed up CONFIG_IPSEC_DYNDEV definitions.
  4669. + *
  4670. + * Revision 1.19 2003/01/30 02:31:43 rgb
  4671. + *
  4672. + * Rename SAref table macro names for clarity.
  4673. + *
  4674. + * Revision 1.18 2002/09/30 19:06:26 rgb
  4675. + * Reduce default table to 16 bits width.
  4676. + *
  4677. + * Revision 1.17 2002/09/20 15:40:29 rgb
  4678. + * Define switch to activate new SAref code.
  4679. + * Prefix macros with "IPSEC_".
  4680. + * Rework saref freelist.
  4681. + * Restrict some bits to kernel context for use to klips utils.
  4682. + *
  4683. + * Revision 1.16 2002/09/20 05:00:31 rgb
  4684. + * Define switch to divulge hmac keys for debugging.
  4685. + * Added IPOPTIONS switch.
  4686. + *
  4687. + * Revision 1.15 2002/09/19 02:34:24 mcr
  4688. + * define IPSEC_PROC_SUBDIRS if we are 2.4, and use that in ipsec_proc.c
  4689. + * to decide if we are to create /proc/net/ipsec/.
  4690. + *
  4691. + * Revision 1.14 2002/08/30 01:20:54 mcr
  4692. + * reorganized 2.0/2.2/2.4 procfs support macro so match
  4693. + * 2.4 values/typedefs.
  4694. + *
  4695. + * Revision 1.13 2002/07/28 22:03:28 mcr
  4696. + * added some documentation to SA_REF_*
  4697. + * turned on fix for ESPIV attack, now that we have the attack code.
  4698. + *
  4699. + * Revision 1.12 2002/07/26 08:48:31 rgb
  4700. + * Added SA ref table code.
  4701. + *
  4702. + * Revision 1.11 2002/07/23 02:57:45 rgb
  4703. + * Define ARPHRD_VOID for < 2.4 kernels.
  4704. + *
  4705. + * Revision 1.10 2002/05/27 21:37:28 rgb
  4706. + * Set the defaults sanely for those adventurous enough to try more than 1
  4707. + * digit of ipsec devices.
  4708. + *
  4709. + * Revision 1.9 2002/05/27 18:56:07 rgb
  4710. + * Convert to dynamic ipsec device allocation.
  4711. + *
  4712. + * Revision 1.8 2002/04/24 07:36:47 mcr
  4713. + * Moved from ./klips/net/ipsec/ipsec_param.h,v
  4714. + *
  4715. + * Revision 1.7 2002/04/20 00:12:25 rgb
  4716. + * Added esp IV CBC attack fix, disabled.
  4717. + *
  4718. + * Revision 1.6 2002/01/29 02:11:42 mcr
  4719. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  4720. + * updating of IPv6 structures to match latest in6.h version.
  4721. + * removed dead code from freeswan.h that also duplicated kversions.h
  4722. + * code.
  4723. + *
  4724. + * Revision 1.5 2002/01/28 19:22:01 mcr
  4725. + * by default, turn off LINEARIZE option
  4726. + * (let kversions.h turn it on)
  4727. + *
  4728. + * Revision 1.4 2002/01/20 20:19:36 mcr
  4729. + * renamed option to IP_FRAGMENT_LINEARIZE.
  4730. + *
  4731. + * Revision 1.3 2002/01/12 02:57:25 mcr
  4732. + * first regression test causes acquire messages to be lost
  4733. + * 100% of the time. This is to help testing of pluto.
  4734. + *
  4735. + * Revision 1.2 2001/11/26 09:16:14 rgb
  4736. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  4737. + *
  4738. + * Revision 1.1.2.3 2001/10/23 04:40:16 mcr
  4739. + * added #define for DIVULGING session keys in debug output.
  4740. + *
  4741. + * Revision 1.1.2.2 2001/10/22 20:53:25 mcr
  4742. + * added a define to control forcing of DES parity.
  4743. + *
  4744. + * Revision 1.1.2.1 2001/09/25 02:20:19 mcr
  4745. + * many common kernel configuration questions centralized.
  4746. + * more things remain that should be moved from freeswan.h.
  4747. + *
  4748. + *
  4749. + * Local variables:
  4750. + * c-file-style: "linux"
  4751. + * End:
  4752. + *
  4753. + */
  4754. --- /dev/null Tue Mar 11 13:02:56 2003
  4755. +++ linux/include/openswan/ipsec_policy.h Mon Feb 9 13:51:03 2004
  4756. @@ -0,0 +1,217 @@
  4757. +#ifndef _IPSEC_POLICY_H
  4758. +/*
  4759. + * policy interface file between pluto and applications
  4760. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  4761. + *
  4762. + * This library is free software; you can redistribute it and/or modify it
  4763. + * under the terms of the GNU Library General Public License as published by
  4764. + * the Free Software Foundation; either version 2 of the License, or (at your
  4765. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  4766. + *
  4767. + * This library is distributed in the hope that it will be useful, but
  4768. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4769. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  4770. + * License for more details.
  4771. + *
  4772. + * RCSID $Id: ipsec_policy.h,v 1.7.6.1 2005/07/26 01:53:07 ken Exp $
  4773. + */
  4774. +#define _IPSEC_POLICY_H /* seen it, no need to see it again */
  4775. +
  4776. +
  4777. +/*
  4778. + * this file defines an interface between an application (or rather an
  4779. + * application library) and a key/policy daemon. It provides for inquiries
  4780. + * as to the current state of a connected socket, as well as for general
  4781. + * questions.
  4782. + *
  4783. + * In general, the interface is defined as a series of functional interfaces,
  4784. + * and the policy messages should be internal. However, because this is in
  4785. + * fact an ABI between pieces of the system that may get compiled and revised
  4786. + * seperately, this ABI must be public and revision controlled.
  4787. + *
  4788. + * It is expected that the daemon will always support previous versions.
  4789. + */
  4790. +
  4791. +#define IPSEC_POLICY_MSG_REVISION (unsigned)200305061
  4792. +
  4793. +enum ipsec_policy_command {
  4794. + IPSEC_CMD_QUERY_FD = 1,
  4795. + IPSEC_CMD_QUERY_HOSTPAIR = 2,
  4796. + IPSEC_CMD_QUERY_DSTONLY = 3,
  4797. +};
  4798. +
  4799. +struct ipsec_policy_msg_head {
  4800. + u_int32_t ipm_version;
  4801. + u_int32_t ipm_msg_len;
  4802. + u_int32_t ipm_msg_type;
  4803. + u_int32_t ipm_msg_seq;
  4804. +};
  4805. +
  4806. +enum ipsec_privacy_quality {
  4807. + IPSEC_PRIVACY_NONE = 0,
  4808. + IPSEC_PRIVACY_INTEGRAL = 4, /* not private at all. AH-like */
  4809. + IPSEC_PRIVACY_UNKNOWN = 8, /* something is claimed, but details unavail */
  4810. + IPSEC_PRIVACY_ROT13 = 12, /* trivially breakable, i.e. 1DES */
  4811. + IPSEC_PRIVACY_GAK = 16, /* known eavesdroppers */
  4812. + IPSEC_PRIVACY_PRIVATE = 32, /* secure for at least a decade */
  4813. + IPSEC_PRIVACY_STRONG = 64, /* ridiculously secure */
  4814. + IPSEC_PRIVACY_TORTOISE = 192, /* even stronger, but very slow */
  4815. + IPSEC_PRIVACY_OTP = 224, /* some kind of *true* one time pad */
  4816. +};
  4817. +
  4818. +enum ipsec_bandwidth_quality {
  4819. + IPSEC_QOS_UNKNOWN = 0, /* unknown bandwidth */
  4820. + IPSEC_QOS_INTERACTIVE = 16, /* reasonably moderate jitter, moderate fast.
  4821. + Good enough for telnet/ssh. */
  4822. + IPSEC_QOS_VOIP = 32, /* faster crypto, predicable jitter */
  4823. + IPSEC_QOS_FTP = 64, /* higher throughput crypto, perhaps hardware
  4824. + offloaded, but latency/jitter may be bad */
  4825. + IPSEC_QOS_WIRESPEED = 128, /* expect to be able to fill your pipe */
  4826. +};
  4827. +
  4828. +/* moved from programs/pluto/constants.h */
  4829. +/* IPsec AH transform values
  4830. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.3
  4831. + * and in http://www.iana.org/assignments/isakmp-registry
  4832. + */
  4833. +enum ipsec_authentication_algo {
  4834. + AH_MD5=2,
  4835. + AH_SHA=3,
  4836. + AH_DES=4,
  4837. + AH_SHA2_256=5,
  4838. + AH_SHA2_384=6,
  4839. + AH_SHA2_512=7
  4840. +};
  4841. +
  4842. +/* IPsec ESP transform values
  4843. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.4
  4844. + * and from http://www.iana.org/assignments/isakmp-registry
  4845. + */
  4846. +
  4847. +enum ipsec_cipher_algo {
  4848. + ESP_reserved=0,
  4849. + ESP_DES_IV64=1,
  4850. + ESP_DES=2,
  4851. + ESP_3DES=3,
  4852. + ESP_RC5=4,
  4853. + ESP_IDEA=5,
  4854. + ESP_CAST=6,
  4855. + ESP_BLOWFISH=7,
  4856. + ESP_3IDEA=8,
  4857. + ESP_DES_IV32=9,
  4858. + ESP_RC4=10,
  4859. + ESP_NULL=11,
  4860. + ESP_AES=12, /* 128 bit AES */
  4861. +};
  4862. +
  4863. +/* IPCOMP transform values
  4864. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.5
  4865. + */
  4866. +
  4867. +enum ipsec_comp_algo {
  4868. + IPCOMP_OUI= 1,
  4869. + IPCOMP_DEFLATE= 2,
  4870. + IPCOMP_LZS= 3,
  4871. + IPCOMP_V42BIS= 4
  4872. +};
  4873. +
  4874. +/* Identification type values
  4875. + * RFC 2407 The Internet IP security Domain of Interpretation for ISAKMP 4.6.2.1
  4876. + */
  4877. +
  4878. +enum ipsec_id_type {
  4879. + ID_IMPOSSIBLE= (-2), /* private to Pluto */
  4880. + ID_MYID= (-1), /* private to Pluto */
  4881. + ID_NONE= 0, /* private to Pluto */
  4882. + ID_IPV4_ADDR= 1,
  4883. + ID_FQDN= 2,
  4884. + ID_USER_FQDN= 3,
  4885. + ID_IPV4_ADDR_SUBNET= 4,
  4886. + ID_IPV6_ADDR= 5,
  4887. + ID_IPV6_ADDR_SUBNET= 6,
  4888. + ID_IPV4_ADDR_RANGE= 7,
  4889. + ID_IPV6_ADDR_RANGE= 8,
  4890. + ID_DER_ASN1_DN= 9,
  4891. + ID_DER_ASN1_GN= 10,
  4892. + ID_KEY_ID= 11
  4893. +};
  4894. +
  4895. +/* Certificate type values
  4896. + * RFC 2408 ISAKMP, chapter 3.9
  4897. + */
  4898. +enum ipsec_cert_type {
  4899. + CERT_NONE= 0, /* none, or guess from file contents */
  4900. + CERT_PKCS7_WRAPPED_X509= 1, /* self-signed certificate from disk */
  4901. + CERT_PGP= 2,
  4902. + CERT_DNS_SIGNED_KEY= 3, /* KEY RR from DNS */
  4903. + CERT_X509_SIGNATURE= 4,
  4904. + CERT_X509_KEY_EXCHANGE= 5,
  4905. + CERT_KERBEROS_TOKENS= 6,
  4906. + CERT_CRL= 7,
  4907. + CERT_ARL= 8,
  4908. + CERT_SPKI= 9,
  4909. + CERT_X509_ATTRIBUTE= 10,
  4910. + CERT_RAW_RSA= 11, /* raw RSA from config file */
  4911. +};
  4912. +
  4913. +/* a SIG record in ASCII */
  4914. +struct ipsec_dns_sig {
  4915. + char fqdn[256];
  4916. + char dns_sig[768]; /* empty string if not signed */
  4917. +};
  4918. +
  4919. +struct ipsec_raw_key {
  4920. + char id_name[256];
  4921. + char fs_keyid[8];
  4922. +};
  4923. +
  4924. +struct ipsec_identity {
  4925. + enum ipsec_id_type ii_type;
  4926. + enum ipsec_cert_type ii_format;
  4927. + union {
  4928. + struct ipsec_dns_sig ipsec_dns_signed;
  4929. + /* some thing for PGP */
  4930. + /* some thing for PKIX */
  4931. + struct ipsec_raw_key ipsec_raw_key;
  4932. + } ii_credential;
  4933. +};
  4934. +
  4935. +#define IPSEC_MAX_CREDENTIALS 32
  4936. +
  4937. +struct ipsec_policy_cmd_query {
  4938. + struct ipsec_policy_msg_head head;
  4939. +
  4940. + /* Query section */
  4941. + ip_address query_local; /* us */
  4942. + ip_address query_remote; /* them */
  4943. + u_int8_t proto; /* TCP, ICMP, etc. */
  4944. + u_short src_port, dst_port;
  4945. +
  4946. + /* Answer section */
  4947. + enum ipsec_privacy_quality strength;
  4948. + enum ipsec_bandwidth_quality bandwidth;
  4949. + enum ipsec_authentication_algo auth_detail;
  4950. + enum ipsec_cipher_algo esp_detail;
  4951. + enum ipsec_comp_algo comp_detail;
  4952. +
  4953. + int credential_count;
  4954. +
  4955. + struct ipsec_identity credentials[IPSEC_MAX_CREDENTIALS];
  4956. +};
  4957. +
  4958. +#define IPSEC_POLICY_SOCKET "/var/run/pluto/pluto.info"
  4959. +
  4960. +/* prototypes */
  4961. +extern err_t ipsec_policy_lookup(int fd, struct ipsec_policy_cmd_query *result);
  4962. +extern err_t ipsec_policy_init(void);
  4963. +extern err_t ipsec_policy_final(void);
  4964. +extern err_t ipsec_policy_readmsg(int policysock,
  4965. + unsigned char *buf, size_t buflen);
  4966. +extern err_t ipsec_policy_sendrecv(unsigned char *buf, size_t buflen);
  4967. +extern err_t ipsec_policy_cgilookup(struct ipsec_policy_cmd_query *result);
  4968. +
  4969. +
  4970. +extern const char *ipsec_policy_version_code(void);
  4971. +extern const char *ipsec_policy_version_string(void);
  4972. +
  4973. +#endif /* _IPSEC_POLICY_H */
  4974. --- /dev/null Tue Mar 11 13:02:56 2003
  4975. +++ linux/include/openswan/ipsec_proto.h Mon Feb 9 13:51:03 2004
  4976. @@ -0,0 +1,199 @@
  4977. +/*
  4978. + * @(#) prototypes for FreeSWAN functions
  4979. + *
  4980. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4981. + * and Michael Richardson <mcr@freeswan.org>
  4982. + *
  4983. + * This program is free software; you can redistribute it and/or modify it
  4984. + * under the terms of the GNU General Public License as published by the
  4985. + * Free Software Foundation; either version 2 of the License, or (at your
  4986. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4987. + *
  4988. + * This program is distributed in the hope that it will be useful, but
  4989. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4990. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4991. + * for more details.
  4992. + *
  4993. + * RCSID $Id: ipsec_proto.h,v 1.14 2005/04/29 04:50:03 mcr Exp $
  4994. + *
  4995. + */
  4996. +
  4997. +#ifndef _IPSEC_PROTO_H_
  4998. +
  4999. +#include "ipsec_param.h"
  5000. +
  5001. +/*
  5002. + * This file is a kernel only file that declares prototypes for
  5003. + * all intra-module function calls and global data structures.
  5004. + *
  5005. + * Include this file last.
  5006. + *
  5007. + */
  5008. +
  5009. +/* forward references */
  5010. +enum ipsec_direction;
  5011. +enum ipsec_life_type;
  5012. +struct ipsec_lifetime64;
  5013. +struct ident;
  5014. +struct sockaddr_encap;
  5015. +struct ipsec_sa;
  5016. +
  5017. +/* ipsec_init.c */
  5018. +extern struct prng ipsec_prng;
  5019. +
  5020. +/* ipsec_sa.c */
  5021. +extern struct ipsec_sa *ipsec_sadb_hash[SADB_HASHMOD];
  5022. +extern spinlock_t tdb_lock;
  5023. +extern int ipsec_sadb_init(void);
  5024. +extern int ipsec_sadb_cleanup(__u8);
  5025. +
  5026. +extern struct ipsec_sa *ipsec_sa_alloc(int*error);
  5027. +
  5028. +
  5029. +extern struct ipsec_sa *ipsec_sa_getbyid(ip_said *);
  5030. +extern int ipsec_sa_put(struct ipsec_sa *);
  5031. +extern /* void */ int ipsec_sa_del(struct ipsec_sa *);
  5032. +extern /* void */ int ipsec_sa_delchain(struct ipsec_sa *);
  5033. +extern /* void */ int ipsec_sa_add(struct ipsec_sa *);
  5034. +
  5035. +extern int ipsec_sa_init(struct ipsec_sa *ipsp);
  5036. +extern int ipsec_sa_wipe(struct ipsec_sa *ipsp);
  5037. +
  5038. +/* debug declarations */
  5039. +
  5040. +/* ipsec_proc.c */
  5041. +extern int ipsec_proc_init(void);
  5042. +extern void ipsec_proc_cleanup(void);
  5043. +
  5044. +/* ipsec_rcv.c */
  5045. +extern int ipsec_rcv(struct sk_buff *skb);
  5046. +extern int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type);
  5047. +
  5048. +/* ipsec_xmit.c */
  5049. +struct ipsec_xmit_state;
  5050. +extern enum ipsec_xmit_value ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs);
  5051. +extern enum ipsec_xmit_value ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs);
  5052. +extern void ipsec_print_ip(struct iphdr *ip);
  5053. +
  5054. +
  5055. +
  5056. +/* ipsec_radij.c */
  5057. +extern int ipsec_makeroute(struct sockaddr_encap *ea,
  5058. + struct sockaddr_encap *em,
  5059. + ip_said said,
  5060. + uint32_t pid,
  5061. + struct sk_buff *skb,
  5062. + struct ident *ident_s,
  5063. + struct ident *ident_d);
  5064. +
  5065. +extern int ipsec_breakroute(struct sockaddr_encap *ea,
  5066. + struct sockaddr_encap *em,
  5067. + struct sk_buff **first,
  5068. + struct sk_buff **last);
  5069. +
  5070. +int ipsec_radijinit(void);
  5071. +int ipsec_cleareroutes(void);
  5072. +int ipsec_radijcleanup(void);
  5073. +
  5074. +/* ipsec_life.c */
  5075. +extern enum ipsec_life_alive ipsec_lifetime_check(struct ipsec_lifetime64 *il64,
  5076. + const char *lifename,
  5077. + const char *saname,
  5078. + enum ipsec_life_type ilt,
  5079. + enum ipsec_direction idir,
  5080. + struct ipsec_sa *ips);
  5081. +
  5082. +
  5083. +extern int ipsec_lifetime_format(char *buffer,
  5084. + int buflen,
  5085. + char *lifename,
  5086. + enum ipsec_life_type timebaselife,
  5087. + struct ipsec_lifetime64 *lifetime);
  5088. +
  5089. +extern void ipsec_lifetime_update_hard(struct ipsec_lifetime64 *lifetime,
  5090. + __u64 newvalue);
  5091. +
  5092. +extern void ipsec_lifetime_update_soft(struct ipsec_lifetime64 *lifetime,
  5093. + __u64 newvalue);
  5094. +
  5095. +/* ipsec_snprintf.c */
  5096. +extern int ipsec_snprintf(char * buf, ssize_t size, const char *fmt, ...);
  5097. +extern void ipsec_dmp_block(char *s, caddr_t bb, int len);
  5098. +
  5099. +
  5100. +/* ipsec_alg.c */
  5101. +extern int ipsec_alg_init(void);
  5102. +
  5103. +
  5104. +#ifdef CONFIG_KLIPS_DEBUG
  5105. +
  5106. +extern int debug_xform;
  5107. +extern int debug_eroute;
  5108. +extern int debug_spi;
  5109. +extern int debug_netlink;
  5110. +
  5111. +#endif /* CONFIG_KLIPS_DEBUG */
  5112. +
  5113. +
  5114. +
  5115. +
  5116. +#define _IPSEC_PROTO_H
  5117. +#endif /* _IPSEC_PROTO_H_ */
  5118. +
  5119. +/*
  5120. + * $Log: ipsec_proto.h,v $
  5121. + * Revision 1.14 2005/04/29 04:50:03 mcr
  5122. + * prototypes for xmit and alg code.
  5123. + *
  5124. + * Revision 1.13 2005/04/17 03:46:07 mcr
  5125. + * added prototypes for ipsec_rcv() routines.
  5126. + *
  5127. + * Revision 1.12 2005/04/14 20:28:37 mcr
  5128. + * added additional prototypes.
  5129. + *
  5130. + * Revision 1.11 2005/04/14 01:16:28 mcr
  5131. + * add prototypes for snprintf.
  5132. + *
  5133. + * Revision 1.10 2005/04/13 22:47:28 mcr
  5134. + * make sure that forward references are available.
  5135. + *
  5136. + * Revision 1.9 2004/07/10 19:08:41 mcr
  5137. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5138. + *
  5139. + * Revision 1.8 2004/04/05 19:55:06 mcr
  5140. + * Moved from linux/include/freeswan/ipsec_proto.h,v
  5141. + *
  5142. + * Revision 1.7 2003/10/31 02:27:05 mcr
  5143. + * pulled up port-selector patches and sa_id elimination.
  5144. + *
  5145. + * Revision 1.6.30.1 2003/10/29 01:10:19 mcr
  5146. + * elimited "struct sa_id"
  5147. + *
  5148. + * Revision 1.6 2002/05/23 07:13:48 rgb
  5149. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  5150. + *
  5151. + * Revision 1.5 2002/05/14 02:36:40 rgb
  5152. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  5153. + * with "put" usage in the kernel.
  5154. + *
  5155. + * Revision 1.4 2002/04/24 07:36:47 mcr
  5156. + * Moved from ./klips/net/ipsec/ipsec_proto.h,v
  5157. + *
  5158. + * Revision 1.3 2002/04/20 00:12:25 rgb
  5159. + * Added esp IV CBC attack fix, disabled.
  5160. + *
  5161. + * Revision 1.2 2001/11/26 09:16:15 rgb
  5162. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5163. + *
  5164. + * Revision 1.1.2.1 2001/09/25 02:21:01 mcr
  5165. + * ipsec_proto.h created to keep prototypes rather than deal with
  5166. + * cyclic dependancies of structures and prototypes in .h files.
  5167. + *
  5168. + *
  5169. + *
  5170. + * Local variables:
  5171. + * c-file-style: "linux"
  5172. + * End:
  5173. + *
  5174. + */
  5175. +
  5176. --- /dev/null Tue Mar 11 13:02:56 2003
  5177. +++ linux/include/openswan/ipsec_radij.h Mon Feb 9 13:51:03 2004
  5178. @@ -0,0 +1,179 @@
  5179. +/*
  5180. + * @(#) Definitions relevant to the IPSEC <> radij tree interfacing
  5181. + * Copyright (C) 1996, 1997 John Ioannidis.
  5182. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  5183. + *
  5184. + * This program is free software; you can redistribute it and/or modify it
  5185. + * under the terms of the GNU General Public License as published by the
  5186. + * Free Software Foundation; either version 2 of the License, or (at your
  5187. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5188. + *
  5189. + * This program is distributed in the hope that it will be useful, but
  5190. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5191. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5192. + * for more details.
  5193. + *
  5194. + * RCSID $Id: ipsec_radij.h,v 1.22 2004/07/10 19:08:41 mcr Exp $
  5195. + */
  5196. +
  5197. +#ifndef _IPSEC_RADIJ_H
  5198. +
  5199. +#include <openswan.h>
  5200. +
  5201. +int ipsec_walk(char *);
  5202. +
  5203. +int ipsec_rj_walker_procprint(struct radij_node *, void *);
  5204. +int ipsec_rj_walker_delete(struct radij_node *, void *);
  5205. +
  5206. +/* This structure is used to pass information between
  5207. + * ipsec_eroute_get_info and ipsec_rj_walker_procprint
  5208. + * (through rj_walktree) and between calls of ipsec_rj_walker_procprint.
  5209. + */
  5210. +struct wsbuf
  5211. +{
  5212. + /* from caller of ipsec_eroute_get_info: */
  5213. + char *const buffer; /* start of buffer provided */
  5214. + const int length; /* length of buffer provided */
  5215. + const off_t offset; /* file position of first character of interest */
  5216. + /* accumulated by ipsec_rj_walker_procprint: */
  5217. + int len; /* number of character filled into buffer */
  5218. + off_t begin; /* file position contained in buffer[0] (<=offset) */
  5219. +};
  5220. +
  5221. +extern struct radij_node_head *rnh;
  5222. +extern spinlock_t eroute_lock;
  5223. +
  5224. +struct eroute * ipsec_findroute(struct sockaddr_encap *);
  5225. +
  5226. +#define O1(x) (int)(((x)>>24)&0xff)
  5227. +#define O2(x) (int)(((x)>>16)&0xff)
  5228. +#define O3(x) (int)(((x)>>8)&0xff)
  5229. +#define O4(x) (int)(((x))&0xff)
  5230. +
  5231. +#ifdef CONFIG_KLIPS_DEBUG
  5232. +extern int debug_radij;
  5233. +void rj_dumptrees(void);
  5234. +
  5235. +#define DB_RJ_DUMPTREES 0x0001
  5236. +#define DB_RJ_FINDROUTE 0x0002
  5237. +#endif /* CONFIG_KLIPS_DEBUG */
  5238. +
  5239. +#define _IPSEC_RADIJ_H
  5240. +#endif
  5241. +
  5242. +/*
  5243. + * $Log: ipsec_radij.h,v $
  5244. + * Revision 1.22 2004/07/10 19:08:41 mcr
  5245. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5246. + *
  5247. + * Revision 1.21 2004/04/29 11:06:42 ken
  5248. + * Last bits from 2.06 procfs updates
  5249. + *
  5250. + * Revision 1.20 2004/04/06 02:49:08 mcr
  5251. + * pullup of algo code from alg-branch.
  5252. + *
  5253. + * Revision 1.19 2004/04/05 19:55:06 mcr
  5254. + * Moved from linux/include/freeswan/ipsec_radij.h,v
  5255. + *
  5256. + * Revision 1.18 2002/04/24 07:36:47 mcr
  5257. + * Moved from ./klips/net/ipsec/ipsec_radij.h,v
  5258. + *
  5259. + * Revision 1.17 2001/11/26 09:23:49 rgb
  5260. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5261. + *
  5262. + * Revision 1.16.2.1 2001/09/25 02:21:17 mcr
  5263. + * ipsec_proto.h created to keep prototypes rather than deal with
  5264. + * cyclic dependancies of structures and prototypes in .h files.
  5265. + *
  5266. + * Revision 1.16 2001/09/15 16:24:04 rgb
  5267. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  5268. + *
  5269. + * Revision 1.15 2001/09/14 16:58:37 rgb
  5270. + * Added support for storing the first and last packets through a HOLD.
  5271. + *
  5272. + * Revision 1.14 2001/09/08 21:13:32 rgb
  5273. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  5274. + *
  5275. + * Revision 1.13 2001/06/14 19:35:09 rgb
  5276. + * Update copyright date.
  5277. + *
  5278. + * Revision 1.12 2001/05/27 06:12:11 rgb
  5279. + * Added structures for pid, packet count and last access time to eroute.
  5280. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  5281. + *
  5282. + * Revision 1.11 2000/09/08 19:12:56 rgb
  5283. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  5284. + *
  5285. + * Revision 1.10 1999/11/17 15:53:39 rgb
  5286. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  5287. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  5288. + * klips/net/ipsec/Makefile.
  5289. + *
  5290. + * Revision 1.9 1999/10/01 00:01:23 rgb
  5291. + * Added eroute structure locking.
  5292. + *
  5293. + * Revision 1.8 1999/04/11 00:28:59 henry
  5294. + * GPL boilerplate
  5295. + *
  5296. + * Revision 1.7 1999/04/06 04:54:26 rgb
  5297. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  5298. + * patch shell fixes.
  5299. + *
  5300. + * Revision 1.6 1999/01/22 06:23:26 rgb
  5301. + * Cruft clean-out.
  5302. + *
  5303. + * Revision 1.5 1998/10/25 02:42:08 rgb
  5304. + * Change return type on ipsec_breakroute and ipsec_makeroute and add an
  5305. + * argument to be able to transmit more infomation about errors.
  5306. + *
  5307. + * Revision 1.4 1998/10/19 14:44:29 rgb
  5308. + * Added inclusion of freeswan.h.
  5309. + * sa_id structure implemented and used: now includes protocol.
  5310. + *
  5311. + * Revision 1.3 1998/07/28 00:03:31 rgb
  5312. + * Comment out temporary inet_nto4u() kluge.
  5313. + *
  5314. + * Revision 1.2 1998/07/14 18:22:00 rgb
  5315. + * Add function to clear the eroute table.
  5316. + *
  5317. + * Revision 1.1 1998/06/18 21:27:49 henry
  5318. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  5319. + * kernel-build scripts happier in the presence of symlinks
  5320. + *
  5321. + * Revision 1.5 1998/05/25 20:30:38 rgb
  5322. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  5323. + *
  5324. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  5325. + * add ipsec_rj_walker_delete.
  5326. + *
  5327. + * Revision 1.4 1998/05/21 13:02:56 rgb
  5328. + * Imported definitions from ipsec_radij.c and radij.c to support /proc 3k
  5329. + * limit fix.
  5330. + *
  5331. + * Revision 1.3 1998/04/21 21:29:09 rgb
  5332. + * Rearrange debug switches to change on the fly debug output from user
  5333. + * space. Only kernel changes checked in at this time. radij.c was also
  5334. + * changed to temporarily remove buggy debugging code in rj_delete causing
  5335. + * an OOPS and hence, netlink device open errors.
  5336. + *
  5337. + * Revision 1.2 1998/04/14 17:30:39 rgb
  5338. + * Fix up compiling errors for radij tree memory reclamation.
  5339. + *
  5340. + * Revision 1.1 1998/04/09 03:06:10 henry
  5341. + * sources moved up from linux/net/ipsec
  5342. + *
  5343. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  5344. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  5345. + *
  5346. + * Revision 0.4 1997/01/15 01:28:15 ji
  5347. + * No changes.
  5348. + *
  5349. + * Revision 0.3 1996/11/20 14:39:04 ji
  5350. + * Minor cleanups.
  5351. + * Rationalized debugging code.
  5352. + *
  5353. + * Revision 0.2 1996/11/02 00:18:33 ji
  5354. + * First limited release.
  5355. + *
  5356. + *
  5357. + */
  5358. --- /dev/null Tue Mar 11 13:02:56 2003
  5359. +++ linux/include/openswan/ipsec_rcv.h Mon Feb 9 13:51:03 2004
  5360. @@ -0,0 +1,199 @@
  5361. +/*
  5362. + *
  5363. + * Copyright (C) 1996, 1997 John Ioannidis.
  5364. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  5365. + *
  5366. + * This program is free software; you can redistribute it and/or modify it
  5367. + * under the terms of the GNU General Public License as published by the
  5368. + * Free Software Foundation; either version 2 of the License, or (at your
  5369. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5370. + *
  5371. + * This program is distributed in the hope that it will be useful, but
  5372. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5373. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5374. + * for more details.
  5375. + *
  5376. + * RCSID $Id: ipsec_rcv.h,v 1.28.2.2 2006/10/06 21:39:26 paul Exp $
  5377. + */
  5378. +
  5379. +#ifndef IPSEC_RCV_H
  5380. +#define IPSEC_RCV_H
  5381. +
  5382. +#include "openswan/ipsec_auth.h"
  5383. +
  5384. +#define DB_RX_PKTRX 0x0001
  5385. +#define DB_RX_PKTRX2 0x0002
  5386. +#define DB_RX_DMP 0x0004
  5387. +#define DB_RX_IPSA 0x0010
  5388. +#define DB_RX_XF 0x0020
  5389. +#define DB_RX_IPAD 0x0040
  5390. +#define DB_RX_INAU 0x0080
  5391. +#define DB_RX_OINFO 0x0100
  5392. +#define DB_RX_OINFO2 0x0200
  5393. +#define DB_RX_OH 0x0400
  5394. +#define DB_RX_REPLAY 0x0800
  5395. +
  5396. +#ifdef __KERNEL__
  5397. +/* struct options; */
  5398. +
  5399. +#define __NO_VERSION__
  5400. +#ifndef AUTOCONF_INCLUDED
  5401. +#include <linux/config.h> /* for CONFIG_IP_FORWARD */
  5402. +#endif
  5403. +#ifdef CONFIG_MODULES
  5404. +#include <linux/module.h>
  5405. +#endif
  5406. +#include <linux/version.h>
  5407. +#include <openswan.h>
  5408. +
  5409. +#define IPSEC_BIRTH_TEMPLATE_MAXLEN 256
  5410. +
  5411. +struct ipsec_birth_reply {
  5412. + int packet_template_len;
  5413. + unsigned char packet_template[IPSEC_BIRTH_TEMPLATE_MAXLEN];
  5414. +};
  5415. +
  5416. +extern struct ipsec_birth_reply ipsec_ipv4_birth_packet;
  5417. +extern struct ipsec_birth_reply ipsec_ipv6_birth_packet;
  5418. +
  5419. +enum ipsec_rcv_value {
  5420. + IPSEC_RCV_LASTPROTO=1,
  5421. + IPSEC_RCV_OK=0,
  5422. + IPSEC_RCV_BADPROTO=-1,
  5423. + IPSEC_RCV_BADLEN=-2,
  5424. + IPSEC_RCV_ESP_BADALG=-3,
  5425. + IPSEC_RCV_3DES_BADBLOCKING=-4,
  5426. + IPSEC_RCV_ESP_DECAPFAIL=-5,
  5427. + IPSEC_RCV_DECAPFAIL=-6,
  5428. + IPSEC_RCV_SAIDNOTFOUND=-7,
  5429. + IPSEC_RCV_IPCOMPALONE=-8,
  5430. + IPSEC_RCV_IPCOMPFAILED=-10,
  5431. + IPSEC_RCV_SAIDNOTLIVE=-11,
  5432. + IPSEC_RCV_FAILEDINBOUND=-12,
  5433. + IPSEC_RCV_LIFETIMEFAILED=-13,
  5434. + IPSEC_RCV_BADAUTH=-14,
  5435. + IPSEC_RCV_REPLAYFAILED=-15,
  5436. + IPSEC_RCV_AUTHFAILED=-16,
  5437. + IPSEC_RCV_REPLAYROLLED=-17,
  5438. + IPSEC_RCV_BAD_DECRYPT=-18
  5439. +};
  5440. +
  5441. +struct ipsec_rcv_state {
  5442. + struct sk_buff *skb;
  5443. + struct net_device_stats *stats;
  5444. + struct iphdr *ipp; /* the IP header */
  5445. + struct ipsec_sa *ipsp; /* current SA being processed */
  5446. + int len; /* length of packet */
  5447. + int ilen; /* length of inner payload (-authlen) */
  5448. + int authlen; /* how big is the auth data at end */
  5449. + int hard_header_len; /* layer 2 size */
  5450. + int iphlen; /* how big is IP header */
  5451. + struct auth_alg *authfuncs;
  5452. + ip_said said;
  5453. + char sa[SATOT_BUF];
  5454. + size_t sa_len;
  5455. + __u8 next_header;
  5456. + __u8 hash[AH_AMAX];
  5457. + char ipsaddr_txt[ADDRTOA_BUF];
  5458. + char ipdaddr_txt[ADDRTOA_BUF];
  5459. + __u8 *octx;
  5460. + __u8 *ictx;
  5461. + int ictx_len;
  5462. + int octx_len;
  5463. + union {
  5464. + struct {
  5465. + struct esphdr *espp;
  5466. + } espstuff;
  5467. + struct {
  5468. + struct ahhdr *ahp;
  5469. + } ahstuff;
  5470. + struct {
  5471. + struct ipcomphdr *compp;
  5472. + } ipcompstuff;
  5473. + } protostuff;
  5474. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  5475. + __u8 natt_type;
  5476. + __u16 natt_sport;
  5477. + __u16 natt_dport;
  5478. + int natt_len;
  5479. +#endif
  5480. +};
  5481. +
  5482. +extern int
  5483. +#ifdef PROTO_HANDLER_SINGLE_PARM
  5484. +ipsec_rcv(struct sk_buff *skb);
  5485. +#else /* PROTO_HANDLER_SINGLE_PARM */
  5486. +ipsec_rcv(struct sk_buff *skb,
  5487. + unsigned short xlen);
  5488. +#endif /* PROTO_HANDLER_SINGLE_PARM */
  5489. +
  5490. +#ifdef CONFIG_KLIPS_DEBUG
  5491. +extern int debug_rcv;
  5492. +#define ipsec_rcv_dmp(_x,_y, _z) if (debug_rcv && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  5493. +#else
  5494. +#define ipsec_rcv_dmp(_x,_y, _z) do {} while(0)
  5495. +#endif /* CONFIG_KLIPS_DEBUG */
  5496. +
  5497. +extern int sysctl_ipsec_inbound_policy_check;
  5498. +#endif /* __KERNEL__ */
  5499. +
  5500. +extern int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type);
  5501. +
  5502. +
  5503. +#endif /* IPSEC_RCV_H */
  5504. +
  5505. +/*
  5506. + * $Log: ipsec_rcv.h,v $
  5507. + * Revision 1.28.2.2 2006/10/06 21:39:26 paul
  5508. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  5509. + * set. This is defined through autoconf.h which is included through the
  5510. + * linux kernel build macros.
  5511. + *
  5512. + * Revision 1.28.2.1 2006/07/10 15:52:20 paul
  5513. + * Fix for bug #642 by Bart Trojanowski
  5514. + *
  5515. + * Revision 1.28 2005/05/11 00:59:45 mcr
  5516. + * do not call debug routines if !defined KLIPS_DEBUG.
  5517. + *
  5518. + * Revision 1.27 2005/04/29 04:59:46 mcr
  5519. + * use ipsec_dmp_block.
  5520. + *
  5521. + * Revision 1.26 2005/04/13 22:48:35 mcr
  5522. + * added comments, and removed some log.
  5523. + * removed Linux 2.0 support.
  5524. + *
  5525. + * Revision 1.25 2005/04/08 18:25:37 mcr
  5526. + * prototype klips26 encap receive function
  5527. + *
  5528. + * Revision 1.24 2004/08/20 21:45:37 mcr
  5529. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  5530. + * be 26sec compatible. But, some defines where changed.
  5531. + *
  5532. + * Revision 1.23 2004/08/03 18:17:40 mcr
  5533. + * in 2.6, use "net_device" instead of #define device->net_device.
  5534. + * this probably breaks 2.0 compiles.
  5535. + *
  5536. + * Revision 1.22 2004/07/10 19:08:41 mcr
  5537. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5538. + *
  5539. + * Revision 1.21 2004/04/06 02:49:08 mcr
  5540. + * pullup of algo code from alg-branch.
  5541. + *
  5542. + * Revision 1.20 2004/04/05 19:55:06 mcr
  5543. + * Moved from linux/include/freeswan/ipsec_rcv.h,v
  5544. + *
  5545. + * Revision 1.19 2003/12/15 18:13:09 mcr
  5546. + * when compiling with NAT traversal, don't assume that the
  5547. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  5548. + * is set.
  5549. + *
  5550. + * history elided 2005-04-12.
  5551. + *
  5552. + * Local Variables:
  5553. + * c-basic-offset:8
  5554. + * c-style:linux
  5555. + * End:
  5556. + *
  5557. + */
  5558. +
  5559. +
  5560. --- /dev/null Tue Mar 11 13:02:56 2003
  5561. +++ linux/include/openswan/ipsec_sa.h Mon Feb 9 13:51:03 2004
  5562. @@ -0,0 +1,355 @@
  5563. +/*
  5564. + * @(#) Definitions of IPsec Security Association (ipsec_sa)
  5565. + *
  5566. + * Copyright (C) 2001, 2002, 2003
  5567. + * Richard Guy Briggs <rgb@freeswan.org>
  5568. + * and Michael Richardson <mcr@freeswan.org>
  5569. + *
  5570. + * This program is free software; you can redistribute it and/or modify it
  5571. + * under the terms of the GNU General Public License as published by the
  5572. + * Free Software Foundation; either version 2 of the License, or (at your
  5573. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5574. + *
  5575. + * This program is distributed in the hope that it will be useful, but
  5576. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5577. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5578. + * for more details.
  5579. + *
  5580. + * RCSID $Id: ipsec_sa.h,v 1.23 2005/05/11 01:18:59 mcr Exp $
  5581. + *
  5582. + * This file derived from ipsec_xform.h on 2001/9/18 by mcr.
  5583. + *
  5584. + */
  5585. +
  5586. +/*
  5587. + * This file describes the IPsec Security Association Structure.
  5588. + *
  5589. + * This structure keeps track of a single transform that may be done
  5590. + * to a set of packets. It can describe applying the transform or
  5591. + * apply the reverse. (e.g. compression vs expansion). However, it
  5592. + * only describes one at a time. To describe both, two structures would
  5593. + * be used, but since the sides of the transform are performed
  5594. + * on different machines typically it is usual to have only one side
  5595. + * of each association.
  5596. + *
  5597. + */
  5598. +
  5599. +#ifndef _IPSEC_SA_H_
  5600. +
  5601. +#ifdef __KERNEL__
  5602. +#include "openswan/ipsec_stats.h"
  5603. +#include "openswan/ipsec_life.h"
  5604. +#include "openswan/ipsec_eroute.h"
  5605. +#endif /* __KERNEL__ */
  5606. +#include "openswan/ipsec_param.h"
  5607. +
  5608. +#include "pfkeyv2.h"
  5609. +
  5610. +
  5611. +/* SAs are held in a table.
  5612. + * Entries in this table are referenced by IPsecSAref_t values.
  5613. + * IPsecSAref_t values are conceptually subscripts. Because
  5614. + * we want to allocate the table piece-meal, the subscripting
  5615. + * is implemented with two levels, a bit like paged virtual memory.
  5616. + * This representation mechanism is known as an Iliffe Vector.
  5617. + *
  5618. + * The Main table (AKA the refTable) consists of 2^IPSEC_SA_REF_MAINTABLE_IDX_WIDTH
  5619. + * pointers to subtables.
  5620. + * Each subtable has 2^IPSEC_SA_REF_SUBTABLE_IDX_WIDTH entries, each of which
  5621. + * is a pointer to an SA.
  5622. + *
  5623. + * An IPsecSAref_t contains either an exceptional value (signified by the
  5624. + * high-order bit being on) or a reference to a table entry. A table entry
  5625. + * reference has the subtable subscript in the low-order
  5626. + * IPSEC_SA_REF_SUBTABLE_IDX_WIDTH bits and the Main table subscript
  5627. + * in the next lowest IPSEC_SA_REF_MAINTABLE_IDX_WIDTH bits.
  5628. + *
  5629. + * The Maintable entry for an IPsecSAref_t x, a pointer to its subtable, is
  5630. + * IPsecSAref2table(x). It is of type struct IPsecSArefSubTable *.
  5631. + *
  5632. + * The pointer to the SA for x is IPsecSAref2SA(x). It is of type
  5633. + * struct ipsec_sa*. The macro definition clearly shows the two-level
  5634. + * access needed to find the SA pointer.
  5635. + *
  5636. + * The Maintable is allocated when IPsec is initialized.
  5637. + * Each subtable is allocated when needed, but the first is allocated
  5638. + * when IPsec is initialized.
  5639. + *
  5640. + * IPsecSAref_t is designed to be smaller than an NFmark so that
  5641. + * they can be stored in NFmarks and still leave a few bits for other
  5642. + * purposes. The spare bits are in the low order of the NFmark
  5643. + * but in the high order of the IPsecSAref_t, so conversion is required.
  5644. + * We pick the upper bits of NFmark on the theory that they are less likely to
  5645. + * interfere with more pedestrian uses of nfmark.
  5646. + */
  5647. +
  5648. +
  5649. +typedef unsigned short int IPsecRefTableUnusedCount;
  5650. +
  5651. +#define IPSEC_SA_REF_TABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH)
  5652. +
  5653. +#ifdef __KERNEL__
  5654. +#if ((IPSEC_SA_REF_TABLE_IDX_WIDTH - (1 + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)) < 0)
  5655. +#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")"
  5656. +#endif
  5657. +
  5658. +#define IPSEC_SA_REF_SUBTABLE_IDX_WIDTH (IPSEC_SA_REF_TABLE_IDX_WIDTH - IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)
  5659. +
  5660. +#define IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)
  5661. +#define IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5662. +
  5663. +#ifdef CONFIG_NETFILTER
  5664. +#define IPSEC_SA_REF_HOST_FIELD(x) ((struct sk_buff*)(x))->nfmark
  5665. +#define IPSEC_SA_REF_HOST_FIELD_TYPE typeof(IPSEC_SA_REF_HOST_FIELD(NULL))
  5666. +#else /* CONFIG_NETFILTER */
  5667. +/* just make it work for now, it doesn't matter, since there is no nfmark */
  5668. +#define IPSEC_SA_REF_HOST_FIELD_TYPE unsigned long
  5669. +#endif /* CONFIG_NETFILTER */
  5670. +#define IPSEC_SA_REF_HOST_FIELD_WIDTH (8 * sizeof(IPSEC_SA_REF_HOST_FIELD_TYPE))
  5671. +#define IPSEC_SA_REF_FIELD_WIDTH (8 * sizeof(IPsecSAref_t))
  5672. +
  5673. +#define IPSEC_SA_REF_MASK (IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  5674. +#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)
  5675. +#define IPSEC_SA_REF_ENTRY_MASK (IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_SUBTABLE_IDX_WIDTH))
  5676. +
  5677. +#define IPsecSAref2table(x) (((x) & IPSEC_SA_REF_TABLE_MASK) >> IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5678. +#define IPsecSAref2entry(x) ((x) & IPSEC_SA_REF_ENTRY_MASK)
  5679. +#define IPsecSArefBuild(x,y) (((x) << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH) + (y))
  5680. +
  5681. +#define IPsecSAref2SA(x) (ipsec_sadb.refTable[IPsecSAref2table(x)]->entry[IPsecSAref2entry(x)])
  5682. +#define IPsecSA2SAref(x) ((x)->ips_ref)
  5683. +
  5684. +#define EMT_INBOUND 0x01 /* SA direction, 1=inbound */
  5685. +
  5686. +/* 'struct ipsec_sa' should be 64bit aligned when allocated. */
  5687. +struct ipsec_sa
  5688. +{
  5689. + IPsecSAref_t ips_ref; /* reference table entry number */
  5690. + atomic_t ips_refcount; /* reference count for this struct */
  5691. + struct ipsec_sa *ips_hnext; /* next in hash chain */
  5692. + struct ipsec_sa *ips_inext; /* pointer to next xform */
  5693. + struct ipsec_sa *ips_onext; /* pointer to prev xform */
  5694. +
  5695. + struct ifnet *ips_rcvif; /* related rcv encap interface */
  5696. +
  5697. + ip_said ips_said; /* SA ID */
  5698. +
  5699. + __u32 ips_seq; /* seq num of msg that initiated this SA */
  5700. + __u32 ips_pid; /* PID of process that initiated this SA */
  5701. + __u8 ips_authalg; /* auth algorithm for this SA */
  5702. + __u8 ips_encalg; /* enc algorithm for this SA */
  5703. +
  5704. + struct ipsec_stats ips_errs;
  5705. +
  5706. + __u8 ips_replaywin; /* replay window size */
  5707. + enum sadb_sastate ips_state; /* state of SA */
  5708. + __u32 ips_replaywin_lastseq; /* last pkt sequence num */
  5709. + __u64 ips_replaywin_bitmap; /* bitmap of received pkts */
  5710. + __u32 ips_replaywin_maxdiff; /* max pkt sequence difference */
  5711. +
  5712. + __u32 ips_flags; /* generic xform flags */
  5713. +
  5714. +
  5715. + struct ipsec_lifetimes ips_life; /* lifetime records */
  5716. +
  5717. + /* selector information */
  5718. + __u8 ips_transport_protocol; /* protocol for this SA, if ports are involved */
  5719. + struct sockaddr*ips_addr_s; /* src sockaddr */
  5720. + struct sockaddr*ips_addr_d; /* dst sockaddr */
  5721. + struct sockaddr*ips_addr_p; /* proxy sockaddr */
  5722. + __u16 ips_addr_s_size;
  5723. + __u16 ips_addr_d_size;
  5724. + __u16 ips_addr_p_size;
  5725. + ip_address ips_flow_s;
  5726. + ip_address ips_flow_d;
  5727. + ip_address ips_mask_s;
  5728. + ip_address ips_mask_d;
  5729. +
  5730. + __u16 ips_key_bits_a; /* size of authkey in bits */
  5731. + __u16 ips_auth_bits; /* size of authenticator in bits */
  5732. + __u16 ips_key_bits_e; /* size of enckey in bits */
  5733. + __u16 ips_iv_bits; /* size of IV in bits */
  5734. + __u8 ips_iv_size;
  5735. + __u16 ips_key_a_size;
  5736. + __u16 ips_key_e_size;
  5737. +
  5738. + caddr_t ips_key_a; /* authentication key */
  5739. + caddr_t ips_key_e; /* encryption key */
  5740. + caddr_t ips_iv; /* Initialisation Vector */
  5741. +
  5742. + struct ident ips_ident_s; /* identity src */
  5743. + struct ident ips_ident_d; /* identity dst */
  5744. +
  5745. + /* these are included even if CONFIG_KLIPS_IPCOMP is off */
  5746. + __u16 ips_comp_adapt_tries; /* ipcomp self-adaption tries */
  5747. + __u16 ips_comp_adapt_skip; /* ipcomp self-adaption to-skip */
  5748. + __u64 ips_comp_ratio_cbytes; /* compressed bytes */
  5749. + __u64 ips_comp_ratio_dbytes; /* decompressed (or uncompressed) bytes */
  5750. +
  5751. + /* these are included even if CONFIG_IPSEC_NAT_TRAVERSAL is off */
  5752. + __u8 ips_natt_type;
  5753. + __u8 ips_natt_reserved[3];
  5754. + __u16 ips_natt_sport;
  5755. + __u16 ips_natt_dport;
  5756. +
  5757. + struct sockaddr *ips_natt_oa;
  5758. + __u16 ips_natt_oa_size;
  5759. + __u16 ips_natt_reserved2;
  5760. +
  5761. +#if 0
  5762. + __u32 ips_sens_dpd;
  5763. + __u8 ips_sens_sens_level;
  5764. + __u8 ips_sens_sens_len;
  5765. + __u64* ips_sens_sens_bitmap;
  5766. + __u8 ips_sens_integ_level;
  5767. + __u8 ips_sens_integ_len;
  5768. + __u64* ips_sens_integ_bitmap;
  5769. +#endif
  5770. + struct ipsec_alg_enc *ips_alg_enc;
  5771. + struct ipsec_alg_auth *ips_alg_auth;
  5772. + IPsecSAref_t ips_ref_rel;
  5773. +};
  5774. +
  5775. +struct IPsecSArefSubTable
  5776. +{
  5777. + struct ipsec_sa* entry[IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES];
  5778. +};
  5779. +
  5780. +struct ipsec_sadb {
  5781. + struct IPsecSArefSubTable* refTable[IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES];
  5782. + IPsecSAref_t refFreeList[IPSEC_SA_REF_FREELIST_NUM_ENTRIES];
  5783. + int refFreeListHead;
  5784. + int refFreeListTail;
  5785. + IPsecSAref_t refFreeListCont;
  5786. + IPsecSAref_t said_hash[SADB_HASHMOD];
  5787. + spinlock_t sadb_lock;
  5788. +};
  5789. +
  5790. +extern struct ipsec_sadb ipsec_sadb;
  5791. +
  5792. +extern int ipsec_SAref_recycle(void);
  5793. +extern int ipsec_SArefSubTable_alloc(unsigned table);
  5794. +extern int ipsec_saref_freelist_init(void);
  5795. +extern int ipsec_sadb_init(void);
  5796. +extern struct ipsec_sa *ipsec_sa_alloc(int*error); /* pass in error var by pointer */
  5797. +extern IPsecSAref_t ipsec_SAref_alloc(int*erorr); /* pass in error var by pointer */
  5798. +extern int ipsec_sa_free(struct ipsec_sa* ips);
  5799. +extern int ipsec_sa_put(struct ipsec_sa *ips);
  5800. +extern int ipsec_sa_add(struct ipsec_sa *ips);
  5801. +extern int ipsec_sa_del(struct ipsec_sa *ips);
  5802. +extern int ipsec_sa_delchain(struct ipsec_sa *ips);
  5803. +extern int ipsec_sadb_cleanup(__u8 proto);
  5804. +extern int ipsec_sadb_free(void);
  5805. +extern int ipsec_sa_wipe(struct ipsec_sa *ips);
  5806. +#endif /* __KERNEL__ */
  5807. +
  5808. +enum ipsec_direction {
  5809. + ipsec_incoming = 1,
  5810. + ipsec_outgoing = 2
  5811. +};
  5812. +
  5813. +#define _IPSEC_SA_H_
  5814. +#endif /* _IPSEC_SA_H_ */
  5815. +
  5816. +/*
  5817. + * $Log: ipsec_sa.h,v $
  5818. + * Revision 1.23 2005/05/11 01:18:59 mcr
  5819. + * do not change structure based upon options, to avoid
  5820. + * too many #ifdef.
  5821. + *
  5822. + * Revision 1.22 2005/04/14 01:17:09 mcr
  5823. + * change sadb_state to an enum.
  5824. + *
  5825. + * Revision 1.21 2004/08/20 21:45:37 mcr
  5826. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  5827. + * be 26sec compatible. But, some defines where changed.
  5828. + *
  5829. + * Revision 1.20 2004/07/10 19:08:41 mcr
  5830. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5831. + *
  5832. + * Revision 1.19 2004/04/05 19:55:06 mcr
  5833. + * Moved from linux/include/freeswan/ipsec_sa.h,v
  5834. + *
  5835. + * Revision 1.18 2004/04/05 19:41:05 mcr
  5836. + * merged alg-branch code.
  5837. + *
  5838. + * Revision 1.17.2.1 2003/12/22 15:25:52 jjo
  5839. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  5840. + *
  5841. + * Revision 1.17 2003/12/10 01:20:06 mcr
  5842. + * NAT-traversal patches to KLIPS.
  5843. + *
  5844. + * Revision 1.16 2003/10/31 02:27:05 mcr
  5845. + * pulled up port-selector patches and sa_id elimination.
  5846. + *
  5847. + * Revision 1.15.4.1 2003/10/29 01:10:19 mcr
  5848. + * elimited "struct sa_id"
  5849. + *
  5850. + * Revision 1.15 2003/05/11 00:53:09 mcr
  5851. + * IPsecSAref_t and macros were moved to freeswan.h.
  5852. + *
  5853. + * Revision 1.14 2003/02/12 19:31:55 rgb
  5854. + * Fixed bug in "file seen" machinery.
  5855. + * Updated copyright year.
  5856. + *
  5857. + * Revision 1.13 2003/01/30 02:31:52 rgb
  5858. + *
  5859. + * Re-wrote comments describing SAref system for accuracy.
  5860. + * Rename SAref table macro names for clarity.
  5861. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  5862. + * Transmit error code through to caller from callee for better diagnosis of problems.
  5863. + * Enclose all macro arguments in parens to avoid any possible obscrure bugs.
  5864. + *
  5865. + * Revision 1.12 2002/10/07 18:31:19 rgb
  5866. + * Change comment to reflect the flexible nature of the main and sub-table widths.
  5867. + * Added a counter for the number of unused entries in each subtable.
  5868. + * Further break up host field type macro to host field.
  5869. + * Move field width sanity checks to ipsec_sa.c
  5870. + * Define a mask for an entire saref.
  5871. + *
  5872. + * Revision 1.11 2002/09/20 15:40:33 rgb
  5873. + * Re-write most of the SAref macros and types to eliminate any pointer references to Entrys.
  5874. + * Fixed SAref/nfmark macros.
  5875. + * Rework saref freeslist.
  5876. + * Place all ipsec sadb globals into one struct.
  5877. + * Restrict some bits to kernel context for use to klips utils.
  5878. + *
  5879. + * Revision 1.10 2002/09/20 05:00:34 rgb
  5880. + * Update copyright date.
  5881. + *
  5882. + * Revision 1.9 2002/09/17 17:19:29 mcr
  5883. + * make it compile even if there is no netfilter - we lost
  5884. + * functionality, but it works, especially on 2.2.
  5885. + *
  5886. + * Revision 1.8 2002/07/28 22:59:53 mcr
  5887. + * clarified/expanded one comment.
  5888. + *
  5889. + * Revision 1.7 2002/07/26 08:48:31 rgb
  5890. + * Added SA ref table code.
  5891. + *
  5892. + * Revision 1.6 2002/05/31 17:27:48 rgb
  5893. + * Comment fix.
  5894. + *
  5895. + * Revision 1.5 2002/05/27 18:55:03 rgb
  5896. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  5897. + *
  5898. + * Revision 1.4 2002/05/23 07:13:36 rgb
  5899. + * Convert "usecount" to "refcount" to remove ambiguity.
  5900. + *
  5901. + * Revision 1.3 2002/04/24 07:36:47 mcr
  5902. + * Moved from ./klips/net/ipsec/ipsec_sa.h,v
  5903. + *
  5904. + * Revision 1.2 2001/11/26 09:16:15 rgb
  5905. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5906. + *
  5907. + * Revision 1.1.2.1 2001/09/25 02:24:58 mcr
  5908. + * struct tdb -> struct ipsec_sa.
  5909. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  5910. + * ipsec_xform.c removed. header file still contains useful things.
  5911. + *
  5912. + *
  5913. + * Local variables:
  5914. + * c-file-style: "linux"
  5915. + * End:
  5916. + *
  5917. + */
  5918. --- /dev/null Tue Mar 11 13:02:56 2003
  5919. +++ linux/include/openswan/ipsec_sha1.h Mon Feb 9 13:51:03 2004
  5920. @@ -0,0 +1,79 @@
  5921. +/*
  5922. + * RCSID $Id: ipsec_sha1.h,v 1.8 2004/04/05 19:55:07 mcr Exp $
  5923. + */
  5924. +
  5925. +/*
  5926. + * Here is the original comment from the distribution:
  5927. +
  5928. +SHA-1 in C
  5929. +By Steve Reid <steve@edmweb.com>
  5930. +100% Public Domain
  5931. +
  5932. + * Adapted for use by the IPSEC code by John Ioannidis
  5933. + */
  5934. +
  5935. +
  5936. +#ifndef _IPSEC_SHA1_H_
  5937. +#define _IPSEC_SHA1_H_
  5938. +
  5939. +typedef struct
  5940. +{
  5941. + __u32 state[5];
  5942. + __u32 count[2];
  5943. + __u8 buffer[64];
  5944. +} SHA1_CTX;
  5945. +
  5946. +void SHA1Transform(__u32 state[5], __u8 buffer[64]);
  5947. +void SHA1Init(void *context);
  5948. +void SHA1Update(void *context, unsigned char *data, __u32 len);
  5949. +void SHA1Final(unsigned char digest[20], void *context);
  5950. +
  5951. +
  5952. +#endif /* _IPSEC_SHA1_H_ */
  5953. +
  5954. +/*
  5955. + * $Log: ipsec_sha1.h,v $
  5956. + * Revision 1.8 2004/04/05 19:55:07 mcr
  5957. + * Moved from linux/include/freeswan/ipsec_sha1.h,v
  5958. + *
  5959. + * Revision 1.7 2002/09/10 01:45:09 mcr
  5960. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  5961. + * the function prototypes would match, and could be placed
  5962. + * into a pointer to a function.
  5963. + *
  5964. + * Revision 1.6 2002/04/24 07:36:47 mcr
  5965. + * Moved from ./klips/net/ipsec/ipsec_sha1.h,v
  5966. + *
  5967. + * Revision 1.5 1999/12/13 13:59:13 rgb
  5968. + * Quick fix to argument size to Update bugs.
  5969. + *
  5970. + * Revision 1.4 1999/12/07 18:16:23 rgb
  5971. + * Fixed comments at end of #endif lines.
  5972. + *
  5973. + * Revision 1.3 1999/04/06 04:54:27 rgb
  5974. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  5975. + * patch shell fixes.
  5976. + *
  5977. + * Revision 1.2 1998/11/30 13:22:54 rgb
  5978. + * Rationalised all the klips kernel file headers. They are much shorter
  5979. + * now and won't conflict under RH5.2.
  5980. + *
  5981. + * Revision 1.1 1998/06/18 21:27:50 henry
  5982. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  5983. + * kernel-build scripts happier in the presence of symlinks
  5984. + *
  5985. + * Revision 1.2 1998/04/23 20:54:05 rgb
  5986. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  5987. + * verified.
  5988. + *
  5989. + * Revision 1.1 1998/04/09 03:04:21 henry
  5990. + * sources moved up from linux/net/ipsec
  5991. + * these two include files modified not to include others except in kernel
  5992. + *
  5993. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  5994. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  5995. + *
  5996. + * Revision 0.4 1997/01/15 01:28:15 ji
  5997. + * New transform
  5998. + *
  5999. + */
  6000. --- /dev/null Tue Mar 11 13:02:56 2003
  6001. +++ linux/include/openswan/ipsec_stats.h Mon Feb 9 13:51:03 2004
  6002. @@ -0,0 +1,76 @@
  6003. +/*
  6004. + * @(#) definition of ipsec_stats structure
  6005. + *
  6006. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  6007. + * and Michael Richardson <mcr@freeswan.org>
  6008. + *
  6009. + * This program is free software; you can redistribute it and/or modify it
  6010. + * under the terms of the GNU General Public License as published by the
  6011. + * Free Software Foundation; either version 2 of the License, or (at your
  6012. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6013. + *
  6014. + * This program is distributed in the hope that it will be useful, but
  6015. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6016. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6017. + * for more details.
  6018. + *
  6019. + * RCSID $Id: ipsec_stats.h,v 1.7 2005/04/14 01:17:45 mcr Exp $
  6020. + *
  6021. + */
  6022. +
  6023. +/*
  6024. + * This file describes the errors/statistics that FreeSWAN collects.
  6025. + */
  6026. +
  6027. +#ifndef _IPSEC_STATS_H_
  6028. +
  6029. +struct ipsec_stats {
  6030. + __u32 ips_alg_errs; /* number of algorithm errors */
  6031. + __u32 ips_auth_errs; /* # of authentication errors */
  6032. + __u32 ips_encsize_errs; /* # of encryption size errors*/
  6033. + __u32 ips_encpad_errs; /* # of encryption pad errors*/
  6034. + __u32 ips_replaywin_errs; /* # of pkt sequence errors */
  6035. +};
  6036. +
  6037. +#define _IPSEC_STATS_H_
  6038. +#endif /* _IPSEC_STATS_H_ */
  6039. +
  6040. +/*
  6041. + * $Log: ipsec_stats.h,v $
  6042. + * Revision 1.7 2005/04/14 01:17:45 mcr
  6043. + * add prototypes for snprintf.
  6044. + *
  6045. + * Revision 1.6 2004/04/05 19:55:07 mcr
  6046. + * Moved from linux/include/freeswan/ipsec_stats.h,v
  6047. + *
  6048. + * Revision 1.5 2004/04/05 19:41:05 mcr
  6049. + * merged alg-branch code.
  6050. + *
  6051. + * Revision 1.4 2004/03/28 20:27:19 paul
  6052. + * Included tested and confirmed fixes mcr made and dhr verified for
  6053. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  6054. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  6055. + * dhr. (thanks dhr!)
  6056. + *
  6057. + * Revision 1.4 2004/03/24 01:58:31 mcr
  6058. + * sprintf->snprintf for formatting into proc buffer.
  6059. + *
  6060. + * Revision 1.3.34.1 2004/04/05 04:30:46 mcr
  6061. + * patches for alg-branch to compile/work with 2.x openswan
  6062. + *
  6063. + * Revision 1.3 2002/04/24 07:36:47 mcr
  6064. + * Moved from ./klips/net/ipsec/ipsec_stats.h,v
  6065. + *
  6066. + * Revision 1.2 2001/11/26 09:16:16 rgb
  6067. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  6068. + *
  6069. + * Revision 1.1.2.1 2001/09/25 02:27:00 mcr
  6070. + * statistics moved to seperate structure.
  6071. + *
  6072. + *
  6073. + *
  6074. + * Local variables:
  6075. + * c-file-style: "linux"
  6076. + * End:
  6077. + *
  6078. + */
  6079. --- /dev/null Tue Mar 11 13:02:56 2003
  6080. +++ linux/include/openswan/ipsec_tunnel.h Mon Feb 9 13:51:03 2004
  6081. @@ -0,0 +1,280 @@
  6082. +/*
  6083. + * IPSEC tunneling code
  6084. + * Copyright (C) 1996, 1997 John Ioannidis.
  6085. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  6086. + *
  6087. + * This program is free software; you can redistribute it and/or modify it
  6088. + * under the terms of the GNU General Public License as published by the
  6089. + * Free Software Foundation; either version 2 of the License, or (at your
  6090. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6091. + *
  6092. + * This program is distributed in the hope that it will be useful, but
  6093. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6094. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6095. + * for more details.
  6096. + *
  6097. + * RCSID $Id: ipsec_tunnel.h,v 1.33 2005/06/04 16:06:05 mcr Exp $
  6098. + */
  6099. +
  6100. +
  6101. +#ifdef NET_21
  6102. +# define DEV_QUEUE_XMIT(skb, device, pri) {\
  6103. + skb->dev = device; \
  6104. + neigh_compat_output(skb); \
  6105. + /* skb->dst->output(skb); */ \
  6106. + }
  6107. +# define ICMP_SEND(skb_in, type, code, info, dev) \
  6108. + icmp_send(skb_in, type, code, htonl(info))
  6109. +# define IP_SEND(skb, dev) \
  6110. + ip_send(skb);
  6111. +#else /* NET_21 */
  6112. +# define DEV_QUEUE_XMIT(skb, device, pri) {\
  6113. + dev_queue_xmit(skb, device, pri); \
  6114. + }
  6115. +# define ICMP_SEND(skb_in, type, code, info, dev) \
  6116. + icmp_send(skb_in, type, code, info, dev)
  6117. +# define IP_SEND(skb, dev) \
  6118. + if(ntohs(iph->tot_len) > physmtu) { \
  6119. + ip_fragment(NULL, skb, dev, 0); \
  6120. + ipsec_kfree_skb(skb); \
  6121. + } else { \
  6122. + dev_queue_xmit(skb, dev, SOPRI_NORMAL); \
  6123. + }
  6124. +#endif /* NET_21 */
  6125. +
  6126. +
  6127. +/*
  6128. + * Heavily based on drivers/net/new_tunnel.c. Lots
  6129. + * of ideas also taken from the 2.1.x version of drivers/net/shaper.c
  6130. + */
  6131. +
  6132. +struct ipsectunnelconf
  6133. +{
  6134. + __u32 cf_cmd;
  6135. + union
  6136. + {
  6137. + char cfu_name[12];
  6138. + } cf_u;
  6139. +#define cf_name cf_u.cfu_name
  6140. +};
  6141. +
  6142. +#define IPSEC_SET_DEV (SIOCDEVPRIVATE)
  6143. +#define IPSEC_DEL_DEV (SIOCDEVPRIVATE + 1)
  6144. +#define IPSEC_CLR_DEV (SIOCDEVPRIVATE + 2)
  6145. +
  6146. +#ifdef __KERNEL__
  6147. +#include <linux/version.h>
  6148. +#ifndef KERNEL_VERSION
  6149. +# define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  6150. +#endif
  6151. +struct ipsecpriv
  6152. +{
  6153. + struct sk_buff_head sendq;
  6154. + struct net_device *dev;
  6155. + struct wait_queue *wait_queue;
  6156. + char locked;
  6157. + int (*hard_start_xmit) (struct sk_buff *skb,
  6158. + struct net_device *dev);
  6159. + int (*hard_header) (struct sk_buff *skb,
  6160. + struct net_device *dev,
  6161. + unsigned short type,
  6162. + void *daddr,
  6163. + void *saddr,
  6164. + unsigned len);
  6165. +#ifdef NET_21
  6166. + int (*rebuild_header)(struct sk_buff *skb);
  6167. +#else /* NET_21 */
  6168. + int (*rebuild_header)(void *buff, struct net_device *dev,
  6169. + unsigned long raddr, struct sk_buff *skb);
  6170. +#endif /* NET_21 */
  6171. + int (*set_mac_address)(struct net_device *dev, void *addr);
  6172. +#ifndef NET_21
  6173. + void (*header_cache_bind)(struct hh_cache **hhp, struct net_device *dev,
  6174. + unsigned short htype, __u32 daddr);
  6175. +#endif /* !NET_21 */
  6176. + void (*header_cache_update)(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr);
  6177. + struct net_device_stats *(*get_stats)(struct net_device *dev);
  6178. + struct net_device_stats mystats;
  6179. + int mtu; /* What is the desired MTU? */
  6180. +};
  6181. +
  6182. +extern char ipsec_tunnel_c_version[];
  6183. +
  6184. +extern struct net_device *ipsecdevices[IPSEC_NUM_IF];
  6185. +
  6186. +int ipsec_tunnel_init_devices(void);
  6187. +
  6188. +/* void */ int ipsec_tunnel_cleanup_devices(void);
  6189. +
  6190. +extern /* void */ int ipsec_init(void);
  6191. +
  6192. +extern int ipsec_tunnel_start_xmit(struct sk_buff *skb, struct net_device *dev);
  6193. +extern struct net_device *ipsec_get_device(int inst);
  6194. +
  6195. +#ifdef CONFIG_KLIPS_DEBUG
  6196. +extern int debug_tunnel;
  6197. +extern int sysctl_ipsec_debug_verbose;
  6198. +#endif /* CONFIG_KLIPS_DEBUG */
  6199. +#endif /* __KERNEL__ */
  6200. +
  6201. +#ifdef CONFIG_KLIPS_DEBUG
  6202. +#define DB_TN_INIT 0x0001
  6203. +#define DB_TN_PROCFS 0x0002
  6204. +#define DB_TN_XMIT 0x0010
  6205. +#define DB_TN_OHDR 0x0020
  6206. +#define DB_TN_CROUT 0x0040
  6207. +#define DB_TN_OXFS 0x0080
  6208. +#define DB_TN_REVEC 0x0100
  6209. +#define DB_TN_ENCAP 0x0200
  6210. +#endif /* CONFIG_KLIPS_DEBUG */
  6211. +
  6212. +/*
  6213. + * $Log: ipsec_tunnel.h,v $
  6214. + * Revision 1.33 2005/06/04 16:06:05 mcr
  6215. + * better patch for nat-t rcv-device code.
  6216. + *
  6217. + * Revision 1.32 2005/05/21 03:18:35 mcr
  6218. + * added additional debug flag tunnelling.
  6219. + *
  6220. + * Revision 1.31 2004/08/03 18:18:02 mcr
  6221. + * in 2.6, use "net_device" instead of #define device->net_device.
  6222. + * this probably breaks 2.0 compiles.
  6223. + *
  6224. + * Revision 1.30 2004/07/10 19:08:41 mcr
  6225. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6226. + *
  6227. + * Revision 1.29 2004/04/05 19:55:07 mcr
  6228. + * Moved from linux/include/freeswan/ipsec_tunnel.h,v
  6229. + *
  6230. + * Revision 1.28 2003/06/24 20:22:32 mcr
  6231. + * added new global: ipsecdevices[] so that we can keep track of
  6232. + * the ipsecX devices. They will be referenced with dev_hold(),
  6233. + * so 2.2 may need this as well.
  6234. + *
  6235. + * Revision 1.27 2003/04/03 17:38:09 rgb
  6236. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  6237. + *
  6238. + * Revision 1.26 2003/02/12 19:32:20 rgb
  6239. + * Updated copyright year.
  6240. + *
  6241. + * Revision 1.25 2002/05/27 18:56:07 rgb
  6242. + * Convert to dynamic ipsec device allocation.
  6243. + *
  6244. + * Revision 1.24 2002/04/24 07:36:48 mcr
  6245. + * Moved from ./klips/net/ipsec/ipsec_tunnel.h,v
  6246. + *
  6247. + * Revision 1.23 2001/11/06 19:50:44 rgb
  6248. + * Moved IP_SEND, ICMP_SEND, DEV_QUEUE_XMIT macros to ipsec_tunnel.h for
  6249. + * use also by pfkey_v2_parser.c
  6250. + *
  6251. + * Revision 1.22 2001/09/15 16:24:05 rgb
  6252. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  6253. + *
  6254. + * Revision 1.21 2001/06/14 19:35:10 rgb
  6255. + * Update copyright date.
  6256. + *
  6257. + * Revision 1.20 2000/09/15 11:37:02 rgb
  6258. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  6259. + * IPCOMP zlib deflate code.
  6260. + *
  6261. + * Revision 1.19 2000/09/08 19:12:56 rgb
  6262. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  6263. + *
  6264. + * Revision 1.18 2000/07/28 13:50:54 rgb
  6265. + * Changed enet_statistics to net_device_stats and added back compatibility
  6266. + * for pre-2.1.19.
  6267. + *
  6268. + * Revision 1.17 1999/11/19 01:12:15 rgb
  6269. + * Purge unneeded proc_info prototypes, now that static linking uses
  6270. + * dynamic proc_info registration.
  6271. + *
  6272. + * Revision 1.16 1999/11/18 18:51:00 rgb
  6273. + * Changed all device registrations for static linking to
  6274. + * dynamic to reduce the number and size of patches.
  6275. + *
  6276. + * Revision 1.15 1999/11/18 04:14:21 rgb
  6277. + * Replaced all kernel version macros to shorter, readable form.
  6278. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  6279. + * Added Marc Boucher's 2.3.25 proc patches.
  6280. + *
  6281. + * Revision 1.14 1999/05/25 02:50:10 rgb
  6282. + * Fix kernel version macros for 2.0.x static linking.
  6283. + *
  6284. + * Revision 1.13 1999/05/25 02:41:06 rgb
  6285. + * Add ipsec_klipsdebug support for static linking.
  6286. + *
  6287. + * Revision 1.12 1999/05/05 22:02:32 rgb
  6288. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  6289. + *
  6290. + * Revision 1.11 1999/04/29 15:19:50 rgb
  6291. + * Add return values to init and cleanup functions.
  6292. + *
  6293. + * Revision 1.10 1999/04/16 16:02:39 rgb
  6294. + * Bump up macro to 4 ipsec I/Fs.
  6295. + *
  6296. + * Revision 1.9 1999/04/15 15:37:25 rgb
  6297. + * Forward check changes from POST1_00 branch.
  6298. + *
  6299. + * Revision 1.5.2.1 1999/04/02 04:26:14 rgb
  6300. + * Backcheck from HEAD, pre1.0.
  6301. + *
  6302. + * Revision 1.8 1999/04/11 00:29:01 henry
  6303. + * GPL boilerplate
  6304. + *
  6305. + * Revision 1.7 1999/04/06 04:54:28 rgb
  6306. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  6307. + * patch shell fixes.
  6308. + *
  6309. + * Revision 1.6 1999/03/31 05:44:48 rgb
  6310. + * Keep PMTU reduction private.
  6311. + *
  6312. + * Revision 1.5 1999/02/10 22:31:20 rgb
  6313. + * Change rebuild_header member to reflect generality of link layer.
  6314. + *
  6315. + * Revision 1.4 1998/12/01 13:22:04 rgb
  6316. + * Added support for debug printing of version info.
  6317. + *
  6318. + * Revision 1.3 1998/07/29 20:42:46 rgb
  6319. + * Add a macro for clearing all tunnel devices.
  6320. + * Rearrange structures and declarations for sharing with userspace.
  6321. + *
  6322. + * Revision 1.2 1998/06/25 20:01:45 rgb
  6323. + * Make prototypes available for ipsec_init and ipsec proc_dir_entries
  6324. + * for static linking.
  6325. + *
  6326. + * Revision 1.1 1998/06/18 21:27:50 henry
  6327. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  6328. + * kernel-build scripts happier in the presence of symlinks
  6329. + *
  6330. + * Revision 1.3 1998/05/18 21:51:50 rgb
  6331. + * Added macros for num of I/F's and a procfs debug switch.
  6332. + *
  6333. + * Revision 1.2 1998/04/21 21:29:09 rgb
  6334. + * Rearrange debug switches to change on the fly debug output from user
  6335. + * space. Only kernel changes checked in at this time. radij.c was also
  6336. + * changed to temporarily remove buggy debugging code in rj_delete causing
  6337. + * an OOPS and hence, netlink device open errors.
  6338. + *
  6339. + * Revision 1.1 1998/04/09 03:06:13 henry
  6340. + * sources moved up from linux/net/ipsec
  6341. + *
  6342. + * Revision 1.1.1.1 1998/04/08 05:35:05 henry
  6343. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  6344. + *
  6345. + * Revision 0.5 1997/06/03 04:24:48 ji
  6346. + * Added transport mode.
  6347. + * Changed the way routing is done.
  6348. + * Lots of bug fixes.
  6349. + *
  6350. + * Revision 0.4 1997/01/15 01:28:15 ji
  6351. + * No changes.
  6352. + *
  6353. + * Revision 0.3 1996/11/20 14:39:04 ji
  6354. + * Minor cleanups.
  6355. + * Rationalized debugging code.
  6356. + *
  6357. + * Revision 0.2 1996/11/02 00:18:33 ji
  6358. + * First limited release.
  6359. + *
  6360. + *
  6361. + */
  6362. --- /dev/null Tue Mar 11 13:02:56 2003
  6363. +++ linux/include/openswan/ipsec_xform.h Mon Feb 9 13:51:03 2004
  6364. @@ -0,0 +1,257 @@
  6365. +/*
  6366. + * Definitions relevant to IPSEC transformations
  6367. + * Copyright (C) 1996, 1997 John Ioannidis.
  6368. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  6369. + * COpyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  6370. + *
  6371. + * This program is free software; you can redistribute it and/or modify it
  6372. + * under the terms of the GNU General Public License as published by the
  6373. + * Free Software Foundation; either version 2 of the License, or (at your
  6374. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6375. + *
  6376. + * This program is distributed in the hope that it will be useful, but
  6377. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6378. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6379. + * for more details.
  6380. + *
  6381. + * RCSID $Id: ipsec_xform.h,v 1.41 2004/07/10 19:08:41 mcr Exp $
  6382. + */
  6383. +
  6384. +#ifndef _IPSEC_XFORM_H_
  6385. +
  6386. +#include <openswan.h>
  6387. +
  6388. +#define XF_NONE 0 /* No transform set */
  6389. +#define XF_IP4 1 /* IPv4 inside IPv4 */
  6390. +#define XF_AHMD5 2 /* AH MD5 */
  6391. +#define XF_AHSHA 3 /* AH SHA */
  6392. +#define XF_ESP3DES 5 /* ESP DES3-CBC */
  6393. +#define XF_AHHMACMD5 6 /* AH-HMAC-MD5 with opt replay prot */
  6394. +#define XF_AHHMACSHA1 7 /* AH-HMAC-SHA1 with opt replay prot */
  6395. +#define XF_ESP3DESMD5 9 /* triple DES, HMAC-MD-5, 128-bits of authentication */
  6396. +#define XF_ESP3DESMD596 10 /* triple DES, HMAC-MD-5, 96-bits of authentication */
  6397. +#define XF_ESPNULLMD596 12 /* NULL, HMAC-MD-5 with 96-bits of authentication */
  6398. +#define XF_ESPNULLSHA196 13 /* NULL, HMAC-SHA-1 with 96-bits of authentication */
  6399. +#define XF_ESP3DESSHA196 14 /* triple DES, HMAC-SHA-1, 96-bits of authentication */
  6400. +#define XF_IP6 15 /* IPv6 inside IPv6 */
  6401. +#define XF_COMPDEFLATE 16 /* IPCOMP deflate */
  6402. +
  6403. +#define XF_CLR 126 /* Clear SA table */
  6404. +#define XF_DEL 127 /* Delete SA */
  6405. +
  6406. +/* IPsec AH transform values
  6407. + * RFC 2407
  6408. + * draft-ietf-ipsec-doi-tc-mib-02.txt
  6409. + */
  6410. +
  6411. +#define AH_NONE 0
  6412. +#define AH_MD5 2
  6413. +#define AH_SHA 3
  6414. +/* draft-ietf-ipsec-ciph-aes-cbc-03.txt */
  6415. +#define AH_SHA2_256 5
  6416. +#define AH_SHA2_384 6
  6417. +#define AH_SHA2_512 7
  6418. +#define AH_RIPEMD 8
  6419. +#define AH_MAX 15
  6420. +
  6421. +/* IPsec ESP transform values */
  6422. +
  6423. +#define ESP_NONE 0
  6424. +#define ESP_DES 2
  6425. +#define ESP_3DES 3
  6426. +#define ESP_RC5 4
  6427. +#define ESP_IDEA 5
  6428. +#define ESP_CAST 6
  6429. +#define ESP_BLOWFISH 7
  6430. +#define ESP_3IDEA 8
  6431. +#define ESP_RC4 10
  6432. +#define ESP_NULL 11
  6433. +#define ESP_AES 12
  6434. +
  6435. +/* as draft-ietf-ipsec-ciph-aes-cbc-02.txt */
  6436. +#define ESP_MARS 249
  6437. +#define ESP_RC6 250
  6438. +#define ESP_SERPENT 252
  6439. +#define ESP_TWOFISH 253
  6440. +
  6441. +/* IPCOMP transform values */
  6442. +
  6443. +#define IPCOMP_NONE 0
  6444. +#define IPCOMP_OUI 1
  6445. +#define IPCOMP_DEFLAT 2
  6446. +#define IPCOMP_LZS 3
  6447. +#define IPCOMP_V42BIS 4
  6448. +
  6449. +#define XFT_AUTH 0x0001
  6450. +#define XFT_CONF 0x0100
  6451. +
  6452. +/* available if CONFIG_KLIPS_DEBUG is defined */
  6453. +#define DB_XF_INIT 0x0001
  6454. +
  6455. +#define PROTO2TXT(x) \
  6456. + (x) == IPPROTO_AH ? "AH" : \
  6457. + (x) == IPPROTO_ESP ? "ESP" : \
  6458. + (x) == IPPROTO_IPIP ? "IPIP" : \
  6459. + (x) == IPPROTO_COMP ? "COMP" : \
  6460. + "UNKNOWN_proto"
  6461. +static inline const char *enc_name_id (unsigned id) {
  6462. + static char buf[16];
  6463. + snprintf(buf, sizeof(buf), "_ID%d", id);
  6464. + return buf;
  6465. +}
  6466. +static inline const char *auth_name_id (unsigned id) {
  6467. + static char buf[16];
  6468. + snprintf(buf, sizeof(buf), "_ID%d", id);
  6469. + return buf;
  6470. +}
  6471. +#define IPS_XFORM_NAME(x) \
  6472. + PROTO2TXT((x)->ips_said.proto), \
  6473. + (x)->ips_said.proto == IPPROTO_COMP ? \
  6474. + ((x)->ips_encalg == SADB_X_CALG_DEFLATE ? \
  6475. + "_DEFLATE" : "_UNKNOWN_comp") : \
  6476. + (x)->ips_encalg == ESP_NONE ? "" : \
  6477. + (x)->ips_encalg == ESP_3DES ? "_3DES" : \
  6478. + (x)->ips_encalg == ESP_AES ? "_AES" : \
  6479. + (x)->ips_encalg == ESP_SERPENT ? "_SERPENT" : \
  6480. + (x)->ips_encalg == ESP_TWOFISH ? "_TWOFISH" : \
  6481. + enc_name_id(x->ips_encalg)/* "_UNKNOWN_encr" */, \
  6482. + (x)->ips_authalg == AH_NONE ? "" : \
  6483. + (x)->ips_authalg == AH_MD5 ? "_HMAC_MD5" : \
  6484. + (x)->ips_authalg == AH_SHA ? "_HMAC_SHA1" : \
  6485. + (x)->ips_authalg == AH_SHA2_256 ? "_HMAC_SHA2_256" : \
  6486. + (x)->ips_authalg == AH_SHA2_384 ? "_HMAC_SHA2_384" : \
  6487. + (x)->ips_authalg == AH_SHA2_512 ? "_HMAC_SHA2_512" : \
  6488. + auth_name_id(x->ips_authalg) /* "_UNKNOWN_auth" */ \
  6489. +
  6490. +#ifdef __KERNEL__
  6491. +struct ipsec_rcv_state;
  6492. +struct ipsec_xmit_state;
  6493. +
  6494. +struct xform_functions {
  6495. + enum ipsec_rcv_value (*rcv_checks)(struct ipsec_rcv_state *irs,
  6496. + struct sk_buff *skb);
  6497. + enum ipsec_rcv_value (*rcv_decrypt)(struct ipsec_rcv_state *irs);
  6498. +
  6499. + enum ipsec_rcv_value (*rcv_setup_auth)(struct ipsec_rcv_state *irs,
  6500. + struct sk_buff *skb,
  6501. + __u32 *replay,
  6502. + unsigned char **authenticator);
  6503. + enum ipsec_rcv_value (*rcv_calc_auth)(struct ipsec_rcv_state *irs,
  6504. + struct sk_buff *skb);
  6505. +
  6506. + enum ipsec_xmit_value (*xmit_setup)(struct ipsec_xmit_state *ixs);
  6507. + enum ipsec_xmit_value (*xmit_encrypt)(struct ipsec_xmit_state *ixs);
  6508. +
  6509. + enum ipsec_xmit_value (*xmit_setup_auth)(struct ipsec_xmit_state *ixs,
  6510. + struct sk_buff *skb,
  6511. + __u32 *replay,
  6512. + unsigned char **authenticator);
  6513. + enum ipsec_xmit_value (*xmit_calc_auth)(struct ipsec_xmit_state *ixs,
  6514. + struct sk_buff *skb);
  6515. + int xmit_headroom;
  6516. + int xmit_needtailroom;
  6517. +};
  6518. +
  6519. +#endif /* __KERNEL__ */
  6520. +
  6521. +#ifdef CONFIG_KLIPS_DEBUG
  6522. +extern void ipsec_dmp(char *s, caddr_t bb, int len);
  6523. +#else /* CONFIG_KLIPS_DEBUG */
  6524. +#define ipsec_dmp(_x, _y, _z)
  6525. +#endif /* CONFIG_KLIPS_DEBUG */
  6526. +
  6527. +
  6528. +#define _IPSEC_XFORM_H_
  6529. +#endif /* _IPSEC_XFORM_H_ */
  6530. +
  6531. +/*
  6532. + * $Log: ipsec_xform.h,v $
  6533. + * Revision 1.41 2004/07/10 19:08:41 mcr
  6534. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6535. + *
  6536. + * Revision 1.40 2004/04/06 02:49:08 mcr
  6537. + * pullup of algo code from alg-branch.
  6538. + *
  6539. + * Revision 1.39 2004/04/05 19:55:07 mcr
  6540. + * Moved from linux/include/freeswan/ipsec_xform.h,v
  6541. + *
  6542. + * Revision 1.38 2004/04/05 19:41:05 mcr
  6543. + * merged alg-branch code.
  6544. + *
  6545. + * Revision 1.37 2003/12/13 19:10:16 mcr
  6546. + * refactored rcv and xmit code - same as FS 2.05.
  6547. + *
  6548. + * Revision 1.36.34.1 2003/12/22 15:25:52 jjo
  6549. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  6550. + *
  6551. + * Revision 1.36 2002/04/24 07:36:48 mcr
  6552. + * Moved from ./klips/net/ipsec/ipsec_xform.h,v
  6553. + *
  6554. + * Revision 1.35 2001/11/26 09:23:51 rgb
  6555. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  6556. + *
  6557. + * Revision 1.33.2.1 2001/09/25 02:24:58 mcr
  6558. + * struct tdb -> struct ipsec_sa.
  6559. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  6560. + * ipsec_xform.c removed. header file still contains useful things.
  6561. + *
  6562. + * Revision 1.34 2001/11/06 19:47:17 rgb
  6563. + * Changed lifetime_packets to uint32 from uint64.
  6564. + *
  6565. + * Revision 1.33 2001/09/08 21:13:34 rgb
  6566. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  6567. + *
  6568. + * Revision 1.32 2001/07/06 07:40:01 rgb
  6569. + * Reformatted for readability.
  6570. + * Added inbound policy checking fields for use with IPIP SAs.
  6571. + *
  6572. + * Revision 1.31 2001/06/14 19:35:11 rgb
  6573. + * Update copyright date.
  6574. + *
  6575. + * Revision 1.30 2001/05/30 08:14:03 rgb
  6576. + * Removed vestiges of esp-null transforms.
  6577. + *
  6578. + * Revision 1.29 2001/01/30 23:42:47 rgb
  6579. + * Allow pfkey msgs from pid other than user context required for ACQUIRE
  6580. + * and subsequent ADD or UDATE.
  6581. + *
  6582. + * Revision 1.28 2000/11/06 04:30:40 rgb
  6583. + * Add Svenning's adaptive content compression.
  6584. + *
  6585. + * Revision 1.27 2000/09/19 00:38:25 rgb
  6586. + * Fixed algorithm name bugs introduced for ipcomp.
  6587. + *
  6588. + * Revision 1.26 2000/09/17 21:36:48 rgb
  6589. + * Added proto2txt macro.
  6590. + *
  6591. + * Revision 1.25 2000/09/17 18:56:47 rgb
  6592. + * Added IPCOMP support.
  6593. + *
  6594. + * Revision 1.24 2000/09/12 19:34:12 rgb
  6595. + * Defined XF_IP6 from Gerhard for ipv6 tunnel support.
  6596. + *
  6597. + * Revision 1.23 2000/09/12 03:23:14 rgb
  6598. + * Cleaned out now unused tdb_xform and tdb_xdata members of struct tdb.
  6599. + *
  6600. + * Revision 1.22 2000/09/08 19:12:56 rgb
  6601. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  6602. + *
  6603. + * Revision 1.21 2000/09/01 18:32:43 rgb
  6604. + * Added (disabled) sensitivity members to tdb struct.
  6605. + *
  6606. + * Revision 1.20 2000/08/30 05:31:01 rgb
  6607. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  6608. + * Kill remainder of tdb_xform, tdb_xdata, xformsw.
  6609. + *
  6610. + * Revision 1.19 2000/08/01 14:51:52 rgb
  6611. + * Removed _all_ remaining traces of DES.
  6612. + *
  6613. + * Revision 1.18 2000/01/21 06:17:45 rgb
  6614. + * Tidied up spacing.
  6615. + *
  6616. + *
  6617. + * Local variables:
  6618. + * c-file-style: "linux"
  6619. + * End:
  6620. + *
  6621. + */
  6622. --- /dev/null Tue Mar 11 13:02:56 2003
  6623. +++ linux/include/openswan/ipsec_xmit.h Mon Feb 9 13:51:03 2004
  6624. @@ -0,0 +1,198 @@
  6625. +/*
  6626. + * IPSEC tunneling code
  6627. + * Copyright (C) 1996, 1997 John Ioannidis.
  6628. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  6629. + *
  6630. + * This program is free software; you can redistribute it and/or modify it
  6631. + * under the terms of the GNU General Public License as published by the
  6632. + * Free Software Foundation; either version 2 of the License, or (at your
  6633. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6634. + *
  6635. + * This program is distributed in the hope that it will be useful, but
  6636. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6637. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6638. + * for more details.
  6639. + *
  6640. + * RCSID $Id: ipsec_xmit.h,v 1.14 2005/05/11 01:00:26 mcr Exp $
  6641. + */
  6642. +
  6643. +#include "openswan/ipsec_sa.h"
  6644. +
  6645. +enum ipsec_xmit_value
  6646. +{
  6647. + IPSEC_XMIT_STOLEN=2,
  6648. + IPSEC_XMIT_PASS=1,
  6649. + IPSEC_XMIT_OK=0,
  6650. + IPSEC_XMIT_ERRMEMALLOC=-1,
  6651. + IPSEC_XMIT_ESP_BADALG=-2,
  6652. + IPSEC_XMIT_BADPROTO=-3,
  6653. + IPSEC_XMIT_ESP_PUSHPULLERR=-4,
  6654. + IPSEC_XMIT_BADLEN=-5,
  6655. + IPSEC_XMIT_AH_BADALG=-6,
  6656. + IPSEC_XMIT_SAIDNOTFOUND=-7,
  6657. + IPSEC_XMIT_SAIDNOTLIVE=-8,
  6658. + IPSEC_XMIT_REPLAYROLLED=-9,
  6659. + IPSEC_XMIT_LIFETIMEFAILED=-10,
  6660. + IPSEC_XMIT_CANNOTFRAG=-11,
  6661. + IPSEC_XMIT_MSSERR=-12,
  6662. + IPSEC_XMIT_ERRSKBALLOC=-13,
  6663. + IPSEC_XMIT_ENCAPFAIL=-14,
  6664. + IPSEC_XMIT_NODEV=-15,
  6665. + IPSEC_XMIT_NOPRIVDEV=-16,
  6666. + IPSEC_XMIT_NOPHYSDEV=-17,
  6667. + IPSEC_XMIT_NOSKB=-18,
  6668. + IPSEC_XMIT_NOIPV6=-19,
  6669. + IPSEC_XMIT_NOIPOPTIONS=-20,
  6670. + IPSEC_XMIT_TTLEXPIRED=-21,
  6671. + IPSEC_XMIT_BADHHLEN=-22,
  6672. + IPSEC_XMIT_PUSHPULLERR=-23,
  6673. + IPSEC_XMIT_ROUTEERR=-24,
  6674. + IPSEC_XMIT_RECURSDETECT=-25,
  6675. + IPSEC_XMIT_IPSENDFAILURE=-26,
  6676. + IPSEC_XMIT_ESPUDP=-27,
  6677. + IPSEC_XMIT_ESPUDP_BADTYPE=-28,
  6678. +};
  6679. +
  6680. +struct ipsec_xmit_state
  6681. +{
  6682. + struct sk_buff *skb; /* working skb pointer */
  6683. + struct net_device *dev; /* working dev pointer */
  6684. + struct ipsecpriv *prv; /* Our device' private space */
  6685. + struct sk_buff *oskb; /* Original skb pointer */
  6686. + struct net_device_stats *stats; /* This device's statistics */
  6687. + struct iphdr *iph; /* Our new IP header */
  6688. + __u32 newdst; /* The other SG's IP address */
  6689. + __u32 orgdst; /* Original IP destination address */
  6690. + __u32 orgedst; /* 1st SG's IP address */
  6691. + __u32 newsrc; /* The new source SG's IP address */
  6692. + __u32 orgsrc; /* Original IP source address */
  6693. + __u32 innersrc; /* Innermost IP source address */
  6694. + int iphlen; /* IP header length */
  6695. + int pyldsz; /* upper protocol payload size */
  6696. + int headroom;
  6697. + int tailroom;
  6698. + int authlen;
  6699. + int max_headroom; /* The extra header space needed */
  6700. + int max_tailroom; /* The extra stuffing needed */
  6701. + int ll_headroom; /* The extra link layer hard_header space needed */
  6702. + int tot_headroom; /* The total header space needed */
  6703. + int tot_tailroom; /* The totalstuffing needed */
  6704. + __u8 *saved_header; /* saved copy of the hard header */
  6705. + unsigned short sport, dport;
  6706. +
  6707. + struct sockaddr_encap matcher; /* eroute search key */
  6708. + struct eroute *eroute;
  6709. + struct ipsec_sa *ipsp, *ipsq; /* ipsec_sa pointers */
  6710. + char sa_txt[SATOT_BUF];
  6711. + size_t sa_len;
  6712. + int hard_header_stripped; /* has the hard header been removed yet? */
  6713. + int hard_header_len;
  6714. + struct net_device *physdev;
  6715. +/* struct device *virtdev; */
  6716. + short physmtu;
  6717. + short cur_mtu; /* copy of prv->mtu, cause prv may == NULL */
  6718. + short mtudiff;
  6719. +#ifdef NET_21
  6720. + struct rtable *route;
  6721. +#endif /* NET_21 */
  6722. + ip_said outgoing_said;
  6723. +#ifdef NET_21
  6724. + int pass;
  6725. +#endif /* NET_21 */
  6726. + int error;
  6727. + uint32_t eroute_pid;
  6728. + struct ipsec_sa ips;
  6729. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  6730. + uint8_t natt_type;
  6731. + uint8_t natt_head;
  6732. + uint16_t natt_sport;
  6733. + uint16_t natt_dport;
  6734. +#endif
  6735. +};
  6736. +
  6737. +enum ipsec_xmit_value
  6738. +ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs);
  6739. +
  6740. +enum ipsec_xmit_value
  6741. +ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs);
  6742. +
  6743. +enum ipsec_xmit_value
  6744. +ipsec_xmit_encap_bundle(struct ipsec_xmit_state *ixs);
  6745. +
  6746. +extern void ipsec_extract_ports(struct iphdr * iph, struct sockaddr_encap * er);
  6747. +
  6748. +
  6749. +extern int ipsec_xmit_trap_count;
  6750. +extern int ipsec_xmit_trap_sendcount;
  6751. +
  6752. +#ifdef CONFIG_KLIPS_DEBUG
  6753. +extern int debug_tunnel;
  6754. +
  6755. +#define debug_xmit debug_tunnel
  6756. +
  6757. +#define ipsec_xmit_dmp(_x,_y, _z) if (debug_xmit && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  6758. +#else
  6759. +#define ipsec_xmit_dmp(_x,_y, _z) do {} while(0)
  6760. +
  6761. +#endif /* CONFIG_KLIPS_DEBUG */
  6762. +
  6763. +extern int sysctl_ipsec_debug_verbose;
  6764. +extern int sysctl_ipsec_icmp;
  6765. +extern int sysctl_ipsec_tos;
  6766. +
  6767. +
  6768. +/*
  6769. + * $Log: ipsec_xmit.h,v $
  6770. + * Revision 1.14 2005/05/11 01:00:26 mcr
  6771. + * do not call debug routines if !defined KLIPS_DEBUG.
  6772. + *
  6773. + * Revision 1.13 2005/04/29 05:01:38 mcr
  6774. + * use ipsec_dmp_block.
  6775. + * added cur_mtu to ixs instead of using ixs->dev.
  6776. + *
  6777. + * Revision 1.12 2004/08/20 21:45:37 mcr
  6778. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  6779. + * be 26sec compatible. But, some defines where changed.
  6780. + *
  6781. + * Revision 1.11 2004/08/03 18:18:21 mcr
  6782. + * in 2.6, use "net_device" instead of #define device->net_device.
  6783. + * this probably breaks 2.0 compiles.
  6784. + *
  6785. + * Revision 1.10 2004/07/10 19:08:41 mcr
  6786. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6787. + *
  6788. + * Revision 1.9 2004/04/06 02:49:08 mcr
  6789. + * pullup of algo code from alg-branch.
  6790. + *
  6791. + * Revision 1.8 2004/04/05 19:55:07 mcr
  6792. + * Moved from linux/include/freeswan/ipsec_xmit.h,v
  6793. + *
  6794. + * Revision 1.7 2004/02/03 03:11:40 mcr
  6795. + * new xmit type if the UDP encapsulation is wrong.
  6796. + *
  6797. + * Revision 1.6 2003/12/13 19:10:16 mcr
  6798. + * refactored rcv and xmit code - same as FS 2.05.
  6799. + *
  6800. + * Revision 1.5 2003/12/10 01:20:06 mcr
  6801. + * NAT-traversal patches to KLIPS.
  6802. + *
  6803. + * Revision 1.4 2003/12/06 16:37:04 mcr
  6804. + * 1.4.7a X.509 patch applied.
  6805. + *
  6806. + * Revision 1.3 2003/10/31 02:27:05 mcr
  6807. + * pulled up port-selector patches and sa_id elimination.
  6808. + *
  6809. + * Revision 1.2.4.2 2003/10/29 01:10:19 mcr
  6810. + * elimited "struct sa_id"
  6811. + *
  6812. + * Revision 1.2.4.1 2003/09/21 13:59:38 mcr
  6813. + * pre-liminary X.509 patch - does not yet pass tests.
  6814. + *
  6815. + * Revision 1.2 2003/06/20 01:42:13 mcr
  6816. + * added counters to measure how many ACQUIREs we send to pluto,
  6817. + * and how many are successfully sent.
  6818. + *
  6819. + * Revision 1.1 2003/02/12 19:31:03 rgb
  6820. + * Refactored from ipsec_tunnel.c
  6821. + *
  6822. + */
  6823. --- /dev/null Tue Mar 11 13:02:56 2003
  6824. +++ linux/include/openswan/passert.h Mon Feb 9 13:51:03 2004
  6825. @@ -0,0 +1,75 @@
  6826. +/*
  6827. + * sanitize a string into a printable format.
  6828. + *
  6829. + * Copyright (C) 1998-2002 D. Hugh Redelmeier.
  6830. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  6831. + *
  6832. + * This library is free software; you can redistribute it and/or modify it
  6833. + * under the terms of the GNU Library General Public License as published by
  6834. + * the Free Software Foundation; either version 2 of the License, or (at your
  6835. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  6836. + *
  6837. + * This library is distributed in the hope that it will be useful, but
  6838. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6839. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  6840. + * License for more details.
  6841. + *
  6842. + * RCSID $Id: passert.h,v 1.7 2004/10/21 18:44:42 mcr Exp $
  6843. + */
  6844. +
  6845. +#include "openswan.h"
  6846. +
  6847. +#ifndef _OPENSWAN_PASSERT_H
  6848. +#define _OPENSWAN_PASSERT_H
  6849. +/* our versions of assert: log result */
  6850. +
  6851. +#ifdef DEBUG
  6852. +
  6853. +typedef void (*openswan_passert_fail_t)(const char *pred_str,
  6854. + const char *file_str,
  6855. + unsigned long line_no) NEVER_RETURNS;
  6856. +
  6857. +openswan_passert_fail_t openswan_passert_fail;
  6858. +
  6859. +extern void pexpect_log(const char *pred_str
  6860. + , const char *file_str, unsigned long line_no);
  6861. +
  6862. +# define impossible() do { \
  6863. + if(openswan_passert_fail) { \
  6864. + (*openswan_passert_fail)("impossible", __FILE__, __LINE__); \
  6865. + }} while(0)
  6866. +
  6867. +extern void switch_fail(int n
  6868. + , const char *file_str, unsigned long line_no) NEVER_RETURNS;
  6869. +
  6870. +# define bad_case(n) switch_fail((int) n, __FILE__, __LINE__)
  6871. +
  6872. +# define passert(pred) do { \
  6873. + if (!(pred)) \
  6874. + if(openswan_passert_fail) { \
  6875. + (*openswan_passert_fail)(#pred, __FILE__, __LINE__); \
  6876. + } \
  6877. + } while(0)
  6878. +
  6879. +# define pexpect(pred) do { \
  6880. + if (!(pred)) \
  6881. + pexpect_log(#pred, __FILE__, __LINE__); \
  6882. + } while(0)
  6883. +
  6884. +/* assert that an err_t is NULL; evaluate exactly once */
  6885. +# define happy(x) { \
  6886. + err_t ugh = x; \
  6887. + if (ugh != NULL) \
  6888. + if(openswan_passert_fail) { (*openswan_passert_fail)(ugh, __FILE__, __LINE__); } \
  6889. + }
  6890. +
  6891. +#else /*!DEBUG*/
  6892. +
  6893. +# define impossible() abort()
  6894. +# define bad_case(n) abort()
  6895. +# define passert(pred) { } /* do nothing */
  6896. +# define happy(x) { (void) x; } /* evaluate non-judgementally */
  6897. +
  6898. +#endif /*!DEBUG*/
  6899. +
  6900. +#endif /* _OPENSWAN_PASSERT_H */
  6901. --- /dev/null Tue Mar 11 13:02:56 2003
  6902. +++ linux/include/openswan/pfkey_debug.h Mon Feb 9 13:51:03 2004
  6903. @@ -0,0 +1,54 @@
  6904. +/*
  6905. + * sanitize a string into a printable format.
  6906. + *
  6907. + * Copyright (C) 1998-2002 D. Hugh Redelmeier.
  6908. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  6909. + *
  6910. + * This library is free software; you can redistribute it and/or modify it
  6911. + * under the terms of the GNU Library General Public License as published by
  6912. + * the Free Software Foundation; either version 2 of the License, or (at your
  6913. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  6914. + *
  6915. + * This library is distributed in the hope that it will be useful, but
  6916. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6917. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  6918. + * License for more details.
  6919. + *
  6920. + * RCSID $Id: pfkey_debug.h,v 1.3 2004/04/05 19:55:07 mcr Exp $
  6921. + */
  6922. +
  6923. +#ifndef _FREESWAN_PFKEY_DEBUG_H
  6924. +#define _FREESWAN_PFKEY_DEBUG_H
  6925. +
  6926. +#ifdef __KERNEL__
  6927. +
  6928. +/* note, kernel version ignores pfkey levels */
  6929. +# define DEBUGGING(level,args...) \
  6930. + KLIPS_PRINT(debug_pfkey, "klips_debug:" args)
  6931. +
  6932. +# define ERROR(args...) printk(KERN_ERR "klips:" args)
  6933. +
  6934. +#else
  6935. +
  6936. +extern unsigned int pfkey_lib_debug;
  6937. +
  6938. +extern void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
  6939. +extern void (*pfkey_error_func)(const char *message, ...) PRINTF_LIKE(1);
  6940. +
  6941. +#define DEBUGGING(level,args...) if(pfkey_lib_debug & level) { \
  6942. + if(pfkey_debug_func != NULL) { \
  6943. + (*pfkey_debug_func)("pfkey_lib_debug:" args); \
  6944. + } else { \
  6945. + printf("pfkey_lib_debug:" args); \
  6946. + } }
  6947. +
  6948. +#define ERROR(args...) if(pfkey_error_func != NULL) { \
  6949. + (*pfkey_error_func)("pfkey_lib_debug:" args); \
  6950. + }
  6951. +
  6952. +# define MALLOC(size) malloc(size)
  6953. +# define FREE(obj) free(obj)
  6954. +
  6955. +#endif
  6956. +
  6957. +#endif
  6958. --- /dev/null Tue Mar 11 13:02:56 2003
  6959. +++ linux/include/openswan/radij.h Mon Feb 9 13:51:03 2004
  6960. @@ -0,0 +1,280 @@
  6961. +/*
  6962. + * RCSID $Id: radij.h,v 1.13 2004/04/05 19:55:08 mcr Exp $
  6963. + */
  6964. +
  6965. +/*
  6966. + * This file is defived from ${SRC}/sys/net/radix.h of BSD 4.4lite
  6967. + *
  6968. + * Variable and procedure names have been modified so that they don't
  6969. + * conflict with the original BSD code, as a small number of modifications
  6970. + * have been introduced and we may want to reuse this code in BSD.
  6971. + *
  6972. + * The `j' in `radij' is pronounced as a voiceless guttural (like a Greek
  6973. + * chi or a German ch sound (as `doch', not as in `milch'), or even a
  6974. + * spanish j as in Juan. It is not as far back in the throat like
  6975. + * the corresponding Hebrew sound, nor is it a soft breath like the English h.
  6976. + * It has nothing to do with the Dutch ij sound.
  6977. + *
  6978. + * Here is the appropriate copyright notice:
  6979. + */
  6980. +
  6981. +/*
  6982. + * Copyright (c) 1988, 1989, 1993
  6983. + * The Regents of the University of California. All rights reserved.
  6984. + *
  6985. + * Redistribution and use in source and binary forms, with or without
  6986. + * modification, are permitted provided that the following conditions
  6987. + * are met:
  6988. + * 1. Redistributions of source code must retain the above copyright
  6989. + * notice, this list of conditions and the following disclaimer.
  6990. + * 2. Redistributions in binary form must reproduce the above copyright
  6991. + * notice, this list of conditions and the following disclaimer in the
  6992. + * documentation and/or other materials provided with the distribution.
  6993. + * 3. All advertising materials mentioning features or use of this software
  6994. + * must display the following acknowledgement:
  6995. + * This product includes software developed by the University of
  6996. + * California, Berkeley and its contributors.
  6997. + * 4. Neither the name of the University nor the names of its contributors
  6998. + * may be used to endorse or promote products derived from this software
  6999. + * without specific prior written permission.
  7000. + *
  7001. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  7002. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  7003. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  7004. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  7005. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  7006. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  7007. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  7008. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  7009. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  7010. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  7011. + * SUCH DAMAGE.
  7012. + *
  7013. + * @(#)radix.h 8.1 (Berkeley) 6/10/93
  7014. + */
  7015. +
  7016. +#ifndef _RADIJ_H_
  7017. +#define _RADIJ_H_
  7018. +
  7019. +/*
  7020. +#define RJ_DEBUG
  7021. +*/
  7022. +
  7023. +#ifdef __KERNEL__
  7024. +
  7025. +#ifndef __P
  7026. +#ifdef __STDC__
  7027. +#define __P(x) x
  7028. +#else
  7029. +#define __P(x) ()
  7030. +#endif
  7031. +#endif
  7032. +
  7033. +/*
  7034. + * Radix search tree node layout.
  7035. + */
  7036. +
  7037. +struct radij_node
  7038. +{
  7039. + struct radij_mask *rj_mklist; /* list of masks contained in subtree */
  7040. + struct radij_node *rj_p; /* parent */
  7041. + short rj_b; /* bit offset; -1-index(netmask) */
  7042. + char rj_bmask; /* node: mask for bit test*/
  7043. + u_char rj_flags; /* enumerated next */
  7044. +#define RJF_NORMAL 1 /* leaf contains normal route */
  7045. +#define RJF_ROOT 2 /* leaf is root leaf for tree */
  7046. +#define RJF_ACTIVE 4 /* This node is alive (for rtfree) */
  7047. + union {
  7048. + struct { /* leaf only data: */
  7049. + caddr_t rj_Key; /* object of search */
  7050. + caddr_t rj_Mask; /* netmask, if present */
  7051. + struct radij_node *rj_Dupedkey;
  7052. + } rj_leaf;
  7053. + struct { /* node only data: */
  7054. + int rj_Off; /* where to start compare */
  7055. + struct radij_node *rj_L;/* progeny */
  7056. + struct radij_node *rj_R;/* progeny */
  7057. + }rj_node;
  7058. + } rj_u;
  7059. +#ifdef RJ_DEBUG
  7060. + int rj_info;
  7061. + struct radij_node *rj_twin;
  7062. + struct radij_node *rj_ybro;
  7063. +#endif
  7064. +};
  7065. +
  7066. +#define rj_dupedkey rj_u.rj_leaf.rj_Dupedkey
  7067. +#define rj_key rj_u.rj_leaf.rj_Key
  7068. +#define rj_mask rj_u.rj_leaf.rj_Mask
  7069. +#define rj_off rj_u.rj_node.rj_Off
  7070. +#define rj_l rj_u.rj_node.rj_L
  7071. +#define rj_r rj_u.rj_node.rj_R
  7072. +
  7073. +/*
  7074. + * Annotations to tree concerning potential routes applying to subtrees.
  7075. + */
  7076. +
  7077. +extern struct radij_mask {
  7078. + short rm_b; /* bit offset; -1-index(netmask) */
  7079. + char rm_unused; /* cf. rj_bmask */
  7080. + u_char rm_flags; /* cf. rj_flags */
  7081. + struct radij_mask *rm_mklist; /* more masks to try */
  7082. + caddr_t rm_mask; /* the mask */
  7083. + int rm_refs; /* # of references to this struct */
  7084. +} *rj_mkfreelist;
  7085. +
  7086. +#define MKGet(m) {\
  7087. + if (rj_mkfreelist) {\
  7088. + m = rj_mkfreelist; \
  7089. + rj_mkfreelist = (m)->rm_mklist; \
  7090. + } else \
  7091. + R_Malloc(m, struct radij_mask *, sizeof (*(m))); }\
  7092. +
  7093. +#define MKFree(m) { (m)->rm_mklist = rj_mkfreelist; rj_mkfreelist = (m);}
  7094. +
  7095. +struct radij_node_head {
  7096. + struct radij_node *rnh_treetop;
  7097. + int rnh_addrsize; /* permit, but not require fixed keys */
  7098. + int rnh_pktsize; /* permit, but not require fixed keys */
  7099. +#if 0
  7100. + struct radij_node *(*rnh_addaddr) /* add based on sockaddr */
  7101. + __P((void *v, void *mask,
  7102. + struct radij_node_head *head, struct radij_node nodes[]));
  7103. +#endif
  7104. + int (*rnh_addaddr) /* add based on sockaddr */
  7105. + __P((void *v, void *mask,
  7106. + struct radij_node_head *head, struct radij_node nodes[]));
  7107. + struct radij_node *(*rnh_addpkt) /* add based on packet hdr */
  7108. + __P((void *v, void *mask,
  7109. + struct radij_node_head *head, struct radij_node nodes[]));
  7110. +#if 0
  7111. + struct radij_node *(*rnh_deladdr) /* remove based on sockaddr */
  7112. + __P((void *v, void *mask, struct radij_node_head *head));
  7113. +#endif
  7114. + int (*rnh_deladdr) /* remove based on sockaddr */
  7115. + __P((void *v, void *mask, struct radij_node_head *head, struct radij_node **node));
  7116. + struct radij_node *(*rnh_delpkt) /* remove based on packet hdr */
  7117. + __P((void *v, void *mask, struct radij_node_head *head));
  7118. + struct radij_node *(*rnh_matchaddr) /* locate based on sockaddr */
  7119. + __P((void *v, struct radij_node_head *head));
  7120. + struct radij_node *(*rnh_matchpkt) /* locate based on packet hdr */
  7121. + __P((void *v, struct radij_node_head *head));
  7122. + int (*rnh_walktree) /* traverse tree */
  7123. + __P((struct radij_node_head *head, int (*f)(struct radij_node *rn, void *w), void *w));
  7124. + struct radij_node rnh_nodes[3]; /* empty tree for common case */
  7125. +};
  7126. +
  7127. +
  7128. +#define Bcmp(a, b, n) memcmp(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
  7129. +#define Bcopy(a, b, n) memmove(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
  7130. +#define Bzero(p, n) memset((caddr_t)(p), 0, (unsigned)(n))
  7131. +#define R_Malloc(p, t, n) ((p = (t) kmalloc((size_t)(n), GFP_ATOMIC)), Bzero((p),(n)))
  7132. +#define Free(p) kfree((caddr_t)p);
  7133. +
  7134. +void rj_init __P((void));
  7135. +int rj_inithead __P((void **, int));
  7136. +int rj_refines __P((void *, void *));
  7137. +int rj_walktree __P((struct radij_node_head *head, int (*f)(struct radij_node *rn, void *w), void *w));
  7138. +struct radij_node
  7139. + *rj_addmask __P((void *, int, int)) /* , rgb */ ;
  7140. +int /* * */ rj_addroute __P((void *, void *, struct radij_node_head *,
  7141. + struct radij_node [2])) /* , rgb */ ;
  7142. +int /* * */ rj_delete __P((void *, void *, struct radij_node_head *, struct radij_node **)) /* , rgb */ ;
  7143. +struct radij_node /* rgb */
  7144. + *rj_insert __P((void *, struct radij_node_head *, int *,
  7145. + struct radij_node [2])),
  7146. + *rj_match __P((void *, struct radij_node_head *)),
  7147. + *rj_newpair __P((void *, int, struct radij_node[2])),
  7148. + *rj_search __P((void *, struct radij_node *)),
  7149. + *rj_search_m __P((void *, struct radij_node *, void *));
  7150. +
  7151. +void rj_deltree(struct radij_node_head *);
  7152. +void rj_delnodes(struct radij_node *);
  7153. +void rj_free_mkfreelist(void);
  7154. +int radijcleartree(void);
  7155. +int radijcleanup(void);
  7156. +
  7157. +extern struct radij_node_head *mask_rjhead;
  7158. +extern int maj_keylen;
  7159. +#endif /* __KERNEL__ */
  7160. +
  7161. +#endif /* _RADIJ_H_ */
  7162. +
  7163. +
  7164. +/*
  7165. + * $Log: radij.h,v $
  7166. + * Revision 1.13 2004/04/05 19:55:08 mcr
  7167. + * Moved from linux/include/freeswan/radij.h,v
  7168. + *
  7169. + * Revision 1.12 2002/04/24 07:36:48 mcr
  7170. + * Moved from ./klips/net/ipsec/radij.h,v
  7171. + *
  7172. + * Revision 1.11 2001/09/20 15:33:00 rgb
  7173. + * Min/max cleanup.
  7174. + *
  7175. + * Revision 1.10 1999/11/18 04:09:20 rgb
  7176. + * Replaced all kernel version macros to shorter, readable form.
  7177. + *
  7178. + * Revision 1.9 1999/05/05 22:02:33 rgb
  7179. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  7180. + *
  7181. + * Revision 1.8 1999/04/29 15:24:58 rgb
  7182. + * Add check for existence of macros min/max.
  7183. + *
  7184. + * Revision 1.7 1999/04/11 00:29:02 henry
  7185. + * GPL boilerplate
  7186. + *
  7187. + * Revision 1.6 1999/04/06 04:54:29 rgb
  7188. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  7189. + * patch shell fixes.
  7190. + *
  7191. + * Revision 1.5 1999/01/22 06:30:32 rgb
  7192. + * 64-bit clean-up.
  7193. + *
  7194. + * Revision 1.4 1998/11/30 13:22:55 rgb
  7195. + * Rationalised all the klips kernel file headers. They are much shorter
  7196. + * now and won't conflict under RH5.2.
  7197. + *
  7198. + * Revision 1.3 1998/10/25 02:43:27 rgb
  7199. + * Change return type on rj_addroute and rj_delete and add and argument
  7200. + * to the latter to be able to transmit more infomation about errors.
  7201. + *
  7202. + * Revision 1.2 1998/07/14 18:09:51 rgb
  7203. + * Add a routine to clear eroute table.
  7204. + * Added #ifdef __KERNEL__ directives to restrict scope of header.
  7205. + *
  7206. + * Revision 1.1 1998/06/18 21:30:22 henry
  7207. + * move sources from klips/src to klips/net/ipsec to keep stupid kernel
  7208. + * build scripts happier about symlinks
  7209. + *
  7210. + * Revision 1.4 1998/05/25 20:34:16 rgb
  7211. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  7212. + *
  7213. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  7214. + * add ipsec_rj_walker_delete.
  7215. + *
  7216. + * Recover memory for eroute table on unload of module.
  7217. + *
  7218. + * Revision 1.3 1998/04/22 16:51:37 rgb
  7219. + * Tidy up radij debug code from recent rash of modifications to debug code.
  7220. + *
  7221. + * Revision 1.2 1998/04/14 17:30:38 rgb
  7222. + * Fix up compiling errors for radij tree memory reclamation.
  7223. + *
  7224. + * Revision 1.1 1998/04/09 03:06:16 henry
  7225. + * sources moved up from linux/net/ipsec
  7226. + *
  7227. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  7228. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  7229. + *
  7230. + * Revision 0.4 1997/01/15 01:28:15 ji
  7231. + * No changes.
  7232. + *
  7233. + * Revision 0.3 1996/11/20 14:44:45 ji
  7234. + * Release update only.
  7235. + *
  7236. + * Revision 0.2 1996/11/02 00:18:33 ji
  7237. + * First limited release.
  7238. + *
  7239. + *
  7240. + */
  7241. --- /dev/null Tue Mar 11 13:02:56 2003
  7242. +++ linux/include/pfkey.h Mon Feb 9 13:51:03 2004
  7243. @@ -0,0 +1,529 @@
  7244. +/*
  7245. + * FreeS/WAN specific PF_KEY headers
  7246. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  7247. + *
  7248. + * This program is free software; you can redistribute it and/or modify it
  7249. + * under the terms of the GNU General Public License as published by the
  7250. + * Free Software Foundation; either version 2 of the License, or (at your
  7251. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  7252. + *
  7253. + * This program is distributed in the hope that it will be useful, but
  7254. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  7255. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  7256. + * for more details.
  7257. + *
  7258. + * RCSID $Id: pfkey.h,v 1.49 2005/05/11 00:57:29 mcr Exp $
  7259. + */
  7260. +
  7261. +#ifndef __NET_IPSEC_PF_KEY_H
  7262. +#define __NET_IPSEC_PF_KEY_H
  7263. +#ifdef __KERNEL__
  7264. +extern struct proto_ops pfkey_proto_ops;
  7265. +typedef struct sock pfkey_sock;
  7266. +extern int debug_pfkey;
  7267. +
  7268. +extern /* void */ int pfkey_init(void);
  7269. +extern /* void */ int pfkey_cleanup(void);
  7270. +
  7271. +struct socket_list
  7272. +{
  7273. + struct socket *socketp;
  7274. + struct socket_list *next;
  7275. +};
  7276. +extern int pfkey_list_insert_socket(struct socket*, struct socket_list**);
  7277. +extern int pfkey_list_remove_socket(struct socket*, struct socket_list**);
  7278. +extern struct socket_list *pfkey_open_sockets;
  7279. +extern struct socket_list *pfkey_registered_sockets[];
  7280. +
  7281. +struct ipsec_alg_supported
  7282. +{
  7283. + uint16_t ias_exttype;
  7284. + uint8_t ias_id;
  7285. + uint8_t ias_ivlen;
  7286. + uint16_t ias_keyminbits;
  7287. + uint16_t ias_keymaxbits;
  7288. + char *ias_name;
  7289. +};
  7290. +
  7291. +extern struct supported_list *pfkey_supported_list[];
  7292. +struct supported_list
  7293. +{
  7294. + struct ipsec_alg_supported *supportedp;
  7295. + struct supported_list *next;
  7296. +};
  7297. +extern int pfkey_list_insert_supported(struct ipsec_alg_supported*, struct supported_list**);
  7298. +extern int pfkey_list_remove_supported(struct ipsec_alg_supported*, struct supported_list**);
  7299. +
  7300. +struct sockaddr_key
  7301. +{
  7302. + uint16_t key_family; /* PF_KEY */
  7303. + uint16_t key_pad; /* not used */
  7304. + uint32_t key_pid; /* process ID */
  7305. +};
  7306. +
  7307. +struct pfkey_extracted_data
  7308. +{
  7309. + struct ipsec_sa* ips;
  7310. + struct ipsec_sa* ips2;
  7311. + struct eroute *eroute;
  7312. +};
  7313. +
  7314. +/* forward reference */
  7315. +struct sadb_ext;
  7316. +struct sadb_msg;
  7317. +struct sockaddr;
  7318. +struct sadb_comb;
  7319. +struct sadb_sadb;
  7320. +struct sadb_alg;
  7321. +
  7322. +extern int
  7323. +pfkey_alloc_eroute(struct eroute** eroute);
  7324. +
  7325. +extern int
  7326. +pfkey_sa_process(struct sadb_ext *pfkey_ext,
  7327. + struct pfkey_extracted_data* extr);
  7328. +
  7329. +extern int
  7330. +pfkey_lifetime_process(struct sadb_ext *pfkey_ext,
  7331. + struct pfkey_extracted_data* extr);
  7332. +
  7333. +extern int
  7334. +pfkey_address_process(struct sadb_ext *pfkey_ext,
  7335. + struct pfkey_extracted_data* extr);
  7336. +
  7337. +extern int
  7338. +pfkey_key_process(struct sadb_ext *pfkey_ext,
  7339. + struct pfkey_extracted_data* extr);
  7340. +
  7341. +extern int
  7342. +pfkey_ident_process(struct sadb_ext *pfkey_ext,
  7343. + struct pfkey_extracted_data* extr);
  7344. +
  7345. +extern int
  7346. +pfkey_sens_process(struct sadb_ext *pfkey_ext,
  7347. + struct pfkey_extracted_data* extr);
  7348. +
  7349. +extern int
  7350. +pfkey_prop_process(struct sadb_ext *pfkey_ext,
  7351. + struct pfkey_extracted_data* extr);
  7352. +
  7353. +extern int
  7354. +pfkey_supported_process(struct sadb_ext *pfkey_ext,
  7355. + struct pfkey_extracted_data* extr);
  7356. +
  7357. +extern int
  7358. +pfkey_spirange_process(struct sadb_ext *pfkey_ext,
  7359. + struct pfkey_extracted_data* extr);
  7360. +
  7361. +extern int
  7362. +pfkey_x_kmprivate_process(struct sadb_ext *pfkey_ext,
  7363. + struct pfkey_extracted_data* extr);
  7364. +
  7365. +extern int
  7366. +pfkey_x_satype_process(struct sadb_ext *pfkey_ext,
  7367. + struct pfkey_extracted_data* extr);
  7368. +
  7369. +extern int
  7370. +pfkey_x_debug_process(struct sadb_ext *pfkey_ext,
  7371. + struct pfkey_extracted_data* extr);
  7372. +
  7373. +extern int pfkey_upmsg(struct socket *, struct sadb_msg *);
  7374. +extern int pfkey_expire(struct ipsec_sa *, int);
  7375. +extern int pfkey_acquire(struct ipsec_sa *);
  7376. +#else /* ! __KERNEL__ */
  7377. +
  7378. +extern void (*pfkey_debug_func)(const char *message, ...);
  7379. +extern void (*pfkey_error_func)(const char *message, ...);
  7380. +extern void pfkey_print(struct sadb_msg *msg, FILE *out);
  7381. +
  7382. +
  7383. +#endif /* __KERNEL__ */
  7384. +
  7385. +extern uint8_t satype2proto(uint8_t satype);
  7386. +extern uint8_t proto2satype(uint8_t proto);
  7387. +extern char* satype2name(uint8_t satype);
  7388. +extern char* proto2name(uint8_t proto);
  7389. +
  7390. +struct key_opt
  7391. +{
  7392. + uint32_t key_pid; /* process ID */
  7393. + struct sock *sk;
  7394. +};
  7395. +
  7396. +#define key_pid(sk) ((struct key_opt*)&((sk)->sk_protinfo))->key_pid
  7397. +
  7398. +/* XXX-mcr this is not an alignment, this is because the count is in 64-bit
  7399. + * words.
  7400. + */
  7401. +#define IPSEC_PFKEYv2_ALIGN (sizeof(uint64_t)/sizeof(uint8_t))
  7402. +#define BITS_PER_OCTET 8
  7403. +#define OCTETBITS 8
  7404. +#define PFKEYBITS 64
  7405. +#define DIVUP(x,y) ((x + y -1) / y) /* divide, rounding upwards */
  7406. +#define ALIGN_N(x,y) (DIVUP(x,y) * y) /* align on y boundary */
  7407. +
  7408. +#define IPSEC_PFKEYv2_LEN(x) ((x) * IPSEC_PFKEYv2_ALIGN)
  7409. +#define IPSEC_PFKEYv2_WORDS(x) ((x) / IPSEC_PFKEYv2_ALIGN)
  7410. +
  7411. +
  7412. +#define PFKEYv2_MAX_MSGSIZE 4096
  7413. +
  7414. +/*
  7415. + * PF_KEYv2 permitted and required extensions in and out bitmaps
  7416. + */
  7417. +struct pf_key_ext_parsers_def {
  7418. + int (*parser)(struct sadb_ext*);
  7419. + char *parser_name;
  7420. +};
  7421. +
  7422. +
  7423. +#define SADB_EXTENSIONS_MAX 31
  7424. +extern unsigned int extensions_bitmaps[2/*in/out*/][2/*perm/req*/][SADB_EXTENSIONS_MAX];
  7425. +#define EXT_BITS_IN 0
  7426. +#define EXT_BITS_OUT 1
  7427. +#define EXT_BITS_PERM 0
  7428. +#define EXT_BITS_REQ 1
  7429. +
  7430. +extern void pfkey_extensions_init(struct sadb_ext *extensions[]);
  7431. +extern void pfkey_extensions_free(struct sadb_ext *extensions[]);
  7432. +extern void pfkey_msg_free(struct sadb_msg **pfkey_msg);
  7433. +
  7434. +extern int pfkey_msg_parse(struct sadb_msg *pfkey_msg,
  7435. + struct pf_key_ext_parsers_def *ext_parsers[],
  7436. + struct sadb_ext **extensions,
  7437. + int dir);
  7438. +
  7439. +extern int pfkey_register_reply(int satype, struct sadb_msg *sadb_msg);
  7440. +
  7441. +/*
  7442. + * PF_KEYv2 build function prototypes
  7443. + */
  7444. +
  7445. +int
  7446. +pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
  7447. + uint8_t msg_type,
  7448. + uint8_t satype,
  7449. + uint8_t msg_errno,
  7450. + uint32_t seq,
  7451. + uint32_t pid);
  7452. +
  7453. +int
  7454. +pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
  7455. + uint16_t exttype,
  7456. + uint32_t spi, /* in network order */
  7457. + uint8_t replay_window,
  7458. + uint8_t sa_state,
  7459. + uint8_t auth,
  7460. + uint8_t encrypt,
  7461. + uint32_t flags,
  7462. + uint32_t/*IPsecSAref_t*/ ref);
  7463. +
  7464. +int
  7465. +pfkey_sa_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. +
  7474. +int
  7475. +pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
  7476. + uint16_t exttype,
  7477. + uint32_t allocations,
  7478. + uint64_t bytes,
  7479. + uint64_t addtime,
  7480. + uint64_t usetime,
  7481. + uint32_t packets);
  7482. +
  7483. +int
  7484. +pfkey_address_build(struct sadb_ext** pfkey_ext,
  7485. + uint16_t exttype,
  7486. + uint8_t proto,
  7487. + uint8_t prefixlen,
  7488. + struct sockaddr* address);
  7489. +
  7490. +int
  7491. +pfkey_key_build(struct sadb_ext** pfkey_ext,
  7492. + uint16_t exttype,
  7493. + uint16_t key_bits,
  7494. + char* key);
  7495. +
  7496. +int
  7497. +pfkey_ident_build(struct sadb_ext** pfkey_ext,
  7498. + uint16_t exttype,
  7499. + uint16_t ident_type,
  7500. + uint64_t ident_id,
  7501. + uint8_t ident_len,
  7502. + char* ident_string);
  7503. +
  7504. +#ifdef __KERNEL__
  7505. +extern int pfkey_nat_t_new_mapping(struct ipsec_sa *, struct sockaddr *, __u16);
  7506. +extern int pfkey_x_nat_t_type_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr);
  7507. +extern int pfkey_x_nat_t_port_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr);
  7508. +#endif /* __KERNEL__ */
  7509. +int
  7510. +pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
  7511. + uint8_t type);
  7512. +int
  7513. +pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
  7514. + uint16_t exttype,
  7515. + uint16_t port);
  7516. +
  7517. +int
  7518. +pfkey_sens_build(struct sadb_ext** pfkey_ext,
  7519. + uint32_t dpd,
  7520. + uint8_t sens_level,
  7521. + uint8_t sens_len,
  7522. + uint64_t* sens_bitmap,
  7523. + uint8_t integ_level,
  7524. + uint8_t integ_len,
  7525. + uint64_t* integ_bitmap);
  7526. +
  7527. +int pfkey_x_protocol_build(struct sadb_ext **, uint8_t);
  7528. +
  7529. +
  7530. +int
  7531. +pfkey_prop_build(struct sadb_ext** pfkey_ext,
  7532. + uint8_t replay,
  7533. + unsigned int comb_num,
  7534. + struct sadb_comb* comb);
  7535. +
  7536. +int
  7537. +pfkey_supported_build(struct sadb_ext** pfkey_ext,
  7538. + uint16_t exttype,
  7539. + unsigned int alg_num,
  7540. + struct sadb_alg* alg);
  7541. +
  7542. +int
  7543. +pfkey_spirange_build(struct sadb_ext** pfkey_ext,
  7544. + uint16_t exttype,
  7545. + uint32_t min,
  7546. + uint32_t max);
  7547. +
  7548. +int
  7549. +pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext);
  7550. +
  7551. +int
  7552. +pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
  7553. + uint8_t satype);
  7554. +
  7555. +int
  7556. +pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
  7557. + uint32_t tunnel,
  7558. + uint32_t netlink,
  7559. + uint32_t xform,
  7560. + uint32_t eroute,
  7561. + uint32_t spi,
  7562. + uint32_t radij,
  7563. + uint32_t esp,
  7564. + uint32_t ah,
  7565. + uint32_t rcv,
  7566. + uint32_t pfkey,
  7567. + uint32_t ipcomp,
  7568. + uint32_t verbose);
  7569. +
  7570. +int
  7571. +pfkey_msg_build(struct sadb_msg** pfkey_msg,
  7572. + struct sadb_ext* extensions[],
  7573. + int dir);
  7574. +
  7575. +/* in pfkey_v2_debug.c - routines to decode numbers -> strings */
  7576. +const char *
  7577. +pfkey_v2_sadb_ext_string(int extnum);
  7578. +
  7579. +const char *
  7580. +pfkey_v2_sadb_type_string(int sadb_type);
  7581. +
  7582. +
  7583. +#endif /* __NET_IPSEC_PF_KEY_H */
  7584. +
  7585. +/*
  7586. + * $Log: pfkey.h,v $
  7587. + * Revision 1.49 2005/05/11 00:57:29 mcr
  7588. + * rename struct supported -> struct ipsec_alg_supported.
  7589. + * make pfkey.h more standalone.
  7590. + *
  7591. + * Revision 1.48 2005/05/01 03:12:50 mcr
  7592. + * include name of algorithm in datastructure.
  7593. + *
  7594. + * Revision 1.47 2004/08/21 00:44:14 mcr
  7595. + * simplify definition of nat_t related prototypes.
  7596. + *
  7597. + * Revision 1.46 2004/08/04 16:27:22 mcr
  7598. + * 2.6 sk_ options.
  7599. + *
  7600. + * Revision 1.45 2004/04/06 02:49:00 mcr
  7601. + * pullup of algo code from alg-branch.
  7602. + *
  7603. + * Revision 1.44 2003/12/10 01:20:01 mcr
  7604. + * NAT-traversal patches to KLIPS.
  7605. + *
  7606. + * Revision 1.43 2003/10/31 02:26:44 mcr
  7607. + * pulled up port-selector patches.
  7608. + *
  7609. + * Revision 1.42.2.2 2003/10/29 01:09:32 mcr
  7610. + * added debugging for pfkey library.
  7611. + *
  7612. + * Revision 1.42.2.1 2003/09/21 13:59:34 mcr
  7613. + * pre-liminary X.509 patch - does not yet pass tests.
  7614. + *
  7615. + * Revision 1.42 2003/08/25 22:08:19 mcr
  7616. + * removed pfkey_proto_init() from pfkey.h for 2.6 support.
  7617. + *
  7618. + * Revision 1.41 2003/05/07 17:28:57 mcr
  7619. + * new function pfkey_debug_func added for us in debugging from
  7620. +
  7621. + * pfkey library.
  7622. + *
  7623. + * Revision 1.40 2003/01/30 02:31:34 rgb
  7624. + *
  7625. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  7626. + *
  7627. + * Revision 1.39 2002/09/20 15:40:21 rgb
  7628. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  7629. + * Added ref parameter to pfkey_sa_build().
  7630. + * Cleaned out unused cruft.
  7631. + *
  7632. + * Revision 1.38 2002/05/14 02:37:24 rgb
  7633. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  7634. + * ipsec_sa or ipsec_sa.
  7635. + * Added function prototypes for the functions moved to
  7636. + * pfkey_v2_ext_process.c.
  7637. + *
  7638. + * Revision 1.37 2002/04/24 07:36:49 mcr
  7639. + * Moved from ./lib/pfkey.h,v
  7640. + *
  7641. + * Revision 1.36 2002/01/20 20:34:49 mcr
  7642. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  7643. + *
  7644. + * Revision 1.35 2001/11/27 05:27:47 mcr
  7645. + * pfkey parses are now maintained by a structure
  7646. + * that includes their name for debug purposes.
  7647. + *
  7648. + * Revision 1.34 2001/11/26 09:23:53 rgb
  7649. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  7650. + *
  7651. + * Revision 1.33 2001/11/06 19:47:47 rgb
  7652. + * Added packet parameter to lifetime and comb structures.
  7653. + *
  7654. + * Revision 1.32 2001/09/08 21:13:34 rgb
  7655. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  7656. + *
  7657. + * Revision 1.31 2001/06/14 19:35:16 rgb
  7658. + * Update copyright date.
  7659. + *
  7660. + * Revision 1.30 2001/02/27 07:04:52 rgb
  7661. + * Added satype2name prototype.
  7662. + *
  7663. + * Revision 1.29 2001/02/26 19:59:33 rgb
  7664. + * Ditch unused sadb_satype2proto[], replaced by satype2proto().
  7665. + *
  7666. + * Revision 1.28 2000/10/10 20:10:19 rgb
  7667. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  7668. + *
  7669. + * Revision 1.27 2000/09/21 04:20:45 rgb
  7670. + * Fixed array size off-by-one error. (Thanks Svenning!)
  7671. + *
  7672. + * Revision 1.26 2000/09/12 03:26:05 rgb
  7673. + * Added pfkey_acquire prototype.
  7674. + *
  7675. + * Revision 1.25 2000/09/08 19:21:28 rgb
  7676. + * Fix pfkey_prop_build() parameter to be only single indirection.
  7677. + *
  7678. + * Revision 1.24 2000/09/01 18:46:42 rgb
  7679. + * Added a supported algorithms array lists, one per satype and registered
  7680. + * existing algorithms.
  7681. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  7682. + * list.
  7683. + *
  7684. + * Revision 1.23 2000/08/27 01:55:26 rgb
  7685. + * Define OCTETBITS and PFKEYBITS to avoid using 'magic' numbers in code.
  7686. + *
  7687. + * Revision 1.22 2000/08/20 21:39:23 rgb
  7688. + * Added kernel prototypes for kernel funcitions pfkey_upmsg() and
  7689. + * pfkey_expire().
  7690. + *
  7691. + * Revision 1.21 2000/08/15 17:29:23 rgb
  7692. + * Fixes from SZI to untested pfkey_prop_build().
  7693. + *
  7694. + * Revision 1.20 2000/05/10 20:14:19 rgb
  7695. + * Fleshed out sensitivity, proposal and supported extensions.
  7696. + *
  7697. + * Revision 1.19 2000/03/16 14:07:23 rgb
  7698. + * Renamed ALIGN macro to avoid fighting with others in kernel.
  7699. + *
  7700. + * Revision 1.18 2000/01/22 23:24:06 rgb
  7701. + * Added prototypes for proto2satype(), satype2proto() and proto2name().
  7702. + *
  7703. + * Revision 1.17 2000/01/21 06:26:59 rgb
  7704. + * Converted from double tdb arguments to one structure (extr)
  7705. + * containing pointers to all temporary information structures.
  7706. + * Added klipsdebug switching capability.
  7707. + * Dropped unused argument to pfkey_x_satype_build().
  7708. + *
  7709. + * Revision 1.16 1999/12/29 21:17:41 rgb
  7710. + * Changed pfkey_msg_build() I/F to include a struct sadb_msg**
  7711. + * parameter for cleaner manipulation of extensions[] and to guard
  7712. + * against potential memory leaks.
  7713. + * Changed the I/F to pfkey_msg_free() for the same reason.
  7714. + *
  7715. + * Revision 1.15 1999/12/09 23:12:54 rgb
  7716. + * Added macro for BITS_PER_OCTET.
  7717. + * Added argument to pfkey_sa_build() to do eroutes.
  7718. + *
  7719. + * Revision 1.14 1999/12/08 20:33:25 rgb
  7720. + * Changed sa_family_t to uint16_t for 2.0.xx compatibility.
  7721. + *
  7722. + * Revision 1.13 1999/12/07 19:53:40 rgb
  7723. + * Removed unused first argument from extension parsers.
  7724. + * Changed __u* types to uint* to avoid use of asm/types.h and
  7725. + * sys/types.h in userspace code.
  7726. + * Added function prototypes for pfkey message and extensions
  7727. + * initialisation and cleanup.
  7728. + *
  7729. + * Revision 1.12 1999/12/01 22:19:38 rgb
  7730. + * Change pfkey_sa_build to accept an SPI in network byte order.
  7731. + *
  7732. + * Revision 1.11 1999/11/27 11:55:26 rgb
  7733. + * Added extern sadb_satype2proto to enable moving protocol lookup table
  7734. + * to lib/pfkey_v2_parse.c.
  7735. + * Delete unused, moved typedefs.
  7736. + * Add argument to pfkey_msg_parse() for direction.
  7737. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  7738. + *
  7739. + * Revision 1.10 1999/11/23 22:29:21 rgb
  7740. + * This file has been moved in the distribution from klips/net/ipsec to
  7741. + * lib.
  7742. + * Add macros for dealing with alignment and rounding up more opaquely.
  7743. + * The uint<n>_t type defines have been moved to freeswan.h to avoid
  7744. + * chicken-and-egg problems.
  7745. + * Add macros for dealing with alignment and rounding up more opaque.
  7746. + * Added prototypes for using extention header bitmaps.
  7747. + * Added prototypes of all the build functions.
  7748. + *
  7749. + * Revision 1.9 1999/11/20 21:59:48 rgb
  7750. + * Moved socketlist type declarations and prototypes for shared use.
  7751. + * Slightly modified scope of sockaddr_key declaration.
  7752. + *
  7753. + * Revision 1.8 1999/11/17 14:34:25 rgb
  7754. + * Protect sa_family_t from being used in userspace with GLIBC<2.
  7755. + *
  7756. + * Revision 1.7 1999/10/27 19:40:35 rgb
  7757. + * Add a maximum PFKEY packet size macro.
  7758. + *
  7759. + * Revision 1.6 1999/10/26 16:58:58 rgb
  7760. + * Created a sockaddr_key and key_opt socket extension structures.
  7761. + *
  7762. + * Revision 1.5 1999/06/10 05:24:41 rgb
  7763. + * Renamed variables to reduce confusion.
  7764. + *
  7765. + * Revision 1.4 1999/04/29 15:21:11 rgb
  7766. + * Add pfkey support to debugging.
  7767. + * Add return values to init and cleanup functions.
  7768. + *
  7769. + * Revision 1.3 1999/04/15 17:58:07 rgb
  7770. + * Add RCSID labels.
  7771. + *
  7772. + */
  7773. --- /dev/null Tue Mar 11 13:02:56 2003
  7774. +++ linux/include/pfkeyv2.h Mon Feb 9 13:51:03 2004
  7775. @@ -0,0 +1,472 @@
  7776. +/*
  7777. + * RCSID $Id: pfkeyv2.h,v 1.31 2005/04/14 01:14:54 mcr Exp $
  7778. + */
  7779. +
  7780. +/*
  7781. +RFC 2367 PF_KEY Key Management API July 1998
  7782. +
  7783. +
  7784. +Appendix D: Sample Header File
  7785. +
  7786. +This file defines structures and symbols for the PF_KEY Version 2
  7787. +key management interface. It was written at the U.S. Naval Research
  7788. +Laboratory. This file is in the public domain. The authors ask that
  7789. +you leave this credit intact on any copies of this file.
  7790. +*/
  7791. +#ifndef __PFKEY_V2_H
  7792. +#define __PFKEY_V2_H 1
  7793. +
  7794. +#define PF_KEY_V2 2
  7795. +#define PFKEYV2_REVISION 199806L
  7796. +
  7797. +#define SADB_RESERVED 0
  7798. +#define SADB_GETSPI 1
  7799. +#define SADB_UPDATE 2
  7800. +#define SADB_ADD 3
  7801. +#define SADB_DELETE 4
  7802. +#define SADB_GET 5
  7803. +#define SADB_ACQUIRE 6
  7804. +#define SADB_REGISTER 7
  7805. +#define SADB_EXPIRE 8
  7806. +#define SADB_FLUSH 9
  7807. +#define SADB_DUMP 10
  7808. +#define SADB_X_PROMISC 11
  7809. +#define SADB_X_PCHANGE 12
  7810. +#define SADB_X_GRPSA 13
  7811. +#define SADB_X_ADDFLOW 14
  7812. +#define SADB_X_DELFLOW 15
  7813. +#define SADB_X_DEBUG 16
  7814. +#define SADB_X_NAT_T_NEW_MAPPING 17
  7815. +#define SADB_MAX 17
  7816. +
  7817. +struct sadb_msg {
  7818. + uint8_t sadb_msg_version;
  7819. + uint8_t sadb_msg_type;
  7820. + uint8_t sadb_msg_errno;
  7821. + uint8_t sadb_msg_satype;
  7822. + uint16_t sadb_msg_len;
  7823. + uint16_t sadb_msg_reserved;
  7824. + uint32_t sadb_msg_seq;
  7825. + uint32_t sadb_msg_pid;
  7826. +};
  7827. +
  7828. +struct sadb_ext {
  7829. + uint16_t sadb_ext_len;
  7830. + uint16_t sadb_ext_type;
  7831. +};
  7832. +
  7833. +struct sadb_sa {
  7834. + uint16_t sadb_sa_len;
  7835. + uint16_t sadb_sa_exttype;
  7836. + uint32_t sadb_sa_spi;
  7837. + uint8_t sadb_sa_replay;
  7838. + uint8_t sadb_sa_state;
  7839. + uint8_t sadb_sa_auth;
  7840. + uint8_t sadb_sa_encrypt;
  7841. + uint32_t sadb_sa_flags;
  7842. + uint32_t /*IPsecSAref_t*/ sadb_x_sa_ref; /* 32 bits */
  7843. + uint8_t sadb_x_reserved[4];
  7844. +};
  7845. +
  7846. +struct sadb_sa_v1 {
  7847. + uint16_t sadb_sa_len;
  7848. + uint16_t sadb_sa_exttype;
  7849. + uint32_t sadb_sa_spi;
  7850. + uint8_t sadb_sa_replay;
  7851. + uint8_t sadb_sa_state;
  7852. + uint8_t sadb_sa_auth;
  7853. + uint8_t sadb_sa_encrypt;
  7854. + uint32_t sadb_sa_flags;
  7855. +};
  7856. +
  7857. +struct sadb_lifetime {
  7858. + uint16_t sadb_lifetime_len;
  7859. + uint16_t sadb_lifetime_exttype;
  7860. + uint32_t sadb_lifetime_allocations;
  7861. + uint64_t sadb_lifetime_bytes;
  7862. + uint64_t sadb_lifetime_addtime;
  7863. + uint64_t sadb_lifetime_usetime;
  7864. + uint32_t sadb_x_lifetime_packets;
  7865. + uint32_t sadb_x_lifetime_reserved;
  7866. +};
  7867. +
  7868. +struct sadb_address {
  7869. + uint16_t sadb_address_len;
  7870. + uint16_t sadb_address_exttype;
  7871. + uint8_t sadb_address_proto;
  7872. + uint8_t sadb_address_prefixlen;
  7873. + uint16_t sadb_address_reserved;
  7874. +};
  7875. +
  7876. +struct sadb_key {
  7877. + uint16_t sadb_key_len;
  7878. + uint16_t sadb_key_exttype;
  7879. + uint16_t sadb_key_bits;
  7880. + uint16_t sadb_key_reserved;
  7881. +};
  7882. +
  7883. +struct sadb_ident {
  7884. + uint16_t sadb_ident_len;
  7885. + uint16_t sadb_ident_exttype;
  7886. + uint16_t sadb_ident_type;
  7887. + uint16_t sadb_ident_reserved;
  7888. + uint64_t sadb_ident_id;
  7889. +};
  7890. +
  7891. +struct sadb_sens {
  7892. + uint16_t sadb_sens_len;
  7893. + uint16_t sadb_sens_exttype;
  7894. + uint32_t sadb_sens_dpd;
  7895. + uint8_t sadb_sens_sens_level;
  7896. + uint8_t sadb_sens_sens_len;
  7897. + uint8_t sadb_sens_integ_level;
  7898. + uint8_t sadb_sens_integ_len;
  7899. + uint32_t sadb_sens_reserved;
  7900. +};
  7901. +
  7902. +struct sadb_prop {
  7903. + uint16_t sadb_prop_len;
  7904. + uint16_t sadb_prop_exttype;
  7905. + uint8_t sadb_prop_replay;
  7906. + uint8_t sadb_prop_reserved[3];
  7907. +};
  7908. +
  7909. +struct sadb_comb {
  7910. + uint8_t sadb_comb_auth;
  7911. + uint8_t sadb_comb_encrypt;
  7912. + uint16_t sadb_comb_flags;
  7913. + uint16_t sadb_comb_auth_minbits;
  7914. + uint16_t sadb_comb_auth_maxbits;
  7915. + uint16_t sadb_comb_encrypt_minbits;
  7916. + uint16_t sadb_comb_encrypt_maxbits;
  7917. + uint32_t sadb_comb_reserved;
  7918. + uint32_t sadb_comb_soft_allocations;
  7919. + uint32_t sadb_comb_hard_allocations;
  7920. + uint64_t sadb_comb_soft_bytes;
  7921. + uint64_t sadb_comb_hard_bytes;
  7922. + uint64_t sadb_comb_soft_addtime;
  7923. + uint64_t sadb_comb_hard_addtime;
  7924. + uint64_t sadb_comb_soft_usetime;
  7925. + uint64_t sadb_comb_hard_usetime;
  7926. + uint32_t sadb_x_comb_soft_packets;
  7927. + uint32_t sadb_x_comb_hard_packets;
  7928. +};
  7929. +
  7930. +struct sadb_supported {
  7931. + uint16_t sadb_supported_len;
  7932. + uint16_t sadb_supported_exttype;
  7933. + uint32_t sadb_supported_reserved;
  7934. +};
  7935. +
  7936. +struct sadb_alg {
  7937. + uint8_t sadb_alg_id;
  7938. + uint8_t sadb_alg_ivlen;
  7939. + uint16_t sadb_alg_minbits;
  7940. + uint16_t sadb_alg_maxbits;
  7941. + uint16_t sadb_alg_reserved;
  7942. +};
  7943. +
  7944. +struct sadb_spirange {
  7945. + uint16_t sadb_spirange_len;
  7946. + uint16_t sadb_spirange_exttype;
  7947. + uint32_t sadb_spirange_min;
  7948. + uint32_t sadb_spirange_max;
  7949. + uint32_t sadb_spirange_reserved;
  7950. +};
  7951. +
  7952. +struct sadb_x_kmprivate {
  7953. + uint16_t sadb_x_kmprivate_len;
  7954. + uint16_t sadb_x_kmprivate_exttype;
  7955. + uint32_t sadb_x_kmprivate_reserved;
  7956. +};
  7957. +
  7958. +struct sadb_x_satype {
  7959. + uint16_t sadb_x_satype_len;
  7960. + uint16_t sadb_x_satype_exttype;
  7961. + uint8_t sadb_x_satype_satype;
  7962. + uint8_t sadb_x_satype_reserved[3];
  7963. +};
  7964. +
  7965. +struct sadb_x_policy {
  7966. + uint16_t sadb_x_policy_len;
  7967. + uint16_t sadb_x_policy_exttype;
  7968. + uint16_t sadb_x_policy_type;
  7969. + uint8_t sadb_x_policy_dir;
  7970. + uint8_t sadb_x_policy_reserved;
  7971. + uint32_t sadb_x_policy_id;
  7972. + uint32_t sadb_x_policy_reserved2;
  7973. +};
  7974. +
  7975. +struct sadb_x_debug {
  7976. + uint16_t sadb_x_debug_len;
  7977. + uint16_t sadb_x_debug_exttype;
  7978. + uint32_t sadb_x_debug_tunnel;
  7979. + uint32_t sadb_x_debug_netlink;
  7980. + uint32_t sadb_x_debug_xform;
  7981. + uint32_t sadb_x_debug_eroute;
  7982. + uint32_t sadb_x_debug_spi;
  7983. + uint32_t sadb_x_debug_radij;
  7984. + uint32_t sadb_x_debug_esp;
  7985. + uint32_t sadb_x_debug_ah;
  7986. + uint32_t sadb_x_debug_rcv;
  7987. + uint32_t sadb_x_debug_pfkey;
  7988. + uint32_t sadb_x_debug_ipcomp;
  7989. + uint32_t sadb_x_debug_verbose;
  7990. + uint8_t sadb_x_debug_reserved[4];
  7991. +};
  7992. +
  7993. +struct sadb_x_nat_t_type {
  7994. + uint16_t sadb_x_nat_t_type_len;
  7995. + uint16_t sadb_x_nat_t_type_exttype;
  7996. + uint8_t sadb_x_nat_t_type_type;
  7997. + uint8_t sadb_x_nat_t_type_reserved[3];
  7998. +};
  7999. +struct sadb_x_nat_t_port {
  8000. + uint16_t sadb_x_nat_t_port_len;
  8001. + uint16_t sadb_x_nat_t_port_exttype;
  8002. + uint16_t sadb_x_nat_t_port_port;
  8003. + uint16_t sadb_x_nat_t_port_reserved;
  8004. +};
  8005. +
  8006. +/*
  8007. + * A protocol structure for passing through the transport level
  8008. + * protocol. It contains more fields than are actually used/needed
  8009. + * but it is this way to be compatible with the structure used in
  8010. + * OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/sys/net/pfkeyv2.h)
  8011. + */
  8012. +struct sadb_protocol {
  8013. + uint16_t sadb_protocol_len;
  8014. + uint16_t sadb_protocol_exttype;
  8015. + uint8_t sadb_protocol_proto;
  8016. + uint8_t sadb_protocol_direction;
  8017. + uint8_t sadb_protocol_flags;
  8018. + uint8_t sadb_protocol_reserved2;
  8019. +};
  8020. +
  8021. +#define SADB_EXT_RESERVED 0
  8022. +#define SADB_EXT_SA 1
  8023. +#define SADB_EXT_LIFETIME_CURRENT 2
  8024. +#define SADB_EXT_LIFETIME_HARD 3
  8025. +#define SADB_EXT_LIFETIME_SOFT 4
  8026. +#define SADB_EXT_ADDRESS_SRC 5
  8027. +#define SADB_EXT_ADDRESS_DST 6
  8028. +#define SADB_EXT_ADDRESS_PROXY 7
  8029. +#define SADB_EXT_KEY_AUTH 8
  8030. +#define SADB_EXT_KEY_ENCRYPT 9
  8031. +#define SADB_EXT_IDENTITY_SRC 10
  8032. +#define SADB_EXT_IDENTITY_DST 11
  8033. +#define SADB_EXT_SENSITIVITY 12
  8034. +#define SADB_EXT_PROPOSAL 13
  8035. +#define SADB_EXT_SUPPORTED_AUTH 14
  8036. +#define SADB_EXT_SUPPORTED_ENCRYPT 15
  8037. +#define SADB_EXT_SPIRANGE 16
  8038. +#define SADB_X_EXT_KMPRIVATE 17
  8039. +#define SADB_X_EXT_SATYPE2 18
  8040. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8041. +#define SADB_X_EXT_POLICY 18
  8042. +#endif
  8043. +#define SADB_X_EXT_SA2 19
  8044. +#define SADB_X_EXT_ADDRESS_DST2 20
  8045. +#define SADB_X_EXT_ADDRESS_SRC_FLOW 21
  8046. +#define SADB_X_EXT_ADDRESS_DST_FLOW 22
  8047. +#define SADB_X_EXT_ADDRESS_SRC_MASK 23
  8048. +#define SADB_X_EXT_ADDRESS_DST_MASK 24
  8049. +#define SADB_X_EXT_DEBUG 25
  8050. +#define SADB_X_EXT_PROTOCOL 26
  8051. +#define SADB_X_EXT_NAT_T_TYPE 27
  8052. +#define SADB_X_EXT_NAT_T_SPORT 28
  8053. +#define SADB_X_EXT_NAT_T_DPORT 29
  8054. +#define SADB_X_EXT_NAT_T_OA 30
  8055. +#define SADB_EXT_MAX 30
  8056. +
  8057. +/* SADB_X_DELFLOW required over and above SADB_X_SAFLAGS_CLEARFLOW */
  8058. +#define SADB_X_EXT_ADDRESS_DELFLOW \
  8059. + ( (1<<SADB_X_EXT_ADDRESS_SRC_FLOW) \
  8060. + | (1<<SADB_X_EXT_ADDRESS_DST_FLOW) \
  8061. + | (1<<SADB_X_EXT_ADDRESS_SRC_MASK) \
  8062. + | (1<<SADB_X_EXT_ADDRESS_DST_MASK))
  8063. +
  8064. +#define SADB_SATYPE_UNSPEC 0
  8065. +#define SADB_SATYPE_AH 2
  8066. +#define SADB_SATYPE_ESP 3
  8067. +#define SADB_SATYPE_RSVP 5
  8068. +#define SADB_SATYPE_OSPFV2 6
  8069. +#define SADB_SATYPE_RIPV2 7
  8070. +#define SADB_SATYPE_MIP 8
  8071. +#define SADB_X_SATYPE_IPIP 9
  8072. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8073. +#define SADB_X_SATYPE_IPCOMP 9 /* ICK! */
  8074. +#endif
  8075. +#define SADB_X_SATYPE_COMP 10
  8076. +#define SADB_X_SATYPE_INT 11
  8077. +#define SADB_SATYPE_MAX 11
  8078. +
  8079. +enum sadb_sastate {
  8080. + SADB_SASTATE_LARVAL=0,
  8081. + SADB_SASTATE_MATURE=1,
  8082. + SADB_SASTATE_DYING=2,
  8083. + SADB_SASTATE_DEAD=3
  8084. +};
  8085. +#define SADB_SASTATE_MAX 3
  8086. +
  8087. +#define SADB_SAFLAGS_PFS 1
  8088. +#define SADB_X_SAFLAGS_REPLACEFLOW 2
  8089. +#define SADB_X_SAFLAGS_CLEARFLOW 4
  8090. +#define SADB_X_SAFLAGS_INFLOW 8
  8091. +
  8092. +/* not obvious, but these are the same values as used in isakmp,
  8093. + * and in freeswan/ipsec_policy.h. If you need to add any, they
  8094. + * should be added as according to
  8095. + * http://www.iana.org/assignments/isakmp-registry
  8096. + *
  8097. + * and if not, then please try to use a private-use value, and
  8098. + * consider asking IANA to assign a value.
  8099. + */
  8100. +#define SADB_AALG_NONE 0
  8101. +#define SADB_AALG_MD5HMAC 2
  8102. +#define SADB_AALG_SHA1HMAC 3
  8103. +#define SADB_X_AALG_SHA2_256HMAC 5
  8104. +#define SADB_X_AALG_SHA2_384HMAC 6
  8105. +#define SADB_X_AALG_SHA2_512HMAC 7
  8106. +#define SADB_X_AALG_RIPEMD160HMAC 8
  8107. +#define SADB_X_AALG_NULL 251 /* kame */
  8108. +#define SADB_AALG_MAX 251
  8109. +
  8110. +#define SADB_EALG_NONE 0
  8111. +#define SADB_EALG_DESCBC 2
  8112. +#define SADB_EALG_3DESCBC 3
  8113. +#define SADB_X_EALG_CASTCBC 6
  8114. +#define SADB_X_EALG_BLOWFISHCBC 7
  8115. +#define SADB_EALG_NULL 11
  8116. +#define SADB_X_EALG_AESCBC 12
  8117. +#define SADB_EALG_MAX 255
  8118. +
  8119. +#define SADB_X_CALG_NONE 0
  8120. +#define SADB_X_CALG_OUI 1
  8121. +#define SADB_X_CALG_DEFLATE 2
  8122. +#define SADB_X_CALG_LZS 3
  8123. +#define SADB_X_CALG_V42BIS 4
  8124. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8125. +#define SADB_X_CALG_LZJH 4
  8126. +#endif
  8127. +#define SADB_X_CALG_MAX 4
  8128. +
  8129. +#define SADB_X_TALG_NONE 0
  8130. +#define SADB_X_TALG_IPv4_in_IPv4 1
  8131. +#define SADB_X_TALG_IPv6_in_IPv4 2
  8132. +#define SADB_X_TALG_IPv4_in_IPv6 3
  8133. +#define SADB_X_TALG_IPv6_in_IPv6 4
  8134. +#define SADB_X_TALG_MAX 4
  8135. +
  8136. +
  8137. +#define SADB_IDENTTYPE_RESERVED 0
  8138. +#define SADB_IDENTTYPE_PREFIX 1
  8139. +#define SADB_IDENTTYPE_FQDN 2
  8140. +#define SADB_IDENTTYPE_USERFQDN 3
  8141. +#define SADB_X_IDENTTYPE_CONNECTION 4
  8142. +#define SADB_IDENTTYPE_MAX 4
  8143. +
  8144. +#define SADB_KEY_FLAGS_MAX 0
  8145. +#endif /* __PFKEY_V2_H */
  8146. +
  8147. +/*
  8148. + * $Log: pfkeyv2.h,v $
  8149. + * Revision 1.31 2005/04/14 01:14:54 mcr
  8150. + * change sadb_state to an enum.
  8151. + *
  8152. + * Revision 1.30 2004/04/06 02:49:00 mcr
  8153. + * pullup of algo code from alg-branch.
  8154. + *
  8155. + * Revision 1.29 2003/12/22 21:35:58 mcr
  8156. + * new patches from Dr{Who}.
  8157. + *
  8158. + * Revision 1.28 2003/12/22 19:33:15 mcr
  8159. + * added 0.6c NAT-T patch.
  8160. + *
  8161. + * Revision 1.27 2003/12/10 01:20:01 mcr
  8162. + * NAT-traversal patches to KLIPS.
  8163. + *
  8164. + * Revision 1.26 2003/10/31 02:26:44 mcr
  8165. + * pulled up port-selector patches.
  8166. + *
  8167. + * Revision 1.25.4.1 2003/09/21 13:59:34 mcr
  8168. + * pre-liminary X.509 patch - does not yet pass tests.
  8169. + *
  8170. + * Revision 1.25 2003/07/31 23:59:17 mcr
  8171. + * re-introduce kernel 2.6 duplicate values for now.
  8172. + * hope to get them changed!
  8173. + *
  8174. + * Revision 1.24 2003/07/31 22:55:27 mcr
  8175. + * added some definitions to keep pfkeyv2.h files in sync.
  8176. + *
  8177. + * Revision 1.23 2003/05/11 00:43:48 mcr
  8178. + * added comment about origin of values used
  8179. + *
  8180. + * Revision 1.22 2003/01/30 02:31:34 rgb
  8181. + *
  8182. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  8183. + *
  8184. + * Revision 1.21 2002/12/16 19:26:49 mcr
  8185. + * added definition of FS 1.xx sadb structure
  8186. + *
  8187. + * Revision 1.20 2002/09/20 15:40:25 rgb
  8188. + * Added sadb_x_sa_ref to struct sadb_sa.
  8189. + *
  8190. + * Revision 1.19 2002/04/24 07:36:49 mcr
  8191. + * Moved from ./lib/pfkeyv2.h,v
  8192. + *
  8193. + * Revision 1.18 2001/11/06 19:47:47 rgb
  8194. + * Added packet parameter to lifetime and comb structures.
  8195. + *
  8196. + * Revision 1.17 2001/09/08 21:13:35 rgb
  8197. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  8198. + *
  8199. + * Revision 1.16 2001/07/06 19:49:46 rgb
  8200. + * Added SADB_X_SAFLAGS_INFLOW for supporting incoming policy checks.
  8201. + *
  8202. + * Revision 1.15 2001/02/26 20:00:43 rgb
  8203. + * Added internal IP protocol 61 for magic SAs.
  8204. + *
  8205. + * Revision 1.14 2001/02/08 18:51:05 rgb
  8206. + * Include RFC document title and appendix subsection title.
  8207. + *
  8208. + * Revision 1.13 2000/10/10 20:10:20 rgb
  8209. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  8210. + *
  8211. + * Revision 1.12 2000/09/15 06:41:50 rgb
  8212. + * Added V42BIS constant.
  8213. + *
  8214. + * Revision 1.11 2000/09/12 22:35:37 rgb
  8215. + * Restructured to remove unused extensions from CLEARFLOW messages.
  8216. + *
  8217. + * Revision 1.10 2000/09/12 18:50:09 rgb
  8218. + * Added IPIP tunnel types as algo support.
  8219. + *
  8220. + * Revision 1.9 2000/08/21 16:47:19 rgb
  8221. + * Added SADB_X_CALG_* macros for IPCOMP.
  8222. + *
  8223. + * Revision 1.8 2000/08/09 20:43:34 rgb
  8224. + * Fixed bitmask value for SADB_X_SAFLAGS_CLEAREROUTE.
  8225. + *
  8226. + * Revision 1.7 2000/01/21 06:28:37 rgb
  8227. + * Added flow add/delete message type macros.
  8228. + * Added flow address extension type macros.
  8229. + * Tidied up spacing.
  8230. + * Added klipsdebug switching capability.
  8231. + *
  8232. + * Revision 1.6 1999/11/27 11:56:08 rgb
  8233. + * Add SADB_X_SATYPE_COMP for compression, eventually.
  8234. + *
  8235. + * Revision 1.5 1999/11/23 22:23:16 rgb
  8236. + * This file has been moved in the distribution from klips/net/ipsec to
  8237. + * lib.
  8238. + *
  8239. + * Revision 1.4 1999/04/29 15:23:29 rgb
  8240. + * Add GRPSA support.
  8241. + * Add support for a second SATYPE, SA and DST_ADDRESS.
  8242. + * Add IPPROTO_IPIP support.
  8243. + *
  8244. + * Revision 1.3 1999/04/15 17:58:08 rgb
  8245. + * Add RCSID labels.
  8246. + *
  8247. + */
  8248. --- /dev/null Tue Mar 11 13:02:56 2003
  8249. +++ linux/include/zlib/zconf.h Mon Feb 9 13:51:03 2004
  8250. @@ -0,0 +1,309 @@
  8251. +/* zconf.h -- configuration of the zlib compression library
  8252. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  8253. + * For conditions of distribution and use, see copyright notice in zlib.h
  8254. + */
  8255. +
  8256. +/* @(#) $Id: zconf.h,v 1.4 2004/07/10 07:48:40 mcr Exp $ */
  8257. +
  8258. +#ifndef _ZCONF_H
  8259. +#define _ZCONF_H
  8260. +
  8261. +/*
  8262. + * If you *really* need a unique prefix for all types and library functions,
  8263. + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
  8264. + */
  8265. +#ifdef IPCOMP_PREFIX
  8266. +# define deflateInit_ ipcomp_deflateInit_
  8267. +# define deflate ipcomp_deflate
  8268. +# define deflateEnd ipcomp_deflateEnd
  8269. +# define inflateInit_ ipcomp_inflateInit_
  8270. +# define inflate ipcomp_inflate
  8271. +# define inflateEnd ipcomp_inflateEnd
  8272. +# define deflateInit2_ ipcomp_deflateInit2_
  8273. +# define deflateSetDictionary ipcomp_deflateSetDictionary
  8274. +# define deflateCopy ipcomp_deflateCopy
  8275. +# define deflateReset ipcomp_deflateReset
  8276. +# define deflateParams ipcomp_deflateParams
  8277. +# define inflateInit2_ ipcomp_inflateInit2_
  8278. +# define inflateSetDictionary ipcomp_inflateSetDictionary
  8279. +# define inflateSync ipcomp_inflateSync
  8280. +# define inflateSyncPoint ipcomp_inflateSyncPoint
  8281. +# define inflateReset ipcomp_inflateReset
  8282. +# define compress ipcomp_compress
  8283. +# define compress2 ipcomp_compress2
  8284. +# define uncompress ipcomp_uncompress
  8285. +# define adler32 ipcomp_adler32
  8286. +# define crc32 ipcomp_crc32
  8287. +# define get_crc_table ipcomp_get_crc_table
  8288. +/* SSS: these also need to be prefixed to avoid clash with ppp_deflate and ext2compression */
  8289. +# define inflate_blocks ipcomp_deflate_blocks
  8290. +# define inflate_blocks_free ipcomp_deflate_blocks_free
  8291. +# define inflate_blocks_new ipcomp_inflate_blocks_new
  8292. +# define inflate_blocks_reset ipcomp_inflate_blocks_reset
  8293. +# define inflate_blocks_sync_point ipcomp_inflate_blocks_sync_point
  8294. +# define inflate_set_dictionary ipcomp_inflate_set_dictionary
  8295. +# define inflate_codes ipcomp_inflate_codes
  8296. +# define inflate_codes_free ipcomp_inflate_codes_free
  8297. +# define inflate_codes_new ipcomp_inflate_codes_new
  8298. +# define inflate_fast ipcomp_inflate_fast
  8299. +# define inflate_trees_bits ipcomp_inflate_trees_bits
  8300. +# define inflate_trees_dynamic ipcomp_inflate_trees_dynamic
  8301. +# define inflate_trees_fixed ipcomp_inflate_trees_fixed
  8302. +# define inflate_flush ipcomp_inflate_flush
  8303. +# define inflate_mask ipcomp_inflate_mask
  8304. +# define _dist_code _ipcomp_dist_code
  8305. +# define _length_code _ipcomp_length_code
  8306. +# define _tr_align _ipcomp_tr_align
  8307. +# define _tr_flush_block _ipcomp_tr_flush_block
  8308. +# define _tr_init _ipcomp_tr_init
  8309. +# define _tr_stored_block _ipcomp_tr_stored_block
  8310. +# define _tr_tally _ipcomp_tr_tally
  8311. +# define zError ipcomp_zError
  8312. +# define z_errmsg ipcomp_z_errmsg
  8313. +# define zlibVersion ipcomp_zlibVersion
  8314. +# define match_init ipcomp_match_init
  8315. +# define longest_match ipcomp_longest_match
  8316. +#endif
  8317. +
  8318. +#ifdef Z_PREFIX
  8319. +# define Byte z_Byte
  8320. +# define uInt z_uInt
  8321. +# define uLong z_uLong
  8322. +# define Bytef z_Bytef
  8323. +# define charf z_charf
  8324. +# define intf z_intf
  8325. +# define uIntf z_uIntf
  8326. +# define uLongf z_uLongf
  8327. +# define voidpf z_voidpf
  8328. +# define voidp z_voidp
  8329. +#endif
  8330. +
  8331. +#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  8332. +# define WIN32
  8333. +#endif
  8334. +#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
  8335. +# ifndef __32BIT__
  8336. +# define __32BIT__
  8337. +# endif
  8338. +#endif
  8339. +#if defined(__MSDOS__) && !defined(MSDOS)
  8340. +# define MSDOS
  8341. +#endif
  8342. +
  8343. +/*
  8344. + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  8345. + * than 64k bytes at a time (needed on systems with 16-bit int).
  8346. + */
  8347. +#if defined(MSDOS) && !defined(__32BIT__)
  8348. +# define MAXSEG_64K
  8349. +#endif
  8350. +#ifdef MSDOS
  8351. +# define UNALIGNED_OK
  8352. +#endif
  8353. +
  8354. +#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
  8355. +# define STDC
  8356. +#endif
  8357. +#if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
  8358. +# ifndef STDC
  8359. +# define STDC
  8360. +# endif
  8361. +#endif
  8362. +
  8363. +#ifndef STDC
  8364. +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
  8365. +# define const
  8366. +# endif
  8367. +#endif
  8368. +
  8369. +/* Some Mac compilers merge all .h files incorrectly: */
  8370. +#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
  8371. +# define NO_DUMMY_DECL
  8372. +#endif
  8373. +
  8374. +/* Old Borland C incorrectly complains about missing returns: */
  8375. +#if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
  8376. +# define NEED_DUMMY_RETURN
  8377. +#endif
  8378. +
  8379. +
  8380. +/* Maximum value for memLevel in deflateInit2 */
  8381. +#ifndef MAX_MEM_LEVEL
  8382. +# ifdef MAXSEG_64K
  8383. +# define MAX_MEM_LEVEL 8
  8384. +# else
  8385. +# define MAX_MEM_LEVEL 9
  8386. +# endif
  8387. +#endif
  8388. +
  8389. +/* Maximum value for windowBits in deflateInit2 and inflateInit2.
  8390. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
  8391. + * created by gzip. (Files created by minigzip can still be extracted by
  8392. + * gzip.)
  8393. + */
  8394. +#ifndef MAX_WBITS
  8395. +# define MAX_WBITS 15 /* 32K LZ77 window */
  8396. +#endif
  8397. +
  8398. +/* The memory requirements for deflate are (in bytes):
  8399. + (1 << (windowBits+2)) + (1 << (memLevel+9))
  8400. + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  8401. + plus a few kilobytes for small objects. For example, if you want to reduce
  8402. + the default memory requirements from 256K to 128K, compile with
  8403. + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
  8404. + Of course this will generally degrade compression (there's no free lunch).
  8405. +
  8406. + The memory requirements for inflate are (in bytes) 1 << windowBits
  8407. + that is, 32K for windowBits=15 (default value) plus a few kilobytes
  8408. + for small objects.
  8409. +*/
  8410. +
  8411. + /* Type declarations */
  8412. +
  8413. +#ifndef OF /* function prototypes */
  8414. +# ifdef STDC
  8415. +# define OF(args) args
  8416. +# else
  8417. +# define OF(args) ()
  8418. +# endif
  8419. +#endif
  8420. +
  8421. +/* The following definitions for FAR are needed only for MSDOS mixed
  8422. + * model programming (small or medium model with some far allocations).
  8423. + * This was tested only with MSC; for other MSDOS compilers you may have
  8424. + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
  8425. + * just define FAR to be empty.
  8426. + */
  8427. +#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
  8428. + /* MSC small or medium model */
  8429. +# define SMALL_MEDIUM
  8430. +# ifdef _MSC_VER
  8431. +# define FAR _far
  8432. +# else
  8433. +# define FAR far
  8434. +# endif
  8435. +#endif
  8436. +#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
  8437. +# ifndef __32BIT__
  8438. +# define SMALL_MEDIUM
  8439. +# define FAR _far
  8440. +# endif
  8441. +#endif
  8442. +
  8443. +/* Compile with -DZLIB_DLL for Windows DLL support */
  8444. +#if defined(ZLIB_DLL)
  8445. +# if defined(_WINDOWS) || defined(WINDOWS)
  8446. +# ifdef FAR
  8447. +# undef FAR
  8448. +# endif
  8449. +# include <windows.h>
  8450. +# define ZEXPORT WINAPI
  8451. +# ifdef WIN32
  8452. +# define ZEXPORTVA WINAPIV
  8453. +# else
  8454. +# define ZEXPORTVA FAR _cdecl _export
  8455. +# endif
  8456. +# endif
  8457. +# if defined (__BORLANDC__)
  8458. +# if (__BORLANDC__ >= 0x0500) && defined (WIN32)
  8459. +# include <windows.h>
  8460. +# define ZEXPORT __declspec(dllexport) WINAPI
  8461. +# define ZEXPORTRVA __declspec(dllexport) WINAPIV
  8462. +# else
  8463. +# if defined (_Windows) && defined (__DLL__)
  8464. +# define ZEXPORT _export
  8465. +# define ZEXPORTVA _export
  8466. +# endif
  8467. +# endif
  8468. +# endif
  8469. +#endif
  8470. +
  8471. +#if defined (__BEOS__)
  8472. +# if defined (ZLIB_DLL)
  8473. +# define ZEXTERN extern __declspec(dllexport)
  8474. +# else
  8475. +# define ZEXTERN extern __declspec(dllimport)
  8476. +# endif
  8477. +#endif
  8478. +
  8479. +#ifndef ZEXPORT
  8480. +# define ZEXPORT
  8481. +#endif
  8482. +#ifndef ZEXPORTVA
  8483. +# define ZEXPORTVA
  8484. +#endif
  8485. +#ifndef ZEXTERN
  8486. +# define ZEXTERN extern
  8487. +#endif
  8488. +
  8489. +#ifndef FAR
  8490. +# define FAR
  8491. +#endif
  8492. +
  8493. +#if !defined(MACOS) && !defined(TARGET_OS_MAC)
  8494. +typedef unsigned char Byte; /* 8 bits */
  8495. +#endif
  8496. +typedef unsigned int uInt; /* 16 bits or more */
  8497. +typedef unsigned long uLong; /* 32 bits or more */
  8498. +
  8499. +#ifdef SMALL_MEDIUM
  8500. + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
  8501. +# define Bytef Byte FAR
  8502. +#else
  8503. + typedef Byte FAR Bytef;
  8504. +#endif
  8505. +typedef char FAR charf;
  8506. +typedef int FAR intf;
  8507. +typedef uInt FAR uIntf;
  8508. +typedef uLong FAR uLongf;
  8509. +
  8510. +#ifdef STDC
  8511. + typedef void FAR *voidpf;
  8512. + typedef void *voidp;
  8513. +#else
  8514. + typedef Byte FAR *voidpf;
  8515. + typedef Byte *voidp;
  8516. +#endif
  8517. +
  8518. +#ifdef HAVE_UNISTD_H
  8519. +# include <sys/types.h> /* for off_t */
  8520. +# include <unistd.h> /* for SEEK_* and off_t */
  8521. +# define z_off_t off_t
  8522. +#endif
  8523. +#ifndef SEEK_SET
  8524. +# define SEEK_SET 0 /* Seek from beginning of file. */
  8525. +# define SEEK_CUR 1 /* Seek from current position. */
  8526. +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
  8527. +#endif
  8528. +#ifndef z_off_t
  8529. +# define z_off_t long
  8530. +#endif
  8531. +
  8532. +/* MVS linker does not support external names larger than 8 bytes */
  8533. +#if defined(__MVS__)
  8534. +# pragma map(deflateInit_,"DEIN")
  8535. +# pragma map(deflateInit2_,"DEIN2")
  8536. +# pragma map(deflateEnd,"DEEND")
  8537. +# pragma map(inflateInit_,"ININ")
  8538. +# pragma map(inflateInit2_,"ININ2")
  8539. +# pragma map(inflateEnd,"INEND")
  8540. +# pragma map(inflateSync,"INSY")
  8541. +# pragma map(inflateSetDictionary,"INSEDI")
  8542. +# pragma map(inflate_blocks,"INBL")
  8543. +# pragma map(inflate_blocks_new,"INBLNE")
  8544. +# pragma map(inflate_blocks_free,"INBLFR")
  8545. +# pragma map(inflate_blocks_reset,"INBLRE")
  8546. +# pragma map(inflate_codes_free,"INCOFR")
  8547. +# pragma map(inflate_codes,"INCO")
  8548. +# pragma map(inflate_fast,"INFA")
  8549. +# pragma map(inflate_flush,"INFLU")
  8550. +# pragma map(inflate_mask,"INMA")
  8551. +# pragma map(inflate_set_dictionary,"INSEDI2")
  8552. +# pragma map(ipcomp_inflate_copyright,"INCOPY")
  8553. +# pragma map(inflate_trees_bits,"INTRBI")
  8554. +# pragma map(inflate_trees_dynamic,"INTRDY")
  8555. +# pragma map(inflate_trees_fixed,"INTRFI")
  8556. +# pragma map(inflate_trees_free,"INTRFR")
  8557. +#endif
  8558. +
  8559. +#endif /* _ZCONF_H */
  8560. --- /dev/null Tue Mar 11 13:02:56 2003
  8561. +++ linux/include/zlib/zlib.h Mon Feb 9 13:51:03 2004
  8562. @@ -0,0 +1,893 @@
  8563. +/* zlib.h -- interface of the 'zlib' general purpose compression library
  8564. + version 1.1.4, March 11th, 2002
  8565. +
  8566. + Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
  8567. +
  8568. + This software is provided 'as-is', without any express or implied
  8569. + warranty. In no event will the authors be held liable for any damages
  8570. + arising from the use of this software.
  8571. +
  8572. + Permission is granted to anyone to use this software for any purpose,
  8573. + including commercial applications, and to alter it and redistribute it
  8574. + freely, subject to the following restrictions:
  8575. +
  8576. + 1. The origin of this software must not be misrepresented; you must not
  8577. + claim that you wrote the original software. If you use this software
  8578. + in a product, an acknowledgment in the product documentation would be
  8579. + appreciated but is not required.
  8580. + 2. Altered source versions must be plainly marked as such, and must not be
  8581. + misrepresented as being the original software.
  8582. + 3. This notice may not be removed or altered from any source distribution.
  8583. +
  8584. + Jean-loup Gailly Mark Adler
  8585. + jloup@gzip.org madler@alumni.caltech.edu
  8586. +
  8587. +
  8588. + The data format used by the zlib library is described by RFCs (Request for
  8589. + Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  8590. + (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  8591. +*/
  8592. +
  8593. +#ifndef _ZLIB_H
  8594. +#define _ZLIB_H
  8595. +
  8596. +#include "zconf.h"
  8597. +
  8598. +#ifdef __cplusplus
  8599. +extern "C" {
  8600. +#endif
  8601. +
  8602. +#define ZLIB_VERSION "1.1.4"
  8603. +
  8604. +/*
  8605. + The 'zlib' compression library provides in-memory compression and
  8606. + decompression functions, including integrity checks of the uncompressed
  8607. + data. This version of the library supports only one compression method
  8608. + (deflation) but other algorithms will be added later and will have the same
  8609. + stream interface.
  8610. +
  8611. + Compression can be done in a single step if the buffers are large
  8612. + enough (for example if an input file is mmap'ed), or can be done by
  8613. + repeated calls of the compression function. In the latter case, the
  8614. + application must provide more input and/or consume the output
  8615. + (providing more output space) before each call.
  8616. +
  8617. + The library also supports reading and writing files in gzip (.gz) format
  8618. + with an interface similar to that of stdio.
  8619. +
  8620. + The library does not install any signal handler. The decoder checks
  8621. + the consistency of the compressed data, so the library should never
  8622. + crash even in case of corrupted input.
  8623. +*/
  8624. +
  8625. +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
  8626. +typedef void (*free_func) OF((voidpf opaque, voidpf address));
  8627. +
  8628. +struct internal_state;
  8629. +
  8630. +typedef struct z_stream_s {
  8631. + Bytef *next_in; /* next input byte */
  8632. + uInt avail_in; /* number of bytes available at next_in */
  8633. + uLong total_in; /* total nb of input bytes read so far */
  8634. +
  8635. + Bytef *next_out; /* next output byte should be put there */
  8636. + uInt avail_out; /* remaining free space at next_out */
  8637. + uLong total_out; /* total nb of bytes output so far */
  8638. +
  8639. + const char *msg; /* last error message, NULL if no error */
  8640. + struct internal_state FAR *state; /* not visible by applications */
  8641. +
  8642. + alloc_func zalloc; /* used to allocate the internal state */
  8643. + free_func zfree; /* used to free the internal state */
  8644. + voidpf opaque; /* private data object passed to zalloc and zfree */
  8645. +
  8646. + int data_type; /* best guess about the data type: ascii or binary */
  8647. + uLong adler; /* adler32 value of the uncompressed data */
  8648. + uLong reserved; /* reserved for future use */
  8649. +} z_stream;
  8650. +
  8651. +typedef z_stream FAR *z_streamp;
  8652. +
  8653. +/*
  8654. + The application must update next_in and avail_in when avail_in has
  8655. + dropped to zero. It must update next_out and avail_out when avail_out
  8656. + has dropped to zero. The application must initialize zalloc, zfree and
  8657. + opaque before calling the init function. All other fields are set by the
  8658. + compression library and must not be updated by the application.
  8659. +
  8660. + The opaque value provided by the application will be passed as the first
  8661. + parameter for calls of zalloc and zfree. This can be useful for custom
  8662. + memory management. The compression library attaches no meaning to the
  8663. + opaque value.
  8664. +
  8665. + zalloc must return Z_NULL if there is not enough memory for the object.
  8666. + If zlib is used in a multi-threaded application, zalloc and zfree must be
  8667. + thread safe.
  8668. +
  8669. + On 16-bit systems, the functions zalloc and zfree must be able to allocate
  8670. + exactly 65536 bytes, but will not be required to allocate more than this
  8671. + if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  8672. + pointers returned by zalloc for objects of exactly 65536 bytes *must*
  8673. + have their offset normalized to zero. The default allocation function
  8674. + provided by this library ensures this (see zutil.c). To reduce memory
  8675. + requirements and avoid any allocation of 64K objects, at the expense of
  8676. + compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  8677. +
  8678. + The fields total_in and total_out can be used for statistics or
  8679. + progress reports. After compression, total_in holds the total size of
  8680. + the uncompressed data and may be saved for use in the decompressor
  8681. + (particularly if the decompressor wants to decompress everything in
  8682. + a single step).
  8683. +*/
  8684. +
  8685. + /* constants */
  8686. +
  8687. +#define Z_NO_FLUSH 0
  8688. +#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
  8689. +#define Z_SYNC_FLUSH 2
  8690. +#define Z_FULL_FLUSH 3
  8691. +#define Z_FINISH 4
  8692. +/* Allowed flush values; see deflate() below for details */
  8693. +
  8694. +#define Z_OK 0
  8695. +#define Z_STREAM_END 1
  8696. +#define Z_NEED_DICT 2
  8697. +#define Z_ERRNO (-1)
  8698. +#define Z_STREAM_ERROR (-2)
  8699. +#define Z_DATA_ERROR (-3)
  8700. +#define Z_MEM_ERROR (-4)
  8701. +#define Z_BUF_ERROR (-5)
  8702. +#define Z_VERSION_ERROR (-6)
  8703. +/* Return codes for the compression/decompression functions. Negative
  8704. + * values are errors, positive values are used for special but normal events.
  8705. + */
  8706. +
  8707. +#define Z_NO_COMPRESSION 0
  8708. +#define Z_BEST_SPEED 1
  8709. +#define Z_BEST_COMPRESSION 9
  8710. +#define Z_DEFAULT_COMPRESSION (-1)
  8711. +/* compression levels */
  8712. +
  8713. +#define Z_FILTERED 1
  8714. +#define Z_HUFFMAN_ONLY 2
  8715. +#define Z_DEFAULT_STRATEGY 0
  8716. +/* compression strategy; see deflateInit2() below for details */
  8717. +
  8718. +#define Z_BINARY 0
  8719. +#define Z_ASCII 1
  8720. +#define Z_UNKNOWN 2
  8721. +/* Possible values of the data_type field */
  8722. +
  8723. +#define Z_DEFLATED 8
  8724. +/* The deflate compression method (the only one supported in this version) */
  8725. +
  8726. +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
  8727. +
  8728. +#define zlib_version zlibVersion()
  8729. +/* for compatibility with versions < 1.0.2 */
  8730. +
  8731. + /* basic functions */
  8732. +
  8733. +ZEXTERN const char * ZEXPORT zlibVersion OF((void));
  8734. +/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
  8735. + If the first character differs, the library code actually used is
  8736. + not compatible with the zlib.h header file used by the application.
  8737. + This check is automatically made by deflateInit and inflateInit.
  8738. + */
  8739. +
  8740. +/*
  8741. +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
  8742. +
  8743. + Initializes the internal stream state for compression. The fields
  8744. + zalloc, zfree and opaque must be initialized before by the caller.
  8745. + If zalloc and zfree are set to Z_NULL, deflateInit updates them to
  8746. + use default allocation functions.
  8747. +
  8748. + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
  8749. + 1 gives best speed, 9 gives best compression, 0 gives no compression at
  8750. + all (the input data is simply copied a block at a time).
  8751. + Z_DEFAULT_COMPRESSION requests a default compromise between speed and
  8752. + compression (currently equivalent to level 6).
  8753. +
  8754. + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  8755. + enough memory, Z_STREAM_ERROR if level is not a valid compression level,
  8756. + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
  8757. + with the version assumed by the caller (ZLIB_VERSION).
  8758. + msg is set to null if there is no error message. deflateInit does not
  8759. + perform any compression: this will be done by deflate().
  8760. +*/
  8761. +
  8762. +
  8763. +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
  8764. +/*
  8765. + deflate compresses as much data as possible, and stops when the input
  8766. + buffer becomes empty or the output buffer becomes full. It may introduce some
  8767. + output latency (reading input without producing any output) except when
  8768. + forced to flush.
  8769. +
  8770. + The detailed semantics are as follows. deflate performs one or both of the
  8771. + following actions:
  8772. +
  8773. + - Compress more input starting at next_in and update next_in and avail_in
  8774. + accordingly. If not all input can be processed (because there is not
  8775. + enough room in the output buffer), next_in and avail_in are updated and
  8776. + processing will resume at this point for the next call of deflate().
  8777. +
  8778. + - Provide more output starting at next_out and update next_out and avail_out
  8779. + accordingly. This action is forced if the parameter flush is non zero.
  8780. + Forcing flush frequently degrades the compression ratio, so this parameter
  8781. + should be set only when necessary (in interactive applications).
  8782. + Some output may be provided even if flush is not set.
  8783. +
  8784. + Before the call of deflate(), the application should ensure that at least
  8785. + one of the actions is possible, by providing more input and/or consuming
  8786. + more output, and updating avail_in or avail_out accordingly; avail_out
  8787. + should never be zero before the call. The application can consume the
  8788. + compressed output when it wants, for example when the output buffer is full
  8789. + (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
  8790. + and with zero avail_out, it must be called again after making room in the
  8791. + output buffer because there might be more output pending.
  8792. +
  8793. + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
  8794. + flushed to the output buffer and the output is aligned on a byte boundary, so
  8795. + that the decompressor can get all input data available so far. (In particular
  8796. + avail_in is zero after the call if enough output space has been provided
  8797. + before the call.) Flushing may degrade compression for some compression
  8798. + algorithms and so it should be used only when necessary.
  8799. +
  8800. + If flush is set to Z_FULL_FLUSH, all output is flushed as with
  8801. + Z_SYNC_FLUSH, and the compression state is reset so that decompression can
  8802. + restart from this point if previous compressed data has been damaged or if
  8803. + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
  8804. + the compression.
  8805. +
  8806. + If deflate returns with avail_out == 0, this function must be called again
  8807. + with the same value of the flush parameter and more output space (updated
  8808. + avail_out), until the flush is complete (deflate returns with non-zero
  8809. + avail_out).
  8810. +
  8811. + If the parameter flush is set to Z_FINISH, pending input is processed,
  8812. + pending output is flushed and deflate returns with Z_STREAM_END if there
  8813. + was enough output space; if deflate returns with Z_OK, this function must be
  8814. + called again with Z_FINISH and more output space (updated avail_out) but no
  8815. + more input data, until it returns with Z_STREAM_END or an error. After
  8816. + deflate has returned Z_STREAM_END, the only possible operations on the
  8817. + stream are deflateReset or deflateEnd.
  8818. +
  8819. + Z_FINISH can be used immediately after deflateInit if all the compression
  8820. + is to be done in a single step. In this case, avail_out must be at least
  8821. + 0.1% larger than avail_in plus 12 bytes. If deflate does not return
  8822. + Z_STREAM_END, then it must be called again as described above.
  8823. +
  8824. + deflate() sets strm->adler to the adler32 checksum of all input read
  8825. + so far (that is, total_in bytes).
  8826. +
  8827. + deflate() may update data_type if it can make a good guess about
  8828. + the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
  8829. + binary. This field is only for information purposes and does not affect
  8830. + the compression algorithm in any manner.
  8831. +
  8832. + deflate() returns Z_OK if some progress has been made (more input
  8833. + processed or more output produced), Z_STREAM_END if all input has been
  8834. + consumed and all output has been produced (only when flush is set to
  8835. + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
  8836. + if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
  8837. + (for example avail_in or avail_out was zero).
  8838. +*/
  8839. +
  8840. +
  8841. +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
  8842. +/*
  8843. + All dynamically allocated data structures for this stream are freed.
  8844. + This function discards any unprocessed input and does not flush any
  8845. + pending output.
  8846. +
  8847. + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
  8848. + stream state was inconsistent, Z_DATA_ERROR if the stream was freed
  8849. + prematurely (some input or output was discarded). In the error case,
  8850. + msg may be set but then points to a static string (which must not be
  8851. + deallocated).
  8852. +*/
  8853. +
  8854. +
  8855. +/*
  8856. +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
  8857. +
  8858. + Initializes the internal stream state for decompression. The fields
  8859. + next_in, avail_in, zalloc, zfree and opaque must be initialized before by
  8860. + the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
  8861. + value depends on the compression method), inflateInit determines the
  8862. + compression method from the zlib header and allocates all data structures
  8863. + accordingly; otherwise the allocation will be deferred to the first call of
  8864. + inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
  8865. + use default allocation functions.
  8866. +
  8867. + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
  8868. + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
  8869. + version assumed by the caller. msg is set to null if there is no error
  8870. + message. inflateInit does not perform any decompression apart from reading
  8871. + the zlib header if present: this will be done by inflate(). (So next_in and
  8872. + avail_in may be modified, but next_out and avail_out are unchanged.)
  8873. +*/
  8874. +
  8875. +
  8876. +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
  8877. +/*
  8878. + inflate decompresses as much data as possible, and stops when the input
  8879. + buffer becomes empty or the output buffer becomes full. It may some
  8880. + introduce some output latency (reading input without producing any output)
  8881. + except when forced to flush.
  8882. +
  8883. + The detailed semantics are as follows. inflate performs one or both of the
  8884. + following actions:
  8885. +
  8886. + - Decompress more input starting at next_in and update next_in and avail_in
  8887. + accordingly. If not all input can be processed (because there is not
  8888. + enough room in the output buffer), next_in is updated and processing
  8889. + will resume at this point for the next call of inflate().
  8890. +
  8891. + - Provide more output starting at next_out and update next_out and avail_out
  8892. + accordingly. inflate() provides as much output as possible, until there
  8893. + is no more input data or no more space in the output buffer (see below
  8894. + about the flush parameter).
  8895. +
  8896. + Before the call of inflate(), the application should ensure that at least
  8897. + one of the actions is possible, by providing more input and/or consuming
  8898. + more output, and updating the next_* and avail_* values accordingly.
  8899. + The application can consume the uncompressed output when it wants, for
  8900. + example when the output buffer is full (avail_out == 0), or after each
  8901. + call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  8902. + must be called again after making room in the output buffer because there
  8903. + might be more output pending.
  8904. +
  8905. + If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
  8906. + output as possible to the output buffer. The flushing behavior of inflate is
  8907. + not specified for values of the flush parameter other than Z_SYNC_FLUSH
  8908. + and Z_FINISH, but the current implementation actually flushes as much output
  8909. + as possible anyway.
  8910. +
  8911. + inflate() should normally be called until it returns Z_STREAM_END or an
  8912. + error. However if all decompression is to be performed in a single step
  8913. + (a single call of inflate), the parameter flush should be set to
  8914. + Z_FINISH. In this case all pending input is processed and all pending
  8915. + output is flushed; avail_out must be large enough to hold all the
  8916. + uncompressed data. (The size of the uncompressed data may have been saved
  8917. + by the compressor for this purpose.) The next operation on this stream must
  8918. + be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  8919. + is never required, but can be used to inform inflate that a faster routine
  8920. + may be used for the single inflate() call.
  8921. +
  8922. + If a preset dictionary is needed at this point (see inflateSetDictionary
  8923. + below), inflate sets strm-adler to the adler32 checksum of the
  8924. + dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
  8925. + it sets strm->adler to the adler32 checksum of all output produced
  8926. + so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
  8927. + an error code as described below. At the end of the stream, inflate()
  8928. + checks that its computed adler32 checksum is equal to that saved by the
  8929. + compressor and returns Z_STREAM_END only if the checksum is correct.
  8930. +
  8931. + inflate() returns Z_OK if some progress has been made (more input processed
  8932. + or more output produced), Z_STREAM_END if the end of the compressed data has
  8933. + been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  8934. + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  8935. + corrupted (input stream not conforming to the zlib format or incorrect
  8936. + adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
  8937. + (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
  8938. + enough memory, Z_BUF_ERROR if no progress is possible or if there was not
  8939. + enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
  8940. + case, the application may then call inflateSync to look for a good
  8941. + compression block.
  8942. +*/
  8943. +
  8944. +
  8945. +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
  8946. +/*
  8947. + All dynamically allocated data structures for this stream are freed.
  8948. + This function discards any unprocessed input and does not flush any
  8949. + pending output.
  8950. +
  8951. + inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  8952. + was inconsistent. In the error case, msg may be set but then points to a
  8953. + static string (which must not be deallocated).
  8954. +*/
  8955. +
  8956. + /* Advanced functions */
  8957. +
  8958. +/*
  8959. + The following functions are needed only in some special applications.
  8960. +*/
  8961. +
  8962. +/*
  8963. +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
  8964. + int level,
  8965. + int method,
  8966. + int windowBits,
  8967. + int memLevel,
  8968. + int strategy));
  8969. +
  8970. + This is another version of deflateInit with more compression options. The
  8971. + fields next_in, zalloc, zfree and opaque must be initialized before by
  8972. + the caller.
  8973. +
  8974. + The method parameter is the compression method. It must be Z_DEFLATED in
  8975. + this version of the library.
  8976. +
  8977. + The windowBits parameter is the base two logarithm of the window size
  8978. + (the size of the history buffer). It should be in the range 8..15 for this
  8979. + version of the library. Larger values of this parameter result in better
  8980. + compression at the expense of memory usage. The default value is 15 if
  8981. + deflateInit is used instead.
  8982. +
  8983. + The memLevel parameter specifies how much memory should be allocated
  8984. + for the internal compression state. memLevel=1 uses minimum memory but
  8985. + is slow and reduces compression ratio; memLevel=9 uses maximum memory
  8986. + for optimal speed. The default value is 8. See zconf.h for total memory
  8987. + usage as a function of windowBits and memLevel.
  8988. +
  8989. + The strategy parameter is used to tune the compression algorithm. Use the
  8990. + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
  8991. + filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
  8992. + string match). Filtered data consists mostly of small values with a
  8993. + somewhat random distribution. In this case, the compression algorithm is
  8994. + tuned to compress them better. The effect of Z_FILTERED is to force more
  8995. + Huffman coding and less string matching; it is somewhat intermediate
  8996. + between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
  8997. + the compression ratio but not the correctness of the compressed output even
  8998. + if it is not set appropriately.
  8999. +
  9000. + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9001. + memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
  9002. + method). msg is set to null if there is no error message. deflateInit2 does
  9003. + not perform any compression: this will be done by deflate().
  9004. +*/
  9005. +
  9006. +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
  9007. + const Bytef *dictionary,
  9008. + uInt dictLength));
  9009. +/*
  9010. + Initializes the compression dictionary from the given byte sequence
  9011. + without producing any compressed output. This function must be called
  9012. + immediately after deflateInit, deflateInit2 or deflateReset, before any
  9013. + call of deflate. The compressor and decompressor must use exactly the same
  9014. + dictionary (see inflateSetDictionary).
  9015. +
  9016. + The dictionary should consist of strings (byte sequences) that are likely
  9017. + to be encountered later in the data to be compressed, with the most commonly
  9018. + used strings preferably put towards the end of the dictionary. Using a
  9019. + dictionary is most useful when the data to be compressed is short and can be
  9020. + predicted with good accuracy; the data can then be compressed better than
  9021. + with the default empty dictionary.
  9022. +
  9023. + Depending on the size of the compression data structures selected by
  9024. + deflateInit or deflateInit2, a part of the dictionary may in effect be
  9025. + discarded, for example if the dictionary is larger than the window size in
  9026. + deflate or deflate2. Thus the strings most likely to be useful should be
  9027. + put at the end of the dictionary, not at the front.
  9028. +
  9029. + Upon return of this function, strm->adler is set to the Adler32 value
  9030. + of the dictionary; the decompressor may later use this value to determine
  9031. + which dictionary has been used by the compressor. (The Adler32 value
  9032. + applies to the whole dictionary even if only a subset of the dictionary is
  9033. + actually used by the compressor.)
  9034. +
  9035. + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
  9036. + parameter is invalid (such as NULL dictionary) or the stream state is
  9037. + inconsistent (for example if deflate has already been called for this stream
  9038. + or if the compression method is bsort). deflateSetDictionary does not
  9039. + perform any compression: this will be done by deflate().
  9040. +*/
  9041. +
  9042. +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
  9043. + z_streamp source));
  9044. +/*
  9045. + Sets the destination stream as a complete copy of the source stream.
  9046. +
  9047. + This function can be useful when several compression strategies will be
  9048. + tried, for example when there are several ways of pre-processing the input
  9049. + data with a filter. The streams that will be discarded should then be freed
  9050. + by calling deflateEnd. Note that deflateCopy duplicates the internal
  9051. + compression state which can be quite large, so this strategy is slow and
  9052. + can consume lots of memory.
  9053. +
  9054. + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  9055. + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  9056. + (such as zalloc being NULL). msg is left unchanged in both source and
  9057. + destination.
  9058. +*/
  9059. +
  9060. +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
  9061. +/*
  9062. + This function is equivalent to deflateEnd followed by deflateInit,
  9063. + but does not free and reallocate all the internal compression state.
  9064. + The stream will keep the same compression level and any other attributes
  9065. + that may have been set by deflateInit2.
  9066. +
  9067. + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  9068. + stream state was inconsistent (such as zalloc or state being NULL).
  9069. +*/
  9070. +
  9071. +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
  9072. + int level,
  9073. + int strategy));
  9074. +/*
  9075. + Dynamically update the compression level and compression strategy. The
  9076. + interpretation of level and strategy is as in deflateInit2. This can be
  9077. + used to switch between compression and straight copy of the input data, or
  9078. + to switch to a different kind of input data requiring a different
  9079. + strategy. If the compression level is changed, the input available so far
  9080. + is compressed with the old level (and may be flushed); the new level will
  9081. + take effect only at the next call of deflate().
  9082. +
  9083. + Before the call of deflateParams, the stream state must be set as for
  9084. + a call of deflate(), since the currently available input may have to
  9085. + be compressed and flushed. In particular, strm->avail_out must be non-zero.
  9086. +
  9087. + deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
  9088. + stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
  9089. + if strm->avail_out was zero.
  9090. +*/
  9091. +
  9092. +/*
  9093. +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
  9094. + int windowBits));
  9095. +
  9096. + This is another version of inflateInit with an extra parameter. The
  9097. + fields next_in, avail_in, zalloc, zfree and opaque must be initialized
  9098. + before by the caller.
  9099. +
  9100. + The windowBits parameter is the base two logarithm of the maximum window
  9101. + size (the size of the history buffer). It should be in the range 8..15 for
  9102. + this version of the library. The default value is 15 if inflateInit is used
  9103. + instead. If a compressed stream with a larger window size is given as
  9104. + input, inflate() will return with the error code Z_DATA_ERROR instead of
  9105. + trying to allocate a larger window.
  9106. +
  9107. + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9108. + memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
  9109. + memLevel). msg is set to null if there is no error message. inflateInit2
  9110. + does not perform any decompression apart from reading the zlib header if
  9111. + present: this will be done by inflate(). (So next_in and avail_in may be
  9112. + modified, but next_out and avail_out are unchanged.)
  9113. +*/
  9114. +
  9115. +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
  9116. + const Bytef *dictionary,
  9117. + uInt dictLength));
  9118. +/*
  9119. + Initializes the decompression dictionary from the given uncompressed byte
  9120. + sequence. This function must be called immediately after a call of inflate
  9121. + if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
  9122. + can be determined from the Adler32 value returned by this call of
  9123. + inflate. The compressor and decompressor must use exactly the same
  9124. + dictionary (see deflateSetDictionary).
  9125. +
  9126. + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  9127. + parameter is invalid (such as NULL dictionary) or the stream state is
  9128. + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  9129. + expected one (incorrect Adler32 value). inflateSetDictionary does not
  9130. + perform any decompression: this will be done by subsequent calls of
  9131. + inflate().
  9132. +*/
  9133. +
  9134. +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
  9135. +/*
  9136. + Skips invalid compressed data until a full flush point (see above the
  9137. + description of deflate with Z_FULL_FLUSH) can be found, or until all
  9138. + available input is skipped. No output is provided.
  9139. +
  9140. + inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  9141. + if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  9142. + or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  9143. + case, the application may save the current current value of total_in which
  9144. + indicates where valid compressed data was found. In the error case, the
  9145. + application may repeatedly call inflateSync, providing more input each time,
  9146. + until success or end of the input data.
  9147. +*/
  9148. +
  9149. +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
  9150. +/*
  9151. + This function is equivalent to inflateEnd followed by inflateInit,
  9152. + but does not free and reallocate all the internal decompression state.
  9153. + The stream will keep attributes that may have been set by inflateInit2.
  9154. +
  9155. + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  9156. + stream state was inconsistent (such as zalloc or state being NULL).
  9157. +*/
  9158. +
  9159. +
  9160. + /* utility functions */
  9161. +
  9162. +/*
  9163. + The following utility functions are implemented on top of the
  9164. + basic stream-oriented functions. To simplify the interface, some
  9165. + default options are assumed (compression level and memory usage,
  9166. + standard memory allocation functions). The source code of these
  9167. + utility functions can easily be modified if you need special options.
  9168. +*/
  9169. +
  9170. +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
  9171. + const Bytef *source, uLong sourceLen));
  9172. +/*
  9173. + Compresses the source buffer into the destination buffer. sourceLen is
  9174. + the byte length of the source buffer. Upon entry, destLen is the total
  9175. + size of the destination buffer, which must be at least 0.1% larger than
  9176. + sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
  9177. + compressed buffer.
  9178. + This function can be used to compress a whole file at once if the
  9179. + input file is mmap'ed.
  9180. + compress returns Z_OK if success, Z_MEM_ERROR if there was not
  9181. + enough memory, Z_BUF_ERROR if there was not enough room in the output
  9182. + buffer.
  9183. +*/
  9184. +
  9185. +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
  9186. + const Bytef *source, uLong sourceLen,
  9187. + int level));
  9188. +/*
  9189. + Compresses the source buffer into the destination buffer. The level
  9190. + parameter has the same meaning as in deflateInit. sourceLen is the byte
  9191. + length of the source buffer. Upon entry, destLen is the total size of the
  9192. + destination buffer, which must be at least 0.1% larger than sourceLen plus
  9193. + 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  9194. +
  9195. + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9196. + memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  9197. + Z_STREAM_ERROR if the level parameter is invalid.
  9198. +*/
  9199. +
  9200. +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
  9201. + const Bytef *source, uLong sourceLen));
  9202. +/*
  9203. + Decompresses the source buffer into the destination buffer. sourceLen is
  9204. + the byte length of the source buffer. Upon entry, destLen is the total
  9205. + size of the destination buffer, which must be large enough to hold the
  9206. + entire uncompressed data. (The size of the uncompressed data must have
  9207. + been saved previously by the compressor and transmitted to the decompressor
  9208. + by some mechanism outside the scope of this compression library.)
  9209. + Upon exit, destLen is the actual size of the compressed buffer.
  9210. + This function can be used to decompress a whole file at once if the
  9211. + input file is mmap'ed.
  9212. +
  9213. + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  9214. + enough memory, Z_BUF_ERROR if there was not enough room in the output
  9215. + buffer, or Z_DATA_ERROR if the input data was corrupted.
  9216. +*/
  9217. +
  9218. +
  9219. +typedef voidp gzFile;
  9220. +
  9221. +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
  9222. +/*
  9223. + Opens a gzip (.gz) file for reading or writing. The mode parameter
  9224. + is as in fopen ("rb" or "wb") but can also include a compression level
  9225. + ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
  9226. + Huffman only compression as in "wb1h". (See the description
  9227. + of deflateInit2 for more information about the strategy parameter.)
  9228. +
  9229. + gzopen can be used to read a file which is not in gzip format; in this
  9230. + case gzread will directly read from the file without decompression.
  9231. +
  9232. + gzopen returns NULL if the file could not be opened or if there was
  9233. + insufficient memory to allocate the (de)compression state; errno
  9234. + can be checked to distinguish the two cases (if errno is zero, the
  9235. + zlib error is Z_MEM_ERROR). */
  9236. +
  9237. +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
  9238. +/*
  9239. + gzdopen() associates a gzFile with the file descriptor fd. File
  9240. + descriptors are obtained from calls like open, dup, creat, pipe or
  9241. + fileno (in the file has been previously opened with fopen).
  9242. + The mode parameter is as in gzopen.
  9243. + The next call of gzclose on the returned gzFile will also close the
  9244. + file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
  9245. + descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
  9246. + gzdopen returns NULL if there was insufficient memory to allocate
  9247. + the (de)compression state.
  9248. +*/
  9249. +
  9250. +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
  9251. +/*
  9252. + Dynamically update the compression level or strategy. See the description
  9253. + of deflateInit2 for the meaning of these parameters.
  9254. + gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
  9255. + opened for writing.
  9256. +*/
  9257. +
  9258. +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
  9259. +/*
  9260. + Reads the given number of uncompressed bytes from the compressed file.
  9261. + If the input file was not in gzip format, gzread copies the given number
  9262. + of bytes into the buffer.
  9263. + gzread returns the number of uncompressed bytes actually read (0 for
  9264. + end of file, -1 for error). */
  9265. +
  9266. +ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
  9267. + const voidp buf, unsigned len));
  9268. +/*
  9269. + Writes the given number of uncompressed bytes into the compressed file.
  9270. + gzwrite returns the number of uncompressed bytes actually written
  9271. + (0 in case of error).
  9272. +*/
  9273. +
  9274. +ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
  9275. +/*
  9276. + Converts, formats, and writes the args to the compressed file under
  9277. + control of the format string, as in fprintf. gzprintf returns the number of
  9278. + uncompressed bytes actually written (0 in case of error).
  9279. +*/
  9280. +
  9281. +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
  9282. +/*
  9283. + Writes the given null-terminated string to the compressed file, excluding
  9284. + the terminating null character.
  9285. + gzputs returns the number of characters written, or -1 in case of error.
  9286. +*/
  9287. +
  9288. +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
  9289. +/*
  9290. + Reads bytes from the compressed file until len-1 characters are read, or
  9291. + a newline character is read and transferred to buf, or an end-of-file
  9292. + condition is encountered. The string is then terminated with a null
  9293. + character.
  9294. + gzgets returns buf, or Z_NULL in case of error.
  9295. +*/
  9296. +
  9297. +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
  9298. +/*
  9299. + Writes c, converted to an unsigned char, into the compressed file.
  9300. + gzputc returns the value that was written, or -1 in case of error.
  9301. +*/
  9302. +
  9303. +ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
  9304. +/*
  9305. + Reads one byte from the compressed file. gzgetc returns this byte
  9306. + or -1 in case of end of file or error.
  9307. +*/
  9308. +
  9309. +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
  9310. +/*
  9311. + Flushes all pending output into the compressed file. The parameter
  9312. + flush is as in the deflate() function. The return value is the zlib
  9313. + error number (see function gzerror below). gzflush returns Z_OK if
  9314. + the flush parameter is Z_FINISH and all output could be flushed.
  9315. + gzflush should be called only when strictly necessary because it can
  9316. + degrade compression.
  9317. +*/
  9318. +
  9319. +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
  9320. + z_off_t offset, int whence));
  9321. +/*
  9322. + Sets the starting position for the next gzread or gzwrite on the
  9323. + given compressed file. The offset represents a number of bytes in the
  9324. + uncompressed data stream. The whence parameter is defined as in lseek(2);
  9325. + the value SEEK_END is not supported.
  9326. + If the file is opened for reading, this function is emulated but can be
  9327. + extremely slow. If the file is opened for writing, only forward seeks are
  9328. + supported; gzseek then compresses a sequence of zeroes up to the new
  9329. + starting position.
  9330. +
  9331. + gzseek returns the resulting offset location as measured in bytes from
  9332. + the beginning of the uncompressed stream, or -1 in case of error, in
  9333. + particular if the file is opened for writing and the new starting position
  9334. + would be before the current position.
  9335. +*/
  9336. +
  9337. +ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
  9338. +/*
  9339. + Rewinds the given file. This function is supported only for reading.
  9340. +
  9341. + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
  9342. +*/
  9343. +
  9344. +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
  9345. +/*
  9346. + Returns the starting position for the next gzread or gzwrite on the
  9347. + given compressed file. This position represents a number of bytes in the
  9348. + uncompressed data stream.
  9349. +
  9350. + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
  9351. +*/
  9352. +
  9353. +ZEXTERN int ZEXPORT gzeof OF((gzFile file));
  9354. +/*
  9355. + Returns 1 when EOF has previously been detected reading the given
  9356. + input stream, otherwise zero.
  9357. +*/
  9358. +
  9359. +ZEXTERN int ZEXPORT gzclose OF((gzFile file));
  9360. +/*
  9361. + Flushes all pending output if necessary, closes the compressed file
  9362. + and deallocates all the (de)compression state. The return value is the zlib
  9363. + error number (see function gzerror below).
  9364. +*/
  9365. +
  9366. +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
  9367. +/*
  9368. + Returns the error message for the last error which occurred on the
  9369. + given compressed file. errnum is set to zlib error number. If an
  9370. + error occurred in the file system and not in the compression library,
  9371. + errnum is set to Z_ERRNO and the application may consult errno
  9372. + to get the exact error code.
  9373. +*/
  9374. +
  9375. + /* checksum functions */
  9376. +
  9377. +/*
  9378. + These functions are not related to compression but are exported
  9379. + anyway because they might be useful in applications using the
  9380. + compression library.
  9381. +*/
  9382. +
  9383. +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
  9384. +
  9385. +/*
  9386. + Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  9387. + return the updated checksum. If buf is NULL, this function returns
  9388. + the required initial value for the checksum.
  9389. + An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  9390. + much faster. Usage example:
  9391. +
  9392. + uLong adler = adler32(0L, Z_NULL, 0);
  9393. +
  9394. + while (read_buffer(buffer, length) != EOF) {
  9395. + adler = adler32(adler, buffer, length);
  9396. + }
  9397. + if (adler != original_adler) error();
  9398. +*/
  9399. +
  9400. +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
  9401. +/*
  9402. + Update a running crc with the bytes buf[0..len-1] and return the updated
  9403. + crc. If buf is NULL, this function returns the required initial value
  9404. + for the crc. Pre- and post-conditioning (one's complement) is performed
  9405. + within this function so it shouldn't be done by the application.
  9406. + Usage example:
  9407. +
  9408. + uLong crc = crc32(0L, Z_NULL, 0);
  9409. +
  9410. + while (read_buffer(buffer, length) != EOF) {
  9411. + crc = crc32(crc, buffer, length);
  9412. + }
  9413. + if (crc != original_crc) error();
  9414. +*/
  9415. +
  9416. +
  9417. + /* various hacks, don't look :) */
  9418. +
  9419. +/* deflateInit and inflateInit are macros to allow checking the zlib version
  9420. + * and the compiler's view of z_stream:
  9421. + */
  9422. +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
  9423. + const char *version, int stream_size));
  9424. +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
  9425. + const char *version, int stream_size));
  9426. +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
  9427. + int windowBits, int memLevel,
  9428. + int strategy, const char *version,
  9429. + int stream_size));
  9430. +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
  9431. + const char *version, int stream_size));
  9432. +#define deflateInit(strm, level) \
  9433. + deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
  9434. +#define inflateInit(strm) \
  9435. + inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
  9436. +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
  9437. + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
  9438. + (strategy), ZLIB_VERSION, sizeof(z_stream))
  9439. +#define inflateInit2(strm, windowBits) \
  9440. + inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
  9441. +
  9442. +
  9443. +#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
  9444. + struct internal_state {int dummy;}; /* hack for buggy compilers */
  9445. +#endif
  9446. +
  9447. +ZEXTERN const char * ZEXPORT zError OF((int err));
  9448. +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
  9449. +ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
  9450. +
  9451. +#ifdef __cplusplus
  9452. +}
  9453. +#endif
  9454. +
  9455. +#endif /* _ZLIB_H */
  9456. --- /dev/null Tue Mar 11 13:02:56 2003
  9457. +++ linux/include/zlib/zutil.h Mon Feb 9 13:51:03 2004
  9458. @@ -0,0 +1,225 @@
  9459. +/* zutil.h -- internal interface and configuration of the compression library
  9460. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  9461. + * For conditions of distribution and use, see copyright notice in zlib.h
  9462. + */
  9463. +
  9464. +/* WARNING: this file should *not* be used by applications. It is
  9465. + part of the implementation of the compression library and is
  9466. + subject to change. Applications should only use zlib.h.
  9467. + */
  9468. +
  9469. +/* @(#) $Id: zutil.h,v 1.4 2002/04/24 07:36:48 mcr Exp $ */
  9470. +
  9471. +#ifndef _Z_UTIL_H
  9472. +#define _Z_UTIL_H
  9473. +
  9474. +#include "zlib.h"
  9475. +
  9476. +#include <linux/string.h>
  9477. +#define HAVE_MEMCPY
  9478. +
  9479. +#if 0 // #ifdef STDC
  9480. +# include <stddef.h>
  9481. +# include <string.h>
  9482. +# include <stdlib.h>
  9483. +#endif
  9484. +#ifndef __KERNEL__
  9485. +#ifdef NO_ERRNO_H
  9486. + extern int errno;
  9487. +#else
  9488. +# include <errno.h>
  9489. +#endif
  9490. +#endif
  9491. +
  9492. +#ifndef local
  9493. +# define local static
  9494. +#endif
  9495. +/* compile with -Dlocal if your debugger can't find static symbols */
  9496. +
  9497. +typedef unsigned char uch;
  9498. +typedef uch FAR uchf;
  9499. +typedef unsigned short ush;
  9500. +typedef ush FAR ushf;
  9501. +typedef unsigned long ulg;
  9502. +
  9503. +extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
  9504. +/* (size given to avoid silly warnings with Visual C++) */
  9505. +
  9506. +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  9507. +
  9508. +#define ERR_RETURN(strm,err) \
  9509. + return (strm->msg = ERR_MSG(err), (err))
  9510. +/* To be used only when the state is known to be valid */
  9511. +
  9512. + /* common constants */
  9513. +
  9514. +#ifndef DEF_WBITS
  9515. +# define DEF_WBITS MAX_WBITS
  9516. +#endif
  9517. +/* default windowBits for decompression. MAX_WBITS is for compression only */
  9518. +
  9519. +#if MAX_MEM_LEVEL >= 8
  9520. +# define DEF_MEM_LEVEL 8
  9521. +#else
  9522. +# define DEF_MEM_LEVEL MAX_MEM_LEVEL
  9523. +#endif
  9524. +/* default memLevel */
  9525. +
  9526. +#define STORED_BLOCK 0
  9527. +#define STATIC_TREES 1
  9528. +#define DYN_TREES 2
  9529. +/* The three kinds of block type */
  9530. +
  9531. +#define MIN_MATCH 3
  9532. +#define MAX_MATCH 258
  9533. +/* The minimum and maximum match lengths */
  9534. +
  9535. +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
  9536. +
  9537. + /* target dependencies */
  9538. +
  9539. +#ifdef MSDOS
  9540. +# define OS_CODE 0x00
  9541. +# if defined(__TURBOC__) || defined(__BORLANDC__)
  9542. +# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
  9543. + /* Allow compilation with ANSI keywords only enabled */
  9544. + void _Cdecl farfree( void *block );
  9545. + void *_Cdecl farmalloc( unsigned long nbytes );
  9546. +# else
  9547. +# include <alloc.h>
  9548. +# endif
  9549. +# else /* MSC or DJGPP */
  9550. +# include <malloc.h>
  9551. +# endif
  9552. +#endif
  9553. +
  9554. +#ifdef OS2
  9555. +# define OS_CODE 0x06
  9556. +#endif
  9557. +
  9558. +#ifdef WIN32 /* Window 95 & Windows NT */
  9559. +# define OS_CODE 0x0b
  9560. +#endif
  9561. +
  9562. +#if defined(VAXC) || defined(VMS)
  9563. +# define OS_CODE 0x02
  9564. +# define F_OPEN(name, mode) \
  9565. + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
  9566. +#endif
  9567. +
  9568. +#ifdef AMIGA
  9569. +# define OS_CODE 0x01
  9570. +#endif
  9571. +
  9572. +#if defined(ATARI) || defined(atarist)
  9573. +# define OS_CODE 0x05
  9574. +#endif
  9575. +
  9576. +#if defined(MACOS) || defined(TARGET_OS_MAC)
  9577. +# define OS_CODE 0x07
  9578. +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  9579. +# include <unix.h> /* for fdopen */
  9580. +# else
  9581. +# ifndef fdopen
  9582. +# define fdopen(fd,mode) NULL /* No fdopen() */
  9583. +# endif
  9584. +# endif
  9585. +#endif
  9586. +
  9587. +#ifdef __50SERIES /* Prime/PRIMOS */
  9588. +# define OS_CODE 0x0F
  9589. +#endif
  9590. +
  9591. +#ifdef TOPS20
  9592. +# define OS_CODE 0x0a
  9593. +#endif
  9594. +
  9595. +#if defined(_BEOS_) || defined(RISCOS)
  9596. +# define fdopen(fd,mode) NULL /* No fdopen() */
  9597. +#endif
  9598. +
  9599. +#if (defined(_MSC_VER) && (_MSC_VER > 600))
  9600. +# define fdopen(fd,type) _fdopen(fd,type)
  9601. +#endif
  9602. +
  9603. +
  9604. + /* Common defaults */
  9605. +
  9606. +#ifndef OS_CODE
  9607. +# define OS_CODE 0x03 /* assume Unix */
  9608. +#endif
  9609. +
  9610. +#ifndef F_OPEN
  9611. +# define F_OPEN(name, mode) fopen((name), (mode))
  9612. +#endif
  9613. +
  9614. + /* functions */
  9615. +
  9616. +#ifdef HAVE_STRERROR
  9617. + extern char *strerror OF((int));
  9618. +# define zstrerror(errnum) strerror(errnum)
  9619. +#else
  9620. +# define zstrerror(errnum) ""
  9621. +#endif
  9622. +
  9623. +#if defined(pyr)
  9624. +# define NO_MEMCPY
  9625. +#endif
  9626. +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
  9627. + /* Use our own functions for small and medium model with MSC <= 5.0.
  9628. + * You may have to use the same strategy for Borland C (untested).
  9629. + * The __SC__ check is for Symantec.
  9630. + */
  9631. +# define NO_MEMCPY
  9632. +#endif
  9633. +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
  9634. +# define HAVE_MEMCPY
  9635. +#endif
  9636. +#ifdef HAVE_MEMCPY
  9637. +# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
  9638. +# define zmemcpy _fmemcpy
  9639. +# define zmemcmp _fmemcmp
  9640. +# define zmemzero(dest, len) _fmemset(dest, 0, len)
  9641. +# else
  9642. +# define zmemcpy memcpy
  9643. +# define zmemcmp memcmp
  9644. +# define zmemzero(dest, len) memset(dest, 0, len)
  9645. +# endif
  9646. +#else
  9647. + extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
  9648. + extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
  9649. + extern void zmemzero OF((Bytef* dest, uInt len));
  9650. +#endif
  9651. +
  9652. +/* Diagnostic functions */
  9653. +#ifdef DEBUG
  9654. +# include <stdio.h>
  9655. + extern int z_verbose;
  9656. + extern void z_error OF((char *m));
  9657. +# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
  9658. +# define Trace(x) {if (z_verbose>=0) fprintf x ;}
  9659. +# define Tracev(x) {if (z_verbose>0) fprintf x ;}
  9660. +# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
  9661. +# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
  9662. +# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
  9663. +#else
  9664. +# define Assert(cond,msg)
  9665. +# define Trace(x)
  9666. +# define Tracev(x)
  9667. +# define Tracevv(x)
  9668. +# define Tracec(c,x)
  9669. +# define Tracecv(c,x)
  9670. +#endif
  9671. +
  9672. +
  9673. +typedef uLong (ZEXPORT *check_func) OF((uLong check, const Bytef *buf,
  9674. + uInt len));
  9675. +voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
  9676. +void zcfree OF((voidpf opaque, voidpf ptr));
  9677. +
  9678. +#define ZALLOC(strm, items, size) \
  9679. + (*((strm)->zalloc))((strm)->opaque, (items), (size))
  9680. +#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
  9681. +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
  9682. +
  9683. +#endif /* _Z_UTIL_H */
  9684. --- /dev/null Tue Mar 11 13:02:56 2003
  9685. +++ linux/lib/libfreeswan/Makefile.objs Mon Feb 9 13:51:03 2004
  9686. @@ -0,0 +1,21 @@
  9687. +obj-y += satot.o
  9688. +obj-y += addrtot.o
  9689. +obj-y += ultot.o
  9690. +obj-y += addrtypeof.o
  9691. +obj-y += anyaddr.o
  9692. +obj-y += initaddr.o
  9693. +obj-y += ultoa.o
  9694. +obj-y += addrtoa.o
  9695. +obj-y += subnettoa.o
  9696. +obj-y += subnetof.o
  9697. +obj-y += goodmask.o
  9698. +obj-y += datatot.o
  9699. +obj-y += rangetoa.o
  9700. +obj-y += prng.o
  9701. +obj-y += pfkey_v2_parse.o
  9702. +obj-y += pfkey_v2_build.o
  9703. +obj-y += pfkey_v2_debug.o
  9704. +obj-y += pfkey_v2_ext_bits.o
  9705. +
  9706. +#version.c: ${LIBFREESWANDIR}/version.in.c ${OPENSWANSRCDIR}/Makefile.ver
  9707. +# sed '/"/s/xxx/$(IPSECVERSION)/' ${LIBFREESWANDIR}/version.in.c >$@
  9708. --- /dev/null Tue Mar 11 13:02:56 2003
  9709. +++ linux/lib/zlib/Makefile Mon Feb 9 13:51:03 2004
  9710. @@ -0,0 +1,118 @@
  9711. +# (kernel) Makefile for IPCOMP zlib deflate code
  9712. +# Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  9713. +# Copyright (C) 2000 Svenning Soerensen
  9714. +#
  9715. +# This program is free software; you can redistribute it and/or modify it
  9716. +# under the terms of the GNU General Public License as published by the
  9717. +# Free Software Foundation; either version 2 of the License, or (at your
  9718. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9719. +#
  9720. +# This program is distributed in the hope that it will be useful, but
  9721. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9722. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9723. +# for more details.
  9724. +#
  9725. +# RCSID $Id: Makefile,v 1.9 2002/04/24 07:55:32 mcr Exp $
  9726. +#
  9727. +
  9728. +
  9729. +
  9730. +include ../Makefile.inc
  9731. +
  9732. +
  9733. +
  9734. +ifndef TOPDIR
  9735. +TOPDIR := /usr/src/linux
  9736. +endif
  9737. +
  9738. +
  9739. +L_TARGET := zlib.a
  9740. +
  9741. +obj-y :=
  9742. +
  9743. +include Makefile.objs
  9744. +
  9745. +EXTRA_CFLAGS += $(KLIPSCOMPILE)
  9746. +
  9747. +EXTRA_CFLAGS += -Wall
  9748. +#EXTRA_CFLAGS += -Wconversion
  9749. +#EXTRA_CFLAGS += -Wmissing-prototypes
  9750. +EXTRA_CFLAGS += -Wpointer-arith
  9751. +#EXTRA_CFLAGS += -Wcast-qual
  9752. +#EXTRA_CFLAGS += -Wmissing-declarations
  9753. +EXTRA_CFLAGS += -Wstrict-prototypes
  9754. +#EXTRA_CFLAGS += -pedantic
  9755. +#EXTRA_CFLAGS += -W
  9756. +#EXTRA_CFLAGS += -Wwrite-strings
  9757. +EXTRA_CFLAGS += -Wbad-function-cast
  9758. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  9759. +
  9760. +.S.o:
  9761. + $(CC) -D__ASSEMBLY__ -DNO_UNDERLINE -traditional -c $< -o $*.o
  9762. +
  9763. +asm-obj-$(CONFIG_M586) += match586.o
  9764. +asm-obj-$(CONFIG_M586TSC) += match586.o
  9765. +asm-obj-$(CONFIG_M586MMX) += match586.o
  9766. +asm-obj-$(CONFIG_M686) += match686.o
  9767. +asm-obj-$(CONFIG_MPENTIUMIII) += match686.o
  9768. +asm-obj-$(CONFIG_MPENTIUM4) += match686.o
  9769. +asm-obj-$(CONFIG_MK6) += match586.o
  9770. +asm-obj-$(CONFIG_MK7) += match686.o
  9771. +asm-obj-$(CONFIG_MCRUSOE) += match586.o
  9772. +asm-obj-$(CONFIG_MWINCHIPC6) += match586.o
  9773. +asm-obj-$(CONFIG_MWINCHIP2) += match686.o
  9774. +asm-obj-$(CONFIG_MWINCHIP3D) += match686.o
  9775. +
  9776. +obj-y += $(asm-obj-y)
  9777. +ifneq ($(strip $(asm-obj-y)),)
  9778. + EXTRA_CFLAGS += -DASMV
  9779. +endif
  9780. +
  9781. +active-objs := $(sort $(obj-y) $(obj-m))
  9782. +L_OBJS := $(obj-y)
  9783. +M_OBJS := $(obj-m)
  9784. +MIX_OBJS := $(filter $(export-objs), $(active-objs))
  9785. +
  9786. +include $(TOPDIR)/Rules.make
  9787. +
  9788. +$(obj-y) : $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h
  9789. +
  9790. +
  9791. +clean:
  9792. + -rm -f *.o *.a
  9793. +
  9794. +checkprograms:
  9795. +programs: $(L_TARGET)
  9796. +
  9797. +#
  9798. +# $Log: Makefile,v $
  9799. +# Revision 1.9 2002/04/24 07:55:32 mcr
  9800. +# #include patches and Makefiles for post-reorg compilation.
  9801. +#
  9802. +# Revision 1.8 2002/04/24 07:36:44 mcr
  9803. +# Moved from ./zlib/Makefile,v
  9804. +#
  9805. +# Revision 1.7 2002/03/27 23:34:35 mcr
  9806. +# added programs: target
  9807. +#
  9808. +# Revision 1.6 2001/12/05 20:19:08 henry
  9809. +# use new compile-control variable
  9810. +#
  9811. +# Revision 1.5 2001/11/27 16:38:08 mcr
  9812. +# added new "checkprograms" target to deal with programs that
  9813. +# are required for "make check", but that may not be ready to
  9814. +# build for every user due to external dependancies.
  9815. +#
  9816. +# Revision 1.4 2001/10/24 14:46:24 henry
  9817. +# Makefile.inc
  9818. +#
  9819. +# Revision 1.3 2001/04/21 23:05:24 rgb
  9820. +# Update asm directives for 2.4 style makefiles.
  9821. +#
  9822. +# Revision 1.2 2001/01/29 22:22:00 rgb
  9823. +# Convert to 2.4 new style with back compat.
  9824. +#
  9825. +# Revision 1.1.1.1 2000/09/29 18:51:33 rgb
  9826. +# zlib_beginnings
  9827. +#
  9828. +#
  9829. --- /dev/null Tue Mar 11 13:02:56 2003
  9830. +++ linux/lib/zlib/Makefile.objs Mon Feb 9 13:51:03 2004
  9831. @@ -0,0 +1,27 @@
  9832. +obj-$(CONFIG_IPSEC_IPCOMP) += adler32.o
  9833. +obj-$(CONFIG_IPSEC_IPCOMP) += deflate.o
  9834. +obj-$(CONFIG_IPSEC_IPCOMP) += infblock.o
  9835. +obj-$(CONFIG_IPSEC_IPCOMP) += infcodes.o
  9836. +obj-$(CONFIG_IPSEC_IPCOMP) += inffast.o
  9837. +obj-$(CONFIG_IPSEC_IPCOMP) += inflate.o
  9838. +obj-$(CONFIG_IPSEC_IPCOMP) += inftrees.o
  9839. +obj-$(CONFIG_IPSEC_IPCOMP) += infutil.o
  9840. +obj-$(CONFIG_IPSEC_IPCOMP) += trees.o
  9841. +obj-$(CONFIG_IPSEC_IPCOMP) += zutil.o
  9842. +
  9843. +asm-obj-$(CONFIG_M586) += ${LIBZLIBSRCDIR}/match586.o
  9844. +asm-obj-$(CONFIG_M586TSC) += ${LIBZLIBSRCDIR}/match586.o
  9845. +asm-obj-$(CONFIG_M586MMX) += ${LIBZLIBSRCDIR}/match586.o
  9846. +asm-obj-$(CONFIG_M686) += ${LIBZLIBSRCDIR}/match686.o
  9847. +asm-obj-$(CONFIG_MPENTIUMIII) += ${LIBZLIBSRCDIR}/match686.o
  9848. +asm-obj-$(CONFIG_MPENTIUM4) += ${LIBZLIBSRCDIR}/match686.o
  9849. +asm-obj-$(CONFIG_MK6) += ${LIBZLIBSRCDIR}/match586.o
  9850. +asm-obj-$(CONFIG_MK7) += ${LIBZLIBSRCDIR}/match686.o
  9851. +asm-obj-$(CONFIG_MCRUSOE) += ${LIBZLIBSRCDIR}/match586.o
  9852. +asm-obj-$(CONFIG_MWINCHIPC6) += ${LIBZLIBSRCDIR}/match586.o
  9853. +asm-obj-$(CONFIG_MWINCHIP2) += ${LIBZLIBSRCDIR}/match686.o
  9854. +asm-obj-$(CONFIG_MWINCHIP3D) += ${LIBZLIBSRCDIR}/match686.o
  9855. +
  9856. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  9857. +
  9858. +
  9859. --- swan26/net/Kconfig.preipsec 2005-09-01 18:15:19.000000000 -0400
  9860. +++ swan26/net/Kconfig 2005-09-03 16:51:17.000000000 -0400
  9861. @@ -215,2 +215,6 @@
  9862. +if INET
  9863. +source "net/ipsec/Kconfig"
  9864. +endif # if INET
  9865. +
  9866. endif # if NET
  9867. --- /distros/kernel/linux-2.6.3-rc4/net/Makefile Mon Feb 16 21:22:12 2004
  9868. +++ ref26/net/Makefile Thu Feb 19 21:02:25 2004
  9869. @@ -42,3 +42,6 @@
  9870. ifeq ($(CONFIG_NET),y)
  9871. obj-$(CONFIG_SYSCTL) += sysctl_net.o
  9872. endif
  9873. +
  9874. +obj-$(CONFIG_KLIPS) += ipsec/
  9875. +
  9876. --- /dev/null Tue Mar 11 13:02:56 2003
  9877. +++ linux/net/ipsec/Kconfig Mon Feb 9 13:51:03 2004
  9878. @@ -0,0 +1,161 @@
  9879. +#
  9880. +# IPSEC configuration
  9881. +# Copyright (C) 2004 Michael Richardson <mcr@freeswan.org>
  9882. +#
  9883. +# This program is free software; you can redistribute it and/or modify it
  9884. +# under the terms of the GNU General Public License as published by the
  9885. +# Free Software Foundation; either version 2 of the License, or (at your
  9886. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9887. +#
  9888. +# This program is distributed in the hope that it will be useful, but
  9889. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9890. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9891. +# for more details.
  9892. +#
  9893. +# RCSID $Id: Kconfig,v 1.6.2.2 2006/10/11 18:14:33 paul Exp $
  9894. +
  9895. +config KLIPS
  9896. + tristate "Openswan IPsec (KLIPS26)"
  9897. + default n
  9898. + help
  9899. + KLIPS is the Openswan (www.openswan.org) Kernel Level IP Security
  9900. + system. It is extensively tested, and has interoperated with
  9901. + many other systems.
  9902. + It provides "ipsecX" devices on which one can do firewalling.
  9903. + The userland, is compatible with both KLIPS and 26sec.
  9904. +
  9905. +menu "KLIPS options"
  9906. + depends on KLIPS
  9907. +
  9908. +config KLIPS_ESP
  9909. + bool 'Encapsulating Security Payload - ESP ("VPN")'
  9910. + default y
  9911. + help
  9912. + This option provides support for the IPSEC Encapsulation Security
  9913. + Payload (IP protocol 50) which provides packet layer content
  9914. + hiding, and content authentication.
  9915. + It is recommended to enable this. RFC2406
  9916. +
  9917. +config KLIPS_AH
  9918. + bool 'Authentication Header - AH'
  9919. + default n
  9920. + help
  9921. + This option provides support for the IPSEC Authentication Header
  9922. + (IP protocol 51) which provides packet layer sender and content
  9923. + authentication. It does not provide for confidentiality.
  9924. + It is not recommended to enable this. RFC2402
  9925. +
  9926. +config KLIPS_AUTH_HMAC_MD5
  9927. + bool 'HMAC-MD5 authentication algorithm'
  9928. + default y
  9929. + help
  9930. + The HMAC-MD5 algorithm is used by ESP (and AH) to guarantee packet
  9931. + integrity. There is little reason not to include it.
  9932. +
  9933. +config KLIPS_AUTH_HMAC_SHA1
  9934. + bool 'HMAC-SHA1 authentication algorithm'
  9935. + default y
  9936. + help
  9937. + The HMAC-SHA1 algorithm is used by ESP (and AH) to guarantee packet
  9938. + integrity. SHA1 is a little slower than MD5, but is said to be
  9939. + a bit more secure. There is little reason not to include it.
  9940. +
  9941. +config KLIPS_ENC_CRYPTOAPI
  9942. + bool 'CryptoAPI algorithm interface'
  9943. + default n
  9944. + help
  9945. + Enable the algorithm interface to make all CryptoAPI 1.0 algorithms
  9946. + available to KLIPS.
  9947. +
  9948. +config KLIPS_ENC_1DES
  9949. + bool 'Include 1DES with CryptoAPI'
  9950. + default n
  9951. + depends on KLIPS_ENC_CRYPTOAPI
  9952. + help
  9953. + The CryptoAPI interface does not include support for every algorithm
  9954. + yet, and one that it doesn't support by default is the VERY WEAK
  9955. + 1DES. Select this if you are terminally stupid.
  9956. +
  9957. +config KLIPS_ENC_3DES
  9958. + bool '3DES encryption algorithm'
  9959. + default y
  9960. + help
  9961. + The 3DES algorithm is used by ESP to provide for packet privacy.
  9962. + 3DES is 3-repeats of the DES algorithm. 3DES is widely supported,
  9963. + and analyzed and is considered very secure. 1DES is not supported.
  9964. +
  9965. +config KLIPS_ENC_AES
  9966. + bool 'AES encryption algorithm'
  9967. + default y
  9968. + help
  9969. + The AES algorithm is used by ESP to provide for packet privacy.
  9970. + AES the NIST replacement for DES. AES is being widely analyzed,
  9971. + and is very fast.
  9972. +
  9973. +config KLIPS_ENC_NULL
  9974. + bool 'NULL NON-encryption algorithm'
  9975. + default n
  9976. + help
  9977. + NON encryption algo , maybe useful for ESP auth only scenarios
  9978. + (eg: with NAT-T), see RFC 2410.
  9979. +
  9980. +config KLIPS_IPCOMP
  9981. + bool 'IP compression'
  9982. + default y
  9983. + help
  9984. + The IPcomp protocol is used prior to ESP to make the packet
  9985. + smaller. Once encrypted, compression will fail, so any link
  9986. + layer efforts (e.g. PPP) will not work.
  9987. +
  9988. +config KLIPS_DEBUG
  9989. + bool 'IPsec debugging'
  9990. + default y
  9991. + help
  9992. + KLIPS includes a lot of debugging code. Unless there is a real
  9993. + tangible benefit to removing this code, it should be left in place.
  9994. + Debugging connections without access to kernel level debugging is
  9995. + essentially impossible. Leave this on.
  9996. +
  9997. +endmenu
  9998. +
  9999. +#
  10000. +#
  10001. +# $Log: Kconfig,v $
  10002. +# Revision 1.6.2.2 2006/10/11 18:14:33 paul
  10003. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  10004. +# per default.
  10005. +#
  10006. +# Revision 1.6.2.1 2006/04/20 16:33:06 mcr
  10007. +# remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  10008. +# Fix in-kernel module compilation. Sub-makefiles do not work.
  10009. +#
  10010. +# Revision 1.6 2005/05/18 20:55:27 mcr
  10011. +# default cryptoapi to n.
  10012. +#
  10013. +# Revision 1.5 2005/05/11 01:23:25 mcr
  10014. +# added 1DES option to cryptoapi.
  10015. +#
  10016. +# Revision 1.4 2005/04/29 05:29:54 mcr
  10017. +# add option to include cryptoapi algorithms.
  10018. +#
  10019. +# Revision 1.3 2004/08/17 03:27:23 mcr
  10020. +# klips 2.6 edits.
  10021. +#
  10022. +# Revision 1.2 2004/08/14 03:27:39 mcr
  10023. +# 2.6 kernel build/configuration files.
  10024. +#
  10025. +# Revision 1.1 2004/08/14 02:47:55 mcr
  10026. +# kernel build/config patches
  10027. +#
  10028. +# Revision 1.3 2004/02/24 17:17:04 mcr
  10029. +# s/CONFIG_IPSEC/CONFIG_KLIPS/ as 26sec uses "CONFIG_IPSEC" to
  10030. +# turn it on/off as well.
  10031. +#
  10032. +# Revision 1.2 2004/02/22 06:50:42 mcr
  10033. +# kernel 2.6 port - merged with 2.4 code.
  10034. +#
  10035. +# Revision 1.1.2.1 2004/02/20 02:07:53 mcr
  10036. +# module configuration for KLIPS 2.6
  10037. +#
  10038. +#
  10039. +
  10040. --- /dev/null Tue Mar 11 13:02:56 2003
  10041. +++ linux/net/ipsec/Makefile Mon Feb 9 13:51:03 2004
  10042. @@ -0,0 +1,195 @@
  10043. +# Makefile for KLIPS kernel code as a module for 2.6 kernels
  10044. +#
  10045. +# Makefile for KLIPS kernel code as a module
  10046. +# Copyright (C) 1998, 1999, 2000,2001 Richard Guy Briggs.
  10047. +# Copyright (C) 2002-2004 Michael Richardson <mcr@freeswan.org>
  10048. +#
  10049. +# This program is free software; you can redistribute it and/or modify it
  10050. +# under the terms of the GNU General Public License as published by the
  10051. +# Free Software Foundation; either version 2 of the License, or (at your
  10052. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  10053. +#
  10054. +# This program is distributed in the hope that it will be useful, but
  10055. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10056. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10057. +# for more details.
  10058. +#
  10059. +# RCSID $Id: Makefile.fs2_6,v 1.8.2.2 2006/10/11 18:14:33 paul Exp $
  10060. +#
  10061. +# Note! Dependencies are done automagically by 'make dep', which also
  10062. +# removes any old dependencies. DON'T put your own dependencies here
  10063. +# unless it's something special (ie not a .c file).
  10064. +#
  10065. +
  10066. +OPENSWANSRCDIR?=.
  10067. +KLIPS_TOP?=.
  10068. +
  10069. +-include ${OPENSWANSRCDIR}/Makefile.ver
  10070. +
  10071. +base-klips-objs :=
  10072. +
  10073. +base-klips-objs+= ipsec_init.o ipsec_sa.o ipsec_radij.o radij.o
  10074. +base-klips-objs+= ipsec_life.o ipsec_proc.o
  10075. +base-klips-objs+= ipsec_tunnel.o ipsec_xmit.o ipsec_rcv.o ipsec_ipip.o
  10076. +base-klips-objs+= ipsec_snprintf.o
  10077. +base-klips-objs+= sysctl_net_ipsec.o
  10078. +base-klips-objs+= pfkey_v2.o pfkey_v2_parser.o pfkey_v2_ext_process.o
  10079. +base-klips-objs+= version.o
  10080. +
  10081. +base-klips-objs+= satot.o
  10082. +base-klips-objs+= addrtot.o
  10083. +base-klips-objs+= ultot.o
  10084. +base-klips-objs+= addrtypeof.o
  10085. +base-klips-objs+= anyaddr.o
  10086. +base-klips-objs+= initaddr.o
  10087. +base-klips-objs+= ultoa.o
  10088. +base-klips-objs+= addrtoa.o
  10089. +base-klips-objs+= subnettoa.o
  10090. +base-klips-objs+= subnetof.o
  10091. +base-klips-objs+= goodmask.o
  10092. +base-klips-objs+= datatot.o
  10093. +base-klips-objs+= rangetoa.o
  10094. +base-klips-objs+= prng.o
  10095. +base-klips-objs+= pfkey_v2_parse.o
  10096. +base-klips-objs+= pfkey_v2_build.o
  10097. +base-klips-objs+= pfkey_v2_debug.o
  10098. +base-klips-objs+= pfkey_v2_ext_bits.o
  10099. +base-klips-objs+= version.o
  10100. +
  10101. +obj-${CONFIG_KLIPS} += ipsec.o
  10102. +
  10103. +ipsec-objs += ${base-klips-objs}
  10104. +
  10105. +ipsec-$(CONFIG_KLIPS_ESP) += ipsec_esp.o
  10106. +ipsec-$(CONFIG_KLIPS_IPCOMP) += ipsec_ipcomp.o
  10107. +ipsec-$(CONFIG_KLIPS_AUTH_HMAC_MD5) += ipsec_md5c.o
  10108. +ipsec-$(CONFIG_KLIPS_AUTH_HMAC_SHA1) += ipsec_sha1.o
  10109. +
  10110. +# AH, if you really think you need it.
  10111. +ipsec-$(CONFIG_KLIPS_AH) += ipsec_ah.o
  10112. +
  10113. +ipsec-y += ipsec_alg.o
  10114. +
  10115. +# include code from DES subdir
  10116. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/ipsec_alg_3des.o
  10117. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/cbc_enc.o
  10118. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/ecb_enc.o
  10119. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/set_key.o
  10120. +
  10121. +ifeq ($(strip ${SUBARCH}),)
  10122. +SUBARCH:=${ARCH}
  10123. +endif
  10124. +
  10125. +# the assembly version expects frame pointers, which are
  10126. +# optional in many kernel builds. If you want speed, you should
  10127. +# probably use cryptoapi code instead.
  10128. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  10129. +ifeq (${USEASSEMBLY},i386y)
  10130. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/dx86unix.o
  10131. +else
  10132. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/des_enc.o
  10133. +endif
  10134. +
  10135. +# include code from AES subdir
  10136. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/ipsec_alg_aes.o
  10137. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes_xcbc_mac.o
  10138. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes_cbc.o
  10139. +
  10140. +ifeq ($(strip ${SUBARCH}),)
  10141. +SUBARCH:=${ARCH}
  10142. +endif
  10143. +
  10144. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  10145. +ifeq (${USEASSEMBLY},i386y)
  10146. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes-i586.o
  10147. +else
  10148. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes.o
  10149. +endif
  10150. +
  10151. +crypto-$(CONFIG_KLIPS_ENC_NULL) += null/ipsec_alg_null.o
  10152. +
  10153. +ipsec-y += ${crypto-y}
  10154. +
  10155. +ipsec-$(CONFIG_KLIPS_ENC_CRYPTOAPI) += ipsec_alg_cryptoapi.o
  10156. +
  10157. +# IPcomp stuff
  10158. +base-ipcomp-objs := ipcomp.o
  10159. +base-ipcomp-objs += adler32.o
  10160. +base-ipcomp-objs += deflate.o
  10161. +base-ipcomp-objs += infblock.o
  10162. +base-ipcomp-objs += infcodes.o
  10163. +base-ipcomp-objs += inffast.o
  10164. +base-ipcomp-objs += inflate.o
  10165. +base-ipcomp-objs += inftrees.o
  10166. +base-ipcomp-objs += infutil.o
  10167. +base-ipcomp-objs += trees.o
  10168. +base-ipcomp-objs += zutil.o
  10169. +asm-ipcomp-obj-$(CONFIG_M586) += match586.o
  10170. +asm-ipcomp-obj-$(CONFIG_M586TSC) += match586.o
  10171. +asm-ipcomp-obj-$(CONFIG_M586MMX) += match586.o
  10172. +asm-ipcomp-obj-$(CONFIG_M686) += match686.o
  10173. +asm-ipcomp-obj-$(CONFIG_MPENTIUMIII) += match686.o
  10174. +asm-ipcomp-obj-$(CONFIG_MPENTIUM4) += match686.o
  10175. +asm-ipcomp-obj-$(CONFIG_MK6) += match586.o
  10176. +asm-ipcomp-obj-$(CONFIG_MK7) += match686.o
  10177. +asm-ipcomp-obj-$(CONFIG_MCRUSOE) += match586.o
  10178. +asm-ipcomp-obj-$(CONFIG_MWINCHIPC6) += match586.o
  10179. +asm-ipcomp-obj-$(CONFIG_MWINCHIP2) += match686.o
  10180. +asm-ipcomp-obj-$(CONFIG_MWINCHIP3D) += match686.o
  10181. +base-ipcomp-objs += ${asm-ipcomp-obj-y}
  10182. +
  10183. +ipsec-$(CONFIG_KLIPS_IPCOMP) += ${base-ipcomp-objs}
  10184. +
  10185. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  10186. +
  10187. +#
  10188. +# $Log: Makefile.fs2_6,v $
  10189. +# Revision 1.8.2.2 2006/10/11 18:14:33 paul
  10190. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  10191. +# per default.
  10192. +#
  10193. +# Revision 1.8.2.1 2006/04/20 16:33:06 mcr
  10194. +# remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  10195. +# Fix in-kernel module compilation. Sub-makefiles do not work.
  10196. +#
  10197. +# Revision 1.8 2005/05/11 03:15:42 mcr
  10198. +# adjusted makefiles to sanely build modules properly.
  10199. +#
  10200. +# Revision 1.7 2005/04/13 22:52:12 mcr
  10201. +# moved KLIPS specific snprintf() wrapper to seperate file.
  10202. +#
  10203. +# Revision 1.6 2004/08/22 05:02:03 mcr
  10204. +# organized symbols such that it is easier to build modules.
  10205. +#
  10206. +# Revision 1.5 2004/08/18 01:43:56 mcr
  10207. +# adjusted makefile enumation so that it can be used by module
  10208. +# wrapper.
  10209. +#
  10210. +# Revision 1.4 2004/08/17 03:27:23 mcr
  10211. +# klips 2.6 edits.
  10212. +#
  10213. +# Revision 1.3 2004/08/04 16:50:13 mcr
  10214. +# removed duplicate definition of dx86unix.o
  10215. +#
  10216. +# Revision 1.2 2004/08/03 18:21:09 mcr
  10217. +# only set KLIPS_TOP and OPENSWANSRCDIR if not already set.
  10218. +#
  10219. +# Revision 1.1 2004/07/26 15:02:22 mcr
  10220. +# makefile for KLIPS module for 2.6.
  10221. +#
  10222. +# Revision 1.3 2004/02/24 17:17:04 mcr
  10223. +# s/CONFIG_IPSEC/CONFIG_KLIPS/ as 26sec uses "CONFIG_IPSEC" to
  10224. +# turn it on/off as well.
  10225. +#
  10226. +# Revision 1.2 2004/02/22 06:50:42 mcr
  10227. +# kernel 2.6 port - merged with 2.4 code.
  10228. +#
  10229. +# Revision 1.1.2.1 2004/02/20 02:07:53 mcr
  10230. +# module configuration for KLIPS 2.6
  10231. +#
  10232. +#
  10233. +# Local Variables:
  10234. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  10235. +# End Variables:
  10236. +#
  10237. +
  10238. --- /dev/null Tue Mar 11 13:02:56 2003
  10239. +++ linux/net/ipsec/README-zlib Mon Feb 9 13:51:03 2004
  10240. @@ -0,0 +1,147 @@
  10241. +zlib 1.1.4 is a general purpose data compression library. All the code
  10242. +is thread safe. The data format used by the zlib library
  10243. +is described by RFCs (Request for Comments) 1950 to 1952 in the files
  10244. +http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate
  10245. +format) and rfc1952.txt (gzip format). These documents are also available in
  10246. +other formats from ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html
  10247. +
  10248. +All functions of the compression library are documented in the file zlib.h
  10249. +(volunteer to write man pages welcome, contact jloup@gzip.org). A usage
  10250. +example of the library is given in the file example.c which also tests that
  10251. +the library is working correctly. Another example is given in the file
  10252. +minigzip.c. The compression library itself is composed of all source files
  10253. +except example.c and minigzip.c.
  10254. +
  10255. +To compile all files and run the test program, follow the instructions
  10256. +given at the top of Makefile. In short "make test; make install"
  10257. +should work for most machines. For Unix: "./configure; make test; make install"
  10258. +For MSDOS, use one of the special makefiles such as Makefile.msc.
  10259. +For VMS, use Make_vms.com or descrip.mms.
  10260. +
  10261. +Questions about zlib should be sent to <zlib@gzip.org>, or to
  10262. +Gilles Vollant <info@winimage.com> for the Windows DLL version.
  10263. +The zlib home page is http://www.zlib.org or http://www.gzip.org/zlib/
  10264. +Before reporting a problem, please check this site to verify that
  10265. +you have the latest version of zlib; otherwise get the latest version and
  10266. +check whether the problem still exists or not.
  10267. +
  10268. +PLEASE read the zlib FAQ http://www.gzip.org/zlib/zlib_faq.html
  10269. +before asking for help.
  10270. +
  10271. +Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
  10272. +issue of Dr. Dobb's Journal; a copy of the article is available in
  10273. +http://dogma.net/markn/articles/zlibtool/zlibtool.htm
  10274. +
  10275. +The changes made in version 1.1.4 are documented in the file ChangeLog.
  10276. +The only changes made since 1.1.3 are bug corrections:
  10277. +
  10278. +- ZFREE was repeated on same allocation on some error conditions.
  10279. + This creates a security problem described in
  10280. + http://www.zlib.org/advisory-2002-03-11.txt
  10281. +- Returned incorrect error (Z_MEM_ERROR) on some invalid data
  10282. +- Avoid accesses before window for invalid distances with inflate window
  10283. + less than 32K.
  10284. +- force windowBits > 8 to avoid a bug in the encoder for a window size
  10285. + of 256 bytes. (A complete fix will be available in 1.1.5).
  10286. +
  10287. +The beta version 1.1.5beta includes many more changes. A new official
  10288. +version 1.1.5 will be released as soon as extensive testing has been
  10289. +completed on it.
  10290. +
  10291. +
  10292. +Unsupported third party contributions are provided in directory "contrib".
  10293. +
  10294. +A Java implementation of zlib is available in the Java Development Kit
  10295. +http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html
  10296. +See the zlib home page http://www.zlib.org for details.
  10297. +
  10298. +A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk>
  10299. +is in the CPAN (Comprehensive Perl Archive Network) sites
  10300. +http://www.cpan.org/modules/by-module/Compress/
  10301. +
  10302. +A Python interface to zlib written by A.M. Kuchling <amk@magnet.com>
  10303. +is available in Python 1.5 and later versions, see
  10304. +http://www.python.org/doc/lib/module-zlib.html
  10305. +
  10306. +A zlib binding for TCL written by Andreas Kupries <a.kupries@westend.com>
  10307. +is availlable at http://www.westend.com/~kupries/doc/trf/man/man.html
  10308. +
  10309. +An experimental package to read and write files in .zip format,
  10310. +written on top of zlib by Gilles Vollant <info@winimage.com>, is
  10311. +available at http://www.winimage.com/zLibDll/unzip.html
  10312. +and also in the contrib/minizip directory of zlib.
  10313. +
  10314. +
  10315. +Notes for some targets:
  10316. +
  10317. +- To build a Windows DLL version, include in a DLL project zlib.def, zlib.rc
  10318. + and all .c files except example.c and minigzip.c; compile with -DZLIB_DLL
  10319. + The zlib DLL support was initially done by Alessandro Iacopetti and is
  10320. + now maintained by Gilles Vollant <info@winimage.com>. Check the zlib DLL
  10321. + home page at http://www.winimage.com/zLibDll
  10322. +
  10323. + From Visual Basic, you can call the DLL functions which do not take
  10324. + a structure as argument: compress, uncompress and all gz* functions.
  10325. + See contrib/visual-basic.txt for more information, or get
  10326. + http://www.tcfb.com/dowseware/cmp-z-it.zip
  10327. +
  10328. +- For 64-bit Irix, deflate.c must be compiled without any optimization.
  10329. + With -O, one libpng test fails. The test works in 32 bit mode (with
  10330. + the -n32 compiler flag). The compiler bug has been reported to SGI.
  10331. +
  10332. +- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1
  10333. + it works when compiled with cc.
  10334. +
  10335. +- on Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1
  10336. + is necessary to get gzprintf working correctly. This is done by configure.
  10337. +
  10338. +- zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works
  10339. + with other compilers. Use "make test" to check your compiler.
  10340. +
  10341. +- gzdopen is not supported on RISCOS, BEOS and by some Mac compilers.
  10342. +
  10343. +- For Turbo C the small model is supported only with reduced performance to
  10344. + avoid any far allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
  10345. +
  10346. +- For PalmOs, see http://www.cs.uit.no/~perm/PASTA/pilot/software.html
  10347. + Per Harald Myrvang <perm@stud.cs.uit.no>
  10348. +
  10349. +
  10350. +Acknowledgments:
  10351. +
  10352. + The deflate format used by zlib was defined by Phil Katz. The deflate
  10353. + and zlib specifications were written by L. Peter Deutsch. Thanks to all the
  10354. + people who reported problems and suggested various improvements in zlib;
  10355. + they are too numerous to cite here.
  10356. +
  10357. +Copyright notice:
  10358. +
  10359. + (C) 1995-2002 Jean-loup Gailly and Mark Adler
  10360. +
  10361. + This software is provided 'as-is', without any express or implied
  10362. + warranty. In no event will the authors be held liable for any damages
  10363. + arising from the use of this software.
  10364. +
  10365. + Permission is granted to anyone to use this software for any purpose,
  10366. + including commercial applications, and to alter it and redistribute it
  10367. + freely, subject to the following restrictions:
  10368. +
  10369. + 1. The origin of this software must not be misrepresented; you must not
  10370. + claim that you wrote the original software. If you use this software
  10371. + in a product, an acknowledgment in the product documentation would be
  10372. + appreciated but is not required.
  10373. + 2. Altered source versions must be plainly marked as such, and must not be
  10374. + misrepresented as being the original software.
  10375. + 3. This notice may not be removed or altered from any source distribution.
  10376. +
  10377. + Jean-loup Gailly Mark Adler
  10378. + jloup@gzip.org madler@alumni.caltech.edu
  10379. +
  10380. +If you use the zlib library in a product, we would appreciate *not*
  10381. +receiving lengthy legal documents to sign. The sources are provided
  10382. +for free but without warranty of any kind. The library has been
  10383. +entirely written by Jean-loup Gailly and Mark Adler; it does not
  10384. +include third-party code.
  10385. +
  10386. +If you redistribute modified sources, we would appreciate that you include
  10387. +in the file ChangeLog history information documenting your changes.
  10388. --- /dev/null Tue Mar 11 13:02:56 2003
  10389. +++ linux/net/ipsec/README-zlib.freeswan Mon Feb 9 13:51:03 2004
  10390. @@ -0,0 +1,13 @@
  10391. +The only changes made to these files for use in FreeS/WAN are:
  10392. +
  10393. + - In zconf.h, macros are defined to prefix global symbols with "ipcomp_"
  10394. + (or "_ipcomp"), when compiled with -DIPCOMP_PREFIX.
  10395. + - The copyright strings are defined local (static)
  10396. +
  10397. + The above changes are made to avoid name collisions with ppp_deflate
  10398. + and ext2compr.
  10399. +
  10400. + - Files not needed for FreeS/WAN have been removed
  10401. +
  10402. + See the "README" file for information about where to obtain the complete
  10403. + zlib package.
  10404. --- /dev/null Tue Mar 11 13:02:56 2003
  10405. +++ linux/net/ipsec/addrtoa.c Mon Feb 9 13:51:03 2004
  10406. @@ -0,0 +1,67 @@
  10407. +/*
  10408. + * addresses to ASCII
  10409. + * Copyright (C) 1998, 1999 Henry Spencer.
  10410. + *
  10411. + * This library is free software; you can redistribute it and/or modify it
  10412. + * under the terms of the GNU Library General Public License as published by
  10413. + * the Free Software Foundation; either version 2 of the License, or (at your
  10414. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10415. + *
  10416. + * This library is distributed in the hope that it will be useful, but
  10417. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10418. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10419. + * License for more details.
  10420. + *
  10421. + * RCSID $Id: addrtoa.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  10422. + */
  10423. +#include "openswan.h"
  10424. +
  10425. +#define NBYTES 4 /* bytes in an address */
  10426. +#define PERBYTE 4 /* three digits plus a dot or NUL */
  10427. +#define BUFLEN (NBYTES*PERBYTE)
  10428. +
  10429. +#if BUFLEN != ADDRTOA_BUF
  10430. +#error "ADDRTOA_BUF in openswan.h inconsistent with addrtoa() code"
  10431. +#endif
  10432. +
  10433. +/*
  10434. + - addrtoa - convert binary address to ASCII dotted decimal
  10435. + */
  10436. +size_t /* space needed for full conversion */
  10437. +addrtoa(addr, format, dst, dstlen)
  10438. +struct in_addr addr;
  10439. +int format; /* character */
  10440. +char *dst; /* need not be valid if dstlen is 0 */
  10441. +size_t dstlen;
  10442. +{
  10443. + unsigned long a = ntohl(addr.s_addr);
  10444. + int i;
  10445. + size_t n;
  10446. + unsigned long byte;
  10447. + char buf[BUFLEN];
  10448. + char *p;
  10449. +
  10450. + switch (format) {
  10451. + case 0:
  10452. + break;
  10453. + default:
  10454. + return 0;
  10455. + break;
  10456. + }
  10457. +
  10458. + p = buf;
  10459. + for (i = NBYTES-1; i >= 0; i--) {
  10460. + byte = (a >> (i*8)) & 0xff;
  10461. + p += ultoa(byte, 10, p, PERBYTE);
  10462. + if (i != 0)
  10463. + *(p-1) = '.';
  10464. + }
  10465. + n = p - buf;
  10466. +
  10467. + if (dstlen > 0) {
  10468. + if (n > dstlen)
  10469. + buf[dstlen - 1] = '\0';
  10470. + strcpy(dst, buf);
  10471. + }
  10472. + return n;
  10473. +}
  10474. --- /dev/null Tue Mar 11 13:02:56 2003
  10475. +++ linux/net/ipsec/addrtot.c Mon Feb 9 13:51:03 2004
  10476. @@ -0,0 +1,423 @@
  10477. +/*
  10478. + * addresses to text
  10479. + * Copyright (C) 2000 Henry Spencer.
  10480. + *
  10481. + * This library is free software; you can redistribute it and/or modify it
  10482. + * under the terms of the GNU Library General Public License as published by
  10483. + * the Free Software Foundation; either version 2 of the License, or (at your
  10484. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10485. + *
  10486. + * This library is distributed in the hope that it will be useful, but
  10487. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10488. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10489. + * License for more details.
  10490. + *
  10491. + * RCSID $Id: addrtot.c,v 1.22.2.1 2005/11/17 22:30:49 paul Exp $
  10492. + */
  10493. +
  10494. +#if defined(__KERNEL__) && defined(__HAVE_ARCH_STRSTR)
  10495. +#include <linux/string.h>
  10496. +#endif
  10497. +
  10498. +#include "openswan.h"
  10499. +
  10500. +#define IP4BYTES 4 /* bytes in an IPv4 address */
  10501. +#define PERBYTE 4 /* three digits plus a dot or NUL */
  10502. +#define IP6BYTES 16 /* bytes in an IPv6 address */
  10503. +
  10504. +/* forwards */
  10505. +static size_t normal4(const unsigned char *s, size_t len, char *b, char **dp);
  10506. +static size_t normal6(const unsigned char *s, size_t len, char *b, char **dp, int squish);
  10507. +static size_t reverse4(const unsigned char *s, size_t len, char *b, char **dp);
  10508. +static size_t reverse6(const unsigned char *s, size_t len, char *b, char **dp);
  10509. +
  10510. +#if defined(__KERNEL__) && !defined(__HAVE_ARCH_STRSTR)
  10511. +#define strstr ipsec_strstr
  10512. +/*
  10513. + * Find the first occurrence of find in s.
  10514. + * (from NetBSD 1.6's /src/lib/libc/string/strstr.c)
  10515. + */
  10516. +static char *
  10517. +strstr(s, find)
  10518. + const char *s, *find;
  10519. +{
  10520. + char c, sc;
  10521. + size_t len;
  10522. +
  10523. + if ((c = *find++) != 0) {
  10524. + len = strlen(find);
  10525. + do {
  10526. + do {
  10527. + if ((sc = *s++) == 0)
  10528. + return (NULL);
  10529. + } while (sc != c);
  10530. + } while (strncmp(s, find, len) != 0);
  10531. + s--;
  10532. + }
  10533. + /* LINTED interface specification */
  10534. + return ((char *)s);
  10535. +}
  10536. +#endif
  10537. +
  10538. +/*
  10539. + - addrtot - convert binary address to text (dotted decimal or IPv6 string)
  10540. + */
  10541. +size_t /* space needed for full conversion */
  10542. +addrtot(src, format, dst, dstlen)
  10543. +const ip_address *src;
  10544. +int format; /* character */
  10545. +char *dst; /* need not be valid if dstlen is 0 */
  10546. +size_t dstlen;
  10547. +{
  10548. + const unsigned char *b;
  10549. + size_t n;
  10550. + char buf[1+ADDRTOT_BUF+1]; /* :address: */
  10551. + char *p;
  10552. + int t = addrtypeof(src);
  10553. +# define TF(t, f) (((t)<<8) | (f))
  10554. +
  10555. + n = addrbytesptr(src, &b);
  10556. + if (n == 0) {
  10557. + bad:
  10558. + dst[0]='\0';
  10559. + strncat(dst, "<invalid>", dstlen);
  10560. + return sizeof("<invalid>");
  10561. + }
  10562. +
  10563. + switch (TF(t, format)) {
  10564. + case TF(AF_INET, 0):
  10565. + n = normal4(b, n, buf, &p);
  10566. + break;
  10567. + case TF(AF_INET6, 0):
  10568. + n = normal6(b, n, buf, &p, 1);
  10569. + break;
  10570. + case TF(AF_INET, 'Q'):
  10571. + n = normal4(b, n, buf, &p);
  10572. + break;
  10573. + case TF(AF_INET6, 'Q'):
  10574. + n = normal6(b, n, buf, &p, 0);
  10575. + break;
  10576. + case TF(AF_INET, 'r'):
  10577. + n = reverse4(b, n, buf, &p);
  10578. + break;
  10579. + case TF(AF_INET6, 'r'):
  10580. + n = reverse6(b, n, buf, &p);
  10581. + break;
  10582. + default: /* including (AF_INET, 'R') */
  10583. + goto bad;
  10584. + break;
  10585. + }
  10586. +
  10587. + if (dstlen > 0) {
  10588. + if (dstlen < n)
  10589. + p[dstlen - 1] = '\0';
  10590. + strcpy(dst, p);
  10591. + }
  10592. + return n;
  10593. +}
  10594. +
  10595. +/*
  10596. + - normal4 - normal IPv4 address-text conversion
  10597. + */
  10598. +static size_t /* size of text, including NUL */
  10599. +normal4(srcp, srclen, buf, dstp)
  10600. +const unsigned char *srcp;
  10601. +size_t srclen;
  10602. +char *buf; /* guaranteed large enough */
  10603. +char **dstp; /* where to put result pointer */
  10604. +{
  10605. + int i;
  10606. + char *p;
  10607. +
  10608. + if (srclen != IP4BYTES) /* "can't happen" */
  10609. + return 0;
  10610. + p = buf;
  10611. + for (i = 0; i < IP4BYTES; i++) {
  10612. + p += ultot(srcp[i], 10, p, PERBYTE);
  10613. + if (i != IP4BYTES - 1)
  10614. + *(p-1) = '.'; /* overwrites the NUL */
  10615. + }
  10616. + *dstp = buf;
  10617. + return p - buf;
  10618. +}
  10619. +
  10620. +/*
  10621. + - normal6 - normal IPv6 address-text conversion
  10622. + */
  10623. +static size_t /* size of text, including NUL */
  10624. +normal6(srcp, srclen, buf, dstp, squish)
  10625. +const unsigned char *srcp;
  10626. +size_t srclen;
  10627. +char *buf; /* guaranteed large enough, plus 2 */
  10628. +char **dstp; /* where to put result pointer */
  10629. +int squish; /* whether to squish out 0:0 */
  10630. +{
  10631. + int i;
  10632. + unsigned long piece;
  10633. + char *p;
  10634. + char *q;
  10635. +
  10636. + if (srclen != IP6BYTES) /* "can't happen" */
  10637. + return 0;
  10638. + p = buf;
  10639. + *p++ = ':';
  10640. + for (i = 0; i < IP6BYTES/2; i++) {
  10641. + piece = (srcp[2*i] << 8) + srcp[2*i + 1];
  10642. + p += ultot(piece, 16, p, 5); /* 5 = abcd + NUL */
  10643. + *(p-1) = ':'; /* overwrites the NUL */
  10644. + }
  10645. + *p = '\0';
  10646. + q = strstr(buf, ":0:0:");
  10647. + if (squish && q != NULL) { /* zero squishing is possible */
  10648. + p = q + 1;
  10649. + while (*p == '0' && *(p+1) == ':')
  10650. + p += 2;
  10651. + q++;
  10652. + *q++ = ':'; /* overwrite first 0 */
  10653. + while (*p != '\0')
  10654. + *q++ = *p++;
  10655. + *q = '\0';
  10656. + if (!(*(q-1) == ':' && *(q-2) == ':'))
  10657. + *--q = '\0'; /* strip final : unless :: */
  10658. + p = buf;
  10659. + if (!(*p == ':' && *(p+1) == ':'))
  10660. + p++; /* skip initial : unless :: */
  10661. + } else {
  10662. + q = p;
  10663. + *--q = '\0'; /* strip final : */
  10664. + p = buf + 1; /* skip initial : */
  10665. + }
  10666. + *dstp = p;
  10667. + return q - p + 1;
  10668. +}
  10669. +
  10670. +/*
  10671. + - reverse4 - IPv4 reverse-lookup conversion
  10672. + */
  10673. +static size_t /* size of text, including NUL */
  10674. +reverse4(srcp, srclen, buf, dstp)
  10675. +const unsigned char *srcp;
  10676. +size_t srclen;
  10677. +char *buf; /* guaranteed large enough */
  10678. +char **dstp; /* where to put result pointer */
  10679. +{
  10680. + int i;
  10681. + char *p;
  10682. +
  10683. + if (srclen != IP4BYTES) /* "can't happen" */
  10684. + return 0;
  10685. + p = buf;
  10686. + for (i = IP4BYTES-1; i >= 0; i--) {
  10687. + p += ultot(srcp[i], 10, p, PERBYTE);
  10688. + *(p-1) = '.'; /* overwrites the NUL */
  10689. + }
  10690. + strcpy(p, "IN-ADDR.ARPA.");
  10691. + *dstp = buf;
  10692. + return strlen(buf) + 1;
  10693. +}
  10694. +
  10695. +/*
  10696. + - reverse6 - IPv6 reverse-lookup conversion (RFC 1886)
  10697. + * A trifle inefficient, really shouldn't use ultot...
  10698. + */
  10699. +static size_t /* size of text, including NUL */
  10700. +reverse6(srcp, srclen, buf, dstp)
  10701. +const unsigned char *srcp;
  10702. +size_t srclen;
  10703. +char *buf; /* guaranteed large enough */
  10704. +char **dstp; /* where to put result pointer */
  10705. +{
  10706. + int i;
  10707. + unsigned long piece;
  10708. + char *p;
  10709. +
  10710. + if (srclen != IP6BYTES) /* "can't happen" */
  10711. + return 0;
  10712. + p = buf;
  10713. + for (i = IP6BYTES-1; i >= 0; i--) {
  10714. + piece = srcp[i];
  10715. + p += ultot(piece&0xf, 16, p, 2);
  10716. + *(p-1) = '.';
  10717. + p += ultot(piece>>4, 16, p, 2);
  10718. + *(p-1) = '.';
  10719. + }
  10720. + strcpy(p, "IP6.ARPA.");
  10721. + *dstp = buf;
  10722. + return strlen(buf) + 1;
  10723. +}
  10724. +
  10725. +/*
  10726. + - reverse6 - modern IPv6 reverse-lookup conversion (RFC 2874)
  10727. + * this version removed as it was obsoleted in the end.
  10728. + */
  10729. +
  10730. +#ifdef ADDRTOT_MAIN
  10731. +
  10732. +#include <stdio.h>
  10733. +#include <sys/socket.h>
  10734. +#include <netinet/in.h>
  10735. +#include <arpa/inet.h>
  10736. +
  10737. +void regress(void);
  10738. +
  10739. +int
  10740. +main(int argc, char *argv[])
  10741. +{
  10742. + if (argc < 2) {
  10743. + fprintf(stderr, "Usage: %s {addr|net/mask|begin...end|-r}\n",
  10744. + argv[0]);
  10745. + exit(2);
  10746. + }
  10747. +
  10748. + if (strcmp(argv[1], "-r") == 0) {
  10749. + regress();
  10750. + fprintf(stderr, "regress() returned?!?\n");
  10751. + exit(1);
  10752. + }
  10753. + exit(0);
  10754. +}
  10755. +
  10756. +struct rtab {
  10757. + char *input;
  10758. + char format;
  10759. + char *output; /* NULL means error expected */
  10760. +} rtab[] = {
  10761. + {"1.2.3.0", 0, "1.2.3.0"},
  10762. + {"1:2::3:4", 0, "1:2::3:4"},
  10763. + {"1:2::3:4", 'Q', "1:2:0:0:0:0:3:4"},
  10764. + {"1:2:0:0:3:4:0:0", 0, "1:2::3:4:0:0"},
  10765. + {"1.2.3.4", 'r' , "4.3.2.1.IN-ADDR.ARPA."},
  10766. + /* 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 */
  10767. + {"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."},
  10768. + {NULL, 0, NULL}
  10769. +};
  10770. +
  10771. +void
  10772. +regress()
  10773. +{
  10774. + struct rtab *r;
  10775. + int status = 0;
  10776. + ip_address a;
  10777. + char in[100];
  10778. + char buf[100];
  10779. + const char *oops;
  10780. + size_t n;
  10781. +
  10782. + for (r = rtab; r->input != NULL; r++) {
  10783. + strcpy(in, r->input);
  10784. +
  10785. + /* convert it *to* internal format */
  10786. + oops = ttoaddr(in, strlen(in), 0, &a);
  10787. +
  10788. + /* now convert it back */
  10789. +
  10790. + n = addrtot(&a, r->format, buf, sizeof(buf));
  10791. +
  10792. + if (n == 0 && r->output == NULL)
  10793. + {} /* okay, error expected */
  10794. +
  10795. + else if (n == 0) {
  10796. + printf("`%s' atoasr failed\n", r->input);
  10797. + status = 1;
  10798. +
  10799. + } else if (r->output == NULL) {
  10800. + printf("`%s' atoasr succeeded unexpectedly '%c'\n",
  10801. + r->input, r->format);
  10802. + status = 1;
  10803. + } else {
  10804. + if (strcasecmp(r->output, buf) != 0) {
  10805. + printf("`%s' '%c' gave `%s', expected `%s'\n",
  10806. + r->input, r->format, buf, r->output);
  10807. + status = 1;
  10808. + }
  10809. + }
  10810. + }
  10811. + exit(status);
  10812. +}
  10813. +
  10814. +#endif /* ADDRTOT_MAIN */
  10815. +
  10816. +/*
  10817. + * $Log: addrtot.c,v $
  10818. + * Revision 1.22.2.1 2005/11/17 22:30:49 paul
  10819. + * pull up strstr fix from head.
  10820. + *
  10821. + * Revision 1.22 2005/05/20 16:47:40 mcr
  10822. + * make strstr static if we need it.
  10823. + *
  10824. + * Revision 1.21 2005/03/21 00:35:12 mcr
  10825. + * test for strstr properly
  10826. + *
  10827. + * Revision 1.20 2004/11/09 22:52:20 mcr
  10828. + * until we figure out which kernels have strsep and which
  10829. + * do not (UML does not under certain circumstances), then
  10830. + * let's just provide our own.
  10831. + *
  10832. + * Revision 1.19 2004/10/08 16:30:33 mcr
  10833. + * pull-up of initial crypto-offload work.
  10834. + *
  10835. + * Revision 1.18 2004/09/18 19:33:08 mcr
  10836. + * use an appropriate kernel happy ifdef for strstr.
  10837. + *
  10838. + * Revision 1.17 2004/09/15 21:49:02 mcr
  10839. + * use local copy of strstr() if this is going in the kernel.
  10840. + * Not clear why this worked before, or why this shows up
  10841. + * for modules only.
  10842. + *
  10843. + * Revision 1.16 2004/07/10 07:43:47 mcr
  10844. + * Moved from linux/lib/libfreeswan/addrtot.c,v
  10845. + *
  10846. + * Revision 1.15 2004/04/11 17:39:25 mcr
  10847. + * removed internal.h requirements.
  10848. + *
  10849. + * Revision 1.14 2004/03/08 01:59:08 ken
  10850. + * freeswan.h -> openswan.h
  10851. + *
  10852. + * Revision 1.13 2004/01/05 23:21:05 mcr
  10853. + * if the address type is invalid, then return length of <invalid>
  10854. + * string!
  10855. + *
  10856. + * Revision 1.12 2003/12/30 06:42:48 mcr
  10857. + * added $Log: addrtot.c,v $
  10858. + * added Revision 1.22.2.1 2005/11/17 22:30:49 paul
  10859. + * added pull up strstr fix from head.
  10860. + * added
  10861. + * added Revision 1.22 2005/05/20 16:47:40 mcr
  10862. + * added make strstr static if we need it.
  10863. + * added
  10864. + * added Revision 1.21 2005/03/21 00:35:12 mcr
  10865. + * added test for strstr properly
  10866. + * added
  10867. + * added Revision 1.20 2004/11/09 22:52:20 mcr
  10868. + * added until we figure out which kernels have strsep and which
  10869. + * added do not (UML does not under certain circumstances), then
  10870. + * added let's just provide our own.
  10871. + * added
  10872. + * added Revision 1.19 2004/10/08 16:30:33 mcr
  10873. + * added pull-up of initial crypto-offload work.
  10874. + * added
  10875. + * added Revision 1.18 2004/09/18 19:33:08 mcr
  10876. + * added use an appropriate kernel happy ifdef for strstr.
  10877. + * added
  10878. + * added Revision 1.17 2004/09/15 21:49:02 mcr
  10879. + * added use local copy of strstr() if this is going in the kernel.
  10880. + * added Not clear why this worked before, or why this shows up
  10881. + * added for modules only.
  10882. + * added
  10883. + * added Revision 1.16 2004/07/10 07:43:47 mcr
  10884. + * added Moved from linux/lib/libfreeswan/addrtot.c,v
  10885. + * added
  10886. + * added Revision 1.15 2004/04/11 17:39:25 mcr
  10887. + * added removed internal.h requirements.
  10888. + * added
  10889. + * added Revision 1.14 2004/03/08 01:59:08 ken
  10890. + * added freeswan.h -> openswan.h
  10891. + * added
  10892. + * added Revision 1.13 2004/01/05 23:21:05 mcr
  10893. + * added if the address type is invalid, then return length of <invalid>
  10894. + * added string!
  10895. + * added
  10896. + *
  10897. + *
  10898. + */
  10899. +
  10900. --- /dev/null Tue Mar 11 13:02:56 2003
  10901. +++ linux/net/ipsec/addrtypeof.c Mon Feb 9 13:51:03 2004
  10902. @@ -0,0 +1,93 @@
  10903. +/*
  10904. + * extract parts of an ip_address
  10905. + * Copyright (C) 2000 Henry Spencer.
  10906. + *
  10907. + * This library is free software; you can redistribute it and/or modify it
  10908. + * under the terms of the GNU Library General Public License as published by
  10909. + * the Free Software Foundation; either version 2 of the License, or (at your
  10910. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10911. + *
  10912. + * This library is distributed in the hope that it will be useful, but
  10913. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10914. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10915. + * License for more details.
  10916. + *
  10917. + * RCSID $Id: addrtypeof.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  10918. + */
  10919. +#include "openswan.h"
  10920. +
  10921. +/*
  10922. + - addrtypeof - get the type of an ip_address
  10923. + */
  10924. +int
  10925. +addrtypeof(src)
  10926. +const ip_address *src;
  10927. +{
  10928. + return src->u.v4.sin_family;
  10929. +}
  10930. +
  10931. +/*
  10932. + - addrbytesptr - get pointer to the address bytes of an ip_address
  10933. + */
  10934. +size_t /* 0 for error */
  10935. +addrbytesptr(src, dstp)
  10936. +const ip_address *src;
  10937. +const unsigned char **dstp; /* NULL means just a size query */
  10938. +{
  10939. + const unsigned char *p;
  10940. + size_t n;
  10941. +
  10942. + switch (src->u.v4.sin_family) {
  10943. + case AF_INET:
  10944. + p = (const unsigned char *)&src->u.v4.sin_addr.s_addr;
  10945. + n = 4;
  10946. + break;
  10947. + case AF_INET6:
  10948. + p = (const unsigned char *)&src->u.v6.sin6_addr;
  10949. + n = 16;
  10950. + break;
  10951. + default:
  10952. + return 0;
  10953. + break;
  10954. + }
  10955. +
  10956. + if (dstp != NULL)
  10957. + *dstp = p;
  10958. + return n;
  10959. +}
  10960. +
  10961. +/*
  10962. + - addrlenof - get length of the address bytes of an ip_address
  10963. + */
  10964. +size_t /* 0 for error */
  10965. +addrlenof(src)
  10966. +const ip_address *src;
  10967. +{
  10968. + return addrbytesptr(src, NULL);
  10969. +}
  10970. +
  10971. +/*
  10972. + - addrbytesof - get the address bytes of an ip_address
  10973. + */
  10974. +size_t /* 0 for error */
  10975. +addrbytesof(src, dst, dstlen)
  10976. +const ip_address *src;
  10977. +unsigned char *dst;
  10978. +size_t dstlen;
  10979. +{
  10980. + const unsigned char *p;
  10981. + size_t n;
  10982. + size_t ncopy;
  10983. +
  10984. + n = addrbytesptr(src, &p);
  10985. + if (n == 0)
  10986. + return 0;
  10987. +
  10988. + if (dstlen > 0) {
  10989. + ncopy = n;
  10990. + if (ncopy > dstlen)
  10991. + ncopy = dstlen;
  10992. + memcpy(dst, p, ncopy);
  10993. + }
  10994. + return n;
  10995. +}
  10996. --- /dev/null Tue Mar 11 13:02:56 2003
  10997. +++ linux/net/ipsec/adler32.c Mon Feb 9 13:51:03 2004
  10998. @@ -0,0 +1,49 @@
  10999. +/* adler32.c -- compute the Adler-32 checksum of a data stream
  11000. + * Copyright (C) 1995-2002 Mark Adler
  11001. + * For conditions of distribution and use, see copyright notice in zlib.h
  11002. + */
  11003. +
  11004. +/* @(#) $Id: adler32.c,v 1.6 2004/07/10 19:11:18 mcr Exp $ */
  11005. +
  11006. +#include <zlib/zlib.h>
  11007. +#include <zlib/zconf.h>
  11008. +
  11009. +#define BASE 65521L /* largest prime smaller than 65536 */
  11010. +#define NMAX 5552
  11011. +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  11012. +
  11013. +#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  11014. +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  11015. +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  11016. +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  11017. +#define DO16(buf) DO8(buf,0); DO8(buf,8);
  11018. +
  11019. +/* ========================================================================= */
  11020. +uLong ZEXPORT adler32(adler, buf, len)
  11021. + uLong adler;
  11022. + const Bytef *buf;
  11023. + uInt len;
  11024. +{
  11025. + unsigned long s1 = adler & 0xffff;
  11026. + unsigned long s2 = (adler >> 16) & 0xffff;
  11027. + int k;
  11028. +
  11029. + if (buf == Z_NULL) return 1L;
  11030. +
  11031. + while (len > 0) {
  11032. + k = len < NMAX ? len : NMAX;
  11033. + len -= k;
  11034. + while (k >= 16) {
  11035. + DO16(buf);
  11036. + buf += 16;
  11037. + k -= 16;
  11038. + }
  11039. + if (k != 0) do {
  11040. + s1 += *buf++;
  11041. + s2 += s1;
  11042. + } while (--k);
  11043. + s1 %= BASE;
  11044. + s2 %= BASE;
  11045. + }
  11046. + return (s2 << 16) | s1;
  11047. +}
  11048. --- /dev/null Tue Mar 11 13:02:56 2003
  11049. +++ linux/net/ipsec/aes/Makefile Mon Feb 9 13:51:03 2004
  11050. @@ -0,0 +1,59 @@
  11051. +# Makefile for KLIPS 3DES kernel code as a module for 2.6 kernels
  11052. +#
  11053. +# Makefile for KLIPS kernel code as a module
  11054. +# Copyright (C) 2002-2004 Michael Richardson <mcr@xelerance.com>
  11055. +#
  11056. +# This program is free software; you can redistribute it and/or modify it
  11057. +# under the terms of the GNU General Public License as published by the
  11058. +# Free Software Foundation; either version 2 of the License, or (at your
  11059. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11060. +#
  11061. +# This program is distributed in the hope that it will be useful, but
  11062. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11063. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11064. +# for more details.
  11065. +#
  11066. +# RCSID $Id: Makefile.fs2_6,v 1.1.10.1 2005/08/12 16:10:05 ken Exp $
  11067. +#
  11068. +# Note! Dependencies are done automagically by 'make dep', which also
  11069. +# removes any old dependencies. DON'T put your own dependencies here
  11070. +# unless it's something special (ie not a .c file).
  11071. +#
  11072. +
  11073. +obj-$(CONFIG_KLIPS_ENC_AES) += ipsec_alg_aes.o
  11074. +obj-$(CONFIG_KLIPS_ENC_AES) += aes_xcbc_mac.o
  11075. +obj-$(CONFIG_KLIPS_ENC_AES) += aes_cbc.o
  11076. +
  11077. +ifeq ($(strip ${SUBARCH}),)
  11078. +SUBARCH:=${ARCH}
  11079. +endif
  11080. +
  11081. +# the assembly version expects frame pointers, which are
  11082. +# optional in many kernel builds. If you want speed, you should
  11083. +# probably use cryptoapi code instead.
  11084. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  11085. +ifeq (${USEASSEMBLY},i386y)
  11086. +obj-$(CONFIG_KLIPS_ENC_AES) += aes-i586.o
  11087. +else
  11088. +obj-$(CONFIG_KLIPS_ENC_AES) += aes.o
  11089. +endif
  11090. +
  11091. +
  11092. +#
  11093. +# $Log: Makefile.fs2_6,v $
  11094. +# Revision 1.1.10.1 2005/08/12 16:10:05 ken
  11095. +# do not use assembly code with there are no frame pointers
  11096. +#
  11097. +# Revision 1.2 2005/08/12 14:13:58 mcr
  11098. +# do not use assembly code with there are no frame pointers,
  11099. +# as it does not have the right linkages.
  11100. +#
  11101. +# Revision 1.1 2004/08/17 03:31:34 mcr
  11102. +# klips 2.6 edits.
  11103. +#
  11104. +#
  11105. +# Local Variables:
  11106. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  11107. +# End Variables:
  11108. +#
  11109. +
  11110. --- /dev/null Tue Mar 11 13:02:56 2003
  11111. +++ linux/net/ipsec/aes/aes-i586.S Mon Feb 9 13:51:03 2004
  11112. @@ -0,0 +1,892 @@
  11113. +//
  11114. +// Copyright (c) 2001, Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK.
  11115. +// All rights reserved.
  11116. +//
  11117. +// TERMS
  11118. +//
  11119. +// Redistribution and use in source and binary forms, with or without
  11120. +// modification, are permitted subject to the following conditions:
  11121. +//
  11122. +// 1. Redistributions of source code must retain the above copyright
  11123. +// notice, this list of conditions and the following disclaimer.
  11124. +//
  11125. +// 2. Redistributions in binary form must reproduce the above copyright
  11126. +// notice, this list of conditions and the following disclaimer in the
  11127. +// documentation and/or other materials provided with the distribution.
  11128. +//
  11129. +// 3. The copyright holder's name must not be used to endorse or promote
  11130. +// any products derived from this software without his specific prior
  11131. +// written permission.
  11132. +//
  11133. +// This software is provided 'as is' with no express or implied warranties
  11134. +// of correctness or fitness for purpose.
  11135. +
  11136. +// Modified by Jari Ruusu, December 24 2001
  11137. +// - Converted syntax to GNU CPP/assembler syntax
  11138. +// - C programming interface converted back to "old" API
  11139. +// - Minor portability cleanups and speed optimizations
  11140. +
  11141. +// An AES (Rijndael) implementation for the Pentium. This version only
  11142. +// implements the standard AES block length (128 bits, 16 bytes). This code
  11143. +// does not preserve the eax, ecx or edx registers or the artihmetic status
  11144. +// flags. However, the ebx, esi, edi, and ebp registers are preserved across
  11145. +// calls.
  11146. +
  11147. +// void aes_set_key(aes_context *cx, const unsigned char key[], const int key_len, const int f)
  11148. +// void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  11149. +// void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  11150. +
  11151. +#if defined(USE_UNDERLINE)
  11152. +# define aes_set_key _aes_set_key
  11153. +# define aes_encrypt _aes_encrypt
  11154. +# define aes_decrypt _aes_decrypt
  11155. +#endif
  11156. +#if !defined(ALIGN32BYTES)
  11157. +# define ALIGN32BYTES 32
  11158. +#endif
  11159. +
  11160. + .file "aes-i586.S"
  11161. + .globl aes_set_key
  11162. + .globl aes_encrypt
  11163. + .globl aes_decrypt
  11164. +
  11165. +#define tlen 1024 // length of each of 4 'xor' arrays (256 32-bit words)
  11166. +
  11167. +// offsets to parameters with one register pushed onto stack
  11168. +
  11169. +#define ctx 8 // AES context structure
  11170. +#define in_blk 12 // input byte array address parameter
  11171. +#define out_blk 16 // output byte array address parameter
  11172. +
  11173. +// offsets in context structure
  11174. +
  11175. +#define nkey 0 // key length, size 4
  11176. +#define nrnd 4 // number of rounds, size 4
  11177. +#define ekey 8 // encryption key schedule base address, size 256
  11178. +#define dkey 264 // decryption key schedule base address, size 256
  11179. +
  11180. +// This macro performs a forward encryption cycle. It is entered with
  11181. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  11182. +// exits with the final values in the same registers.
  11183. +
  11184. +#define fwd_rnd(p1,p2) \
  11185. + mov %ebx,(%esp) ;\
  11186. + movzbl %al,%edx ;\
  11187. + mov %eax,%ecx ;\
  11188. + mov p2(%ebp),%eax ;\
  11189. + mov %edi,4(%esp) ;\
  11190. + mov p2+12(%ebp),%edi ;\
  11191. + xor p1(,%edx,4),%eax ;\
  11192. + movzbl %ch,%edx ;\
  11193. + shr $16,%ecx ;\
  11194. + mov p2+4(%ebp),%ebx ;\
  11195. + xor p1+tlen(,%edx,4),%edi ;\
  11196. + movzbl %cl,%edx ;\
  11197. + movzbl %ch,%ecx ;\
  11198. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  11199. + mov %esi,%ecx ;\
  11200. + mov p1+2*tlen(,%edx,4),%esi ;\
  11201. + movzbl %cl,%edx ;\
  11202. + xor p1(,%edx,4),%esi ;\
  11203. + movzbl %ch,%edx ;\
  11204. + shr $16,%ecx ;\
  11205. + xor p1+tlen(,%edx,4),%ebx ;\
  11206. + movzbl %cl,%edx ;\
  11207. + movzbl %ch,%ecx ;\
  11208. + xor p1+2*tlen(,%edx,4),%eax ;\
  11209. + mov (%esp),%edx ;\
  11210. + xor p1+3*tlen(,%ecx,4),%edi ;\
  11211. + movzbl %dl,%ecx ;\
  11212. + xor p2+8(%ebp),%esi ;\
  11213. + xor p1(,%ecx,4),%ebx ;\
  11214. + movzbl %dh,%ecx ;\
  11215. + shr $16,%edx ;\
  11216. + xor p1+tlen(,%ecx,4),%eax ;\
  11217. + movzbl %dl,%ecx ;\
  11218. + movzbl %dh,%edx ;\
  11219. + xor p1+2*tlen(,%ecx,4),%edi ;\
  11220. + mov 4(%esp),%ecx ;\
  11221. + xor p1+3*tlen(,%edx,4),%esi ;\
  11222. + movzbl %cl,%edx ;\
  11223. + xor p1(,%edx,4),%edi ;\
  11224. + movzbl %ch,%edx ;\
  11225. + shr $16,%ecx ;\
  11226. + xor p1+tlen(,%edx,4),%esi ;\
  11227. + movzbl %cl,%edx ;\
  11228. + movzbl %ch,%ecx ;\
  11229. + xor p1+2*tlen(,%edx,4),%ebx ;\
  11230. + xor p1+3*tlen(,%ecx,4),%eax
  11231. +
  11232. +// This macro performs an inverse encryption cycle. It is entered with
  11233. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  11234. +// exits with the final values in the same registers.
  11235. +
  11236. +#define inv_rnd(p1,p2) \
  11237. + movzbl %al,%edx ;\
  11238. + mov %ebx,(%esp) ;\
  11239. + mov %eax,%ecx ;\
  11240. + mov p2(%ebp),%eax ;\
  11241. + mov %edi,4(%esp) ;\
  11242. + mov p2+4(%ebp),%ebx ;\
  11243. + xor p1(,%edx,4),%eax ;\
  11244. + movzbl %ch,%edx ;\
  11245. + shr $16,%ecx ;\
  11246. + mov p2+12(%ebp),%edi ;\
  11247. + xor p1+tlen(,%edx,4),%ebx ;\
  11248. + movzbl %cl,%edx ;\
  11249. + movzbl %ch,%ecx ;\
  11250. + xor p1+3*tlen(,%ecx,4),%edi ;\
  11251. + mov %esi,%ecx ;\
  11252. + mov p1+2*tlen(,%edx,4),%esi ;\
  11253. + movzbl %cl,%edx ;\
  11254. + xor p1(,%edx,4),%esi ;\
  11255. + movzbl %ch,%edx ;\
  11256. + shr $16,%ecx ;\
  11257. + xor p1+tlen(,%edx,4),%edi ;\
  11258. + movzbl %cl,%edx ;\
  11259. + movzbl %ch,%ecx ;\
  11260. + xor p1+2*tlen(,%edx,4),%eax ;\
  11261. + mov (%esp),%edx ;\
  11262. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  11263. + movzbl %dl,%ecx ;\
  11264. + xor p2+8(%ebp),%esi ;\
  11265. + xor p1(,%ecx,4),%ebx ;\
  11266. + movzbl %dh,%ecx ;\
  11267. + shr $16,%edx ;\
  11268. + xor p1+tlen(,%ecx,4),%esi ;\
  11269. + movzbl %dl,%ecx ;\
  11270. + movzbl %dh,%edx ;\
  11271. + xor p1+2*tlen(,%ecx,4),%edi ;\
  11272. + mov 4(%esp),%ecx ;\
  11273. + xor p1+3*tlen(,%edx,4),%eax ;\
  11274. + movzbl %cl,%edx ;\
  11275. + xor p1(,%edx,4),%edi ;\
  11276. + movzbl %ch,%edx ;\
  11277. + shr $16,%ecx ;\
  11278. + xor p1+tlen(,%edx,4),%eax ;\
  11279. + movzbl %cl,%edx ;\
  11280. + movzbl %ch,%ecx ;\
  11281. + xor p1+2*tlen(,%edx,4),%ebx ;\
  11282. + xor p1+3*tlen(,%ecx,4),%esi
  11283. +
  11284. +// AES (Rijndael) Encryption Subroutine
  11285. +
  11286. + .text
  11287. + .align ALIGN32BYTES
  11288. +aes_encrypt:
  11289. + push %ebp
  11290. + mov ctx(%esp),%ebp // pointer to context
  11291. + mov in_blk(%esp),%ecx
  11292. + push %ebx
  11293. + push %esi
  11294. + push %edi
  11295. + mov nrnd(%ebp),%edx // number of rounds
  11296. + lea ekey+16(%ebp),%ebp // key pointer
  11297. +
  11298. +// input four columns and xor in first round key
  11299. +
  11300. + mov (%ecx),%eax
  11301. + mov 4(%ecx),%ebx
  11302. + mov 8(%ecx),%esi
  11303. + mov 12(%ecx),%edi
  11304. + xor -16(%ebp),%eax
  11305. + xor -12(%ebp),%ebx
  11306. + xor -8(%ebp),%esi
  11307. + xor -4(%ebp),%edi
  11308. +
  11309. + sub $8,%esp // space for register saves on stack
  11310. +
  11311. + sub $10,%edx
  11312. + je aes_15
  11313. + add $32,%ebp
  11314. + sub $2,%edx
  11315. + je aes_13
  11316. + add $32,%ebp
  11317. +
  11318. + fwd_rnd(aes_ft_tab,-64) // 14 rounds for 256-bit key
  11319. + fwd_rnd(aes_ft_tab,-48)
  11320. +aes_13: fwd_rnd(aes_ft_tab,-32) // 12 rounds for 192-bit key
  11321. + fwd_rnd(aes_ft_tab,-16)
  11322. +aes_15: fwd_rnd(aes_ft_tab,0) // 10 rounds for 128-bit key
  11323. + fwd_rnd(aes_ft_tab,16)
  11324. + fwd_rnd(aes_ft_tab,32)
  11325. + fwd_rnd(aes_ft_tab,48)
  11326. + fwd_rnd(aes_ft_tab,64)
  11327. + fwd_rnd(aes_ft_tab,80)
  11328. + fwd_rnd(aes_ft_tab,96)
  11329. + fwd_rnd(aes_ft_tab,112)
  11330. + fwd_rnd(aes_ft_tab,128)
  11331. + fwd_rnd(aes_fl_tab,144) // last round uses a different table
  11332. +
  11333. +// move final values to the output array.
  11334. +
  11335. + mov out_blk+20(%esp),%ebp
  11336. + add $8,%esp
  11337. + mov %eax,(%ebp)
  11338. + mov %ebx,4(%ebp)
  11339. + mov %esi,8(%ebp)
  11340. + mov %edi,12(%ebp)
  11341. + pop %edi
  11342. + pop %esi
  11343. + pop %ebx
  11344. + pop %ebp
  11345. + ret
  11346. +
  11347. +
  11348. +// AES (Rijndael) Decryption Subroutine
  11349. +
  11350. + .align ALIGN32BYTES
  11351. +aes_decrypt:
  11352. + push %ebp
  11353. + mov ctx(%esp),%ebp // pointer to context
  11354. + mov in_blk(%esp),%ecx
  11355. + push %ebx
  11356. + push %esi
  11357. + push %edi
  11358. + mov nrnd(%ebp),%edx // number of rounds
  11359. + lea dkey+16(%ebp),%ebp // key pointer
  11360. +
  11361. +// input four columns and xor in first round key
  11362. +
  11363. + mov (%ecx),%eax
  11364. + mov 4(%ecx),%ebx
  11365. + mov 8(%ecx),%esi
  11366. + mov 12(%ecx),%edi
  11367. + xor -16(%ebp),%eax
  11368. + xor -12(%ebp),%ebx
  11369. + xor -8(%ebp),%esi
  11370. + xor -4(%ebp),%edi
  11371. +
  11372. + sub $8,%esp // space for register saves on stack
  11373. +
  11374. + sub $10,%edx
  11375. + je aes_25
  11376. + add $32,%ebp
  11377. + sub $2,%edx
  11378. + je aes_23
  11379. + add $32,%ebp
  11380. +
  11381. + inv_rnd(aes_it_tab,-64) // 14 rounds for 256-bit key
  11382. + inv_rnd(aes_it_tab,-48)
  11383. +aes_23: inv_rnd(aes_it_tab,-32) // 12 rounds for 192-bit key
  11384. + inv_rnd(aes_it_tab,-16)
  11385. +aes_25: inv_rnd(aes_it_tab,0) // 10 rounds for 128-bit key
  11386. + inv_rnd(aes_it_tab,16)
  11387. + inv_rnd(aes_it_tab,32)
  11388. + inv_rnd(aes_it_tab,48)
  11389. + inv_rnd(aes_it_tab,64)
  11390. + inv_rnd(aes_it_tab,80)
  11391. + inv_rnd(aes_it_tab,96)
  11392. + inv_rnd(aes_it_tab,112)
  11393. + inv_rnd(aes_it_tab,128)
  11394. + inv_rnd(aes_il_tab,144) // last round uses a different table
  11395. +
  11396. +// move final values to the output array.
  11397. +
  11398. + mov out_blk+20(%esp),%ebp
  11399. + add $8,%esp
  11400. + mov %eax,(%ebp)
  11401. + mov %ebx,4(%ebp)
  11402. + mov %esi,8(%ebp)
  11403. + mov %edi,12(%ebp)
  11404. + pop %edi
  11405. + pop %esi
  11406. + pop %ebx
  11407. + pop %ebp
  11408. + ret
  11409. +
  11410. +// AES (Rijndael) Key Schedule Subroutine
  11411. +
  11412. +// input/output parameters
  11413. +
  11414. +#define aes_cx 12 // AES context
  11415. +#define in_key 16 // key input array address
  11416. +#define key_ln 20 // key length, bytes (16,24,32) or bits (128,192,256)
  11417. +#define ed_flg 24 // 0=create both encr/decr keys, 1=create encr key only
  11418. +
  11419. +// offsets for locals
  11420. +
  11421. +#define cnt -4
  11422. +#define kpf -8
  11423. +#define slen 8
  11424. +
  11425. +// This macro performs a column mixing operation on an input 32-bit
  11426. +// word to give a 32-bit result. It uses each of the 4 bytes in the
  11427. +// the input column to index 4 different tables of 256 32-bit words
  11428. +// that are xored together to form the output value.
  11429. +
  11430. +#define mix_col(p1) \
  11431. + movzbl %bl,%ecx ;\
  11432. + mov p1(,%ecx,4),%eax ;\
  11433. + movzbl %bh,%ecx ;\
  11434. + ror $16,%ebx ;\
  11435. + xor p1+tlen(,%ecx,4),%eax ;\
  11436. + movzbl %bl,%ecx ;\
  11437. + xor p1+2*tlen(,%ecx,4),%eax ;\
  11438. + movzbl %bh,%ecx ;\
  11439. + xor p1+3*tlen(,%ecx,4),%eax
  11440. +
  11441. +// Key Schedule Macros
  11442. +
  11443. +#define ksc4(p1) \
  11444. + rol $24,%ebx ;\
  11445. + mix_col(aes_fl_tab) ;\
  11446. + ror $8,%ebx ;\
  11447. + xor 4*p1+aes_rcon_tab,%eax ;\
  11448. + xor %eax,%esi ;\
  11449. + xor %esi,%ebp ;\
  11450. + mov %esi,16*p1(%edi) ;\
  11451. + mov %ebp,16*p1+4(%edi) ;\
  11452. + xor %ebp,%edx ;\
  11453. + xor %edx,%ebx ;\
  11454. + mov %edx,16*p1+8(%edi) ;\
  11455. + mov %ebx,16*p1+12(%edi)
  11456. +
  11457. +#define ksc6(p1) \
  11458. + rol $24,%ebx ;\
  11459. + mix_col(aes_fl_tab) ;\
  11460. + ror $8,%ebx ;\
  11461. + xor 4*p1+aes_rcon_tab,%eax ;\
  11462. + xor 24*p1-24(%edi),%eax ;\
  11463. + mov %eax,24*p1(%edi) ;\
  11464. + xor 24*p1-20(%edi),%eax ;\
  11465. + mov %eax,24*p1+4(%edi) ;\
  11466. + xor %eax,%esi ;\
  11467. + xor %esi,%ebp ;\
  11468. + mov %esi,24*p1+8(%edi) ;\
  11469. + mov %ebp,24*p1+12(%edi) ;\
  11470. + xor %ebp,%edx ;\
  11471. + xor %edx,%ebx ;\
  11472. + mov %edx,24*p1+16(%edi) ;\
  11473. + mov %ebx,24*p1+20(%edi)
  11474. +
  11475. +#define ksc8(p1) \
  11476. + rol $24,%ebx ;\
  11477. + mix_col(aes_fl_tab) ;\
  11478. + ror $8,%ebx ;\
  11479. + xor 4*p1+aes_rcon_tab,%eax ;\
  11480. + xor 32*p1-32(%edi),%eax ;\
  11481. + mov %eax,32*p1(%edi) ;\
  11482. + xor 32*p1-28(%edi),%eax ;\
  11483. + mov %eax,32*p1+4(%edi) ;\
  11484. + xor 32*p1-24(%edi),%eax ;\
  11485. + mov %eax,32*p1+8(%edi) ;\
  11486. + xor 32*p1-20(%edi),%eax ;\
  11487. + mov %eax,32*p1+12(%edi) ;\
  11488. + push %ebx ;\
  11489. + mov %eax,%ebx ;\
  11490. + mix_col(aes_fl_tab) ;\
  11491. + pop %ebx ;\
  11492. + xor %eax,%esi ;\
  11493. + xor %esi,%ebp ;\
  11494. + mov %esi,32*p1+16(%edi) ;\
  11495. + mov %ebp,32*p1+20(%edi) ;\
  11496. + xor %ebp,%edx ;\
  11497. + xor %edx,%ebx ;\
  11498. + mov %edx,32*p1+24(%edi) ;\
  11499. + mov %ebx,32*p1+28(%edi)
  11500. +
  11501. + .align ALIGN32BYTES
  11502. +aes_set_key:
  11503. + pushfl
  11504. + push %ebp
  11505. + mov %esp,%ebp
  11506. + sub $slen,%esp
  11507. + push %ebx
  11508. + push %esi
  11509. + push %edi
  11510. +
  11511. + mov aes_cx(%ebp),%edx // edx -> AES context
  11512. +
  11513. + mov key_ln(%ebp),%ecx // key length
  11514. + cmpl $128,%ecx
  11515. + jb aes_30
  11516. + shr $3,%ecx
  11517. +aes_30: cmpl $32,%ecx
  11518. + je aes_32
  11519. + cmpl $24,%ecx
  11520. + je aes_32
  11521. + mov $16,%ecx
  11522. +aes_32: shr $2,%ecx
  11523. + mov %ecx,nkey(%edx)
  11524. +
  11525. + lea 6(%ecx),%eax // 10/12/14 for 4/6/8 32-bit key length
  11526. + mov %eax,nrnd(%edx)
  11527. +
  11528. + mov in_key(%ebp),%esi // key input array
  11529. + lea ekey(%edx),%edi // key position in AES context
  11530. + cld
  11531. + push %ebp
  11532. + mov %ecx,%eax // save key length in eax
  11533. + rep ; movsl // words in the key schedule
  11534. + mov -4(%esi),%ebx // put some values in registers
  11535. + mov -8(%esi),%edx // to allow faster code
  11536. + mov -12(%esi),%ebp
  11537. + mov -16(%esi),%esi
  11538. +
  11539. + cmpl $4,%eax // jump on key size
  11540. + je aes_36
  11541. + cmpl $6,%eax
  11542. + je aes_35
  11543. +
  11544. + ksc8(0)
  11545. + ksc8(1)
  11546. + ksc8(2)
  11547. + ksc8(3)
  11548. + ksc8(4)
  11549. + ksc8(5)
  11550. + ksc8(6)
  11551. + jmp aes_37
  11552. +aes_35: ksc6(0)
  11553. + ksc6(1)
  11554. + ksc6(2)
  11555. + ksc6(3)
  11556. + ksc6(4)
  11557. + ksc6(5)
  11558. + ksc6(6)
  11559. + ksc6(7)
  11560. + jmp aes_37
  11561. +aes_36: ksc4(0)
  11562. + ksc4(1)
  11563. + ksc4(2)
  11564. + ksc4(3)
  11565. + ksc4(4)
  11566. + ksc4(5)
  11567. + ksc4(6)
  11568. + ksc4(7)
  11569. + ksc4(8)
  11570. + ksc4(9)
  11571. +aes_37: pop %ebp
  11572. + mov aes_cx(%ebp),%edx // edx -> AES context
  11573. + cmpl $0,ed_flg(%ebp)
  11574. + jne aes_39
  11575. +
  11576. +// compile decryption key schedule from encryption schedule - reverse
  11577. +// order and do mix_column operation on round keys except first and last
  11578. +
  11579. + mov nrnd(%edx),%eax // kt = cx->d_key + nc * cx->Nrnd
  11580. + shl $2,%eax
  11581. + lea dkey(%edx,%eax,4),%edi
  11582. + lea ekey(%edx),%esi // kf = cx->e_key
  11583. +
  11584. + movsl // copy first round key (unmodified)
  11585. + movsl
  11586. + movsl
  11587. + movsl
  11588. + sub $32,%edi
  11589. + movl $1,cnt(%ebp)
  11590. +aes_38: // do mix column on each column of
  11591. + lodsl // each round key
  11592. + mov %eax,%ebx
  11593. + mix_col(aes_im_tab)
  11594. + stosl
  11595. + lodsl
  11596. + mov %eax,%ebx
  11597. + mix_col(aes_im_tab)
  11598. + stosl
  11599. + lodsl
  11600. + mov %eax,%ebx
  11601. + mix_col(aes_im_tab)
  11602. + stosl
  11603. + lodsl
  11604. + mov %eax,%ebx
  11605. + mix_col(aes_im_tab)
  11606. + stosl
  11607. + sub $32,%edi
  11608. +
  11609. + incl cnt(%ebp)
  11610. + mov cnt(%ebp),%eax
  11611. + cmp nrnd(%edx),%eax
  11612. + jb aes_38
  11613. +
  11614. + movsl // copy last round key (unmodified)
  11615. + movsl
  11616. + movsl
  11617. + movsl
  11618. +aes_39: pop %edi
  11619. + pop %esi
  11620. + pop %ebx
  11621. + mov %ebp,%esp
  11622. + pop %ebp
  11623. + popfl
  11624. + ret
  11625. +
  11626. +
  11627. +// finite field multiplies by {02}, {04} and {08}
  11628. +
  11629. +#define f2(x) ((x<<1)^(((x>>7)&1)*0x11b))
  11630. +#define f4(x) ((x<<2)^(((x>>6)&1)*0x11b)^(((x>>6)&2)*0x11b))
  11631. +#define f8(x) ((x<<3)^(((x>>5)&1)*0x11b)^(((x>>5)&2)*0x11b)^(((x>>5)&4)*0x11b))
  11632. +
  11633. +// finite field multiplies required in table generation
  11634. +
  11635. +#define f3(x) (f2(x) ^ x)
  11636. +#define f9(x) (f8(x) ^ x)
  11637. +#define fb(x) (f8(x) ^ f2(x) ^ x)
  11638. +#define fd(x) (f8(x) ^ f4(x) ^ x)
  11639. +#define fe(x) (f8(x) ^ f4(x) ^ f2(x))
  11640. +
  11641. +// These defines generate the forward table entries
  11642. +
  11643. +#define u0(x) ((f3(x) << 24) | (x << 16) | (x << 8) | f2(x))
  11644. +#define u1(x) ((x << 24) | (x << 16) | (f2(x) << 8) | f3(x))
  11645. +#define u2(x) ((x << 24) | (f2(x) << 16) | (f3(x) << 8) | x)
  11646. +#define u3(x) ((f2(x) << 24) | (f3(x) << 16) | (x << 8) | x)
  11647. +
  11648. +// These defines generate the inverse table entries
  11649. +
  11650. +#define v0(x) ((fb(x) << 24) | (fd(x) << 16) | (f9(x) << 8) | fe(x))
  11651. +#define v1(x) ((fd(x) << 24) | (f9(x) << 16) | (fe(x) << 8) | fb(x))
  11652. +#define v2(x) ((f9(x) << 24) | (fe(x) << 16) | (fb(x) << 8) | fd(x))
  11653. +#define v3(x) ((fe(x) << 24) | (fb(x) << 16) | (fd(x) << 8) | f9(x))
  11654. +
  11655. +// These defines generate entries for the last round tables
  11656. +
  11657. +#define w0(x) (x)
  11658. +#define w1(x) (x << 8)
  11659. +#define w2(x) (x << 16)
  11660. +#define w3(x) (x << 24)
  11661. +
  11662. +// macro to generate inverse mix column tables (needed for the key schedule)
  11663. +
  11664. +#define im_data0(p1) \
  11665. + .long p1(0x00),p1(0x01),p1(0x02),p1(0x03),p1(0x04),p1(0x05),p1(0x06),p1(0x07) ;\
  11666. + .long p1(0x08),p1(0x09),p1(0x0a),p1(0x0b),p1(0x0c),p1(0x0d),p1(0x0e),p1(0x0f) ;\
  11667. + .long p1(0x10),p1(0x11),p1(0x12),p1(0x13),p1(0x14),p1(0x15),p1(0x16),p1(0x17) ;\
  11668. + .long p1(0x18),p1(0x19),p1(0x1a),p1(0x1b),p1(0x1c),p1(0x1d),p1(0x1e),p1(0x1f)
  11669. +#define im_data1(p1) \
  11670. + .long p1(0x20),p1(0x21),p1(0x22),p1(0x23),p1(0x24),p1(0x25),p1(0x26),p1(0x27) ;\
  11671. + .long p1(0x28),p1(0x29),p1(0x2a),p1(0x2b),p1(0x2c),p1(0x2d),p1(0x2e),p1(0x2f) ;\
  11672. + .long p1(0x30),p1(0x31),p1(0x32),p1(0x33),p1(0x34),p1(0x35),p1(0x36),p1(0x37) ;\
  11673. + .long p1(0x38),p1(0x39),p1(0x3a),p1(0x3b),p1(0x3c),p1(0x3d),p1(0x3e),p1(0x3f)
  11674. +#define im_data2(p1) \
  11675. + .long p1(0x40),p1(0x41),p1(0x42),p1(0x43),p1(0x44),p1(0x45),p1(0x46),p1(0x47) ;\
  11676. + .long p1(0x48),p1(0x49),p1(0x4a),p1(0x4b),p1(0x4c),p1(0x4d),p1(0x4e),p1(0x4f) ;\
  11677. + .long p1(0x50),p1(0x51),p1(0x52),p1(0x53),p1(0x54),p1(0x55),p1(0x56),p1(0x57) ;\
  11678. + .long p1(0x58),p1(0x59),p1(0x5a),p1(0x5b),p1(0x5c),p1(0x5d),p1(0x5e),p1(0x5f)
  11679. +#define im_data3(p1) \
  11680. + .long p1(0x60),p1(0x61),p1(0x62),p1(0x63),p1(0x64),p1(0x65),p1(0x66),p1(0x67) ;\
  11681. + .long p1(0x68),p1(0x69),p1(0x6a),p1(0x6b),p1(0x6c),p1(0x6d),p1(0x6e),p1(0x6f) ;\
  11682. + .long p1(0x70),p1(0x71),p1(0x72),p1(0x73),p1(0x74),p1(0x75),p1(0x76),p1(0x77) ;\
  11683. + .long p1(0x78),p1(0x79),p1(0x7a),p1(0x7b),p1(0x7c),p1(0x7d),p1(0x7e),p1(0x7f)
  11684. +#define im_data4(p1) \
  11685. + .long p1(0x80),p1(0x81),p1(0x82),p1(0x83),p1(0x84),p1(0x85),p1(0x86),p1(0x87) ;\
  11686. + .long p1(0x88),p1(0x89),p1(0x8a),p1(0x8b),p1(0x8c),p1(0x8d),p1(0x8e),p1(0x8f) ;\
  11687. + .long p1(0x90),p1(0x91),p1(0x92),p1(0x93),p1(0x94),p1(0x95),p1(0x96),p1(0x97) ;\
  11688. + .long p1(0x98),p1(0x99),p1(0x9a),p1(0x9b),p1(0x9c),p1(0x9d),p1(0x9e),p1(0x9f)
  11689. +#define im_data5(p1) \
  11690. + .long p1(0xa0),p1(0xa1),p1(0xa2),p1(0xa3),p1(0xa4),p1(0xa5),p1(0xa6),p1(0xa7) ;\
  11691. + .long p1(0xa8),p1(0xa9),p1(0xaa),p1(0xab),p1(0xac),p1(0xad),p1(0xae),p1(0xaf) ;\
  11692. + .long p1(0xb0),p1(0xb1),p1(0xb2),p1(0xb3),p1(0xb4),p1(0xb5),p1(0xb6),p1(0xb7) ;\
  11693. + .long p1(0xb8),p1(0xb9),p1(0xba),p1(0xbb),p1(0xbc),p1(0xbd),p1(0xbe),p1(0xbf)
  11694. +#define im_data6(p1) \
  11695. + .long p1(0xc0),p1(0xc1),p1(0xc2),p1(0xc3),p1(0xc4),p1(0xc5),p1(0xc6),p1(0xc7) ;\
  11696. + .long p1(0xc8),p1(0xc9),p1(0xca),p1(0xcb),p1(0xcc),p1(0xcd),p1(0xce),p1(0xcf) ;\
  11697. + .long p1(0xd0),p1(0xd1),p1(0xd2),p1(0xd3),p1(0xd4),p1(0xd5),p1(0xd6),p1(0xd7) ;\
  11698. + .long p1(0xd8),p1(0xd9),p1(0xda),p1(0xdb),p1(0xdc),p1(0xdd),p1(0xde),p1(0xdf)
  11699. +#define im_data7(p1) \
  11700. + .long p1(0xe0),p1(0xe1),p1(0xe2),p1(0xe3),p1(0xe4),p1(0xe5),p1(0xe6),p1(0xe7) ;\
  11701. + .long p1(0xe8),p1(0xe9),p1(0xea),p1(0xeb),p1(0xec),p1(0xed),p1(0xee),p1(0xef) ;\
  11702. + .long p1(0xf0),p1(0xf1),p1(0xf2),p1(0xf3),p1(0xf4),p1(0xf5),p1(0xf6),p1(0xf7) ;\
  11703. + .long p1(0xf8),p1(0xf9),p1(0xfa),p1(0xfb),p1(0xfc),p1(0xfd),p1(0xfe),p1(0xff)
  11704. +
  11705. +// S-box data - 256 entries
  11706. +
  11707. +#define sb_data0(p1) \
  11708. + .long p1(0x63),p1(0x7c),p1(0x77),p1(0x7b),p1(0xf2),p1(0x6b),p1(0x6f),p1(0xc5) ;\
  11709. + .long p1(0x30),p1(0x01),p1(0x67),p1(0x2b),p1(0xfe),p1(0xd7),p1(0xab),p1(0x76) ;\
  11710. + .long p1(0xca),p1(0x82),p1(0xc9),p1(0x7d),p1(0xfa),p1(0x59),p1(0x47),p1(0xf0) ;\
  11711. + .long p1(0xad),p1(0xd4),p1(0xa2),p1(0xaf),p1(0x9c),p1(0xa4),p1(0x72),p1(0xc0)
  11712. +#define sb_data1(p1) \
  11713. + .long p1(0xb7),p1(0xfd),p1(0x93),p1(0x26),p1(0x36),p1(0x3f),p1(0xf7),p1(0xcc) ;\
  11714. + .long p1(0x34),p1(0xa5),p1(0xe5),p1(0xf1),p1(0x71),p1(0xd8),p1(0x31),p1(0x15) ;\
  11715. + .long p1(0x04),p1(0xc7),p1(0x23),p1(0xc3),p1(0x18),p1(0x96),p1(0x05),p1(0x9a) ;\
  11716. + .long p1(0x07),p1(0x12),p1(0x80),p1(0xe2),p1(0xeb),p1(0x27),p1(0xb2),p1(0x75)
  11717. +#define sb_data2(p1) \
  11718. + .long p1(0x09),p1(0x83),p1(0x2c),p1(0x1a),p1(0x1b),p1(0x6e),p1(0x5a),p1(0xa0) ;\
  11719. + .long p1(0x52),p1(0x3b),p1(0xd6),p1(0xb3),p1(0x29),p1(0xe3),p1(0x2f),p1(0x84) ;\
  11720. + .long p1(0x53),p1(0xd1),p1(0x00),p1(0xed),p1(0x20),p1(0xfc),p1(0xb1),p1(0x5b) ;\
  11721. + .long p1(0x6a),p1(0xcb),p1(0xbe),p1(0x39),p1(0x4a),p1(0x4c),p1(0x58),p1(0xcf)
  11722. +#define sb_data3(p1) \
  11723. + .long p1(0xd0),p1(0xef),p1(0xaa),p1(0xfb),p1(0x43),p1(0x4d),p1(0x33),p1(0x85) ;\
  11724. + .long p1(0x45),p1(0xf9),p1(0x02),p1(0x7f),p1(0x50),p1(0x3c),p1(0x9f),p1(0xa8) ;\
  11725. + .long p1(0x51),p1(0xa3),p1(0x40),p1(0x8f),p1(0x92),p1(0x9d),p1(0x38),p1(0xf5) ;\
  11726. + .long p1(0xbc),p1(0xb6),p1(0xda),p1(0x21),p1(0x10),p1(0xff),p1(0xf3),p1(0xd2)
  11727. +#define sb_data4(p1) \
  11728. + .long p1(0xcd),p1(0x0c),p1(0x13),p1(0xec),p1(0x5f),p1(0x97),p1(0x44),p1(0x17) ;\
  11729. + .long p1(0xc4),p1(0xa7),p1(0x7e),p1(0x3d),p1(0x64),p1(0x5d),p1(0x19),p1(0x73) ;\
  11730. + .long p1(0x60),p1(0x81),p1(0x4f),p1(0xdc),p1(0x22),p1(0x2a),p1(0x90),p1(0x88) ;\
  11731. + .long p1(0x46),p1(0xee),p1(0xb8),p1(0x14),p1(0xde),p1(0x5e),p1(0x0b),p1(0xdb)
  11732. +#define sb_data5(p1) \
  11733. + .long p1(0xe0),p1(0x32),p1(0x3a),p1(0x0a),p1(0x49),p1(0x06),p1(0x24),p1(0x5c) ;\
  11734. + .long p1(0xc2),p1(0xd3),p1(0xac),p1(0x62),p1(0x91),p1(0x95),p1(0xe4),p1(0x79) ;\
  11735. + .long p1(0xe7),p1(0xc8),p1(0x37),p1(0x6d),p1(0x8d),p1(0xd5),p1(0x4e),p1(0xa9) ;\
  11736. + .long p1(0x6c),p1(0x56),p1(0xf4),p1(0xea),p1(0x65),p1(0x7a),p1(0xae),p1(0x08)
  11737. +#define sb_data6(p1) \
  11738. + .long p1(0xba),p1(0x78),p1(0x25),p1(0x2e),p1(0x1c),p1(0xa6),p1(0xb4),p1(0xc6) ;\
  11739. + .long p1(0xe8),p1(0xdd),p1(0x74),p1(0x1f),p1(0x4b),p1(0xbd),p1(0x8b),p1(0x8a) ;\
  11740. + .long p1(0x70),p1(0x3e),p1(0xb5),p1(0x66),p1(0x48),p1(0x03),p1(0xf6),p1(0x0e) ;\
  11741. + .long p1(0x61),p1(0x35),p1(0x57),p1(0xb9),p1(0x86),p1(0xc1),p1(0x1d),p1(0x9e)
  11742. +#define sb_data7(p1) \
  11743. + .long p1(0xe1),p1(0xf8),p1(0x98),p1(0x11),p1(0x69),p1(0xd9),p1(0x8e),p1(0x94) ;\
  11744. + .long p1(0x9b),p1(0x1e),p1(0x87),p1(0xe9),p1(0xce),p1(0x55),p1(0x28),p1(0xdf) ;\
  11745. + .long p1(0x8c),p1(0xa1),p1(0x89),p1(0x0d),p1(0xbf),p1(0xe6),p1(0x42),p1(0x68) ;\
  11746. + .long p1(0x41),p1(0x99),p1(0x2d),p1(0x0f),p1(0xb0),p1(0x54),p1(0xbb),p1(0x16)
  11747. +
  11748. +// Inverse S-box data - 256 entries
  11749. +
  11750. +#define ib_data0(p1) \
  11751. + .long p1(0x52),p1(0x09),p1(0x6a),p1(0xd5),p1(0x30),p1(0x36),p1(0xa5),p1(0x38) ;\
  11752. + .long p1(0xbf),p1(0x40),p1(0xa3),p1(0x9e),p1(0x81),p1(0xf3),p1(0xd7),p1(0xfb) ;\
  11753. + .long p1(0x7c),p1(0xe3),p1(0x39),p1(0x82),p1(0x9b),p1(0x2f),p1(0xff),p1(0x87) ;\
  11754. + .long p1(0x34),p1(0x8e),p1(0x43),p1(0x44),p1(0xc4),p1(0xde),p1(0xe9),p1(0xcb)
  11755. +#define ib_data1(p1) \
  11756. + .long p1(0x54),p1(0x7b),p1(0x94),p1(0x32),p1(0xa6),p1(0xc2),p1(0x23),p1(0x3d) ;\
  11757. + .long p1(0xee),p1(0x4c),p1(0x95),p1(0x0b),p1(0x42),p1(0xfa),p1(0xc3),p1(0x4e) ;\
  11758. + .long p1(0x08),p1(0x2e),p1(0xa1),p1(0x66),p1(0x28),p1(0xd9),p1(0x24),p1(0xb2) ;\
  11759. + .long p1(0x76),p1(0x5b),p1(0xa2),p1(0x49),p1(0x6d),p1(0x8b),p1(0xd1),p1(0x25)
  11760. +#define ib_data2(p1) \
  11761. + .long p1(0x72),p1(0xf8),p1(0xf6),p1(0x64),p1(0x86),p1(0x68),p1(0x98),p1(0x16) ;\
  11762. + .long p1(0xd4),p1(0xa4),p1(0x5c),p1(0xcc),p1(0x5d),p1(0x65),p1(0xb6),p1(0x92) ;\
  11763. + .long p1(0x6c),p1(0x70),p1(0x48),p1(0x50),p1(0xfd),p1(0xed),p1(0xb9),p1(0xda) ;\
  11764. + .long p1(0x5e),p1(0x15),p1(0x46),p1(0x57),p1(0xa7),p1(0x8d),p1(0x9d),p1(0x84)
  11765. +#define ib_data3(p1) \
  11766. + .long p1(0x90),p1(0xd8),p1(0xab),p1(0x00),p1(0x8c),p1(0xbc),p1(0xd3),p1(0x0a) ;\
  11767. + .long p1(0xf7),p1(0xe4),p1(0x58),p1(0x05),p1(0xb8),p1(0xb3),p1(0x45),p1(0x06) ;\
  11768. + .long p1(0xd0),p1(0x2c),p1(0x1e),p1(0x8f),p1(0xca),p1(0x3f),p1(0x0f),p1(0x02) ;\
  11769. + .long p1(0xc1),p1(0xaf),p1(0xbd),p1(0x03),p1(0x01),p1(0x13),p1(0x8a),p1(0x6b)
  11770. +#define ib_data4(p1) \
  11771. + .long p1(0x3a),p1(0x91),p1(0x11),p1(0x41),p1(0x4f),p1(0x67),p1(0xdc),p1(0xea) ;\
  11772. + .long p1(0x97),p1(0xf2),p1(0xcf),p1(0xce),p1(0xf0),p1(0xb4),p1(0xe6),p1(0x73) ;\
  11773. + .long p1(0x96),p1(0xac),p1(0x74),p1(0x22),p1(0xe7),p1(0xad),p1(0x35),p1(0x85) ;\
  11774. + .long p1(0xe2),p1(0xf9),p1(0x37),p1(0xe8),p1(0x1c),p1(0x75),p1(0xdf),p1(0x6e)
  11775. +#define ib_data5(p1) \
  11776. + .long p1(0x47),p1(0xf1),p1(0x1a),p1(0x71),p1(0x1d),p1(0x29),p1(0xc5),p1(0x89) ;\
  11777. + .long p1(0x6f),p1(0xb7),p1(0x62),p1(0x0e),p1(0xaa),p1(0x18),p1(0xbe),p1(0x1b) ;\
  11778. + .long p1(0xfc),p1(0x56),p1(0x3e),p1(0x4b),p1(0xc6),p1(0xd2),p1(0x79),p1(0x20) ;\
  11779. + .long p1(0x9a),p1(0xdb),p1(0xc0),p1(0xfe),p1(0x78),p1(0xcd),p1(0x5a),p1(0xf4)
  11780. +#define ib_data6(p1) \
  11781. + .long p1(0x1f),p1(0xdd),p1(0xa8),p1(0x33),p1(0x88),p1(0x07),p1(0xc7),p1(0x31) ;\
  11782. + .long p1(0xb1),p1(0x12),p1(0x10),p1(0x59),p1(0x27),p1(0x80),p1(0xec),p1(0x5f) ;\
  11783. + .long p1(0x60),p1(0x51),p1(0x7f),p1(0xa9),p1(0x19),p1(0xb5),p1(0x4a),p1(0x0d) ;\
  11784. + .long p1(0x2d),p1(0xe5),p1(0x7a),p1(0x9f),p1(0x93),p1(0xc9),p1(0x9c),p1(0xef)
  11785. +#define ib_data7(p1) \
  11786. + .long p1(0xa0),p1(0xe0),p1(0x3b),p1(0x4d),p1(0xae),p1(0x2a),p1(0xf5),p1(0xb0) ;\
  11787. + .long p1(0xc8),p1(0xeb),p1(0xbb),p1(0x3c),p1(0x83),p1(0x53),p1(0x99),p1(0x61) ;\
  11788. + .long p1(0x17),p1(0x2b),p1(0x04),p1(0x7e),p1(0xba),p1(0x77),p1(0xd6),p1(0x26) ;\
  11789. + .long p1(0xe1),p1(0x69),p1(0x14),p1(0x63),p1(0x55),p1(0x21),p1(0x0c),p1(0x7d)
  11790. +
  11791. +// The rcon_table (needed for the key schedule)
  11792. +//
  11793. +// Here is original Dr Brian Gladman's source code:
  11794. +// _rcon_tab:
  11795. +// %assign x 1
  11796. +// %rep 29
  11797. +// dd x
  11798. +// %assign x f2(x)
  11799. +// %endrep
  11800. +//
  11801. +// Here is precomputed output (it's more portable this way):
  11802. +
  11803. + .align ALIGN32BYTES
  11804. +aes_rcon_tab:
  11805. + .long 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
  11806. + .long 0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f
  11807. + .long 0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4
  11808. + .long 0xb3,0x7d,0xfa,0xef,0xc5
  11809. +
  11810. +// The forward xor tables
  11811. +
  11812. + .align ALIGN32BYTES
  11813. +aes_ft_tab:
  11814. + sb_data0(u0)
  11815. + sb_data1(u0)
  11816. + sb_data2(u0)
  11817. + sb_data3(u0)
  11818. + sb_data4(u0)
  11819. + sb_data5(u0)
  11820. + sb_data6(u0)
  11821. + sb_data7(u0)
  11822. +
  11823. + sb_data0(u1)
  11824. + sb_data1(u1)
  11825. + sb_data2(u1)
  11826. + sb_data3(u1)
  11827. + sb_data4(u1)
  11828. + sb_data5(u1)
  11829. + sb_data6(u1)
  11830. + sb_data7(u1)
  11831. +
  11832. + sb_data0(u2)
  11833. + sb_data1(u2)
  11834. + sb_data2(u2)
  11835. + sb_data3(u2)
  11836. + sb_data4(u2)
  11837. + sb_data5(u2)
  11838. + sb_data6(u2)
  11839. + sb_data7(u2)
  11840. +
  11841. + sb_data0(u3)
  11842. + sb_data1(u3)
  11843. + sb_data2(u3)
  11844. + sb_data3(u3)
  11845. + sb_data4(u3)
  11846. + sb_data5(u3)
  11847. + sb_data6(u3)
  11848. + sb_data7(u3)
  11849. +
  11850. + .align ALIGN32BYTES
  11851. +aes_fl_tab:
  11852. + sb_data0(w0)
  11853. + sb_data1(w0)
  11854. + sb_data2(w0)
  11855. + sb_data3(w0)
  11856. + sb_data4(w0)
  11857. + sb_data5(w0)
  11858. + sb_data6(w0)
  11859. + sb_data7(w0)
  11860. +
  11861. + sb_data0(w1)
  11862. + sb_data1(w1)
  11863. + sb_data2(w1)
  11864. + sb_data3(w1)
  11865. + sb_data4(w1)
  11866. + sb_data5(w1)
  11867. + sb_data6(w1)
  11868. + sb_data7(w1)
  11869. +
  11870. + sb_data0(w2)
  11871. + sb_data1(w2)
  11872. + sb_data2(w2)
  11873. + sb_data3(w2)
  11874. + sb_data4(w2)
  11875. + sb_data5(w2)
  11876. + sb_data6(w2)
  11877. + sb_data7(w2)
  11878. +
  11879. + sb_data0(w3)
  11880. + sb_data1(w3)
  11881. + sb_data2(w3)
  11882. + sb_data3(w3)
  11883. + sb_data4(w3)
  11884. + sb_data5(w3)
  11885. + sb_data6(w3)
  11886. + sb_data7(w3)
  11887. +
  11888. +// The inverse xor tables
  11889. +
  11890. + .align ALIGN32BYTES
  11891. +aes_it_tab:
  11892. + ib_data0(v0)
  11893. + ib_data1(v0)
  11894. + ib_data2(v0)
  11895. + ib_data3(v0)
  11896. + ib_data4(v0)
  11897. + ib_data5(v0)
  11898. + ib_data6(v0)
  11899. + ib_data7(v0)
  11900. +
  11901. + ib_data0(v1)
  11902. + ib_data1(v1)
  11903. + ib_data2(v1)
  11904. + ib_data3(v1)
  11905. + ib_data4(v1)
  11906. + ib_data5(v1)
  11907. + ib_data6(v1)
  11908. + ib_data7(v1)
  11909. +
  11910. + ib_data0(v2)
  11911. + ib_data1(v2)
  11912. + ib_data2(v2)
  11913. + ib_data3(v2)
  11914. + ib_data4(v2)
  11915. + ib_data5(v2)
  11916. + ib_data6(v2)
  11917. + ib_data7(v2)
  11918. +
  11919. + ib_data0(v3)
  11920. + ib_data1(v3)
  11921. + ib_data2(v3)
  11922. + ib_data3(v3)
  11923. + ib_data4(v3)
  11924. + ib_data5(v3)
  11925. + ib_data6(v3)
  11926. + ib_data7(v3)
  11927. +
  11928. + .align ALIGN32BYTES
  11929. +aes_il_tab:
  11930. + ib_data0(w0)
  11931. + ib_data1(w0)
  11932. + ib_data2(w0)
  11933. + ib_data3(w0)
  11934. + ib_data4(w0)
  11935. + ib_data5(w0)
  11936. + ib_data6(w0)
  11937. + ib_data7(w0)
  11938. +
  11939. + ib_data0(w1)
  11940. + ib_data1(w1)
  11941. + ib_data2(w1)
  11942. + ib_data3(w1)
  11943. + ib_data4(w1)
  11944. + ib_data5(w1)
  11945. + ib_data6(w1)
  11946. + ib_data7(w1)
  11947. +
  11948. + ib_data0(w2)
  11949. + ib_data1(w2)
  11950. + ib_data2(w2)
  11951. + ib_data3(w2)
  11952. + ib_data4(w2)
  11953. + ib_data5(w2)
  11954. + ib_data6(w2)
  11955. + ib_data7(w2)
  11956. +
  11957. + ib_data0(w3)
  11958. + ib_data1(w3)
  11959. + ib_data2(w3)
  11960. + ib_data3(w3)
  11961. + ib_data4(w3)
  11962. + ib_data5(w3)
  11963. + ib_data6(w3)
  11964. + ib_data7(w3)
  11965. +
  11966. +// The inverse mix column tables
  11967. +
  11968. + .align ALIGN32BYTES
  11969. +aes_im_tab:
  11970. + im_data0(v0)
  11971. + im_data1(v0)
  11972. + im_data2(v0)
  11973. + im_data3(v0)
  11974. + im_data4(v0)
  11975. + im_data5(v0)
  11976. + im_data6(v0)
  11977. + im_data7(v0)
  11978. +
  11979. + im_data0(v1)
  11980. + im_data1(v1)
  11981. + im_data2(v1)
  11982. + im_data3(v1)
  11983. + im_data4(v1)
  11984. + im_data5(v1)
  11985. + im_data6(v1)
  11986. + im_data7(v1)
  11987. +
  11988. + im_data0(v2)
  11989. + im_data1(v2)
  11990. + im_data2(v2)
  11991. + im_data3(v2)
  11992. + im_data4(v2)
  11993. + im_data5(v2)
  11994. + im_data6(v2)
  11995. + im_data7(v2)
  11996. +
  11997. + im_data0(v3)
  11998. + im_data1(v3)
  11999. + im_data2(v3)
  12000. + im_data3(v3)
  12001. + im_data4(v3)
  12002. + im_data5(v3)
  12003. + im_data6(v3)
  12004. + im_data7(v3)
  12005. --- /dev/null Tue Mar 11 13:02:56 2003
  12006. +++ linux/net/ipsec/aes/aes.c Mon Feb 9 13:51:03 2004
  12007. @@ -0,0 +1,1415 @@
  12008. +// I retain copyright in this code but I encourage its free use provided
  12009. +// that I don't carry any responsibility for the results. I am especially
  12010. +// happy to see it used in free and open source software. If you do use
  12011. +// it I would appreciate an acknowledgement of its origin in the code or
  12012. +// the product that results and I would also appreciate knowing a little
  12013. +// about the use to which it is being put. I am grateful to Frank Yellin
  12014. +// for some ideas that are used in this implementation.
  12015. +//
  12016. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  12017. +//
  12018. +// This is an implementation of the AES encryption algorithm (Rijndael)
  12019. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  12020. +// to provide both fixed and dynamic block and key lengths and can also
  12021. +// run with either big or little endian internal byte order (see aes.h).
  12022. +// It inputs block and key lengths in bytes with the legal values being
  12023. +// 16, 24 and 32.
  12024. +
  12025. +/*
  12026. + * Modified by Jari Ruusu, May 1 2001
  12027. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  12028. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  12029. + * - Major name space cleanup: Names visible to outside now begin
  12030. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  12031. + * - Removed C++ and DLL support as part of name space cleanup.
  12032. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  12033. + * - Merged precomputed constant tables to aes.c file.
  12034. + * - Removed data alignment restrictions for portability reasons.
  12035. + * - Made block and key lengths accept bit count (128/192/256)
  12036. + * as well byte count (16/24/32).
  12037. + * - Removed all error checks. This change also eliminated the need
  12038. + * to preinitialize the context struct to zero.
  12039. + * - Removed some totally unused constants.
  12040. + */
  12041. +
  12042. +#include "crypto/aes.h"
  12043. +
  12044. +// CONFIGURATION OPTIONS (see also aes.h)
  12045. +//
  12046. +// 1. Define UNROLL for full loop unrolling in encryption and decryption.
  12047. +// 2. Define PARTIAL_UNROLL to unroll two loops in encryption and decryption.
  12048. +// 3. Define FIXED_TABLES for compiled rather than dynamic tables.
  12049. +// 4. Define FF_TABLES to use tables for field multiplies and inverses.
  12050. +// Do not enable this without understanding stack space requirements.
  12051. +// 5. Define ARRAYS to use arrays to hold the local state block. If this
  12052. +// is not defined, individually declared 32-bit words are used.
  12053. +// 6. Define FAST_VARIABLE if a high speed variable block implementation
  12054. +// is needed (essentially three separate fixed block size code sequences)
  12055. +// 7. Define either ONE_TABLE or FOUR_TABLES for a fast table driven
  12056. +// version using 1 table (2 kbytes of table space) or 4 tables (8
  12057. +// kbytes of table space) for higher speed.
  12058. +// 8. Define either ONE_LR_TABLE or FOUR_LR_TABLES for a further speed
  12059. +// increase by using tables for the last rounds but with more table
  12060. +// space (2 or 8 kbytes extra).
  12061. +// 9. If neither ONE_TABLE nor FOUR_TABLES is defined, a compact but
  12062. +// slower version is provided.
  12063. +// 10. If fast decryption key scheduling is needed define ONE_IM_TABLE
  12064. +// or FOUR_IM_TABLES for higher speed (2 or 8 kbytes extra).
  12065. +
  12066. +#define UNROLL
  12067. +//#define PARTIAL_UNROLL
  12068. +
  12069. +#define FIXED_TABLES
  12070. +//#define FF_TABLES
  12071. +//#define ARRAYS
  12072. +#define FAST_VARIABLE
  12073. +
  12074. +//#define ONE_TABLE
  12075. +#define FOUR_TABLES
  12076. +
  12077. +//#define ONE_LR_TABLE
  12078. +#define FOUR_LR_TABLES
  12079. +
  12080. +//#define ONE_IM_TABLE
  12081. +#define FOUR_IM_TABLES
  12082. +
  12083. +#if defined(UNROLL) && defined (PARTIAL_UNROLL)
  12084. +#error both UNROLL and PARTIAL_UNROLL are defined
  12085. +#endif
  12086. +
  12087. +#if defined(ONE_TABLE) && defined (FOUR_TABLES)
  12088. +#error both ONE_TABLE and FOUR_TABLES are defined
  12089. +#endif
  12090. +
  12091. +#if defined(ONE_LR_TABLE) && defined (FOUR_LR_TABLES)
  12092. +#error both ONE_LR_TABLE and FOUR_LR_TABLES are defined
  12093. +#endif
  12094. +
  12095. +#if defined(ONE_IM_TABLE) && defined (FOUR_IM_TABLES)
  12096. +#error both ONE_IM_TABLE and FOUR_IM_TABLES are defined
  12097. +#endif
  12098. +
  12099. +#if defined(AES_BLOCK_SIZE) && AES_BLOCK_SIZE != 16 && AES_BLOCK_SIZE != 24 && AES_BLOCK_SIZE != 32
  12100. +#error an illegal block size has been specified
  12101. +#endif
  12102. +
  12103. +// upr(x,n): rotates bytes within words by n positions, moving bytes
  12104. +// to higher index positions with wrap around into low positions
  12105. +// ups(x,n): moves bytes by n positions to higher index positions in
  12106. +// words but without wrap around
  12107. +// bval(x,n): extracts a byte from a word
  12108. +
  12109. +#define upr(x,n) (((x) << 8 * (n)) | ((x) >> (32 - 8 * (n))))
  12110. +#define ups(x,n) ((x) << 8 * (n))
  12111. +#define bval(x,n) ((unsigned char)((x) >> 8 * (n)))
  12112. +#define bytes2word(b0, b1, b2, b3) \
  12113. + ((u_int32_t)(b3) << 24 | (u_int32_t)(b2) << 16 | (u_int32_t)(b1) << 8 | (b0))
  12114. +
  12115. +
  12116. +/* little endian processor without data alignment restrictions: AES_LE_OK */
  12117. +/* original code: i386 */
  12118. +#if defined(i386) || defined(_I386) || defined(__i386__) || defined(__i386)
  12119. +#define AES_LE_OK 1
  12120. +/* added (tested): alpha --jjo */
  12121. +#elif defined(__alpha__)|| defined (__alpha)
  12122. +#define AES_LE_OK 1
  12123. +/* added (tested): ia64 --jjo */
  12124. +#elif defined(__ia64__)|| defined (__ia64)
  12125. +#define AES_LE_OK 1
  12126. +#endif
  12127. +
  12128. +#ifdef AES_LE_OK
  12129. +/* little endian processor without data alignment restrictions */
  12130. +#define word_in(x) *(u_int32_t*)(x)
  12131. +#define const_word_in(x) *(const u_int32_t*)(x)
  12132. +#define word_out(x,v) *(u_int32_t*)(x) = (v)
  12133. +#define const_word_out(x,v) *(const u_int32_t*)(x) = (v)
  12134. +#else
  12135. +/* slower but generic big endian or with data alignment restrictions */
  12136. +/* some additional "const" touches to stop "gcc -Wcast-qual" complains --jjo */
  12137. +#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))
  12138. +#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))
  12139. +#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)
  12140. +#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)
  12141. +#endif
  12142. +
  12143. +// Disable at least some poor combinations of options
  12144. +
  12145. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  12146. +#define FIXED_TABLES
  12147. +#undef UNROLL
  12148. +#undef ONE_LR_TABLE
  12149. +#undef FOUR_LR_TABLES
  12150. +#undef ONE_IM_TABLE
  12151. +#undef FOUR_IM_TABLES
  12152. +#elif !defined(FOUR_TABLES)
  12153. +#ifdef FOUR_LR_TABLES
  12154. +#undef FOUR_LR_TABLES
  12155. +#define ONE_LR_TABLE
  12156. +#endif
  12157. +#ifdef FOUR_IM_TABLES
  12158. +#undef FOUR_IM_TABLES
  12159. +#define ONE_IM_TABLE
  12160. +#endif
  12161. +#elif !defined(AES_BLOCK_SIZE)
  12162. +#if defined(UNROLL)
  12163. +#define PARTIAL_UNROLL
  12164. +#undef UNROLL
  12165. +#endif
  12166. +#endif
  12167. +
  12168. +// the finite field modular polynomial and elements
  12169. +
  12170. +#define ff_poly 0x011b
  12171. +#define ff_hi 0x80
  12172. +
  12173. +// multiply four bytes in GF(2^8) by 'x' {02} in parallel
  12174. +
  12175. +#define m1 0x80808080
  12176. +#define m2 0x7f7f7f7f
  12177. +#define m3 0x0000001b
  12178. +#define FFmulX(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * m3))
  12179. +
  12180. +// The following defines provide alternative definitions of FFmulX that might
  12181. +// give improved performance if a fast 32-bit multiply is not available. Note
  12182. +// that a temporary variable u needs to be defined where FFmulX is used.
  12183. +
  12184. +// #define FFmulX(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
  12185. +// #define m4 0x1b1b1b1b
  12186. +// #define FFmulX(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
  12187. +
  12188. +// perform column mix operation on four bytes in parallel
  12189. +
  12190. +#define fwd_mcol(x) (f2 = FFmulX(x), f2 ^ upr(x ^ f2,3) ^ upr(x,2) ^ upr(x,1))
  12191. +
  12192. +#if defined(FIXED_TABLES)
  12193. +
  12194. +// the S-Box table
  12195. +
  12196. +static const unsigned char s_box[256] =
  12197. +{
  12198. + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  12199. + 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  12200. + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  12201. + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  12202. + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  12203. + 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  12204. + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  12205. + 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  12206. + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  12207. + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  12208. + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  12209. + 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  12210. + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  12211. + 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  12212. + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  12213. + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  12214. + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  12215. + 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  12216. + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  12217. + 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  12218. + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  12219. + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  12220. + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  12221. + 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  12222. + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  12223. + 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  12224. + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  12225. + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  12226. + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  12227. + 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  12228. + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  12229. + 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  12230. +};
  12231. +
  12232. +// the inverse S-Box table
  12233. +
  12234. +static const unsigned char inv_s_box[256] =
  12235. +{
  12236. + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
  12237. + 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  12238. + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
  12239. + 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  12240. + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d,
  12241. + 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  12242. + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2,
  12243. + 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  12244. + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
  12245. + 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  12246. + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda,
  12247. + 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  12248. + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
  12249. + 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  12250. + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
  12251. + 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  12252. + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea,
  12253. + 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  12254. + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85,
  12255. + 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  12256. + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
  12257. + 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  12258. + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20,
  12259. + 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  12260. + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31,
  12261. + 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  12262. + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
  12263. + 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  12264. + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0,
  12265. + 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  12266. + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26,
  12267. + 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  12268. +};
  12269. +
  12270. +#define w0(p) 0x000000##p
  12271. +
  12272. +// Number of elements required in this table for different
  12273. +// block and key lengths is:
  12274. +//
  12275. +// Nk = 4 6 8
  12276. +// ----------
  12277. +// Nb = 4 | 10 8 7
  12278. +// 6 | 19 12 11
  12279. +// 8 | 29 19 14
  12280. +//
  12281. +// this table can be a table of bytes if the key schedule
  12282. +// code is adjusted accordingly
  12283. +
  12284. +static const u_int32_t rcon_tab[29] =
  12285. +{
  12286. + w0(01), w0(02), w0(04), w0(08),
  12287. + w0(10), w0(20), w0(40), w0(80),
  12288. + w0(1b), w0(36), w0(6c), w0(d8),
  12289. + w0(ab), w0(4d), w0(9a), w0(2f),
  12290. + w0(5e), w0(bc), w0(63), w0(c6),
  12291. + w0(97), w0(35), w0(6a), w0(d4),
  12292. + w0(b3), w0(7d), w0(fa), w0(ef),
  12293. + w0(c5)
  12294. +};
  12295. +
  12296. +#undef w0
  12297. +
  12298. +#define r0(p,q,r,s) 0x##p##q##r##s
  12299. +#define r1(p,q,r,s) 0x##q##r##s##p
  12300. +#define r2(p,q,r,s) 0x##r##s##p##q
  12301. +#define r3(p,q,r,s) 0x##s##p##q##r
  12302. +#define w0(p) 0x000000##p
  12303. +#define w1(p) 0x0000##p##00
  12304. +#define w2(p) 0x00##p##0000
  12305. +#define w3(p) 0x##p##000000
  12306. +
  12307. +#if defined(FIXED_TABLES) && (defined(ONE_TABLE) || defined(FOUR_TABLES))
  12308. +
  12309. +// data for forward tables (other than last round)
  12310. +
  12311. +#define f_table \
  12312. + r(a5,63,63,c6), r(84,7c,7c,f8), r(99,77,77,ee), r(8d,7b,7b,f6),\
  12313. + r(0d,f2,f2,ff), r(bd,6b,6b,d6), r(b1,6f,6f,de), r(54,c5,c5,91),\
  12314. + r(50,30,30,60), r(03,01,01,02), r(a9,67,67,ce), r(7d,2b,2b,56),\
  12315. + r(19,fe,fe,e7), r(62,d7,d7,b5), r(e6,ab,ab,4d), r(9a,76,76,ec),\
  12316. + r(45,ca,ca,8f), r(9d,82,82,1f), r(40,c9,c9,89), r(87,7d,7d,fa),\
  12317. + r(15,fa,fa,ef), r(eb,59,59,b2), r(c9,47,47,8e), r(0b,f0,f0,fb),\
  12318. + r(ec,ad,ad,41), r(67,d4,d4,b3), r(fd,a2,a2,5f), r(ea,af,af,45),\
  12319. + r(bf,9c,9c,23), r(f7,a4,a4,53), r(96,72,72,e4), r(5b,c0,c0,9b),\
  12320. + r(c2,b7,b7,75), r(1c,fd,fd,e1), r(ae,93,93,3d), r(6a,26,26,4c),\
  12321. + r(5a,36,36,6c), r(41,3f,3f,7e), r(02,f7,f7,f5), r(4f,cc,cc,83),\
  12322. + r(5c,34,34,68), r(f4,a5,a5,51), r(34,e5,e5,d1), r(08,f1,f1,f9),\
  12323. + r(93,71,71,e2), r(73,d8,d8,ab), r(53,31,31,62), r(3f,15,15,2a),\
  12324. + r(0c,04,04,08), r(52,c7,c7,95), r(65,23,23,46), r(5e,c3,c3,9d),\
  12325. + r(28,18,18,30), r(a1,96,96,37), r(0f,05,05,0a), r(b5,9a,9a,2f),\
  12326. + r(09,07,07,0e), r(36,12,12,24), r(9b,80,80,1b), r(3d,e2,e2,df),\
  12327. + r(26,eb,eb,cd), r(69,27,27,4e), r(cd,b2,b2,7f), r(9f,75,75,ea),\
  12328. + r(1b,09,09,12), r(9e,83,83,1d), r(74,2c,2c,58), r(2e,1a,1a,34),\
  12329. + r(2d,1b,1b,36), r(b2,6e,6e,dc), r(ee,5a,5a,b4), r(fb,a0,a0,5b),\
  12330. + r(f6,52,52,a4), r(4d,3b,3b,76), r(61,d6,d6,b7), r(ce,b3,b3,7d),\
  12331. + r(7b,29,29,52), r(3e,e3,e3,dd), r(71,2f,2f,5e), r(97,84,84,13),\
  12332. + r(f5,53,53,a6), r(68,d1,d1,b9), r(00,00,00,00), r(2c,ed,ed,c1),\
  12333. + r(60,20,20,40), r(1f,fc,fc,e3), r(c8,b1,b1,79), r(ed,5b,5b,b6),\
  12334. + r(be,6a,6a,d4), r(46,cb,cb,8d), r(d9,be,be,67), r(4b,39,39,72),\
  12335. + r(de,4a,4a,94), r(d4,4c,4c,98), r(e8,58,58,b0), r(4a,cf,cf,85),\
  12336. + r(6b,d0,d0,bb), r(2a,ef,ef,c5), r(e5,aa,aa,4f), r(16,fb,fb,ed),\
  12337. + r(c5,43,43,86), r(d7,4d,4d,9a), r(55,33,33,66), r(94,85,85,11),\
  12338. + r(cf,45,45,8a), r(10,f9,f9,e9), r(06,02,02,04), r(81,7f,7f,fe),\
  12339. + r(f0,50,50,a0), r(44,3c,3c,78), r(ba,9f,9f,25), r(e3,a8,a8,4b),\
  12340. + r(f3,51,51,a2), r(fe,a3,a3,5d), r(c0,40,40,80), r(8a,8f,8f,05),\
  12341. + r(ad,92,92,3f), r(bc,9d,9d,21), r(48,38,38,70), r(04,f5,f5,f1),\
  12342. + r(df,bc,bc,63), r(c1,b6,b6,77), r(75,da,da,af), r(63,21,21,42),\
  12343. + r(30,10,10,20), r(1a,ff,ff,e5), r(0e,f3,f3,fd), r(6d,d2,d2,bf),\
  12344. + r(4c,cd,cd,81), r(14,0c,0c,18), r(35,13,13,26), r(2f,ec,ec,c3),\
  12345. + r(e1,5f,5f,be), r(a2,97,97,35), r(cc,44,44,88), r(39,17,17,2e),\
  12346. + r(57,c4,c4,93), r(f2,a7,a7,55), r(82,7e,7e,fc), r(47,3d,3d,7a),\
  12347. + r(ac,64,64,c8), r(e7,5d,5d,ba), r(2b,19,19,32), r(95,73,73,e6),\
  12348. + r(a0,60,60,c0), r(98,81,81,19), r(d1,4f,4f,9e), r(7f,dc,dc,a3),\
  12349. + r(66,22,22,44), r(7e,2a,2a,54), r(ab,90,90,3b), r(83,88,88,0b),\
  12350. + r(ca,46,46,8c), r(29,ee,ee,c7), r(d3,b8,b8,6b), r(3c,14,14,28),\
  12351. + r(79,de,de,a7), r(e2,5e,5e,bc), r(1d,0b,0b,16), r(76,db,db,ad),\
  12352. + r(3b,e0,e0,db), r(56,32,32,64), r(4e,3a,3a,74), r(1e,0a,0a,14),\
  12353. + r(db,49,49,92), r(0a,06,06,0c), r(6c,24,24,48), r(e4,5c,5c,b8),\
  12354. + r(5d,c2,c2,9f), r(6e,d3,d3,bd), r(ef,ac,ac,43), r(a6,62,62,c4),\
  12355. + r(a8,91,91,39), r(a4,95,95,31), r(37,e4,e4,d3), r(8b,79,79,f2),\
  12356. + r(32,e7,e7,d5), r(43,c8,c8,8b), r(59,37,37,6e), r(b7,6d,6d,da),\
  12357. + r(8c,8d,8d,01), r(64,d5,d5,b1), r(d2,4e,4e,9c), r(e0,a9,a9,49),\
  12358. + r(b4,6c,6c,d8), r(fa,56,56,ac), r(07,f4,f4,f3), r(25,ea,ea,cf),\
  12359. + r(af,65,65,ca), r(8e,7a,7a,f4), r(e9,ae,ae,47), r(18,08,08,10),\
  12360. + r(d5,ba,ba,6f), r(88,78,78,f0), r(6f,25,25,4a), r(72,2e,2e,5c),\
  12361. + r(24,1c,1c,38), r(f1,a6,a6,57), r(c7,b4,b4,73), r(51,c6,c6,97),\
  12362. + r(23,e8,e8,cb), r(7c,dd,dd,a1), r(9c,74,74,e8), r(21,1f,1f,3e),\
  12363. + r(dd,4b,4b,96), r(dc,bd,bd,61), r(86,8b,8b,0d), r(85,8a,8a,0f),\
  12364. + r(90,70,70,e0), r(42,3e,3e,7c), r(c4,b5,b5,71), r(aa,66,66,cc),\
  12365. + r(d8,48,48,90), r(05,03,03,06), r(01,f6,f6,f7), r(12,0e,0e,1c),\
  12366. + r(a3,61,61,c2), r(5f,35,35,6a), r(f9,57,57,ae), r(d0,b9,b9,69),\
  12367. + r(91,86,86,17), r(58,c1,c1,99), r(27,1d,1d,3a), r(b9,9e,9e,27),\
  12368. + r(38,e1,e1,d9), r(13,f8,f8,eb), r(b3,98,98,2b), r(33,11,11,22),\
  12369. + r(bb,69,69,d2), r(70,d9,d9,a9), r(89,8e,8e,07), r(a7,94,94,33),\
  12370. + r(b6,9b,9b,2d), r(22,1e,1e,3c), r(92,87,87,15), r(20,e9,e9,c9),\
  12371. + r(49,ce,ce,87), r(ff,55,55,aa), r(78,28,28,50), r(7a,df,df,a5),\
  12372. + r(8f,8c,8c,03), r(f8,a1,a1,59), r(80,89,89,09), r(17,0d,0d,1a),\
  12373. + r(da,bf,bf,65), r(31,e6,e6,d7), r(c6,42,42,84), r(b8,68,68,d0),\
  12374. + r(c3,41,41,82), r(b0,99,99,29), r(77,2d,2d,5a), r(11,0f,0f,1e),\
  12375. + r(cb,b0,b0,7b), r(fc,54,54,a8), r(d6,bb,bb,6d), r(3a,16,16,2c)
  12376. +
  12377. +// data for inverse tables (other than last round)
  12378. +
  12379. +#define i_table \
  12380. + r(50,a7,f4,51), r(53,65,41,7e), r(c3,a4,17,1a), r(96,5e,27,3a),\
  12381. + r(cb,6b,ab,3b), r(f1,45,9d,1f), r(ab,58,fa,ac), r(93,03,e3,4b),\
  12382. + r(55,fa,30,20), r(f6,6d,76,ad), r(91,76,cc,88), r(25,4c,02,f5),\
  12383. + r(fc,d7,e5,4f), r(d7,cb,2a,c5), r(80,44,35,26), r(8f,a3,62,b5),\
  12384. + r(49,5a,b1,de), r(67,1b,ba,25), r(98,0e,ea,45), r(e1,c0,fe,5d),\
  12385. + r(02,75,2f,c3), r(12,f0,4c,81), r(a3,97,46,8d), r(c6,f9,d3,6b),\
  12386. + r(e7,5f,8f,03), r(95,9c,92,15), r(eb,7a,6d,bf), r(da,59,52,95),\
  12387. + r(2d,83,be,d4), r(d3,21,74,58), r(29,69,e0,49), r(44,c8,c9,8e),\
  12388. + r(6a,89,c2,75), r(78,79,8e,f4), r(6b,3e,58,99), r(dd,71,b9,27),\
  12389. + r(b6,4f,e1,be), r(17,ad,88,f0), r(66,ac,20,c9), r(b4,3a,ce,7d),\
  12390. + r(18,4a,df,63), r(82,31,1a,e5), r(60,33,51,97), r(45,7f,53,62),\
  12391. + r(e0,77,64,b1), r(84,ae,6b,bb), r(1c,a0,81,fe), r(94,2b,08,f9),\
  12392. + r(58,68,48,70), r(19,fd,45,8f), r(87,6c,de,94), r(b7,f8,7b,52),\
  12393. + r(23,d3,73,ab), r(e2,02,4b,72), r(57,8f,1f,e3), r(2a,ab,55,66),\
  12394. + r(07,28,eb,b2), r(03,c2,b5,2f), r(9a,7b,c5,86), r(a5,08,37,d3),\
  12395. + r(f2,87,28,30), r(b2,a5,bf,23), r(ba,6a,03,02), r(5c,82,16,ed),\
  12396. + r(2b,1c,cf,8a), r(92,b4,79,a7), r(f0,f2,07,f3), r(a1,e2,69,4e),\
  12397. + r(cd,f4,da,65), r(d5,be,05,06), r(1f,62,34,d1), r(8a,fe,a6,c4),\
  12398. + r(9d,53,2e,34), r(a0,55,f3,a2), r(32,e1,8a,05), r(75,eb,f6,a4),\
  12399. + r(39,ec,83,0b), r(aa,ef,60,40), r(06,9f,71,5e), r(51,10,6e,bd),\
  12400. + r(f9,8a,21,3e), r(3d,06,dd,96), r(ae,05,3e,dd), r(46,bd,e6,4d),\
  12401. + r(b5,8d,54,91), r(05,5d,c4,71), r(6f,d4,06,04), r(ff,15,50,60),\
  12402. + r(24,fb,98,19), r(97,e9,bd,d6), r(cc,43,40,89), r(77,9e,d9,67),\
  12403. + r(bd,42,e8,b0), r(88,8b,89,07), r(38,5b,19,e7), r(db,ee,c8,79),\
  12404. + r(47,0a,7c,a1), r(e9,0f,42,7c), r(c9,1e,84,f8), r(00,00,00,00),\
  12405. + r(83,86,80,09), r(48,ed,2b,32), r(ac,70,11,1e), r(4e,72,5a,6c),\
  12406. + r(fb,ff,0e,fd), r(56,38,85,0f), r(1e,d5,ae,3d), r(27,39,2d,36),\
  12407. + r(64,d9,0f,0a), r(21,a6,5c,68), r(d1,54,5b,9b), r(3a,2e,36,24),\
  12408. + r(b1,67,0a,0c), r(0f,e7,57,93), r(d2,96,ee,b4), r(9e,91,9b,1b),\
  12409. + r(4f,c5,c0,80), r(a2,20,dc,61), r(69,4b,77,5a), r(16,1a,12,1c),\
  12410. + r(0a,ba,93,e2), r(e5,2a,a0,c0), r(43,e0,22,3c), r(1d,17,1b,12),\
  12411. + r(0b,0d,09,0e), r(ad,c7,8b,f2), r(b9,a8,b6,2d), r(c8,a9,1e,14),\
  12412. + r(85,19,f1,57), r(4c,07,75,af), r(bb,dd,99,ee), r(fd,60,7f,a3),\
  12413. + r(9f,26,01,f7), r(bc,f5,72,5c), r(c5,3b,66,44), r(34,7e,fb,5b),\
  12414. + r(76,29,43,8b), r(dc,c6,23,cb), r(68,fc,ed,b6), r(63,f1,e4,b8),\
  12415. + r(ca,dc,31,d7), r(10,85,63,42), r(40,22,97,13), r(20,11,c6,84),\
  12416. + r(7d,24,4a,85), r(f8,3d,bb,d2), r(11,32,f9,ae), r(6d,a1,29,c7),\
  12417. + r(4b,2f,9e,1d), r(f3,30,b2,dc), r(ec,52,86,0d), r(d0,e3,c1,77),\
  12418. + r(6c,16,b3,2b), r(99,b9,70,a9), r(fa,48,94,11), r(22,64,e9,47),\
  12419. + r(c4,8c,fc,a8), r(1a,3f,f0,a0), r(d8,2c,7d,56), r(ef,90,33,22),\
  12420. + r(c7,4e,49,87), r(c1,d1,38,d9), r(fe,a2,ca,8c), r(36,0b,d4,98),\
  12421. + r(cf,81,f5,a6), r(28,de,7a,a5), r(26,8e,b7,da), r(a4,bf,ad,3f),\
  12422. + r(e4,9d,3a,2c), r(0d,92,78,50), r(9b,cc,5f,6a), r(62,46,7e,54),\
  12423. + r(c2,13,8d,f6), r(e8,b8,d8,90), r(5e,f7,39,2e), r(f5,af,c3,82),\
  12424. + r(be,80,5d,9f), r(7c,93,d0,69), r(a9,2d,d5,6f), r(b3,12,25,cf),\
  12425. + r(3b,99,ac,c8), r(a7,7d,18,10), r(6e,63,9c,e8), r(7b,bb,3b,db),\
  12426. + r(09,78,26,cd), r(f4,18,59,6e), r(01,b7,9a,ec), r(a8,9a,4f,83),\
  12427. + r(65,6e,95,e6), r(7e,e6,ff,aa), r(08,cf,bc,21), r(e6,e8,15,ef),\
  12428. + r(d9,9b,e7,ba), r(ce,36,6f,4a), r(d4,09,9f,ea), r(d6,7c,b0,29),\
  12429. + r(af,b2,a4,31), r(31,23,3f,2a), r(30,94,a5,c6), r(c0,66,a2,35),\
  12430. + r(37,bc,4e,74), r(a6,ca,82,fc), r(b0,d0,90,e0), r(15,d8,a7,33),\
  12431. + r(4a,98,04,f1), r(f7,da,ec,41), r(0e,50,cd,7f), r(2f,f6,91,17),\
  12432. + r(8d,d6,4d,76), r(4d,b0,ef,43), r(54,4d,aa,cc), r(df,04,96,e4),\
  12433. + r(e3,b5,d1,9e), r(1b,88,6a,4c), r(b8,1f,2c,c1), r(7f,51,65,46),\
  12434. + r(04,ea,5e,9d), r(5d,35,8c,01), r(73,74,87,fa), r(2e,41,0b,fb),\
  12435. + r(5a,1d,67,b3), r(52,d2,db,92), r(33,56,10,e9), r(13,47,d6,6d),\
  12436. + r(8c,61,d7,9a), r(7a,0c,a1,37), r(8e,14,f8,59), r(89,3c,13,eb),\
  12437. + r(ee,27,a9,ce), r(35,c9,61,b7), r(ed,e5,1c,e1), r(3c,b1,47,7a),\
  12438. + r(59,df,d2,9c), r(3f,73,f2,55), r(79,ce,14,18), r(bf,37,c7,73),\
  12439. + r(ea,cd,f7,53), r(5b,aa,fd,5f), r(14,6f,3d,df), r(86,db,44,78),\
  12440. + r(81,f3,af,ca), r(3e,c4,68,b9), r(2c,34,24,38), r(5f,40,a3,c2),\
  12441. + r(72,c3,1d,16), r(0c,25,e2,bc), r(8b,49,3c,28), r(41,95,0d,ff),\
  12442. + r(71,01,a8,39), r(de,b3,0c,08), r(9c,e4,b4,d8), r(90,c1,56,64),\
  12443. + r(61,84,cb,7b), r(70,b6,32,d5), r(74,5c,6c,48), r(42,57,b8,d0)
  12444. +
  12445. +// generate the required tables in the desired endian format
  12446. +
  12447. +#undef r
  12448. +#define r r0
  12449. +
  12450. +#if defined(ONE_TABLE)
  12451. +static const u_int32_t ft_tab[256] =
  12452. + { f_table };
  12453. +#elif defined(FOUR_TABLES)
  12454. +static const u_int32_t ft_tab[4][256] =
  12455. +{ { f_table },
  12456. +#undef r
  12457. +#define r r1
  12458. + { f_table },
  12459. +#undef r
  12460. +#define r r2
  12461. + { f_table },
  12462. +#undef r
  12463. +#define r r3
  12464. + { f_table }
  12465. +};
  12466. +#endif
  12467. +
  12468. +#undef r
  12469. +#define r r0
  12470. +#if defined(ONE_TABLE)
  12471. +static const u_int32_t it_tab[256] =
  12472. + { i_table };
  12473. +#elif defined(FOUR_TABLES)
  12474. +static const u_int32_t it_tab[4][256] =
  12475. +{ { i_table },
  12476. +#undef r
  12477. +#define r r1
  12478. + { i_table },
  12479. +#undef r
  12480. +#define r r2
  12481. + { i_table },
  12482. +#undef r
  12483. +#define r r3
  12484. + { i_table }
  12485. +};
  12486. +#endif
  12487. +
  12488. +#endif
  12489. +
  12490. +#if defined(FIXED_TABLES) && (defined(ONE_LR_TABLE) || defined(FOUR_LR_TABLES))
  12491. +
  12492. +// data for inverse tables (last round)
  12493. +
  12494. +#define li_table \
  12495. + w(52), w(09), w(6a), w(d5), w(30), w(36), w(a5), w(38),\
  12496. + w(bf), w(40), w(a3), w(9e), w(81), w(f3), w(d7), w(fb),\
  12497. + w(7c), w(e3), w(39), w(82), w(9b), w(2f), w(ff), w(87),\
  12498. + w(34), w(8e), w(43), w(44), w(c4), w(de), w(e9), w(cb),\
  12499. + w(54), w(7b), w(94), w(32), w(a6), w(c2), w(23), w(3d),\
  12500. + w(ee), w(4c), w(95), w(0b), w(42), w(fa), w(c3), w(4e),\
  12501. + w(08), w(2e), w(a1), w(66), w(28), w(d9), w(24), w(b2),\
  12502. + w(76), w(5b), w(a2), w(49), w(6d), w(8b), w(d1), w(25),\
  12503. + w(72), w(f8), w(f6), w(64), w(86), w(68), w(98), w(16),\
  12504. + w(d4), w(a4), w(5c), w(cc), w(5d), w(65), w(b6), w(92),\
  12505. + w(6c), w(70), w(48), w(50), w(fd), w(ed), w(b9), w(da),\
  12506. + w(5e), w(15), w(46), w(57), w(a7), w(8d), w(9d), w(84),\
  12507. + w(90), w(d8), w(ab), w(00), w(8c), w(bc), w(d3), w(0a),\
  12508. + w(f7), w(e4), w(58), w(05), w(b8), w(b3), w(45), w(06),\
  12509. + w(d0), w(2c), w(1e), w(8f), w(ca), w(3f), w(0f), w(02),\
  12510. + w(c1), w(af), w(bd), w(03), w(01), w(13), w(8a), w(6b),\
  12511. + w(3a), w(91), w(11), w(41), w(4f), w(67), w(dc), w(ea),\
  12512. + w(97), w(f2), w(cf), w(ce), w(f0), w(b4), w(e6), w(73),\
  12513. + w(96), w(ac), w(74), w(22), w(e7), w(ad), w(35), w(85),\
  12514. + w(e2), w(f9), w(37), w(e8), w(1c), w(75), w(df), w(6e),\
  12515. + w(47), w(f1), w(1a), w(71), w(1d), w(29), w(c5), w(89),\
  12516. + w(6f), w(b7), w(62), w(0e), w(aa), w(18), w(be), w(1b),\
  12517. + w(fc), w(56), w(3e), w(4b), w(c6), w(d2), w(79), w(20),\
  12518. + w(9a), w(db), w(c0), w(fe), w(78), w(cd), w(5a), w(f4),\
  12519. + w(1f), w(dd), w(a8), w(33), w(88), w(07), w(c7), w(31),\
  12520. + w(b1), w(12), w(10), w(59), w(27), w(80), w(ec), w(5f),\
  12521. + w(60), w(51), w(7f), w(a9), w(19), w(b5), w(4a), w(0d),\
  12522. + w(2d), w(e5), w(7a), w(9f), w(93), w(c9), w(9c), w(ef),\
  12523. + w(a0), w(e0), w(3b), w(4d), w(ae), w(2a), w(f5), w(b0),\
  12524. + w(c8), w(eb), w(bb), w(3c), w(83), w(53), w(99), w(61),\
  12525. + w(17), w(2b), w(04), w(7e), w(ba), w(77), w(d6), w(26),\
  12526. + w(e1), w(69), w(14), w(63), w(55), w(21), w(0c), w(7d),
  12527. +
  12528. +// generate the required tables in the desired endian format
  12529. +
  12530. +#undef r
  12531. +#define r(p,q,r,s) w0(q)
  12532. +#if defined(ONE_LR_TABLE)
  12533. +static const u_int32_t fl_tab[256] =
  12534. + { f_table };
  12535. +#elif defined(FOUR_LR_TABLES)
  12536. +static const u_int32_t fl_tab[4][256] =
  12537. +{ { f_table },
  12538. +#undef r
  12539. +#define r(p,q,r,s) w1(q)
  12540. + { f_table },
  12541. +#undef r
  12542. +#define r(p,q,r,s) w2(q)
  12543. + { f_table },
  12544. +#undef r
  12545. +#define r(p,q,r,s) w3(q)
  12546. + { f_table }
  12547. +};
  12548. +#endif
  12549. +
  12550. +#undef w
  12551. +#define w w0
  12552. +#if defined(ONE_LR_TABLE)
  12553. +static const u_int32_t il_tab[256] =
  12554. + { li_table };
  12555. +#elif defined(FOUR_LR_TABLES)
  12556. +static const u_int32_t il_tab[4][256] =
  12557. +{ { li_table },
  12558. +#undef w
  12559. +#define w w1
  12560. + { li_table },
  12561. +#undef w
  12562. +#define w w2
  12563. + { li_table },
  12564. +#undef w
  12565. +#define w w3
  12566. + { li_table }
  12567. +};
  12568. +#endif
  12569. +
  12570. +#endif
  12571. +
  12572. +#if defined(FIXED_TABLES) && (defined(ONE_IM_TABLE) || defined(FOUR_IM_TABLES))
  12573. +
  12574. +#define m_table \
  12575. + r(00,00,00,00), r(0b,0d,09,0e), r(16,1a,12,1c), r(1d,17,1b,12),\
  12576. + r(2c,34,24,38), r(27,39,2d,36), r(3a,2e,36,24), r(31,23,3f,2a),\
  12577. + r(58,68,48,70), r(53,65,41,7e), r(4e,72,5a,6c), r(45,7f,53,62),\
  12578. + r(74,5c,6c,48), r(7f,51,65,46), r(62,46,7e,54), r(69,4b,77,5a),\
  12579. + r(b0,d0,90,e0), r(bb,dd,99,ee), r(a6,ca,82,fc), r(ad,c7,8b,f2),\
  12580. + r(9c,e4,b4,d8), r(97,e9,bd,d6), r(8a,fe,a6,c4), r(81,f3,af,ca),\
  12581. + r(e8,b8,d8,90), r(e3,b5,d1,9e), r(fe,a2,ca,8c), r(f5,af,c3,82),\
  12582. + r(c4,8c,fc,a8), r(cf,81,f5,a6), r(d2,96,ee,b4), r(d9,9b,e7,ba),\
  12583. + r(7b,bb,3b,db), r(70,b6,32,d5), r(6d,a1,29,c7), r(66,ac,20,c9),\
  12584. + r(57,8f,1f,e3), r(5c,82,16,ed), r(41,95,0d,ff), r(4a,98,04,f1),\
  12585. + r(23,d3,73,ab), r(28,de,7a,a5), r(35,c9,61,b7), r(3e,c4,68,b9),\
  12586. + r(0f,e7,57,93), r(04,ea,5e,9d), r(19,fd,45,8f), r(12,f0,4c,81),\
  12587. + r(cb,6b,ab,3b), r(c0,66,a2,35), r(dd,71,b9,27), r(d6,7c,b0,29),\
  12588. + r(e7,5f,8f,03), r(ec,52,86,0d), r(f1,45,9d,1f), r(fa,48,94,11),\
  12589. + r(93,03,e3,4b), r(98,0e,ea,45), r(85,19,f1,57), r(8e,14,f8,59),\
  12590. + r(bf,37,c7,73), r(b4,3a,ce,7d), r(a9,2d,d5,6f), r(a2,20,dc,61),\
  12591. + r(f6,6d,76,ad), r(fd,60,7f,a3), r(e0,77,64,b1), r(eb,7a,6d,bf),\
  12592. + r(da,59,52,95), r(d1,54,5b,9b), r(cc,43,40,89), r(c7,4e,49,87),\
  12593. + r(ae,05,3e,dd), r(a5,08,37,d3), r(b8,1f,2c,c1), r(b3,12,25,cf),\
  12594. + r(82,31,1a,e5), r(89,3c,13,eb), r(94,2b,08,f9), r(9f,26,01,f7),\
  12595. + r(46,bd,e6,4d), r(4d,b0,ef,43), r(50,a7,f4,51), r(5b,aa,fd,5f),\
  12596. + r(6a,89,c2,75), r(61,84,cb,7b), r(7c,93,d0,69), r(77,9e,d9,67),\
  12597. + r(1e,d5,ae,3d), r(15,d8,a7,33), r(08,cf,bc,21), r(03,c2,b5,2f),\
  12598. + r(32,e1,8a,05), r(39,ec,83,0b), r(24,fb,98,19), r(2f,f6,91,17),\
  12599. + r(8d,d6,4d,76), r(86,db,44,78), r(9b,cc,5f,6a), r(90,c1,56,64),\
  12600. + r(a1,e2,69,4e), r(aa,ef,60,40), r(b7,f8,7b,52), r(bc,f5,72,5c),\
  12601. + r(d5,be,05,06), r(de,b3,0c,08), r(c3,a4,17,1a), r(c8,a9,1e,14),\
  12602. + r(f9,8a,21,3e), r(f2,87,28,30), r(ef,90,33,22), r(e4,9d,3a,2c),\
  12603. + r(3d,06,dd,96), r(36,0b,d4,98), r(2b,1c,cf,8a), r(20,11,c6,84),\
  12604. + r(11,32,f9,ae), r(1a,3f,f0,a0), r(07,28,eb,b2), r(0c,25,e2,bc),\
  12605. + r(65,6e,95,e6), r(6e,63,9c,e8), r(73,74,87,fa), r(78,79,8e,f4),\
  12606. + r(49,5a,b1,de), r(42,57,b8,d0), r(5f,40,a3,c2), r(54,4d,aa,cc),\
  12607. + r(f7,da,ec,41), r(fc,d7,e5,4f), r(e1,c0,fe,5d), r(ea,cd,f7,53),\
  12608. + r(db,ee,c8,79), r(d0,e3,c1,77), r(cd,f4,da,65), r(c6,f9,d3,6b),\
  12609. + r(af,b2,a4,31), r(a4,bf,ad,3f), r(b9,a8,b6,2d), r(b2,a5,bf,23),\
  12610. + r(83,86,80,09), r(88,8b,89,07), r(95,9c,92,15), r(9e,91,9b,1b),\
  12611. + r(47,0a,7c,a1), r(4c,07,75,af), r(51,10,6e,bd), r(5a,1d,67,b3),\
  12612. + r(6b,3e,58,99), r(60,33,51,97), r(7d,24,4a,85), r(76,29,43,8b),\
  12613. + r(1f,62,34,d1), r(14,6f,3d,df), r(09,78,26,cd), r(02,75,2f,c3),\
  12614. + r(33,56,10,e9), r(38,5b,19,e7), r(25,4c,02,f5), r(2e,41,0b,fb),\
  12615. + r(8c,61,d7,9a), r(87,6c,de,94), r(9a,7b,c5,86), r(91,76,cc,88),\
  12616. + r(a0,55,f3,a2), r(ab,58,fa,ac), r(b6,4f,e1,be), r(bd,42,e8,b0),\
  12617. + r(d4,09,9f,ea), r(df,04,96,e4), r(c2,13,8d,f6), r(c9,1e,84,f8),\
  12618. + r(f8,3d,bb,d2), r(f3,30,b2,dc), r(ee,27,a9,ce), r(e5,2a,a0,c0),\
  12619. + r(3c,b1,47,7a), r(37,bc,4e,74), r(2a,ab,55,66), r(21,a6,5c,68),\
  12620. + r(10,85,63,42), r(1b,88,6a,4c), r(06,9f,71,5e), r(0d,92,78,50),\
  12621. + r(64,d9,0f,0a), r(6f,d4,06,04), r(72,c3,1d,16), r(79,ce,14,18),\
  12622. + r(48,ed,2b,32), r(43,e0,22,3c), r(5e,f7,39,2e), r(55,fa,30,20),\
  12623. + r(01,b7,9a,ec), r(0a,ba,93,e2), r(17,ad,88,f0), r(1c,a0,81,fe),\
  12624. + r(2d,83,be,d4), r(26,8e,b7,da), r(3b,99,ac,c8), r(30,94,a5,c6),\
  12625. + r(59,df,d2,9c), r(52,d2,db,92), r(4f,c5,c0,80), r(44,c8,c9,8e),\
  12626. + r(75,eb,f6,a4), r(7e,e6,ff,aa), r(63,f1,e4,b8), r(68,fc,ed,b6),\
  12627. + r(b1,67,0a,0c), r(ba,6a,03,02), r(a7,7d,18,10), r(ac,70,11,1e),\
  12628. + r(9d,53,2e,34), r(96,5e,27,3a), r(8b,49,3c,28), r(80,44,35,26),\
  12629. + r(e9,0f,42,7c), r(e2,02,4b,72), r(ff,15,50,60), r(f4,18,59,6e),\
  12630. + r(c5,3b,66,44), r(ce,36,6f,4a), r(d3,21,74,58), r(d8,2c,7d,56),\
  12631. + r(7a,0c,a1,37), r(71,01,a8,39), r(6c,16,b3,2b), r(67,1b,ba,25),\
  12632. + r(56,38,85,0f), r(5d,35,8c,01), r(40,22,97,13), r(4b,2f,9e,1d),\
  12633. + r(22,64,e9,47), r(29,69,e0,49), r(34,7e,fb,5b), r(3f,73,f2,55),\
  12634. + r(0e,50,cd,7f), r(05,5d,c4,71), r(18,4a,df,63), r(13,47,d6,6d),\
  12635. + r(ca,dc,31,d7), r(c1,d1,38,d9), r(dc,c6,23,cb), r(d7,cb,2a,c5),\
  12636. + r(e6,e8,15,ef), r(ed,e5,1c,e1), r(f0,f2,07,f3), r(fb,ff,0e,fd),\
  12637. + r(92,b4,79,a7), r(99,b9,70,a9), r(84,ae,6b,bb), r(8f,a3,62,b5),\
  12638. + r(be,80,5d,9f), r(b5,8d,54,91), r(a8,9a,4f,83), r(a3,97,46,8d)
  12639. +
  12640. +#undef r
  12641. +#define r r0
  12642. +
  12643. +#if defined(ONE_IM_TABLE)
  12644. +static const u_int32_t im_tab[256] =
  12645. + { m_table };
  12646. +#elif defined(FOUR_IM_TABLES)
  12647. +static const u_int32_t im_tab[4][256] =
  12648. +{ { m_table },
  12649. +#undef r
  12650. +#define r r1
  12651. + { m_table },
  12652. +#undef r
  12653. +#define r r2
  12654. + { m_table },
  12655. +#undef r
  12656. +#define r r3
  12657. + { m_table }
  12658. +};
  12659. +#endif
  12660. +
  12661. +#endif
  12662. +
  12663. +#else
  12664. +
  12665. +static int tab_gen = 0;
  12666. +
  12667. +static unsigned char s_box[256]; // the S box
  12668. +static unsigned char inv_s_box[256]; // the inverse S box
  12669. +static u_int32_t rcon_tab[AES_RC_LENGTH]; // table of round constants
  12670. +
  12671. +#if defined(ONE_TABLE)
  12672. +static u_int32_t ft_tab[256];
  12673. +static u_int32_t it_tab[256];
  12674. +#elif defined(FOUR_TABLES)
  12675. +static u_int32_t ft_tab[4][256];
  12676. +static u_int32_t it_tab[4][256];
  12677. +#endif
  12678. +
  12679. +#if defined(ONE_LR_TABLE)
  12680. +static u_int32_t fl_tab[256];
  12681. +static u_int32_t il_tab[256];
  12682. +#elif defined(FOUR_LR_TABLES)
  12683. +static u_int32_t fl_tab[4][256];
  12684. +static u_int32_t il_tab[4][256];
  12685. +#endif
  12686. +
  12687. +#if defined(ONE_IM_TABLE)
  12688. +static u_int32_t im_tab[256];
  12689. +#elif defined(FOUR_IM_TABLES)
  12690. +static u_int32_t im_tab[4][256];
  12691. +#endif
  12692. +
  12693. +// Generate the tables for the dynamic table option
  12694. +
  12695. +#if !defined(FF_TABLES)
  12696. +
  12697. +// It will generally be sensible to use tables to compute finite
  12698. +// field multiplies and inverses but where memory is scarse this
  12699. +// code might sometimes be better.
  12700. +
  12701. +// return 2 ^ (n - 1) where n is the bit number of the highest bit
  12702. +// set in x with x in the range 1 < x < 0x00000200. This form is
  12703. +// used so that locals within FFinv can be bytes rather than words
  12704. +
  12705. +static unsigned char hibit(const u_int32_t x)
  12706. +{ unsigned char r = (unsigned char)((x >> 1) | (x >> 2));
  12707. +
  12708. + r |= (r >> 2);
  12709. + r |= (r >> 4);
  12710. + return (r + 1) >> 1;
  12711. +}
  12712. +
  12713. +// return the inverse of the finite field element x
  12714. +
  12715. +static unsigned char FFinv(const unsigned char x)
  12716. +{ unsigned char p1 = x, p2 = 0x1b, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  12717. +
  12718. + if(x < 2) return x;
  12719. +
  12720. + for(;;)
  12721. + {
  12722. + if(!n1) return v1;
  12723. +
  12724. + while(n2 >= n1)
  12725. + {
  12726. + n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
  12727. + }
  12728. +
  12729. + if(!n2) return v2;
  12730. +
  12731. + while(n1 >= n2)
  12732. + {
  12733. + n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
  12734. + }
  12735. + }
  12736. +}
  12737. +
  12738. +// define the finite field multiplies required for Rijndael
  12739. +
  12740. +#define FFmul02(x) ((((x) & 0x7f) << 1) ^ ((x) & 0x80 ? 0x1b : 0))
  12741. +#define FFmul03(x) ((x) ^ FFmul02(x))
  12742. +#define FFmul09(x) ((x) ^ FFmul02(FFmul02(FFmul02(x))))
  12743. +#define FFmul0b(x) ((x) ^ FFmul02((x) ^ FFmul02(FFmul02(x))))
  12744. +#define FFmul0d(x) ((x) ^ FFmul02(FFmul02((x) ^ FFmul02(x))))
  12745. +#define FFmul0e(x) FFmul02((x) ^ FFmul02((x) ^ FFmul02(x)))
  12746. +
  12747. +#else
  12748. +
  12749. +#define FFinv(x) ((x) ? pow[255 - log[x]]: 0)
  12750. +
  12751. +#define FFmul02(x) (x ? pow[log[x] + 0x19] : 0)
  12752. +#define FFmul03(x) (x ? pow[log[x] + 0x01] : 0)
  12753. +#define FFmul09(x) (x ? pow[log[x] + 0xc7] : 0)
  12754. +#define FFmul0b(x) (x ? pow[log[x] + 0x68] : 0)
  12755. +#define FFmul0d(x) (x ? pow[log[x] + 0xee] : 0)
  12756. +#define FFmul0e(x) (x ? pow[log[x] + 0xdf] : 0)
  12757. +
  12758. +#endif
  12759. +
  12760. +// The forward and inverse affine transformations used in the S-box
  12761. +
  12762. +#define fwd_affine(x) \
  12763. + (w = (u_int32_t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(unsigned char)(w^(w>>8)))
  12764. +
  12765. +#define inv_affine(x) \
  12766. + (w = (u_int32_t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(unsigned char)(w^(w>>8)))
  12767. +
  12768. +static void gen_tabs(void)
  12769. +{ u_int32_t i, w;
  12770. +
  12771. +#if defined(FF_TABLES)
  12772. +
  12773. + unsigned char pow[512], log[256];
  12774. +
  12775. + // log and power tables for GF(2^8) finite field with
  12776. + // 0x011b as modular polynomial - the simplest primitive
  12777. + // root is 0x03, used here to generate the tables
  12778. +
  12779. + i = 0; w = 1;
  12780. + do
  12781. + {
  12782. + pow[i] = (unsigned char)w;
  12783. + pow[i + 255] = (unsigned char)w;
  12784. + log[w] = (unsigned char)i++;
  12785. + w ^= (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  12786. + }
  12787. + while (w != 1);
  12788. +
  12789. +#endif
  12790. +
  12791. + for(i = 0, w = 1; i < AES_RC_LENGTH; ++i)
  12792. + {
  12793. + rcon_tab[i] = bytes2word(w, 0, 0, 0);
  12794. + w = (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  12795. + }
  12796. +
  12797. + for(i = 0; i < 256; ++i)
  12798. + { unsigned char b;
  12799. +
  12800. + s_box[i] = b = fwd_affine(FFinv((unsigned char)i));
  12801. +
  12802. + w = bytes2word(b, 0, 0, 0);
  12803. +#if defined(ONE_LR_TABLE)
  12804. + fl_tab[i] = w;
  12805. +#elif defined(FOUR_LR_TABLES)
  12806. + fl_tab[0][i] = w;
  12807. + fl_tab[1][i] = upr(w,1);
  12808. + fl_tab[2][i] = upr(w,2);
  12809. + fl_tab[3][i] = upr(w,3);
  12810. +#endif
  12811. + w = bytes2word(FFmul02(b), b, b, FFmul03(b));
  12812. +#if defined(ONE_TABLE)
  12813. + ft_tab[i] = w;
  12814. +#elif defined(FOUR_TABLES)
  12815. + ft_tab[0][i] = w;
  12816. + ft_tab[1][i] = upr(w,1);
  12817. + ft_tab[2][i] = upr(w,2);
  12818. + ft_tab[3][i] = upr(w,3);
  12819. +#endif
  12820. + inv_s_box[i] = b = FFinv(inv_affine((unsigned char)i));
  12821. +
  12822. + w = bytes2word(b, 0, 0, 0);
  12823. +#if defined(ONE_LR_TABLE)
  12824. + il_tab[i] = w;
  12825. +#elif defined(FOUR_LR_TABLES)
  12826. + il_tab[0][i] = w;
  12827. + il_tab[1][i] = upr(w,1);
  12828. + il_tab[2][i] = upr(w,2);
  12829. + il_tab[3][i] = upr(w,3);
  12830. +#endif
  12831. + w = bytes2word(FFmul0e(b), FFmul09(b), FFmul0d(b), FFmul0b(b));
  12832. +#if defined(ONE_TABLE)
  12833. + it_tab[i] = w;
  12834. +#elif defined(FOUR_TABLES)
  12835. + it_tab[0][i] = w;
  12836. + it_tab[1][i] = upr(w,1);
  12837. + it_tab[2][i] = upr(w,2);
  12838. + it_tab[3][i] = upr(w,3);
  12839. +#endif
  12840. +#if defined(ONE_IM_TABLE)
  12841. + im_tab[b] = w;
  12842. +#elif defined(FOUR_IM_TABLES)
  12843. + im_tab[0][b] = w;
  12844. + im_tab[1][b] = upr(w,1);
  12845. + im_tab[2][b] = upr(w,2);
  12846. + im_tab[3][b] = upr(w,3);
  12847. +#endif
  12848. +
  12849. + }
  12850. +}
  12851. +
  12852. +#endif
  12853. +
  12854. +#define no_table(x,box,vf,rf,c) bytes2word( \
  12855. + box[bval(vf(x,0,c),rf(0,c))], \
  12856. + box[bval(vf(x,1,c),rf(1,c))], \
  12857. + box[bval(vf(x,2,c),rf(2,c))], \
  12858. + box[bval(vf(x,3,c),rf(3,c))])
  12859. +
  12860. +#define one_table(x,op,tab,vf,rf,c) \
  12861. + ( tab[bval(vf(x,0,c),rf(0,c))] \
  12862. + ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
  12863. + ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
  12864. + ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
  12865. +
  12866. +#define four_tables(x,tab,vf,rf,c) \
  12867. + ( tab[0][bval(vf(x,0,c),rf(0,c))] \
  12868. + ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
  12869. + ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
  12870. + ^ tab[3][bval(vf(x,3,c),rf(3,c))])
  12871. +
  12872. +#define vf1(x,r,c) (x)
  12873. +#define rf1(r,c) (r)
  12874. +#define rf2(r,c) ((r-c)&3)
  12875. +
  12876. +#if defined(FOUR_LR_TABLES)
  12877. +#define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
  12878. +#elif defined(ONE_LR_TABLE)
  12879. +#define ls_box(x,c) one_table(x,upr,fl_tab,vf1,rf2,c)
  12880. +#else
  12881. +#define ls_box(x,c) no_table(x,s_box,vf1,rf2,c)
  12882. +#endif
  12883. +
  12884. +#if defined(FOUR_IM_TABLES)
  12885. +#define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
  12886. +#elif defined(ONE_IM_TABLE)
  12887. +#define inv_mcol(x) one_table(x,upr,im_tab,vf1,rf1,0)
  12888. +#else
  12889. +#define inv_mcol(x) \
  12890. + (f9 = (x),f2 = FFmulX(f9), f4 = FFmulX(f2), f8 = FFmulX(f4), f9 ^= f8, \
  12891. + f2 ^= f4 ^ f8 ^ upr(f2 ^ f9,3) ^ upr(f4 ^ f9,2) ^ upr(f9,1))
  12892. +#endif
  12893. +
  12894. +// Subroutine to set the block size (if variable) in bytes, legal
  12895. +// values being 16, 24 and 32.
  12896. +
  12897. +#if defined(AES_BLOCK_SIZE)
  12898. +#define nc (AES_BLOCK_SIZE / 4)
  12899. +#else
  12900. +#define nc (cx->aes_Ncol)
  12901. +
  12902. +void aes_set_blk(aes_context *cx, int n_bytes)
  12903. +{
  12904. +#if !defined(FIXED_TABLES)
  12905. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  12906. +#endif
  12907. +
  12908. + switch(n_bytes) {
  12909. + case 32: /* bytes */
  12910. + case 256: /* bits */
  12911. + nc = 8;
  12912. + break;
  12913. + case 24: /* bytes */
  12914. + case 192: /* bits */
  12915. + nc = 6;
  12916. + break;
  12917. + case 16: /* bytes */
  12918. + case 128: /* bits */
  12919. + default:
  12920. + nc = 4;
  12921. + break;
  12922. + }
  12923. +}
  12924. +
  12925. +#endif
  12926. +
  12927. +// Initialise the key schedule from the user supplied key. The key
  12928. +// length is now specified in bytes - 16, 24 or 32 as appropriate.
  12929. +// This corresponds to bit lengths of 128, 192 and 256 bits, and
  12930. +// to Nk values of 4, 6 and 8 respectively.
  12931. +
  12932. +#define mx(t,f) (*t++ = inv_mcol(*f),f++)
  12933. +#define cp(t,f) *t++ = *f++
  12934. +
  12935. +#if AES_BLOCK_SIZE == 16
  12936. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  12937. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  12938. +#elif AES_BLOCK_SIZE == 24
  12939. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  12940. + cp(d,s); cp(d,s)
  12941. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  12942. + mx(d,s); mx(d,s)
  12943. +#elif AES_BLOCK_SIZE == 32
  12944. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  12945. + cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  12946. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  12947. + mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  12948. +#else
  12949. +
  12950. +#define cpy(d,s) \
  12951. +switch(nc) \
  12952. +{ case 8: cp(d,s); cp(d,s); \
  12953. + case 6: cp(d,s); cp(d,s); \
  12954. + case 4: cp(d,s); cp(d,s); \
  12955. + cp(d,s); cp(d,s); \
  12956. +}
  12957. +
  12958. +#define mix(d,s) \
  12959. +switch(nc) \
  12960. +{ case 8: mx(d,s); mx(d,s); \
  12961. + case 6: mx(d,s); mx(d,s); \
  12962. + case 4: mx(d,s); mx(d,s); \
  12963. + mx(d,s); mx(d,s); \
  12964. +}
  12965. +
  12966. +#endif
  12967. +
  12968. +void aes_set_key(aes_context *cx, const unsigned char in_key[], int n_bytes, const int f)
  12969. +{ u_int32_t *kf, *kt, rci;
  12970. +
  12971. +#if !defined(FIXED_TABLES)
  12972. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  12973. +#endif
  12974. +
  12975. + switch(n_bytes) {
  12976. + case 32: /* bytes */
  12977. + case 256: /* bits */
  12978. + cx->aes_Nkey = 8;
  12979. + break;
  12980. + case 24: /* bytes */
  12981. + case 192: /* bits */
  12982. + cx->aes_Nkey = 6;
  12983. + break;
  12984. + case 16: /* bytes */
  12985. + case 128: /* bits */
  12986. + default:
  12987. + cx->aes_Nkey = 4;
  12988. + break;
  12989. + }
  12990. +
  12991. + cx->aes_Nrnd = (cx->aes_Nkey > nc ? cx->aes_Nkey : nc) + 6;
  12992. +
  12993. + cx->aes_e_key[0] = const_word_in(in_key );
  12994. + cx->aes_e_key[1] = const_word_in(in_key + 4);
  12995. + cx->aes_e_key[2] = const_word_in(in_key + 8);
  12996. + cx->aes_e_key[3] = const_word_in(in_key + 12);
  12997. +
  12998. + kf = cx->aes_e_key;
  12999. + kt = kf + nc * (cx->aes_Nrnd + 1) - cx->aes_Nkey;
  13000. + rci = 0;
  13001. +
  13002. + switch(cx->aes_Nkey)
  13003. + {
  13004. + case 4: do
  13005. + { kf[4] = kf[0] ^ ls_box(kf[3],3) ^ rcon_tab[rci++];
  13006. + kf[5] = kf[1] ^ kf[4];
  13007. + kf[6] = kf[2] ^ kf[5];
  13008. + kf[7] = kf[3] ^ kf[6];
  13009. + kf += 4;
  13010. + }
  13011. + while(kf < kt);
  13012. + break;
  13013. +
  13014. + case 6: cx->aes_e_key[4] = const_word_in(in_key + 16);
  13015. + cx->aes_e_key[5] = const_word_in(in_key + 20);
  13016. + do
  13017. + { kf[ 6] = kf[0] ^ ls_box(kf[5],3) ^ rcon_tab[rci++];
  13018. + kf[ 7] = kf[1] ^ kf[ 6];
  13019. + kf[ 8] = kf[2] ^ kf[ 7];
  13020. + kf[ 9] = kf[3] ^ kf[ 8];
  13021. + kf[10] = kf[4] ^ kf[ 9];
  13022. + kf[11] = kf[5] ^ kf[10];
  13023. + kf += 6;
  13024. + }
  13025. + while(kf < kt);
  13026. + break;
  13027. +
  13028. + case 8: cx->aes_e_key[4] = const_word_in(in_key + 16);
  13029. + cx->aes_e_key[5] = const_word_in(in_key + 20);
  13030. + cx->aes_e_key[6] = const_word_in(in_key + 24);
  13031. + cx->aes_e_key[7] = const_word_in(in_key + 28);
  13032. + do
  13033. + { kf[ 8] = kf[0] ^ ls_box(kf[7],3) ^ rcon_tab[rci++];
  13034. + kf[ 9] = kf[1] ^ kf[ 8];
  13035. + kf[10] = kf[2] ^ kf[ 9];
  13036. + kf[11] = kf[3] ^ kf[10];
  13037. + kf[12] = kf[4] ^ ls_box(kf[11],0);
  13038. + kf[13] = kf[5] ^ kf[12];
  13039. + kf[14] = kf[6] ^ kf[13];
  13040. + kf[15] = kf[7] ^ kf[14];
  13041. + kf += 8;
  13042. + }
  13043. + while (kf < kt);
  13044. + break;
  13045. + }
  13046. +
  13047. + if(!f)
  13048. + { u_int32_t i;
  13049. +
  13050. + kt = cx->aes_d_key + nc * cx->aes_Nrnd;
  13051. + kf = cx->aes_e_key;
  13052. +
  13053. + cpy(kt, kf); kt -= 2 * nc;
  13054. +
  13055. + for(i = 1; i < cx->aes_Nrnd; ++i)
  13056. + {
  13057. +#if defined(ONE_TABLE) || defined(FOUR_TABLES)
  13058. +#if !defined(ONE_IM_TABLE) && !defined(FOUR_IM_TABLES)
  13059. + u_int32_t f2, f4, f8, f9;
  13060. +#endif
  13061. + mix(kt, kf);
  13062. +#else
  13063. + cpy(kt, kf);
  13064. +#endif
  13065. + kt -= 2 * nc;
  13066. + }
  13067. +
  13068. + cpy(kt, kf);
  13069. + }
  13070. +}
  13071. +
  13072. +// y = output word, x = input word, r = row, c = column
  13073. +// for r = 0, 1, 2 and 3 = column accessed for row r
  13074. +
  13075. +#if defined(ARRAYS)
  13076. +#define s(x,c) x[c]
  13077. +#else
  13078. +#define s(x,c) x##c
  13079. +#endif
  13080. +
  13081. +// I am grateful to Frank Yellin for the following constructions
  13082. +// which, given the column (c) of the output state variable that
  13083. +// is being computed, return the input state variables which are
  13084. +// needed for each row (r) of the state
  13085. +
  13086. +// For the fixed block size options, compilers reduce these two
  13087. +// expressions to fixed variable references. For variable block
  13088. +// size code conditional clauses will sometimes be returned
  13089. +
  13090. +#define unused 77 // Sunset Strip
  13091. +
  13092. +#define fwd_var(x,r,c) \
  13093. + ( r==0 ? \
  13094. + ( c==0 ? s(x,0) \
  13095. + : c==1 ? s(x,1) \
  13096. + : c==2 ? s(x,2) \
  13097. + : c==3 ? s(x,3) \
  13098. + : c==4 ? s(x,4) \
  13099. + : c==5 ? s(x,5) \
  13100. + : c==6 ? s(x,6) \
  13101. + : s(x,7)) \
  13102. + : r==1 ? \
  13103. + ( c==0 ? s(x,1) \
  13104. + : c==1 ? s(x,2) \
  13105. + : c==2 ? s(x,3) \
  13106. + : c==3 ? nc==4 ? s(x,0) : s(x,4) \
  13107. + : c==4 ? s(x,5) \
  13108. + : c==5 ? nc==8 ? s(x,6) : s(x,0) \
  13109. + : c==6 ? s(x,7) \
  13110. + : s(x,0)) \
  13111. + : r==2 ? \
  13112. + ( c==0 ? nc==8 ? s(x,3) : s(x,2) \
  13113. + : c==1 ? nc==8 ? s(x,4) : s(x,3) \
  13114. + : c==2 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  13115. + : c==3 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  13116. + : c==4 ? nc==8 ? s(x,7) : s(x,0) \
  13117. + : c==5 ? nc==8 ? s(x,0) : s(x,1) \
  13118. + : c==6 ? s(x,1) \
  13119. + : s(x,2)) \
  13120. + : \
  13121. + ( c==0 ? nc==8 ? s(x,4) : s(x,3) \
  13122. + : c==1 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  13123. + : c==2 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  13124. + : c==3 ? nc==4 ? s(x,2) : nc==8 ? s(x,7) : s(x,0) \
  13125. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  13126. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  13127. + : c==6 ? s(x,2) \
  13128. + : s(x,3)))
  13129. +
  13130. +#define inv_var(x,r,c) \
  13131. + ( r==0 ? \
  13132. + ( c==0 ? s(x,0) \
  13133. + : c==1 ? s(x,1) \
  13134. + : c==2 ? s(x,2) \
  13135. + : c==3 ? s(x,3) \
  13136. + : c==4 ? s(x,4) \
  13137. + : c==5 ? s(x,5) \
  13138. + : c==6 ? s(x,6) \
  13139. + : s(x,7)) \
  13140. + : r==1 ? \
  13141. + ( c==0 ? nc==4 ? s(x,3) : nc==8 ? s(x,7) : s(x,5) \
  13142. + : c==1 ? s(x,0) \
  13143. + : c==2 ? s(x,1) \
  13144. + : c==3 ? s(x,2) \
  13145. + : c==4 ? s(x,3) \
  13146. + : c==5 ? s(x,4) \
  13147. + : c==6 ? s(x,5) \
  13148. + : s(x,6)) \
  13149. + : r==2 ? \
  13150. + ( c==0 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  13151. + : c==1 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  13152. + : c==2 ? nc==8 ? s(x,7) : s(x,0) \
  13153. + : c==3 ? nc==8 ? s(x,0) : s(x,1) \
  13154. + : c==4 ? nc==8 ? s(x,1) : s(x,2) \
  13155. + : c==5 ? nc==8 ? s(x,2) : s(x,3) \
  13156. + : c==6 ? s(x,3) \
  13157. + : s(x,4)) \
  13158. + : \
  13159. + ( c==0 ? nc==4 ? s(x,1) : nc==8 ? s(x,4) : s(x,3) \
  13160. + : c==1 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  13161. + : c==2 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  13162. + : c==3 ? nc==8 ? s(x,7) : s(x,0) \
  13163. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  13164. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  13165. + : c==6 ? s(x,2) \
  13166. + : s(x,3)))
  13167. +
  13168. +#define si(y,x,k,c) s(y,c) = const_word_in(x + 4 * c) ^ k[c]
  13169. +#define so(y,x,c) word_out(y + 4 * c, s(x,c))
  13170. +
  13171. +#if defined(FOUR_TABLES)
  13172. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,ft_tab,fwd_var,rf1,c)
  13173. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,it_tab,inv_var,rf1,c)
  13174. +#elif defined(ONE_TABLE)
  13175. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,ft_tab,fwd_var,rf1,c)
  13176. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,it_tab,inv_var,rf1,c)
  13177. +#else
  13178. +#define fwd_rnd(y,x,k,c) s(y,c) = fwd_mcol(no_table(x,s_box,fwd_var,rf1,c)) ^ (k)[c]
  13179. +#define inv_rnd(y,x,k,c) s(y,c) = inv_mcol(no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c])
  13180. +#endif
  13181. +
  13182. +#if defined(FOUR_LR_TABLES)
  13183. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,fl_tab,fwd_var,rf1,c)
  13184. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,il_tab,inv_var,rf1,c)
  13185. +#elif defined(ONE_LR_TABLE)
  13186. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,fl_tab,fwd_var,rf1,c)
  13187. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,il_tab,inv_var,rf1,c)
  13188. +#else
  13189. +#define fwd_lrnd(y,x,k,c) s(y,c) = no_table(x,s_box,fwd_var,rf1,c) ^ (k)[c]
  13190. +#define inv_lrnd(y,x,k,c) s(y,c) = no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c]
  13191. +#endif
  13192. +
  13193. +#if AES_BLOCK_SIZE == 16
  13194. +
  13195. +#if defined(ARRAYS)
  13196. +#define locals(y,x) x[4],y[4]
  13197. +#else
  13198. +#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
  13199. +// the following defines prevent the compiler requiring the declaration
  13200. +// of generated but unused variables in the fwd_var and inv_var macros
  13201. +#define b04 unused
  13202. +#define b05 unused
  13203. +#define b06 unused
  13204. +#define b07 unused
  13205. +#define b14 unused
  13206. +#define b15 unused
  13207. +#define b16 unused
  13208. +#define b17 unused
  13209. +#endif
  13210. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13211. + s(y,2) = s(x,2); s(y,3) = s(x,3);
  13212. +#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)
  13213. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
  13214. +#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)
  13215. +
  13216. +#elif AES_BLOCK_SIZE == 24
  13217. +
  13218. +#if defined(ARRAYS)
  13219. +#define locals(y,x) x[6],y[6]
  13220. +#else
  13221. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5, \
  13222. + y##0,y##1,y##2,y##3,y##4,y##5
  13223. +#define b06 unused
  13224. +#define b07 unused
  13225. +#define b16 unused
  13226. +#define b17 unused
  13227. +#endif
  13228. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13229. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  13230. + s(y,4) = s(x,4); s(y,5) = s(x,5);
  13231. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); \
  13232. + si(y,x,k,3); si(y,x,k,4); si(y,x,k,5)
  13233. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); \
  13234. + so(y,x,3); so(y,x,4); so(y,x,5)
  13235. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); \
  13236. + rm(y,x,k,3); rm(y,x,k,4); rm(y,x,k,5)
  13237. +#else
  13238. +
  13239. +#if defined(ARRAYS)
  13240. +#define locals(y,x) x[8],y[8]
  13241. +#else
  13242. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5,x##6,x##7, \
  13243. + y##0,y##1,y##2,y##3,y##4,y##5,y##6,y##7
  13244. +#endif
  13245. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13246. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  13247. + s(y,4) = s(x,4); s(y,5) = s(x,5); \
  13248. + s(y,6) = s(x,6); s(y,7) = s(x,7);
  13249. +
  13250. +#if AES_BLOCK_SIZE == 32
  13251. +
  13252. +#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); \
  13253. + si(y,x,k,4); si(y,x,k,5); si(y,x,k,6); si(y,x,k,7)
  13254. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3); \
  13255. + so(y,x,4); so(y,x,5); so(y,x,6); so(y,x,7)
  13256. +#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); \
  13257. + rm(y,x,k,4); rm(y,x,k,5); rm(y,x,k,6); rm(y,x,k,7)
  13258. +#else
  13259. +
  13260. +#define state_in(y,x,k) \
  13261. +switch(nc) \
  13262. +{ case 8: si(y,x,k,7); si(y,x,k,6); \
  13263. + case 6: si(y,x,k,5); si(y,x,k,4); \
  13264. + case 4: si(y,x,k,3); si(y,x,k,2); \
  13265. + si(y,x,k,1); si(y,x,k,0); \
  13266. +}
  13267. +
  13268. +#define state_out(y,x) \
  13269. +switch(nc) \
  13270. +{ case 8: so(y,x,7); so(y,x,6); \
  13271. + case 6: so(y,x,5); so(y,x,4); \
  13272. + case 4: so(y,x,3); so(y,x,2); \
  13273. + so(y,x,1); so(y,x,0); \
  13274. +}
  13275. +
  13276. +#if defined(FAST_VARIABLE)
  13277. +
  13278. +#define round(rm,y,x,k) \
  13279. +switch(nc) \
  13280. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  13281. + rm(y,x,k,5); rm(y,x,k,4); \
  13282. + rm(y,x,k,3); rm(y,x,k,2); \
  13283. + rm(y,x,k,1); rm(y,x,k,0); \
  13284. + break; \
  13285. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  13286. + rm(y,x,k,3); rm(y,x,k,2); \
  13287. + rm(y,x,k,1); rm(y,x,k,0); \
  13288. + break; \
  13289. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  13290. + rm(y,x,k,1); rm(y,x,k,0); \
  13291. + break; \
  13292. +}
  13293. +#else
  13294. +
  13295. +#define round(rm,y,x,k) \
  13296. +switch(nc) \
  13297. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  13298. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  13299. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  13300. + rm(y,x,k,1); rm(y,x,k,0); \
  13301. +}
  13302. +
  13303. +#endif
  13304. +
  13305. +#endif
  13306. +#endif
  13307. +
  13308. +void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  13309. +{ u_int32_t locals(b0, b1);
  13310. + const u_int32_t *kp = cx->aes_e_key;
  13311. +
  13312. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  13313. + u_int32_t f2;
  13314. +#endif
  13315. +
  13316. + state_in(b0, in_blk, kp); kp += nc;
  13317. +
  13318. +#if defined(UNROLL)
  13319. +
  13320. + switch(cx->aes_Nrnd)
  13321. + {
  13322. + case 14: round(fwd_rnd, b1, b0, kp );
  13323. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13324. + case 12: round(fwd_rnd, b1, b0, kp );
  13325. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13326. + case 10: round(fwd_rnd, b1, b0, kp );
  13327. + round(fwd_rnd, b0, b1, kp + nc);
  13328. + round(fwd_rnd, b1, b0, kp + 2 * nc);
  13329. + round(fwd_rnd, b0, b1, kp + 3 * nc);
  13330. + round(fwd_rnd, b1, b0, kp + 4 * nc);
  13331. + round(fwd_rnd, b0, b1, kp + 5 * nc);
  13332. + round(fwd_rnd, b1, b0, kp + 6 * nc);
  13333. + round(fwd_rnd, b0, b1, kp + 7 * nc);
  13334. + round(fwd_rnd, b1, b0, kp + 8 * nc);
  13335. + round(fwd_lrnd, b0, b1, kp + 9 * nc);
  13336. + }
  13337. +
  13338. +#elif defined(PARTIAL_UNROLL)
  13339. + { u_int32_t rnd;
  13340. +
  13341. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  13342. + {
  13343. + round(fwd_rnd, b1, b0, kp);
  13344. + round(fwd_rnd, b0, b1, kp + nc); kp += 2 * nc;
  13345. + }
  13346. +
  13347. + round(fwd_rnd, b1, b0, kp);
  13348. + round(fwd_lrnd, b0, b1, kp + nc);
  13349. + }
  13350. +#else
  13351. + { u_int32_t rnd;
  13352. +
  13353. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  13354. + {
  13355. + round(fwd_rnd, b1, b0, kp);
  13356. + l_copy(b0, b1); kp += nc;
  13357. + }
  13358. +
  13359. + round(fwd_lrnd, b0, b1, kp);
  13360. + }
  13361. +#endif
  13362. +
  13363. + state_out(out_blk, b0);
  13364. +}
  13365. +
  13366. +void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  13367. +{ u_int32_t locals(b0, b1);
  13368. + const u_int32_t *kp = cx->aes_d_key;
  13369. +
  13370. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  13371. + u_int32_t f2, f4, f8, f9;
  13372. +#endif
  13373. +
  13374. + state_in(b0, in_blk, kp); kp += nc;
  13375. +
  13376. +#if defined(UNROLL)
  13377. +
  13378. + switch(cx->aes_Nrnd)
  13379. + {
  13380. + case 14: round(inv_rnd, b1, b0, kp );
  13381. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13382. + case 12: round(inv_rnd, b1, b0, kp );
  13383. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13384. + case 10: round(inv_rnd, b1, b0, kp );
  13385. + round(inv_rnd, b0, b1, kp + nc);
  13386. + round(inv_rnd, b1, b0, kp + 2 * nc);
  13387. + round(inv_rnd, b0, b1, kp + 3 * nc);
  13388. + round(inv_rnd, b1, b0, kp + 4 * nc);
  13389. + round(inv_rnd, b0, b1, kp + 5 * nc);
  13390. + round(inv_rnd, b1, b0, kp + 6 * nc);
  13391. + round(inv_rnd, b0, b1, kp + 7 * nc);
  13392. + round(inv_rnd, b1, b0, kp + 8 * nc);
  13393. + round(inv_lrnd, b0, b1, kp + 9 * nc);
  13394. + }
  13395. +
  13396. +#elif defined(PARTIAL_UNROLL)
  13397. + { u_int32_t rnd;
  13398. +
  13399. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  13400. + {
  13401. + round(inv_rnd, b1, b0, kp);
  13402. + round(inv_rnd, b0, b1, kp + nc); kp += 2 * nc;
  13403. + }
  13404. +
  13405. + round(inv_rnd, b1, b0, kp);
  13406. + round(inv_lrnd, b0, b1, kp + nc);
  13407. + }
  13408. +#else
  13409. + { u_int32_t rnd;
  13410. +
  13411. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  13412. + {
  13413. + round(inv_rnd, b1, b0, kp);
  13414. + l_copy(b0, b1); kp += nc;
  13415. + }
  13416. +
  13417. + round(inv_lrnd, b0, b1, kp);
  13418. + }
  13419. +#endif
  13420. +
  13421. + state_out(out_blk, b0);
  13422. +}
  13423. --- /dev/null Tue Mar 11 13:02:56 2003
  13424. +++ linux/net/ipsec/aes/aes_cbc.c Mon Feb 9 13:51:03 2004
  13425. @@ -0,0 +1,46 @@
  13426. +/*
  13427. +// I retain copyright in this code but I encourage its free use provided
  13428. +// that I don't carry any responsibility for the results. I am especially
  13429. +// happy to see it used in free and open source software. If you do use
  13430. +// it I would appreciate an acknowledgement of its origin in the code or
  13431. +// the product that results and I would also appreciate knowing a little
  13432. +// about the use to which it is being put. I am grateful to Frank Yellin
  13433. +// for some ideas that are used in this implementation.
  13434. +//
  13435. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  13436. +//
  13437. +// This is an implementation of the AES encryption algorithm (Rijndael)
  13438. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  13439. +// to provide both fixed and dynamic block and key lengths and can also
  13440. +// run with either big or little endian internal byte order (see aes.h).
  13441. +// It inputs block and key lengths in bytes with the legal values being
  13442. +// 16, 24 and 32.
  13443. +*
  13444. +*/
  13445. +
  13446. +#ifdef __KERNEL__
  13447. +#include <linux/types.h>
  13448. +#else
  13449. +#include <sys/types.h>
  13450. +#endif
  13451. +#include "crypto/aes_cbc.h"
  13452. +#include "crypto/cbc_generic.h"
  13453. +
  13454. +/* returns bool success */
  13455. +int AES_set_key(aes_context *aes_ctx, const u_int8_t *key, int keysize) {
  13456. + aes_set_key(aes_ctx, key, keysize, 0);
  13457. + return 1;
  13458. +}
  13459. +CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
  13460. +
  13461. +
  13462. +/*
  13463. + * $Log: aes_cbc.c,v $
  13464. + * Revision 1.2 2004/07/10 07:48:40 mcr
  13465. + * Moved from linux/crypto/ciphers/aes/aes_cbc.c,v
  13466. + *
  13467. + * Revision 1.1 2004/04/06 02:48:12 mcr
  13468. + * pullup of AES cipher from alg-branch.
  13469. + *
  13470. + *
  13471. + */
  13472. --- /dev/null Tue Mar 11 13:02:56 2003
  13473. +++ linux/net/ipsec/aes/aes_xcbc_mac.c Mon Feb 9 13:51:03 2004
  13474. @@ -0,0 +1,67 @@
  13475. +#ifdef __KERNEL__
  13476. +#include <linux/types.h>
  13477. +#include <linux/kernel.h>
  13478. +#define DEBUG(x)
  13479. +#else
  13480. +#include <stdio.h>
  13481. +#include <sys/types.h>
  13482. +#define DEBUG(x) x
  13483. +#endif
  13484. +
  13485. +#include "crypto/aes.h"
  13486. +#include "crypto/aes_xcbc_mac.h"
  13487. +
  13488. +int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen)
  13489. +{
  13490. + int ret=1;
  13491. + aes_block kn[3] = {
  13492. + { 0x01010101, 0x01010101, 0x01010101, 0x01010101 },
  13493. + { 0x02020202, 0x02020202, 0x02020202, 0x02020202 },
  13494. + { 0x03030303, 0x03030303, 0x03030303, 0x03030303 },
  13495. + };
  13496. + aes_set_key(&ctxm->ctx_k1, key, keylen, 0);
  13497. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[0], (u_int8_t *) kn[0]);
  13498. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[1], (u_int8_t *) ctxm->k2);
  13499. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[2], (u_int8_t *) ctxm->k3);
  13500. + aes_set_key(&ctxm->ctx_k1, (u_int8_t *) kn[0], 16, 0);
  13501. + return ret;
  13502. +}
  13503. +static void do_pad_xor(u_int8_t *out, const u_int8_t *in, int len) {
  13504. + int pos=0;
  13505. + for (pos=1; pos <= 16; pos++, in++, out++) {
  13506. + if (pos <= len)
  13507. + *out ^= *in;
  13508. + if (pos > len) {
  13509. + DEBUG(printf("put 0x80 at pos=%d\n", pos));
  13510. + *out ^= 0x80;
  13511. + break;
  13512. + }
  13513. + }
  13514. +}
  13515. +static void xor_block(aes_block res, const aes_block op) {
  13516. + res[0] ^= op[0];
  13517. + res[1] ^= op[1];
  13518. + res[2] ^= op[2];
  13519. + res[3] ^= op[3];
  13520. +}
  13521. +int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]) {
  13522. + int ret=ilen;
  13523. + u_int32_t out[4] = { 0, 0, 0, 0 };
  13524. + for (; ilen > 16 ; ilen-=16) {
  13525. + xor_block(out, (const u_int32_t*) &in[0]);
  13526. + aes_encrypt(&ctxm->ctx_k1, in, (u_int8_t *)&out[0]);
  13527. + in+=16;
  13528. + }
  13529. + do_pad_xor((u_int8_t *)&out, in, ilen);
  13530. + if (ilen==16) {
  13531. + DEBUG(printf("using k3\n"));
  13532. + xor_block(out, ctxm->k3);
  13533. + }
  13534. + else
  13535. + {
  13536. + DEBUG(printf("using k2\n"));
  13537. + xor_block(out, ctxm->k2);
  13538. + }
  13539. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *)out, hash);
  13540. + return ret;
  13541. +}
  13542. --- /dev/null Tue Mar 11 13:02:56 2003
  13543. +++ linux/net/ipsec/aes/ipsec_alg_aes.c Mon Feb 9 13:51:03 2004
  13544. @@ -0,0 +1,296 @@
  13545. +/*
  13546. + * ipsec_alg AES cipher stubs
  13547. + *
  13548. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  13549. + *
  13550. + * ipsec_alg_aes.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  13551. + *
  13552. + * This program is free software; you can redistribute it and/or modify it
  13553. + * under the terms of the GNU General Public License as published by the
  13554. + * Free Software Foundation; either version 2 of the License, or (at your
  13555. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  13556. + *
  13557. + * This program is distributed in the hope that it will be useful, but
  13558. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13559. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13560. + * for more details.
  13561. + *
  13562. + * Fixes by:
  13563. + * PK: Pawel Krawczyk <kravietz@aba.krakow.pl>
  13564. + * Fixes list:
  13565. + * PK: make XCBC comply with latest draft (keylength)
  13566. + *
  13567. + */
  13568. +#ifndef AUTOCONF_INCLUDED
  13569. +#include <linux/config.h>
  13570. +#endif
  13571. +#include <linux/version.h>
  13572. +
  13573. +/*
  13574. + * special case: ipsec core modular with this static algo inside:
  13575. + * must avoid MODULE magic for this file
  13576. + */
  13577. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_AES)
  13578. +#undef MODULE
  13579. +#endif
  13580. +
  13581. +#include <linux/module.h>
  13582. +#include <linux/init.h>
  13583. +
  13584. +#include <linux/kernel.h> /* printk() */
  13585. +#include <linux/errno.h> /* error codes */
  13586. +#include <linux/types.h> /* size_t */
  13587. +#include <linux/string.h>
  13588. +
  13589. +/* Check if __exit is defined, if not null it */
  13590. +#ifndef __exit
  13591. +#define __exit
  13592. +#endif
  13593. +
  13594. +/* Low freeswan header coupling */
  13595. +#include "openswan/ipsec_alg.h"
  13596. +#include "crypto/aes_cbc.h"
  13597. +
  13598. +#define CONFIG_KLIPS_ENC_AES_MAC 1
  13599. +
  13600. +#define AES_CONTEXT_T aes_context
  13601. +static int debug_aes=0;
  13602. +static int test_aes=0;
  13603. +static int excl_aes=0;
  13604. +static int keyminbits=0;
  13605. +static int keymaxbits=0;
  13606. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13607. +MODULE_AUTHOR("JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>");
  13608. +#ifdef module_param
  13609. +module_param(debug_aes,int,0600)
  13610. +module_param(test_aes,int,0600)
  13611. +module_param(excl_aes,int,0600)
  13612. +module_param(keyminbits,int,0600)
  13613. +module_param(keymaxbits,int,0600)
  13614. +#else
  13615. +MODULE_PARM(debug_aes, "i");
  13616. +MODULE_PARM(test_aes, "i");
  13617. +MODULE_PARM(excl_aes, "i");
  13618. +MODULE_PARM(keyminbits, "i");
  13619. +MODULE_PARM(keymaxbits, "i");
  13620. +#endif
  13621. +#endif
  13622. +
  13623. +#if CONFIG_KLIPS_ENC_AES_MAC
  13624. +#include "crypto/aes_xcbc_mac.h"
  13625. +
  13626. +/*
  13627. + * Not IANA number yet (draft-ietf-ipsec-ciph-aes-xcbc-mac-00.txt).
  13628. + * We use 9 for non-modular algorithm and none for modular, thus
  13629. + * forcing user to specify one on module load. -kravietz
  13630. + */
  13631. +#ifdef MODULE
  13632. +static int auth_id=0;
  13633. +#else
  13634. +static int auth_id=9;
  13635. +#endif
  13636. +#ifdef module_param
  13637. +module_param(auth_id, int, 0600);
  13638. +#else
  13639. +MODULE_PARM(auth_id, "i");
  13640. +#endif
  13641. +#endif
  13642. +
  13643. +#define ESP_AES 12 /* truely _constant_ :) */
  13644. +
  13645. +/* 128, 192 or 256 */
  13646. +#define ESP_AES_KEY_SZ_MIN 16 /* 128 bit secret key */
  13647. +#define ESP_AES_KEY_SZ_MAX 32 /* 256 bit secret key */
  13648. +#define ESP_AES_CBC_BLK_LEN 16 /* AES-CBC block size */
  13649. +
  13650. +/* Values according to draft-ietf-ipsec-ciph-aes-xcbc-mac-02.txt
  13651. + * -kravietz
  13652. + */
  13653. +#define ESP_AES_MAC_KEY_SZ 16 /* 128 bit MAC key */
  13654. +#define ESP_AES_MAC_BLK_LEN 16 /* 128 bit block */
  13655. +
  13656. +static int _aes_set_key(struct ipsec_alg_enc *alg,
  13657. + __u8 * key_e, const __u8 * key,
  13658. + size_t keysize)
  13659. +{
  13660. + int ret;
  13661. + AES_CONTEXT_T *ctx=(AES_CONTEXT_T*)key_e;
  13662. + ret=AES_set_key(ctx, key, keysize)!=0? 0: -EINVAL;
  13663. + if (debug_aes > 0)
  13664. + printk(KERN_DEBUG "klips_debug:_aes_set_key:"
  13665. + "ret=%d key_e=%p key=%p keysize=%ld\n",
  13666. + ret, key_e, key, (unsigned long int) keysize);
  13667. + return ret;
  13668. +}
  13669. +
  13670. +static int _aes_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e,
  13671. + __u8 * in, int ilen, const __u8 * iv,
  13672. + int encrypt)
  13673. +{
  13674. + AES_CONTEXT_T *ctx=(AES_CONTEXT_T*)key_e;
  13675. + if (debug_aes > 0)
  13676. + printk(KERN_DEBUG "klips_debug:_aes_cbc_encrypt:"
  13677. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  13678. + key_e, in, ilen, iv, encrypt);
  13679. + return AES_cbc_encrypt(ctx, in, in, ilen, iv, encrypt);
  13680. +}
  13681. +#if CONFIG_KLIPS_ENC_AES_MAC
  13682. +static int _aes_mac_set_key(struct ipsec_alg_auth *alg, __u8 * key_a, const __u8 * key, int keylen) {
  13683. + aes_context_mac *ctxm=(aes_context_mac *)key_a;
  13684. + return AES_xcbc_mac_set_key(ctxm, key, keylen)? 0 : -EINVAL;
  13685. +}
  13686. +static int _aes_mac_hash(struct ipsec_alg_auth *alg, __u8 * key_a, const __u8 * dat, int len, __u8 * hash, int hashlen) {
  13687. + int ret;
  13688. + char hash_buf[16];
  13689. + aes_context_mac *ctxm=(aes_context_mac *)key_a;
  13690. + ret=AES_xcbc_mac_hash(ctxm, dat, len, hash_buf);
  13691. + memcpy(hash, hash_buf, hashlen);
  13692. + return ret;
  13693. +}
  13694. +static struct ipsec_alg_auth ipsec_alg_AES_MAC = {
  13695. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  13696. + ixt_refcnt: ATOMIC_INIT(0),
  13697. + ixt_name: "aes_mac",
  13698. + ixt_blocksize: ESP_AES_MAC_BLK_LEN,
  13699. + ixt_support: {
  13700. + ias_exttype: IPSEC_ALG_TYPE_AUTH,
  13701. + ias_id: 0,
  13702. + ias_keyminbits: ESP_AES_MAC_KEY_SZ*8,
  13703. + ias_keymaxbits: ESP_AES_MAC_KEY_SZ*8,
  13704. + },
  13705. + },
  13706. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13707. + ixt_module: THIS_MODULE,
  13708. +#endif
  13709. + ixt_a_keylen: ESP_AES_MAC_KEY_SZ,
  13710. + ixt_a_ctx_size: sizeof(aes_context_mac),
  13711. + ixt_a_hmac_set_key: _aes_mac_set_key,
  13712. + ixt_a_hmac_hash:_aes_mac_hash,
  13713. +};
  13714. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13715. +static struct ipsec_alg_enc ipsec_alg_AES = {
  13716. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  13717. + ixt_refcnt: ATOMIC_INIT(0),
  13718. + ixt_name: "aes",
  13719. + ixt_blocksize: ESP_AES_CBC_BLK_LEN,
  13720. + ixt_support: {
  13721. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  13722. + ias_id: ESP_AES,
  13723. + ias_keyminbits: ESP_AES_KEY_SZ_MIN*8,
  13724. + ias_keymaxbits: ESP_AES_KEY_SZ_MAX*8,
  13725. + },
  13726. + },
  13727. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13728. + ixt_module: THIS_MODULE,
  13729. +#endif
  13730. + ixt_e_keylen: ESP_AES_KEY_SZ_MAX,
  13731. + ixt_e_ctx_size: sizeof(AES_CONTEXT_T),
  13732. + ixt_e_set_key: _aes_set_key,
  13733. + ixt_e_cbc_encrypt:_aes_cbc_encrypt,
  13734. +};
  13735. +
  13736. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13737. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_aes_init )
  13738. +#else
  13739. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_aes_init )
  13740. +#endif
  13741. +{
  13742. + int ret, test_ret;
  13743. +
  13744. + if (keyminbits)
  13745. + ipsec_alg_AES.ixt_common.ixt_support.ias_keyminbits=keyminbits;
  13746. + if (keymaxbits) {
  13747. + ipsec_alg_AES.ixt_common.ixt_support.ias_keymaxbits=keymaxbits;
  13748. + if (keymaxbits*8>ipsec_alg_AES.ixt_common.ixt_support.ias_keymaxbits)
  13749. + ipsec_alg_AES.ixt_e_keylen=keymaxbits*8;
  13750. + }
  13751. + if (excl_aes) ipsec_alg_AES.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  13752. + ret=register_ipsec_alg_enc(&ipsec_alg_AES);
  13753. + printk("ipsec_aes_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  13754. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype,
  13755. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13756. + ipsec_alg_AES.ixt_common.ixt_name,
  13757. + ret);
  13758. + if (ret==0 && test_aes) {
  13759. + test_ret=ipsec_alg_test(
  13760. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype ,
  13761. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13762. + test_aes);
  13763. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  13764. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype ,
  13765. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13766. + test_ret);
  13767. + }
  13768. +#if CONFIG_KLIPS_ENC_AES_MAC
  13769. + if (auth_id!=0){
  13770. + int ret;
  13771. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id=auth_id;
  13772. + ret=register_ipsec_alg_auth(&ipsec_alg_AES_MAC);
  13773. + printk("ipsec_aes_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  13774. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13775. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13776. + ipsec_alg_AES_MAC.ixt_common.ixt_name,
  13777. + ret);
  13778. + if (ret==0 && test_aes) {
  13779. + test_ret=ipsec_alg_test(
  13780. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13781. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13782. + test_aes);
  13783. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  13784. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13785. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13786. + test_ret);
  13787. + }
  13788. + } else {
  13789. + printk(KERN_DEBUG "klips_debug: experimental ipsec_alg_AES_MAC not registered [Ok] (auth_id=%d)\n", auth_id);
  13790. + }
  13791. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13792. + return ret;
  13793. +}
  13794. +
  13795. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13796. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_aes_fini )
  13797. +#else
  13798. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_aes_fini )
  13799. +#endif
  13800. +{
  13801. +#if CONFIG_KLIPS_ENC_AES_MAC
  13802. + if (auth_id) unregister_ipsec_alg_auth(&ipsec_alg_AES_MAC);
  13803. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13804. + unregister_ipsec_alg_enc(&ipsec_alg_AES);
  13805. + return;
  13806. +}
  13807. +#ifdef MODULE_LICENSE
  13808. +MODULE_LICENSE("GPL");
  13809. +#endif
  13810. +
  13811. +#if 0 /* +NOT_YET */
  13812. +#ifndef MODULE
  13813. +/*
  13814. + * This is intended for static module setups, currently
  13815. + * doesn't work for modular ipsec.o with static algos inside
  13816. + */
  13817. +static int setup_keybits(const char *str)
  13818. +{
  13819. + unsigned aux;
  13820. + char *end;
  13821. +
  13822. + aux = simple_strtoul(str,&end,0);
  13823. + if (aux != 128 && aux != 192 && aux != 256)
  13824. + return 0;
  13825. + keyminbits = aux;
  13826. +
  13827. + if (*end == 0 || *end != ',')
  13828. + return 1;
  13829. + str=end+1;
  13830. + aux = simple_strtoul(str, NULL, 0);
  13831. + if (aux != 128 && aux != 192 && aux != 256)
  13832. + return 0;
  13833. + if (aux >= keyminbits)
  13834. + keymaxbits = aux;
  13835. + return 1;
  13836. +}
  13837. +__setup("ipsec_aes_keybits=", setup_keybits);
  13838. +#endif
  13839. +#endif
  13840. +
  13841. --- /dev/null Tue Mar 11 13:02:56 2003
  13842. +++ linux/net/ipsec/alg/Config.alg_aes.in Mon Feb 9 13:51:03 2004
  13843. @@ -0,0 +1,3 @@
  13844. +if [ "$CONFIG_IPSEC_ALG" = "y" ]; then
  13845. + tristate ' AES encryption algorithm' CONFIG_IPSEC_ENC_AES
  13846. +fi
  13847. --- /dev/null Tue Mar 11 13:02:56 2003
  13848. +++ linux/net/ipsec/alg/Config.alg_cryptoapi.in Mon Feb 9 13:51:03 2004
  13849. @@ -0,0 +1,6 @@
  13850. +if [ "$CONFIG_IPSEC_ALG" = "y" ]; then
  13851. + dep_tristate ' CRYPTOAPI ciphers support (needs cryptoapi patch)' CONFIG_IPSEC_ALG_CRYPTOAPI $CONFIG_CRYPTO
  13852. + if [ "$CONFIG_IPSEC_ALG_CRYPTOAPI" != "n" ]; then
  13853. + bool ' CRYPTOAPI proprietary ciphers ' CONFIG_IPSEC_ALG_NON_LIBRE
  13854. + fi
  13855. +fi
  13856. --- /dev/null Tue Mar 11 13:02:56 2003
  13857. +++ linux/net/ipsec/alg/Config.in Mon Feb 9 13:51:03 2004
  13858. @@ -0,0 +1,3 @@
  13859. +#Placeholder
  13860. +source net/ipsec/alg/Config.alg_aes.in
  13861. +source net/ipsec/alg/Config.alg_cryptoapi.in
  13862. --- /dev/null Tue Mar 11 13:02:56 2003
  13863. +++ linux/net/ipsec/alg/Makefile Mon Feb 9 13:51:03 2004
  13864. @@ -0,0 +1,112 @@
  13865. +# Makefile,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  13866. +ifeq ($(strip $(KLIPSMODULE)),)
  13867. +FREESWANSRCDIR=.
  13868. +else
  13869. +FREESWANSRCDIR=../../../..
  13870. +endif
  13871. +ifeq ($(strip $(KLIPS_TOP)),)
  13872. +KLIPS_TOP=../../..
  13873. +override EXTRA_CFLAGS += -I$(KLIPS_TOP)/include
  13874. +endif
  13875. +
  13876. +ifeq ($(CONFIG_IPSEC_DEBUG),y)
  13877. +override EXTRA_CFLAGS += -g
  13878. +endif
  13879. +
  13880. +# LIBCRYPTO normally comes as an argument from "parent" Makefile
  13881. +# (this applies both to FS' "make module" and eg. Linux' "make modules"
  13882. +# But make dep doest follow same evaluations, so we need this default:
  13883. +LIBCRYPTO=$(TOPDIR)/lib/libcrypto
  13884. +
  13885. +override EXTRA_CFLAGS += -I$(LIBCRYPTO)/include
  13886. +override EXTRA_CFLAGS += -Wall -Wpointer-arith -Wstrict-prototypes
  13887. +
  13888. +MOD_LIST_NAME := NET_MISC_MODULES
  13889. +
  13890. +#O_TARGET := static_init.o
  13891. +
  13892. +subdir- :=
  13893. +subdir-n :=
  13894. +subdir-y :=
  13895. +subdir-m :=
  13896. +
  13897. +obj-y := static_init.o
  13898. +
  13899. +ARCH_ASM-y :=
  13900. +ARCH_ASM-$(CONFIG_M586) := i586
  13901. +ARCH_ASM-$(CONFIG_M586TSC) := i586
  13902. +ARCH_ASM-$(CONFIG_M586MMX) := i586
  13903. +ARCH_ASM-$(CONFIG_MK6) := i586
  13904. +ARCH_ASM-$(CONFIG_M686) := i686
  13905. +ARCH_ASM-$(CONFIG_MPENTIUMIII) := i686
  13906. +ARCH_ASM-$(CONFIG_MPENTIUM4) := i686
  13907. +ARCH_ASM-$(CONFIG_MK7) := i686
  13908. +ARCH_ASM-$(CONFIG_MCRUSOE) := i586
  13909. +ARCH_ASM-$(CONFIG_MWINCHIPC6) := i586
  13910. +ARCH_ASM-$(CONFIG_MWINCHIP2) := i586
  13911. +ARCH_ASM-$(CONFIG_MWINCHIP3D) := i586
  13912. +ARCH_ASM-$(CONFIG_USERMODE) := i586
  13913. +
  13914. +ARCH_ASM :=$(ARCH_ASM-y)
  13915. +ifdef NO_ASM
  13916. +ARCH_ASM :=
  13917. +endif
  13918. +
  13919. +# The algorithm makefiles may put dependences, short-circuit them
  13920. +null:
  13921. +
  13922. +makefiles=$(filter-out %.preipsec, $(wildcard Makefile.alg_*))
  13923. +ifneq ($(makefiles),)
  13924. +#include Makefile.alg_aes
  13925. +#include Makefile.alg_aes-opt
  13926. +include $(makefiles)
  13927. +endif
  13928. +
  13929. +# These rules translate from new to old makefile rules
  13930. +# Translate to Rules.make lists.
  13931. +multi-used := $(filter $(list-multi), $(obj-y) $(obj-m))
  13932. +multi-objs := $(foreach m, $(multi-used), $($(basename $(m))-objs))
  13933. +active-objs := $(sort $(multi-objs) $(obj-y) $(obj-m))
  13934. +O_OBJS := $(obj-y)
  13935. +M_OBJS := $(obj-m)
  13936. +MIX_OBJS := $(filter $(export-objs), $(active-objs))
  13937. +#OX_OBJS := $(export-objs)
  13938. +SUB_DIRS := $(subdir-y)
  13939. +ALL_SUB_DIRS := $(subdir-y) $(subdir-m)
  13940. +MOD_SUB_DIRS := $(subdir-m)
  13941. +
  13942. +
  13943. +static_init_mod.o: $(obj-y)
  13944. + rm -f $@
  13945. + $(LD) $(LD_EXTRAFLAGS) $(obj-y) -r -o $@
  13946. +
  13947. +perlasm: ../../../crypto/ciphers/des/asm/perlasm
  13948. + ln -sf $? $@
  13949. +
  13950. +$(obj-y) $(obj-m): $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h $(KLIPS_TOP)/include/freeswan/ipsec_alg.h
  13951. +$(alg_obj-y) $(alg_obj-m): perlasm $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h $(KLIPS_TOP)/include/freeswan/ipsec_alg.h
  13952. +
  13953. +
  13954. +all_alg_modules: perlasm $(ALG_MODULES)
  13955. + @echo "ALG_MODULES=$(ALG_MODULES)"
  13956. +
  13957. +
  13958. +#
  13959. +# Construct alg. init. function: call ipsec_ALGO_init() for every static algo
  13960. +# Needed when there are static algos (with static or modular ipsec.o)
  13961. +#
  13962. +static_init.c: $(TOPDIR)/include/linux/autoconf.h Makefile $(makefiles) scripts/mk-static_init.c.sh
  13963. + @echo "Re-creating $@"
  13964. + $(SHELL) scripts/mk-static_init.c.sh $(static_init-func-y) > $@
  13965. +
  13966. +clean:
  13967. + @for i in $(ALG_SUBDIRS);do test -d $$i && make -C $$i clean;done;exit 0
  13968. + @find . -type l -exec rm -f {} \;
  13969. + -rm -f perlasm
  13970. + -rm -rf $(ALG_SUBDIRS)
  13971. + -rm -f *.o static_init.c
  13972. +
  13973. +ifdef TOPDIR
  13974. +include $(TOPDIR)/Rules.make
  13975. +endif
  13976. +
  13977. --- /dev/null Tue Mar 11 13:02:56 2003
  13978. +++ linux/net/ipsec/alg/Makefile.alg_aes Mon Feb 9 13:51:03 2004
  13979. @@ -0,0 +1,18 @@
  13980. +MOD_AES := ipsec_aes.o
  13981. +
  13982. +ALG_MODULES += $(MOD_AES)
  13983. +ALG_SUBDIRS += libaes
  13984. +
  13985. +obj-$(CONFIG_IPSEC_ALG_AES) += $(MOD_AES)
  13986. +static_init-func-$(CONFIG_IPSEC_ALG_AES)+= ipsec_aes_init
  13987. +alg_obj-$(CONFIG_IPSEC_ALG_AES) += ipsec_alg_aes.o
  13988. +
  13989. +AES_OBJS := ipsec_alg_aes.o $(LIBCRYPTO)/libaes/libaes.a
  13990. +
  13991. +
  13992. +$(MOD_AES): $(AES_OBJS)
  13993. + $(LD) $(EXTRA_LDFLAGS) -r $(AES_OBJS) -o $@
  13994. +
  13995. +$(LIBCRYPTO)/libaes/libaes.a:
  13996. + $(MAKE) -C $(LIBCRYPTO)/libaes CC='$(CC)' 'ARCH_ASM=$(ARCH_ASM)' CFLAGS='$(CFLAGS) $(EXTRA_CFLAGS)' libaes.a
  13997. +
  13998. --- /dev/null Tue Mar 11 13:02:56 2003
  13999. +++ linux/net/ipsec/alg/Makefile.alg_cryptoapi Mon Feb 9 13:51:03 2004
  14000. @@ -0,0 +1,14 @@
  14001. +MOD_CRYPTOAPI := ipsec_cryptoapi.o
  14002. +
  14003. +ifneq ($(wildcard $(TOPDIR)/include/linux/crypto.h),)
  14004. +ALG_MODULES += $(MOD_CRYPTOAPI)
  14005. +obj-$(CONFIG_IPSEC_ALG_CRYPTOAPI) += $(MOD_CRYPTOAPI)
  14006. +static_init-func-$(CONFIG_IPSEC_ALG_CRYPTOAPI)+= ipsec_cryptoapi_init
  14007. +alg_obj-$(CONFIG_IPSEC_ALG_CRYPTOAPI) += ipsec_alg_cryptoapi.o
  14008. +else
  14009. +$(warning "Linux CryptoAPI (2.4.22+ or 2.6.x) not found, not building ipsec_cryptoapi.o")
  14010. +endif
  14011. +
  14012. +CRYPTOAPI_OBJS := ipsec_alg_cryptoapi.o
  14013. +$(MOD_CRYPTOAPI): $(CRYPTOAPI_OBJS)
  14014. + $(LD) -r $(CRYPTOAPI_OBJS) -o $@
  14015. --- /dev/null Tue Mar 11 13:02:56 2003
  14016. +++ linux/net/ipsec/alg/ipsec_alg_cryptoapi.c Mon Feb 9 13:51:03 2004
  14017. @@ -0,0 +1,442 @@
  14018. +/*
  14019. + * ipsec_alg to linux cryptoapi GLUE
  14020. + *
  14021. + * Authors: CODE.ar TEAM
  14022. + * Harpo MAxx <harpo@linuxmendoza.org.ar>
  14023. + * JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  14024. + * Luciano Ruete <docemeses@softhome.net>
  14025. + *
  14026. + * ipsec_alg_cryptoapi.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  14027. + *
  14028. + * This program is free software; you can redistribute it and/or modify it
  14029. + * under the terms of the GNU General Public License as published by the
  14030. + * Free Software Foundation; either version 2 of the License, or (at your
  14031. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  14032. + *
  14033. + * This program is distributed in the hope that it will be useful, but
  14034. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14035. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14036. + * for more details.
  14037. + *
  14038. + * Example usage:
  14039. + * modinfo -p ipsec_cryptoapi (quite useful info, including supported algos)
  14040. + * modprobe ipsec_cryptoapi
  14041. + * modprobe ipsec_cryptoapi test=1
  14042. + * modprobe ipsec_cryptoapi excl=1 (exclusive cipher/algo)
  14043. + * modprobe ipsec_cryptoapi noauto=1 aes=1 twofish=1 (only these ciphers)
  14044. + * modprobe ipsec_cryptoapi aes=128,128 (force these keylens)
  14045. + * modprobe ipsec_cryptoapi des_ede3=0 (everything but 3DES)
  14046. + */
  14047. +#ifndef AUTOCONF_INCLUDED
  14048. +#include <linux/config.h>
  14049. +#endif
  14050. +#include <linux/version.h>
  14051. +
  14052. +/*
  14053. + * special case: ipsec core modular with this static algo inside:
  14054. + * must avoid MODULE magic for this file
  14055. + */
  14056. +#if CONFIG_IPSEC_MODULE && CONFIG_IPSEC_ALG_CRYPTOAPI
  14057. +#undef MODULE
  14058. +#endif
  14059. +
  14060. +#include <linux/module.h>
  14061. +#include <linux/init.h>
  14062. +
  14063. +#include <linux/kernel.h> /* printk() */
  14064. +#include <linux/errno.h> /* error codes */
  14065. +#include <linux/types.h> /* size_t */
  14066. +#include <linux/string.h>
  14067. +
  14068. +/* Check if __exit is defined, if not null it */
  14069. +#ifndef __exit
  14070. +#define __exit
  14071. +#endif
  14072. +
  14073. +/* warn the innocent */
  14074. +#if !defined (CONFIG_CRYPTO) && !defined (CONFIG_CRYPTO_MODULE)
  14075. +#warning "No linux CryptoAPI found, install 2.4.22+ or 2.6.x"
  14076. +#define NO_CRYPTOAPI_SUPPORT
  14077. +#endif
  14078. +/* Low freeswan header coupling */
  14079. +#include "openswan/ipsec_alg.h"
  14080. +
  14081. +#include <linux/crypto.h>
  14082. +#ifdef CRYPTO_API_VERSION_CODE
  14083. +#warning "Old CryptoAPI is not supported. Only linux-2.4.22+ or linux-2.6.x are supported"
  14084. +#define NO_CRYPTOAPI_SUPPORT
  14085. +#endif
  14086. +
  14087. +#ifdef NO_CRYPTOAPI_SUPPORT
  14088. +#warning "Building an unusable module :P"
  14089. +/* Catch old CryptoAPI by not allowing module to load */
  14090. +IPSEC_ALG_MODULE_INIT( ipsec_cryptoapi_init )
  14091. +{
  14092. + printk(KERN_WARNING "ipsec_cryptoapi.o was not built on stock Linux CryptoAPI (2.4.22+ or 2.6.x), not loading.\n");
  14093. + return -EINVAL;
  14094. +}
  14095. +#else
  14096. +#include <asm/scatterlist.h>
  14097. +#include <asm/pgtable.h>
  14098. +#include <linux/mm.h>
  14099. +
  14100. +#define CIPHERNAME_AES "aes"
  14101. +#define CIPHERNAME_3DES "des3_ede"
  14102. +#define CIPHERNAME_BLOWFISH "blowfish"
  14103. +#define CIPHERNAME_CAST "cast5"
  14104. +#define CIPHERNAME_SERPENT "serpent"
  14105. +#define CIPHERNAME_TWOFISH "twofish"
  14106. +
  14107. +#define ESP_3DES 3
  14108. +#define ESP_AES 12
  14109. +#define ESP_BLOWFISH 7 /* truely _constant_ :) */
  14110. +#define ESP_CAST 6 /* quite constant :) */
  14111. +#define ESP_SERPENT 252 /* from ipsec drafts */
  14112. +#define ESP_TWOFISH 253 /* from ipsec drafts */
  14113. +
  14114. +#define AH_MD5 2
  14115. +#define AH_SHA 3
  14116. +#define DIGESTNAME_MD5 "md5"
  14117. +#define DIGESTNAME_SHA1 "sha1"
  14118. +
  14119. +MODULE_AUTHOR("Juanjo Ciarlante, Harpo MAxx, Luciano Ruete");
  14120. +static int debug=0;
  14121. +static int test=0;
  14122. +static int excl=0;
  14123. +static int noauto = 0;
  14124. +
  14125. +static int des_ede3[] = {-1, -1};
  14126. +static int aes[] = {-1, -1};
  14127. +static int blowfish[] = {-1, -1};
  14128. +static int cast[] = {-1, -1};
  14129. +static int serpent[] = {-1, -1};
  14130. +static int twofish[] = {-1, -1};
  14131. +
  14132. +#ifdef module_param
  14133. +module_param(debug,int,0600);
  14134. +module_param(test,int,0600);
  14135. +module_param(ebug,int,0600);
  14136. +
  14137. +module_param(noauto,int,0600);
  14138. +module_param(ebug,int,0600);
  14139. +
  14140. +module_param_array(des_ede3,int,NULL,0);
  14141. +module_param(aes,int,NULL,0);
  14142. +module_param(blowfish,int,NULL,0);
  14143. +module_param(cast,int,NULL,0);
  14144. +module_param(serpent,int,NULL,0);
  14145. +module_param(twofish,int,NULL,0);
  14146. +#else
  14147. +MODULE_PARM(debug, "i");
  14148. +MODULE_PARM(test, "i");
  14149. +MODULE_PARM(excl, "i");
  14150. +
  14151. +MODULE_PARM(noauto,"i");
  14152. +
  14153. +MODULE_PARM(des_ede3,"1-2i");
  14154. +MODULE_PARM(aes,"1-2i");
  14155. +MODULE_PARM(blowfish,"1-2i");
  14156. +MODULE_PARM(cast,"1-2i");
  14157. +MODULE_PARM(serpent,"1-2i");
  14158. +MODULE_PARM(twofish,"1-2i");
  14159. +#endif
  14160. +
  14161. +MODULE_PARM_DESC(noauto, "Dont try all known algos, just setup enabled ones");
  14162. +
  14163. +MODULE_PARM_DESC(des_ede3, "0: disable | 1: force_enable | min,max: dontuse");
  14164. +MODULE_PARM_DESC(aes, "0: disable | 1: force_enable | min,max: keybitlens");
  14165. +MODULE_PARM_DESC(blowfish, "0: disable | 1: force_enable | min,max: keybitlens");
  14166. +MODULE_PARM_DESC(cast, "0: disable | 1: force_enable | min,max: keybitlens");
  14167. +MODULE_PARM_DESC(serpent, "0: disable | 1: force_enable | min,max: keybitlens");
  14168. +MODULE_PARM_DESC(twofish, "0: disable | 1: force_enable | min,max: keybitlens");
  14169. +
  14170. +struct ipsec_alg_capi_cipher {
  14171. + const char *ciphername; /* cryptoapi's ciphername */
  14172. + unsigned blocksize;
  14173. + unsigned short minbits;
  14174. + unsigned short maxbits;
  14175. + int *parm; /* lkm param for this cipher */
  14176. + struct ipsec_alg_enc alg; /* note it's not a pointer */
  14177. +};
  14178. +static struct ipsec_alg_capi_cipher alg_capi_carray[] = {
  14179. + { CIPHERNAME_AES , 16, 128, 256, aes , { ixt_alg_id: ESP_AES, }},
  14180. + { CIPHERNAME_TWOFISH , 16, 128, 256, twofish, { ixt_alg_id: ESP_TWOFISH, }},
  14181. + { CIPHERNAME_SERPENT , 16, 128, 256, serpent, { ixt_alg_id: ESP_SERPENT, }},
  14182. + { CIPHERNAME_CAST , 8, 128, 128, cast , { ixt_alg_id: ESP_CAST, }},
  14183. + { CIPHERNAME_BLOWFISH , 8, 96, 448, blowfish,{ ixt_alg_id: ESP_BLOWFISH, }},
  14184. + { CIPHERNAME_3DES , 8, 192, 192, des_ede3,{ ixt_alg_id: ESP_3DES, }},
  14185. + { NULL, 0, 0, 0, NULL, {} }
  14186. +};
  14187. +#ifdef NOT_YET
  14188. +struct ipsec_alg_capi_digest {
  14189. + const char *digestname; /* cryptoapi's digestname */
  14190. + struct digest_implementation *di;
  14191. + struct ipsec_alg_auth alg; /* note it's not a pointer */
  14192. +};
  14193. +static struct ipsec_alg_capi_cipher alg_capi_darray[] = {
  14194. + { DIGESTNAME_MD5, NULL, { ixt_alg_id: AH_MD5, }},
  14195. + { DIGESTNAME_SHA1, NULL, { ixt_alg_id: AH_SHA, }},
  14196. + { NULL, NULL, {} }
  14197. +};
  14198. +#endif
  14199. +/*
  14200. + * "generic" linux cryptoapi setup_cipher() function
  14201. + */
  14202. +int setup_cipher(const char *ciphername)
  14203. +{
  14204. + return crypto_alg_available(ciphername, 0);
  14205. +}
  14206. +
  14207. +/*
  14208. + * setups ipsec_alg_capi_cipher "hyper" struct components, calling
  14209. + * register_ipsec_alg for cointaned ipsec_alg object
  14210. + */
  14211. +static void _capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e);
  14212. +static __u8 * _capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen);
  14213. +static int _capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt);
  14214. +
  14215. +static int
  14216. +setup_ipsec_alg_capi_cipher(struct ipsec_alg_capi_cipher *cptr)
  14217. +{
  14218. + int ret;
  14219. + cptr->alg.ixt_version = IPSEC_ALG_VERSION;
  14220. + cptr->alg.ixt_module = THIS_MODULE;
  14221. + atomic_set (& cptr->alg.ixt_refcnt, 0);
  14222. + strncpy (cptr->alg.ixt_name , cptr->ciphername, sizeof (cptr->alg.ixt_name));
  14223. +
  14224. + cptr->alg.ixt_blocksize=cptr->blocksize;
  14225. + cptr->alg.ixt_keyminbits=cptr->minbits;
  14226. + cptr->alg.ixt_keymaxbits=cptr->maxbits;
  14227. + cptr->alg.ixt_state = 0;
  14228. + if (excl) cptr->alg.ixt_state |= IPSEC_ALG_ST_EXCL;
  14229. + cptr->alg.ixt_e_keylen=cptr->alg.ixt_keymaxbits/8;
  14230. + cptr->alg.ixt_e_ctx_size = 0;
  14231. + cptr->alg.ixt_alg_type = IPSEC_ALG_TYPE_ENCRYPT;
  14232. + cptr->alg.ixt_e_new_key = _capi_new_key;
  14233. + cptr->alg.ixt_e_destroy_key = _capi_destroy_key;
  14234. + cptr->alg.ixt_e_cbc_encrypt = _capi_cbc_encrypt;
  14235. + cptr->alg.ixt_data = cptr;
  14236. +
  14237. + ret=register_ipsec_alg_enc(&cptr->alg);
  14238. + printk("setup_ipsec_alg_capi_cipher(): "
  14239. + "alg_type=%d alg_id=%d name=%s "
  14240. + "keyminbits=%d keymaxbits=%d, ret=%d\n",
  14241. + cptr->alg.ixt_alg_type,
  14242. + cptr->alg.ixt_alg_id,
  14243. + cptr->alg.ixt_name,
  14244. + cptr->alg.ixt_keyminbits,
  14245. + cptr->alg.ixt_keymaxbits,
  14246. + ret);
  14247. + return ret;
  14248. +}
  14249. +/*
  14250. + * called in ipsec_sa_wipe() time, will destroy key contexts
  14251. + * and do 1 unbind()
  14252. + */
  14253. +static void
  14254. +_capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e)
  14255. +{
  14256. + struct crypto_tfm *tfm=(struct crypto_tfm*)key_e;
  14257. +
  14258. + if (debug > 0)
  14259. + printk(KERN_DEBUG "klips_debug: _capi_destroy_key:"
  14260. + "name=%s key_e=%p \n",
  14261. + alg->ixt_name, key_e);
  14262. + if (!key_e) {
  14263. + printk(KERN_ERR "klips_debug: _capi_destroy_key:"
  14264. + "name=%s NULL key_e!\n",
  14265. + alg->ixt_name);
  14266. + return;
  14267. + }
  14268. + crypto_free_tfm(tfm);
  14269. +}
  14270. +
  14271. +/*
  14272. + * create new key context, need alg->ixt_data to know which
  14273. + * (of many) cipher inside this module is the target
  14274. + */
  14275. +static __u8 *
  14276. +_capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen)
  14277. +{
  14278. + struct ipsec_alg_capi_cipher *cptr;
  14279. + struct crypto_tfm *tfm=NULL;
  14280. +
  14281. + cptr = alg->ixt_data;
  14282. + if (!cptr) {
  14283. + printk(KERN_ERR "_capi_new_key(): "
  14284. + "NULL ixt_data (?!) for \"%s\" algo\n"
  14285. + , alg->ixt_name);
  14286. + goto err;
  14287. + }
  14288. + if (debug > 0)
  14289. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  14290. + "name=%s cptr=%p key=%p keysize=%d\n",
  14291. + alg->ixt_name, cptr, key, keylen);
  14292. +
  14293. + /*
  14294. + * alloc tfm
  14295. + */
  14296. + tfm = crypto_alloc_tfm(cptr->ciphername, CRYPTO_TFM_MODE_CBC);
  14297. + if (!tfm) {
  14298. + printk(KERN_ERR "_capi_new_key(): "
  14299. + "NULL tfm for \"%s\" cryptoapi (\"%s\") algo\n"
  14300. + , alg->ixt_name, cptr->ciphername);
  14301. + goto err;
  14302. + }
  14303. + if (crypto_cipher_setkey(tfm, key, keylen) < 0) {
  14304. + printk(KERN_ERR "_capi_new_key(): "
  14305. + "failed new_key() for \"%s\" cryptoapi algo (keylen=%d)\n"
  14306. + , alg->ixt_name, keylen);
  14307. + crypto_free_tfm(tfm);
  14308. + tfm=NULL;
  14309. + }
  14310. +err:
  14311. + if (debug > 0)
  14312. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  14313. + "name=%s key=%p keylen=%d tfm=%p\n",
  14314. + alg->ixt_name, key, keylen, tfm);
  14315. + return (__u8 *) tfm;
  14316. +}
  14317. +/*
  14318. + * core encryption function: will use cx->ci to call actual cipher's
  14319. + * cbc function
  14320. + */
  14321. +static int
  14322. +_capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt) {
  14323. + int error =0;
  14324. + struct crypto_tfm *tfm=(struct crypto_tfm *)key_e;
  14325. + struct scatterlist sg = {
  14326. + .page = virt_to_page(in),
  14327. + .offset = (unsigned long)(in) % PAGE_SIZE,
  14328. + .length=ilen,
  14329. + };
  14330. + if (debug > 1)
  14331. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  14332. + "key_e=%p "
  14333. + "in=%p out=%p ilen=%d iv=%p encrypt=%d\n"
  14334. + , key_e
  14335. + , in, in, ilen, iv, encrypt);
  14336. + crypto_cipher_set_iv(tfm, iv, crypto_tfm_alg_ivsize(tfm));
  14337. + if (encrypt)
  14338. + error = crypto_cipher_encrypt (tfm, &sg, &sg, ilen);
  14339. + else
  14340. + error = crypto_cipher_decrypt (tfm, &sg, &sg, ilen);
  14341. + if (debug > 1)
  14342. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  14343. + "error=%d\n"
  14344. + , error);
  14345. + return (error<0)? error : ilen;
  14346. +}
  14347. +/*
  14348. + * main initialization loop: for each cipher in list, do
  14349. + * 1) setup cryptoapi cipher else continue
  14350. + * 2) register ipsec_alg object
  14351. + */
  14352. +static int
  14353. +setup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14354. +{
  14355. + struct ipsec_alg_capi_cipher *cptr;
  14356. + /* foreach cipher in list ... */
  14357. + for (cptr=clist;cptr->ciphername;cptr++) {
  14358. + /*
  14359. + * see if cipher has been disabled (0) or
  14360. + * if noauto set and not enabled (1)
  14361. + */
  14362. + if (cptr->parm[0] == 0 || (noauto && cptr->parm[0] < 0)) {
  14363. + if (debug>0)
  14364. + printk(KERN_INFO "setup_cipher_list(): "
  14365. + "ciphername=%s skipped at user request: "
  14366. + "noauto=%d parm[0]=%d parm[1]=%d\n"
  14367. + , cptr->ciphername
  14368. + , noauto
  14369. + , cptr->parm[0]
  14370. + , cptr->parm[1]);
  14371. + continue;
  14372. + }
  14373. + /*
  14374. + * use a local ci to avoid touching cptr->ci,
  14375. + * if register ipsec_alg success then bind cipher
  14376. + */
  14377. + if( setup_cipher(cptr->ciphername) ) {
  14378. + if (debug > 0)
  14379. + printk(KERN_DEBUG "klips_debug:"
  14380. + "setup_cipher_list():"
  14381. + "ciphername=%s found\n"
  14382. + , cptr->ciphername);
  14383. + if (setup_ipsec_alg_capi_cipher(cptr) == 0) {
  14384. +
  14385. +
  14386. + } else {
  14387. + printk(KERN_ERR "klips_debug:"
  14388. + "setup_cipher_list():"
  14389. + "ciphername=%s failed ipsec_alg_register\n"
  14390. + , cptr->ciphername);
  14391. + }
  14392. + } else {
  14393. + if (debug>0)
  14394. + printk(KERN_INFO "setup_cipher_list(): lookup for ciphername=%s: not found \n",
  14395. + cptr->ciphername);
  14396. + }
  14397. + }
  14398. + return 0;
  14399. +}
  14400. +/*
  14401. + * deregister ipsec_alg objects and unbind ciphers
  14402. + */
  14403. +static int
  14404. +unsetup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14405. +{
  14406. + struct ipsec_alg_capi_cipher *cptr;
  14407. + /* foreach cipher in list ... */
  14408. + for (cptr=clist;cptr->ciphername;cptr++) {
  14409. + if (cptr->alg.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  14410. + unregister_ipsec_alg_enc(&cptr->alg);
  14411. + }
  14412. + }
  14413. + return 0;
  14414. +}
  14415. +/*
  14416. + * test loop for registered algos
  14417. + */
  14418. +static int
  14419. +test_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14420. +{
  14421. + int test_ret;
  14422. + struct ipsec_alg_capi_cipher *cptr;
  14423. + /* foreach cipher in list ... */
  14424. + for (cptr=clist;cptr->ciphername;cptr++) {
  14425. + if (cptr->alg.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  14426. + test_ret=ipsec_alg_test(
  14427. + cptr->alg.ixt_alg_type,
  14428. + cptr->alg.ixt_alg_id,
  14429. + test);
  14430. + printk("test_cipher_list(alg_type=%d alg_id=%d): test_ret=%d\n",
  14431. + cptr->alg.ixt_alg_type,
  14432. + cptr->alg.ixt_alg_id,
  14433. + test_ret);
  14434. + }
  14435. + }
  14436. + return 0;
  14437. +}
  14438. +
  14439. +IPSEC_ALG_MODULE_INIT( ipsec_cryptoapi_init )
  14440. +{
  14441. + int ret, test_ret;
  14442. + if ((ret=setup_cipher_list(alg_capi_carray)) < 0)
  14443. + return -EPROTONOSUPPORT;
  14444. + if (ret==0 && test) {
  14445. + test_ret=test_cipher_list(alg_capi_carray);
  14446. + }
  14447. + return ret;
  14448. +}
  14449. +IPSEC_ALG_MODULE_EXIT( ipsec_cryptoapi_fini )
  14450. +{
  14451. + unsetup_cipher_list(alg_capi_carray);
  14452. + return;
  14453. +}
  14454. +#ifdef MODULE_LICENSE
  14455. +MODULE_LICENSE("GPL");
  14456. +#endif
  14457. +
  14458. +EXPORT_NO_SYMBOLS;
  14459. +#endif /* NO_CRYPTOAPI_SUPPORT */
  14460. --- /dev/null Tue Mar 11 13:02:56 2003
  14461. +++ linux/net/ipsec/alg/scripts/mk-static_init.c.sh Mon Feb 9 13:51:03 2004
  14462. @@ -0,0 +1,18 @@
  14463. +#!/bin/sh
  14464. +cat << EOF
  14465. +#include <linux/kernel.h>
  14466. +#include <linux/list.h>
  14467. +#include "freeswan/ipsec_alg.h"
  14468. +$(for i in $*; do
  14469. + test -z "$i" && continue
  14470. + echo "extern int $i(void);"
  14471. +done)
  14472. +void ipsec_alg_static_init(void){
  14473. + int __attribute__ ((unused)) err=0;
  14474. +$(for i in $*; do
  14475. + test -z "$i" && continue
  14476. + echo " if ((err=$i()) < 0)"
  14477. + echo " printk(KERN_WARNING \"$i() returned %d\", err);"
  14478. +done)
  14479. +}
  14480. +EOF
  14481. --- /dev/null Tue Mar 11 13:02:56 2003
  14482. +++ linux/net/ipsec/anyaddr.c Mon Feb 9 13:51:03 2004
  14483. @@ -0,0 +1,145 @@
  14484. +/*
  14485. + * special addresses
  14486. + * Copyright (C) 2000 Henry Spencer.
  14487. + *
  14488. + * This library is free software; you can redistribute it and/or modify it
  14489. + * under the terms of the GNU Library General Public License as published by
  14490. + * the Free Software Foundation; either version 2 of the License, or (at your
  14491. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  14492. + *
  14493. + * This library is distributed in the hope that it will be useful, but
  14494. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14495. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  14496. + * License for more details.
  14497. + *
  14498. + * RCSID $Id: anyaddr.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  14499. + */
  14500. +#include "openswan.h"
  14501. +
  14502. +/* these are mostly fallbacks for the no-IPv6-support-in-library case */
  14503. +#ifndef IN6ADDR_ANY_INIT
  14504. +#define IN6ADDR_ANY_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
  14505. +#endif
  14506. +#ifndef IN6ADDR_LOOPBACK_INIT
  14507. +#define IN6ADDR_LOOPBACK_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
  14508. +#endif
  14509. +
  14510. +static struct in6_addr v6any = IN6ADDR_ANY_INIT;
  14511. +static struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;
  14512. +
  14513. +/*
  14514. + - anyaddr - initialize to the any-address value
  14515. + */
  14516. +err_t /* NULL for success, else string literal */
  14517. +anyaddr(af, dst)
  14518. +int af; /* address family */
  14519. +ip_address *dst;
  14520. +{
  14521. + uint32_t v4any = htonl(INADDR_ANY);
  14522. +
  14523. + switch (af) {
  14524. + case AF_INET:
  14525. + return initaddr((unsigned char *)&v4any, sizeof(v4any), af, dst);
  14526. + break;
  14527. + case AF_INET6:
  14528. + return initaddr((unsigned char *)&v6any, sizeof(v6any), af, dst);
  14529. + break;
  14530. + default:
  14531. + return "unknown address family in anyaddr/unspecaddr";
  14532. + break;
  14533. + }
  14534. +}
  14535. +
  14536. +/*
  14537. + - unspecaddr - initialize to the unspecified-address value
  14538. + */
  14539. +err_t /* NULL for success, else string literal */
  14540. +unspecaddr(af, dst)
  14541. +int af; /* address family */
  14542. +ip_address *dst;
  14543. +{
  14544. + return anyaddr(af, dst);
  14545. +}
  14546. +
  14547. +/*
  14548. + - loopbackaddr - initialize to the loopback-address value
  14549. + */
  14550. +err_t /* NULL for success, else string literal */
  14551. +loopbackaddr(af, dst)
  14552. +int af; /* address family */
  14553. +ip_address *dst;
  14554. +{
  14555. + uint32_t v4loop = htonl(INADDR_LOOPBACK);
  14556. +
  14557. + switch (af) {
  14558. + case AF_INET:
  14559. + return initaddr((unsigned char *)&v4loop, sizeof(v4loop), af, dst);
  14560. + break;
  14561. + case AF_INET6:
  14562. + return initaddr((unsigned char *)&v6loop, sizeof(v6loop), af, dst);
  14563. + break;
  14564. + default:
  14565. + return "unknown address family in loopbackaddr";
  14566. + break;
  14567. + }
  14568. +}
  14569. +
  14570. +/*
  14571. + - isanyaddr - test for the any-address value
  14572. + */
  14573. +int
  14574. +isanyaddr(src)
  14575. +const ip_address *src;
  14576. +{
  14577. + uint32_t v4any = htonl(INADDR_ANY);
  14578. + int cmp;
  14579. +
  14580. + switch (src->u.v4.sin_family) {
  14581. + case AF_INET:
  14582. + cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4any, sizeof(v4any));
  14583. + break;
  14584. + case AF_INET6:
  14585. + cmp = memcmp(&src->u.v6.sin6_addr, &v6any, sizeof(v6any));
  14586. + break;
  14587. + default:
  14588. + return 0;
  14589. + break;
  14590. + }
  14591. +
  14592. + return (cmp == 0) ? 1 : 0;
  14593. +}
  14594. +
  14595. +/*
  14596. + - isunspecaddr - test for the unspecified-address value
  14597. + */
  14598. +int
  14599. +isunspecaddr(src)
  14600. +const ip_address *src;
  14601. +{
  14602. + return isanyaddr(src);
  14603. +}
  14604. +
  14605. +/*
  14606. + - isloopbackaddr - test for the loopback-address value
  14607. + */
  14608. +int
  14609. +isloopbackaddr(src)
  14610. +const ip_address *src;
  14611. +{
  14612. + uint32_t v4loop = htonl(INADDR_LOOPBACK);
  14613. + int cmp;
  14614. +
  14615. + switch (src->u.v4.sin_family) {
  14616. + case AF_INET:
  14617. + cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4loop, sizeof(v4loop));
  14618. + break;
  14619. + case AF_INET6:
  14620. + cmp = memcmp(&src->u.v6.sin6_addr, &v6loop, sizeof(v6loop));
  14621. + break;
  14622. + default:
  14623. + return 0;
  14624. + break;
  14625. + }
  14626. +
  14627. + return (cmp == 0) ? 1 : 0;
  14628. +}
  14629. --- /dev/null Tue Mar 11 13:02:56 2003
  14630. +++ linux/net/ipsec/datatot.c Mon Feb 9 13:51:03 2004
  14631. @@ -0,0 +1,234 @@
  14632. +/*
  14633. + * convert from binary data (e.g. key) to text form
  14634. + * Copyright (C) 2000 Henry Spencer.
  14635. + *
  14636. + * This library is free software; you can redistribute it and/or modify it
  14637. + * under the terms of the GNU Library General Public License as published by
  14638. + * the Free Software Foundation; either version 2 of the License, or (at your
  14639. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  14640. + *
  14641. + * This library is distributed in the hope that it will be useful, but
  14642. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14643. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  14644. + * License for more details.
  14645. + *
  14646. + * RCSID $Id: datatot.c,v 1.7 2005/04/14 20:48:43 mcr Exp $
  14647. + */
  14648. +#include "openswan.h"
  14649. +
  14650. +static void convert(const char *src, size_t nreal, int format, char *out);
  14651. +
  14652. +/*
  14653. + - datatot - convert data bytes to text
  14654. + */
  14655. +size_t /* true length (with NUL) for success */
  14656. +datatot(src, srclen, format, dst, dstlen)
  14657. +const char *src;
  14658. +size_t srclen;
  14659. +int format; /* character indicating what format */
  14660. +char *dst; /* need not be valid if dstlen is 0 */
  14661. +size_t dstlen;
  14662. +{
  14663. + size_t inblocksize; /* process this many bytes at a time */
  14664. + size_t outblocksize; /* producing this many */
  14665. + size_t breakevery; /* add a _ every this many (0 means don't) */
  14666. + size_t sincebreak; /* output bytes since last _ */
  14667. + char breakchar; /* character used to break between groups */
  14668. + char inblock[10]; /* enough for any format */
  14669. + char outblock[10]; /* enough for any format */
  14670. + char fake[1]; /* fake output area for dstlen == 0 */
  14671. + size_t needed; /* return value */
  14672. + char *stop; /* where the terminating NUL will go */
  14673. + size_t ntodo; /* remaining input */
  14674. + size_t nreal;
  14675. + char *out;
  14676. + char *prefix;
  14677. +
  14678. + breakevery = 0;
  14679. + breakchar = '_';
  14680. +
  14681. + switch (format) {
  14682. + case 0:
  14683. + case 'h':
  14684. + format = 'x';
  14685. + breakevery = 8;
  14686. + /* FALLTHROUGH */
  14687. + case 'x':
  14688. + inblocksize = 1;
  14689. + outblocksize = 2;
  14690. + prefix = "0x";
  14691. + break;
  14692. + case ':':
  14693. + format = 'x';
  14694. + breakevery = 2;
  14695. + breakchar = ':';
  14696. + /* FALLTHROUGH */
  14697. + case 16:
  14698. + inblocksize = 1;
  14699. + outblocksize = 2;
  14700. + prefix = "";
  14701. + format = 'x';
  14702. + break;
  14703. + case 's':
  14704. + inblocksize = 3;
  14705. + outblocksize = 4;
  14706. + prefix = "0s";
  14707. + break;
  14708. + case 64: /* beware, equals ' ' */
  14709. + inblocksize = 3;
  14710. + outblocksize = 4;
  14711. + prefix = "";
  14712. + format = 's';
  14713. + break;
  14714. + default:
  14715. + return 0;
  14716. + break;
  14717. + }
  14718. +
  14719. + user_assert(inblocksize < sizeof(inblock));
  14720. + user_assert(outblocksize < sizeof(outblock));
  14721. + user_assert(breakevery % outblocksize == 0);
  14722. +
  14723. + if (srclen == 0)
  14724. + return 0;
  14725. + ntodo = srclen;
  14726. +
  14727. + if (dstlen == 0) { /* dispose of awkward special case */
  14728. + dst = fake;
  14729. + dstlen = 1;
  14730. + }
  14731. + stop = dst + dstlen - 1;
  14732. +
  14733. + nreal = strlen(prefix);
  14734. + needed = nreal; /* for starters */
  14735. + if (dstlen <= nreal) { /* prefix won't fit */
  14736. + strncpy(dst, prefix, dstlen - 1);
  14737. + dst += dstlen - 1;
  14738. + } else {
  14739. + strcpy(dst, prefix);
  14740. + dst += nreal;
  14741. + }
  14742. +
  14743. + user_assert(dst <= stop);
  14744. + sincebreak = 0;
  14745. +
  14746. + while (ntodo > 0) {
  14747. + if (ntodo < inblocksize) { /* incomplete input */
  14748. + memset(inblock, 0, sizeof(inblock));
  14749. + memcpy(inblock, src, ntodo);
  14750. + src = inblock;
  14751. + nreal = ntodo;
  14752. + ntodo = inblocksize;
  14753. + } else
  14754. + nreal = inblocksize;
  14755. + out = (outblocksize > stop - dst) ? outblock : dst;
  14756. +
  14757. + convert(src, nreal, format, out);
  14758. + needed += outblocksize;
  14759. + sincebreak += outblocksize;
  14760. + if (dst < stop) {
  14761. + if (out != dst) {
  14762. + user_assert(outblocksize > stop - dst);
  14763. + memcpy(dst, out, stop - dst);
  14764. + dst = stop;
  14765. + } else
  14766. + dst += outblocksize;
  14767. + }
  14768. +
  14769. + src += inblocksize;
  14770. + ntodo -= inblocksize;
  14771. + if (breakevery != 0 && sincebreak >= breakevery && ntodo > 0) {
  14772. + if (dst < stop)
  14773. + *dst++ = breakchar;
  14774. + needed++;
  14775. + sincebreak = 0;
  14776. + }
  14777. + }
  14778. +
  14779. + user_assert(dst <= stop);
  14780. + *dst++ = '\0';
  14781. + needed++;
  14782. +
  14783. + return needed;
  14784. +}
  14785. +
  14786. +/*
  14787. + - convert - convert one input block to one output block
  14788. + */
  14789. +static void
  14790. +convert(src, nreal, format, out)
  14791. +const char *src;
  14792. +size_t nreal; /* how much of the input block is real */
  14793. +int format;
  14794. +char *out;
  14795. +{
  14796. + static char hex[] = "0123456789abcdef";
  14797. + static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  14798. + "abcdefghijklmnopqrstuvwxyz"
  14799. + "0123456789+/";
  14800. + unsigned char c;
  14801. + unsigned char c1, c2, c3;
  14802. +
  14803. + user_assert(nreal > 0);
  14804. + switch (format) {
  14805. + case 'x':
  14806. + user_assert(nreal == 1);
  14807. + c = (unsigned char)*src;
  14808. + *out++ = hex[c >> 4];
  14809. + *out++ = hex[c & 0xf];
  14810. + break;
  14811. + case 's':
  14812. + c1 = (unsigned char)*src++;
  14813. + c2 = (unsigned char)*src++;
  14814. + c3 = (unsigned char)*src++;
  14815. + *out++ = base64[c1 >> 2]; /* top 6 bits of c1 */
  14816. + c = (c1 & 0x3) << 4; /* bottom 2 of c1... */
  14817. + c |= c2 >> 4; /* ...top 4 of c2 */
  14818. + *out++ = base64[c];
  14819. + if (nreal == 1)
  14820. + *out++ = '=';
  14821. + else {
  14822. + c = (c2 & 0xf) << 2; /* bottom 4 of c2... */
  14823. + c |= c3 >> 6; /* ...top 2 of c3 */
  14824. + *out++ = base64[c];
  14825. + }
  14826. + if (nreal <= 2)
  14827. + *out++ = '=';
  14828. + else
  14829. + *out++ = base64[c3 & 0x3f]; /* bottom 6 of c3 */
  14830. + break;
  14831. + default:
  14832. + user_assert(nreal == 0); /* unknown format */
  14833. + break;
  14834. + }
  14835. +}
  14836. +
  14837. +/*
  14838. + - datatoa - convert data to ASCII
  14839. + * backward-compatibility synonym for datatot
  14840. + */
  14841. +size_t /* true length (with NUL) for success */
  14842. +datatoa(src, srclen, format, dst, dstlen)
  14843. +const char *src;
  14844. +size_t srclen;
  14845. +int format; /* character indicating what format */
  14846. +char *dst; /* need not be valid if dstlen is 0 */
  14847. +size_t dstlen;
  14848. +{
  14849. + return datatot(src, srclen, format, dst, dstlen);
  14850. +}
  14851. +
  14852. +/*
  14853. + - bytestoa - convert data bytes to ASCII
  14854. + * backward-compatibility synonym for datatot
  14855. + */
  14856. +size_t /* true length (with NUL) for success */
  14857. +bytestoa(src, srclen, format, dst, dstlen)
  14858. +const char *src;
  14859. +size_t srclen;
  14860. +int format; /* character indicating what format */
  14861. +char *dst; /* need not be valid if dstlen is 0 */
  14862. +size_t dstlen;
  14863. +{
  14864. + return datatot(src, srclen, format, dst, dstlen);
  14865. +}
  14866. --- /dev/null Tue Mar 11 13:02:56 2003
  14867. +++ linux/net/ipsec/defconfig Mon Feb 9 13:51:03 2004
  14868. @@ -0,0 +1,148 @@
  14869. +
  14870. +#
  14871. +# RCSID $Id: defconfig,v 1.28.2.1 2006/10/11 18:14:33 paul Exp $
  14872. +#
  14873. +
  14874. +#
  14875. +# FreeS/WAN IPSec implementation, KLIPS kernel config defaults
  14876. +#
  14877. +
  14878. +#
  14879. +# First, lets override stuff already set or not in the kernel config.
  14880. +#
  14881. +# We can't even think about leaving this off...
  14882. +CONFIG_INET=y
  14883. +
  14884. +#
  14885. +# This must be on for subnet protection.
  14886. +CONFIG_IP_FORWARD=y
  14887. +
  14888. +# Shut off IPSEC masquerading if it has been enabled, since it will
  14889. +# break the compile. IPPROTO_ESP and IPPROTO_AH were included in
  14890. +# net/ipv4/ip_masq.c when they should have gone into include/linux/in.h.
  14891. +CONFIG_IP_MASQUERADE_IPSEC=n
  14892. +
  14893. +#
  14894. +# Next, lets set the recommended FreeS/WAN configuration.
  14895. +#
  14896. +
  14897. +# To config as static (preferred), 'y'. To config as module, 'm'.
  14898. +CONFIG_KLIPS=m
  14899. +
  14900. +# To do tunnel mode IPSec, this must be enabled.
  14901. +CONFIG_KLIPS_IPIP=y
  14902. +
  14903. +# To enable authentication, say 'y'. (Highly recommended)
  14904. +CONFIG_KLIPS_AH=y
  14905. +
  14906. +# Authentication algorithm(s):
  14907. +CONFIG_KLIPS_AUTH_HMAC_MD5=y
  14908. +CONFIG_KLIPS_AUTH_HMAC_SHA1=y
  14909. +
  14910. +# To enable encryption, say 'y'. (Highly recommended)
  14911. +CONFIG_KLIPS_ESP=y
  14912. +
  14913. +# modular algo extensions (and new ALGOs)
  14914. +CONFIG_KLIPS_ALG=y
  14915. +
  14916. +# Encryption algorithm(s):
  14917. +CONFIG_KLIPS_ENC_3DES=y
  14918. +CONFIG_KLIPS_ENC_AES=y
  14919. +# CONFIG_KLIPS_ENC_NULL=y
  14920. +
  14921. +# Use CryptoAPI for ALG? - by default, no.
  14922. +CONFIG_KLIPS_ENC_CRYPTOAPI=n
  14923. +
  14924. +# IP Compression: new, probably still has minor bugs.
  14925. +CONFIG_KLIPS_IPCOMP=y
  14926. +
  14927. +# To enable userspace-switchable KLIPS debugging, say 'y'.
  14928. +CONFIG_KLIPS_DEBUG=y
  14929. +
  14930. +# NAT Traversal
  14931. +CONFIG_IPSEC_NAT_TRAVERSAL=y
  14932. +
  14933. +#
  14934. +#
  14935. +# $Log: defconfig,v $
  14936. +# Revision 1.28.2.1 2006/10/11 18:14:33 paul
  14937. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  14938. +# per default.
  14939. +#
  14940. +# Revision 1.28 2005/05/11 03:15:42 mcr
  14941. +# adjusted makefiles to sanely build modules properly.
  14942. +#
  14943. +# Revision 1.27 2005/03/20 03:00:05 mcr
  14944. +# default configuration should enable NAT_TRAVERSAL.
  14945. +#
  14946. +# Revision 1.26 2004/07/10 19:11:18 mcr
  14947. +# CONFIG_IPSEC -> CONFIG_KLIPS.
  14948. +#
  14949. +# Revision 1.25 2004/07/05 01:03:53 mcr
  14950. +# fix for adding cryptoapi code.
  14951. +# keep it off for now, since UMLs do not have it yet.
  14952. +#
  14953. +# Revision 1.24 2004/04/06 02:49:25 mcr
  14954. +# pullup of algo code from alg-branch.
  14955. +#
  14956. +# Revision 1.23.2.2 2004/04/05 04:30:46 mcr
  14957. +# patches for alg-branch to compile/work with 2.x openswan
  14958. +#
  14959. +# Revision 1.23.2.1 2003/12/22 15:25:52 jjo
  14960. +# . Merged algo-0.8.1-rc11-test1 into alg-branch
  14961. +#
  14962. +# Revision 1.23 2003/12/10 01:14:27 mcr
  14963. +# NAT-traversal patches to KLIPS.
  14964. +#
  14965. +# Revision 1.22 2003/02/24 19:37:27 mcr
  14966. +# changed default compilation mode to static.
  14967. +#
  14968. +# Revision 1.21 2002/04/24 07:36:27 mcr
  14969. +# Moved from ./klips/net/ipsec/defconfig,v
  14970. +#
  14971. +# Revision 1.20 2002/04/02 04:07:40 mcr
  14972. +# default build is now 'm'odule for KLIPS
  14973. +#
  14974. +# Revision 1.19 2002/03/08 18:57:17 rgb
  14975. +# Added a blank line at the beginning of the file to make it easier for
  14976. +# other projects to patch ./arch/i386/defconfig, for example
  14977. +# LIDS+grSecurity requested by Jason Pattie.
  14978. +#
  14979. +# Revision 1.18 2000/11/30 17:26:56 rgb
  14980. +# Cleaned out unused options and enabled ipcomp by default.
  14981. +#
  14982. +# Revision 1.17 2000/09/15 11:37:01 rgb
  14983. +# Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  14984. +# IPCOMP zlib deflate code.
  14985. +#
  14986. +# Revision 1.16 2000/09/08 19:12:55 rgb
  14987. +# Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  14988. +#
  14989. +# Revision 1.15 2000/05/24 19:37:13 rgb
  14990. +# *** empty log message ***
  14991. +#
  14992. +# Revision 1.14 2000/05/11 21:14:57 henry
  14993. +# just commenting the FOOBAR=y lines out is not enough
  14994. +#
  14995. +# Revision 1.13 2000/05/10 20:17:58 rgb
  14996. +# Comment out netlink defaults, which are no longer needed.
  14997. +#
  14998. +# Revision 1.12 2000/05/10 19:13:38 rgb
  14999. +# Added configure option to shut off no eroute passthrough.
  15000. +#
  15001. +# Revision 1.11 2000/03/16 07:09:46 rgb
  15002. +# Hardcode PF_KEYv2 support.
  15003. +# Disable IPSEC_ICMP by default.
  15004. +# Remove DES config option from defaults file.
  15005. +#
  15006. +# Revision 1.10 2000/01/11 03:09:42 rgb
  15007. +# Added a default of 'y' to PF_KEYv2 keying I/F.
  15008. +#
  15009. +# Revision 1.9 1999/05/08 21:23:12 rgb
  15010. +# Added support for 2.2.x kernels.
  15011. +#
  15012. +# Revision 1.8 1999/04/06 04:54:25 rgb
  15013. +# Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  15014. +# patch shell fixes.
  15015. +#
  15016. +#
  15017. --- /dev/null Tue Mar 11 13:02:56 2003
  15018. +++ linux/net/ipsec/deflate.c Mon Feb 9 13:51:03 2004
  15019. @@ -0,0 +1,1351 @@
  15020. +/* deflate.c -- compress data using the deflation algorithm
  15021. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  15022. + * For conditions of distribution and use, see copyright notice in zlib.h
  15023. + */
  15024. +
  15025. +/*
  15026. + * ALGORITHM
  15027. + *
  15028. + * The "deflation" process depends on being able to identify portions
  15029. + * of the input text which are identical to earlier input (within a
  15030. + * sliding window trailing behind the input currently being processed).
  15031. + *
  15032. + * The most straightforward technique turns out to be the fastest for
  15033. + * most input files: try all possible matches and select the longest.
  15034. + * The key feature of this algorithm is that insertions into the string
  15035. + * dictionary are very simple and thus fast, and deletions are avoided
  15036. + * completely. Insertions are performed at each input character, whereas
  15037. + * string matches are performed only when the previous match ends. So it
  15038. + * is preferable to spend more time in matches to allow very fast string
  15039. + * insertions and avoid deletions. The matching algorithm for small
  15040. + * strings is inspired from that of Rabin & Karp. A brute force approach
  15041. + * is used to find longer strings when a small match has been found.
  15042. + * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  15043. + * (by Leonid Broukhis).
  15044. + * A previous version of this file used a more sophisticated algorithm
  15045. + * (by Fiala and Greene) which is guaranteed to run in linear amortized
  15046. + * time, but has a larger average cost, uses more memory and is patented.
  15047. + * However the F&G algorithm may be faster for some highly redundant
  15048. + * files if the parameter max_chain_length (described below) is too large.
  15049. + *
  15050. + * ACKNOWLEDGEMENTS
  15051. + *
  15052. + * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  15053. + * I found it in 'freeze' written by Leonid Broukhis.
  15054. + * Thanks to many people for bug reports and testing.
  15055. + *
  15056. + * REFERENCES
  15057. + *
  15058. + * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  15059. + * Available in ftp://ds.internic.net/rfc/rfc1951.txt
  15060. + *
  15061. + * A description of the Rabin and Karp algorithm is given in the book
  15062. + * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  15063. + *
  15064. + * Fiala,E.R., and Greene,D.H.
  15065. + * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  15066. + *
  15067. + */
  15068. +
  15069. +/* @(#) $Id: deflate.c,v 1.4 2004/07/10 07:48:37 mcr Exp $ */
  15070. +
  15071. +#include "deflate.h"
  15072. +
  15073. +local const char deflate_copyright[] =
  15074. + " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly ";
  15075. +/*
  15076. + If you use the zlib library in a product, an acknowledgment is welcome
  15077. + in the documentation of your product. If for some reason you cannot
  15078. + include such an acknowledgment, I would appreciate that you keep this
  15079. + copyright string in the executable of your product.
  15080. + */
  15081. +
  15082. +/* ===========================================================================
  15083. + * Function prototypes.
  15084. + */
  15085. +typedef enum {
  15086. + need_more, /* block not completed, need more input or more output */
  15087. + block_done, /* block flush performed */
  15088. + finish_started, /* finish started, need only more output at next deflate */
  15089. + finish_done /* finish done, accept no more input or output */
  15090. +} block_state;
  15091. +
  15092. +typedef block_state (*compress_func) OF((deflate_state *s, int flush));
  15093. +/* Compression function. Returns the block state after the call. */
  15094. +
  15095. +local void fill_window OF((deflate_state *s));
  15096. +local block_state deflate_stored OF((deflate_state *s, int flush));
  15097. +local block_state deflate_fast OF((deflate_state *s, int flush));
  15098. +local block_state deflate_slow OF((deflate_state *s, int flush));
  15099. +local void lm_init OF((deflate_state *s));
  15100. +local void putShortMSB OF((deflate_state *s, uInt b));
  15101. +local void flush_pending OF((z_streamp strm));
  15102. +local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
  15103. +#ifdef ASMV
  15104. + void match_init OF((void)); /* asm code initialization */
  15105. + uInt longest_match OF((deflate_state *s, IPos cur_match));
  15106. +#else
  15107. +local uInt longest_match OF((deflate_state *s, IPos cur_match));
  15108. +#endif
  15109. +
  15110. +#ifdef DEBUG
  15111. +local void check_match OF((deflate_state *s, IPos start, IPos match,
  15112. + int length));
  15113. +#endif
  15114. +
  15115. +/* ===========================================================================
  15116. + * Local data
  15117. + */
  15118. +
  15119. +#define NIL 0
  15120. +/* Tail of hash chains */
  15121. +
  15122. +#ifndef TOO_FAR
  15123. +# define TOO_FAR 4096
  15124. +#endif
  15125. +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  15126. +
  15127. +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  15128. +/* Minimum amount of lookahead, except at the end of the input file.
  15129. + * See deflate.c for comments about the MIN_MATCH+1.
  15130. + */
  15131. +
  15132. +/* Values for max_lazy_match, good_match and max_chain_length, depending on
  15133. + * the desired pack level (0..9). The values given below have been tuned to
  15134. + * exclude worst case performance for pathological files. Better values may be
  15135. + * found for specific files.
  15136. + */
  15137. +typedef struct config_s {
  15138. + ush good_length; /* reduce lazy search above this match length */
  15139. + ush max_lazy; /* do not perform lazy search above this match length */
  15140. + ush nice_length; /* quit search above this match length */
  15141. + ush max_chain;
  15142. + compress_func func;
  15143. +} config;
  15144. +
  15145. +local const config configuration_table[10] = {
  15146. +/* good lazy nice chain */
  15147. +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  15148. +/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
  15149. +/* 2 */ {4, 5, 16, 8, deflate_fast},
  15150. +/* 3 */ {4, 6, 32, 32, deflate_fast},
  15151. +
  15152. +/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
  15153. +/* 5 */ {8, 16, 32, 32, deflate_slow},
  15154. +/* 6 */ {8, 16, 128, 128, deflate_slow},
  15155. +/* 7 */ {8, 32, 128, 256, deflate_slow},
  15156. +/* 8 */ {32, 128, 258, 1024, deflate_slow},
  15157. +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
  15158. +
  15159. +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  15160. + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  15161. + * meaning.
  15162. + */
  15163. +
  15164. +#define EQUAL 0
  15165. +/* result of memcmp for equal strings */
  15166. +
  15167. +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
  15168. +
  15169. +/* ===========================================================================
  15170. + * Update a hash value with the given input byte
  15171. + * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  15172. + * input characters, so that a running hash key can be computed from the
  15173. + * previous key instead of complete recalculation each time.
  15174. + */
  15175. +#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  15176. +
  15177. +
  15178. +/* ===========================================================================
  15179. + * Insert string str in the dictionary and set match_head to the previous head
  15180. + * of the hash chain (the most recent string with same hash key). Return
  15181. + * the previous length of the hash chain.
  15182. + * If this file is compiled with -DFASTEST, the compression level is forced
  15183. + * to 1, and no hash chains are maintained.
  15184. + * IN assertion: all calls to to INSERT_STRING are made with consecutive
  15185. + * input characters and the first MIN_MATCH bytes of str are valid
  15186. + * (except for the last MIN_MATCH-1 bytes of the input file).
  15187. + */
  15188. +#ifdef FASTEST
  15189. +#define INSERT_STRING(s, str, match_head) \
  15190. + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  15191. + match_head = s->head[s->ins_h], \
  15192. + s->head[s->ins_h] = (Pos)(str))
  15193. +#else
  15194. +#define INSERT_STRING(s, str, match_head) \
  15195. + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  15196. + s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
  15197. + s->head[s->ins_h] = (Pos)(str))
  15198. +#endif
  15199. +
  15200. +/* ===========================================================================
  15201. + * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  15202. + * prev[] will be initialized on the fly.
  15203. + */
  15204. +#define CLEAR_HASH(s) \
  15205. + s->head[s->hash_size-1] = NIL; \
  15206. + zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  15207. +
  15208. +/* ========================================================================= */
  15209. +int ZEXPORT deflateInit_(strm, level, version, stream_size)
  15210. + z_streamp strm;
  15211. + int level;
  15212. + const char *version;
  15213. + int stream_size;
  15214. +{
  15215. + return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
  15216. + Z_DEFAULT_STRATEGY, version, stream_size);
  15217. + /* To do: ignore strm->next_in if we use it as window */
  15218. +}
  15219. +
  15220. +/* ========================================================================= */
  15221. +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
  15222. + version, stream_size)
  15223. + z_streamp strm;
  15224. + int level;
  15225. + int method;
  15226. + int windowBits;
  15227. + int memLevel;
  15228. + int strategy;
  15229. + const char *version;
  15230. + int stream_size;
  15231. +{
  15232. + deflate_state *s;
  15233. + int noheader = 0;
  15234. + static const char* my_version = ZLIB_VERSION;
  15235. +
  15236. + ushf *overlay;
  15237. + /* We overlay pending_buf and d_buf+l_buf. This works since the average
  15238. + * output size for (length,distance) codes is <= 24 bits.
  15239. + */
  15240. +
  15241. + if (version == Z_NULL || version[0] != my_version[0] ||
  15242. + stream_size != sizeof(z_stream)) {
  15243. + return Z_VERSION_ERROR;
  15244. + }
  15245. + if (strm == Z_NULL) return Z_STREAM_ERROR;
  15246. +
  15247. + strm->msg = Z_NULL;
  15248. + if (strm->zalloc == Z_NULL) {
  15249. + return Z_STREAM_ERROR;
  15250. +/* strm->zalloc = zcalloc;
  15251. + strm->opaque = (voidpf)0;*/
  15252. + }
  15253. + if (strm->zfree == Z_NULL) return Z_STREAM_ERROR; /* strm->zfree = zcfree; */
  15254. +
  15255. + if (level == Z_DEFAULT_COMPRESSION) level = 6;
  15256. +#ifdef FASTEST
  15257. + level = 1;
  15258. +#endif
  15259. +
  15260. + if (windowBits < 0) { /* undocumented feature: suppress zlib header */
  15261. + noheader = 1;
  15262. + windowBits = -windowBits;
  15263. + }
  15264. + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  15265. + windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
  15266. + strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  15267. + return Z_STREAM_ERROR;
  15268. + }
  15269. + s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  15270. + if (s == Z_NULL) return Z_MEM_ERROR;
  15271. + strm->state = (struct internal_state FAR *)s;
  15272. + s->strm = strm;
  15273. +
  15274. + s->noheader = noheader;
  15275. + s->w_bits = windowBits;
  15276. + s->w_size = 1 << s->w_bits;
  15277. + s->w_mask = s->w_size - 1;
  15278. +
  15279. + s->hash_bits = memLevel + 7;
  15280. + s->hash_size = 1 << s->hash_bits;
  15281. + s->hash_mask = s->hash_size - 1;
  15282. + s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  15283. +
  15284. + s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  15285. + s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
  15286. + s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
  15287. +
  15288. + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  15289. +
  15290. + overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  15291. + s->pending_buf = (uchf *) overlay;
  15292. + s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  15293. +
  15294. + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  15295. + s->pending_buf == Z_NULL) {
  15296. + strm->msg = ERR_MSG(Z_MEM_ERROR);
  15297. + deflateEnd (strm);
  15298. + return Z_MEM_ERROR;
  15299. + }
  15300. + s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  15301. + s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  15302. +
  15303. + s->level = level;
  15304. + s->strategy = strategy;
  15305. + s->method = (Byte)method;
  15306. +
  15307. + return deflateReset(strm);
  15308. +}
  15309. +
  15310. +/* ========================================================================= */
  15311. +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
  15312. + z_streamp strm;
  15313. + const Bytef *dictionary;
  15314. + uInt dictLength;
  15315. +{
  15316. + deflate_state *s;
  15317. + uInt length = dictLength;
  15318. + uInt n;
  15319. + IPos hash_head = 0;
  15320. +
  15321. + if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
  15322. + strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
  15323. +
  15324. + s = strm->state;
  15325. + strm->adler = adler32(strm->adler, dictionary, dictLength);
  15326. +
  15327. + if (length < MIN_MATCH) return Z_OK;
  15328. + if (length > MAX_DIST(s)) {
  15329. + length = MAX_DIST(s);
  15330. +#ifndef USE_DICT_HEAD
  15331. + dictionary += dictLength - length; /* use the tail of the dictionary */
  15332. +#endif
  15333. + }
  15334. + zmemcpy(s->window, dictionary, length);
  15335. + s->strstart = length;
  15336. + s->block_start = (long)length;
  15337. +
  15338. + /* Insert all strings in the hash table (except for the last two bytes).
  15339. + * s->lookahead stays null, so s->ins_h will be recomputed at the next
  15340. + * call of fill_window.
  15341. + */
  15342. + s->ins_h = s->window[0];
  15343. + UPDATE_HASH(s, s->ins_h, s->window[1]);
  15344. + for (n = 0; n <= length - MIN_MATCH; n++) {
  15345. + INSERT_STRING(s, n, hash_head);
  15346. + }
  15347. + if (hash_head) hash_head = 0; /* to make compiler happy */
  15348. + return Z_OK;
  15349. +}
  15350. +
  15351. +/* ========================================================================= */
  15352. +int ZEXPORT deflateReset (strm)
  15353. + z_streamp strm;
  15354. +{
  15355. + deflate_state *s;
  15356. +
  15357. + if (strm == Z_NULL || strm->state == Z_NULL ||
  15358. + strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
  15359. +
  15360. + strm->total_in = strm->total_out = 0;
  15361. + strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  15362. + strm->data_type = Z_UNKNOWN;
  15363. +
  15364. + s = (deflate_state *)strm->state;
  15365. + s->pending = 0;
  15366. + s->pending_out = s->pending_buf;
  15367. +
  15368. + if (s->noheader < 0) {
  15369. + s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
  15370. + }
  15371. + s->status = s->noheader ? BUSY_STATE : INIT_STATE;
  15372. + strm->adler = 1;
  15373. + s->last_flush = Z_NO_FLUSH;
  15374. +
  15375. + _tr_init(s);
  15376. + lm_init(s);
  15377. +
  15378. + return Z_OK;
  15379. +}
  15380. +
  15381. +/* ========================================================================= */
  15382. +int ZEXPORT deflateParams(strm, level, strategy)
  15383. + z_streamp strm;
  15384. + int level;
  15385. + int strategy;
  15386. +{
  15387. + deflate_state *s;
  15388. + compress_func func;
  15389. + int err = Z_OK;
  15390. +
  15391. + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  15392. + s = strm->state;
  15393. +
  15394. + if (level == Z_DEFAULT_COMPRESSION) {
  15395. + level = 6;
  15396. + }
  15397. + if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  15398. + return Z_STREAM_ERROR;
  15399. + }
  15400. + func = configuration_table[s->level].func;
  15401. +
  15402. + if (func != configuration_table[level].func && strm->total_in != 0) {
  15403. + /* Flush the last buffer: */
  15404. + err = deflate(strm, Z_PARTIAL_FLUSH);
  15405. + }
  15406. + if (s->level != level) {
  15407. + s->level = level;
  15408. + s->max_lazy_match = configuration_table[level].max_lazy;
  15409. + s->good_match = configuration_table[level].good_length;
  15410. + s->nice_match = configuration_table[level].nice_length;
  15411. + s->max_chain_length = configuration_table[level].max_chain;
  15412. + }
  15413. + s->strategy = strategy;
  15414. + return err;
  15415. +}
  15416. +
  15417. +/* =========================================================================
  15418. + * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  15419. + * IN assertion: the stream state is correct and there is enough room in
  15420. + * pending_buf.
  15421. + */
  15422. +local void putShortMSB (s, b)
  15423. + deflate_state *s;
  15424. + uInt b;
  15425. +{
  15426. + put_byte(s, (Byte)(b >> 8));
  15427. + put_byte(s, (Byte)(b & 0xff));
  15428. +}
  15429. +
  15430. +/* =========================================================================
  15431. + * Flush as much pending output as possible. All deflate() output goes
  15432. + * through this function so some applications may wish to modify it
  15433. + * to avoid allocating a large strm->next_out buffer and copying into it.
  15434. + * (See also read_buf()).
  15435. + */
  15436. +local void flush_pending(strm)
  15437. + z_streamp strm;
  15438. +{
  15439. + unsigned len = strm->state->pending;
  15440. +
  15441. + if (len > strm->avail_out) len = strm->avail_out;
  15442. + if (len == 0) return;
  15443. +
  15444. + zmemcpy(strm->next_out, strm->state->pending_out, len);
  15445. + strm->next_out += len;
  15446. + strm->state->pending_out += len;
  15447. + strm->total_out += len;
  15448. + strm->avail_out -= len;
  15449. + strm->state->pending -= len;
  15450. + if (strm->state->pending == 0) {
  15451. + strm->state->pending_out = strm->state->pending_buf;
  15452. + }
  15453. +}
  15454. +
  15455. +/* ========================================================================= */
  15456. +int ZEXPORT deflate (strm, flush)
  15457. + z_streamp strm;
  15458. + int flush;
  15459. +{
  15460. + int old_flush; /* value of flush param for previous deflate call */
  15461. + deflate_state *s;
  15462. +
  15463. + if (strm == Z_NULL || strm->state == Z_NULL ||
  15464. + flush > Z_FINISH || flush < 0) {
  15465. + return Z_STREAM_ERROR;
  15466. + }
  15467. + s = strm->state;
  15468. +
  15469. + if (strm->next_out == Z_NULL ||
  15470. + (strm->next_in == Z_NULL && strm->avail_in != 0) ||
  15471. + (s->status == FINISH_STATE && flush != Z_FINISH)) {
  15472. + ERR_RETURN(strm, Z_STREAM_ERROR);
  15473. + }
  15474. + if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  15475. +
  15476. + s->strm = strm; /* just in case */
  15477. + old_flush = s->last_flush;
  15478. + s->last_flush = flush;
  15479. +
  15480. + /* Write the zlib header */
  15481. + if (s->status == INIT_STATE) {
  15482. +
  15483. + uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  15484. + uInt level_flags = (s->level-1) >> 1;
  15485. +
  15486. + if (level_flags > 3) level_flags = 3;
  15487. + header |= (level_flags << 6);
  15488. + if (s->strstart != 0) header |= PRESET_DICT;
  15489. + header += 31 - (header % 31);
  15490. +
  15491. + s->status = BUSY_STATE;
  15492. + putShortMSB(s, header);
  15493. +
  15494. + /* Save the adler32 of the preset dictionary: */
  15495. + if (s->strstart != 0) {
  15496. + putShortMSB(s, (uInt)(strm->adler >> 16));
  15497. + putShortMSB(s, (uInt)(strm->adler & 0xffff));
  15498. + }
  15499. + strm->adler = 1L;
  15500. + }
  15501. +
  15502. + /* Flush as much pending output as possible */
  15503. + if (s->pending != 0) {
  15504. + flush_pending(strm);
  15505. + if (strm->avail_out == 0) {
  15506. + /* Since avail_out is 0, deflate will be called again with
  15507. + * more output space, but possibly with both pending and
  15508. + * avail_in equal to zero. There won't be anything to do,
  15509. + * but this is not an error situation so make sure we
  15510. + * return OK instead of BUF_ERROR at next call of deflate:
  15511. + */
  15512. + s->last_flush = -1;
  15513. + return Z_OK;
  15514. + }
  15515. +
  15516. + /* Make sure there is something to do and avoid duplicate consecutive
  15517. + * flushes. For repeated and useless calls with Z_FINISH, we keep
  15518. + * returning Z_STREAM_END instead of Z_BUFF_ERROR.
  15519. + */
  15520. + } else if (strm->avail_in == 0 && flush <= old_flush &&
  15521. + flush != Z_FINISH) {
  15522. + ERR_RETURN(strm, Z_BUF_ERROR);
  15523. + }
  15524. +
  15525. + /* User must not provide more input after the first FINISH: */
  15526. + if (s->status == FINISH_STATE && strm->avail_in != 0) {
  15527. + ERR_RETURN(strm, Z_BUF_ERROR);
  15528. + }
  15529. +
  15530. + /* Start a new block or continue the current one.
  15531. + */
  15532. + if (strm->avail_in != 0 || s->lookahead != 0 ||
  15533. + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  15534. + block_state bstate;
  15535. +
  15536. + bstate = (*(configuration_table[s->level].func))(s, flush);
  15537. +
  15538. + if (bstate == finish_started || bstate == finish_done) {
  15539. + s->status = FINISH_STATE;
  15540. + }
  15541. + if (bstate == need_more || bstate == finish_started) {
  15542. + if (strm->avail_out == 0) {
  15543. + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  15544. + }
  15545. + return Z_OK;
  15546. + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  15547. + * of deflate should use the same flush parameter to make sure
  15548. + * that the flush is complete. So we don't have to output an
  15549. + * empty block here, this will be done at next call. This also
  15550. + * ensures that for a very small output buffer, we emit at most
  15551. + * one empty block.
  15552. + */
  15553. + }
  15554. + if (bstate == block_done) {
  15555. + if (flush == Z_PARTIAL_FLUSH) {
  15556. + _tr_align(s);
  15557. + } else { /* FULL_FLUSH or SYNC_FLUSH */
  15558. + _tr_stored_block(s, (char*)0, 0L, 0);
  15559. + /* For a full flush, this empty block will be recognized
  15560. + * as a special marker by inflate_sync().
  15561. + */
  15562. + if (flush == Z_FULL_FLUSH) {
  15563. + CLEAR_HASH(s); /* forget history */
  15564. + }
  15565. + }
  15566. + flush_pending(strm);
  15567. + if (strm->avail_out == 0) {
  15568. + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  15569. + return Z_OK;
  15570. + }
  15571. + }
  15572. + }
  15573. + Assert(strm->avail_out > 0, "bug2");
  15574. +
  15575. + if (flush != Z_FINISH) return Z_OK;
  15576. + if (s->noheader) return Z_STREAM_END;
  15577. +
  15578. + /* Write the zlib trailer (adler32) */
  15579. + putShortMSB(s, (uInt)(strm->adler >> 16));
  15580. + putShortMSB(s, (uInt)(strm->adler & 0xffff));
  15581. + flush_pending(strm);
  15582. + /* If avail_out is zero, the application will call deflate again
  15583. + * to flush the rest.
  15584. + */
  15585. + s->noheader = -1; /* write the trailer only once! */
  15586. + return s->pending != 0 ? Z_OK : Z_STREAM_END;
  15587. +}
  15588. +
  15589. +/* ========================================================================= */
  15590. +int ZEXPORT deflateEnd (strm)
  15591. + z_streamp strm;
  15592. +{
  15593. + int status;
  15594. +
  15595. + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  15596. +
  15597. + status = strm->state->status;
  15598. + if (status != INIT_STATE && status != BUSY_STATE &&
  15599. + status != FINISH_STATE) {
  15600. + return Z_STREAM_ERROR;
  15601. + }
  15602. +
  15603. + /* Deallocate in reverse order of allocations: */
  15604. + TRY_FREE(strm, strm->state->pending_buf);
  15605. + TRY_FREE(strm, strm->state->head);
  15606. + TRY_FREE(strm, strm->state->prev);
  15607. + TRY_FREE(strm, strm->state->window);
  15608. +
  15609. + ZFREE(strm, strm->state);
  15610. + strm->state = Z_NULL;
  15611. +
  15612. + return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  15613. +}
  15614. +
  15615. +/* =========================================================================
  15616. + * Copy the source state to the destination state.
  15617. + * To simplify the source, this is not supported for 16-bit MSDOS (which
  15618. + * doesn't have enough memory anyway to duplicate compression states).
  15619. + */
  15620. +int ZEXPORT deflateCopy (dest, source)
  15621. + z_streamp dest;
  15622. + z_streamp source;
  15623. +{
  15624. +#ifdef MAXSEG_64K
  15625. + return Z_STREAM_ERROR;
  15626. +#else
  15627. + deflate_state *ds;
  15628. + deflate_state *ss;
  15629. + ushf *overlay;
  15630. +
  15631. +
  15632. + if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
  15633. + return Z_STREAM_ERROR;
  15634. + }
  15635. +
  15636. + ss = source->state;
  15637. +
  15638. + *dest = *source;
  15639. +
  15640. + ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
  15641. + if (ds == Z_NULL) return Z_MEM_ERROR;
  15642. + dest->state = (struct internal_state FAR *) ds;
  15643. + *ds = *ss;
  15644. + ds->strm = dest;
  15645. +
  15646. + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
  15647. + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
  15648. + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
  15649. + overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
  15650. + ds->pending_buf = (uchf *) overlay;
  15651. +
  15652. + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
  15653. + ds->pending_buf == Z_NULL) {
  15654. + deflateEnd (dest);
  15655. + return Z_MEM_ERROR;
  15656. + }
  15657. + /* following zmemcpy do not work for 16-bit MSDOS */
  15658. + zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  15659. + zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
  15660. + zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
  15661. + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  15662. +
  15663. + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  15664. + ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
  15665. + ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
  15666. +
  15667. + ds->l_desc.dyn_tree = ds->dyn_ltree;
  15668. + ds->d_desc.dyn_tree = ds->dyn_dtree;
  15669. + ds->bl_desc.dyn_tree = ds->bl_tree;
  15670. +
  15671. + return Z_OK;
  15672. +#endif
  15673. +}
  15674. +
  15675. +/* ===========================================================================
  15676. + * Read a new buffer from the current input stream, update the adler32
  15677. + * and total number of bytes read. All deflate() input goes through
  15678. + * this function so some applications may wish to modify it to avoid
  15679. + * allocating a large strm->next_in buffer and copying from it.
  15680. + * (See also flush_pending()).
  15681. + */
  15682. +local int read_buf(strm, buf, size)
  15683. + z_streamp strm;
  15684. + Bytef *buf;
  15685. + unsigned size;
  15686. +{
  15687. + unsigned len = strm->avail_in;
  15688. +
  15689. + if (len > size) len = size;
  15690. + if (len == 0) return 0;
  15691. +
  15692. + strm->avail_in -= len;
  15693. +
  15694. + if (!strm->state->noheader) {
  15695. + strm->adler = adler32(strm->adler, strm->next_in, len);
  15696. + }
  15697. + zmemcpy(buf, strm->next_in, len);
  15698. + strm->next_in += len;
  15699. + strm->total_in += len;
  15700. +
  15701. + return (int)len;
  15702. +}
  15703. +
  15704. +/* ===========================================================================
  15705. + * Initialize the "longest match" routines for a new zlib stream
  15706. + */
  15707. +local void lm_init (s)
  15708. + deflate_state *s;
  15709. +{
  15710. + s->window_size = (ulg)2L*s->w_size;
  15711. +
  15712. + CLEAR_HASH(s);
  15713. +
  15714. + /* Set the default configuration parameters:
  15715. + */
  15716. + s->max_lazy_match = configuration_table[s->level].max_lazy;
  15717. + s->good_match = configuration_table[s->level].good_length;
  15718. + s->nice_match = configuration_table[s->level].nice_length;
  15719. + s->max_chain_length = configuration_table[s->level].max_chain;
  15720. +
  15721. + s->strstart = 0;
  15722. + s->block_start = 0L;
  15723. + s->lookahead = 0;
  15724. + s->match_length = s->prev_length = MIN_MATCH-1;
  15725. + s->match_available = 0;
  15726. + s->ins_h = 0;
  15727. +#ifdef ASMV
  15728. + match_init(); /* initialize the asm code */
  15729. +#endif
  15730. +}
  15731. +
  15732. +/* ===========================================================================
  15733. + * Set match_start to the longest match starting at the given string and
  15734. + * return its length. Matches shorter or equal to prev_length are discarded,
  15735. + * in which case the result is equal to prev_length and match_start is
  15736. + * garbage.
  15737. + * IN assertions: cur_match is the head of the hash chain for the current
  15738. + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  15739. + * OUT assertion: the match length is not greater than s->lookahead.
  15740. + */
  15741. +#ifndef ASMV
  15742. +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  15743. + * match.S. The code will be functionally equivalent.
  15744. + */
  15745. +#ifndef FASTEST
  15746. +local uInt longest_match(s, cur_match)
  15747. + deflate_state *s;
  15748. + IPos cur_match; /* current match */
  15749. +{
  15750. + unsigned chain_length = s->max_chain_length;/* max hash chain length */
  15751. + register Bytef *scan = s->window + s->strstart; /* current string */
  15752. + register Bytef *match; /* matched string */
  15753. + register int len; /* length of current match */
  15754. + int best_len = s->prev_length; /* best match length so far */
  15755. + int nice_match = s->nice_match; /* stop if match long enough */
  15756. + IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  15757. + s->strstart - (IPos)MAX_DIST(s) : NIL;
  15758. + /* Stop when cur_match becomes <= limit. To simplify the code,
  15759. + * we prevent matches with the string of window index 0.
  15760. + */
  15761. + Posf *prev = s->prev;
  15762. + uInt wmask = s->w_mask;
  15763. +
  15764. +#ifdef UNALIGNED_OK
  15765. + /* Compare two bytes at a time. Note: this is not always beneficial.
  15766. + * Try with and without -DUNALIGNED_OK to check.
  15767. + */
  15768. + register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  15769. + register ush scan_start = *(ushf*)scan;
  15770. + register ush scan_end = *(ushf*)(scan+best_len-1);
  15771. +#else
  15772. + register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  15773. + register Byte scan_end1 = scan[best_len-1];
  15774. + register Byte scan_end = scan[best_len];
  15775. +#endif
  15776. +
  15777. + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  15778. + * It is easy to get rid of this optimization if necessary.
  15779. + */
  15780. + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  15781. +
  15782. + /* Do not waste too much time if we already have a good match: */
  15783. + if (s->prev_length >= s->good_match) {
  15784. + chain_length >>= 2;
  15785. + }
  15786. + /* Do not look for matches beyond the end of the input. This is necessary
  15787. + * to make deflate deterministic.
  15788. + */
  15789. + if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  15790. +
  15791. + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  15792. +
  15793. + do {
  15794. + Assert(cur_match < s->strstart, "no future");
  15795. + match = s->window + cur_match;
  15796. +
  15797. + /* Skip to next match if the match length cannot increase
  15798. + * or if the match length is less than 2:
  15799. + */
  15800. +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  15801. + /* This code assumes sizeof(unsigned short) == 2. Do not use
  15802. + * UNALIGNED_OK if your compiler uses a different size.
  15803. + */
  15804. + if (*(ushf*)(match+best_len-1) != scan_end ||
  15805. + *(ushf*)match != scan_start) continue;
  15806. +
  15807. + /* It is not necessary to compare scan[2] and match[2] since they are
  15808. + * always equal when the other bytes match, given that the hash keys
  15809. + * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  15810. + * strstart+3, +5, ... up to strstart+257. We check for insufficient
  15811. + * lookahead only every 4th comparison; the 128th check will be made
  15812. + * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  15813. + * necessary to put more guard bytes at the end of the window, or
  15814. + * to check more often for insufficient lookahead.
  15815. + */
  15816. + Assert(scan[2] == match[2], "scan[2]?");
  15817. + scan++, match++;
  15818. + do {
  15819. + } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15820. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15821. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15822. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15823. + scan < strend);
  15824. + /* The funny "do {}" generates better code on most compilers */
  15825. +
  15826. + /* Here, scan <= window+strstart+257 */
  15827. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15828. + if (*scan == *match) scan++;
  15829. +
  15830. + len = (MAX_MATCH - 1) - (int)(strend-scan);
  15831. + scan = strend - (MAX_MATCH-1);
  15832. +
  15833. +#else /* UNALIGNED_OK */
  15834. +
  15835. + if (match[best_len] != scan_end ||
  15836. + match[best_len-1] != scan_end1 ||
  15837. + *match != *scan ||
  15838. + *++match != scan[1]) continue;
  15839. +
  15840. + /* The check at best_len-1 can be removed because it will be made
  15841. + * again later. (This heuristic is not always a win.)
  15842. + * It is not necessary to compare scan[2] and match[2] since they
  15843. + * are always equal when the other bytes match, given that
  15844. + * the hash keys are equal and that HASH_BITS >= 8.
  15845. + */
  15846. + scan += 2, match++;
  15847. + Assert(*scan == *match, "match[2]?");
  15848. +
  15849. + /* We check for insufficient lookahead only every 8th comparison;
  15850. + * the 256th check will be made at strstart+258.
  15851. + */
  15852. + do {
  15853. + } while (*++scan == *++match && *++scan == *++match &&
  15854. + *++scan == *++match && *++scan == *++match &&
  15855. + *++scan == *++match && *++scan == *++match &&
  15856. + *++scan == *++match && *++scan == *++match &&
  15857. + scan < strend);
  15858. +
  15859. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15860. +
  15861. + len = MAX_MATCH - (int)(strend - scan);
  15862. + scan = strend - MAX_MATCH;
  15863. +
  15864. +#endif /* UNALIGNED_OK */
  15865. +
  15866. + if (len > best_len) {
  15867. + s->match_start = cur_match;
  15868. + best_len = len;
  15869. + if (len >= nice_match) break;
  15870. +#ifdef UNALIGNED_OK
  15871. + scan_end = *(ushf*)(scan+best_len-1);
  15872. +#else
  15873. + scan_end1 = scan[best_len-1];
  15874. + scan_end = scan[best_len];
  15875. +#endif
  15876. + }
  15877. + } while ((cur_match = prev[cur_match & wmask]) > limit
  15878. + && --chain_length != 0);
  15879. +
  15880. + if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  15881. + return s->lookahead;
  15882. +}
  15883. +
  15884. +#else /* FASTEST */
  15885. +/* ---------------------------------------------------------------------------
  15886. + * Optimized version for level == 1 only
  15887. + */
  15888. +local uInt longest_match(s, cur_match)
  15889. + deflate_state *s;
  15890. + IPos cur_match; /* current match */
  15891. +{
  15892. + register Bytef *scan = s->window + s->strstart; /* current string */
  15893. + register Bytef *match; /* matched string */
  15894. + register int len; /* length of current match */
  15895. + register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  15896. +
  15897. + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  15898. + * It is easy to get rid of this optimization if necessary.
  15899. + */
  15900. + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  15901. +
  15902. + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  15903. +
  15904. + Assert(cur_match < s->strstart, "no future");
  15905. +
  15906. + match = s->window + cur_match;
  15907. +
  15908. + /* Return failure if the match length is less than 2:
  15909. + */
  15910. + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  15911. +
  15912. + /* The check at best_len-1 can be removed because it will be made
  15913. + * again later. (This heuristic is not always a win.)
  15914. + * It is not necessary to compare scan[2] and match[2] since they
  15915. + * are always equal when the other bytes match, given that
  15916. + * the hash keys are equal and that HASH_BITS >= 8.
  15917. + */
  15918. + scan += 2, match += 2;
  15919. + Assert(*scan == *match, "match[2]?");
  15920. +
  15921. + /* We check for insufficient lookahead only every 8th comparison;
  15922. + * the 256th check will be made at strstart+258.
  15923. + */
  15924. + do {
  15925. + } while (*++scan == *++match && *++scan == *++match &&
  15926. + *++scan == *++match && *++scan == *++match &&
  15927. + *++scan == *++match && *++scan == *++match &&
  15928. + *++scan == *++match && *++scan == *++match &&
  15929. + scan < strend);
  15930. +
  15931. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15932. +
  15933. + len = MAX_MATCH - (int)(strend - scan);
  15934. +
  15935. + if (len < MIN_MATCH) return MIN_MATCH - 1;
  15936. +
  15937. + s->match_start = cur_match;
  15938. + return len <= s->lookahead ? len : s->lookahead;
  15939. +}
  15940. +#endif /* FASTEST */
  15941. +#endif /* ASMV */
  15942. +
  15943. +#ifdef DEBUG
  15944. +/* ===========================================================================
  15945. + * Check that the match at match_start is indeed a match.
  15946. + */
  15947. +local void check_match(s, start, match, length)
  15948. + deflate_state *s;
  15949. + IPos start, match;
  15950. + int length;
  15951. +{
  15952. + /* check that the match is indeed a match */
  15953. + if (zmemcmp(s->window + match,
  15954. + s->window + start, length) != EQUAL) {
  15955. + fprintf(stderr, " start %u, match %u, length %d\n",
  15956. + start, match, length);
  15957. + do {
  15958. + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  15959. + } while (--length != 0);
  15960. + z_error("invalid match");
  15961. + }
  15962. + if (z_verbose > 1) {
  15963. + fprintf(stderr,"\\[%d,%d]", start-match, length);
  15964. + do { putc(s->window[start++], stderr); } while (--length != 0);
  15965. + }
  15966. +}
  15967. +#else
  15968. +# define check_match(s, start, match, length)
  15969. +#endif
  15970. +
  15971. +/* ===========================================================================
  15972. + * Fill the window when the lookahead becomes insufficient.
  15973. + * Updates strstart and lookahead.
  15974. + *
  15975. + * IN assertion: lookahead < MIN_LOOKAHEAD
  15976. + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  15977. + * At least one byte has been read, or avail_in == 0; reads are
  15978. + * performed for at least two bytes (required for the zip translate_eol
  15979. + * option -- not supported here).
  15980. + */
  15981. +local void fill_window(s)
  15982. + deflate_state *s;
  15983. +{
  15984. + register unsigned n, m;
  15985. + register Posf *p;
  15986. + unsigned more; /* Amount of free space at the end of the window. */
  15987. + uInt wsize = s->w_size;
  15988. +
  15989. + do {
  15990. + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  15991. +
  15992. + /* Deal with !@#$% 64K limit: */
  15993. + if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  15994. + more = wsize;
  15995. +
  15996. + } else if (more == (unsigned)(-1)) {
  15997. + /* Very unlikely, but possible on 16 bit machine if strstart == 0
  15998. + * and lookahead == 1 (input done one byte at time)
  15999. + */
  16000. + more--;
  16001. +
  16002. + /* If the window is almost full and there is insufficient lookahead,
  16003. + * move the upper half to the lower one to make room in the upper half.
  16004. + */
  16005. + } else if (s->strstart >= wsize+MAX_DIST(s)) {
  16006. +
  16007. + zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
  16008. + s->match_start -= wsize;
  16009. + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  16010. + s->block_start -= (long) wsize;
  16011. +
  16012. + /* Slide the hash table (could be avoided with 32 bit values
  16013. + at the expense of memory usage). We slide even when level == 0
  16014. + to keep the hash table consistent if we switch back to level > 0
  16015. + later. (Using level 0 permanently is not an optimal usage of
  16016. + zlib, so we don't care about this pathological case.)
  16017. + */
  16018. + n = s->hash_size;
  16019. + p = &s->head[n];
  16020. + do {
  16021. + m = *--p;
  16022. + *p = (Pos)(m >= wsize ? m-wsize : NIL);
  16023. + } while (--n);
  16024. +
  16025. + n = wsize;
  16026. +#ifndef FASTEST
  16027. + p = &s->prev[n];
  16028. + do {
  16029. + m = *--p;
  16030. + *p = (Pos)(m >= wsize ? m-wsize : NIL);
  16031. + /* If n is not on any hash chain, prev[n] is garbage but
  16032. + * its value will never be used.
  16033. + */
  16034. + } while (--n);
  16035. +#endif
  16036. + more += wsize;
  16037. + }
  16038. + if (s->strm->avail_in == 0) return;
  16039. +
  16040. + /* If there was no sliding:
  16041. + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  16042. + * more == window_size - lookahead - strstart
  16043. + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  16044. + * => more >= window_size - 2*WSIZE + 2
  16045. + * In the BIG_MEM or MMAP case (not yet supported),
  16046. + * window_size == input_size + MIN_LOOKAHEAD &&
  16047. + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  16048. + * Otherwise, window_size == 2*WSIZE so more >= 2.
  16049. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  16050. + */
  16051. + Assert(more >= 2, "more < 2");
  16052. +
  16053. + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  16054. + s->lookahead += n;
  16055. +
  16056. + /* Initialize the hash value now that we have some input: */
  16057. + if (s->lookahead >= MIN_MATCH) {
  16058. + s->ins_h = s->window[s->strstart];
  16059. + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  16060. +#if MIN_MATCH != 3
  16061. + Call UPDATE_HASH() MIN_MATCH-3 more times
  16062. +#endif
  16063. + }
  16064. + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  16065. + * but this is not important since only literal bytes will be emitted.
  16066. + */
  16067. +
  16068. + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  16069. +}
  16070. +
  16071. +/* ===========================================================================
  16072. + * Flush the current block, with given end-of-file flag.
  16073. + * IN assertion: strstart is set to the end of the current match.
  16074. + */
  16075. +#define FLUSH_BLOCK_ONLY(s, eof) { \
  16076. + _tr_flush_block(s, (s->block_start >= 0L ? \
  16077. + (charf *)&s->window[(unsigned)s->block_start] : \
  16078. + (charf *)Z_NULL), \
  16079. + (ulg)((long)s->strstart - s->block_start), \
  16080. + (eof)); \
  16081. + s->block_start = s->strstart; \
  16082. + flush_pending(s->strm); \
  16083. + Tracev((stderr,"[FLUSH]")); \
  16084. +}
  16085. +
  16086. +/* Same but force premature exit if necessary. */
  16087. +#define FLUSH_BLOCK(s, eof) { \
  16088. + FLUSH_BLOCK_ONLY(s, eof); \
  16089. + if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  16090. +}
  16091. +
  16092. +/* ===========================================================================
  16093. + * Copy without compression as much as possible from the input stream, return
  16094. + * the current block state.
  16095. + * This function does not insert new strings in the dictionary since
  16096. + * uncompressible data is probably not useful. This function is used
  16097. + * only for the level=0 compression option.
  16098. + * NOTE: this function should be optimized to avoid extra copying from
  16099. + * window to pending_buf.
  16100. + */
  16101. +local block_state deflate_stored(s, flush)
  16102. + deflate_state *s;
  16103. + int flush;
  16104. +{
  16105. + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  16106. + * to pending_buf_size, and each stored block has a 5 byte header:
  16107. + */
  16108. + ulg max_block_size = 0xffff;
  16109. + ulg max_start;
  16110. +
  16111. + if (max_block_size > s->pending_buf_size - 5) {
  16112. + max_block_size = s->pending_buf_size - 5;
  16113. + }
  16114. +
  16115. + /* Copy as much as possible from input to output: */
  16116. + for (;;) {
  16117. + /* Fill the window as much as possible: */
  16118. + if (s->lookahead <= 1) {
  16119. +
  16120. + Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  16121. + s->block_start >= (long)s->w_size, "slide too late");
  16122. +
  16123. + fill_window(s);
  16124. + if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  16125. +
  16126. + if (s->lookahead == 0) break; /* flush the current block */
  16127. + }
  16128. + Assert(s->block_start >= 0L, "block gone");
  16129. +
  16130. + s->strstart += s->lookahead;
  16131. + s->lookahead = 0;
  16132. +
  16133. + /* Emit a stored block if pending_buf will be full: */
  16134. + max_start = s->block_start + max_block_size;
  16135. + if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  16136. + /* strstart == 0 is possible when wraparound on 16-bit machine */
  16137. + s->lookahead = (uInt)(s->strstart - max_start);
  16138. + s->strstart = (uInt)max_start;
  16139. + FLUSH_BLOCK(s, 0);
  16140. + }
  16141. + /* Flush if we may have to slide, otherwise block_start may become
  16142. + * negative and the data will be gone:
  16143. + */
  16144. + if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  16145. + FLUSH_BLOCK(s, 0);
  16146. + }
  16147. + }
  16148. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16149. + return flush == Z_FINISH ? finish_done : block_done;
  16150. +}
  16151. +
  16152. +/* ===========================================================================
  16153. + * Compress as much as possible from the input stream, return the current
  16154. + * block state.
  16155. + * This function does not perform lazy evaluation of matches and inserts
  16156. + * new strings in the dictionary only for unmatched strings or for short
  16157. + * matches. It is used only for the fast compression options.
  16158. + */
  16159. +local block_state deflate_fast(s, flush)
  16160. + deflate_state *s;
  16161. + int flush;
  16162. +{
  16163. + IPos hash_head = NIL; /* head of the hash chain */
  16164. + int bflush; /* set if current block must be flushed */
  16165. +
  16166. + for (;;) {
  16167. + /* Make sure that we always have enough lookahead, except
  16168. + * at the end of the input file. We need MAX_MATCH bytes
  16169. + * for the next match, plus MIN_MATCH bytes to insert the
  16170. + * string following the next match.
  16171. + */
  16172. + if (s->lookahead < MIN_LOOKAHEAD) {
  16173. + fill_window(s);
  16174. + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  16175. + return need_more;
  16176. + }
  16177. + if (s->lookahead == 0) break; /* flush the current block */
  16178. + }
  16179. +
  16180. + /* Insert the string window[strstart .. strstart+2] in the
  16181. + * dictionary, and set hash_head to the head of the hash chain:
  16182. + */
  16183. + if (s->lookahead >= MIN_MATCH) {
  16184. + INSERT_STRING(s, s->strstart, hash_head);
  16185. + }
  16186. +
  16187. + /* Find the longest match, discarding those <= prev_length.
  16188. + * At this point we have always match_length < MIN_MATCH
  16189. + */
  16190. + if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  16191. + /* To simplify the code, we prevent matches with the string
  16192. + * of window index 0 (in particular we have to avoid a match
  16193. + * of the string with itself at the start of the input file).
  16194. + */
  16195. + if (s->strategy != Z_HUFFMAN_ONLY) {
  16196. + s->match_length = longest_match (s, hash_head);
  16197. + }
  16198. + /* longest_match() sets match_start */
  16199. + }
  16200. + if (s->match_length >= MIN_MATCH) {
  16201. + check_match(s, s->strstart, s->match_start, s->match_length);
  16202. +
  16203. + _tr_tally_dist(s, s->strstart - s->match_start,
  16204. + s->match_length - MIN_MATCH, bflush);
  16205. +
  16206. + s->lookahead -= s->match_length;
  16207. +
  16208. + /* Insert new strings in the hash table only if the match length
  16209. + * is not too large. This saves time but degrades compression.
  16210. + */
  16211. +#ifndef FASTEST
  16212. + if (s->match_length <= s->max_insert_length &&
  16213. + s->lookahead >= MIN_MATCH) {
  16214. + s->match_length--; /* string at strstart already in hash table */
  16215. + do {
  16216. + s->strstart++;
  16217. + INSERT_STRING(s, s->strstart, hash_head);
  16218. + /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  16219. + * always MIN_MATCH bytes ahead.
  16220. + */
  16221. + } while (--s->match_length != 0);
  16222. + s->strstart++;
  16223. + } else
  16224. +#endif
  16225. + {
  16226. + s->strstart += s->match_length;
  16227. + s->match_length = 0;
  16228. + s->ins_h = s->window[s->strstart];
  16229. + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  16230. +#if MIN_MATCH != 3
  16231. + Call UPDATE_HASH() MIN_MATCH-3 more times
  16232. +#endif
  16233. + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  16234. + * matter since it will be recomputed at next deflate call.
  16235. + */
  16236. + }
  16237. + } else {
  16238. + /* No match, output a literal byte */
  16239. + Tracevv((stderr,"%c", s->window[s->strstart]));
  16240. + _tr_tally_lit (s, s->window[s->strstart], bflush);
  16241. + s->lookahead--;
  16242. + s->strstart++;
  16243. + }
  16244. + if (bflush) FLUSH_BLOCK(s, 0);
  16245. + }
  16246. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16247. + return flush == Z_FINISH ? finish_done : block_done;
  16248. +}
  16249. +
  16250. +/* ===========================================================================
  16251. + * Same as above, but achieves better compression. We use a lazy
  16252. + * evaluation for matches: a match is finally adopted only if there is
  16253. + * no better match at the next window position.
  16254. + */
  16255. +local block_state deflate_slow(s, flush)
  16256. + deflate_state *s;
  16257. + int flush;
  16258. +{
  16259. + IPos hash_head = NIL; /* head of hash chain */
  16260. + int bflush; /* set if current block must be flushed */
  16261. +
  16262. + /* Process the input block. */
  16263. + for (;;) {
  16264. + /* Make sure that we always have enough lookahead, except
  16265. + * at the end of the input file. We need MAX_MATCH bytes
  16266. + * for the next match, plus MIN_MATCH bytes to insert the
  16267. + * string following the next match.
  16268. + */
  16269. + if (s->lookahead < MIN_LOOKAHEAD) {
  16270. + fill_window(s);
  16271. + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  16272. + return need_more;
  16273. + }
  16274. + if (s->lookahead == 0) break; /* flush the current block */
  16275. + }
  16276. +
  16277. + /* Insert the string window[strstart .. strstart+2] in the
  16278. + * dictionary, and set hash_head to the head of the hash chain:
  16279. + */
  16280. + if (s->lookahead >= MIN_MATCH) {
  16281. + INSERT_STRING(s, s->strstart, hash_head);
  16282. + }
  16283. +
  16284. + /* Find the longest match, discarding those <= prev_length.
  16285. + */
  16286. + s->prev_length = s->match_length, s->prev_match = s->match_start;
  16287. + s->match_length = MIN_MATCH-1;
  16288. +
  16289. + if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  16290. + s->strstart - hash_head <= MAX_DIST(s)) {
  16291. + /* To simplify the code, we prevent matches with the string
  16292. + * of window index 0 (in particular we have to avoid a match
  16293. + * of the string with itself at the start of the input file).
  16294. + */
  16295. + if (s->strategy != Z_HUFFMAN_ONLY) {
  16296. + s->match_length = longest_match (s, hash_head);
  16297. + }
  16298. + /* longest_match() sets match_start */
  16299. +
  16300. + if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
  16301. + (s->match_length == MIN_MATCH &&
  16302. + s->strstart - s->match_start > TOO_FAR))) {
  16303. +
  16304. + /* If prev_match is also MIN_MATCH, match_start is garbage
  16305. + * but we will ignore the current match anyway.
  16306. + */
  16307. + s->match_length = MIN_MATCH-1;
  16308. + }
  16309. + }
  16310. + /* If there was a match at the previous step and the current
  16311. + * match is not better, output the previous match:
  16312. + */
  16313. + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  16314. + uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  16315. + /* Do not insert strings in hash table beyond this. */
  16316. +
  16317. + check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  16318. +
  16319. + _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  16320. + s->prev_length - MIN_MATCH, bflush);
  16321. +
  16322. + /* Insert in hash table all strings up to the end of the match.
  16323. + * strstart-1 and strstart are already inserted. If there is not
  16324. + * enough lookahead, the last two strings are not inserted in
  16325. + * the hash table.
  16326. + */
  16327. + s->lookahead -= s->prev_length-1;
  16328. + s->prev_length -= 2;
  16329. + do {
  16330. + if (++s->strstart <= max_insert) {
  16331. + INSERT_STRING(s, s->strstart, hash_head);
  16332. + }
  16333. + } while (--s->prev_length != 0);
  16334. + s->match_available = 0;
  16335. + s->match_length = MIN_MATCH-1;
  16336. + s->strstart++;
  16337. +
  16338. + if (bflush) FLUSH_BLOCK(s, 0);
  16339. +
  16340. + } else if (s->match_available) {
  16341. + /* If there was no match at the previous position, output a
  16342. + * single literal. If there was a match but the current match
  16343. + * is longer, truncate the previous match to a single literal.
  16344. + */
  16345. + Tracevv((stderr,"%c", s->window[s->strstart-1]));
  16346. + _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  16347. + if (bflush) {
  16348. + FLUSH_BLOCK_ONLY(s, 0);
  16349. + }
  16350. + s->strstart++;
  16351. + s->lookahead--;
  16352. + if (s->strm->avail_out == 0) return need_more;
  16353. + } else {
  16354. + /* There is no previous match to compare with, wait for
  16355. + * the next step to decide.
  16356. + */
  16357. + s->match_available = 1;
  16358. + s->strstart++;
  16359. + s->lookahead--;
  16360. + }
  16361. + }
  16362. + Assert (flush != Z_NO_FLUSH, "no flush?");
  16363. + if (s->match_available) {
  16364. + Tracevv((stderr,"%c", s->window[s->strstart-1]));
  16365. + _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  16366. + s->match_available = 0;
  16367. + }
  16368. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16369. + return flush == Z_FINISH ? finish_done : block_done;
  16370. +}
  16371. --- /dev/null Tue Mar 11 13:02:56 2003
  16372. +++ linux/net/ipsec/deflate.h Mon Feb 9 13:51:03 2004
  16373. @@ -0,0 +1,318 @@
  16374. +/* deflate.h -- internal compression state
  16375. + * Copyright (C) 1995-2002 Jean-loup Gailly
  16376. + * For conditions of distribution and use, see copyright notice in zlib.h
  16377. + */
  16378. +
  16379. +/* WARNING: this file should *not* be used by applications. It is
  16380. + part of the implementation of the compression library and is
  16381. + subject to change. Applications should only use zlib.h.
  16382. + */
  16383. +
  16384. +/* @(#) $Id: deflate.h,v 1.5 2004/07/10 07:48:38 mcr Exp $ */
  16385. +
  16386. +#ifndef _DEFLATE_H
  16387. +#define _DEFLATE_H
  16388. +
  16389. +#include "zlib/zutil.h"
  16390. +
  16391. +/* ===========================================================================
  16392. + * Internal compression state.
  16393. + */
  16394. +
  16395. +#define LENGTH_CODES 29
  16396. +/* number of length codes, not counting the special END_BLOCK code */
  16397. +
  16398. +#define LITERALS 256
  16399. +/* number of literal bytes 0..255 */
  16400. +
  16401. +#define L_CODES (LITERALS+1+LENGTH_CODES)
  16402. +/* number of Literal or Length codes, including the END_BLOCK code */
  16403. +
  16404. +#define D_CODES 30
  16405. +/* number of distance codes */
  16406. +
  16407. +#define BL_CODES 19
  16408. +/* number of codes used to transfer the bit lengths */
  16409. +
  16410. +#define HEAP_SIZE (2*L_CODES+1)
  16411. +/* maximum heap size */
  16412. +
  16413. +#define MAX_BITS 15
  16414. +/* All codes must not exceed MAX_BITS bits */
  16415. +
  16416. +#define INIT_STATE 42
  16417. +#define BUSY_STATE 113
  16418. +#define FINISH_STATE 666
  16419. +/* Stream status */
  16420. +
  16421. +
  16422. +/* Data structure describing a single value and its code string. */
  16423. +typedef struct ct_data_s {
  16424. + union {
  16425. + ush freq; /* frequency count */
  16426. + ush code; /* bit string */
  16427. + } fc;
  16428. + union {
  16429. + ush dad; /* father node in Huffman tree */
  16430. + ush len; /* length of bit string */
  16431. + } dl;
  16432. +} FAR ct_data;
  16433. +
  16434. +#define Freq fc.freq
  16435. +#define Code fc.code
  16436. +#define Dad dl.dad
  16437. +#define Len dl.len
  16438. +
  16439. +typedef struct static_tree_desc_s static_tree_desc;
  16440. +
  16441. +typedef struct tree_desc_s {
  16442. + ct_data *dyn_tree; /* the dynamic tree */
  16443. + int max_code; /* largest code with non zero frequency */
  16444. + static_tree_desc *stat_desc; /* the corresponding static tree */
  16445. +} FAR tree_desc;
  16446. +
  16447. +typedef ush Pos;
  16448. +typedef Pos FAR Posf;
  16449. +typedef unsigned IPos;
  16450. +
  16451. +/* A Pos is an index in the character window. We use short instead of int to
  16452. + * save space in the various tables. IPos is used only for parameter passing.
  16453. + */
  16454. +
  16455. +typedef struct internal_state {
  16456. + z_streamp strm; /* pointer back to this zlib stream */
  16457. + int status; /* as the name implies */
  16458. + Bytef *pending_buf; /* output still pending */
  16459. + ulg pending_buf_size; /* size of pending_buf */
  16460. + Bytef *pending_out; /* next pending byte to output to the stream */
  16461. + int pending; /* nb of bytes in the pending buffer */
  16462. + int noheader; /* suppress zlib header and adler32 */
  16463. + Byte data_type; /* UNKNOWN, BINARY or ASCII */
  16464. + Byte method; /* STORED (for zip only) or DEFLATED */
  16465. + int last_flush; /* value of flush param for previous deflate call */
  16466. +
  16467. + /* used by deflate.c: */
  16468. +
  16469. + uInt w_size; /* LZ77 window size (32K by default) */
  16470. + uInt w_bits; /* log2(w_size) (8..16) */
  16471. + uInt w_mask; /* w_size - 1 */
  16472. +
  16473. + Bytef *window;
  16474. + /* Sliding window. Input bytes are read into the second half of the window,
  16475. + * and move to the first half later to keep a dictionary of at least wSize
  16476. + * bytes. With this organization, matches are limited to a distance of
  16477. + * wSize-MAX_MATCH bytes, but this ensures that IO is always
  16478. + * performed with a length multiple of the block size. Also, it limits
  16479. + * the window size to 64K, which is quite useful on MSDOS.
  16480. + * To do: use the user input buffer as sliding window.
  16481. + */
  16482. +
  16483. + ulg window_size;
  16484. + /* Actual size of window: 2*wSize, except when the user input buffer
  16485. + * is directly used as sliding window.
  16486. + */
  16487. +
  16488. + Posf *prev;
  16489. + /* Link to older string with same hash index. To limit the size of this
  16490. + * array to 64K, this link is maintained only for the last 32K strings.
  16491. + * An index in this array is thus a window index modulo 32K.
  16492. + */
  16493. +
  16494. + Posf *head; /* Heads of the hash chains or NIL. */
  16495. +
  16496. + uInt ins_h; /* hash index of string to be inserted */
  16497. + uInt hash_size; /* number of elements in hash table */
  16498. + uInt hash_bits; /* log2(hash_size) */
  16499. + uInt hash_mask; /* hash_size-1 */
  16500. +
  16501. + uInt hash_shift;
  16502. + /* Number of bits by which ins_h must be shifted at each input
  16503. + * step. It must be such that after MIN_MATCH steps, the oldest
  16504. + * byte no longer takes part in the hash key, that is:
  16505. + * hash_shift * MIN_MATCH >= hash_bits
  16506. + */
  16507. +
  16508. + long block_start;
  16509. + /* Window position at the beginning of the current output block. Gets
  16510. + * negative when the window is moved backwards.
  16511. + */
  16512. +
  16513. + uInt match_length; /* length of best match */
  16514. + IPos prev_match; /* previous match */
  16515. + int match_available; /* set if previous match exists */
  16516. + uInt strstart; /* start of string to insert */
  16517. + uInt match_start; /* start of matching string */
  16518. + uInt lookahead; /* number of valid bytes ahead in window */
  16519. +
  16520. + uInt prev_length;
  16521. + /* Length of the best match at previous step. Matches not greater than this
  16522. + * are discarded. This is used in the lazy match evaluation.
  16523. + */
  16524. +
  16525. + uInt max_chain_length;
  16526. + /* To speed up deflation, hash chains are never searched beyond this
  16527. + * length. A higher limit improves compression ratio but degrades the
  16528. + * speed.
  16529. + */
  16530. +
  16531. + uInt max_lazy_match;
  16532. + /* Attempt to find a better match only when the current match is strictly
  16533. + * smaller than this value. This mechanism is used only for compression
  16534. + * levels >= 4.
  16535. + */
  16536. +# define max_insert_length max_lazy_match
  16537. + /* Insert new strings in the hash table only if the match length is not
  16538. + * greater than this length. This saves time but degrades compression.
  16539. + * max_insert_length is used only for compression levels <= 3.
  16540. + */
  16541. +
  16542. + int level; /* compression level (1..9) */
  16543. + int strategy; /* favor or force Huffman coding*/
  16544. +
  16545. + uInt good_match;
  16546. + /* Use a faster search when the previous match is longer than this */
  16547. +
  16548. + int nice_match; /* Stop searching when current match exceeds this */
  16549. +
  16550. + /* used by trees.c: */
  16551. + /* Didn't use ct_data typedef below to supress compiler warning */
  16552. + struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  16553. + struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  16554. + struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  16555. +
  16556. + struct tree_desc_s l_desc; /* desc. for literal tree */
  16557. + struct tree_desc_s d_desc; /* desc. for distance tree */
  16558. + struct tree_desc_s bl_desc; /* desc. for bit length tree */
  16559. +
  16560. + ush bl_count[MAX_BITS+1];
  16561. + /* number of codes at each bit length for an optimal tree */
  16562. +
  16563. + int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  16564. + int heap_len; /* number of elements in the heap */
  16565. + int heap_max; /* element of largest frequency */
  16566. + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  16567. + * The same heap array is used to build all trees.
  16568. + */
  16569. +
  16570. + uch depth[2*L_CODES+1];
  16571. + /* Depth of each subtree used as tie breaker for trees of equal frequency
  16572. + */
  16573. +
  16574. + uchf *l_buf; /* buffer for literals or lengths */
  16575. +
  16576. + uInt lit_bufsize;
  16577. + /* Size of match buffer for literals/lengths. There are 4 reasons for
  16578. + * limiting lit_bufsize to 64K:
  16579. + * - frequencies can be kept in 16 bit counters
  16580. + * - if compression is not successful for the first block, all input
  16581. + * data is still in the window so we can still emit a stored block even
  16582. + * when input comes from standard input. (This can also be done for
  16583. + * all blocks if lit_bufsize is not greater than 32K.)
  16584. + * - if compression is not successful for a file smaller than 64K, we can
  16585. + * even emit a stored file instead of a stored block (saving 5 bytes).
  16586. + * This is applicable only for zip (not gzip or zlib).
  16587. + * - creating new Huffman trees less frequently may not provide fast
  16588. + * adaptation to changes in the input data statistics. (Take for
  16589. + * example a binary file with poorly compressible code followed by
  16590. + * a highly compressible string table.) Smaller buffer sizes give
  16591. + * fast adaptation but have of course the overhead of transmitting
  16592. + * trees more frequently.
  16593. + * - I can't count above 4
  16594. + */
  16595. +
  16596. + uInt last_lit; /* running index in l_buf */
  16597. +
  16598. + ushf *d_buf;
  16599. + /* Buffer for distances. To simplify the code, d_buf and l_buf have
  16600. + * the same number of elements. To use different lengths, an extra flag
  16601. + * array would be necessary.
  16602. + */
  16603. +
  16604. + ulg opt_len; /* bit length of current block with optimal trees */
  16605. + ulg static_len; /* bit length of current block with static trees */
  16606. + uInt matches; /* number of string matches in current block */
  16607. + int last_eob_len; /* bit length of EOB code for last block */
  16608. +
  16609. +#ifdef DEBUG
  16610. + ulg compressed_len; /* total bit length of compressed file mod 2^32 */
  16611. + ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
  16612. +#endif
  16613. +
  16614. + ush bi_buf;
  16615. + /* Output buffer. bits are inserted starting at the bottom (least
  16616. + * significant bits).
  16617. + */
  16618. + int bi_valid;
  16619. + /* Number of valid bits in bi_buf. All bits above the last valid bit
  16620. + * are always zero.
  16621. + */
  16622. +
  16623. +} FAR deflate_state;
  16624. +
  16625. +/* Output a byte on the stream.
  16626. + * IN assertion: there is enough room in pending_buf.
  16627. + */
  16628. +#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
  16629. +
  16630. +
  16631. +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  16632. +/* Minimum amount of lookahead, except at the end of the input file.
  16633. + * See deflate.c for comments about the MIN_MATCH+1.
  16634. + */
  16635. +
  16636. +#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
  16637. +/* In order to simplify the code, particularly on 16 bit machines, match
  16638. + * distances are limited to MAX_DIST instead of WSIZE.
  16639. + */
  16640. +
  16641. + /* in trees.c */
  16642. +void _tr_init OF((deflate_state *s));
  16643. +int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
  16644. +void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
  16645. + int eof));
  16646. +void _tr_align OF((deflate_state *s));
  16647. +void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
  16648. + int eof));
  16649. +
  16650. +#define d_code(dist) \
  16651. + ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
  16652. +/* Mapping from a distance to a distance code. dist is the distance - 1 and
  16653. + * must not have side effects. _dist_code[256] and _dist_code[257] are never
  16654. + * used.
  16655. + */
  16656. +
  16657. +#ifndef DEBUG
  16658. +/* Inline versions of _tr_tally for speed: */
  16659. +
  16660. +#if defined(GEN_TREES_H) || !defined(STDC)
  16661. + extern uch _length_code[];
  16662. + extern uch _dist_code[];
  16663. +#else
  16664. + extern const uch _length_code[];
  16665. + extern const uch _dist_code[];
  16666. +#endif
  16667. +
  16668. +# define _tr_tally_lit(s, c, flush) \
  16669. + { uch cc = (c); \
  16670. + s->d_buf[s->last_lit] = 0; \
  16671. + s->l_buf[s->last_lit++] = cc; \
  16672. + s->dyn_ltree[cc].Freq++; \
  16673. + flush = (s->last_lit == s->lit_bufsize-1); \
  16674. + }
  16675. +# define _tr_tally_dist(s, distance, length, flush) \
  16676. + { uch len = (length); \
  16677. + ush dist = (distance); \
  16678. + s->d_buf[s->last_lit] = dist; \
  16679. + s->l_buf[s->last_lit++] = len; \
  16680. + dist--; \
  16681. + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
  16682. + s->dyn_dtree[d_code(dist)].Freq++; \
  16683. + flush = (s->last_lit == s->lit_bufsize-1); \
  16684. + }
  16685. +#else
  16686. +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
  16687. +# define _tr_tally_dist(s, distance, length, flush) \
  16688. + flush = _tr_tally(s, distance, length)
  16689. +#endif
  16690. +
  16691. +#endif /* _DEFLATE_H */
  16692. --- /dev/null Tue Mar 11 13:02:56 2003
  16693. +++ linux/net/ipsec/des/COPYRIGHT Mon Feb 9 13:51:03 2004
  16694. @@ -0,0 +1,50 @@
  16695. +Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  16696. +All rights reserved.
  16697. +
  16698. +This package is an DES implementation written by Eric Young (eay@cryptsoft.com).
  16699. +The implementation was written so as to conform with MIT's libdes.
  16700. +
  16701. +This library is free for commercial and non-commercial use as long as
  16702. +the following conditions are aheared to. The following conditions
  16703. +apply to all code found in this distribution.
  16704. +
  16705. +Copyright remains Eric Young's, and as such any Copyright notices in
  16706. +the code are not to be removed.
  16707. +If this package is used in a product, Eric Young should be given attribution
  16708. +as the author of that the SSL library. This can be in the form of a textual
  16709. +message at program startup or in documentation (online or textual) provided
  16710. +with the package.
  16711. +
  16712. +Redistribution and use in source and binary forms, with or without
  16713. +modification, are permitted provided that the following conditions
  16714. +are met:
  16715. +1. Redistributions of source code must retain the copyright
  16716. + notice, this list of conditions and the following disclaimer.
  16717. +2. Redistributions in binary form must reproduce the above copyright
  16718. + notice, this list of conditions and the following disclaimer in the
  16719. + documentation and/or other materials provided with the distribution.
  16720. +3. All advertising materials mentioning features or use of this software
  16721. + must display the following acknowledgement:
  16722. + This product includes software developed by Eric Young (eay@cryptsoft.com)
  16723. +
  16724. +THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  16725. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16726. +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16727. +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16728. +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16729. +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  16730. +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  16731. +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16732. +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16733. +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16734. +SUCH DAMAGE.
  16735. +
  16736. +The license and distribution terms for any publically available version or
  16737. +derivative of this code cannot be changed. i.e. this code cannot simply be
  16738. +copied and put under another distrubution license
  16739. +[including the GNU Public License.]
  16740. +
  16741. +The reason behind this being stated in this direct manner is past
  16742. +experience in code simply being copied and the attribution removed
  16743. +from it and then being distributed as part of other packages. This
  16744. +implementation was a non-trivial and unpaid effort.
  16745. --- /dev/null Tue Mar 11 13:02:56 2003
  16746. +++ linux/net/ipsec/des/INSTALL Mon Feb 9 13:51:03 2004
  16747. @@ -0,0 +1,69 @@
  16748. +Check the CC and CFLAGS lines in the makefile
  16749. +
  16750. +If your C library does not support the times(3) function, change the
  16751. +#define TIMES to
  16752. +#undef TIMES in speed.c
  16753. +If it does, check the HZ value for the times(3) function.
  16754. +If your system does not define CLK_TCK it will be assumed to
  16755. +be 100.0.
  16756. +
  16757. +If possible use gcc v 2.7.?
  16758. +Turn on the maximum optimising (normally '-O3 -fomit-frame-pointer' for gcc)
  16759. +In recent times, some system compilers give better performace.
  16760. +
  16761. +type 'make'
  16762. +
  16763. +run './destest' to check things are ok.
  16764. +run './rpw' to check the tty code for reading passwords works.
  16765. +run './speed' to see how fast those optimisations make the library run :-)
  16766. +run './des_opts' to determin the best compile time options.
  16767. +
  16768. +The output from des_opts should be put in the makefile options and des_enc.c
  16769. +should be rebuilt. For 64 bit computers, do not use the DES_PTR option.
  16770. +For the DEC Alpha, edit des.h and change DES_LONG to 'unsigned int'
  16771. +and then you can use the 'DES_PTR' option.
  16772. +
  16773. +The file options.txt has the options listed for best speed on quite a
  16774. +few systems. Look and the options (UNROLL, PTR, RISC2 etc) and then
  16775. +turn on the relevent option in the Makefile
  16776. +
  16777. +There are some special Makefile targets that make life easier.
  16778. +make cc - standard cc build
  16779. +make gcc - standard gcc build
  16780. +make x86-elf - x86 assembler (elf), linux-elf.
  16781. +make x86-out - x86 assembler (a.out), FreeBSD
  16782. +make x86-solaris- x86 assembler
  16783. +make x86-bsdi - x86 assembler (a.out with primative assembler).
  16784. +
  16785. +If at all possible use the assembler (for Windows NT/95, use
  16786. +asm/win32.obj to link with). The x86 assembler is very very fast.
  16787. +
  16788. +A make install will by default install
  16789. +libdes.a in /usr/local/lib/libdes.a
  16790. +des in /usr/local/bin/des
  16791. +des_crypt.man in /usr/local/man/man3/des_crypt.3
  16792. +des.man in /usr/local/man/man1/des.1
  16793. +des.h in /usr/include/des.h
  16794. +
  16795. +des(1) should be compatible with sunOS's but I have been unable to
  16796. +test it.
  16797. +
  16798. +These routines should compile on MSDOS, most 32bit and 64bit version
  16799. +of Unix (BSD and SYSV) and VMS, without modification.
  16800. +The only problems should be #include files that are in the wrong places.
  16801. +
  16802. +These routines can be compiled under MSDOS.
  16803. +I have successfully encrypted files using des(1) under MSDOS and then
  16804. +decrypted the files on a SparcStation.
  16805. +I have been able to compile and test the routines with
  16806. +Microsoft C v 5.1 and Turbo C v 2.0.
  16807. +The code in this library is in no way optimised for the 16bit
  16808. +operation of MSDOS.
  16809. +
  16810. +When building for glibc, ignore all of the above and just unpack into
  16811. +glibc-1.??/des and then gmake as per normal.
  16812. +
  16813. +As a final note on performace. Certain CPUs like sparcs and Alpha often give
  16814. +a %10 speed difference depending on the link order. It is rather anoying
  16815. +when one program reports 'x' DES encrypts a second and another reports
  16816. +'x*0.9' the speed.
  16817. --- /dev/null Tue Mar 11 13:02:56 2003
  16818. +++ linux/net/ipsec/des/Makefile Mon Feb 9 13:51:03 2004
  16819. @@ -0,0 +1,63 @@
  16820. +# Makefile for KLIPS kernel code as a module for 2.6 kernels
  16821. +#
  16822. +# Makefile for KLIPS kernel code as a module
  16823. +# Copyright (C) 1998, 1999, 2000,2001 Richard Guy Briggs.
  16824. +# Copyright (C) 2002-2004 Michael Richardson <mcr@freeswan.org>
  16825. +#
  16826. +# This program is free software; you can redistribute it and/or modify it
  16827. +# under the terms of the GNU General Public License as published by the
  16828. +# Free Software Foundation; either version 2 of the License, or (at your
  16829. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  16830. +#
  16831. +# This program is distributed in the hope that it will be useful, but
  16832. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16833. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16834. +# for more details.
  16835. +#
  16836. +# RCSID $Id: Makefile.fs2_6,v 1.2.2.1 2005/08/12 16:10:57 ken Exp $
  16837. +#
  16838. +# Note! Dependencies are done automagically by 'make dep', which also
  16839. +# removes any old dependencies. DON'T put your own dependencies here
  16840. +# unless it's something special (ie not a .c file).
  16841. +#
  16842. +
  16843. +obj-$(CONFIG_KLIPS_ENC_3DES) += ipsec_alg_3des.o
  16844. +obj-$(CONFIG_KLIPS_ENC_3DES) += cbc_enc.o
  16845. +obj-$(CONFIG_KLIPS_ENC_3DES) += ecb_enc.o
  16846. +obj-$(CONFIG_KLIPS_ENC_3DES) += set_key.o
  16847. +
  16848. +ifeq ($(strip ${SUBARCH}),)
  16849. +SUBARCH:=${ARCH}
  16850. +endif
  16851. +
  16852. +# the assembly version expects frame pointers, which are
  16853. +# optional in many kernel builds. If you want speed, you should
  16854. +# probably use cryptoapi code instead.
  16855. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  16856. +ifeq (${USEASSEMBLY},i386y)
  16857. +obj-$(CONFIG_KLIPS_ENC_3DES) += dx86unix.o
  16858. +else
  16859. +obj-$(CONFIG_KLIPS_ENC_3DES) += des_enc.o
  16860. +endif
  16861. +
  16862. +#
  16863. +# $Log: Makefile.fs2_6,v $
  16864. +# Revision 1.2.2.1 2005/08/12 16:10:57 ken
  16865. +# do not use assembly code with there are no frame pointers
  16866. +#
  16867. +# Revision 1.3 2005/08/12 14:13:59 mcr
  16868. +# do not use assembly code with there are no frame pointers,
  16869. +# as it does not have the right linkages.
  16870. +#
  16871. +# Revision 1.2 2005/04/29 05:13:07 mcr
  16872. +# 3DES algorithm code.
  16873. +#
  16874. +# Revision 1.1 2004/08/17 03:27:30 mcr
  16875. +# klips 2.6 edits.
  16876. +#
  16877. +#
  16878. +# Local Variables:
  16879. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  16880. +# End Variables:
  16881. +#
  16882. +
  16883. --- /dev/null Tue Mar 11 13:02:56 2003
  16884. +++ linux/net/ipsec/des/README Mon Feb 9 13:51:03 2004
  16885. @@ -0,0 +1,54 @@
  16886. +
  16887. + libdes, Version 4.01 10-Jan-97
  16888. +
  16889. + Copyright (c) 1997, Eric Young
  16890. + All rights reserved.
  16891. +
  16892. + This program is free software; you can redistribute it and/or modify
  16893. + it under the terms specified in COPYRIGHT.
  16894. +
  16895. +--
  16896. +The primary ftp site for this library is
  16897. +ftp://ftp.psy.uq.oz.au/pub/Crypto/DES/libdes-x.xx.tar.gz
  16898. +libdes is now also shipped with SSLeay. Primary ftp site of
  16899. +ftp://ftp.psy.uq.oz.au/pub/Crypto/SSL/SSLeay-x.x.x.tar.gz
  16900. +
  16901. +The best way to build this library is to build it as part of SSLeay.
  16902. +
  16903. +This kit builds a DES encryption library and a DES encryption program.
  16904. +It supports ecb, cbc, ofb, cfb, triple ecb, triple cbc, triple ofb,
  16905. +triple cfb, desx, and MIT's pcbc encryption modes and also has a fast
  16906. +implementation of crypt(3).
  16907. +It contains support routines to read keys from a terminal,
  16908. +generate a random key, generate a key from an arbitrary length string,
  16909. +read/write encrypted data from/to a file descriptor.
  16910. +
  16911. +The implementation was written so as to conform with the manual entry
  16912. +for the des_crypt(3) library routines from MIT's project Athena.
  16913. +
  16914. +destest should be run after compilation to test the des routines.
  16915. +rpw should be run after compilation to test the read password routines.
  16916. +The des program is a replacement for the sun des command. I believe it
  16917. +conforms to the sun version.
  16918. +
  16919. +The Imakefile is setup for use in the kerberos distribution.
  16920. +
  16921. +These routines are best compiled with gcc or any other good
  16922. +optimising compiler.
  16923. +Just turn you optimiser up to the highest settings and run destest
  16924. +after the build to make sure everything works.
  16925. +
  16926. +I believe these routines are close to the fastest and most portable DES
  16927. +routines that use small lookup tables (4.5k) that are publicly available.
  16928. +The fcrypt routine is faster than ufc's fcrypt (when compiling with
  16929. +gcc2 -O2) on the sparc 2 (1410 vs 1270) but is not so good on other machines
  16930. +(on a sun3/260 168 vs 336). It is a function of CPU on chip cache size.
  16931. +[ 10-Jan-97 and a function of an incorrect speed testing program in
  16932. + ufc which gave much better test figures that reality ].
  16933. +
  16934. +It is worth noting that on sparc and Alpha CPUs, performance of the DES
  16935. +library can vary by upto %10 due to the positioning of files after application
  16936. +linkage.
  16937. +
  16938. +Eric Young (eay@cryptsoft.com)
  16939. +
  16940. --- /dev/null Tue Mar 11 13:02:56 2003
  16941. +++ linux/net/ipsec/des/README.freeswan Mon Feb 9 13:51:03 2004
  16942. @@ -0,0 +1,33 @@
  16943. +The only changes the FreeS/WAN project has made to libdes-lite 4.04b are:
  16944. +
  16945. +We #ifdef-ed the declaration of DES_LONG in des.h, so it's more efficient
  16946. +on the Alpha, instead of just noting the issue in a comment.
  16947. +
  16948. +We #ifdef-ed out the des_options() function in ecb_enc.c, because we don't
  16949. +use it, and its call to sprintf() can cause subtle difficulties when KLIPS
  16950. +is built as a module (depending on details of Linux configuration options).
  16951. +
  16952. +We changed some instances of CC=$(CC) in the Makefile to CC='$(CC)' to make
  16953. +it cope better with Linux kernel Makefile stupidities, and took out an
  16954. +explicit CC=gcc (unwise on systems with strange compilers).
  16955. +
  16956. +We deleted some references to <stdio.h> and <stdlib.h>, and a declaration
  16957. +of one function found only in the full libdes (not in libdes-lite), to
  16958. +avoid dragging in bits of stdio/stdlib unnecessarily. (Our thanks to Hans
  16959. +Schultz for spotting this and pointing out the fixes.)
  16960. +
  16961. +We deleted a couple of .obj files in the asm subdirectory, which appear to
  16962. +have been included in the original library by accident.
  16963. +
  16964. +We have added an include of our Makefile.inc file, to permit overriding
  16965. +things like choice of compiler (although the libdes Makefile would
  16966. +probably need some work to make this effective).
  16967. +
  16968. +
  16969. +
  16970. +Note that Eric Young is no longer at the email address listed in these
  16971. +files, and is (alas) no longer working on free crypto software.
  16972. +
  16973. +
  16974. +
  16975. +This file is RCSID $Id: README.freeswan,v 1.12 2004/07/10 08:06:51 mcr Exp $
  16976. --- /dev/null Tue Mar 11 13:02:56 2003
  16977. +++ linux/net/ipsec/des/VERSION Mon Feb 9 13:51:03 2004
  16978. @@ -0,0 +1,406 @@
  16979. +Version 4.04
  16980. + Fixed a few tests in destest. Also added x86 assember for
  16981. + des_ncbc_encrypt() which is the standard cbc mode function.
  16982. + This makes a very very large performace difference.
  16983. + Ariel Glenn ariel@columbia.edu reports that the terminal
  16984. + 'turn echo off' can return (errno == EINVAL) under solaris
  16985. + when redirection is used. So I now catch that as well as ENOTTY.
  16986. +
  16987. +
  16988. +Version 4.03
  16989. + Left a static out of enc_write.c, which caused to buffer to be
  16990. + continiously malloc()ed. Does anyone use these functions? I keep
  16991. + on feeling like removing them since I only had these in there
  16992. + for a version of kerberised login. Anyway, this was pointed out
  16993. + by Theo de Raadt <deraadt@cvs.openbsd.org>
  16994. + The 'n' bit ofb code was wrong, it was not shifting the shift
  16995. + register. It worked correctly for n == 64. Thanks to
  16996. + Gigi Ankeny <Gigi.Ankeny@Eng.Sun.COM> for pointing this one out.
  16997. +
  16998. +Version 4.02
  16999. + I was doing 'if (memcmp(weak_keys[i],key,sizeof(key)) == 0)'
  17000. + when checking for weak keys which is wrong :-(, pointed out by
  17001. + Markus F.X.J. Oberhumer <markus.oberhumer@jk.uni-linz.ac.at>.
  17002. +
  17003. +Version 4.01
  17004. + Even faster inner loop in the DES assembler for x86 and a modification
  17005. + for IP/FP which is faster on x86. Both of these changes are
  17006. + from Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>. His
  17007. + changes make the assembler run %40 faster on a pentium. This is just
  17008. + a case of getting the instruction sequence 'just right'.
  17009. + All credit to 'Svend' :-)
  17010. + Quite a few special x86 'make' targets.
  17011. + A libdes-l (lite) distribution.
  17012. +
  17013. +Version 4.00
  17014. + After a bit of a pause, I'll up the major version number since this
  17015. + is mostly a performace release. I've added x86 assembler and
  17016. + added more options for performance. A %28 speedup for gcc
  17017. + on a pentium and the assembler is a %50 speedup.
  17018. + MIPS CPU's, sparc and Alpha are the main CPU's with speedups.
  17019. + Run des_opts to work out which options should be used.
  17020. + DES_RISC1/DES_RISC2 use alternative inner loops which use
  17021. + more registers but should give speedups on any CPU that does
  17022. + dual issue (pentium). DES_UNROLL unrolls the inner loop,
  17023. + which costs in code size.
  17024. +
  17025. +Version 3.26
  17026. + I've finally removed one of the shifts in D_ENCRYPT. This
  17027. + meant I've changed the des_SPtrans table (spr.h), the set_key()
  17028. + function and some things in des_enc.c. This has definitly
  17029. + made things faster :-). I've known about this one for some
  17030. + time but I've been too lazy to follow it up :-).
  17031. + Noticed that in the D_ENCRYPT() macro, we can just do L^=(..)^(..)^..
  17032. + instead of L^=((..)|(..)|(..).. This should save a register at
  17033. + least.
  17034. + Assember for x86. The file to replace is des_enc.c, which is replaced
  17035. + by one of the assembler files found in asm. Look at des/asm/readme
  17036. + for more info.
  17037. +
  17038. + /* Modification to fcrypt so it can be compiled to support
  17039. + HPUX 10.x's long password format, define -DLONGCRYPT to use this.
  17040. + Thanks to Jens Kupferschmidt <bt1cu@hpboot.rz.uni-leipzig.de>. */
  17041. +
  17042. + SIGWINCH case put in des_read_passwd() so the function does not
  17043. + 'exit' if this function is recieved.
  17044. +
  17045. +Version 3.25 17/07/96
  17046. + Modified read_pwd.c so that stdin can be read if not a tty.
  17047. + Thanks to Jeff Barber <jeffb@issl.atl.hp.com> for the patches.
  17048. + des_init_random_number_generator() shortened due to VMS linker
  17049. + limits.
  17050. + Added RSA's DESX cbc mode. It is a form of cbc encryption, with 2
  17051. + 8 byte quantites xored before and after encryption.
  17052. + des_xcbc_encryption() - the name is funny to preserve the des_
  17053. + prefix on all functions.
  17054. +
  17055. +Version 3.24 20/04/96
  17056. + The DES_PTR macro option checked and used by SSLeay configuration
  17057. +
  17058. +Version 3.23 11/04/96
  17059. + Added DES_LONG. If defined to 'unsigned int' on the DEC Alpha,
  17060. + it gives a %20 speedup :-)
  17061. + Fixed the problem with des.pl under perl5. The patches were
  17062. + sent by Ed Kubaitis (ejk@uiuc.edu).
  17063. + if fcrypt.c, changed values to handle illegal salt values the way
  17064. + normal crypt() implementations do. Some programs apparently use
  17065. + them :-(. The patch was sent by Bjorn Gronvall <bg@sics.se>
  17066. +
  17067. +Version 3.22 29/11/95
  17068. + Bug in des(1), an error with the uuencoding stuff when the
  17069. + 'data' is small, thanks to Geoff Keating <keagchon@mehta.anu.edu.au>
  17070. + for the patch.
  17071. +
  17072. +Version 3.21 22/11/95
  17073. + After some emailing back and forth with
  17074. + Colin Plumb <colin@nyx10.cs.du.edu>, I've tweaked a few things
  17075. + and in a future version I will probably put in some of the
  17076. + optimisation he suggested for use with the DES_USE_PTR option.
  17077. + Extra routines from Mark Murray <mark@grondar.za> for use in
  17078. + freeBSD. They mostly involve random number generation for use
  17079. + with kerberos. They involve evil machine specific system calls
  17080. + etc so I would normally suggest pushing this stuff into the
  17081. + application and/or using RAND_seed()/RAND_bytes() if you are
  17082. + using this DES library as part of SSLeay.
  17083. + Redone the read_pw() function so that it is cleaner and
  17084. + supports termios, thanks to Sameer Parekh <sameer@c2.org>
  17085. + for the initial patches for this.
  17086. + Renamed 3ecb_encrypt() to ecb3_encrypt(). This has been
  17087. + done just to make things more consistent.
  17088. + I have also now added triple DES versions of cfb and ofb.
  17089. +
  17090. +Version 3.20
  17091. + Damn, Damn, Damn, as pointed out by Mike_Spreitzer.PARC@xerox.com,
  17092. + my des_random_seed() function was only copying 4 bytes of the
  17093. + passed seed into the init structure. It is now fixed to copy 8.
  17094. + My own suggestion is to used something like MD5 :-)
  17095. +
  17096. +Version 3.19
  17097. + While looking at my code one day, I though, why do I keep on
  17098. + calling des_encrypt(in,out,ks,enc) when every function that
  17099. + calls it has in and out the same. So I dropped the 'out'
  17100. + parameter, people should not be using this function.
  17101. +
  17102. +Version 3.18 30/08/95
  17103. + Fixed a few bit with the distribution and the filenames.
  17104. + 3.17 had been munged via a move to DOS and back again.
  17105. + NO CODE CHANGES
  17106. +
  17107. +Version 3.17 14/07/95
  17108. + Fixed ede3 cbc which I had broken in 3.16. I have also
  17109. + removed some unneeded variables in 7-8 of the routines.
  17110. +
  17111. +Version 3.16 26/06/95
  17112. + Added des_encrypt2() which does not use IP/FP, used by triple
  17113. + des routines. Tweaked things a bit elsewhere. %13 speedup on
  17114. + sparc and %6 on a R4400 for ede3 cbc mode.
  17115. +
  17116. +Version 3.15 06/06/95
  17117. + Added des_ncbc_encrypt(), it is des_cbc mode except that it is
  17118. + 'normal' and copies the new iv value back over the top of the
  17119. + passed parameter.
  17120. + CHANGED des_ede3_cbc_encrypt() so that it too now overwrites
  17121. + the iv. THIS WILL BREAK EXISTING CODE, but since this function
  17122. + only new, I feel I can change it, not so with des_cbc_encrypt :-(.
  17123. + I need to update the documentation.
  17124. +
  17125. +Version 3.14 31/05/95
  17126. + New release upon the world, as part of my SSL implementation.
  17127. + New copyright and usage stuff. Basically free for all to use
  17128. + as long as you say it came from me :-)
  17129. +
  17130. +Version 3.13 31/05/95
  17131. + A fix in speed.c, if HZ is not defined, I set it to 100.0
  17132. + which is reasonable for most unixes except SunOS 4.x.
  17133. + I now have a #ifdef sun but timing for SunOS 4.x looked very
  17134. + good :-(. At my last job where I used SunOS 4.x, it was
  17135. + defined to be 60.0 (look at the old INSTALL documentation), at
  17136. + the last release had it changed to 100.0 since I now work with
  17137. + Solaris2 and SVR4 boxes.
  17138. + Thanks to Rory Chisholm <rchishol@math.ethz.ch> for pointing this
  17139. + one out.
  17140. +
  17141. +Version 3.12 08/05/95
  17142. + As pointed out by The Crypt Keeper <tck@bend.UCSD.EDU>,
  17143. + my D_ENCRYPT macro in crypt() had an un-necessary variable.
  17144. + It has been removed.
  17145. +
  17146. +Version 3.11 03/05/95
  17147. + Added des_ede3_cbc_encrypt() which is cbc mode des with 3 keys
  17148. + and one iv. It is a standard and I needed it for my SSL code.
  17149. + It makes more sense to use this for triple DES than
  17150. + 3cbc_encrypt(). I have also added (or should I say tested :-)
  17151. + cfb64_encrypt() which is cfb64 but it will encrypt a partial
  17152. + number of bytes - 3 bytes in 3 bytes out. Again this is for
  17153. + my SSL library, as a form of encryption to use with SSL
  17154. + telnet.
  17155. +
  17156. +Version 3.10 22/03/95
  17157. + Fixed a bug in 3cbc_encrypt() :-(. When making repeated calls
  17158. + to cbc3_encrypt, the 2 iv values that were being returned to
  17159. + be used in the next call were reversed :-(.
  17160. + Many thanks to Bill Wade <wade@Stoner.COM> for pointing out
  17161. + this error.
  17162. +
  17163. +Version 3.09 01/02/95
  17164. + Fixed des_random_key to far more random, it was rather feeble
  17165. + with regards to picking the initial seed. The problem was
  17166. + pointed out by Olaf Kirch <okir@monad.swb.de>.
  17167. +
  17168. +Version 3.08 14/12/94
  17169. + Added Makefile.PL so libdes can be built into perl5.
  17170. + Changed des_locl.h so RAND is always defined.
  17171. +
  17172. +Version 3.07 05/12/94
  17173. + Added GNUmake and stuff so the library can be build with
  17174. + glibc.
  17175. +
  17176. +Version 3.06 30/08/94
  17177. + Added rpc_enc.c which contains _des_crypt. This is for use in
  17178. + secure_rpc v 4.0
  17179. + Finally fixed the cfb_enc problems.
  17180. + Fixed a few parameter parsing bugs in des (-3 and -b), thanks
  17181. + to Rob McMillan <R.McMillan@its.gu.edu.au>
  17182. +
  17183. +Version 3.05 21/04/94
  17184. + for unsigned long l; gcc does not produce ((l>>34) == 0)
  17185. + This causes bugs in cfb_enc.
  17186. + Thanks to Hadmut Danisch <danisch@ira.uka.de>
  17187. +
  17188. +Version 3.04 20/04/94
  17189. + Added a version number to des.c and libdes.a
  17190. +
  17191. +Version 3.03 12/01/94
  17192. + Fixed a bug in non zero iv in 3cbc_enc.
  17193. +
  17194. +Version 3.02 29/10/93
  17195. + I now work in a place where there are 6+ architectures and 14+
  17196. + OS versions :-).
  17197. + Fixed TERMIO definition so the most sys V boxes will work :-)
  17198. +
  17199. +Release upon comp.sources.misc
  17200. +Version 3.01 08/10/93
  17201. + Added des_3cbc_encrypt()
  17202. +
  17203. +Version 3.00 07/10/93
  17204. + Fixed up documentation.
  17205. + quad_cksum definitely compatible with MIT's now.
  17206. +
  17207. +Version 2.30 24/08/93
  17208. + Triple DES now defaults to triple cbc but can do triple ecb
  17209. + with the -b flag.
  17210. + Fixed some MSDOS uuen/uudecoding problems, thanks to
  17211. + Added prototypes.
  17212. +
  17213. +Version 2.22 29/06/93
  17214. + Fixed a bug in des_is_weak_key() which stopped it working :-(
  17215. + thanks to engineering@MorningStar.Com.
  17216. +
  17217. +Version 2.21 03/06/93
  17218. + des(1) with no arguments gives quite a bit of help.
  17219. + Added -c (generate ckecksum) flag to des(1).
  17220. + Added -3 (triple DES) flag to des(1).
  17221. + Added cfb and ofb routines to the library.
  17222. +
  17223. +Version 2.20 11/03/93
  17224. + Added -u (uuencode) flag to des(1).
  17225. + I have been playing with byte order in quad_cksum to make it
  17226. + compatible with MIT's version. All I can say is avid this
  17227. + function if possible since MIT's output is endian dependent.
  17228. +
  17229. +Version 2.12 14/10/92
  17230. + Added MSDOS specific macro in ecb_encrypt which gives a %70
  17231. + speed up when the code is compiled with turbo C.
  17232. +
  17233. +Version 2.11 12/10/92
  17234. + Speedup in set_key (recoding of PC-1)
  17235. + I now do it in 47 simple operations, down from 60.
  17236. + Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  17237. + for motivating me to look for a faster system :-)
  17238. + The speedup is probably less that 1% but it is still 13
  17239. + instructions less :-).
  17240. +
  17241. +Version 2.10 06/10/92
  17242. + The code now works on the 64bit ETA10 and CRAY without modifications or
  17243. + #defines. I believe the code should work on any machine that
  17244. + defines long, int or short to be 8 bytes long.
  17245. + Thanks to Shabbir J. Safdar (shabby@mentor.cc.purdue.edu)
  17246. + for helping me fix the code to run on 64bit machines (he had
  17247. + access to an ETA10).
  17248. + Thanks also to John Fletcher <john_fletcher@lccmail.ocf.llnl.gov>
  17249. + for testing the routines on a CRAY.
  17250. + read_password.c has been renamed to read_passwd.c
  17251. + string_to_key.c has been renamed to string2key.c
  17252. +
  17253. +Version 2.00 14/09/92
  17254. + Made mods so that the library should work on 64bit CPU's.
  17255. + Removed all my uchar and ulong defs. To many different
  17256. + versions of unix define them in their header files in too many
  17257. + different combinations :-)
  17258. + IRIX - Sillicon Graphics mods (mostly in read_password.c).
  17259. + Thanks to Andrew Daviel (advax@erich.triumf.ca)
  17260. +
  17261. +Version 1.99 26/08/92
  17262. + Fixed a bug or 2 in enc_read.c
  17263. + Fixed a bug in enc_write.c
  17264. + Fixed a pseudo bug in fcrypt.c (very obscure).
  17265. +
  17266. +Version 1.98 31/07/92
  17267. + Support for the ETA10. This is a strange machine that defines
  17268. + longs and ints as 8 bytes and shorts as 4 bytes.
  17269. + Since I do evil things with long * that assume that they are 4
  17270. + bytes. Look in the Makefile for the option to compile for
  17271. + this machine. quad_cksum appears to have problems but I
  17272. + will don't have the time to fix it right now, and this is not
  17273. + a function that uses DES and so will not effect the main uses
  17274. + of the library.
  17275. +
  17276. +Version 1.97 20/05/92 eay
  17277. + Fixed the Imakefile and made some changes to des.h to fix some
  17278. + problems when building this package with Kerberos v 4.
  17279. +
  17280. +Version 1.96 18/05/92 eay
  17281. + Fixed a small bug in string_to_key() where problems could
  17282. + occur if des_check_key was set to true and the string
  17283. + generated a weak key.
  17284. +
  17285. +Patch2 posted to comp.sources.misc
  17286. +Version 1.95 13/05/92 eay
  17287. + Added an alternative version of the D_ENCRYPT macro in
  17288. + ecb_encrypt and fcrypt. Depending on the compiler, one version or the
  17289. + other will be faster. This was inspired by
  17290. + Dana How <how@isl.stanford.edu>, and her pointers about doing the
  17291. + *(ulong *)((uchar *)ptr+(value&0xfc))
  17292. + vs
  17293. + ptr[value&0x3f]
  17294. + to stop the C compiler doing a <<2 to convert the long array index.
  17295. +
  17296. +Version 1.94 05/05/92 eay
  17297. + Fixed an incompatibility between my string_to_key and the MIT
  17298. + version. When the key is longer than 8 chars, I was wrapping
  17299. + with a different method. To use the old version, define
  17300. + OLD_STR_TO_KEY in the makefile. Thanks to
  17301. + viktor@newsu.shearson.com (Viktor Dukhovni).
  17302. +
  17303. +Version 1.93 28/04/92 eay
  17304. + Fixed the VMS mods so that echo is now turned off in
  17305. + read_password. Thanks again to brennan@coco.cchs.su.oz.AU.
  17306. + MSDOS support added. The routines can be compiled with
  17307. + Turbo C (v2.0) and MSC (v5.1). Make sure MSDOS is defined.
  17308. +
  17309. +Patch1 posted to comp.sources.misc
  17310. +Version 1.92 13/04/92 eay
  17311. + Changed D_ENCRYPT so that the rotation of R occurs outside of
  17312. + the loop. This required rotating all the longs in sp.h (now
  17313. + called spr.h). Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  17314. + speed.c has been changed so it will work without SIGALRM. If
  17315. + times(3) is not present it will try to use ftime() instead.
  17316. +
  17317. +Version 1.91 08/04/92 eay
  17318. + Added -E/-D options to des(1) so it can use string_to_key.
  17319. + Added SVR4 mods suggested by witr@rwwa.COM
  17320. + Added VMS mods suggested by brennan@coco.cchs.su.oz.AU. If
  17321. + anyone knows how to turn of tty echo in VMS please tell me or
  17322. + implement it yourself :-).
  17323. + Changed FILE *IN/*OUT to *DES_IN/*DES_OUT since it appears VMS
  17324. + does not like IN/OUT being used.
  17325. +
  17326. +Libdes posted to comp.sources.misc
  17327. +Version 1.9 24/03/92 eay
  17328. + Now contains a fast small crypt replacement.
  17329. + Added des(1) command.
  17330. + Added des_rw_mode so people can use cbc encryption with
  17331. + enc_read and enc_write.
  17332. +
  17333. +Version 1.8 15/10/91 eay
  17334. + Bug in cbc_cksum.
  17335. + Many thanks to Keith Reynolds (keithr@sco.COM) for pointing this
  17336. + one out.
  17337. +
  17338. +Version 1.7 24/09/91 eay
  17339. + Fixed set_key :-)
  17340. + set_key is 4 times faster and takes less space.
  17341. + There are a few minor changes that could be made.
  17342. +
  17343. +Version 1.6 19/09/1991 eay
  17344. + Finally go IP and FP finished.
  17345. + Now I need to fix set_key.
  17346. + This version is quite a bit faster that 1.51
  17347. +
  17348. +Version 1.52 15/06/1991 eay
  17349. + 20% speedup in ecb_encrypt by changing the E bit selection
  17350. + to use 2 32bit words. This also required modification of the
  17351. + sp table. There is still a way to speedup the IP and IP-1
  17352. + (hints from outer@sq.com) still working on this one :-(.
  17353. +
  17354. +Version 1.51 07/06/1991 eay
  17355. + Faster des_encrypt by loop unrolling
  17356. + Fixed bug in quad_cksum.c (thanks to hughes@logos.ucs.indiana.edu)
  17357. +
  17358. +Version 1.50 28/05/1991 eay
  17359. + Optimised the code a bit more for the sparc. I have improved the
  17360. + speed of the inner des_encrypt by speeding up the initial and
  17361. + final permutations.
  17362. +
  17363. +Version 1.40 23/10/1990 eay
  17364. + Fixed des_random_key, it did not produce a random key :-(
  17365. +
  17366. +Version 1.30 2/10/1990 eay
  17367. + Have made des_quad_cksum the same as MIT's, the full package
  17368. + should be compatible with MIT's
  17369. + Have tested on a DECstation 3100
  17370. + Still need to fix des_set_key (make it faster).
  17371. + Does des_cbc_encrypts at 70.5k/sec on a 3100.
  17372. +
  17373. +Version 1.20 18/09/1990 eay
  17374. + Fixed byte order dependencies.
  17375. + Fixed (I hope) all the word alignment problems.
  17376. + Speedup in des_ecb_encrypt.
  17377. +
  17378. +Version 1.10 11/09/1990 eay
  17379. + Added des_enc_read and des_enc_write.
  17380. + Still need to fix des_quad_cksum.
  17381. + Still need to document des_enc_read and des_enc_write.
  17382. +
  17383. +Version 1.00 27/08/1990 eay
  17384. +
  17385. --- /dev/null Tue Mar 11 13:02:56 2003
  17386. +++ linux/net/ipsec/des/asm/des-586.pl Mon Feb 9 13:51:03 2004
  17387. @@ -0,0 +1,251 @@
  17388. +#!/usr/local/bin/perl
  17389. +#
  17390. +# The inner loop instruction sequence and the IP/FP modifications are from
  17391. +# Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
  17392. +#
  17393. +
  17394. +push(@INC,"perlasm","../../perlasm");
  17395. +require "x86asm.pl";
  17396. +require "cbc.pl";
  17397. +require "desboth.pl";
  17398. +
  17399. +# base code is in microsft
  17400. +# op dest, source
  17401. +# format.
  17402. +#
  17403. +
  17404. +&asm_init($ARGV[0],"des-586.pl");
  17405. +
  17406. +$L="edi";
  17407. +$R="esi";
  17408. +
  17409. +&external_label("des_SPtrans");
  17410. +&des_encrypt("des_encrypt",1);
  17411. +&des_encrypt("des_encrypt2",0);
  17412. +&des_encrypt3("des_encrypt3",1);
  17413. +&des_encrypt3("des_decrypt3",0);
  17414. +&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
  17415. +&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
  17416. +
  17417. +&asm_finish();
  17418. +
  17419. +sub des_encrypt
  17420. + {
  17421. + local($name,$do_ip)=@_;
  17422. +
  17423. + &function_begin_B($name,"EXTRN _des_SPtrans:DWORD");
  17424. +
  17425. + &push("esi");
  17426. + &push("edi");
  17427. +
  17428. + &comment("");
  17429. + &comment("Load the 2 words");
  17430. + $ks="ebp";
  17431. +
  17432. + if ($do_ip)
  17433. + {
  17434. + &mov($R,&wparam(0));
  17435. + &xor( "ecx", "ecx" );
  17436. +
  17437. + &push("ebx");
  17438. + &push("ebp");
  17439. +
  17440. + &mov("eax",&DWP(0,$R,"",0));
  17441. + &mov("ebx",&wparam(2)); # get encrypt flag
  17442. + &mov($L,&DWP(4,$R,"",0));
  17443. + &comment("");
  17444. + &comment("IP");
  17445. + &IP_new("eax",$L,$R,3);
  17446. + }
  17447. + else
  17448. + {
  17449. + &mov("eax",&wparam(0));
  17450. + &xor( "ecx", "ecx" );
  17451. +
  17452. + &push("ebx");
  17453. + &push("ebp");
  17454. +
  17455. + &mov($R,&DWP(0,"eax","",0));
  17456. + &mov("ebx",&wparam(2)); # get encrypt flag
  17457. + &rotl($R,3);
  17458. + &mov($L,&DWP(4,"eax","",0));
  17459. + &rotl($L,3);
  17460. + }
  17461. +
  17462. + &mov( $ks, &wparam(1) );
  17463. + &cmp("ebx","0");
  17464. + &je(&label("start_decrypt"));
  17465. +
  17466. + for ($i=0; $i<16; $i+=2)
  17467. + {
  17468. + &comment("");
  17469. + &comment("Round $i");
  17470. + &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17471. +
  17472. + &comment("");
  17473. + &comment("Round ".sprintf("%d",$i+1));
  17474. + &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17475. + }
  17476. + &jmp(&label("end"));
  17477. +
  17478. + &set_label("start_decrypt");
  17479. +
  17480. + for ($i=15; $i>0; $i-=2)
  17481. + {
  17482. + &comment("");
  17483. + &comment("Round $i");
  17484. + &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17485. + &comment("");
  17486. + &comment("Round ".sprintf("%d",$i-1));
  17487. + &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17488. + }
  17489. +
  17490. + &set_label("end");
  17491. +
  17492. + if ($do_ip)
  17493. + {
  17494. + &comment("");
  17495. + &comment("FP");
  17496. + &mov("edx",&wparam(0));
  17497. + &FP_new($L,$R,"eax",3);
  17498. +
  17499. + &mov(&DWP(0,"edx","",0),"eax");
  17500. + &mov(&DWP(4,"edx","",0),$R);
  17501. + }
  17502. + else
  17503. + {
  17504. + &comment("");
  17505. + &comment("Fixup");
  17506. + &rotr($L,3); # r
  17507. + &mov("eax",&wparam(0));
  17508. + &rotr($R,3); # l
  17509. + &mov(&DWP(0,"eax","",0),$L);
  17510. + &mov(&DWP(4,"eax","",0),$R);
  17511. + }
  17512. +
  17513. + &pop("ebp");
  17514. + &pop("ebx");
  17515. + &pop("edi");
  17516. + &pop("esi");
  17517. + &ret();
  17518. +
  17519. + &function_end_B($name);
  17520. + }
  17521. +
  17522. +sub D_ENCRYPT
  17523. + {
  17524. + local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
  17525. +
  17526. + &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
  17527. + &xor( $tmp1, $tmp1);
  17528. + &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
  17529. + &xor( $u, $R);
  17530. + &xor( $t, $R);
  17531. + &and( $u, "0xfcfcfcfc" );
  17532. + &and( $t, "0xcfcfcfcf" );
  17533. + &movb( &LB($tmp1), &LB($u) );
  17534. + &movb( &LB($tmp2), &HB($u) );
  17535. + &rotr( $t, 4 );
  17536. + &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
  17537. + &movb( &LB($tmp1), &LB($t) );
  17538. + &xor( $L, $ks);
  17539. + &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
  17540. + &xor( $L, $ks); ######
  17541. + &movb( &LB($tmp2), &HB($t) );
  17542. + &shr( $u, 16);
  17543. + &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
  17544. + &xor( $L, $ks); ######
  17545. + &movb( &LB($tmp1), &HB($u) );
  17546. + &shr( $t, 16);
  17547. + &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
  17548. + &xor( $L, $ks);
  17549. + &mov( $ks, &wparam(1) );
  17550. + &movb( &LB($tmp2), &HB($t) );
  17551. + &and( $u, "0xff" );
  17552. + &and( $t, "0xff" );
  17553. + &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
  17554. + &xor( $L, $tmp1);
  17555. + &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
  17556. + &xor( $L, $tmp1);
  17557. + &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
  17558. + &xor( $L, $tmp1);
  17559. + &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
  17560. + &xor( $L, $tmp1);
  17561. + }
  17562. +
  17563. +sub n2a
  17564. + {
  17565. + sprintf("%d",$_[0]);
  17566. + }
  17567. +
  17568. +# now has a side affect of rotating $a by $shift
  17569. +sub R_PERM_OP
  17570. + {
  17571. + local($a,$b,$tt,$shift,$mask,$last)=@_;
  17572. +
  17573. + &rotl( $a, $shift ) if ($shift != 0);
  17574. + &mov( $tt, $a );
  17575. + &xor( $a, $b );
  17576. + &and( $a, $mask );
  17577. + if (!$last eq $b)
  17578. + {
  17579. + &xor( $b, $a );
  17580. + &xor( $tt, $a );
  17581. + }
  17582. + else
  17583. + {
  17584. + &xor( $tt, $a );
  17585. + &xor( $b, $a );
  17586. + }
  17587. + &comment("");
  17588. + }
  17589. +
  17590. +sub IP_new
  17591. + {
  17592. + local($l,$r,$tt,$lr)=@_;
  17593. +
  17594. + &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
  17595. + &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
  17596. + &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
  17597. + &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
  17598. + &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
  17599. +
  17600. + if ($lr != 3)
  17601. + {
  17602. + if (($lr-3) < 0)
  17603. + { &rotr($tt, 3-$lr); }
  17604. + else { &rotl($tt, $lr-3); }
  17605. + }
  17606. + if ($lr != 2)
  17607. + {
  17608. + if (($lr-2) < 0)
  17609. + { &rotr($r, 2-$lr); }
  17610. + else { &rotl($r, $lr-2); }
  17611. + }
  17612. + }
  17613. +
  17614. +sub FP_new
  17615. + {
  17616. + local($l,$r,$tt,$lr)=@_;
  17617. +
  17618. + if ($lr != 2)
  17619. + {
  17620. + if (($lr-2) < 0)
  17621. + { &rotl($r, 2-$lr); }
  17622. + else { &rotr($r, $lr-2); }
  17623. + }
  17624. + if ($lr != 3)
  17625. + {
  17626. + if (($lr-3) < 0)
  17627. + { &rotl($l, 3-$lr); }
  17628. + else { &rotr($l, $lr-3); }
  17629. + }
  17630. +
  17631. + &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
  17632. + &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
  17633. + &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
  17634. + &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
  17635. + &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
  17636. + &rotr($tt , 4);
  17637. + }
  17638. +
  17639. --- /dev/null Tue Mar 11 13:02:56 2003
  17640. +++ linux/net/ipsec/des/asm/des686.pl Mon Feb 9 13:51:03 2004
  17641. @@ -0,0 +1,230 @@
  17642. +#!/usr/local/bin/perl
  17643. +
  17644. +$prog="des686.pl";
  17645. +
  17646. +# base code is in microsft
  17647. +# op dest, source
  17648. +# format.
  17649. +#
  17650. +
  17651. +# WILL NOT WORK ANYMORE WITH desboth.pl
  17652. +require "desboth.pl";
  17653. +
  17654. +if ( ($ARGV[0] eq "elf"))
  17655. + { require "x86unix.pl"; }
  17656. +elsif ( ($ARGV[0] eq "a.out"))
  17657. + { $aout=1; require "x86unix.pl"; }
  17658. +elsif ( ($ARGV[0] eq "sol"))
  17659. + { $sol=1; require "x86unix.pl"; }
  17660. +elsif ( ($ARGV[0] eq "cpp"))
  17661. + { $cpp=1; require "x86unix.pl"; }
  17662. +elsif ( ($ARGV[0] eq "win32"))
  17663. + { require "x86ms.pl"; }
  17664. +else
  17665. + {
  17666. + print STDERR <<"EOF";
  17667. +Pick one target type from
  17668. + elf - linux, FreeBSD etc
  17669. + a.out - old linux
  17670. + sol - x86 solaris
  17671. + cpp - format so x86unix.cpp can be used
  17672. + win32 - Windows 95/Windows NT
  17673. +EOF
  17674. + exit(1);
  17675. + }
  17676. +
  17677. +&comment("Don't even think of reading this code");
  17678. +&comment("It was automatically generated by $prog");
  17679. +&comment("Which is a perl program used to generate the x86 assember for");
  17680. +&comment("any of elf, a.out, Win32, or Solaris");
  17681. +&comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
  17682. +&comment("eric <eay\@cryptsoft.com>");
  17683. +&comment("");
  17684. +
  17685. +&file("dx86xxxx");
  17686. +
  17687. +$L="edi";
  17688. +$R="esi";
  17689. +
  17690. +&des_encrypt("des_encrypt",1);
  17691. +&des_encrypt("des_encrypt2",0);
  17692. +
  17693. +&des_encrypt3("des_encrypt3",1);
  17694. +&des_encrypt3("des_decrypt3",0);
  17695. +
  17696. +&file_end();
  17697. +
  17698. +sub des_encrypt
  17699. + {
  17700. + local($name,$do_ip)=@_;
  17701. +
  17702. + &function_begin($name,"EXTRN _des_SPtrans:DWORD");
  17703. +
  17704. + &comment("");
  17705. + &comment("Load the 2 words");
  17706. + &mov("eax",&wparam(0));
  17707. + &mov($L,&DWP(0,"eax","",0));
  17708. + &mov($R,&DWP(4,"eax","",0));
  17709. +
  17710. + $ksp=&wparam(1);
  17711. +
  17712. + if ($do_ip)
  17713. + {
  17714. + &comment("");
  17715. + &comment("IP");
  17716. + &IP_new($L,$R,"eax");
  17717. + }
  17718. +
  17719. + &comment("");
  17720. + &comment("fixup rotate");
  17721. + &rotl($R,3);
  17722. + &rotl($L,3);
  17723. + &exch($L,$R);
  17724. +
  17725. + &comment("");
  17726. + &comment("load counter, key_schedule and enc flag");
  17727. + &mov("eax",&wparam(2)); # get encrypt flag
  17728. + &mov("ebp",&wparam(1)); # get ks
  17729. + &cmp("eax","0");
  17730. + &je(&label("start_decrypt"));
  17731. +
  17732. + # encrypting part
  17733. +
  17734. + for ($i=0; $i<16; $i+=2)
  17735. + {
  17736. + &comment("");
  17737. + &comment("Round $i");
  17738. + &D_ENCRYPT($L,$R,$i*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17739. +
  17740. + &comment("");
  17741. + &comment("Round ".sprintf("%d",$i+1));
  17742. + &D_ENCRYPT($R,$L,($i+1)*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17743. + }
  17744. + &jmp(&label("end"));
  17745. +
  17746. + &set_label("start_decrypt");
  17747. +
  17748. + for ($i=15; $i>0; $i-=2)
  17749. + {
  17750. + &comment("");
  17751. + &comment("Round $i");
  17752. + &D_ENCRYPT($L,$R,$i*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17753. + &comment("");
  17754. + &comment("Round ".sprintf("%d",$i-1));
  17755. + &D_ENCRYPT($R,$L,($i-1)*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17756. + }
  17757. +
  17758. + &set_label("end");
  17759. +
  17760. + &comment("");
  17761. + &comment("Fixup");
  17762. + &rotr($L,3); # r
  17763. + &rotr($R,3); # l
  17764. +
  17765. + if ($do_ip)
  17766. + {
  17767. + &comment("");
  17768. + &comment("FP");
  17769. + &FP_new($R,$L,"eax");
  17770. + }
  17771. +
  17772. + &mov("eax",&wparam(0));
  17773. + &mov(&DWP(0,"eax","",0),$L);
  17774. + &mov(&DWP(4,"eax","",0),$R);
  17775. +
  17776. + &function_end($name);
  17777. + }
  17778. +
  17779. +
  17780. +# The logic is to load R into 2 registers and operate on both at the same time.
  17781. +# We also load the 2 R's into 2 more registers so we can do the 'move word down a byte'
  17782. +# while also masking the other copy and doing a lookup. We then also accumulate the
  17783. +# L value in 2 registers then combine them at the end.
  17784. +sub D_ENCRYPT
  17785. + {
  17786. + local($L,$R,$S,$ks,$desSP,$u,$t,$tmp1,$tmp2,$tmp3)=@_;
  17787. +
  17788. + &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
  17789. + &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
  17790. + &xor( $u, $R );
  17791. + &xor( $t, $R );
  17792. + &rotr( $t, 4 );
  17793. +
  17794. + # the numbers at the end of the line are origional instruction order
  17795. + &mov( $tmp2, $u ); # 1 2
  17796. + &mov( $tmp1, $t ); # 1 1
  17797. + &and( $tmp2, "0xfc" ); # 1 4
  17798. + &and( $tmp1, "0xfc" ); # 1 3
  17799. + &shr( $t, 8 ); # 1 5
  17800. + &xor( $L, &DWP("0x100+$desSP",$tmp1,"",0)); # 1 7
  17801. + &shr( $u, 8 ); # 1 6
  17802. + &mov( $tmp1, &DWP(" $desSP",$tmp2,"",0)); # 1 8
  17803. +
  17804. + &mov( $tmp2, $u ); # 2 2
  17805. + &xor( $L, $tmp1 ); # 1 9
  17806. + &and( $tmp2, "0xfc" ); # 2 4
  17807. + &mov( $tmp1, $t ); # 2 1
  17808. + &and( $tmp1, "0xfc" ); # 2 3
  17809. + &shr( $t, 8 ); # 2 5
  17810. + &xor( $L, &DWP("0x300+$desSP",$tmp1,"",0)); # 2 7
  17811. + &shr( $u, 8 ); # 2 6
  17812. + &mov( $tmp1, &DWP("0x200+$desSP",$tmp2,"",0)); # 2 8
  17813. + &mov( $tmp2, $u ); # 3 2
  17814. +
  17815. + &xor( $L, $tmp1 ); # 2 9
  17816. + &and( $tmp2, "0xfc" ); # 3 4
  17817. +
  17818. + &mov( $tmp1, $t ); # 3 1
  17819. + &shr( $u, 8 ); # 3 6
  17820. + &and( $tmp1, "0xfc" ); # 3 3
  17821. + &shr( $t, 8 ); # 3 5
  17822. + &xor( $L, &DWP("0x500+$desSP",$tmp1,"",0)); # 3 7
  17823. + &mov( $tmp1, &DWP("0x400+$desSP",$tmp2,"",0)); # 3 8
  17824. +
  17825. + &and( $t, "0xfc" ); # 4 1
  17826. + &xor( $L, $tmp1 ); # 3 9
  17827. +
  17828. + &and( $u, "0xfc" ); # 4 2
  17829. + &xor( $L, &DWP("0x700+$desSP",$t,"",0)); # 4 3
  17830. + &xor( $L, &DWP("0x600+$desSP",$u,"",0)); # 4 4
  17831. + }
  17832. +
  17833. +sub PERM_OP
  17834. + {
  17835. + local($a,$b,$tt,$shift,$mask)=@_;
  17836. +
  17837. + &mov( $tt, $a );
  17838. + &shr( $tt, $shift );
  17839. + &xor( $tt, $b );
  17840. + &and( $tt, $mask );
  17841. + &xor( $b, $tt );
  17842. + &shl( $tt, $shift );
  17843. + &xor( $a, $tt );
  17844. + }
  17845. +
  17846. +sub IP_new
  17847. + {
  17848. + local($l,$r,$tt)=@_;
  17849. +
  17850. + &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
  17851. + &PERM_OP($l,$r,$tt,16,"0x0000ffff");
  17852. + &PERM_OP($r,$l,$tt, 2,"0x33333333");
  17853. + &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
  17854. + &PERM_OP($r,$l,$tt, 1,"0x55555555");
  17855. + }
  17856. +
  17857. +sub FP_new
  17858. + {
  17859. + local($l,$r,$tt)=@_;
  17860. +
  17861. + &PERM_OP($l,$r,$tt, 1,"0x55555555");
  17862. + &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
  17863. + &PERM_OP($l,$r,$tt, 2,"0x33333333");
  17864. + &PERM_OP($r,$l,$tt,16,"0x0000ffff");
  17865. + &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
  17866. + }
  17867. +
  17868. +sub n2a
  17869. + {
  17870. + sprintf("%d",$_[0]);
  17871. + }
  17872. --- /dev/null Tue Mar 11 13:02:56 2003
  17873. +++ linux/net/ipsec/des/asm/desboth.pl Mon Feb 9 13:51:03 2004
  17874. @@ -0,0 +1,79 @@
  17875. +#!/usr/local/bin/perl
  17876. +
  17877. +$L="edi";
  17878. +$R="esi";
  17879. +
  17880. +sub des_encrypt3
  17881. + {
  17882. + local($name,$enc)=@_;
  17883. +
  17884. + &function_begin_B($name,"");
  17885. + &push("ebx");
  17886. + &mov("ebx",&wparam(0));
  17887. +
  17888. + &push("ebp");
  17889. + &push("esi");
  17890. +
  17891. + &push("edi");
  17892. +
  17893. + &comment("");
  17894. + &comment("Load the data words");
  17895. + &mov($L,&DWP(0,"ebx","",0));
  17896. + &mov($R,&DWP(4,"ebx","",0));
  17897. + &stack_push(3);
  17898. +
  17899. + &comment("");
  17900. + &comment("IP");
  17901. + &IP_new($L,$R,"edx",0);
  17902. +
  17903. + # put them back
  17904. +
  17905. + if ($enc)
  17906. + {
  17907. + &mov(&DWP(4,"ebx","",0),$R);
  17908. + &mov("eax",&wparam(1));
  17909. + &mov(&DWP(0,"ebx","",0),"edx");
  17910. + &mov("edi",&wparam(2));
  17911. + &mov("esi",&wparam(3));
  17912. + }
  17913. + else
  17914. + {
  17915. + &mov(&DWP(4,"ebx","",0),$R);
  17916. + &mov("esi",&wparam(1));
  17917. + &mov(&DWP(0,"ebx","",0),"edx");
  17918. + &mov("edi",&wparam(2));
  17919. + &mov("eax",&wparam(3));
  17920. + }
  17921. + &mov(&swtmp(2), (($enc)?"1":"0"));
  17922. + &mov(&swtmp(1), "eax");
  17923. + &mov(&swtmp(0), "ebx");
  17924. + &call("des_encrypt2");
  17925. + &mov(&swtmp(2), (($enc)?"0":"1"));
  17926. + &mov(&swtmp(1), "edi");
  17927. + &mov(&swtmp(0), "ebx");
  17928. + &call("des_encrypt2");
  17929. + &mov(&swtmp(2), (($enc)?"1":"0"));
  17930. + &mov(&swtmp(1), "esi");
  17931. + &mov(&swtmp(0), "ebx");
  17932. + &call("des_encrypt2");
  17933. +
  17934. + &stack_pop(3);
  17935. + &mov($L,&DWP(0,"ebx","",0));
  17936. + &mov($R,&DWP(4,"ebx","",0));
  17937. +
  17938. + &comment("");
  17939. + &comment("FP");
  17940. + &FP_new($L,$R,"eax",0);
  17941. +
  17942. + &mov(&DWP(0,"ebx","",0),"eax");
  17943. + &mov(&DWP(4,"ebx","",0),$R);
  17944. +
  17945. + &pop("edi");
  17946. + &pop("esi");
  17947. + &pop("ebp");
  17948. + &pop("ebx");
  17949. + &ret();
  17950. + &function_end_B($name);
  17951. + }
  17952. +
  17953. +
  17954. --- /dev/null Tue Mar 11 13:02:56 2003
  17955. +++ linux/net/ipsec/des/asm/readme Mon Feb 9 13:51:03 2004
  17956. @@ -0,0 +1,131 @@
  17957. +First up, let me say I don't like writing in assembler. It is not portable,
  17958. +dependant on the particular CPU architecture release and is generally a pig
  17959. +to debug and get right. Having said that, the x86 architecture is probably
  17960. +the most important for speed due to number of boxes and since
  17961. +it appears to be the worst architecture to to get
  17962. +good C compilers for. So due to this, I have lowered myself to do
  17963. +assembler for the inner DES routines in libdes :-).
  17964. +
  17965. +The file to implement in assembler is des_enc.c. Replace the following
  17966. +4 functions
  17967. +des_encrypt(DES_LONG data[2],des_key_schedule ks, int encrypt);
  17968. +des_encrypt2(DES_LONG data[2],des_key_schedule ks, int encrypt);
  17969. +des_encrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  17970. +des_decrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  17971. +
  17972. +They encrypt/decrypt the 64 bits held in 'data' using
  17973. +the 'ks' key schedules. The only difference between the 4 functions is that
  17974. +des_encrypt2() does not perform IP() or FP() on the data (this is an
  17975. +optimization for when doing triple DES and des_encrypt3() and des_decrypt3()
  17976. +perform triple des. The triple DES routines are in here because it does
  17977. +make a big difference to have them located near the des_encrypt2 function
  17978. +at link time..
  17979. +
  17980. +Now as we all know, there are lots of different operating systems running on
  17981. +x86 boxes, and unfortunately they normally try to make sure their assembler
  17982. +formating is not the same as the other peoples.
  17983. +The 4 main formats I know of are
  17984. +Microsoft Windows 95/Windows NT
  17985. +Elf Includes Linux and FreeBSD(?).
  17986. +a.out The older Linux.
  17987. +Solaris Same as Elf but different comments :-(.
  17988. +
  17989. +Now I was not overly keen to write 4 different copies of the same code,
  17990. +so I wrote a few perl routines to output the correct assembler, given
  17991. +a target assembler type. This code is ugly and is just a hack.
  17992. +The libraries are x86unix.pl and x86ms.pl.
  17993. +des586.pl, des686.pl and des-som[23].pl are the programs to actually
  17994. +generate the assembler.
  17995. +
  17996. +So to generate elf assembler
  17997. +perl des-som3.pl elf >dx86-elf.s
  17998. +For Windows 95/NT
  17999. +perl des-som2.pl win32 >win32.asm
  18000. +
  18001. +[ update 4 Jan 1996 ]
  18002. +I have added another way to do things.
  18003. +perl des-som3.pl cpp >dx86-cpp.s
  18004. +generates a file that will be included by dx86unix.cpp when it is compiled.
  18005. +To build for elf, a.out, solaris, bsdi etc,
  18006. +cc -E -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o
  18007. +cc -E -DSOL asm/dx86unix.cpp | as -o asm/dx86-sol.o
  18008. +cc -E -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o
  18009. +cc -E -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o
  18010. +This was done to cut down the number of files in the distribution.
  18011. +
  18012. +Now the ugly part. I acquired my copy of Intels
  18013. +"Optimization's For Intel's 32-Bit Processors" and found a few interesting
  18014. +things. First, the aim of the exersize is to 'extract' one byte at a time
  18015. +from a word and do an array lookup. This involves getting the byte from
  18016. +the 4 locations in the word and moving it to a new word and doing the lookup.
  18017. +The most obvious way to do this is
  18018. +xor eax, eax # clear word
  18019. +movb al, cl # get low byte
  18020. +xor edi DWORD PTR 0x100+des_SP[eax] # xor in word
  18021. +movb al, ch # get next byte
  18022. +xor edi DWORD PTR 0x300+des_SP[eax] # xor in word
  18023. +shr ecx 16
  18024. +which seems ok. For the pentium, this system appears to be the best.
  18025. +One has to do instruction interleaving to keep both functional units
  18026. +operating, but it is basically very efficient.
  18027. +
  18028. +Now the crunch. When a full register is used after a partial write, eg.
  18029. +mov al, cl
  18030. +xor edi, DWORD PTR 0x100+des_SP[eax]
  18031. +386 - 1 cycle stall
  18032. +486 - 1 cycle stall
  18033. +586 - 0 cycle stall
  18034. +686 - at least 7 cycle stall (page 22 of the above mentioned document).
  18035. +
  18036. +So the technique that produces the best results on a pentium, according to
  18037. +the documentation, will produce hideous results on a pentium pro.
  18038. +
  18039. +To get around this, des686.pl will generate code that is not as fast on
  18040. +a pentium, should be very good on a pentium pro.
  18041. +mov eax, ecx # copy word
  18042. +shr ecx, 8 # line up next byte
  18043. +and eax, 0fch # mask byte
  18044. +xor edi DWORD PTR 0x100+des_SP[eax] # xor in array lookup
  18045. +mov eax, ecx # get word
  18046. +shr ecx 8 # line up next byte
  18047. +and eax, 0fch # mask byte
  18048. +xor edi DWORD PTR 0x300+des_SP[eax] # xor in array lookup
  18049. +
  18050. +Due to the execution units in the pentium, this actually works quite well.
  18051. +For a pentium pro it should be very good. This is the type of output
  18052. +Visual C++ generates.
  18053. +
  18054. +There is a third option. instead of using
  18055. +mov al, ch
  18056. +which is bad on the pentium pro, one may be able to use
  18057. +movzx eax, ch
  18058. +which may not incur the partial write penalty. On the pentium,
  18059. +this instruction takes 4 cycles so is not worth using but on the
  18060. +pentium pro it appears it may be worth while. I need access to one to
  18061. +experiment :-).
  18062. +
  18063. +eric (20 Oct 1996)
  18064. +
  18065. +22 Nov 1996 - I have asked people to run the 2 different version on pentium
  18066. +pros and it appears that the intel documentation is wrong. The
  18067. +mov al,bh is still faster on a pentium pro, so just use the des586.pl
  18068. +install des686.pl
  18069. +
  18070. +3 Dec 1996 - I added des_encrypt3/des_decrypt3 because I have moved these
  18071. +functions into des_enc.c because it does make a massive performance
  18072. +difference on some boxes to have the functions code located close to
  18073. +the des_encrypt2() function.
  18074. +
  18075. +9 Jan 1997 - des-som2.pl is now the correct perl script to use for
  18076. +pentiums. It contains an inner loop from
  18077. +Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk> which does raw ecb DES calls at
  18078. +273,000 per second. He had a previous version at 250,000 and the best
  18079. +I was able to get was 203,000. The content has not changed, this is all
  18080. +due to instruction sequencing (and actual instructions choice) which is able
  18081. +to keep both functional units of the pentium going.
  18082. +We may have lost the ugly register usage restrictions when x86 went 32 bit
  18083. +but for the pentium it has been replaced by evil instruction ordering tricks.
  18084. +
  18085. +13 Jan 1997 - des-som3.pl, more optimizations from Svend Olaf.
  18086. +raw DES at 281,000 per second on a pentium 100.
  18087. +
  18088. --- /dev/null Tue Mar 11 13:02:56 2003
  18089. +++ linux/net/ipsec/des/cbc_enc.c Mon Feb 9 13:51:03 2004
  18090. @@ -0,0 +1,135 @@
  18091. +/* crypto/des/cbc_enc.c */
  18092. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  18093. + * All rights reserved.
  18094. + *
  18095. + * This package is an SSL implementation written
  18096. + * by Eric Young (eay@cryptsoft.com).
  18097. + * The implementation was written so as to conform with Netscapes SSL.
  18098. + *
  18099. + * This library is free for commercial and non-commercial use as long as
  18100. + * the following conditions are aheared to. The following conditions
  18101. + * apply to all code found in this distribution, be it the RC4, RSA,
  18102. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18103. + * included with this distribution is covered by the same copyright terms
  18104. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  18105. + *
  18106. + * Copyright remains Eric Young's, and as such any Copyright notices in
  18107. + * the code are not to be removed.
  18108. + * If this package is used in a product, Eric Young should be given attribution
  18109. + * as the author of the parts of the library used.
  18110. + * This can be in the form of a textual message at program startup or
  18111. + * in documentation (online or textual) provided with the package.
  18112. + *
  18113. + * Redistribution and use in source and binary forms, with or without
  18114. + * modification, are permitted provided that the following conditions
  18115. + * are met:
  18116. + * 1. Redistributions of source code must retain the copyright
  18117. + * notice, this list of conditions and the following disclaimer.
  18118. + * 2. Redistributions in binary form must reproduce the above copyright
  18119. + * notice, this list of conditions and the following disclaimer in the
  18120. + * documentation and/or other materials provided with the distribution.
  18121. + * 3. All advertising materials mentioning features or use of this software
  18122. + * must display the following acknowledgement:
  18123. + * "This product includes cryptographic software written by
  18124. + * Eric Young (eay@cryptsoft.com)"
  18125. + * The word 'cryptographic' can be left out if the rouines from the library
  18126. + * being used are not cryptographic related :-).
  18127. + * 4. If you include any Windows specific code (or a derivative thereof) from
  18128. + * the apps directory (application code) you must include an acknowledgement:
  18129. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  18130. + *
  18131. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  18132. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18133. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18134. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18135. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18136. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18137. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18138. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18139. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18140. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18141. + * SUCH DAMAGE.
  18142. + *
  18143. + * The licence and distribution terms for any publically available version or
  18144. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  18145. + * copied and put under another distribution licence
  18146. + * [including the GNU Public Licence.]
  18147. + */
  18148. +
  18149. +#include "des/des_locl.h"
  18150. +
  18151. +void des_cbc_encrypt(input, output, length, schedule, ivec, enc)
  18152. +des_cblock (*input);
  18153. +des_cblock (*output);
  18154. +long length;
  18155. +des_key_schedule schedule;
  18156. +des_cblock (*ivec);
  18157. +int enc;
  18158. + {
  18159. + register DES_LONG tin0,tin1;
  18160. + register DES_LONG tout0,tout1,xor0,xor1;
  18161. + register unsigned char *in,*out;
  18162. + register long l=length;
  18163. + DES_LONG tin[2];
  18164. + unsigned char *iv;
  18165. +
  18166. + in=(unsigned char *)input;
  18167. + out=(unsigned char *)output;
  18168. + iv=(unsigned char *)ivec;
  18169. +
  18170. + if (enc)
  18171. + {
  18172. + c2l(iv,tout0);
  18173. + c2l(iv,tout1);
  18174. + for (l-=8; l>=0; l-=8)
  18175. + {
  18176. + c2l(in,tin0);
  18177. + c2l(in,tin1);
  18178. + tin0^=tout0; tin[0]=tin0;
  18179. + tin1^=tout1; tin[1]=tin1;
  18180. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  18181. + tout0=tin[0]; l2c(tout0,out);
  18182. + tout1=tin[1]; l2c(tout1,out);
  18183. + }
  18184. + if (l != -8)
  18185. + {
  18186. + c2ln(in,tin0,tin1,l+8);
  18187. + tin0^=tout0; tin[0]=tin0;
  18188. + tin1^=tout1; tin[1]=tin1;
  18189. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  18190. + tout0=tin[0]; l2c(tout0,out);
  18191. + tout1=tin[1]; l2c(tout1,out);
  18192. + }
  18193. + }
  18194. + else
  18195. + {
  18196. + c2l(iv,xor0);
  18197. + c2l(iv,xor1);
  18198. + for (l-=8; l>=0; l-=8)
  18199. + {
  18200. + c2l(in,tin0); tin[0]=tin0;
  18201. + c2l(in,tin1); tin[1]=tin1;
  18202. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  18203. + tout0=tin[0]^xor0;
  18204. + tout1=tin[1]^xor1;
  18205. + l2c(tout0,out);
  18206. + l2c(tout1,out);
  18207. + xor0=tin0;
  18208. + xor1=tin1;
  18209. + }
  18210. + if (l != -8)
  18211. + {
  18212. + c2l(in,tin0); tin[0]=tin0;
  18213. + c2l(in,tin1); tin[1]=tin1;
  18214. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  18215. + tout0=tin[0]^xor0;
  18216. + tout1=tin[1]^xor1;
  18217. + l2cn(tout0,tout1,out,l+8);
  18218. + /* xor0=tin0;
  18219. + xor1=tin1; */
  18220. + }
  18221. + }
  18222. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  18223. + tin[0]=tin[1]=0;
  18224. + }
  18225. +
  18226. --- /dev/null Tue Mar 11 13:02:56 2003
  18227. +++ linux/net/ipsec/des/des.doc Mon Feb 9 13:51:03 2004
  18228. @@ -0,0 +1,505 @@
  18229. +The DES library.
  18230. +
  18231. +Please note that this library was originally written to operate with
  18232. +eBones, a version of Kerberos that had had encryption removed when it left
  18233. +the USA and then put back in. As such there are some routines that I will
  18234. +advise not using but they are still in the library for historical reasons.
  18235. +For all calls that have an 'input' and 'output' variables, they can be the
  18236. +same.
  18237. +
  18238. +This library requires the inclusion of 'des.h'.
  18239. +
  18240. +All of the encryption functions take what is called a des_key_schedule as an
  18241. +argument. A des_key_schedule is an expanded form of the des key.
  18242. +A des_key is 8 bytes of odd parity, the type used to hold the key is a
  18243. +des_cblock. A des_cblock is an array of 8 bytes, often in this library
  18244. +description I will refer to input bytes when the function specifies
  18245. +des_cblock's as input or output, this just means that the variable should
  18246. +be a multiple of 8 bytes.
  18247. +
  18248. +The define DES_ENCRYPT is passed to specify encryption, DES_DECRYPT to
  18249. +specify decryption. The functions and global variable are as follows:
  18250. +
  18251. +int des_check_key;
  18252. + DES keys are supposed to be odd parity. If this variable is set to
  18253. + a non-zero value, des_set_key() will check that the key has odd
  18254. + parity and is not one of the known weak DES keys. By default this
  18255. + variable is turned off;
  18256. +
  18257. +void des_set_odd_parity(
  18258. +des_cblock *key );
  18259. + This function takes a DES key (8 bytes) and sets the parity to odd.
  18260. +
  18261. +int des_is_weak_key(
  18262. +des_cblock *key );
  18263. + This function returns a non-zero value if the DES key passed is a
  18264. + weak, DES key. If it is a weak key, don't use it, try a different
  18265. + one. If you are using 'random' keys, the chances of hitting a weak
  18266. + key are 1/2^52 so it is probably not worth checking for them.
  18267. +
  18268. +int des_set_key(
  18269. +des_cblock *key,
  18270. +des_key_schedule schedule);
  18271. + Des_set_key converts an 8 byte DES key into a des_key_schedule.
  18272. + A des_key_schedule is an expanded form of the key which is used to
  18273. + perform actual encryption. It can be regenerated from the DES key
  18274. + so it only needs to be kept when encryption or decryption is about
  18275. + to occur. Don't save or pass around des_key_schedule's since they
  18276. + are CPU architecture dependent, DES keys are not. If des_check_key
  18277. + is non zero, zero is returned if the key has the wrong parity or
  18278. + the key is a weak key, else 1 is returned.
  18279. +
  18280. +int des_key_sched(
  18281. +des_cblock *key,
  18282. +des_key_schedule schedule);
  18283. + An alternative name for des_set_key().
  18284. +
  18285. +int des_rw_mode; /* defaults to DES_PCBC_MODE */
  18286. + This flag holds either DES_CBC_MODE or DES_PCBC_MODE (default).
  18287. + This specifies the function to use in the enc_read() and enc_write()
  18288. + functions.
  18289. +
  18290. +void des_encrypt(
  18291. +unsigned long *data,
  18292. +des_key_schedule ks,
  18293. +int enc);
  18294. + This is the DES encryption function that gets called by just about
  18295. + every other DES routine in the library. You should not use this
  18296. + function except to implement 'modes' of DES. I say this because the
  18297. + functions that call this routine do the conversion from 'char *' to
  18298. + long, and this needs to be done to make sure 'non-aligned' memory
  18299. + access do not occur. The characters are loaded 'little endian',
  18300. + have a look at my source code for more details on how I use this
  18301. + function.
  18302. + Data is a pointer to 2 unsigned long's and ks is the
  18303. + des_key_schedule to use. enc, is non zero specifies encryption,
  18304. + zero if decryption.
  18305. +
  18306. +void des_encrypt2(
  18307. +unsigned long *data,
  18308. +des_key_schedule ks,
  18309. +int enc);
  18310. + This functions is the same as des_encrypt() except that the DES
  18311. + initial permutation (IP) and final permutation (FP) have been left
  18312. + out. As for des_encrypt(), you should not use this function.
  18313. + It is used by the routines in my library that implement triple DES.
  18314. + IP() des_encrypt2() des_encrypt2() des_encrypt2() FP() is the same
  18315. + as des_encrypt() des_encrypt() des_encrypt() except faster :-).
  18316. +
  18317. +void des_ecb_encrypt(
  18318. +des_cblock *input,
  18319. +des_cblock *output,
  18320. +des_key_schedule ks,
  18321. +int enc);
  18322. + This is the basic Electronic Code Book form of DES, the most basic
  18323. + form. Input is encrypted into output using the key represented by
  18324. + ks. If enc is non zero (DES_ENCRYPT), encryption occurs, otherwise
  18325. + decryption occurs. Input is 8 bytes long and output is 8 bytes.
  18326. + (the des_cblock structure is 8 chars).
  18327. +
  18328. +void des_ecb3_encrypt(
  18329. +des_cblock *input,
  18330. +des_cblock *output,
  18331. +des_key_schedule ks1,
  18332. +des_key_schedule ks2,
  18333. +des_key_schedule ks3,
  18334. +int enc);
  18335. + This is the 3 key EDE mode of ECB DES. What this means is that
  18336. + the 8 bytes of input is encrypted with ks1, decrypted with ks2 and
  18337. + then encrypted again with ks3, before being put into output;
  18338. + C=E(ks3,D(ks2,E(ks1,M))). There is a macro, des_ecb2_encrypt()
  18339. + that only takes 2 des_key_schedules that implements,
  18340. + C=E(ks1,D(ks2,E(ks1,M))) in that the final encrypt is done with ks1.
  18341. +
  18342. +void des_cbc_encrypt(
  18343. +des_cblock *input,
  18344. +des_cblock *output,
  18345. +long length,
  18346. +des_key_schedule ks,
  18347. +des_cblock *ivec,
  18348. +int enc);
  18349. + This routine implements DES in Cipher Block Chaining mode.
  18350. + Input, which should be a multiple of 8 bytes is encrypted
  18351. + (or decrypted) to output which will also be a multiple of 8 bytes.
  18352. + The number of bytes is in length (and from what I've said above,
  18353. + should be a multiple of 8). If length is not a multiple of 8, I'm
  18354. + not being held responsible :-). ivec is the initialisation vector.
  18355. + This function does not modify this variable. To correctly implement
  18356. + cbc mode, you need to do one of 2 things; copy the last 8 bytes of
  18357. + cipher text for use as the next ivec in your application,
  18358. + or use des_ncbc_encrypt().
  18359. + Only this routine has this problem with updating the ivec, all
  18360. + other routines that are implementing cbc mode update ivec.
  18361. +
  18362. +void des_ncbc_encrypt(
  18363. +des_cblock *input,
  18364. +des_cblock *output,
  18365. +long length,
  18366. +des_key_schedule sk,
  18367. +des_cblock *ivec,
  18368. +int enc);
  18369. + For historical reasons, des_cbc_encrypt() did not update the
  18370. + ivec with the value requires so that subsequent calls to
  18371. + des_cbc_encrypt() would 'chain'. This was needed so that the same
  18372. + 'length' values would not need to be used when decrypting.
  18373. + des_ncbc_encrypt() does the right thing. It is the same as
  18374. + des_cbc_encrypt accept that ivec is updates with the correct value
  18375. + to pass in subsequent calls to des_ncbc_encrypt(). I advise using
  18376. + des_ncbc_encrypt() instead of des_cbc_encrypt();
  18377. +
  18378. +void des_xcbc_encrypt(
  18379. +des_cblock *input,
  18380. +des_cblock *output,
  18381. +long length,
  18382. +des_key_schedule sk,
  18383. +des_cblock *ivec,
  18384. +des_cblock *inw,
  18385. +des_cblock *outw,
  18386. +int enc);
  18387. + This is RSA's DESX mode of DES. It uses inw and outw to
  18388. + 'whiten' the encryption. inw and outw are secret (unlike the iv)
  18389. + and are as such, part of the key. So the key is sort of 24 bytes.
  18390. + This is much better than cbc des.
  18391. +
  18392. +void des_3cbc_encrypt(
  18393. +des_cblock *input,
  18394. +des_cblock *output,
  18395. +long length,
  18396. +des_key_schedule sk1,
  18397. +des_key_schedule sk2,
  18398. +des_cblock *ivec1,
  18399. +des_cblock *ivec2,
  18400. +int enc);
  18401. + This function is flawed, do not use it. I have left it in the
  18402. + library because it is used in my des(1) program and will function
  18403. + correctly when used by des(1). If I removed the function, people
  18404. + could end up unable to decrypt files.
  18405. + This routine implements outer triple cbc encryption using 2 ks and
  18406. + 2 ivec's. Use des_ede2_cbc_encrypt() instead.
  18407. +
  18408. +void des_ede3_cbc_encrypt(
  18409. +des_cblock *input,
  18410. +des_cblock *output,
  18411. +long length,
  18412. +des_key_schedule ks1,
  18413. +des_key_schedule ks2,
  18414. +des_key_schedule ks3,
  18415. +des_cblock *ivec,
  18416. +int enc);
  18417. + This function implements inner triple CBC DES encryption with 3
  18418. + keys. What this means is that each 'DES' operation
  18419. + inside the cbc mode is really an C=E(ks3,D(ks2,E(ks1,M))).
  18420. + Again, this is cbc mode so an ivec is requires.
  18421. + This mode is used by SSL.
  18422. + There is also a des_ede2_cbc_encrypt() that only uses 2
  18423. + des_key_schedule's, the first being reused for the final
  18424. + encryption. C=E(ks1,D(ks2,E(ks1,M))). This form of triple DES
  18425. + is used by the RSAref library.
  18426. +
  18427. +void des_pcbc_encrypt(
  18428. +des_cblock *input,
  18429. +des_cblock *output,
  18430. +long length,
  18431. +des_key_schedule ks,
  18432. +des_cblock *ivec,
  18433. +int enc);
  18434. + This is Propagating Cipher Block Chaining mode of DES. It is used
  18435. + by Kerberos v4. It's parameters are the same as des_ncbc_encrypt().
  18436. +
  18437. +void des_cfb_encrypt(
  18438. +unsigned char *in,
  18439. +unsigned char *out,
  18440. +int numbits,
  18441. +long length,
  18442. +des_key_schedule ks,
  18443. +des_cblock *ivec,
  18444. +int enc);
  18445. + Cipher Feedback Back mode of DES. This implementation 'feeds back'
  18446. + in numbit blocks. The input (and output) is in multiples of numbits
  18447. + bits. numbits should to be a multiple of 8 bits. Length is the
  18448. + number of bytes input. If numbits is not a multiple of 8 bits,
  18449. + the extra bits in the bytes will be considered padding. So if
  18450. + numbits is 12, for each 2 input bytes, the 4 high bits of the
  18451. + second byte will be ignored. So to encode 72 bits when using
  18452. + a numbits of 12 take 12 bytes. To encode 72 bits when using
  18453. + numbits of 9 will take 16 bytes. To encode 80 bits when using
  18454. + numbits of 16 will take 10 bytes. etc, etc. This padding will
  18455. + apply to both input and output.
  18456. +
  18457. +
  18458. +void des_cfb64_encrypt(
  18459. +unsigned char *in,
  18460. +unsigned char *out,
  18461. +long length,
  18462. +des_key_schedule ks,
  18463. +des_cblock *ivec,
  18464. +int *num,
  18465. +int enc);
  18466. + This is one of the more useful functions in this DES library, it
  18467. + implements CFB mode of DES with 64bit feedback. Why is this
  18468. + useful you ask? Because this routine will allow you to encrypt an
  18469. + arbitrary number of bytes, no 8 byte padding. Each call to this
  18470. + routine will encrypt the input bytes to output and then update ivec
  18471. + and num. num contains 'how far' we are though ivec. If this does
  18472. + not make much sense, read more about cfb mode of DES :-).
  18473. +
  18474. +void des_ede3_cfb64_encrypt(
  18475. +unsigned char *in,
  18476. +unsigned char *out,
  18477. +long length,
  18478. +des_key_schedule ks1,
  18479. +des_key_schedule ks2,
  18480. +des_key_schedule ks3,
  18481. +des_cblock *ivec,
  18482. +int *num,
  18483. +int enc);
  18484. + Same as des_cfb64_encrypt() accept that the DES operation is
  18485. + triple DES. As usual, there is a macro for
  18486. + des_ede2_cfb64_encrypt() which reuses ks1.
  18487. +
  18488. +void des_ofb_encrypt(
  18489. +unsigned char *in,
  18490. +unsigned char *out,
  18491. +int numbits,
  18492. +long length,
  18493. +des_key_schedule ks,
  18494. +des_cblock *ivec);
  18495. + This is a implementation of Output Feed Back mode of DES. It is
  18496. + the same as des_cfb_encrypt() in that numbits is the size of the
  18497. + units dealt with during input and output (in bits).
  18498. +
  18499. +void des_ofb64_encrypt(
  18500. +unsigned char *in,
  18501. +unsigned char *out,
  18502. +long length,
  18503. +des_key_schedule ks,
  18504. +des_cblock *ivec,
  18505. +int *num);
  18506. + The same as des_cfb64_encrypt() except that it is Output Feed Back
  18507. + mode.
  18508. +
  18509. +void des_ede3_ofb64_encrypt(
  18510. +unsigned char *in,
  18511. +unsigned char *out,
  18512. +long length,
  18513. +des_key_schedule ks1,
  18514. +des_key_schedule ks2,
  18515. +des_key_schedule ks3,
  18516. +des_cblock *ivec,
  18517. +int *num);
  18518. + Same as des_ofb64_encrypt() accept that the DES operation is
  18519. + triple DES. As usual, there is a macro for
  18520. + des_ede2_ofb64_encrypt() which reuses ks1.
  18521. +
  18522. +int des_read_pw_string(
  18523. +char *buf,
  18524. +int length,
  18525. +char *prompt,
  18526. +int verify);
  18527. + This routine is used to get a password from the terminal with echo
  18528. + turned off. Buf is where the string will end up and length is the
  18529. + size of buf. Prompt is a string presented to the 'user' and if
  18530. + verify is set, the key is asked for twice and unless the 2 copies
  18531. + match, an error is returned. A return code of -1 indicates a
  18532. + system error, 1 failure due to use interaction, and 0 is success.
  18533. +
  18534. +unsigned long des_cbc_cksum(
  18535. +des_cblock *input,
  18536. +des_cblock *output,
  18537. +long length,
  18538. +des_key_schedule ks,
  18539. +des_cblock *ivec);
  18540. + This function produces an 8 byte checksum from input that it puts in
  18541. + output and returns the last 4 bytes as a long. The checksum is
  18542. + generated via cbc mode of DES in which only the last 8 byes are
  18543. + kept. I would recommend not using this function but instead using
  18544. + the EVP_Digest routines, or at least using MD5 or SHA. This
  18545. + function is used by Kerberos v4 so that is why it stays in the
  18546. + library.
  18547. +
  18548. +char *des_fcrypt(
  18549. +const char *buf,
  18550. +const char *salt
  18551. +char *ret);
  18552. + This is my fast version of the unix crypt(3) function. This version
  18553. + takes only a small amount of space relative to other fast
  18554. + crypt() implementations. This is different to the normal crypt
  18555. + in that the third parameter is the buffer that the return value
  18556. + is written into. It needs to be at least 14 bytes long. This
  18557. + function is thread safe, unlike the normal crypt.
  18558. +
  18559. +char *crypt(
  18560. +const char *buf,
  18561. +const char *salt);
  18562. + This function calls des_fcrypt() with a static array passed as the
  18563. + third parameter. This emulates the normal non-thread safe semantics
  18564. + of crypt(3).
  18565. +
  18566. +void des_string_to_key(
  18567. +char *str,
  18568. +des_cblock *key);
  18569. + This function takes str and converts it into a DES key. I would
  18570. + recommend using MD5 instead and use the first 8 bytes of output.
  18571. + When I wrote the first version of these routines back in 1990, MD5
  18572. + did not exist but I feel these routines are still sound. This
  18573. + routines is compatible with the one in MIT's libdes.
  18574. +
  18575. +void des_string_to_2keys(
  18576. +char *str,
  18577. +des_cblock *key1,
  18578. +des_cblock *key2);
  18579. + This function takes str and converts it into 2 DES keys.
  18580. + I would recommend using MD5 and using the 16 bytes as the 2 keys.
  18581. + I have nothing against these 2 'string_to_key' routines, it's just
  18582. + that if you say that your encryption key is generated by using the
  18583. + 16 bytes of an MD5 hash, every-one knows how you generated your
  18584. + keys.
  18585. +
  18586. +int des_read_password(
  18587. +des_cblock *key,
  18588. +char *prompt,
  18589. +int verify);
  18590. + This routine combines des_read_pw_string() with des_string_to_key().
  18591. +
  18592. +int des_read_2passwords(
  18593. +des_cblock *key1,
  18594. +des_cblock *key2,
  18595. +char *prompt,
  18596. +int verify);
  18597. + This routine combines des_read_pw_string() with des_string_to_2key().
  18598. +
  18599. +void des_random_seed(
  18600. +des_cblock key);
  18601. + This routine sets a starting point for des_random_key().
  18602. +
  18603. +void des_random_key(
  18604. +des_cblock ret);
  18605. + This function return a random key. Make sure to 'seed' the random
  18606. + number generator (with des_random_seed()) before using this function.
  18607. + I personally now use a MD5 based random number system.
  18608. +
  18609. +int des_enc_read(
  18610. +int fd,
  18611. +char *buf,
  18612. +int len,
  18613. +des_key_schedule ks,
  18614. +des_cblock *iv);
  18615. + This function will write to a file descriptor the encrypted data
  18616. + from buf. This data will be preceded by a 4 byte 'byte count' and
  18617. + will be padded out to 8 bytes. The encryption is either CBC of
  18618. + PCBC depending on the value of des_rw_mode. If it is DES_PCBC_MODE,
  18619. + pcbc is used, if DES_CBC_MODE, cbc is used. The default is to use
  18620. + DES_PCBC_MODE.
  18621. +
  18622. +int des_enc_write(
  18623. +int fd,
  18624. +char *buf,
  18625. +int len,
  18626. +des_key_schedule ks,
  18627. +des_cblock *iv);
  18628. + This routines read stuff written by des_enc_read() and decrypts it.
  18629. + I have used these routines quite a lot but I don't believe they are
  18630. + suitable for non-blocking io. If you are after a full
  18631. + authentication/encryption over networks, have a look at SSL instead.
  18632. +
  18633. +unsigned long des_quad_cksum(
  18634. +des_cblock *input,
  18635. +des_cblock *output,
  18636. +long length,
  18637. +int out_count,
  18638. +des_cblock *seed);
  18639. + This is a function from Kerberos v4 that is not anything to do with
  18640. + DES but was needed. It is a cksum that is quicker to generate than
  18641. + des_cbc_cksum(); I personally would use MD5 routines now.
  18642. +=====
  18643. +Modes of DES
  18644. +Quite a bit of the following information has been taken from
  18645. + AS 2805.5.2
  18646. + Australian Standard
  18647. + Electronic funds transfer - Requirements for interfaces,
  18648. + Part 5.2: Modes of operation for an n-bit block cipher algorithm
  18649. + Appendix A
  18650. +
  18651. +There are several different modes in which DES can be used, they are
  18652. +as follows.
  18653. +
  18654. +Electronic Codebook Mode (ECB) (des_ecb_encrypt())
  18655. +- 64 bits are enciphered at a time.
  18656. +- The order of the blocks can be rearranged without detection.
  18657. +- The same plaintext block always produces the same ciphertext block
  18658. + (for the same key) making it vulnerable to a 'dictionary attack'.
  18659. +- An error will only affect one ciphertext block.
  18660. +
  18661. +Cipher Block Chaining Mode (CBC) (des_cbc_encrypt())
  18662. +- a multiple of 64 bits are enciphered at a time.
  18663. +- The CBC mode produces the same ciphertext whenever the same
  18664. + plaintext is encrypted using the same key and starting variable.
  18665. +- The chaining operation makes the ciphertext blocks dependent on the
  18666. + current and all preceding plaintext blocks and therefore blocks can not
  18667. + be rearranged.
  18668. +- The use of different starting variables prevents the same plaintext
  18669. + enciphering to the same ciphertext.
  18670. +- An error will affect the current and the following ciphertext blocks.
  18671. +
  18672. +Cipher Feedback Mode (CFB) (des_cfb_encrypt())
  18673. +- a number of bits (j) <= 64 are enciphered at a time.
  18674. +- The CFB mode produces the same ciphertext whenever the same
  18675. + plaintext is encrypted using the same key and starting variable.
  18676. +- The chaining operation makes the ciphertext variables dependent on the
  18677. + current and all preceding variables and therefore j-bit variables are
  18678. + chained together and can not be rearranged.
  18679. +- The use of different starting variables prevents the same plaintext
  18680. + enciphering to the same ciphertext.
  18681. +- The strength of the CFB mode depends on the size of k (maximal if
  18682. + j == k). In my implementation this is always the case.
  18683. +- Selection of a small value for j will require more cycles through
  18684. + the encipherment algorithm per unit of plaintext and thus cause
  18685. + greater processing overheads.
  18686. +- Only multiples of j bits can be enciphered.
  18687. +- An error will affect the current and the following ciphertext variables.
  18688. +
  18689. +Output Feedback Mode (OFB) (des_ofb_encrypt())
  18690. +- a number of bits (j) <= 64 are enciphered at a time.
  18691. +- The OFB mode produces the same ciphertext whenever the same
  18692. + plaintext enciphered using the same key and starting variable. More
  18693. + over, in the OFB mode the same key stream is produced when the same
  18694. + key and start variable are used. Consequently, for security reasons
  18695. + a specific start variable should be used only once for a given key.
  18696. +- The absence of chaining makes the OFB more vulnerable to specific attacks.
  18697. +- The use of different start variables values prevents the same
  18698. + plaintext enciphering to the same ciphertext, by producing different
  18699. + key streams.
  18700. +- Selection of a small value for j will require more cycles through
  18701. + the encipherment algorithm per unit of plaintext and thus cause
  18702. + greater processing overheads.
  18703. +- Only multiples of j bits can be enciphered.
  18704. +- OFB mode of operation does not extend ciphertext errors in the
  18705. + resultant plaintext output. Every bit error in the ciphertext causes
  18706. + only one bit to be in error in the deciphered plaintext.
  18707. +- OFB mode is not self-synchronising. If the two operation of
  18708. + encipherment and decipherment get out of synchronism, the system needs
  18709. + to be re-initialised.
  18710. +- Each re-initialisation should use a value of the start variable
  18711. + different from the start variable values used before with the same
  18712. + key. The reason for this is that an identical bit stream would be
  18713. + produced each time from the same parameters. This would be
  18714. + susceptible to a ' known plaintext' attack.
  18715. +
  18716. +Triple ECB Mode (des_ecb3_encrypt())
  18717. +- Encrypt with key1, decrypt with key2 and encrypt with key3 again.
  18718. +- As for ECB encryption but increases the key length to 168 bits.
  18719. + There are theoretic attacks that can be used that make the effective
  18720. + key length 112 bits, but this attack also requires 2^56 blocks of
  18721. + memory, not very likely, even for the NSA.
  18722. +- If both keys are the same it is equivalent to encrypting once with
  18723. + just one key.
  18724. +- If the first and last key are the same, the key length is 112 bits.
  18725. + There are attacks that could reduce the key space to 55 bit's but it
  18726. + requires 2^56 blocks of memory.
  18727. +- If all 3 keys are the same, this is effectively the same as normal
  18728. + ecb mode.
  18729. +
  18730. +Triple CBC Mode (des_ede3_cbc_encrypt())
  18731. +- Encrypt with key1, decrypt with key2 and then encrypt with key3.
  18732. +- As for CBC encryption but increases the key length to 168 bits with
  18733. + the same restrictions as for triple ecb mode.
  18734. --- /dev/null Tue Mar 11 13:02:56 2003
  18735. +++ linux/net/ipsec/des/des_enc.c Mon Feb 9 13:51:03 2004
  18736. @@ -0,0 +1,502 @@
  18737. +/* crypto/des/des_enc.c */
  18738. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  18739. + * All rights reserved.
  18740. + *
  18741. + * This package is an SSL implementation written
  18742. + * by Eric Young (eay@cryptsoft.com).
  18743. + * The implementation was written so as to conform with Netscapes SSL.
  18744. + *
  18745. + * This library is free for commercial and non-commercial use as long as
  18746. + * the following conditions are aheared to. The following conditions
  18747. + * apply to all code found in this distribution, be it the RC4, RSA,
  18748. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18749. + * included with this distribution is covered by the same copyright terms
  18750. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  18751. + *
  18752. + * Copyright remains Eric Young's, and as such any Copyright notices in
  18753. + * the code are not to be removed.
  18754. + * If this package is used in a product, Eric Young should be given attribution
  18755. + * as the author of the parts of the library used.
  18756. + * This can be in the form of a textual message at program startup or
  18757. + * in documentation (online or textual) provided with the package.
  18758. + *
  18759. + * Redistribution and use in source and binary forms, with or without
  18760. + * modification, are permitted provided that the following conditions
  18761. + * are met:
  18762. + * 1. Redistributions of source code must retain the copyright
  18763. + * notice, this list of conditions and the following disclaimer.
  18764. + * 2. Redistributions in binary form must reproduce the above copyright
  18765. + * notice, this list of conditions and the following disclaimer in the
  18766. + * documentation and/or other materials provided with the distribution.
  18767. + * 3. All advertising materials mentioning features or use of this software
  18768. + * must display the following acknowledgement:
  18769. + * "This product includes cryptographic software written by
  18770. + * Eric Young (eay@cryptsoft.com)"
  18771. + * The word 'cryptographic' can be left out if the rouines from the library
  18772. + * being used are not cryptographic related :-).
  18773. + * 4. If you include any Windows specific code (or a derivative thereof) from
  18774. + * the apps directory (application code) you must include an acknowledgement:
  18775. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  18776. + *
  18777. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  18778. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18779. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18780. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18781. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18782. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18783. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18784. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18785. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18786. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18787. + * SUCH DAMAGE.
  18788. + *
  18789. + * The licence and distribution terms for any publically available version or
  18790. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  18791. + * copied and put under another distribution licence
  18792. + * [including the GNU Public Licence.]
  18793. + */
  18794. +
  18795. +#include "des/des_locl.h"
  18796. +
  18797. +void des_encrypt(data, ks, enc)
  18798. +DES_LONG *data;
  18799. +des_key_schedule ks;
  18800. +int enc;
  18801. + {
  18802. + register DES_LONG l,r,t,u;
  18803. +#ifdef DES_PTR
  18804. + register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  18805. +#endif
  18806. +#ifndef DES_UNROLL
  18807. + register int i;
  18808. +#endif
  18809. + register DES_LONG *s;
  18810. +
  18811. + r=data[0];
  18812. + l=data[1];
  18813. +
  18814. + IP(r,l);
  18815. + /* Things have been modified so that the initial rotate is
  18816. + * done outside the loop. This required the
  18817. + * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  18818. + * One perl script later and things have a 5% speed up on a sparc2.
  18819. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  18820. + * for pointing this out. */
  18821. + /* clear the top bits on machines with 8byte longs */
  18822. + /* shift left by 2 */
  18823. + r=ROTATE(r,29)&0xffffffffL;
  18824. + l=ROTATE(l,29)&0xffffffffL;
  18825. +
  18826. + s=(DES_LONG *)ks;
  18827. + /* I don't know if it is worth the effort of loop unrolling the
  18828. + * inner loop */
  18829. + if (enc)
  18830. + {
  18831. +#ifdef DES_UNROLL
  18832. + D_ENCRYPT(l,r, 0); /* 1 */
  18833. + D_ENCRYPT(r,l, 2); /* 2 */
  18834. + D_ENCRYPT(l,r, 4); /* 3 */
  18835. + D_ENCRYPT(r,l, 6); /* 4 */
  18836. + D_ENCRYPT(l,r, 8); /* 5 */
  18837. + D_ENCRYPT(r,l,10); /* 6 */
  18838. + D_ENCRYPT(l,r,12); /* 7 */
  18839. + D_ENCRYPT(r,l,14); /* 8 */
  18840. + D_ENCRYPT(l,r,16); /* 9 */
  18841. + D_ENCRYPT(r,l,18); /* 10 */
  18842. + D_ENCRYPT(l,r,20); /* 11 */
  18843. + D_ENCRYPT(r,l,22); /* 12 */
  18844. + D_ENCRYPT(l,r,24); /* 13 */
  18845. + D_ENCRYPT(r,l,26); /* 14 */
  18846. + D_ENCRYPT(l,r,28); /* 15 */
  18847. + D_ENCRYPT(r,l,30); /* 16 */
  18848. +#else
  18849. + for (i=0; i<32; i+=8)
  18850. + {
  18851. + D_ENCRYPT(l,r,i+0); /* 1 */
  18852. + D_ENCRYPT(r,l,i+2); /* 2 */
  18853. + D_ENCRYPT(l,r,i+4); /* 3 */
  18854. + D_ENCRYPT(r,l,i+6); /* 4 */
  18855. + }
  18856. +#endif
  18857. + }
  18858. + else
  18859. + {
  18860. +#ifdef DES_UNROLL
  18861. + D_ENCRYPT(l,r,30); /* 16 */
  18862. + D_ENCRYPT(r,l,28); /* 15 */
  18863. + D_ENCRYPT(l,r,26); /* 14 */
  18864. + D_ENCRYPT(r,l,24); /* 13 */
  18865. + D_ENCRYPT(l,r,22); /* 12 */
  18866. + D_ENCRYPT(r,l,20); /* 11 */
  18867. + D_ENCRYPT(l,r,18); /* 10 */
  18868. + D_ENCRYPT(r,l,16); /* 9 */
  18869. + D_ENCRYPT(l,r,14); /* 8 */
  18870. + D_ENCRYPT(r,l,12); /* 7 */
  18871. + D_ENCRYPT(l,r,10); /* 6 */
  18872. + D_ENCRYPT(r,l, 8); /* 5 */
  18873. + D_ENCRYPT(l,r, 6); /* 4 */
  18874. + D_ENCRYPT(r,l, 4); /* 3 */
  18875. + D_ENCRYPT(l,r, 2); /* 2 */
  18876. + D_ENCRYPT(r,l, 0); /* 1 */
  18877. +#else
  18878. + for (i=30; i>0; i-=8)
  18879. + {
  18880. + D_ENCRYPT(l,r,i-0); /* 16 */
  18881. + D_ENCRYPT(r,l,i-2); /* 15 */
  18882. + D_ENCRYPT(l,r,i-4); /* 14 */
  18883. + D_ENCRYPT(r,l,i-6); /* 13 */
  18884. + }
  18885. +#endif
  18886. + }
  18887. +
  18888. + /* rotate and clear the top bits on machines with 8byte longs */
  18889. + l=ROTATE(l,3)&0xffffffffL;
  18890. + r=ROTATE(r,3)&0xffffffffL;
  18891. +
  18892. + FP(r,l);
  18893. + data[0]=l;
  18894. + data[1]=r;
  18895. + l=r=t=u=0;
  18896. + }
  18897. +
  18898. +void des_encrypt2(data, ks, enc)
  18899. +DES_LONG *data;
  18900. +des_key_schedule ks;
  18901. +int enc;
  18902. + {
  18903. + register DES_LONG l,r,t,u;
  18904. +#ifdef DES_PTR
  18905. + register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  18906. +#endif
  18907. +#ifndef DES_UNROLL
  18908. + register int i;
  18909. +#endif
  18910. + register DES_LONG *s;
  18911. +
  18912. + r=data[0];
  18913. + l=data[1];
  18914. +
  18915. + /* Things have been modified so that the initial rotate is
  18916. + * done outside the loop. This required the
  18917. + * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  18918. + * One perl script later and things have a 5% speed up on a sparc2.
  18919. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  18920. + * for pointing this out. */
  18921. + /* clear the top bits on machines with 8byte longs */
  18922. + r=ROTATE(r,29)&0xffffffffL;
  18923. + l=ROTATE(l,29)&0xffffffffL;
  18924. +
  18925. + s=(DES_LONG *)ks;
  18926. + /* I don't know if it is worth the effort of loop unrolling the
  18927. + * inner loop */
  18928. + if (enc)
  18929. + {
  18930. +#ifdef DES_UNROLL
  18931. + D_ENCRYPT(l,r, 0); /* 1 */
  18932. + D_ENCRYPT(r,l, 2); /* 2 */
  18933. + D_ENCRYPT(l,r, 4); /* 3 */
  18934. + D_ENCRYPT(r,l, 6); /* 4 */
  18935. + D_ENCRYPT(l,r, 8); /* 5 */
  18936. + D_ENCRYPT(r,l,10); /* 6 */
  18937. + D_ENCRYPT(l,r,12); /* 7 */
  18938. + D_ENCRYPT(r,l,14); /* 8 */
  18939. + D_ENCRYPT(l,r,16); /* 9 */
  18940. + D_ENCRYPT(r,l,18); /* 10 */
  18941. + D_ENCRYPT(l,r,20); /* 11 */
  18942. + D_ENCRYPT(r,l,22); /* 12 */
  18943. + D_ENCRYPT(l,r,24); /* 13 */
  18944. + D_ENCRYPT(r,l,26); /* 14 */
  18945. + D_ENCRYPT(l,r,28); /* 15 */
  18946. + D_ENCRYPT(r,l,30); /* 16 */
  18947. +#else
  18948. + for (i=0; i<32; i+=8)
  18949. + {
  18950. + D_ENCRYPT(l,r,i+0); /* 1 */
  18951. + D_ENCRYPT(r,l,i+2); /* 2 */
  18952. + D_ENCRYPT(l,r,i+4); /* 3 */
  18953. + D_ENCRYPT(r,l,i+6); /* 4 */
  18954. + }
  18955. +#endif
  18956. + }
  18957. + else
  18958. + {
  18959. +#ifdef DES_UNROLL
  18960. + D_ENCRYPT(l,r,30); /* 16 */
  18961. + D_ENCRYPT(r,l,28); /* 15 */
  18962. + D_ENCRYPT(l,r,26); /* 14 */
  18963. + D_ENCRYPT(r,l,24); /* 13 */
  18964. + D_ENCRYPT(l,r,22); /* 12 */
  18965. + D_ENCRYPT(r,l,20); /* 11 */
  18966. + D_ENCRYPT(l,r,18); /* 10 */
  18967. + D_ENCRYPT(r,l,16); /* 9 */
  18968. + D_ENCRYPT(l,r,14); /* 8 */
  18969. + D_ENCRYPT(r,l,12); /* 7 */
  18970. + D_ENCRYPT(l,r,10); /* 6 */
  18971. + D_ENCRYPT(r,l, 8); /* 5 */
  18972. + D_ENCRYPT(l,r, 6); /* 4 */
  18973. + D_ENCRYPT(r,l, 4); /* 3 */
  18974. + D_ENCRYPT(l,r, 2); /* 2 */
  18975. + D_ENCRYPT(r,l, 0); /* 1 */
  18976. +#else
  18977. + for (i=30; i>0; i-=8)
  18978. + {
  18979. + D_ENCRYPT(l,r,i-0); /* 16 */
  18980. + D_ENCRYPT(r,l,i-2); /* 15 */
  18981. + D_ENCRYPT(l,r,i-4); /* 14 */
  18982. + D_ENCRYPT(r,l,i-6); /* 13 */
  18983. + }
  18984. +#endif
  18985. + }
  18986. + /* rotate and clear the top bits on machines with 8byte longs */
  18987. + data[0]=ROTATE(l,3)&0xffffffffL;
  18988. + data[1]=ROTATE(r,3)&0xffffffffL;
  18989. + l=r=t=u=0;
  18990. + }
  18991. +
  18992. +void des_encrypt3(data,ks1,ks2,ks3)
  18993. +DES_LONG *data;
  18994. +des_key_schedule ks1;
  18995. +des_key_schedule ks2;
  18996. +des_key_schedule ks3;
  18997. + {
  18998. + register DES_LONG l,r;
  18999. +
  19000. + l=data[0];
  19001. + r=data[1];
  19002. + IP(l,r);
  19003. + data[0]=l;
  19004. + data[1]=r;
  19005. + des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
  19006. + des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
  19007. + des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
  19008. + l=data[0];
  19009. + r=data[1];
  19010. + FP(r,l);
  19011. + data[0]=l;
  19012. + data[1]=r;
  19013. + }
  19014. +
  19015. +void des_decrypt3(data,ks1,ks2,ks3)
  19016. +DES_LONG *data;
  19017. +des_key_schedule ks1;
  19018. +des_key_schedule ks2;
  19019. +des_key_schedule ks3;
  19020. + {
  19021. + register DES_LONG l,r;
  19022. +
  19023. + l=data[0];
  19024. + r=data[1];
  19025. + IP(l,r);
  19026. + data[0]=l;
  19027. + data[1]=r;
  19028. + des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
  19029. + des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
  19030. + des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
  19031. + l=data[0];
  19032. + r=data[1];
  19033. + FP(r,l);
  19034. + data[0]=l;
  19035. + data[1]=r;
  19036. + }
  19037. +
  19038. +#ifndef DES_DEFAULT_OPTIONS
  19039. +
  19040. +void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
  19041. +des_cblock (*input);
  19042. +des_cblock (*output);
  19043. +long length;
  19044. +des_key_schedule schedule;
  19045. +des_cblock (*ivec);
  19046. +int enc;
  19047. + {
  19048. + register DES_LONG tin0,tin1;
  19049. + register DES_LONG tout0,tout1,xor0,xor1;
  19050. + register unsigned char *in,*out;
  19051. + register long l=length;
  19052. + DES_LONG tin[2];
  19053. + unsigned char *iv;
  19054. +
  19055. + in=(unsigned char *)input;
  19056. + out=(unsigned char *)output;
  19057. + iv=(unsigned char *)ivec;
  19058. +
  19059. + if (enc)
  19060. + {
  19061. + c2l(iv,tout0);
  19062. + c2l(iv,tout1);
  19063. + for (l-=8; l>=0; l-=8)
  19064. + {
  19065. + c2l(in,tin0);
  19066. + c2l(in,tin1);
  19067. + tin0^=tout0; tin[0]=tin0;
  19068. + tin1^=tout1; tin[1]=tin1;
  19069. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  19070. + tout0=tin[0]; l2c(tout0,out);
  19071. + tout1=tin[1]; l2c(tout1,out);
  19072. + }
  19073. + if (l != -8)
  19074. + {
  19075. + c2ln(in,tin0,tin1,l+8);
  19076. + tin0^=tout0; tin[0]=tin0;
  19077. + tin1^=tout1; tin[1]=tin1;
  19078. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  19079. + tout0=tin[0]; l2c(tout0,out);
  19080. + tout1=tin[1]; l2c(tout1,out);
  19081. + }
  19082. + iv=(unsigned char *)ivec;
  19083. + l2c(tout0,iv);
  19084. + l2c(tout1,iv);
  19085. + }
  19086. + else
  19087. + {
  19088. + c2l(iv,xor0);
  19089. + c2l(iv,xor1);
  19090. + for (l-=8; l>=0; l-=8)
  19091. + {
  19092. + c2l(in,tin0); tin[0]=tin0;
  19093. + c2l(in,tin1); tin[1]=tin1;
  19094. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  19095. + tout0=tin[0]^xor0;
  19096. + tout1=tin[1]^xor1;
  19097. + l2c(tout0,out);
  19098. + l2c(tout1,out);
  19099. + xor0=tin0;
  19100. + xor1=tin1;
  19101. + }
  19102. + if (l != -8)
  19103. + {
  19104. + c2l(in,tin0); tin[0]=tin0;
  19105. + c2l(in,tin1); tin[1]=tin1;
  19106. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  19107. + tout0=tin[0]^xor0;
  19108. + tout1=tin[1]^xor1;
  19109. + l2cn(tout0,tout1,out,l+8);
  19110. + xor0=tin0;
  19111. + xor1=tin1;
  19112. + }
  19113. +
  19114. + iv=(unsigned char *)ivec;
  19115. + l2c(xor0,iv);
  19116. + l2c(xor1,iv);
  19117. + }
  19118. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  19119. + tin[0]=tin[1]=0;
  19120. + }
  19121. +
  19122. +void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, enc)
  19123. +des_cblock (*input);
  19124. +des_cblock (*output);
  19125. +long length;
  19126. +des_key_schedule ks1;
  19127. +des_key_schedule ks2;
  19128. +des_key_schedule ks3;
  19129. +des_cblock (*ivec);
  19130. +int enc;
  19131. + {
  19132. + register DES_LONG tin0,tin1;
  19133. + register DES_LONG tout0,tout1,xor0,xor1;
  19134. + register unsigned char *in,*out;
  19135. + register long l=length;
  19136. + DES_LONG tin[2];
  19137. + unsigned char *iv;
  19138. +
  19139. + in=(unsigned char *)input;
  19140. + out=(unsigned char *)output;
  19141. + iv=(unsigned char *)ivec;
  19142. +
  19143. + if (enc)
  19144. + {
  19145. + c2l(iv,tout0);
  19146. + c2l(iv,tout1);
  19147. + for (l-=8; l>=0; l-=8)
  19148. + {
  19149. + c2l(in,tin0);
  19150. + c2l(in,tin1);
  19151. + tin0^=tout0;
  19152. + tin1^=tout1;
  19153. +
  19154. + tin[0]=tin0;
  19155. + tin[1]=tin1;
  19156. + des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19157. + tout0=tin[0];
  19158. + tout1=tin[1];
  19159. +
  19160. + l2c(tout0,out);
  19161. + l2c(tout1,out);
  19162. + }
  19163. + if (l != -8)
  19164. + {
  19165. + c2ln(in,tin0,tin1,l+8);
  19166. + tin0^=tout0;
  19167. + tin1^=tout1;
  19168. +
  19169. + tin[0]=tin0;
  19170. + tin[1]=tin1;
  19171. + des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19172. + tout0=tin[0];
  19173. + tout1=tin[1];
  19174. +
  19175. + l2c(tout0,out);
  19176. + l2c(tout1,out);
  19177. + }
  19178. + iv=(unsigned char *)ivec;
  19179. + l2c(tout0,iv);
  19180. + l2c(tout1,iv);
  19181. + }
  19182. + else
  19183. + {
  19184. + register DES_LONG t0,t1;
  19185. +
  19186. + c2l(iv,xor0);
  19187. + c2l(iv,xor1);
  19188. + for (l-=8; l>=0; l-=8)
  19189. + {
  19190. + c2l(in,tin0);
  19191. + c2l(in,tin1);
  19192. +
  19193. + t0=tin0;
  19194. + t1=tin1;
  19195. +
  19196. + tin[0]=tin0;
  19197. + tin[1]=tin1;
  19198. + des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19199. + tout0=tin[0];
  19200. + tout1=tin[1];
  19201. +
  19202. + tout0^=xor0;
  19203. + tout1^=xor1;
  19204. + l2c(tout0,out);
  19205. + l2c(tout1,out);
  19206. + xor0=t0;
  19207. + xor1=t1;
  19208. + }
  19209. + if (l != -8)
  19210. + {
  19211. + c2l(in,tin0);
  19212. + c2l(in,tin1);
  19213. +
  19214. + t0=tin0;
  19215. + t1=tin1;
  19216. +
  19217. + tin[0]=tin0;
  19218. + tin[1]=tin1;
  19219. + des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19220. + tout0=tin[0];
  19221. + tout1=tin[1];
  19222. +
  19223. + tout0^=xor0;
  19224. + tout1^=xor1;
  19225. + l2cn(tout0,tout1,out,l+8);
  19226. + xor0=t0;
  19227. + xor1=t1;
  19228. + }
  19229. +
  19230. + iv=(unsigned char *)ivec;
  19231. + l2c(xor0,iv);
  19232. + l2c(xor1,iv);
  19233. + }
  19234. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  19235. + tin[0]=tin[1]=0;
  19236. + }
  19237. +
  19238. +#endif /* DES_DEFAULT_OPTIONS */
  19239. --- /dev/null Tue Mar 11 13:02:56 2003
  19240. +++ linux/net/ipsec/des/des_opts.c Mon Feb 9 13:51:03 2004
  19241. @@ -0,0 +1,620 @@
  19242. +/* crypto/des/des_opts.c */
  19243. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  19244. + * All rights reserved.
  19245. + *
  19246. + * This package is an SSL implementation written
  19247. + * by Eric Young (eay@cryptsoft.com).
  19248. + * The implementation was written so as to conform with Netscapes SSL.
  19249. + *
  19250. + * This library is free for commercial and non-commercial use as long as
  19251. + * the following conditions are aheared to. The following conditions
  19252. + * apply to all code found in this distribution, be it the RC4, RSA,
  19253. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  19254. + * included with this distribution is covered by the same copyright terms
  19255. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  19256. + *
  19257. + * Copyright remains Eric Young's, and as such any Copyright notices in
  19258. + * the code are not to be removed.
  19259. + * If this package is used in a product, Eric Young should be given attribution
  19260. + * as the author of the parts of the library used.
  19261. + * This can be in the form of a textual message at program startup or
  19262. + * in documentation (online or textual) provided with the package.
  19263. + *
  19264. + * Redistribution and use in source and binary forms, with or without
  19265. + * modification, are permitted provided that the following conditions
  19266. + * are met:
  19267. + * 1. Redistributions of source code must retain the copyright
  19268. + * notice, this list of conditions and the following disclaimer.
  19269. + * 2. Redistributions in binary form must reproduce the above copyright
  19270. + * notice, this list of conditions and the following disclaimer in the
  19271. + * documentation and/or other materials provided with the distribution.
  19272. + * 3. All advertising materials mentioning features or use of this software
  19273. + * must display the following acknowledgement:
  19274. + * "This product includes cryptographic software written by
  19275. + * Eric Young (eay@cryptsoft.com)"
  19276. + * The word 'cryptographic' can be left out if the rouines from the library
  19277. + * being used are not cryptographic related :-).
  19278. + * 4. If you include any Windows specific code (or a derivative thereof) from
  19279. + * the apps directory (application code) you must include an acknowledgement:
  19280. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  19281. + *
  19282. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  19283. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19284. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19285. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19286. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19287. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19288. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19289. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19290. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19291. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  19292. + * SUCH DAMAGE.
  19293. + *
  19294. + * The licence and distribution terms for any publically available version or
  19295. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  19296. + * copied and put under another distribution licence
  19297. + * [including the GNU Public Licence.]
  19298. + */
  19299. +
  19300. +/* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
  19301. + * This is for machines with 64k code segment size restrictions. */
  19302. +
  19303. +#ifndef MSDOS
  19304. +#define TIMES
  19305. +#endif
  19306. +
  19307. +#include <stdio.h>
  19308. +#ifndef MSDOS
  19309. +#include <unistd.h>
  19310. +#else
  19311. +#include <io.h>
  19312. +extern void exit();
  19313. +#endif
  19314. +#include <signal.h>
  19315. +#ifndef VMS
  19316. +#ifndef _IRIX
  19317. +#include <time.h>
  19318. +#endif
  19319. +#ifdef TIMES
  19320. +#include <sys/types.h>
  19321. +#include <sys/times.h>
  19322. +#endif
  19323. +#else /* VMS */
  19324. +#include <types.h>
  19325. +struct tms {
  19326. + time_t tms_utime;
  19327. + time_t tms_stime;
  19328. + time_t tms_uchild; /* I dunno... */
  19329. + time_t tms_uchildsys; /* so these names are a guess :-) */
  19330. + }
  19331. +#endif
  19332. +#ifndef TIMES
  19333. +#include <sys/timeb.h>
  19334. +#endif
  19335. +
  19336. +#ifdef sun
  19337. +#include <limits.h>
  19338. +#include <sys/param.h>
  19339. +#endif
  19340. +
  19341. +#include "des/des_locl.h"
  19342. +#include "des/spr.h"
  19343. +
  19344. +#define DES_DEFAULT_OPTIONS
  19345. +
  19346. +#if !defined(PART1) && !defined(PART2) && !defined(PART3) && !defined(PART4)
  19347. +#define PART1
  19348. +#define PART2
  19349. +#define PART3
  19350. +#define PART4
  19351. +#endif
  19352. +
  19353. +#ifdef PART1
  19354. +
  19355. +#undef DES_UNROLL
  19356. +#undef DES_RISC1
  19357. +#undef DES_RISC2
  19358. +#undef DES_PTR
  19359. +#undef D_ENCRYPT
  19360. +#define des_encrypt des_encrypt_u4_cisc_idx
  19361. +#define des_encrypt2 des_encrypt2_u4_cisc_idx
  19362. +#define des_encrypt3 des_encrypt3_u4_cisc_idx
  19363. +#define des_decrypt3 des_decrypt3_u4_cisc_idx
  19364. +#undef HEADER_DES_LOCL_H
  19365. +#include "des_enc.c"
  19366. +
  19367. +#define DES_UNROLL
  19368. +#undef DES_RISC1
  19369. +#undef DES_RISC2
  19370. +#undef DES_PTR
  19371. +#undef D_ENCRYPT
  19372. +#undef des_encrypt
  19373. +#undef des_encrypt2
  19374. +#undef des_encrypt3
  19375. +#undef des_decrypt3
  19376. +#define des_encrypt des_encrypt_u16_cisc_idx
  19377. +#define des_encrypt2 des_encrypt2_u16_cisc_idx
  19378. +#define des_encrypt3 des_encrypt3_u16_cisc_idx
  19379. +#define des_decrypt3 des_decrypt3_u16_cisc_idx
  19380. +#undef HEADER_DES_LOCL_H
  19381. +#include "des_enc.c"
  19382. +
  19383. +#undef DES_UNROLL
  19384. +#define DES_RISC1
  19385. +#undef DES_RISC2
  19386. +#undef DES_PTR
  19387. +#undef D_ENCRYPT
  19388. +#undef des_encrypt
  19389. +#undef des_encrypt2
  19390. +#undef des_encrypt3
  19391. +#undef des_decrypt3
  19392. +#define des_encrypt des_encrypt_u4_risc1_idx
  19393. +#define des_encrypt2 des_encrypt2_u4_risc1_idx
  19394. +#define des_encrypt3 des_encrypt3_u4_risc1_idx
  19395. +#define des_decrypt3 des_decrypt3_u4_risc1_idx
  19396. +#undef HEADER_DES_LOCL_H
  19397. +#include "des_enc.c"
  19398. +
  19399. +#endif
  19400. +
  19401. +#ifdef PART2
  19402. +
  19403. +#undef DES_UNROLL
  19404. +#undef DES_RISC1
  19405. +#define DES_RISC2
  19406. +#undef DES_PTR
  19407. +#undef D_ENCRYPT
  19408. +#undef des_encrypt
  19409. +#undef des_encrypt2
  19410. +#undef des_encrypt3
  19411. +#undef des_decrypt3
  19412. +#define des_encrypt des_encrypt_u4_risc2_idx
  19413. +#define des_encrypt2 des_encrypt2_u4_risc2_idx
  19414. +#define des_encrypt3 des_encrypt3_u4_risc2_idx
  19415. +#define des_decrypt3 des_decrypt3_u4_risc2_idx
  19416. +#undef HEADER_DES_LOCL_H
  19417. +#include "des_enc.c"
  19418. +
  19419. +#define DES_UNROLL
  19420. +#define DES_RISC1
  19421. +#undef DES_RISC2
  19422. +#undef DES_PTR
  19423. +#undef D_ENCRYPT
  19424. +#undef des_encrypt
  19425. +#undef des_encrypt2
  19426. +#undef des_encrypt3
  19427. +#undef des_decrypt3
  19428. +#define des_encrypt des_encrypt_u16_risc1_idx
  19429. +#define des_encrypt2 des_encrypt2_u16_risc1_idx
  19430. +#define des_encrypt3 des_encrypt3_u16_risc1_idx
  19431. +#define des_decrypt3 des_decrypt3_u16_risc1_idx
  19432. +#undef HEADER_DES_LOCL_H
  19433. +#include "des_enc.c"
  19434. +
  19435. +#define DES_UNROLL
  19436. +#undef DES_RISC1
  19437. +#define DES_RISC2
  19438. +#undef DES_PTR
  19439. +#undef D_ENCRYPT
  19440. +#undef des_encrypt
  19441. +#undef des_encrypt2
  19442. +#undef des_encrypt3
  19443. +#undef des_decrypt3
  19444. +#define des_encrypt des_encrypt_u16_risc2_idx
  19445. +#define des_encrypt2 des_encrypt2_u16_risc2_idx
  19446. +#define des_encrypt3 des_encrypt3_u16_risc2_idx
  19447. +#define des_decrypt3 des_decrypt3_u16_risc2_idx
  19448. +#undef HEADER_DES_LOCL_H
  19449. +#include "des_enc.c"
  19450. +
  19451. +#endif
  19452. +
  19453. +#ifdef PART3
  19454. +
  19455. +#undef DES_UNROLL
  19456. +#undef DES_RISC1
  19457. +#undef DES_RISC2
  19458. +#define DES_PTR
  19459. +#undef D_ENCRYPT
  19460. +#undef des_encrypt
  19461. +#undef des_encrypt2
  19462. +#undef des_encrypt3
  19463. +#undef des_decrypt3
  19464. +#define des_encrypt des_encrypt_u4_cisc_ptr
  19465. +#define des_encrypt2 des_encrypt2_u4_cisc_ptr
  19466. +#define des_encrypt3 des_encrypt3_u4_cisc_ptr
  19467. +#define des_decrypt3 des_decrypt3_u4_cisc_ptr
  19468. +#undef HEADER_DES_LOCL_H
  19469. +#include "des_enc.c"
  19470. +
  19471. +#define DES_UNROLL
  19472. +#undef DES_RISC1
  19473. +#undef DES_RISC2
  19474. +#define DES_PTR
  19475. +#undef D_ENCRYPT
  19476. +#undef des_encrypt
  19477. +#undef des_encrypt2
  19478. +#undef des_encrypt3
  19479. +#undef des_decrypt3
  19480. +#define des_encrypt des_encrypt_u16_cisc_ptr
  19481. +#define des_encrypt2 des_encrypt2_u16_cisc_ptr
  19482. +#define des_encrypt3 des_encrypt3_u16_cisc_ptr
  19483. +#define des_decrypt3 des_decrypt3_u16_cisc_ptr
  19484. +#undef HEADER_DES_LOCL_H
  19485. +#include "des_enc.c"
  19486. +
  19487. +#undef DES_UNROLL
  19488. +#define DES_RISC1
  19489. +#undef DES_RISC2
  19490. +#define DES_PTR
  19491. +#undef D_ENCRYPT
  19492. +#undef des_encrypt
  19493. +#undef des_encrypt2
  19494. +#undef des_encrypt3
  19495. +#undef des_decrypt3
  19496. +#define des_encrypt des_encrypt_u4_risc1_ptr
  19497. +#define des_encrypt2 des_encrypt2_u4_risc1_ptr
  19498. +#define des_encrypt3 des_encrypt3_u4_risc1_ptr
  19499. +#define des_decrypt3 des_decrypt3_u4_risc1_ptr
  19500. +#undef HEADER_DES_LOCL_H
  19501. +#include "des_enc.c"
  19502. +
  19503. +#endif
  19504. +
  19505. +#ifdef PART4
  19506. +
  19507. +#undef DES_UNROLL
  19508. +#undef DES_RISC1
  19509. +#define DES_RISC2
  19510. +#define DES_PTR
  19511. +#undef D_ENCRYPT
  19512. +#undef des_encrypt
  19513. +#undef des_encrypt2
  19514. +#undef des_encrypt3
  19515. +#undef des_decrypt3
  19516. +#define des_encrypt des_encrypt_u4_risc2_ptr
  19517. +#define des_encrypt2 des_encrypt2_u4_risc2_ptr
  19518. +#define des_encrypt3 des_encrypt3_u4_risc2_ptr
  19519. +#define des_decrypt3 des_decrypt3_u4_risc2_ptr
  19520. +#undef HEADER_DES_LOCL_H
  19521. +#include "des_enc.c"
  19522. +
  19523. +#define DES_UNROLL
  19524. +#define DES_RISC1
  19525. +#undef DES_RISC2
  19526. +#define DES_PTR
  19527. +#undef D_ENCRYPT
  19528. +#undef des_encrypt
  19529. +#undef des_encrypt2
  19530. +#undef des_encrypt3
  19531. +#undef des_decrypt3
  19532. +#define des_encrypt des_encrypt_u16_risc1_ptr
  19533. +#define des_encrypt2 des_encrypt2_u16_risc1_ptr
  19534. +#define des_encrypt3 des_encrypt3_u16_risc1_ptr
  19535. +#define des_decrypt3 des_decrypt3_u16_risc1_ptr
  19536. +#undef HEADER_DES_LOCL_H
  19537. +#include "des_enc.c"
  19538. +
  19539. +#define DES_UNROLL
  19540. +#undef DES_RISC1
  19541. +#define DES_RISC2
  19542. +#define DES_PTR
  19543. +#undef D_ENCRYPT
  19544. +#undef des_encrypt
  19545. +#undef des_encrypt2
  19546. +#undef des_encrypt3
  19547. +#undef des_decrypt3
  19548. +#define des_encrypt des_encrypt_u16_risc2_ptr
  19549. +#define des_encrypt2 des_encrypt2_u16_risc2_ptr
  19550. +#define des_encrypt3 des_encrypt3_u16_risc2_ptr
  19551. +#define des_decrypt3 des_decrypt3_u16_risc2_ptr
  19552. +#undef HEADER_DES_LOCL_H
  19553. +#include "des_enc.c"
  19554. +
  19555. +#endif
  19556. +
  19557. +/* The following if from times(3) man page. It may need to be changed */
  19558. +#ifndef HZ
  19559. +# ifndef CLK_TCK
  19560. +# ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
  19561. +# ifndef VMS
  19562. +# define HZ 100.0
  19563. +# else /* VMS */
  19564. +# define HZ 100.0
  19565. +# endif
  19566. +# else /* _BSD_CLK_TCK_ */
  19567. +# define HZ ((double)_BSD_CLK_TCK_)
  19568. +# endif
  19569. +# else /* CLK_TCK */
  19570. +# define HZ ((double)CLK_TCK)
  19571. +# endif
  19572. +#endif
  19573. +
  19574. +#define BUFSIZE ((long)1024)
  19575. +long run=0;
  19576. +
  19577. +#ifndef NOPROTO
  19578. +double Time_F(int s);
  19579. +#else
  19580. +double Time_F();
  19581. +#endif
  19582. +
  19583. +#ifdef SIGALRM
  19584. +#if defined(__STDC__) || defined(sgi)
  19585. +#define SIGRETTYPE void
  19586. +#else
  19587. +#define SIGRETTYPE int
  19588. +#endif
  19589. +
  19590. +#ifndef NOPROTO
  19591. +SIGRETTYPE sig_done(int sig);
  19592. +#else
  19593. +SIGRETTYPE sig_done();
  19594. +#endif
  19595. +
  19596. +SIGRETTYPE sig_done(sig)
  19597. +int sig;
  19598. + {
  19599. + signal(SIGALRM,sig_done);
  19600. + run=0;
  19601. +#ifdef LINT
  19602. + sig=sig;
  19603. +#endif
  19604. + }
  19605. +#endif
  19606. +
  19607. +#define START 0
  19608. +#define STOP 1
  19609. +
  19610. +double Time_F(s)
  19611. +int s;
  19612. + {
  19613. + double ret;
  19614. +#ifdef TIMES
  19615. + static struct tms tstart,tend;
  19616. +
  19617. + if (s == START)
  19618. + {
  19619. + times(&tstart);
  19620. + return(0);
  19621. + }
  19622. + else
  19623. + {
  19624. + times(&tend);
  19625. + ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
  19626. + return((ret == 0.0)?1e-6:ret);
  19627. + }
  19628. +#else /* !times() */
  19629. + static struct timeb tstart,tend;
  19630. + long i;
  19631. +
  19632. + if (s == START)
  19633. + {
  19634. + ftime(&tstart);
  19635. + return(0);
  19636. + }
  19637. + else
  19638. + {
  19639. + ftime(&tend);
  19640. + i=(long)tend.millitm-(long)tstart.millitm;
  19641. + ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
  19642. + return((ret == 0.0)?1e-6:ret);
  19643. + }
  19644. +#endif
  19645. + }
  19646. +
  19647. +#ifdef SIGALRM
  19648. +#define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
  19649. +#else
  19650. +#define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
  19651. +#endif
  19652. +
  19653. +#define time_it(func,name,index) \
  19654. + print_name(name); \
  19655. + Time_F(START); \
  19656. + for (count=0,run=1; COND(cb); count++) \
  19657. + { \
  19658. + unsigned long d[2]; \
  19659. + func(d,&(sch[0]),DES_ENCRYPT); \
  19660. + } \
  19661. + tm[index]=Time_F(STOP); \
  19662. + fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
  19663. + tm[index]=((double)COUNT(cb))/tm[index];
  19664. +
  19665. +#define print_it(name,index) \
  19666. + fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
  19667. + tm[index]*8,1.0e6/tm[index]);
  19668. +
  19669. +int main(argc,argv)
  19670. +int argc;
  19671. +char **argv;
  19672. + {
  19673. + long count;
  19674. + static unsigned char buf[BUFSIZE];
  19675. + static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
  19676. + static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
  19677. + static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
  19678. + des_key_schedule sch,sch2,sch3;
  19679. + double d,tm[16],max=0;
  19680. + int rank[16];
  19681. + char *str[16];
  19682. + int max_idx=0,i,num=0,j;
  19683. +#ifndef SIGALARM
  19684. + long ca,cb,cc,cd,ce;
  19685. +#endif
  19686. +
  19687. + for (i=0; i<12; i++)
  19688. + {
  19689. + tm[i]=0.0;
  19690. + rank[i]=0;
  19691. + }
  19692. +
  19693. +#ifndef TIMES
  19694. + fprintf(stderr,"To get the most acurate results, try to run this\n");
  19695. + fprintf(stderr,"program when this computer is idle.\n");
  19696. +#endif
  19697. +
  19698. + des_set_key((C_Block *)key,sch);
  19699. + des_set_key((C_Block *)key2,sch2);
  19700. + des_set_key((C_Block *)key3,sch3);
  19701. +
  19702. +#ifndef SIGALRM
  19703. + fprintf(stderr,"First we calculate the approximate speed ...\n");
  19704. + des_set_key((C_Block *)key,sch);
  19705. + count=10;
  19706. + do {
  19707. + long i;
  19708. + unsigned long data[2];
  19709. +
  19710. + count*=2;
  19711. + Time_F(START);
  19712. + for (i=count; i; i--)
  19713. + des_encrypt(data,&(sch[0]),DES_ENCRYPT);
  19714. + d=Time_F(STOP);
  19715. + } while (d < 3.0);
  19716. + ca=count;
  19717. + cb=count*3;
  19718. + cc=count*3*8/BUFSIZE+1;
  19719. + cd=count*8/BUFSIZE+1;
  19720. +
  19721. + ce=count/20+1;
  19722. +#define COND(d) (count != (d))
  19723. +#define COUNT(d) (d)
  19724. +#else
  19725. +#define COND(c) (run)
  19726. +#define COUNT(d) (count)
  19727. + signal(SIGALRM,sig_done);
  19728. + alarm(10);
  19729. +#endif
  19730. +
  19731. +#ifdef PART1
  19732. + time_it(des_encrypt_u4_cisc_idx, "des_encrypt_u4_cisc_idx ", 0);
  19733. + time_it(des_encrypt_u16_cisc_idx, "des_encrypt_u16_cisc_idx ", 1);
  19734. + time_it(des_encrypt_u4_risc1_idx, "des_encrypt_u4_risc1_idx ", 2);
  19735. + num+=3;
  19736. +#endif
  19737. +#ifdef PART2
  19738. + time_it(des_encrypt_u16_risc1_idx,"des_encrypt_u16_risc1_idx", 3);
  19739. + time_it(des_encrypt_u4_risc2_idx, "des_encrypt_u4_risc2_idx ", 4);
  19740. + time_it(des_encrypt_u16_risc2_idx,"des_encrypt_u16_risc2_idx", 5);
  19741. + num+=3;
  19742. +#endif
  19743. +#ifdef PART3
  19744. + time_it(des_encrypt_u4_cisc_ptr, "des_encrypt_u4_cisc_ptr ", 6);
  19745. + time_it(des_encrypt_u16_cisc_ptr, "des_encrypt_u16_cisc_ptr ", 7);
  19746. + time_it(des_encrypt_u4_risc1_ptr, "des_encrypt_u4_risc1_ptr ", 8);
  19747. + num+=3;
  19748. +#endif
  19749. +#ifdef PART4
  19750. + time_it(des_encrypt_u16_risc1_ptr,"des_encrypt_u16_risc1_ptr", 9);
  19751. + time_it(des_encrypt_u4_risc2_ptr, "des_encrypt_u4_risc2_ptr ",10);
  19752. + time_it(des_encrypt_u16_risc2_ptr,"des_encrypt_u16_risc2_ptr",11);
  19753. + num+=3;
  19754. +#endif
  19755. +
  19756. +#ifdef PART1
  19757. + str[0]=" 4 c i";
  19758. + print_it("des_encrypt_u4_cisc_idx ",0);
  19759. + max=tm[0];
  19760. + max_idx=0;
  19761. + str[1]="16 c i";
  19762. + print_it("des_encrypt_u16_cisc_idx ",1);
  19763. + if (max < tm[1]) { max=tm[1]; max_idx=1; }
  19764. + str[2]=" 4 r1 i";
  19765. + print_it("des_encrypt_u4_risc1_idx ",2);
  19766. + if (max < tm[2]) { max=tm[2]; max_idx=2; }
  19767. +#endif
  19768. +#ifdef PART2
  19769. + str[3]="16 r1 i";
  19770. + print_it("des_encrypt_u16_risc1_idx",3);
  19771. + if (max < tm[3]) { max=tm[3]; max_idx=3; }
  19772. + str[4]=" 4 r2 i";
  19773. + print_it("des_encrypt_u4_risc2_idx ",4);
  19774. + if (max < tm[4]) { max=tm[4]; max_idx=4; }
  19775. + str[5]="16 r2 i";
  19776. + print_it("des_encrypt_u16_risc2_idx",5);
  19777. + if (max < tm[5]) { max=tm[5]; max_idx=5; }
  19778. +#endif
  19779. +#ifdef PART3
  19780. + str[6]=" 4 c p";
  19781. + print_it("des_encrypt_u4_cisc_ptr ",6);
  19782. + if (max < tm[6]) { max=tm[6]; max_idx=6; }
  19783. + str[7]="16 c p";
  19784. + print_it("des_encrypt_u16_cisc_ptr ",7);
  19785. + if (max < tm[7]) { max=tm[7]; max_idx=7; }
  19786. + str[8]=" 4 r1 p";
  19787. + print_it("des_encrypt_u4_risc1_ptr ",8);
  19788. + if (max < tm[8]) { max=tm[8]; max_idx=8; }
  19789. +#endif
  19790. +#ifdef PART4
  19791. + str[9]="16 r1 p";
  19792. + print_it("des_encrypt_u16_risc1_ptr",9);
  19793. + if (max < tm[9]) { max=tm[9]; max_idx=9; }
  19794. + str[10]=" 4 r2 p";
  19795. + print_it("des_encrypt_u4_risc2_ptr ",10);
  19796. + if (max < tm[10]) { max=tm[10]; max_idx=10; }
  19797. + str[11]="16 r2 p";
  19798. + print_it("des_encrypt_u16_risc2_ptr",11);
  19799. + if (max < tm[11]) { max=tm[11]; max_idx=11; }
  19800. +#endif
  19801. + printf("options des ecb/s\n");
  19802. + printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
  19803. + d=tm[max_idx];
  19804. + tm[max_idx]= -2.0;
  19805. + max= -1.0;
  19806. + for (;;)
  19807. + {
  19808. + for (i=0; i<12; i++)
  19809. + {
  19810. + if (max < tm[i]) { max=tm[i]; j=i; }
  19811. + }
  19812. + if (max < 0.0) break;
  19813. + printf("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
  19814. + tm[j]= -2.0;
  19815. + max= -1.0;
  19816. + }
  19817. +
  19818. + switch (max_idx)
  19819. + {
  19820. + case 0:
  19821. + printf("-DDES_DEFAULT_OPTIONS\n");
  19822. + break;
  19823. + case 1:
  19824. + printf("-DDES_UNROLL\n");
  19825. + break;
  19826. + case 2:
  19827. + printf("-DDES_RISC1\n");
  19828. + break;
  19829. + case 3:
  19830. + printf("-DDES_UNROLL -DDES_RISC1\n");
  19831. + break;
  19832. + case 4:
  19833. + printf("-DDES_RISC2\n");
  19834. + break;
  19835. + case 5:
  19836. + printf("-DDES_UNROLL -DDES_RISC2\n");
  19837. + break;
  19838. + case 6:
  19839. + printf("-DDES_PTR\n");
  19840. + break;
  19841. + case 7:
  19842. + printf("-DDES_UNROLL -DDES_PTR\n");
  19843. + break;
  19844. + case 8:
  19845. + printf("-DDES_RISC1 -DDES_PTR\n");
  19846. + break;
  19847. + case 9:
  19848. + printf("-DDES_UNROLL -DDES_RISC1 -DDES_PTR\n");
  19849. + break;
  19850. + case 10:
  19851. + printf("-DDES_RISC2 -DDES_PTR\n");
  19852. + break;
  19853. + case 11:
  19854. + printf("-DDES_UNROLL -DDES_RISC2 -DDES_PTR\n");
  19855. + break;
  19856. + }
  19857. + exit(0);
  19858. +#if defined(LINT) || defined(MSDOS)
  19859. + return(0);
  19860. +#endif
  19861. + }
  19862. --- /dev/null Tue Mar 11 13:02:56 2003
  19863. +++ linux/net/ipsec/des/dx86unix.S Mon Feb 9 13:51:03 2004
  19864. @@ -0,0 +1,3160 @@
  19865. +/*
  19866. + * This file was originally generated by Michael Richardson <mcr@freeswan.org>
  19867. + * via the perl scripts found in the ASM subdir. It remains copyright of
  19868. + * Eric Young, see the file COPYRIGHT.
  19869. + *
  19870. + * This was last done on October 9, 2002.
  19871. + *
  19872. + * While this file does not need to go through cpp, we pass it through
  19873. + * CPP by naming it dx86unix.S instead of dx86unix.s because there is
  19874. + * a bug in Rules.make for .s builds - specifically it references EXTRA_CFLAGS
  19875. + * which may contain stuff that AS doesn't understand instead of
  19876. + * referencing EXTRA_AFLAGS.
  19877. + */
  19878. +
  19879. + .file "dx86unix.S"
  19880. + .version "01.01"
  19881. +.text
  19882. + .align 16
  19883. +.globl des_encrypt
  19884. + .type des_encrypt , @function
  19885. +des_encrypt:
  19886. + pushl %esi
  19887. + pushl %edi
  19888. +
  19889. +
  19890. + movl 12(%esp), %esi
  19891. + xorl %ecx, %ecx
  19892. + pushl %ebx
  19893. + pushl %ebp
  19894. + movl (%esi), %eax
  19895. + movl 28(%esp), %ebx
  19896. + movl 4(%esi), %edi
  19897. +
  19898. +
  19899. + roll $4, %eax
  19900. + movl %eax, %esi
  19901. + xorl %edi, %eax
  19902. + andl $0xf0f0f0f0, %eax
  19903. + xorl %eax, %esi
  19904. + xorl %eax, %edi
  19905. +
  19906. + roll $20, %edi
  19907. + movl %edi, %eax
  19908. + xorl %esi, %edi
  19909. + andl $0xfff0000f, %edi
  19910. + xorl %edi, %eax
  19911. + xorl %edi, %esi
  19912. +
  19913. + roll $14, %eax
  19914. + movl %eax, %edi
  19915. + xorl %esi, %eax
  19916. + andl $0x33333333, %eax
  19917. + xorl %eax, %edi
  19918. + xorl %eax, %esi
  19919. +
  19920. + roll $22, %esi
  19921. + movl %esi, %eax
  19922. + xorl %edi, %esi
  19923. + andl $0x03fc03fc, %esi
  19924. + xorl %esi, %eax
  19925. + xorl %esi, %edi
  19926. +
  19927. + roll $9, %eax
  19928. + movl %eax, %esi
  19929. + xorl %edi, %eax
  19930. + andl $0xaaaaaaaa, %eax
  19931. + xorl %eax, %esi
  19932. + xorl %eax, %edi
  19933. +
  19934. +.byte 209
  19935. +.byte 199
  19936. + movl 24(%esp), %ebp
  19937. + cmpl $0, %ebx
  19938. + je .L000start_decrypt
  19939. +
  19940. +
  19941. + movl (%ebp), %eax
  19942. + xorl %ebx, %ebx
  19943. + movl 4(%ebp), %edx
  19944. + xorl %esi, %eax
  19945. + xorl %esi, %edx
  19946. + andl $0xfcfcfcfc, %eax
  19947. + andl $0xcfcfcfcf, %edx
  19948. + movb %al, %bl
  19949. + movb %ah, %cl
  19950. + rorl $4, %edx
  19951. + movl des_SPtrans(%ebx),%ebp
  19952. + movb %dl, %bl
  19953. + xorl %ebp, %edi
  19954. + movl 0x200+des_SPtrans(%ecx),%ebp
  19955. + xorl %ebp, %edi
  19956. + movb %dh, %cl
  19957. + shrl $16, %eax
  19958. + movl 0x100+des_SPtrans(%ebx),%ebp
  19959. + xorl %ebp, %edi
  19960. + movb %ah, %bl
  19961. + shrl $16, %edx
  19962. + movl 0x300+des_SPtrans(%ecx),%ebp
  19963. + xorl %ebp, %edi
  19964. + movl 24(%esp), %ebp
  19965. + movb %dh, %cl
  19966. + andl $0xff, %eax
  19967. + andl $0xff, %edx
  19968. + movl 0x600+des_SPtrans(%ebx),%ebx
  19969. + xorl %ebx, %edi
  19970. + movl 0x700+des_SPtrans(%ecx),%ebx
  19971. + xorl %ebx, %edi
  19972. + movl 0x400+des_SPtrans(%eax),%ebx
  19973. + xorl %ebx, %edi
  19974. + movl 0x500+des_SPtrans(%edx),%ebx
  19975. + xorl %ebx, %edi
  19976. +
  19977. +
  19978. + movl 8(%ebp), %eax
  19979. + xorl %ebx, %ebx
  19980. + movl 12(%ebp), %edx
  19981. + xorl %edi, %eax
  19982. + xorl %edi, %edx
  19983. + andl $0xfcfcfcfc, %eax
  19984. + andl $0xcfcfcfcf, %edx
  19985. + movb %al, %bl
  19986. + movb %ah, %cl
  19987. + rorl $4, %edx
  19988. + movl des_SPtrans(%ebx),%ebp
  19989. + movb %dl, %bl
  19990. + xorl %ebp, %esi
  19991. + movl 0x200+des_SPtrans(%ecx),%ebp
  19992. + xorl %ebp, %esi
  19993. + movb %dh, %cl
  19994. + shrl $16, %eax
  19995. + movl 0x100+des_SPtrans(%ebx),%ebp
  19996. + xorl %ebp, %esi
  19997. + movb %ah, %bl
  19998. + shrl $16, %edx
  19999. + movl 0x300+des_SPtrans(%ecx),%ebp
  20000. + xorl %ebp, %esi
  20001. + movl 24(%esp), %ebp
  20002. + movb %dh, %cl
  20003. + andl $0xff, %eax
  20004. + andl $0xff, %edx
  20005. + movl 0x600+des_SPtrans(%ebx),%ebx
  20006. + xorl %ebx, %esi
  20007. + movl 0x700+des_SPtrans(%ecx),%ebx
  20008. + xorl %ebx, %esi
  20009. + movl 0x400+des_SPtrans(%eax),%ebx
  20010. + xorl %ebx, %esi
  20011. + movl 0x500+des_SPtrans(%edx),%ebx
  20012. + xorl %ebx, %esi
  20013. +
  20014. +
  20015. + movl 16(%ebp), %eax
  20016. + xorl %ebx, %ebx
  20017. + movl 20(%ebp), %edx
  20018. + xorl %esi, %eax
  20019. + xorl %esi, %edx
  20020. + andl $0xfcfcfcfc, %eax
  20021. + andl $0xcfcfcfcf, %edx
  20022. + movb %al, %bl
  20023. + movb %ah, %cl
  20024. + rorl $4, %edx
  20025. + movl des_SPtrans(%ebx),%ebp
  20026. + movb %dl, %bl
  20027. + xorl %ebp, %edi
  20028. + movl 0x200+des_SPtrans(%ecx),%ebp
  20029. + xorl %ebp, %edi
  20030. + movb %dh, %cl
  20031. + shrl $16, %eax
  20032. + movl 0x100+des_SPtrans(%ebx),%ebp
  20033. + xorl %ebp, %edi
  20034. + movb %ah, %bl
  20035. + shrl $16, %edx
  20036. + movl 0x300+des_SPtrans(%ecx),%ebp
  20037. + xorl %ebp, %edi
  20038. + movl 24(%esp), %ebp
  20039. + movb %dh, %cl
  20040. + andl $0xff, %eax
  20041. + andl $0xff, %edx
  20042. + movl 0x600+des_SPtrans(%ebx),%ebx
  20043. + xorl %ebx, %edi
  20044. + movl 0x700+des_SPtrans(%ecx),%ebx
  20045. + xorl %ebx, %edi
  20046. + movl 0x400+des_SPtrans(%eax),%ebx
  20047. + xorl %ebx, %edi
  20048. + movl 0x500+des_SPtrans(%edx),%ebx
  20049. + xorl %ebx, %edi
  20050. +
  20051. +
  20052. + movl 24(%ebp), %eax
  20053. + xorl %ebx, %ebx
  20054. + movl 28(%ebp), %edx
  20055. + xorl %edi, %eax
  20056. + xorl %edi, %edx
  20057. + andl $0xfcfcfcfc, %eax
  20058. + andl $0xcfcfcfcf, %edx
  20059. + movb %al, %bl
  20060. + movb %ah, %cl
  20061. + rorl $4, %edx
  20062. + movl des_SPtrans(%ebx),%ebp
  20063. + movb %dl, %bl
  20064. + xorl %ebp, %esi
  20065. + movl 0x200+des_SPtrans(%ecx),%ebp
  20066. + xorl %ebp, %esi
  20067. + movb %dh, %cl
  20068. + shrl $16, %eax
  20069. + movl 0x100+des_SPtrans(%ebx),%ebp
  20070. + xorl %ebp, %esi
  20071. + movb %ah, %bl
  20072. + shrl $16, %edx
  20073. + movl 0x300+des_SPtrans(%ecx),%ebp
  20074. + xorl %ebp, %esi
  20075. + movl 24(%esp), %ebp
  20076. + movb %dh, %cl
  20077. + andl $0xff, %eax
  20078. + andl $0xff, %edx
  20079. + movl 0x600+des_SPtrans(%ebx),%ebx
  20080. + xorl %ebx, %esi
  20081. + movl 0x700+des_SPtrans(%ecx),%ebx
  20082. + xorl %ebx, %esi
  20083. + movl 0x400+des_SPtrans(%eax),%ebx
  20084. + xorl %ebx, %esi
  20085. + movl 0x500+des_SPtrans(%edx),%ebx
  20086. + xorl %ebx, %esi
  20087. +
  20088. +
  20089. + movl 32(%ebp), %eax
  20090. + xorl %ebx, %ebx
  20091. + movl 36(%ebp), %edx
  20092. + xorl %esi, %eax
  20093. + xorl %esi, %edx
  20094. + andl $0xfcfcfcfc, %eax
  20095. + andl $0xcfcfcfcf, %edx
  20096. + movb %al, %bl
  20097. + movb %ah, %cl
  20098. + rorl $4, %edx
  20099. + movl des_SPtrans(%ebx),%ebp
  20100. + movb %dl, %bl
  20101. + xorl %ebp, %edi
  20102. + movl 0x200+des_SPtrans(%ecx),%ebp
  20103. + xorl %ebp, %edi
  20104. + movb %dh, %cl
  20105. + shrl $16, %eax
  20106. + movl 0x100+des_SPtrans(%ebx),%ebp
  20107. + xorl %ebp, %edi
  20108. + movb %ah, %bl
  20109. + shrl $16, %edx
  20110. + movl 0x300+des_SPtrans(%ecx),%ebp
  20111. + xorl %ebp, %edi
  20112. + movl 24(%esp), %ebp
  20113. + movb %dh, %cl
  20114. + andl $0xff, %eax
  20115. + andl $0xff, %edx
  20116. + movl 0x600+des_SPtrans(%ebx),%ebx
  20117. + xorl %ebx, %edi
  20118. + movl 0x700+des_SPtrans(%ecx),%ebx
  20119. + xorl %ebx, %edi
  20120. + movl 0x400+des_SPtrans(%eax),%ebx
  20121. + xorl %ebx, %edi
  20122. + movl 0x500+des_SPtrans(%edx),%ebx
  20123. + xorl %ebx, %edi
  20124. +
  20125. +
  20126. + movl 40(%ebp), %eax
  20127. + xorl %ebx, %ebx
  20128. + movl 44(%ebp), %edx
  20129. + xorl %edi, %eax
  20130. + xorl %edi, %edx
  20131. + andl $0xfcfcfcfc, %eax
  20132. + andl $0xcfcfcfcf, %edx
  20133. + movb %al, %bl
  20134. + movb %ah, %cl
  20135. + rorl $4, %edx
  20136. + movl des_SPtrans(%ebx),%ebp
  20137. + movb %dl, %bl
  20138. + xorl %ebp, %esi
  20139. + movl 0x200+des_SPtrans(%ecx),%ebp
  20140. + xorl %ebp, %esi
  20141. + movb %dh, %cl
  20142. + shrl $16, %eax
  20143. + movl 0x100+des_SPtrans(%ebx),%ebp
  20144. + xorl %ebp, %esi
  20145. + movb %ah, %bl
  20146. + shrl $16, %edx
  20147. + movl 0x300+des_SPtrans(%ecx),%ebp
  20148. + xorl %ebp, %esi
  20149. + movl 24(%esp), %ebp
  20150. + movb %dh, %cl
  20151. + andl $0xff, %eax
  20152. + andl $0xff, %edx
  20153. + movl 0x600+des_SPtrans(%ebx),%ebx
  20154. + xorl %ebx, %esi
  20155. + movl 0x700+des_SPtrans(%ecx),%ebx
  20156. + xorl %ebx, %esi
  20157. + movl 0x400+des_SPtrans(%eax),%ebx
  20158. + xorl %ebx, %esi
  20159. + movl 0x500+des_SPtrans(%edx),%ebx
  20160. + xorl %ebx, %esi
  20161. +
  20162. +
  20163. + movl 48(%ebp), %eax
  20164. + xorl %ebx, %ebx
  20165. + movl 52(%ebp), %edx
  20166. + xorl %esi, %eax
  20167. + xorl %esi, %edx
  20168. + andl $0xfcfcfcfc, %eax
  20169. + andl $0xcfcfcfcf, %edx
  20170. + movb %al, %bl
  20171. + movb %ah, %cl
  20172. + rorl $4, %edx
  20173. + movl des_SPtrans(%ebx),%ebp
  20174. + movb %dl, %bl
  20175. + xorl %ebp, %edi
  20176. + movl 0x200+des_SPtrans(%ecx),%ebp
  20177. + xorl %ebp, %edi
  20178. + movb %dh, %cl
  20179. + shrl $16, %eax
  20180. + movl 0x100+des_SPtrans(%ebx),%ebp
  20181. + xorl %ebp, %edi
  20182. + movb %ah, %bl
  20183. + shrl $16, %edx
  20184. + movl 0x300+des_SPtrans(%ecx),%ebp
  20185. + xorl %ebp, %edi
  20186. + movl 24(%esp), %ebp
  20187. + movb %dh, %cl
  20188. + andl $0xff, %eax
  20189. + andl $0xff, %edx
  20190. + movl 0x600+des_SPtrans(%ebx),%ebx
  20191. + xorl %ebx, %edi
  20192. + movl 0x700+des_SPtrans(%ecx),%ebx
  20193. + xorl %ebx, %edi
  20194. + movl 0x400+des_SPtrans(%eax),%ebx
  20195. + xorl %ebx, %edi
  20196. + movl 0x500+des_SPtrans(%edx),%ebx
  20197. + xorl %ebx, %edi
  20198. +
  20199. +
  20200. + movl 56(%ebp), %eax
  20201. + xorl %ebx, %ebx
  20202. + movl 60(%ebp), %edx
  20203. + xorl %edi, %eax
  20204. + xorl %edi, %edx
  20205. + andl $0xfcfcfcfc, %eax
  20206. + andl $0xcfcfcfcf, %edx
  20207. + movb %al, %bl
  20208. + movb %ah, %cl
  20209. + rorl $4, %edx
  20210. + movl des_SPtrans(%ebx),%ebp
  20211. + movb %dl, %bl
  20212. + xorl %ebp, %esi
  20213. + movl 0x200+des_SPtrans(%ecx),%ebp
  20214. + xorl %ebp, %esi
  20215. + movb %dh, %cl
  20216. + shrl $16, %eax
  20217. + movl 0x100+des_SPtrans(%ebx),%ebp
  20218. + xorl %ebp, %esi
  20219. + movb %ah, %bl
  20220. + shrl $16, %edx
  20221. + movl 0x300+des_SPtrans(%ecx),%ebp
  20222. + xorl %ebp, %esi
  20223. + movl 24(%esp), %ebp
  20224. + movb %dh, %cl
  20225. + andl $0xff, %eax
  20226. + andl $0xff, %edx
  20227. + movl 0x600+des_SPtrans(%ebx),%ebx
  20228. + xorl %ebx, %esi
  20229. + movl 0x700+des_SPtrans(%ecx),%ebx
  20230. + xorl %ebx, %esi
  20231. + movl 0x400+des_SPtrans(%eax),%ebx
  20232. + xorl %ebx, %esi
  20233. + movl 0x500+des_SPtrans(%edx),%ebx
  20234. + xorl %ebx, %esi
  20235. +
  20236. +
  20237. + movl 64(%ebp), %eax
  20238. + xorl %ebx, %ebx
  20239. + movl 68(%ebp), %edx
  20240. + xorl %esi, %eax
  20241. + xorl %esi, %edx
  20242. + andl $0xfcfcfcfc, %eax
  20243. + andl $0xcfcfcfcf, %edx
  20244. + movb %al, %bl
  20245. + movb %ah, %cl
  20246. + rorl $4, %edx
  20247. + movl des_SPtrans(%ebx),%ebp
  20248. + movb %dl, %bl
  20249. + xorl %ebp, %edi
  20250. + movl 0x200+des_SPtrans(%ecx),%ebp
  20251. + xorl %ebp, %edi
  20252. + movb %dh, %cl
  20253. + shrl $16, %eax
  20254. + movl 0x100+des_SPtrans(%ebx),%ebp
  20255. + xorl %ebp, %edi
  20256. + movb %ah, %bl
  20257. + shrl $16, %edx
  20258. + movl 0x300+des_SPtrans(%ecx),%ebp
  20259. + xorl %ebp, %edi
  20260. + movl 24(%esp), %ebp
  20261. + movb %dh, %cl
  20262. + andl $0xff, %eax
  20263. + andl $0xff, %edx
  20264. + movl 0x600+des_SPtrans(%ebx),%ebx
  20265. + xorl %ebx, %edi
  20266. + movl 0x700+des_SPtrans(%ecx),%ebx
  20267. + xorl %ebx, %edi
  20268. + movl 0x400+des_SPtrans(%eax),%ebx
  20269. + xorl %ebx, %edi
  20270. + movl 0x500+des_SPtrans(%edx),%ebx
  20271. + xorl %ebx, %edi
  20272. +
  20273. +
  20274. + movl 72(%ebp), %eax
  20275. + xorl %ebx, %ebx
  20276. + movl 76(%ebp), %edx
  20277. + xorl %edi, %eax
  20278. + xorl %edi, %edx
  20279. + andl $0xfcfcfcfc, %eax
  20280. + andl $0xcfcfcfcf, %edx
  20281. + movb %al, %bl
  20282. + movb %ah, %cl
  20283. + rorl $4, %edx
  20284. + movl des_SPtrans(%ebx),%ebp
  20285. + movb %dl, %bl
  20286. + xorl %ebp, %esi
  20287. + movl 0x200+des_SPtrans(%ecx),%ebp
  20288. + xorl %ebp, %esi
  20289. + movb %dh, %cl
  20290. + shrl $16, %eax
  20291. + movl 0x100+des_SPtrans(%ebx),%ebp
  20292. + xorl %ebp, %esi
  20293. + movb %ah, %bl
  20294. + shrl $16, %edx
  20295. + movl 0x300+des_SPtrans(%ecx),%ebp
  20296. + xorl %ebp, %esi
  20297. + movl 24(%esp), %ebp
  20298. + movb %dh, %cl
  20299. + andl $0xff, %eax
  20300. + andl $0xff, %edx
  20301. + movl 0x600+des_SPtrans(%ebx),%ebx
  20302. + xorl %ebx, %esi
  20303. + movl 0x700+des_SPtrans(%ecx),%ebx
  20304. + xorl %ebx, %esi
  20305. + movl 0x400+des_SPtrans(%eax),%ebx
  20306. + xorl %ebx, %esi
  20307. + movl 0x500+des_SPtrans(%edx),%ebx
  20308. + xorl %ebx, %esi
  20309. +
  20310. +
  20311. + movl 80(%ebp), %eax
  20312. + xorl %ebx, %ebx
  20313. + movl 84(%ebp), %edx
  20314. + xorl %esi, %eax
  20315. + xorl %esi, %edx
  20316. + andl $0xfcfcfcfc, %eax
  20317. + andl $0xcfcfcfcf, %edx
  20318. + movb %al, %bl
  20319. + movb %ah, %cl
  20320. + rorl $4, %edx
  20321. + movl des_SPtrans(%ebx),%ebp
  20322. + movb %dl, %bl
  20323. + xorl %ebp, %edi
  20324. + movl 0x200+des_SPtrans(%ecx),%ebp
  20325. + xorl %ebp, %edi
  20326. + movb %dh, %cl
  20327. + shrl $16, %eax
  20328. + movl 0x100+des_SPtrans(%ebx),%ebp
  20329. + xorl %ebp, %edi
  20330. + movb %ah, %bl
  20331. + shrl $16, %edx
  20332. + movl 0x300+des_SPtrans(%ecx),%ebp
  20333. + xorl %ebp, %edi
  20334. + movl 24(%esp), %ebp
  20335. + movb %dh, %cl
  20336. + andl $0xff, %eax
  20337. + andl $0xff, %edx
  20338. + movl 0x600+des_SPtrans(%ebx),%ebx
  20339. + xorl %ebx, %edi
  20340. + movl 0x700+des_SPtrans(%ecx),%ebx
  20341. + xorl %ebx, %edi
  20342. + movl 0x400+des_SPtrans(%eax),%ebx
  20343. + xorl %ebx, %edi
  20344. + movl 0x500+des_SPtrans(%edx),%ebx
  20345. + xorl %ebx, %edi
  20346. +
  20347. +
  20348. + movl 88(%ebp), %eax
  20349. + xorl %ebx, %ebx
  20350. + movl 92(%ebp), %edx
  20351. + xorl %edi, %eax
  20352. + xorl %edi, %edx
  20353. + andl $0xfcfcfcfc, %eax
  20354. + andl $0xcfcfcfcf, %edx
  20355. + movb %al, %bl
  20356. + movb %ah, %cl
  20357. + rorl $4, %edx
  20358. + movl des_SPtrans(%ebx),%ebp
  20359. + movb %dl, %bl
  20360. + xorl %ebp, %esi
  20361. + movl 0x200+des_SPtrans(%ecx),%ebp
  20362. + xorl %ebp, %esi
  20363. + movb %dh, %cl
  20364. + shrl $16, %eax
  20365. + movl 0x100+des_SPtrans(%ebx),%ebp
  20366. + xorl %ebp, %esi
  20367. + movb %ah, %bl
  20368. + shrl $16, %edx
  20369. + movl 0x300+des_SPtrans(%ecx),%ebp
  20370. + xorl %ebp, %esi
  20371. + movl 24(%esp), %ebp
  20372. + movb %dh, %cl
  20373. + andl $0xff, %eax
  20374. + andl $0xff, %edx
  20375. + movl 0x600+des_SPtrans(%ebx),%ebx
  20376. + xorl %ebx, %esi
  20377. + movl 0x700+des_SPtrans(%ecx),%ebx
  20378. + xorl %ebx, %esi
  20379. + movl 0x400+des_SPtrans(%eax),%ebx
  20380. + xorl %ebx, %esi
  20381. + movl 0x500+des_SPtrans(%edx),%ebx
  20382. + xorl %ebx, %esi
  20383. +
  20384. +
  20385. + movl 96(%ebp), %eax
  20386. + xorl %ebx, %ebx
  20387. + movl 100(%ebp), %edx
  20388. + xorl %esi, %eax
  20389. + xorl %esi, %edx
  20390. + andl $0xfcfcfcfc, %eax
  20391. + andl $0xcfcfcfcf, %edx
  20392. + movb %al, %bl
  20393. + movb %ah, %cl
  20394. + rorl $4, %edx
  20395. + movl des_SPtrans(%ebx),%ebp
  20396. + movb %dl, %bl
  20397. + xorl %ebp, %edi
  20398. + movl 0x200+des_SPtrans(%ecx),%ebp
  20399. + xorl %ebp, %edi
  20400. + movb %dh, %cl
  20401. + shrl $16, %eax
  20402. + movl 0x100+des_SPtrans(%ebx),%ebp
  20403. + xorl %ebp, %edi
  20404. + movb %ah, %bl
  20405. + shrl $16, %edx
  20406. + movl 0x300+des_SPtrans(%ecx),%ebp
  20407. + xorl %ebp, %edi
  20408. + movl 24(%esp), %ebp
  20409. + movb %dh, %cl
  20410. + andl $0xff, %eax
  20411. + andl $0xff, %edx
  20412. + movl 0x600+des_SPtrans(%ebx),%ebx
  20413. + xorl %ebx, %edi
  20414. + movl 0x700+des_SPtrans(%ecx),%ebx
  20415. + xorl %ebx, %edi
  20416. + movl 0x400+des_SPtrans(%eax),%ebx
  20417. + xorl %ebx, %edi
  20418. + movl 0x500+des_SPtrans(%edx),%ebx
  20419. + xorl %ebx, %edi
  20420. +
  20421. +
  20422. + movl 104(%ebp), %eax
  20423. + xorl %ebx, %ebx
  20424. + movl 108(%ebp), %edx
  20425. + xorl %edi, %eax
  20426. + xorl %edi, %edx
  20427. + andl $0xfcfcfcfc, %eax
  20428. + andl $0xcfcfcfcf, %edx
  20429. + movb %al, %bl
  20430. + movb %ah, %cl
  20431. + rorl $4, %edx
  20432. + movl des_SPtrans(%ebx),%ebp
  20433. + movb %dl, %bl
  20434. + xorl %ebp, %esi
  20435. + movl 0x200+des_SPtrans(%ecx),%ebp
  20436. + xorl %ebp, %esi
  20437. + movb %dh, %cl
  20438. + shrl $16, %eax
  20439. + movl 0x100+des_SPtrans(%ebx),%ebp
  20440. + xorl %ebp, %esi
  20441. + movb %ah, %bl
  20442. + shrl $16, %edx
  20443. + movl 0x300+des_SPtrans(%ecx),%ebp
  20444. + xorl %ebp, %esi
  20445. + movl 24(%esp), %ebp
  20446. + movb %dh, %cl
  20447. + andl $0xff, %eax
  20448. + andl $0xff, %edx
  20449. + movl 0x600+des_SPtrans(%ebx),%ebx
  20450. + xorl %ebx, %esi
  20451. + movl 0x700+des_SPtrans(%ecx),%ebx
  20452. + xorl %ebx, %esi
  20453. + movl 0x400+des_SPtrans(%eax),%ebx
  20454. + xorl %ebx, %esi
  20455. + movl 0x500+des_SPtrans(%edx),%ebx
  20456. + xorl %ebx, %esi
  20457. +
  20458. +
  20459. + movl 112(%ebp), %eax
  20460. + xorl %ebx, %ebx
  20461. + movl 116(%ebp), %edx
  20462. + xorl %esi, %eax
  20463. + xorl %esi, %edx
  20464. + andl $0xfcfcfcfc, %eax
  20465. + andl $0xcfcfcfcf, %edx
  20466. + movb %al, %bl
  20467. + movb %ah, %cl
  20468. + rorl $4, %edx
  20469. + movl des_SPtrans(%ebx),%ebp
  20470. + movb %dl, %bl
  20471. + xorl %ebp, %edi
  20472. + movl 0x200+des_SPtrans(%ecx),%ebp
  20473. + xorl %ebp, %edi
  20474. + movb %dh, %cl
  20475. + shrl $16, %eax
  20476. + movl 0x100+des_SPtrans(%ebx),%ebp
  20477. + xorl %ebp, %edi
  20478. + movb %ah, %bl
  20479. + shrl $16, %edx
  20480. + movl 0x300+des_SPtrans(%ecx),%ebp
  20481. + xorl %ebp, %edi
  20482. + movl 24(%esp), %ebp
  20483. + movb %dh, %cl
  20484. + andl $0xff, %eax
  20485. + andl $0xff, %edx
  20486. + movl 0x600+des_SPtrans(%ebx),%ebx
  20487. + xorl %ebx, %edi
  20488. + movl 0x700+des_SPtrans(%ecx),%ebx
  20489. + xorl %ebx, %edi
  20490. + movl 0x400+des_SPtrans(%eax),%ebx
  20491. + xorl %ebx, %edi
  20492. + movl 0x500+des_SPtrans(%edx),%ebx
  20493. + xorl %ebx, %edi
  20494. +
  20495. +
  20496. + movl 120(%ebp), %eax
  20497. + xorl %ebx, %ebx
  20498. + movl 124(%ebp), %edx
  20499. + xorl %edi, %eax
  20500. + xorl %edi, %edx
  20501. + andl $0xfcfcfcfc, %eax
  20502. + andl $0xcfcfcfcf, %edx
  20503. + movb %al, %bl
  20504. + movb %ah, %cl
  20505. + rorl $4, %edx
  20506. + movl des_SPtrans(%ebx),%ebp
  20507. + movb %dl, %bl
  20508. + xorl %ebp, %esi
  20509. + movl 0x200+des_SPtrans(%ecx),%ebp
  20510. + xorl %ebp, %esi
  20511. + movb %dh, %cl
  20512. + shrl $16, %eax
  20513. + movl 0x100+des_SPtrans(%ebx),%ebp
  20514. + xorl %ebp, %esi
  20515. + movb %ah, %bl
  20516. + shrl $16, %edx
  20517. + movl 0x300+des_SPtrans(%ecx),%ebp
  20518. + xorl %ebp, %esi
  20519. + movl 24(%esp), %ebp
  20520. + movb %dh, %cl
  20521. + andl $0xff, %eax
  20522. + andl $0xff, %edx
  20523. + movl 0x600+des_SPtrans(%ebx),%ebx
  20524. + xorl %ebx, %esi
  20525. + movl 0x700+des_SPtrans(%ecx),%ebx
  20526. + xorl %ebx, %esi
  20527. + movl 0x400+des_SPtrans(%eax),%ebx
  20528. + xorl %ebx, %esi
  20529. + movl 0x500+des_SPtrans(%edx),%ebx
  20530. + xorl %ebx, %esi
  20531. + jmp .L001end
  20532. +.L000start_decrypt:
  20533. +
  20534. +
  20535. + movl 120(%ebp), %eax
  20536. + xorl %ebx, %ebx
  20537. + movl 124(%ebp), %edx
  20538. + xorl %esi, %eax
  20539. + xorl %esi, %edx
  20540. + andl $0xfcfcfcfc, %eax
  20541. + andl $0xcfcfcfcf, %edx
  20542. + movb %al, %bl
  20543. + movb %ah, %cl
  20544. + rorl $4, %edx
  20545. + movl des_SPtrans(%ebx),%ebp
  20546. + movb %dl, %bl
  20547. + xorl %ebp, %edi
  20548. + movl 0x200+des_SPtrans(%ecx),%ebp
  20549. + xorl %ebp, %edi
  20550. + movb %dh, %cl
  20551. + shrl $16, %eax
  20552. + movl 0x100+des_SPtrans(%ebx),%ebp
  20553. + xorl %ebp, %edi
  20554. + movb %ah, %bl
  20555. + shrl $16, %edx
  20556. + movl 0x300+des_SPtrans(%ecx),%ebp
  20557. + xorl %ebp, %edi
  20558. + movl 24(%esp), %ebp
  20559. + movb %dh, %cl
  20560. + andl $0xff, %eax
  20561. + andl $0xff, %edx
  20562. + movl 0x600+des_SPtrans(%ebx),%ebx
  20563. + xorl %ebx, %edi
  20564. + movl 0x700+des_SPtrans(%ecx),%ebx
  20565. + xorl %ebx, %edi
  20566. + movl 0x400+des_SPtrans(%eax),%ebx
  20567. + xorl %ebx, %edi
  20568. + movl 0x500+des_SPtrans(%edx),%ebx
  20569. + xorl %ebx, %edi
  20570. +
  20571. +
  20572. + movl 112(%ebp), %eax
  20573. + xorl %ebx, %ebx
  20574. + movl 116(%ebp), %edx
  20575. + xorl %edi, %eax
  20576. + xorl %edi, %edx
  20577. + andl $0xfcfcfcfc, %eax
  20578. + andl $0xcfcfcfcf, %edx
  20579. + movb %al, %bl
  20580. + movb %ah, %cl
  20581. + rorl $4, %edx
  20582. + movl des_SPtrans(%ebx),%ebp
  20583. + movb %dl, %bl
  20584. + xorl %ebp, %esi
  20585. + movl 0x200+des_SPtrans(%ecx),%ebp
  20586. + xorl %ebp, %esi
  20587. + movb %dh, %cl
  20588. + shrl $16, %eax
  20589. + movl 0x100+des_SPtrans(%ebx),%ebp
  20590. + xorl %ebp, %esi
  20591. + movb %ah, %bl
  20592. + shrl $16, %edx
  20593. + movl 0x300+des_SPtrans(%ecx),%ebp
  20594. + xorl %ebp, %esi
  20595. + movl 24(%esp), %ebp
  20596. + movb %dh, %cl
  20597. + andl $0xff, %eax
  20598. + andl $0xff, %edx
  20599. + movl 0x600+des_SPtrans(%ebx),%ebx
  20600. + xorl %ebx, %esi
  20601. + movl 0x700+des_SPtrans(%ecx),%ebx
  20602. + xorl %ebx, %esi
  20603. + movl 0x400+des_SPtrans(%eax),%ebx
  20604. + xorl %ebx, %esi
  20605. + movl 0x500+des_SPtrans(%edx),%ebx
  20606. + xorl %ebx, %esi
  20607. +
  20608. +
  20609. + movl 104(%ebp), %eax
  20610. + xorl %ebx, %ebx
  20611. + movl 108(%ebp), %edx
  20612. + xorl %esi, %eax
  20613. + xorl %esi, %edx
  20614. + andl $0xfcfcfcfc, %eax
  20615. + andl $0xcfcfcfcf, %edx
  20616. + movb %al, %bl
  20617. + movb %ah, %cl
  20618. + rorl $4, %edx
  20619. + movl des_SPtrans(%ebx),%ebp
  20620. + movb %dl, %bl
  20621. + xorl %ebp, %edi
  20622. + movl 0x200+des_SPtrans(%ecx),%ebp
  20623. + xorl %ebp, %edi
  20624. + movb %dh, %cl
  20625. + shrl $16, %eax
  20626. + movl 0x100+des_SPtrans(%ebx),%ebp
  20627. + xorl %ebp, %edi
  20628. + movb %ah, %bl
  20629. + shrl $16, %edx
  20630. + movl 0x300+des_SPtrans(%ecx),%ebp
  20631. + xorl %ebp, %edi
  20632. + movl 24(%esp), %ebp
  20633. + movb %dh, %cl
  20634. + andl $0xff, %eax
  20635. + andl $0xff, %edx
  20636. + movl 0x600+des_SPtrans(%ebx),%ebx
  20637. + xorl %ebx, %edi
  20638. + movl 0x700+des_SPtrans(%ecx),%ebx
  20639. + xorl %ebx, %edi
  20640. + movl 0x400+des_SPtrans(%eax),%ebx
  20641. + xorl %ebx, %edi
  20642. + movl 0x500+des_SPtrans(%edx),%ebx
  20643. + xorl %ebx, %edi
  20644. +
  20645. +
  20646. + movl 96(%ebp), %eax
  20647. + xorl %ebx, %ebx
  20648. + movl 100(%ebp), %edx
  20649. + xorl %edi, %eax
  20650. + xorl %edi, %edx
  20651. + andl $0xfcfcfcfc, %eax
  20652. + andl $0xcfcfcfcf, %edx
  20653. + movb %al, %bl
  20654. + movb %ah, %cl
  20655. + rorl $4, %edx
  20656. + movl des_SPtrans(%ebx),%ebp
  20657. + movb %dl, %bl
  20658. + xorl %ebp, %esi
  20659. + movl 0x200+des_SPtrans(%ecx),%ebp
  20660. + xorl %ebp, %esi
  20661. + movb %dh, %cl
  20662. + shrl $16, %eax
  20663. + movl 0x100+des_SPtrans(%ebx),%ebp
  20664. + xorl %ebp, %esi
  20665. + movb %ah, %bl
  20666. + shrl $16, %edx
  20667. + movl 0x300+des_SPtrans(%ecx),%ebp
  20668. + xorl %ebp, %esi
  20669. + movl 24(%esp), %ebp
  20670. + movb %dh, %cl
  20671. + andl $0xff, %eax
  20672. + andl $0xff, %edx
  20673. + movl 0x600+des_SPtrans(%ebx),%ebx
  20674. + xorl %ebx, %esi
  20675. + movl 0x700+des_SPtrans(%ecx),%ebx
  20676. + xorl %ebx, %esi
  20677. + movl 0x400+des_SPtrans(%eax),%ebx
  20678. + xorl %ebx, %esi
  20679. + movl 0x500+des_SPtrans(%edx),%ebx
  20680. + xorl %ebx, %esi
  20681. +
  20682. +
  20683. + movl 88(%ebp), %eax
  20684. + xorl %ebx, %ebx
  20685. + movl 92(%ebp), %edx
  20686. + xorl %esi, %eax
  20687. + xorl %esi, %edx
  20688. + andl $0xfcfcfcfc, %eax
  20689. + andl $0xcfcfcfcf, %edx
  20690. + movb %al, %bl
  20691. + movb %ah, %cl
  20692. + rorl $4, %edx
  20693. + movl des_SPtrans(%ebx),%ebp
  20694. + movb %dl, %bl
  20695. + xorl %ebp, %edi
  20696. + movl 0x200+des_SPtrans(%ecx),%ebp
  20697. + xorl %ebp, %edi
  20698. + movb %dh, %cl
  20699. + shrl $16, %eax
  20700. + movl 0x100+des_SPtrans(%ebx),%ebp
  20701. + xorl %ebp, %edi
  20702. + movb %ah, %bl
  20703. + shrl $16, %edx
  20704. + movl 0x300+des_SPtrans(%ecx),%ebp
  20705. + xorl %ebp, %edi
  20706. + movl 24(%esp), %ebp
  20707. + movb %dh, %cl
  20708. + andl $0xff, %eax
  20709. + andl $0xff, %edx
  20710. + movl 0x600+des_SPtrans(%ebx),%ebx
  20711. + xorl %ebx, %edi
  20712. + movl 0x700+des_SPtrans(%ecx),%ebx
  20713. + xorl %ebx, %edi
  20714. + movl 0x400+des_SPtrans(%eax),%ebx
  20715. + xorl %ebx, %edi
  20716. + movl 0x500+des_SPtrans(%edx),%ebx
  20717. + xorl %ebx, %edi
  20718. +
  20719. +
  20720. + movl 80(%ebp), %eax
  20721. + xorl %ebx, %ebx
  20722. + movl 84(%ebp), %edx
  20723. + xorl %edi, %eax
  20724. + xorl %edi, %edx
  20725. + andl $0xfcfcfcfc, %eax
  20726. + andl $0xcfcfcfcf, %edx
  20727. + movb %al, %bl
  20728. + movb %ah, %cl
  20729. + rorl $4, %edx
  20730. + movl des_SPtrans(%ebx),%ebp
  20731. + movb %dl, %bl
  20732. + xorl %ebp, %esi
  20733. + movl 0x200+des_SPtrans(%ecx),%ebp
  20734. + xorl %ebp, %esi
  20735. + movb %dh, %cl
  20736. + shrl $16, %eax
  20737. + movl 0x100+des_SPtrans(%ebx),%ebp
  20738. + xorl %ebp, %esi
  20739. + movb %ah, %bl
  20740. + shrl $16, %edx
  20741. + movl 0x300+des_SPtrans(%ecx),%ebp
  20742. + xorl %ebp, %esi
  20743. + movl 24(%esp), %ebp
  20744. + movb %dh, %cl
  20745. + andl $0xff, %eax
  20746. + andl $0xff, %edx
  20747. + movl 0x600+des_SPtrans(%ebx),%ebx
  20748. + xorl %ebx, %esi
  20749. + movl 0x700+des_SPtrans(%ecx),%ebx
  20750. + xorl %ebx, %esi
  20751. + movl 0x400+des_SPtrans(%eax),%ebx
  20752. + xorl %ebx, %esi
  20753. + movl 0x500+des_SPtrans(%edx),%ebx
  20754. + xorl %ebx, %esi
  20755. +
  20756. +
  20757. + movl 72(%ebp), %eax
  20758. + xorl %ebx, %ebx
  20759. + movl 76(%ebp), %edx
  20760. + xorl %esi, %eax
  20761. + xorl %esi, %edx
  20762. + andl $0xfcfcfcfc, %eax
  20763. + andl $0xcfcfcfcf, %edx
  20764. + movb %al, %bl
  20765. + movb %ah, %cl
  20766. + rorl $4, %edx
  20767. + movl des_SPtrans(%ebx),%ebp
  20768. + movb %dl, %bl
  20769. + xorl %ebp, %edi
  20770. + movl 0x200+des_SPtrans(%ecx),%ebp
  20771. + xorl %ebp, %edi
  20772. + movb %dh, %cl
  20773. + shrl $16, %eax
  20774. + movl 0x100+des_SPtrans(%ebx),%ebp
  20775. + xorl %ebp, %edi
  20776. + movb %ah, %bl
  20777. + shrl $16, %edx
  20778. + movl 0x300+des_SPtrans(%ecx),%ebp
  20779. + xorl %ebp, %edi
  20780. + movl 24(%esp), %ebp
  20781. + movb %dh, %cl
  20782. + andl $0xff, %eax
  20783. + andl $0xff, %edx
  20784. + movl 0x600+des_SPtrans(%ebx),%ebx
  20785. + xorl %ebx, %edi
  20786. + movl 0x700+des_SPtrans(%ecx),%ebx
  20787. + xorl %ebx, %edi
  20788. + movl 0x400+des_SPtrans(%eax),%ebx
  20789. + xorl %ebx, %edi
  20790. + movl 0x500+des_SPtrans(%edx),%ebx
  20791. + xorl %ebx, %edi
  20792. +
  20793. +
  20794. + movl 64(%ebp), %eax
  20795. + xorl %ebx, %ebx
  20796. + movl 68(%ebp), %edx
  20797. + xorl %edi, %eax
  20798. + xorl %edi, %edx
  20799. + andl $0xfcfcfcfc, %eax
  20800. + andl $0xcfcfcfcf, %edx
  20801. + movb %al, %bl
  20802. + movb %ah, %cl
  20803. + rorl $4, %edx
  20804. + movl des_SPtrans(%ebx),%ebp
  20805. + movb %dl, %bl
  20806. + xorl %ebp, %esi
  20807. + movl 0x200+des_SPtrans(%ecx),%ebp
  20808. + xorl %ebp, %esi
  20809. + movb %dh, %cl
  20810. + shrl $16, %eax
  20811. + movl 0x100+des_SPtrans(%ebx),%ebp
  20812. + xorl %ebp, %esi
  20813. + movb %ah, %bl
  20814. + shrl $16, %edx
  20815. + movl 0x300+des_SPtrans(%ecx),%ebp
  20816. + xorl %ebp, %esi
  20817. + movl 24(%esp), %ebp
  20818. + movb %dh, %cl
  20819. + andl $0xff, %eax
  20820. + andl $0xff, %edx
  20821. + movl 0x600+des_SPtrans(%ebx),%ebx
  20822. + xorl %ebx, %esi
  20823. + movl 0x700+des_SPtrans(%ecx),%ebx
  20824. + xorl %ebx, %esi
  20825. + movl 0x400+des_SPtrans(%eax),%ebx
  20826. + xorl %ebx, %esi
  20827. + movl 0x500+des_SPtrans(%edx),%ebx
  20828. + xorl %ebx, %esi
  20829. +
  20830. +
  20831. + movl 56(%ebp), %eax
  20832. + xorl %ebx, %ebx
  20833. + movl 60(%ebp), %edx
  20834. + xorl %esi, %eax
  20835. + xorl %esi, %edx
  20836. + andl $0xfcfcfcfc, %eax
  20837. + andl $0xcfcfcfcf, %edx
  20838. + movb %al, %bl
  20839. + movb %ah, %cl
  20840. + rorl $4, %edx
  20841. + movl des_SPtrans(%ebx),%ebp
  20842. + movb %dl, %bl
  20843. + xorl %ebp, %edi
  20844. + movl 0x200+des_SPtrans(%ecx),%ebp
  20845. + xorl %ebp, %edi
  20846. + movb %dh, %cl
  20847. + shrl $16, %eax
  20848. + movl 0x100+des_SPtrans(%ebx),%ebp
  20849. + xorl %ebp, %edi
  20850. + movb %ah, %bl
  20851. + shrl $16, %edx
  20852. + movl 0x300+des_SPtrans(%ecx),%ebp
  20853. + xorl %ebp, %edi
  20854. + movl 24(%esp), %ebp
  20855. + movb %dh, %cl
  20856. + andl $0xff, %eax
  20857. + andl $0xff, %edx
  20858. + movl 0x600+des_SPtrans(%ebx),%ebx
  20859. + xorl %ebx, %edi
  20860. + movl 0x700+des_SPtrans(%ecx),%ebx
  20861. + xorl %ebx, %edi
  20862. + movl 0x400+des_SPtrans(%eax),%ebx
  20863. + xorl %ebx, %edi
  20864. + movl 0x500+des_SPtrans(%edx),%ebx
  20865. + xorl %ebx, %edi
  20866. +
  20867. +
  20868. + movl 48(%ebp), %eax
  20869. + xorl %ebx, %ebx
  20870. + movl 52(%ebp), %edx
  20871. + xorl %edi, %eax
  20872. + xorl %edi, %edx
  20873. + andl $0xfcfcfcfc, %eax
  20874. + andl $0xcfcfcfcf, %edx
  20875. + movb %al, %bl
  20876. + movb %ah, %cl
  20877. + rorl $4, %edx
  20878. + movl des_SPtrans(%ebx),%ebp
  20879. + movb %dl, %bl
  20880. + xorl %ebp, %esi
  20881. + movl 0x200+des_SPtrans(%ecx),%ebp
  20882. + xorl %ebp, %esi
  20883. + movb %dh, %cl
  20884. + shrl $16, %eax
  20885. + movl 0x100+des_SPtrans(%ebx),%ebp
  20886. + xorl %ebp, %esi
  20887. + movb %ah, %bl
  20888. + shrl $16, %edx
  20889. + movl 0x300+des_SPtrans(%ecx),%ebp
  20890. + xorl %ebp, %esi
  20891. + movl 24(%esp), %ebp
  20892. + movb %dh, %cl
  20893. + andl $0xff, %eax
  20894. + andl $0xff, %edx
  20895. + movl 0x600+des_SPtrans(%ebx),%ebx
  20896. + xorl %ebx, %esi
  20897. + movl 0x700+des_SPtrans(%ecx),%ebx
  20898. + xorl %ebx, %esi
  20899. + movl 0x400+des_SPtrans(%eax),%ebx
  20900. + xorl %ebx, %esi
  20901. + movl 0x500+des_SPtrans(%edx),%ebx
  20902. + xorl %ebx, %esi
  20903. +
  20904. +
  20905. + movl 40(%ebp), %eax
  20906. + xorl %ebx, %ebx
  20907. + movl 44(%ebp), %edx
  20908. + xorl %esi, %eax
  20909. + xorl %esi, %edx
  20910. + andl $0xfcfcfcfc, %eax
  20911. + andl $0xcfcfcfcf, %edx
  20912. + movb %al, %bl
  20913. + movb %ah, %cl
  20914. + rorl $4, %edx
  20915. + movl des_SPtrans(%ebx),%ebp
  20916. + movb %dl, %bl
  20917. + xorl %ebp, %edi
  20918. + movl 0x200+des_SPtrans(%ecx),%ebp
  20919. + xorl %ebp, %edi
  20920. + movb %dh, %cl
  20921. + shrl $16, %eax
  20922. + movl 0x100+des_SPtrans(%ebx),%ebp
  20923. + xorl %ebp, %edi
  20924. + movb %ah, %bl
  20925. + shrl $16, %edx
  20926. + movl 0x300+des_SPtrans(%ecx),%ebp
  20927. + xorl %ebp, %edi
  20928. + movl 24(%esp), %ebp
  20929. + movb %dh, %cl
  20930. + andl $0xff, %eax
  20931. + andl $0xff, %edx
  20932. + movl 0x600+des_SPtrans(%ebx),%ebx
  20933. + xorl %ebx, %edi
  20934. + movl 0x700+des_SPtrans(%ecx),%ebx
  20935. + xorl %ebx, %edi
  20936. + movl 0x400+des_SPtrans(%eax),%ebx
  20937. + xorl %ebx, %edi
  20938. + movl 0x500+des_SPtrans(%edx),%ebx
  20939. + xorl %ebx, %edi
  20940. +
  20941. +
  20942. + movl 32(%ebp), %eax
  20943. + xorl %ebx, %ebx
  20944. + movl 36(%ebp), %edx
  20945. + xorl %edi, %eax
  20946. + xorl %edi, %edx
  20947. + andl $0xfcfcfcfc, %eax
  20948. + andl $0xcfcfcfcf, %edx
  20949. + movb %al, %bl
  20950. + movb %ah, %cl
  20951. + rorl $4, %edx
  20952. + movl des_SPtrans(%ebx),%ebp
  20953. + movb %dl, %bl
  20954. + xorl %ebp, %esi
  20955. + movl 0x200+des_SPtrans(%ecx),%ebp
  20956. + xorl %ebp, %esi
  20957. + movb %dh, %cl
  20958. + shrl $16, %eax
  20959. + movl 0x100+des_SPtrans(%ebx),%ebp
  20960. + xorl %ebp, %esi
  20961. + movb %ah, %bl
  20962. + shrl $16, %edx
  20963. + movl 0x300+des_SPtrans(%ecx),%ebp
  20964. + xorl %ebp, %esi
  20965. + movl 24(%esp), %ebp
  20966. + movb %dh, %cl
  20967. + andl $0xff, %eax
  20968. + andl $0xff, %edx
  20969. + movl 0x600+des_SPtrans(%ebx),%ebx
  20970. + xorl %ebx, %esi
  20971. + movl 0x700+des_SPtrans(%ecx),%ebx
  20972. + xorl %ebx, %esi
  20973. + movl 0x400+des_SPtrans(%eax),%ebx
  20974. + xorl %ebx, %esi
  20975. + movl 0x500+des_SPtrans(%edx),%ebx
  20976. + xorl %ebx, %esi
  20977. +
  20978. +
  20979. + movl 24(%ebp), %eax
  20980. + xorl %ebx, %ebx
  20981. + movl 28(%ebp), %edx
  20982. + xorl %esi, %eax
  20983. + xorl %esi, %edx
  20984. + andl $0xfcfcfcfc, %eax
  20985. + andl $0xcfcfcfcf, %edx
  20986. + movb %al, %bl
  20987. + movb %ah, %cl
  20988. + rorl $4, %edx
  20989. + movl des_SPtrans(%ebx),%ebp
  20990. + movb %dl, %bl
  20991. + xorl %ebp, %edi
  20992. + movl 0x200+des_SPtrans(%ecx),%ebp
  20993. + xorl %ebp, %edi
  20994. + movb %dh, %cl
  20995. + shrl $16, %eax
  20996. + movl 0x100+des_SPtrans(%ebx),%ebp
  20997. + xorl %ebp, %edi
  20998. + movb %ah, %bl
  20999. + shrl $16, %edx
  21000. + movl 0x300+des_SPtrans(%ecx),%ebp
  21001. + xorl %ebp, %edi
  21002. + movl 24(%esp), %ebp
  21003. + movb %dh, %cl
  21004. + andl $0xff, %eax
  21005. + andl $0xff, %edx
  21006. + movl 0x600+des_SPtrans(%ebx),%ebx
  21007. + xorl %ebx, %edi
  21008. + movl 0x700+des_SPtrans(%ecx),%ebx
  21009. + xorl %ebx, %edi
  21010. + movl 0x400+des_SPtrans(%eax),%ebx
  21011. + xorl %ebx, %edi
  21012. + movl 0x500+des_SPtrans(%edx),%ebx
  21013. + xorl %ebx, %edi
  21014. +
  21015. +
  21016. + movl 16(%ebp), %eax
  21017. + xorl %ebx, %ebx
  21018. + movl 20(%ebp), %edx
  21019. + xorl %edi, %eax
  21020. + xorl %edi, %edx
  21021. + andl $0xfcfcfcfc, %eax
  21022. + andl $0xcfcfcfcf, %edx
  21023. + movb %al, %bl
  21024. + movb %ah, %cl
  21025. + rorl $4, %edx
  21026. + movl des_SPtrans(%ebx),%ebp
  21027. + movb %dl, %bl
  21028. + xorl %ebp, %esi
  21029. + movl 0x200+des_SPtrans(%ecx),%ebp
  21030. + xorl %ebp, %esi
  21031. + movb %dh, %cl
  21032. + shrl $16, %eax
  21033. + movl 0x100+des_SPtrans(%ebx),%ebp
  21034. + xorl %ebp, %esi
  21035. + movb %ah, %bl
  21036. + shrl $16, %edx
  21037. + movl 0x300+des_SPtrans(%ecx),%ebp
  21038. + xorl %ebp, %esi
  21039. + movl 24(%esp), %ebp
  21040. + movb %dh, %cl
  21041. + andl $0xff, %eax
  21042. + andl $0xff, %edx
  21043. + movl 0x600+des_SPtrans(%ebx),%ebx
  21044. + xorl %ebx, %esi
  21045. + movl 0x700+des_SPtrans(%ecx),%ebx
  21046. + xorl %ebx, %esi
  21047. + movl 0x400+des_SPtrans(%eax),%ebx
  21048. + xorl %ebx, %esi
  21049. + movl 0x500+des_SPtrans(%edx),%ebx
  21050. + xorl %ebx, %esi
  21051. +
  21052. +
  21053. + movl 8(%ebp), %eax
  21054. + xorl %ebx, %ebx
  21055. + movl 12(%ebp), %edx
  21056. + xorl %esi, %eax
  21057. + xorl %esi, %edx
  21058. + andl $0xfcfcfcfc, %eax
  21059. + andl $0xcfcfcfcf, %edx
  21060. + movb %al, %bl
  21061. + movb %ah, %cl
  21062. + rorl $4, %edx
  21063. + movl des_SPtrans(%ebx),%ebp
  21064. + movb %dl, %bl
  21065. + xorl %ebp, %edi
  21066. + movl 0x200+des_SPtrans(%ecx),%ebp
  21067. + xorl %ebp, %edi
  21068. + movb %dh, %cl
  21069. + shrl $16, %eax
  21070. + movl 0x100+des_SPtrans(%ebx),%ebp
  21071. + xorl %ebp, %edi
  21072. + movb %ah, %bl
  21073. + shrl $16, %edx
  21074. + movl 0x300+des_SPtrans(%ecx),%ebp
  21075. + xorl %ebp, %edi
  21076. + movl 24(%esp), %ebp
  21077. + movb %dh, %cl
  21078. + andl $0xff, %eax
  21079. + andl $0xff, %edx
  21080. + movl 0x600+des_SPtrans(%ebx),%ebx
  21081. + xorl %ebx, %edi
  21082. + movl 0x700+des_SPtrans(%ecx),%ebx
  21083. + xorl %ebx, %edi
  21084. + movl 0x400+des_SPtrans(%eax),%ebx
  21085. + xorl %ebx, %edi
  21086. + movl 0x500+des_SPtrans(%edx),%ebx
  21087. + xorl %ebx, %edi
  21088. +
  21089. +
  21090. + movl (%ebp), %eax
  21091. + xorl %ebx, %ebx
  21092. + movl 4(%ebp), %edx
  21093. + xorl %edi, %eax
  21094. + xorl %edi, %edx
  21095. + andl $0xfcfcfcfc, %eax
  21096. + andl $0xcfcfcfcf, %edx
  21097. + movb %al, %bl
  21098. + movb %ah, %cl
  21099. + rorl $4, %edx
  21100. + movl des_SPtrans(%ebx),%ebp
  21101. + movb %dl, %bl
  21102. + xorl %ebp, %esi
  21103. + movl 0x200+des_SPtrans(%ecx),%ebp
  21104. + xorl %ebp, %esi
  21105. + movb %dh, %cl
  21106. + shrl $16, %eax
  21107. + movl 0x100+des_SPtrans(%ebx),%ebp
  21108. + xorl %ebp, %esi
  21109. + movb %ah, %bl
  21110. + shrl $16, %edx
  21111. + movl 0x300+des_SPtrans(%ecx),%ebp
  21112. + xorl %ebp, %esi
  21113. + movl 24(%esp), %ebp
  21114. + movb %dh, %cl
  21115. + andl $0xff, %eax
  21116. + andl $0xff, %edx
  21117. + movl 0x600+des_SPtrans(%ebx),%ebx
  21118. + xorl %ebx, %esi
  21119. + movl 0x700+des_SPtrans(%ecx),%ebx
  21120. + xorl %ebx, %esi
  21121. + movl 0x400+des_SPtrans(%eax),%ebx
  21122. + xorl %ebx, %esi
  21123. + movl 0x500+des_SPtrans(%edx),%ebx
  21124. + xorl %ebx, %esi
  21125. +.L001end:
  21126. +
  21127. +
  21128. + movl 20(%esp), %edx
  21129. +.byte 209
  21130. +.byte 206
  21131. + movl %edi, %eax
  21132. + xorl %esi, %edi
  21133. + andl $0xaaaaaaaa, %edi
  21134. + xorl %edi, %eax
  21135. + xorl %edi, %esi
  21136. +
  21137. + roll $23, %eax
  21138. + movl %eax, %edi
  21139. + xorl %esi, %eax
  21140. + andl $0x03fc03fc, %eax
  21141. + xorl %eax, %edi
  21142. + xorl %eax, %esi
  21143. +
  21144. + roll $10, %edi
  21145. + movl %edi, %eax
  21146. + xorl %esi, %edi
  21147. + andl $0x33333333, %edi
  21148. + xorl %edi, %eax
  21149. + xorl %edi, %esi
  21150. +
  21151. + roll $18, %esi
  21152. + movl %esi, %edi
  21153. + xorl %eax, %esi
  21154. + andl $0xfff0000f, %esi
  21155. + xorl %esi, %edi
  21156. + xorl %esi, %eax
  21157. +
  21158. + roll $12, %edi
  21159. + movl %edi, %esi
  21160. + xorl %eax, %edi
  21161. + andl $0xf0f0f0f0, %edi
  21162. + xorl %edi, %esi
  21163. + xorl %edi, %eax
  21164. +
  21165. + rorl $4, %eax
  21166. + movl %eax, (%edx)
  21167. + movl %esi, 4(%edx)
  21168. + popl %ebp
  21169. + popl %ebx
  21170. + popl %edi
  21171. + popl %esi
  21172. + ret
  21173. +.des_encrypt_end:
  21174. + .size des_encrypt , .des_encrypt_end-des_encrypt
  21175. +.ident "desasm.pl"
  21176. +.text
  21177. + .align 16
  21178. +.globl des_encrypt2
  21179. + .type des_encrypt2 , @function
  21180. +des_encrypt2:
  21181. + pushl %esi
  21182. + pushl %edi
  21183. +
  21184. +
  21185. + movl 12(%esp), %eax
  21186. + xorl %ecx, %ecx
  21187. + pushl %ebx
  21188. + pushl %ebp
  21189. + movl (%eax), %esi
  21190. + movl 28(%esp), %ebx
  21191. + roll $3, %esi
  21192. + movl 4(%eax), %edi
  21193. + roll $3, %edi
  21194. + movl 24(%esp), %ebp
  21195. + cmpl $0, %ebx
  21196. + je .L002start_decrypt
  21197. +
  21198. +
  21199. + movl (%ebp), %eax
  21200. + xorl %ebx, %ebx
  21201. + movl 4(%ebp), %edx
  21202. + xorl %esi, %eax
  21203. + xorl %esi, %edx
  21204. + andl $0xfcfcfcfc, %eax
  21205. + andl $0xcfcfcfcf, %edx
  21206. + movb %al, %bl
  21207. + movb %ah, %cl
  21208. + rorl $4, %edx
  21209. + movl des_SPtrans(%ebx),%ebp
  21210. + movb %dl, %bl
  21211. + xorl %ebp, %edi
  21212. + movl 0x200+des_SPtrans(%ecx),%ebp
  21213. + xorl %ebp, %edi
  21214. + movb %dh, %cl
  21215. + shrl $16, %eax
  21216. + movl 0x100+des_SPtrans(%ebx),%ebp
  21217. + xorl %ebp, %edi
  21218. + movb %ah, %bl
  21219. + shrl $16, %edx
  21220. + movl 0x300+des_SPtrans(%ecx),%ebp
  21221. + xorl %ebp, %edi
  21222. + movl 24(%esp), %ebp
  21223. + movb %dh, %cl
  21224. + andl $0xff, %eax
  21225. + andl $0xff, %edx
  21226. + movl 0x600+des_SPtrans(%ebx),%ebx
  21227. + xorl %ebx, %edi
  21228. + movl 0x700+des_SPtrans(%ecx),%ebx
  21229. + xorl %ebx, %edi
  21230. + movl 0x400+des_SPtrans(%eax),%ebx
  21231. + xorl %ebx, %edi
  21232. + movl 0x500+des_SPtrans(%edx),%ebx
  21233. + xorl %ebx, %edi
  21234. +
  21235. +
  21236. + movl 8(%ebp), %eax
  21237. + xorl %ebx, %ebx
  21238. + movl 12(%ebp), %edx
  21239. + xorl %edi, %eax
  21240. + xorl %edi, %edx
  21241. + andl $0xfcfcfcfc, %eax
  21242. + andl $0xcfcfcfcf, %edx
  21243. + movb %al, %bl
  21244. + movb %ah, %cl
  21245. + rorl $4, %edx
  21246. + movl des_SPtrans(%ebx),%ebp
  21247. + movb %dl, %bl
  21248. + xorl %ebp, %esi
  21249. + movl 0x200+des_SPtrans(%ecx),%ebp
  21250. + xorl %ebp, %esi
  21251. + movb %dh, %cl
  21252. + shrl $16, %eax
  21253. + movl 0x100+des_SPtrans(%ebx),%ebp
  21254. + xorl %ebp, %esi
  21255. + movb %ah, %bl
  21256. + shrl $16, %edx
  21257. + movl 0x300+des_SPtrans(%ecx),%ebp
  21258. + xorl %ebp, %esi
  21259. + movl 24(%esp), %ebp
  21260. + movb %dh, %cl
  21261. + andl $0xff, %eax
  21262. + andl $0xff, %edx
  21263. + movl 0x600+des_SPtrans(%ebx),%ebx
  21264. + xorl %ebx, %esi
  21265. + movl 0x700+des_SPtrans(%ecx),%ebx
  21266. + xorl %ebx, %esi
  21267. + movl 0x400+des_SPtrans(%eax),%ebx
  21268. + xorl %ebx, %esi
  21269. + movl 0x500+des_SPtrans(%edx),%ebx
  21270. + xorl %ebx, %esi
  21271. +
  21272. +
  21273. + movl 16(%ebp), %eax
  21274. + xorl %ebx, %ebx
  21275. + movl 20(%ebp), %edx
  21276. + xorl %esi, %eax
  21277. + xorl %esi, %edx
  21278. + andl $0xfcfcfcfc, %eax
  21279. + andl $0xcfcfcfcf, %edx
  21280. + movb %al, %bl
  21281. + movb %ah, %cl
  21282. + rorl $4, %edx
  21283. + movl des_SPtrans(%ebx),%ebp
  21284. + movb %dl, %bl
  21285. + xorl %ebp, %edi
  21286. + movl 0x200+des_SPtrans(%ecx),%ebp
  21287. + xorl %ebp, %edi
  21288. + movb %dh, %cl
  21289. + shrl $16, %eax
  21290. + movl 0x100+des_SPtrans(%ebx),%ebp
  21291. + xorl %ebp, %edi
  21292. + movb %ah, %bl
  21293. + shrl $16, %edx
  21294. + movl 0x300+des_SPtrans(%ecx),%ebp
  21295. + xorl %ebp, %edi
  21296. + movl 24(%esp), %ebp
  21297. + movb %dh, %cl
  21298. + andl $0xff, %eax
  21299. + andl $0xff, %edx
  21300. + movl 0x600+des_SPtrans(%ebx),%ebx
  21301. + xorl %ebx, %edi
  21302. + movl 0x700+des_SPtrans(%ecx),%ebx
  21303. + xorl %ebx, %edi
  21304. + movl 0x400+des_SPtrans(%eax),%ebx
  21305. + xorl %ebx, %edi
  21306. + movl 0x500+des_SPtrans(%edx),%ebx
  21307. + xorl %ebx, %edi
  21308. +
  21309. +
  21310. + movl 24(%ebp), %eax
  21311. + xorl %ebx, %ebx
  21312. + movl 28(%ebp), %edx
  21313. + xorl %edi, %eax
  21314. + xorl %edi, %edx
  21315. + andl $0xfcfcfcfc, %eax
  21316. + andl $0xcfcfcfcf, %edx
  21317. + movb %al, %bl
  21318. + movb %ah, %cl
  21319. + rorl $4, %edx
  21320. + movl des_SPtrans(%ebx),%ebp
  21321. + movb %dl, %bl
  21322. + xorl %ebp, %esi
  21323. + movl 0x200+des_SPtrans(%ecx),%ebp
  21324. + xorl %ebp, %esi
  21325. + movb %dh, %cl
  21326. + shrl $16, %eax
  21327. + movl 0x100+des_SPtrans(%ebx),%ebp
  21328. + xorl %ebp, %esi
  21329. + movb %ah, %bl
  21330. + shrl $16, %edx
  21331. + movl 0x300+des_SPtrans(%ecx),%ebp
  21332. + xorl %ebp, %esi
  21333. + movl 24(%esp), %ebp
  21334. + movb %dh, %cl
  21335. + andl $0xff, %eax
  21336. + andl $0xff, %edx
  21337. + movl 0x600+des_SPtrans(%ebx),%ebx
  21338. + xorl %ebx, %esi
  21339. + movl 0x700+des_SPtrans(%ecx),%ebx
  21340. + xorl %ebx, %esi
  21341. + movl 0x400+des_SPtrans(%eax),%ebx
  21342. + xorl %ebx, %esi
  21343. + movl 0x500+des_SPtrans(%edx),%ebx
  21344. + xorl %ebx, %esi
  21345. +
  21346. +
  21347. + movl 32(%ebp), %eax
  21348. + xorl %ebx, %ebx
  21349. + movl 36(%ebp), %edx
  21350. + xorl %esi, %eax
  21351. + xorl %esi, %edx
  21352. + andl $0xfcfcfcfc, %eax
  21353. + andl $0xcfcfcfcf, %edx
  21354. + movb %al, %bl
  21355. + movb %ah, %cl
  21356. + rorl $4, %edx
  21357. + movl des_SPtrans(%ebx),%ebp
  21358. + movb %dl, %bl
  21359. + xorl %ebp, %edi
  21360. + movl 0x200+des_SPtrans(%ecx),%ebp
  21361. + xorl %ebp, %edi
  21362. + movb %dh, %cl
  21363. + shrl $16, %eax
  21364. + movl 0x100+des_SPtrans(%ebx),%ebp
  21365. + xorl %ebp, %edi
  21366. + movb %ah, %bl
  21367. + shrl $16, %edx
  21368. + movl 0x300+des_SPtrans(%ecx),%ebp
  21369. + xorl %ebp, %edi
  21370. + movl 24(%esp), %ebp
  21371. + movb %dh, %cl
  21372. + andl $0xff, %eax
  21373. + andl $0xff, %edx
  21374. + movl 0x600+des_SPtrans(%ebx),%ebx
  21375. + xorl %ebx, %edi
  21376. + movl 0x700+des_SPtrans(%ecx),%ebx
  21377. + xorl %ebx, %edi
  21378. + movl 0x400+des_SPtrans(%eax),%ebx
  21379. + xorl %ebx, %edi
  21380. + movl 0x500+des_SPtrans(%edx),%ebx
  21381. + xorl %ebx, %edi
  21382. +
  21383. +
  21384. + movl 40(%ebp), %eax
  21385. + xorl %ebx, %ebx
  21386. + movl 44(%ebp), %edx
  21387. + xorl %edi, %eax
  21388. + xorl %edi, %edx
  21389. + andl $0xfcfcfcfc, %eax
  21390. + andl $0xcfcfcfcf, %edx
  21391. + movb %al, %bl
  21392. + movb %ah, %cl
  21393. + rorl $4, %edx
  21394. + movl des_SPtrans(%ebx),%ebp
  21395. + movb %dl, %bl
  21396. + xorl %ebp, %esi
  21397. + movl 0x200+des_SPtrans(%ecx),%ebp
  21398. + xorl %ebp, %esi
  21399. + movb %dh, %cl
  21400. + shrl $16, %eax
  21401. + movl 0x100+des_SPtrans(%ebx),%ebp
  21402. + xorl %ebp, %esi
  21403. + movb %ah, %bl
  21404. + shrl $16, %edx
  21405. + movl 0x300+des_SPtrans(%ecx),%ebp
  21406. + xorl %ebp, %esi
  21407. + movl 24(%esp), %ebp
  21408. + movb %dh, %cl
  21409. + andl $0xff, %eax
  21410. + andl $0xff, %edx
  21411. + movl 0x600+des_SPtrans(%ebx),%ebx
  21412. + xorl %ebx, %esi
  21413. + movl 0x700+des_SPtrans(%ecx),%ebx
  21414. + xorl %ebx, %esi
  21415. + movl 0x400+des_SPtrans(%eax),%ebx
  21416. + xorl %ebx, %esi
  21417. + movl 0x500+des_SPtrans(%edx),%ebx
  21418. + xorl %ebx, %esi
  21419. +
  21420. +
  21421. + movl 48(%ebp), %eax
  21422. + xorl %ebx, %ebx
  21423. + movl 52(%ebp), %edx
  21424. + xorl %esi, %eax
  21425. + xorl %esi, %edx
  21426. + andl $0xfcfcfcfc, %eax
  21427. + andl $0xcfcfcfcf, %edx
  21428. + movb %al, %bl
  21429. + movb %ah, %cl
  21430. + rorl $4, %edx
  21431. + movl des_SPtrans(%ebx),%ebp
  21432. + movb %dl, %bl
  21433. + xorl %ebp, %edi
  21434. + movl 0x200+des_SPtrans(%ecx),%ebp
  21435. + xorl %ebp, %edi
  21436. + movb %dh, %cl
  21437. + shrl $16, %eax
  21438. + movl 0x100+des_SPtrans(%ebx),%ebp
  21439. + xorl %ebp, %edi
  21440. + movb %ah, %bl
  21441. + shrl $16, %edx
  21442. + movl 0x300+des_SPtrans(%ecx),%ebp
  21443. + xorl %ebp, %edi
  21444. + movl 24(%esp), %ebp
  21445. + movb %dh, %cl
  21446. + andl $0xff, %eax
  21447. + andl $0xff, %edx
  21448. + movl 0x600+des_SPtrans(%ebx),%ebx
  21449. + xorl %ebx, %edi
  21450. + movl 0x700+des_SPtrans(%ecx),%ebx
  21451. + xorl %ebx, %edi
  21452. + movl 0x400+des_SPtrans(%eax),%ebx
  21453. + xorl %ebx, %edi
  21454. + movl 0x500+des_SPtrans(%edx),%ebx
  21455. + xorl %ebx, %edi
  21456. +
  21457. +
  21458. + movl 56(%ebp), %eax
  21459. + xorl %ebx, %ebx
  21460. + movl 60(%ebp), %edx
  21461. + xorl %edi, %eax
  21462. + xorl %edi, %edx
  21463. + andl $0xfcfcfcfc, %eax
  21464. + andl $0xcfcfcfcf, %edx
  21465. + movb %al, %bl
  21466. + movb %ah, %cl
  21467. + rorl $4, %edx
  21468. + movl des_SPtrans(%ebx),%ebp
  21469. + movb %dl, %bl
  21470. + xorl %ebp, %esi
  21471. + movl 0x200+des_SPtrans(%ecx),%ebp
  21472. + xorl %ebp, %esi
  21473. + movb %dh, %cl
  21474. + shrl $16, %eax
  21475. + movl 0x100+des_SPtrans(%ebx),%ebp
  21476. + xorl %ebp, %esi
  21477. + movb %ah, %bl
  21478. + shrl $16, %edx
  21479. + movl 0x300+des_SPtrans(%ecx),%ebp
  21480. + xorl %ebp, %esi
  21481. + movl 24(%esp), %ebp
  21482. + movb %dh, %cl
  21483. + andl $0xff, %eax
  21484. + andl $0xff, %edx
  21485. + movl 0x600+des_SPtrans(%ebx),%ebx
  21486. + xorl %ebx, %esi
  21487. + movl 0x700+des_SPtrans(%ecx),%ebx
  21488. + xorl %ebx, %esi
  21489. + movl 0x400+des_SPtrans(%eax),%ebx
  21490. + xorl %ebx, %esi
  21491. + movl 0x500+des_SPtrans(%edx),%ebx
  21492. + xorl %ebx, %esi
  21493. +
  21494. +
  21495. + movl 64(%ebp), %eax
  21496. + xorl %ebx, %ebx
  21497. + movl 68(%ebp), %edx
  21498. + xorl %esi, %eax
  21499. + xorl %esi, %edx
  21500. + andl $0xfcfcfcfc, %eax
  21501. + andl $0xcfcfcfcf, %edx
  21502. + movb %al, %bl
  21503. + movb %ah, %cl
  21504. + rorl $4, %edx
  21505. + movl des_SPtrans(%ebx),%ebp
  21506. + movb %dl, %bl
  21507. + xorl %ebp, %edi
  21508. + movl 0x200+des_SPtrans(%ecx),%ebp
  21509. + xorl %ebp, %edi
  21510. + movb %dh, %cl
  21511. + shrl $16, %eax
  21512. + movl 0x100+des_SPtrans(%ebx),%ebp
  21513. + xorl %ebp, %edi
  21514. + movb %ah, %bl
  21515. + shrl $16, %edx
  21516. + movl 0x300+des_SPtrans(%ecx),%ebp
  21517. + xorl %ebp, %edi
  21518. + movl 24(%esp), %ebp
  21519. + movb %dh, %cl
  21520. + andl $0xff, %eax
  21521. + andl $0xff, %edx
  21522. + movl 0x600+des_SPtrans(%ebx),%ebx
  21523. + xorl %ebx, %edi
  21524. + movl 0x700+des_SPtrans(%ecx),%ebx
  21525. + xorl %ebx, %edi
  21526. + movl 0x400+des_SPtrans(%eax),%ebx
  21527. + xorl %ebx, %edi
  21528. + movl 0x500+des_SPtrans(%edx),%ebx
  21529. + xorl %ebx, %edi
  21530. +
  21531. +
  21532. + movl 72(%ebp), %eax
  21533. + xorl %ebx, %ebx
  21534. + movl 76(%ebp), %edx
  21535. + xorl %edi, %eax
  21536. + xorl %edi, %edx
  21537. + andl $0xfcfcfcfc, %eax
  21538. + andl $0xcfcfcfcf, %edx
  21539. + movb %al, %bl
  21540. + movb %ah, %cl
  21541. + rorl $4, %edx
  21542. + movl des_SPtrans(%ebx),%ebp
  21543. + movb %dl, %bl
  21544. + xorl %ebp, %esi
  21545. + movl 0x200+des_SPtrans(%ecx),%ebp
  21546. + xorl %ebp, %esi
  21547. + movb %dh, %cl
  21548. + shrl $16, %eax
  21549. + movl 0x100+des_SPtrans(%ebx),%ebp
  21550. + xorl %ebp, %esi
  21551. + movb %ah, %bl
  21552. + shrl $16, %edx
  21553. + movl 0x300+des_SPtrans(%ecx),%ebp
  21554. + xorl %ebp, %esi
  21555. + movl 24(%esp), %ebp
  21556. + movb %dh, %cl
  21557. + andl $0xff, %eax
  21558. + andl $0xff, %edx
  21559. + movl 0x600+des_SPtrans(%ebx),%ebx
  21560. + xorl %ebx, %esi
  21561. + movl 0x700+des_SPtrans(%ecx),%ebx
  21562. + xorl %ebx, %esi
  21563. + movl 0x400+des_SPtrans(%eax),%ebx
  21564. + xorl %ebx, %esi
  21565. + movl 0x500+des_SPtrans(%edx),%ebx
  21566. + xorl %ebx, %esi
  21567. +
  21568. +
  21569. + movl 80(%ebp), %eax
  21570. + xorl %ebx, %ebx
  21571. + movl 84(%ebp), %edx
  21572. + xorl %esi, %eax
  21573. + xorl %esi, %edx
  21574. + andl $0xfcfcfcfc, %eax
  21575. + andl $0xcfcfcfcf, %edx
  21576. + movb %al, %bl
  21577. + movb %ah, %cl
  21578. + rorl $4, %edx
  21579. + movl des_SPtrans(%ebx),%ebp
  21580. + movb %dl, %bl
  21581. + xorl %ebp, %edi
  21582. + movl 0x200+des_SPtrans(%ecx),%ebp
  21583. + xorl %ebp, %edi
  21584. + movb %dh, %cl
  21585. + shrl $16, %eax
  21586. + movl 0x100+des_SPtrans(%ebx),%ebp
  21587. + xorl %ebp, %edi
  21588. + movb %ah, %bl
  21589. + shrl $16, %edx
  21590. + movl 0x300+des_SPtrans(%ecx),%ebp
  21591. + xorl %ebp, %edi
  21592. + movl 24(%esp), %ebp
  21593. + movb %dh, %cl
  21594. + andl $0xff, %eax
  21595. + andl $0xff, %edx
  21596. + movl 0x600+des_SPtrans(%ebx),%ebx
  21597. + xorl %ebx, %edi
  21598. + movl 0x700+des_SPtrans(%ecx),%ebx
  21599. + xorl %ebx, %edi
  21600. + movl 0x400+des_SPtrans(%eax),%ebx
  21601. + xorl %ebx, %edi
  21602. + movl 0x500+des_SPtrans(%edx),%ebx
  21603. + xorl %ebx, %edi
  21604. +
  21605. +
  21606. + movl 88(%ebp), %eax
  21607. + xorl %ebx, %ebx
  21608. + movl 92(%ebp), %edx
  21609. + xorl %edi, %eax
  21610. + xorl %edi, %edx
  21611. + andl $0xfcfcfcfc, %eax
  21612. + andl $0xcfcfcfcf, %edx
  21613. + movb %al, %bl
  21614. + movb %ah, %cl
  21615. + rorl $4, %edx
  21616. + movl des_SPtrans(%ebx),%ebp
  21617. + movb %dl, %bl
  21618. + xorl %ebp, %esi
  21619. + movl 0x200+des_SPtrans(%ecx),%ebp
  21620. + xorl %ebp, %esi
  21621. + movb %dh, %cl
  21622. + shrl $16, %eax
  21623. + movl 0x100+des_SPtrans(%ebx),%ebp
  21624. + xorl %ebp, %esi
  21625. + movb %ah, %bl
  21626. + shrl $16, %edx
  21627. + movl 0x300+des_SPtrans(%ecx),%ebp
  21628. + xorl %ebp, %esi
  21629. + movl 24(%esp), %ebp
  21630. + movb %dh, %cl
  21631. + andl $0xff, %eax
  21632. + andl $0xff, %edx
  21633. + movl 0x600+des_SPtrans(%ebx),%ebx
  21634. + xorl %ebx, %esi
  21635. + movl 0x700+des_SPtrans(%ecx),%ebx
  21636. + xorl %ebx, %esi
  21637. + movl 0x400+des_SPtrans(%eax),%ebx
  21638. + xorl %ebx, %esi
  21639. + movl 0x500+des_SPtrans(%edx),%ebx
  21640. + xorl %ebx, %esi
  21641. +
  21642. +
  21643. + movl 96(%ebp), %eax
  21644. + xorl %ebx, %ebx
  21645. + movl 100(%ebp), %edx
  21646. + xorl %esi, %eax
  21647. + xorl %esi, %edx
  21648. + andl $0xfcfcfcfc, %eax
  21649. + andl $0xcfcfcfcf, %edx
  21650. + movb %al, %bl
  21651. + movb %ah, %cl
  21652. + rorl $4, %edx
  21653. + movl des_SPtrans(%ebx),%ebp
  21654. + movb %dl, %bl
  21655. + xorl %ebp, %edi
  21656. + movl 0x200+des_SPtrans(%ecx),%ebp
  21657. + xorl %ebp, %edi
  21658. + movb %dh, %cl
  21659. + shrl $16, %eax
  21660. + movl 0x100+des_SPtrans(%ebx),%ebp
  21661. + xorl %ebp, %edi
  21662. + movb %ah, %bl
  21663. + shrl $16, %edx
  21664. + movl 0x300+des_SPtrans(%ecx),%ebp
  21665. + xorl %ebp, %edi
  21666. + movl 24(%esp), %ebp
  21667. + movb %dh, %cl
  21668. + andl $0xff, %eax
  21669. + andl $0xff, %edx
  21670. + movl 0x600+des_SPtrans(%ebx),%ebx
  21671. + xorl %ebx, %edi
  21672. + movl 0x700+des_SPtrans(%ecx),%ebx
  21673. + xorl %ebx, %edi
  21674. + movl 0x400+des_SPtrans(%eax),%ebx
  21675. + xorl %ebx, %edi
  21676. + movl 0x500+des_SPtrans(%edx),%ebx
  21677. + xorl %ebx, %edi
  21678. +
  21679. +
  21680. + movl 104(%ebp), %eax
  21681. + xorl %ebx, %ebx
  21682. + movl 108(%ebp), %edx
  21683. + xorl %edi, %eax
  21684. + xorl %edi, %edx
  21685. + andl $0xfcfcfcfc, %eax
  21686. + andl $0xcfcfcfcf, %edx
  21687. + movb %al, %bl
  21688. + movb %ah, %cl
  21689. + rorl $4, %edx
  21690. + movl des_SPtrans(%ebx),%ebp
  21691. + movb %dl, %bl
  21692. + xorl %ebp, %esi
  21693. + movl 0x200+des_SPtrans(%ecx),%ebp
  21694. + xorl %ebp, %esi
  21695. + movb %dh, %cl
  21696. + shrl $16, %eax
  21697. + movl 0x100+des_SPtrans(%ebx),%ebp
  21698. + xorl %ebp, %esi
  21699. + movb %ah, %bl
  21700. + shrl $16, %edx
  21701. + movl 0x300+des_SPtrans(%ecx),%ebp
  21702. + xorl %ebp, %esi
  21703. + movl 24(%esp), %ebp
  21704. + movb %dh, %cl
  21705. + andl $0xff, %eax
  21706. + andl $0xff, %edx
  21707. + movl 0x600+des_SPtrans(%ebx),%ebx
  21708. + xorl %ebx, %esi
  21709. + movl 0x700+des_SPtrans(%ecx),%ebx
  21710. + xorl %ebx, %esi
  21711. + movl 0x400+des_SPtrans(%eax),%ebx
  21712. + xorl %ebx, %esi
  21713. + movl 0x500+des_SPtrans(%edx),%ebx
  21714. + xorl %ebx, %esi
  21715. +
  21716. +
  21717. + movl 112(%ebp), %eax
  21718. + xorl %ebx, %ebx
  21719. + movl 116(%ebp), %edx
  21720. + xorl %esi, %eax
  21721. + xorl %esi, %edx
  21722. + andl $0xfcfcfcfc, %eax
  21723. + andl $0xcfcfcfcf, %edx
  21724. + movb %al, %bl
  21725. + movb %ah, %cl
  21726. + rorl $4, %edx
  21727. + movl des_SPtrans(%ebx),%ebp
  21728. + movb %dl, %bl
  21729. + xorl %ebp, %edi
  21730. + movl 0x200+des_SPtrans(%ecx),%ebp
  21731. + xorl %ebp, %edi
  21732. + movb %dh, %cl
  21733. + shrl $16, %eax
  21734. + movl 0x100+des_SPtrans(%ebx),%ebp
  21735. + xorl %ebp, %edi
  21736. + movb %ah, %bl
  21737. + shrl $16, %edx
  21738. + movl 0x300+des_SPtrans(%ecx),%ebp
  21739. + xorl %ebp, %edi
  21740. + movl 24(%esp), %ebp
  21741. + movb %dh, %cl
  21742. + andl $0xff, %eax
  21743. + andl $0xff, %edx
  21744. + movl 0x600+des_SPtrans(%ebx),%ebx
  21745. + xorl %ebx, %edi
  21746. + movl 0x700+des_SPtrans(%ecx),%ebx
  21747. + xorl %ebx, %edi
  21748. + movl 0x400+des_SPtrans(%eax),%ebx
  21749. + xorl %ebx, %edi
  21750. + movl 0x500+des_SPtrans(%edx),%ebx
  21751. + xorl %ebx, %edi
  21752. +
  21753. +
  21754. + movl 120(%ebp), %eax
  21755. + xorl %ebx, %ebx
  21756. + movl 124(%ebp), %edx
  21757. + xorl %edi, %eax
  21758. + xorl %edi, %edx
  21759. + andl $0xfcfcfcfc, %eax
  21760. + andl $0xcfcfcfcf, %edx
  21761. + movb %al, %bl
  21762. + movb %ah, %cl
  21763. + rorl $4, %edx
  21764. + movl des_SPtrans(%ebx),%ebp
  21765. + movb %dl, %bl
  21766. + xorl %ebp, %esi
  21767. + movl 0x200+des_SPtrans(%ecx),%ebp
  21768. + xorl %ebp, %esi
  21769. + movb %dh, %cl
  21770. + shrl $16, %eax
  21771. + movl 0x100+des_SPtrans(%ebx),%ebp
  21772. + xorl %ebp, %esi
  21773. + movb %ah, %bl
  21774. + shrl $16, %edx
  21775. + movl 0x300+des_SPtrans(%ecx),%ebp
  21776. + xorl %ebp, %esi
  21777. + movl 24(%esp), %ebp
  21778. + movb %dh, %cl
  21779. + andl $0xff, %eax
  21780. + andl $0xff, %edx
  21781. + movl 0x600+des_SPtrans(%ebx),%ebx
  21782. + xorl %ebx, %esi
  21783. + movl 0x700+des_SPtrans(%ecx),%ebx
  21784. + xorl %ebx, %esi
  21785. + movl 0x400+des_SPtrans(%eax),%ebx
  21786. + xorl %ebx, %esi
  21787. + movl 0x500+des_SPtrans(%edx),%ebx
  21788. + xorl %ebx, %esi
  21789. + jmp .L003end
  21790. +.L002start_decrypt:
  21791. +
  21792. +
  21793. + movl 120(%ebp), %eax
  21794. + xorl %ebx, %ebx
  21795. + movl 124(%ebp), %edx
  21796. + xorl %esi, %eax
  21797. + xorl %esi, %edx
  21798. + andl $0xfcfcfcfc, %eax
  21799. + andl $0xcfcfcfcf, %edx
  21800. + movb %al, %bl
  21801. + movb %ah, %cl
  21802. + rorl $4, %edx
  21803. + movl des_SPtrans(%ebx),%ebp
  21804. + movb %dl, %bl
  21805. + xorl %ebp, %edi
  21806. + movl 0x200+des_SPtrans(%ecx),%ebp
  21807. + xorl %ebp, %edi
  21808. + movb %dh, %cl
  21809. + shrl $16, %eax
  21810. + movl 0x100+des_SPtrans(%ebx),%ebp
  21811. + xorl %ebp, %edi
  21812. + movb %ah, %bl
  21813. + shrl $16, %edx
  21814. + movl 0x300+des_SPtrans(%ecx),%ebp
  21815. + xorl %ebp, %edi
  21816. + movl 24(%esp), %ebp
  21817. + movb %dh, %cl
  21818. + andl $0xff, %eax
  21819. + andl $0xff, %edx
  21820. + movl 0x600+des_SPtrans(%ebx),%ebx
  21821. + xorl %ebx, %edi
  21822. + movl 0x700+des_SPtrans(%ecx),%ebx
  21823. + xorl %ebx, %edi
  21824. + movl 0x400+des_SPtrans(%eax),%ebx
  21825. + xorl %ebx, %edi
  21826. + movl 0x500+des_SPtrans(%edx),%ebx
  21827. + xorl %ebx, %edi
  21828. +
  21829. +
  21830. + movl 112(%ebp), %eax
  21831. + xorl %ebx, %ebx
  21832. + movl 116(%ebp), %edx
  21833. + xorl %edi, %eax
  21834. + xorl %edi, %edx
  21835. + andl $0xfcfcfcfc, %eax
  21836. + andl $0xcfcfcfcf, %edx
  21837. + movb %al, %bl
  21838. + movb %ah, %cl
  21839. + rorl $4, %edx
  21840. + movl des_SPtrans(%ebx),%ebp
  21841. + movb %dl, %bl
  21842. + xorl %ebp, %esi
  21843. + movl 0x200+des_SPtrans(%ecx),%ebp
  21844. + xorl %ebp, %esi
  21845. + movb %dh, %cl
  21846. + shrl $16, %eax
  21847. + movl 0x100+des_SPtrans(%ebx),%ebp
  21848. + xorl %ebp, %esi
  21849. + movb %ah, %bl
  21850. + shrl $16, %edx
  21851. + movl 0x300+des_SPtrans(%ecx),%ebp
  21852. + xorl %ebp, %esi
  21853. + movl 24(%esp), %ebp
  21854. + movb %dh, %cl
  21855. + andl $0xff, %eax
  21856. + andl $0xff, %edx
  21857. + movl 0x600+des_SPtrans(%ebx),%ebx
  21858. + xorl %ebx, %esi
  21859. + movl 0x700+des_SPtrans(%ecx),%ebx
  21860. + xorl %ebx, %esi
  21861. + movl 0x400+des_SPtrans(%eax),%ebx
  21862. + xorl %ebx, %esi
  21863. + movl 0x500+des_SPtrans(%edx),%ebx
  21864. + xorl %ebx, %esi
  21865. +
  21866. +
  21867. + movl 104(%ebp), %eax
  21868. + xorl %ebx, %ebx
  21869. + movl 108(%ebp), %edx
  21870. + xorl %esi, %eax
  21871. + xorl %esi, %edx
  21872. + andl $0xfcfcfcfc, %eax
  21873. + andl $0xcfcfcfcf, %edx
  21874. + movb %al, %bl
  21875. + movb %ah, %cl
  21876. + rorl $4, %edx
  21877. + movl des_SPtrans(%ebx),%ebp
  21878. + movb %dl, %bl
  21879. + xorl %ebp, %edi
  21880. + movl 0x200+des_SPtrans(%ecx),%ebp
  21881. + xorl %ebp, %edi
  21882. + movb %dh, %cl
  21883. + shrl $16, %eax
  21884. + movl 0x100+des_SPtrans(%ebx),%ebp
  21885. + xorl %ebp, %edi
  21886. + movb %ah, %bl
  21887. + shrl $16, %edx
  21888. + movl 0x300+des_SPtrans(%ecx),%ebp
  21889. + xorl %ebp, %edi
  21890. + movl 24(%esp), %ebp
  21891. + movb %dh, %cl
  21892. + andl $0xff, %eax
  21893. + andl $0xff, %edx
  21894. + movl 0x600+des_SPtrans(%ebx),%ebx
  21895. + xorl %ebx, %edi
  21896. + movl 0x700+des_SPtrans(%ecx),%ebx
  21897. + xorl %ebx, %edi
  21898. + movl 0x400+des_SPtrans(%eax),%ebx
  21899. + xorl %ebx, %edi
  21900. + movl 0x500+des_SPtrans(%edx),%ebx
  21901. + xorl %ebx, %edi
  21902. +
  21903. +
  21904. + movl 96(%ebp), %eax
  21905. + xorl %ebx, %ebx
  21906. + movl 100(%ebp), %edx
  21907. + xorl %edi, %eax
  21908. + xorl %edi, %edx
  21909. + andl $0xfcfcfcfc, %eax
  21910. + andl $0xcfcfcfcf, %edx
  21911. + movb %al, %bl
  21912. + movb %ah, %cl
  21913. + rorl $4, %edx
  21914. + movl des_SPtrans(%ebx),%ebp
  21915. + movb %dl, %bl
  21916. + xorl %ebp, %esi
  21917. + movl 0x200+des_SPtrans(%ecx),%ebp
  21918. + xorl %ebp, %esi
  21919. + movb %dh, %cl
  21920. + shrl $16, %eax
  21921. + movl 0x100+des_SPtrans(%ebx),%ebp
  21922. + xorl %ebp, %esi
  21923. + movb %ah, %bl
  21924. + shrl $16, %edx
  21925. + movl 0x300+des_SPtrans(%ecx),%ebp
  21926. + xorl %ebp, %esi
  21927. + movl 24(%esp), %ebp
  21928. + movb %dh, %cl
  21929. + andl $0xff, %eax
  21930. + andl $0xff, %edx
  21931. + movl 0x600+des_SPtrans(%ebx),%ebx
  21932. + xorl %ebx, %esi
  21933. + movl 0x700+des_SPtrans(%ecx),%ebx
  21934. + xorl %ebx, %esi
  21935. + movl 0x400+des_SPtrans(%eax),%ebx
  21936. + xorl %ebx, %esi
  21937. + movl 0x500+des_SPtrans(%edx),%ebx
  21938. + xorl %ebx, %esi
  21939. +
  21940. +
  21941. + movl 88(%ebp), %eax
  21942. + xorl %ebx, %ebx
  21943. + movl 92(%ebp), %edx
  21944. + xorl %esi, %eax
  21945. + xorl %esi, %edx
  21946. + andl $0xfcfcfcfc, %eax
  21947. + andl $0xcfcfcfcf, %edx
  21948. + movb %al, %bl
  21949. + movb %ah, %cl
  21950. + rorl $4, %edx
  21951. + movl des_SPtrans(%ebx),%ebp
  21952. + movb %dl, %bl
  21953. + xorl %ebp, %edi
  21954. + movl 0x200+des_SPtrans(%ecx),%ebp
  21955. + xorl %ebp, %edi
  21956. + movb %dh, %cl
  21957. + shrl $16, %eax
  21958. + movl 0x100+des_SPtrans(%ebx),%ebp
  21959. + xorl %ebp, %edi
  21960. + movb %ah, %bl
  21961. + shrl $16, %edx
  21962. + movl 0x300+des_SPtrans(%ecx),%ebp
  21963. + xorl %ebp, %edi
  21964. + movl 24(%esp), %ebp
  21965. + movb %dh, %cl
  21966. + andl $0xff, %eax
  21967. + andl $0xff, %edx
  21968. + movl 0x600+des_SPtrans(%ebx),%ebx
  21969. + xorl %ebx, %edi
  21970. + movl 0x700+des_SPtrans(%ecx),%ebx
  21971. + xorl %ebx, %edi
  21972. + movl 0x400+des_SPtrans(%eax),%ebx
  21973. + xorl %ebx, %edi
  21974. + movl 0x500+des_SPtrans(%edx),%ebx
  21975. + xorl %ebx, %edi
  21976. +
  21977. +
  21978. + movl 80(%ebp), %eax
  21979. + xorl %ebx, %ebx
  21980. + movl 84(%ebp), %edx
  21981. + xorl %edi, %eax
  21982. + xorl %edi, %edx
  21983. + andl $0xfcfcfcfc, %eax
  21984. + andl $0xcfcfcfcf, %edx
  21985. + movb %al, %bl
  21986. + movb %ah, %cl
  21987. + rorl $4, %edx
  21988. + movl des_SPtrans(%ebx),%ebp
  21989. + movb %dl, %bl
  21990. + xorl %ebp, %esi
  21991. + movl 0x200+des_SPtrans(%ecx),%ebp
  21992. + xorl %ebp, %esi
  21993. + movb %dh, %cl
  21994. + shrl $16, %eax
  21995. + movl 0x100+des_SPtrans(%ebx),%ebp
  21996. + xorl %ebp, %esi
  21997. + movb %ah, %bl
  21998. + shrl $16, %edx
  21999. + movl 0x300+des_SPtrans(%ecx),%ebp
  22000. + xorl %ebp, %esi
  22001. + movl 24(%esp), %ebp
  22002. + movb %dh, %cl
  22003. + andl $0xff, %eax
  22004. + andl $0xff, %edx
  22005. + movl 0x600+des_SPtrans(%ebx),%ebx
  22006. + xorl %ebx, %esi
  22007. + movl 0x700+des_SPtrans(%ecx),%ebx
  22008. + xorl %ebx, %esi
  22009. + movl 0x400+des_SPtrans(%eax),%ebx
  22010. + xorl %ebx, %esi
  22011. + movl 0x500+des_SPtrans(%edx),%ebx
  22012. + xorl %ebx, %esi
  22013. +
  22014. +
  22015. + movl 72(%ebp), %eax
  22016. + xorl %ebx, %ebx
  22017. + movl 76(%ebp), %edx
  22018. + xorl %esi, %eax
  22019. + xorl %esi, %edx
  22020. + andl $0xfcfcfcfc, %eax
  22021. + andl $0xcfcfcfcf, %edx
  22022. + movb %al, %bl
  22023. + movb %ah, %cl
  22024. + rorl $4, %edx
  22025. + movl des_SPtrans(%ebx),%ebp
  22026. + movb %dl, %bl
  22027. + xorl %ebp, %edi
  22028. + movl 0x200+des_SPtrans(%ecx),%ebp
  22029. + xorl %ebp, %edi
  22030. + movb %dh, %cl
  22031. + shrl $16, %eax
  22032. + movl 0x100+des_SPtrans(%ebx),%ebp
  22033. + xorl %ebp, %edi
  22034. + movb %ah, %bl
  22035. + shrl $16, %edx
  22036. + movl 0x300+des_SPtrans(%ecx),%ebp
  22037. + xorl %ebp, %edi
  22038. + movl 24(%esp), %ebp
  22039. + movb %dh, %cl
  22040. + andl $0xff, %eax
  22041. + andl $0xff, %edx
  22042. + movl 0x600+des_SPtrans(%ebx),%ebx
  22043. + xorl %ebx, %edi
  22044. + movl 0x700+des_SPtrans(%ecx),%ebx
  22045. + xorl %ebx, %edi
  22046. + movl 0x400+des_SPtrans(%eax),%ebx
  22047. + xorl %ebx, %edi
  22048. + movl 0x500+des_SPtrans(%edx),%ebx
  22049. + xorl %ebx, %edi
  22050. +
  22051. +
  22052. + movl 64(%ebp), %eax
  22053. + xorl %ebx, %ebx
  22054. + movl 68(%ebp), %edx
  22055. + xorl %edi, %eax
  22056. + xorl %edi, %edx
  22057. + andl $0xfcfcfcfc, %eax
  22058. + andl $0xcfcfcfcf, %edx
  22059. + movb %al, %bl
  22060. + movb %ah, %cl
  22061. + rorl $4, %edx
  22062. + movl des_SPtrans(%ebx),%ebp
  22063. + movb %dl, %bl
  22064. + xorl %ebp, %esi
  22065. + movl 0x200+des_SPtrans(%ecx),%ebp
  22066. + xorl %ebp, %esi
  22067. + movb %dh, %cl
  22068. + shrl $16, %eax
  22069. + movl 0x100+des_SPtrans(%ebx),%ebp
  22070. + xorl %ebp, %esi
  22071. + movb %ah, %bl
  22072. + shrl $16, %edx
  22073. + movl 0x300+des_SPtrans(%ecx),%ebp
  22074. + xorl %ebp, %esi
  22075. + movl 24(%esp), %ebp
  22076. + movb %dh, %cl
  22077. + andl $0xff, %eax
  22078. + andl $0xff, %edx
  22079. + movl 0x600+des_SPtrans(%ebx),%ebx
  22080. + xorl %ebx, %esi
  22081. + movl 0x700+des_SPtrans(%ecx),%ebx
  22082. + xorl %ebx, %esi
  22083. + movl 0x400+des_SPtrans(%eax),%ebx
  22084. + xorl %ebx, %esi
  22085. + movl 0x500+des_SPtrans(%edx),%ebx
  22086. + xorl %ebx, %esi
  22087. +
  22088. +
  22089. + movl 56(%ebp), %eax
  22090. + xorl %ebx, %ebx
  22091. + movl 60(%ebp), %edx
  22092. + xorl %esi, %eax
  22093. + xorl %esi, %edx
  22094. + andl $0xfcfcfcfc, %eax
  22095. + andl $0xcfcfcfcf, %edx
  22096. + movb %al, %bl
  22097. + movb %ah, %cl
  22098. + rorl $4, %edx
  22099. + movl des_SPtrans(%ebx),%ebp
  22100. + movb %dl, %bl
  22101. + xorl %ebp, %edi
  22102. + movl 0x200+des_SPtrans(%ecx),%ebp
  22103. + xorl %ebp, %edi
  22104. + movb %dh, %cl
  22105. + shrl $16, %eax
  22106. + movl 0x100+des_SPtrans(%ebx),%ebp
  22107. + xorl %ebp, %edi
  22108. + movb %ah, %bl
  22109. + shrl $16, %edx
  22110. + movl 0x300+des_SPtrans(%ecx),%ebp
  22111. + xorl %ebp, %edi
  22112. + movl 24(%esp), %ebp
  22113. + movb %dh, %cl
  22114. + andl $0xff, %eax
  22115. + andl $0xff, %edx
  22116. + movl 0x600+des_SPtrans(%ebx),%ebx
  22117. + xorl %ebx, %edi
  22118. + movl 0x700+des_SPtrans(%ecx),%ebx
  22119. + xorl %ebx, %edi
  22120. + movl 0x400+des_SPtrans(%eax),%ebx
  22121. + xorl %ebx, %edi
  22122. + movl 0x500+des_SPtrans(%edx),%ebx
  22123. + xorl %ebx, %edi
  22124. +
  22125. +
  22126. + movl 48(%ebp), %eax
  22127. + xorl %ebx, %ebx
  22128. + movl 52(%ebp), %edx
  22129. + xorl %edi, %eax
  22130. + xorl %edi, %edx
  22131. + andl $0xfcfcfcfc, %eax
  22132. + andl $0xcfcfcfcf, %edx
  22133. + movb %al, %bl
  22134. + movb %ah, %cl
  22135. + rorl $4, %edx
  22136. + movl des_SPtrans(%ebx),%ebp
  22137. + movb %dl, %bl
  22138. + xorl %ebp, %esi
  22139. + movl 0x200+des_SPtrans(%ecx),%ebp
  22140. + xorl %ebp, %esi
  22141. + movb %dh, %cl
  22142. + shrl $16, %eax
  22143. + movl 0x100+des_SPtrans(%ebx),%ebp
  22144. + xorl %ebp, %esi
  22145. + movb %ah, %bl
  22146. + shrl $16, %edx
  22147. + movl 0x300+des_SPtrans(%ecx),%ebp
  22148. + xorl %ebp, %esi
  22149. + movl 24(%esp), %ebp
  22150. + movb %dh, %cl
  22151. + andl $0xff, %eax
  22152. + andl $0xff, %edx
  22153. + movl 0x600+des_SPtrans(%ebx),%ebx
  22154. + xorl %ebx, %esi
  22155. + movl 0x700+des_SPtrans(%ecx),%ebx
  22156. + xorl %ebx, %esi
  22157. + movl 0x400+des_SPtrans(%eax),%ebx
  22158. + xorl %ebx, %esi
  22159. + movl 0x500+des_SPtrans(%edx),%ebx
  22160. + xorl %ebx, %esi
  22161. +
  22162. +
  22163. + movl 40(%ebp), %eax
  22164. + xorl %ebx, %ebx
  22165. + movl 44(%ebp), %edx
  22166. + xorl %esi, %eax
  22167. + xorl %esi, %edx
  22168. + andl $0xfcfcfcfc, %eax
  22169. + andl $0xcfcfcfcf, %edx
  22170. + movb %al, %bl
  22171. + movb %ah, %cl
  22172. + rorl $4, %edx
  22173. + movl des_SPtrans(%ebx),%ebp
  22174. + movb %dl, %bl
  22175. + xorl %ebp, %edi
  22176. + movl 0x200+des_SPtrans(%ecx),%ebp
  22177. + xorl %ebp, %edi
  22178. + movb %dh, %cl
  22179. + shrl $16, %eax
  22180. + movl 0x100+des_SPtrans(%ebx),%ebp
  22181. + xorl %ebp, %edi
  22182. + movb %ah, %bl
  22183. + shrl $16, %edx
  22184. + movl 0x300+des_SPtrans(%ecx),%ebp
  22185. + xorl %ebp, %edi
  22186. + movl 24(%esp), %ebp
  22187. + movb %dh, %cl
  22188. + andl $0xff, %eax
  22189. + andl $0xff, %edx
  22190. + movl 0x600+des_SPtrans(%ebx),%ebx
  22191. + xorl %ebx, %edi
  22192. + movl 0x700+des_SPtrans(%ecx),%ebx
  22193. + xorl %ebx, %edi
  22194. + movl 0x400+des_SPtrans(%eax),%ebx
  22195. + xorl %ebx, %edi
  22196. + movl 0x500+des_SPtrans(%edx),%ebx
  22197. + xorl %ebx, %edi
  22198. +
  22199. +
  22200. + movl 32(%ebp), %eax
  22201. + xorl %ebx, %ebx
  22202. + movl 36(%ebp), %edx
  22203. + xorl %edi, %eax
  22204. + xorl %edi, %edx
  22205. + andl $0xfcfcfcfc, %eax
  22206. + andl $0xcfcfcfcf, %edx
  22207. + movb %al, %bl
  22208. + movb %ah, %cl
  22209. + rorl $4, %edx
  22210. + movl des_SPtrans(%ebx),%ebp
  22211. + movb %dl, %bl
  22212. + xorl %ebp, %esi
  22213. + movl 0x200+des_SPtrans(%ecx),%ebp
  22214. + xorl %ebp, %esi
  22215. + movb %dh, %cl
  22216. + shrl $16, %eax
  22217. + movl 0x100+des_SPtrans(%ebx),%ebp
  22218. + xorl %ebp, %esi
  22219. + movb %ah, %bl
  22220. + shrl $16, %edx
  22221. + movl 0x300+des_SPtrans(%ecx),%ebp
  22222. + xorl %ebp, %esi
  22223. + movl 24(%esp), %ebp
  22224. + movb %dh, %cl
  22225. + andl $0xff, %eax
  22226. + andl $0xff, %edx
  22227. + movl 0x600+des_SPtrans(%ebx),%ebx
  22228. + xorl %ebx, %esi
  22229. + movl 0x700+des_SPtrans(%ecx),%ebx
  22230. + xorl %ebx, %esi
  22231. + movl 0x400+des_SPtrans(%eax),%ebx
  22232. + xorl %ebx, %esi
  22233. + movl 0x500+des_SPtrans(%edx),%ebx
  22234. + xorl %ebx, %esi
  22235. +
  22236. +
  22237. + movl 24(%ebp), %eax
  22238. + xorl %ebx, %ebx
  22239. + movl 28(%ebp), %edx
  22240. + xorl %esi, %eax
  22241. + xorl %esi, %edx
  22242. + andl $0xfcfcfcfc, %eax
  22243. + andl $0xcfcfcfcf, %edx
  22244. + movb %al, %bl
  22245. + movb %ah, %cl
  22246. + rorl $4, %edx
  22247. + movl des_SPtrans(%ebx),%ebp
  22248. + movb %dl, %bl
  22249. + xorl %ebp, %edi
  22250. + movl 0x200+des_SPtrans(%ecx),%ebp
  22251. + xorl %ebp, %edi
  22252. + movb %dh, %cl
  22253. + shrl $16, %eax
  22254. + movl 0x100+des_SPtrans(%ebx),%ebp
  22255. + xorl %ebp, %edi
  22256. + movb %ah, %bl
  22257. + shrl $16, %edx
  22258. + movl 0x300+des_SPtrans(%ecx),%ebp
  22259. + xorl %ebp, %edi
  22260. + movl 24(%esp), %ebp
  22261. + movb %dh, %cl
  22262. + andl $0xff, %eax
  22263. + andl $0xff, %edx
  22264. + movl 0x600+des_SPtrans(%ebx),%ebx
  22265. + xorl %ebx, %edi
  22266. + movl 0x700+des_SPtrans(%ecx),%ebx
  22267. + xorl %ebx, %edi
  22268. + movl 0x400+des_SPtrans(%eax),%ebx
  22269. + xorl %ebx, %edi
  22270. + movl 0x500+des_SPtrans(%edx),%ebx
  22271. + xorl %ebx, %edi
  22272. +
  22273. +
  22274. + movl 16(%ebp), %eax
  22275. + xorl %ebx, %ebx
  22276. + movl 20(%ebp), %edx
  22277. + xorl %edi, %eax
  22278. + xorl %edi, %edx
  22279. + andl $0xfcfcfcfc, %eax
  22280. + andl $0xcfcfcfcf, %edx
  22281. + movb %al, %bl
  22282. + movb %ah, %cl
  22283. + rorl $4, %edx
  22284. + movl des_SPtrans(%ebx),%ebp
  22285. + movb %dl, %bl
  22286. + xorl %ebp, %esi
  22287. + movl 0x200+des_SPtrans(%ecx),%ebp
  22288. + xorl %ebp, %esi
  22289. + movb %dh, %cl
  22290. + shrl $16, %eax
  22291. + movl 0x100+des_SPtrans(%ebx),%ebp
  22292. + xorl %ebp, %esi
  22293. + movb %ah, %bl
  22294. + shrl $16, %edx
  22295. + movl 0x300+des_SPtrans(%ecx),%ebp
  22296. + xorl %ebp, %esi
  22297. + movl 24(%esp), %ebp
  22298. + movb %dh, %cl
  22299. + andl $0xff, %eax
  22300. + andl $0xff, %edx
  22301. + movl 0x600+des_SPtrans(%ebx),%ebx
  22302. + xorl %ebx, %esi
  22303. + movl 0x700+des_SPtrans(%ecx),%ebx
  22304. + xorl %ebx, %esi
  22305. + movl 0x400+des_SPtrans(%eax),%ebx
  22306. + xorl %ebx, %esi
  22307. + movl 0x500+des_SPtrans(%edx),%ebx
  22308. + xorl %ebx, %esi
  22309. +
  22310. +
  22311. + movl 8(%ebp), %eax
  22312. + xorl %ebx, %ebx
  22313. + movl 12(%ebp), %edx
  22314. + xorl %esi, %eax
  22315. + xorl %esi, %edx
  22316. + andl $0xfcfcfcfc, %eax
  22317. + andl $0xcfcfcfcf, %edx
  22318. + movb %al, %bl
  22319. + movb %ah, %cl
  22320. + rorl $4, %edx
  22321. + movl des_SPtrans(%ebx),%ebp
  22322. + movb %dl, %bl
  22323. + xorl %ebp, %edi
  22324. + movl 0x200+des_SPtrans(%ecx),%ebp
  22325. + xorl %ebp, %edi
  22326. + movb %dh, %cl
  22327. + shrl $16, %eax
  22328. + movl 0x100+des_SPtrans(%ebx),%ebp
  22329. + xorl %ebp, %edi
  22330. + movb %ah, %bl
  22331. + shrl $16, %edx
  22332. + movl 0x300+des_SPtrans(%ecx),%ebp
  22333. + xorl %ebp, %edi
  22334. + movl 24(%esp), %ebp
  22335. + movb %dh, %cl
  22336. + andl $0xff, %eax
  22337. + andl $0xff, %edx
  22338. + movl 0x600+des_SPtrans(%ebx),%ebx
  22339. + xorl %ebx, %edi
  22340. + movl 0x700+des_SPtrans(%ecx),%ebx
  22341. + xorl %ebx, %edi
  22342. + movl 0x400+des_SPtrans(%eax),%ebx
  22343. + xorl %ebx, %edi
  22344. + movl 0x500+des_SPtrans(%edx),%ebx
  22345. + xorl %ebx, %edi
  22346. +
  22347. +
  22348. + movl (%ebp), %eax
  22349. + xorl %ebx, %ebx
  22350. + movl 4(%ebp), %edx
  22351. + xorl %edi, %eax
  22352. + xorl %edi, %edx
  22353. + andl $0xfcfcfcfc, %eax
  22354. + andl $0xcfcfcfcf, %edx
  22355. + movb %al, %bl
  22356. + movb %ah, %cl
  22357. + rorl $4, %edx
  22358. + movl des_SPtrans(%ebx),%ebp
  22359. + movb %dl, %bl
  22360. + xorl %ebp, %esi
  22361. + movl 0x200+des_SPtrans(%ecx),%ebp
  22362. + xorl %ebp, %esi
  22363. + movb %dh, %cl
  22364. + shrl $16, %eax
  22365. + movl 0x100+des_SPtrans(%ebx),%ebp
  22366. + xorl %ebp, %esi
  22367. + movb %ah, %bl
  22368. + shrl $16, %edx
  22369. + movl 0x300+des_SPtrans(%ecx),%ebp
  22370. + xorl %ebp, %esi
  22371. + movl 24(%esp), %ebp
  22372. + movb %dh, %cl
  22373. + andl $0xff, %eax
  22374. + andl $0xff, %edx
  22375. + movl 0x600+des_SPtrans(%ebx),%ebx
  22376. + xorl %ebx, %esi
  22377. + movl 0x700+des_SPtrans(%ecx),%ebx
  22378. + xorl %ebx, %esi
  22379. + movl 0x400+des_SPtrans(%eax),%ebx
  22380. + xorl %ebx, %esi
  22381. + movl 0x500+des_SPtrans(%edx),%ebx
  22382. + xorl %ebx, %esi
  22383. +.L003end:
  22384. +
  22385. +
  22386. + rorl $3, %edi
  22387. + movl 20(%esp), %eax
  22388. + rorl $3, %esi
  22389. + movl %edi, (%eax)
  22390. + movl %esi, 4(%eax)
  22391. + popl %ebp
  22392. + popl %ebx
  22393. + popl %edi
  22394. + popl %esi
  22395. + ret
  22396. +.des_encrypt2_end:
  22397. + .size des_encrypt2 , .des_encrypt2_end-des_encrypt2
  22398. +.ident "desasm.pl"
  22399. +.text
  22400. + .align 16
  22401. +.globl des_encrypt3
  22402. + .type des_encrypt3 , @function
  22403. +des_encrypt3:
  22404. + pushl %ebx
  22405. + movl 8(%esp), %ebx
  22406. + pushl %ebp
  22407. + pushl %esi
  22408. + pushl %edi
  22409. +
  22410. +
  22411. + movl (%ebx), %edi
  22412. + movl 4(%ebx), %esi
  22413. + subl $12, %esp
  22414. +
  22415. +
  22416. + roll $4, %edi
  22417. + movl %edi, %edx
  22418. + xorl %esi, %edi
  22419. + andl $0xf0f0f0f0, %edi
  22420. + xorl %edi, %edx
  22421. + xorl %edi, %esi
  22422. +
  22423. + roll $20, %esi
  22424. + movl %esi, %edi
  22425. + xorl %edx, %esi
  22426. + andl $0xfff0000f, %esi
  22427. + xorl %esi, %edi
  22428. + xorl %esi, %edx
  22429. +
  22430. + roll $14, %edi
  22431. + movl %edi, %esi
  22432. + xorl %edx, %edi
  22433. + andl $0x33333333, %edi
  22434. + xorl %edi, %esi
  22435. + xorl %edi, %edx
  22436. +
  22437. + roll $22, %edx
  22438. + movl %edx, %edi
  22439. + xorl %esi, %edx
  22440. + andl $0x03fc03fc, %edx
  22441. + xorl %edx, %edi
  22442. + xorl %edx, %esi
  22443. +
  22444. + roll $9, %edi
  22445. + movl %edi, %edx
  22446. + xorl %esi, %edi
  22447. + andl $0xaaaaaaaa, %edi
  22448. + xorl %edi, %edx
  22449. + xorl %edi, %esi
  22450. +
  22451. + rorl $3, %edx
  22452. + rorl $2, %esi
  22453. + movl %esi, 4(%ebx)
  22454. + movl 36(%esp), %eax
  22455. + movl %edx, (%ebx)
  22456. + movl 40(%esp), %edi
  22457. + movl 44(%esp), %esi
  22458. + movl $1, 8(%esp)
  22459. + movl %eax, 4(%esp)
  22460. + movl %ebx, (%esp)
  22461. + call des_encrypt2
  22462. + movl $0, 8(%esp)
  22463. + movl %edi, 4(%esp)
  22464. + movl %ebx, (%esp)
  22465. + call des_encrypt2
  22466. + movl $1, 8(%esp)
  22467. + movl %esi, 4(%esp)
  22468. + movl %ebx, (%esp)
  22469. + call des_encrypt2
  22470. + addl $12, %esp
  22471. + movl (%ebx), %edi
  22472. + movl 4(%ebx), %esi
  22473. +
  22474. +
  22475. + roll $2, %esi
  22476. + roll $3, %edi
  22477. + movl %edi, %eax
  22478. + xorl %esi, %edi
  22479. + andl $0xaaaaaaaa, %edi
  22480. + xorl %edi, %eax
  22481. + xorl %edi, %esi
  22482. +
  22483. + roll $23, %eax
  22484. + movl %eax, %edi
  22485. + xorl %esi, %eax
  22486. + andl $0x03fc03fc, %eax
  22487. + xorl %eax, %edi
  22488. + xorl %eax, %esi
  22489. +
  22490. + roll $10, %edi
  22491. + movl %edi, %eax
  22492. + xorl %esi, %edi
  22493. + andl $0x33333333, %edi
  22494. + xorl %edi, %eax
  22495. + xorl %edi, %esi
  22496. +
  22497. + roll $18, %esi
  22498. + movl %esi, %edi
  22499. + xorl %eax, %esi
  22500. + andl $0xfff0000f, %esi
  22501. + xorl %esi, %edi
  22502. + xorl %esi, %eax
  22503. +
  22504. + roll $12, %edi
  22505. + movl %edi, %esi
  22506. + xorl %eax, %edi
  22507. + andl $0xf0f0f0f0, %edi
  22508. + xorl %edi, %esi
  22509. + xorl %edi, %eax
  22510. +
  22511. + rorl $4, %eax
  22512. + movl %eax, (%ebx)
  22513. + movl %esi, 4(%ebx)
  22514. + popl %edi
  22515. + popl %esi
  22516. + popl %ebp
  22517. + popl %ebx
  22518. + ret
  22519. +.des_encrypt3_end:
  22520. + .size des_encrypt3 , .des_encrypt3_end-des_encrypt3
  22521. +.ident "desasm.pl"
  22522. +.text
  22523. + .align 16
  22524. +.globl des_decrypt3
  22525. + .type des_decrypt3 , @function
  22526. +des_decrypt3:
  22527. + pushl %ebx
  22528. + movl 8(%esp), %ebx
  22529. + pushl %ebp
  22530. + pushl %esi
  22531. + pushl %edi
  22532. +
  22533. +
  22534. + movl (%ebx), %edi
  22535. + movl 4(%ebx), %esi
  22536. + subl $12, %esp
  22537. +
  22538. +
  22539. + roll $4, %edi
  22540. + movl %edi, %edx
  22541. + xorl %esi, %edi
  22542. + andl $0xf0f0f0f0, %edi
  22543. + xorl %edi, %edx
  22544. + xorl %edi, %esi
  22545. +
  22546. + roll $20, %esi
  22547. + movl %esi, %edi
  22548. + xorl %edx, %esi
  22549. + andl $0xfff0000f, %esi
  22550. + xorl %esi, %edi
  22551. + xorl %esi, %edx
  22552. +
  22553. + roll $14, %edi
  22554. + movl %edi, %esi
  22555. + xorl %edx, %edi
  22556. + andl $0x33333333, %edi
  22557. + xorl %edi, %esi
  22558. + xorl %edi, %edx
  22559. +
  22560. + roll $22, %edx
  22561. + movl %edx, %edi
  22562. + xorl %esi, %edx
  22563. + andl $0x03fc03fc, %edx
  22564. + xorl %edx, %edi
  22565. + xorl %edx, %esi
  22566. +
  22567. + roll $9, %edi
  22568. + movl %edi, %edx
  22569. + xorl %esi, %edi
  22570. + andl $0xaaaaaaaa, %edi
  22571. + xorl %edi, %edx
  22572. + xorl %edi, %esi
  22573. +
  22574. + rorl $3, %edx
  22575. + rorl $2, %esi
  22576. + movl %esi, 4(%ebx)
  22577. + movl 36(%esp), %esi
  22578. + movl %edx, (%ebx)
  22579. + movl 40(%esp), %edi
  22580. + movl 44(%esp), %eax
  22581. + movl $0, 8(%esp)
  22582. + movl %eax, 4(%esp)
  22583. + movl %ebx, (%esp)
  22584. + call des_encrypt2
  22585. + movl $1, 8(%esp)
  22586. + movl %edi, 4(%esp)
  22587. + movl %ebx, (%esp)
  22588. + call des_encrypt2
  22589. + movl $0, 8(%esp)
  22590. + movl %esi, 4(%esp)
  22591. + movl %ebx, (%esp)
  22592. + call des_encrypt2
  22593. + addl $12, %esp
  22594. + movl (%ebx), %edi
  22595. + movl 4(%ebx), %esi
  22596. +
  22597. +
  22598. + roll $2, %esi
  22599. + roll $3, %edi
  22600. + movl %edi, %eax
  22601. + xorl %esi, %edi
  22602. + andl $0xaaaaaaaa, %edi
  22603. + xorl %edi, %eax
  22604. + xorl %edi, %esi
  22605. +
  22606. + roll $23, %eax
  22607. + movl %eax, %edi
  22608. + xorl %esi, %eax
  22609. + andl $0x03fc03fc, %eax
  22610. + xorl %eax, %edi
  22611. + xorl %eax, %esi
  22612. +
  22613. + roll $10, %edi
  22614. + movl %edi, %eax
  22615. + xorl %esi, %edi
  22616. + andl $0x33333333, %edi
  22617. + xorl %edi, %eax
  22618. + xorl %edi, %esi
  22619. +
  22620. + roll $18, %esi
  22621. + movl %esi, %edi
  22622. + xorl %eax, %esi
  22623. + andl $0xfff0000f, %esi
  22624. + xorl %esi, %edi
  22625. + xorl %esi, %eax
  22626. +
  22627. + roll $12, %edi
  22628. + movl %edi, %esi
  22629. + xorl %eax, %edi
  22630. + andl $0xf0f0f0f0, %edi
  22631. + xorl %edi, %esi
  22632. + xorl %edi, %eax
  22633. +
  22634. + rorl $4, %eax
  22635. + movl %eax, (%ebx)
  22636. + movl %esi, 4(%ebx)
  22637. + popl %edi
  22638. + popl %esi
  22639. + popl %ebp
  22640. + popl %ebx
  22641. + ret
  22642. +.des_decrypt3_end:
  22643. + .size des_decrypt3 , .des_decrypt3_end-des_decrypt3
  22644. +.ident "desasm.pl"
  22645. +.text
  22646. + .align 16
  22647. +.globl des_ncbc_encrypt
  22648. + .type des_ncbc_encrypt , @function
  22649. +des_ncbc_encrypt:
  22650. +
  22651. + pushl %ebp
  22652. + pushl %ebx
  22653. + pushl %esi
  22654. + pushl %edi
  22655. + movl 28(%esp), %ebp
  22656. +
  22657. + movl 36(%esp), %ebx
  22658. + movl (%ebx), %esi
  22659. + movl 4(%ebx), %edi
  22660. + pushl %edi
  22661. + pushl %esi
  22662. + pushl %edi
  22663. + pushl %esi
  22664. + movl %esp, %ebx
  22665. + movl 36(%esp), %esi
  22666. + movl 40(%esp), %edi
  22667. +
  22668. + movl 56(%esp), %ecx
  22669. +
  22670. + pushl %ecx
  22671. +
  22672. + movl 52(%esp), %eax
  22673. + pushl %eax
  22674. + pushl %ebx
  22675. + cmpl $0, %ecx
  22676. + jz .L004decrypt
  22677. + andl $4294967288, %ebp
  22678. + movl 12(%esp), %eax
  22679. + movl 16(%esp), %ebx
  22680. + jz .L005encrypt_finish
  22681. +.L006encrypt_loop:
  22682. + movl (%esi), %ecx
  22683. + movl 4(%esi), %edx
  22684. + xorl %ecx, %eax
  22685. + xorl %edx, %ebx
  22686. + movl %eax, 12(%esp)
  22687. + movl %ebx, 16(%esp)
  22688. + call des_encrypt
  22689. + movl 12(%esp), %eax
  22690. + movl 16(%esp), %ebx
  22691. + movl %eax, (%edi)
  22692. + movl %ebx, 4(%edi)
  22693. + addl $8, %esi
  22694. + addl $8, %edi
  22695. + subl $8, %ebp
  22696. + jnz .L006encrypt_loop
  22697. +.L005encrypt_finish:
  22698. + movl 56(%esp), %ebp
  22699. + andl $7, %ebp
  22700. + jz .L007finish
  22701. + xorl %ecx, %ecx
  22702. + xorl %edx, %edx
  22703. + movl .L008cbc_enc_jmp_table(,%ebp,4),%ebp
  22704. + jmp *%ebp
  22705. +.L009ej7:
  22706. + movb 6(%esi), %dh
  22707. + sall $8, %edx
  22708. +.L010ej6:
  22709. + movb 5(%esi), %dh
  22710. +.L011ej5:
  22711. + movb 4(%esi), %dl
  22712. +.L012ej4:
  22713. + movl (%esi), %ecx
  22714. + jmp .L013ejend
  22715. +.L014ej3:
  22716. + movb 2(%esi), %ch
  22717. + sall $8, %ecx
  22718. +.L015ej2:
  22719. + movb 1(%esi), %ch
  22720. +.L016ej1:
  22721. + movb (%esi), %cl
  22722. +.L013ejend:
  22723. + xorl %ecx, %eax
  22724. + xorl %edx, %ebx
  22725. + movl %eax, 12(%esp)
  22726. + movl %ebx, 16(%esp)
  22727. + call des_encrypt
  22728. + movl 12(%esp), %eax
  22729. + movl 16(%esp), %ebx
  22730. + movl %eax, (%edi)
  22731. + movl %ebx, 4(%edi)
  22732. + jmp .L007finish
  22733. +.align 16
  22734. +.L004decrypt:
  22735. + andl $4294967288, %ebp
  22736. + movl 20(%esp), %eax
  22737. + movl 24(%esp), %ebx
  22738. + jz .L017decrypt_finish
  22739. +.L018decrypt_loop:
  22740. + movl (%esi), %eax
  22741. + movl 4(%esi), %ebx
  22742. + movl %eax, 12(%esp)
  22743. + movl %ebx, 16(%esp)
  22744. + call des_encrypt
  22745. + movl 12(%esp), %eax
  22746. + movl 16(%esp), %ebx
  22747. + movl 20(%esp), %ecx
  22748. + movl 24(%esp), %edx
  22749. + xorl %eax, %ecx
  22750. + xorl %ebx, %edx
  22751. + movl (%esi), %eax
  22752. + movl 4(%esi), %ebx
  22753. + movl %ecx, (%edi)
  22754. + movl %edx, 4(%edi)
  22755. + movl %eax, 20(%esp)
  22756. + movl %ebx, 24(%esp)
  22757. + addl $8, %esi
  22758. + addl $8, %edi
  22759. + subl $8, %ebp
  22760. + jnz .L018decrypt_loop
  22761. +.L017decrypt_finish:
  22762. + movl 56(%esp), %ebp
  22763. + andl $7, %ebp
  22764. + jz .L007finish
  22765. + movl (%esi), %eax
  22766. + movl 4(%esi), %ebx
  22767. + movl %eax, 12(%esp)
  22768. + movl %ebx, 16(%esp)
  22769. + call des_encrypt
  22770. + movl 12(%esp), %eax
  22771. + movl 16(%esp), %ebx
  22772. + movl 20(%esp), %ecx
  22773. + movl 24(%esp), %edx
  22774. + xorl %eax, %ecx
  22775. + xorl %ebx, %edx
  22776. + movl (%esi), %eax
  22777. + movl 4(%esi), %ebx
  22778. +.L019dj7:
  22779. + rorl $16, %edx
  22780. + movb %dl, 6(%edi)
  22781. + shrl $16, %edx
  22782. +.L020dj6:
  22783. + movb %dh, 5(%edi)
  22784. +.L021dj5:
  22785. + movb %dl, 4(%edi)
  22786. +.L022dj4:
  22787. + movl %ecx, (%edi)
  22788. + jmp .L023djend
  22789. +.L024dj3:
  22790. + rorl $16, %ecx
  22791. + movb %cl, 2(%edi)
  22792. + sall $16, %ecx
  22793. +.L025dj2:
  22794. + movb %ch, 1(%esi)
  22795. +.L026dj1:
  22796. + movb %cl, (%esi)
  22797. +.L023djend:
  22798. + jmp .L007finish
  22799. +.align 16
  22800. +.L007finish:
  22801. + movl 64(%esp), %ecx
  22802. + addl $28, %esp
  22803. + movl %eax, (%ecx)
  22804. + movl %ebx, 4(%ecx)
  22805. + popl %edi
  22806. + popl %esi
  22807. + popl %ebx
  22808. + popl %ebp
  22809. + ret
  22810. +.align 16
  22811. +.L008cbc_enc_jmp_table:
  22812. + .long 0
  22813. + .long .L016ej1
  22814. + .long .L015ej2
  22815. + .long .L014ej3
  22816. + .long .L012ej4
  22817. + .long .L011ej5
  22818. + .long .L010ej6
  22819. + .long .L009ej7
  22820. +.align 16
  22821. +.L027cbc_dec_jmp_table:
  22822. + .long 0
  22823. + .long .L026dj1
  22824. + .long .L025dj2
  22825. + .long .L024dj3
  22826. + .long .L022dj4
  22827. + .long .L021dj5
  22828. + .long .L020dj6
  22829. + .long .L019dj7
  22830. +.des_ncbc_encrypt_end:
  22831. + .size des_ncbc_encrypt , .des_ncbc_encrypt_end-des_ncbc_encrypt
  22832. +.ident "desasm.pl"
  22833. +.text
  22834. + .align 16
  22835. +.globl des_ede3_cbc_encrypt
  22836. + .type des_ede3_cbc_encrypt , @function
  22837. +des_ede3_cbc_encrypt:
  22838. +
  22839. + pushl %ebp
  22840. + pushl %ebx
  22841. + pushl %esi
  22842. + pushl %edi
  22843. + movl 28(%esp), %ebp
  22844. +
  22845. + movl 44(%esp), %ebx
  22846. + movl (%ebx), %esi
  22847. + movl 4(%ebx), %edi
  22848. + pushl %edi
  22849. + pushl %esi
  22850. + pushl %edi
  22851. + pushl %esi
  22852. + movl %esp, %ebx
  22853. + movl 36(%esp), %esi
  22854. + movl 40(%esp), %edi
  22855. +
  22856. + movl 64(%esp), %ecx
  22857. +
  22858. + movl 56(%esp), %eax
  22859. + pushl %eax
  22860. +
  22861. + movl 56(%esp), %eax
  22862. + pushl %eax
  22863. +
  22864. + movl 56(%esp), %eax
  22865. + pushl %eax
  22866. + pushl %ebx
  22867. + cmpl $0, %ecx
  22868. + jz .L028decrypt
  22869. + andl $4294967288, %ebp
  22870. + movl 16(%esp), %eax
  22871. + movl 20(%esp), %ebx
  22872. + jz .L029encrypt_finish
  22873. +.L030encrypt_loop:
  22874. + movl (%esi), %ecx
  22875. + movl 4(%esi), %edx
  22876. + xorl %ecx, %eax
  22877. + xorl %edx, %ebx
  22878. + movl %eax, 16(%esp)
  22879. + movl %ebx, 20(%esp)
  22880. + call des_encrypt3
  22881. + movl 16(%esp), %eax
  22882. + movl 20(%esp), %ebx
  22883. + movl %eax, (%edi)
  22884. + movl %ebx, 4(%edi)
  22885. + addl $8, %esi
  22886. + addl $8, %edi
  22887. + subl $8, %ebp
  22888. + jnz .L030encrypt_loop
  22889. +.L029encrypt_finish:
  22890. + movl 60(%esp), %ebp
  22891. + andl $7, %ebp
  22892. + jz .L031finish
  22893. + xorl %ecx, %ecx
  22894. + xorl %edx, %edx
  22895. + movl .L032cbc_enc_jmp_table(,%ebp,4),%ebp
  22896. + jmp *%ebp
  22897. +.L033ej7:
  22898. + movb 6(%esi), %dh
  22899. + sall $8, %edx
  22900. +.L034ej6:
  22901. + movb 5(%esi), %dh
  22902. +.L035ej5:
  22903. + movb 4(%esi), %dl
  22904. +.L036ej4:
  22905. + movl (%esi), %ecx
  22906. + jmp .L037ejend
  22907. +.L038ej3:
  22908. + movb 2(%esi), %ch
  22909. + sall $8, %ecx
  22910. +.L039ej2:
  22911. + movb 1(%esi), %ch
  22912. +.L040ej1:
  22913. + movb (%esi), %cl
  22914. +.L037ejend:
  22915. + xorl %ecx, %eax
  22916. + xorl %edx, %ebx
  22917. + movl %eax, 16(%esp)
  22918. + movl %ebx, 20(%esp)
  22919. + call des_encrypt3
  22920. + movl 16(%esp), %eax
  22921. + movl 20(%esp), %ebx
  22922. + movl %eax, (%edi)
  22923. + movl %ebx, 4(%edi)
  22924. + jmp .L031finish
  22925. +.align 16
  22926. +.L028decrypt:
  22927. + andl $4294967288, %ebp
  22928. + movl 24(%esp), %eax
  22929. + movl 28(%esp), %ebx
  22930. + jz .L041decrypt_finish
  22931. +.L042decrypt_loop:
  22932. + movl (%esi), %eax
  22933. + movl 4(%esi), %ebx
  22934. + movl %eax, 16(%esp)
  22935. + movl %ebx, 20(%esp)
  22936. + call des_decrypt3
  22937. + movl 16(%esp), %eax
  22938. + movl 20(%esp), %ebx
  22939. + movl 24(%esp), %ecx
  22940. + movl 28(%esp), %edx
  22941. + xorl %eax, %ecx
  22942. + xorl %ebx, %edx
  22943. + movl (%esi), %eax
  22944. + movl 4(%esi), %ebx
  22945. + movl %ecx, (%edi)
  22946. + movl %edx, 4(%edi)
  22947. + movl %eax, 24(%esp)
  22948. + movl %ebx, 28(%esp)
  22949. + addl $8, %esi
  22950. + addl $8, %edi
  22951. + subl $8, %ebp
  22952. + jnz .L042decrypt_loop
  22953. +.L041decrypt_finish:
  22954. + movl 60(%esp), %ebp
  22955. + andl $7, %ebp
  22956. + jz .L031finish
  22957. + movl (%esi), %eax
  22958. + movl 4(%esi), %ebx
  22959. + movl %eax, 16(%esp)
  22960. + movl %ebx, 20(%esp)
  22961. + call des_decrypt3
  22962. + movl 16(%esp), %eax
  22963. + movl 20(%esp), %ebx
  22964. + movl 24(%esp), %ecx
  22965. + movl 28(%esp), %edx
  22966. + xorl %eax, %ecx
  22967. + xorl %ebx, %edx
  22968. + movl (%esi), %eax
  22969. + movl 4(%esi), %ebx
  22970. +.L043dj7:
  22971. + rorl $16, %edx
  22972. + movb %dl, 6(%edi)
  22973. + shrl $16, %edx
  22974. +.L044dj6:
  22975. + movb %dh, 5(%edi)
  22976. +.L045dj5:
  22977. + movb %dl, 4(%edi)
  22978. +.L046dj4:
  22979. + movl %ecx, (%edi)
  22980. + jmp .L047djend
  22981. +.L048dj3:
  22982. + rorl $16, %ecx
  22983. + movb %cl, 2(%edi)
  22984. + sall $16, %ecx
  22985. +.L049dj2:
  22986. + movb %ch, 1(%esi)
  22987. +.L050dj1:
  22988. + movb %cl, (%esi)
  22989. +.L047djend:
  22990. + jmp .L031finish
  22991. +.align 16
  22992. +.L031finish:
  22993. + movl 76(%esp), %ecx
  22994. + addl $32, %esp
  22995. + movl %eax, (%ecx)
  22996. + movl %ebx, 4(%ecx)
  22997. + popl %edi
  22998. + popl %esi
  22999. + popl %ebx
  23000. + popl %ebp
  23001. + ret
  23002. +.align 16
  23003. +.L032cbc_enc_jmp_table:
  23004. + .long 0
  23005. + .long .L040ej1
  23006. + .long .L039ej2
  23007. + .long .L038ej3
  23008. + .long .L036ej4
  23009. + .long .L035ej5
  23010. + .long .L034ej6
  23011. + .long .L033ej7
  23012. +.align 16
  23013. +.L051cbc_dec_jmp_table:
  23014. + .long 0
  23015. + .long .L050dj1
  23016. + .long .L049dj2
  23017. + .long .L048dj3
  23018. + .long .L046dj4
  23019. + .long .L045dj5
  23020. + .long .L044dj6
  23021. + .long .L043dj7
  23022. +.des_ede3_cbc_encrypt_end:
  23023. + .size des_ede3_cbc_encrypt , .des_ede3_cbc_encrypt_end-des_ede3_cbc_encrypt
  23024. +.ident "desasm.pl"
  23025. --- /dev/null Tue Mar 11 13:02:56 2003
  23026. +++ linux/net/ipsec/des/ecb_enc.c Mon Feb 9 13:51:03 2004
  23027. @@ -0,0 +1,128 @@
  23028. +/* crypto/des/ecb_enc.c */
  23029. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  23030. + * All rights reserved.
  23031. + *
  23032. + * This package is an SSL implementation written
  23033. + * by Eric Young (eay@cryptsoft.com).
  23034. + * The implementation was written so as to conform with Netscapes SSL.
  23035. + *
  23036. + * This library is free for commercial and non-commercial use as long as
  23037. + * the following conditions are aheared to. The following conditions
  23038. + * apply to all code found in this distribution, be it the RC4, RSA,
  23039. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  23040. + * included with this distribution is covered by the same copyright terms
  23041. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  23042. + *
  23043. + * Copyright remains Eric Young's, and as such any Copyright notices in
  23044. + * the code are not to be removed.
  23045. + * If this package is used in a product, Eric Young should be given attribution
  23046. + * as the author of the parts of the library used.
  23047. + * This can be in the form of a textual message at program startup or
  23048. + * in documentation (online or textual) provided with the package.
  23049. + *
  23050. + * Redistribution and use in source and binary forms, with or without
  23051. + * modification, are permitted provided that the following conditions
  23052. + * are met:
  23053. + * 1. Redistributions of source code must retain the copyright
  23054. + * notice, this list of conditions and the following disclaimer.
  23055. + * 2. Redistributions in binary form must reproduce the above copyright
  23056. + * notice, this list of conditions and the following disclaimer in the
  23057. + * documentation and/or other materials provided with the distribution.
  23058. + * 3. All advertising materials mentioning features or use of this software
  23059. + * must display the following acknowledgement:
  23060. + * "This product includes cryptographic software written by
  23061. + * Eric Young (eay@cryptsoft.com)"
  23062. + * The word 'cryptographic' can be left out if the rouines from the library
  23063. + * being used are not cryptographic related :-).
  23064. + * 4. If you include any Windows specific code (or a derivative thereof) from
  23065. + * the apps directory (application code) you must include an acknowledgement:
  23066. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  23067. + *
  23068. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  23069. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23070. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23071. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23072. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23073. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23074. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23075. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23076. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23077. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23078. + * SUCH DAMAGE.
  23079. + *
  23080. + * The licence and distribution terms for any publically available version or
  23081. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  23082. + * copied and put under another distribution licence
  23083. + * [including the GNU Public Licence.]
  23084. + */
  23085. +
  23086. +#include "des/des_locl.h"
  23087. +#include "des/spr.h"
  23088. +
  23089. +char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay";
  23090. +char *DES_version="DES part of SSLeay 0.8.2b 08-Jan-1998";
  23091. +
  23092. +/* RCSID $Id: ecb_enc.c,v 1.8 2004/08/04 15:57:22 mcr Exp $ */
  23093. +/* This function ifdef'ed out for FreeS/WAN project. */
  23094. +#ifdef notdef
  23095. +char *des_options()
  23096. + {
  23097. + static int init=1;
  23098. + static char buf[32];
  23099. +
  23100. + if (init)
  23101. + {
  23102. + char *ptr,*unroll,*risc,*size;
  23103. +
  23104. + init=0;
  23105. +#ifdef DES_PTR
  23106. + ptr="ptr";
  23107. +#else
  23108. + ptr="idx";
  23109. +#endif
  23110. +#if defined(DES_RISC1) || defined(DES_RISC2)
  23111. +#ifdef DES_RISC1
  23112. + risc="risc1";
  23113. +#endif
  23114. +#ifdef DES_RISC2
  23115. + risc="risc2";
  23116. +#endif
  23117. +#else
  23118. + risc="cisc";
  23119. +#endif
  23120. +#ifdef DES_UNROLL
  23121. + unroll="16";
  23122. +#else
  23123. + unroll="4";
  23124. +#endif
  23125. + if (sizeof(DES_LONG) != sizeof(long))
  23126. + size="int";
  23127. + else
  23128. + size="long";
  23129. + sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
  23130. + }
  23131. + return(buf);
  23132. + }
  23133. +#endif
  23134. +
  23135. +
  23136. +void des_ecb_encrypt(input, output, ks, enc)
  23137. +des_cblock (*input);
  23138. +des_cblock (*output);
  23139. +des_key_schedule ks;
  23140. +int enc;
  23141. + {
  23142. + register DES_LONG l;
  23143. + register unsigned char *in,*out;
  23144. + DES_LONG ll[2];
  23145. +
  23146. + in=(unsigned char *)input;
  23147. + out=(unsigned char *)output;
  23148. + c2l(in,l); ll[0]=l;
  23149. + c2l(in,l); ll[1]=l;
  23150. + des_encrypt(ll,ks,enc);
  23151. + l=ll[0]; l2c(l,out);
  23152. + l=ll[1]; l2c(l,out);
  23153. + l=ll[0]=ll[1]=0;
  23154. + }
  23155. +
  23156. --- /dev/null Tue Mar 11 13:02:56 2003
  23157. +++ linux/net/ipsec/des/ipsec_alg_3des.c Mon Feb 9 13:51:03 2004
  23158. @@ -0,0 +1,181 @@
  23159. +/*
  23160. + * ipsec_alg 3DES cipher stubs
  23161. + *
  23162. + * Copyright (C) 2005 Michael Richardson <mcr@xelerance.com>
  23163. + *
  23164. + * Adapted from ipsec_alg_aes.c by JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  23165. + *
  23166. + * ipsec_alg_aes.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  23167. + *
  23168. + * This program is free software; you can redistribute it and/or modify it
  23169. + * under the terms of the GNU General Public License as published by the
  23170. + * Free Software Foundation; either version 2 of the License, or (at your
  23171. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  23172. + *
  23173. + * This program is distributed in the hope that it will be useful, but
  23174. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23175. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23176. + * for more details.
  23177. + *
  23178. + */
  23179. +#ifndef AUTOCONF_INCLUDED
  23180. +#include <linux/config.h>
  23181. +#endif
  23182. +#include <linux/version.h>
  23183. +
  23184. +/*
  23185. + * special case: ipsec core modular with this static algo inside:
  23186. + * must avoid MODULE magic for this file
  23187. + */
  23188. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_3DES)
  23189. +#undef MODULE
  23190. +#endif
  23191. +
  23192. +#include <linux/module.h>
  23193. +#include <linux/init.h>
  23194. +
  23195. +#include <linux/kernel.h> /* printk() */
  23196. +#include <linux/errno.h> /* error codes */
  23197. +#include <linux/types.h> /* size_t */
  23198. +#include <linux/string.h>
  23199. +
  23200. +/* Low freeswan header coupling */
  23201. +#include "openswan/ipsec_xform.h"
  23202. +#include "openswan/ipsec_alg.h"
  23203. +#include "crypto/des.h"
  23204. +#include "openswan/ipsec_alg_3des.h"
  23205. +
  23206. +#define AES_CONTEXT_T aes_context
  23207. +static int debug_3des=0;
  23208. +static int test_3des=0;
  23209. +static int excl_3des=0;
  23210. +
  23211. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23212. +MODULE_AUTHOR("Michael Richardson <mcr@xelerance.com>");
  23213. +#ifdef module_param
  23214. +module_param(debug_3des,int,0600)
  23215. +module_param(test_des,int,0600)
  23216. +module_param(excl_des,int,0600)
  23217. +#else
  23218. +MODULE_PARM(debug_3des, "i");
  23219. +MODULE_PARM(test_des, "i");
  23220. +MODULE_PARM(excl_des, "i");
  23221. +#endif
  23222. +#endif
  23223. +
  23224. +#define ESP_AES_MAC_KEY_SZ 16 /* 128 bit MAC key */
  23225. +#define ESP_AES_MAC_BLK_LEN 16 /* 128 bit block */
  23226. +
  23227. +static int _3des_set_key(struct ipsec_alg_enc *alg,
  23228. + __u8 * key_e, const __u8 * key,
  23229. + size_t keysize)
  23230. +{
  23231. + int ret = 0;
  23232. + TripleDES_context *ctx = (TripleDES_context*)key_e;
  23233. +
  23234. + if(keysize != 192/8) {
  23235. + return EINVAL;
  23236. + }
  23237. +
  23238. + des_set_key((des_cblock *)(key + DES_KEY_SZ*0), ctx->s1);
  23239. + des_set_key((des_cblock *)(key + DES_KEY_SZ*1), ctx->s2);
  23240. + des_set_key((des_cblock *)(key + DES_KEY_SZ*2), ctx->s3);
  23241. +
  23242. + if (debug_3des > 0)
  23243. + printk(KERN_DEBUG "klips_debug:_3des_set_key:"
  23244. + "ret=%d key_e=%p key=%p keysize=%ld\n",
  23245. + ret, key_e, key, (unsigned long int) keysize);
  23246. + return ret;
  23247. +}
  23248. +
  23249. +static int _3des_cbc_encrypt(struct ipsec_alg_enc *alg,
  23250. + __u8 * key_e,
  23251. + __u8 * in,
  23252. + int ilen, const __u8 * iv,
  23253. + int encrypt)
  23254. +{
  23255. + TripleDES_context *ctx=(TripleDES_context*)key_e;
  23256. + des_cblock miv;
  23257. +
  23258. + memcpy(&miv, iv, sizeof(miv));
  23259. +
  23260. + if (debug_3des > 0)
  23261. + printk(KERN_DEBUG "klips_debug:_aes_cbc_encrypt:"
  23262. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  23263. + key_e, in, ilen, iv, encrypt);
  23264. +
  23265. + des_ede3_cbc_encrypt((des_cblock *)in,
  23266. + (des_cblock *)in,
  23267. + ilen,
  23268. + ctx->s1,
  23269. + ctx->s2,
  23270. + ctx->s3,
  23271. + &miv, encrypt);
  23272. + return 1;
  23273. +}
  23274. +
  23275. +static struct ipsec_alg_enc ipsec_alg_3DES = {
  23276. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  23277. + ixt_refcnt: ATOMIC_INIT(0),
  23278. + ixt_name: "3des",
  23279. + ixt_blocksize: ESP_3DES_CBC_BLK_LEN,
  23280. + ixt_support: {
  23281. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  23282. + ias_id: ESP_3DES,
  23283. + ias_keyminbits: ESP_3DES_KEY_SZ*8,
  23284. + ias_keymaxbits: ESP_3DES_KEY_SZ*8,
  23285. + },
  23286. + },
  23287. +#if defined(MODULE_KLIPS_ENC_3DES_MODULE)
  23288. + ixt_module: THIS_MODULE,
  23289. +#endif
  23290. + ixt_e_keylen: ESP_3DES_KEY_SZ*8,
  23291. + ixt_e_ctx_size: sizeof(TripleDES_context),
  23292. + ixt_e_set_key: _3des_set_key,
  23293. + ixt_e_cbc_encrypt:_3des_cbc_encrypt,
  23294. +};
  23295. +
  23296. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23297. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_3des_init )
  23298. +#else
  23299. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_3des_init )
  23300. +#endif
  23301. +{
  23302. + int ret, test_ret;
  23303. +
  23304. + if (excl_3des) ipsec_alg_3DES.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  23305. + ret=register_ipsec_alg_enc(&ipsec_alg_3DES);
  23306. + printk("ipsec_3des_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  23307. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23308. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23309. + ipsec_alg_3DES.ixt_common.ixt_name,
  23310. + ret);
  23311. + if (ret==0 && test_3des) {
  23312. + test_ret=ipsec_alg_test(
  23313. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23314. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23315. + test_3des);
  23316. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  23317. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23318. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23319. + test_ret);
  23320. + }
  23321. + return ret;
  23322. +}
  23323. +
  23324. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23325. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_3des_fini )
  23326. +#else
  23327. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_3des_fini )
  23328. +#endif
  23329. +{
  23330. + unregister_ipsec_alg_enc(&ipsec_alg_3DES);
  23331. + return;
  23332. +}
  23333. +
  23334. +/* Dual, because 3des code is 4-clause BSD licensed */
  23335. +#ifdef MODULE_LICENSE
  23336. +MODULE_LICENSE("Dual BSD/GPL");
  23337. +#endif
  23338. +
  23339. +
  23340. --- /dev/null Tue Mar 11 13:02:56 2003
  23341. +++ linux/net/ipsec/des/set_key.c Mon Feb 9 13:51:03 2004
  23342. @@ -0,0 +1,246 @@
  23343. +/* crypto/des/set_key.c */
  23344. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  23345. + * All rights reserved.
  23346. + *
  23347. + * This package is an SSL implementation written
  23348. + * by Eric Young (eay@cryptsoft.com).
  23349. + * The implementation was written so as to conform with Netscapes SSL.
  23350. + *
  23351. + * This library is free for commercial and non-commercial use as long as
  23352. + * the following conditions are aheared to. The following conditions
  23353. + * apply to all code found in this distribution, be it the RC4, RSA,
  23354. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  23355. + * included with this distribution is covered by the same copyright terms
  23356. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  23357. + *
  23358. + * Copyright remains Eric Young's, and as such any Copyright notices in
  23359. + * the code are not to be removed.
  23360. + * If this package is used in a product, Eric Young should be given attribution
  23361. + * as the author of the parts of the library used.
  23362. + * This can be in the form of a textual message at program startup or
  23363. + * in documentation (online or textual) provided with the package.
  23364. + *
  23365. + * Redistribution and use in source and binary forms, with or without
  23366. + * modification, are permitted provided that the following conditions
  23367. + * are met:
  23368. + * 1. Redistributions of source code must retain the copyright
  23369. + * notice, this list of conditions and the following disclaimer.
  23370. + * 2. Redistributions in binary form must reproduce the above copyright
  23371. + * notice, this list of conditions and the following disclaimer in the
  23372. + * documentation and/or other materials provided with the distribution.
  23373. + * 3. All advertising materials mentioning features or use of this software
  23374. + * must display the following acknowledgement:
  23375. + * "This product includes cryptographic software written by
  23376. + * Eric Young (eay@cryptsoft.com)"
  23377. + * The word 'cryptographic' can be left out if the rouines from the library
  23378. + * being used are not cryptographic related :-).
  23379. + * 4. If you include any Windows specific code (or a derivative thereof) from
  23380. + * the apps directory (application code) you must include an acknowledgement:
  23381. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  23382. + *
  23383. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  23384. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23385. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23386. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23387. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23388. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23389. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23390. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23391. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23392. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23393. + * SUCH DAMAGE.
  23394. + *
  23395. + * The licence and distribution terms for any publically available version or
  23396. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  23397. + * copied and put under another distribution licence
  23398. + * [including the GNU Public Licence.]
  23399. + */
  23400. +
  23401. +/* set_key.c v 1.4 eay 24/9/91
  23402. + * 1.4 Speed up by 400% :-)
  23403. + * 1.3 added register declarations.
  23404. + * 1.2 unrolled make_key_sched a bit more
  23405. + * 1.1 added norm_expand_bits
  23406. + * 1.0 First working version
  23407. + */
  23408. +#include "des/des_locl.h"
  23409. +#include "des/podd.h"
  23410. +#include "des/sk.h"
  23411. +
  23412. +#ifndef NOPROTO
  23413. +static int check_parity(des_cblock (*key));
  23414. +#else
  23415. +static int check_parity();
  23416. +#endif
  23417. +
  23418. +int des_check_key=0;
  23419. +
  23420. +void des_set_odd_parity(key)
  23421. +des_cblock (*key);
  23422. + {
  23423. + int i;
  23424. +
  23425. + for (i=0; i<DES_KEY_SZ; i++)
  23426. + (*key)[i]=odd_parity[(*key)[i]];
  23427. + }
  23428. +
  23429. +static int check_parity(key)
  23430. +des_cblock (*key);
  23431. + {
  23432. + int i;
  23433. +
  23434. + for (i=0; i<DES_KEY_SZ; i++)
  23435. + {
  23436. + if ((*key)[i] != odd_parity[(*key)[i]])
  23437. + return(0);
  23438. + }
  23439. + return(1);
  23440. + }
  23441. +
  23442. +/* Weak and semi week keys as take from
  23443. + * %A D.W. Davies
  23444. + * %A W.L. Price
  23445. + * %T Security for Computer Networks
  23446. + * %I John Wiley & Sons
  23447. + * %D 1984
  23448. + * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
  23449. + * (and actual cblock values).
  23450. + */
  23451. +#define NUM_WEAK_KEY 16
  23452. +static des_cblock weak_keys[NUM_WEAK_KEY]={
  23453. + /* weak keys */
  23454. + {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  23455. + {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
  23456. + {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
  23457. + {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
  23458. + /* semi-weak keys */
  23459. + {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
  23460. + {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
  23461. + {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
  23462. + {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
  23463. + {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
  23464. + {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
  23465. + {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
  23466. + {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
  23467. + {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
  23468. + {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
  23469. + {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  23470. + {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
  23471. +
  23472. +int des_is_weak_key(key)
  23473. +des_cblock (*key);
  23474. + {
  23475. + int i;
  23476. +
  23477. + for (i=0; i<NUM_WEAK_KEY; i++)
  23478. + /* Added == 0 to comparision, I obviously don't run
  23479. + * this section very often :-(, thanks to
  23480. + * engineering@MorningStar.Com for the fix
  23481. + * eay 93/06/29
  23482. + * Another problem, I was comparing only the first 4
  23483. + * bytes, 97/03/18 */
  23484. + if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
  23485. + return(0);
  23486. + }
  23487. +
  23488. +/* NOW DEFINED IN des_local.h
  23489. + * See ecb_encrypt.c for a pseudo description of these macros.
  23490. + * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  23491. + * (b)^=(t),\
  23492. + * (a)=((a)^((t)<<(n))))
  23493. + */
  23494. +
  23495. +#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
  23496. + (a)=(a)^(t)^(t>>(16-(n))))
  23497. +
  23498. +/* return 0 if key parity is odd (correct),
  23499. + * return -1 if key parity error,
  23500. + * return -2 if illegal weak key.
  23501. + */
  23502. +int des_set_key(key, schedule)
  23503. +des_cblock (*key);
  23504. +des_key_schedule schedule;
  23505. + {
  23506. + static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
  23507. + register DES_LONG c,d,t,s,t2;
  23508. + register unsigned char *in;
  23509. + register DES_LONG *k;
  23510. + register int i;
  23511. +
  23512. + if (des_check_key)
  23513. + {
  23514. + if (!check_parity(key))
  23515. + return(-1);
  23516. +
  23517. + if (des_is_weak_key(key))
  23518. + return(-2);
  23519. + }
  23520. +
  23521. + k=(DES_LONG *)schedule;
  23522. + in=(unsigned char *)key;
  23523. +
  23524. + c2l(in,c);
  23525. + c2l(in,d);
  23526. +
  23527. + /* do PC1 in 60 simple operations */
  23528. +/* PERM_OP(d,c,t,4,0x0f0f0f0fL);
  23529. + HPERM_OP(c,t,-2, 0xcccc0000L);
  23530. + HPERM_OP(c,t,-1, 0xaaaa0000L);
  23531. + HPERM_OP(c,t, 8, 0x00ff0000L);
  23532. + HPERM_OP(c,t,-1, 0xaaaa0000L);
  23533. + HPERM_OP(d,t,-8, 0xff000000L);
  23534. + HPERM_OP(d,t, 8, 0x00ff0000L);
  23535. + HPERM_OP(d,t, 2, 0x33330000L);
  23536. + d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
  23537. + d=(d>>8)|((c&0xf0000000L)>>4);
  23538. + c&=0x0fffffffL; */
  23539. +
  23540. + /* I now do it in 47 simple operations :-)
  23541. + * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  23542. + * for the inspiration. :-) */
  23543. + PERM_OP (d,c,t,4,0x0f0f0f0fL);
  23544. + HPERM_OP(c,t,-2,0xcccc0000L);
  23545. + HPERM_OP(d,t,-2,0xcccc0000L);
  23546. + PERM_OP (d,c,t,1,0x55555555L);
  23547. + PERM_OP (c,d,t,8,0x00ff00ffL);
  23548. + PERM_OP (d,c,t,1,0x55555555L);
  23549. + d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
  23550. + ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
  23551. + c&=0x0fffffffL;
  23552. +
  23553. + for (i=0; i<ITERATIONS; i++)
  23554. + {
  23555. + if (shifts2[i])
  23556. + { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
  23557. + else
  23558. + { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
  23559. + c&=0x0fffffffL;
  23560. + d&=0x0fffffffL;
  23561. + /* could be a few less shifts but I am to lazy at this
  23562. + * point in time to investigate */
  23563. + s= des_skb[0][ (c )&0x3f ]|
  23564. + des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
  23565. + des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
  23566. + des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
  23567. + ((c>>22L)&0x38)];
  23568. + t= des_skb[4][ (d )&0x3f ]|
  23569. + des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
  23570. + des_skb[6][ (d>>15L)&0x3f ]|
  23571. + des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
  23572. +
  23573. + /* table contained 0213 4657 */
  23574. + t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
  23575. + *(k++)=ROTATE(t2,30)&0xffffffffL;
  23576. +
  23577. + t2=((s>>16L)|(t&0xffff0000L));
  23578. + *(k++)=ROTATE(t2,26)&0xffffffffL;
  23579. + }
  23580. + return(0);
  23581. + }
  23582. +
  23583. +int des_key_sched(key, schedule)
  23584. +des_cblock (*key);
  23585. +des_key_schedule schedule;
  23586. + {
  23587. + return(des_set_key(key,schedule));
  23588. + }
  23589. --- /dev/null Tue Mar 11 13:02:56 2003
  23590. +++ linux/net/ipsec/goodmask.c Mon Feb 9 13:51:03 2004
  23591. @@ -0,0 +1,100 @@
  23592. +/*
  23593. + * minor utilities for subnet-mask manipulation
  23594. + * Copyright (C) 1998, 1999 Henry Spencer.
  23595. + *
  23596. + * This library is free software; you can redistribute it and/or modify it
  23597. + * under the terms of the GNU Library General Public License as published by
  23598. + * the Free Software Foundation; either version 2 of the License, or (at your
  23599. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  23600. + *
  23601. + * This library is distributed in the hope that it will be useful, but
  23602. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23603. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  23604. + * License for more details.
  23605. + *
  23606. + * RCSID $Id: goodmask.c,v 1.12 2004/07/10 07:43:47 mcr Exp $
  23607. + */
  23608. +#include "openswan.h"
  23609. +
  23610. +#ifndef ABITS
  23611. +#define ABITS 32 /* bits in an IPv4 address */
  23612. +#endif
  23613. +
  23614. +/*
  23615. + - goodmask - is this a good (^1*0*$) subnet mask?
  23616. + * You are not expected to understand this. See Henry S. Warren Jr,
  23617. + * "Functions realizable with word-parallel logical and two's-complement
  23618. + * addition instructions", CACM 20.6 (June 1977), p.439.
  23619. + */
  23620. +int /* predicate */
  23621. +goodmask(mask)
  23622. +struct in_addr mask;
  23623. +{
  23624. + unsigned long x = ntohl(mask.s_addr);
  23625. + /* clear rightmost contiguous string of 1-bits */
  23626. +# define CRCS1B(x) (((x|(x-1))+1)&x)
  23627. +# define TOPBIT (1UL << 31)
  23628. +
  23629. + /* either zero, or has one string of 1-bits which is left-justified */
  23630. + if (x == 0 || (CRCS1B(x) == 0 && (x&TOPBIT)))
  23631. + return 1;
  23632. + return 0;
  23633. +}
  23634. +
  23635. +/*
  23636. + - masktobits - how many bits in this mask?
  23637. + * The algorithm is essentially a binary search, but highly optimized
  23638. + * for this particular task.
  23639. + */
  23640. +int /* -1 means !goodmask() */
  23641. +masktobits(mask)
  23642. +struct in_addr mask;
  23643. +{
  23644. + unsigned long m = ntohl(mask.s_addr);
  23645. + int masklen;
  23646. +
  23647. + if (!goodmask(mask))
  23648. + return -1;
  23649. +
  23650. + if (m&0x00000001UL)
  23651. + return 32;
  23652. + masklen = 0;
  23653. + if (m&(0x0000ffffUL<<1)) { /* <<1 for 1-origin numbering */
  23654. + masklen |= 0x10;
  23655. + m <<= 16;
  23656. + }
  23657. + if (m&(0x00ff0000UL<<1)) {
  23658. + masklen |= 0x08;
  23659. + m <<= 8;
  23660. + }
  23661. + if (m&(0x0f000000UL<<1)) {
  23662. + masklen |= 0x04;
  23663. + m <<= 4;
  23664. + }
  23665. + if (m&(0x30000000UL<<1)) {
  23666. + masklen |= 0x02;
  23667. + m <<= 2;
  23668. + }
  23669. + if (m&(0x40000000UL<<1))
  23670. + masklen |= 0x01;
  23671. +
  23672. + return masklen;
  23673. +}
  23674. +
  23675. +/*
  23676. + - bitstomask - return a mask with this many high bits on
  23677. + */
  23678. +struct in_addr
  23679. +bitstomask(n)
  23680. +int n;
  23681. +{
  23682. + struct in_addr result;
  23683. +
  23684. + if (n > 0 && n <= ABITS)
  23685. + result.s_addr = htonl(~((1UL << (ABITS - n)) - 1));
  23686. + else if (n == 0)
  23687. + result.s_addr = 0;
  23688. + else
  23689. + result.s_addr = 0; /* best error report we can do */
  23690. + return result;
  23691. +}
  23692. --- /dev/null Tue Mar 11 13:02:56 2003
  23693. +++ linux/net/ipsec/infblock.c Mon Feb 9 13:51:03 2004
  23694. @@ -0,0 +1,403 @@
  23695. +/* infblock.c -- interpret and process block types to last block
  23696. + * Copyright (C) 1995-2002 Mark Adler
  23697. + * For conditions of distribution and use, see copyright notice in zlib.h
  23698. + */
  23699. +
  23700. +#include <zlib/zutil.h>
  23701. +#include "infblock.h"
  23702. +#include "inftrees.h"
  23703. +#include "infcodes.h"
  23704. +#include "infutil.h"
  23705. +
  23706. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  23707. +
  23708. +/* simplify the use of the inflate_huft type with some defines */
  23709. +#define exop word.what.Exop
  23710. +#define bits word.what.Bits
  23711. +
  23712. +/* Table for deflate from PKZIP's appnote.txt. */
  23713. +local const uInt border[] = { /* Order of the bit length code lengths */
  23714. + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  23715. +
  23716. +/*
  23717. + Notes beyond the 1.93a appnote.txt:
  23718. +
  23719. + 1. Distance pointers never point before the beginning of the output
  23720. + stream.
  23721. + 2. Distance pointers can point back across blocks, up to 32k away.
  23722. + 3. There is an implied maximum of 7 bits for the bit length table and
  23723. + 15 bits for the actual data.
  23724. + 4. If only one code exists, then it is encoded using one bit. (Zero
  23725. + would be more efficient, but perhaps a little confusing.) If two
  23726. + codes exist, they are coded using one bit each (0 and 1).
  23727. + 5. There is no way of sending zero distance codes--a dummy must be
  23728. + sent if there are none. (History: a pre 2.0 version of PKZIP would
  23729. + store blocks with no distance codes, but this was discovered to be
  23730. + too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  23731. + zero distance codes, which is sent as one code of zero bits in
  23732. + length.
  23733. + 6. There are up to 286 literal/length codes. Code 256 represents the
  23734. + end-of-block. Note however that the static length tree defines
  23735. + 288 codes just to fill out the Huffman codes. Codes 286 and 287
  23736. + cannot be used though, since there is no length base or extra bits
  23737. + defined for them. Similarily, there are up to 30 distance codes.
  23738. + However, static trees define 32 codes (all 5 bits) to fill out the
  23739. + Huffman codes, but the last two had better not show up in the data.
  23740. + 7. Unzip can check dynamic Huffman blocks for complete code sets.
  23741. + The exception is that a single code would not be complete (see #4).
  23742. + 8. The five bits following the block type is really the number of
  23743. + literal codes sent minus 257.
  23744. + 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  23745. + (1+6+6). Therefore, to output three times the length, you output
  23746. + three codes (1+1+1), whereas to output four times the same length,
  23747. + you only need two codes (1+3). Hmm.
  23748. + 10. In the tree reconstruction algorithm, Code = Code + Increment
  23749. + only if BitLength(i) is not zero. (Pretty obvious.)
  23750. + 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  23751. + 12. Note: length code 284 can represent 227-258, but length code 285
  23752. + really is 258. The last length deserves its own, short code
  23753. + since it gets used a lot in very redundant files. The length
  23754. + 258 is special since 258 - 3 (the min match length) is 255.
  23755. + 13. The literal/length and distance code bit lengths are read as a
  23756. + single stream of lengths. It is possible (and advantageous) for
  23757. + a repeat code (16, 17, or 18) to go across the boundary between
  23758. + the two sets of lengths.
  23759. + */
  23760. +
  23761. +
  23762. +void inflate_blocks_reset(s, z, c)
  23763. +inflate_blocks_statef *s;
  23764. +z_streamp z;
  23765. +uLongf *c;
  23766. +{
  23767. + if (c != Z_NULL)
  23768. + *c = s->check;
  23769. + if (s->mode == BTREE || s->mode == DTREE)
  23770. + ZFREE(z, s->sub.trees.blens);
  23771. + if (s->mode == CODES)
  23772. + inflate_codes_free(s->sub.decode.codes, z);
  23773. + s->mode = TYPE;
  23774. + s->bitk = 0;
  23775. + s->bitb = 0;
  23776. + s->read = s->write = s->window;
  23777. + if (s->checkfn != Z_NULL)
  23778. + z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
  23779. + Tracev((stderr, "inflate: blocks reset\n"));
  23780. +}
  23781. +
  23782. +
  23783. +inflate_blocks_statef *inflate_blocks_new(z, c, w)
  23784. +z_streamp z;
  23785. +check_func c;
  23786. +uInt w;
  23787. +{
  23788. + inflate_blocks_statef *s;
  23789. +
  23790. + if ((s = (inflate_blocks_statef *)ZALLOC
  23791. + (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
  23792. + return s;
  23793. + if ((s->hufts =
  23794. + (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
  23795. + {
  23796. + ZFREE(z, s);
  23797. + return Z_NULL;
  23798. + }
  23799. + if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
  23800. + {
  23801. + ZFREE(z, s->hufts);
  23802. + ZFREE(z, s);
  23803. + return Z_NULL;
  23804. + }
  23805. + s->end = s->window + w;
  23806. + s->checkfn = c;
  23807. + s->mode = TYPE;
  23808. + Tracev((stderr, "inflate: blocks allocated\n"));
  23809. + inflate_blocks_reset(s, z, Z_NULL);
  23810. + return s;
  23811. +}
  23812. +
  23813. +
  23814. +int inflate_blocks(s, z, r)
  23815. +inflate_blocks_statef *s;
  23816. +z_streamp z;
  23817. +int r;
  23818. +{
  23819. + uInt t; /* temporary storage */
  23820. + uLong b; /* bit buffer */
  23821. + uInt k; /* bits in bit buffer */
  23822. + Bytef *p; /* input data pointer */
  23823. + uInt n; /* bytes available there */
  23824. + Bytef *q; /* output window write pointer */
  23825. + uInt m; /* bytes to end of window or read pointer */
  23826. +
  23827. + /* copy input/output information to locals (UPDATE macro restores) */
  23828. + LOAD
  23829. +
  23830. + /* process input based on current state */
  23831. + while (1) switch (s->mode)
  23832. + {
  23833. + case TYPE:
  23834. + NEEDBITS(3)
  23835. + t = (uInt)b & 7;
  23836. + s->last = t & 1;
  23837. + switch (t >> 1)
  23838. + {
  23839. + case 0: /* stored */
  23840. + Tracev((stderr, "inflate: stored block%s\n",
  23841. + s->last ? " (last)" : ""));
  23842. + DUMPBITS(3)
  23843. + t = k & 7; /* go to byte boundary */
  23844. + DUMPBITS(t)
  23845. + s->mode = LENS; /* get length of stored block */
  23846. + break;
  23847. + case 1: /* fixed */
  23848. + Tracev((stderr, "inflate: fixed codes block%s\n",
  23849. + s->last ? " (last)" : ""));
  23850. + {
  23851. + uInt bl, bd;
  23852. + inflate_huft *tl, *td;
  23853. +
  23854. + inflate_trees_fixed(&bl, &bd, &tl, &td, z);
  23855. + s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
  23856. + if (s->sub.decode.codes == Z_NULL)
  23857. + {
  23858. + r = Z_MEM_ERROR;
  23859. + LEAVE
  23860. + }
  23861. + }
  23862. + DUMPBITS(3)
  23863. + s->mode = CODES;
  23864. + break;
  23865. + case 2: /* dynamic */
  23866. + Tracev((stderr, "inflate: dynamic codes block%s\n",
  23867. + s->last ? " (last)" : ""));
  23868. + DUMPBITS(3)
  23869. + s->mode = TABLE;
  23870. + break;
  23871. + case 3: /* illegal */
  23872. + DUMPBITS(3)
  23873. + s->mode = BAD;
  23874. + z->msg = (char*)"invalid block type";
  23875. + r = Z_DATA_ERROR;
  23876. + LEAVE
  23877. + }
  23878. + break;
  23879. + case LENS:
  23880. + NEEDBITS(32)
  23881. + if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
  23882. + {
  23883. + s->mode = BAD;
  23884. + z->msg = (char*)"invalid stored block lengths";
  23885. + r = Z_DATA_ERROR;
  23886. + LEAVE
  23887. + }
  23888. + s->sub.left = (uInt)b & 0xffff;
  23889. + b = k = 0; /* dump bits */
  23890. + Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
  23891. + s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
  23892. + break;
  23893. + case STORED:
  23894. + if (n == 0)
  23895. + LEAVE
  23896. + NEEDOUT
  23897. + t = s->sub.left;
  23898. + if (t > n) t = n;
  23899. + if (t > m) t = m;
  23900. + zmemcpy(q, p, t);
  23901. + p += t; n -= t;
  23902. + q += t; m -= t;
  23903. + if ((s->sub.left -= t) != 0)
  23904. + break;
  23905. + Tracev((stderr, "inflate: stored end, %lu total out\n",
  23906. + z->total_out + (q >= s->read ? q - s->read :
  23907. + (s->end - s->read) + (q - s->window))));
  23908. + s->mode = s->last ? DRY : TYPE;
  23909. + break;
  23910. + case TABLE:
  23911. + NEEDBITS(14)
  23912. + s->sub.trees.table = t = (uInt)b & 0x3fff;
  23913. +#ifndef PKZIP_BUG_WORKAROUND
  23914. + if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  23915. + {
  23916. + s->mode = BAD;
  23917. + z->msg = (char*)"too many length or distance symbols";
  23918. + r = Z_DATA_ERROR;
  23919. + LEAVE
  23920. + }
  23921. +#endif
  23922. + t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  23923. + if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
  23924. + {
  23925. + r = Z_MEM_ERROR;
  23926. + LEAVE
  23927. + }
  23928. + DUMPBITS(14)
  23929. + s->sub.trees.index = 0;
  23930. + Tracev((stderr, "inflate: table sizes ok\n"));
  23931. + s->mode = BTREE;
  23932. + case BTREE:
  23933. + while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  23934. + {
  23935. + NEEDBITS(3)
  23936. + s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  23937. + DUMPBITS(3)
  23938. + }
  23939. + while (s->sub.trees.index < 19)
  23940. + s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
  23941. + s->sub.trees.bb = 7;
  23942. + t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
  23943. + &s->sub.trees.tb, s->hufts, z);
  23944. + if (t != Z_OK)
  23945. + {
  23946. + r = t;
  23947. + if (r == Z_DATA_ERROR)
  23948. + {
  23949. + ZFREE(z, s->sub.trees.blens);
  23950. + s->mode = BAD;
  23951. + }
  23952. + LEAVE
  23953. + }
  23954. + s->sub.trees.index = 0;
  23955. + Tracev((stderr, "inflate: bits tree ok\n"));
  23956. + s->mode = DTREE;
  23957. + case DTREE:
  23958. + while (t = s->sub.trees.table,
  23959. + s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  23960. + {
  23961. + inflate_huft *h;
  23962. + uInt i, j, c;
  23963. +
  23964. + t = s->sub.trees.bb;
  23965. + NEEDBITS(t)
  23966. + h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
  23967. + t = h->bits;
  23968. + c = h->base;
  23969. + if (c < 16)
  23970. + {
  23971. + DUMPBITS(t)
  23972. + s->sub.trees.blens[s->sub.trees.index++] = c;
  23973. + }
  23974. + else /* c == 16..18 */
  23975. + {
  23976. + i = c == 18 ? 7 : c - 14;
  23977. + j = c == 18 ? 11 : 3;
  23978. + NEEDBITS(t + i)
  23979. + DUMPBITS(t)
  23980. + j += (uInt)b & inflate_mask[i];
  23981. + DUMPBITS(i)
  23982. + i = s->sub.trees.index;
  23983. + t = s->sub.trees.table;
  23984. + if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  23985. + (c == 16 && i < 1))
  23986. + {
  23987. + ZFREE(z, s->sub.trees.blens);
  23988. + s->mode = BAD;
  23989. + z->msg = (char*)"invalid bit length repeat";
  23990. + r = Z_DATA_ERROR;
  23991. + LEAVE
  23992. + }
  23993. + c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
  23994. + do {
  23995. + s->sub.trees.blens[i++] = c;
  23996. + } while (--j);
  23997. + s->sub.trees.index = i;
  23998. + }
  23999. + }
  24000. + s->sub.trees.tb = Z_NULL;
  24001. + {
  24002. + uInt bl, bd;
  24003. + inflate_huft *tl, *td;
  24004. + inflate_codes_statef *c;
  24005. +
  24006. + bl = 9; /* must be <= 9 for lookahead assumptions */
  24007. + bd = 6; /* must be <= 9 for lookahead assumptions */
  24008. + t = s->sub.trees.table;
  24009. + t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
  24010. + s->sub.trees.blens, &bl, &bd, &tl, &td,
  24011. + s->hufts, z);
  24012. + if (t != Z_OK)
  24013. + {
  24014. + if (t == (uInt)Z_DATA_ERROR)
  24015. + {
  24016. + ZFREE(z, s->sub.trees.blens);
  24017. + s->mode = BAD;
  24018. + }
  24019. + r = t;
  24020. + LEAVE
  24021. + }
  24022. + Tracev((stderr, "inflate: trees ok\n"));
  24023. + if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
  24024. + {
  24025. + r = Z_MEM_ERROR;
  24026. + LEAVE
  24027. + }
  24028. + s->sub.decode.codes = c;
  24029. + }
  24030. + ZFREE(z, s->sub.trees.blens);
  24031. + s->mode = CODES;
  24032. + case CODES:
  24033. + UPDATE
  24034. + if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
  24035. + return inflate_flush(s, z, r);
  24036. + r = Z_OK;
  24037. + inflate_codes_free(s->sub.decode.codes, z);
  24038. + LOAD
  24039. + Tracev((stderr, "inflate: codes end, %lu total out\n",
  24040. + z->total_out + (q >= s->read ? q - s->read :
  24041. + (s->end - s->read) + (q - s->window))));
  24042. + if (!s->last)
  24043. + {
  24044. + s->mode = TYPE;
  24045. + break;
  24046. + }
  24047. + s->mode = DRY;
  24048. + case DRY:
  24049. + FLUSH
  24050. + if (s->read != s->write)
  24051. + LEAVE
  24052. + s->mode = DONE;
  24053. + case DONE:
  24054. + r = Z_STREAM_END;
  24055. + LEAVE
  24056. + case BAD:
  24057. + r = Z_DATA_ERROR;
  24058. + LEAVE
  24059. + default:
  24060. + r = Z_STREAM_ERROR;
  24061. + LEAVE
  24062. + }
  24063. +}
  24064. +
  24065. +
  24066. +int inflate_blocks_free(s, z)
  24067. +inflate_blocks_statef *s;
  24068. +z_streamp z;
  24069. +{
  24070. + inflate_blocks_reset(s, z, Z_NULL);
  24071. + ZFREE(z, s->window);
  24072. + ZFREE(z, s->hufts);
  24073. + ZFREE(z, s);
  24074. + Tracev((stderr, "inflate: blocks freed\n"));
  24075. + return Z_OK;
  24076. +}
  24077. +
  24078. +
  24079. +void inflate_set_dictionary(s, d, n)
  24080. +inflate_blocks_statef *s;
  24081. +const Bytef *d;
  24082. +uInt n;
  24083. +{
  24084. + zmemcpy(s->window, d, n);
  24085. + s->read = s->write = s->window + n;
  24086. +}
  24087. +
  24088. +
  24089. +/* Returns true if inflate is currently at the end of a block generated
  24090. + * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  24091. + * IN assertion: s != Z_NULL
  24092. + */
  24093. +int inflate_blocks_sync_point(s)
  24094. +inflate_blocks_statef *s;
  24095. +{
  24096. + return s->mode == LENS;
  24097. +}
  24098. --- /dev/null Tue Mar 11 13:02:56 2003
  24099. +++ linux/net/ipsec/infblock.h Mon Feb 9 13:51:03 2004
  24100. @@ -0,0 +1,39 @@
  24101. +/* infblock.h -- header to use infblock.c
  24102. + * Copyright (C) 1995-2002 Mark Adler
  24103. + * For conditions of distribution and use, see copyright notice in zlib.h
  24104. + */
  24105. +
  24106. +/* WARNING: this file should *not* be used by applications. It is
  24107. + part of the implementation of the compression library and is
  24108. + subject to change. Applications should only use zlib.h.
  24109. + */
  24110. +
  24111. +struct inflate_blocks_state;
  24112. +typedef struct inflate_blocks_state FAR inflate_blocks_statef;
  24113. +
  24114. +extern inflate_blocks_statef * inflate_blocks_new OF((
  24115. + z_streamp z,
  24116. + check_func c, /* check function */
  24117. + uInt w)); /* window size */
  24118. +
  24119. +extern int inflate_blocks OF((
  24120. + inflate_blocks_statef *,
  24121. + z_streamp ,
  24122. + int)); /* initial return code */
  24123. +
  24124. +extern void inflate_blocks_reset OF((
  24125. + inflate_blocks_statef *,
  24126. + z_streamp ,
  24127. + uLongf *)); /* check value on output */
  24128. +
  24129. +extern int inflate_blocks_free OF((
  24130. + inflate_blocks_statef *,
  24131. + z_streamp));
  24132. +
  24133. +extern void inflate_set_dictionary OF((
  24134. + inflate_blocks_statef *s,
  24135. + const Bytef *d, /* dictionary */
  24136. + uInt n)); /* dictionary length */
  24137. +
  24138. +extern int inflate_blocks_sync_point OF((
  24139. + inflate_blocks_statef *s));
  24140. --- /dev/null Tue Mar 11 13:02:56 2003
  24141. +++ linux/net/ipsec/infcodes.c Mon Feb 9 13:51:03 2004
  24142. @@ -0,0 +1,251 @@
  24143. +/* infcodes.c -- process literals and length/distance pairs
  24144. + * Copyright (C) 1995-2002 Mark Adler
  24145. + * For conditions of distribution and use, see copyright notice in zlib.h
  24146. + */
  24147. +
  24148. +#include <zlib/zutil.h>
  24149. +#include "inftrees.h"
  24150. +#include "infblock.h"
  24151. +#include "infcodes.h"
  24152. +#include "infutil.h"
  24153. +#include "inffast.h"
  24154. +
  24155. +/* simplify the use of the inflate_huft type with some defines */
  24156. +#define exop word.what.Exop
  24157. +#define bits word.what.Bits
  24158. +
  24159. +typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  24160. + START, /* x: set up for LEN */
  24161. + LEN, /* i: get length/literal/eob next */
  24162. + LENEXT, /* i: getting length extra (have base) */
  24163. + DIST, /* i: get distance next */
  24164. + DISTEXT, /* i: getting distance extra */
  24165. + COPY, /* o: copying bytes in window, waiting for space */
  24166. + LIT, /* o: got literal, waiting for output space */
  24167. + WASH, /* o: got eob, possibly still output waiting */
  24168. + END, /* x: got eob and all data flushed */
  24169. + BADCODE} /* x: got error */
  24170. +inflate_codes_mode;
  24171. +
  24172. +/* inflate codes private state */
  24173. +struct inflate_codes_state {
  24174. +
  24175. + /* mode */
  24176. + inflate_codes_mode mode; /* current inflate_codes mode */
  24177. +
  24178. + /* mode dependent information */
  24179. + uInt len;
  24180. + union {
  24181. + struct {
  24182. + inflate_huft *tree; /* pointer into tree */
  24183. + uInt need; /* bits needed */
  24184. + } code; /* if LEN or DIST, where in tree */
  24185. + uInt lit; /* if LIT, literal */
  24186. + struct {
  24187. + uInt get; /* bits to get for extra */
  24188. + uInt dist; /* distance back to copy from */
  24189. + } copy; /* if EXT or COPY, where and how much */
  24190. + } sub; /* submode */
  24191. +
  24192. + /* mode independent information */
  24193. + Byte lbits; /* ltree bits decoded per branch */
  24194. + Byte dbits; /* dtree bits decoder per branch */
  24195. + inflate_huft *ltree; /* literal/length/eob tree */
  24196. + inflate_huft *dtree; /* distance tree */
  24197. +
  24198. +};
  24199. +
  24200. +
  24201. +inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
  24202. +uInt bl, bd;
  24203. +inflate_huft *tl;
  24204. +inflate_huft *td; /* need separate declaration for Borland C++ */
  24205. +z_streamp z;
  24206. +{
  24207. + inflate_codes_statef *c;
  24208. +
  24209. + if ((c = (inflate_codes_statef *)
  24210. + ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  24211. + {
  24212. + c->mode = START;
  24213. + c->lbits = (Byte)bl;
  24214. + c->dbits = (Byte)bd;
  24215. + c->ltree = tl;
  24216. + c->dtree = td;
  24217. + Tracev((stderr, "inflate: codes new\n"));
  24218. + }
  24219. + return c;
  24220. +}
  24221. +
  24222. +
  24223. +int inflate_codes(s, z, r)
  24224. +inflate_blocks_statef *s;
  24225. +z_streamp z;
  24226. +int r;
  24227. +{
  24228. + uInt j; /* temporary storage */
  24229. + inflate_huft *t; /* temporary pointer */
  24230. + uInt e; /* extra bits or operation */
  24231. + uLong b; /* bit buffer */
  24232. + uInt k; /* bits in bit buffer */
  24233. + Bytef *p; /* input data pointer */
  24234. + uInt n; /* bytes available there */
  24235. + Bytef *q; /* output window write pointer */
  24236. + uInt m; /* bytes to end of window or read pointer */
  24237. + Bytef *f; /* pointer to copy strings from */
  24238. + inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
  24239. +
  24240. + /* copy input/output information to locals (UPDATE macro restores) */
  24241. + LOAD
  24242. +
  24243. + /* process input and output based on current state */
  24244. + while (1) switch (c->mode)
  24245. + { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  24246. + case START: /* x: set up for LEN */
  24247. +#ifndef SLOW
  24248. + if (m >= 258 && n >= 10)
  24249. + {
  24250. + UPDATE
  24251. + r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  24252. + LOAD
  24253. + if (r != Z_OK)
  24254. + {
  24255. + c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  24256. + break;
  24257. + }
  24258. + }
  24259. +#endif /* !SLOW */
  24260. + c->sub.code.need = c->lbits;
  24261. + c->sub.code.tree = c->ltree;
  24262. + c->mode = LEN;
  24263. + case LEN: /* i: get length/literal/eob next */
  24264. + j = c->sub.code.need;
  24265. + NEEDBITS(j)
  24266. + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  24267. + DUMPBITS(t->bits)
  24268. + e = (uInt)(t->exop);
  24269. + if (e == 0) /* literal */
  24270. + {
  24271. + c->sub.lit = t->base;
  24272. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24273. + "inflate: literal '%c'\n" :
  24274. + "inflate: literal 0x%02x\n", t->base));
  24275. + c->mode = LIT;
  24276. + break;
  24277. + }
  24278. + if (e & 16) /* length */
  24279. + {
  24280. + c->sub.copy.get = e & 15;
  24281. + c->len = t->base;
  24282. + c->mode = LENEXT;
  24283. + break;
  24284. + }
  24285. + if ((e & 64) == 0) /* next table */
  24286. + {
  24287. + c->sub.code.need = e;
  24288. + c->sub.code.tree = t + t->base;
  24289. + break;
  24290. + }
  24291. + if (e & 32) /* end of block */
  24292. + {
  24293. + Tracevv((stderr, "inflate: end of block\n"));
  24294. + c->mode = WASH;
  24295. + break;
  24296. + }
  24297. + c->mode = BADCODE; /* invalid code */
  24298. + z->msg = (char*)"invalid literal/length code";
  24299. + r = Z_DATA_ERROR;
  24300. + LEAVE
  24301. + case LENEXT: /* i: getting length extra (have base) */
  24302. + j = c->sub.copy.get;
  24303. + NEEDBITS(j)
  24304. + c->len += (uInt)b & inflate_mask[j];
  24305. + DUMPBITS(j)
  24306. + c->sub.code.need = c->dbits;
  24307. + c->sub.code.tree = c->dtree;
  24308. + Tracevv((stderr, "inflate: length %u\n", c->len));
  24309. + c->mode = DIST;
  24310. + case DIST: /* i: get distance next */
  24311. + j = c->sub.code.need;
  24312. + NEEDBITS(j)
  24313. + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  24314. + DUMPBITS(t->bits)
  24315. + e = (uInt)(t->exop);
  24316. + if (e & 16) /* distance */
  24317. + {
  24318. + c->sub.copy.get = e & 15;
  24319. + c->sub.copy.dist = t->base;
  24320. + c->mode = DISTEXT;
  24321. + break;
  24322. + }
  24323. + if ((e & 64) == 0) /* next table */
  24324. + {
  24325. + c->sub.code.need = e;
  24326. + c->sub.code.tree = t + t->base;
  24327. + break;
  24328. + }
  24329. + c->mode = BADCODE; /* invalid code */
  24330. + z->msg = (char*)"invalid distance code";
  24331. + r = Z_DATA_ERROR;
  24332. + LEAVE
  24333. + case DISTEXT: /* i: getting distance extra */
  24334. + j = c->sub.copy.get;
  24335. + NEEDBITS(j)
  24336. + c->sub.copy.dist += (uInt)b & inflate_mask[j];
  24337. + DUMPBITS(j)
  24338. + Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
  24339. + c->mode = COPY;
  24340. + case COPY: /* o: copying bytes in window, waiting for space */
  24341. + f = q - c->sub.copy.dist;
  24342. + while (f < s->window) /* modulo window size-"while" instead */
  24343. + f += s->end - s->window; /* of "if" handles invalid distances */
  24344. + while (c->len)
  24345. + {
  24346. + NEEDOUT
  24347. + OUTBYTE(*f++)
  24348. + if (f == s->end)
  24349. + f = s->window;
  24350. + c->len--;
  24351. + }
  24352. + c->mode = START;
  24353. + break;
  24354. + case LIT: /* o: got literal, waiting for output space */
  24355. + NEEDOUT
  24356. + OUTBYTE(c->sub.lit)
  24357. + c->mode = START;
  24358. + break;
  24359. + case WASH: /* o: got eob, possibly more output */
  24360. + if (k > 7) /* return unused byte, if any */
  24361. + {
  24362. + Assert(k < 16, "inflate_codes grabbed too many bytes")
  24363. + k -= 8;
  24364. + n++;
  24365. + p--; /* can always return one */
  24366. + }
  24367. + FLUSH
  24368. + if (s->read != s->write)
  24369. + LEAVE
  24370. + c->mode = END;
  24371. + case END:
  24372. + r = Z_STREAM_END;
  24373. + LEAVE
  24374. + case BADCODE: /* x: got error */
  24375. + r = Z_DATA_ERROR;
  24376. + LEAVE
  24377. + default:
  24378. + r = Z_STREAM_ERROR;
  24379. + LEAVE
  24380. + }
  24381. +#ifdef NEED_DUMMY_RETURN
  24382. + return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  24383. +#endif
  24384. +}
  24385. +
  24386. +
  24387. +void inflate_codes_free(c, z)
  24388. +inflate_codes_statef *c;
  24389. +z_streamp z;
  24390. +{
  24391. + ZFREE(z, c);
  24392. + Tracev((stderr, "inflate: codes free\n"));
  24393. +}
  24394. --- /dev/null Tue Mar 11 13:02:56 2003
  24395. +++ linux/net/ipsec/infcodes.h Mon Feb 9 13:51:03 2004
  24396. @@ -0,0 +1,31 @@
  24397. +/* infcodes.h -- header to use infcodes.c
  24398. + * Copyright (C) 1995-2002 Mark Adler
  24399. + * For conditions of distribution and use, see copyright notice in zlib.h
  24400. + */
  24401. +
  24402. +/* WARNING: this file should *not* be used by applications. It is
  24403. + part of the implementation of the compression library and is
  24404. + subject to change. Applications should only use zlib.h.
  24405. + */
  24406. +
  24407. +#ifndef _INFCODES_H
  24408. +#define _INFCODES_H
  24409. +
  24410. +struct inflate_codes_state;
  24411. +typedef struct inflate_codes_state FAR inflate_codes_statef;
  24412. +
  24413. +extern inflate_codes_statef *inflate_codes_new OF((
  24414. + uInt, uInt,
  24415. + inflate_huft *, inflate_huft *,
  24416. + z_streamp ));
  24417. +
  24418. +extern int inflate_codes OF((
  24419. + inflate_blocks_statef *,
  24420. + z_streamp ,
  24421. + int));
  24422. +
  24423. +extern void inflate_codes_free OF((
  24424. + inflate_codes_statef *,
  24425. + z_streamp ));
  24426. +
  24427. +#endif /* _INFCODES_H */
  24428. --- /dev/null Tue Mar 11 13:02:56 2003
  24429. +++ linux/net/ipsec/inffast.c Mon Feb 9 13:51:03 2004
  24430. @@ -0,0 +1,183 @@
  24431. +/* inffast.c -- process literals and length/distance pairs fast
  24432. + * Copyright (C) 1995-2002 Mark Adler
  24433. + * For conditions of distribution and use, see copyright notice in zlib.h
  24434. + */
  24435. +
  24436. +#include <zlib/zutil.h>
  24437. +#include "inftrees.h"
  24438. +#include "infblock.h"
  24439. +#include "infcodes.h"
  24440. +#include "infutil.h"
  24441. +#include "inffast.h"
  24442. +
  24443. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  24444. +
  24445. +/* simplify the use of the inflate_huft type with some defines */
  24446. +#define exop word.what.Exop
  24447. +#define bits word.what.Bits
  24448. +
  24449. +/* macros for bit input with no checking and for returning unused bytes */
  24450. +#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  24451. +#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
  24452. +
  24453. +/* Called with number of bytes left to write in window at least 258
  24454. + (the maximum string length) and number of input bytes available
  24455. + at least ten. The ten bytes are six bytes for the longest length/
  24456. + distance pair plus four bytes for overloading the bit buffer. */
  24457. +
  24458. +int inflate_fast(bl, bd, tl, td, s, z)
  24459. +uInt bl, bd;
  24460. +inflate_huft *tl;
  24461. +inflate_huft *td; /* need separate declaration for Borland C++ */
  24462. +inflate_blocks_statef *s;
  24463. +z_streamp z;
  24464. +{
  24465. + inflate_huft *t; /* temporary pointer */
  24466. + uInt e; /* extra bits or operation */
  24467. + uLong b; /* bit buffer */
  24468. + uInt k; /* bits in bit buffer */
  24469. + Bytef *p; /* input data pointer */
  24470. + uInt n; /* bytes available there */
  24471. + Bytef *q; /* output window write pointer */
  24472. + uInt m; /* bytes to end of window or read pointer */
  24473. + uInt ml; /* mask for literal/length tree */
  24474. + uInt md; /* mask for distance tree */
  24475. + uInt c; /* bytes to copy */
  24476. + uInt d; /* distance back to copy from */
  24477. + Bytef *r; /* copy source pointer */
  24478. +
  24479. + /* load input, output, bit values */
  24480. + LOAD
  24481. +
  24482. + /* initialize masks */
  24483. + ml = inflate_mask[bl];
  24484. + md = inflate_mask[bd];
  24485. +
  24486. + /* do until not enough input or output space for fast loop */
  24487. + do { /* assume called with m >= 258 && n >= 10 */
  24488. + /* get literal/length code */
  24489. + GRABBITS(20) /* max bits for literal/length code */
  24490. + if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
  24491. + {
  24492. + DUMPBITS(t->bits)
  24493. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24494. + "inflate: * literal '%c'\n" :
  24495. + "inflate: * literal 0x%02x\n", t->base));
  24496. + *q++ = (Byte)t->base;
  24497. + m--;
  24498. + continue;
  24499. + }
  24500. + do {
  24501. + DUMPBITS(t->bits)
  24502. + if (e & 16)
  24503. + {
  24504. + /* get extra bits for length */
  24505. + e &= 15;
  24506. + c = t->base + ((uInt)b & inflate_mask[e]);
  24507. + DUMPBITS(e)
  24508. + Tracevv((stderr, "inflate: * length %u\n", c));
  24509. +
  24510. + /* decode distance base of block to copy */
  24511. + GRABBITS(15); /* max bits for distance code */
  24512. + e = (t = td + ((uInt)b & md))->exop;
  24513. + do {
  24514. + DUMPBITS(t->bits)
  24515. + if (e & 16)
  24516. + {
  24517. + /* get extra bits to add to distance base */
  24518. + e &= 15;
  24519. + GRABBITS(e) /* get extra bits (up to 13) */
  24520. + d = t->base + ((uInt)b & inflate_mask[e]);
  24521. + DUMPBITS(e)
  24522. + Tracevv((stderr, "inflate: * distance %u\n", d));
  24523. +
  24524. + /* do the copy */
  24525. + m -= c;
  24526. + r = q - d;
  24527. + if (r < s->window) /* wrap if needed */
  24528. + {
  24529. + do {
  24530. + r += s->end - s->window; /* force pointer in window */
  24531. + } while (r < s->window); /* covers invalid distances */
  24532. + e = s->end - r;
  24533. + if (c > e)
  24534. + {
  24535. + c -= e; /* wrapped copy */
  24536. + do {
  24537. + *q++ = *r++;
  24538. + } while (--e);
  24539. + r = s->window;
  24540. + do {
  24541. + *q++ = *r++;
  24542. + } while (--c);
  24543. + }
  24544. + else /* normal copy */
  24545. + {
  24546. + *q++ = *r++; c--;
  24547. + *q++ = *r++; c--;
  24548. + do {
  24549. + *q++ = *r++;
  24550. + } while (--c);
  24551. + }
  24552. + }
  24553. + else /* normal copy */
  24554. + {
  24555. + *q++ = *r++; c--;
  24556. + *q++ = *r++; c--;
  24557. + do {
  24558. + *q++ = *r++;
  24559. + } while (--c);
  24560. + }
  24561. + break;
  24562. + }
  24563. + else if ((e & 64) == 0)
  24564. + {
  24565. + t += t->base;
  24566. + e = (t += ((uInt)b & inflate_mask[e]))->exop;
  24567. + }
  24568. + else
  24569. + {
  24570. + z->msg = (char*)"invalid distance code";
  24571. + UNGRAB
  24572. + UPDATE
  24573. + return Z_DATA_ERROR;
  24574. + }
  24575. + } while (1);
  24576. + break;
  24577. + }
  24578. + if ((e & 64) == 0)
  24579. + {
  24580. + t += t->base;
  24581. + if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
  24582. + {
  24583. + DUMPBITS(t->bits)
  24584. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24585. + "inflate: * literal '%c'\n" :
  24586. + "inflate: * literal 0x%02x\n", t->base));
  24587. + *q++ = (Byte)t->base;
  24588. + m--;
  24589. + break;
  24590. + }
  24591. + }
  24592. + else if (e & 32)
  24593. + {
  24594. + Tracevv((stderr, "inflate: * end of block\n"));
  24595. + UNGRAB
  24596. + UPDATE
  24597. + return Z_STREAM_END;
  24598. + }
  24599. + else
  24600. + {
  24601. + z->msg = (char*)"invalid literal/length code";
  24602. + UNGRAB
  24603. + UPDATE
  24604. + return Z_DATA_ERROR;
  24605. + }
  24606. + } while (1);
  24607. + } while (m >= 258 && n >= 10);
  24608. +
  24609. + /* not enough input or output--restore pointers and return */
  24610. + UNGRAB
  24611. + UPDATE
  24612. + return Z_OK;
  24613. +}
  24614. --- /dev/null Tue Mar 11 13:02:56 2003
  24615. +++ linux/net/ipsec/inffast.h Mon Feb 9 13:51:03 2004
  24616. @@ -0,0 +1,22 @@
  24617. +/* inffast.h -- header to use inffast.c
  24618. + * Copyright (C) 1995-2002 Mark Adler
  24619. + * For conditions of distribution and use, see copyright notice in zlib.h
  24620. + */
  24621. +
  24622. +/* WARNING: this file should *not* be used by applications. It is
  24623. + part of the implementation of the compression library and is
  24624. + subject to change. Applications should only use zlib.h.
  24625. + */
  24626. +
  24627. +#ifndef _INFFAST_H
  24628. +#define _INFFAST_H
  24629. +
  24630. +extern int inflate_fast OF((
  24631. + uInt,
  24632. + uInt,
  24633. + inflate_huft *,
  24634. + inflate_huft *,
  24635. + inflate_blocks_statef *,
  24636. + z_streamp ));
  24637. +
  24638. +#endif /* _INFFAST_H */
  24639. --- /dev/null Tue Mar 11 13:02:56 2003
  24640. +++ linux/net/ipsec/inffixed.h Mon Feb 9 13:51:03 2004
  24641. @@ -0,0 +1,151 @@
  24642. +/* inffixed.h -- table for decoding fixed codes
  24643. + * Generated automatically by the maketree.c program
  24644. + */
  24645. +
  24646. +/* WARNING: this file should *not* be used by applications. It is
  24647. + part of the implementation of the compression library and is
  24648. + subject to change. Applications should only use zlib.h.
  24649. + */
  24650. +
  24651. +local uInt fixed_bl = 9;
  24652. +local uInt fixed_bd = 5;
  24653. +local inflate_huft fixed_tl[] = {
  24654. + {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  24655. + {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
  24656. + {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
  24657. + {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
  24658. + {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
  24659. + {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
  24660. + {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
  24661. + {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
  24662. + {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  24663. + {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
  24664. + {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
  24665. + {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
  24666. + {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
  24667. + {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
  24668. + {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
  24669. + {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
  24670. + {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  24671. + {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
  24672. + {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
  24673. + {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
  24674. + {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
  24675. + {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
  24676. + {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
  24677. + {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
  24678. + {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  24679. + {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
  24680. + {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
  24681. + {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
  24682. + {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
  24683. + {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
  24684. + {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
  24685. + {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
  24686. + {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  24687. + {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
  24688. + {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
  24689. + {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
  24690. + {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
  24691. + {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
  24692. + {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
  24693. + {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
  24694. + {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  24695. + {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
  24696. + {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
  24697. + {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
  24698. + {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
  24699. + {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
  24700. + {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
  24701. + {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
  24702. + {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  24703. + {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
  24704. + {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
  24705. + {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
  24706. + {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
  24707. + {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
  24708. + {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
  24709. + {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
  24710. + {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  24711. + {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
  24712. + {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
  24713. + {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
  24714. + {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
  24715. + {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
  24716. + {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
  24717. + {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
  24718. + {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  24719. + {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
  24720. + {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
  24721. + {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
  24722. + {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
  24723. + {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
  24724. + {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
  24725. + {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
  24726. + {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  24727. + {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
  24728. + {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
  24729. + {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
  24730. + {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
  24731. + {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
  24732. + {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
  24733. + {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
  24734. + {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  24735. + {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
  24736. + {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
  24737. + {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
  24738. + {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
  24739. + {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
  24740. + {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
  24741. + {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
  24742. + {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  24743. + {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
  24744. + {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
  24745. + {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
  24746. + {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
  24747. + {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
  24748. + {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
  24749. + {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
  24750. + {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  24751. + {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
  24752. + {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
  24753. + {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
  24754. + {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
  24755. + {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
  24756. + {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
  24757. + {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
  24758. + {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  24759. + {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
  24760. + {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
  24761. + {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
  24762. + {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
  24763. + {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
  24764. + {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
  24765. + {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
  24766. + {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  24767. + {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
  24768. + {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
  24769. + {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
  24770. + {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
  24771. + {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
  24772. + {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
  24773. + {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
  24774. + {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  24775. + {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
  24776. + {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
  24777. + {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
  24778. + {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
  24779. + {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
  24780. + {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
  24781. + {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
  24782. + };
  24783. +local inflate_huft fixed_td[] = {
  24784. + {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
  24785. + {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
  24786. + {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
  24787. + {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
  24788. + {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
  24789. + {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
  24790. + {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
  24791. + {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
  24792. + };
  24793. --- /dev/null Tue Mar 11 13:02:56 2003
  24794. +++ linux/net/ipsec/inflate.c Mon Feb 9 13:51:03 2004
  24795. @@ -0,0 +1,368 @@
  24796. +/* inflate.c -- zlib interface to inflate modules
  24797. + * Copyright (C) 1995-2002 Mark Adler
  24798. + * For conditions of distribution and use, see copyright notice in zlib.h
  24799. + */
  24800. +
  24801. +#include <zlib/zutil.h>
  24802. +#include "infblock.h"
  24803. +
  24804. +struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
  24805. +
  24806. +typedef enum {
  24807. + METHOD, /* waiting for method byte */
  24808. + FLAG, /* waiting for flag byte */
  24809. + DICT4, /* four dictionary check bytes to go */
  24810. + DICT3, /* three dictionary check bytes to go */
  24811. + DICT2, /* two dictionary check bytes to go */
  24812. + DICT1, /* one dictionary check byte to go */
  24813. + DICT0, /* waiting for inflateSetDictionary */
  24814. + BLOCKS, /* decompressing blocks */
  24815. + CHECK4, /* four check bytes to go */
  24816. + CHECK3, /* three check bytes to go */
  24817. + CHECK2, /* two check bytes to go */
  24818. + CHECK1, /* one check byte to go */
  24819. + DONE, /* finished check, done */
  24820. + BAD} /* got an error--stay here */
  24821. +inflate_mode;
  24822. +
  24823. +/* inflate private state */
  24824. +struct internal_state {
  24825. +
  24826. + /* mode */
  24827. + inflate_mode mode; /* current inflate mode */
  24828. +
  24829. + /* mode dependent information */
  24830. + union {
  24831. + uInt method; /* if FLAGS, method byte */
  24832. + struct {
  24833. + uLong was; /* computed check value */
  24834. + uLong need; /* stream check value */
  24835. + } check; /* if CHECK, check values to compare */
  24836. + uInt marker; /* if BAD, inflateSync's marker bytes count */
  24837. + } sub; /* submode */
  24838. +
  24839. + /* mode independent information */
  24840. + int nowrap; /* flag for no wrapper */
  24841. + uInt wbits; /* log2(window size) (8..15, defaults to 15) */
  24842. + inflate_blocks_statef
  24843. + *blocks; /* current inflate_blocks state */
  24844. +
  24845. +};
  24846. +
  24847. +
  24848. +int ZEXPORT inflateReset(z)
  24849. +z_streamp z;
  24850. +{
  24851. + if (z == Z_NULL || z->state == Z_NULL)
  24852. + return Z_STREAM_ERROR;
  24853. + z->total_in = z->total_out = 0;
  24854. + z->msg = Z_NULL;
  24855. + z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  24856. + inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  24857. + Tracev((stderr, "inflate: reset\n"));
  24858. + return Z_OK;
  24859. +}
  24860. +
  24861. +
  24862. +int ZEXPORT inflateEnd(z)
  24863. +z_streamp z;
  24864. +{
  24865. + if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  24866. + return Z_STREAM_ERROR;
  24867. + if (z->state->blocks != Z_NULL)
  24868. + inflate_blocks_free(z->state->blocks, z);
  24869. + ZFREE(z, z->state);
  24870. + z->state = Z_NULL;
  24871. + Tracev((stderr, "inflate: end\n"));
  24872. + return Z_OK;
  24873. +}
  24874. +
  24875. +
  24876. +int ZEXPORT inflateInit2_(z, w, version, stream_size)
  24877. +z_streamp z;
  24878. +int w;
  24879. +const char *version;
  24880. +int stream_size;
  24881. +{
  24882. + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  24883. + stream_size != sizeof(z_stream))
  24884. + return Z_VERSION_ERROR;
  24885. +
  24886. + /* initialize state */
  24887. + if (z == Z_NULL)
  24888. + return Z_STREAM_ERROR;
  24889. + z->msg = Z_NULL;
  24890. + if (z->zalloc == Z_NULL)
  24891. + {
  24892. + return Z_STREAM_ERROR;
  24893. +/* z->zalloc = zcalloc;
  24894. + z->opaque = (voidpf)0;
  24895. +*/
  24896. + }
  24897. + if (z->zfree == Z_NULL) return Z_STREAM_ERROR; /* z->zfree = zcfree; */
  24898. + if ((z->state = (struct internal_state FAR *)
  24899. + ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  24900. + return Z_MEM_ERROR;
  24901. + z->state->blocks = Z_NULL;
  24902. +
  24903. + /* handle undocumented nowrap option (no zlib header or check) */
  24904. + z->state->nowrap = 0;
  24905. + if (w < 0)
  24906. + {
  24907. + w = - w;
  24908. + z->state->nowrap = 1;
  24909. + }
  24910. +
  24911. + /* set window size */
  24912. + if (w < 8 || w > 15)
  24913. + {
  24914. + inflateEnd(z);
  24915. + return Z_STREAM_ERROR;
  24916. + }
  24917. + z->state->wbits = (uInt)w;
  24918. +
  24919. + /* create inflate_blocks state */
  24920. + if ((z->state->blocks =
  24921. + inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  24922. + == Z_NULL)
  24923. + {
  24924. + inflateEnd(z);
  24925. + return Z_MEM_ERROR;
  24926. + }
  24927. + Tracev((stderr, "inflate: allocated\n"));
  24928. +
  24929. + /* reset state */
  24930. + inflateReset(z);
  24931. + return Z_OK;
  24932. +}
  24933. +
  24934. +
  24935. +int ZEXPORT inflateInit_(z, version, stream_size)
  24936. +z_streamp z;
  24937. +const char *version;
  24938. +int stream_size;
  24939. +{
  24940. + return inflateInit2_(z, DEF_WBITS, version, stream_size);
  24941. +}
  24942. +
  24943. +
  24944. +#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  24945. +#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  24946. +
  24947. +int ZEXPORT inflate(z, f)
  24948. +z_streamp z;
  24949. +int f;
  24950. +{
  24951. + int r;
  24952. + uInt b;
  24953. +
  24954. + if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  24955. + return Z_STREAM_ERROR;
  24956. + f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  24957. + r = Z_BUF_ERROR;
  24958. + while (1) switch (z->state->mode)
  24959. + {
  24960. + case METHOD:
  24961. + NEEDBYTE
  24962. + if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  24963. + {
  24964. + z->state->mode = BAD;
  24965. + z->msg = (char*)"unknown compression method";
  24966. + z->state->sub.marker = 5; /* can't try inflateSync */
  24967. + break;
  24968. + }
  24969. + if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  24970. + {
  24971. + z->state->mode = BAD;
  24972. + z->msg = (char*)"invalid window size";
  24973. + z->state->sub.marker = 5; /* can't try inflateSync */
  24974. + break;
  24975. + }
  24976. + z->state->mode = FLAG;
  24977. + case FLAG:
  24978. + NEEDBYTE
  24979. + b = NEXTBYTE;
  24980. + if (((z->state->sub.method << 8) + b) % 31)
  24981. + {
  24982. + z->state->mode = BAD;
  24983. + z->msg = (char*)"incorrect header check";
  24984. + z->state->sub.marker = 5; /* can't try inflateSync */
  24985. + break;
  24986. + }
  24987. + Tracev((stderr, "inflate: zlib header ok\n"));
  24988. + if (!(b & PRESET_DICT))
  24989. + {
  24990. + z->state->mode = BLOCKS;
  24991. + break;
  24992. + }
  24993. + z->state->mode = DICT4;
  24994. + case DICT4:
  24995. + NEEDBYTE
  24996. + z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  24997. + z->state->mode = DICT3;
  24998. + case DICT3:
  24999. + NEEDBYTE
  25000. + z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  25001. + z->state->mode = DICT2;
  25002. + case DICT2:
  25003. + NEEDBYTE
  25004. + z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  25005. + z->state->mode = DICT1;
  25006. + case DICT1:
  25007. + NEEDBYTE
  25008. + z->state->sub.check.need += (uLong)NEXTBYTE;
  25009. + z->adler = z->state->sub.check.need;
  25010. + z->state->mode = DICT0;
  25011. + return Z_NEED_DICT;
  25012. + case DICT0:
  25013. + z->state->mode = BAD;
  25014. + z->msg = (char*)"need dictionary";
  25015. + z->state->sub.marker = 0; /* can try inflateSync */
  25016. + return Z_STREAM_ERROR;
  25017. + case BLOCKS:
  25018. + r = inflate_blocks(z->state->blocks, z, r);
  25019. + if (r == Z_DATA_ERROR)
  25020. + {
  25021. + z->state->mode = BAD;
  25022. + z->state->sub.marker = 0; /* can try inflateSync */
  25023. + break;
  25024. + }
  25025. + if (r == Z_OK)
  25026. + r = f;
  25027. + if (r != Z_STREAM_END)
  25028. + return r;
  25029. + r = f;
  25030. + inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  25031. + if (z->state->nowrap)
  25032. + {
  25033. + z->state->mode = DONE;
  25034. + break;
  25035. + }
  25036. + z->state->mode = CHECK4;
  25037. + case CHECK4:
  25038. + NEEDBYTE
  25039. + z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  25040. + z->state->mode = CHECK3;
  25041. + case CHECK3:
  25042. + NEEDBYTE
  25043. + z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  25044. + z->state->mode = CHECK2;
  25045. + case CHECK2:
  25046. + NEEDBYTE
  25047. + z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  25048. + z->state->mode = CHECK1;
  25049. + case CHECK1:
  25050. + NEEDBYTE
  25051. + z->state->sub.check.need += (uLong)NEXTBYTE;
  25052. +
  25053. + if (z->state->sub.check.was != z->state->sub.check.need)
  25054. + {
  25055. + z->state->mode = BAD;
  25056. + z->msg = (char*)"incorrect data check";
  25057. + z->state->sub.marker = 5; /* can't try inflateSync */
  25058. + break;
  25059. + }
  25060. + Tracev((stderr, "inflate: zlib check ok\n"));
  25061. + z->state->mode = DONE;
  25062. + case DONE:
  25063. + return Z_STREAM_END;
  25064. + case BAD:
  25065. + return Z_DATA_ERROR;
  25066. + default:
  25067. + return Z_STREAM_ERROR;
  25068. + }
  25069. +#ifdef NEED_DUMMY_RETURN
  25070. + return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  25071. +#endif
  25072. +}
  25073. +
  25074. +
  25075. +int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
  25076. +z_streamp z;
  25077. +const Bytef *dictionary;
  25078. +uInt dictLength;
  25079. +{
  25080. + uInt length = dictLength;
  25081. +
  25082. + if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
  25083. + return Z_STREAM_ERROR;
  25084. +
  25085. + if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
  25086. + z->adler = 1L;
  25087. +
  25088. + if (length >= ((uInt)1<<z->state->wbits))
  25089. + {
  25090. + length = (1<<z->state->wbits)-1;
  25091. + dictionary += dictLength - length;
  25092. + }
  25093. + inflate_set_dictionary(z->state->blocks, dictionary, length);
  25094. + z->state->mode = BLOCKS;
  25095. + return Z_OK;
  25096. +}
  25097. +
  25098. +
  25099. +int ZEXPORT inflateSync(z)
  25100. +z_streamp z;
  25101. +{
  25102. + uInt n; /* number of bytes to look at */
  25103. + Bytef *p; /* pointer to bytes */
  25104. + uInt m; /* number of marker bytes found in a row */
  25105. + uLong r, w; /* temporaries to save total_in and total_out */
  25106. +
  25107. + /* set up */
  25108. + if (z == Z_NULL || z->state == Z_NULL)
  25109. + return Z_STREAM_ERROR;
  25110. + if (z->state->mode != BAD)
  25111. + {
  25112. + z->state->mode = BAD;
  25113. + z->state->sub.marker = 0;
  25114. + }
  25115. + if ((n = z->avail_in) == 0)
  25116. + return Z_BUF_ERROR;
  25117. + p = z->next_in;
  25118. + m = z->state->sub.marker;
  25119. +
  25120. + /* search */
  25121. + while (n && m < 4)
  25122. + {
  25123. + static const Byte mark[4] = {0, 0, 0xff, 0xff};
  25124. + if (*p == mark[m])
  25125. + m++;
  25126. + else if (*p)
  25127. + m = 0;
  25128. + else
  25129. + m = 4 - m;
  25130. + p++, n--;
  25131. + }
  25132. +
  25133. + /* restore */
  25134. + z->total_in += p - z->next_in;
  25135. + z->next_in = p;
  25136. + z->avail_in = n;
  25137. + z->state->sub.marker = m;
  25138. +
  25139. + /* return no joy or set up to restart on a new block */
  25140. + if (m != 4)
  25141. + return Z_DATA_ERROR;
  25142. + r = z->total_in; w = z->total_out;
  25143. + inflateReset(z);
  25144. + z->total_in = r; z->total_out = w;
  25145. + z->state->mode = BLOCKS;
  25146. + return Z_OK;
  25147. +}
  25148. +
  25149. +
  25150. +/* Returns true if inflate is currently at the end of a block generated
  25151. + * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  25152. + * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  25153. + * but removes the length bytes of the resulting empty stored block. When
  25154. + * decompressing, PPP checks that at the end of input packet, inflate is
  25155. + * waiting for these length bytes.
  25156. + */
  25157. +int ZEXPORT inflateSyncPoint(z)
  25158. +z_streamp z;
  25159. +{
  25160. + if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
  25161. + return Z_STREAM_ERROR;
  25162. + return inflate_blocks_sync_point(z->state->blocks);
  25163. +}
  25164. --- /dev/null Tue Mar 11 13:02:56 2003
  25165. +++ linux/net/ipsec/inftrees.c Mon Feb 9 13:51:03 2004
  25166. @@ -0,0 +1,454 @@
  25167. +/* inftrees.c -- generate Huffman trees for efficient decoding
  25168. + * Copyright (C) 1995-2002 Mark Adler
  25169. + * For conditions of distribution and use, see copyright notice in zlib.h
  25170. + */
  25171. +
  25172. +#include <zlib/zutil.h>
  25173. +#include "inftrees.h"
  25174. +
  25175. +#if !defined(BUILDFIXED) && !defined(STDC)
  25176. +# define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
  25177. +#endif
  25178. +
  25179. +local const char inflate_copyright[] =
  25180. + " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
  25181. +/*
  25182. + If you use the zlib library in a product, an acknowledgment is welcome
  25183. + in the documentation of your product. If for some reason you cannot
  25184. + include such an acknowledgment, I would appreciate that you keep this
  25185. + copyright string in the executable of your product.
  25186. + */
  25187. +struct internal_state {int dummy;}; /* for buggy compilers */
  25188. +
  25189. +/* simplify the use of the inflate_huft type with some defines */
  25190. +#define exop word.what.Exop
  25191. +#define bits word.what.Bits
  25192. +
  25193. +
  25194. +local int huft_build OF((
  25195. + uIntf *, /* code lengths in bits */
  25196. + uInt, /* number of codes */
  25197. + uInt, /* number of "simple" codes */
  25198. + const uIntf *, /* list of base values for non-simple codes */
  25199. + const uIntf *, /* list of extra bits for non-simple codes */
  25200. + inflate_huft * FAR*,/* result: starting table */
  25201. + uIntf *, /* maximum lookup bits (returns actual) */
  25202. + inflate_huft *, /* space for trees */
  25203. + uInt *, /* hufts used in space */
  25204. + uIntf * )); /* space for values */
  25205. +
  25206. +/* Tables for deflate from PKZIP's appnote.txt. */
  25207. +local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
  25208. + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  25209. + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  25210. + /* see note #13 above about 258 */
  25211. +local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
  25212. + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  25213. + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
  25214. +local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
  25215. + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  25216. + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  25217. + 8193, 12289, 16385, 24577};
  25218. +local const uInt cpdext[30] = { /* Extra bits for distance codes */
  25219. + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  25220. + 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  25221. + 12, 12, 13, 13};
  25222. +
  25223. +/*
  25224. + Huffman code decoding is performed using a multi-level table lookup.
  25225. + The fastest way to decode is to simply build a lookup table whose
  25226. + size is determined by the longest code. However, the time it takes
  25227. + to build this table can also be a factor if the data being decoded
  25228. + is not very long. The most common codes are necessarily the
  25229. + shortest codes, so those codes dominate the decoding time, and hence
  25230. + the speed. The idea is you can have a shorter table that decodes the
  25231. + shorter, more probable codes, and then point to subsidiary tables for
  25232. + the longer codes. The time it costs to decode the longer codes is
  25233. + then traded against the time it takes to make longer tables.
  25234. +
  25235. + This results of this trade are in the variables lbits and dbits
  25236. + below. lbits is the number of bits the first level table for literal/
  25237. + length codes can decode in one step, and dbits is the same thing for
  25238. + the distance codes. Subsequent tables are also less than or equal to
  25239. + those sizes. These values may be adjusted either when all of the
  25240. + codes are shorter than that, in which case the longest code length in
  25241. + bits is used, or when the shortest code is *longer* than the requested
  25242. + table size, in which case the length of the shortest code in bits is
  25243. + used.
  25244. +
  25245. + There are two different values for the two tables, since they code a
  25246. + different number of possibilities each. The literal/length table
  25247. + codes 286 possible values, or in a flat code, a little over eight
  25248. + bits. The distance table codes 30 possible values, or a little less
  25249. + than five bits, flat. The optimum values for speed end up being
  25250. + about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  25251. + The optimum values may differ though from machine to machine, and
  25252. + possibly even between compilers. Your mileage may vary.
  25253. + */
  25254. +
  25255. +
  25256. +/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
  25257. +#define BMAX 15 /* maximum bit length of any code */
  25258. +
  25259. +local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
  25260. +uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
  25261. +uInt n; /* number of codes (assumed <= 288) */
  25262. +uInt s; /* number of simple-valued codes (0..s-1) */
  25263. +const uIntf *d; /* list of base values for non-simple codes */
  25264. +const uIntf *e; /* list of extra bits for non-simple codes */
  25265. +inflate_huft * FAR *t; /* result: starting table */
  25266. +uIntf *m; /* maximum lookup bits, returns actual */
  25267. +inflate_huft *hp; /* space for trees */
  25268. +uInt *hn; /* hufts used in space */
  25269. +uIntf *v; /* working area: values in order of bit length */
  25270. +/* Given a list of code lengths and a maximum table size, make a set of
  25271. + tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
  25272. + if the given code set is incomplete (the tables are still built in this
  25273. + case), or Z_DATA_ERROR if the input is invalid. */
  25274. +{
  25275. +
  25276. + uInt a; /* counter for codes of length k */
  25277. + uInt c[BMAX+1]; /* bit length count table */
  25278. + uInt f; /* i repeats in table every f entries */
  25279. + int g; /* maximum code length */
  25280. + int h; /* table level */
  25281. + register uInt i; /* counter, current code */
  25282. + register uInt j; /* counter */
  25283. + register int k; /* number of bits in current code */
  25284. + int l; /* bits per table (returned in m) */
  25285. + uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */
  25286. + register uIntf *p; /* pointer into c[], b[], or v[] */
  25287. + inflate_huft *q; /* points to current table */
  25288. + struct inflate_huft_s r; /* table entry for structure assignment */
  25289. + inflate_huft *u[BMAX]; /* table stack */
  25290. + register int w; /* bits before this table == (l * h) */
  25291. + uInt x[BMAX+1]; /* bit offsets, then code stack */
  25292. + uIntf *xp; /* pointer into x */
  25293. + int y; /* number of dummy codes added */
  25294. + uInt z; /* number of entries in current table */
  25295. +
  25296. +
  25297. + /* Generate counts for each bit length */
  25298. + p = c;
  25299. +#define C0 *p++ = 0;
  25300. +#define C2 C0 C0 C0 C0
  25301. +#define C4 C2 C2 C2 C2
  25302. + C4 /* clear c[]--assume BMAX+1 is 16 */
  25303. + p = b; i = n;
  25304. + do {
  25305. + c[*p++]++; /* assume all entries <= BMAX */
  25306. + } while (--i);
  25307. + if (c[0] == n) /* null input--all zero length codes */
  25308. + {
  25309. + *t = (inflate_huft *)Z_NULL;
  25310. + *m = 0;
  25311. + return Z_OK;
  25312. + }
  25313. +
  25314. +
  25315. + /* Find minimum and maximum length, bound *m by those */
  25316. + l = *m;
  25317. + for (j = 1; j <= BMAX; j++)
  25318. + if (c[j])
  25319. + break;
  25320. + k = j; /* minimum code length */
  25321. + if ((uInt)l < j)
  25322. + l = j;
  25323. + for (i = BMAX; i; i--)
  25324. + if (c[i])
  25325. + break;
  25326. + g = i; /* maximum code length */
  25327. + if ((uInt)l > i)
  25328. + l = i;
  25329. + *m = l;
  25330. +
  25331. +
  25332. + /* Adjust last length count to fill out codes, if needed */
  25333. + for (y = 1 << j; j < i; j++, y <<= 1)
  25334. + if ((y -= c[j]) < 0)
  25335. + return Z_DATA_ERROR;
  25336. + if ((y -= c[i]) < 0)
  25337. + return Z_DATA_ERROR;
  25338. + c[i] += y;
  25339. +
  25340. +
  25341. + /* Generate starting offsets into the value table for each length */
  25342. + x[1] = j = 0;
  25343. + p = c + 1; xp = x + 2;
  25344. + while (--i) { /* note that i == g from above */
  25345. + *xp++ = (j += *p++);
  25346. + }
  25347. +
  25348. +
  25349. + /* Make a table of values in order of bit lengths */
  25350. + p = b; i = 0;
  25351. + do {
  25352. + if ((j = *p++) != 0)
  25353. + v[x[j]++] = i;
  25354. + } while (++i < n);
  25355. + n = x[g]; /* set n to length of v */
  25356. +
  25357. +
  25358. + /* Generate the Huffman codes and for each, make the table entries */
  25359. + x[0] = i = 0; /* first Huffman code is zero */
  25360. + p = v; /* grab values in bit order */
  25361. + h = -1; /* no tables yet--level -1 */
  25362. + w = -l; /* bits decoded == (l * h) */
  25363. + u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
  25364. + q = (inflate_huft *)Z_NULL; /* ditto */
  25365. + z = 0; /* ditto */
  25366. +
  25367. + /* go through the bit lengths (k already is bits in shortest code) */
  25368. + for (; k <= g; k++)
  25369. + {
  25370. + a = c[k];
  25371. + while (a--)
  25372. + {
  25373. + /* here i is the Huffman code of length k bits for value *p */
  25374. + /* make tables up to required level */
  25375. + while (k > w + l)
  25376. + {
  25377. + h++;
  25378. + w += l; /* previous table always l bits */
  25379. +
  25380. + /* compute minimum size table less than or equal to l bits */
  25381. + z = g - w;
  25382. + z = z > (uInt)l ? l : z; /* table size upper limit */
  25383. + if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  25384. + { /* too few codes for k-w bit table */
  25385. + f -= a + 1; /* deduct codes from patterns left */
  25386. + xp = c + k;
  25387. + if (j < z)
  25388. + while (++j < z) /* try smaller tables up to z bits */
  25389. + {
  25390. + if ((f <<= 1) <= *++xp)
  25391. + break; /* enough codes to use up j bits */
  25392. + f -= *xp; /* else deduct codes from patterns */
  25393. + }
  25394. + }
  25395. + z = 1 << j; /* table entries for j-bit table */
  25396. +
  25397. + /* allocate new table */
  25398. + if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
  25399. + return Z_DATA_ERROR; /* overflow of MANY */
  25400. + u[h] = q = hp + *hn;
  25401. + *hn += z;
  25402. +
  25403. + /* connect to last table, if there is one */
  25404. + if (h)
  25405. + {
  25406. + x[h] = i; /* save pattern for backing up */
  25407. + r.bits = (Byte)l; /* bits to dump before this table */
  25408. + r.exop = (Byte)j; /* bits in this table */
  25409. + j = i >> (w - l);
  25410. + r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
  25411. + u[h-1][j] = r; /* connect to last table */
  25412. + }
  25413. + else
  25414. + *t = q; /* first table is returned result */
  25415. + }
  25416. +
  25417. + /* set up table entry in r */
  25418. + r.bits = (Byte)(k - w);
  25419. + if (p >= v + n)
  25420. + r.exop = 128 + 64; /* out of values--invalid code */
  25421. + else if (*p < s)
  25422. + {
  25423. + r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
  25424. + r.base = *p++; /* simple code is just the value */
  25425. + }
  25426. + else
  25427. + {
  25428. + r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
  25429. + r.base = d[*p++ - s];
  25430. + }
  25431. +
  25432. + /* fill code-like entries with r */
  25433. + f = 1 << (k - w);
  25434. + for (j = i >> w; j < z; j += f)
  25435. + q[j] = r;
  25436. +
  25437. + /* backwards increment the k-bit code i */
  25438. + for (j = 1 << (k - 1); i & j; j >>= 1)
  25439. + i ^= j;
  25440. + i ^= j;
  25441. +
  25442. + /* backup over finished tables */
  25443. + mask = (1 << w) - 1; /* needed on HP, cc -O bug */
  25444. + while ((i & mask) != x[h])
  25445. + {
  25446. + h--; /* don't need to update q */
  25447. + w -= l;
  25448. + mask = (1 << w) - 1;
  25449. + }
  25450. + }
  25451. + }
  25452. +
  25453. +
  25454. + /* Return Z_BUF_ERROR if we were given an incomplete table */
  25455. + return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  25456. +}
  25457. +
  25458. +
  25459. +int inflate_trees_bits(c, bb, tb, hp, z)
  25460. +uIntf *c; /* 19 code lengths */
  25461. +uIntf *bb; /* bits tree desired/actual depth */
  25462. +inflate_huft * FAR *tb; /* bits tree result */
  25463. +inflate_huft *hp; /* space for trees */
  25464. +z_streamp z; /* for messages */
  25465. +{
  25466. + int r;
  25467. + uInt hn = 0; /* hufts used in space */
  25468. + uIntf *v; /* work area for huft_build */
  25469. +
  25470. + if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
  25471. + return Z_MEM_ERROR;
  25472. + r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
  25473. + tb, bb, hp, &hn, v);
  25474. + if (r == Z_DATA_ERROR)
  25475. + z->msg = (char*)"oversubscribed dynamic bit lengths tree";
  25476. + else if (r == Z_BUF_ERROR || *bb == 0)
  25477. + {
  25478. + z->msg = (char*)"incomplete dynamic bit lengths tree";
  25479. + r = Z_DATA_ERROR;
  25480. + }
  25481. + ZFREE(z, v);
  25482. + return r;
  25483. +}
  25484. +
  25485. +
  25486. +int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
  25487. +uInt nl; /* number of literal/length codes */
  25488. +uInt nd; /* number of distance codes */
  25489. +uIntf *c; /* that many (total) code lengths */
  25490. +uIntf *bl; /* literal desired/actual bit depth */
  25491. +uIntf *bd; /* distance desired/actual bit depth */
  25492. +inflate_huft * FAR *tl; /* literal/length tree result */
  25493. +inflate_huft * FAR *td; /* distance tree result */
  25494. +inflate_huft *hp; /* space for trees */
  25495. +z_streamp z; /* for messages */
  25496. +{
  25497. + int r;
  25498. + uInt hn = 0; /* hufts used in space */
  25499. + uIntf *v; /* work area for huft_build */
  25500. +
  25501. + /* allocate work area */
  25502. + if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25503. + return Z_MEM_ERROR;
  25504. +
  25505. + /* build literal/length tree */
  25506. + r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  25507. + if (r != Z_OK || *bl == 0)
  25508. + {
  25509. + if (r == Z_DATA_ERROR)
  25510. + z->msg = (char*)"oversubscribed literal/length tree";
  25511. + else if (r != Z_MEM_ERROR)
  25512. + {
  25513. + z->msg = (char*)"incomplete literal/length tree";
  25514. + r = Z_DATA_ERROR;
  25515. + }
  25516. + ZFREE(z, v);
  25517. + return r;
  25518. + }
  25519. +
  25520. + /* build distance tree */
  25521. + r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  25522. + if (r != Z_OK || (*bd == 0 && nl > 257))
  25523. + {
  25524. + if (r == Z_DATA_ERROR)
  25525. + z->msg = (char*)"oversubscribed distance tree";
  25526. + else if (r == Z_BUF_ERROR) {
  25527. +#ifdef PKZIP_BUG_WORKAROUND
  25528. + r = Z_OK;
  25529. + }
  25530. +#else
  25531. + z->msg = (char*)"incomplete distance tree";
  25532. + r = Z_DATA_ERROR;
  25533. + }
  25534. + else if (r != Z_MEM_ERROR)
  25535. + {
  25536. + z->msg = (char*)"empty distance tree with lengths";
  25537. + r = Z_DATA_ERROR;
  25538. + }
  25539. + ZFREE(z, v);
  25540. + return r;
  25541. +#endif
  25542. + }
  25543. +
  25544. + /* done */
  25545. + ZFREE(z, v);
  25546. + return Z_OK;
  25547. +}
  25548. +
  25549. +
  25550. +/* build fixed tables only once--keep them here */
  25551. +#ifdef BUILDFIXED
  25552. +local int fixed_built = 0;
  25553. +#define FIXEDH 544 /* number of hufts used by fixed tables */
  25554. +local inflate_huft fixed_mem[FIXEDH];
  25555. +local uInt fixed_bl;
  25556. +local uInt fixed_bd;
  25557. +local inflate_huft *fixed_tl;
  25558. +local inflate_huft *fixed_td;
  25559. +#else
  25560. +#include "inffixed.h"
  25561. +#endif
  25562. +
  25563. +
  25564. +int inflate_trees_fixed(bl, bd, tl, td, z)
  25565. +uIntf *bl; /* literal desired/actual bit depth */
  25566. +uIntf *bd; /* distance desired/actual bit depth */
  25567. +inflate_huft * FAR *tl; /* literal/length tree result */
  25568. +inflate_huft * FAR *td; /* distance tree result */
  25569. +z_streamp z; /* for memory allocation */
  25570. +{
  25571. +#ifdef BUILDFIXED
  25572. + /* build fixed tables if not already */
  25573. + if (!fixed_built)
  25574. + {
  25575. + int k; /* temporary variable */
  25576. + uInt f = 0; /* number of hufts used in fixed_mem */
  25577. + uIntf *c; /* length list for huft_build */
  25578. + uIntf *v; /* work area for huft_build */
  25579. +
  25580. + /* allocate memory */
  25581. + if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25582. + return Z_MEM_ERROR;
  25583. + if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25584. + {
  25585. + ZFREE(z, c);
  25586. + return Z_MEM_ERROR;
  25587. + }
  25588. +
  25589. + /* literal table */
  25590. + for (k = 0; k < 144; k++)
  25591. + c[k] = 8;
  25592. + for (; k < 256; k++)
  25593. + c[k] = 9;
  25594. + for (; k < 280; k++)
  25595. + c[k] = 7;
  25596. + for (; k < 288; k++)
  25597. + c[k] = 8;
  25598. + fixed_bl = 9;
  25599. + huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
  25600. + fixed_mem, &f, v);
  25601. +
  25602. + /* distance table */
  25603. + for (k = 0; k < 30; k++)
  25604. + c[k] = 5;
  25605. + fixed_bd = 5;
  25606. + huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
  25607. + fixed_mem, &f, v);
  25608. +
  25609. + /* done */
  25610. + ZFREE(z, v);
  25611. + ZFREE(z, c);
  25612. + fixed_built = 1;
  25613. + }
  25614. +#endif
  25615. + *bl = fixed_bl;
  25616. + *bd = fixed_bd;
  25617. + *tl = fixed_tl;
  25618. + *td = fixed_td;
  25619. + return Z_OK;
  25620. +}
  25621. --- /dev/null Tue Mar 11 13:02:56 2003
  25622. +++ linux/net/ipsec/inftrees.h Mon Feb 9 13:51:03 2004
  25623. @@ -0,0 +1,63 @@
  25624. +/* inftrees.h -- header to use inftrees.c
  25625. + * Copyright (C) 1995-2002 Mark Adler
  25626. + * For conditions of distribution and use, see copyright notice in zlib.h
  25627. + */
  25628. +
  25629. +/* WARNING: this file should *not* be used by applications. It is
  25630. + part of the implementation of the compression library and is
  25631. + subject to change. Applications should only use zlib.h.
  25632. + */
  25633. +
  25634. +/* Huffman code lookup table entry--this entry is four bytes for machines
  25635. + that have 16-bit pointers (e.g. PC's in the small or medium model). */
  25636. +
  25637. +#ifndef _INFTREES_H
  25638. +#define _INFTREES_H
  25639. +
  25640. +typedef struct inflate_huft_s FAR inflate_huft;
  25641. +
  25642. +struct inflate_huft_s {
  25643. + union {
  25644. + struct {
  25645. + Byte Exop; /* number of extra bits or operation */
  25646. + Byte Bits; /* number of bits in this code or subcode */
  25647. + } what;
  25648. + uInt pad; /* pad structure to a power of 2 (4 bytes for */
  25649. + } word; /* 16-bit, 8 bytes for 32-bit int's) */
  25650. + uInt base; /* literal, length base, distance base,
  25651. + or table offset */
  25652. +};
  25653. +
  25654. +/* Maximum size of dynamic tree. The maximum found in a long but non-
  25655. + exhaustive search was 1004 huft structures (850 for length/literals
  25656. + and 154 for distances, the latter actually the result of an
  25657. + exhaustive search). The actual maximum is not known, but the
  25658. + value below is more than safe. */
  25659. +#define MANY 1440
  25660. +
  25661. +extern int inflate_trees_bits OF((
  25662. + uIntf *, /* 19 code lengths */
  25663. + uIntf *, /* bits tree desired/actual depth */
  25664. + inflate_huft * FAR *, /* bits tree result */
  25665. + inflate_huft *, /* space for trees */
  25666. + z_streamp)); /* for messages */
  25667. +
  25668. +extern int inflate_trees_dynamic OF((
  25669. + uInt, /* number of literal/length codes */
  25670. + uInt, /* number of distance codes */
  25671. + uIntf *, /* that many (total) code lengths */
  25672. + uIntf *, /* literal desired/actual bit depth */
  25673. + uIntf *, /* distance desired/actual bit depth */
  25674. + inflate_huft * FAR *, /* literal/length tree result */
  25675. + inflate_huft * FAR *, /* distance tree result */
  25676. + inflate_huft *, /* space for trees */
  25677. + z_streamp)); /* for messages */
  25678. +
  25679. +extern int inflate_trees_fixed OF((
  25680. + uIntf *, /* literal desired/actual bit depth */
  25681. + uIntf *, /* distance desired/actual bit depth */
  25682. + inflate_huft * FAR *, /* literal/length tree result */
  25683. + inflate_huft * FAR *, /* distance tree result */
  25684. + z_streamp)); /* for memory allocation */
  25685. +
  25686. +#endif /* _INFTREES_H */
  25687. --- /dev/null Tue Mar 11 13:02:56 2003
  25688. +++ linux/net/ipsec/infutil.c Mon Feb 9 13:51:03 2004
  25689. @@ -0,0 +1,87 @@
  25690. +/* inflate_util.c -- data and routines common to blocks and codes
  25691. + * Copyright (C) 1995-2002 Mark Adler
  25692. + * For conditions of distribution and use, see copyright notice in zlib.h
  25693. + */
  25694. +
  25695. +#include <zlib/zutil.h>
  25696. +#include "infblock.h"
  25697. +#include "inftrees.h"
  25698. +#include "infcodes.h"
  25699. +#include "infutil.h"
  25700. +
  25701. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  25702. +
  25703. +/* And'ing with mask[n] masks the lower n bits */
  25704. +uInt inflate_mask[17] = {
  25705. + 0x0000,
  25706. + 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  25707. + 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  25708. +};
  25709. +
  25710. +
  25711. +/* copy as much as possible from the sliding window to the output area */
  25712. +int inflate_flush(s, z, r)
  25713. +inflate_blocks_statef *s;
  25714. +z_streamp z;
  25715. +int r;
  25716. +{
  25717. + uInt n;
  25718. + Bytef *p;
  25719. + Bytef *q;
  25720. +
  25721. + /* local copies of source and destination pointers */
  25722. + p = z->next_out;
  25723. + q = s->read;
  25724. +
  25725. + /* compute number of bytes to copy as far as end of window */
  25726. + n = (uInt)((q <= s->write ? s->write : s->end) - q);
  25727. + if (n > z->avail_out) n = z->avail_out;
  25728. + if (n && r == Z_BUF_ERROR) r = Z_OK;
  25729. +
  25730. + /* update counters */
  25731. + z->avail_out -= n;
  25732. + z->total_out += n;
  25733. +
  25734. + /* update check information */
  25735. + if (s->checkfn != Z_NULL)
  25736. + z->adler = s->check = (*s->checkfn)(s->check, q, n);
  25737. +
  25738. + /* copy as far as end of window */
  25739. + zmemcpy(p, q, n);
  25740. + p += n;
  25741. + q += n;
  25742. +
  25743. + /* see if more to copy at beginning of window */
  25744. + if (q == s->end)
  25745. + {
  25746. + /* wrap pointers */
  25747. + q = s->window;
  25748. + if (s->write == s->end)
  25749. + s->write = s->window;
  25750. +
  25751. + /* compute bytes to copy */
  25752. + n = (uInt)(s->write - q);
  25753. + if (n > z->avail_out) n = z->avail_out;
  25754. + if (n && r == Z_BUF_ERROR) r = Z_OK;
  25755. +
  25756. + /* update counters */
  25757. + z->avail_out -= n;
  25758. + z->total_out += n;
  25759. +
  25760. + /* update check information */
  25761. + if (s->checkfn != Z_NULL)
  25762. + z->adler = s->check = (*s->checkfn)(s->check, q, n);
  25763. +
  25764. + /* copy */
  25765. + zmemcpy(p, q, n);
  25766. + p += n;
  25767. + q += n;
  25768. + }
  25769. +
  25770. + /* update pointers */
  25771. + z->next_out = p;
  25772. + s->read = q;
  25773. +
  25774. + /* done */
  25775. + return r;
  25776. +}
  25777. --- /dev/null Tue Mar 11 13:02:56 2003
  25778. +++ linux/net/ipsec/infutil.h Mon Feb 9 13:51:03 2004
  25779. @@ -0,0 +1,98 @@
  25780. +/* infutil.h -- types and macros common to blocks and codes
  25781. + * Copyright (C) 1995-2002 Mark Adler
  25782. + * For conditions of distribution and use, see copyright notice in zlib.h
  25783. + */
  25784. +
  25785. +/* WARNING: this file should *not* be used by applications. It is
  25786. + part of the implementation of the compression library and is
  25787. + subject to change. Applications should only use zlib.h.
  25788. + */
  25789. +
  25790. +#ifndef _INFUTIL_H
  25791. +#define _INFUTIL_H
  25792. +
  25793. +typedef enum {
  25794. + TYPE, /* get type bits (3, including end bit) */
  25795. + LENS, /* get lengths for stored */
  25796. + STORED, /* processing stored block */
  25797. + TABLE, /* get table lengths */
  25798. + BTREE, /* get bit lengths tree for a dynamic block */
  25799. + DTREE, /* get length, distance trees for a dynamic block */
  25800. + CODES, /* processing fixed or dynamic block */
  25801. + DRY, /* output remaining window bytes */
  25802. + DONE, /* finished last block, done */
  25803. + BAD} /* got a data error--stuck here */
  25804. +inflate_block_mode;
  25805. +
  25806. +/* inflate blocks semi-private state */
  25807. +struct inflate_blocks_state {
  25808. +
  25809. + /* mode */
  25810. + inflate_block_mode mode; /* current inflate_block mode */
  25811. +
  25812. + /* mode dependent information */
  25813. + union {
  25814. + uInt left; /* if STORED, bytes left to copy */
  25815. + struct {
  25816. + uInt table; /* table lengths (14 bits) */
  25817. + uInt index; /* index into blens (or border) */
  25818. + uIntf *blens; /* bit lengths of codes */
  25819. + uInt bb; /* bit length tree depth */
  25820. + inflate_huft *tb; /* bit length decoding tree */
  25821. + } trees; /* if DTREE, decoding info for trees */
  25822. + struct {
  25823. + inflate_codes_statef
  25824. + *codes;
  25825. + } decode; /* if CODES, current state */
  25826. + } sub; /* submode */
  25827. + uInt last; /* true if this block is the last block */
  25828. +
  25829. + /* mode independent information */
  25830. + uInt bitk; /* bits in bit buffer */
  25831. + uLong bitb; /* bit buffer */
  25832. + inflate_huft *hufts; /* single malloc for tree space */
  25833. + Bytef *window; /* sliding window */
  25834. + Bytef *end; /* one byte after sliding window */
  25835. + Bytef *read; /* window read pointer */
  25836. + Bytef *write; /* window write pointer */
  25837. + check_func checkfn; /* check function */
  25838. + uLong check; /* check on output */
  25839. +
  25840. +};
  25841. +
  25842. +
  25843. +/* defines for inflate input/output */
  25844. +/* update pointers and return */
  25845. +#define UPDBITS {s->bitb=b;s->bitk=k;}
  25846. +#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
  25847. +#define UPDOUT {s->write=q;}
  25848. +#define UPDATE {UPDBITS UPDIN UPDOUT}
  25849. +#define LEAVE {UPDATE return inflate_flush(s,z,r);}
  25850. +/* get bytes and bits */
  25851. +#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  25852. +#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
  25853. +#define NEXTBYTE (n--,*p++)
  25854. +#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  25855. +#define DUMPBITS(j) {b>>=(j);k-=(j);}
  25856. +/* output bytes */
  25857. +#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  25858. +#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
  25859. +#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  25860. +#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
  25861. +#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
  25862. +#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  25863. +/* load local pointers */
  25864. +#define LOAD {LOADIN LOADOUT}
  25865. +
  25866. +/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
  25867. +extern uInt inflate_mask[17];
  25868. +
  25869. +/* copy as much as possible from the sliding window to the output area */
  25870. +extern int inflate_flush OF((
  25871. + inflate_blocks_statef *,
  25872. + z_streamp ,
  25873. + int));
  25874. +
  25875. +struct internal_state {int dummy;}; /* for buggy compilers */
  25876. +
  25877. +#endif /* _INFUTIL_H */
  25878. --- /dev/null Tue Mar 11 13:02:56 2003
  25879. +++ linux/net/ipsec/initaddr.c Mon Feb 9 13:51:03 2004
  25880. @@ -0,0 +1,50 @@
  25881. +/*
  25882. + * initialize address structure
  25883. + * Copyright (C) 2000 Henry Spencer.
  25884. + *
  25885. + * This library is free software; you can redistribute it and/or modify it
  25886. + * under the terms of the GNU Library General Public License as published by
  25887. + * the Free Software Foundation; either version 2 of the License, or (at your
  25888. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  25889. + *
  25890. + * This library is distributed in the hope that it will be useful, but
  25891. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  25892. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  25893. + * License for more details.
  25894. + *
  25895. + * RCSID $Id: initaddr.c,v 1.6 2004/07/10 07:43:47 mcr Exp $
  25896. + */
  25897. +#include "openswan.h"
  25898. +
  25899. +/*
  25900. + - initaddr - initialize ip_address from bytes
  25901. + */
  25902. +err_t /* NULL for success, else string literal */
  25903. +initaddr(src, srclen, af, dst)
  25904. +const unsigned char *src;
  25905. +size_t srclen;
  25906. +int af; /* address family */
  25907. +ip_address *dst;
  25908. +{
  25909. + switch (af) {
  25910. + case AF_INET:
  25911. + if (srclen != 4)
  25912. + return "IPv4 address must be exactly 4 bytes";
  25913. + dst->u.v4.sin_family = af;
  25914. + dst->u.v4.sin_port = 0; /* unused */
  25915. + memcpy((char *)&dst->u.v4.sin_addr.s_addr, src, srclen);
  25916. + break;
  25917. + case AF_INET6:
  25918. + if (srclen != 16)
  25919. + return "IPv6 address must be exactly 16 bytes";
  25920. + dst->u.v6.sin6_family = af;
  25921. + dst->u.v6.sin6_flowinfo = 0; /* unused */
  25922. + dst->u.v6.sin6_port = 0; /* unused */
  25923. + memcpy((char *)&dst->u.v6.sin6_addr, src, srclen);
  25924. + break;
  25925. + default:
  25926. + return "unknown address family in initaddr";
  25927. + break;
  25928. + }
  25929. + return NULL;
  25930. +}
  25931. --- /dev/null Tue Mar 11 13:02:56 2003
  25932. +++ linux/net/ipsec/ipcomp.c Mon Feb 9 13:51:03 2004
  25933. @@ -0,0 +1,701 @@
  25934. +/*
  25935. + * IPCOMP zlib interface code.
  25936. + * Copyright (C) 2000 Svenning Soerensen <svenning@post5.tele.dk>
  25937. + * Copyright (C) 2000, 2001 Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  25938. + *
  25939. + * This program is free software; you can redistribute it and/or modify it
  25940. + * under the terms of the GNU General Public License as published by the
  25941. + * Free Software Foundation; either version 2 of the License, or (at your
  25942. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  25943. + *
  25944. + * This program is distributed in the hope that it will be useful, but
  25945. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  25946. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  25947. + * for more details.
  25948. + */
  25949. +
  25950. +char ipcomp_c_version[] = "RCSID $Id: ipcomp.c,v 1.41.2.5 2006/10/06 21:39:26 paul Exp $";
  25951. +
  25952. +/* SSS */
  25953. +
  25954. +#ifndef AUTOCONF_INCLUDED
  25955. +#include <linux/config.h>
  25956. +#endif
  25957. +#include <linux/version.h>
  25958. +
  25959. +#define __NO_VERSION__
  25960. +#include <linux/module.h>
  25961. +#include <linux/kernel.h> /* printk() */
  25962. +
  25963. +#include "openswan/ipsec_param.h"
  25964. +
  25965. +#ifdef MALLOC_SLAB
  25966. +# include <linux/slab.h> /* kmalloc() */
  25967. +#else /* MALLOC_SLAB */
  25968. +# include <linux/malloc.h> /* kmalloc() */
  25969. +#endif /* MALLOC_SLAB */
  25970. +#include <linux/errno.h> /* error codes */
  25971. +#include <linux/types.h>
  25972. +#include <linux/netdevice.h>
  25973. +#include <linux/ip.h>
  25974. +#include <linux/skbuff.h>
  25975. +
  25976. +#include <linux/netdevice.h> /* struct device, and other headers */
  25977. +#include <linux/etherdevice.h> /* eth_type_trans */
  25978. +#include <linux/ip.h> /* struct iphdr */
  25979. +#include <linux/skbuff.h>
  25980. +#include <asm/uaccess.h>
  25981. +#include <asm/checksum.h>
  25982. +
  25983. +#include <openswan.h>
  25984. +
  25985. +#include <net/ip.h>
  25986. +
  25987. +#include "openswan/radij.h"
  25988. +#include "openswan/ipsec_encap.h"
  25989. +#include "openswan/ipsec_sa.h"
  25990. +
  25991. +#include "openswan/ipsec_xform.h"
  25992. +#include "openswan/ipsec_tunnel.h"
  25993. +#include "openswan/ipsec_rcv.h" /* sysctl_ipsec_inbound_policy_check */
  25994. +#include "openswan/ipsec_proto.h"
  25995. +#include "openswan/ipcomp.h"
  25996. +#include "zlib/zlib.h"
  25997. +#include "zlib/zutil.h"
  25998. +
  25999. +#include <pfkeyv2.h> /* SADB_X_CALG_DEFLATE */
  26000. +
  26001. +#ifdef CONFIG_KLIPS_DEBUG
  26002. +int sysctl_ipsec_debug_ipcomp = 0;
  26003. +#endif /* CONFIG_KLIPS_DEBUG */
  26004. +
  26005. +static
  26006. +struct sk_buff *skb_copy_ipcomp(struct sk_buff *skb, int data_growth, int gfp_mask);
  26007. +
  26008. +static
  26009. +voidpf my_zcalloc(voidpf opaque, uInt items, uInt size)
  26010. +{
  26011. + return (voidpf) kmalloc(items*size, GFP_ATOMIC);
  26012. +}
  26013. +
  26014. +static
  26015. +void my_zfree(voidpf opaque, voidpf address)
  26016. +{
  26017. + kfree(address);
  26018. +}
  26019. +
  26020. +/*
  26021. + * We use this function because sometimes we want to pass a negative offset
  26022. + * into skb_put(), this does not work on 64bit platforms because long to
  26023. + * unsigned int casting.
  26024. + */
  26025. +static inline unsigned char *
  26026. +safe_skb_put(struct sk_buff *skb, int extend)
  26027. +{
  26028. + unsigned char *ptr;
  26029. +
  26030. + if (extend>0) {
  26031. + // increase the size of the packet
  26032. + ptr = skb_put(skb, extend);
  26033. + } else {
  26034. + // shrink the size of the packet
  26035. + ptr = skb->tail;
  26036. + skb_trim (skb, skb->len + extend);
  26037. + }
  26038. +
  26039. + return ptr;
  26040. +}
  26041. +
  26042. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags)
  26043. +{
  26044. + struct iphdr *iph;
  26045. + unsigned int iphlen, pyldsz, cpyldsz;
  26046. + unsigned char *buffer;
  26047. + z_stream zs;
  26048. + int zresult;
  26049. +
  26050. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26051. + "klips_debug:skb_compress: .\n");
  26052. +
  26053. + if(skb == NULL) {
  26054. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26055. + "klips_debug:skb_compress: "
  26056. + "passed in NULL skb, returning ERROR.\n");
  26057. + if(flags != NULL) {
  26058. + *flags |= IPCOMP_PARMERROR;
  26059. + }
  26060. + return skb;
  26061. + }
  26062. +
  26063. + if(ips == NULL) {
  26064. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26065. + "klips_debug:skb_compress: "
  26066. + "passed in NULL ipsec_sa needed for cpi, returning ERROR.\n");
  26067. + if(flags) {
  26068. + *flags |= IPCOMP_PARMERROR;
  26069. + }
  26070. + return skb;
  26071. + }
  26072. +
  26073. + if (flags == NULL) {
  26074. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26075. + "klips_debug:skb_compress: "
  26076. + "passed in NULL flags, returning ERROR.\n");
  26077. + ipsec_kfree_skb(skb);
  26078. + return NULL;
  26079. + }
  26080. +
  26081. +#ifdef NET_21
  26082. + iph = skb->nh.iph;
  26083. +#else /* NET_21 */
  26084. + iph = skb->ip_hdr;
  26085. +#endif /* NET_21 */
  26086. +
  26087. + switch (iph->protocol) {
  26088. + case IPPROTO_COMP:
  26089. + case IPPROTO_AH:
  26090. + case IPPROTO_ESP:
  26091. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26092. + "klips_debug:skb_compress: "
  26093. + "skipping compression of packet with ip protocol %d.\n",
  26094. + iph->protocol);
  26095. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26096. + return skb;
  26097. + }
  26098. +
  26099. + /* Don't compress packets already fragmented */
  26100. + if (iph->frag_off & __constant_htons(IP_MF | IP_OFFSET)) {
  26101. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26102. + "klips_debug:skb_compress: "
  26103. + "skipping compression of fragmented packet.\n");
  26104. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26105. + return skb;
  26106. + }
  26107. +
  26108. + iphlen = iph->ihl << 2;
  26109. + pyldsz = ntohs(iph->tot_len) - iphlen;
  26110. +
  26111. + /* Don't compress less than 90 bytes (rfc 2394) */
  26112. + if (pyldsz < 90) {
  26113. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26114. + "klips_debug:skb_compress: "
  26115. + "skipping compression of tiny packet, len=%d.\n",
  26116. + pyldsz);
  26117. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26118. + return skb;
  26119. + }
  26120. +
  26121. + /* Adaptive decision */
  26122. + if (ips->ips_comp_adapt_skip) {
  26123. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26124. + "klips_debug:skb_compress: "
  26125. + "skipping compression: ips_comp_adapt_skip=%d.\n",
  26126. + ips->ips_comp_adapt_skip);
  26127. + ips->ips_comp_adapt_skip--;
  26128. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26129. + return skb;
  26130. + }
  26131. +
  26132. + zs.zalloc = my_zcalloc;
  26133. + zs.zfree = my_zfree;
  26134. + zs.opaque = 0;
  26135. +
  26136. + /* We want to use deflateInit2 because we don't want the adler
  26137. + header. */
  26138. + zresult = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -11,
  26139. + DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  26140. + if (zresult != Z_OK) {
  26141. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26142. + "klips_error:skb_compress: "
  26143. + "deflateInit2() returned error %d (%s), "
  26144. + "skipping compression.\n",
  26145. + zresult,
  26146. + zs.msg ? zs.msg : zError(zresult));
  26147. + *flags |= IPCOMP_COMPRESSIONERROR;
  26148. + return skb;
  26149. + }
  26150. +
  26151. +
  26152. + /* Max output size. Result should be max this size.
  26153. + * Implementation specific tweak:
  26154. + * If it's not at least 32 bytes and 6.25% smaller than
  26155. + * the original packet, it's probably not worth wasting
  26156. + * the receiver's CPU cycles decompressing it.
  26157. + * Your mileage may vary.
  26158. + */
  26159. + cpyldsz = pyldsz - sizeof(struct ipcomphdr) - (pyldsz <= 512 ? 32 : pyldsz >> 4);
  26160. +
  26161. + buffer = kmalloc(cpyldsz, GFP_ATOMIC);
  26162. + if (!buffer) {
  26163. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26164. + "klips_error:skb_compress: "
  26165. + "unable to kmalloc(%d, GFP_ATOMIC), "
  26166. + "skipping compression.\n",
  26167. + cpyldsz);
  26168. + *flags |= IPCOMP_COMPRESSIONERROR;
  26169. + deflateEnd(&zs);
  26170. + return skb;
  26171. + }
  26172. +
  26173. +#ifdef CONFIG_KLIPS_DEBUG
  26174. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26175. + __u8 *c;
  26176. +
  26177. + c = (__u8*)iph + iphlen;
  26178. + ipsec_dmp_block("compress before", c, pyldsz);
  26179. + }
  26180. +#endif /* CONFIG_KLIPS_DEBUG */
  26181. +
  26182. + zs.next_in = (char *) iph + iphlen; /* start of payload */
  26183. + zs.avail_in = pyldsz;
  26184. + zs.next_out = buffer; /* start of compressed payload */
  26185. + zs.avail_out = cpyldsz;
  26186. +
  26187. + /* Finish compression in one step */
  26188. + zresult = deflate(&zs, Z_FINISH);
  26189. +
  26190. + /* Free all dynamically allocated buffers */
  26191. + deflateEnd(&zs);
  26192. + if (zresult != Z_STREAM_END) {
  26193. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26194. + kfree(buffer);
  26195. +
  26196. + /* Adjust adaptive counters */
  26197. + if (++(ips->ips_comp_adapt_tries) == IPCOMP_ADAPT_INITIAL_TRIES) {
  26198. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26199. + "klips_debug:skb_compress: "
  26200. + "first %d packets didn't compress, "
  26201. + "skipping next %d\n",
  26202. + IPCOMP_ADAPT_INITIAL_TRIES,
  26203. + IPCOMP_ADAPT_INITIAL_SKIP);
  26204. + ips->ips_comp_adapt_skip = IPCOMP_ADAPT_INITIAL_SKIP;
  26205. + }
  26206. + else if (ips->ips_comp_adapt_tries == IPCOMP_ADAPT_INITIAL_TRIES + IPCOMP_ADAPT_SUBSEQ_TRIES) {
  26207. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26208. + "klips_debug:skb_compress: "
  26209. + "next %d packets didn't compress, "
  26210. + "skipping next %d\n",
  26211. + IPCOMP_ADAPT_SUBSEQ_TRIES,
  26212. + IPCOMP_ADAPT_SUBSEQ_SKIP);
  26213. + ips->ips_comp_adapt_skip = IPCOMP_ADAPT_SUBSEQ_SKIP;
  26214. + ips->ips_comp_adapt_tries = IPCOMP_ADAPT_INITIAL_TRIES;
  26215. + }
  26216. +
  26217. + return skb;
  26218. + }
  26219. +
  26220. + /* resulting compressed size */
  26221. + cpyldsz -= zs.avail_out;
  26222. +
  26223. + /* Insert IPCOMP header */
  26224. + ((struct ipcomphdr*) ((char*) iph + iphlen))->ipcomp_nh = iph->protocol;
  26225. + ((struct ipcomphdr*) ((char*) iph + iphlen))->ipcomp_flags = 0;
  26226. + /* use the bottom 16 bits of the spi for the cpi. The top 16 bits are
  26227. + for internal reference only. */
  26228. + ((struct ipcomphdr*) (((char*)iph) + iphlen))->ipcomp_cpi = htons((__u16)(ntohl(ips->ips_said.spi) & 0x0000ffff));
  26229. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26230. + "klips_debug:skb_compress: "
  26231. + "spi=%08x, spi&0xffff=%04x, cpi=%04x, payload size: raw=%d, comp=%d.\n",
  26232. + ntohl(ips->ips_said.spi),
  26233. + ntohl(ips->ips_said.spi) & 0x0000ffff,
  26234. + ntohs(((struct ipcomphdr*)(((char*)iph)+iphlen))->ipcomp_cpi),
  26235. + pyldsz,
  26236. + cpyldsz);
  26237. +
  26238. + /* Update IP header */
  26239. + iph->protocol = IPPROTO_COMP;
  26240. + iph->tot_len = htons(iphlen + sizeof(struct ipcomphdr) + cpyldsz);
  26241. +#if 1 /* XXX checksum is done by ipsec_tunnel ? */
  26242. + iph->check = 0;
  26243. + iph->check = ip_fast_csum((char *) iph, iph->ihl);
  26244. +#endif
  26245. +
  26246. + /* Copy compressed payload */
  26247. + memcpy((char *) iph + iphlen + sizeof(struct ipcomphdr),
  26248. + buffer,
  26249. + cpyldsz);
  26250. + kfree(buffer);
  26251. +
  26252. + /* Update skb length/tail by "unputting" the shrinkage */
  26253. + safe_skb_put (skb, cpyldsz + sizeof(struct ipcomphdr) - pyldsz);
  26254. +
  26255. +#ifdef CONFIG_KLIPS_DEBUG
  26256. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26257. + __u8 *c;
  26258. +
  26259. + c = (__u8*)iph + iphlen + sizeof(struct ipcomphdr);
  26260. + ipsec_dmp_block("compress result", c, cpyldsz);
  26261. + }
  26262. +#endif /* CONFIG_KLIPS_DEBUG */
  26263. +
  26264. + ips->ips_comp_adapt_skip = 0;
  26265. + ips->ips_comp_adapt_tries = 0;
  26266. +
  26267. + return skb;
  26268. +}
  26269. +
  26270. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags)
  26271. +{
  26272. + struct sk_buff *nskb = NULL;
  26273. +
  26274. + /* original ip header */
  26275. + struct iphdr *oiph, *iph;
  26276. + unsigned int iphlen, pyldsz, cpyldsz;
  26277. + z_stream zs;
  26278. + int zresult;
  26279. +
  26280. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26281. + "klips_debug:skb_decompress: .\n");
  26282. +
  26283. + if(!skb) {
  26284. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26285. + "klips_error:skb_decompress: "
  26286. + "passed in NULL skb, returning ERROR.\n");
  26287. + if (flags) *flags |= IPCOMP_PARMERROR;
  26288. + return skb;
  26289. + }
  26290. +
  26291. + if(!ips && sysctl_ipsec_inbound_policy_check) {
  26292. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26293. + "klips_error:skb_decompress: "
  26294. + "passed in NULL ipsec_sa needed for comp alg, returning ERROR.\n");
  26295. + if (flags) *flags |= IPCOMP_PARMERROR;
  26296. + return skb;
  26297. + }
  26298. +
  26299. + if (!flags) {
  26300. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26301. + "klips_error:skb_decompress: "
  26302. + "passed in NULL flags, returning ERROR.\n");
  26303. + ipsec_kfree_skb(skb);
  26304. + return NULL;
  26305. + }
  26306. +
  26307. +#ifdef NET_21
  26308. + oiph = skb->nh.iph;
  26309. +#else /* NET_21 */
  26310. + oiph = skb->ip_hdr;
  26311. +#endif /* NET_21 */
  26312. +
  26313. + iphlen = oiph->ihl << 2;
  26314. +
  26315. + if (oiph->protocol != IPPROTO_COMP) {
  26316. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26317. + "klips_error:skb_decompress: "
  26318. + "called with non-IPCOMP packet (protocol=%d),"
  26319. + "skipping decompression.\n",
  26320. + oiph->protocol);
  26321. + *flags |= IPCOMP_PARMERROR;
  26322. + return skb;
  26323. + }
  26324. +
  26325. + if ( (((struct ipcomphdr*)((char*) oiph + iphlen))->ipcomp_flags != 0)
  26326. + || ((((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_cpi
  26327. + != htons(SADB_X_CALG_DEFLATE))
  26328. + && sysctl_ipsec_inbound_policy_check
  26329. + && (!ips || (ips && (ips->ips_encalg != SADB_X_CALG_DEFLATE)))) ) {
  26330. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26331. + "klips_error:skb_decompress: "
  26332. + "called with incompatible IPCOMP packet (flags=%d, "
  26333. + "cpi=%d), ips-compalg=%d, skipping decompression.\n",
  26334. + ntohs(((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_flags),
  26335. + ntohs(((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_cpi),
  26336. + ips ? ips->ips_encalg : 0);
  26337. + *flags |= IPCOMP_PARMERROR;
  26338. +
  26339. + return skb;
  26340. + }
  26341. +
  26342. + if (ntohs(oiph->frag_off) & ~0x4000) {
  26343. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26344. + "klips_error:skb_decompress: "
  26345. + "called with fragmented IPCOMP packet, "
  26346. + "skipping decompression.\n");
  26347. + *flags |= IPCOMP_PARMERROR;
  26348. + return skb;
  26349. + }
  26350. +
  26351. + /* original compressed payload size */
  26352. + cpyldsz = ntohs(oiph->tot_len) - iphlen - sizeof(struct ipcomphdr);
  26353. +
  26354. + zs.zalloc = my_zcalloc;
  26355. + zs.zfree = my_zfree;
  26356. + zs.opaque = 0;
  26357. +
  26358. + zs.next_in = (char *) oiph + iphlen + sizeof(struct ipcomphdr);
  26359. + zs.avail_in = cpyldsz;
  26360. +
  26361. + /* Maybe we should be a bit conservative about memory
  26362. + requirements and use inflateInit2 */
  26363. + /* Beware, that this might make us unable to decompress packets
  26364. + from other implementations - HINT: check PGPnet source code */
  26365. + /* We want to use inflateInit2 because we don't want the adler
  26366. + header. */
  26367. + zresult = inflateInit2(&zs, -15);
  26368. + if (zresult != Z_OK) {
  26369. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26370. + "klips_error:skb_decompress: "
  26371. + "inflateInit2() returned error %d (%s), "
  26372. + "skipping decompression.\n",
  26373. + zresult,
  26374. + zs.msg ? zs.msg : zError(zresult));
  26375. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26376. +
  26377. + return skb;
  26378. + }
  26379. +
  26380. + /* We have no way of knowing the exact length of the resulting
  26381. + decompressed output before we have actually done the decompression.
  26382. + For now, we guess that the packet will not be bigger than the
  26383. + attached ipsec device's mtu or 16260, whichever is biggest.
  26384. + This may be wrong, since the sender's mtu may be bigger yet.
  26385. + XXX This must be dealt with later XXX
  26386. + */
  26387. +
  26388. + /* max payload size */
  26389. + pyldsz = skb->dev ? (skb->dev->mtu < 16260 ? 16260 : skb->dev->mtu)
  26390. + : (65520 - iphlen);
  26391. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26392. + "klips_debug:skb_decompress: "
  26393. + "max payload size: %d\n", pyldsz);
  26394. +
  26395. + while (pyldsz > (cpyldsz + sizeof(struct ipcomphdr)) &&
  26396. + (nskb = skb_copy_ipcomp(skb,
  26397. + pyldsz - cpyldsz - sizeof(struct ipcomphdr),
  26398. + GFP_ATOMIC)) == NULL) {
  26399. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26400. + "klips_error:skb_decompress: "
  26401. + "unable to skb_copy_ipcomp(skb, %d, GFP_ATOMIC), "
  26402. + "trying with less payload size.\n",
  26403. + (int)(pyldsz - cpyldsz - sizeof(struct ipcomphdr)));
  26404. + pyldsz >>=1;
  26405. + }
  26406. +
  26407. + if (!nskb) {
  26408. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26409. + "klips_error:skb_decompress: "
  26410. + "unable to allocate memory, dropping packet.\n");
  26411. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26412. + inflateEnd(&zs);
  26413. +
  26414. + return skb;
  26415. + }
  26416. +
  26417. +#ifdef CONFIG_KLIPS_DEBUG
  26418. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26419. + __u8 *c;
  26420. +
  26421. + c = (__u8*)oiph + iphlen + sizeof(struct ipcomphdr);
  26422. + ipsec_dmp_block("decompress before", c, cpyldsz);
  26423. + }
  26424. +#endif /* CONFIG_KLIPS_DEBUG */
  26425. +
  26426. +#ifdef NET_21
  26427. + iph = nskb->nh.iph;
  26428. +#else /* NET_21 */
  26429. + iph = nskb->ip_hdr;
  26430. +#endif /* NET_21 */
  26431. + zs.next_out = (char *)iph + iphlen;
  26432. + zs.avail_out = pyldsz;
  26433. +
  26434. + zresult = inflate(&zs, Z_SYNC_FLUSH);
  26435. +
  26436. + /* work around a bug in zlib, which sometimes wants to taste an extra
  26437. + * byte when being used in the (undocumented) raw deflate mode.
  26438. + */
  26439. + if (zresult == Z_OK && !zs.avail_in && zs.avail_out) {
  26440. + __u8 zerostuff = 0;
  26441. +
  26442. + zs.next_in = &zerostuff;
  26443. + zs.avail_in = 1;
  26444. + zresult = inflate(&zs, Z_FINISH);
  26445. + }
  26446. +
  26447. + inflateEnd(&zs);
  26448. + if (zresult != Z_STREAM_END) {
  26449. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26450. + "klips_error:skb_decompress: "
  26451. + "inflate() returned error %d (%s), "
  26452. + "skipping decompression.\n",
  26453. + zresult,
  26454. + zs.msg ? zs.msg : zError(zresult));
  26455. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26456. + ipsec_kfree_skb(nskb);
  26457. +
  26458. + return skb;
  26459. + }
  26460. +
  26461. + /* Update IP header */
  26462. + /* resulting decompressed size */
  26463. + pyldsz -= zs.avail_out;
  26464. + iph->tot_len = htons(iphlen + pyldsz);
  26465. + iph->protocol = ((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_nh;
  26466. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26467. + "klips_debug:skb_decompress: "
  26468. + "spi=%08x, spi&0xffff=%04x, cpi=%04x, payload size: comp=%d, raw=%d, nh=%d.\n",
  26469. + ips ? ntohl(ips->ips_said.spi) : 0,
  26470. + ips ? ntohl(ips->ips_said.spi) & 0x0000ffff : 0,
  26471. + ntohs(((struct ipcomphdr*)(((char*)oiph)+iphlen))->ipcomp_cpi),
  26472. + cpyldsz,
  26473. + pyldsz,
  26474. + iph->protocol);
  26475. +
  26476. +#if 1 /* XXX checksum is done by ipsec_rcv ? */
  26477. + iph->check = 0;
  26478. + iph->check = ip_fast_csum((char*) iph, iph->ihl);
  26479. +#endif
  26480. +
  26481. + /* Update skb length/tail by "unputting" the unused data area */
  26482. + safe_skb_put(nskb, -zs.avail_out);
  26483. +
  26484. + ipsec_kfree_skb(skb);
  26485. +
  26486. + if (iph->protocol == IPPROTO_COMP)
  26487. + {
  26488. +#ifdef CONFIG_KLIPS_DEBUG
  26489. + if(sysctl_ipsec_debug_ipcomp)
  26490. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26491. + "klips_debug:skb_decompress: "
  26492. + "Eh? inner packet is also compressed, dropping.\n");
  26493. +#endif /* CONFIG_KLIPS_DEBUG */
  26494. +
  26495. + ipsec_kfree_skb(nskb);
  26496. + return NULL;
  26497. + }
  26498. +
  26499. +#ifdef CONFIG_KLIPS_DEBUG
  26500. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26501. + __u8 *c;
  26502. +
  26503. + c = (__u8*)iph + iphlen;
  26504. + ipsec_dmp_block("decompress result", c, pyldsz);
  26505. + }
  26506. +#endif /* CONFIG_KLIPS_DEBUG */
  26507. +
  26508. + return nskb;
  26509. +}
  26510. +
  26511. +
  26512. +/* this is derived from skb_copy() in linux 2.2.14 */
  26513. +/* May be incompatible with other kernel versions!! */
  26514. +static
  26515. +struct sk_buff *skb_copy_ipcomp(struct sk_buff *skb, int data_growth, int gfp_mask)
  26516. +{
  26517. + struct sk_buff *n;
  26518. + struct iphdr *iph;
  26519. + unsigned long offset;
  26520. + unsigned int iphlen;
  26521. +
  26522. + if(!skb) {
  26523. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26524. + "klips_debug:skb_copy_ipcomp: "
  26525. + "passed in NULL skb, returning NULL.\n");
  26526. + return NULL;
  26527. + }
  26528. +
  26529. + /*
  26530. + * Allocate the copy buffer
  26531. + */
  26532. +
  26533. +#ifdef NET_21
  26534. + iph = skb->nh.iph;
  26535. +#else /* NET_21 */
  26536. + iph = skb->ip_hdr;
  26537. +#endif /* NET_21 */
  26538. + if (!iph) return NULL;
  26539. + iphlen = iph->ihl << 2;
  26540. +
  26541. + n=alloc_skb(skb->end - skb->head + data_growth, gfp_mask);
  26542. + if(n==NULL)
  26543. + return NULL;
  26544. +
  26545. + /*
  26546. + * Shift between the two data areas in bytes
  26547. + */
  26548. +
  26549. + offset=n->head-skb->head;
  26550. +
  26551. + /* Set the data pointer */
  26552. + skb_reserve(n,skb->data-skb->head);
  26553. + /* Set the tail pointer and length */
  26554. + safe_skb_put(n,skb->len+data_growth);
  26555. + /* Copy the bytes up to and including the ip header */
  26556. + memcpy(n->head,
  26557. + skb->head,
  26558. + ((char *)iph - (char *)skb->head) + iphlen);
  26559. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  26560. + n->list=NULL;
  26561. +#endif
  26562. + n->next=NULL;
  26563. + n->prev=NULL;
  26564. + n->sk=NULL;
  26565. + n->dev=skb->dev;
  26566. + if (skb->h.raw)
  26567. + n->h.raw=skb->h.raw+offset;
  26568. + else
  26569. + n->h.raw=NULL;
  26570. + n->protocol=skb->protocol;
  26571. +#ifdef NET_21
  26572. + n->csum = 0;
  26573. + n->priority=skb->priority;
  26574. + n->dst=dst_clone(skb->dst);
  26575. + n->nh.raw=skb->nh.raw+offset;
  26576. +#ifndef NETDEV_23
  26577. + n->is_clone=0;
  26578. +#endif /* NETDEV_23 */
  26579. + atomic_set(&n->users, 1);
  26580. + n->destructor = NULL;
  26581. +#ifdef HAVE_SOCK_SECURITY
  26582. + n->security=skb->security;
  26583. +#endif
  26584. + memcpy(n->cb, skb->cb, sizeof(skb->cb));
  26585. +#ifdef CONFIG_IP_FIREWALL
  26586. + n->fwmark = skb->fwmark;
  26587. +#endif
  26588. +#else /* NET_21 */
  26589. + n->link3=NULL;
  26590. + n->when=skb->when;
  26591. + n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset);
  26592. + n->saddr=skb->saddr;
  26593. + n->daddr=skb->daddr;
  26594. + n->raddr=skb->raddr;
  26595. + n->seq=skb->seq;
  26596. + n->end_seq=skb->end_seq;
  26597. + n->ack_seq=skb->ack_seq;
  26598. + n->acked=skb->acked;
  26599. + n->free=1;
  26600. + n->arp=skb->arp;
  26601. + n->tries=0;
  26602. + n->lock=0;
  26603. + n->users=0;
  26604. + memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
  26605. +#endif /* NET_21 */
  26606. + if (skb->mac.raw)
  26607. + n->mac.raw=skb->mac.raw+offset;
  26608. + else
  26609. + n->mac.raw=NULL;
  26610. +#ifndef NETDEV_23
  26611. + n->used=skb->used;
  26612. +#endif /* !NETDEV_23 */
  26613. + n->pkt_type=skb->pkt_type;
  26614. +#ifndef NETDEV_23
  26615. + n->pkt_bridged=skb->pkt_bridged;
  26616. +#endif /* NETDEV_23 */
  26617. + n->ip_summed=0;
  26618. +#ifdef HAVE_TSTAMP
  26619. + n->tstamp = skb->tstamp;
  26620. +#else
  26621. + n->stamp=skb->stamp;
  26622. +#endif
  26623. +#ifndef NETDEV_23 /* this seems to have been removed in 2.4 */
  26624. +#if defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE)
  26625. + n->shapelatency=skb->shapelatency; /* Latency on frame */
  26626. + n->shapeclock=skb->shapeclock; /* Time it should go out */
  26627. + n->shapelen=skb->shapelen; /* Frame length in clocks */
  26628. + n->shapestamp=skb->shapestamp; /* Stamp for shaper */
  26629. + n->shapepend=skb->shapepend; /* Pending */
  26630. +#endif /* defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE) */
  26631. +#endif /* NETDEV_23 */
  26632. +
  26633. + return n;
  26634. +}
  26635. --- /dev/null Tue Mar 11 13:02:56 2003
  26636. +++ linux/net/ipsec/ipsec_ah.c Mon Feb 9 13:51:03 2004
  26637. @@ -0,0 +1,407 @@
  26638. +/*
  26639. + * processing code for AH
  26640. + * Copyright (C) 2003-2004 Michael Richardson <mcr@xelerance.com>
  26641. + *
  26642. + * This program is free software; you can redistribute it and/or modify it
  26643. + * under the terms of the GNU General Public License as published by the
  26644. + * Free Software Foundation; either version 2 of the License, or (at your
  26645. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  26646. + *
  26647. + * This program is distributed in the hope that it will be useful, but
  26648. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  26649. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  26650. + * for more details.
  26651. + */
  26652. +
  26653. +char ipsec_ah_c_version[] = "RCSID $Id: ipsec_ah.c,v 1.12.2.2 2006/10/06 21:39:26 paul Exp $";
  26654. +#ifndef AUTOCONF_INCLUDED
  26655. +#include <linux/config.h>
  26656. +#endif
  26657. +#include <linux/version.h>
  26658. +
  26659. +#define __NO_VERSION__
  26660. +#include <linux/module.h>
  26661. +#include <linux/kernel.h> /* printk() */
  26662. +
  26663. +#include "openswan/ipsec_param.h"
  26664. +
  26665. +#ifdef MALLOC_SLAB
  26666. +# include <linux/slab.h> /* kmalloc() */
  26667. +#else /* MALLOC_SLAB */
  26668. +# include <linux/malloc.h> /* kmalloc() */
  26669. +#endif /* MALLOC_SLAB */
  26670. +#include <linux/errno.h> /* error codes */
  26671. +#include <linux/types.h> /* size_t */
  26672. +#include <linux/interrupt.h> /* mark_bh */
  26673. +
  26674. +#include <linux/netdevice.h> /* struct device, and other headers */
  26675. +#include <linux/etherdevice.h> /* eth_type_trans */
  26676. +#include <linux/ip.h> /* struct iphdr */
  26677. +#include <linux/skbuff.h>
  26678. +#include <openswan.h>
  26679. +#ifdef SPINLOCK
  26680. +# ifdef SPINLOCK_23
  26681. +# include <linux/spinlock.h> /* *lock* */
  26682. +# else /* SPINLOCK_23 */
  26683. +# include <asm/spinlock.h> /* *lock* */
  26684. +# endif /* SPINLOCK_23 */
  26685. +#endif /* SPINLOCK */
  26686. +
  26687. +#include <net/ip.h>
  26688. +#include <net/protocol.h>
  26689. +
  26690. +#include "openswan/radij.h"
  26691. +#include "openswan/ipsec_encap.h"
  26692. +#include "openswan/ipsec_sa.h"
  26693. +
  26694. +#include "openswan/ipsec_radij.h"
  26695. +#include "openswan/ipsec_xform.h"
  26696. +#include "openswan/ipsec_tunnel.h"
  26697. +#include "openswan/ipsec_rcv.h"
  26698. +#include "openswan/ipsec_xmit.h"
  26699. +
  26700. +#include "openswan/ipsec_auth.h"
  26701. +#include "openswan/ipsec_ah.h"
  26702. +#include "openswan/ipsec_proto.h"
  26703. +
  26704. +__u32 zeroes[AH_AMAX];
  26705. +
  26706. +enum ipsec_rcv_value
  26707. +ipsec_rcv_ah_checks(struct ipsec_rcv_state *irs,
  26708. + struct sk_buff *skb)
  26709. +{
  26710. + int ahminlen;
  26711. +
  26712. + ahminlen = irs->hard_header_len + sizeof(struct iphdr);
  26713. +
  26714. + /* take care not to deref this pointer until we check the minlen though */
  26715. + irs->protostuff.ahstuff.ahp = (struct ahhdr *)skb->h.raw;
  26716. +
  26717. + if((skb->len < ahminlen+sizeof(struct ahhdr)) ||
  26718. + (skb->len < ahminlen+(irs->protostuff.ahstuff.ahp->ah_hl << 2))) {
  26719. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  26720. + "klips_debug:ipsec_rcv: "
  26721. + "runt ah packet of skb->len=%d received from %s, dropped.\n",
  26722. + skb->len,
  26723. + irs->ipsaddr_txt);
  26724. + if(irs->stats) {
  26725. + irs->stats->rx_errors++;
  26726. + }
  26727. + return IPSEC_RCV_BADLEN;
  26728. + }
  26729. +
  26730. + irs->said.spi = irs->protostuff.ahstuff.ahp->ah_spi;
  26731. +
  26732. + /* XXX we only support the one 12-byte authenticator for now */
  26733. + if(irs->protostuff.ahstuff.ahp->ah_hl != ((AHHMAC_HASHLEN+AHHMAC_RPLLEN) >> 2)) {
  26734. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  26735. + "klips_debug:ipsec_rcv: "
  26736. + "bad authenticator length %ld, expected %lu from %s.\n",
  26737. + (long)(irs->protostuff.ahstuff.ahp->ah_hl << 2),
  26738. + (unsigned long) sizeof(struct ahhdr),
  26739. + irs->ipsaddr_txt);
  26740. + if(irs->stats) {
  26741. + irs->stats->rx_errors++;
  26742. + }
  26743. + return IPSEC_RCV_BADLEN;
  26744. + }
  26745. +
  26746. + return IPSEC_RCV_OK;
  26747. +}
  26748. +
  26749. +
  26750. +enum ipsec_rcv_value
  26751. +ipsec_rcv_ah_setup_auth(struct ipsec_rcv_state *irs,
  26752. + struct sk_buff *skb,
  26753. + __u32 *replay,
  26754. + unsigned char **authenticator)
  26755. +{
  26756. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26757. +
  26758. + *replay = ntohl(ahp->ah_rpl);
  26759. + *authenticator = ahp->ah_data;
  26760. +
  26761. + return IPSEC_RCV_OK;
  26762. +}
  26763. +
  26764. +enum ipsec_rcv_value
  26765. +ipsec_rcv_ah_authcalc(struct ipsec_rcv_state *irs,
  26766. + struct sk_buff *skb)
  26767. +{
  26768. + struct auth_alg *aa;
  26769. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26770. + union {
  26771. + MD5_CTX md5;
  26772. + SHA1_CTX sha1;
  26773. + } tctx;
  26774. + struct iphdr ipo;
  26775. + int ahhlen;
  26776. +
  26777. + aa = irs->authfuncs;
  26778. +
  26779. + /* copy the initialized keying material */
  26780. + memcpy(&tctx, irs->ictx, irs->ictx_len);
  26781. +
  26782. + ipo = *irs->ipp;
  26783. + ipo.tos = 0; /* mutable RFC 2402 3.3.3.1.1.1 */
  26784. + ipo.frag_off = 0;
  26785. + ipo.ttl = 0;
  26786. + ipo.check = 0;
  26787. +
  26788. +
  26789. + /* do the sanitized header */
  26790. + (*aa->update)((void*)&tctx, (caddr_t)&ipo, sizeof(struct iphdr));
  26791. +
  26792. + /* XXX we didn't do the options here! */
  26793. +
  26794. + /* now do the AH header itself */
  26795. + ahhlen = AH_BASIC_LEN + (ahp->ah_hl << 2);
  26796. + (*aa->update)((void*)&tctx, (caddr_t)ahp, ahhlen - AHHMAC_HASHLEN);
  26797. +
  26798. + /* now, do some zeroes */
  26799. + (*aa->update)((void*)&tctx, (caddr_t)zeroes, AHHMAC_HASHLEN);
  26800. +
  26801. + /* finally, do the packet contents themselves */
  26802. + (*aa->update)((void*)&tctx,
  26803. + (caddr_t)skb->h.raw + ahhlen,
  26804. + skb->len - ahhlen);
  26805. +
  26806. + (*aa->final)(irs->hash, (void *)&tctx);
  26807. +
  26808. + memcpy(&tctx, irs->octx, irs->octx_len);
  26809. +
  26810. + (*aa->update)((void *)&tctx, irs->hash, aa->hashlen);
  26811. + (*aa->final)(irs->hash, (void *)&tctx);
  26812. +
  26813. + return IPSEC_RCV_OK;
  26814. +}
  26815. +
  26816. +enum ipsec_rcv_value
  26817. +ipsec_rcv_ah_decap(struct ipsec_rcv_state *irs)
  26818. +{
  26819. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26820. + struct sk_buff *skb;
  26821. + int ahhlen;
  26822. +
  26823. + skb=irs->skb;
  26824. +
  26825. + ahhlen = AH_BASIC_LEN + (ahp->ah_hl << 2);
  26826. +
  26827. + irs->ipp->tot_len = htons(ntohs(irs->ipp->tot_len) - ahhlen);
  26828. + irs->next_header = ahp->ah_nh;
  26829. +
  26830. + /*
  26831. + * move the IP header forward by the size of the AH header, which
  26832. + * will remove the the AH header from the packet.
  26833. + */
  26834. + memmove((void *)(skb->nh.raw + ahhlen),
  26835. + (void *)(skb->nh.raw), irs->iphlen);
  26836. +
  26837. + ipsec_rcv_dmp("ah postmove", skb->data, skb->len);
  26838. +
  26839. + /* skb_pull below, will move up by ahhlen */
  26840. +
  26841. + /* XXX not clear how this can happen, as the message indicates */
  26842. + if(skb->len < ahhlen) {
  26843. + printk(KERN_WARNING
  26844. + "klips_error:ipsec_rcv: "
  26845. + "tried to skb_pull ahhlen=%d, %d available. This should never happen, please report.\n",
  26846. + ahhlen,
  26847. + (int)(skb->len));
  26848. + return IPSEC_RCV_DECAPFAIL;
  26849. + }
  26850. + skb_pull(skb, ahhlen);
  26851. +
  26852. + skb->nh.raw = skb->nh.raw + ahhlen;
  26853. + irs->ipp = skb->nh.iph;
  26854. +
  26855. + ipsec_rcv_dmp("ah postpull", (void *)skb->nh.iph, skb->len);
  26856. +
  26857. + return IPSEC_RCV_OK;
  26858. +}
  26859. +
  26860. +enum ipsec_xmit_value
  26861. +ipsec_xmit_ah_setup(struct ipsec_xmit_state *ixs)
  26862. +{
  26863. + struct iphdr ipo;
  26864. + struct ahhdr *ahp;
  26865. + __u8 hash[AH_AMAX];
  26866. + union {
  26867. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  26868. + MD5_CTX md5;
  26869. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  26870. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  26871. + SHA1_CTX sha1;
  26872. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  26873. + } tctx;
  26874. + unsigned char *dat = (unsigned char *)ixs->iph;
  26875. +
  26876. + ahp = (struct ahhdr *)(dat + ixs->iphlen);
  26877. + ahp->ah_spi = ixs->ipsp->ips_said.spi;
  26878. + ahp->ah_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  26879. + ahp->ah_rv = 0;
  26880. + ahp->ah_nh = ixs->iph->protocol;
  26881. + ahp->ah_hl = (sizeof(struct ahhdr) >> 2) - sizeof(__u64)/sizeof(__u32);
  26882. + ixs->iph->protocol = IPPROTO_AH;
  26883. + ipsec_xmit_dmp("ahp", (char*)ahp, sizeof(*ahp));
  26884. +
  26885. + ipo = *ixs->iph;
  26886. + ipo.tos = 0;
  26887. + ipo.frag_off = 0;
  26888. + ipo.ttl = 0;
  26889. + ipo.check = 0;
  26890. + ipsec_xmit_dmp("ipo", (char*)&ipo, sizeof(ipo));
  26891. +
  26892. + switch(ixs->ipsp->ips_authalg) {
  26893. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  26894. + case AH_MD5:
  26895. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  26896. + ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  26897. + osMD5Update(&tctx.md5, (unsigned char *)&ipo, sizeof (struct iphdr));
  26898. + ipsec_xmit_dmp("ictx+ipo", (char*)&tctx.md5, sizeof(tctx.md5));
  26899. + osMD5Update(&tctx.md5, (unsigned char *)ahp,
  26900. + sizeof(struct ahhdr) - sizeof(ahp->ah_data));
  26901. + ipsec_xmit_dmp("ictx+ahp", (char*)&tctx.md5, sizeof(tctx.md5));
  26902. + osMD5Update(&tctx.md5, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  26903. + ipsec_xmit_dmp("ictx+zeroes", (char*)&tctx.md5, sizeof(tctx.md5));
  26904. + osMD5Update(&tctx.md5, dat + ixs->iphlen + sizeof(struct ahhdr),
  26905. + ixs->skb->len - ixs->iphlen - sizeof(struct ahhdr));
  26906. + ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  26907. + osMD5Final(hash, &tctx.md5);
  26908. + ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
  26909. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  26910. + ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  26911. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  26912. + ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  26913. + osMD5Final(hash, &tctx.md5);
  26914. + ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
  26915. +
  26916. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  26917. +
  26918. + /* paranoid */
  26919. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  26920. + memset((caddr_t)hash, 0, sizeof(*hash));
  26921. + break;
  26922. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  26923. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  26924. + case AH_SHA:
  26925. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  26926. + SHA1Update(&tctx.sha1, (unsigned char *)&ipo, sizeof (struct iphdr));
  26927. + SHA1Update(&tctx.sha1, (unsigned char *)ahp, sizeof(struct ahhdr) - sizeof(ahp->ah_data));
  26928. + SHA1Update(&tctx.sha1, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  26929. + SHA1Update(&tctx.sha1, dat + ixs->iphlen + sizeof(struct ahhdr),
  26930. + ixs->skb->len - ixs->iphlen - sizeof(struct ahhdr));
  26931. + SHA1Final(hash, &tctx.sha1);
  26932. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  26933. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  26934. + SHA1Final(hash, &tctx.sha1);
  26935. +
  26936. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  26937. +
  26938. + /* paranoid */
  26939. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  26940. + memset((caddr_t)hash, 0, sizeof(*hash));
  26941. + break;
  26942. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  26943. + default:
  26944. + ixs->stats->tx_errors++;
  26945. + return IPSEC_XMIT_AH_BADALG;
  26946. + }
  26947. +#ifdef NET_21
  26948. + ixs->skb->h.raw = (unsigned char*)ahp;
  26949. +#endif /* NET_21 */
  26950. +
  26951. + return IPSEC_XMIT_OK;
  26952. +}
  26953. +
  26954. +struct xform_functions ah_xform_funcs[]={
  26955. + { rcv_checks: ipsec_rcv_ah_checks,
  26956. + rcv_setup_auth: ipsec_rcv_ah_setup_auth,
  26957. + rcv_calc_auth: ipsec_rcv_ah_authcalc,
  26958. + rcv_decrypt: ipsec_rcv_ah_decap,
  26959. +
  26960. + xmit_setup: ipsec_xmit_ah_setup,
  26961. + xmit_headroom: sizeof(struct ahhdr),
  26962. + xmit_needtailroom: 0,
  26963. + },
  26964. +};
  26965. +
  26966. +
  26967. +#ifdef NET_26
  26968. +struct inet_protocol ah_protocol = {
  26969. + .handler = ipsec_rcv,
  26970. + .no_policy = 1,
  26971. +};
  26972. +#else
  26973. +struct inet_protocol ah_protocol =
  26974. +{
  26975. + ipsec_rcv, /* AH handler */
  26976. + NULL, /* TUNNEL error control */
  26977. +#ifdef NETDEV_25
  26978. + 1, /* no policy */
  26979. +#else
  26980. + 0, /* next */
  26981. + IPPROTO_AH, /* protocol ID */
  26982. + 0, /* copy */
  26983. + NULL, /* data */
  26984. + "AH" /* name */
  26985. +#endif
  26986. +};
  26987. +#endif /* NET_26 */
  26988. +
  26989. +/*
  26990. + * $Log: ipsec_ah.c,v $
  26991. + * Revision 1.12.2.2 2006/10/06 21:39:26 paul
  26992. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  26993. + * set. This is defined through autoconf.h which is included through the
  26994. + * linux kernel build macros.
  26995. + *
  26996. + * Revision 1.12.2.1 2006/02/15 05:35:14 paul
  26997. + * Patch by David McCullough <davidm@snapgear.com>
  26998. + * If you setup a tunnel without ESP it doesn't work. It used to work in
  26999. + * an older openswan version but stopped when klips was modified to deal
  27000. + * with the pulled IP header on the received SKB's.
  27001. + *
  27002. + * The code in ipsec_ah.c still thinks the IP header is there and runs the
  27003. + * hash on the incorrect data.
  27004. + *
  27005. + * Revision 1.12 2005/04/29 05:10:22 mcr
  27006. + * removed from extraenous includes to make unit testing easier.
  27007. + *
  27008. + * Revision 1.11 2005/04/15 19:50:55 mcr
  27009. + * adjustments to use proper skb fields for data.
  27010. + *
  27011. + * Revision 1.10 2004/09/14 00:22:57 mcr
  27012. + * adjustment of MD5* functions.
  27013. + *
  27014. + * Revision 1.9 2004/09/13 02:22:47 mcr
  27015. + * #define inet_protocol if necessary.
  27016. + *
  27017. + * Revision 1.8 2004/09/06 18:35:48 mcr
  27018. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  27019. + * so adjust for that.
  27020. + *
  27021. + * Revision 1.7 2004/08/22 05:00:48 mcr
  27022. + * if we choose to compile the file, we want the contents,
  27023. + * so don't pull any punches.
  27024. + *
  27025. + * Revision 1.6 2004/08/17 03:27:23 mcr
  27026. + * klips 2.6 edits.
  27027. + *
  27028. + * Revision 1.5 2004/08/14 03:28:24 mcr
  27029. + * fixed log comment to remove warning about embedded comment.
  27030. + *
  27031. + * Revision 1.4 2004/08/04 15:57:07 mcr
  27032. + * moved des .h files to include/des/ *
  27033. + * included 2.6 protocol specific things
  27034. + * started at NAT-T support, but it will require a kernel patch.
  27035. + *
  27036. + * Revision 1.3 2004/07/10 19:11:18 mcr
  27037. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  27038. + *
  27039. + * Revision 1.2 2004/04/06 02:49:25 mcr
  27040. + * pullup of algo code from alg-branch.
  27041. + *
  27042. + *
  27043. + *
  27044. + */
  27045. --- /dev/null Tue Mar 11 13:02:56 2003
  27046. +++ linux/net/ipsec/ipsec_alg.c Mon Feb 9 13:51:03 2004
  27047. @@ -0,0 +1,1057 @@
  27048. +/*
  27049. + * Modular extensions service and registration functions
  27050. + *
  27051. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  27052. + *
  27053. + * Version: 0.8.1
  27054. + *
  27055. + * ipsec_alg.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  27056. + *
  27057. + * This program is free software; you can redistribute it and/or modify it
  27058. + * under the terms of the GNU General Public License as published by the
  27059. + * Free Software Foundation; either version 2 of the License, or (at your
  27060. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  27061. + *
  27062. + * This program is distributed in the hope that it will be useful, but
  27063. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  27064. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  27065. + * for more details.
  27066. + *
  27067. + */
  27068. +#define __NO_VERSION__
  27069. +
  27070. +#if defined (MODULE)
  27071. +#include <linux/module.h>
  27072. +#endif
  27073. +
  27074. +#include <linux/kernel.h> /* printk() */
  27075. +
  27076. +#include <linux/netdevice.h> /* struct device, and other headers */
  27077. +#include <linux/etherdevice.h> /* eth_type_trans */
  27078. +#include <linux/ip.h> /* struct iphdr */
  27079. +#include <linux/skbuff.h>
  27080. +#include <linux/socket.h>
  27081. +#include <linux/in.h>
  27082. +#include <linux/types.h>
  27083. +#include <linux/string.h> /* memcmp() */
  27084. +#include <linux/random.h> /* get_random_bytes() */
  27085. +#include <linux/errno.h> /* error codes */
  27086. +#ifdef SPINLOCK
  27087. +# ifdef SPINLOCK_23
  27088. +# include <linux/spinlock.h> /* *lock* */
  27089. +# else /* SPINLOCK_23 */
  27090. +# include <asm/spinlock.h> /* *lock* */
  27091. +# endif /* SPINLOCK_23 */
  27092. +#endif /* SPINLOCK */
  27093. +
  27094. +#include "openswan/ipsec_param.h"
  27095. +#include <openswan.h>
  27096. +#include "openswan/ipsec_sa.h"
  27097. +#include "openswan/radij.h"
  27098. +#include "openswan/ipsec_encap.h"
  27099. +#include "openswan/ipsec_radij.h"
  27100. +#include "openswan/ipsec_xform.h"
  27101. +#include "openswan/ipsec_tunnel.h"
  27102. +#include "openswan/ipsec_rcv.h"
  27103. +#if defined(CONFIG_KLIPS_ESP) || defined(CONFIG_KLIPS_AH)
  27104. +# include "openswan/ipsec_ah.h"
  27105. +#endif /* defined(CONFIG_KLIPS_ESP) || defined(CONFIG_KLIPS_AH) */
  27106. +#ifdef CONFIG_KLIPS_ESP
  27107. +# include "openswan/ipsec_esp.h"
  27108. +#endif /* !CONFIG_KLIPS_ESP */
  27109. +#ifdef CONFIG_KLIPS_IPCOMP
  27110. +# include "openswan/ipcomp.h"
  27111. +#endif /* CONFIG_KLIPS_COMP */
  27112. +
  27113. +#include <pfkeyv2.h>
  27114. +#include <pfkey.h>
  27115. +
  27116. +#include "openswan/ipsec_alg.h"
  27117. +#include "openswan/ipsec_proto.h"
  27118. +
  27119. +#if SADB_EALG_MAX < 255
  27120. +#warning Compiling with limited ESP support ( SADB_EALG_MAX < 256 )
  27121. +#endif
  27122. +
  27123. +static rwlock_t ipsec_alg_lock = RW_LOCK_UNLOCKED;
  27124. +#define IPSEC_ALG_HASHSZ 16 /* must be power of 2, even 2^0=1 */
  27125. +static struct list_head ipsec_alg_hash_table[IPSEC_ALG_HASHSZ];
  27126. +
  27127. +/* Old gcc's will fail here */
  27128. +#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) \
  27129. + ; goto out; } while(0)
  27130. +
  27131. +#ifdef NET_26
  27132. +/*
  27133. + * Must be already protected by lock
  27134. + */
  27135. +static void __ipsec_alg_usage_inc(struct ipsec_alg *ixt)
  27136. +{
  27137. +#ifdef MODULE
  27138. + if (ixt->ixt_module)
  27139. + try_module_get(ixt->ixt_module);
  27140. +#endif
  27141. + atomic_inc(&ixt->ixt_refcnt);
  27142. +}
  27143. +static void __ipsec_alg_usage_dec(struct ipsec_alg *ixt) {
  27144. + atomic_dec(&ixt->ixt_refcnt);
  27145. +#ifdef MODULE
  27146. + if (ixt->ixt_module)
  27147. + module_put(ixt->ixt_module);
  27148. +#endif
  27149. +}
  27150. +
  27151. +#else
  27152. +
  27153. +/*
  27154. + * Must be already protected by lock
  27155. + */
  27156. +static void __ipsec_alg_usage_inc(struct ipsec_alg *ixt) {
  27157. +#ifdef MODULE
  27158. + if (ixt->ixt_module) {
  27159. + __MOD_INC_USE_COUNT(ixt->ixt_module);
  27160. + }
  27161. +#endif
  27162. + atomic_inc(&ixt->ixt_refcnt);
  27163. +}
  27164. +static void __ipsec_alg_usage_dec(struct ipsec_alg *ixt) {
  27165. + atomic_dec(&ixt->ixt_refcnt);
  27166. +#ifdef MODULE
  27167. + if (ixt->ixt_module)
  27168. + __MOD_DEC_USE_COUNT(ixt->ixt_module);
  27169. +#endif
  27170. +}
  27171. +#endif
  27172. +
  27173. +/*
  27174. + * simple hash function, optimized for 0-hash (1 list) special
  27175. + * case
  27176. + */
  27177. +#if IPSEC_ALG_HASHSZ > 1
  27178. +static inline unsigned ipsec_alg_hashfn(int alg_type, int alg_id) {
  27179. + return ((alg_type^alg_id)&(IPSEC_ALG_HASHSZ-1));
  27180. +}
  27181. +#else
  27182. +#define ipsec_alg_hashfn(x,y) (0)
  27183. +#endif
  27184. +
  27185. +/*****************************************************************
  27186. + *
  27187. + * INTERNAL table handling: insert, delete, find
  27188. + *
  27189. + *****************************************************************/
  27190. +
  27191. +/*
  27192. + * hash table initialization, called from ipsec_alg_init()
  27193. + */
  27194. +static void ipsec_alg_hash_init(void) {
  27195. + struct list_head *head = ipsec_alg_hash_table;
  27196. + int i = IPSEC_ALG_HASHSZ;
  27197. + do {
  27198. + INIT_LIST_HEAD(head);
  27199. + head++;
  27200. + i--;
  27201. + } while (i);
  27202. +}
  27203. +/*
  27204. + * hash list lookup by {alg_type, alg_id} and table head,
  27205. + * must be already protected by lock
  27206. + */
  27207. +static struct ipsec_alg *__ipsec_alg_find(unsigned alg_type, unsigned alg_id, struct list_head * head) {
  27208. + struct list_head *p;
  27209. + struct ipsec_alg *ixt=NULL;
  27210. + for (p=head->next; p!=head; p=p->next) {
  27211. + ixt = list_entry(p, struct ipsec_alg, ixt_list);
  27212. + if (ixt->ixt_alg_type == alg_type && ixt->ixt_alg_id==alg_id) {
  27213. + goto out;
  27214. + }
  27215. + }
  27216. + ixt=NULL;
  27217. +out:
  27218. + return ixt;
  27219. +}
  27220. +/*
  27221. + * inserts (in front) a new entry in hash table,
  27222. + * called from ipsec_alg_register() when new algorithm is registered.
  27223. + */
  27224. +static int ipsec_alg_insert(struct ipsec_alg *ixt) {
  27225. + int ret=-EINVAL;
  27226. + unsigned hashval=ipsec_alg_hashfn(ixt->ixt_alg_type, ixt->ixt_alg_id);
  27227. + struct list_head *head= ipsec_alg_hash_table + hashval;
  27228. + struct ipsec_alg *ixt_cur;
  27229. +
  27230. + /* new element must be virgin ... */
  27231. + if (ixt->ixt_list.next != &ixt->ixt_list ||
  27232. + ixt->ixt_list.prev != &ixt->ixt_list) {
  27233. + printk(KERN_ERR "ipsec_alg_insert: ixt object \"%s\" "
  27234. + "list head not initialized\n",
  27235. + ixt->ixt_name);
  27236. + return ret;
  27237. + }
  27238. + write_lock_bh(&ipsec_alg_lock);
  27239. +
  27240. + ixt_cur = __ipsec_alg_find(ixt->ixt_alg_type, ixt->ixt_alg_id, head);
  27241. +
  27242. + /* if previous (current) ipsec_alg found check excl flag of _anyone_ */
  27243. + if (ixt_cur
  27244. + && ((ixt->ixt_state|ixt_cur->ixt_state) & IPSEC_ALG_ST_EXCL)) {
  27245. + barf_out("ipsec_alg for alg_type=%d, alg_id=%d already exist. "
  27246. + "Not loaded (ret=%d).\n",
  27247. + ixt->ixt_alg_type,
  27248. + ixt->ixt_alg_id, ret=-EEXIST);
  27249. + }
  27250. + list_add(&ixt->ixt_list, head);
  27251. + ixt->ixt_state |= IPSEC_ALG_ST_REGISTERED;
  27252. + ret=0;
  27253. +out:
  27254. + write_unlock_bh(&ipsec_alg_lock);
  27255. + return ret;
  27256. +}
  27257. +
  27258. +/*
  27259. + * deletes an existing entry in hash table,
  27260. + * called from ipsec_alg_unregister() when algorithm is unregistered.
  27261. + */
  27262. +static int ipsec_alg_delete(struct ipsec_alg *ixt) {
  27263. + write_lock_bh(&ipsec_alg_lock);
  27264. + list_del(&ixt->ixt_list);
  27265. + write_unlock_bh(&ipsec_alg_lock);
  27266. + return 0;
  27267. +}
  27268. +
  27269. +/*
  27270. + * here @user context (read-only when @kernel bh context)
  27271. + * -> no bh disabling
  27272. + *
  27273. + * called from ipsec_sa_init() -> ipsec_alg_sa_init()
  27274. + */
  27275. +static struct ipsec_alg *ipsec_alg_get(int alg_type, int alg_id)
  27276. +{
  27277. + unsigned hashval=ipsec_alg_hashfn(alg_type, alg_id);
  27278. + struct list_head *head= ipsec_alg_hash_table + hashval;
  27279. + struct ipsec_alg *ixt;
  27280. +
  27281. + read_lock(&ipsec_alg_lock);
  27282. + ixt=__ipsec_alg_find(alg_type, alg_id, head);
  27283. + if (ixt) __ipsec_alg_usage_inc(ixt);
  27284. + read_unlock(&ipsec_alg_lock);
  27285. +
  27286. + return ixt;
  27287. +}
  27288. +
  27289. +static void ipsec_alg_put(struct ipsec_alg *ixt) {
  27290. + __ipsec_alg_usage_dec((struct ipsec_alg *)ixt);
  27291. +}
  27292. +
  27293. +/*****************************************************************
  27294. + *
  27295. + * INTERFACE for ENC services: key creation, encrypt function
  27296. + *
  27297. + *****************************************************************/
  27298. +
  27299. +/*
  27300. + * main encrypt service entry point
  27301. + * called from ipsec_rcv() with encrypt=IPSEC_ALG_DECRYPT and
  27302. + * ipsec_tunnel_start_xmit with encrypt=IPSEC_ALG_ENCRYPT
  27303. + */
  27304. +int ipsec_alg_esp_encrypt(struct ipsec_sa *sa_p, __u8 * idat,
  27305. + int ilen, const __u8 * iv, int encrypt)
  27306. +{
  27307. + int ret;
  27308. + struct ipsec_alg_enc *ixt_e=sa_p->ips_alg_enc;
  27309. +#ifdef CONFIG_KLIPS_DEBUG
  27310. + int debug_flag = (encrypt==IPSEC_ALG_ENCRYPT ?
  27311. + debug_tunnel : debug_rcv);
  27312. +#endif
  27313. +
  27314. + KLIPS_PRINT(debug_flag,
  27315. + "klips_debug:ipsec_alg_esp_encrypt: "
  27316. + "entering with encalg=%d, ixt_e=%p\n",
  27317. + sa_p->ips_encalg, ixt_e);
  27318. + if (ixt_e == NULL) {
  27319. +#ifdef CONFIG_KLIPS_DEBUG
  27320. + KLIPS_ERROR(debug_flag,
  27321. + "klips_debug:ipsec_alg_esp_encrypt: "
  27322. + "NULL ipsec_alg_enc object\n");
  27323. +#endif
  27324. + return -1;
  27325. + }
  27326. + KLIPS_PRINT(debug_flag,
  27327. + "klips_debug:ipsec_alg_esp_encrypt: "
  27328. + "calling cbc_encrypt encalg=%d "
  27329. + "ips_key_e=%p idat=%p ilen=%d iv=%p, encrypt=%d\n",
  27330. + sa_p->ips_encalg,
  27331. + sa_p->ips_key_e, idat, ilen, iv, encrypt);
  27332. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, sa_p->ips_key_e, idat,
  27333. + ilen, iv, encrypt);
  27334. + KLIPS_PRINT(debug_flag,
  27335. + "klips_debug:ipsec_alg_esp_encrypt: "
  27336. + "returned ret=%d\n",
  27337. + ret);
  27338. + return ret;
  27339. +}
  27340. +
  27341. +/*
  27342. + * encryption key context creation function
  27343. + * called from pfkey_v2_parser.c:pfkey_ips_init()
  27344. + */
  27345. +int ipsec_alg_enc_key_create(struct ipsec_sa *sa_p) {
  27346. + int ret=-EINVAL;
  27347. + int keyminbits, keymaxbits;
  27348. + caddr_t ekp;
  27349. + struct ipsec_alg_enc *ixt_e=sa_p->ips_alg_enc;
  27350. +
  27351. + KLIPS_PRINT(debug_pfkey,
  27352. + "klips_debug:ipsec_alg_enc_key_create: "
  27353. + "entering with encalg=%d ixt_e=%p\n",
  27354. + sa_p->ips_encalg, ixt_e);
  27355. + if (!ixt_e) {
  27356. + KLIPS_PRINT(debug_pfkey,
  27357. + "klips_debug:ipsec_alg_enc_key_create: "
  27358. + "NULL ipsec_alg_enc object\n");
  27359. + return -EPROTO;
  27360. + }
  27361. +
  27362. + /*
  27363. + * grRRR... DES 7bits jurassic stuff ... f*ckk --jjo
  27364. + */
  27365. + switch(ixt_e->ixt_common.ixt_support.ias_id) {
  27366. + case ESP_3DES:
  27367. + keyminbits=keymaxbits=192;break;
  27368. + case ESP_DES:
  27369. + keyminbits=keymaxbits=64;break;
  27370. + default:
  27371. + keyminbits=ixt_e->ixt_common.ixt_support.ias_keyminbits;
  27372. + keymaxbits=ixt_e->ixt_common.ixt_support.ias_keymaxbits;
  27373. + }
  27374. + if(sa_p->ips_key_bits_e<keyminbits ||
  27375. + sa_p->ips_key_bits_e>keymaxbits) {
  27376. + KLIPS_PRINT(debug_pfkey,
  27377. + "klips_debug:ipsec_alg_enc_key_create: "
  27378. + "incorrect encryption key size for id=%d: %d bits -- "
  27379. + "must be between %d,%d bits\n" /*octets (bytes)\n"*/,
  27380. + ixt_e->ixt_common.ixt_support.ias_id,
  27381. + sa_p->ips_key_bits_e, keyminbits, keymaxbits);
  27382. + ret=-EINVAL;
  27383. + goto ixt_out;
  27384. + }
  27385. + /* save encryption key pointer */
  27386. + ekp = sa_p->ips_key_e;
  27387. +
  27388. +
  27389. + if (ixt_e->ixt_e_new_key) {
  27390. + sa_p->ips_key_e = ixt_e->ixt_e_new_key(ixt_e,
  27391. + ekp, sa_p->ips_key_bits_e/8);
  27392. + ret = (sa_p->ips_key_e)? 0 : -EINVAL;
  27393. + } else {
  27394. + if((sa_p->ips_key_e = (caddr_t)
  27395. + kmalloc((sa_p->ips_key_e_size = ixt_e->ixt_e_ctx_size),
  27396. + GFP_ATOMIC)) == NULL) {
  27397. + ret=-ENOMEM;
  27398. + goto ixt_out;
  27399. + }
  27400. + /* zero-out key_e */
  27401. + memset(sa_p->ips_key_e, 0, sa_p->ips_key_e_size);
  27402. +
  27403. + /* I cast here to allow more decoupling in alg module */
  27404. + KLIPS_PRINT(debug_pfkey,
  27405. + "klips_debug:ipsec_alg_enc_key_create: about to call:"
  27406. + "set_key(key_e=%p, ekp=%p, key_size=%d)\n",
  27407. + (caddr_t)sa_p->ips_key_e, ekp, sa_p->ips_key_bits_e/8);
  27408. + ret = ixt_e->ixt_e_set_key(ixt_e, (caddr_t)sa_p->ips_key_e, ekp, sa_p->ips_key_bits_e/8);
  27409. + }
  27410. + /* paranoid */
  27411. + memset(ekp, 0, sa_p->ips_key_bits_e/8);
  27412. + kfree(ekp);
  27413. +ixt_out:
  27414. + return ret;
  27415. +}
  27416. +
  27417. +/***************************************************************
  27418. + *
  27419. + * INTERFACE for AUTH services: key creation, hash functions
  27420. + *
  27421. + ***************************************************************/
  27422. +
  27423. +/*
  27424. + * auth key context creation function
  27425. + * called from pfkey_v2_parser.c:pfkey_ips_init()
  27426. + */
  27427. +int ipsec_alg_auth_key_create(struct ipsec_sa *sa_p) {
  27428. + int ret=-EINVAL;
  27429. + struct ipsec_alg_auth *ixt_a=sa_p->ips_alg_auth;
  27430. + int keyminbits, keymaxbits;
  27431. + unsigned char *akp;
  27432. + unsigned int aks;
  27433. + KLIPS_PRINT(debug_pfkey,
  27434. + "klips_debug:ipsec_alg_auth_key_create: "
  27435. + "entering with authalg=%d ixt_a=%p\n",
  27436. + sa_p->ips_authalg, ixt_a);
  27437. + if (!ixt_a) {
  27438. + KLIPS_PRINT(debug_pfkey,
  27439. + "klips_debug:ipsec_alg_auth_key_create: "
  27440. + "NULL ipsec_alg_auth object\n");
  27441. + return -EPROTO;
  27442. + }
  27443. + keyminbits=ixt_a->ixt_common.ixt_support.ias_keyminbits;
  27444. + keymaxbits=ixt_a->ixt_common.ixt_support.ias_keymaxbits;
  27445. + if(sa_p->ips_key_bits_a<keyminbits || sa_p->ips_key_bits_a>keymaxbits) {
  27446. + KLIPS_PRINT(debug_pfkey,
  27447. + "klips_debug:ipsec_alg_auth_key_create: incorrect auth"
  27448. + "key size: %d bits -- must be between %d,%d bits\n"/*octets (bytes)\n"*/,
  27449. + sa_p->ips_key_bits_a, keyminbits, keymaxbits);
  27450. + ret=-EINVAL;
  27451. + goto ixt_out;
  27452. + }
  27453. + /* save auth key pointer */
  27454. + sa_p->ips_auth_bits = ixt_a->ixt_a_keylen * 8; /* XXX XXX */
  27455. + akp = sa_p->ips_key_a;
  27456. + aks = sa_p->ips_key_a_size;
  27457. +
  27458. + /* will hold: 2 ctx and a blocksize buffer: kb */
  27459. + sa_p->ips_key_a_size = ixt_a->ixt_a_ctx_size;
  27460. + if((sa_p->ips_key_a =
  27461. + (caddr_t) kmalloc(sa_p->ips_key_a_size, GFP_ATOMIC)) == NULL) {
  27462. + ret=-ENOMEM;
  27463. + goto ixt_out;
  27464. + }
  27465. + ixt_a->ixt_a_hmac_set_key(ixt_a, sa_p->ips_key_a, akp, sa_p->ips_key_bits_a/8); /* XXX XXX */
  27466. + ret=0;
  27467. + memset(akp, 0, aks);
  27468. + kfree(akp);
  27469. +
  27470. +ixt_out:
  27471. + return ret;
  27472. +}
  27473. +
  27474. +
  27475. +int ipsec_alg_sa_esp_hash(const struct ipsec_sa *sa_p, const __u8 *espp,
  27476. + int len, __u8 *hash, int hashlen)
  27477. +{
  27478. + struct ipsec_alg_auth *ixt_a=sa_p->ips_alg_auth;
  27479. + if (!ixt_a) {
  27480. + KLIPS_PRINT(debug_pfkey,
  27481. + "klips_debug:ipsec_sa_esp_hash: "
  27482. + "NULL ipsec_alg_auth object\n");
  27483. + return -EPROTO;
  27484. + }
  27485. + KLIPS_PRINT(debug_tunnel|debug_rcv,
  27486. + "klips_debug:ipsec_sa_esp_hash: "
  27487. + "hashing %p (%d bytes) to %p (%d bytes)\n",
  27488. + espp, len,
  27489. + hash, hashlen);
  27490. + ixt_a->ixt_a_hmac_hash(ixt_a,
  27491. + sa_p->ips_key_a,
  27492. + espp, len,
  27493. + hash, hashlen);
  27494. + return 0;
  27495. +}
  27496. +
  27497. +/***************************************************************
  27498. + *
  27499. + * INTERFACE for module loading,testing, and unloading
  27500. + *
  27501. + ***************************************************************/
  27502. +
  27503. +/* validation for registering (enc) module */
  27504. +static int check_enc(struct ipsec_alg_enc *ixt)
  27505. +{
  27506. + int ret=-EINVAL;
  27507. + if (ixt->ixt_common.ixt_blocksize==0) /* || ixt->ixt_common.ixt_blocksize%2) need for ESP_NULL */
  27508. + barf_out(KERN_ERR "invalid blocksize=%d\n", ixt->ixt_common.ixt_blocksize);
  27509. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0
  27510. + && ixt->ixt_common.ixt_support.ias_keymaxbits==0
  27511. + && ixt->ixt_e_keylen==0)
  27512. + goto zero_key_ok;
  27513. +
  27514. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0)
  27515. + barf_out(KERN_ERR "invalid keyminbits=%d\n", ixt->ixt_common.ixt_support.ias_keyminbits);
  27516. +
  27517. + if (ixt->ixt_common.ixt_support.ias_keymaxbits==0)
  27518. + barf_out(KERN_ERR "invalid keymaxbits=%d\n", ixt->ixt_common.ixt_support.ias_keymaxbits);
  27519. +
  27520. + if (ixt->ixt_e_keylen==0)
  27521. + barf_out(KERN_ERR "invalid keysize=%d\n", ixt->ixt_e_keylen);
  27522. +
  27523. +zero_key_ok:
  27524. + if (ixt->ixt_e_ctx_size==0 && ixt->ixt_e_new_key == NULL)
  27525. + barf_out(KERN_ERR "invalid key_e_size=%d and ixt_e_new_key=NULL\n", ixt->ixt_e_ctx_size);
  27526. + if (ixt->ixt_e_cbc_encrypt==NULL)
  27527. + barf_out(KERN_ERR "e_cbc_encrypt() must be not NULL\n");
  27528. + ret=0;
  27529. +out:
  27530. + return ret;
  27531. +}
  27532. +
  27533. +/* validation for registering (auth) module */
  27534. +static int check_auth(struct ipsec_alg_auth *ixt)
  27535. +{
  27536. + int ret=-EINVAL;
  27537. + if (ixt->ixt_common.ixt_support.ias_id==0 || ixt->ixt_common.ixt_support.ias_id > SADB_AALG_MAX)
  27538. + barf_out("invalid alg_id=%d > %d (SADB_AALG_MAX)\n",
  27539. + ixt->ixt_common.ixt_support.ias_id, SADB_AALG_MAX);
  27540. +
  27541. + if (ixt->ixt_common.ixt_blocksize==0
  27542. + || ixt->ixt_common.ixt_blocksize%2)
  27543. + barf_out(KERN_ERR "invalid blocksize=%d\n",
  27544. + ixt->ixt_common.ixt_blocksize);
  27545. +
  27546. + if (ixt->ixt_common.ixt_blocksize>AH_BLKLEN_MAX)
  27547. + barf_out(KERN_ERR "sorry blocksize=%d > %d. "
  27548. + "Please increase AH_BLKLEN_MAX and recompile\n",
  27549. + ixt->ixt_common.ixt_blocksize,
  27550. + AH_BLKLEN_MAX);
  27551. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0 && ixt->ixt_common.ixt_support.ias_keymaxbits==0 && ixt->ixt_a_keylen==0)
  27552. + goto zero_key_ok;
  27553. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0)
  27554. + barf_out(KERN_ERR "invalid keyminbits=%d\n", ixt->ixt_common.ixt_support.ias_keyminbits);
  27555. + if (ixt->ixt_common.ixt_support.ias_keymaxbits==0)
  27556. + barf_out(KERN_ERR "invalid keymaxbits=%d\n", ixt->ixt_common.ixt_support.ias_keymaxbits);
  27557. + if (ixt->ixt_common.ixt_support.ias_keymaxbits!=ixt->ixt_common.ixt_support.ias_keyminbits)
  27558. + barf_out(KERN_ERR "keymaxbits must equal keyminbits (not sure).\n");
  27559. + if (ixt->ixt_a_keylen==0)
  27560. + barf_out(KERN_ERR "invalid keysize=%d\n", ixt->ixt_a_keylen);
  27561. +zero_key_ok:
  27562. + if (ixt->ixt_a_ctx_size==0)
  27563. + barf_out(KERN_ERR "invalid a_ctx_size=%d\n", ixt->ixt_a_ctx_size);
  27564. + if (ixt->ixt_a_hmac_set_key==NULL)
  27565. + barf_out(KERN_ERR "a_hmac_set_key() must be not NULL\n");
  27566. + if (ixt->ixt_a_hmac_hash==NULL)
  27567. + barf_out(KERN_ERR "a_hmac_hash() must be not NULL\n");
  27568. + ret=0;
  27569. +out:
  27570. + return ret;
  27571. +}
  27572. +
  27573. +/*
  27574. + * Generic (enc, auth) registration entry point
  27575. + */
  27576. +int register_ipsec_alg(struct ipsec_alg *ixt)
  27577. +{
  27578. + int ret=-EINVAL;
  27579. + /* Validation */
  27580. + if (ixt==NULL)
  27581. + barf_out("NULL ipsec_alg object passed\n");
  27582. + if ((ixt->ixt_version&0xffffff00) != (IPSEC_ALG_VERSION&0xffffff00))
  27583. + barf_out("incorrect version: %d.%d.%d-%d, "
  27584. + "must be %d.%d.%d[-%d]\n",
  27585. + IPSEC_ALG_VERSION_QUAD(ixt->ixt_version),
  27586. + IPSEC_ALG_VERSION_QUAD(IPSEC_ALG_VERSION));
  27587. +
  27588. + switch(ixt->ixt_alg_type) {
  27589. + case IPSEC_ALG_TYPE_AUTH:
  27590. + if ((ret=check_auth((struct ipsec_alg_auth *)ixt)<0))
  27591. + goto out;
  27592. + break;
  27593. + case IPSEC_ALG_TYPE_ENCRYPT:
  27594. + if ((ret=check_enc((struct ipsec_alg_enc *)ixt)<0))
  27595. + goto out;
  27596. + /*
  27597. + * Adapted two lines below:
  27598. + * ivlen == 0 is possible (NULL enc has blocksize==1)
  27599. + *
  27600. + * fixed NULL support by David De Reu <DeReu@tComLabs.com>
  27601. + */
  27602. + if (ixt->ixt_support.ias_ivlen == 0
  27603. + && ixt->ixt_blocksize > 1) {
  27604. + ixt->ixt_support.ias_ivlen = ixt->ixt_blocksize*8;
  27605. + }
  27606. + break;
  27607. + default:
  27608. + barf_out("alg_type=%d not supported\n", ixt->ixt_alg_type);
  27609. + }
  27610. + INIT_LIST_HEAD(&ixt->ixt_list);
  27611. + ret = ipsec_alg_insert(ixt);
  27612. + if (ret<0)
  27613. + barf_out(KERN_WARNING "ipsec_alg for alg_id=%d failed."
  27614. + "Not loaded (ret=%d).\n",
  27615. + ixt->ixt_support.ias_id, ret);
  27616. +
  27617. +
  27618. + ret = pfkey_list_insert_supported((struct ipsec_alg_supported *)&ixt->ixt_support
  27619. + , &(pfkey_supported_list[SADB_SATYPE_ESP]));
  27620. +
  27621. + if (ret==0) {
  27622. + ixt->ixt_state |= IPSEC_ALG_ST_SUPP;
  27623. + /* send register event to userspace */
  27624. + pfkey_register_reply(SADB_SATYPE_ESP, NULL);
  27625. + } else
  27626. + printk(KERN_ERR "pfkey_list_insert_supported returned %d. "
  27627. + "Loading anyway.\n", ret);
  27628. + ret=0;
  27629. +out:
  27630. + return ret;
  27631. +}
  27632. +
  27633. +/*
  27634. + * unregister ipsec_alg object from own tables, if
  27635. + * success => calls pfkey_list_remove_supported()
  27636. + */
  27637. +int unregister_ipsec_alg(struct ipsec_alg *ixt) {
  27638. + int ret= -EINVAL;
  27639. + switch(ixt->ixt_alg_type) {
  27640. + case IPSEC_ALG_TYPE_AUTH:
  27641. + case IPSEC_ALG_TYPE_ENCRYPT:
  27642. + break;
  27643. + default:
  27644. + /* this is not a typo :) */
  27645. + barf_out("frog found in list (\"%s\"): ixt_p=NULL\n",
  27646. + ixt->ixt_name);
  27647. + }
  27648. +
  27649. + ret=ipsec_alg_delete(ixt);
  27650. + if (ixt->ixt_state&IPSEC_ALG_ST_SUPP) {
  27651. + ixt->ixt_state &= ~IPSEC_ALG_ST_SUPP;
  27652. + pfkey_list_remove_supported((struct ipsec_alg_supported *)&ixt->ixt_support
  27653. + , &(pfkey_supported_list[SADB_SATYPE_ESP]));
  27654. +
  27655. + /* send register event to userspace */
  27656. + pfkey_register_reply(SADB_SATYPE_ESP, NULL);
  27657. + }
  27658. +
  27659. +out:
  27660. + return ret;
  27661. +}
  27662. +
  27663. +/*
  27664. + * Must be called from user context
  27665. + * used at module load type for testing algo implementation
  27666. + */
  27667. +static int ipsec_alg_test_encrypt(int enc_alg, int test) {
  27668. + int ret;
  27669. + caddr_t buf = NULL;
  27670. + int iv_size, keysize, key_e_size;
  27671. + struct ipsec_alg_enc *ixt_e;
  27672. + void *tmp_key_e = NULL;
  27673. + #define BUFSZ 1024
  27674. + #define MARGIN 0
  27675. + #define test_enc (buf+MARGIN)
  27676. + #define test_dec (test_enc+BUFSZ+MARGIN)
  27677. + #define test_tmp (test_dec+BUFSZ+MARGIN)
  27678. + #define test_key_e (test_tmp+BUFSZ+MARGIN)
  27679. + #define test_iv (test_key_e+key_e_size+MARGIN)
  27680. + #define test_key (test_iv+iv_size+MARGIN)
  27681. + #define test_size (BUFSZ*3+key_e_size+iv_size+keysize+MARGIN*7)
  27682. + ixt_e=(struct ipsec_alg_enc *)ipsec_alg_get(IPSEC_ALG_TYPE_ENCRYPT, enc_alg);
  27683. + if (ixt_e==NULL) {
  27684. + KLIPS_PRINT(1,
  27685. + "klips_debug: ipsec_alg_test_encrypt: "
  27686. + "encalg=%d object not found\n",
  27687. + enc_alg);
  27688. + ret=-EINVAL;
  27689. + goto out;
  27690. + }
  27691. + iv_size=ixt_e->ixt_common.ixt_support.ias_ivlen / 8;
  27692. + key_e_size=ixt_e->ixt_e_ctx_size;
  27693. + keysize=ixt_e->ixt_e_keylen;
  27694. + KLIPS_PRINT(1,
  27695. + "klips_debug: ipsec_alg_test_encrypt: "
  27696. + "enc_alg=%d blocksize=%d key_e_size=%d keysize=%d\n",
  27697. + enc_alg, iv_size, key_e_size, keysize);
  27698. + if ((buf=kmalloc (test_size, GFP_KERNEL)) == NULL) {
  27699. + ret= -ENOMEM;
  27700. + goto out;
  27701. + }
  27702. + get_random_bytes(test_key, keysize);
  27703. + get_random_bytes(test_iv, iv_size);
  27704. + if (ixt_e->ixt_e_new_key) {
  27705. + tmp_key_e = ixt_e->ixt_e_new_key(ixt_e, test_key, keysize);
  27706. + ret = tmp_key_e ? 0 : -EINVAL;
  27707. + } else {
  27708. + tmp_key_e = test_key_e;
  27709. + ret = ixt_e->ixt_e_set_key(ixt_e, test_key_e, test_key, keysize);
  27710. + }
  27711. + if (ret < 0)
  27712. + goto out;
  27713. + get_random_bytes(test_enc, BUFSZ);
  27714. + memcpy(test_tmp, test_enc, BUFSZ);
  27715. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, tmp_key_e, test_enc, BUFSZ, test_iv, 1);
  27716. + printk(KERN_INFO
  27717. + "klips_info: ipsec_alg_test_encrypt: "
  27718. + "cbc_encrypt=1 ret=%d\n",
  27719. + ret);
  27720. + ret=memcmp(test_enc, test_tmp, BUFSZ);
  27721. + printk(KERN_INFO
  27722. + "klips_info: ipsec_alg_test_encrypt: "
  27723. + "memcmp(enc, tmp) ret=%d: %s\n", ret,
  27724. + ret!=0? "OK. (encr->DIFFers)" : "FAIL! (encr->SAME)" );
  27725. + memcpy(test_dec, test_enc, BUFSZ);
  27726. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, tmp_key_e, test_dec, BUFSZ, test_iv, 0);
  27727. + printk(KERN_INFO
  27728. + "klips_info: ipsec_alg_test_encrypt: "
  27729. + "cbc_encrypt=0 ret=%d\n", ret);
  27730. + ret=memcmp(test_dec, test_tmp, BUFSZ);
  27731. + printk(KERN_INFO
  27732. + "klips_info: ipsec_alg_test_encrypt: "
  27733. + "memcmp(dec,tmp) ret=%d: %s\n", ret,
  27734. + ret==0? "OK. (encr->decr->SAME)" : "FAIL! (encr->decr->DIFFers)" );
  27735. + {
  27736. + /* Shamelessly taken from drivers/md sources O:) */
  27737. + unsigned long now;
  27738. + int i, count, max=0;
  27739. + int encrypt, speed;
  27740. + for (encrypt=0; encrypt <2;encrypt ++) {
  27741. + for (i = 0; i < 5; i++) {
  27742. + now = jiffies;
  27743. + count = 0;
  27744. + while (jiffies == now) {
  27745. + mb();
  27746. + ixt_e->ixt_e_cbc_encrypt(ixt_e,
  27747. + tmp_key_e, test_tmp,
  27748. + BUFSZ, test_iv, encrypt);
  27749. + mb();
  27750. + count++;
  27751. + mb();
  27752. + }
  27753. + if (count > max)
  27754. + max = count;
  27755. + }
  27756. + speed = max * (HZ * BUFSZ / 1024);
  27757. + printk(KERN_INFO
  27758. + "klips_info: ipsec_alg_test_encrypt: "
  27759. + "%s %s speed=%d KB/s\n",
  27760. + ixt_e->ixt_common.ixt_name,
  27761. + encrypt? "encrypt": "decrypt", speed);
  27762. + }
  27763. + }
  27764. +out:
  27765. + if (tmp_key_e && ixt_e->ixt_e_destroy_key) ixt_e->ixt_e_destroy_key(ixt_e, tmp_key_e);
  27766. + if (buf) kfree(buf);
  27767. + if (ixt_e) ipsec_alg_put((struct ipsec_alg *)ixt_e);
  27768. + return ret;
  27769. + #undef test_enc
  27770. + #undef test_dec
  27771. + #undef test_tmp
  27772. + #undef test_key_e
  27773. + #undef test_iv
  27774. + #undef test_key
  27775. + #undef test_size
  27776. +}
  27777. +
  27778. +/*
  27779. + * Must be called from user context
  27780. + * used at module load type for testing algo implementation
  27781. + */
  27782. +static int ipsec_alg_test_auth(int auth_alg, int test) {
  27783. + int ret;
  27784. + caddr_t buf = NULL;
  27785. + int blocksize, keysize, key_a_size;
  27786. + struct ipsec_alg_auth *ixt_a;
  27787. + #define BUFSZ 1024
  27788. + #define MARGIN 0
  27789. + #define test_auth (buf+MARGIN)
  27790. + #define test_key_a (test_auth+BUFSZ+MARGIN)
  27791. + #define test_key (test_key_a+key_a_size+MARGIN)
  27792. + #define test_hash (test_key+keysize+MARGIN)
  27793. + #define test_size (BUFSZ+key_a_size+keysize+AHHMAC_HASHLEN+MARGIN*4)
  27794. + ixt_a=(struct ipsec_alg_auth *)ipsec_alg_get(IPSEC_ALG_TYPE_AUTH, auth_alg);
  27795. + if (ixt_a==NULL) {
  27796. + KLIPS_PRINT(1,
  27797. + "klips_debug: ipsec_alg_test_auth: "
  27798. + "encalg=%d object not found\n",
  27799. + auth_alg);
  27800. + ret=-EINVAL;
  27801. + goto out;
  27802. + }
  27803. + blocksize=ixt_a->ixt_common.ixt_blocksize;
  27804. + key_a_size=ixt_a->ixt_a_ctx_size;
  27805. + keysize=ixt_a->ixt_a_keylen;
  27806. + KLIPS_PRINT(1,
  27807. + "klips_debug: ipsec_alg_test_auth: "
  27808. + "auth_alg=%d blocksize=%d key_a_size=%d keysize=%d\n",
  27809. + auth_alg, blocksize, key_a_size, keysize);
  27810. + if ((buf=kmalloc (test_size, GFP_KERNEL)) == NULL) {
  27811. + ret= -ENOMEM;
  27812. + goto out;
  27813. + }
  27814. + get_random_bytes(test_key, keysize);
  27815. + ret = ixt_a->ixt_a_hmac_set_key(ixt_a, test_key_a, test_key, keysize);
  27816. + if (ret < 0 )
  27817. + goto out;
  27818. + get_random_bytes(test_auth, BUFSZ);
  27819. + ret=ixt_a->ixt_a_hmac_hash(ixt_a, test_key_a, test_auth, BUFSZ, test_hash, AHHMAC_HASHLEN);
  27820. + printk(KERN_INFO
  27821. + "klips_info: ipsec_alg_test_auth: "
  27822. + "ret=%d\n", ret);
  27823. + {
  27824. + /* Shamelessly taken from drivers/md sources O:) */
  27825. + unsigned long now;
  27826. + int i, count, max=0;
  27827. + int speed;
  27828. + for (i = 0; i < 5; i++) {
  27829. + now = jiffies;
  27830. + count = 0;
  27831. + while (jiffies == now) {
  27832. + mb();
  27833. + ixt_a->ixt_a_hmac_hash(ixt_a, test_key_a, test_auth, BUFSZ, test_hash, AHHMAC_HASHLEN);
  27834. + mb();
  27835. + count++;
  27836. + mb();
  27837. + }
  27838. + if (count > max)
  27839. + max = count;
  27840. + }
  27841. + speed = max * (HZ * BUFSZ / 1024);
  27842. + printk(KERN_INFO
  27843. + "klips_info: ipsec_alg_test_auth: "
  27844. + "%s hash speed=%d KB/s\n",
  27845. + ixt_a->ixt_common.ixt_name,
  27846. + speed);
  27847. + }
  27848. +out:
  27849. + if (buf) kfree(buf);
  27850. + if (ixt_a) ipsec_alg_put((struct ipsec_alg *)ixt_a);
  27851. + return ret;
  27852. + #undef test_auth
  27853. + #undef test_key_a
  27854. + #undef test_key
  27855. + #undef test_hash
  27856. + #undef test_size
  27857. +}
  27858. +
  27859. +int ipsec_alg_test(unsigned alg_type, unsigned alg_id, int test) {
  27860. + switch(alg_type) {
  27861. + case IPSEC_ALG_TYPE_ENCRYPT:
  27862. + return ipsec_alg_test_encrypt(alg_id, test);
  27863. + break;
  27864. + case IPSEC_ALG_TYPE_AUTH:
  27865. + return ipsec_alg_test_auth(alg_id, test);
  27866. + break;
  27867. + }
  27868. + printk(KERN_ERR "klips_info: ipsec_alg_test() called incorrectly: "
  27869. + "alg_type=%d alg_id=%d\n",
  27870. + alg_type, alg_id);
  27871. + return -EINVAL;
  27872. +}
  27873. +
  27874. +int ipsec_alg_init(void) {
  27875. + KLIPS_PRINT(1, "klips_info:ipsec_alg_init: "
  27876. + "KLIPS alg v=%d.%d.%d-%d (EALG_MAX=%d, AALG_MAX=%d)\n",
  27877. + IPSEC_ALG_VERSION_QUAD(IPSEC_ALG_VERSION),
  27878. + SADB_EALG_MAX, SADB_AALG_MAX);
  27879. + /* Initialize tables */
  27880. + write_lock_bh(&ipsec_alg_lock);
  27881. + ipsec_alg_hash_init();
  27882. + write_unlock_bh(&ipsec_alg_lock);
  27883. +
  27884. + /* Initialize static algos */
  27885. + KLIPS_PRINT(1, "klips_info:ipsec_alg_init: "
  27886. + "calling ipsec_alg_static_init()\n");
  27887. +
  27888. + /* If we are suppose to use our AES, and don't have
  27889. + * CryptoAPI enabled...
  27890. + */
  27891. +#if defined(CONFIG_KLIPS_ENC_AES) && CONFIG_KLIPS_ENC_AES && !defined(CONFIG_KLIPS_ENC_AES_MODULE)
  27892. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27893. +#warning "Using built-in AES rather than CryptoAPI AES"
  27894. +#endif
  27895. + {
  27896. + extern int ipsec_aes_init(void);
  27897. + ipsec_aes_init();
  27898. + }
  27899. +#endif
  27900. +
  27901. +#if defined(CONFIG_KLIPS_ENC_3DES) && CONFIG_KLIPS_ENC_3DES && !defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  27902. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27903. +#warning "Using built-in 3des rather than CryptoAPI 3des"
  27904. +#endif
  27905. + {
  27906. + extern int ipsec_3des_init(void);
  27907. + ipsec_3des_init();
  27908. + }
  27909. +#endif
  27910. +#if defined(CONFIG_KLIPS_ENC_NULL) && CONFIG_KLIPS_ENC_NULL && !defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  27911. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27912. +#warning "Using built-in null cipher rather than CryptoAPI null cipher"
  27913. +#endif
  27914. +#warning "Building with null cipher (ESP_NULL), blame on you :-)"
  27915. + {
  27916. + extern int ipsec_null_init(void);
  27917. + ipsec_null_init();
  27918. + }
  27919. +#endif
  27920. +
  27921. +
  27922. + /* If we are doing CryptoAPI, then init */
  27923. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI && !defined(CONFIG_KLIPS_ENC_CRYPTOAPI_MODULE)
  27924. + {
  27925. + extern int ipsec_cryptoapi_init(void);
  27926. + ipsec_cryptoapi_init();
  27927. + }
  27928. +#endif
  27929. +
  27930. +
  27931. + return 0;
  27932. +}
  27933. +
  27934. +/**********************************************
  27935. + *
  27936. + * INTERFACE for ipsec_sa init and wipe
  27937. + *
  27938. + **********************************************/
  27939. +
  27940. +/*
  27941. + * Called from pluto -> pfkey_v2_parser.c:pfkey_ipsec_sa_init()
  27942. + */
  27943. +int ipsec_alg_sa_init(struct ipsec_sa *sa_p) {
  27944. + struct ipsec_alg_enc *ixt_e;
  27945. + struct ipsec_alg_auth *ixt_a;
  27946. +
  27947. + /* Only ESP for now ... */
  27948. + if (sa_p->ips_said.proto != IPPROTO_ESP)
  27949. + return -EPROTONOSUPPORT;
  27950. +
  27951. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_init() :"
  27952. + "entering for encalg=%d, authalg=%d\n",
  27953. + sa_p->ips_encalg, sa_p->ips_authalg);
  27954. +
  27955. + if ((ixt_e=(struct ipsec_alg_enc *)
  27956. + ipsec_alg_get(IPSEC_ALG_TYPE_ENCRYPT, sa_p->ips_encalg))) {
  27957. + KLIPS_PRINT(debug_pfkey,
  27958. + "klips_debug: ipsec_alg_sa_init() :"
  27959. + "found ipsec_alg (ixt_e=%p) for encalg=%d\n",
  27960. + ixt_e, sa_p->ips_encalg);
  27961. + sa_p->ips_alg_enc=ixt_e;
  27962. + }
  27963. +
  27964. + if ((ixt_a=(struct ipsec_alg_auth *)
  27965. + ipsec_alg_get(IPSEC_ALG_TYPE_AUTH, sa_p->ips_authalg))) {
  27966. + KLIPS_PRINT(debug_pfkey,
  27967. + "klips_debug: ipsec_alg_sa_init() :"
  27968. + "found ipsec_alg (ixt_a=%p) for auth=%d\n",
  27969. + ixt_a, sa_p->ips_authalg);
  27970. + sa_p->ips_alg_auth=ixt_a;
  27971. + }
  27972. + return 0;
  27973. +}
  27974. +
  27975. +/*
  27976. + * Called from pluto -> ipsec_sa.c:ipsec_sa_delchain()
  27977. + */
  27978. +int ipsec_alg_sa_wipe(struct ipsec_sa *sa_p) {
  27979. + struct ipsec_alg *ixt;
  27980. + if ((ixt=(struct ipsec_alg *)sa_p->ips_alg_enc)) {
  27981. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_wipe() :"
  27982. + "unlinking for encalg=%d\n",
  27983. + ixt->ixt_support.ias_id);
  27984. + ipsec_alg_put(ixt);
  27985. + }
  27986. + if ((ixt=(struct ipsec_alg *)sa_p->ips_alg_auth)) {
  27987. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_wipe() :"
  27988. + "unlinking for authalg=%d\n",
  27989. + ixt->ixt_support.ias_id);
  27990. + ipsec_alg_put(ixt);
  27991. + }
  27992. + return 0;
  27993. +}
  27994. +
  27995. +IPSEC_PROCFS_DEBUG_NO_STATIC
  27996. +int
  27997. +ipsec_xform_get_info(char *buffer,
  27998. + char **start,
  27999. + off_t offset,
  28000. + int length IPSEC_PROC_LAST_ARG)
  28001. +{
  28002. + int len = 0;
  28003. + off_t begin = 0;
  28004. + int i;
  28005. + struct list_head *head;
  28006. + struct ipsec_alg *ixt;
  28007. +
  28008. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  28009. + "klips_debug:ipsec_tncfg_get_info: "
  28010. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  28011. + buffer,
  28012. + *start,
  28013. + (int)offset,
  28014. + length);
  28015. +
  28016. + for(i = 0, head = ipsec_alg_hash_table;
  28017. + i<IPSEC_ALG_HASHSZ;
  28018. + i++, head++)
  28019. + {
  28020. + struct list_head *p;
  28021. + for (p=head->next; p!=head; p=p->next)
  28022. + {
  28023. + ixt = list_entry(p, struct ipsec_alg, ixt_list);
  28024. + len += ipsec_snprintf(buffer+len, length-len,
  28025. + "VERSION=%d TYPE=%d ID=%d NAME=%s REFCNT=%d ",
  28026. + ixt->ixt_version, ixt->ixt_alg_type, ixt->ixt_support.ias_id,
  28027. + ixt->ixt_name, ixt->ixt_refcnt);
  28028. +
  28029. + len += ipsec_snprintf(buffer+len, length-len,
  28030. + "STATE=%08x BLOCKSIZE=%d IVLEN=%d KEYMINBITS=%d KEYMAXBITS=%d ",
  28031. + ixt->ixt_state, ixt->ixt_blocksize,
  28032. + ixt->ixt_support.ias_ivlen, ixt->ixt_support.ias_keyminbits, ixt->ixt_support.ias_keymaxbits);
  28033. +
  28034. + len += ipsec_snprintf(buffer+len, length-len,
  28035. + "IVLEN=%d KEYMINBITS=%d KEYMAXBITS=%d ",
  28036. + ixt->ixt_support.ias_ivlen, ixt->ixt_support.ias_keyminbits, ixt->ixt_support.ias_keymaxbits);
  28037. +
  28038. + switch(ixt->ixt_alg_type)
  28039. + {
  28040. + case IPSEC_ALG_TYPE_AUTH:
  28041. + {
  28042. + struct ipsec_alg_auth *auth = (struct ipsec_alg_auth *)ixt;
  28043. +
  28044. + len += ipsec_snprintf(buffer+len, length-len,
  28045. + "KEYLEN=%d CTXSIZE=%d AUTHLEN=%d ",
  28046. + auth->ixt_a_keylen, auth->ixt_a_ctx_size,
  28047. + auth->ixt_a_authlen);
  28048. + break;
  28049. + }
  28050. + case IPSEC_ALG_TYPE_ENCRYPT:
  28051. + {
  28052. + struct ipsec_alg_enc *enc = (struct ipsec_alg_enc *)ixt;
  28053. + len += ipsec_snprintf(buffer+len, length-len,
  28054. + "KEYLEN=%d CTXSIZE=%d ",
  28055. + enc->ixt_e_keylen, enc->ixt_e_ctx_size);
  28056. +
  28057. + break;
  28058. + }
  28059. + }
  28060. +
  28061. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  28062. + }
  28063. + }
  28064. +
  28065. + *start = buffer + (offset - begin); /* Start of wanted data */
  28066. + len -= (offset - begin); /* Start slop */
  28067. + if (len > length)
  28068. + len = length;
  28069. + return len;
  28070. +}
  28071. +
  28072. +
  28073. +/*
  28074. + * As the author of this module, I ONLY ALLOW using it from
  28075. + * GPL (or same LICENSE TERMS as kernel source) modules.
  28076. + *
  28077. + * In respect to hardware crypto engines this means:
  28078. + * * Closed-source device drivers ARE NOT ALLOWED to use
  28079. + * this interface.
  28080. + * * Closed-source VHDL/Verilog firmware running on
  28081. + * the crypto hardware device IS ALLOWED to use this interface
  28082. + * via a GPL (or same LICENSE TERMS as kernel source) device driver.
  28083. + * --Juan Jose Ciarlante 20/03/2002 (thanks RGB for the correct wording)
  28084. + */
  28085. +
  28086. +/*
  28087. + * These symbols can only be used from GPL modules
  28088. + * for now, I'm disabling this because it creates false
  28089. + * symbol problems for old modutils.
  28090. + */
  28091. +
  28092. +#ifdef CONFIG_MODULES
  28093. +#ifndef NET_26
  28094. +#if 0
  28095. +#ifndef EXPORT_SYMBOL_GPL
  28096. +#undef EXPORT_SYMBOL_GPL
  28097. +#define EXPORT_SYMBOL_GPL EXPORT_SYMBOL
  28098. +#endif
  28099. +#endif
  28100. +EXPORT_SYMBOL(register_ipsec_alg);
  28101. +EXPORT_SYMBOL(unregister_ipsec_alg);
  28102. +EXPORT_SYMBOL(ipsec_alg_test);
  28103. +#endif
  28104. +#endif
  28105. --- /dev/null Tue Mar 11 13:02:56 2003
  28106. +++ linux/net/ipsec/ipsec_alg_cryptoapi.c Mon Feb 9 13:51:03 2004
  28107. @@ -0,0 +1,455 @@
  28108. +/*
  28109. + * ipsec_alg to linux cryptoapi GLUE
  28110. + *
  28111. + * Authors: CODE.ar TEAM
  28112. + * Harpo MAxx <harpo@linuxmendoza.org.ar>
  28113. + * JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  28114. + * Luciano Ruete <docemeses@softhome.net>
  28115. + *
  28116. + * ipsec_alg_cryptoapi.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  28117. + *
  28118. + * This program is free software; you can redistribute it and/or modify it
  28119. + * under the terms of the GNU General Public License as published by the
  28120. + * Free Software Foundation; either version 2 of the License, or (at your
  28121. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  28122. + *
  28123. + * This program is distributed in the hope that it will be useful, but
  28124. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  28125. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  28126. + * for more details.
  28127. + *
  28128. + * Example usage:
  28129. + * modinfo -p ipsec_cryptoapi (quite useful info, including supported algos)
  28130. + * modprobe ipsec_cryptoapi
  28131. + * modprobe ipsec_cryptoapi test=1
  28132. + * modprobe ipsec_cryptoapi excl=1 (exclusive cipher/algo)
  28133. + * modprobe ipsec_cryptoapi noauto=1 aes=1 twofish=1 (only these ciphers)
  28134. + * modprobe ipsec_cryptoapi aes=128,128 (force these keylens)
  28135. + * modprobe ipsec_cryptoapi des_ede3=0 (everything but 3DES)
  28136. + */
  28137. +#ifndef AUTOCONF_INCLUDED
  28138. +#include <linux/config.h>
  28139. +#endif
  28140. +#include <linux/version.h>
  28141. +
  28142. +/*
  28143. + * special case: ipsec core modular with this static algo inside:
  28144. + * must avoid MODULE magic for this file
  28145. + */
  28146. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_CRYPTOAPI)
  28147. +#undef MODULE
  28148. +#endif
  28149. +
  28150. +#include <linux/module.h>
  28151. +#include <linux/init.h>
  28152. +
  28153. +#include <linux/kernel.h> /* printk() */
  28154. +#include <linux/errno.h> /* error codes */
  28155. +#include <linux/types.h> /* size_t */
  28156. +#include <linux/string.h>
  28157. +
  28158. +/* Check if __exit is defined, if not null it */
  28159. +#ifndef __exit
  28160. +#define __exit
  28161. +#endif
  28162. +
  28163. +/* warn the innocent */
  28164. +#if !defined (CONFIG_CRYPTO) && !defined (CONFIG_CRYPTO_MODULE)
  28165. +#warning "No linux CryptoAPI found, install 2.4.22+ or 2.6.x"
  28166. +#define NO_CRYPTOAPI_SUPPORT
  28167. +#endif
  28168. +
  28169. +#include "openswan.h"
  28170. +#include "openswan/ipsec_alg.h"
  28171. +#include "openswan/ipsec_policy.h"
  28172. +
  28173. +#include <linux/crypto.h>
  28174. +#ifdef CRYPTO_API_VERSION_CODE
  28175. +#warning "Old CryptoAPI is not supported. Only linux-2.4.22+ or linux-2.6.x are supported"
  28176. +#define NO_CRYPTOAPI_SUPPORT
  28177. +#endif
  28178. +
  28179. +#ifdef NO_CRYPTOAPI_SUPPORT
  28180. +#warning "Building an unusable module :P"
  28181. +/* Catch old CryptoAPI by not allowing module to load */
  28182. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_cryptoapi_init )
  28183. +{
  28184. + printk(KERN_WARNING "ipsec_cryptoapi.o was not built on stock Linux CryptoAPI (2.4.22+ or 2.6.x), not loading.\n");
  28185. + return -EINVAL;
  28186. +}
  28187. +#else
  28188. +#include <asm/scatterlist.h>
  28189. +#include <asm/pgtable.h>
  28190. +#include <linux/mm.h>
  28191. +
  28192. +#define CIPHERNAME_AES "aes"
  28193. +#define CIPHERNAME_1DES "des"
  28194. +#define CIPHERNAME_3DES "des3_ede"
  28195. +#define CIPHERNAME_BLOWFISH "blowfish"
  28196. +#define CIPHERNAME_CAST "cast5"
  28197. +#define CIPHERNAME_SERPENT "serpent"
  28198. +#define CIPHERNAME_TWOFISH "twofish"
  28199. +
  28200. +#define ESP_SERPENT 252 /* from ipsec drafts */
  28201. +#define ESP_TWOFISH 253 /* from ipsec drafts */
  28202. +
  28203. +#define DIGESTNAME_MD5 "md5"
  28204. +#define DIGESTNAME_SHA1 "sha1"
  28205. +
  28206. +MODULE_AUTHOR("Juanjo Ciarlante, Harpo MAxx, Luciano Ruete");
  28207. +static int debug_crypto=0;
  28208. +static int test_crypto=0;
  28209. +static int excl_crypto=0;
  28210. +
  28211. +static int noauto = 0;
  28212. +
  28213. +#ifdef module_param
  28214. +module_param(debug_crypto,int,0600)
  28215. +module_param(test_crypto,int,0600)
  28216. +module_param(excl_crypto,int,0600)
  28217. +
  28218. +module_param(noauto,int,0600)
  28219. +#else
  28220. +MODULE_PARM(debug_crypto, "i");
  28221. +MODULE_PARM(test_crypto, "i");
  28222. +MODULE_PARM(excl_crypto, "i");
  28223. +
  28224. +MODULE_PARM(noauto,"i");
  28225. +#endif
  28226. +MODULE_PARM_DESC(noauto, "Dont try all known algos, just setup enabled ones");
  28227. +
  28228. +#ifdef CONFIG_KLIPS_ENC_1DES
  28229. +static int des_ede1[] = {-1, -1};
  28230. +#endif
  28231. +static int des_ede3[] = {-1, -1};
  28232. +static int aes[] = {-1, -1};
  28233. +static int blowfish[] = {-1, -1};
  28234. +static int cast[] = {-1, -1};
  28235. +static int serpent[] = {-1, -1};
  28236. +static int twofish[] = {-1, -1};
  28237. +
  28238. +#ifdef CONFIG_KLIPS_ENC_1DES
  28239. +#ifdef module_param
  28240. +module_param_array(des_ede1,int,NULL,0)
  28241. +#else
  28242. +MODULE_PARM(des_ede1,"1-2i");
  28243. +#endif
  28244. +#endif
  28245. +#ifdef module_param
  28246. +module_param_array(des_ede3,int,NULL,0)
  28247. +module_param_array(aes,int,NULL,0)
  28248. +module_param_array(blowfish,int,NULL,0)
  28249. +module_param_array(cast,int,NULL,0)
  28250. +module_param_array(serpent,int,NULL,0)
  28251. +module_param_array(twofish,int,NULL,0)
  28252. +#else
  28253. +MODULE_PARM(des_ede3,"1-2i");
  28254. +MODULE_PARM(aes,"1-2i");
  28255. +MODULE_PARM(blowfish,"1-2i");
  28256. +MODULE_PARM(cast,"1-2i");
  28257. +MODULE_PARM(serpent,"1-2i");
  28258. +MODULE_PARM(twofish,"1-2i");
  28259. +#endif
  28260. +MODULE_PARM_DESC(des_ede1, "0: disable | 1: force_enable | min,max: dontuse");
  28261. +MODULE_PARM_DESC(des_ede3, "0: disable | 1: force_enable | min,max: dontuse");
  28262. +MODULE_PARM_DESC(aes, "0: disable | 1: force_enable | min,max: keybitlens");
  28263. +MODULE_PARM_DESC(blowfish, "0: disable | 1: force_enable | min,max: keybitlens");
  28264. +MODULE_PARM_DESC(cast, "0: disable | 1: force_enable | min,max: keybitlens");
  28265. +MODULE_PARM_DESC(serpent, "0: disable | 1: force_enable | min,max: keybitlens");
  28266. +MODULE_PARM_DESC(twofish, "0: disable | 1: force_enable | min,max: keybitlens");
  28267. +
  28268. +struct ipsec_alg_capi_cipher {
  28269. + const char *ciphername; /* cryptoapi's ciphername */
  28270. + unsigned blocksize;
  28271. + unsigned short minbits;
  28272. + unsigned short maxbits;
  28273. + int *parm; /* lkm param for this cipher */
  28274. + struct ipsec_alg_enc alg; /* note it's not a pointer */
  28275. +};
  28276. +
  28277. +static struct ipsec_alg_capi_cipher alg_capi_carray[] = {
  28278. + { CIPHERNAME_AES, 16, 128, 256, aes, { ixt_common:{ ixt_support:{ ias_id: ESP_AES}}}},
  28279. + { CIPHERNAME_TWOFISH, 16, 128, 256, twofish, { ixt_common:{ ixt_support:{ ias_id: ESP_TWOFISH,}}}},
  28280. + { CIPHERNAME_SERPENT, 16, 128, 256, serpent, { ixt_common:{ ixt_support:{ ias_id: ESP_SERPENT,}}}},
  28281. + { CIPHERNAME_CAST, 8, 128, 128, cast , { ixt_common:{ ixt_support:{ ias_id: ESP_CAST,}}}},
  28282. + { CIPHERNAME_BLOWFISH, 8, 96, 448, blowfish, { ixt_common:{ ixt_support:{ ias_id: ESP_BLOWFISH,}}}},
  28283. + { CIPHERNAME_3DES, 8, 192, 192, des_ede3, { ixt_common:{ ixt_support:{ ias_id: ESP_3DES,}}}},
  28284. +#ifdef CONFIG_KLIPS_ENC_1DES
  28285. + { CIPHERNAME_1DES, 8, 64, 64, des_ede1, { ixt_common:{ ixt_support:{ ias_id: ESP_DES,}}}},
  28286. +#endif
  28287. + { NULL, 0, 0, 0, NULL, {} }
  28288. +};
  28289. +
  28290. +#ifdef NOT_YET
  28291. +struct ipsec_alg_capi_digest {
  28292. + const char *digestname; /* cryptoapi's digestname */
  28293. + struct digest_implementation *di;
  28294. + struct ipsec_alg_auth alg; /* note it's not a pointer */
  28295. +};
  28296. +static struct ipsec_alg_capi_cipher alg_capi_darray[] = {
  28297. + { DIGESTNAME_MD5, NULL, { ixt_alg_id: AH_MD5, }},
  28298. + { DIGESTNAME_SHA1, NULL, { ixt_alg_id: AH_SHA, }},
  28299. + { NULL, NULL, {} }
  28300. +};
  28301. +#endif
  28302. +/*
  28303. + * "generic" linux cryptoapi setup_cipher() function
  28304. + */
  28305. +int setup_cipher(const char *ciphername)
  28306. +{
  28307. + return crypto_alg_available(ciphername, 0);
  28308. +}
  28309. +
  28310. +/*
  28311. + * setups ipsec_alg_capi_cipher "hyper" struct components, calling
  28312. + * register_ipsec_alg for cointaned ipsec_alg object
  28313. + */
  28314. +static void _capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e);
  28315. +static __u8 * _capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen);
  28316. +static int _capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt);
  28317. +
  28318. +static int
  28319. +setup_ipsec_alg_capi_cipher(struct ipsec_alg_capi_cipher *cptr)
  28320. +{
  28321. + int ret;
  28322. + cptr->alg.ixt_common.ixt_version = IPSEC_ALG_VERSION;
  28323. + cptr->alg.ixt_common.ixt_module = THIS_MODULE;
  28324. + atomic_set (& cptr->alg.ixt_common.ixt_refcnt, 0);
  28325. + strncpy (cptr->alg.ixt_common.ixt_name , cptr->ciphername, sizeof (cptr->alg.ixt_common.ixt_name));
  28326. +
  28327. + cptr->alg.ixt_common.ixt_blocksize=cptr->blocksize;
  28328. + cptr->alg.ixt_common.ixt_support.ias_keyminbits=cptr->minbits;
  28329. + cptr->alg.ixt_common.ixt_support.ias_keymaxbits=cptr->maxbits;
  28330. + cptr->alg.ixt_common.ixt_state = 0;
  28331. + if (excl_crypto) cptr->alg.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  28332. + cptr->alg.ixt_e_keylen=cptr->alg.ixt_common.ixt_support.ias_keymaxbits/8;
  28333. + cptr->alg.ixt_e_ctx_size = 0;
  28334. + cptr->alg.ixt_common.ixt_support.ias_exttype = IPSEC_ALG_TYPE_ENCRYPT;
  28335. + cptr->alg.ixt_e_new_key = _capi_new_key;
  28336. + cptr->alg.ixt_e_destroy_key = _capi_destroy_key;
  28337. + cptr->alg.ixt_e_cbc_encrypt = _capi_cbc_encrypt;
  28338. + cptr->alg.ixt_common.ixt_data = cptr;
  28339. +
  28340. + ret=register_ipsec_alg_enc(&cptr->alg);
  28341. + printk(KERN_INFO "KLIPS cryptoapi interface: "
  28342. + "alg_type=%d alg_id=%d name=%s "
  28343. + "keyminbits=%d keymaxbits=%d, %s(%d)\n",
  28344. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28345. + cptr->alg.ixt_common.ixt_support.ias_id,
  28346. + cptr->alg.ixt_common.ixt_name,
  28347. + cptr->alg.ixt_common.ixt_support.ias_keyminbits,
  28348. + cptr->alg.ixt_common.ixt_support.ias_keymaxbits,
  28349. + ret ? "not found" : "found", ret);
  28350. + return ret;
  28351. +}
  28352. +/*
  28353. + * called in ipsec_sa_wipe() time, will destroy key contexts
  28354. + * and do 1 unbind()
  28355. + */
  28356. +static void
  28357. +_capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e)
  28358. +{
  28359. + struct crypto_tfm *tfm=(struct crypto_tfm*)key_e;
  28360. +
  28361. + if (debug_crypto > 0)
  28362. + printk(KERN_DEBUG "klips_debug: _capi_destroy_key:"
  28363. + "name=%s key_e=%p \n",
  28364. + alg->ixt_common.ixt_name, key_e);
  28365. + if (!key_e) {
  28366. + printk(KERN_ERR "klips_debug: _capi_destroy_key:"
  28367. + "name=%s NULL key_e!\n",
  28368. + alg->ixt_common.ixt_name);
  28369. + return;
  28370. + }
  28371. + crypto_free_tfm(tfm);
  28372. +}
  28373. +
  28374. +/*
  28375. + * create new key context, need alg->ixt_data to know which
  28376. + * (of many) cipher inside this module is the target
  28377. + */
  28378. +static __u8 *
  28379. +_capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen)
  28380. +{
  28381. + struct ipsec_alg_capi_cipher *cptr;
  28382. + struct crypto_tfm *tfm=NULL;
  28383. +
  28384. + cptr = alg->ixt_common.ixt_data;
  28385. + if (!cptr) {
  28386. + printk(KERN_ERR "_capi_new_key(): "
  28387. + "NULL ixt_data (?!) for \"%s\" algo\n"
  28388. + , alg->ixt_common.ixt_name);
  28389. + goto err;
  28390. + }
  28391. + if (debug_crypto > 0)
  28392. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  28393. + "name=%s cptr=%p key=%p keysize=%d\n",
  28394. + alg->ixt_common.ixt_name, cptr, key, keylen);
  28395. +
  28396. + /*
  28397. + * alloc tfm
  28398. + */
  28399. + tfm = crypto_alloc_tfm(cptr->ciphername, CRYPTO_TFM_MODE_CBC);
  28400. + if (!tfm) {
  28401. + printk(KERN_ERR "_capi_new_key(): "
  28402. + "NULL tfm for \"%s\" cryptoapi (\"%s\") algo\n"
  28403. + , alg->ixt_common.ixt_name, cptr->ciphername);
  28404. + goto err;
  28405. + }
  28406. + if (crypto_cipher_setkey(tfm, key, keylen) < 0) {
  28407. + printk(KERN_ERR "_capi_new_key(): "
  28408. + "failed new_key() for \"%s\" cryptoapi algo (keylen=%d)\n"
  28409. + , alg->ixt_common.ixt_name, keylen);
  28410. + crypto_free_tfm(tfm);
  28411. + tfm=NULL;
  28412. + }
  28413. +err:
  28414. + if (debug_crypto > 0)
  28415. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  28416. + "name=%s key=%p keylen=%d tfm=%p\n",
  28417. + alg->ixt_common.ixt_name, key, keylen, tfm);
  28418. + return (__u8 *) tfm;
  28419. +}
  28420. +/*
  28421. + * core encryption function: will use cx->ci to call actual cipher's
  28422. + * cbc function
  28423. + */
  28424. +static int
  28425. +_capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt) {
  28426. + int error =0;
  28427. + struct crypto_tfm *tfm=(struct crypto_tfm *)key_e;
  28428. + struct scatterlist sg = {
  28429. + .page = virt_to_page(in),
  28430. + .offset = (unsigned long)(in) % PAGE_SIZE,
  28431. + .length=ilen,
  28432. + };
  28433. + if (debug_crypto > 1)
  28434. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  28435. + "key_e=%p "
  28436. + "in=%p out=%p ilen=%d iv=%p encrypt=%d\n"
  28437. + , key_e
  28438. + , in, in, ilen, iv, encrypt);
  28439. + crypto_cipher_set_iv(tfm, iv, crypto_tfm_alg_ivsize(tfm));
  28440. + if (encrypt)
  28441. + error = crypto_cipher_encrypt (tfm, &sg, &sg, ilen);
  28442. + else
  28443. + error = crypto_cipher_decrypt (tfm, &sg, &sg, ilen);
  28444. + if (debug_crypto > 1)
  28445. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  28446. + "error=%d\n"
  28447. + , error);
  28448. + return (error<0)? error : ilen;
  28449. +}
  28450. +/*
  28451. + * main initialization loop: for each cipher in list, do
  28452. + * 1) setup cryptoapi cipher else continue
  28453. + * 2) register ipsec_alg object
  28454. + */
  28455. +static int
  28456. +setup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28457. +{
  28458. + struct ipsec_alg_capi_cipher *cptr;
  28459. + /* foreach cipher in list ... */
  28460. + for (cptr=clist;cptr->ciphername;cptr++) {
  28461. + /*
  28462. + * see if cipher has been disabled (0) or
  28463. + * if noauto set and not enabled (1)
  28464. + */
  28465. + if (cptr->parm[0] == 0 || (noauto && cptr->parm[0] < 0)) {
  28466. + if (debug_crypto>0)
  28467. + printk(KERN_INFO "setup_cipher_list(): "
  28468. + "ciphername=%s skipped at user request: "
  28469. + "noauto=%d parm[0]=%d parm[1]=%d\n"
  28470. + , cptr->ciphername
  28471. + , noauto
  28472. + , cptr->parm[0]
  28473. + , cptr->parm[1]);
  28474. + continue;
  28475. + }
  28476. + /*
  28477. + * use a local ci to avoid touching cptr->ci,
  28478. + * if register ipsec_alg success then bind cipher
  28479. + */
  28480. + if(cptr->alg.ixt_common.ixt_support.ias_name == NULL) {
  28481. + cptr->alg.ixt_common.ixt_support.ias_name = cptr->ciphername;
  28482. + }
  28483. +
  28484. + if( setup_cipher(cptr->ciphername) ) {
  28485. + if (debug_crypto > 0)
  28486. + printk(KERN_DEBUG "klips_debug:"
  28487. + "setup_cipher_list():"
  28488. + "ciphername=%s found\n"
  28489. + , cptr->ciphername);
  28490. +
  28491. + if (setup_ipsec_alg_capi_cipher(cptr) != 0) {
  28492. + printk(KERN_ERR "klips_debug:"
  28493. + "setup_cipher_list():"
  28494. + "ciphername=%s failed ipsec_alg_register\n"
  28495. + , cptr->ciphername);
  28496. + }
  28497. + } else {
  28498. + printk(KERN_INFO "KLIPS: lookup for ciphername=%s: not found \n",
  28499. + cptr->ciphername);
  28500. + }
  28501. + }
  28502. + return 0;
  28503. +}
  28504. +/*
  28505. + * deregister ipsec_alg objects and unbind ciphers
  28506. + */
  28507. +static int
  28508. +unsetup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28509. +{
  28510. + struct ipsec_alg_capi_cipher *cptr;
  28511. + /* foreach cipher in list ... */
  28512. + for (cptr=clist;cptr->ciphername;cptr++) {
  28513. + if (cptr->alg.ixt_common.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  28514. + unregister_ipsec_alg_enc(&cptr->alg);
  28515. + }
  28516. + }
  28517. + return 0;
  28518. +}
  28519. +/*
  28520. + * test loop for registered algos
  28521. + */
  28522. +static int
  28523. +test_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28524. +{
  28525. + int test_ret;
  28526. + struct ipsec_alg_capi_cipher *cptr;
  28527. + /* foreach cipher in list ... */
  28528. + for (cptr=clist;cptr->ciphername;cptr++) {
  28529. + if (cptr->alg.ixt_common.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  28530. + test_ret=ipsec_alg_test(
  28531. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28532. + cptr->alg.ixt_common.ixt_support.ias_id,
  28533. + test_crypto);
  28534. + printk("test_cipher_list(alg_type=%d alg_id=%d): test_ret=%d\n",
  28535. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28536. + cptr->alg.ixt_common.ixt_support.ias_id,
  28537. + test_ret);
  28538. + }
  28539. + }
  28540. + return 0;
  28541. +}
  28542. +
  28543. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_cryptoapi_init )
  28544. +{
  28545. + int ret, test_ret;
  28546. + if ((ret=setup_cipher_list(alg_capi_carray)) < 0)
  28547. + return -EPROTONOSUPPORT;
  28548. + if (ret==0 && test_crypto) {
  28549. + test_ret=test_cipher_list(alg_capi_carray);
  28550. + }
  28551. + return ret;
  28552. +}
  28553. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_cryptoapi_fini )
  28554. +{
  28555. + unsetup_cipher_list(alg_capi_carray);
  28556. + return;
  28557. +}
  28558. +#ifdef MODULE_LICENSE
  28559. +MODULE_LICENSE("GPL");
  28560. +#endif
  28561. +
  28562. +#endif /* NO_CRYPTOAPI_SUPPORT */
  28563. --- /dev/null Tue Mar 11 13:02:56 2003
  28564. +++ linux/net/ipsec/ipsec_esp.c Mon Feb 9 13:51:03 2004
  28565. @@ -0,0 +1,607 @@
  28566. +/*
  28567. + * processing code for ESP
  28568. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  28569. + *
  28570. + * This program is free software; you can redistribute it and/or modify it
  28571. + * under the terms of the GNU General Public License as published by the
  28572. + * Free Software Foundation; either version 2 of the License, or (at your
  28573. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  28574. + *
  28575. + * This program is distributed in the hope that it will be useful, but
  28576. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  28577. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  28578. + * for more details.
  28579. + */
  28580. +
  28581. +char ipsec_esp_c_version[] = "RCSID $Id: ipsec_esp.c,v 1.13.2.6 2006/10/06 21:39:26 paul Exp $";
  28582. +#ifndef AUTOCONF_INCLUDED
  28583. +#include <linux/config.h>
  28584. +#endif
  28585. +#include <linux/version.h>
  28586. +
  28587. +#define __NO_VERSION__
  28588. +#include <linux/module.h>
  28589. +#include <linux/kernel.h> /* printk() */
  28590. +
  28591. +#include "openswan/ipsec_param.h"
  28592. +
  28593. +#ifdef MALLOC_SLAB
  28594. +# include <linux/slab.h> /* kmalloc() */
  28595. +#else /* MALLOC_SLAB */
  28596. +# include <linux/malloc.h> /* kmalloc() */
  28597. +#endif /* MALLOC_SLAB */
  28598. +#include <linux/errno.h> /* error codes */
  28599. +#include <linux/types.h> /* size_t */
  28600. +#include <linux/interrupt.h> /* mark_bh */
  28601. +
  28602. +#include <linux/netdevice.h> /* struct device, and other headers */
  28603. +#include <linux/etherdevice.h> /* eth_type_trans */
  28604. +#include <linux/ip.h> /* struct iphdr */
  28605. +#include <linux/skbuff.h>
  28606. +#include <openswan.h>
  28607. +#ifdef SPINLOCK
  28608. +# ifdef SPINLOCK_23
  28609. +# include <linux/spinlock.h> /* *lock* */
  28610. +# else /* SPINLOCK_23 */
  28611. +# include <asm/spinlock.h> /* *lock* */
  28612. +# endif /* SPINLOCK_23 */
  28613. +#endif /* SPINLOCK */
  28614. +
  28615. +#include <net/ip.h>
  28616. +#include <net/protocol.h>
  28617. +
  28618. +#include "openswan/radij.h"
  28619. +#include "openswan/ipsec_encap.h"
  28620. +#include "openswan/ipsec_sa.h"
  28621. +
  28622. +#include "openswan/ipsec_radij.h"
  28623. +#include "openswan/ipsec_xform.h"
  28624. +#include "openswan/ipsec_tunnel.h"
  28625. +#include "openswan/ipsec_rcv.h"
  28626. +#include "openswan/ipsec_xmit.h"
  28627. +
  28628. +#include "openswan/ipsec_auth.h"
  28629. +
  28630. +#ifdef CONFIG_KLIPS_ESP
  28631. +#include "openswan/ipsec_esp.h"
  28632. +#endif /* CONFIG_KLIPS_ESP */
  28633. +
  28634. +#include "openswan/ipsec_proto.h"
  28635. +#include "openswan/ipsec_alg.h"
  28636. +
  28637. +#ifdef CONFIG_KLIPS_DEBUG
  28638. +#define ESP_DMP(_x,_y,_z) if(debug_rcv && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  28639. +#else
  28640. +#define ESP_DMP(_x,_y,_z)
  28641. +#endif
  28642. +
  28643. +#ifdef CONFIG_KLIPS_ESP
  28644. +enum ipsec_rcv_value
  28645. +ipsec_rcv_esp_checks(struct ipsec_rcv_state *irs,
  28646. + struct sk_buff *skb)
  28647. +{
  28648. + __u8 proto;
  28649. + int len; /* packet length */
  28650. +
  28651. + len = skb->len;
  28652. + proto = irs->ipp->protocol;
  28653. +
  28654. + /* XXX this will need to be 8 for IPv6 */
  28655. + if ((proto == IPPROTO_ESP) && ((len - irs->iphlen) % 4)) {
  28656. + printk("klips_error:ipsec_rcv: "
  28657. + "got packet with content length = %d from %s -- should be on 4 octet boundary, packet dropped\n",
  28658. + len - irs->iphlen,
  28659. + irs->ipsaddr_txt);
  28660. + if(irs->stats) {
  28661. + irs->stats->rx_errors++;
  28662. + }
  28663. + return IPSEC_RCV_BADLEN;
  28664. + }
  28665. +
  28666. + if(skb->len < (irs->hard_header_len + sizeof(struct iphdr) + sizeof(struct esphdr))) {
  28667. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  28668. + "klips_debug:ipsec_rcv: "
  28669. + "runt esp packet of skb->len=%d received from %s, dropped.\n",
  28670. + skb->len,
  28671. + irs->ipsaddr_txt);
  28672. + if(irs->stats) {
  28673. + irs->stats->rx_errors++;
  28674. + }
  28675. + return IPSEC_RCV_BADLEN;
  28676. + }
  28677. +
  28678. + irs->protostuff.espstuff.espp = (struct esphdr *)skb->h.raw;
  28679. + irs->said.spi = irs->protostuff.espstuff.espp->esp_spi;
  28680. +
  28681. + return IPSEC_RCV_OK;
  28682. +}
  28683. +
  28684. +enum ipsec_rcv_value
  28685. +ipsec_rcv_esp_decrypt_setup(struct ipsec_rcv_state *irs,
  28686. + struct sk_buff *skb,
  28687. + __u32 *replay,
  28688. + unsigned char **authenticator)
  28689. +{
  28690. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28691. + //unsigned char *idat = (unsigned char *)espp;
  28692. +
  28693. + KLIPS_PRINT(debug_rcv,
  28694. + "klips_debug:ipsec_rcv: "
  28695. + "packet from %s received with seq=%d (iv)=0x%08x%08x iplen=%d esplen=%d sa=%s\n",
  28696. + irs->ipsaddr_txt,
  28697. + (__u32)ntohl(espp->esp_rpl),
  28698. + (__u32)ntohl(*((__u32 *)(espp->esp_iv) )),
  28699. + (__u32)ntohl(*((__u32 *)(espp->esp_iv) + 1)),
  28700. + irs->len,
  28701. + irs->ilen,
  28702. + irs->sa_len ? irs->sa : " (error)");
  28703. +
  28704. + *replay = ntohl(espp->esp_rpl);
  28705. + *authenticator = &(skb->h.raw[irs->ilen]);
  28706. +
  28707. + return IPSEC_RCV_OK;
  28708. +}
  28709. +
  28710. +enum ipsec_rcv_value
  28711. +ipsec_rcv_esp_authcalc(struct ipsec_rcv_state *irs,
  28712. + struct sk_buff *skb)
  28713. +{
  28714. + struct auth_alg *aa;
  28715. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28716. + union {
  28717. + MD5_CTX md5;
  28718. + SHA1_CTX sha1;
  28719. + } tctx;
  28720. +
  28721. + if (irs->ipsp->ips_alg_auth) {
  28722. + KLIPS_PRINT(debug_rcv,
  28723. + "klips_debug:ipsec_rcv: "
  28724. + "ipsec_alg hashing proto=%d... ",
  28725. + irs->said.proto);
  28726. + if(irs->said.proto == IPPROTO_ESP) {
  28727. + ipsec_alg_sa_esp_hash(irs->ipsp,
  28728. + (caddr_t)espp, irs->ilen,
  28729. + irs->hash, AHHMAC_HASHLEN);
  28730. + return IPSEC_RCV_OK;
  28731. + }
  28732. + return IPSEC_RCV_BADPROTO;
  28733. + }
  28734. + aa = irs->authfuncs;
  28735. +
  28736. + /* copy the initialized keying material */
  28737. + memcpy(&tctx, irs->ictx, irs->ictx_len);
  28738. +
  28739. +#ifdef HASH_DEBUG
  28740. + ESP_DMP("ictx", irs->ictx, irs->ictx_len);
  28741. +
  28742. + ESP_DMP("mac_esp", (caddr_t)espp, irs->ilen);
  28743. +#endif
  28744. + (*aa->update)((void *)&tctx, (caddr_t)espp, irs->ilen);
  28745. +
  28746. + (*aa->final)(irs->hash, (void *)&tctx);
  28747. +
  28748. +#ifdef HASH_DEBUG
  28749. + ESP_DMP("hash1", irs->hash, aa->hashlen);
  28750. +#endif
  28751. +
  28752. + memcpy(&tctx, irs->octx, irs->octx_len);
  28753. +
  28754. +#ifdef HASH_DEBUG
  28755. + ESP_DMP("octx", irs->octx, irs->octx_len);
  28756. +#endif
  28757. +
  28758. + (*aa->update)((void *)&tctx, irs->hash, aa->hashlen);
  28759. + (*aa->final)(irs->hash, (void *)&tctx);
  28760. +
  28761. + return IPSEC_RCV_OK;
  28762. +}
  28763. +
  28764. +
  28765. +enum ipsec_rcv_value
  28766. +ipsec_rcv_esp_decrypt(struct ipsec_rcv_state *irs)
  28767. +{
  28768. + struct ipsec_sa *ipsp = irs->ipsp;
  28769. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28770. + int i;
  28771. + int pad = 0, padlen;
  28772. + int badpad = 0;
  28773. + int esphlen = 0;
  28774. + __u8 *idat; /* pointer to content to be decrypted/authenticated */
  28775. + int encaplen = 0;
  28776. + struct sk_buff *skb;
  28777. + struct ipsec_alg_enc *ixt_e=NULL;
  28778. +
  28779. + skb=irs->skb;
  28780. +
  28781. + idat = skb->h.raw;
  28782. +
  28783. + /* encaplen is the distance between the end of the IP
  28784. + * header and the beginning of the ESP header.
  28785. + * on ESP headers it is zero, but on UDP-encap ESP
  28786. + * it includes the space for the UDP header.
  28787. + *
  28788. + * Note: UDP-encap code has already moved the
  28789. + * skb->data forward to accomodate this.
  28790. + */
  28791. + encaplen = idat - (skb->nh.raw + irs->iphlen);
  28792. +
  28793. + ixt_e=ipsp->ips_alg_enc;
  28794. + esphlen = ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  28795. + KLIPS_PRINT(debug_rcv,
  28796. + "klips_debug:ipsec_rcv: "
  28797. + "encalg=%d esphlen=%d\n",
  28798. + ipsp->ips_encalg, esphlen);
  28799. +
  28800. + idat += esphlen;
  28801. + irs->ilen -= esphlen;
  28802. +
  28803. + if (ipsec_alg_esp_encrypt(ipsp,
  28804. + idat, irs->ilen, espp->esp_iv,
  28805. + IPSEC_ALG_DECRYPT) <= 0) {
  28806. +#ifdef CONFIG_KLIPS_DEBUG
  28807. + KLIPS_ERROR(debug_rcv, "klips_error:ipsec_rcv: "
  28808. + "got packet with esplen = %d "
  28809. + "from %s -- should be on "
  28810. + "ENC(%d) octet boundary, "
  28811. + "packet dropped\n",
  28812. + irs->ilen,
  28813. + irs->ipsaddr_txt,
  28814. + ipsp->ips_encalg);
  28815. +#endif
  28816. + if(irs->stats) {
  28817. + irs->stats->rx_errors++;
  28818. + }
  28819. + return IPSEC_RCV_BAD_DECRYPT;
  28820. + }
  28821. +
  28822. + ESP_DMP("postdecrypt", idat, irs->ilen);
  28823. +
  28824. + irs->next_header = idat[irs->ilen - 1];
  28825. + padlen = idat[irs->ilen - 2];
  28826. + pad = padlen + 2 + irs->authlen;
  28827. +
  28828. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28829. + "klips_debug:ipsec_rcv: "
  28830. + "padlen=%d, contents: 0x<offset>: 0x<value> 0x<value> ...\n",
  28831. + padlen);
  28832. +
  28833. + for (i = 1; i <= padlen; i++) {
  28834. + if((i % 16) == 1) {
  28835. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28836. + "klips_debug: %02x:",
  28837. + i - 1);
  28838. + }
  28839. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28840. + " %02x",
  28841. + idat[irs->ilen - 2 - padlen + i - 1]);
  28842. + if(i != idat[irs->ilen - 2 - padlen + i - 1]) {
  28843. + badpad = 1;
  28844. + }
  28845. + if((i % 16) == 0) {
  28846. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28847. + "\n");
  28848. + }
  28849. + }
  28850. + if((i % 16) != 1) {
  28851. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28852. + "\n");
  28853. + }
  28854. + if(badpad) {
  28855. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28856. + "klips_debug:ipsec_rcv: "
  28857. + "warning, decrypted packet from %s has bad padding\n",
  28858. + irs->ipsaddr_txt);
  28859. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28860. + "klips_debug:ipsec_rcv: "
  28861. + "...may be bad decryption -- not dropped\n");
  28862. + ipsp->ips_errs.ips_encpad_errs += 1;
  28863. + }
  28864. +
  28865. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28866. + "klips_debug:ipsec_rcv: "
  28867. + "packet decrypted from %s: next_header = %d, padding = %d\n",
  28868. + irs->ipsaddr_txt,
  28869. + irs->next_header,
  28870. + pad - 2 - irs->authlen);
  28871. +
  28872. + irs->ipp->tot_len = htons(ntohs(irs->ipp->tot_len) - (esphlen + pad));
  28873. +
  28874. + /*
  28875. + * move the IP header forward by the size of the ESP header, which
  28876. + * will remove the the ESP header from the packet.
  28877. + *
  28878. + * XXX this is really unnecessary, since odds we are in tunnel
  28879. + * mode, and we will be *removing* this IP header.
  28880. + *
  28881. + */
  28882. + memmove((void *)(idat - irs->iphlen),
  28883. + (void *)(skb->nh.raw), irs->iphlen);
  28884. +
  28885. + ESP_DMP("esp postmove", (idat - irs->iphlen),
  28886. + irs->iphlen + irs->ilen);
  28887. +
  28888. + /* skb_pull below, will move up by esphlen */
  28889. +
  28890. + /* XXX not clear how this can happen, as the message indicates */
  28891. + if(skb->len < esphlen) {
  28892. + printk(KERN_WARNING
  28893. + "klips_error:ipsec_rcv: "
  28894. + "tried to skb_pull esphlen=%d, %d available. This should never happen, please report.\n",
  28895. + esphlen, (int)(skb->len));
  28896. + return IPSEC_RCV_ESP_DECAPFAIL;
  28897. + }
  28898. + skb_pull(skb, esphlen);
  28899. + skb->nh.raw = idat - irs->iphlen;
  28900. + irs->ipp = skb->nh.iph;
  28901. +
  28902. + ESP_DMP("esp postpull", skb->data, skb->len);
  28903. +
  28904. + /* now, trip off the padding from the end */
  28905. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  28906. + "klips_debug:ipsec_rcv: "
  28907. + "trimming to %d.\n",
  28908. + irs->len - esphlen - pad);
  28909. + if(pad + esphlen <= irs->len) {
  28910. + skb_trim(skb, irs->len - esphlen - pad);
  28911. + } else {
  28912. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  28913. + "klips_debug:ipsec_rcv: "
  28914. + "bogus packet, size is zero or negative, dropping.\n");
  28915. + return IPSEC_RCV_DECAPFAIL;
  28916. + }
  28917. +
  28918. + return IPSEC_RCV_OK;
  28919. +}
  28920. +
  28921. +/*
  28922. + *
  28923. + */
  28924. +enum ipsec_xmit_value
  28925. +ipsec_xmit_esp_setup(struct ipsec_xmit_state *ixs)
  28926. +{
  28927. +#ifdef CONFIG_KLIPS_ENC_3DES
  28928. + __u32 iv[2];
  28929. +#endif
  28930. + struct esphdr *espp;
  28931. + int ilen = 0;
  28932. + int padlen = 0, i;
  28933. + unsigned char *dat;
  28934. + unsigned char *idat, *pad;
  28935. + __u8 hash[AH_AMAX];
  28936. + union {
  28937. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  28938. + MD5_CTX md5;
  28939. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  28940. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  28941. + SHA1_CTX sha1;
  28942. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  28943. + } tctx;
  28944. +
  28945. + dat = (unsigned char *)ixs->iph;
  28946. +
  28947. + espp = (struct esphdr *)(dat + ixs->iphlen);
  28948. + espp->esp_spi = ixs->ipsp->ips_said.spi;
  28949. + espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  28950. +
  28951. + switch(ixs->ipsp->ips_encalg) {
  28952. +#if defined(CONFIG_KLIPS_ENC_3DES)
  28953. +#ifdef CONFIG_KLIPS_ENC_3DES
  28954. + case ESP_3DES:
  28955. +#endif /* CONFIG_KLIPS_ENC_3DES */
  28956. + iv[0] = *((__u32*)&(espp->esp_iv) ) =
  28957. + ((__u32*)(ixs->ipsp->ips_iv))[0];
  28958. + iv[1] = *((__u32*)&(espp->esp_iv) + 1) =
  28959. + ((__u32*)(ixs->ipsp->ips_iv))[1];
  28960. + break;
  28961. +#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
  28962. + default:
  28963. + ixs->stats->tx_errors++;
  28964. + return IPSEC_XMIT_ESP_BADALG;
  28965. + }
  28966. +
  28967. + idat = dat + ixs->iphlen + sizeof(struct esphdr);
  28968. + ilen = ixs->skb->len - (ixs->iphlen + sizeof(struct esphdr) + ixs->authlen);
  28969. +
  28970. + /* Self-describing padding */
  28971. + pad = &dat[ixs->skb->len - ixs->tailroom];
  28972. + padlen = ixs->tailroom - 2 - ixs->authlen;
  28973. + for (i = 0; i < padlen; i++) {
  28974. + pad[i] = i + 1;
  28975. + }
  28976. + dat[ixs->skb->len - ixs->authlen - 2] = padlen;
  28977. +
  28978. + dat[ixs->skb->len - ixs->authlen - 1] = ixs->iph->protocol;
  28979. + ixs->iph->protocol = IPPROTO_ESP;
  28980. +
  28981. + switch(ixs->ipsp->ips_encalg) {
  28982. +#ifdef CONFIG_KLIPS_ENC_3DES
  28983. + case ESP_3DES:
  28984. + des_ede3_cbc_encrypt((des_cblock *)idat,
  28985. + (des_cblock *)idat,
  28986. + ilen,
  28987. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[0].ks,
  28988. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[1].ks,
  28989. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[2].ks,
  28990. + (des_cblock *)iv, 1);
  28991. + break;
  28992. +#endif /* CONFIG_KLIPS_ENC_3DES */
  28993. + default:
  28994. + ixs->stats->tx_errors++;
  28995. + return IPSEC_XMIT_ESP_BADALG;
  28996. + }
  28997. +
  28998. + switch(ixs->ipsp->ips_encalg) {
  28999. +#if defined(CONFIG_KLIPS_ENC_3DES)
  29000. +#ifdef CONFIG_KLIPS_ENC_3DES
  29001. + case ESP_3DES:
  29002. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29003. + /* XXX update IV with the last 8 octets of the encryption */
  29004. +#if KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK
  29005. + ((__u32*)(ixs->ipsp->ips_iv))[0] =
  29006. + ((__u32 *)(idat))[(ilen >> 2) - 2];
  29007. + ((__u32*)(ixs->ipsp->ips_iv))[1] =
  29008. + ((__u32 *)(idat))[(ilen >> 2) - 1];
  29009. +#else /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
  29010. + prng_bytes(&ipsec_prng, (char *)ixs->ipsp->ips_iv, EMT_ESPDES_IV_SZ);
  29011. +#endif /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
  29012. + break;
  29013. +#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
  29014. + default:
  29015. + ixs->stats->tx_errors++;
  29016. + return IPSEC_XMIT_ESP_BADALG;
  29017. + }
  29018. +
  29019. + switch(ixs->ipsp->ips_authalg) {
  29020. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  29021. + case AH_MD5:
  29022. + ipsec_xmit_dmp("espp", (char*)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29023. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  29024. + ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  29025. + osMD5Update(&tctx.md5, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29026. + ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  29027. + osMD5Final(hash, &tctx.md5);
  29028. + ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
  29029. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  29030. + ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  29031. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  29032. + ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  29033. + osMD5Final(hash, &tctx.md5);
  29034. + ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
  29035. + memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
  29036. +
  29037. + /* paranoid */
  29038. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  29039. + memset((caddr_t)hash, 0, sizeof(*hash));
  29040. + break;
  29041. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  29042. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  29043. + case AH_SHA:
  29044. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  29045. + SHA1Update(&tctx.sha1, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29046. + SHA1Final(hash, &tctx.sha1);
  29047. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  29048. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  29049. + SHA1Final(hash, &tctx.sha1);
  29050. + memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
  29051. +
  29052. + /* paranoid */
  29053. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  29054. + memset((caddr_t)hash, 0, sizeof(*hash));
  29055. + break;
  29056. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  29057. + case AH_NONE:
  29058. + break;
  29059. + default:
  29060. + ixs->stats->tx_errors++;
  29061. + return IPSEC_XMIT_AH_BADALG;
  29062. + }
  29063. +
  29064. + ixs->skb->h.raw = (unsigned char*)espp;
  29065. +
  29066. + return IPSEC_XMIT_OK;
  29067. +}
  29068. +
  29069. +
  29070. +struct xform_functions esp_xform_funcs[]={
  29071. + { rcv_checks: ipsec_rcv_esp_checks,
  29072. + rcv_setup_auth: ipsec_rcv_esp_decrypt_setup,
  29073. + rcv_calc_auth: ipsec_rcv_esp_authcalc,
  29074. + rcv_decrypt: ipsec_rcv_esp_decrypt,
  29075. +
  29076. + xmit_setup: ipsec_xmit_esp_setup,
  29077. + xmit_headroom: sizeof(struct esphdr),
  29078. + xmit_needtailroom: 1,
  29079. + },
  29080. +};
  29081. +
  29082. +#ifdef NET_26
  29083. +struct inet_protocol esp_protocol = {
  29084. + .handler = ipsec_rcv,
  29085. + .no_policy = 1,
  29086. +};
  29087. +#else
  29088. +struct inet_protocol esp_protocol =
  29089. +{
  29090. + ipsec_rcv, /* ESP handler */
  29091. + NULL, /* TUNNEL error control */
  29092. +#ifdef NETDEV_25
  29093. + 1, /* no policy */
  29094. +#else
  29095. + 0, /* next */
  29096. + IPPROTO_ESP, /* protocol ID */
  29097. + 0, /* copy */
  29098. + NULL, /* data */
  29099. + "ESP" /* name */
  29100. +#endif
  29101. +};
  29102. +#endif /* NET_26 */
  29103. +
  29104. +#endif /* !CONFIG_KLIPS_ESP */
  29105. +
  29106. +
  29107. +/*
  29108. + * $Log: ipsec_esp.c,v $
  29109. + * Revision 1.13.2.6 2006/10/06 21:39:26 paul
  29110. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  29111. + * set. This is defined through autoconf.h which is included through the
  29112. + * linux kernel build macros.
  29113. + *
  29114. + * Revision 1.13.2.5 2006/08/24 03:02:01 paul
  29115. + * Compile fixes for when CONFIG_KLIPS_DEBUG is not set. (bug #642)
  29116. + *
  29117. + * Revision 1.13.2.4 2006/05/06 03:07:38 ken
  29118. + * Pull in proper padsize->tailroom fix from #public
  29119. + * Need to do correct math on padlen since padsize is not equal to tailroom
  29120. + *
  29121. + * Revision 1.13.2.3 2006/05/05 03:58:04 ken
  29122. + * ixs->padsize becomes ixs->tailroom
  29123. + *
  29124. + * Revision 1.13.2.2 2006/05/01 14:36:03 mcr
  29125. + * use KLIPS_ERROR for fatal things.
  29126. + *
  29127. + * Revision 1.13.2.1 2006/04/20 16:33:06 mcr
  29128. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  29129. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  29130. + *
  29131. + * Revision 1.13 2005/05/21 03:19:57 mcr
  29132. + * hash ctx is not really that interesting most of the time.
  29133. + *
  29134. + * Revision 1.12 2005/05/11 01:28:49 mcr
  29135. + * removed "poor-man"s OOP in favour of proper C structures.
  29136. + *
  29137. + * Revision 1.11 2005/04/29 05:10:22 mcr
  29138. + * removed from extraenous includes to make unit testing easier.
  29139. + *
  29140. + * Revision 1.10 2005/04/17 04:36:14 mcr
  29141. + * code now deals with ESP and UDP-ESP code.
  29142. + *
  29143. + * Revision 1.9 2005/04/15 19:52:30 mcr
  29144. + * adjustments to use proper skb fields for data.
  29145. + *
  29146. + * Revision 1.8 2004/09/14 00:22:57 mcr
  29147. + * adjustment of MD5* functions.
  29148. + *
  29149. + * Revision 1.7 2004/09/13 02:23:01 mcr
  29150. + * #define inet_protocol if necessary.
  29151. + *
  29152. + * Revision 1.6 2004/09/06 18:35:49 mcr
  29153. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  29154. + * so adjust for that.
  29155. + *
  29156. + * Revision 1.5 2004/08/17 03:27:23 mcr
  29157. + * klips 2.6 edits.
  29158. + *
  29159. + * Revision 1.4 2004/08/04 15:57:07 mcr
  29160. + * moved des .h files to include/des/ *
  29161. + * included 2.6 protocol specific things
  29162. + * started at NAT-T support, but it will require a kernel patch.
  29163. + *
  29164. + * Revision 1.3 2004/07/10 19:11:18 mcr
  29165. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  29166. + *
  29167. + * Revision 1.2 2004/04/06 02:49:25 mcr
  29168. + * pullup of algo code from alg-branch.
  29169. + *
  29170. + *
  29171. + *
  29172. + */
  29173. --- /dev/null Tue Mar 11 13:02:56 2003
  29174. +++ linux/net/ipsec/ipsec_init.c Mon Feb 9 13:51:03 2004
  29175. @@ -0,0 +1,614 @@
  29176. +/*
  29177. + * @(#) Initialization code.
  29178. + * Copyright (C) 1996, 1997 John Ioannidis.
  29179. + * Copyright (C) 1998 - 2002 Richard Guy Briggs <rgb@freeswan.org>
  29180. + * 2001 - 2004 Michael Richardson <mcr@xelerance.com>
  29181. + *
  29182. + * This program is free software; you can redistribute it and/or modify it
  29183. + * under the terms of the GNU General Public License as published by the
  29184. + * Free Software Foundation; either version 2 of the License, or (at your
  29185. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  29186. + *
  29187. + * This program is distributed in the hope that it will be useful, but
  29188. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29189. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  29190. + * for more details.
  29191. + *
  29192. + * /proc system code was split out into ipsec_proc.c after rev. 1.70.
  29193. + *
  29194. + */
  29195. +
  29196. +char ipsec_init_c_version[] = "RCSID $Id: ipsec_init.c,v 1.104.2.4 2006/10/06 21:39:26 paul Exp $";
  29197. +
  29198. +#ifndef AUTOCONF_INCLUDED
  29199. +#include <linux/config.h>
  29200. +#endif
  29201. +#include <linux/version.h>
  29202. +#include <linux/module.h>
  29203. +#include <linux/kernel.h> /* printk() */
  29204. +
  29205. +#include "openswan/ipsec_param.h"
  29206. +
  29207. +#ifdef MALLOC_SLAB
  29208. +# include <linux/slab.h> /* kmalloc() */
  29209. +#else /* MALLOC_SLAB */
  29210. +# include <linux/malloc.h> /* kmalloc() */
  29211. +#endif /* MALLOC_SLAB */
  29212. +#include <linux/errno.h> /* error codes */
  29213. +#include <linux/types.h> /* size_t */
  29214. +#include <linux/interrupt.h> /* mark_bh */
  29215. +
  29216. +#include <linux/netdevice.h> /* struct device, and other headers */
  29217. +#include <linux/etherdevice.h> /* eth_type_trans */
  29218. +#include <linux/ip.h> /* struct iphdr */
  29219. +#include <linux/in.h> /* struct sockaddr_in */
  29220. +#include <linux/skbuff.h>
  29221. +#include <linux/random.h> /* get_random_bytes() */
  29222. +#include <net/protocol.h>
  29223. +
  29224. +#include <openswan.h>
  29225. +
  29226. +#ifdef SPINLOCK
  29227. +# ifdef SPINLOCK_23
  29228. +# include <linux/spinlock.h> /* *lock* */
  29229. +# else /* 23_SPINLOCK */
  29230. +# include <asm/spinlock.h> /* *lock* */
  29231. +# endif /* 23_SPINLOCK */
  29232. +#endif /* SPINLOCK */
  29233. +
  29234. +#include <net/ip.h>
  29235. +
  29236. +#ifdef CONFIG_PROC_FS
  29237. +# include <linux/proc_fs.h>
  29238. +#endif /* CONFIG_PROC_FS */
  29239. +
  29240. +#ifdef NETLINK_SOCK
  29241. +# include <linux/netlink.h>
  29242. +#else
  29243. +# include <net/netlink.h>
  29244. +#endif
  29245. +
  29246. +#include "openswan/radij.h"
  29247. +
  29248. +#include "openswan/ipsec_life.h"
  29249. +#include "openswan/ipsec_stats.h"
  29250. +#include "openswan/ipsec_sa.h"
  29251. +
  29252. +#include "openswan/ipsec_encap.h"
  29253. +#include "openswan/ipsec_radij.h"
  29254. +#include "openswan/ipsec_xform.h"
  29255. +#include "openswan/ipsec_tunnel.h"
  29256. +
  29257. +#include "openswan/ipsec_rcv.h"
  29258. +#include "openswan/ipsec_ah.h"
  29259. +#include "openswan/ipsec_esp.h"
  29260. +
  29261. +#ifdef CONFIG_KLIPS_IPCOMP
  29262. +# include "openswan/ipcomp.h"
  29263. +#endif /* CONFIG_KLIPS_IPCOMP */
  29264. +
  29265. +#include "openswan/ipsec_proto.h"
  29266. +#include "openswan/ipsec_alg.h"
  29267. +
  29268. +#include <pfkeyv2.h>
  29269. +#include <pfkey.h>
  29270. +
  29271. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29272. +#include <net/xfrmudp.h>
  29273. +#endif
  29274. +
  29275. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(HAVE_XFRM4_UDP_REGISTER)
  29276. +#warning "You are trying to build KLIPS2.6 with NAT-T support, but you did not"
  29277. +#error "properly apply the NAT-T patch to your 2.6 kernel source tree."
  29278. +#endif
  29279. +
  29280. +#if !defined(CONFIG_KLIPS_ESP) && !defined(CONFIG_KLIPS_AH)
  29281. +#error "kernel configuration must include ESP or AH"
  29282. +#endif
  29283. +
  29284. +/*
  29285. + * seems to be present in 2.4.10 (Linus), but also in some RH and other
  29286. + * distro kernels of a lower number.
  29287. + */
  29288. +#ifdef MODULE_LICENSE
  29289. +MODULE_LICENSE("GPL");
  29290. +#endif
  29291. +
  29292. +#ifdef CONFIG_KLIPS_DEBUG
  29293. +int debug_eroute = 0;
  29294. +int debug_spi = 0;
  29295. +int debug_netlink = 0;
  29296. +#endif /* CONFIG_KLIPS_DEBUG */
  29297. +
  29298. +struct prng ipsec_prng;
  29299. +
  29300. +
  29301. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29302. +xfrm4_rcv_encap_t klips_old_encap = NULL;
  29303. +#endif
  29304. +
  29305. +extern int ipsec_device_event(struct notifier_block *dnot, unsigned long event, void *ptr);
  29306. +/*
  29307. + * the following structure is required so that we receive
  29308. + * event notifications when network devices are enabled and
  29309. + * disabled (ifconfig up and down).
  29310. + */
  29311. +static struct notifier_block ipsec_dev_notifier={
  29312. + ipsec_device_event,
  29313. + NULL,
  29314. + 0
  29315. +};
  29316. +
  29317. +#ifdef CONFIG_SYSCTL
  29318. +extern int ipsec_sysctl_register(void);
  29319. +extern void ipsec_sysctl_unregister(void);
  29320. +#endif
  29321. +
  29322. +#if defined(NET_26) || defined(IPSKB_XFRM_TUNNEL_SIZE)
  29323. +static inline int
  29324. +openswan_inet_add_protocol(struct inet_protocol *prot, unsigned protocol)
  29325. +{
  29326. + return inet_add_protocol(prot, protocol);
  29327. +}
  29328. +
  29329. +static inline int
  29330. +openswan_inet_del_protocol(struct inet_protocol *prot, unsigned protocol)
  29331. +{
  29332. + return inet_del_protocol(prot, protocol);
  29333. +}
  29334. +
  29335. +#else
  29336. +static inline int
  29337. +openswan_inet_add_protocol(struct inet_protocol *prot, unsigned protocol)
  29338. +{
  29339. + inet_add_protocol(prot);
  29340. + return 0;
  29341. +}
  29342. +
  29343. +static inline int
  29344. +openswan_inet_del_protocol(struct inet_protocol *prot, unsigned protocol)
  29345. +{
  29346. + inet_del_protocol(prot);
  29347. + return 0;
  29348. +}
  29349. +
  29350. +#endif
  29351. +
  29352. +/* void */
  29353. +int
  29354. +ipsec_klips_init(void)
  29355. +{
  29356. + int error = 0;
  29357. + unsigned char seed[256];
  29358. +#ifdef CONFIG_KLIPS_ENC_3DES
  29359. + extern int des_check_key;
  29360. +
  29361. + /* turn off checking of keys */
  29362. + des_check_key=0;
  29363. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29364. +
  29365. + KLIPS_PRINT(1, "klips_info:ipsec_init: "
  29366. + "KLIPS startup, Openswan KLIPS IPsec stack version: %s\n",
  29367. + ipsec_version_code());
  29368. +
  29369. + error |= ipsec_proc_init();
  29370. +
  29371. +#ifdef SPINLOCK
  29372. + ipsec_sadb.sadb_lock = SPIN_LOCK_UNLOCKED;
  29373. +#else /* SPINLOCK */
  29374. + ipsec_sadb.sadb_lock = 0;
  29375. +#endif /* SPINLOCK */
  29376. +
  29377. +#ifndef SPINLOCK
  29378. + tdb_lock.lock = 0;
  29379. + eroute_lock.lock = 0;
  29380. +#endif /* !SPINLOCK */
  29381. +
  29382. + error |= ipsec_sadb_init();
  29383. + error |= ipsec_radijinit();
  29384. +
  29385. + error |= pfkey_init();
  29386. +
  29387. + error |= register_netdevice_notifier(&ipsec_dev_notifier);
  29388. +
  29389. +#ifdef CONFIG_KLIPS_ESP
  29390. + openswan_inet_add_protocol(&esp_protocol, IPPROTO_ESP);
  29391. +#endif /* CONFIG_KLIPS_ESP */
  29392. +
  29393. +#ifdef CONFIG_KLIPS_AH
  29394. + openswan_inet_add_protocol(&ah_protocol, IPPROTO_AH);
  29395. +#endif /* CONFIG_KLIPS_AH */
  29396. +
  29397. +/* we never actually link IPCOMP to the stack */
  29398. +#ifdef IPCOMP_USED_ALONE
  29399. +#ifdef CONFIG_KLIPS_IPCOMP
  29400. + openswan_inet_add_protocol(&comp_protocol, IPPROTO_COMP);
  29401. +#endif /* CONFIG_KLIPS_IPCOMP */
  29402. +#endif
  29403. +
  29404. + error |= ipsec_tunnel_init_devices();
  29405. +
  29406. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29407. + /* register our ESP-UDP handler */
  29408. + if(udp4_register_esp_rcvencap(klips26_rcv_encap
  29409. + , &klips_old_encap)!=0) {
  29410. + printk(KERN_ERR "KLIPS: can not register klips_rcv_encap function\n");
  29411. + }
  29412. +#endif
  29413. +
  29414. +
  29415. +#ifdef CONFIG_SYSCTL
  29416. + error |= ipsec_sysctl_register();
  29417. +#endif
  29418. +
  29419. + ipsec_alg_init();
  29420. +
  29421. + get_random_bytes((void *)seed, sizeof(seed));
  29422. + prng_init(&ipsec_prng, seed, sizeof(seed));
  29423. +
  29424. + return error;
  29425. +}
  29426. +
  29427. +
  29428. +/* void */
  29429. +int
  29430. +ipsec_cleanup(void)
  29431. +{
  29432. + int error = 0;
  29433. +
  29434. +#ifdef CONFIG_SYSCTL
  29435. + ipsec_sysctl_unregister();
  29436. +#endif
  29437. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29438. + if(udp4_unregister_esp_rcvencap(klips_old_encap) < 0) {
  29439. + printk(KERN_ERR "KLIPS: can not unregister klips_rcv_encap function\n");
  29440. + }
  29441. +#endif
  29442. +
  29443. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29444. + "klips_debug:ipsec_cleanup: "
  29445. + "calling ipsec_tunnel_cleanup_devices.\n");
  29446. + error |= ipsec_tunnel_cleanup_devices();
  29447. +
  29448. + KLIPS_PRINT(debug_netlink, "called ipsec_tunnel_cleanup_devices");
  29449. +
  29450. +/* we never actually link IPCOMP to the stack */
  29451. +#ifdef IPCOMP_USED_ALONE
  29452. +#ifdef CONFIG_KLIPS_IPCOMP
  29453. + if (openswan_inet_del_protocol(&comp_protocol, IPPROTO_COMP) < 0)
  29454. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29455. + "comp close: can't remove protocol\n");
  29456. +#endif /* CONFIG_KLIPS_IPCOMP */
  29457. +#endif /* IPCOMP_USED_ALONE */
  29458. +
  29459. +#ifdef CONFIG_KLIPS_AH
  29460. + if (openswan_inet_del_protocol(&ah_protocol, IPPROTO_AH) < 0)
  29461. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29462. + "ah close: can't remove protocol\n");
  29463. +#endif /* CONFIG_KLIPS_AH */
  29464. +
  29465. +#ifdef CONFIG_KLIPS_ESP
  29466. + if (openswan_inet_del_protocol(&esp_protocol, IPPROTO_ESP) < 0)
  29467. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29468. + "esp close: can't remove protocol\n");
  29469. +#endif /* CONFIG_KLIPS_ESP */
  29470. +
  29471. + error |= unregister_netdevice_notifier(&ipsec_dev_notifier);
  29472. +
  29473. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29474. + "klips_debug:ipsec_cleanup: "
  29475. + "calling ipsec_sadb_cleanup.\n");
  29476. + error |= ipsec_sadb_cleanup(0);
  29477. + error |= ipsec_sadb_free();
  29478. +
  29479. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29480. + "klips_debug:ipsec_cleanup: "
  29481. + "calling ipsec_radijcleanup.\n");
  29482. + error |= ipsec_radijcleanup();
  29483. +
  29484. + KLIPS_PRINT(debug_pfkey, /* debug_tunnel & DB_TN_INIT, */
  29485. + "klips_debug:ipsec_cleanup: "
  29486. + "calling pfkey_cleanup.\n");
  29487. + error |= pfkey_cleanup();
  29488. +
  29489. + ipsec_proc_cleanup();
  29490. +
  29491. + prng_final(&ipsec_prng);
  29492. +
  29493. + return error;
  29494. +}
  29495. +
  29496. +#ifdef MODULE
  29497. +int
  29498. +init_module(void)
  29499. +{
  29500. + int error = 0;
  29501. +
  29502. + error |= ipsec_klips_init();
  29503. +
  29504. + return error;
  29505. +}
  29506. +
  29507. +void
  29508. +cleanup_module(void)
  29509. +{
  29510. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29511. + "klips_debug:cleanup_module: "
  29512. + "calling ipsec_cleanup.\n");
  29513. +
  29514. + ipsec_cleanup();
  29515. +
  29516. + KLIPS_PRINT(1, "klips_info:cleanup_module: "
  29517. + "ipsec module unloaded.\n");
  29518. +}
  29519. +#endif /* MODULE */
  29520. +
  29521. +/*
  29522. + * $Log: ipsec_init.c,v $
  29523. + * Revision 1.104.2.4 2006/10/06 21:39:26 paul
  29524. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  29525. + * set. This is defined through autoconf.h which is included through the
  29526. + * linux kernel build macros.
  29527. + *
  29528. + * Revision 1.104.2.3 2006/07/31 15:25:20 paul
  29529. + * Check for NETKEY backport in Debian using IPSKB_XFRM_TUNNEL_SIZE to
  29530. + * determine wether inet_add_protocol needs the protocol argument.
  29531. + *
  29532. + * Revision 1.104.2.2 2006/04/20 16:33:06 mcr
  29533. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  29534. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  29535. + *
  29536. + * Revision 1.104.2.1 2005/08/12 01:18:20 ken
  29537. + * Warn people who don't have NAT-T patch applied, but try and compile NAT-T code
  29538. + *
  29539. + * Revision 1.105 2005/08/12 00:56:33 mcr
  29540. + * add warning for people who didn't apply nat-t patch.
  29541. + *
  29542. + * Revision 1.104 2005/07/08 15:51:41 mcr
  29543. + * removed duplicate NAT-T code.
  29544. + * if CONFIG_IPSEC_NAT_TRAVERSAL isn't defined, then there is no issue.
  29545. + *
  29546. + * Revision 1.103 2005/07/08 03:02:05 paul
  29547. + * Fixed garbled define that accidentally got commited to the real tree.
  29548. + *
  29549. + * Revision 1.102 2005/07/08 02:56:37 paul
  29550. + * gcc4 fixes that were not commited because vault was down
  29551. + *
  29552. + * Revision 1.101 2005/04/29 05:10:22 mcr
  29553. + * removed from extraenous includes to make unit testing easier.
  29554. + *
  29555. + * Revision 1.100 2005/04/10 22:56:09 mcr
  29556. + * change to udp.c registration API.
  29557. + *
  29558. + * Revision 1.99 2005/04/08 18:26:13 mcr
  29559. + * register with udp.c, the klips26 encap receive function
  29560. + *
  29561. + * Revision 1.98 2004/09/13 02:23:18 mcr
  29562. + * #define inet_protocol if necessary.
  29563. + *
  29564. + * Revision 1.97 2004/09/06 18:35:49 mcr
  29565. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  29566. + * so adjust for that.
  29567. + *
  29568. + * Revision 1.96 2004/08/17 03:27:23 mcr
  29569. + * klips 2.6 edits.
  29570. + *
  29571. + * Revision 1.95 2004/08/03 18:19:08 mcr
  29572. + * in 2.6, use "net_device" instead of #define device->net_device.
  29573. + * this probably breaks 2.0 compiles.
  29574. + *
  29575. + * Revision 1.94 2004/07/10 19:11:18 mcr
  29576. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  29577. + *
  29578. + * Revision 1.93 2004/04/06 02:49:26 mcr
  29579. + * pullup of algo code from alg-branch.
  29580. + *
  29581. + * Revision 1.92 2004/03/30 15:30:39 ken
  29582. + * Proper Capitalization
  29583. + *
  29584. + * Revision 1.91 2004/03/22 01:51:51 ken
  29585. + * We are open
  29586. + *
  29587. + * Revision 1.90.4.2 2004/04/05 04:30:46 mcr
  29588. + * patches for alg-branch to compile/work with 2.x openswan
  29589. + *
  29590. + * Revision 1.90.4.1 2003/12/22 15:25:52 jjo
  29591. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  29592. + *
  29593. + * Revision 1.90 2003/10/31 02:27:55 mcr
  29594. + * pulled up port-selector patches and sa_id elimination.
  29595. + *
  29596. + * Revision 1.89.4.1 2003/10/29 01:30:41 mcr
  29597. + * elimited "struct sa_id".
  29598. + *
  29599. + * Revision 1.89 2003/07/31 22:47:16 mcr
  29600. + * preliminary (untested by FS-team) 2.5 patches.
  29601. + *
  29602. + * Revision 1.88 2003/06/22 20:05:36 mcr
  29603. + * clarified why IPCOMP was not being registered, and put a new
  29604. + * #ifdef in rather than #if 0.
  29605. + *
  29606. + * Revision 1.87 2002/09/20 15:40:51 rgb
  29607. + * Added a lock to the global ipsec_sadb struct for future use.
  29608. + * Split ipsec_sadb_cleanup from new funciton ipsec_sadb_free to avoid problem
  29609. + * of freeing newly created structures when clearing the reftable upon startup
  29610. + * to start from a known state.
  29611. + *
  29612. + * Revision 1.86 2002/08/15 18:39:15 rgb
  29613. + * Move ipsec_prng outside debug code.
  29614. + *
  29615. + * Revision 1.85 2002/05/14 02:35:29 rgb
  29616. + * Change reference to tdb to ipsa.
  29617. + *
  29618. + * Revision 1.84 2002/04/24 07:55:32 mcr
  29619. + * #include patches and Makefiles for post-reorg compilation.
  29620. + *
  29621. + * Revision 1.83 2002/04/24 07:36:28 mcr
  29622. + * Moved from ./klips/net/ipsec/ipsec_init.c,v
  29623. + *
  29624. + * Revision 1.82 2002/04/20 00:12:25 rgb
  29625. + * Added esp IV CBC attack fix, disabled.
  29626. + *
  29627. + * Revision 1.81 2002/04/09 16:13:32 mcr
  29628. + * switch license to straight GPL.
  29629. + *
  29630. + * Revision 1.80 2002/03/24 07:34:08 rgb
  29631. + * Sanity check for at least one of AH or ESP configured.
  29632. + *
  29633. + * Revision 1.79 2002/02/05 22:55:15 mcr
  29634. + * added MODULE_LICENSE declaration.
  29635. + * This macro does not appear in all kernel versions (see comment).
  29636. + *
  29637. + * Revision 1.78 2002/01/29 17:17:55 mcr
  29638. + * moved include of ipsec_param.h to after include of linux/kernel.h
  29639. + * otherwise, it seems that some option that is set in ipsec_param.h
  29640. + * screws up something subtle in the include path to kernel.h, and
  29641. + * it complains on the snprintf() prototype.
  29642. + *
  29643. + * Revision 1.77 2002/01/29 04:00:51 mcr
  29644. + * more excise of kversions.h header.
  29645. + *
  29646. + * Revision 1.76 2002/01/29 02:13:17 mcr
  29647. + * introduction of ipsec_kversion.h means that include of
  29648. + * ipsec_param.h must preceed any decisions about what files to
  29649. + * include to deal with differences in kernel source.
  29650. + *
  29651. + * Revision 1.75 2001/11/26 09:23:48 rgb
  29652. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  29653. + *
  29654. + * Revision 1.74 2001/11/22 05:44:11 henry
  29655. + * new version stuff
  29656. + *
  29657. + * Revision 1.71.2.2 2001/10/22 20:51:00 mcr
  29658. + * explicitely set des_check_key.
  29659. + *
  29660. + * Revision 1.71.2.1 2001/09/25 02:19:39 mcr
  29661. + * /proc manipulation code moved to new ipsec_proc.c
  29662. + *
  29663. + * Revision 1.73 2001/11/06 19:47:17 rgb
  29664. + * Changed lifetime_packets to uint32 from uint64.
  29665. + *
  29666. + * Revision 1.72 2001/10/18 04:45:19 rgb
  29667. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  29668. + * lib/freeswan.h version macros moved to lib/kversions.h.
  29669. + * Other compiler directive cleanups.
  29670. + *
  29671. + * Revision 1.71 2001/09/20 15:32:45 rgb
  29672. + * Minor pfkey lifetime fixes.
  29673. + *
  29674. + * Revision 1.70 2001/07/06 19:51:21 rgb
  29675. + * Added inbound policy checking code for IPIP SAs.
  29676. + *
  29677. + * Revision 1.69 2001/06/14 19:33:26 rgb
  29678. + * Silence startup message for console, but allow it to be logged.
  29679. + * Update copyright date.
  29680. + *
  29681. + * Revision 1.68 2001/05/29 05:14:36 rgb
  29682. + * Added PMTU to /proc/net/ipsec_tncfg output. See 'man 5 ipsec_tncfg'.
  29683. + *
  29684. + * Revision 1.67 2001/05/04 16:34:52 rgb
  29685. + * Rremove erroneous checking of return codes for proc_net_* in 2.4.
  29686. + *
  29687. + * Revision 1.66 2001/05/03 19:40:34 rgb
  29688. + * Check error return codes in startup and shutdown.
  29689. + *
  29690. + * Revision 1.65 2001/02/28 05:03:27 rgb
  29691. + * Clean up and rationalise startup messages.
  29692. + *
  29693. + * Revision 1.64 2001/02/27 22:24:53 rgb
  29694. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  29695. + * Check for satoa() return codes.
  29696. + *
  29697. + * Revision 1.63 2000/11/29 20:14:06 rgb
  29698. + * Add src= to the output of /proc/net/ipsec_spi and delete dst from IPIP.
  29699. + *
  29700. + * Revision 1.62 2000/11/06 04:31:24 rgb
  29701. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  29702. + * Fixed longlong for pre-2.4 kernels (Svenning).
  29703. + * Add Svenning's adaptive content compression.
  29704. + * Disabled registration of ipcomp handler.
  29705. + *
  29706. + * Revision 1.61 2000/10/11 13:37:54 rgb
  29707. + * #ifdef out debug print that causes proc/net/ipsec_version to oops.
  29708. + *
  29709. + * Revision 1.60 2000/09/20 03:59:01 rgb
  29710. + * Change static info functions to DEBUG_NO_STATIC to reveal function names
  29711. + * in oopsen.
  29712. + *
  29713. + * Revision 1.59 2000/09/16 01:06:26 rgb
  29714. + * Added cast of var to silence compiler warning about long fed to int
  29715. + * format.
  29716. + *
  29717. + * Revision 1.58 2000/09/15 11:37:01 rgb
  29718. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  29719. + * IPCOMP zlib deflate code.
  29720. + *
  29721. + * Revision 1.57 2000/09/12 03:21:50 rgb
  29722. + * Moved radij_c_version printing to ipsec_version_get_info().
  29723. + * Reformatted ipsec_version_get_info().
  29724. + * Added sysctl_{,un}register() calls.
  29725. + *
  29726. + * Revision 1.56 2000/09/08 19:16:50 rgb
  29727. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  29728. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  29729. + *
  29730. + * Revision 1.55 2000/08/30 05:19:03 rgb
  29731. + * Cleaned up no longer used spi_next, netlink register/unregister, other
  29732. + * minor cleanup.
  29733. + * Removed cruft replaced by TDB_XFORM_NAME.
  29734. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  29735. + * Moved debug version strings to printk when /proc/net/ipsec_version is
  29736. + * called.
  29737. + *
  29738. + * Revision 1.54 2000/08/20 18:31:05 rgb
  29739. + * Changed cosmetic alignment in spi_info.
  29740. + * Changed addtime and usetime to use actual value which is relative
  29741. + * anyways, as intended. (Momchil)
  29742. + *
  29743. + * Revision 1.53 2000/08/18 17:37:03 rgb
  29744. + * Added an (int) cast to shut up the compiler...
  29745. + *
  29746. + * Revision 1.52 2000/08/01 14:51:50 rgb
  29747. + * Removed _all_ remaining traces of DES.
  29748. + *
  29749. + * Revision 1.51 2000/07/25 20:41:22 rgb
  29750. + * Removed duplicate parameter in spi_getinfo.
  29751. + *
  29752. + * Revision 1.50 2000/07/17 03:21:45 rgb
  29753. + * Removed /proc/net/ipsec_spinew.
  29754. + *
  29755. + * Revision 1.49 2000/06/28 05:46:51 rgb
  29756. + * Renamed ivlen to iv_bits for consistency.
  29757. + * Changed output of add and use times to be relative to now.
  29758. + *
  29759. + * Revision 1.48 2000/05/11 18:26:10 rgb
  29760. + * Commented out calls to netlink_attach/detach to avoid activating netlink
  29761. + * in the kenrel config.
  29762. + *
  29763. + * Revision 1.47 2000/05/10 22:35:26 rgb
  29764. + * Comment out most of the startup version information.
  29765. + *
  29766. + * Revision 1.46 2000/03/22 16:15:36 rgb
  29767. + * Fixed renaming of dev_get (MB).
  29768. + *
  29769. + * Revision 1.45 2000/03/16 06:40:48 rgb
  29770. + * Hardcode PF_KEYv2 support.
  29771. + *
  29772. + * Revision 1.44 2000/01/22 23:19:20 rgb
  29773. + * Simplified code to use existing macro TDB_XFORM_NAME().
  29774. + *
  29775. + * Revision 1.43 2000/01/21 06:14:04 rgb
  29776. + * Print individual stats only if non-zero.
  29777. + * Removed 'bits' from each keylength for brevity.
  29778. + * Shortened lifetimes legend for brevity.
  29779. + * Changed wording from 'last_used' to the clearer 'idle'.
  29780. + *
  29781. + * Revision 1.42 1999/12/31 14:57:19 rgb
  29782. + * MB fix for new dummy-less proc_get_info in 2.3.35.
  29783. + *
  29784. + *
  29785. + * Local variables:
  29786. + * c-file-style: "linux"
  29787. + * End:
  29788. + *
  29789. + */
  29790. --- /dev/null Tue Mar 11 13:02:56 2003
  29791. +++ linux/net/ipsec/ipsec_ipcomp.c Mon Feb 9 13:51:03 2004
  29792. @@ -0,0 +1,256 @@
  29793. +/*
  29794. + * processing code for IPCOMP
  29795. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  29796. + *
  29797. + * This program is free software; you can redistribute it and/or modify it
  29798. + * under the terms of the GNU General Public License as published by the
  29799. + * Free Software Foundation; either version 2 of the License, or (at your
  29800. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  29801. + *
  29802. + * This program is distributed in the hope that it will be useful, but
  29803. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29804. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  29805. + * for more details.
  29806. + */
  29807. +
  29808. +char ipsec_ipcomp_c_version[] = "RCSID $Id: ipsec_ipcomp.c,v 1.5.2.2 2006/10/06 21:39:26 paul Exp $";
  29809. +#ifndef AUTOCONF_INCLUDED
  29810. +#include <linux/config.h>
  29811. +#endif
  29812. +#include <linux/version.h>
  29813. +
  29814. +#define __NO_VERSION__
  29815. +#include <linux/module.h>
  29816. +#include <linux/kernel.h> /* printk() */
  29817. +
  29818. +#include "openswan/ipsec_param.h"
  29819. +
  29820. +#ifdef MALLOC_SLAB
  29821. +# include <linux/slab.h> /* kmalloc() */
  29822. +#else /* MALLOC_SLAB */
  29823. +# include <linux/malloc.h> /* kmalloc() */
  29824. +#endif /* MALLOC_SLAB */
  29825. +#include <linux/errno.h> /* error codes */
  29826. +#include <linux/types.h> /* size_t */
  29827. +#include <linux/interrupt.h> /* mark_bh */
  29828. +
  29829. +#include <linux/netdevice.h> /* struct device, and other headers */
  29830. +#include <linux/etherdevice.h> /* eth_type_trans */
  29831. +#include <linux/ip.h> /* struct iphdr */
  29832. +#include <linux/skbuff.h>
  29833. +#include <openswan.h>
  29834. +#ifdef SPINLOCK
  29835. +# ifdef SPINLOCK_23
  29836. +# include <linux/spinlock.h> /* *lock* */
  29837. +# else /* SPINLOCK_23 */
  29838. +# include <asm/spinlock.h> /* *lock* */
  29839. +# endif /* SPINLOCK_23 */
  29840. +#endif /* SPINLOCK */
  29841. +
  29842. +#include <net/ip.h>
  29843. +
  29844. +#include "openswan/radij.h"
  29845. +#include "openswan/ipsec_encap.h"
  29846. +#include "openswan/ipsec_sa.h"
  29847. +
  29848. +#include "openswan/ipsec_radij.h"
  29849. +#include "openswan/ipsec_xform.h"
  29850. +#include "openswan/ipsec_tunnel.h"
  29851. +#include "openswan/ipsec_rcv.h"
  29852. +#include "openswan/ipsec_xmit.h"
  29853. +
  29854. +#include "openswan/ipsec_auth.h"
  29855. +
  29856. +#ifdef CONFIG_KLIPS_IPCOMP
  29857. +#include "openswan/ipsec_ipcomp.h"
  29858. +#endif /* CONFIG_KLIPS_IPCOMP */
  29859. +
  29860. +#include "openswan/ipsec_proto.h"
  29861. +
  29862. +#ifdef CONFIG_KLIPS_DEBUG
  29863. +int debug_ipcomp = 0;
  29864. +#endif /* CONFIG_KLIPS_DEBUG */
  29865. +
  29866. +
  29867. +#ifdef CONFIG_KLIPS_IPCOMP
  29868. +enum ipsec_rcv_value
  29869. +ipsec_rcv_ipcomp_checks(struct ipsec_rcv_state *irs,
  29870. + struct sk_buff *skb)
  29871. +{
  29872. + int ipcompminlen;
  29873. +
  29874. + ipcompminlen = sizeof(struct iphdr);
  29875. +
  29876. + if(skb->len < (ipcompminlen + sizeof(struct ipcomphdr))) {
  29877. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  29878. + "klips_debug:ipsec_rcv: "
  29879. + "runt comp packet of skb->len=%d received from %s, dropped.\n",
  29880. + skb->len,
  29881. + irs->ipsaddr_txt);
  29882. + if(irs->stats) {
  29883. + irs->stats->rx_errors++;
  29884. + }
  29885. + return IPSEC_RCV_BADLEN;
  29886. + }
  29887. +
  29888. + irs->protostuff.ipcompstuff.compp = (struct ipcomphdr *)skb->h.raw;
  29889. + irs->said.spi = htonl((__u32)ntohs(irs->protostuff.ipcompstuff.compp->ipcomp_cpi));
  29890. + return IPSEC_RCV_OK;
  29891. +}
  29892. +
  29893. +enum ipsec_rcv_value
  29894. +ipsec_rcv_ipcomp_decomp(struct ipsec_rcv_state *irs)
  29895. +{
  29896. + unsigned int flags = 0;
  29897. + struct ipsec_sa *ipsp = irs->ipsp;
  29898. + struct sk_buff *skb;
  29899. +
  29900. + skb=irs->skb;
  29901. +
  29902. + ipsec_xmit_dmp("ipcomp", skb->h.raw, skb->len);
  29903. +
  29904. + if(ipsp == NULL) {
  29905. + return IPSEC_RCV_SAIDNOTFOUND;
  29906. + }
  29907. +
  29908. + if(sysctl_ipsec_inbound_policy_check &&
  29909. + ((((ntohl(ipsp->ips_said.spi) & 0x0000ffff) != ntohl(irs->said.spi)) &&
  29910. + (ipsp->ips_encalg != ntohl(irs->said.spi)) /* this is a workaround for peer non-compliance with rfc2393 */
  29911. + ))) {
  29912. + char sa2[SATOT_BUF];
  29913. + size_t sa_len2 = 0;
  29914. +
  29915. + sa_len2 = satot(&ipsp->ips_said, 0, sa2, sizeof(sa2));
  29916. +
  29917. + KLIPS_PRINT(debug_rcv,
  29918. + "klips_debug:ipsec_rcv: "
  29919. + "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",
  29920. + irs->sa_len ? irs->sa : " (error)",
  29921. + ipsp != NULL ? (sa_len2 ? sa2 : " (error)") : "NULL",
  29922. + ntohs(irs->protostuff.ipcompstuff.compp->ipcomp_cpi),
  29923. + (__u32)ntohl(irs->said.spi),
  29924. + ipsp != NULL ? (__u32)ntohl((ipsp->ips_said.spi)) : 0,
  29925. + ipsp != NULL ? (__u16)(ntohl(ipsp->ips_said.spi) & 0x0000ffff) : 0);
  29926. + if(irs->stats) {
  29927. + irs->stats->rx_dropped++;
  29928. + }
  29929. + return IPSEC_RCV_SAIDNOTFOUND;
  29930. + }
  29931. +
  29932. + ipsp->ips_comp_ratio_cbytes += ntohs(irs->ipp->tot_len);
  29933. + irs->next_header = irs->protostuff.ipcompstuff.compp->ipcomp_nh;
  29934. +
  29935. + skb = skb_decompress(skb, ipsp, &flags);
  29936. + if (!skb || flags) {
  29937. + spin_unlock(&tdb_lock);
  29938. + KLIPS_PRINT(debug_rcv,
  29939. + "klips_debug:ipsec_rcv: "
  29940. + "skb_decompress() returned error flags=%x, dropped.\n",
  29941. + flags);
  29942. + if (irs->stats) {
  29943. + if (flags)
  29944. + irs->stats->rx_errors++;
  29945. + else
  29946. + irs->stats->rx_dropped++;
  29947. + }
  29948. + return IPSEC_RCV_IPCOMPFAILED;
  29949. + }
  29950. +
  29951. + /* make sure we update the pointer */
  29952. + irs->skb = skb;
  29953. +
  29954. +#ifdef NET_21
  29955. + irs->ipp = skb->nh.iph;
  29956. +#else /* NET_21 */
  29957. + irs->ipp = skb->ip_hdr;
  29958. +#endif /* NET_21 */
  29959. +
  29960. + ipsp->ips_comp_ratio_dbytes += ntohs(irs->ipp->tot_len);
  29961. +
  29962. + KLIPS_PRINT(debug_rcv,
  29963. + "klips_debug:ipsec_rcv: "
  29964. + "packet decompressed SA(IPCA):%s cpi->spi=%08x spi=%08x, spi->cpi=%04x, nh=%d.\n",
  29965. + irs->sa_len ? irs->sa : " (error)",
  29966. + (__u32)ntohl(irs->said.spi),
  29967. + ipsp != NULL ? (__u32)ntohl((ipsp->ips_said.spi)) : 0,
  29968. + ipsp != NULL ? (__u16)(ntohl(ipsp->ips_said.spi) & 0x0000ffff) : 0,
  29969. + irs->next_header);
  29970. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, irs->ipp);
  29971. +
  29972. + return IPSEC_RCV_OK;
  29973. +}
  29974. +
  29975. +enum ipsec_xmit_value
  29976. +ipsec_xmit_ipcomp_setup(struct ipsec_xmit_state *ixs)
  29977. +{
  29978. + unsigned int flags = 0;
  29979. +#ifdef CONFIG_KLIPS_DEBUG
  29980. + unsigned int old_tot_len = ntohs(ixs->iph->tot_len);
  29981. +#endif /* CONFIG_KLIPS_DEBUG */
  29982. +
  29983. + ixs->ipsp->ips_comp_ratio_dbytes += ntohs(ixs->iph->tot_len);
  29984. +
  29985. + ixs->skb = skb_compress(ixs->skb, ixs->ipsp, &flags);
  29986. +
  29987. +#ifdef NET_21
  29988. + ixs->iph = ixs->skb->nh.iph;
  29989. +#else /* NET_21 */
  29990. + ixs->iph = ixs->skb->ip_hdr;
  29991. +#endif /* NET_21 */
  29992. +
  29993. + ixs->ipsp->ips_comp_ratio_cbytes += ntohs(ixs->iph->tot_len);
  29994. +
  29995. +#ifdef CONFIG_KLIPS_DEBUG
  29996. + if (debug_tunnel & DB_TN_CROUT)
  29997. + {
  29998. + if (old_tot_len > ntohs(ixs->iph->tot_len))
  29999. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  30000. + "klips_debug:ipsec_xmit_encap_once: "
  30001. + "packet shrunk from %d to %d bytes after compression, cpi=%04x (should be from spi=%08x, spi&0xffff=%04x.\n",
  30002. + old_tot_len, ntohs(ixs->iph->tot_len),
  30003. + ntohs(((struct ipcomphdr*)(((char*)ixs->iph) + ((ixs->iph->ihl) << 2)))->ipcomp_cpi),
  30004. + ntohl(ixs->ipsp->ips_said.spi),
  30005. + (__u16)(ntohl(ixs->ipsp->ips_said.spi) & 0x0000ffff));
  30006. + else
  30007. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  30008. + "klips_debug:ipsec_xmit_encap_once: "
  30009. + "packet did not compress (flags = %d).\n",
  30010. + flags);
  30011. + }
  30012. +#endif /* CONFIG_KLIPS_DEBUG */
  30013. +
  30014. + return IPSEC_XMIT_OK;
  30015. +}
  30016. +
  30017. +struct xform_functions ipcomp_xform_funcs[]={
  30018. + {rcv_checks: ipsec_rcv_ipcomp_checks,
  30019. + rcv_decrypt: ipsec_rcv_ipcomp_decomp,
  30020. + xmit_setup: ipsec_xmit_ipcomp_setup,
  30021. + xmit_headroom: 0,
  30022. + xmit_needtailroom: 0,
  30023. + },
  30024. +};
  30025. +
  30026. +#if 0
  30027. +/* We probably don't want to install a pure IPCOMP protocol handler, but
  30028. + only want to handle IPCOMP if it is encapsulated inside an ESP payload
  30029. + (which is already handled) */
  30030. +#ifdef CONFIG_KLIPS_IPCOMP
  30031. +struct inet_protocol comp_protocol =
  30032. +{
  30033. + ipsec_rcv, /* COMP handler */
  30034. + NULL, /* COMP error control */
  30035. +#ifdef NETDEV_25
  30036. + 1, /* no policy */
  30037. +#else
  30038. + 0, /* next */
  30039. + IPPROTO_COMP, /* protocol ID */
  30040. + 0, /* copy */
  30041. + NULL, /* data */
  30042. + "COMP" /* name */
  30043. +#endif
  30044. +};
  30045. +#endif /* CONFIG_KLIPS_IPCOMP */
  30046. +#endif
  30047. +
  30048. +#endif /* CONFIG_KLIPS_IPCOMP */
  30049. --- /dev/null Tue Mar 11 13:02:56 2003
  30050. +++ linux/net/ipsec/ipsec_ipip.c Mon Feb 9 13:51:03 2004
  30051. @@ -0,0 +1,122 @@
  30052. +/*
  30053. + * processing code for IPIP
  30054. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  30055. + *
  30056. + * This program is free software; you can redistribute it and/or modify it
  30057. + * under the terms of the GNU General Public License as published by the
  30058. + * Free Software Foundation; either version 2 of the License, or (at your
  30059. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30060. + *
  30061. + * This program is distributed in the hope that it will be useful, but
  30062. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30063. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30064. + * for more details.
  30065. + */
  30066. +
  30067. +char ipsec_ipip_c_version[] = "RCSID $Id: ipsec_ipip.c,v 1.3.2.3 2006/10/06 21:39:26 paul Exp $";
  30068. +#ifndef AUTOCONF_INCLUDED
  30069. +#include <linux/config.h>
  30070. +#endif
  30071. +#include <linux/version.h>
  30072. +
  30073. +#define __NO_VERSION__
  30074. +#include <linux/module.h>
  30075. +#include <linux/kernel.h> /* printk() */
  30076. +
  30077. +#include "openswan/ipsec_param.h"
  30078. +
  30079. +#ifdef MALLOC_SLAB
  30080. +# include <linux/slab.h> /* kmalloc() */
  30081. +#else /* MALLOC_SLAB */
  30082. +# include <linux/malloc.h> /* kmalloc() */
  30083. +#endif /* MALLOC_SLAB */
  30084. +#include <linux/errno.h> /* error codes */
  30085. +#include <linux/types.h> /* size_t */
  30086. +#include <linux/interrupt.h> /* mark_bh */
  30087. +
  30088. +#include <linux/netdevice.h> /* struct device, and other headers */
  30089. +#include <linux/etherdevice.h> /* eth_type_trans */
  30090. +#include <linux/ip.h> /* struct iphdr */
  30091. +#include <linux/skbuff.h>
  30092. +#include <openswan.h>
  30093. +#ifdef SPINLOCK
  30094. +# ifdef SPINLOCK_23
  30095. +# include <linux/spinlock.h> /* *lock* */
  30096. +# else /* SPINLOCK_23 */
  30097. +# include <asm/spinlock.h> /* *lock* */
  30098. +# endif /* SPINLOCK_23 */
  30099. +#endif /* SPINLOCK */
  30100. +
  30101. +#include <net/ip.h>
  30102. +
  30103. +#include "openswan/radij.h"
  30104. +#include "openswan/ipsec_encap.h"
  30105. +#include "openswan/ipsec_sa.h"
  30106. +
  30107. +#include "openswan/ipsec_radij.h"
  30108. +#include "openswan/ipsec_xform.h"
  30109. +#include "openswan/ipsec_tunnel.h"
  30110. +#include "openswan/ipsec_rcv.h"
  30111. +#include "openswan/ipsec_xmit.h"
  30112. +
  30113. +#include "openswan/ipsec_auth.h"
  30114. +#include "openswan/ipsec_ipip.h"
  30115. +#include "openswan/ipsec_param.h"
  30116. +
  30117. +#include "openswan/ipsec_proto.h"
  30118. +
  30119. +enum ipsec_xmit_value
  30120. +ipsec_xmit_ipip_setup(struct ipsec_xmit_state *ixs)
  30121. +{
  30122. + ixs->iph->version = 4;
  30123. +
  30124. + switch(sysctl_ipsec_tos) {
  30125. + case 0:
  30126. +#ifdef NET_21
  30127. + ixs->iph->tos = ixs->skb->nh.iph->tos;
  30128. +#else /* NET_21 */
  30129. + ixs->iph->tos = ixs->skb->ip_hdr->tos;
  30130. +#endif /* NET_21 */
  30131. + break;
  30132. + case 1:
  30133. + ixs->iph->tos = 0;
  30134. + break;
  30135. + default:
  30136. + break;
  30137. + }
  30138. + ixs->iph->ttl = SYSCTL_IPSEC_DEFAULT_TTL;
  30139. + ixs->iph->frag_off = 0;
  30140. + ixs->iph->saddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_s))->sin_addr.s_addr;
  30141. + ixs->iph->daddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_d))->sin_addr.s_addr;
  30142. + ixs->iph->protocol = IPPROTO_IPIP;
  30143. + ixs->iph->ihl = sizeof(struct iphdr) >> 2;
  30144. +
  30145. + KLIPS_IP_SELECT_IDENT(ixs->iph, ixs->skb);
  30146. +
  30147. + ixs->newdst = (__u32)ixs->iph->daddr;
  30148. + ixs->newsrc = (__u32)ixs->iph->saddr;
  30149. +
  30150. +#ifdef NET_21
  30151. + ixs->skb->h.ipiph = ixs->skb->nh.iph;
  30152. +#endif /* NET_21 */
  30153. + return IPSEC_XMIT_OK;
  30154. +}
  30155. +
  30156. +struct xform_functions ipip_xform_funcs[]={
  30157. + { rcv_checks: NULL,
  30158. + rcv_setup_auth: NULL,
  30159. + rcv_calc_auth: NULL,
  30160. + rcv_decrypt: NULL,
  30161. +
  30162. + xmit_setup: ipsec_xmit_ipip_setup,
  30163. + xmit_headroom: sizeof(struct iphdr),
  30164. + xmit_needtailroom: 0,
  30165. + },
  30166. +};
  30167. +
  30168. +
  30169. +
  30170. +
  30171. +
  30172. +
  30173. +
  30174. --- /dev/null Tue Mar 11 13:02:56 2003
  30175. +++ linux/net/ipsec/ipsec_kern24.c Mon Feb 9 13:51:03 2004
  30176. @@ -0,0 +1,74 @@
  30177. +/*
  30178. + * Copyright 2005 (C) Michael Richardson <mcr@xelerance.com>
  30179. + *
  30180. + * This is a file of functions which are present in 2.6 kernels,
  30181. + * but are not available by default in the 2.4 series.
  30182. + *
  30183. + * As such this code is usually from the Linux kernel, and is covered by
  30184. + * GPL.
  30185. + *
  30186. + * This program is free software; you can redistribute it and/or modify it
  30187. + * under the terms of the GNU General Public License as published by the
  30188. + * Free Software Foundation; either version 2 of the License, or (at your
  30189. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30190. + *
  30191. + * This program is distributed in the hope that it will be useful, but
  30192. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30193. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30194. + * for more details.
  30195. + *
  30196. + * $Id: ipsec_kern24.c,v 1.2 2005/05/20 03:19:18 mcr Exp $
  30197. + *
  30198. + */
  30199. +
  30200. +#include <linux/kernel.h>
  30201. +#include <linux/mm.h>
  30202. +#include <linux/spinlock.h>
  30203. +
  30204. +/*
  30205. + * printk rate limiting, lifted from the networking subsystem.
  30206. + *
  30207. + * This enforces a rate limit: not more than one kernel message
  30208. + * every printk_ratelimit_jiffies to make a denial-of-service
  30209. + * attack impossible.
  30210. + */
  30211. +static spinlock_t ratelimit_lock = SPIN_LOCK_UNLOCKED;
  30212. +
  30213. +int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  30214. +{
  30215. + static unsigned long toks = 10*5*HZ;
  30216. + static unsigned long last_msg;
  30217. + static int missed;
  30218. + unsigned long flags;
  30219. + unsigned long now = jiffies;
  30220. +
  30221. + spin_lock_irqsave(&ratelimit_lock, flags);
  30222. + toks += now - last_msg;
  30223. + last_msg = now;
  30224. + if (toks > (ratelimit_burst * ratelimit_jiffies))
  30225. + toks = ratelimit_burst * ratelimit_jiffies;
  30226. + if (toks >= ratelimit_jiffies) {
  30227. + int lost = missed;
  30228. + missed = 0;
  30229. + toks -= ratelimit_jiffies;
  30230. + spin_unlock_irqrestore(&ratelimit_lock, flags);
  30231. + if (lost)
  30232. + printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  30233. + return 1;
  30234. + }
  30235. + missed++;
  30236. + spin_unlock_irqrestore(&ratelimit_lock, flags);
  30237. + return 0;
  30238. +}
  30239. +
  30240. +/* minimum time in jiffies between messages */
  30241. +int printk_ratelimit_jiffies = 5*HZ;
  30242. +
  30243. +/* number of messages we send before ratelimiting */
  30244. +int printk_ratelimit_burst = 10;
  30245. +
  30246. +int printk_ratelimit(void)
  30247. +{
  30248. + return __printk_ratelimit(printk_ratelimit_jiffies,
  30249. + printk_ratelimit_burst);
  30250. +}
  30251. --- /dev/null Tue Mar 11 13:02:56 2003
  30252. +++ linux/net/ipsec/ipsec_life.c Mon Feb 9 13:51:03 2004
  30253. @@ -0,0 +1,273 @@
  30254. +/*
  30255. + * @(#) lifetime structure utilities
  30256. + *
  30257. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  30258. + * and Michael Richardson <mcr@freeswan.org>
  30259. + *
  30260. + * This program is free software; you can redistribute it and/or modify it
  30261. + * under the terms of the GNU General Public License as published by the
  30262. + * Free Software Foundation; either version 2 of the License, or (at your
  30263. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30264. + *
  30265. + * This program is distributed in the hope that it will be useful, but
  30266. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30267. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30268. + * for more details.
  30269. + *
  30270. + * RCSID $Id: ipsec_life.c,v 1.13.10.1 2006/10/06 21:39:26 paul Exp $
  30271. + *
  30272. + */
  30273. +
  30274. +/*
  30275. + * This provides series of utility functions for dealing with lifetime
  30276. + * structures.
  30277. + *
  30278. + * ipsec_check_lifetime - returns -1 hard lifetime exceeded
  30279. + * 0 soft lifetime exceeded
  30280. + * 1 everything is okay
  30281. + * based upon whether or not the count exceeds hard/soft
  30282. + *
  30283. + */
  30284. +
  30285. +#define __NO_VERSION__
  30286. +#include <linux/module.h>
  30287. +#ifndef AUTOCONF_INCLUDED
  30288. +#include <linux/config.h>
  30289. +#endif /* for CONFIG_IP_FORWARD */
  30290. +#include <linux/version.h>
  30291. +#include <linux/kernel.h> /* printk() */
  30292. +
  30293. +#include "openswan/ipsec_param.h"
  30294. +
  30295. +#include <linux/netdevice.h> /* struct device, struct net_device_stats and other headers */
  30296. +#include <linux/etherdevice.h> /* eth_type_trans */
  30297. +#include <linux/skbuff.h>
  30298. +#include <openswan.h>
  30299. +
  30300. +#include "openswan/radij.h"
  30301. +#include "openswan/ipsec_life.h"
  30302. +#include "openswan/ipsec_xform.h"
  30303. +#include "openswan/ipsec_eroute.h"
  30304. +#include "openswan/ipsec_encap.h"
  30305. +#include "openswan/ipsec_radij.h"
  30306. +
  30307. +#include "openswan/ipsec_sa.h"
  30308. +#include "openswan/ipsec_tunnel.h"
  30309. +#include "openswan/ipsec_ipe4.h"
  30310. +#include "openswan/ipsec_ah.h"
  30311. +#include "openswan/ipsec_esp.h"
  30312. +
  30313. +#ifdef CONFIG_KLIPS_IPCOMP
  30314. +#include "openswan/ipcomp.h"
  30315. +#endif /* CONFIG_KLIPS_IPCOMP */
  30316. +
  30317. +#include <pfkeyv2.h>
  30318. +#include <pfkey.h>
  30319. +
  30320. +#include "openswan/ipsec_proto.h"
  30321. +
  30322. +
  30323. +enum ipsec_life_alive
  30324. +ipsec_lifetime_check(struct ipsec_lifetime64 *il64,
  30325. + const char *lifename,
  30326. + const char *saname,
  30327. + enum ipsec_life_type ilt,
  30328. + enum ipsec_direction idir,
  30329. + struct ipsec_sa *ips)
  30330. +{
  30331. + __u64 count;
  30332. + const char *dir;
  30333. +
  30334. + if(saname == NULL) {
  30335. + saname = "unknown-SA";
  30336. + }
  30337. +
  30338. + if(idir == ipsec_incoming) {
  30339. + dir = "incoming";
  30340. + } else {
  30341. + dir = "outgoing";
  30342. + }
  30343. +
  30344. +
  30345. + if(ilt == ipsec_life_timebased) {
  30346. + count = jiffies/HZ - il64->ipl_count;
  30347. + } else {
  30348. + count = il64->ipl_count;
  30349. + }
  30350. +
  30351. + if(il64->ipl_hard &&
  30352. + (count > il64->ipl_hard)) {
  30353. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  30354. + "klips_debug:ipsec_lifetime_check: "
  30355. + "hard %s lifetime of SA:<%s%s%s> %s has been reached, SA expired, "
  30356. + "%s packet dropped.\n",
  30357. + lifename,
  30358. + IPS_XFORM_NAME(ips),
  30359. + saname,
  30360. + dir);
  30361. +
  30362. + pfkey_expire(ips, 1);
  30363. + return ipsec_life_harddied;
  30364. + }
  30365. +
  30366. + if(il64->ipl_soft &&
  30367. + (count > il64->ipl_soft)) {
  30368. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  30369. + "klips_debug:ipsec_lifetime_check: "
  30370. + "soft %s lifetime of SA:<%s%s%s> %s has been reached, SA expiring, "
  30371. + "soft expire message sent up, %s packet still processed.\n",
  30372. + lifename,
  30373. + IPS_XFORM_NAME(ips),
  30374. + saname,
  30375. + dir);
  30376. +
  30377. + if(ips->ips_state != SADB_SASTATE_DYING) {
  30378. + pfkey_expire(ips, 0);
  30379. + }
  30380. + ips->ips_state = SADB_SASTATE_DYING;
  30381. +
  30382. + return ipsec_life_softdied;
  30383. + }
  30384. + return ipsec_life_okay;
  30385. +}
  30386. +
  30387. +
  30388. +/*
  30389. + * This function takes a buffer (with length), a lifetime name and type,
  30390. + * and formats a string to represent the current values of the lifetime.
  30391. + *
  30392. + * It returns the number of bytes that the format took (or would take,
  30393. + * if the buffer were large enough: snprintf semantics).
  30394. + * This is used in /proc routines and in debug output.
  30395. + */
  30396. +int
  30397. +ipsec_lifetime_format(char *buffer,
  30398. + int buflen,
  30399. + char *lifename,
  30400. + enum ipsec_life_type timebaselife,
  30401. + struct ipsec_lifetime64 *lifetime)
  30402. +{
  30403. + int len = 0;
  30404. + __u64 count;
  30405. +
  30406. + if(timebaselife == ipsec_life_timebased) {
  30407. + count = jiffies/HZ - lifetime->ipl_count;
  30408. + } else {
  30409. + count = lifetime->ipl_count;
  30410. + }
  30411. +
  30412. + if(lifetime->ipl_count > 1 ||
  30413. + lifetime->ipl_soft ||
  30414. + lifetime->ipl_hard) {
  30415. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
  30416. + len = ipsec_snprintf(buffer, buflen,
  30417. + "%s(%Lu,%Lu,%Lu)",
  30418. + lifename,
  30419. + count,
  30420. + lifetime->ipl_soft,
  30421. + lifetime->ipl_hard);
  30422. +#else /* XXX high 32 bits are not displayed */
  30423. + len = ipsec_snprintf(buffer, buflen,
  30424. + "%s(%lu,%lu,%lu)",
  30425. + lifename,
  30426. + (unsigned long)count,
  30427. + (unsigned long)lifetime->ipl_soft,
  30428. + (unsigned long)lifetime->ipl_hard);
  30429. +#endif
  30430. + }
  30431. +
  30432. + return len;
  30433. +}
  30434. +
  30435. +void
  30436. +ipsec_lifetime_update_hard(struct ipsec_lifetime64 *lifetime,
  30437. + __u64 newvalue)
  30438. +{
  30439. + if(newvalue &&
  30440. + (!lifetime->ipl_hard ||
  30441. + (newvalue < lifetime->ipl_hard))) {
  30442. + lifetime->ipl_hard = newvalue;
  30443. +
  30444. + if(!lifetime->ipl_soft &&
  30445. + (lifetime->ipl_hard < lifetime->ipl_soft)) {
  30446. + lifetime->ipl_soft = lifetime->ipl_hard;
  30447. + }
  30448. + }
  30449. +}
  30450. +
  30451. +void
  30452. +ipsec_lifetime_update_soft(struct ipsec_lifetime64 *lifetime,
  30453. + __u64 newvalue)
  30454. +{
  30455. + if(newvalue &&
  30456. + (!lifetime->ipl_soft ||
  30457. + (newvalue < lifetime->ipl_soft))) {
  30458. + lifetime->ipl_soft = newvalue;
  30459. +
  30460. + if(lifetime->ipl_hard &&
  30461. + (lifetime->ipl_hard < lifetime->ipl_soft)) {
  30462. + lifetime->ipl_soft = lifetime->ipl_hard;
  30463. + }
  30464. + }
  30465. +}
  30466. +
  30467. +
  30468. +/*
  30469. + * $Log: ipsec_life.c,v $
  30470. + * Revision 1.13.10.1 2006/10/06 21:39:26 paul
  30471. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  30472. + * set. This is defined through autoconf.h which is included through the
  30473. + * linux kernel build macros.
  30474. + *
  30475. + * Revision 1.13 2004/07/10 19:11:18 mcr
  30476. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  30477. + *
  30478. + * Revision 1.12 2004/04/23 20:44:35 ken
  30479. + * Update comments
  30480. + *
  30481. + * Revision 1.11 2004/04/06 02:49:26 mcr
  30482. + * pullup of algo code from alg-branch.
  30483. + *
  30484. + * Revision 1.10 2004/03/30 11:03:10 paul
  30485. + * two more occurances of snprintf, found by Sam from a users oops msg.
  30486. + *
  30487. + * Revision 1.9 2003/10/31 02:27:55 mcr
  30488. + * pulled up port-selector patches and sa_id elimination.
  30489. + *
  30490. + * Revision 1.8.4.1 2003/10/29 01:30:41 mcr
  30491. + * elimited "struct sa_id".
  30492. + *
  30493. + * Revision 1.8 2003/02/06 02:00:10 rgb
  30494. + * Fixed incorrect debugging text label
  30495. + *
  30496. + * Revision 1.7 2002/05/23 07:16:26 rgb
  30497. + * Fixed absolute/relative reference to lifetime count printout.
  30498. + *
  30499. + * Revision 1.6 2002/04/24 07:55:32 mcr
  30500. + * #include patches and Makefiles for post-reorg compilation.
  30501. + *
  30502. + * Revision 1.5 2002/04/24 07:36:28 mcr
  30503. + * Moved from ./klips/net/ipsec/ipsec_life.c,v
  30504. + *
  30505. + * Revision 1.4 2002/01/29 17:17:55 mcr
  30506. + * moved include of ipsec_param.h to after include of linux/kernel.h
  30507. + * otherwise, it seems that some option that is set in ipsec_param.h
  30508. + * screws up something subtle in the include path to kernel.h, and
  30509. + * it complains on the snprintf() prototype.
  30510. + *
  30511. + * Revision 1.3 2002/01/29 02:13:17 mcr
  30512. + * introduction of ipsec_kversion.h means that include of
  30513. + * ipsec_param.h must preceed any decisions about what files to
  30514. + * include to deal with differences in kernel source.
  30515. + *
  30516. + * Revision 1.2 2001/11/26 09:16:14 rgb
  30517. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  30518. + *
  30519. + * Revision 1.1.2.1 2001/09/25 02:25:57 mcr
  30520. + * lifetime structure created and common functions created.
  30521. + *
  30522. + * Local variables:
  30523. + * c-file-style: "linux"
  30524. + * End:
  30525. + *
  30526. + */
  30527. --- /dev/null Tue Mar 11 13:02:56 2003
  30528. +++ linux/net/ipsec/ipsec_mast.c Mon Feb 9 13:51:03 2004
  30529. @@ -0,0 +1,1099 @@
  30530. +/*
  30531. + * IPSEC MAST code.
  30532. + * Copyright (C) 1996, 1997 John Ioannidis.
  30533. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Richard Guy Briggs.
  30534. + *
  30535. + * This program is free software; you can redistribute it and/or modify it
  30536. + * under the terms of the GNU General Public License as published by the
  30537. + * Free Software Foundation; either version 2 of the License, or (at your
  30538. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30539. + *
  30540. + * This program is distributed in the hope that it will be useful, but
  30541. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30542. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30543. + * for more details.
  30544. + */
  30545. +
  30546. +char ipsec_mast_c_version[] = "RCSID $Id: ipsec_mast.c,v 1.7.2.1 2006/10/06 21:39:26 paul Exp $";
  30547. +
  30548. +#define __NO_VERSION__
  30549. +#include <linux/module.h>
  30550. +#ifndef AUTOCONF_INCLUDED
  30551. +#include <linux/config.h>
  30552. +#endif /* for CONFIG_IP_FORWARD */
  30553. +#include <linux/version.h>
  30554. +#include <linux/kernel.h> /* printk() */
  30555. +
  30556. +#include "freeswan/ipsec_param.h"
  30557. +
  30558. +#ifdef MALLOC_SLAB
  30559. +# include <linux/slab.h> /* kmalloc() */
  30560. +#else /* MALLOC_SLAB */
  30561. +# include <linux/malloc.h> /* kmalloc() */
  30562. +#endif /* MALLOC_SLAB */
  30563. +#include <linux/errno.h> /* error codes */
  30564. +#include <linux/types.h> /* size_t */
  30565. +#include <linux/interrupt.h> /* mark_bh */
  30566. +
  30567. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  30568. +#include <linux/etherdevice.h> /* eth_type_trans */
  30569. +#include <linux/ip.h> /* struct iphdr */
  30570. +#include <linux/tcp.h> /* struct tcphdr */
  30571. +#include <linux/udp.h> /* struct udphdr */
  30572. +#include <linux/skbuff.h>
  30573. +#include <freeswan.h>
  30574. +#include <linux/in6.h>
  30575. +#include <net/dst.h>
  30576. +#undef dev_kfree_skb
  30577. +#define dev_kfree_skb(a,b) kfree_skb(a)
  30578. +#define PHYSDEV_TYPE
  30579. +#include <net/icmp.h> /* icmp_send() */
  30580. +#include <net/ip.h>
  30581. +#include <linux/netfilter_ipv4.h>
  30582. +
  30583. +#include <linux/if_arp.h>
  30584. +
  30585. +#include "freeswan/radij.h"
  30586. +#include "freeswan/ipsec_life.h"
  30587. +#include "freeswan/ipsec_xform.h"
  30588. +#include "freeswan/ipsec_eroute.h"
  30589. +#include "freeswan/ipsec_encap.h"
  30590. +#include "freeswan/ipsec_radij.h"
  30591. +#include "freeswan/ipsec_sa.h"
  30592. +#include "freeswan/ipsec_tunnel.h"
  30593. +#include "freeswan/ipsec_mast.h"
  30594. +#include "freeswan/ipsec_ipe4.h"
  30595. +#include "freeswan/ipsec_ah.h"
  30596. +#include "freeswan/ipsec_esp.h"
  30597. +
  30598. +#include <pfkeyv2.h>
  30599. +#include <pfkey.h>
  30600. +
  30601. +#include "freeswan/ipsec_proto.h"
  30602. +
  30603. +int ipsec_maxdevice_count = -1;
  30604. +
  30605. +DEBUG_NO_STATIC int
  30606. +ipsec_mast_open(struct net_device *dev)
  30607. +{
  30608. + struct ipsecpriv *prv = dev->priv;
  30609. +
  30610. + /*
  30611. + * Can't open until attached.
  30612. + */
  30613. +
  30614. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  30615. + "klips_debug:ipsec_mast_open: "
  30616. + "dev = %s, prv->dev = %s\n",
  30617. + dev->name, prv->dev?prv->dev->name:"NONE");
  30618. +
  30619. + if (prv->dev == NULL)
  30620. + return -ENODEV;
  30621. +
  30622. + KLIPS_INC_USE;
  30623. + return 0;
  30624. +}
  30625. +
  30626. +DEBUG_NO_STATIC int
  30627. +ipsec_mast_close(struct net_device *dev)
  30628. +{
  30629. + KLIPS_DEC_USE;
  30630. + return 0;
  30631. +}
  30632. +
  30633. +static inline int ipsec_mast_xmit2(struct sk_buff *skb)
  30634. +{
  30635. + return ip_send(skb);
  30636. +}
  30637. +
  30638. +enum ipsec_xmit_value
  30639. +ipsec_mast_send(struct ipsec_xmit_state*ixs)
  30640. +{
  30641. + /* new route/dst cache code from James Morris */
  30642. + ixs->skb->dev = ixs->physdev;
  30643. + /*skb_orphan(ixs->skb);*/
  30644. + if((ixs->error = ip_route_output(&ixs->route,
  30645. + ixs->skb->nh.iph->daddr,
  30646. + ixs->pass ? 0 : ixs->skb->nh.iph->saddr,
  30647. + RT_TOS(ixs->skb->nh.iph->tos),
  30648. + ixs->physdev->iflink /* rgb: should this be 0? */))) {
  30649. + ixs->stats->tx_errors++;
  30650. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30651. + "klips_debug:ipsec_xmit_send: "
  30652. + "ip_route_output failed with error code %d, rt->u.dst.dev=%s, dropped\n",
  30653. + ixs->error,
  30654. + ixs->route->u.dst.dev->name);
  30655. + return IPSEC_XMIT_ROUTEERR;
  30656. + }
  30657. + if(ixs->dev == ixs->route->u.dst.dev) {
  30658. + ip_rt_put(ixs->route);
  30659. + /* This is recursion, drop it. */
  30660. + ixs->stats->tx_errors++;
  30661. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30662. + "klips_debug:ipsec_xmit_send: "
  30663. + "suspect recursion, dev=rt->u.dst.dev=%s, dropped\n",
  30664. + ixs->dev->name);
  30665. + return IPSEC_XMIT_RECURSDETECT;
  30666. + }
  30667. + dst_release(ixs->skb->dst);
  30668. + ixs->skb->dst = &ixs->route->u.dst;
  30669. + ixs->stats->tx_bytes += ixs->skb->len;
  30670. + if(ixs->skb->len < ixs->skb->nh.raw - ixs->skb->data) {
  30671. + ixs->stats->tx_errors++;
  30672. + printk(KERN_WARNING
  30673. + "klips_error:ipsec_xmit_send: "
  30674. + "tried to __skb_pull nh-data=%ld, %d available. This should never happen, please report.\n",
  30675. + (unsigned long)(ixs->skb->nh.raw - ixs->skb->data),
  30676. + ixs->skb->len);
  30677. + return IPSEC_XMIT_PUSHPULLERR;
  30678. + }
  30679. + __skb_pull(ixs->skb, ixs->skb->nh.raw - ixs->skb->data);
  30680. +#ifdef SKB_RESET_NFCT
  30681. + nf_conntrack_put(ixs->skb->nfct);
  30682. + ixs->skb->nfct = NULL;
  30683. +#ifdef CONFIG_NETFILTER_DEBUG
  30684. + ixs->skb->nf_debug = 0;
  30685. +#endif /* CONFIG_NETFILTER_DEBUG */
  30686. +#endif /* SKB_RESET_NFCT */
  30687. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30688. + "klips_debug:ipsec_xmit_send: "
  30689. + "...done, calling ip_send() on device:%s\n",
  30690. + ixs->skb->dev ? ixs->skb->dev->name : "NULL");
  30691. + KLIPS_IP_PRINT(debug_mast & DB_MAST_XMIT, ixs->skb->nh.iph);
  30692. + {
  30693. + int err;
  30694. +
  30695. + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, ixs->skb, NULL, ixs->route->u.dst.dev,
  30696. + ipsec_mast_xmit2);
  30697. + if(err != NET_XMIT_SUCCESS && err != NET_XMIT_CN) {
  30698. + if(net_ratelimit())
  30699. + printk(KERN_ERR
  30700. + "klips_error:ipsec_xmit_send: "
  30701. + "ip_send() failed, err=%d\n",
  30702. + -err);
  30703. + ixs->stats->tx_errors++;
  30704. + ixs->stats->tx_aborted_errors++;
  30705. + ixs->skb = NULL;
  30706. + return IPSEC_XMIT_IPSENDFAILURE;
  30707. + }
  30708. + }
  30709. + ixs->stats->tx_packets++;
  30710. +
  30711. + ixs->skb = NULL;
  30712. +
  30713. + return IPSEC_XMIT_OK;
  30714. +}
  30715. +
  30716. +void
  30717. +ipsec_mast_cleanup(struct ipsec_xmit_state*ixs)
  30718. +{
  30719. +#if defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE)
  30720. + netif_wake_queue(ixs->dev);
  30721. +#else /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  30722. + ixs->dev->tbusy = 0;
  30723. +#endif /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  30724. + if(ixs->saved_header) {
  30725. + kfree(ixs->saved_header);
  30726. + }
  30727. + if(ixs->skb) {
  30728. + dev_kfree_skb(ixs->skb, FREE_WRITE);
  30729. + }
  30730. + if(ixs->oskb) {
  30731. + dev_kfree_skb(ixs->oskb, FREE_WRITE);
  30732. + }
  30733. + if (ixs->ips.ips_ident_s.data) {
  30734. + kfree(ixs->ips.ips_ident_s.data);
  30735. + }
  30736. + if (ixs->ips.ips_ident_d.data) {
  30737. + kfree(ixs->ips.ips_ident_d.data);
  30738. + }
  30739. +}
  30740. +
  30741. +#if 0
  30742. +/*
  30743. + * This function assumes it is being called from dev_queue_xmit()
  30744. + * and that skb is filled properly by that function.
  30745. + */
  30746. +int
  30747. +ipsec_mast_start_xmit(struct sk_buff *skb, struct net_device *dev, IPsecSAref_t SAref)
  30748. +{
  30749. + struct ipsec_xmit_state ixs_mem;
  30750. + struct ipsec_xmit_state *ixs = &ixs_mem;
  30751. + enum ipsec_xmit_value stat = IPSEC_XMIT_OK;
  30752. +
  30753. + /* dev could be a mast device, but should be optional, I think... */
  30754. + /* SAref is also optional, but one of the two must be present. */
  30755. + /* I wonder if it could accept no device or saref and guess? */
  30756. +
  30757. +/* ipsec_xmit_sanity_check_dev(ixs); */
  30758. +
  30759. + ipsec_xmit_sanity_check_skb(ixs);
  30760. +
  30761. + ipsec_xmit_adjust_hard_header(ixs);
  30762. +
  30763. + stat = ipsec_xmit_encap_bundle(ixs);
  30764. + if(stat != IPSEC_XMIT_OK) {
  30765. + /* SA processing failed */
  30766. + }
  30767. +
  30768. + ipsec_xmit_hard_header_restore();
  30769. +}
  30770. +#endif
  30771. +
  30772. +DEBUG_NO_STATIC struct net_device_stats *
  30773. +ipsec_mast_get_stats(struct net_device *dev)
  30774. +{
  30775. + return &(((struct ipsecpriv *)(dev->priv))->mystats);
  30776. +}
  30777. +
  30778. +/*
  30779. + * Revectored calls.
  30780. + * For each of these calls, a field exists in our private structure.
  30781. + */
  30782. +
  30783. +DEBUG_NO_STATIC int
  30784. +ipsec_mast_hard_header(struct sk_buff *skb, struct net_device *dev,
  30785. + unsigned short type, void *daddr, void *saddr, unsigned len)
  30786. +{
  30787. + struct ipsecpriv *prv = dev->priv;
  30788. + struct net_device *tmp;
  30789. + int ret;
  30790. + struct net_device_stats *stats; /* This device's statistics */
  30791. +
  30792. + if(skb == NULL) {
  30793. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30794. + "klips_debug:ipsec_mast_hard_header: "
  30795. + "no skb...\n");
  30796. + return -ENODATA;
  30797. + }
  30798. +
  30799. + if(dev == NULL) {
  30800. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30801. + "klips_debug:ipsec_mast_hard_header: "
  30802. + "no device...\n");
  30803. + return -ENODEV;
  30804. + }
  30805. +
  30806. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30807. + "klips_debug:ipsec_mast_hard_header: "
  30808. + "skb->dev=%s dev=%s.\n",
  30809. + skb->dev ? skb->dev->name : "NULL",
  30810. + dev->name);
  30811. +
  30812. + if(prv == NULL) {
  30813. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30814. + "klips_debug:ipsec_mast_hard_header: "
  30815. + "no private space associated with dev=%s\n",
  30816. + dev->name ? dev->name : "NULL");
  30817. + return -ENODEV;
  30818. + }
  30819. +
  30820. + stats = (struct net_device_stats *) &(prv->mystats);
  30821. +
  30822. + if(prv->dev == NULL) {
  30823. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30824. + "klips_debug:ipsec_mast_hard_header: "
  30825. + "no physical device associated with dev=%s\n",
  30826. + dev->name ? dev->name : "NULL");
  30827. + stats->tx_dropped++;
  30828. + return -ENODEV;
  30829. + }
  30830. +
  30831. + /* check if we have to send a IPv6 packet. It might be a Router
  30832. + Solicitation, where the building of the packet happens in
  30833. + reverse order:
  30834. + 1. ll hdr,
  30835. + 2. IPv6 hdr,
  30836. + 3. ICMPv6 hdr
  30837. + -> skb->nh.raw is still uninitialized when this function is
  30838. + called!! If this is no IPv6 packet, we can print debugging
  30839. + messages, otherwise we skip all debugging messages and just
  30840. + build the ll header */
  30841. + if(type != ETH_P_IPV6) {
  30842. + /* execute this only, if we don't have to build the
  30843. + header for a IPv6 packet */
  30844. + if(!prv->hard_header) {
  30845. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30846. + "klips_debug:ipsec_mast_hard_header: "
  30847. + "physical device has been detached, packet dropped 0p%p->0p%p len=%d type=%d dev=%s->NULL ",
  30848. + saddr,
  30849. + daddr,
  30850. + len,
  30851. + type,
  30852. + dev->name);
  30853. + KLIPS_PRINTMORE(debug_mast & DB_MAST_REVEC,
  30854. + "ip=%08x->%08x\n",
  30855. + (__u32)ntohl(skb->nh.iph->saddr),
  30856. + (__u32)ntohl(skb->nh.iph->daddr) );
  30857. + stats->tx_dropped++;
  30858. + return -ENODEV;
  30859. + }
  30860. +
  30861. +#define da ((struct net_device *)(prv->dev))->dev_addr
  30862. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30863. + "klips_debug:ipsec_mast_hard_header: "
  30864. + "Revectored 0p%p->0p%p len=%d type=%d dev=%s->%s dev_addr=%02x:%02x:%02x:%02x:%02x:%02x ",
  30865. + saddr,
  30866. + daddr,
  30867. + len,
  30868. + type,
  30869. + dev->name,
  30870. + prv->dev->name,
  30871. + da[0], da[1], da[2], da[3], da[4], da[5]);
  30872. + KLIPS_PRINTMORE(debug_mast & DB_MAST_REVEC,
  30873. + "ip=%08x->%08x\n",
  30874. + (__u32)ntohl(skb->nh.iph->saddr),
  30875. + (__u32)ntohl(skb->nh.iph->daddr) );
  30876. + } else {
  30877. + KLIPS_PRINT(debug_mast,
  30878. + "klips_debug:ipsec_mast_hard_header: "
  30879. + "is IPv6 packet, skip debugging messages, only revector and build linklocal header.\n");
  30880. + }
  30881. + tmp = skb->dev;
  30882. + skb->dev = prv->dev;
  30883. + ret = prv->hard_header(skb, prv->dev, type, (void *)daddr, (void *)saddr, len);
  30884. + skb->dev = tmp;
  30885. + return ret;
  30886. +}
  30887. +
  30888. +DEBUG_NO_STATIC int
  30889. +ipsec_mast_rebuild_header(struct sk_buff *skb)
  30890. +{
  30891. + struct ipsecpriv *prv = skb->dev->priv;
  30892. + struct net_device *tmp;
  30893. + int ret;
  30894. + struct net_device_stats *stats; /* This device's statistics */
  30895. +
  30896. + if(skb->dev == NULL) {
  30897. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30898. + "klips_debug:ipsec_mast_rebuild_header: "
  30899. + "no device...");
  30900. + return -ENODEV;
  30901. + }
  30902. +
  30903. + if(prv == NULL) {
  30904. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30905. + "klips_debug:ipsec_mast_rebuild_header: "
  30906. + "no private space associated with dev=%s",
  30907. + skb->dev->name ? skb->dev->name : "NULL");
  30908. + return -ENODEV;
  30909. + }
  30910. +
  30911. + stats = (struct net_device_stats *) &(prv->mystats);
  30912. +
  30913. + if(prv->dev == NULL) {
  30914. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30915. + "klips_debug:ipsec_mast_rebuild_header: "
  30916. + "no physical device associated with dev=%s",
  30917. + skb->dev->name ? skb->dev->name : "NULL");
  30918. + stats->tx_dropped++;
  30919. + return -ENODEV;
  30920. + }
  30921. +
  30922. + if(!prv->rebuild_header) {
  30923. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30924. + "klips_debug:ipsec_mast_rebuild_header: "
  30925. + "physical device has been detached, packet dropped skb->dev=%s->NULL ",
  30926. + skb->dev->name);
  30927. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30928. + "ip=%08x->%08x\n",
  30929. + (__u32)ntohl(skb->nh.iph->saddr),
  30930. + (__u32)ntohl(skb->nh.iph->daddr) );
  30931. + stats->tx_dropped++;
  30932. + return -ENODEV;
  30933. + }
  30934. +
  30935. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30936. + "klips_debug:ipsec_mast: "
  30937. + "Revectored rebuild_header dev=%s->%s ",
  30938. + skb->dev->name, prv->dev->name);
  30939. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30940. + "ip=%08x->%08x\n",
  30941. + (__u32)ntohl(skb->nh.iph->saddr),
  30942. + (__u32)ntohl(skb->nh.iph->daddr) );
  30943. + tmp = skb->dev;
  30944. + skb->dev = prv->dev;
  30945. +
  30946. + ret = prv->rebuild_header(skb);
  30947. + skb->dev = tmp;
  30948. + return ret;
  30949. +}
  30950. +
  30951. +DEBUG_NO_STATIC int
  30952. +ipsec_mast_set_mac_address(struct net_device *dev, void *addr)
  30953. +{
  30954. + struct ipsecpriv *prv = dev->priv;
  30955. +
  30956. + struct net_device_stats *stats; /* This device's statistics */
  30957. +
  30958. + if(dev == NULL) {
  30959. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30960. + "klips_debug:ipsec_mast_set_mac_address: "
  30961. + "no device...");
  30962. + return -ENODEV;
  30963. + }
  30964. +
  30965. + if(prv == NULL) {
  30966. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30967. + "klips_debug:ipsec_mast_set_mac_address: "
  30968. + "no private space associated with dev=%s",
  30969. + dev->name ? dev->name : "NULL");
  30970. + return -ENODEV;
  30971. + }
  30972. +
  30973. + stats = (struct net_device_stats *) &(prv->mystats);
  30974. +
  30975. + if(prv->dev == NULL) {
  30976. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30977. + "klips_debug:ipsec_mast_set_mac_address: "
  30978. + "no physical device associated with dev=%s",
  30979. + dev->name ? dev->name : "NULL");
  30980. + stats->tx_dropped++;
  30981. + return -ENODEV;
  30982. + }
  30983. +
  30984. + if(!prv->set_mac_address) {
  30985. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30986. + "klips_debug:ipsec_mast_set_mac_address: "
  30987. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  30988. + dev->name);
  30989. + return -ENODEV;
  30990. + }
  30991. +
  30992. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30993. + "klips_debug:ipsec_mast_set_mac_address: "
  30994. + "Revectored dev=%s->%s addr=0p%p\n",
  30995. + dev->name, prv->dev->name, addr);
  30996. + return prv->set_mac_address(prv->dev, addr);
  30997. +
  30998. +}
  30999. +
  31000. +DEBUG_NO_STATIC void
  31001. +ipsec_mast_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
  31002. +{
  31003. + struct ipsecpriv *prv = dev->priv;
  31004. +
  31005. + struct net_device_stats *stats; /* This device's statistics */
  31006. +
  31007. + if(dev == NULL) {
  31008. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31009. + "klips_debug:ipsec_mast_cache_update: "
  31010. + "no device...");
  31011. + return;
  31012. + }
  31013. +
  31014. + if(prv == NULL) {
  31015. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31016. + "klips_debug:ipsec_mast_cache_update: "
  31017. + "no private space associated with dev=%s",
  31018. + dev->name ? dev->name : "NULL");
  31019. + return;
  31020. + }
  31021. +
  31022. + stats = (struct net_device_stats *) &(prv->mystats);
  31023. +
  31024. + if(prv->dev == NULL) {
  31025. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31026. + "klips_debug:ipsec_mast_cache_update: "
  31027. + "no physical device associated with dev=%s",
  31028. + dev->name ? dev->name : "NULL");
  31029. + stats->tx_dropped++;
  31030. + return;
  31031. + }
  31032. +
  31033. + if(!prv->header_cache_update) {
  31034. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31035. + "klips_debug:ipsec_mast_cache_update: "
  31036. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  31037. + dev->name);
  31038. + return;
  31039. + }
  31040. +
  31041. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31042. + "klips_debug:ipsec_mast: "
  31043. + "Revectored cache_update\n");
  31044. + prv->header_cache_update(hh, prv->dev, haddr);
  31045. + return;
  31046. +}
  31047. +
  31048. +DEBUG_NO_STATIC int
  31049. +ipsec_mast_neigh_setup(struct neighbour *n)
  31050. +{
  31051. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31052. + "klips_debug:ipsec_mast_neigh_setup:\n");
  31053. +
  31054. + if (n->nud_state == NUD_NONE) {
  31055. + n->ops = &arp_broken_ops;
  31056. + n->output = n->ops->output;
  31057. + }
  31058. + return 0;
  31059. +}
  31060. +
  31061. +DEBUG_NO_STATIC int
  31062. +ipsec_mast_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  31063. +{
  31064. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31065. + "klips_debug:ipsec_mast_neigh_setup_dev: "
  31066. + "setting up %s\n",
  31067. + dev ? dev->name : "NULL");
  31068. +
  31069. + if (p->tbl->family == AF_INET) {
  31070. + p->neigh_setup = ipsec_mast_neigh_setup;
  31071. + p->ucast_probes = 0;
  31072. + p->mcast_probes = 0;
  31073. + }
  31074. + return 0;
  31075. +}
  31076. +
  31077. +/*
  31078. + * We call the attach routine to attach another device.
  31079. + */
  31080. +
  31081. +DEBUG_NO_STATIC int
  31082. +ipsec_mast_attach(struct net_device *dev, struct net_device *physdev)
  31083. +{
  31084. + int i;
  31085. + struct ipsecpriv *prv = dev->priv;
  31086. +
  31087. + if(dev == NULL) {
  31088. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31089. + "klips_debug:ipsec_mast_attach: "
  31090. + "no device...");
  31091. + return -ENODEV;
  31092. + }
  31093. +
  31094. + if(prv == NULL) {
  31095. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31096. + "klips_debug:ipsec_mast_attach: "
  31097. + "no private space associated with dev=%s",
  31098. + dev->name ? dev->name : "NULL");
  31099. + return -ENODATA;
  31100. + }
  31101. +
  31102. + prv->dev = physdev;
  31103. + prv->hard_start_xmit = physdev->hard_start_xmit;
  31104. + prv->get_stats = physdev->get_stats;
  31105. +
  31106. + if (physdev->hard_header) {
  31107. + prv->hard_header = physdev->hard_header;
  31108. + dev->hard_header = ipsec_mast_hard_header;
  31109. + } else
  31110. + dev->hard_header = NULL;
  31111. +
  31112. + if (physdev->rebuild_header) {
  31113. + prv->rebuild_header = physdev->rebuild_header;
  31114. + dev->rebuild_header = ipsec_mast_rebuild_header;
  31115. + } else
  31116. + dev->rebuild_header = NULL;
  31117. +
  31118. + if (physdev->set_mac_address) {
  31119. + prv->set_mac_address = physdev->set_mac_address;
  31120. + dev->set_mac_address = ipsec_mast_set_mac_address;
  31121. + } else
  31122. + dev->set_mac_address = NULL;
  31123. +
  31124. + if (physdev->header_cache_update) {
  31125. + prv->header_cache_update = physdev->header_cache_update;
  31126. + dev->header_cache_update = ipsec_mast_cache_update;
  31127. + } else
  31128. + dev->header_cache_update = NULL;
  31129. +
  31130. + dev->hard_header_len = physdev->hard_header_len;
  31131. +
  31132. +/* prv->neigh_setup = physdev->neigh_setup; */
  31133. + dev->neigh_setup = ipsec_mast_neigh_setup_dev;
  31134. + dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */
  31135. + prv->mtu = physdev->mtu;
  31136. +
  31137. +#ifdef PHYSDEV_TYPE
  31138. + dev->type = physdev->type; /* ARPHRD_MAST; */
  31139. +#endif /* PHYSDEV_TYPE */
  31140. +
  31141. + dev->addr_len = physdev->addr_len;
  31142. + for (i=0; i<dev->addr_len; i++) {
  31143. + dev->dev_addr[i] = physdev->dev_addr[i];
  31144. + }
  31145. +#ifdef CONFIG_KLIPS_DEBUG
  31146. + if(debug_mast & DB_MAST_INIT) {
  31147. + printk(KERN_INFO "klips_debug:ipsec_mast_attach: "
  31148. + "physical device %s being attached has HW address: %2x",
  31149. + physdev->name, physdev->dev_addr[0]);
  31150. + for (i=1; i < physdev->addr_len; i++) {
  31151. + printk(":%02x", physdev->dev_addr[i]);
  31152. + }
  31153. + printk("\n");
  31154. + }
  31155. +#endif /* CONFIG_KLIPS_DEBUG */
  31156. +
  31157. + return 0;
  31158. +}
  31159. +
  31160. +/*
  31161. + * We call the detach routine to detach the ipsec mast from another device.
  31162. + */
  31163. +
  31164. +DEBUG_NO_STATIC int
  31165. +ipsec_mast_detach(struct net_device *dev)
  31166. +{
  31167. + int i;
  31168. + struct ipsecpriv *prv = dev->priv;
  31169. +
  31170. + if(dev == NULL) {
  31171. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31172. + "klips_debug:ipsec_mast_detach: "
  31173. + "no device...");
  31174. + return -ENODEV;
  31175. + }
  31176. +
  31177. + if(prv == NULL) {
  31178. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31179. + "klips_debug:ipsec_mast_detach: "
  31180. + "no private space associated with dev=%s",
  31181. + dev->name ? dev->name : "NULL");
  31182. + return -ENODATA;
  31183. + }
  31184. +
  31185. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31186. + "klips_debug:ipsec_mast_detach: "
  31187. + "physical device %s being detached from virtual device %s\n",
  31188. + prv->dev ? prv->dev->name : "NULL",
  31189. + dev->name);
  31190. +
  31191. + prv->dev = NULL;
  31192. + prv->hard_start_xmit = NULL;
  31193. + prv->get_stats = NULL;
  31194. +
  31195. + prv->hard_header = NULL;
  31196. +#ifdef DETACH_AND_DOWN
  31197. + dev->hard_header = NULL;
  31198. +#endif /* DETACH_AND_DOWN */
  31199. +
  31200. + prv->rebuild_header = NULL;
  31201. +#ifdef DETACH_AND_DOWN
  31202. + dev->rebuild_header = NULL;
  31203. +#endif /* DETACH_AND_DOWN */
  31204. +
  31205. + prv->set_mac_address = NULL;
  31206. +#ifdef DETACH_AND_DOWN
  31207. + dev->set_mac_address = NULL;
  31208. +#endif /* DETACH_AND_DOWN */
  31209. +
  31210. + prv->header_cache_update = NULL;
  31211. +#ifdef DETACH_AND_DOWN
  31212. + dev->header_cache_update = NULL;
  31213. +#endif /* DETACH_AND_DOWN */
  31214. +
  31215. +#ifdef DETACH_AND_DOWN
  31216. + dev->neigh_setup = NULL;
  31217. +#endif /* DETACH_AND_DOWN */
  31218. +
  31219. + dev->hard_header_len = 0;
  31220. +#ifdef DETACH_AND_DOWN
  31221. + dev->mtu = 0;
  31222. +#endif /* DETACH_AND_DOWN */
  31223. + prv->mtu = 0;
  31224. + for (i=0; i<MAX_ADDR_LEN; i++) {
  31225. + dev->dev_addr[i] = 0;
  31226. + }
  31227. + dev->addr_len = 0;
  31228. +#ifdef PHYSDEV_TYPE
  31229. + dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */
  31230. +#endif /* PHYSDEV_TYPE */
  31231. +
  31232. + return 0;
  31233. +}
  31234. +
  31235. +/*
  31236. + * We call the clear routine to detach all ipsec masts from other devices.
  31237. + */
  31238. +DEBUG_NO_STATIC int
  31239. +ipsec_mast_clear(void)
  31240. +{
  31241. + int i;
  31242. + struct net_device *ipsecdev = NULL, *prvdev;
  31243. + struct ipsecpriv *prv;
  31244. + char name[9];
  31245. + int ret;
  31246. +
  31247. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31248. + "klips_debug:ipsec_mast_clear: .\n");
  31249. +
  31250. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  31251. + sprintf(name, IPSEC_DEV_FORMAT, i);
  31252. + if((ipsecdev = ipsec_dev_get(name)) != NULL) {
  31253. + if((prv = (struct ipsecpriv *)(ipsecdev->priv))) {
  31254. + prvdev = (struct net_device *)(prv->dev);
  31255. + if(prvdev) {
  31256. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31257. + "klips_debug:ipsec_mast_clear: "
  31258. + "physical device for device %s is %s\n",
  31259. + name, prvdev->name);
  31260. + if((ret = ipsec_mast_detach(ipsecdev))) {
  31261. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31262. + "klips_debug:ipsec_mast_clear: "
  31263. + "error %d detatching device %s from device %s.\n",
  31264. + ret, name, prvdev->name);
  31265. + return ret;
  31266. + }
  31267. + }
  31268. + }
  31269. + }
  31270. + }
  31271. + return 0;
  31272. +}
  31273. +
  31274. +DEBUG_NO_STATIC int
  31275. +ipsec_mast_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  31276. +{
  31277. + struct ipsecmastconf *cf = (struct ipsecmastconf *)&ifr->ifr_data;
  31278. + struct ipsecpriv *prv = dev->priv;
  31279. + struct net_device *them; /* physical device */
  31280. +#ifdef CONFIG_IP_ALIAS
  31281. + char *colon;
  31282. + char realphysname[IFNAMSIZ];
  31283. +#endif /* CONFIG_IP_ALIAS */
  31284. +
  31285. + if(dev == NULL) {
  31286. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31287. + "klips_debug:ipsec_mast_ioctl: "
  31288. + "device not supplied.\n");
  31289. + return -ENODEV;
  31290. + }
  31291. +
  31292. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31293. + "klips_debug:ipsec_mast_ioctl: "
  31294. + "tncfg service call #%d for dev=%s\n",
  31295. + cmd,
  31296. + dev->name ? dev->name : "NULL");
  31297. + switch (cmd) {
  31298. + /* attach a virtual ipsec? device to a physical device */
  31299. + case IPSEC_SET_DEV:
  31300. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31301. + "klips_debug:ipsec_mast_ioctl: "
  31302. + "calling ipsec_mast_attatch...\n");
  31303. +#ifdef CONFIG_IP_ALIAS
  31304. + /* If this is an IP alias interface, get its real physical name */
  31305. + strncpy(realphysname, cf->cf_name, IFNAMSIZ);
  31306. + realphysname[IFNAMSIZ-1] = 0;
  31307. + colon = strchr(realphysname, ':');
  31308. + if (colon) *colon = 0;
  31309. + them = ipsec_dev_get(realphysname);
  31310. +#else /* CONFIG_IP_ALIAS */
  31311. + them = ipsec_dev_get(cf->cf_name);
  31312. +#endif /* CONFIG_IP_ALIAS */
  31313. +
  31314. + if (them == NULL) {
  31315. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31316. + "klips_debug:ipsec_mast_ioctl: "
  31317. + "physical device %s requested is null\n",
  31318. + cf->cf_name);
  31319. + return -ENXIO;
  31320. + }
  31321. +
  31322. +#if 0
  31323. + if (them->flags & IFF_UP) {
  31324. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31325. + "klips_debug:ipsec_mast_ioctl: "
  31326. + "physical device %s requested is not up.\n",
  31327. + cf->cf_name);
  31328. + return -ENXIO;
  31329. + }
  31330. +#endif
  31331. +
  31332. + if (prv && prv->dev) {
  31333. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31334. + "klips_debug:ipsec_mast_ioctl: "
  31335. + "virtual device is already connected to %s.\n",
  31336. + prv->dev->name ? prv->dev->name : "NULL");
  31337. + return -EBUSY;
  31338. + }
  31339. + return ipsec_mast_attach(dev, them);
  31340. +
  31341. + case IPSEC_DEL_DEV:
  31342. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31343. + "klips_debug:ipsec_mast_ioctl: "
  31344. + "calling ipsec_mast_detatch.\n");
  31345. + if (! prv->dev) {
  31346. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31347. + "klips_debug:ipsec_mast_ioctl: "
  31348. + "physical device not connected.\n");
  31349. + return -ENODEV;
  31350. + }
  31351. + return ipsec_mast_detach(dev);
  31352. +
  31353. + case IPSEC_CLR_DEV:
  31354. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31355. + "klips_debug:ipsec_mast_ioctl: "
  31356. + "calling ipsec_mast_clear.\n");
  31357. + return ipsec_mast_clear();
  31358. +
  31359. + default:
  31360. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31361. + "klips_debug:ipsec_mast_ioctl: "
  31362. + "unknown command %d.\n",
  31363. + cmd);
  31364. + return -EOPNOTSUPP;
  31365. + }
  31366. +}
  31367. +
  31368. +int
  31369. +ipsec_mast_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  31370. +{
  31371. + struct net_device *dev = ptr;
  31372. + struct net_device *ipsec_dev;
  31373. + struct ipsecpriv *priv;
  31374. + char name[9];
  31375. + int i;
  31376. +
  31377. + if (dev == NULL) {
  31378. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31379. + "klips_debug:ipsec_mast_device_event: "
  31380. + "dev=NULL for event type %ld.\n",
  31381. + event);
  31382. + return(NOTIFY_DONE);
  31383. + }
  31384. +
  31385. + /* check for loopback devices */
  31386. + if (dev && (dev->flags & IFF_LOOPBACK)) {
  31387. + return(NOTIFY_DONE);
  31388. + }
  31389. +
  31390. + switch (event) {
  31391. + case NETDEV_DOWN:
  31392. + /* look very carefully at the scope of these compiler
  31393. + directives before changing anything... -- RGB */
  31394. +
  31395. + case NETDEV_UNREGISTER:
  31396. + switch (event) {
  31397. + case NETDEV_DOWN:
  31398. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31399. + "klips_debug:ipsec_mast_device_event: "
  31400. + "NETDEV_DOWN dev=%s flags=%x\n",
  31401. + dev->name,
  31402. + dev->flags);
  31403. + if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) {
  31404. + printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n",
  31405. + dev->name);
  31406. + }
  31407. + break;
  31408. + case NETDEV_UNREGISTER:
  31409. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31410. + "klips_debug:ipsec_mast_device_event: "
  31411. + "NETDEV_UNREGISTER dev=%s flags=%x\n",
  31412. + dev->name,
  31413. + dev->flags);
  31414. + break;
  31415. + }
  31416. +
  31417. + /* find the attached physical device and detach it. */
  31418. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  31419. + sprintf(name, IPSEC_DEV_FORMAT, i);
  31420. + ipsec_dev = ipsec_dev_get(name);
  31421. + if(ipsec_dev) {
  31422. + priv = (struct ipsecpriv *)(ipsec_dev->priv);
  31423. + if(priv) {
  31424. + ;
  31425. + if(((struct net_device *)(priv->dev)) == dev) {
  31426. + /* dev_close(ipsec_dev); */
  31427. + /* return */ ipsec_mast_detach(ipsec_dev);
  31428. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31429. + "klips_debug:ipsec_mast_device_event: "
  31430. + "device '%s' has been detached.\n",
  31431. + ipsec_dev->name);
  31432. + break;
  31433. + }
  31434. + } else {
  31435. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31436. + "klips_debug:ipsec_mast_device_event: "
  31437. + "device '%s' has no private data space!\n",
  31438. + ipsec_dev->name);
  31439. + }
  31440. + }
  31441. + }
  31442. + break;
  31443. + case NETDEV_UP:
  31444. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31445. + "klips_debug:ipsec_mast_device_event: "
  31446. + "NETDEV_UP dev=%s\n",
  31447. + dev->name);
  31448. + break;
  31449. + case NETDEV_REBOOT:
  31450. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31451. + "klips_debug:ipsec_mast_device_event: "
  31452. + "NETDEV_REBOOT dev=%s\n",
  31453. + dev->name);
  31454. + break;
  31455. + case NETDEV_CHANGE:
  31456. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31457. + "klips_debug:ipsec_mast_device_event: "
  31458. + "NETDEV_CHANGE dev=%s flags=%x\n",
  31459. + dev->name,
  31460. + dev->flags);
  31461. + break;
  31462. + case NETDEV_REGISTER:
  31463. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31464. + "klips_debug:ipsec_mast_device_event: "
  31465. + "NETDEV_REGISTER dev=%s\n",
  31466. + dev->name);
  31467. + break;
  31468. + case NETDEV_CHANGEMTU:
  31469. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31470. + "klips_debug:ipsec_mast_device_event: "
  31471. + "NETDEV_CHANGEMTU dev=%s to mtu=%d\n",
  31472. + dev->name,
  31473. + dev->mtu);
  31474. + break;
  31475. + case NETDEV_CHANGEADDR:
  31476. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31477. + "klips_debug:ipsec_mast_device_event: "
  31478. + "NETDEV_CHANGEADDR dev=%s\n",
  31479. + dev->name);
  31480. + break;
  31481. + case NETDEV_GOING_DOWN:
  31482. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31483. + "klips_debug:ipsec_mast_device_event: "
  31484. + "NETDEV_GOING_DOWN dev=%s\n",
  31485. + dev->name);
  31486. + break;
  31487. + case NETDEV_CHANGENAME:
  31488. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31489. + "klips_debug:ipsec_mast_device_event: "
  31490. + "NETDEV_CHANGENAME dev=%s\n",
  31491. + dev->name);
  31492. + break;
  31493. + default:
  31494. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31495. + "klips_debug:ipsec_mast_device_event: "
  31496. + "event type %ld unrecognised for dev=%s\n",
  31497. + event,
  31498. + dev->name);
  31499. + break;
  31500. + }
  31501. + return NOTIFY_DONE;
  31502. +}
  31503. +
  31504. +/*
  31505. + * Called when an ipsec mast device is initialized.
  31506. + * The ipsec mast device structure is passed to us.
  31507. + */
  31508. +
  31509. +int
  31510. +ipsec_mast_init(struct net_device *dev)
  31511. +{
  31512. + int i;
  31513. +
  31514. + KLIPS_PRINT(debug_mast,
  31515. + "klips_debug:ipsec_mast_init: "
  31516. + "allocating %lu bytes initialising device: %s\n",
  31517. + (unsigned long) sizeof(struct ipsecpriv),
  31518. + dev->name ? dev->name : "NULL");
  31519. +
  31520. + /* Add our mast functions to the device */
  31521. + dev->open = ipsec_mast_open;
  31522. + dev->stop = ipsec_mast_close;
  31523. + dev->hard_start_xmit = ipsec_mast_start_xmit;
  31524. + dev->get_stats = ipsec_mast_get_stats;
  31525. +
  31526. + dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL);
  31527. + if (dev->priv == NULL)
  31528. + return -ENOMEM;
  31529. + memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv));
  31530. +
  31531. + for(i = 0; i < sizeof(zeroes); i++) {
  31532. + ((__u8*)(zeroes))[i] = 0;
  31533. + }
  31534. +
  31535. + dev->set_multicast_list = NULL;
  31536. + dev->do_ioctl = ipsec_mast_ioctl;
  31537. + dev->hard_header = NULL;
  31538. + dev->rebuild_header = NULL;
  31539. + dev->set_mac_address = NULL;
  31540. + dev->header_cache_update= NULL;
  31541. + dev->neigh_setup = ipsec_mast_neigh_setup_dev;
  31542. + dev->hard_header_len = 0;
  31543. + dev->mtu = 0;
  31544. + dev->addr_len = 0;
  31545. + dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */ /* ARPHRD_ETHER; */
  31546. + dev->tx_queue_len = 10; /* Small queue */
  31547. + memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */
  31548. +
  31549. + /* New-style flags. */
  31550. + dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */;
  31551. + dev_init_buffers(dev);
  31552. +
  31553. + /* We're done. Have I forgotten anything? */
  31554. + return 0;
  31555. +}
  31556. +
  31557. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31558. +/* Module specific interface (but it links with the rest of IPSEC) */
  31559. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31560. +
  31561. +int
  31562. +ipsec_mast_probe(struct net_device *dev)
  31563. +{
  31564. + ipsec_mast_init(dev);
  31565. + return 0;
  31566. +}
  31567. +
  31568. +int
  31569. +ipsec_mast_init_devices(void)
  31570. +{
  31571. + return 0;
  31572. +}
  31573. +
  31574. +/* void */
  31575. +int
  31576. +ipsec_mast_cleanup_devices(void)
  31577. +{
  31578. + int error = 0;
  31579. + int i;
  31580. + char name[10];
  31581. + struct net_device *dev_mast;
  31582. +
  31583. + for(i = 0; i < ipsec_mastdevice_count; i++) {
  31584. + sprintf(name, MAST_DEV_FORMAT, i);
  31585. + if((dev_mast = ipsec_dev_get(name)) == NULL) {
  31586. + break;
  31587. + }
  31588. + unregister_netdev(dev_mast);
  31589. + kfree(dev_mast->priv);
  31590. + dev_mast->priv=NULL;
  31591. + }
  31592. + return error;
  31593. +}
  31594. +
  31595. +/*
  31596. + * $Log: ipsec_mast.c,v $
  31597. + * Revision 1.7.2.1 2006/10/06 21:39:26 paul
  31598. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  31599. + * set. This is defined through autoconf.h which is included through the
  31600. + * linux kernel build macros.
  31601. + *
  31602. + * Revision 1.7 2005/04/29 05:10:22 mcr
  31603. + * removed from extraenous includes to make unit testing easier.
  31604. + *
  31605. + * Revision 1.6 2004/12/03 21:25:57 mcr
  31606. + * compile time fixes for running on 2.6.
  31607. + * still experimental.
  31608. + *
  31609. + * Revision 1.5 2004/08/03 18:19:08 mcr
  31610. + * in 2.6, use "net_device" instead of #define device->net_device.
  31611. + * this probably breaks 2.0 compiles.
  31612. + *
  31613. + * Revision 1.4 2004/07/10 19:11:18 mcr
  31614. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  31615. + *
  31616. + * Revision 1.3 2003/10/31 02:27:55 mcr
  31617. + * pulled up port-selector patches and sa_id elimination.
  31618. + *
  31619. + * Revision 1.2.4.1 2003/10/29 01:30:41 mcr
  31620. + * elimited "struct sa_id".
  31621. + *
  31622. + * Revision 1.2 2003/06/22 20:06:17 mcr
  31623. + * refactored mast code still had lots of ipsecX junk in it.
  31624. + *
  31625. + * Revision 1.1 2003/02/12 19:31:12 rgb
  31626. + * Refactored from ipsec_tunnel.c
  31627. + *
  31628. + */
  31629. --- /dev/null Tue Mar 11 13:02:56 2003
  31630. +++ linux/net/ipsec/ipsec_md5c.c Mon Feb 9 13:51:03 2004
  31631. @@ -0,0 +1,453 @@
  31632. +/*
  31633. + * RCSID $Id: ipsec_md5c.c,v 1.10 2005/04/15 01:25:57 mcr Exp $
  31634. + */
  31635. +
  31636. +/*
  31637. + * The rest of the code is derived from MD5C.C by RSADSI. Minor cosmetic
  31638. + * changes to accomodate it in the kernel by ji.
  31639. + */
  31640. +
  31641. +#include <asm/byteorder.h>
  31642. +#include <linux/string.h>
  31643. +
  31644. +#include "openswan/ipsec_md5h.h"
  31645. +
  31646. +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  31647. + */
  31648. +
  31649. +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  31650. +rights reserved.
  31651. +
  31652. +License to copy and use this software is granted provided that it
  31653. +is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  31654. +Algorithm" in all material mentioning or referencing this software
  31655. +or this function.
  31656. +
  31657. +License is also granted to make and use derivative works provided
  31658. +that such works are identified as "derived from the RSA Data
  31659. +Security, Inc. MD5 Message-Digest Algorithm" in all material
  31660. +mentioning or referencing the derived work.
  31661. +
  31662. +RSA Data Security, Inc. makes no representations concerning either
  31663. +the merchantability of this software or the suitability of this
  31664. +software for any particular purpose. It is provided "as is"
  31665. +without express or implied warranty of any kind.
  31666. +
  31667. +These notices must be retained in any copies of any part of this
  31668. +documentation and/or software.
  31669. + */
  31670. +
  31671. +/*
  31672. + * Additions by JI
  31673. + *
  31674. + * HAVEMEMCOPY is defined if mem* routines are available
  31675. + *
  31676. + * HAVEHTON is defined if htons() and htonl() can be used
  31677. + * for big/little endian conversions
  31678. + *
  31679. + */
  31680. +
  31681. +#define HAVEMEMCOPY
  31682. +#ifdef __LITTLE_ENDIAN
  31683. +#define LITTLENDIAN
  31684. +#endif
  31685. +#ifdef __BIG_ENDIAN
  31686. +#define BIGENDIAN
  31687. +#endif
  31688. +
  31689. +/* Constants for MD5Transform routine.
  31690. + */
  31691. +
  31692. +#define S11 7
  31693. +#define S12 12
  31694. +#define S13 17
  31695. +#define S14 22
  31696. +#define S21 5
  31697. +#define S22 9
  31698. +#define S23 14
  31699. +#define S24 20
  31700. +#define S31 4
  31701. +#define S32 11
  31702. +#define S33 16
  31703. +#define S34 23
  31704. +#define S41 6
  31705. +#define S42 10
  31706. +#define S43 15
  31707. +#define S44 21
  31708. +
  31709. +static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  31710. +
  31711. +#ifdef LITTLEENDIAN
  31712. +#define Encode MD5_memcpy
  31713. +#define Decode MD5_memcpy
  31714. +#else
  31715. +static void Encode PROTO_LIST
  31716. + ((unsigned char *, UINT4 *, unsigned int));
  31717. +static void Decode PROTO_LIST
  31718. + ((UINT4 *, unsigned char *, unsigned int));
  31719. +#endif
  31720. +
  31721. +#ifdef HAVEMEMCOPY
  31722. +/* no need to include <memory.h> here; <linux/string.h> defines these */
  31723. +#define MD5_memcpy memcpy
  31724. +#define MD5_memset memset
  31725. +#else
  31726. +#ifdef HAVEBCOPY
  31727. +#define MD5_memcpy(_a,_b,_c) bcopy((_b),(_a),(_c))
  31728. +#define MD5_memset(_a,_b,_c) bzero((_a),(_c))
  31729. +#else
  31730. +static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  31731. +static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  31732. +#endif
  31733. +#endif
  31734. +static unsigned char PADDING[64] = {
  31735. + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31736. + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31737. + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  31738. +};
  31739. +
  31740. +/* F, G, H and I are basic MD5 functions.
  31741. + */
  31742. +#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  31743. +#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  31744. +#define H(x, y, z) ((x) ^ (y) ^ (z))
  31745. +#define I(x, y, z) ((y) ^ ((x) | (~z)))
  31746. +
  31747. +/* ROTATE_LEFT rotates x left n bits.
  31748. + */
  31749. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  31750. +
  31751. +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  31752. +Rotation is separate from addition to prevent recomputation.
  31753. + */
  31754. +#define FF(a, b, c, d, x, s, ac) { \
  31755. + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31756. + (a) = ROTATE_LEFT ((a), (s)); \
  31757. + (a) += (b); \
  31758. + }
  31759. +#define GG(a, b, c, d, x, s, ac) { \
  31760. + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31761. + (a) = ROTATE_LEFT ((a), (s)); \
  31762. + (a) += (b); \
  31763. + }
  31764. +#define HH(a, b, c, d, x, s, ac) { \
  31765. + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31766. + (a) = ROTATE_LEFT ((a), (s)); \
  31767. + (a) += (b); \
  31768. + }
  31769. +#define II(a, b, c, d, x, s, ac) { \
  31770. + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31771. + (a) = ROTATE_LEFT ((a), (s)); \
  31772. + (a) += (b); \
  31773. + }
  31774. +
  31775. +/*
  31776. + * MD5 initialization. Begins an MD5 operation, writing a new context.
  31777. + */
  31778. +void osMD5Init(void *vcontext)
  31779. +{
  31780. + MD5_CTX *context = vcontext;
  31781. +
  31782. + context->count[0] = context->count[1] = 0;
  31783. + /* Load magic initialization constants.*/
  31784. + context->state[0] = 0x67452301;
  31785. + context->state[1] = 0xefcdab89;
  31786. + context->state[2] = 0x98badcfe;
  31787. + context->state[3] = 0x10325476;
  31788. +}
  31789. +
  31790. +/* MD5 block update operation. Continues an MD5 message-digest
  31791. + operation, processing another message block, and updating the
  31792. + context.
  31793. + */
  31794. +void osMD5Update (vcontext, input, inputLen)
  31795. + void *vcontext;
  31796. + unsigned char *input; /* input block */
  31797. + __u32 inputLen; /* length of input block */
  31798. +{
  31799. + MD5_CTX *context = vcontext;
  31800. + __u32 i;
  31801. + unsigned int index, partLen;
  31802. +
  31803. + /* Compute number of bytes mod 64 */
  31804. + index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  31805. +
  31806. + /* Update number of bits */
  31807. + if ((context->count[0] += ((UINT4)inputLen << 3))
  31808. + < ((UINT4)inputLen << 3))
  31809. + context->count[1]++;
  31810. + context->count[1] += ((UINT4)inputLen >> 29);
  31811. +
  31812. + partLen = 64 - index;
  31813. +
  31814. + /* Transform as many times as possible.
  31815. +*/
  31816. + if (inputLen >= partLen) {
  31817. + MD5_memcpy
  31818. + ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  31819. + MD5Transform (context->state, context->buffer);
  31820. +
  31821. + for (i = partLen; i + 63 < inputLen; i += 64)
  31822. + MD5Transform (context->state, &input[i]);
  31823. +
  31824. + index = 0;
  31825. + }
  31826. + else
  31827. + i = 0;
  31828. +
  31829. + /* Buffer remaining input */
  31830. + MD5_memcpy
  31831. + ((POINTER)&context->buffer[index], (POINTER)&input[i],
  31832. + inputLen-i);
  31833. +}
  31834. +
  31835. +/* MD5 finalization. Ends an MD5 message-digest operation, writing the
  31836. + the message digest and zeroizing the context.
  31837. + */
  31838. +void osMD5Final (digest, vcontext)
  31839. +unsigned char digest[16]; /* message digest */
  31840. +void *vcontext; /* context */
  31841. +{
  31842. + MD5_CTX *context = vcontext;
  31843. + unsigned char bits[8];
  31844. + unsigned int index, padLen;
  31845. +
  31846. + /* Save number of bits */
  31847. + Encode (bits, context->count, 8);
  31848. +
  31849. + /* Pad out to 56 mod 64.
  31850. +*/
  31851. + index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  31852. + padLen = (index < 56) ? (56 - index) : (120 - index);
  31853. + osMD5Update (context, PADDING, padLen);
  31854. +
  31855. + /* Append length (before padding) */
  31856. + osMD5Update (context, bits, 8);
  31857. +
  31858. + if (digest != NULL) /* Bill Simpson's padding */
  31859. + {
  31860. + /* store state in digest */
  31861. + Encode (digest, context->state, 16);
  31862. +
  31863. + /* Zeroize sensitive information.
  31864. + */
  31865. + MD5_memset ((POINTER)context, 0, sizeof (*context));
  31866. + }
  31867. +}
  31868. +
  31869. +/* MD5 basic transformation. Transforms state based on block.
  31870. + */
  31871. +static void MD5Transform (state, block)
  31872. +UINT4 state[4];
  31873. +unsigned char block[64];
  31874. +{
  31875. + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  31876. +
  31877. + Decode (x, block, 64);
  31878. +
  31879. + /* Round 1 */
  31880. + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  31881. + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  31882. + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  31883. + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  31884. + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  31885. + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  31886. + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  31887. + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  31888. + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  31889. + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  31890. + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  31891. + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  31892. + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  31893. + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  31894. + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  31895. + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  31896. +
  31897. + /* Round 2 */
  31898. + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  31899. + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  31900. + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  31901. + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  31902. + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  31903. + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  31904. + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  31905. + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  31906. + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  31907. + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  31908. + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  31909. + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  31910. + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  31911. + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  31912. + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  31913. + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  31914. +
  31915. + /* Round 3 */
  31916. + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  31917. + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  31918. + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  31919. + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  31920. + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  31921. + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  31922. + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  31923. + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  31924. + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  31925. + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  31926. + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  31927. + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  31928. + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  31929. + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  31930. + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  31931. + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  31932. +
  31933. + /* Round 4 */
  31934. + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  31935. + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  31936. + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  31937. + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  31938. + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  31939. + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  31940. + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  31941. + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  31942. + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  31943. + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  31944. + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  31945. + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  31946. + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  31947. + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  31948. + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  31949. + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  31950. +
  31951. + state[0] += a;
  31952. + state[1] += b;
  31953. + state[2] += c;
  31954. + state[3] += d;
  31955. +
  31956. + /* Zeroize sensitive information.
  31957. +*/
  31958. + MD5_memset ((POINTER)x, 0, sizeof (x));
  31959. +}
  31960. +
  31961. +#ifndef LITTLEENDIAN
  31962. +
  31963. +/* Encodes input (UINT4) into output (unsigned char). Assumes len is
  31964. + a multiple of 4.
  31965. + */
  31966. +static void Encode (output, input, len)
  31967. +unsigned char *output;
  31968. +UINT4 *input;
  31969. +unsigned int len;
  31970. +{
  31971. + unsigned int i, j;
  31972. +
  31973. + for (i = 0, j = 0; j < len; i++, j += 4) {
  31974. + output[j] = (unsigned char)(input[i] & 0xff);
  31975. + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  31976. + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  31977. + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  31978. + }
  31979. +}
  31980. +
  31981. +/* Decodes input (unsigned char) into output (UINT4). Assumes len is
  31982. + a multiple of 4.
  31983. + */
  31984. +static void Decode (output, input, len)
  31985. +UINT4 *output;
  31986. +unsigned char *input;
  31987. +unsigned int len;
  31988. +{
  31989. + unsigned int i, j;
  31990. +
  31991. + for (i = 0, j = 0; j < len; i++, j += 4)
  31992. + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  31993. + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  31994. +}
  31995. +
  31996. +#endif
  31997. +
  31998. +#ifndef HAVEMEMCOPY
  31999. +#ifndef HAVEBCOPY
  32000. +/* Note: Replace "for loop" with standard memcpy if possible.
  32001. + */
  32002. +
  32003. +static void MD5_memcpy (output, input, len)
  32004. +POINTER output;
  32005. +POINTER input;
  32006. +unsigned int len;
  32007. +{
  32008. + unsigned int i;
  32009. +
  32010. + for (i = 0; i < len; i++)
  32011. +
  32012. + output[i] = input[i];
  32013. +}
  32014. +
  32015. +/* Note: Replace "for loop" with standard memset if possible.
  32016. + */
  32017. +
  32018. +static void MD5_memset (output, value, len)
  32019. +POINTER output;
  32020. +int value;
  32021. +unsigned int len;
  32022. +{
  32023. + unsigned int i;
  32024. +
  32025. + for (i = 0; i < len; i++)
  32026. + ((char *)output)[i] = (char)value;
  32027. +}
  32028. +#endif
  32029. +#endif
  32030. +
  32031. +/*
  32032. + * $Log: ipsec_md5c.c,v $
  32033. + * Revision 1.10 2005/04/15 01:25:57 mcr
  32034. + * minor fix to comments.
  32035. + *
  32036. + * Revision 1.9 2004/09/08 17:21:36 ken
  32037. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  32038. + *
  32039. + * Revision 1.8 2004/04/06 02:49:26 mcr
  32040. + * pullup of algo code from alg-branch.
  32041. + *
  32042. + * Revision 1.7 2002/09/10 01:45:14 mcr
  32043. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  32044. + * the function prototypes would match, and could be placed
  32045. + * into a pointer to a function.
  32046. + *
  32047. + * Revision 1.6 2002/04/24 07:55:32 mcr
  32048. + * #include patches and Makefiles for post-reorg compilation.
  32049. + *
  32050. + * Revision 1.5 2002/04/24 07:36:28 mcr
  32051. + * Moved from ./klips/net/ipsec/ipsec_md5c.c,v
  32052. + *
  32053. + * Revision 1.4 1999/12/13 13:59:12 rgb
  32054. + * Quick fix to argument size to Update bugs.
  32055. + *
  32056. + * Revision 1.3 1999/05/21 18:09:28 henry
  32057. + * unnecessary <memory.h> include causes trouble in 2.2
  32058. + *
  32059. + * Revision 1.2 1999/04/06 04:54:26 rgb
  32060. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  32061. + * patch shell fixes.
  32062. + *
  32063. + * Revision 1.1 1998/06/18 21:27:48 henry
  32064. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  32065. + * kernel-build scripts happier in the presence of symlinks
  32066. + *
  32067. + * Revision 1.2 1998/04/23 20:54:02 rgb
  32068. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  32069. + * verified.
  32070. + *
  32071. + * Revision 1.1 1998/04/09 03:06:08 henry
  32072. + * sources moved up from linux/net/ipsec
  32073. + *
  32074. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  32075. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  32076. + *
  32077. + * Revision 0.3 1996/11/20 14:48:53 ji
  32078. + * Release update only.
  32079. + *
  32080. + * Revision 0.2 1996/11/02 00:18:33 ji
  32081. + * First limited release.
  32082. + *
  32083. + *
  32084. + */
  32085. --- /dev/null Tue Mar 11 13:02:56 2003
  32086. +++ linux/net/ipsec/ipsec_proc.c Mon Feb 9 13:51:03 2004
  32087. @@ -0,0 +1,1176 @@
  32088. +/*
  32089. + * @(#) /proc file system interface code.
  32090. + *
  32091. + * Copyright (C) 1996, 1997 John Ioannidis.
  32092. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  32093. + * 2001 Michael Richardson <mcr@freeswan.org>
  32094. + *
  32095. + * This program is free software; you can redistribute it and/or modify it
  32096. + * under the terms of the GNU General Public License as published by the
  32097. + * Free Software Foundation; either version 2 of the License, or (at your
  32098. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  32099. + *
  32100. + * This program is distributed in the hope that it will be useful, but
  32101. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32102. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  32103. + * for more details.
  32104. + *
  32105. + * Split out from ipsec_init.c version 1.70.
  32106. + */
  32107. +
  32108. +char ipsec_proc_c_version[] = "RCSID $Id: ipsec_proc.c,v 1.39.2.3 2006/10/06 21:39:26 paul Exp $";
  32109. +
  32110. +
  32111. +#ifndef AUTOCONF_INCLUDED
  32112. +#include <linux/config.h>
  32113. +#endif
  32114. +#include <linux/version.h>
  32115. +#define __NO_VERSION__
  32116. +#include <linux/module.h>
  32117. +#include <linux/kernel.h> /* printk() */
  32118. +
  32119. +#include "openswan/ipsec_kversion.h"
  32120. +#include "openswan/ipsec_param.h"
  32121. +
  32122. +#ifdef MALLOC_SLAB
  32123. +# include <linux/slab.h> /* kmalloc() */
  32124. +#else /* MALLOC_SLAB */
  32125. +# include <linux/malloc.h> /* kmalloc() */
  32126. +#endif /* MALLOC_SLAB */
  32127. +#include <linux/errno.h> /* error codes */
  32128. +#include <linux/types.h> /* size_t */
  32129. +#include <linux/interrupt.h> /* mark_bh */
  32130. +
  32131. +#include <linux/netdevice.h> /* struct device, and other headers */
  32132. +#include <linux/etherdevice.h> /* eth_type_trans */
  32133. +#include <linux/ip.h> /* struct iphdr */
  32134. +#include <linux/in.h> /* struct sockaddr_in */
  32135. +#include <linux/skbuff.h>
  32136. +#include <asm/uaccess.h> /* copy_from_user */
  32137. +#include <openswan.h>
  32138. +#ifdef SPINLOCK
  32139. +#ifdef SPINLOCK_23
  32140. +#include <linux/spinlock.h> /* *lock* */
  32141. +#else /* SPINLOCK_23 */
  32142. +#include <asm/spinlock.h> /* *lock* */
  32143. +#endif /* SPINLOCK_23 */
  32144. +#endif /* SPINLOCK */
  32145. +
  32146. +#include <net/ip.h>
  32147. +#ifdef CONFIG_PROC_FS
  32148. +#include <linux/proc_fs.h>
  32149. +#endif /* CONFIG_PROC_FS */
  32150. +#ifdef NETLINK_SOCK
  32151. +#include <linux/netlink.h>
  32152. +#else
  32153. +#include <net/netlink.h>
  32154. +#endif
  32155. +
  32156. +#include "openswan/radij.h"
  32157. +
  32158. +#include "openswan/ipsec_life.h"
  32159. +#include "openswan/ipsec_stats.h"
  32160. +#include "openswan/ipsec_sa.h"
  32161. +
  32162. +#include "openswan/ipsec_encap.h"
  32163. +#include "openswan/ipsec_radij.h"
  32164. +#include "openswan/ipsec_xform.h"
  32165. +#include "openswan/ipsec_tunnel.h"
  32166. +#include "openswan/ipsec_xmit.h"
  32167. +
  32168. +#include "openswan/ipsec_rcv.h"
  32169. +#include "openswan/ipsec_ah.h"
  32170. +#include "openswan/ipsec_esp.h"
  32171. +#include "openswan/ipsec_kern24.h"
  32172. +
  32173. +#ifdef CONFIG_KLIPS_IPCOMP
  32174. +#include "openswan/ipcomp.h"
  32175. +#endif /* CONFIG_KLIPS_IPCOMP */
  32176. +
  32177. +#include "openswan/ipsec_proto.h"
  32178. +
  32179. +#include <pfkeyv2.h>
  32180. +#include <pfkey.h>
  32181. +
  32182. +#ifdef CONFIG_PROC_FS
  32183. +
  32184. +#ifdef IPSEC_PROC_SUBDIRS
  32185. +static struct proc_dir_entry *proc_net_ipsec_dir = NULL;
  32186. +static struct proc_dir_entry *proc_eroute_dir = NULL;
  32187. +static struct proc_dir_entry *proc_spi_dir = NULL;
  32188. +static struct proc_dir_entry *proc_spigrp_dir = NULL;
  32189. +static struct proc_dir_entry *proc_birth_dir = NULL;
  32190. +static struct proc_dir_entry *proc_stats_dir = NULL;
  32191. +#endif
  32192. +
  32193. +struct ipsec_birth_reply ipsec_ipv4_birth_packet;
  32194. +struct ipsec_birth_reply ipsec_ipv6_birth_packet;
  32195. +
  32196. +#ifdef CONFIG_KLIPS_DEBUG
  32197. +int debug_esp = 0;
  32198. +int debug_ah = 0;
  32199. +#endif /* CONFIG_KLIPS_DEBUG */
  32200. +
  32201. +#define DECREMENT_UNSIGNED(X, amount) ((amount < (X)) ? (X)-amount : 0)
  32202. +
  32203. +extern int ipsec_xform_get_info(char *buffer, char **start,
  32204. + off_t offset, int length IPSEC_PROC_LAST_ARG);
  32205. +
  32206. +
  32207. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32208. +int
  32209. +ipsec_eroute_get_info(char *buffer,
  32210. + char **start,
  32211. + off_t offset,
  32212. + int length IPSEC_PROC_LAST_ARG)
  32213. +{
  32214. + struct wsbuf w = {buffer, length, offset, 0, 0};
  32215. +
  32216. +#ifdef CONFIG_KLIPS_DEBUG
  32217. + if (debug_radij & DB_RJ_DUMPTREES)
  32218. + rj_dumptrees(); /* XXXXXXXXX */
  32219. +#endif /* CONFIG_KLIPS_DEBUG */
  32220. +
  32221. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32222. + "klips_debug:ipsec_eroute_get_info: "
  32223. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32224. + buffer,
  32225. + *start,
  32226. + (int)offset,
  32227. + length);
  32228. +
  32229. + spin_lock_bh(&eroute_lock);
  32230. +
  32231. + rj_walktree(rnh, ipsec_rj_walker_procprint, &w);
  32232. +/* rj_walktree(mask_rjhead, ipsec_rj_walker_procprint, &w); */
  32233. +
  32234. + spin_unlock_bh(&eroute_lock);
  32235. +
  32236. + *start = buffer + (offset - w.begin); /* Start of wanted data */
  32237. + return w.len - (offset - w.begin);
  32238. +}
  32239. +
  32240. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32241. +int
  32242. +ipsec_spi_get_info(char *buffer,
  32243. + char **start,
  32244. + off_t offset,
  32245. + int length IPSEC_PROC_LAST_ARG)
  32246. +{
  32247. + const int max_content = length > 0? length-1 : 0;
  32248. + int len = 0;
  32249. + off_t begin = 0;
  32250. + int i;
  32251. + struct ipsec_sa *sa_p;
  32252. + char sa[SATOT_BUF];
  32253. + char buf_s[SUBNETTOA_BUF];
  32254. + char buf_d[SUBNETTOA_BUF];
  32255. + size_t sa_len;
  32256. +
  32257. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32258. + "klips_debug:ipsec_spi_get_info: "
  32259. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32260. + buffer,
  32261. + *start,
  32262. + (int)offset,
  32263. + length);
  32264. +
  32265. + spin_lock_bh(&tdb_lock);
  32266. +
  32267. + for (i = 0; i < SADB_HASHMOD; i++) {
  32268. + for (sa_p = ipsec_sadb_hash[i];
  32269. + sa_p;
  32270. + sa_p = sa_p->ips_hnext) {
  32271. + atomic_inc(&sa_p->ips_refcount);
  32272. + sa_len = satot(&sa_p->ips_said, 'x', sa, sizeof(sa));
  32273. + len += ipsec_snprintf(buffer+len, length-len, "%s ",
  32274. + sa_len ? sa : " (error)");
  32275. +
  32276. + len += ipsec_snprintf(buffer+len, length-len, "%s%s%s",
  32277. + IPS_XFORM_NAME(sa_p));
  32278. +
  32279. + len += ipsec_snprintf(buffer+len, length-len, ": dir=%s",
  32280. + (sa_p->ips_flags & EMT_INBOUND) ?
  32281. + "in " : "out");
  32282. +
  32283. + if(sa_p->ips_addr_s) {
  32284. + addrtoa(((struct sockaddr_in*)(sa_p->ips_addr_s))->sin_addr,
  32285. + 0, buf_s, sizeof(buf_s));
  32286. + len += ipsec_snprintf(buffer+len, length-len, " src=%s",
  32287. + buf_s);
  32288. + }
  32289. +
  32290. + if((sa_p->ips_said.proto == IPPROTO_IPIP)
  32291. + && (sa_p->ips_flags & SADB_X_SAFLAGS_INFLOW)) {
  32292. + subnettoa(sa_p->ips_flow_s.u.v4.sin_addr,
  32293. + sa_p->ips_mask_s.u.v4.sin_addr,
  32294. + 0,
  32295. + buf_s,
  32296. + sizeof(buf_s));
  32297. +
  32298. + subnettoa(sa_p->ips_flow_d.u.v4.sin_addr,
  32299. + sa_p->ips_mask_d.u.v4.sin_addr,
  32300. + 0,
  32301. + buf_d,
  32302. + sizeof(buf_d));
  32303. +
  32304. + len += ipsec_snprintf(buffer+len, length-len, " policy=%s->%s",
  32305. + buf_s, buf_d);
  32306. + }
  32307. +
  32308. + if(sa_p->ips_iv_bits) {
  32309. + int j;
  32310. + len += ipsec_snprintf(buffer+len, length-len, " iv_bits=%dbits iv=0x",
  32311. + sa_p->ips_iv_bits);
  32312. +
  32313. + for(j = 0; j < sa_p->ips_iv_bits / 8; j++) {
  32314. + len += ipsec_snprintf(buffer+len, length-len, "%02x",
  32315. + (__u32)((__u8*)(sa_p->ips_iv))[j]);
  32316. + }
  32317. + }
  32318. +
  32319. + if(sa_p->ips_encalg || sa_p->ips_authalg) {
  32320. + if(sa_p->ips_replaywin) {
  32321. + len += ipsec_snprintf(buffer+len, length-len, " ooowin=%d",
  32322. + sa_p->ips_replaywin);
  32323. + }
  32324. + if(sa_p->ips_errs.ips_replaywin_errs) {
  32325. + len += ipsec_snprintf(buffer+len, length-len, " ooo_errs=%d",
  32326. + sa_p->ips_errs.ips_replaywin_errs);
  32327. + }
  32328. + if(sa_p->ips_replaywin_lastseq) {
  32329. + len += ipsec_snprintf(buffer+len, length-len, " seq=%d",
  32330. + sa_p->ips_replaywin_lastseq);
  32331. + }
  32332. + if(sa_p->ips_replaywin_bitmap) {
  32333. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32334. + len += ipsec_snprintf(buffer+len, length-len, " bit=0x%Lx",
  32335. + sa_p->ips_replaywin_bitmap);
  32336. +#else
  32337. + len += ipsec_snprintf(buffer+len, length-len, " bit=0x%x%08x",
  32338. + (__u32)(sa_p->ips_replaywin_bitmap >> 32),
  32339. + (__u32)sa_p->ips_replaywin_bitmap);
  32340. +#endif
  32341. + }
  32342. + if(sa_p->ips_replaywin_maxdiff) {
  32343. + len += ipsec_snprintf(buffer+len, length-len, " max_seq_diff=%d",
  32344. + sa_p->ips_replaywin_maxdiff);
  32345. + }
  32346. + }
  32347. + if(sa_p->ips_flags & ~EMT_INBOUND) {
  32348. + len += ipsec_snprintf(buffer+len, length-len, " flags=0x%x",
  32349. + sa_p->ips_flags & ~EMT_INBOUND);
  32350. + len += ipsec_snprintf(buffer+len, length-len, "<");
  32351. + /* flag printing goes here */
  32352. + len += ipsec_snprintf(buffer+len, length-len, ">");
  32353. + }
  32354. + if(sa_p->ips_auth_bits) {
  32355. + len += ipsec_snprintf(buffer+len, length-len, " alen=%d",
  32356. + sa_p->ips_auth_bits);
  32357. + }
  32358. + if(sa_p->ips_key_bits_a) {
  32359. + len += ipsec_snprintf(buffer+len, length-len, " aklen=%d",
  32360. + sa_p->ips_key_bits_a);
  32361. + }
  32362. + if(sa_p->ips_errs.ips_auth_errs) {
  32363. + len += ipsec_snprintf(buffer+len, length-len, " auth_errs=%d",
  32364. + sa_p->ips_errs.ips_auth_errs);
  32365. + }
  32366. + if(sa_p->ips_key_bits_e) {
  32367. + len += ipsec_snprintf(buffer+len, length-len, " eklen=%d",
  32368. + sa_p->ips_key_bits_e);
  32369. + }
  32370. + if(sa_p->ips_errs.ips_encsize_errs) {
  32371. + len += ipsec_snprintf(buffer+len, length-len, " encr_size_errs=%d",
  32372. + sa_p->ips_errs.ips_encsize_errs);
  32373. + }
  32374. + if(sa_p->ips_errs.ips_encpad_errs) {
  32375. + len += ipsec_snprintf(buffer+len, length-len, " encr_pad_errs=%d",
  32376. + sa_p->ips_errs.ips_encpad_errs);
  32377. + }
  32378. +
  32379. + len += ipsec_snprintf(buffer+len, length-len, " life(c,s,h)=");
  32380. +
  32381. + len += ipsec_lifetime_format(buffer + len,
  32382. + length - len,
  32383. + "alloc",
  32384. + ipsec_life_countbased,
  32385. + &sa_p->ips_life.ipl_allocations);
  32386. +
  32387. + len += ipsec_lifetime_format(buffer + len,
  32388. + length - len,
  32389. + "bytes",
  32390. + ipsec_life_countbased,
  32391. + &sa_p->ips_life.ipl_bytes);
  32392. +
  32393. + len += ipsec_lifetime_format(buffer + len,
  32394. + length - len,
  32395. + "addtime",
  32396. + ipsec_life_timebased,
  32397. + &sa_p->ips_life.ipl_addtime);
  32398. +
  32399. + len += ipsec_lifetime_format(buffer + len,
  32400. + length - len,
  32401. + "usetime",
  32402. + ipsec_life_timebased,
  32403. + &sa_p->ips_life.ipl_usetime);
  32404. +
  32405. + len += ipsec_lifetime_format(buffer + len,
  32406. + length - len,
  32407. + "packets",
  32408. + ipsec_life_countbased,
  32409. + &sa_p->ips_life.ipl_packets);
  32410. +
  32411. + if(sa_p->ips_life.ipl_usetime.ipl_last) { /* XXX-MCR should be last? */
  32412. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32413. + len += ipsec_snprintf(buffer+len, length-len, " idle=%Ld",
  32414. + jiffies / HZ - sa_p->ips_life.ipl_usetime.ipl_last);
  32415. +#else
  32416. + len += ipsec_snprintf(buffer+len, length-len, " idle=%lu",
  32417. + jiffies / HZ - (unsigned long)sa_p->ips_life.ipl_usetime.ipl_last);
  32418. +#endif
  32419. + }
  32420. +
  32421. +#ifdef CONFIG_KLIPS_IPCOMP
  32422. + if(sa_p->ips_said.proto == IPPROTO_COMP &&
  32423. + (sa_p->ips_comp_ratio_dbytes ||
  32424. + sa_p->ips_comp_ratio_cbytes)) {
  32425. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32426. + len += ipsec_snprintf(buffer+len, length-len, " ratio=%Ld:%Ld",
  32427. + sa_p->ips_comp_ratio_dbytes,
  32428. + sa_p->ips_comp_ratio_cbytes);
  32429. +#else
  32430. + len += ipsec_snprintf(buffer+len, length-len, " ratio=%lu:%lu",
  32431. + (unsigned long)sa_p->ips_comp_ratio_dbytes,
  32432. + (unsigned long)sa_p->ips_comp_ratio_cbytes);
  32433. +#endif
  32434. + }
  32435. +#endif /* CONFIG_KLIPS_IPCOMP */
  32436. +
  32437. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32438. + {
  32439. + char *natttype_name;
  32440. +
  32441. + switch(sa_p->ips_natt_type)
  32442. + {
  32443. + case 0:
  32444. + natttype_name="none";
  32445. + break;
  32446. + case ESPINUDP_WITH_NON_IKE:
  32447. + natttype_name="nonike";
  32448. + break;
  32449. + case ESPINUDP_WITH_NON_ESP:
  32450. + natttype_name="nonesp";
  32451. + break;
  32452. + default:
  32453. + natttype_name = "unknown";
  32454. + break;
  32455. + }
  32456. +
  32457. + len += ipsec_snprintf(buffer + len, length-len, " natencap=%s",
  32458. + natttype_name);
  32459. +
  32460. + len += ipsec_snprintf(buffer + len, length-len, " natsport=%d",
  32461. + sa_p->ips_natt_sport);
  32462. +
  32463. + len += ipsec_snprintf(buffer + len,length-len, " natdport=%d",
  32464. + sa_p->ips_natt_dport);
  32465. + }
  32466. +#else
  32467. + len += ipsec_snprintf(buffer + len, length-len, " natencap=na");
  32468. +#endif /* CONFIG_IPSEC_NAT_TRAVERSAL */
  32469. +
  32470. + len += ipsec_snprintf(buffer + len,length-len, " refcount=%d",
  32471. + atomic_read(&sa_p->ips_refcount));
  32472. +
  32473. + len += ipsec_snprintf(buffer+len, length-len, " ref=%d",
  32474. + sa_p->ips_ref);
  32475. +#ifdef CONFIG_KLIPS_DEBUG
  32476. + if(debug_xform) {
  32477. + len += ipsec_snprintf(buffer+len, length-len, " reftable=%lu refentry=%lu",
  32478. + (unsigned long)IPsecSAref2table(sa_p->ips_ref),
  32479. + (unsigned long)IPsecSAref2entry(sa_p->ips_ref));
  32480. + }
  32481. +#endif /* CONFIG_KLIPS_DEBUG */
  32482. +
  32483. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32484. +
  32485. + atomic_dec(&sa_p->ips_refcount);
  32486. +
  32487. + if (len >= max_content) {
  32488. + /* we've done all that can fit -- stop loops */
  32489. + len = max_content; /* truncate crap */
  32490. + goto done_spi_i;
  32491. + } else {
  32492. + const off_t pos = begin + len; /* file position of end of what we've generated */
  32493. +
  32494. + if (pos <= offset) {
  32495. + /* all is before first interesting character:
  32496. + * discard, but note where we are.
  32497. + */
  32498. + len = 0;
  32499. + begin = pos;
  32500. + }
  32501. + }
  32502. + }
  32503. + }
  32504. +
  32505. +done_spi_i:
  32506. + spin_unlock_bh(&tdb_lock);
  32507. +
  32508. + *start = buffer + (offset - begin); /* Start of wanted data */
  32509. + return len - (offset - begin);
  32510. +}
  32511. +
  32512. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32513. +int
  32514. +ipsec_spigrp_get_info(char *buffer,
  32515. + char **start,
  32516. + off_t offset,
  32517. + int length IPSEC_PROC_LAST_ARG)
  32518. +{
  32519. + /* Limit of useful snprintf output */
  32520. + const int max_content = length > 0? length-1 : 0;
  32521. +
  32522. + int len = 0;
  32523. + off_t begin = 0;
  32524. + int i;
  32525. + struct ipsec_sa *sa_p, *sa_p2;
  32526. + char sa[SATOT_BUF];
  32527. + size_t sa_len;
  32528. +
  32529. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32530. + "klips_debug:ipsec_spigrp_get_info: "
  32531. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32532. + buffer,
  32533. + *start,
  32534. + (int)offset,
  32535. + length);
  32536. +
  32537. + spin_lock_bh(&tdb_lock);
  32538. +
  32539. + for (i = 0; i < SADB_HASHMOD; i++) {
  32540. + for (sa_p = ipsec_sadb_hash[i];
  32541. + sa_p != NULL;
  32542. + sa_p = sa_p->ips_hnext)
  32543. + {
  32544. + atomic_inc(&sa_p->ips_refcount);
  32545. + if(sa_p->ips_inext == NULL) {
  32546. + sa_p2 = sa_p;
  32547. + while(sa_p2 != NULL) {
  32548. + atomic_inc(&sa_p2->ips_refcount);
  32549. + sa_len = satot(&sa_p2->ips_said,
  32550. + 'x', sa, sizeof(sa));
  32551. +
  32552. + len += ipsec_snprintf(buffer+len, length-len, "%s ",
  32553. + sa_len ? sa : " (error)");
  32554. + atomic_dec(&sa_p2->ips_refcount);
  32555. + sa_p2 = sa_p2->ips_onext;
  32556. + }
  32557. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32558. + }
  32559. +
  32560. + atomic_dec(&sa_p->ips_refcount);
  32561. +
  32562. + if (len >= max_content) {
  32563. + /* we've done all that can fit -- stop loops */
  32564. + len = max_content; /* truncate crap */
  32565. + goto done_spigrp_i;
  32566. + } else {
  32567. + const off_t pos = begin + len;
  32568. +
  32569. + if (pos <= offset) {
  32570. + /* all is before first interesting character:
  32571. + * discard, but note where we are.
  32572. + */
  32573. + len = 0;
  32574. + begin = pos;
  32575. + }
  32576. + }
  32577. + }
  32578. + }
  32579. +
  32580. +done_spigrp_i:
  32581. + spin_unlock_bh(&tdb_lock);
  32582. +
  32583. + *start = buffer + (offset - begin); /* Start of wanted data */
  32584. + return len - (offset - begin);
  32585. +}
  32586. +
  32587. +
  32588. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32589. +int
  32590. +ipsec_tncfg_get_info(char *buffer,
  32591. + char **start,
  32592. + off_t offset,
  32593. + int length IPSEC_PROC_LAST_ARG)
  32594. +{
  32595. + /* limit of useful snprintf output */
  32596. + const int max_content = length > 0? length-1 : 0;
  32597. + int len = 0;
  32598. + off_t begin = 0;
  32599. + int i;
  32600. + char name[9];
  32601. + struct net_device *dev, *privdev;
  32602. + struct ipsecpriv *priv;
  32603. +
  32604. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32605. + "klips_debug:ipsec_tncfg_get_info: "
  32606. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32607. + buffer,
  32608. + *start,
  32609. + (int)offset,
  32610. + length);
  32611. +
  32612. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  32613. + ipsec_snprintf(name, (ssize_t) sizeof(name), IPSEC_DEV_FORMAT, i);
  32614. + dev = __ipsec_dev_get(name);
  32615. + if(dev) {
  32616. + priv = (struct ipsecpriv *)(dev->priv);
  32617. + len += ipsec_snprintf(buffer+len, length-len, "%s",
  32618. + dev->name);
  32619. + if(priv) {
  32620. + privdev = (struct net_device *)(priv->dev);
  32621. + len += ipsec_snprintf(buffer+len, length-len, " -> %s",
  32622. + privdev ? privdev->name : "NULL");
  32623. + len += ipsec_snprintf(buffer+len, length-len, " mtu=%d(%d) -> %d",
  32624. + dev->mtu,
  32625. + priv->mtu,
  32626. + privdev ? privdev->mtu : 0);
  32627. + } else {
  32628. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32629. + "klips_debug:ipsec_tncfg_get_info: device '%s' has no private data space!\n",
  32630. + dev->name);
  32631. + }
  32632. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32633. +
  32634. + if (len >= max_content) {
  32635. + /* we've done all that can fit -- stop loop */
  32636. + len = max_content; /* truncate crap */
  32637. + break;
  32638. + } else {
  32639. + const off_t pos = begin + len;
  32640. + if (pos <= offset) {
  32641. + len = 0;
  32642. + begin = pos;
  32643. + }
  32644. + }
  32645. + }
  32646. + }
  32647. + *start = buffer + (offset - begin); /* Start of wanted data */
  32648. + len -= (offset - begin); /* Start slop */
  32649. + if (len > length)
  32650. + len = length;
  32651. + return len;
  32652. +}
  32653. +
  32654. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32655. +int
  32656. +ipsec_version_get_info(char *buffer,
  32657. + char **start,
  32658. + off_t offset,
  32659. + int length IPSEC_PROC_LAST_ARG)
  32660. +{
  32661. + int len = 0;
  32662. + off_t begin = 0;
  32663. +
  32664. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32665. + "klips_debug:ipsec_version_get_info: "
  32666. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32667. + buffer,
  32668. + *start,
  32669. + (int)offset,
  32670. + length);
  32671. +
  32672. + len += ipsec_snprintf(buffer + len,length-len, "Openswan version: %s\n",
  32673. + ipsec_version_code());
  32674. +#if 0
  32675. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32676. + "klips_debug:ipsec_version_get_info: "
  32677. + "ipsec_init version: %s\n",
  32678. + ipsec_init_c_version);
  32679. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32680. + "klips_debug:ipsec_version_get_info: "
  32681. + "ipsec_tunnel version: %s\n",
  32682. + ipsec_tunnel_c_version);
  32683. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32684. + "klips_debug:ipsec_version_get_info: "
  32685. + "ipsec_netlink version: %s\n",
  32686. + ipsec_netlink_c_version);
  32687. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32688. + "klips_debug:ipsec_version_get_info: "
  32689. + "radij_c_version: %s\n",
  32690. + radij_c_version);
  32691. +#endif
  32692. +
  32693. +
  32694. + *start = buffer + (offset - begin); /* Start of wanted data */
  32695. + len -= (offset - begin); /* Start slop */
  32696. + if (len > length)
  32697. + len = length;
  32698. + return len;
  32699. +}
  32700. +
  32701. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32702. +int
  32703. +ipsec_natt_get_info(char *buffer,
  32704. + char **start,
  32705. + off_t offset,
  32706. + int length IPSEC_PROC_LAST_ARG)
  32707. +{
  32708. + int len = 0;
  32709. + off_t begin = 0;
  32710. +
  32711. + len += ipsec_snprintf(buffer + len,
  32712. + length-len, "%d\n",
  32713. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32714. + 1
  32715. +#else
  32716. + 0
  32717. +#endif
  32718. + );
  32719. +
  32720. + *start = buffer + (offset - begin); /* Start of wanted data */
  32721. + len -= (offset - begin); /* Start slop */
  32722. + if (len > length)
  32723. + len = length;
  32724. + return len;
  32725. +}
  32726. +
  32727. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32728. +int
  32729. +ipsec_birth_info(char *page,
  32730. + char **start,
  32731. + off_t offset,
  32732. + int count,
  32733. + int *eof,
  32734. + void *data)
  32735. +{
  32736. + struct ipsec_birth_reply *ibr = (struct ipsec_birth_reply *)data;
  32737. + int len;
  32738. +
  32739. + if(offset >= ibr->packet_template_len) {
  32740. + if(eof) {
  32741. + *eof=1;
  32742. + }
  32743. + return 0;
  32744. + }
  32745. +
  32746. + len = ibr->packet_template_len;
  32747. + len -= offset;
  32748. + if (len > count)
  32749. + len = count;
  32750. +
  32751. + memcpy(page + offset, ibr->packet_template+offset, len);
  32752. +
  32753. + return len;
  32754. +}
  32755. +
  32756. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32757. +int
  32758. +ipsec_birth_set(struct file *file, const char *buffer,
  32759. + unsigned long count, void *data)
  32760. +{
  32761. + struct ipsec_birth_reply *ibr = (struct ipsec_birth_reply *)data;
  32762. + int len;
  32763. +
  32764. + KLIPS_INC_USE;
  32765. + if(count > IPSEC_BIRTH_TEMPLATE_MAXLEN) {
  32766. + len = IPSEC_BIRTH_TEMPLATE_MAXLEN;
  32767. + } else {
  32768. + len = count;
  32769. + }
  32770. +
  32771. + if(copy_from_user(ibr->packet_template, buffer, len)) {
  32772. + KLIPS_DEC_USE;
  32773. + return -EFAULT;
  32774. + }
  32775. + ibr->packet_template_len = len;
  32776. +
  32777. + KLIPS_DEC_USE;
  32778. +
  32779. + return len;
  32780. +}
  32781. +
  32782. +
  32783. +#ifdef CONFIG_KLIPS_DEBUG
  32784. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32785. +int
  32786. +ipsec_klipsdebug_get_info(char *buffer,
  32787. + char **start,
  32788. + off_t offset,
  32789. + int length IPSEC_PROC_LAST_ARG)
  32790. +{
  32791. + int len = 0;
  32792. + off_t begin = 0;
  32793. +
  32794. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32795. + "klips_debug:ipsec_klipsdebug_get_info: "
  32796. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32797. + buffer,
  32798. + *start,
  32799. + (int)offset,
  32800. + length);
  32801. +
  32802. + len += ipsec_snprintf(buffer+len, length-len, "debug_tunnel=%08x.\n", debug_tunnel);
  32803. + len += ipsec_snprintf(buffer+len, length-len, "debug_xform=%08x.\n", debug_xform);
  32804. + len += ipsec_snprintf(buffer+len, length-len, "debug_eroute=%08x.\n", debug_eroute);
  32805. + len += ipsec_snprintf(buffer+len, length-len, "debug_spi=%08x.\n", debug_spi);
  32806. + len += ipsec_snprintf(buffer+len, length-len, "debug_radij=%08x.\n", debug_radij);
  32807. + len += ipsec_snprintf(buffer+len, length-len, "debug_esp=%08x.\n", debug_esp);
  32808. + len += ipsec_snprintf(buffer+len, length-len, "debug_ah=%08x.\n", debug_ah);
  32809. + len += ipsec_snprintf(buffer+len, length-len, "debug_rcv=%08x.\n", debug_rcv);
  32810. + len += ipsec_snprintf(buffer+len, length-len, "debug_pfkey=%08x.\n", debug_pfkey);
  32811. +
  32812. + *start = buffer + (offset - begin); /* Start of wanted data */
  32813. + len -= (offset - begin); /* Start slop */
  32814. + if (len > length)
  32815. + len = length;
  32816. + return len;
  32817. +}
  32818. +#endif /* CONFIG_KLIPS_DEBUG */
  32819. +
  32820. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32821. +int
  32822. +ipsec_stats_get_int_info(char *buffer,
  32823. + char **start,
  32824. + off_t offset,
  32825. + int length,
  32826. + int *eof,
  32827. + void *data)
  32828. +{
  32829. +
  32830. + const int max_content = length > 0? length-1 : 0;
  32831. + int len = 0;
  32832. + int *thing;
  32833. +
  32834. + thing = (int *)data;
  32835. +
  32836. + len = ipsec_snprintf(buffer+len, length-len, "%08x\n", *thing);
  32837. +
  32838. + if (len >= max_content)
  32839. + len = max_content; /* truncate crap */
  32840. +
  32841. + *start = buffer + offset; /* Start of wanted data */
  32842. + return len > offset? len - offset : 0;
  32843. +
  32844. +}
  32845. +
  32846. +#ifndef PROC_FS_2325
  32847. +struct proc_dir_entry ipsec_eroute =
  32848. +{
  32849. + 0,
  32850. + 12, "ipsec_eroute",
  32851. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32852. + &proc_net_inode_operations,
  32853. + ipsec_eroute_get_info,
  32854. + NULL, NULL, NULL, NULL, NULL
  32855. +};
  32856. +
  32857. +struct proc_dir_entry ipsec_spi =
  32858. +{
  32859. + 0,
  32860. + 9, "ipsec_spi",
  32861. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32862. + &proc_net_inode_operations,
  32863. + ipsec_spi_get_info,
  32864. + NULL, NULL, NULL, NULL, NULL
  32865. +};
  32866. +
  32867. +struct proc_dir_entry ipsec_spigrp =
  32868. +{
  32869. + 0,
  32870. + 12, "ipsec_spigrp",
  32871. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32872. + &proc_net_inode_operations,
  32873. + ipsec_spigrp_get_info,
  32874. + NULL, NULL, NULL, NULL, NULL
  32875. +};
  32876. +
  32877. +struct proc_dir_entry ipsec_tncfg =
  32878. +{
  32879. + 0,
  32880. + 11, "ipsec_tncfg",
  32881. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32882. + &proc_net_inode_operations,
  32883. + ipsec_tncfg_get_info,
  32884. + NULL, NULL, NULL, NULL, NULL
  32885. +};
  32886. +
  32887. +struct proc_dir_entry ipsec_version =
  32888. +{
  32889. + 0,
  32890. + 13, "ipsec_version",
  32891. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32892. + &proc_net_inode_operations,
  32893. + ipsec_version_get_info,
  32894. + NULL, NULL, NULL, NULL, NULL
  32895. +};
  32896. +
  32897. +#ifdef CONFIG_KLIPS_DEBUG
  32898. +struct proc_dir_entry ipsec_klipsdebug =
  32899. +{
  32900. + 0,
  32901. + 16, "ipsec_klipsdebug",
  32902. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32903. + &proc_net_inode_operations,
  32904. + ipsec_klipsdebug_get_info,
  32905. + NULL, NULL, NULL, NULL, NULL
  32906. +};
  32907. +#endif /* CONFIG_KLIPS_DEBUG */
  32908. +#endif /* !PROC_FS_2325 */
  32909. +#endif /* CONFIG_PROC_FS */
  32910. +
  32911. +#if defined(PROC_FS_2325)
  32912. +struct ipsec_proc_list {
  32913. + char *name;
  32914. + struct proc_dir_entry **parent;
  32915. + struct proc_dir_entry **dir;
  32916. + read_proc_t *readthing;
  32917. + write_proc_t *writething;
  32918. + void *data;
  32919. +};
  32920. +static struct ipsec_proc_list proc_items[]={
  32921. +#ifdef CONFIG_KLIPS_DEBUG
  32922. + {"klipsdebug", &proc_net_ipsec_dir, NULL, ipsec_klipsdebug_get_info, NULL, NULL},
  32923. +#endif
  32924. + {"eroute", &proc_net_ipsec_dir, &proc_eroute_dir, NULL, NULL, NULL},
  32925. + {"all", &proc_eroute_dir, NULL, ipsec_eroute_get_info, NULL, NULL},
  32926. + {"spi", &proc_net_ipsec_dir, &proc_spi_dir, NULL, NULL, NULL},
  32927. + {"all", &proc_spi_dir, NULL, ipsec_spi_get_info, NULL, NULL},
  32928. + {"spigrp", &proc_net_ipsec_dir, &proc_spigrp_dir, NULL, NULL, NULL},
  32929. + {"all", &proc_spigrp_dir, NULL, ipsec_spigrp_get_info, NULL, NULL},
  32930. + {"birth", &proc_net_ipsec_dir, &proc_birth_dir, NULL, NULL, NULL},
  32931. + {"ipv4", &proc_birth_dir, NULL, ipsec_birth_info, ipsec_birth_set, (void *)&ipsec_ipv4_birth_packet},
  32932. + {"ipv6", &proc_birth_dir, NULL, ipsec_birth_info, ipsec_birth_set, (void *)&ipsec_ipv6_birth_packet},
  32933. + {"tncfg", &proc_net_ipsec_dir, NULL, ipsec_tncfg_get_info, NULL, NULL},
  32934. + {"xforms", &proc_net_ipsec_dir, NULL, ipsec_xform_get_info, NULL, NULL},
  32935. + {"stats", &proc_net_ipsec_dir, &proc_stats_dir, NULL, NULL, NULL},
  32936. + {"trap_count", &proc_stats_dir, NULL, ipsec_stats_get_int_info, NULL, &ipsec_xmit_trap_count},
  32937. + {"trap_sendcount", &proc_stats_dir, NULL, ipsec_stats_get_int_info, NULL, &ipsec_xmit_trap_sendcount},
  32938. + {"version", &proc_net_ipsec_dir, NULL, ipsec_version_get_info, NULL, NULL},
  32939. + {NULL, NULL, NULL, NULL, NULL, NULL}
  32940. +};
  32941. +#endif
  32942. +
  32943. +int
  32944. +ipsec_proc_init()
  32945. +{
  32946. + int error = 0;
  32947. +#ifdef IPSEC_PROC_SUBDIRS
  32948. + struct proc_dir_entry *item;
  32949. +#endif
  32950. +
  32951. + /*
  32952. + * just complain because pluto won't run without /proc!
  32953. + */
  32954. +#ifndef CONFIG_PROC_FS
  32955. +#error You must have PROC_FS built in to use KLIPS
  32956. +#endif
  32957. +
  32958. + /* for 2.0 kernels */
  32959. +#if !defined(PROC_FS_2325) && !defined(PROC_FS_21)
  32960. + error |= proc_register_dynamic(&proc_net, &ipsec_eroute);
  32961. + error |= proc_register_dynamic(&proc_net, &ipsec_spi);
  32962. + error |= proc_register_dynamic(&proc_net, &ipsec_spigrp);
  32963. + error |= proc_register_dynamic(&proc_net, &ipsec_tncfg);
  32964. + error |= proc_register_dynamic(&proc_net, &ipsec_version);
  32965. +#ifdef CONFIG_KLIPS_DEBUG
  32966. + error |= proc_register_dynamic(&proc_net, &ipsec_klipsdebug);
  32967. +#endif /* CONFIG_KLIPS_DEBUG */
  32968. +#endif
  32969. +
  32970. + /* for 2.2 kernels */
  32971. +#if !defined(PROC_FS_2325) && defined(PROC_FS_21)
  32972. + error |= proc_register(proc_net, &ipsec_eroute);
  32973. + error |= proc_register(proc_net, &ipsec_spi);
  32974. + error |= proc_register(proc_net, &ipsec_spigrp);
  32975. + error |= proc_register(proc_net, &ipsec_tncfg);
  32976. + error |= proc_register(proc_net, &ipsec_version);
  32977. +#ifdef CONFIG_KLIPS_DEBUG
  32978. + error |= proc_register(proc_net, &ipsec_klipsdebug);
  32979. +#endif /* CONFIG_KLIPS_DEBUG */
  32980. +#endif
  32981. +
  32982. + /* for 2.4 kernels */
  32983. +#if defined(PROC_FS_2325)
  32984. + /* create /proc/net/ipsec */
  32985. +
  32986. + /* zero these out before we initialize /proc/net/ipsec/birth/stuff */
  32987. + memset(&ipsec_ipv4_birth_packet, 0, sizeof(struct ipsec_birth_reply));
  32988. + memset(&ipsec_ipv6_birth_packet, 0, sizeof(struct ipsec_birth_reply));
  32989. +
  32990. + proc_net_ipsec_dir = proc_mkdir("ipsec", proc_net);
  32991. + if(proc_net_ipsec_dir == NULL) {
  32992. + /* no point in continuing */
  32993. + return 1;
  32994. + }
  32995. +
  32996. + {
  32997. + struct ipsec_proc_list *it;
  32998. +
  32999. + it=proc_items;
  33000. + while(it->name!=NULL) {
  33001. + if(it->dir) {
  33002. + /* make a dir instead */
  33003. + item = proc_mkdir(it->name, *it->parent);
  33004. + *it->dir = item;
  33005. + } else {
  33006. + item = create_proc_entry(it->name, 0400, *it->parent);
  33007. + }
  33008. + if(item) {
  33009. + item->read_proc = it->readthing;
  33010. + item->write_proc = it->writething;
  33011. + item->data = it->data;
  33012. +#ifdef MODULE
  33013. + item->owner = THIS_MODULE;
  33014. +#endif
  33015. + } else {
  33016. + error |= 1;
  33017. + }
  33018. + it++;
  33019. + }
  33020. + }
  33021. +
  33022. + /* now create some symlinks to provide compatibility */
  33023. + proc_symlink("ipsec_eroute", proc_net, "ipsec/eroute/all");
  33024. + proc_symlink("ipsec_spi", proc_net, "ipsec/spi/all");
  33025. + proc_symlink("ipsec_spigrp", proc_net, "ipsec/spigrp/all");
  33026. + proc_symlink("ipsec_tncfg", proc_net, "ipsec/tncfg");
  33027. + proc_symlink("ipsec_version",proc_net, "ipsec/version");
  33028. + proc_symlink("ipsec_klipsdebug",proc_net,"ipsec/klipsdebug");
  33029. +
  33030. +#endif /* !PROC_FS_2325 */
  33031. +
  33032. + return error;
  33033. +}
  33034. +
  33035. +void
  33036. +ipsec_proc_cleanup()
  33037. +{
  33038. +
  33039. + /* for 2.0 and 2.2 kernels */
  33040. +#if !defined(PROC_FS_2325)
  33041. +
  33042. +#ifdef CONFIG_KLIPS_DEBUG
  33043. + if (proc_net_unregister(ipsec_klipsdebug.low_ino) != 0)
  33044. + printk("klips_debug:ipsec_cleanup: "
  33045. + "cannot unregister /proc/net/ipsec_klipsdebug\n");
  33046. +#endif /* CONFIG_KLIPS_DEBUG */
  33047. +
  33048. + if (proc_net_unregister(ipsec_version.low_ino) != 0)
  33049. + printk("klips_debug:ipsec_cleanup: "
  33050. + "cannot unregister /proc/net/ipsec_version\n");
  33051. + if (proc_net_unregister(ipsec_eroute.low_ino) != 0)
  33052. + printk("klips_debug:ipsec_cleanup: "
  33053. + "cannot unregister /proc/net/ipsec_eroute\n");
  33054. + if (proc_net_unregister(ipsec_spi.low_ino) != 0)
  33055. + printk("klips_debug:ipsec_cleanup: "
  33056. + "cannot unregister /proc/net/ipsec_spi\n");
  33057. + if (proc_net_unregister(ipsec_spigrp.low_ino) != 0)
  33058. + printk("klips_debug:ipsec_cleanup: "
  33059. + "cannot unregister /proc/net/ipsec_spigrp\n");
  33060. + if (proc_net_unregister(ipsec_tncfg.low_ino) != 0)
  33061. + printk("klips_debug:ipsec_cleanup: "
  33062. + "cannot unregister /proc/net/ipsec_tncfg\n");
  33063. +#endif
  33064. +
  33065. + /* for 2.4 kernels */
  33066. +#if defined(PROC_FS_2325)
  33067. + {
  33068. + struct ipsec_proc_list *it;
  33069. +
  33070. + /* find end of list */
  33071. + it=proc_items;
  33072. + while(it->name!=NULL) {
  33073. + it++;
  33074. + }
  33075. + it--;
  33076. +
  33077. + do {
  33078. + remove_proc_entry(it->name, *it->parent);
  33079. + it--;
  33080. + } while(it >= proc_items);
  33081. + }
  33082. +
  33083. +
  33084. +#ifdef CONFIG_KLIPS_DEBUG
  33085. + remove_proc_entry("ipsec_klipsdebug", proc_net);
  33086. +#endif /* CONFIG_KLIPS_DEBUG */
  33087. + remove_proc_entry("ipsec_eroute", proc_net);
  33088. + remove_proc_entry("ipsec_spi", proc_net);
  33089. + remove_proc_entry("ipsec_spigrp", proc_net);
  33090. + remove_proc_entry("ipsec_tncfg", proc_net);
  33091. + remove_proc_entry("ipsec_version", proc_net);
  33092. + remove_proc_entry("ipsec", proc_net);
  33093. +#endif /* 2.4 kernel */
  33094. +}
  33095. +
  33096. +/*
  33097. + * $Log: ipsec_proc.c,v $
  33098. + * Revision 1.39.2.3 2006/10/06 21:39:26 paul
  33099. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  33100. + * set. This is defined through autoconf.h which is included through the
  33101. + * linux kernel build macros.
  33102. + *
  33103. + * Revision 1.39.2.2 2006/02/13 18:48:12 paul
  33104. + * Fix by Ankit Desai <ankit@elitecore.com> for module unloading.
  33105. + *
  33106. + * Revision 1.39.2.1 2005/09/07 00:45:59 paul
  33107. + * pull up of mcr's nat-t klips detection patch from head
  33108. + *
  33109. + * Revision 1.39 2005/05/20 03:19:18 mcr
  33110. + * modifications for use on 2.4.30 kernel, with backported
  33111. + * printk_ratelimit(). all warnings removed.
  33112. + *
  33113. + * Revision 1.38 2005/04/29 05:10:22 mcr
  33114. + * removed from extraenous includes to make unit testing easier.
  33115. + *
  33116. + * Revision 1.37 2005/04/13 22:49:49 mcr
  33117. + * moved KLIPS specific snprintf() wrapper to seperate file.
  33118. + *
  33119. + * Revision 1.36 2005/04/06 17:44:36 mcr
  33120. + * when NAT-T is compiled out, show encap as "NA"
  33121. + *
  33122. + * Revision 1.35 2005/01/26 00:50:35 mcr
  33123. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  33124. + * and make sure that NAT_TRAVERSAL is set as well to match
  33125. + * userspace compiles of code.
  33126. + *
  33127. + * Revision 1.34 2004/12/03 21:25:57 mcr
  33128. + * compile time fixes for running on 2.6.
  33129. + * still experimental.
  33130. + *
  33131. + * Revision 1.33 2004/08/17 03:27:23 mcr
  33132. + * klips 2.6 edits.
  33133. + *
  33134. + * Revision 1.32 2004/08/03 18:19:08 mcr
  33135. + * in 2.6, use "net_device" instead of #define device->net_device.
  33136. + * this probably breaks 2.0 compiles.
  33137. + *
  33138. + * Revision 1.31 2004/07/10 19:11:18 mcr
  33139. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  33140. + *
  33141. + * Revision 1.30 2004/04/25 21:23:11 ken
  33142. + * Pull in dhr's changes from FreeS/WAN 2.06
  33143. + *
  33144. + * Revision 1.29 2004/04/06 02:49:26 mcr
  33145. + * pullup of algo code from alg-branch.
  33146. + *
  33147. + * Revision 1.28 2004/03/28 20:29:58 paul
  33148. + * <hugh_> ssize_t, not ssized_t
  33149. + *
  33150. + * Revision 1.27 2004/03/28 20:27:20 paul
  33151. + * Included tested and confirmed fixes mcr made and dhr verified for
  33152. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  33153. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  33154. + * dhr. (thanks dhr!)
  33155. + *
  33156. + * Revision 1.26 2004/02/09 22:07:06 mcr
  33157. + * added information about nat-traversal setting to spi-output.
  33158. + *
  33159. + * Revision 1.25.4.1 2004/04/05 04:30:46 mcr
  33160. + * patches for alg-branch to compile/work with 2.x openswan
  33161. + *
  33162. + * Revision 1.25 2003/10/31 02:27:55 mcr
  33163. + * pulled up port-selector patches and sa_id elimination.
  33164. + *
  33165. + * Revision 1.24.4.1 2003/10/29 01:30:41 mcr
  33166. + * elimited "struct sa_id".
  33167. + *
  33168. + * Revision 1.24 2003/06/20 01:42:21 mcr
  33169. + * added counters to measure how many ACQUIREs we send to pluto,
  33170. + * and how many are successfully sent.
  33171. + *
  33172. + * Revision 1.23 2003/04/03 17:38:09 rgb
  33173. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  33174. + *
  33175. + * Revision 1.22 2002/09/20 15:40:57 rgb
  33176. + * Renamed saref macros for consistency and brevity.
  33177. + *
  33178. + * Revision 1.21 2002/09/20 05:01:35 rgb
  33179. + * Print ref and reftable, refentry seperately.
  33180. + *
  33181. + * Revision 1.20 2002/09/19 02:35:39 mcr
  33182. + * do not define structures needed by /proc/net/ipsec/ if we
  33183. + * aren't going create that directory.
  33184. + *
  33185. + * Revision 1.19 2002/09/10 01:43:25 mcr
  33186. + * fixed problem in /-* comment.
  33187. + *
  33188. + * Revision 1.18 2002/09/03 16:22:11 mcr
  33189. + * fixed initialization of birth/stuff values - some simple
  33190. + * screw ups in the code.
  33191. + * removed debugging that was left in by mistake.
  33192. + *
  33193. + * Revision 1.17 2002/09/02 17:54:53 mcr
  33194. + * changed how the table driven /proc entries are created so that
  33195. + * making subdirs is now explicit rather than implicit.
  33196. + *
  33197. + * Revision 1.16 2002/08/30 01:23:37 mcr
  33198. + * reorganized /proc creating code to clear up ifdefs,
  33199. + * make the 2.4 code table driven, and put things into
  33200. + * /proc/net/ipsec subdir. Symlinks are left for compatibility.
  33201. + *
  33202. + * Revision 1.15 2002/08/13 19:01:25 mcr
  33203. + * patches from kenb to permit compilation of FreeSWAN on ia64.
  33204. + * des library patched to use proper DES_LONG type for ia64.
  33205. + *
  33206. + * Revision 1.14 2002/07/26 08:48:31 rgb
  33207. + * Added SA ref table code.
  33208. + *
  33209. + * Revision 1.13 2002/07/24 18:44:54 rgb
  33210. + * Type fiddling to tame ia64 compiler.
  33211. + *
  33212. + * Revision 1.12 2002/05/27 18:56:07 rgb
  33213. + * Convert to dynamic ipsec device allocation.
  33214. + *
  33215. + * Revision 1.11 2002/05/23 07:14:50 rgb
  33216. + * Added refcount code.
  33217. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  33218. + * Convert "usecount" to "refcount" to remove ambiguity.
  33219. + *
  33220. + * Revision 1.10 2002/04/24 07:55:32 mcr
  33221. + * #include patches and Makefiles for post-reorg compilation.
  33222. + *
  33223. + * Revision 1.9 2002/04/24 07:36:28 mcr
  33224. + * Moved from ./klips/net/ipsec/ipsec_proc.c,v
  33225. + *
  33226. + * Revision 1.8 2002/01/29 17:17:55 mcr
  33227. + * moved include of ipsec_param.h to after include of linux/kernel.h
  33228. + * otherwise, it seems that some option that is set in ipsec_param.h
  33229. + * screws up something subtle in the include path to kernel.h, and
  33230. + * it complains on the snprintf() prototype.
  33231. + *
  33232. + * Revision 1.7 2002/01/29 04:00:52 mcr
  33233. + * more excise of kversions.h header.
  33234. + *
  33235. + * Revision 1.6 2002/01/29 02:13:17 mcr
  33236. + * introduction of ipsec_kversion.h means that include of
  33237. + * ipsec_param.h must preceed any decisions about what files to
  33238. + * include to deal with differences in kernel source.
  33239. + *
  33240. + * Revision 1.5 2002/01/12 02:54:30 mcr
  33241. + * beginnings of /proc/net/ipsec dir.
  33242. + *
  33243. + * Revision 1.4 2001/12/11 02:21:05 rgb
  33244. + * Don't include module version here, fixing 2.2 compile bug.
  33245. + *
  33246. + * Revision 1.3 2001/12/05 07:19:44 rgb
  33247. + * Fixed extraneous #include "version.c" bug causing modular KLIPS failure.
  33248. + *
  33249. + * Revision 1.2 2001/11/26 09:16:14 rgb
  33250. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  33251. + *
  33252. + * Revision 1.74 2001/11/22 05:44:11 henry
  33253. + * new version stuff
  33254. + *
  33255. + * Revision 1.1.2.1 2001/09/25 02:19:40 mcr
  33256. + * /proc manipulation code moved to new ipsec_proc.c
  33257. + *
  33258. + *
  33259. + * Local variables:
  33260. + * c-file-style: "linux"
  33261. + * End:
  33262. + *
  33263. + */
  33264. --- /dev/null Tue Mar 11 13:02:56 2003
  33265. +++ linux/net/ipsec/ipsec_radij.c Mon Feb 9 13:51:03 2004
  33266. @@ -0,0 +1,889 @@
  33267. +/*
  33268. + * Interface between the IPSEC code and the radix (radij) tree code
  33269. + * Copyright (C) 1996, 1997 John Ioannidis.
  33270. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  33271. + *
  33272. + * This program is free software; you can redistribute it and/or modify it
  33273. + * under the terms of the GNU General Public License as published by the
  33274. + * Free Software Foundation; either version 2 of the License, or (at your
  33275. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  33276. + *
  33277. + * This program is distributed in the hope that it will be useful, but
  33278. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  33279. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  33280. + * for more details.
  33281. + *
  33282. + * RCSID $Id: ipsec_radij.c,v 1.73.2.1 2006/10/06 21:39:26 paul Exp $
  33283. + */
  33284. +
  33285. +#ifndef AUTOCONF_INCLUDED
  33286. +#include <linux/config.h>
  33287. +#endif
  33288. +#include <linux/version.h>
  33289. +#include <linux/kernel.h> /* printk() */
  33290. +
  33291. +#include "openswan/ipsec_param.h"
  33292. +
  33293. +#ifdef MALLOC_SLAB
  33294. +# include <linux/slab.h> /* kmalloc() */
  33295. +#else /* MALLOC_SLAB */
  33296. +# include <linux/malloc.h> /* kmalloc() */
  33297. +#endif /* MALLOC_SLAB */
  33298. +#include <linux/errno.h> /* error codes */
  33299. +#include <linux/types.h> /* size_t */
  33300. +#include <linux/interrupt.h> /* mark_bh */
  33301. +
  33302. +#include <linux/netdevice.h> /* struct device, struct net_device_stats and other headers */
  33303. +#include <linux/etherdevice.h> /* eth_type_trans */
  33304. +#include <linux/ip.h> /* struct iphdr */
  33305. +#include <linux/skbuff.h>
  33306. +#include <openswan.h>
  33307. +#ifdef SPINLOCK
  33308. +# ifdef SPINLOCK_23
  33309. +# include <linux/spinlock.h> /* *lock* */
  33310. +# else /* 23_SPINLOCK */
  33311. +# include <asm/spinlock.h> /* *lock* */
  33312. +# endif /* 23_SPINLOCK */
  33313. +#endif /* SPINLOCK */
  33314. +
  33315. +#include <net/ip.h>
  33316. +
  33317. +#include "openswan/ipsec_eroute.h"
  33318. +#include "openswan/ipsec_sa.h"
  33319. +
  33320. +#include "openswan/radij.h"
  33321. +#include "openswan/ipsec_encap.h"
  33322. +#include "openswan/radij.h"
  33323. +#include "openswan/ipsec_encap.h"
  33324. +#include "openswan/ipsec_radij.h"
  33325. +#include "openswan/ipsec_tunnel.h" /* struct ipsecpriv */
  33326. +#include "openswan/ipsec_xform.h"
  33327. +
  33328. +#include <pfkeyv2.h>
  33329. +#include <pfkey.h>
  33330. +
  33331. +#include "openswan/ipsec_proto.h"
  33332. +
  33333. +#ifdef CONFIG_KLIPS_DEBUG
  33334. +int debug_radij = 0;
  33335. +#endif /* CONFIG_KLIPS_DEBUG */
  33336. +
  33337. +struct radij_node_head *rnh = NULL;
  33338. +#ifdef SPINLOCK
  33339. +spinlock_t eroute_lock = SPIN_LOCK_UNLOCKED;
  33340. +#else /* SPINLOCK */
  33341. +spinlock_t eroute_lock;
  33342. +#endif /* SPINLOCK */
  33343. +
  33344. +int
  33345. +ipsec_radijinit(void)
  33346. +{
  33347. + maj_keylen = sizeof (struct sockaddr_encap);
  33348. +
  33349. + rj_init();
  33350. +
  33351. + if (rj_inithead((void **)&rnh, /*16*/offsetof(struct sockaddr_encap, sen_type) * sizeof(__u8)) == 0) /* 16 is bit offset of sen_type */
  33352. + return -1;
  33353. + return 0;
  33354. +}
  33355. +
  33356. +int
  33357. +ipsec_radijcleanup(void)
  33358. +{
  33359. + int error;
  33360. +
  33361. + spin_lock_bh(&eroute_lock);
  33362. +
  33363. + error = radijcleanup();
  33364. +
  33365. + spin_unlock_bh(&eroute_lock);
  33366. +
  33367. + return error;
  33368. +}
  33369. +
  33370. +int
  33371. +ipsec_cleareroutes(void)
  33372. +{
  33373. + int error;
  33374. +
  33375. + spin_lock_bh(&eroute_lock);
  33376. +
  33377. + error = radijcleartree();
  33378. +
  33379. + spin_unlock_bh(&eroute_lock);
  33380. +
  33381. + return error;
  33382. +}
  33383. +
  33384. +int
  33385. +ipsec_breakroute(struct sockaddr_encap *eaddr,
  33386. + struct sockaddr_encap *emask,
  33387. + struct sk_buff **first,
  33388. + struct sk_buff **last)
  33389. +{
  33390. + struct eroute *ro;
  33391. + struct radij_node *rn;
  33392. + int error;
  33393. +#ifdef CONFIG_KLIPS_DEBUG
  33394. +
  33395. + if (debug_eroute) {
  33396. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33397. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33398. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33399. + KLIPS_PRINT(debug_eroute,
  33400. + "klips_debug:ipsec_breakroute: "
  33401. + "attempting to delete eroute for %s:%d->%s:%d %d\n",
  33402. + buf1, ntohs(eaddr->sen_sport),
  33403. + buf2, ntohs(eaddr->sen_dport), eaddr->sen_proto);
  33404. + }
  33405. +#endif /* CONFIG_KLIPS_DEBUG */
  33406. +
  33407. + spin_lock_bh(&eroute_lock);
  33408. +
  33409. + if ((error = rj_delete(eaddr, emask, rnh, &rn)) != 0) {
  33410. + spin_unlock_bh(&eroute_lock);
  33411. + KLIPS_PRINT(debug_eroute,
  33412. + "klips_debug:ipsec_breakroute: "
  33413. + "node not found, eroute delete failed.\n");
  33414. + return error;
  33415. + }
  33416. +
  33417. + spin_unlock_bh(&eroute_lock);
  33418. +
  33419. + ro = (struct eroute *)rn;
  33420. +
  33421. + KLIPS_PRINT(debug_eroute,
  33422. + "klips_debug:ipsec_breakroute: "
  33423. + "deleted eroute=0p%p, ident=0p%p->0p%p, first=0p%p, last=0p%p\n",
  33424. + ro,
  33425. + ro->er_ident_s.data,
  33426. + ro->er_ident_d.data,
  33427. + ro->er_first,
  33428. + ro->er_last);
  33429. +
  33430. + if (ro->er_ident_s.data != NULL) {
  33431. + kfree(ro->er_ident_s.data);
  33432. + }
  33433. + if (ro->er_ident_d.data != NULL) {
  33434. + kfree(ro->er_ident_d.data);
  33435. + }
  33436. + if (ro->er_first != NULL) {
  33437. +#if 0
  33438. + struct net_device_stats *stats = (struct net_device_stats *) &(((struct ipsecpriv *)(ro->er_first->dev->priv))->mystats);
  33439. + stats->tx_dropped--;
  33440. +#endif
  33441. + *first = ro->er_first;
  33442. + }
  33443. + if (ro->er_last != NULL) {
  33444. +#if 0
  33445. + struct net_device_stats *stats = (struct net_device_stats *) &(((struct ipsecpriv *)(ro->er_last->dev->priv))->mystats);
  33446. + stats->tx_dropped--;
  33447. +#endif
  33448. + *last = ro->er_last;
  33449. + }
  33450. +
  33451. + if (rn->rj_flags & (RJF_ACTIVE | RJF_ROOT))
  33452. + panic ("ipsec_breakroute RMT_DELEROUTE root or active node\n");
  33453. + memset((caddr_t)rn, 0, sizeof (struct eroute));
  33454. + kfree(rn);
  33455. +
  33456. + return 0;
  33457. +}
  33458. +
  33459. +int
  33460. +ipsec_makeroute(struct sockaddr_encap *eaddr,
  33461. + struct sockaddr_encap *emask,
  33462. + ip_said said,
  33463. + uint32_t pid,
  33464. + struct sk_buff *skb,
  33465. + struct ident *ident_s,
  33466. + struct ident *ident_d)
  33467. +{
  33468. + struct eroute *retrt;
  33469. + int error;
  33470. + char sa[SATOT_BUF];
  33471. + size_t sa_len;
  33472. +
  33473. +#ifdef CONFIG_KLIPS_DEBUG
  33474. +
  33475. + if (debug_eroute) {
  33476. +
  33477. + {
  33478. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33479. +
  33480. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33481. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33482. + sa_len = satot(&said, 0, sa, sizeof(sa));
  33483. + KLIPS_PRINT(debug_eroute,
  33484. + "klips_debug:ipsec_makeroute: "
  33485. + "attempting to allocate %lu bytes to insert eroute for %s->%s, SA: %s, PID:%d, skb=0p%p, ident:%s->%s\n",
  33486. + (unsigned long) sizeof(struct eroute),
  33487. + buf1,
  33488. + buf2,
  33489. + sa_len ? sa : " (error)",
  33490. + pid,
  33491. + skb,
  33492. + (ident_s ? (ident_s->data ? ident_s->data : "NULL") : "NULL"),
  33493. + (ident_d ? (ident_d->data ? ident_d->data : "NULL") : "NULL"));
  33494. + }
  33495. + {
  33496. + char buf1[sizeof(struct sockaddr_encap)*2 + 1],
  33497. + buf2[sizeof(struct sockaddr_encap)*2 + 1];
  33498. + int i;
  33499. + unsigned char *b1 = buf1,
  33500. + *b2 = buf2,
  33501. + *ea = (unsigned char *)eaddr,
  33502. + *em = (unsigned char *)emask;
  33503. +
  33504. +
  33505. + for (i=0; i<sizeof(struct sockaddr_encap); i++) {
  33506. + sprintf(b1, "%02x", ea[i]);
  33507. + sprintf(b2, "%02x", em[i]);
  33508. + b1+=2;
  33509. + b2+=2;
  33510. + }
  33511. + KLIPS_PRINT(debug_eroute, "klips_debug:ipsec_makeroute: %s / %s \n", buf1, buf2);
  33512. + }
  33513. +
  33514. + }
  33515. +#endif /* CONFIG_KLIPS_DEBUG */
  33516. +
  33517. + retrt = (struct eroute *)kmalloc(sizeof (struct eroute), GFP_ATOMIC);
  33518. + if (retrt == NULL) {
  33519. + printk("klips_error:ipsec_makeroute: "
  33520. + "not able to allocate kernel memory");
  33521. + return -ENOMEM;
  33522. + }
  33523. + memset((caddr_t)retrt, 0, sizeof (struct eroute));
  33524. +
  33525. + retrt->er_eaddr = *eaddr;
  33526. + retrt->er_emask = *emask;
  33527. + retrt->er_said = said;
  33528. + retrt->er_pid = pid;
  33529. + retrt->er_count = 0;
  33530. + retrt->er_lasttime = jiffies/HZ;
  33531. +
  33532. + {
  33533. + /* this is because gcc 3. doesn't like cast's as lvalues */
  33534. + struct rjtentry *rje = (struct rjtentry *)&(retrt->er_rjt);
  33535. + caddr_t er = (caddr_t)&(retrt->er_eaddr);
  33536. +
  33537. + rje->rd_nodes->rj_key= er;
  33538. + }
  33539. +
  33540. + if (ident_s && ident_s->type != SADB_IDENTTYPE_RESERVED) {
  33541. + int data_len = ident_s->len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  33542. +
  33543. + retrt->er_ident_s.type = ident_s->type;
  33544. + retrt->er_ident_s.id = ident_s->id;
  33545. + retrt->er_ident_s.len = ident_s->len;
  33546. + if(data_len) {
  33547. + KLIPS_PRINT(debug_eroute,
  33548. + "klips_debug:ipsec_makeroute: "
  33549. + "attempting to allocate %u bytes for ident_s.\n",
  33550. + data_len);
  33551. + if(!(retrt->er_ident_s.data = kmalloc(data_len, GFP_KERNEL))) {
  33552. + kfree(retrt);
  33553. + printk("klips_error:ipsec_makeroute: not able to allocate kernel memory (%d)\n", data_len);
  33554. + return ENOMEM;
  33555. + }
  33556. + memcpy(retrt->er_ident_s.data, ident_s->data, data_len);
  33557. + } else {
  33558. + retrt->er_ident_s.data = NULL;
  33559. + }
  33560. + }
  33561. +
  33562. + if (ident_d && ident_d->type != SADB_IDENTTYPE_RESERVED) {
  33563. + int data_len = ident_d->len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  33564. +
  33565. + retrt->er_ident_d.type = ident_d->type;
  33566. + retrt->er_ident_d.id = ident_d->id;
  33567. + retrt->er_ident_d.len = ident_d->len;
  33568. + if(data_len) {
  33569. + KLIPS_PRINT(debug_eroute,
  33570. + "klips_debug:ipsec_makeroute: "
  33571. + "attempting to allocate %u bytes for ident_d.\n",
  33572. + data_len);
  33573. + if(!(retrt->er_ident_d.data = kmalloc(data_len, GFP_KERNEL))) {
  33574. + if (retrt->er_ident_s.data)
  33575. + kfree(retrt->er_ident_s.data);
  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_d.data, ident_d->data, data_len);
  33581. + } else {
  33582. + retrt->er_ident_d.data = NULL;
  33583. + }
  33584. + }
  33585. + retrt->er_first = skb;
  33586. + retrt->er_last = NULL;
  33587. +
  33588. + KLIPS_PRINT(debug_eroute,
  33589. + "klips_debug:ipsec_makeroute: "
  33590. + "calling rj_addroute now\n");
  33591. +
  33592. + spin_lock_bh(&eroute_lock);
  33593. +
  33594. + error = rj_addroute(&(retrt->er_eaddr), &(retrt->er_emask),
  33595. + rnh, retrt->er_rjt.rd_nodes);
  33596. +
  33597. + spin_unlock_bh(&eroute_lock);
  33598. +
  33599. + if(error) {
  33600. + sa_len = satot(&said, 0, sa, sizeof(sa));
  33601. + KLIPS_PRINT(debug_eroute,
  33602. + "klips_debug:ipsec_makeroute: "
  33603. + "rj_addroute not able to insert eroute for SA:%s (error:%d)\n",
  33604. + sa_len ? sa : " (error)", error);
  33605. + if (retrt->er_ident_s.data)
  33606. + kfree(retrt->er_ident_s.data);
  33607. + if (retrt->er_ident_d.data)
  33608. + kfree(retrt->er_ident_d.data);
  33609. +
  33610. + kfree(retrt);
  33611. +
  33612. + return error;
  33613. + }
  33614. +
  33615. +#ifdef CONFIG_KLIPS_DEBUG
  33616. + if (debug_eroute) {
  33617. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33618. +/*
  33619. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33620. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33621. +*/
  33622. + subnettoa(rd_key((&(retrt->er_rjt)))->sen_ip_src, rd_mask((&(retrt->er_rjt)))->sen_ip_src, 0, buf1, sizeof(buf1));
  33623. + subnettoa(rd_key((&(retrt->er_rjt)))->sen_ip_dst, rd_mask((&(retrt->er_rjt)))->sen_ip_dst, 0, buf2, sizeof(buf2));
  33624. + sa_len = satot(&retrt->er_said, 0, sa, sizeof(sa));
  33625. +
  33626. + KLIPS_PRINT(debug_eroute,
  33627. + "klips_debug:ipsec_makeroute: "
  33628. + "pid=%05d "
  33629. + "count=%10d "
  33630. + "lasttime=%6d "
  33631. + "%-18s -> %-18s => %s\n",
  33632. + retrt->er_pid,
  33633. + retrt->er_count,
  33634. + (int)(jiffies/HZ - retrt->er_lasttime),
  33635. + buf1,
  33636. + buf2,
  33637. + sa_len ? sa : " (error)");
  33638. + }
  33639. +#endif /* CONFIG_KLIPS_DEBUG */
  33640. + KLIPS_PRINT(debug_eroute,
  33641. + "klips_debug:ipsec_makeroute: "
  33642. + "succeeded.\n");
  33643. + return 0;
  33644. +}
  33645. +
  33646. +struct eroute *
  33647. +ipsec_findroute(struct sockaddr_encap *eaddr)
  33648. +{
  33649. + struct radij_node *rn;
  33650. +#ifdef CONFIG_KLIPS_DEBUG
  33651. + char buf1[ADDRTOA_BUF], buf2[ADDRTOA_BUF];
  33652. +
  33653. + if (debug_radij & DB_RJ_FINDROUTE) {
  33654. + addrtoa(eaddr->sen_ip_src, 0, buf1, sizeof(buf1));
  33655. + addrtoa(eaddr->sen_ip_dst, 0, buf2, sizeof(buf2));
  33656. + KLIPS_PRINT(debug_eroute,
  33657. + "klips_debug:ipsec_findroute: "
  33658. + "%s:%d->%s:%d %d\n",
  33659. + buf1, ntohs(eaddr->sen_sport),
  33660. + buf2, ntohs(eaddr->sen_dport),
  33661. + eaddr->sen_proto);
  33662. + }
  33663. +#endif /* CONFIG_KLIPS_DEBUG */
  33664. + rn = rj_match((caddr_t)eaddr, rnh);
  33665. + if(rn) {
  33666. + KLIPS_PRINT(debug_eroute && sysctl_ipsec_debug_verbose,
  33667. + "klips_debug:ipsec_findroute: "
  33668. + "found, points to proto=%d, spi=%x, dst=%x.\n",
  33669. + ((struct eroute*)rn)->er_said.proto,
  33670. + ntohl(((struct eroute*)rn)->er_said.spi),
  33671. + ntohl(((struct eroute*)rn)->er_said.dst.u.v4.sin_addr.s_addr));
  33672. + }
  33673. + return (struct eroute *)rn;
  33674. +}
  33675. +
  33676. +#ifdef CONFIG_PROC_FS
  33677. +/** ipsec_rj_walker_procprint: print one line of eroute table output.
  33678. + *
  33679. + * Theoretical BUG: if w->length is less than the length
  33680. + * of some line we should produce, that line will never
  33681. + * be finished. In effect, the "file" will stop part way
  33682. + * through that line.
  33683. + */
  33684. +int
  33685. +ipsec_rj_walker_procprint(struct radij_node *rn, void *w0)
  33686. +{
  33687. + struct eroute *ro = (struct eroute *)rn;
  33688. + struct rjtentry *rd = (struct rjtentry *)rn;
  33689. + struct wsbuf *w = (struct wsbuf *)w0;
  33690. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33691. + char buf3[16];
  33692. + char sa[SATOT_BUF];
  33693. + size_t sa_len, buf_len;
  33694. + struct sockaddr_encap *key, *mask;
  33695. +
  33696. + KLIPS_PRINT(debug_radij,
  33697. + "klips_debug:ipsec_rj_walker_procprint: "
  33698. + "rn=0p%p, w0=0p%p\n",
  33699. + rn,
  33700. + w0);
  33701. + if (rn->rj_b >= 0) {
  33702. + return 0;
  33703. + }
  33704. +
  33705. + key = rd_key(rd);
  33706. + mask = rd_mask(rd);
  33707. +
  33708. + if (key == NULL || mask == NULL) {
  33709. + return 0;
  33710. + }
  33711. +
  33712. + buf_len = subnettoa(key->sen_ip_src, mask->sen_ip_src, 0, buf1, sizeof(buf1));
  33713. + if(key->sen_sport != 0) {
  33714. + sprintf(buf1+buf_len-1, ":%d", ntohs(key->sen_sport));
  33715. + }
  33716. +
  33717. + buf_len = subnettoa(key->sen_ip_dst, mask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33718. + if(key->sen_dport != 0) {
  33719. + sprintf(buf2+buf_len-1, ":%d", ntohs(key->sen_dport));
  33720. + }
  33721. +
  33722. + buf3[0]='\0';
  33723. + if(key->sen_proto != 0) {
  33724. + sprintf(buf3, ":%d", key->sen_proto);
  33725. + }
  33726. +
  33727. + sa_len = satot(&ro->er_said, 'x', sa, sizeof(sa));
  33728. + w->len += ipsec_snprintf(w->buffer + w->len,
  33729. + w->length - w->len,
  33730. + "%-10d "
  33731. + "%-18s -> %-18s => %s%s\n",
  33732. + ro->er_count,
  33733. + buf1,
  33734. + buf2,
  33735. + sa_len ? sa : " (error)",
  33736. + buf3);
  33737. +
  33738. + {
  33739. + /* snprintf can only fill the last character with NUL
  33740. + * so the maximum useful character is w->length-1.
  33741. + * However, if w->length == 0, we cannot go back.
  33742. + * (w->length surely cannot be negative.)
  33743. + */
  33744. + int max_content = w->length > 0? w->length-1 : 0;
  33745. +
  33746. + if (w->len >= max_content) {
  33747. + /* we've done all that can fit -- stop treewalking */
  33748. + w->len = max_content; /* truncate crap */
  33749. + return -ENOBUFS;
  33750. + } else {
  33751. + const off_t pos = w->begin + w->len; /* file position of end of what we've generated */
  33752. +
  33753. + if (pos <= w->offset) {
  33754. + /* all is before first interesting character:
  33755. + * discard, but note where we are.
  33756. + */
  33757. + w->len = 0;
  33758. + w->begin = pos;
  33759. + }
  33760. + return 0;
  33761. + }
  33762. + }
  33763. +}
  33764. +#endif /* CONFIG_PROC_FS */
  33765. +
  33766. +int
  33767. +ipsec_rj_walker_delete(struct radij_node *rn, void *w0)
  33768. +{
  33769. + struct eroute *ro;
  33770. + struct rjtentry *rd = (struct rjtentry *)rn;
  33771. + struct radij_node *rn2;
  33772. + int error;
  33773. + struct sockaddr_encap *key, *mask;
  33774. +
  33775. + key = rd_key(rd);
  33776. + mask = rd_mask(rd);
  33777. +
  33778. + if(!key || !mask) {
  33779. + return -ENODATA;
  33780. + }
  33781. +#ifdef CONFIG_KLIPS_DEBUG
  33782. + if(debug_radij) {
  33783. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33784. + subnettoa(key->sen_ip_src, mask->sen_ip_src, 0, buf1, sizeof(buf1));
  33785. + subnettoa(key->sen_ip_dst, mask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33786. + KLIPS_PRINT(debug_radij,
  33787. + "klips_debug:ipsec_rj_walker_delete: "
  33788. + "deleting: %s -> %s\n",
  33789. + buf1,
  33790. + buf2);
  33791. + }
  33792. +#endif /* CONFIG_KLIPS_DEBUG */
  33793. +
  33794. + if((error = rj_delete(key, mask, rnh, &rn2))) {
  33795. + KLIPS_PRINT(debug_radij,
  33796. + "klips_debug:ipsec_rj_walker_delete: "
  33797. + "rj_delete failed with error=%d.\n", error);
  33798. + return error;
  33799. + }
  33800. +
  33801. + if(rn2 != rn) {
  33802. + printk("klips_debug:ipsec_rj_walker_delete: "
  33803. + "tried to delete a different node?!? This should never happen!\n");
  33804. + }
  33805. +
  33806. + ro = (struct eroute *)rn;
  33807. +
  33808. + if (ro->er_ident_s.data)
  33809. + kfree(ro->er_ident_s.data);
  33810. + if (ro->er_ident_d.data)
  33811. + kfree(ro->er_ident_d.data);
  33812. +
  33813. + memset((caddr_t)rn, 0, sizeof (struct eroute));
  33814. + kfree(rn);
  33815. +
  33816. + return 0;
  33817. +}
  33818. +
  33819. +/*
  33820. + * $Log: ipsec_radij.c,v $
  33821. + * Revision 1.73.2.1 2006/10/06 21:39:26 paul
  33822. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  33823. + * set. This is defined through autoconf.h which is included through the
  33824. + * linux kernel build macros.
  33825. + *
  33826. + * Revision 1.73 2005/04/29 05:10:22 mcr
  33827. + * removed from extraenous includes to make unit testing easier.
  33828. + *
  33829. + * Revision 1.72 2004/12/03 21:25:57 mcr
  33830. + * compile time fixes for running on 2.6.
  33831. + * still experimental.
  33832. + *
  33833. + * Revision 1.71 2004/07/10 19:11:18 mcr
  33834. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  33835. + *
  33836. + * Revision 1.70 2004/04/25 21:10:52 ken
  33837. + * Pull in dhr's changes from FreeS/WAN 2.06
  33838. + *
  33839. + * Revision 1.69 2004/04/06 02:49:26 mcr
  33840. + * pullup of algo code from alg-branch.
  33841. + *
  33842. + * Revision 1.68 2004/03/28 20:27:20 paul
  33843. + * Included tested and confirmed fixes mcr made and dhr verified for
  33844. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  33845. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  33846. + * dhr. (thanks dhr!)
  33847. + *
  33848. + * Revision 1.67.4.1 2004/04/05 04:30:46 mcr
  33849. + * patches for alg-branch to compile/work with 2.x openswan
  33850. + *
  33851. + * Revision 1.67 2003/10/31 02:27:55 mcr
  33852. + * pulled up port-selector patches and sa_id elimination.
  33853. + *
  33854. + * Revision 1.66.24.2 2003/10/29 01:30:41 mcr
  33855. + * elimited "struct sa_id".
  33856. + *
  33857. + * Revision 1.66.24.1 2003/09/21 13:59:56 mcr
  33858. + * pre-liminary X.509 patch - does not yet pass tests.
  33859. + *
  33860. + * Revision 1.66 2002/10/12 23:11:53 dhr
  33861. + *
  33862. + * [KenB + DHR] more 64-bit cleanup
  33863. + *
  33864. + * Revision 1.65 2002/09/20 05:01:40 rgb
  33865. + * Added memory allocation debugging.
  33866. + *
  33867. + * Revision 1.64 2002/05/31 01:46:05 mcr
  33868. + * added && sysctl_ipsec_debug_verbose verbose to ipsec_findroute
  33869. + * as requested in PR#14.
  33870. + *
  33871. + * Revision 1.63 2002/05/23 07:14:11 rgb
  33872. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  33873. + *
  33874. + * Revision 1.62 2002/04/24 07:55:32 mcr
  33875. + * #include patches and Makefiles for post-reorg compilation.
  33876. + *
  33877. + * Revision 1.61 2002/04/24 07:36:29 mcr
  33878. + * Moved from ./klips/net/ipsec/ipsec_radij.c,v
  33879. + *
  33880. + * Revision 1.60 2002/02/19 23:59:45 rgb
  33881. + * Removed redundant compiler directives.
  33882. + *
  33883. + * Revision 1.59 2002/02/06 04:13:47 mcr
  33884. + * missing #ifdef CONFIG_IPSEC_DEBUG.
  33885. + *
  33886. + * Revision 1.58 2002/01/29 17:17:56 mcr
  33887. + * moved include of ipsec_param.h to after include of linux/kernel.h
  33888. + * otherwise, it seems that some option that is set in ipsec_param.h
  33889. + * screws up something subtle in the include path to kernel.h, and
  33890. + * it complains on the snprintf() prototype.
  33891. + *
  33892. + * Revision 1.57 2002/01/29 04:00:52 mcr
  33893. + * more excise of kversions.h header.
  33894. + *
  33895. + * Revision 1.56 2002/01/29 02:13:17 mcr
  33896. + * introduction of ipsec_kversion.h means that include of
  33897. + * ipsec_param.h must preceed any decisions about what files to
  33898. + * include to deal with differences in kernel source.
  33899. + *
  33900. + * Revision 1.55 2001/11/26 09:23:48 rgb
  33901. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  33902. + *
  33903. + * Revision 1.53.2.1 2001/09/25 02:26:32 mcr
  33904. + * headers adjusted for new usage.
  33905. + *
  33906. + * Revision 1.54 2001/10/18 04:45:20 rgb
  33907. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  33908. + * lib/freeswan.h version macros moved to lib/kversions.h.
  33909. + * Other compiler directive cleanups.
  33910. + *
  33911. + * Revision 1.53 2001/09/19 17:19:40 rgb
  33912. + * Debug output bugfix for NetCelo's PF_KEY ident patch.
  33913. + *
  33914. + * Revision 1.52 2001/09/19 16:33:37 rgb
  33915. + * Temporarily disable ident fields to /proc/net/ipsec_eroute.
  33916. + *
  33917. + * Revision 1.51 2001/09/15 16:24:04 rgb
  33918. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  33919. + *
  33920. + * Revision 1.50 2001/09/14 16:58:36 rgb
  33921. + * Added support for storing the first and last packets through a HOLD.
  33922. + *
  33923. + * Revision 1.49 2001/09/08 21:13:32 rgb
  33924. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  33925. + *
  33926. + * Revision 1.48 2001/06/15 04:12:56 rgb
  33927. + * Fixed kernel memory allocation error return code polarity bug.
  33928. + *
  33929. + * Revision 1.47 2001/06/14 19:35:09 rgb
  33930. + * Update copyright date.
  33931. + *
  33932. + * Revision 1.46 2001/06/08 08:47:18 rgb
  33933. + * Fixed for debug disabled.
  33934. + *
  33935. + * Revision 1.45 2001/05/27 06:12:11 rgb
  33936. + * Added structures for pid, packet count and last access time to eroute.
  33937. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  33938. + *
  33939. + * Revision 1.44 2001/05/03 19:41:01 rgb
  33940. + * Initialise error return variable.
  33941. + * Use more appropriate return value for ipsec_rj_walker_delete().
  33942. + *
  33943. + * Revision 1.43 2001/02/27 22:24:54 rgb
  33944. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  33945. + * Check for satoa() return codes.
  33946. + *
  33947. + * Revision 1.42 2001/02/27 06:21:57 rgb
  33948. + * Added findroute success instrumentation.
  33949. + *
  33950. + * Revision 1.41 2000/11/06 04:32:08 rgb
  33951. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  33952. + *
  33953. + * Revision 1.40 2000/09/08 19:12:56 rgb
  33954. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  33955. + *
  33956. + * Revision 1.39 2000/08/30 05:25:20 rgb
  33957. + * Correct debug text in ipsec_breakroute() from incorrect
  33958. + * "ipsec_callback".
  33959. + *
  33960. + * Revision 1.38 2000/07/28 14:58:31 rgb
  33961. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  33962. + *
  33963. + * Revision 1.37 2000/03/16 14:02:50 rgb
  33964. + * Fixed debug scope to enable compilation with debug off.
  33965. + *
  33966. + * Revision 1.36 2000/01/21 06:14:46 rgb
  33967. + * Added debugging text to ipsec_rj_walker_delete().
  33968. + * Set return code to negative for consistency.
  33969. + *
  33970. + * Revision 1.35 1999/11/23 23:05:24 rgb
  33971. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  33972. + *
  33973. + * Revision 1.34 1999/11/18 04:13:56 rgb
  33974. + * Replaced all kernel version macros to shorter, readable form.
  33975. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  33976. + *
  33977. + * Revision 1.33 1999/11/17 15:53:39 rgb
  33978. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  33979. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  33980. + * klips/net/ipsec/Makefile.
  33981. + *
  33982. + * Revision 1.32 1999/10/26 13:58:33 rgb
  33983. + * Put spinlock flags variable declaration outside the debug compiler
  33984. + * directive to enable compilation with debug shut off.
  33985. + *
  33986. + * Revision 1.31 1999/10/15 22:13:29 rgb
  33987. + * Clean out cruft.
  33988. + * Align /proc/net/ipsec_eroute output for easier readability.
  33989. + * Fix double linefeed in radij debug output.
  33990. + * Fix double locking bug that locks up 2.0.36 but not 2.0.38.
  33991. + *
  33992. + * Revision 1.30 1999/10/08 18:37:33 rgb
  33993. + * Fix end-of-line spacing to sate whining PHMs.
  33994. + *
  33995. + * Revision 1.29 1999/10/03 18:52:45 rgb
  33996. + * Spinlock support for 2.0.xx.
  33997. + * Dumb return code spin_unlock fix.
  33998. + *
  33999. + * Revision 1.28 1999/10/01 16:22:24 rgb
  34000. + * Switch from assignment init. to functional init. of spinlocks.
  34001. + *
  34002. + * Revision 1.27 1999/10/01 15:44:53 rgb
  34003. + * Move spinlock header include to 2.1> scope.
  34004. + *
  34005. + * Revision 1.26 1999/10/01 00:01:23 rgb
  34006. + * Added eroute structure locking.
  34007. + *
  34008. + * Revision 1.25 1999/06/10 16:07:30 rgb
  34009. + * Silence delete eroute on no debug.
  34010. + *
  34011. + * Revision 1.24 1999/05/09 03:25:36 rgb
  34012. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  34013. + *
  34014. + * Revision 1.23 1999/05/05 22:02:31 rgb
  34015. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  34016. + *
  34017. + * Revision 1.22 1999/04/29 15:17:23 rgb
  34018. + * Add return values to init and cleanup functions.
  34019. + * Add sanity checking for null pointer arguments.
  34020. + *
  34021. + * Revision 1.21 1999/04/11 00:28:58 henry
  34022. + * GPL boilerplate
  34023. + *
  34024. + * Revision 1.20 1999/04/06 04:54:26 rgb
  34025. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  34026. + * patch shell fixes.
  34027. + *
  34028. + * Revision 1.19 1999/02/17 16:50:35 rgb
  34029. + * Clean out unused cruft.
  34030. + * Consolidate for space and speed efficiency.
  34031. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  34032. + *
  34033. + * Revision 1.18 1999/01/22 06:22:06 rgb
  34034. + * Cruft clean-out.
  34035. + * 64-bit clean-up.
  34036. + *
  34037. + * Revision 1.17 1998/12/02 03:09:39 rgb
  34038. + * Clean up debug printing conditionals to compile with debugging off.
  34039. + *
  34040. + * Revision 1.16 1998/12/01 13:49:39 rgb
  34041. + * Wrap version info printing in debug switches.
  34042. + *
  34043. + * Revision 1.15 1998/11/30 13:22:54 rgb
  34044. + * Rationalised all the klips kernel file headers. They are much shorter
  34045. + * now and won't conflict under RH5.2.
  34046. + *
  34047. + * Revision 1.14 1998/10/31 06:48:17 rgb
  34048. + * Fixed up comments in #endif directives.
  34049. + *
  34050. + * Revision 1.13 1998/10/27 13:48:09 rgb
  34051. + * Cleaned up /proc/net/ipsec_* filesystem for easy parsing by scripts.
  34052. + * Fixed less(1) truncated output bug.
  34053. + * Code clean-up.
  34054. + *
  34055. + * Revision 1.12 1998/10/25 02:41:36 rgb
  34056. + * Change return type on ipsec_breakroute and ipsec_makeroute and add an
  34057. + * argument to be able to transmit more infomation about errors.
  34058. + * Fix cut-and-paste debug statement identifier.
  34059. + *
  34060. + * Revision 1.11 1998/10/22 06:45:39 rgb
  34061. + * Cleaned up cruft.
  34062. + * Convert to use satoa for printk.
  34063. + *
  34064. + * Revision 1.10 1998/10/19 14:44:28 rgb
  34065. + * Added inclusion of freeswan.h.
  34066. + * sa_id structure implemented and used: now includes protocol.
  34067. + *
  34068. + * Revision 1.9 1998/10/09 04:30:52 rgb
  34069. + * Added 'klips_debug' prefix to all klips printk debug statements.
  34070. + * Deleted old commented out cruft.
  34071. + *
  34072. + * Revision 1.8 1998/08/06 17:24:23 rgb
  34073. + * Fix addrtoa return code bug from stale manpage advice preventing packets
  34074. + * from being erouted.
  34075. + *
  34076. + * Revision 1.7 1998/08/06 07:44:59 rgb
  34077. + * Fixed /proc/net/ipsec_eroute subnettoa and addrtoa return value bug that
  34078. + * ended up in nothing being printed.
  34079. + *
  34080. + * Revision 1.6 1998/08/05 22:16:41 rgb
  34081. + * Cleanup to prevent cosmetic errors (ie. debug output) from being fatal.
  34082. + *
  34083. + * Revision 1.5 1998/07/29 20:38:44 rgb
  34084. + * Debug and fix subnettoa and addrtoa output.
  34085. + *
  34086. + * Revision 1.4 1998/07/28 00:02:39 rgb
  34087. + * Converting to exclusive use of addrtoa.
  34088. + * Fix eroute delete.
  34089. + *
  34090. + * Revision 1.3 1998/07/14 18:21:26 rgb
  34091. + * Add function to clear the eroute table.
  34092. + *
  34093. + * Revision 1.2 1998/06/23 02:59:14 rgb
  34094. + * Added debugging output to eroute add/delete routines.
  34095. + *
  34096. + * Revision 1.9 1998/06/18 21:29:06 henry
  34097. + * move sources from klips/src to klips/net/ipsec, to keep stupid kernel
  34098. + * build scripts happier in presence of symbolic links
  34099. + *
  34100. + * Revision 1.8 1998/06/05 02:32:26 rgb
  34101. + * Fix spi ntoh kernel debug output.
  34102. + *
  34103. + * Revision 1.7 1998/05/25 20:30:37 rgb
  34104. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  34105. + *
  34106. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  34107. + * add ipsec_rj_walker_delete.
  34108. + *
  34109. + * Revision 1.6 1998/05/21 13:08:57 rgb
  34110. + * Rewrote procinfo subroutines to avoid *bad things* when more that 3k of
  34111. + * information is available for printout.
  34112. + *
  34113. + * Revision 1.5 1998/05/18 21:35:55 rgb
  34114. + * Clean up output for numerical consistency and readability. Zero freed
  34115. + * eroute memory.
  34116. + *
  34117. + * Revision 1.4 1998/04/21 21:28:58 rgb
  34118. + * Rearrange debug switches to change on the fly debug output from user
  34119. + * space. Only kernel changes checked in at this time. radij.c was also
  34120. + * changed to temporarily remove buggy debugging code in rj_delete causing
  34121. + * an OOPS and hence, netlink device open errors.
  34122. + *
  34123. + * Revision 1.3 1998/04/14 17:30:39 rgb
  34124. + * Fix up compiling errors for radij tree memory reclamation.
  34125. + *
  34126. + * Revision 1.2 1998/04/12 22:03:23 rgb
  34127. + * Updated ESP-3DES-HMAC-MD5-96,
  34128. + * ESP-DES-HMAC-MD5-96,
  34129. + * AH-HMAC-MD5-96,
  34130. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  34131. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  34132. + *
  34133. + * Fixed eroute references in /proc/net/ipsec*.
  34134. + *
  34135. + * Started to patch module unloading memory leaks in ipsec_netlink and
  34136. + * radij tree unloading.
  34137. + *
  34138. + * Revision 1.1 1998/04/09 03:06:10 henry
  34139. + * sources moved up from linux/net/ipsec
  34140. + *
  34141. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  34142. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  34143. + *
  34144. + * Revision 0.4 1997/01/15 01:28:15 ji
  34145. + * No changes.
  34146. + *
  34147. + * Revision 0.3 1996/11/20 14:39:04 ji
  34148. + * Minor cleanups.
  34149. + * Rationalized debugging code.
  34150. + *
  34151. + * Revision 0.2 1996/11/02 00:18:33 ji
  34152. + * First limited release.
  34153. + *
  34154. + *
  34155. + */
  34156. --- /dev/null Tue Mar 11 13:02:56 2003
  34157. +++ linux/net/ipsec/ipsec_rcv.c Mon Feb 9 13:51:03 2004
  34158. @@ -0,0 +1,2304 @@
  34159. +/*
  34160. + * receive code
  34161. + * Copyright (C) 1996, 1997 John Ioannidis.
  34162. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  34163. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  34164. + *
  34165. + * This program is free software; you can redistribute it and/or modify it
  34166. + * under the terms of the GNU General Public License as published by the
  34167. + * Free Software Foundation; either version 2 of the License, or (at your
  34168. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  34169. + *
  34170. + * This program is distributed in the hope that it will be useful, but
  34171. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  34172. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  34173. + * for more details.
  34174. + */
  34175. +
  34176. +char ipsec_rcv_c_version[] = "RCSID $Id: ipsec_rcv.c,v 1.171.2.10 2006/10/06 21:39:26 paul Exp $";
  34177. +
  34178. +#ifndef AUTOCONF_INCLUDED
  34179. +#include <linux/config.h>
  34180. +#endif
  34181. +#include <linux/version.h>
  34182. +
  34183. +#define __NO_VERSION__
  34184. +#include <linux/module.h>
  34185. +#include <linux/kernel.h> /* printk() */
  34186. +
  34187. +#include "openswan/ipsec_param.h"
  34188. +
  34189. +#ifdef MALLOC_SLAB
  34190. +# include <linux/slab.h> /* kmalloc() */
  34191. +#else /* MALLOC_SLAB */
  34192. +# include <linux/malloc.h> /* kmalloc() */
  34193. +#endif /* MALLOC_SLAB */
  34194. +#include <linux/errno.h> /* error codes */
  34195. +#include <linux/types.h> /* size_t */
  34196. +#include <linux/interrupt.h> /* mark_bh */
  34197. +
  34198. +#include <linux/netdevice.h> /* struct device, and other headers */
  34199. +#include <linux/etherdevice.h> /* eth_type_trans */
  34200. +#include <linux/ip.h> /* struct iphdr */
  34201. +
  34202. +#include <net/tcp.h>
  34203. +#include <net/udp.h>
  34204. +#include <linux/skbuff.h>
  34205. +#include <openswan.h>
  34206. +#ifdef SPINLOCK
  34207. +# ifdef SPINLOCK_23
  34208. +# include <linux/spinlock.h> /* *lock* */
  34209. +# else /* SPINLOCK_23 */
  34210. +# include <asm/spinlock.h> /* *lock* */
  34211. +# endif /* SPINLOCK_23 */
  34212. +#endif /* SPINLOCK */
  34213. +
  34214. +#include <net/ip.h>
  34215. +
  34216. +#include "openswan/ipsec_kern24.h"
  34217. +#include "openswan/radij.h"
  34218. +#include "openswan/ipsec_encap.h"
  34219. +#include "openswan/ipsec_sa.h"
  34220. +
  34221. +#include "openswan/ipsec_radij.h"
  34222. +#include "openswan/ipsec_xform.h"
  34223. +#include "openswan/ipsec_tunnel.h"
  34224. +#include "openswan/ipsec_rcv.h"
  34225. +
  34226. +#include "openswan/ipsec_auth.h"
  34227. +
  34228. +#include "openswan/ipsec_esp.h"
  34229. +
  34230. +#ifdef CONFIG_KLIPS_AH
  34231. +#include "openswan/ipsec_ah.h"
  34232. +#endif /* CONFIG_KLIPS_AH */
  34233. +
  34234. +#ifdef CONFIG_KLIPS_IPCOMP
  34235. +#include "openswan/ipsec_ipcomp.h"
  34236. +#endif /* CONFIG_KLIPS_COMP */
  34237. +
  34238. +#include <pfkeyv2.h>
  34239. +#include <pfkey.h>
  34240. +
  34241. +#include "openswan/ipsec_proto.h"
  34242. +#include "openswan/ipsec_alg.h"
  34243. +#include "openswan/ipsec_kern24.h"
  34244. +
  34245. +#ifdef CONFIG_KLIPS_DEBUG
  34246. +int debug_rcv = 0;
  34247. +#endif /* CONFIG_KLIPS_DEBUG */
  34248. +
  34249. +int sysctl_ipsec_inbound_policy_check = 1;
  34250. +
  34251. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34252. +#include <linux/udp.h>
  34253. +#endif
  34254. +
  34255. +/* This is a private use protocol, and AT&T should be ashamed. They should have
  34256. + * used protocol # 59, which is "no next header" instead of 0xFE.
  34257. + */
  34258. +#ifndef IPPROTO_ATT_HEARTBEAT
  34259. +#define IPPROTO_ATT_HEARTBEAT 0xFE
  34260. +#endif
  34261. +
  34262. +/*
  34263. + * Check-replay-window routine, adapted from the original
  34264. + * by J. Hughes, from draft-ietf-ipsec-esp-des-md5-03.txt
  34265. + *
  34266. + * This is a routine that implements a 64 packet window. This is intend-
  34267. + * ed on being an implementation sample.
  34268. + */
  34269. +
  34270. +DEBUG_NO_STATIC int
  34271. +ipsec_checkreplaywindow(struct ipsec_sa*ipsp, __u32 seq)
  34272. +{
  34273. + __u32 diff;
  34274. +
  34275. + if (ipsp->ips_replaywin == 0) /* replay shut off */
  34276. + return 1;
  34277. + if (seq == 0)
  34278. + return 0; /* first == 0 or wrapped */
  34279. +
  34280. + /* new larger sequence number */
  34281. + if (seq > ipsp->ips_replaywin_lastseq) {
  34282. + return 1; /* larger is good */
  34283. + }
  34284. + diff = ipsp->ips_replaywin_lastseq - seq;
  34285. +
  34286. + /* too old or wrapped */ /* if wrapped, kill off SA? */
  34287. + if (diff >= ipsp->ips_replaywin) {
  34288. + return 0;
  34289. + }
  34290. + /* this packet already seen */
  34291. + if (ipsp->ips_replaywin_bitmap & (1 << diff))
  34292. + return 0;
  34293. + return 1; /* out of order but good */
  34294. +}
  34295. +
  34296. +DEBUG_NO_STATIC int
  34297. +ipsec_updatereplaywindow(struct ipsec_sa*ipsp, __u32 seq)
  34298. +{
  34299. + __u32 diff;
  34300. +
  34301. + if (ipsp->ips_replaywin == 0) /* replay shut off */
  34302. + return 1;
  34303. + if (seq == 0)
  34304. + return 0; /* first == 0 or wrapped */
  34305. +
  34306. + /* new larger sequence number */
  34307. + if (seq > ipsp->ips_replaywin_lastseq) {
  34308. + diff = seq - ipsp->ips_replaywin_lastseq;
  34309. +
  34310. + /* In win, set bit for this pkt */
  34311. + if (diff < ipsp->ips_replaywin)
  34312. + ipsp->ips_replaywin_bitmap =
  34313. + (ipsp->ips_replaywin_bitmap << diff) | 1;
  34314. + else
  34315. + /* This packet has way larger seq num */
  34316. + ipsp->ips_replaywin_bitmap = 1;
  34317. +
  34318. + if(seq - ipsp->ips_replaywin_lastseq - 1 > ipsp->ips_replaywin_maxdiff) {
  34319. + ipsp->ips_replaywin_maxdiff = seq - ipsp->ips_replaywin_lastseq - 1;
  34320. + }
  34321. + ipsp->ips_replaywin_lastseq = seq;
  34322. + return 1; /* larger is good */
  34323. + }
  34324. + diff = ipsp->ips_replaywin_lastseq - seq;
  34325. +
  34326. + /* too old or wrapped */ /* if wrapped, kill off SA? */
  34327. + if (diff >= ipsp->ips_replaywin) {
  34328. +/*
  34329. + if(seq < 0.25*max && ipsp->ips_replaywin_lastseq > 0.75*max) {
  34330. + ipsec_sa_delchain(ipsp);
  34331. + }
  34332. +*/
  34333. + return 0;
  34334. + }
  34335. + /* this packet already seen */
  34336. + if (ipsp->ips_replaywin_bitmap & (1 << diff))
  34337. + return 0;
  34338. + ipsp->ips_replaywin_bitmap |= (1 << diff); /* mark as seen */
  34339. + return 1; /* out of order but good */
  34340. +}
  34341. +
  34342. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  34343. +struct auth_alg ipsec_rcv_md5[]={
  34344. + {osMD5Init, osMD5Update, osMD5Final, AHMD596_ALEN}
  34345. +};
  34346. +
  34347. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34348. +
  34349. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  34350. +struct auth_alg ipsec_rcv_sha1[]={
  34351. + {SHA1Init, SHA1Update, SHA1Final, AHSHA196_ALEN}
  34352. +};
  34353. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34354. +
  34355. +/*
  34356. + * decapsulate a single layer of the system
  34357. + *
  34358. + * the following things should be setup to enter this function.
  34359. + *
  34360. + * irs->stats == stats structure (or NULL)
  34361. + * irs->ipp = IP header.
  34362. + * irs->len = total length of packet
  34363. + * skb->nh.iph = ipp;
  34364. + * skb->h.raw = start of payload
  34365. + * irs->ipsp = NULL.
  34366. + * irs->iphlen = N/A = is recalculated.
  34367. + * irs->ilen = 0;
  34368. + * irs->authlen = 0;
  34369. + * irs->authfuncs = NULL;
  34370. + * irs->skb = the skb;
  34371. + *
  34372. + * proto_funcs should be from ipsec_esp.c, ipsec_ah.c or ipsec_ipcomp.c.
  34373. + *
  34374. + */
  34375. +enum ipsec_rcv_value
  34376. +ipsec_rcv_decap_once(struct ipsec_rcv_state *irs
  34377. + , struct xform_functions *proto_funcs)
  34378. +{
  34379. + int iphlen;
  34380. + __u8 proto;
  34381. + struct in_addr ipsaddr;
  34382. + struct in_addr ipdaddr;
  34383. + int replay = 0; /* replay value in AH or ESP packet */
  34384. + struct ipsec_sa* ipsnext = NULL; /* next SA towards inside of packet */
  34385. + struct ipsec_sa *newipsp;
  34386. + struct iphdr *ipp;
  34387. + struct sk_buff *skb;
  34388. + struct ipsec_alg_auth *ixt_a=NULL;
  34389. +
  34390. + skb = irs->skb;
  34391. + irs->len = skb->len;
  34392. + ipp = irs->ipp;
  34393. + proto = ipp->protocol;
  34394. + ipsaddr.s_addr = ipp->saddr;
  34395. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  34396. + ipdaddr.s_addr = ipp->daddr;
  34397. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  34398. +
  34399. + iphlen = ipp->ihl << 2;
  34400. + irs->iphlen=iphlen;
  34401. + ipp->check = 0; /* we know the sum is good */
  34402. +
  34403. + KLIPS_PRINT(debug_rcv,
  34404. + "klips_debug:ipsec_rcv_decap_once: "
  34405. + "decap (%d) from %s -> %s\n",
  34406. + proto, irs->ipsaddr_txt, irs->ipdaddr_txt);
  34407. +
  34408. + /*
  34409. + * Find tunnel control block and (indirectly) call the
  34410. + * appropriate tranform routine. The resulting sk_buf
  34411. + * is a valid IP packet ready to go through input processing.
  34412. + */
  34413. +
  34414. + irs->said.dst.u.v4.sin_addr.s_addr = ipp->daddr;
  34415. + irs->said.dst.u.v4.sin_family = AF_INET;
  34416. +
  34417. + /* note: rcv_checks set up the said.spi value, if appropriate */
  34418. + if(proto_funcs->rcv_checks) {
  34419. + enum ipsec_rcv_value retval =
  34420. + (*proto_funcs->rcv_checks)(irs, skb);
  34421. +
  34422. + if(retval < 0) {
  34423. + return retval;
  34424. + }
  34425. + }
  34426. +
  34427. + irs->said.proto = proto;
  34428. + irs->sa_len = satot(&irs->said, 0, irs->sa, sizeof(irs->sa));
  34429. + if(irs->sa_len == 0) {
  34430. + strcpy(irs->sa, "(error)");
  34431. + }
  34432. +
  34433. + newipsp = ipsec_sa_getbyid(&irs->said);
  34434. + if (newipsp == NULL) {
  34435. + KLIPS_PRINT(debug_rcv,
  34436. + "klips_debug:ipsec_rcv: "
  34437. + "no ipsec_sa for SA:%s: incoming packet with no SA dropped\n",
  34438. + irs->sa_len ? irs->sa : " (error)");
  34439. + if(irs->stats) {
  34440. + irs->stats->rx_dropped++;
  34441. + }
  34442. + return IPSEC_RCV_SAIDNOTFOUND;
  34443. + }
  34444. +
  34445. + /* MCR - XXX this is bizarre. ipsec_sa_getbyid returned it, having
  34446. + * incremented the refcount, why in the world would we decrement it
  34447. + * here? */
  34448. + /* ipsec_sa_put(irs->ipsp);*/ /* incomplete */
  34449. +
  34450. + /* If it is in larval state, drop the packet, we cannot process yet. */
  34451. + if(newipsp->ips_state == SADB_SASTATE_LARVAL) {
  34452. + KLIPS_PRINT(debug_rcv,
  34453. + "klips_debug:ipsec_rcv: "
  34454. + "ipsec_sa in larval state, cannot be used yet, dropping packet.\n");
  34455. + if(irs->stats) {
  34456. + irs->stats->rx_dropped++;
  34457. + }
  34458. + ipsec_sa_put(newipsp);
  34459. + return IPSEC_RCV_SAIDNOTLIVE;
  34460. + }
  34461. +
  34462. + if(newipsp->ips_state == SADB_SASTATE_DEAD) {
  34463. + KLIPS_PRINT(debug_rcv,
  34464. + "klips_debug:ipsec_rcv: "
  34465. + "ipsec_sa in dead state, cannot be used any more, dropping packet.\n");
  34466. + if(irs->stats) {
  34467. + irs->stats->rx_dropped++;
  34468. + }
  34469. + ipsec_sa_put(newipsp);
  34470. + return IPSEC_RCV_SAIDNOTLIVE;
  34471. + }
  34472. +
  34473. + if(sysctl_ipsec_inbound_policy_check) {
  34474. + if(irs->ipp->saddr != ((struct sockaddr_in*)(newipsp->ips_addr_s))->sin_addr.s_addr) {
  34475. + KLIPS_PRINT(debug_rcv,
  34476. + "klips_debug:ipsec_rcv: "
  34477. + "SA:%s, src=%s of pkt does not agree with expected SA source address policy.\n",
  34478. + irs->sa_len ? irs->sa : " (error)",
  34479. + irs->ipsaddr_txt);
  34480. + if(irs->stats) {
  34481. + irs->stats->rx_dropped++;
  34482. + }
  34483. + ipsec_sa_put(newipsp);
  34484. + return IPSEC_RCV_FAILEDINBOUND;
  34485. + }
  34486. +
  34487. + KLIPS_PRINT(debug_rcv,
  34488. + "klips_debug:ipsec_rcv: "
  34489. + "SA:%s, src=%s of pkt agrees with expected SA source address policy.\n",
  34490. + irs->sa_len ? irs->sa : " (error)",
  34491. + irs->ipsaddr_txt);
  34492. +
  34493. + /*
  34494. + * at this point, we have looked up a new SA, and we want to make sure that if this
  34495. + * isn't the first SA in the list, that the previous SA actually points at this one.
  34496. + */
  34497. + if(irs->ipsp) {
  34498. + if(irs->ipsp->ips_inext != newipsp) {
  34499. + KLIPS_PRINT(debug_rcv,
  34500. + "klips_debug:ipsec_rcv: "
  34501. + "unexpected SA:%s: does not agree with ips->inext policy, dropped\n",
  34502. + irs->sa_len ? irs->sa : " (error)");
  34503. + if(irs->stats) {
  34504. + irs->stats->rx_dropped++;
  34505. + }
  34506. + ipsec_sa_put(newipsp);
  34507. + return IPSEC_RCV_FAILEDINBOUND;
  34508. + }
  34509. + KLIPS_PRINT(debug_rcv,
  34510. + "klips_debug:ipsec_rcv: "
  34511. + "SA:%s grouping from previous SA is OK.\n",
  34512. + irs->sa_len ? irs->sa : " (error)");
  34513. + } else {
  34514. + KLIPS_PRINT(debug_rcv,
  34515. + "klips_debug:ipsec_rcv: "
  34516. + "SA:%s First SA in group.\n",
  34517. + irs->sa_len ? irs->sa : " (error)");
  34518. + }
  34519. +
  34520. +
  34521. +
  34522. +
  34523. +
  34524. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34525. + if (proto == IPPROTO_ESP) {
  34526. + KLIPS_PRINT(debug_rcv,
  34527. + "klips_debug:ipsec_rcv: "
  34528. + "natt_type=%u tdbp->ips_natt_type=%u : %s\n",
  34529. + irs->natt_type, newipsp->ips_natt_type,
  34530. + (irs->natt_type==newipsp->ips_natt_type)?"ok":"bad");
  34531. + if (irs->natt_type != newipsp->ips_natt_type) {
  34532. + KLIPS_PRINT(debug_rcv,
  34533. + "klips_debug:ipsec_rcv: "
  34534. + "SA:%s does not agree with expected NAT-T policy.\n",
  34535. + irs->sa_len ? irs->sa : " (error)");
  34536. + if(irs->stats) {
  34537. + irs->stats->rx_dropped++;
  34538. + }
  34539. + ipsec_sa_put(newipsp);
  34540. + return IPSEC_RCV_FAILEDINBOUND;
  34541. + }
  34542. + }
  34543. +#endif
  34544. + }
  34545. +
  34546. + /* okay, SA checks out, so free any previous SA, and record a new one*/
  34547. +
  34548. + if(irs->ipsp) {
  34549. + ipsec_sa_put(irs->ipsp);
  34550. + }
  34551. + irs->ipsp=newipsp;
  34552. +
  34553. + /* note that the outer code will free the irs->ipsp
  34554. + if there is an error */
  34555. +
  34556. +
  34557. + /* now check the lifetimes */
  34558. + if(ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_bytes, "bytes",
  34559. + irs->sa, ipsec_life_countbased, ipsec_incoming,
  34560. + irs->ipsp) == ipsec_life_harddied ||
  34561. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_addtime, "addtime",
  34562. + irs->sa, ipsec_life_timebased, ipsec_incoming,
  34563. + irs->ipsp) == ipsec_life_harddied ||
  34564. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_addtime, "usetime",
  34565. + irs->sa, ipsec_life_timebased, ipsec_incoming,
  34566. + irs->ipsp) == ipsec_life_harddied ||
  34567. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_packets, "packets",
  34568. + irs->sa, ipsec_life_countbased, ipsec_incoming,
  34569. + irs->ipsp) == ipsec_life_harddied) {
  34570. + ipsec_sa_delchain(irs->ipsp);
  34571. + if(irs->stats) {
  34572. + irs->stats->rx_dropped++;
  34573. + }
  34574. +
  34575. + KLIPS_PRINT(debug_rcv,
  34576. + "klips_debug:ipsec_rcv_decap_once: "
  34577. + "decap (%d) failed lifetime check\n",
  34578. + proto);
  34579. +
  34580. + return IPSEC_RCV_LIFETIMEFAILED;
  34581. + }
  34582. +
  34583. +#if 0
  34584. + /*
  34585. + * This is removed for some reasons:
  34586. + * 1) it needs to happen *after* authentication.
  34587. + * 2) do we really care, if it authenticates, if it came
  34588. + * from the wrong location?
  34589. + * 3) the NAT_KA messages in IKE will also get to pluto
  34590. + * and it will figure out that stuff has moved.
  34591. + * 4) the 2.6 udp-esp encap function does not pass us
  34592. + * the originating port number, and I can't tell
  34593. + * if skb->sk is guaranteed to be valid here.
  34594. + * 2005-04-16: mcr@xelerance.com
  34595. + */
  34596. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34597. + /*
  34598. + *
  34599. + * XXX we should ONLY update pluto if the SA passes all checks,
  34600. + * which we clearly do not now.
  34601. + */
  34602. + if ((irs->natt_type) &&
  34603. + ( (irs->ipp->saddr != (((struct sockaddr_in*)(newipsp->ips_addr_s))->sin_addr.s_addr)) ||
  34604. + (irs->natt_sport != newipsp->ips_natt_sport)
  34605. + )) {
  34606. + struct sockaddr sipaddr;
  34607. + struct sockaddr_in *psin = (struct sockaddr_in*)(newipsp->ips_addr_s);
  34608. +
  34609. + /** Advertise NAT-T addr change to pluto **/
  34610. + sipaddr.sa_family = AF_INET;
  34611. + ((struct sockaddr_in*)&sipaddr)->sin_addr.s_addr = irs->ipp->saddr;
  34612. + ((struct sockaddr_in*)&sipaddr)->sin_port = htons(irs->natt_sport);
  34613. + pfkey_nat_t_new_mapping(newipsp, &sipaddr, irs->natt_sport);
  34614. +
  34615. + /**
  34616. + * Then allow or block packet depending on
  34617. + * sysctl_ipsec_inbound_policy_check.
  34618. + *
  34619. + * In all cases, pluto will update SA if new mapping is
  34620. + * accepted.
  34621. + */
  34622. + if (sysctl_ipsec_inbound_policy_check) {
  34623. + KLIPS_PRINT(debug_rcv,
  34624. + "klips_debug:ipsec_rcv: "
  34625. + "SA:%s, src=%s:%u of pkt does not agree with expected "
  34626. + "SA source address [%08x:%u] (notifying pluto of change).\n",
  34627. + irs->sa_len ? irs->sa : " (error)",
  34628. + irs->ipsaddr_txt, irs->natt_sport,
  34629. + psin->sin_addr.s_addr,
  34630. + newipsp->ips_natt_sport);
  34631. + if(irs->stats) {
  34632. + irs->stats->rx_dropped++;
  34633. + }
  34634. + ipsec_sa_put(newipsp);
  34635. + return IPSEC_RCV_FAILEDINBOUND;
  34636. + }
  34637. + }
  34638. +#endif
  34639. +#endif
  34640. +
  34641. + irs->authfuncs=NULL;
  34642. +
  34643. + /* authenticate, if required */
  34644. + if ((ixt_a=irs->ipsp->ips_alg_auth)) {
  34645. + irs->authlen = AHHMAC_HASHLEN;
  34646. + irs->authfuncs = NULL;
  34647. + irs->ictx = NULL;
  34648. + irs->octx = NULL;
  34649. + irs->ictx_len = 0;
  34650. + irs->octx_len = 0;
  34651. + KLIPS_PRINT(debug_rcv,
  34652. + "klips_debug:ipsec_rcv: "
  34653. + "authalg=%d authlen=%d\n",
  34654. + irs->ipsp->ips_authalg,
  34655. + irs->authlen);
  34656. + } else
  34657. + switch(irs->ipsp->ips_authalg) {
  34658. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  34659. + case AH_MD5:
  34660. + irs->authlen = AHHMAC_HASHLEN;
  34661. + irs->authfuncs = ipsec_rcv_md5;
  34662. + irs->ictx = (void *)&((struct md5_ctx*)(irs->ipsp->ips_key_a))->ictx;
  34663. + irs->octx = (void *)&((struct md5_ctx*)(irs->ipsp->ips_key_a))->octx;
  34664. + irs->ictx_len = sizeof(((struct md5_ctx*)(irs->ipsp->ips_key_a))->ictx);
  34665. + irs->octx_len = sizeof(((struct md5_ctx*)(irs->ipsp->ips_key_a))->octx);
  34666. + break;
  34667. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34668. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  34669. + case AH_SHA:
  34670. + irs->authlen = AHHMAC_HASHLEN;
  34671. + irs->authfuncs = ipsec_rcv_sha1;
  34672. + irs->ictx = (void *)&((struct sha1_ctx*)(irs->ipsp->ips_key_a))->ictx;
  34673. + irs->octx = (void *)&((struct sha1_ctx*)(irs->ipsp->ips_key_a))->octx;
  34674. + irs->ictx_len = sizeof(((struct sha1_ctx*)(irs->ipsp->ips_key_a))->ictx);
  34675. + irs->octx_len = sizeof(((struct sha1_ctx*)(irs->ipsp->ips_key_a))->octx);
  34676. + break;
  34677. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  34678. + case AH_NONE:
  34679. + irs->authlen = 0;
  34680. + irs->authfuncs = NULL;
  34681. + irs->ictx = NULL;
  34682. + irs->octx = NULL;
  34683. + irs->ictx_len = 0;
  34684. + irs->octx_len = 0;
  34685. + break;
  34686. + default:
  34687. + irs->ipsp->ips_errs.ips_alg_errs += 1;
  34688. + if(irs->stats) {
  34689. + irs->stats->rx_errors++;
  34690. + }
  34691. + return IPSEC_RCV_BADAUTH;
  34692. + }
  34693. +
  34694. + /* ilen counts number of bytes in ESP portion */
  34695. + irs->ilen = ((skb->data + skb->len) - skb->h.raw) - irs->authlen;
  34696. + if(irs->ilen <= 0) {
  34697. + KLIPS_PRINT(debug_rcv,
  34698. + "klips_debug:ipsec_rcv: "
  34699. + "runt %s packet with no data, dropping.\n",
  34700. + (proto == IPPROTO_ESP ? "esp" : "ah"));
  34701. + if(irs->stats) {
  34702. + irs->stats->rx_dropped++;
  34703. + }
  34704. + return IPSEC_RCV_BADLEN;
  34705. + }
  34706. +
  34707. + if(irs->authfuncs || ixt_a) {
  34708. + unsigned char *authenticator = NULL;
  34709. +
  34710. + if(proto_funcs->rcv_setup_auth) {
  34711. + enum ipsec_rcv_value retval
  34712. + = (*proto_funcs->rcv_setup_auth)(irs, skb,
  34713. + &replay,
  34714. + &authenticator);
  34715. + if(retval < 0) {
  34716. + return retval;
  34717. + }
  34718. + }
  34719. +
  34720. + if(!authenticator) {
  34721. + irs->ipsp->ips_errs.ips_auth_errs += 1;
  34722. + if(irs->stats) {
  34723. + irs->stats->rx_dropped++;
  34724. + }
  34725. + return IPSEC_RCV_BADAUTH;
  34726. + }
  34727. +
  34728. + if(!ipsec_checkreplaywindow(irs->ipsp, replay)) {
  34729. + irs->ipsp->ips_errs.ips_replaywin_errs += 1;
  34730. + KLIPS_PRINT(debug_rcv & DB_RX_REPLAY,
  34731. + "klips_debug:ipsec_rcv: "
  34732. + "duplicate frame from %s, packet dropped\n",
  34733. + irs->ipsaddr_txt);
  34734. + if(irs->stats) {
  34735. + irs->stats->rx_dropped++;
  34736. + }
  34737. + return IPSEC_RCV_REPLAYFAILED;
  34738. + }
  34739. +
  34740. + /*
  34741. + * verify authenticator
  34742. + */
  34743. +
  34744. + KLIPS_PRINT(debug_rcv,
  34745. + "klips_debug:ipsec_rcv: "
  34746. + "encalg = %d, authalg = %d.\n",
  34747. + irs->ipsp->ips_encalg,
  34748. + irs->ipsp->ips_authalg);
  34749. +
  34750. + /* calculate authenticator */
  34751. + if(proto_funcs->rcv_calc_auth == NULL) {
  34752. + return IPSEC_RCV_BADAUTH;
  34753. + }
  34754. + (*proto_funcs->rcv_calc_auth)(irs, skb);
  34755. +
  34756. + if (memcmp(irs->hash, authenticator, irs->authlen)) {
  34757. + irs->ipsp->ips_errs.ips_auth_errs += 1;
  34758. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  34759. + "klips_debug:ipsec_rcv: "
  34760. + "auth failed on incoming packet from %s: hash=%08x%08x%08x auth=%08x%08x%08x, dropped\n",
  34761. + irs->ipsaddr_txt,
  34762. + ntohl(*(__u32*)&irs->hash[0]),
  34763. + ntohl(*(__u32*)&irs->hash[4]),
  34764. + ntohl(*(__u32*)&irs->hash[8]),
  34765. + ntohl(*(__u32*)authenticator),
  34766. + ntohl(*((__u32*)authenticator + 1)),
  34767. + ntohl(*((__u32*)authenticator + 2)));
  34768. + if(irs->stats) {
  34769. + irs->stats->rx_dropped++;
  34770. + }
  34771. + return IPSEC_RCV_AUTHFAILED;
  34772. + } else {
  34773. + KLIPS_PRINT(debug_rcv,
  34774. + "klips_debug:ipsec_rcv: "
  34775. + "authentication successful.\n");
  34776. + }
  34777. +
  34778. + /* Crypto hygiene: clear memory used to calculate autheticator.
  34779. + * The length varies with the algorithm.
  34780. + */
  34781. + memset(irs->hash, 0, irs->authlen);
  34782. +
  34783. + /* If the sequence number == 0, expire SA, it had rolled */
  34784. + if(irs->ipsp->ips_replaywin && !replay /* !irs->ipsp->ips_replaywin_lastseq */) {
  34785. + ipsec_sa_delchain(irs->ipsp);
  34786. + KLIPS_PRINT(debug_rcv,
  34787. + "klips_debug:ipsec_rcv: "
  34788. + "replay window counter rolled, expiring SA.\n");
  34789. + if(irs->stats) {
  34790. + irs->stats->rx_dropped++;
  34791. + }
  34792. + return IPSEC_RCV_REPLAYROLLED;
  34793. + }
  34794. +
  34795. + /* now update the replay counter */
  34796. + if (!ipsec_updatereplaywindow(irs->ipsp, replay)) {
  34797. + irs->ipsp->ips_errs.ips_replaywin_errs += 1;
  34798. + KLIPS_PRINT(debug_rcv & DB_RX_REPLAY,
  34799. + "klips_debug:ipsec_rcv: "
  34800. + "duplicate frame from %s, packet dropped\n",
  34801. + irs->ipsaddr_txt);
  34802. + if(irs->stats) {
  34803. + irs->stats->rx_dropped++;
  34804. + }
  34805. + return IPSEC_RCV_REPLAYROLLED;
  34806. + }
  34807. + }
  34808. +
  34809. + if(proto_funcs->rcv_decrypt) {
  34810. + enum ipsec_rcv_value retval =
  34811. + (*proto_funcs->rcv_decrypt)(irs);
  34812. +
  34813. + if(retval != IPSEC_RCV_OK) {
  34814. + return retval;
  34815. + }
  34816. + }
  34817. +
  34818. + /*
  34819. + * Adjust pointers
  34820. + */
  34821. + skb = irs->skb;
  34822. + irs->len = skb->len;
  34823. + ipp = irs->ipp = skb->nh.iph;
  34824. + iphlen = ipp->ihl<<2;
  34825. + skb->h.raw = skb->nh.raw + iphlen;
  34826. +
  34827. + /* zero any options that there might be */
  34828. + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  34829. +
  34830. + ipsaddr.s_addr = ipp->saddr;
  34831. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  34832. + ipdaddr.s_addr = ipp->daddr;
  34833. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  34834. +
  34835. + /*
  34836. + * Discard the original ESP/AH header
  34837. + */
  34838. + ipp->protocol = irs->next_header;
  34839. +
  34840. + ipp->check = 0; /* NOTE: this will be included in checksum */
  34841. + ipp->check = ip_fast_csum((unsigned char *)skb->nh.iph, iphlen >> 2);
  34842. +
  34843. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  34844. + "klips_debug:ipsec_rcv: "
  34845. + "after <%s%s%s>, SA:%s:\n",
  34846. + IPS_XFORM_NAME(irs->ipsp),
  34847. + irs->sa_len ? irs->sa : " (error)");
  34848. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, ipp);
  34849. +
  34850. + skb->protocol = htons(ETH_P_IP);
  34851. + skb->ip_summed = 0;
  34852. +
  34853. + ipsnext = irs->ipsp->ips_inext;
  34854. + if(sysctl_ipsec_inbound_policy_check) {
  34855. + if(ipsnext) {
  34856. + if(
  34857. + ipp->protocol != IPPROTO_AH
  34858. + && ipp->protocol != IPPROTO_ESP
  34859. +#ifdef CONFIG_KLIPS_IPCOMP
  34860. + && ipp->protocol != IPPROTO_COMP
  34861. + && (ipsnext->ips_said.proto != IPPROTO_COMP
  34862. + || ipsnext->ips_inext)
  34863. +#endif /* CONFIG_KLIPS_IPCOMP */
  34864. + && ipp->protocol != IPPROTO_IPIP
  34865. + && ipp->protocol != IPPROTO_ATT_HEARTBEAT /* heartbeats to AT&T SIG/GIG */
  34866. + ) {
  34867. + KLIPS_PRINT(debug_rcv,
  34868. + "klips_debug:ipsec_rcv: "
  34869. + "packet with incomplete policy dropped, last successful SA:%s.\n",
  34870. + irs->sa_len ? irs->sa : " (error)");
  34871. + if(irs->stats) {
  34872. + irs->stats->rx_dropped++;
  34873. + }
  34874. + return IPSEC_RCV_FAILEDINBOUND;
  34875. + }
  34876. + KLIPS_PRINT(debug_rcv,
  34877. + "klips_debug:ipsec_rcv: "
  34878. + "SA:%s, Another IPSEC header to process.\n",
  34879. + irs->sa_len ? irs->sa : " (error)");
  34880. + } else {
  34881. + KLIPS_PRINT(debug_rcv,
  34882. + "klips_debug:ipsec_rcv: "
  34883. + "No ips_inext from this SA:%s.\n",
  34884. + irs->sa_len ? irs->sa : " (error)");
  34885. + }
  34886. + }
  34887. +
  34888. +#ifdef CONFIG_KLIPS_IPCOMP
  34889. + /* update ipcomp ratio counters, even if no ipcomp packet is present */
  34890. + if (ipsnext
  34891. + && ipsnext->ips_said.proto == IPPROTO_COMP
  34892. + && ipp->protocol != IPPROTO_COMP) {
  34893. + ipsnext->ips_comp_ratio_cbytes += ntohs(ipp->tot_len);
  34894. + ipsnext->ips_comp_ratio_dbytes += ntohs(ipp->tot_len);
  34895. + }
  34896. +#endif /* CONFIG_KLIPS_IPCOMP */
  34897. +
  34898. + irs->ipsp->ips_life.ipl_bytes.ipl_count += irs->len;
  34899. + irs->ipsp->ips_life.ipl_bytes.ipl_last = irs->len;
  34900. +
  34901. + if(!irs->ipsp->ips_life.ipl_usetime.ipl_count) {
  34902. + irs->ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  34903. + }
  34904. + irs->ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  34905. + irs->ipsp->ips_life.ipl_packets.ipl_count += 1;
  34906. +
  34907. +#ifdef CONFIG_NETFILTER
  34908. + if(proto == IPPROTO_ESP || proto == IPPROTO_AH) {
  34909. + skb->nfmark = (skb->nfmark & (~(IPsecSAref2NFmark(IPSEC_SA_REF_MASK))))
  34910. + | IPsecSAref2NFmark(IPsecSA2SAref(irs->ipsp));
  34911. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  34912. + "klips_debug:ipsec_rcv: "
  34913. + "%s SA sets skb->nfmark=0x%x.\n",
  34914. + proto == IPPROTO_ESP ? "ESP" : "AH",
  34915. + (unsigned)skb->nfmark);
  34916. + }
  34917. +#endif /* CONFIG_NETFILTER */
  34918. +
  34919. + return IPSEC_RCV_OK;
  34920. +}
  34921. +
  34922. +
  34923. +/*
  34924. + * core decapsulation loop for all protocols.
  34925. + *
  34926. + * the following things should be setup to enter this function.
  34927. + *
  34928. + * irs->stats == stats structure (or NULL)
  34929. + * irs->ipp = IP header.
  34930. + * irs->ipsp = NULL.
  34931. + * irs->ilen = 0;
  34932. + * irs->authlen = 0;
  34933. + * irs->authfuncs = NULL;
  34934. + * irs->skb = skb;
  34935. + * skb->nh.iph = ipp;
  34936. + * skb->h.raw = start of payload
  34937. + *
  34938. + */
  34939. +int ipsec_rcv_decap(struct ipsec_rcv_state *irs)
  34940. +{
  34941. + struct ipsec_sa *ipsp = NULL;
  34942. + struct ipsec_sa* ipsnext = NULL;
  34943. + struct in_addr ipsaddr;
  34944. + struct in_addr ipdaddr;
  34945. + struct iphdr *ipp;
  34946. + struct sk_buff *skb = NULL;
  34947. +
  34948. + /* begin decapsulating loop here */
  34949. +
  34950. + /*
  34951. + The spinlock is to prevent any other process from
  34952. + accessing or deleting the ipsec_sa hash table or any of the
  34953. + ipsec_sa s while we are using and updating them.
  34954. +
  34955. + This is not optimal, but was relatively straightforward
  34956. + at the time. A better way to do it has been planned for
  34957. + more than a year, to lock the hash table and put reference
  34958. + counts on each ipsec_sa instead. This is not likely to happen
  34959. + in KLIPS1 unless a volunteer contributes it, but will be
  34960. + designed into KLIPS2.
  34961. + */
  34962. + spin_lock(&tdb_lock);
  34963. +
  34964. + do {
  34965. + int decap_stat;
  34966. + struct xform_functions *proto_funcs;
  34967. +
  34968. + switch(irs->ipp->protocol) {
  34969. + case IPPROTO_ESP:
  34970. + proto_funcs = esp_xform_funcs;
  34971. + break;
  34972. +
  34973. +#ifdef CONFIG_KLIPS_AH
  34974. + case IPPROTO_AH:
  34975. + proto_funcs = ah_xform_funcs;
  34976. + break;
  34977. +#endif /* !CONFIG_KLIPS_AH */
  34978. +
  34979. +#ifdef CONFIG_KLIPS_IPCOMP
  34980. + case IPPROTO_COMP:
  34981. + proto_funcs = ipcomp_xform_funcs;
  34982. + break;
  34983. +#endif /* !CONFIG_KLIPS_IPCOMP */
  34984. + default:
  34985. + if(irs->stats) {
  34986. + irs->stats->rx_errors++;
  34987. + }
  34988. + decap_stat = IPSEC_RCV_BADPROTO;
  34989. + goto rcvleave;
  34990. + }
  34991. +
  34992. + decap_stat = ipsec_rcv_decap_once(irs, proto_funcs);
  34993. +
  34994. + if(decap_stat != IPSEC_RCV_OK) {
  34995. + spin_unlock(&tdb_lock);
  34996. + KLIPS_PRINT(debug_rcv,
  34997. + "klips_debug:ipsec_rcv: decap_once failed: %d\n",
  34998. + decap_stat);
  34999. +
  35000. + goto rcvleave;
  35001. + }
  35002. + /* end decapsulation loop here */
  35003. + } while( (irs->ipp->protocol == IPPROTO_ESP )
  35004. + || (irs->ipp->protocol == IPPROTO_AH )
  35005. +#ifdef CONFIG_KLIPS_IPCOMP
  35006. + || (irs->ipp->protocol == IPPROTO_COMP)
  35007. +#endif /* CONFIG_KLIPS_IPCOMP */
  35008. + );
  35009. +
  35010. + /* set up for decap loop */
  35011. + ipp =irs->ipp;
  35012. + ipsp =irs->ipsp;
  35013. + ipsnext = ipsp->ips_inext;
  35014. + skb = irs->skb;
  35015. +
  35016. + /* if there is an IPCOMP, but we don't have an IPPROTO_COMP,
  35017. + * then we can just skip it
  35018. + */
  35019. +#ifdef CONFIG_KLIPS_IPCOMP
  35020. + if(ipsnext && ipsnext->ips_said.proto == IPPROTO_COMP) {
  35021. + ipsp = ipsnext;
  35022. + ipsnext = ipsp->ips_inext;
  35023. + }
  35024. +#endif /* CONFIG_KLIPS_IPCOMP */
  35025. +
  35026. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  35027. + if ((irs->natt_type) && (ipp->protocol != IPPROTO_IPIP)) {
  35028. + /**
  35029. + * NAT-Traversal and Transport Mode:
  35030. + * we need to correct TCP/UDP checksum
  35031. + *
  35032. + * If we've got NAT-OA, we can fix checksum without recalculation.
  35033. + */
  35034. + __u32 natt_oa = ipsp->ips_natt_oa ?
  35035. + ((struct sockaddr_in*)(ipsp->ips_natt_oa))->sin_addr.s_addr : 0;
  35036. + __u16 pkt_len = skb->tail - (unsigned char *)ipp;
  35037. + __u16 data_len = pkt_len - (ipp->ihl << 2);
  35038. +
  35039. + switch (ipp->protocol) {
  35040. + case IPPROTO_TCP:
  35041. + if (data_len >= sizeof(struct tcphdr)) {
  35042. + struct tcphdr *tcp = skb->h.th;
  35043. + if (natt_oa) {
  35044. + __u32 buff[2] = { ~natt_oa, ipp->saddr };
  35045. + KLIPS_PRINT(debug_rcv,
  35046. + "klips_debug:ipsec_rcv: "
  35047. + "NAT-T & TRANSPORT: "
  35048. + "fix TCP checksum using NAT-OA\n");
  35049. + tcp->check = csum_fold(
  35050. + csum_partial((unsigned char *)buff, sizeof(buff),
  35051. + tcp->check^0xffff));
  35052. + }
  35053. + else {
  35054. + KLIPS_PRINT(debug_rcv,
  35055. + "klips_debug:ipsec_rcv: "
  35056. + "NAT-T & TRANSPORT: recalc TCP checksum\n");
  35057. + if (pkt_len > (ntohs(ipp->tot_len)))
  35058. + data_len -= (pkt_len - ntohs(ipp->tot_len));
  35059. + tcp->check = 0;
  35060. + tcp->check = csum_tcpudp_magic(ipp->saddr, ipp->daddr,
  35061. + data_len, IPPROTO_TCP,
  35062. + csum_partial((unsigned char *)tcp, data_len, 0));
  35063. + }
  35064. + }
  35065. + else {
  35066. + KLIPS_PRINT(debug_rcv,
  35067. + "klips_debug:ipsec_rcv: "
  35068. + "NAT-T & TRANSPORT: can't fix TCP checksum\n");
  35069. + }
  35070. + break;
  35071. + case IPPROTO_UDP:
  35072. + if (data_len >= sizeof(struct udphdr)) {
  35073. + struct udphdr *udp = skb->h.uh;
  35074. + if (udp->check == 0) {
  35075. + KLIPS_PRINT(debug_rcv,
  35076. + "klips_debug:ipsec_rcv: "
  35077. + "NAT-T & TRANSPORT: UDP checksum already 0\n");
  35078. + }
  35079. + else if (natt_oa) {
  35080. + __u32 buff[2] = { ~natt_oa, ipp->saddr };
  35081. + KLIPS_PRINT(debug_rcv,
  35082. + "klips_debug:ipsec_rcv: "
  35083. + "NAT-T & TRANSPORT: "
  35084. + "fix UDP checksum using NAT-OA\n");
  35085. + udp->check = csum_fold(
  35086. + csum_partial((unsigned char *)buff, sizeof(buff),
  35087. + udp->check^0xffff));
  35088. + }
  35089. + else {
  35090. + KLIPS_PRINT(debug_rcv,
  35091. + "klips_debug:ipsec_rcv: "
  35092. + "NAT-T & TRANSPORT: zero UDP checksum\n");
  35093. + udp->check = 0;
  35094. + }
  35095. + }
  35096. + else {
  35097. + KLIPS_PRINT(debug_rcv,
  35098. + "klips_debug:ipsec_rcv: "
  35099. + "NAT-T & TRANSPORT: can't fix UDP checksum\n");
  35100. + }
  35101. + break;
  35102. + default:
  35103. + KLIPS_PRINT(debug_rcv,
  35104. + "klips_debug:ipsec_rcv: "
  35105. + "NAT-T & TRANSPORT: non TCP/UDP packet -- do nothing\n");
  35106. + break;
  35107. + }
  35108. + }
  35109. +#endif
  35110. +
  35111. + /*
  35112. + * XXX this needs to be locked from when it was first looked
  35113. + * up in the decapsulation loop. Perhaps it is better to put
  35114. + * the IPIP decap inside the loop.
  35115. + */
  35116. + if(ipsnext) {
  35117. + ipsp = ipsnext;
  35118. + irs->sa_len = satot(&irs->said, 0, irs->sa, sizeof(irs->sa));
  35119. + if((ipp->protocol != IPPROTO_IPIP) &&
  35120. + (ipp->protocol != IPPROTO_ATT_HEARTBEAT)) { /* AT&T heartbeats to SIG/GIG */
  35121. + spin_unlock(&tdb_lock);
  35122. + KLIPS_PRINT(debug_rcv,
  35123. + "klips_debug:ipsec_rcv: "
  35124. + "SA:%s, Hey! How did this get through? Dropped.\n",
  35125. + irs->sa_len ? irs->sa : " (error)");
  35126. + if(irs->stats) {
  35127. + irs->stats->rx_dropped++;
  35128. + }
  35129. + goto rcvleave;
  35130. + }
  35131. + if(sysctl_ipsec_inbound_policy_check) {
  35132. + struct sockaddr_in *psin = (struct sockaddr_in*)(ipsp->ips_addr_s);
  35133. + if((ipsnext = ipsp->ips_inext)) {
  35134. + char sa2[SATOT_BUF];
  35135. + size_t sa_len2;
  35136. + sa_len2 = satot(&ipsnext->ips_said, 0, sa2, sizeof(sa2));
  35137. + spin_unlock(&tdb_lock);
  35138. + KLIPS_PRINT(debug_rcv,
  35139. + "klips_debug:ipsec_rcv: "
  35140. + "unexpected SA:%s after IPIP SA:%s\n",
  35141. + sa_len2 ? sa2 : " (error)",
  35142. + irs->sa_len ? irs->sa : " (error)");
  35143. + if(irs->stats) {
  35144. + irs->stats->rx_dropped++;
  35145. + }
  35146. + goto rcvleave;
  35147. + }
  35148. + if(ipp->saddr != psin->sin_addr.s_addr) {
  35149. + spin_unlock(&tdb_lock);
  35150. + KLIPS_PRINT(debug_rcv,
  35151. + "klips_debug:ipsec_rcv: "
  35152. + "SA:%s, src=%s(%08x) does match expected 0x%08x.\n",
  35153. + irs->sa_len ? irs->sa : " (error)",
  35154. + irs->ipsaddr_txt,
  35155. + ipp->saddr, psin->sin_addr.s_addr);
  35156. + if(irs->stats) {
  35157. + irs->stats->rx_dropped++;
  35158. + }
  35159. + goto rcvleave;
  35160. + }
  35161. + }
  35162. +
  35163. + if(ipp->protocol == IPPROTO_IPIP) /* added to support AT&T heartbeats to SIG/GIG */
  35164. + {
  35165. + /*
  35166. + * XXX this needs to be locked from when it was first looked
  35167. + * up in the decapsulation loop. Perhaps it is better to put
  35168. + * the IPIP decap inside the loop.
  35169. + */
  35170. + ipsp->ips_life.ipl_bytes.ipl_count += skb->len;
  35171. + ipsp->ips_life.ipl_bytes.ipl_last = skb->len;
  35172. +
  35173. + if(!ipsp->ips_life.ipl_usetime.ipl_count) {
  35174. + ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  35175. + }
  35176. + ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  35177. + ipsp->ips_life.ipl_packets.ipl_count += 1;
  35178. +
  35179. + if(skb->len < irs->iphlen) {
  35180. + spin_unlock(&tdb_lock);
  35181. + printk(KERN_WARNING "klips_debug:ipsec_rcv: "
  35182. + "tried to skb_pull iphlen=%d, %d available. This should never happen, please report.\n",
  35183. + irs->iphlen,
  35184. + (int)(skb->len));
  35185. +
  35186. + goto rcvleave;
  35187. + }
  35188. +
  35189. + /*
  35190. + * we need to pull up by size of IP header,
  35191. + * options, but also by any UDP/ESP encap there might
  35192. + * have been, and this deals with all cases.
  35193. + */
  35194. + skb_pull(skb, (skb->h.raw - skb->nh.raw));
  35195. +
  35196. + /* new L3 header is where L4 payload was */
  35197. + skb->nh.raw = skb->h.raw;
  35198. +
  35199. + /* now setup new L4 payload location */
  35200. + ipp = (struct iphdr *)skb->nh.raw;
  35201. + skb->h.raw = skb->nh.raw + (ipp->ihl << 2);
  35202. +
  35203. +
  35204. + /* remove any saved options that we might have,
  35205. + * since we have a new IP header.
  35206. + */
  35207. + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  35208. +
  35209. +#if 0
  35210. + KLIPS_PRINT(debug_rcv, "csum: %d\n", ip_fast_csum((u8 *)ipp, ipp->ihl));
  35211. +#endif
  35212. +
  35213. + /* re-do any strings for debugging */
  35214. + ipsaddr.s_addr = ipp->saddr;
  35215. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  35216. + ipdaddr.s_addr = ipp->daddr;
  35217. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  35218. +
  35219. + skb->protocol = htons(ETH_P_IP);
  35220. + skb->ip_summed = 0;
  35221. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35222. + "klips_debug:ipsec_rcv: "
  35223. + "IPIP tunnel stripped.\n");
  35224. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, ipp);
  35225. + }
  35226. +
  35227. + if(sysctl_ipsec_inbound_policy_check
  35228. + /*
  35229. + Note: "xor" (^) logically replaces "not equal"
  35230. + (!=) and "bitwise or" (|) logically replaces
  35231. + "boolean or" (||). This is done to speed up
  35232. + execution by doing only bitwise operations and
  35233. + no branch operations
  35234. + */
  35235. + && (((ipp->saddr & ipsp->ips_mask_s.u.v4.sin_addr.s_addr)
  35236. + ^ ipsp->ips_flow_s.u.v4.sin_addr.s_addr)
  35237. + | ((ipp->daddr & ipsp->ips_mask_d.u.v4.sin_addr.s_addr)
  35238. + ^ ipsp->ips_flow_d.u.v4.sin_addr.s_addr)) )
  35239. + {
  35240. + char sflow_txt[SUBNETTOA_BUF], dflow_txt[SUBNETTOA_BUF];
  35241. +
  35242. + subnettoa(ipsp->ips_flow_s.u.v4.sin_addr,
  35243. + ipsp->ips_mask_s.u.v4.sin_addr,
  35244. + 0, sflow_txt, sizeof(sflow_txt));
  35245. + subnettoa(ipsp->ips_flow_d.u.v4.sin_addr,
  35246. + ipsp->ips_mask_d.u.v4.sin_addr,
  35247. + 0, dflow_txt, sizeof(dflow_txt));
  35248. + spin_unlock(&tdb_lock);
  35249. + KLIPS_PRINT(debug_rcv,
  35250. + "klips_debug:ipsec_rcv: "
  35251. + "SA:%s, inner tunnel policy [%s -> %s] does not agree with pkt contents [%s -> %s].\n",
  35252. + irs->sa_len ? irs->sa : " (error)",
  35253. + sflow_txt,
  35254. + dflow_txt,
  35255. + irs->ipsaddr_txt,
  35256. + irs->ipdaddr_txt);
  35257. + if(irs->stats) {
  35258. + irs->stats->rx_dropped++;
  35259. + }
  35260. + goto rcvleave;
  35261. + }
  35262. +#ifdef CONFIG_NETFILTER
  35263. + skb->nfmark = (skb->nfmark & (~(IPsecSAref2NFmark(IPSEC_SA_REF_TABLE_MASK))))
  35264. + | IPsecSAref2NFmark(IPsecSA2SAref(ipsp));
  35265. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35266. + "klips_debug:ipsec_rcv: "
  35267. + "IPIP SA sets skb->nfmark=0x%x.\n",
  35268. + (unsigned)skb->nfmark);
  35269. +#endif /* CONFIG_NETFILTER */
  35270. + }
  35271. +
  35272. + spin_unlock(&tdb_lock);
  35273. +
  35274. + if(irs->stats) {
  35275. + irs->stats->rx_bytes += skb->len;
  35276. + }
  35277. + if(skb->dst) {
  35278. + dst_release(skb->dst);
  35279. + skb->dst = NULL;
  35280. + }
  35281. + skb->pkt_type = PACKET_HOST;
  35282. + if(irs->hard_header_len &&
  35283. + (skb->mac.raw != (skb->nh.raw - irs->hard_header_len)) &&
  35284. + (irs->hard_header_len <= skb_headroom(skb))) {
  35285. + /* copy back original MAC header */
  35286. + memmove(skb->nh.raw - irs->hard_header_len,
  35287. + skb->mac.raw, irs->hard_header_len);
  35288. + skb->mac.raw = skb->nh.raw - irs->hard_header_len;
  35289. + }
  35290. +
  35291. +#ifdef CONFIG_KLIPS_IPCOMP
  35292. + if(ipp->protocol == IPPROTO_COMP) {
  35293. + unsigned int flags = 0;
  35294. +
  35295. + if(sysctl_ipsec_inbound_policy_check) {
  35296. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35297. + "klips_debug:ipsec_rcv: "
  35298. + "inbound policy checking enabled, IPCOMP follows IPIP, dropped.\n");
  35299. + if (irs->stats) {
  35300. + irs->stats->rx_errors++;
  35301. + }
  35302. + goto rcvleave;
  35303. + }
  35304. + /*
  35305. + XXX need a ipsec_sa for updating ratio counters but it is not
  35306. + following policy anyways so it is not a priority
  35307. + */
  35308. + skb = skb_decompress(skb, NULL, &flags);
  35309. + if (!skb || flags) {
  35310. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35311. + "klips_debug:ipsec_rcv: "
  35312. + "skb_decompress() returned error flags: %d, dropped.\n",
  35313. + flags);
  35314. + if (irs->stats) {
  35315. + irs->stats->rx_errors++;
  35316. + }
  35317. + goto rcvleave;
  35318. + }
  35319. + }
  35320. +#endif /* CONFIG_KLIPS_IPCOMP */
  35321. +
  35322. + /*
  35323. + * make sure that data now starts at IP header, since we are going
  35324. + * to pass this back to ip_input (aka netif_rx). Rules for what the
  35325. + * pointers wind up a different for 2.6 vs 2.4, so we just fudge it here.
  35326. + */
  35327. +#ifdef NET_26
  35328. + skb->data = skb_push(skb, skb->h.raw - skb->nh.raw);
  35329. +#else
  35330. + skb->data = skb->nh.raw;
  35331. + {
  35332. + struct iphdr *iph = skb->nh.iph;
  35333. + int len = ntohs(iph->tot_len);
  35334. + skb->len = len;
  35335. + }
  35336. +#endif
  35337. +
  35338. +#ifdef SKB_RESET_NFCT
  35339. + nf_conntrack_put(skb->nfct);
  35340. + skb->nfct = NULL;
  35341. +#if defined(CONFIG_NETFILTER_DEBUG) && defined(HAVE_SKB_NF_DEBUG)
  35342. + skb->nf_debug = 0;
  35343. +#endif /* CONFIG_NETFILTER_DEBUG */
  35344. +#endif /* SKB_RESET_NFCT */
  35345. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35346. + "klips_debug:ipsec_rcv: "
  35347. + "netif_rx() called.\n");
  35348. + netif_rx(skb);
  35349. + skb=NULL;
  35350. +
  35351. + rcvleave:
  35352. + if(skb) {
  35353. + ipsec_kfree_skb(skb);
  35354. + }
  35355. +
  35356. + /* KLIPS_DEC_USE; Artifact from refactor? bug # 454 */
  35357. + return(0);
  35358. +}
  35359. +
  35360. +struct sk_buff *ipsec_rcv_unclone(struct sk_buff *skb,
  35361. + struct ipsec_rcv_state *irs)
  35362. +{
  35363. + /* if skb was cloned (most likely due to a packet sniffer such as
  35364. + tcpdump being momentarily attached to the interface), make
  35365. + a copy of our own to modify */
  35366. + if(skb_cloned(skb)) {
  35367. + /* include any mac header while copying.. */
  35368. + if(skb_headroom(skb) < irs->hard_header_len) {
  35369. + printk(KERN_WARNING "klips_error:ipsec_rcv: "
  35370. + "tried to skb_push hhlen=%d, %d available. This should never happen, please report.\n",
  35371. + irs->hard_header_len,
  35372. + skb_headroom(skb));
  35373. + goto rcvleave;
  35374. + }
  35375. + skb_push(skb, irs->hard_header_len);
  35376. + if
  35377. +#ifdef SKB_COW_NEW
  35378. + (skb_cow(skb, skb_headroom(skb)) != 0)
  35379. +#else /* SKB_COW_NEW */
  35380. + ((skb = skb_cow(skb, skb_headroom(skb))) == NULL)
  35381. +#endif /* SKB_COW_NEW */
  35382. + {
  35383. + goto rcvleave;
  35384. + }
  35385. + if(skb->len < irs->hard_header_len) {
  35386. + printk(KERN_WARNING "klips_error:ipsec_rcv: "
  35387. + "tried to skb_pull hhlen=%d, %d available. This should never happen, please report.\n",
  35388. + irs->hard_header_len,
  35389. + skb->len);
  35390. + goto rcvleave;
  35391. + }
  35392. + skb_pull(skb, irs->hard_header_len);
  35393. + }
  35394. + return skb;
  35395. +
  35396. +rcvleave:
  35397. + ipsec_kfree_skb(skb);
  35398. + return NULL;
  35399. +}
  35400. +
  35401. +
  35402. +#if !defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  35403. +/*
  35404. + * decapsulate a UDP encapsulated ESP packet
  35405. + */
  35406. +struct sk_buff *ipsec_rcv_natt_decap(struct sk_buff *skb
  35407. + , struct ipsec_rcv_state *irs
  35408. + , int *udp_decap_ret_p)
  35409. +{
  35410. + *udp_decap_ret_p = 0;
  35411. + if (skb->sk && skb->nh.iph && skb->nh.iph->protocol==IPPROTO_UDP) {
  35412. + /**
  35413. + * Packet comes from udp_queue_rcv_skb so it is already defrag,
  35414. + * checksum verified, ... (ie safe to use)
  35415. + *
  35416. + * If the packet is not for us, return -1 and udp_queue_rcv_skb
  35417. + * will continue to handle it (do not kfree skb !!).
  35418. + */
  35419. +
  35420. +#ifndef UDP_OPT_IN_SOCK
  35421. + struct udp_opt {
  35422. + __u32 esp_in_udp;
  35423. + };
  35424. + struct udp_opt *tp = (struct udp_opt *)&(skb->sk->tp_pinfo.af_tcp);
  35425. +#else
  35426. + struct udp_opt *tp = &(skb->sk->tp_pinfo.af_udp);
  35427. +#endif
  35428. +
  35429. + struct iphdr *ip = (struct iphdr *)skb->nh.iph;
  35430. + struct udphdr *udp = (struct udphdr *)((__u32 *)ip+ip->ihl);
  35431. + __u8 *udpdata = (__u8 *)udp + sizeof(struct udphdr);
  35432. + __u32 *udpdata32 = (__u32 *)udpdata;
  35433. +
  35434. + irs->natt_sport = ntohs(udp->source);
  35435. + irs->natt_dport = ntohs(udp->dest);
  35436. +
  35437. + KLIPS_PRINT(debug_rcv,
  35438. + "klips_debug:ipsec_rcv: "
  35439. + "suspected ESPinUDP packet (NAT-Traversal) [%d].\n",
  35440. + tp->esp_in_udp);
  35441. + KLIPS_IP_PRINT(debug_rcv, ip);
  35442. +
  35443. + if (udpdata < skb->tail) {
  35444. + unsigned int len = skb->tail - udpdata;
  35445. + if ((len==1) && (udpdata[0]==0xff)) {
  35446. + KLIPS_PRINT(debug_rcv,
  35447. + "klips_debug:ipsec_rcv: "
  35448. + /* not IPv6 compliant message */
  35449. + "NAT-keepalive from %d.%d.%d.%d.\n", NIPQUAD(ip->saddr));
  35450. + *udp_decap_ret_p = 0;
  35451. + return NULL;
  35452. + }
  35453. + else if ( (tp->esp_in_udp == ESPINUDP_WITH_NON_IKE) &&
  35454. + (len > (2*sizeof(__u32) + sizeof(struct esphdr))) &&
  35455. + (udpdata32[0]==0) && (udpdata32[1]==0) ) {
  35456. + /* ESP Packet with Non-IKE header */
  35457. + KLIPS_PRINT(debug_rcv,
  35458. + "klips_debug:ipsec_rcv: "
  35459. + "ESPinUDP pkt with Non-IKE - spi=0x%x\n",
  35460. + ntohl(udpdata32[2]));
  35461. + irs->natt_type = ESPINUDP_WITH_NON_IKE;
  35462. + irs->natt_len = sizeof(struct udphdr)+(2*sizeof(__u32));
  35463. + }
  35464. + else if ( (tp->esp_in_udp == ESPINUDP_WITH_NON_ESP) &&
  35465. + (len > sizeof(struct esphdr)) &&
  35466. + (udpdata32[0]!=0) ) {
  35467. + /* ESP Packet without Non-ESP header */
  35468. + irs->natt_type = ESPINUDP_WITH_NON_ESP;
  35469. + irs->natt_len = sizeof(struct udphdr);
  35470. + KLIPS_PRINT(debug_rcv,
  35471. + "klips_debug:ipsec_rcv: "
  35472. + "ESPinUDP pkt without Non-ESP - spi=0x%x\n",
  35473. + ntohl(udpdata32[0]));
  35474. + }
  35475. + else {
  35476. + KLIPS_PRINT(debug_rcv,
  35477. + "klips_debug:ipsec_rcv: "
  35478. + "IKE packet - not handled here\n");
  35479. + *udp_decap_ret_p = -1;
  35480. + return NULL;
  35481. + }
  35482. + }
  35483. + else {
  35484. + return NULL;
  35485. + }
  35486. + }
  35487. + return skb;
  35488. +}
  35489. +#endif
  35490. +
  35491. +
  35492. +int
  35493. +ipsec_rcv(struct sk_buff *skb
  35494. +#ifndef PROTO_HANDLER_SINGLE_PARM
  35495. + unsigned short xlen
  35496. +#endif /* PROTO_HANDLER_SINGLE_PARM */
  35497. + )
  35498. +{
  35499. +#ifdef CONFIG_KLIPS_DEBUG
  35500. + struct net_device *dev = skb->dev;
  35501. +#endif /* CONFIG_KLIPS_DEBUG */
  35502. + unsigned char protoc;
  35503. + struct net_device_stats *stats = NULL; /* This device's statistics */
  35504. + struct net_device *ipsecdev = NULL, *prvdev;
  35505. + struct ipsecpriv *prv;
  35506. + struct ipsec_rcv_state nirs, *irs = &nirs;
  35507. + struct iphdr *ipp;
  35508. + char name[9];
  35509. + int i;
  35510. +
  35511. + /* Don't unlink in the middle of a turnaround */
  35512. + KLIPS_INC_USE;
  35513. +
  35514. + memset(&nirs, 0, sizeof(struct ipsec_rcv_state));
  35515. +
  35516. + if (skb == NULL) {
  35517. + KLIPS_PRINT(debug_rcv,
  35518. + "klips_debug:ipsec_rcv: "
  35519. + "NULL skb passed in.\n");
  35520. + goto rcvleave;
  35521. + }
  35522. +
  35523. + if (skb->data == NULL) {
  35524. + KLIPS_PRINT(debug_rcv,
  35525. + "klips_debug:ipsec_rcv: "
  35526. + "NULL skb->data passed in, packet is bogus, dropping.\n");
  35527. + goto rcvleave;
  35528. + }
  35529. +
  35530. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(NET_26)
  35531. + {
  35532. + /* NET_26 NAT-T is handled by seperate function */
  35533. + struct sk_buff *nskb;
  35534. + int udp_decap_ret = 0;
  35535. +
  35536. + nskb = ipsec_rcv_natt_decap(skb, irs, &udp_decap_ret);
  35537. + if(nskb == NULL) {
  35538. + /* return with non-zero, because UDP.c code
  35539. + * need to send it upstream.
  35540. + */
  35541. + if(skb && udp_decap_ret == 0) {
  35542. + ipsec_kfree_skb(skb);
  35543. + }
  35544. + KLIPS_DEC_USE;
  35545. + return(udp_decap_ret);
  35546. + }
  35547. + skb = nskb;
  35548. + }
  35549. +#endif /* NAT_T */
  35550. +
  35551. + /* dev->hard_header_len is unreliable and should not be used */
  35552. + irs->hard_header_len = skb->mac.raw ? (skb->nh.raw - skb->mac.raw) : 0;
  35553. + if((irs->hard_header_len < 0) || (irs->hard_header_len > skb_headroom(skb)))
  35554. + irs->hard_header_len = 0;
  35555. +
  35556. + skb = ipsec_rcv_unclone(skb, irs);
  35557. + if(skb == NULL) {
  35558. + goto rcvleave;
  35559. + }
  35560. +
  35561. +#if IP_FRAGMENT_LINEARIZE
  35562. + /* In Linux 2.4.4, we may have to reassemble fragments. They are
  35563. + not assembled automatically to save TCP from having to copy
  35564. + twice.
  35565. + */
  35566. + if (skb_is_nonlinear(skb)) {
  35567. +#ifdef HAVE_NEW_SKB_LINEARIZE
  35568. + if (skb_linearize_cow(skb) != 0)
  35569. +#else
  35570. + if (skb_linearize(skb, GFP_ATOMIC) != 0)
  35571. +#endif
  35572. + {
  35573. + goto rcvleave;
  35574. + }
  35575. + }
  35576. +#endif /* IP_FRAGMENT_LINEARIZE */
  35577. +
  35578. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(NET_26)
  35579. + if (irs->natt_len) {
  35580. + /**
  35581. + * Now, we are sure packet is ESPinUDP, and we have a private
  35582. + * copy that has been linearized, remove natt_len bytes
  35583. + * from packet and modify protocol to ESP.
  35584. + */
  35585. + if (((unsigned char *)skb->data > (unsigned char *)skb->nh.iph)
  35586. + && ((unsigned char *)skb->nh.iph > (unsigned char *)skb->head))
  35587. + {
  35588. + unsigned int _len = (unsigned char *)skb->data -
  35589. + (unsigned char *)skb->nh.iph;
  35590. + KLIPS_PRINT(debug_rcv,
  35591. + "klips_debug:ipsec_rcv: adjusting skb: skb_push(%u)\n",
  35592. + _len);
  35593. + skb_push(skb, _len);
  35594. + }
  35595. + KLIPS_PRINT(debug_rcv,
  35596. + "klips_debug:ipsec_rcv: "
  35597. + "removing %d bytes from ESPinUDP packet\n", irs->natt_len);
  35598. + ipp = skb->nh.iph;
  35599. + irs->iphlen = ipp->ihl << 2;
  35600. + ipp->tot_len = htons(ntohs(ipp->tot_len) - irs->natt_len);
  35601. + if (skb->len < irs->iphlen + irs->natt_len) {
  35602. + printk(KERN_WARNING
  35603. + "klips_error:ipsec_rcv: "
  35604. + "ESPinUDP packet is too small (%d < %d+%d). "
  35605. + "This should never happen, please report.\n",
  35606. + (int)(skb->len), irs->iphlen, irs->natt_len);
  35607. + goto rcvleave;
  35608. + }
  35609. +
  35610. + /* advance payload pointer to point past the UDP header */
  35611. + skb->h.raw = skb->h.raw + irs->natt_len;
  35612. +
  35613. + /* modify protocol */
  35614. + ipp->protocol = IPPROTO_ESP;
  35615. +
  35616. + skb->sk = NULL;
  35617. +
  35618. + KLIPS_IP_PRINT(debug_rcv, skb->nh.iph);
  35619. + }
  35620. +#endif
  35621. +
  35622. + ipp = skb->nh.iph;
  35623. +
  35624. + {
  35625. + struct in_addr ipsaddr;
  35626. + struct in_addr ipdaddr;
  35627. +
  35628. + ipsaddr.s_addr = ipp->saddr;
  35629. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt
  35630. + , sizeof(irs->ipsaddr_txt));
  35631. + ipdaddr.s_addr = ipp->daddr;
  35632. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt
  35633. + , sizeof(irs->ipdaddr_txt));
  35634. + }
  35635. +
  35636. + irs->iphlen = ipp->ihl << 2;
  35637. +
  35638. + KLIPS_PRINT(debug_rcv,
  35639. + "klips_debug:ipsec_rcv: "
  35640. + "<<< Info -- ");
  35641. + KLIPS_PRINTMORE(debug_rcv && skb->dev, "skb->dev=%s ",
  35642. + skb->dev->name ? skb->dev->name : "NULL");
  35643. + KLIPS_PRINTMORE(debug_rcv && dev, "dev=%s ",
  35644. + dev->name ? dev->name : "NULL");
  35645. + KLIPS_PRINTMORE(debug_rcv, "\n");
  35646. +
  35647. + KLIPS_PRINT(debug_rcv && !(skb->dev && dev && (skb->dev == dev)),
  35648. + "klips_debug:ipsec_rcv: "
  35649. + "Informational -- **if this happens, find out why** skb->dev:%s is not equal to dev:%s\n",
  35650. + skb->dev ? (skb->dev->name ? skb->dev->name : "NULL") : "NULL",
  35651. + dev ? (dev->name ? dev->name : "NULL") : "NULL");
  35652. +
  35653. + protoc = ipp->protocol;
  35654. +#ifndef NET_21
  35655. + if((!protocol) || (protocol->protocol != protoc)) {
  35656. + KLIPS_PRINT(debug_rcv & DB_RX_IPSA,
  35657. + "klips_debug:ipsec_rcv: "
  35658. + "protocol arg is NULL or unequal to the packet contents, this is odd, using value in packet.\n");
  35659. + }
  35660. +#endif /* !NET_21 */
  35661. +
  35662. + if( (protoc != IPPROTO_AH) &&
  35663. +#ifdef CONFIG_KLIPS_IPCOMP_disabled_until_we_register_IPCOMP_HANDLER
  35664. + (protoc != IPPROTO_COMP) &&
  35665. +#endif /* CONFIG_KLIPS_IPCOMP */
  35666. + (protoc != IPPROTO_ESP) ) {
  35667. + KLIPS_PRINT(debug_rcv & DB_RX_IPSA,
  35668. + "klips_debug:ipsec_rcv: Why the hell is someone "
  35669. + "passing me a non-ipsec protocol = %d packet? -- dropped.\n",
  35670. + protoc);
  35671. + goto rcvleave;
  35672. + }
  35673. +
  35674. + if(skb->dev) {
  35675. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  35676. + sprintf(name, IPSEC_DEV_FORMAT, i);
  35677. + if(!strcmp(name, skb->dev->name)) {
  35678. + prv = (struct ipsecpriv *)(skb->dev->priv);
  35679. + if(prv) {
  35680. + stats = (struct net_device_stats *) &(prv->mystats);
  35681. + }
  35682. + ipsecdev = skb->dev;
  35683. + KLIPS_PRINT(debug_rcv,
  35684. + "klips_debug:ipsec_rcv: "
  35685. + "Info -- pkt already proc'ed a group of ipsec headers, processing next group of ipsec headers.\n");
  35686. + break;
  35687. + }
  35688. + if((ipsecdev = __ipsec_dev_get(name)) == NULL) {
  35689. + KLIPS_PRINT(debug_rcv,
  35690. + "klips_error:ipsec_rcv: "
  35691. + "device %s does not exist\n",
  35692. + name);
  35693. + }
  35694. + prv = ipsecdev ? (struct ipsecpriv *)(ipsecdev->priv) : NULL;
  35695. + prvdev = prv ? (struct net_device *)(prv->dev) : NULL;
  35696. +
  35697. +#if 0
  35698. + KLIPS_PRINT(debug_rcv && prvdev,
  35699. + "klips_debug:ipsec_rcv: "
  35700. + "physical device for device %s is %s\n",
  35701. + name,
  35702. + prvdev->name);
  35703. +#endif
  35704. + if(prvdev && skb->dev &&
  35705. + !strcmp(prvdev->name, skb->dev->name)) {
  35706. + stats = prv ? ((struct net_device_stats *) &(prv->mystats)) : NULL;
  35707. + skb->dev = ipsecdev;
  35708. + KLIPS_PRINT(debug_rcv && prvdev,
  35709. + "klips_debug:ipsec_rcv: "
  35710. + "assigning packet ownership to virtual device %s from physical device %s.\n",
  35711. + name, prvdev->name);
  35712. + if(stats) {
  35713. + stats->rx_packets++;
  35714. + }
  35715. + break;
  35716. + }
  35717. + }
  35718. + } else {
  35719. + KLIPS_PRINT(debug_rcv,
  35720. + "klips_debug:ipsec_rcv: "
  35721. + "device supplied with skb is NULL\n");
  35722. + }
  35723. +
  35724. + if(stats == NULL) {
  35725. + KLIPS_PRINT((debug_rcv),
  35726. + "klips_error:ipsec_rcv: "
  35727. + "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",
  35728. + skb->dev ? (skb->dev->name ? skb->dev->name : "NULL") : "NULL");
  35729. + }
  35730. +
  35731. + KLIPS_IP_PRINT(debug_rcv, ipp);
  35732. +
  35733. + /* set up for decap loop */
  35734. + irs->stats= stats;
  35735. + irs->ipp = ipp;
  35736. + irs->ipsp = NULL;
  35737. + irs->ilen = 0;
  35738. + irs->authlen=0;
  35739. + irs->authfuncs=NULL;
  35740. + irs->skb = skb;
  35741. +
  35742. + ipsec_rcv_decap(irs);
  35743. + KLIPS_DEC_USE;
  35744. + return(0);
  35745. +
  35746. + rcvleave:
  35747. + if(skb) {
  35748. + ipsec_kfree_skb(skb);
  35749. + }
  35750. + KLIPS_DEC_USE;
  35751. + return(0);
  35752. +
  35753. +}
  35754. +
  35755. +#ifdef NET_26
  35756. +/*
  35757. + * this entry point is not a protocol entry point, so the entry
  35758. + * is a bit different.
  35759. + *
  35760. + * skb->iph->tot_len has been byte-swapped, and reduced by the size of
  35761. + * the IP header (and options).
  35762. + *
  35763. + * skb->h.raw has been pulled up the ESP header.
  35764. + *
  35765. + * skb->iph->protocol = 50 IPPROTO_ESP;
  35766. + *
  35767. + */
  35768. +int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type)
  35769. +{
  35770. + struct ipsec_rcv_state nirs, *irs = &nirs;
  35771. + struct iphdr *ipp;
  35772. +
  35773. + /* Don't unlink in the middle of a turnaround */
  35774. + KLIPS_INC_USE;
  35775. +
  35776. + memset(irs, 0, sizeof(*irs));
  35777. +
  35778. + /* XXX fudge it so that all nat-t stuff comes from ipsec0 */
  35779. + /* eventually, the SA itself will determine which device
  35780. + * it comes from
  35781. + */
  35782. + {
  35783. + skb->dev = ipsec_get_device(0);
  35784. + }
  35785. +
  35786. + /* set up for decap loop */
  35787. + irs->hard_header_len = skb->dev->hard_header_len;
  35788. +
  35789. + skb = ipsec_rcv_unclone(skb, irs);
  35790. +
  35791. +#if IP_FRAGMENT_LINEARIZE
  35792. + /* In Linux 2.4.4, we may have to reassemble fragments. They are
  35793. + not assembled automatically to save TCP from having to copy
  35794. + twice.
  35795. + */
  35796. + if (skb_is_nonlinear(skb)) {
  35797. +#ifdef HAVE_NEW_SKB_LINEARIZE
  35798. + if (skb_linearize_cow(skb) != 0)
  35799. +#else
  35800. + if (skb_linearize(skb, GFP_ATOMIC) != 0)
  35801. +#endif
  35802. + {
  35803. + goto rcvleave;
  35804. + }
  35805. + }
  35806. +#endif /* IP_FRAGMENT_LINEARIZE */
  35807. +
  35808. + ipp = skb->nh.iph;
  35809. +
  35810. + {
  35811. + struct in_addr ipsaddr;
  35812. + struct in_addr ipdaddr;
  35813. +
  35814. + ipsaddr.s_addr = ipp->saddr;
  35815. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt
  35816. + , sizeof(irs->ipsaddr_txt));
  35817. + ipdaddr.s_addr = ipp->daddr;
  35818. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt
  35819. + , sizeof(irs->ipdaddr_txt));
  35820. + }
  35821. +
  35822. + irs->iphlen = ipp->ihl << 2;
  35823. +
  35824. + KLIPS_IP_PRINT(debug_rcv, ipp);
  35825. +
  35826. + irs->stats= NULL;
  35827. + irs->ipp = ipp;
  35828. + irs->ipsp = NULL;
  35829. + irs->ilen = 0;
  35830. + irs->authlen=0;
  35831. + irs->authfuncs=NULL;
  35832. + irs->skb = skb;
  35833. +
  35834. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  35835. + switch(encap_type) {
  35836. + case UDP_ENCAP_ESPINUDP:
  35837. + irs->natt_type = ESPINUDP_WITH_NON_ESP;
  35838. + break;
  35839. +
  35840. + case UDP_ENCAP_ESPINUDP_NON_IKE:
  35841. + irs->natt_type = ESPINUDP_WITH_NON_IKE;
  35842. + break;
  35843. +
  35844. + default:
  35845. + if(printk_ratelimit()) {
  35846. + printk(KERN_INFO "KLIPS received unknown UDP-ESP encap type %u\n",
  35847. + encap_type);
  35848. + }
  35849. + return -1;
  35850. + }
  35851. +
  35852. +#endif
  35853. + ipsec_rcv_decap(irs);
  35854. + KLIPS_DEC_USE;
  35855. + return 0;
  35856. +
  35857. +rcvleave:
  35858. + if(skb) {
  35859. + ipsec_kfree_skb(skb);
  35860. + }
  35861. + KLIPS_DEC_USE;
  35862. + return 0;
  35863. +}
  35864. +#endif
  35865. +
  35866. +
  35867. +/*
  35868. + * $Log: ipsec_rcv.c,v $
  35869. + * Revision 1.171.2.10 2006/10/06 21:39:26 paul
  35870. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  35871. + * set. This is defined through autoconf.h which is included through the
  35872. + * linux kernel build macros.
  35873. + *
  35874. + * Revision 1.171.2.9 2006/07/30 02:09:33 paul
  35875. + * Author: Bart Trojanowski <bart@xelerance.com>
  35876. + * This fixes a NATT+ESP bug in rcv path.
  35877. + *
  35878. + * We only want to test NATT policy on the ESP packet. Doing so on the
  35879. + * bundled SA breaks because the next layer does not know anything about
  35880. + * NATT.
  35881. + *
  35882. + * Fix just puts an if(proto == IPPROTO_ESP) around the NATT policy check.
  35883. + *
  35884. + * Revision 1.171.2.8 2006/07/29 05:03:04 paul
  35885. + * Added check for new version of skb_linearize that only takes 1 argument,
  35886. + * for 2.6.18+ kernels.
  35887. + *
  35888. + * Revision 1.171.2.7 2006/04/20 16:33:07 mcr
  35889. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  35890. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  35891. + *
  35892. + * Revision 1.171.2.6 2005/12/07 06:07:04 paul
  35893. + * comment out KLIPS_DEC_USE in ipsec_rcv_decap. Likely an artifact from
  35894. + * refactoring. http://bugs.xelerance.com/view.php?id=454
  35895. + *
  35896. + * Revision 1.171.2.5 2005/10/21 02:22:29 mcr
  35897. + * pull up of another try at 2.4.x kernel fix
  35898. + *
  35899. + * Revision 1.171.2.4 2005/10/21 01:39:56 mcr
  35900. + * nat-t fix is 2.4/2.6 specific
  35901. + *
  35902. + * Revision 1.178 2005/10/21 02:19:34 mcr
  35903. + * on 2.4 systems, we have to fix up the length as well.
  35904. + *
  35905. + * Revision 1.177 2005/10/21 00:18:31 mcr
  35906. + * nat-t fix is 2.4 specific.
  35907. + *
  35908. + * Revision 1.176 2005/10/20 21:06:11 mcr
  35909. + * possible fix for nat-t problem on 2.4 kernels.
  35910. + *
  35911. + * Revision 1.175 2005/10/13 02:49:24 mcr
  35912. + * tested UDP-encapsulated ESP packets that were not actually ESP,
  35913. + * (but IKE) were being eaten.
  35914. + *
  35915. + * Revision 1.174 2005/10/13 01:25:22 mcr
  35916. + * UDP-encapsulated ESP packets that were not actually ESP,
  35917. + * (but IKE) were being eaten.
  35918. + *
  35919. + * Revision 1.173 2005/08/31 23:26:11 mcr
  35920. + * fixes for 2.6.13
  35921. + *
  35922. + * Revision 1.172 2005/08/05 08:44:54 mcr
  35923. + * ipsec_kern24.h (compat code for 2.4) must be include
  35924. + * explicitely now.
  35925. + *
  35926. + * Revision 1.171 2005/07/08 23:56:06 ken
  35927. + * #ifdef
  35928. + *
  35929. + * Revision 1.170 2005/07/08 23:50:05 ken
  35930. + * Don't attempt to decapsulate if NAT-T isn't available in the code
  35931. + *
  35932. + * Revision 1.169 2005/06/06 00:27:31 mcr
  35933. + * fix for making tcpdump (packet capture) work correctly for
  35934. + * nat-t received packets.
  35935. + *
  35936. + * Revision 1.168 2005/06/04 16:06:06 mcr
  35937. + * better patch for nat-t rcv-device code.
  35938. + *
  35939. + * Revision 1.167 2005/06/03 17:04:46 mcr
  35940. + * nat-t packets are forced to arrive from ipsec0.
  35941. + *
  35942. + * Revision 1.166 2005/04/29 05:10:22 mcr
  35943. + * removed from extraenous includes to make unit testing easier.
  35944. + *
  35945. + * Revision 1.165 2005/04/20 17:11:32 mcr
  35946. + * fixed to compile on 2.4.
  35947. + *
  35948. + * Revision 1.164 2005/04/18 03:09:50 ken
  35949. + * Fix typo
  35950. + *
  35951. + * Revision 1.163 2005/04/17 05:32:58 mcr
  35952. + * remove extraneous debugging
  35953. + * make sure to return success from klips26_encap_rcv().
  35954. + *
  35955. + * Revision 1.162 2005/04/17 04:37:01 mcr
  35956. + * make sure that irs->ipp is still set.
  35957. + *
  35958. + * Revision 1.161 2005/04/17 03:51:52 mcr
  35959. + * removed old comment about removed code.
  35960. + * added translation from udp.c/2.6 to KLIPS NAT-ESP naming.
  35961. + * comment about check for origin address/port for incoming NAT-ESP packets.
  35962. + *
  35963. + * Revision 1.160 2005/04/15 19:55:58 mcr
  35964. + * adjustments to use proper skb fields for data.
  35965. + *
  35966. + * Revision 1.159 2005/04/10 22:58:20 mcr
  35967. + * refactoring of receive functions to make it easier to
  35968. + * call the ESP decap.
  35969. + *
  35970. + * Revision 1.158 2005/04/08 18:27:53 mcr
  35971. + * refactored ipsec_rcv() into ipsec_rcv() and ipsec_rcv_decap().
  35972. + *
  35973. + * Revision 1.157 2004/12/28 23:13:09 mcr
  35974. + * use consistent CONFIG_IPSEC_NAT_TRAVERSAL.
  35975. + *
  35976. + * Revision 1.156 2004/12/03 21:34:51 mcr
  35977. + * mistype of KLIPS_USE_COUNT -> KLIPS_INC_USE;
  35978. + *
  35979. + * Revision 1.155 2004/12/03 21:25:57 mcr
  35980. + * compile time fixes for running on 2.6.
  35981. + * still experimental.
  35982. + *
  35983. + * Revision 1.154 2004/09/08 17:21:36 ken
  35984. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  35985. + *
  35986. + * Revision 1.153 2004/08/22 20:10:00 mcr
  35987. + * removed check for incorrect setting of NET_26.
  35988. + *
  35989. + * Revision 1.152 2004/08/21 15:22:39 mcr
  35990. + * added #defines for ATT heartbeat.
  35991. + *
  35992. + * Revision 1.151 2004/08/21 02:16:32 ken
  35993. + * Patch from Jochen Eisinger for AT&T MTS Heartbeat packet support
  35994. + *
  35995. + * Revision 1.150 2004/08/21 00:44:48 mcr
  35996. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  35997. + *
  35998. + * Revision 1.149 2004/08/20 21:45:45 mcr
  35999. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  36000. + * be 26sec compatible. But, some defines where changed.
  36001. + *
  36002. + * Revision 1.148 2004/08/17 03:27:23 mcr
  36003. + * klips 2.6 edits.
  36004. + *
  36005. + * Revision 1.147 2004/08/05 23:29:27 mcr
  36006. + * fixed nesting of #ifdef vs {} in ipsec_rcv().
  36007. + *
  36008. + * Revision 1.146 2004/08/04 15:57:07 mcr
  36009. + * moved des .h files to include/des/ *
  36010. + * included 2.6 protocol specific things
  36011. + * started at NAT-T support, but it will require a kernel patch.
  36012. + *
  36013. + * Revision 1.145 2004/08/03 18:19:08 mcr
  36014. + * in 2.6, use "net_device" instead of #define device->net_device.
  36015. + * this probably breaks 2.0 compiles.
  36016. + *
  36017. + * Revision 1.144 2004/07/10 19:11:18 mcr
  36018. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  36019. + *
  36020. + * Revision 1.143 2004/05/10 22:27:00 mcr
  36021. + * fix for ESP-3DES-noauth test case.
  36022. + *
  36023. + * Revision 1.142 2004/05/10 22:25:57 mcr
  36024. + * reformat of calls to ipsec_lifetime_check().
  36025. + *
  36026. + * Revision 1.141 2004/04/06 02:49:26 mcr
  36027. + * pullup of algo code from alg-branch.
  36028. + *
  36029. + * Revision 1.140 2004/02/03 03:12:53 mcr
  36030. + * removed erroneously, double patched code.
  36031. + *
  36032. + * Revision 1.139 2004/01/05 23:21:29 mcr
  36033. + * initialize sin_family in ipsec_rcv.c
  36034. + *
  36035. + * Revision 1.138 2003/12/24 19:46:52 mcr
  36036. + * if sock.h patch has not been applied, then define appropriate
  36037. + * structure so we can use it. This is serious inferior, and
  36038. + * depends upon the concept that the structure in question is
  36039. + * smaller than the other members of that union.
  36040. + * getting rid of differing methods is a better solution.
  36041. + *
  36042. + * Revision 1.137 2003/12/22 19:40:57 mcr
  36043. + * NAT-T patches 0.6c.
  36044. + *
  36045. + * Revision 1.136 2003/12/15 18:13:12 mcr
  36046. + * when compiling with NAT traversal, don't assume that the
  36047. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  36048. + * is set.
  36049. + *
  36050. + * Revision 1.135 2003/12/13 19:10:21 mcr
  36051. + * refactored rcv and xmit code - same as FS 2.05.
  36052. + *
  36053. + * Revision 1.134.2.1 2003/12/22 15:25:52 jjo
  36054. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  36055. + *
  36056. + * Revision 1.134 2003/12/10 01:14:27 mcr
  36057. + * NAT-traversal patches to KLIPS.
  36058. + *
  36059. + * Revision 1.133 2003/10/31 02:27:55 mcr
  36060. + * pulled up port-selector patches and sa_id elimination.
  36061. + *
  36062. + * Revision 1.132.2.1 2003/10/29 01:30:41 mcr
  36063. + * elimited "struct sa_id".
  36064. + *
  36065. + * Revision 1.132 2003/09/02 19:51:48 mcr
  36066. + * fixes for PR#252.
  36067. + *
  36068. + * Revision 1.131 2003/07/31 22:47:16 mcr
  36069. + * preliminary (untested by FS-team) 2.5 patches.
  36070. + *
  36071. + * Revision 1.130 2003/04/03 17:38:25 rgb
  36072. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  36073. + * Clarified logic for non-connected devices.
  36074. + *
  36075. + * Revision 1.129 2003/02/06 02:21:34 rgb
  36076. + *
  36077. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  36078. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  36079. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  36080. + *
  36081. + * Revision 1.128 2002/12/13 20:58:03 rgb
  36082. + * Relegated MCR's recent "_dmp" routine to debug_verbose.
  36083. + * Cleaned up printing of source and destination addresses in debug output.
  36084. + *
  36085. + * Revision 1.127 2002/12/04 16:00:16 rgb
  36086. + *
  36087. + * Fixed AH decapsulation pointer update bug and added some comments and
  36088. + * debugging.
  36089. + * This bug was caught by west-ah-0[12].
  36090. + *
  36091. + * Revision 1.126 2002/11/04 05:03:43 mcr
  36092. + * fixes for IPCOMP. There were two problems:
  36093. + * 1) the irs->ipp pointer was not being updated properly after
  36094. + * the ESP descryption. The meant nothing for IPIP, as the
  36095. + * later IP header overwrote the earlier one.
  36096. + * 2) the more serious problem was that skb_decompress will
  36097. + * usually allocate a new SKB, so we have to make sure that
  36098. + * it doesn't get lost.
  36099. + * #2 meant removing the skb argument from the ->decrypt routine
  36100. + * and moving it to the irs->skb, so it could be value/result.
  36101. + *
  36102. + * Revision 1.125 2002/11/01 01:53:35 dhr
  36103. + *
  36104. + * fix typo
  36105. + *
  36106. + * Revision 1.124 2002/10/31 22:49:01 dhr
  36107. + *
  36108. + * - eliminate unused variable "hash"
  36109. + * - reduce scope of variable "authenticator"
  36110. + * - add comment on a couple of tricky bits
  36111. + *
  36112. + * Revision 1.123 2002/10/31 22:39:56 dhr
  36113. + *
  36114. + * use correct type for result of function calls
  36115. + *
  36116. + * Revision 1.122 2002/10/31 22:36:25 dhr
  36117. + *
  36118. + * simplify complex test
  36119. + *
  36120. + * Revision 1.121 2002/10/31 22:34:04 dhr
  36121. + *
  36122. + * ipsprev is never used: ditch it
  36123. + *
  36124. + * Revision 1.120 2002/10/31 22:30:21 dhr
  36125. + *
  36126. + * eliminate redundant assignments
  36127. + *
  36128. + * Revision 1.119 2002/10/31 22:27:43 dhr
  36129. + *
  36130. + * make whitespace canonical
  36131. + *
  36132. + * Revision 1.118 2002/10/30 05:47:17 rgb
  36133. + * Fixed cut-and-paste error mis-identifying comp runt as ah.
  36134. + *
  36135. + * Revision 1.117 2002/10/17 16:37:45 rgb
  36136. + * Remove compp intermediate variable and in-line its contents
  36137. + * where used
  36138. + *
  36139. + * Revision 1.116 2002/10/12 23:11:53 dhr
  36140. + *
  36141. + * [KenB + DHR] more 64-bit cleanup
  36142. + *
  36143. + * Revision 1.115 2002/10/07 19:06:58 rgb
  36144. + * Minor fixups and activation to west-rcv-nfmark-set-01 test to check for SA reference properly set on incoming.
  36145. + *
  36146. + * Revision 1.114 2002/10/07 18:31:31 rgb
  36147. + * Set saref on incoming packets.
  36148. + *
  36149. + * Revision 1.113 2002/09/16 21:28:12 mcr
  36150. + * adjust hash length for HMAC calculation - must look at whether
  36151. + * it is MD5 or SHA1.
  36152. + *
  36153. + * Revision 1.112 2002/09/16 21:19:15 mcr
  36154. + * fixes for west-ah-icmp-01 - length of AH header must be
  36155. + * calculated properly, and next_header field properly copied.
  36156. + *
  36157. + * Revision 1.111 2002/09/10 02:45:56 mcr
  36158. + * re-factored the ipsec_rcv function into several functions,
  36159. + * ipsec_rcv_decap_once, and a set of functions for AH, ESP and IPCOMP.
  36160. + * In addition, the MD5 and SHA1 functions are replaced with pointers.
  36161. + *
  36162. + * Revision 1.110 2002/08/30 06:34:33 rgb
  36163. + * Fix scope of shift in AH header length check.
  36164. + *
  36165. + * Revision 1.109 2002/08/27 16:49:20 rgb
  36166. + * Fixed ESP short packet DOS (and AH and IPCOMP).
  36167. + *
  36168. + * Revision 1.108 2002/07/24 18:44:54 rgb
  36169. + * Type fiddling to tame ia64 compiler.
  36170. + *
  36171. + * Revision 1.107 2002/05/27 18:58:18 rgb
  36172. + * Convert to dynamic ipsec device allocation.
  36173. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  36174. + *
  36175. + * Revision 1.106 2002/05/23 07:15:21 rgb
  36176. + * Pointer clean-up.
  36177. + * Added refcount code.
  36178. + *
  36179. + * Revision 1.105 2002/05/14 02:35:06 rgb
  36180. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  36181. + * ipsec_sa or ipsec_sa.
  36182. + * Change references to _TDB to _IPSA.
  36183. + *
  36184. + * Revision 1.104 2002/04/24 07:55:32 mcr
  36185. + * #include patches and Makefiles for post-reorg compilation.
  36186. + *
  36187. + * Revision 1.103 2002/04/24 07:36:30 mcr
  36188. + * Moved from ./klips/net/ipsec/ipsec_rcv.c,v
  36189. + *
  36190. + * Revision 1.102 2002/01/29 17:17:56 mcr
  36191. + * moved include of ipsec_param.h to after include of linux/kernel.h
  36192. + * otherwise, it seems that some option that is set in ipsec_param.h
  36193. + * screws up something subtle in the include path to kernel.h, and
  36194. + * it complains on the snprintf() prototype.
  36195. + *
  36196. + * Revision 1.101 2002/01/29 04:00:52 mcr
  36197. + * more excise of kversions.h header.
  36198. + *
  36199. + * Revision 1.100 2002/01/29 02:13:17 mcr
  36200. + * introduction of ipsec_kversion.h means that include of
  36201. + * ipsec_param.h must preceed any decisions about what files to
  36202. + * include to deal with differences in kernel source.
  36203. + *
  36204. + * Revision 1.99 2002/01/28 21:40:59 mcr
  36205. + * should use #if to test boolean option rather than #ifdef.
  36206. + *
  36207. + * Revision 1.98 2002/01/20 20:19:36 mcr
  36208. + * renamed option to IP_FRAGMENT_LINEARIZE.
  36209. + *
  36210. + * Revision 1.97 2002/01/12 02:55:36 mcr
  36211. + * fix for post-2.4.4 to linearize skb's when ESP packet
  36212. + * was assembled from fragments.
  36213. + *
  36214. + * Revision 1.96 2001/11/26 09:23:49 rgb
  36215. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  36216. + *
  36217. + * Revision 1.93.2.2 2001/10/22 20:54:07 mcr
  36218. + * include des.h, removed phony prototypes and fixed calling
  36219. + * conventions to match real prototypes.
  36220. + *
  36221. + * Revision 1.93.2.1 2001/09/25 02:22:22 mcr
  36222. + * struct tdb -> struct ipsec_sa.
  36223. + * lifetime checks moved to ipsec_life.c
  36224. + * some sa(tdb) manipulation functions renamed.
  36225. + *
  36226. + * Revision 1.95 2001/11/06 19:49:07 rgb
  36227. + * Added variable descriptions.
  36228. + * Removed unauthenticated sequence==0 check to prevent DoS.
  36229. + *
  36230. + * Revision 1.94 2001/10/18 04:45:20 rgb
  36231. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  36232. + * lib/freeswan.h version macros moved to lib/kversions.h.
  36233. + * Other compiler directive cleanups.
  36234. + *
  36235. + * Revision 1.93 2001/09/07 22:17:24 rgb
  36236. + * Fix for removal of transport layer protocol handler arg in 2.4.4.
  36237. + * Fix to accomodate peer non-conformance to IPCOMP rfc2393.
  36238. + *
  36239. + * Revision 1.92 2001/08/27 19:44:41 rgb
  36240. + * Fix error in comment.
  36241. + *
  36242. + * Revision 1.91 2001/07/20 19:31:48 dhr
  36243. + * [DHR] fix source and destination subnets of policy in diagnostic
  36244. + *
  36245. + * Revision 1.90 2001/07/06 19:51:09 rgb
  36246. + * Added inbound policy checking code for IPIP SAs.
  36247. + * Renamed unused function argument for ease and intuitive naming.
  36248. + *
  36249. + * Revision 1.89 2001/06/22 19:35:23 rgb
  36250. + * Disable ipcomp processing if we are handed a ipcomp packet with no esp
  36251. + * or ah header.
  36252. + * Print protocol if we are handed a non-ipsec packet.
  36253. + *
  36254. + * Revision 1.88 2001/06/20 06:30:47 rgb
  36255. + * Fixed transport mode IPCOMP policy check bug.
  36256. + *
  36257. + * Revision 1.87 2001/06/13 20:58:40 rgb
  36258. + * Added parentheses around assignment used as truth value to silence
  36259. + * compiler.
  36260. + *
  36261. + * Revision 1.86 2001/06/07 22:25:23 rgb
  36262. + * Added a source address policy check for tunnel mode. It still does
  36263. + * not check client addresses and masks.
  36264. + * Only decapsulate IPIP if it is expected.
  36265. + *
  36266. + * Revision 1.85 2001/05/30 08:14:02 rgb
  36267. + * Removed vestiges of esp-null transforms.
  36268. + *
  36269. + * Revision 1.84 2001/05/27 06:12:11 rgb
  36270. + * Added structures for pid, packet count and last access time to eroute.
  36271. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  36272. + *
  36273. + * Revision 1.83 2001/05/04 16:45:47 rgb
  36274. + * Remove unneeded code. ipp is not used after this point.
  36275. + *
  36276. + * Revision 1.82 2001/05/04 16:36:00 rgb
  36277. + * Fix skb_cow() call for 2.4.4. (SS)
  36278. + *
  36279. + * Revision 1.81 2001/05/02 14:46:53 rgb
  36280. + * Fix typo for compiler directive to pull IPH back.
  36281. + *
  36282. + * Revision 1.80 2001/04/30 19:46:34 rgb
  36283. + * Update for 2.4.4. We now receive the skb with skb->data pointing to
  36284. + * h.raw.
  36285. + *
  36286. + * Revision 1.79 2001/04/23 15:01:15 rgb
  36287. + * Added spin_lock() check to prevent double-locking for multiple
  36288. + * transforms and hence kernel lock-ups with SMP kernels.
  36289. + * Minor spin_unlock() adjustments to unlock before non-dependant prints
  36290. + * and IPSEC device stats updates.
  36291. + *
  36292. + * Revision 1.78 2001/04/21 23:04:24 rgb
  36293. + * Check if soft expire has already been sent before sending another to
  36294. + * prevent ACQUIRE flooding.
  36295. + *
  36296. + * Revision 1.77 2001/03/16 07:35:20 rgb
  36297. + * Ditch extra #if 1 around now permanent policy checking code.
  36298. + *
  36299. + * Revision 1.76 2001/02/27 22:24:54 rgb
  36300. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  36301. + * Check for satoa() return codes.
  36302. + *
  36303. + * Revision 1.75 2001/02/19 22:28:30 rgb
  36304. + * Minor change to virtual device discovery code to assert which I/F has
  36305. + * been found.
  36306. + *
  36307. + * Revision 1.74 2000/11/25 03:50:36 rgb
  36308. + * Oops fix by minor re-arrangement of code to avoid accessing a freed tdb.
  36309. + *
  36310. + * Revision 1.73 2000/11/09 20:52:15 rgb
  36311. + * More spinlock shuffling, locking earlier and unlocking later in rcv to
  36312. + * include ipcomp and prevent races, renaming some tdb variables that got
  36313. + * forgotten, moving some unlocks to include tdbs and adding a missing
  36314. + * unlock. Thanks to Svenning for some of these.
  36315. + *
  36316. + * Revision 1.72 2000/11/09 20:11:22 rgb
  36317. + * Minor shuffles to fix non-standard kernel config option selection.
  36318. + *
  36319. + * Revision 1.71 2000/11/06 04:36:18 rgb
  36320. + * Ditched spin_lock_irqsave in favour of spin_lock.
  36321. + * Minor initial protocol check rewrite.
  36322. + * Clean up debug printing.
  36323. + * Clean up tdb handling on ipcomp.
  36324. + * Fixed transport mode null pointer de-reference without ipcomp.
  36325. + * Add Svenning's adaptive content compression.
  36326. + * Disabled registration of ipcomp handler.
  36327. + *
  36328. + * Revision 1.70 2000/10/30 23:41:43 henry
  36329. + * Hans-Joerg Hoexer's null-pointer fix
  36330. + *
  36331. + * Revision 1.69 2000/10/10 18:54:16 rgb
  36332. + * Added a fix for incoming policy check with ipcomp enabled but
  36333. + * uncompressible.
  36334. + *
  36335. + * Revision 1.68 2000/09/22 17:53:12 rgb
  36336. + * Fixed ipcomp tdb pointers update for policy checking.
  36337. + *
  36338. + * Revision 1.67 2000/09/21 03:40:58 rgb
  36339. + * Added more debugging to try and track down the cpi outward copy problem.
  36340. + *
  36341. + * Revision 1.66 2000/09/20 04:00:10 rgb
  36342. + * Changed static functions to DEBUG_NO_STATIC to reveal function names for
  36343. + * debugging oopsen.
  36344. + *
  36345. + * Revision 1.65 2000/09/19 07:07:16 rgb
  36346. + * Added debugging to inbound policy check for ipcomp.
  36347. + * Added missing spin_unlocks (thanks Svenning!).
  36348. + * Fixed misplaced tdbnext pointers causing mismatched ipip policy check.
  36349. + * Protect ipcomp policy check following ipip decap with sysctl switch.
  36350. + *
  36351. + * Revision 1.64 2000/09/18 21:27:29 rgb
  36352. + * 2.0 fixes.
  36353. + *
  36354. + * Revision 1.63 2000/09/18 02:35:50 rgb
  36355. + * Added policy checking to ipcomp and re-enabled policy checking by
  36356. + * default.
  36357. + * Optimised satoa calls.
  36358. + *
  36359. + * Revision 1.62 2000/09/17 21:02:32 rgb
  36360. + * Clean up debugging, removing slow timestamp debug code.
  36361. + *
  36362. + * Revision 1.61 2000/09/16 01:07:55 rgb
  36363. + * Fixed erroneous ref from struct ipcomp to struct ipcomphdr.
  36364. + *
  36365. + * Revision 1.60 2000/09/15 11:37:01 rgb
  36366. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  36367. + * IPCOMP zlib deflate code.
  36368. + *
  36369. + * Revision 1.59 2000/09/15 04:56:20 rgb
  36370. + * Remove redundant satoa() call, reformat comment.
  36371. + *
  36372. + * Revision 1.58 2000/09/13 08:00:52 rgb
  36373. + * Flick on inbound policy checking.
  36374. + *
  36375. + * Revision 1.57 2000/09/12 03:22:19 rgb
  36376. + * Converted inbound_policy_check to sysctl.
  36377. + * Re-enabled policy backcheck.
  36378. + * Moved policy checks to top and within tdb lock.
  36379. + *
  36380. + * Revision 1.56 2000/09/08 19:12:56 rgb
  36381. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  36382. + *
  36383. + * Revision 1.55 2000/08/28 18:15:46 rgb
  36384. + * Added MB's nf-debug reset patch.
  36385. + *
  36386. + * Revision 1.54 2000/08/27 01:41:26 rgb
  36387. + * More minor tweaks to the bad padding debug code.
  36388. + *
  36389. + * Revision 1.53 2000/08/24 16:54:16 rgb
  36390. + * Added KLIPS_PRINTMORE macro to continue lines without KERN_INFO level
  36391. + * info.
  36392. + * Tidied up device reporting at the start of ipsec_rcv.
  36393. + * Tidied up bad padding debugging and processing.
  36394. + *
  36395. + * Revision 1.52 2000/08/20 21:36:03 rgb
  36396. + * Activated pfkey_expire() calls.
  36397. + * Added a hard/soft expiry parameter to pfkey_expire().
  36398. + * Added sanity checking to avoid propagating zero or smaller-length skbs
  36399. + * from a bogus decryption.
  36400. + * Re-arranged the order of soft and hard expiry to conform to RFC2367.
  36401. + * Clean up references to CONFIG_IPSEC_PFKEYv2.
  36402. + *
  36403. + * Revision 1.51 2000/08/18 21:23:30 rgb
  36404. + * Improve bad padding warning so that the printk buffer doesn't get
  36405. + * trampled.
  36406. + *
  36407. + * Revision 1.50 2000/08/01 14:51:51 rgb
  36408. + * Removed _all_ remaining traces of DES.
  36409. + *
  36410. + * Revision 1.49 2000/07/28 13:50:53 rgb
  36411. + * Changed enet_statistics to net_device_stats and added back compatibility
  36412. + * for pre-2.1.19.
  36413. + *
  36414. + * Revision 1.48 2000/05/10 19:14:40 rgb
  36415. + * Only check usetime against soft and hard limits if the tdb has been
  36416. + * used.
  36417. + * Cast output of ntohl so that the broken prototype doesn't make our
  36418. + * compile noisy.
  36419. + *
  36420. + * Revision 1.47 2000/05/09 17:45:43 rgb
  36421. + * Fix replay bitmap corruption bug upon receipt of bogus packet
  36422. + * with correct SPI. This was a DoS.
  36423. + *
  36424. + * Revision 1.46 2000/03/27 02:31:58 rgb
  36425. + * Fixed authentication failure printout bug.
  36426. + *
  36427. + * Revision 1.45 2000/03/22 16:15:37 rgb
  36428. + * Fixed renaming of dev_get (MB).
  36429. + *
  36430. + * Revision 1.44 2000/03/16 08:17:24 rgb
  36431. + * Hardcode PF_KEYv2 support.
  36432. + * Fixed minor bug checking AH header length.
  36433. + *
  36434. + * Revision 1.43 2000/03/14 12:26:59 rgb
  36435. + * Added skb->nfct support for clearing netfilter conntrack bits (MB).
  36436. + *
  36437. + * Revision 1.42 2000/01/26 10:04:04 rgb
  36438. + * Fixed inbound policy checking on transport mode bug.
  36439. + * Fixed noisy 2.0 printk arguments.
  36440. + *
  36441. + * Revision 1.41 2000/01/24 20:58:02 rgb
  36442. + * Improve debugging/reporting support for (disabled) inbound
  36443. + * policy checking.
  36444. + *
  36445. + * Revision 1.40 2000/01/22 23:20:10 rgb
  36446. + * Fixed up inboud policy checking code.
  36447. + * Cleaned out unused crud.
  36448. + *
  36449. + * Revision 1.39 2000/01/21 06:15:29 rgb
  36450. + * Added sanity checks on skb_push(), skb_pull() to prevent panics.
  36451. + * Fixed cut-and-paste debug_tunnel to debug_rcv.
  36452. + * Added inbound policy checking code, disabled.
  36453. + * Simplified output code by updating ipp to post-IPIP decapsulation.
  36454. + *
  36455. + * elided pre-2000 comments. Use "cvs log"
  36456. + *
  36457. + *
  36458. + * Local Variables:
  36459. + * c-set-style: linux
  36460. + * End:
  36461. + *
  36462. + */
  36463. --- /dev/null Tue Mar 11 13:02:56 2003
  36464. +++ linux/net/ipsec/ipsec_sa.c Mon Feb 9 13:51:03 2004
  36465. @@ -0,0 +1,1870 @@
  36466. +/*
  36467. + * Common routines for IPsec SA maintenance routines.
  36468. + *
  36469. + * Copyright (C) 1996, 1997 John Ioannidis.
  36470. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Richard Guy Briggs.
  36471. + *
  36472. + * This program is free software; you can redistribute it and/or modify it
  36473. + * under the terms of the GNU General Public License as published by the
  36474. + * Free Software Foundation; either version 2 of the License, or (at your
  36475. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  36476. + *
  36477. + * This program is distributed in the hope that it will be useful, but
  36478. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  36479. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  36480. + * for more details.
  36481. + *
  36482. + * RCSID $Id: ipsec_sa.c,v 1.30.2.2 2006/10/06 21:39:26 paul Exp $
  36483. + *
  36484. + * This is the file formerly known as "ipsec_xform.h"
  36485. + *
  36486. + */
  36487. +
  36488. +#ifndef AUTOCONF_INCLUDED
  36489. +#include <linux/config.h>
  36490. +#endif
  36491. +#include <linux/version.h>
  36492. +#include <linux/kernel.h> /* printk() */
  36493. +
  36494. +#include "openswan/ipsec_param.h"
  36495. +
  36496. +#ifdef MALLOC_SLAB
  36497. +# include <linux/slab.h> /* kmalloc() */
  36498. +#else /* MALLOC_SLAB */
  36499. +# include <linux/malloc.h> /* kmalloc() */
  36500. +#endif /* MALLOC_SLAB */
  36501. +#include <linux/vmalloc.h> /* vmalloc() */
  36502. +#include <linux/errno.h> /* error codes */
  36503. +#include <linux/types.h> /* size_t */
  36504. +#include <linux/interrupt.h> /* mark_bh */
  36505. +
  36506. +#include <linux/netdevice.h> /* struct device, and other headers */
  36507. +#include <linux/etherdevice.h> /* eth_type_trans */
  36508. +#include <linux/ip.h> /* struct iphdr */
  36509. +#include <linux/skbuff.h>
  36510. +#include <openswan.h>
  36511. +#ifdef SPINLOCK
  36512. +#ifdef SPINLOCK_23
  36513. +#include <linux/spinlock.h> /* *lock* */
  36514. +#else /* SPINLOCK_23 */
  36515. +#include <asm/spinlock.h> /* *lock* */
  36516. +#endif /* SPINLOCK_23 */
  36517. +#endif /* SPINLOCK */
  36518. +
  36519. +#include <net/ip.h>
  36520. +
  36521. +#include "openswan/radij.h"
  36522. +
  36523. +#include "openswan/ipsec_stats.h"
  36524. +#include "openswan/ipsec_life.h"
  36525. +#include "openswan/ipsec_sa.h"
  36526. +#include "openswan/ipsec_xform.h"
  36527. +
  36528. +#include "openswan/ipsec_encap.h"
  36529. +#include "openswan/ipsec_radij.h"
  36530. +#include "openswan/ipsec_xform.h"
  36531. +#include "openswan/ipsec_ipe4.h"
  36532. +#include "openswan/ipsec_ah.h"
  36533. +#include "openswan/ipsec_esp.h"
  36534. +
  36535. +#include <pfkeyv2.h>
  36536. +#include <pfkey.h>
  36537. +
  36538. +#include "openswan/ipsec_proto.h"
  36539. +#include "openswan/ipsec_alg.h"
  36540. +
  36541. +
  36542. +#ifdef CONFIG_KLIPS_DEBUG
  36543. +int debug_xform = 0;
  36544. +#endif /* CONFIG_KLIPS_DEBUG */
  36545. +
  36546. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  36547. +
  36548. +struct ipsec_sa *ipsec_sadb_hash[SADB_HASHMOD];
  36549. +#ifdef SPINLOCK
  36550. +spinlock_t tdb_lock = SPIN_LOCK_UNLOCKED;
  36551. +#else /* SPINLOCK */
  36552. +spinlock_t tdb_lock;
  36553. +#endif /* SPINLOCK */
  36554. +
  36555. +struct ipsec_sadb ipsec_sadb;
  36556. +
  36557. +#if IPSEC_SA_REF_CODE
  36558. +
  36559. +/* the sub table must be narrower (or equal) in bits than the variable type
  36560. + in the main table to count the number of unused entries in it. */
  36561. +typedef struct {
  36562. + int testSizeOf_refSubTable :
  36563. + ((sizeof(IPsecRefTableUnusedCount) * 8) < IPSEC_SA_REF_SUBTABLE_IDX_WIDTH ? -1 : 1);
  36564. +} dummy;
  36565. +
  36566. +
  36567. +/* The field where the saref will be hosted in the skb must be wide enough to
  36568. + accomodate the information it needs to store. */
  36569. +typedef struct {
  36570. + int testSizeOf_refField :
  36571. + (IPSEC_SA_REF_HOST_FIELD_WIDTH < IPSEC_SA_REF_TABLE_IDX_WIDTH ? -1 : 1 );
  36572. +} dummy2;
  36573. +
  36574. +
  36575. +#define IPS_HASH(said) (((said)->spi + (said)->dst.u.v4.sin_addr.s_addr + (said)->proto) % SADB_HASHMOD)
  36576. +
  36577. +
  36578. +void
  36579. +ipsec_SAtest(void)
  36580. +{
  36581. + IPsecSAref_t SAref = 258;
  36582. + struct ipsec_sa ips;
  36583. + ips.ips_ref = 772;
  36584. +
  36585. + printk("klips_debug:ipsec_SAtest: "
  36586. + "IPSEC_SA_REF_SUBTABLE_IDX_WIDTH=%u\n"
  36587. + "IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES=%u\n"
  36588. + "IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES=%u\n"
  36589. + "IPSEC_SA_REF_HOST_FIELD_WIDTH=%lu\n"
  36590. + "IPSEC_SA_REF_TABLE_MASK=%x\n"
  36591. + "IPSEC_SA_REF_ENTRY_MASK=%x\n"
  36592. + "IPsecSAref2table(%d)=%u\n"
  36593. + "IPsecSAref2entry(%d)=%u\n"
  36594. + "IPsecSAref2NFmark(%d)=%u\n"
  36595. + "IPsecSAref2SA(%d)=%p\n"
  36596. + "IPsecSA2SAref(%p)=%d\n"
  36597. + ,
  36598. + IPSEC_SA_REF_SUBTABLE_IDX_WIDTH,
  36599. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES,
  36600. + IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES,
  36601. + (unsigned long) IPSEC_SA_REF_HOST_FIELD_WIDTH,
  36602. + IPSEC_SA_REF_TABLE_MASK,
  36603. + IPSEC_SA_REF_ENTRY_MASK,
  36604. + SAref, IPsecSAref2table(SAref),
  36605. + SAref, IPsecSAref2entry(SAref),
  36606. + SAref, IPsecSAref2NFmark(SAref),
  36607. + SAref, IPsecSAref2SA(SAref),
  36608. + (&ips), IPsecSA2SAref((&ips))
  36609. + );
  36610. + return;
  36611. +}
  36612. +
  36613. +int
  36614. +ipsec_SAref_recycle(void)
  36615. +{
  36616. + int table;
  36617. + int entry;
  36618. + int error = 0;
  36619. +
  36620. + ipsec_sadb.refFreeListHead = -1;
  36621. + ipsec_sadb.refFreeListTail = -1;
  36622. +
  36623. + if(ipsec_sadb.refFreeListCont == IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES * IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES) {
  36624. + KLIPS_PRINT(debug_xform,
  36625. + "klips_debug:ipsec_SAref_recycle: "
  36626. + "end of table reached, continuing at start..\n");
  36627. + ipsec_sadb.refFreeListCont = 0;
  36628. + }
  36629. +
  36630. + KLIPS_PRINT(debug_xform,
  36631. + "klips_debug:ipsec_SAref_recycle: "
  36632. + "recycling, continuing from SAref=%d (0p%p), table=%d, entry=%d.\n",
  36633. + ipsec_sadb.refFreeListCont,
  36634. + (ipsec_sadb.refTable[IPsecSAref2table(ipsec_sadb.refFreeListCont)] != NULL) ? IPsecSAref2SA(ipsec_sadb.refFreeListCont) : NULL,
  36635. + IPsecSAref2table(ipsec_sadb.refFreeListCont),
  36636. + IPsecSAref2entry(ipsec_sadb.refFreeListCont));
  36637. +
  36638. + for(table = IPsecSAref2table(ipsec_sadb.refFreeListCont);
  36639. + table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES;
  36640. + table++) {
  36641. + if(ipsec_sadb.refTable[table] == NULL) {
  36642. + error = ipsec_SArefSubTable_alloc(table);
  36643. + if(error) {
  36644. + return error;
  36645. + }
  36646. + }
  36647. + for(entry = IPsecSAref2entry(ipsec_sadb.refFreeListCont);
  36648. + entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES;
  36649. + entry++) {
  36650. + if(ipsec_sadb.refTable[table]->entry[entry] == NULL) {
  36651. + ipsec_sadb.refFreeList[++ipsec_sadb.refFreeListTail] = IPsecSArefBuild(table, entry);
  36652. + if(ipsec_sadb.refFreeListTail == (IPSEC_SA_REF_FREELIST_NUM_ENTRIES - 1)) {
  36653. + ipsec_sadb.refFreeListHead = 0;
  36654. + ipsec_sadb.refFreeListCont = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListTail] + 1;
  36655. + KLIPS_PRINT(debug_xform,
  36656. + "klips_debug:ipsec_SAref_recycle: "
  36657. + "SArefFreeList refilled.\n");
  36658. + return 0;
  36659. + }
  36660. + }
  36661. + }
  36662. + }
  36663. +
  36664. + if(ipsec_sadb.refFreeListTail == -1) {
  36665. + KLIPS_PRINT(debug_xform,
  36666. + "klips_debug:ipsec_SAref_recycle: "
  36667. + "out of room in the SArefTable.\n");
  36668. +
  36669. + return(-ENOSPC);
  36670. + }
  36671. +
  36672. + ipsec_sadb.refFreeListHead = 0;
  36673. + ipsec_sadb.refFreeListCont = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListTail] + 1;
  36674. + KLIPS_PRINT(debug_xform,
  36675. + "klips_debug:ipsec_SAref_recycle: "
  36676. + "SArefFreeList partly refilled to %d of %d.\n",
  36677. + ipsec_sadb.refFreeListTail,
  36678. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36679. + return 0;
  36680. +}
  36681. +
  36682. +int
  36683. +ipsec_SArefSubTable_alloc(unsigned table)
  36684. +{
  36685. + unsigned entry;
  36686. + struct IPsecSArefSubTable* SArefsub;
  36687. +
  36688. + KLIPS_PRINT(debug_xform,
  36689. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36690. + "allocating %lu bytes for table %u of %u.\n",
  36691. + (unsigned long) (IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES * sizeof(struct ipsec_sa *)),
  36692. + table,
  36693. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES);
  36694. +
  36695. + /* allocate another sub-table */
  36696. + SArefsub = vmalloc(IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES * sizeof(struct ipsec_sa *));
  36697. + if(SArefsub == NULL) {
  36698. + KLIPS_PRINT(debug_xform,
  36699. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36700. + "error allocating memory for table %u of %u!\n",
  36701. + table,
  36702. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES);
  36703. + return -ENOMEM;
  36704. + }
  36705. +
  36706. + /* add this sub-table to the main table */
  36707. + ipsec_sadb.refTable[table] = SArefsub;
  36708. +
  36709. + /* initialise each element to NULL */
  36710. + KLIPS_PRINT(debug_xform,
  36711. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36712. + "initialising %u elements (2 ^ %u) of table %u.\n",
  36713. + IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES,
  36714. + IPSEC_SA_REF_SUBTABLE_IDX_WIDTH,
  36715. + table);
  36716. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  36717. + SArefsub->entry[entry] = NULL;
  36718. + }
  36719. +
  36720. + return 0;
  36721. +}
  36722. +#endif /* IPSEC_SA_REF_CODE */
  36723. +
  36724. +int
  36725. +ipsec_saref_freelist_init(void)
  36726. +{
  36727. + int i;
  36728. +
  36729. + KLIPS_PRINT(debug_xform,
  36730. + "klips_debug:ipsec_saref_freelist_init: "
  36731. + "initialising %u elements of FreeList.\n",
  36732. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36733. +
  36734. + for(i = 0; i < IPSEC_SA_REF_FREELIST_NUM_ENTRIES; i++) {
  36735. + ipsec_sadb.refFreeList[i] = IPSEC_SAREF_NULL;
  36736. + }
  36737. + ipsec_sadb.refFreeListHead = -1;
  36738. + ipsec_sadb.refFreeListCont = 0;
  36739. + ipsec_sadb.refFreeListTail = -1;
  36740. +
  36741. + return 0;
  36742. +}
  36743. +
  36744. +int
  36745. +ipsec_sadb_init(void)
  36746. +{
  36747. + int error = 0;
  36748. + unsigned i;
  36749. +
  36750. + for(i = 0; i < SADB_HASHMOD; i++) {
  36751. + ipsec_sadb_hash[i] = NULL;
  36752. + }
  36753. + /* parts above are for the old style SADB hash table */
  36754. +
  36755. +
  36756. +#if IPSEC_SA_REF_CODE
  36757. + /* initialise SA reference table */
  36758. +
  36759. + /* initialise the main table */
  36760. + KLIPS_PRINT(debug_xform,
  36761. + "klips_debug:ipsec_sadb_init: "
  36762. + "initialising main table of size %u (2 ^ %u).\n",
  36763. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES,
  36764. + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH);
  36765. + {
  36766. + unsigned table;
  36767. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  36768. + ipsec_sadb.refTable[table] = NULL;
  36769. + }
  36770. + }
  36771. +
  36772. + /* allocate the first sub-table */
  36773. + error = ipsec_SArefSubTable_alloc(0);
  36774. + if(error) {
  36775. + return error;
  36776. + }
  36777. +
  36778. + error = ipsec_saref_freelist_init();
  36779. +#endif /* IPSEC_SA_REF_CODE */
  36780. + return error;
  36781. +}
  36782. +
  36783. +#if IPSEC_SA_REF_CODE
  36784. +IPsecSAref_t
  36785. +ipsec_SAref_alloc(int*error) /* pass in error var by pointer */
  36786. +{
  36787. + IPsecSAref_t SAref;
  36788. +
  36789. + KLIPS_PRINT(debug_xform,
  36790. + "klips_debug:ipsec_SAref_alloc: "
  36791. + "SAref requested... head=%d, cont=%d, tail=%d, listsize=%d.\n",
  36792. + ipsec_sadb.refFreeListHead,
  36793. + ipsec_sadb.refFreeListCont,
  36794. + ipsec_sadb.refFreeListTail,
  36795. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36796. +
  36797. + if(ipsec_sadb.refFreeListHead == -1) {
  36798. + KLIPS_PRINT(debug_xform,
  36799. + "klips_debug:ipsec_SAref_alloc: "
  36800. + "FreeList empty, recycling...\n");
  36801. + *error = ipsec_SAref_recycle();
  36802. + if(*error) {
  36803. + return IPSEC_SAREF_NULL;
  36804. + }
  36805. + }
  36806. +
  36807. + SAref = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListHead];
  36808. + if(SAref == IPSEC_SAREF_NULL) {
  36809. + KLIPS_PRINT(debug_xform,
  36810. + "klips_debug:ipsec_SAref_alloc: "
  36811. + "unexpected error, refFreeListHead = %d points to invalid entry.\n",
  36812. + ipsec_sadb.refFreeListHead);
  36813. + *error = -ESPIPE;
  36814. + return IPSEC_SAREF_NULL;
  36815. + }
  36816. +
  36817. + KLIPS_PRINT(debug_xform,
  36818. + "klips_debug:ipsec_SAref_alloc: "
  36819. + "allocating SAref=%d, table=%u, entry=%u of %u.\n",
  36820. + SAref,
  36821. + IPsecSAref2table(SAref),
  36822. + IPsecSAref2entry(SAref),
  36823. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES * IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES);
  36824. +
  36825. + ipsec_sadb.refFreeList[ipsec_sadb.refFreeListHead] = IPSEC_SAREF_NULL;
  36826. + ipsec_sadb.refFreeListHead++;
  36827. + if(ipsec_sadb.refFreeListHead > ipsec_sadb.refFreeListTail) {
  36828. + KLIPS_PRINT(debug_xform,
  36829. + "klips_debug:ipsec_SAref_alloc: "
  36830. + "last FreeList entry allocated, resetting list head to empty.\n");
  36831. + ipsec_sadb.refFreeListHead = -1;
  36832. + }
  36833. +
  36834. + return SAref;
  36835. +}
  36836. +#endif /* IPSEC_SA_REF_CODE */
  36837. +
  36838. +int
  36839. +ipsec_sa_print(struct ipsec_sa *ips)
  36840. +{
  36841. + char sa[SATOT_BUF];
  36842. + size_t sa_len;
  36843. +
  36844. + printk(KERN_INFO "klips_debug: SA:");
  36845. + if(ips == NULL) {
  36846. + printk("NULL\n");
  36847. + return -ENOENT;
  36848. + }
  36849. + printk(" ref=%d", ips->ips_ref);
  36850. + printk(" refcount=%d", atomic_read(&ips->ips_refcount));
  36851. + if(ips->ips_hnext != NULL) {
  36852. + printk(" hnext=0p%p", ips->ips_hnext);
  36853. + }
  36854. + if(ips->ips_inext != NULL) {
  36855. + printk(" inext=0p%p", ips->ips_inext);
  36856. + }
  36857. + if(ips->ips_onext != NULL) {
  36858. + printk(" onext=0p%p", ips->ips_onext);
  36859. + }
  36860. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  36861. + printk(" said=%s", sa_len ? sa : " (error)");
  36862. + if(ips->ips_seq) {
  36863. + printk(" seq=%u", ips->ips_seq);
  36864. + }
  36865. + if(ips->ips_pid) {
  36866. + printk(" pid=%u", ips->ips_pid);
  36867. + }
  36868. + if(ips->ips_authalg) {
  36869. + printk(" authalg=%u", ips->ips_authalg);
  36870. + }
  36871. + if(ips->ips_encalg) {
  36872. + printk(" encalg=%u", ips->ips_encalg);
  36873. + }
  36874. + printk(" XFORM=%s%s%s", IPS_XFORM_NAME(ips));
  36875. + if(ips->ips_replaywin) {
  36876. + printk(" ooowin=%u", ips->ips_replaywin);
  36877. + }
  36878. + if(ips->ips_flags) {
  36879. + printk(" flags=%u", ips->ips_flags);
  36880. + }
  36881. + if(ips->ips_addr_s) {
  36882. + char buf[SUBNETTOA_BUF];
  36883. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_s))->sin_addr,
  36884. + 0, buf, sizeof(buf));
  36885. + printk(" src=%s", buf);
  36886. + }
  36887. + if(ips->ips_addr_d) {
  36888. + char buf[SUBNETTOA_BUF];
  36889. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_s))->sin_addr,
  36890. + 0, buf, sizeof(buf));
  36891. + printk(" dst=%s", buf);
  36892. + }
  36893. + if(ips->ips_addr_p) {
  36894. + char buf[SUBNETTOA_BUF];
  36895. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_p))->sin_addr,
  36896. + 0, buf, sizeof(buf));
  36897. + printk(" proxy=%s", buf);
  36898. + }
  36899. + if(ips->ips_key_bits_a) {
  36900. + printk(" key_bits_a=%u", ips->ips_key_bits_a);
  36901. + }
  36902. + if(ips->ips_key_bits_e) {
  36903. + printk(" key_bits_e=%u", ips->ips_key_bits_e);
  36904. + }
  36905. +
  36906. + printk("\n");
  36907. + return 0;
  36908. +}
  36909. +
  36910. +struct ipsec_sa*
  36911. +ipsec_sa_alloc(int*error) /* pass in error var by pointer */
  36912. +{
  36913. + struct ipsec_sa* ips;
  36914. +
  36915. + if((ips = kmalloc(sizeof(*ips), GFP_ATOMIC) ) == NULL) {
  36916. + KLIPS_PRINT(debug_xform,
  36917. + "klips_debug:ipsec_sa_alloc: "
  36918. + "memory allocation error\n");
  36919. + *error = -ENOMEM;
  36920. + return NULL;
  36921. + }
  36922. + memset((caddr_t)ips, 0, sizeof(*ips));
  36923. +#if IPSEC_SA_REF_CODE
  36924. + ips->ips_ref = ipsec_SAref_alloc(error); /* pass in error return by pointer */
  36925. + KLIPS_PRINT(debug_xform,
  36926. + "klips_debug:ipsec_sa_alloc: "
  36927. + "allocated %lu bytes for ipsec_sa struct=0p%p ref=%d.\n",
  36928. + (unsigned long) sizeof(*ips),
  36929. + ips,
  36930. + ips->ips_ref);
  36931. + if(ips->ips_ref == IPSEC_SAREF_NULL) {
  36932. + kfree(ips);
  36933. + KLIPS_PRINT(debug_xform,
  36934. + "klips_debug:ipsec_sa_alloc: "
  36935. + "SAref allocation error\n");
  36936. + return NULL;
  36937. + }
  36938. +
  36939. + atomic_inc(&ips->ips_refcount);
  36940. + IPsecSAref2SA(ips->ips_ref) = ips;
  36941. +#endif /* IPSEC_SA_REF_CODE */
  36942. +
  36943. + *error = 0;
  36944. + return(ips);
  36945. +}
  36946. +
  36947. +int
  36948. +ipsec_sa_free(struct ipsec_sa* ips)
  36949. +{
  36950. + return ipsec_sa_wipe(ips);
  36951. +}
  36952. +
  36953. +struct ipsec_sa *
  36954. +ipsec_sa_getbyid(ip_said *said)
  36955. +{
  36956. + int hashval;
  36957. + struct ipsec_sa *ips;
  36958. + char sa[SATOT_BUF];
  36959. + size_t sa_len;
  36960. +
  36961. + if(said == NULL) {
  36962. + KLIPS_PRINT(debug_xform,
  36963. + "klips_error:ipsec_sa_getbyid: "
  36964. + "null pointer passed in!\n");
  36965. + return NULL;
  36966. + }
  36967. +
  36968. + sa_len = satot(said, 0, sa, sizeof(sa));
  36969. +
  36970. + hashval = IPS_HASH(said);
  36971. +
  36972. + KLIPS_PRINT(debug_xform,
  36973. + "klips_debug:ipsec_sa_getbyid: "
  36974. + "linked entry in ipsec_sa table for hash=%d of SA:%s requested.\n",
  36975. + hashval,
  36976. + sa_len ? sa : " (error)");
  36977. +
  36978. + if((ips = ipsec_sadb_hash[hashval]) == NULL) {
  36979. + KLIPS_PRINT(debug_xform,
  36980. + "klips_debug:ipsec_sa_getbyid: "
  36981. + "no entries in ipsec_sa table for hash=%d of SA:%s.\n",
  36982. + hashval,
  36983. + sa_len ? sa : " (error)");
  36984. + return NULL;
  36985. + }
  36986. +
  36987. + for (; ips; ips = ips->ips_hnext) {
  36988. + if ((ips->ips_said.spi == said->spi) &&
  36989. + (ips->ips_said.dst.u.v4.sin_addr.s_addr == said->dst.u.v4.sin_addr.s_addr) &&
  36990. + (ips->ips_said.proto == said->proto)) {
  36991. + atomic_inc(&ips->ips_refcount);
  36992. + return ips;
  36993. + }
  36994. + }
  36995. +
  36996. + KLIPS_PRINT(debug_xform,
  36997. + "klips_debug:ipsec_sa_getbyid: "
  36998. + "no entry in linked list for hash=%d of SA:%s.\n",
  36999. + hashval,
  37000. + sa_len ? sa : " (error)");
  37001. + return NULL;
  37002. +}
  37003. +
  37004. +int
  37005. +ipsec_sa_put(struct ipsec_sa *ips)
  37006. +{
  37007. + char sa[SATOT_BUF];
  37008. + size_t sa_len;
  37009. +
  37010. + if(ips == NULL) {
  37011. + KLIPS_PRINT(debug_xform,
  37012. + "klips_error:ipsec_sa_put: "
  37013. + "null pointer passed in!\n");
  37014. + return -1;
  37015. + }
  37016. +
  37017. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37018. +
  37019. + KLIPS_PRINT(debug_xform,
  37020. + "klips_debug:ipsec_sa_put: "
  37021. + "ipsec_sa SA:%s, ref:%d reference count decremented.\n",
  37022. + sa_len ? sa : " (error)",
  37023. + ips->ips_ref);
  37024. +
  37025. + atomic_dec(&ips->ips_refcount);
  37026. +
  37027. + return 0;
  37028. +}
  37029. +
  37030. +/*
  37031. + The ipsec_sa table better *NOT* be locked before it is handed in, or SMP locks will happen
  37032. +*/
  37033. +int
  37034. +ipsec_sa_add(struct ipsec_sa *ips)
  37035. +{
  37036. + int error = 0;
  37037. + unsigned int hashval;
  37038. +
  37039. + if(ips == NULL) {
  37040. + KLIPS_PRINT(debug_xform,
  37041. + "klips_error:ipsec_sa_add: "
  37042. + "null pointer passed in!\n");
  37043. + return -ENODATA;
  37044. + }
  37045. + hashval = IPS_HASH(&ips->ips_said);
  37046. +
  37047. + atomic_inc(&ips->ips_refcount);
  37048. + spin_lock_bh(&tdb_lock);
  37049. +
  37050. + ips->ips_hnext = ipsec_sadb_hash[hashval];
  37051. + ipsec_sadb_hash[hashval] = ips;
  37052. +
  37053. + spin_unlock_bh(&tdb_lock);
  37054. +
  37055. + return error;
  37056. +}
  37057. +
  37058. +/*
  37059. + The ipsec_sa table better be locked before it is handed in, or races might happen
  37060. +*/
  37061. +int
  37062. +ipsec_sa_del(struct ipsec_sa *ips)
  37063. +{
  37064. + unsigned int hashval;
  37065. + struct ipsec_sa *ipstp;
  37066. + char sa[SATOT_BUF];
  37067. + size_t sa_len;
  37068. +
  37069. + if(ips == NULL) {
  37070. + KLIPS_PRINT(debug_xform,
  37071. + "klips_error:ipsec_sa_del: "
  37072. + "null pointer passed in!\n");
  37073. + return -ENODATA;
  37074. + }
  37075. +
  37076. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37077. + if(ips->ips_inext || ips->ips_onext) {
  37078. + KLIPS_PRINT(debug_xform,
  37079. + "klips_error:ipsec_sa_del: "
  37080. + "SA:%s still linked!\n",
  37081. + sa_len ? sa : " (error)");
  37082. + return -EMLINK;
  37083. + }
  37084. +
  37085. + hashval = IPS_HASH(&ips->ips_said);
  37086. +
  37087. + KLIPS_PRINT(debug_xform,
  37088. + "klips_debug:ipsec_sa_del: "
  37089. + "deleting SA:%s, hashval=%d.\n",
  37090. + sa_len ? sa : " (error)",
  37091. + hashval);
  37092. + if(ipsec_sadb_hash[hashval] == NULL) {
  37093. + KLIPS_PRINT(debug_xform,
  37094. + "klips_debug:ipsec_sa_del: "
  37095. + "no entries in ipsec_sa table for hash=%d of SA:%s.\n",
  37096. + hashval,
  37097. + sa_len ? sa : " (error)");
  37098. + return -ENOENT;
  37099. + }
  37100. +
  37101. + if (ips == ipsec_sadb_hash[hashval]) {
  37102. + ipsec_sadb_hash[hashval] = ipsec_sadb_hash[hashval]->ips_hnext;
  37103. + ips->ips_hnext = NULL;
  37104. + atomic_dec(&ips->ips_refcount);
  37105. + KLIPS_PRINT(debug_xform,
  37106. + "klips_debug:ipsec_sa_del: "
  37107. + "successfully deleted first ipsec_sa in chain.\n");
  37108. + return 0;
  37109. + } else {
  37110. + for (ipstp = ipsec_sadb_hash[hashval];
  37111. + ipstp;
  37112. + ipstp = ipstp->ips_hnext) {
  37113. + if (ipstp->ips_hnext == ips) {
  37114. + ipstp->ips_hnext = ips->ips_hnext;
  37115. + ips->ips_hnext = NULL;
  37116. + atomic_dec(&ips->ips_refcount);
  37117. + KLIPS_PRINT(debug_xform,
  37118. + "klips_debug:ipsec_sa_del: "
  37119. + "successfully deleted link in ipsec_sa chain.\n");
  37120. + return 0;
  37121. + }
  37122. + }
  37123. + }
  37124. +
  37125. + KLIPS_PRINT(debug_xform,
  37126. + "klips_debug:ipsec_sa_del: "
  37127. + "no entries in linked list for hash=%d of SA:%s.\n",
  37128. + hashval,
  37129. + sa_len ? sa : " (error)");
  37130. + return -ENOENT;
  37131. +}
  37132. +
  37133. +/*
  37134. + The ipsec_sa table better be locked before it is handed in, or races
  37135. + might happen
  37136. +*/
  37137. +int
  37138. +ipsec_sa_delchain(struct ipsec_sa *ips)
  37139. +{
  37140. + struct ipsec_sa *ipsdel;
  37141. + int error = 0;
  37142. + char sa[SATOT_BUF];
  37143. + size_t sa_len;
  37144. +
  37145. + if(ips == NULL) {
  37146. + KLIPS_PRINT(debug_xform,
  37147. + "klips_error:ipsec_sa_delchain: "
  37148. + "null pointer passed in!\n");
  37149. + return -ENODATA;
  37150. + }
  37151. +
  37152. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37153. + KLIPS_PRINT(debug_xform,
  37154. + "klips_debug:ipsec_sa_delchain: "
  37155. + "passed SA:%s\n",
  37156. + sa_len ? sa : " (error)");
  37157. + while(ips->ips_onext != NULL) {
  37158. + ips = ips->ips_onext;
  37159. + }
  37160. +
  37161. + while(ips) {
  37162. + /* XXX send a pfkey message up to advise of deleted ipsec_sa */
  37163. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37164. + KLIPS_PRINT(debug_xform,
  37165. + "klips_debug:ipsec_sa_delchain: "
  37166. + "unlinking and delting SA:%s",
  37167. + sa_len ? sa : " (error)");
  37168. + ipsdel = ips;
  37169. + ips = ips->ips_inext;
  37170. + if(ips != NULL) {
  37171. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37172. + KLIPS_PRINT(debug_xform,
  37173. + ", inext=%s",
  37174. + sa_len ? sa : " (error)");
  37175. + atomic_dec(&ipsdel->ips_refcount);
  37176. + ipsdel->ips_inext = NULL;
  37177. + atomic_dec(&ips->ips_refcount);
  37178. + ips->ips_onext = NULL;
  37179. + }
  37180. + KLIPS_PRINT(debug_xform,
  37181. + ".\n");
  37182. + if((error = ipsec_sa_del(ipsdel))) {
  37183. + KLIPS_PRINT(debug_xform,
  37184. + "klips_debug:ipsec_sa_delchain: "
  37185. + "ipsec_sa_del returned error %d.\n", -error);
  37186. + return error;
  37187. + }
  37188. + if((error = ipsec_sa_wipe(ipsdel))) {
  37189. + KLIPS_PRINT(debug_xform,
  37190. + "klips_debug:ipsec_sa_delchain: "
  37191. + "ipsec_sa_wipe returned error %d.\n", -error);
  37192. + return error;
  37193. + }
  37194. + }
  37195. + return error;
  37196. +}
  37197. +
  37198. +int
  37199. +ipsec_sadb_cleanup(__u8 proto)
  37200. +{
  37201. + unsigned i;
  37202. + int error = 0;
  37203. + struct ipsec_sa *ips, **ipsprev, *ipsdel;
  37204. + char sa[SATOT_BUF];
  37205. + size_t sa_len;
  37206. +
  37207. + KLIPS_PRINT(debug_xform,
  37208. + "klips_debug:ipsec_sadb_cleanup: "
  37209. + "cleaning up proto=%d.\n",
  37210. + proto);
  37211. +
  37212. + spin_lock_bh(&tdb_lock);
  37213. +
  37214. + for (i = 0; i < SADB_HASHMOD; i++) {
  37215. + ipsprev = &(ipsec_sadb_hash[i]);
  37216. + ips = ipsec_sadb_hash[i];
  37217. + if(ips != NULL) {
  37218. + atomic_inc(&ips->ips_refcount);
  37219. + }
  37220. + for(; ips != NULL;) {
  37221. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37222. + KLIPS_PRINT(debug_xform,
  37223. + "klips_debug:ipsec_sadb_cleanup: "
  37224. + "checking SA:%s, hash=%d, ref=%d",
  37225. + sa_len ? sa : " (error)",
  37226. + i,
  37227. + ips->ips_ref);
  37228. + ipsdel = ips;
  37229. + ips = ipsdel->ips_hnext;
  37230. + if(ips != NULL) {
  37231. + atomic_inc(&ips->ips_refcount);
  37232. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37233. + KLIPS_PRINT(debug_xform,
  37234. + ", hnext=%s",
  37235. + sa_len ? sa : " (error)");
  37236. + }
  37237. + if(*ipsprev != NULL) {
  37238. + sa_len = satot(&(*ipsprev)->ips_said, 0, sa, sizeof(sa));
  37239. + KLIPS_PRINT(debug_xform,
  37240. + ", *ipsprev=%s",
  37241. + sa_len ? sa : " (error)");
  37242. + if((*ipsprev)->ips_hnext) {
  37243. + sa_len = satot(&(*ipsprev)->ips_hnext->ips_said, 0, sa, sizeof(sa));
  37244. + KLIPS_PRINT(debug_xform,
  37245. + ", *ipsprev->ips_hnext=%s",
  37246. + sa_len ? sa : " (error)");
  37247. + }
  37248. + }
  37249. + KLIPS_PRINT(debug_xform,
  37250. + ".\n");
  37251. + if(proto == 0 || (proto == ipsdel->ips_said.proto)) {
  37252. + sa_len = satot(&ipsdel->ips_said, 0, sa, sizeof(sa));
  37253. + KLIPS_PRINT(debug_xform,
  37254. + "klips_debug:ipsec_sadb_cleanup: "
  37255. + "deleting SA chain:%s.\n",
  37256. + sa_len ? sa : " (error)");
  37257. + if((error = ipsec_sa_delchain(ipsdel))) {
  37258. + SENDERR(-error);
  37259. + }
  37260. + ipsprev = &(ipsec_sadb_hash[i]);
  37261. + ips = ipsec_sadb_hash[i];
  37262. +
  37263. + KLIPS_PRINT(debug_xform,
  37264. + "klips_debug:ipsec_sadb_cleanup: "
  37265. + "deleted SA chain:%s",
  37266. + sa_len ? sa : " (error)");
  37267. + if(ips != NULL) {
  37268. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37269. + KLIPS_PRINT(debug_xform,
  37270. + ", ipsec_sadb_hash[%d]=%s",
  37271. + i,
  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 != NULL) {
  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. + } else {
  37289. + ipsprev = &ipsdel;
  37290. + }
  37291. + if(ipsdel != NULL) {
  37292. + ipsec_sa_put(ipsdel);
  37293. + }
  37294. + }
  37295. + }
  37296. + errlab:
  37297. +
  37298. + spin_unlock_bh(&tdb_lock);
  37299. +
  37300. +
  37301. +#if IPSEC_SA_REF_CODE
  37302. + /* clean up SA reference table */
  37303. +
  37304. + /* go through the ref table and clean out all the SAs */
  37305. + KLIPS_PRINT(debug_xform,
  37306. + "klips_debug:ipsec_sadb_cleanup: "
  37307. + "removing SAref entries and tables.");
  37308. + {
  37309. + unsigned table, entry;
  37310. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  37311. + KLIPS_PRINT(debug_xform,
  37312. + "klips_debug:ipsec_sadb_cleanup: "
  37313. + "cleaning SAref table=%u.\n",
  37314. + table);
  37315. + if(ipsec_sadb.refTable[table] == NULL) {
  37316. + printk("\n");
  37317. + KLIPS_PRINT(debug_xform,
  37318. + "klips_debug:ipsec_sadb_cleanup: "
  37319. + "cleaned %u used refTables.\n",
  37320. + table);
  37321. + break;
  37322. + }
  37323. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  37324. + if(ipsec_sadb.refTable[table]->entry[entry] != NULL) {
  37325. + ipsec_sa_delchain(ipsec_sadb.refTable[table]->entry[entry]);
  37326. + ipsec_sadb.refTable[table]->entry[entry] = NULL;
  37327. + }
  37328. + }
  37329. + }
  37330. + }
  37331. +#endif /* IPSEC_SA_REF_CODE */
  37332. +
  37333. + return(error);
  37334. +}
  37335. +
  37336. +int
  37337. +ipsec_sadb_free(void)
  37338. +{
  37339. + int error = 0;
  37340. +
  37341. + KLIPS_PRINT(debug_xform,
  37342. + "klips_debug:ipsec_sadb_free: "
  37343. + "freeing SArefTable memory.\n");
  37344. +
  37345. + /* clean up SA reference table */
  37346. +
  37347. + /* go through the ref table and clean out all the SAs if any are
  37348. + left and free table memory */
  37349. + KLIPS_PRINT(debug_xform,
  37350. + "klips_debug:ipsec_sadb_free: "
  37351. + "removing SAref entries and tables.\n");
  37352. + {
  37353. + unsigned table, entry;
  37354. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  37355. + KLIPS_PRINT(debug_xform,
  37356. + "klips_debug:ipsec_sadb_free: "
  37357. + "removing SAref table=%u.\n",
  37358. + table);
  37359. + if(ipsec_sadb.refTable[table] == NULL) {
  37360. + KLIPS_PRINT(debug_xform,
  37361. + "klips_debug:ipsec_sadb_free: "
  37362. + "removed %u used refTables.\n",
  37363. + table);
  37364. + break;
  37365. + }
  37366. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  37367. + if(ipsec_sadb.refTable[table]->entry[entry] != NULL) {
  37368. + ipsec_sa_delchain(ipsec_sadb.refTable[table]->entry[entry]);
  37369. + ipsec_sadb.refTable[table]->entry[entry] = NULL;
  37370. + }
  37371. + }
  37372. + vfree(ipsec_sadb.refTable[table]);
  37373. + ipsec_sadb.refTable[table] = NULL;
  37374. + }
  37375. + }
  37376. +
  37377. + return(error);
  37378. +}
  37379. +
  37380. +int
  37381. +ipsec_sa_wipe(struct ipsec_sa *ips)
  37382. +{
  37383. + if(ips == NULL) {
  37384. + return -ENODATA;
  37385. + }
  37386. +
  37387. + /* if(atomic_dec_and_test(ips)) {
  37388. + }; */
  37389. +
  37390. +#if IPSEC_SA_REF_CODE
  37391. + /* remove me from the SArefTable */
  37392. + {
  37393. + char sa[SATOT_BUF];
  37394. + size_t sa_len;
  37395. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37396. + KLIPS_PRINT(debug_xform,
  37397. + "klips_debug:ipsec_sa_wipe: "
  37398. + "removing SA=%s(0p%p), SAref=%d, table=%d(0p%p), entry=%d from the refTable.\n",
  37399. + sa_len ? sa : " (error)",
  37400. + ips,
  37401. + ips->ips_ref,
  37402. + IPsecSAref2table(IPsecSA2SAref(ips)),
  37403. + ipsec_sadb.refTable[IPsecSAref2table(IPsecSA2SAref(ips))],
  37404. + IPsecSAref2entry(IPsecSA2SAref(ips)));
  37405. + }
  37406. + if(ips->ips_ref == IPSEC_SAREF_NULL) {
  37407. + KLIPS_PRINT(debug_xform,
  37408. + "klips_debug:ipsec_sa_wipe: "
  37409. + "why does this SA not have a valid SAref?.\n");
  37410. + }
  37411. + ipsec_sadb.refTable[IPsecSAref2table(IPsecSA2SAref(ips))]->entry[IPsecSAref2entry(IPsecSA2SAref(ips))] = NULL;
  37412. + ips->ips_ref = IPSEC_SAREF_NULL;
  37413. + ipsec_sa_put(ips);
  37414. +#endif /* IPSEC_SA_REF_CODE */
  37415. +
  37416. + /* paranoid clean up */
  37417. + if(ips->ips_addr_s != NULL) {
  37418. + memset((caddr_t)(ips->ips_addr_s), 0, ips->ips_addr_s_size);
  37419. + kfree(ips->ips_addr_s);
  37420. + }
  37421. + ips->ips_addr_s = NULL;
  37422. +
  37423. + if(ips->ips_addr_d != NULL) {
  37424. + memset((caddr_t)(ips->ips_addr_d), 0, ips->ips_addr_d_size);
  37425. + kfree(ips->ips_addr_d);
  37426. + }
  37427. + ips->ips_addr_d = NULL;
  37428. +
  37429. + if(ips->ips_addr_p != NULL) {
  37430. + memset((caddr_t)(ips->ips_addr_p), 0, ips->ips_addr_p_size);
  37431. + kfree(ips->ips_addr_p);
  37432. + }
  37433. + ips->ips_addr_p = NULL;
  37434. +
  37435. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  37436. + if(ips->ips_natt_oa) {
  37437. + memset((caddr_t)(ips->ips_natt_oa), 0, ips->ips_natt_oa_size);
  37438. + kfree(ips->ips_natt_oa);
  37439. + }
  37440. + ips->ips_natt_oa = NULL;
  37441. +#endif
  37442. +
  37443. + if(ips->ips_key_a != NULL) {
  37444. + memset((caddr_t)(ips->ips_key_a), 0, ips->ips_key_a_size);
  37445. + kfree(ips->ips_key_a);
  37446. + }
  37447. + ips->ips_key_a = NULL;
  37448. +
  37449. + if(ips->ips_key_e != NULL) {
  37450. + if (ips->ips_alg_enc &&
  37451. + ips->ips_alg_enc->ixt_e_destroy_key)
  37452. + {
  37453. + ips->ips_alg_enc->ixt_e_destroy_key(ips->ips_alg_enc,
  37454. + ips->ips_key_e);
  37455. + } else
  37456. + {
  37457. + memset((caddr_t)(ips->ips_key_e), 0, ips->ips_key_e_size);
  37458. + kfree(ips->ips_key_e);
  37459. + }
  37460. + }
  37461. + ips->ips_key_e = NULL;
  37462. +
  37463. + if(ips->ips_iv != NULL) {
  37464. + memset((caddr_t)(ips->ips_iv), 0, ips->ips_iv_size);
  37465. + kfree(ips->ips_iv);
  37466. + }
  37467. + ips->ips_iv = NULL;
  37468. +
  37469. + if(ips->ips_ident_s.data != NULL) {
  37470. + memset((caddr_t)(ips->ips_ident_s.data),
  37471. + 0,
  37472. + ips->ips_ident_s.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident));
  37473. + kfree(ips->ips_ident_s.data);
  37474. + }
  37475. + ips->ips_ident_s.data = NULL;
  37476. +
  37477. + if(ips->ips_ident_d.data != NULL) {
  37478. + memset((caddr_t)(ips->ips_ident_d.data),
  37479. + 0,
  37480. + ips->ips_ident_d.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident));
  37481. + kfree(ips->ips_ident_d.data);
  37482. + }
  37483. + ips->ips_ident_d.data = NULL;
  37484. +
  37485. + if (ips->ips_alg_enc||ips->ips_alg_auth) {
  37486. + ipsec_alg_sa_wipe(ips);
  37487. + }
  37488. +
  37489. + memset((caddr_t)ips, 0, sizeof(*ips));
  37490. + kfree(ips);
  37491. + ips = NULL;
  37492. +
  37493. + return 0;
  37494. +}
  37495. +
  37496. +extern int sysctl_ipsec_debug_verbose;
  37497. +
  37498. +int ipsec_sa_init(struct ipsec_sa *ipsp)
  37499. +{
  37500. + int i;
  37501. + int error = 0;
  37502. + char sa[SATOT_BUF];
  37503. + size_t sa_len;
  37504. + char ipaddr_txt[ADDRTOA_BUF];
  37505. + char ipaddr2_txt[ADDRTOA_BUF];
  37506. +#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)
  37507. + unsigned char kb[AHMD596_BLKLEN];
  37508. +#endif
  37509. + struct ipsec_alg_enc *ixt_e = NULL;
  37510. + struct ipsec_alg_auth *ixt_a = NULL;
  37511. +
  37512. + if(ipsp == NULL) {
  37513. + KLIPS_PRINT(debug_pfkey,
  37514. + "ipsec_sa_init: "
  37515. + "ipsp is NULL, fatal\n");
  37516. + SENDERR(EINVAL);
  37517. + }
  37518. +
  37519. + sa_len = satot(&ipsp->ips_said, 0, sa, sizeof(sa));
  37520. +
  37521. + KLIPS_PRINT(debug_pfkey,
  37522. + "ipsec_sa_init: "
  37523. + "(pfkey defined) called for SA:%s\n",
  37524. + sa_len ? sa : " (error)");
  37525. +
  37526. + KLIPS_PRINT(debug_pfkey,
  37527. + "ipsec_sa_init: "
  37528. + "calling init routine of %s%s%s\n",
  37529. + IPS_XFORM_NAME(ipsp));
  37530. +
  37531. + switch(ipsp->ips_said.proto) {
  37532. +
  37533. +#ifdef CONFIG_KLIPS_IPIP
  37534. + case IPPROTO_IPIP: {
  37535. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_s))->sin_addr,
  37536. + 0,
  37537. + ipaddr_txt, sizeof(ipaddr_txt));
  37538. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,
  37539. + 0,
  37540. + ipaddr2_txt, sizeof(ipaddr_txt));
  37541. + KLIPS_PRINT(debug_pfkey,
  37542. + "ipsec_sa_init: "
  37543. + "(pfkey defined) IPIP ipsec_sa set for %s->%s.\n",
  37544. + ipaddr_txt,
  37545. + ipaddr2_txt);
  37546. + }
  37547. + break;
  37548. +#endif /* !CONFIG_KLIPS_IPIP */
  37549. +
  37550. +#ifdef CONFIG_KLIPS_AH
  37551. + case IPPROTO_AH:
  37552. + switch(ipsp->ips_authalg) {
  37553. +# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  37554. + case AH_MD5: {
  37555. + unsigned char *akp;
  37556. + unsigned int aks;
  37557. + MD5_CTX *ictx;
  37558. + MD5_CTX *octx;
  37559. +
  37560. + if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {
  37561. + KLIPS_PRINT(debug_pfkey,
  37562. + "ipsec_sa_init: "
  37563. + "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37564. + ipsp->ips_key_bits_a, AHMD596_KLEN * 8);
  37565. + SENDERR(EINVAL);
  37566. + }
  37567. +
  37568. +# if KLIPS_DIVULGE_HMAC_KEY
  37569. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37570. + "ipsec_sa_init: "
  37571. + "hmac md5-96 key is 0x%08x %08x %08x %08x\n",
  37572. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37573. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37574. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37575. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37576. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37577. +
  37578. + ipsp->ips_auth_bits = AHMD596_ALEN * 8;
  37579. +
  37580. + /* save the pointer to the key material */
  37581. + akp = ipsp->ips_key_a;
  37582. + aks = ipsp->ips_key_a_size;
  37583. +
  37584. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37585. + "ipsec_sa_init: "
  37586. + "allocating %lu bytes for md5_ctx.\n",
  37587. + (unsigned long) sizeof(struct md5_ctx));
  37588. + if((ipsp->ips_key_a = (caddr_t)
  37589. + kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {
  37590. + ipsp->ips_key_a = akp;
  37591. + SENDERR(ENOMEM);
  37592. + }
  37593. + ipsp->ips_key_a_size = sizeof(struct md5_ctx);
  37594. +
  37595. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37596. + kb[i] = akp[i] ^ HMAC_IPAD;
  37597. + }
  37598. + for (; i < AHMD596_BLKLEN; i++) {
  37599. + kb[i] = HMAC_IPAD;
  37600. + }
  37601. +
  37602. + ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);
  37603. + osMD5Init(ictx);
  37604. + osMD5Update(ictx, kb, AHMD596_BLKLEN);
  37605. +
  37606. + for (i = 0; i < AHMD596_BLKLEN; i++) {
  37607. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37608. + }
  37609. +
  37610. + octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);
  37611. + osMD5Init(octx);
  37612. + osMD5Update(octx, kb, AHMD596_BLKLEN);
  37613. +
  37614. +# if KLIPS_DIVULGE_HMAC_KEY
  37615. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37616. + "ipsec_sa_init: "
  37617. + "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37618. + ((__u32*)ictx)[0],
  37619. + ((__u32*)ictx)[1],
  37620. + ((__u32*)ictx)[2],
  37621. + ((__u32*)ictx)[3],
  37622. + ((__u32*)octx)[0],
  37623. + ((__u32*)octx)[1],
  37624. + ((__u32*)octx)[2],
  37625. + ((__u32*)octx)[3] );
  37626. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37627. +
  37628. + /* zero key buffer -- paranoid */
  37629. + memset(akp, 0, aks);
  37630. + kfree(akp);
  37631. + }
  37632. + break;
  37633. +# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  37634. +# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  37635. + case AH_SHA: {
  37636. + unsigned char *akp;
  37637. + unsigned int aks;
  37638. + SHA1_CTX *ictx;
  37639. + SHA1_CTX *octx;
  37640. +
  37641. + if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {
  37642. + KLIPS_PRINT(debug_pfkey,
  37643. + "ipsec_sa_init: "
  37644. + "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37645. + ipsp->ips_key_bits_a, AHSHA196_KLEN * 8);
  37646. + SENDERR(EINVAL);
  37647. + }
  37648. +
  37649. +# if KLIPS_DIVULGE_HMAC_KEY
  37650. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37651. + "ipsec_sa_init: "
  37652. + "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",
  37653. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37654. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37655. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37656. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37657. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37658. +
  37659. + ipsp->ips_auth_bits = AHSHA196_ALEN * 8;
  37660. +
  37661. + /* save the pointer to the key material */
  37662. + akp = ipsp->ips_key_a;
  37663. + aks = ipsp->ips_key_a_size;
  37664. +
  37665. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37666. + "ipsec_sa_init: "
  37667. + "allocating %lu bytes for sha1_ctx.\n",
  37668. + (unsigned long) sizeof(struct sha1_ctx));
  37669. + if((ipsp->ips_key_a = (caddr_t)
  37670. + kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {
  37671. + ipsp->ips_key_a = akp;
  37672. + SENDERR(ENOMEM);
  37673. + }
  37674. + ipsp->ips_key_a_size = sizeof(struct sha1_ctx);
  37675. +
  37676. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37677. + kb[i] = akp[i] ^ HMAC_IPAD;
  37678. + }
  37679. + for (; i < AHMD596_BLKLEN; i++) {
  37680. + kb[i] = HMAC_IPAD;
  37681. + }
  37682. +
  37683. + ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);
  37684. + SHA1Init(ictx);
  37685. + SHA1Update(ictx, kb, AHSHA196_BLKLEN);
  37686. +
  37687. + for (i = 0; i < AHSHA196_BLKLEN; i++) {
  37688. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37689. + }
  37690. +
  37691. + octx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->octx);
  37692. + SHA1Init(octx);
  37693. + SHA1Update(octx, kb, AHSHA196_BLKLEN);
  37694. +
  37695. +# if KLIPS_DIVULGE_HMAC_KEY
  37696. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37697. + "ipsec_sa_init: "
  37698. + "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37699. + ((__u32*)ictx)[0],
  37700. + ((__u32*)ictx)[1],
  37701. + ((__u32*)ictx)[2],
  37702. + ((__u32*)ictx)[3],
  37703. + ((__u32*)octx)[0],
  37704. + ((__u32*)octx)[1],
  37705. + ((__u32*)octx)[2],
  37706. + ((__u32*)octx)[3] );
  37707. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37708. + /* zero key buffer -- paranoid */
  37709. + memset(akp, 0, aks);
  37710. + kfree(akp);
  37711. + }
  37712. + break;
  37713. +# endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  37714. + default:
  37715. + KLIPS_PRINT(debug_pfkey,
  37716. + "ipsec_sa_init: "
  37717. + "authalg=%d support not available in the kernel",
  37718. + ipsp->ips_authalg);
  37719. + SENDERR(EINVAL);
  37720. + }
  37721. + break;
  37722. +#endif /* CONFIG_KLIPS_AH */
  37723. +
  37724. +#ifdef CONFIG_KLIPS_ESP
  37725. + case IPPROTO_ESP:
  37726. + {
  37727. +#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)
  37728. + unsigned char *akp;
  37729. + unsigned int aks;
  37730. +#endif
  37731. +
  37732. + ipsec_alg_sa_init(ipsp);
  37733. + ixt_e=ipsp->ips_alg_enc;
  37734. +
  37735. + if (ixt_e == NULL) {
  37736. + if(printk_ratelimit()) {
  37737. + printk(KERN_INFO
  37738. + "ipsec_sa_init: "
  37739. + "encalg=%d support not available in the kernel",
  37740. + ipsp->ips_encalg);
  37741. + }
  37742. + SENDERR(ENOENT);
  37743. + }
  37744. +
  37745. + ipsp->ips_iv_size = ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  37746. +
  37747. + /* Create IV */
  37748. + if (ipsp->ips_iv_size) {
  37749. + if((ipsp->ips_iv = (caddr_t)
  37750. + kmalloc(ipsp->ips_iv_size, GFP_ATOMIC)) == NULL) {
  37751. + SENDERR(ENOMEM);
  37752. + }
  37753. + prng_bytes(&ipsec_prng,
  37754. + (char *)ipsp->ips_iv,
  37755. + ipsp->ips_iv_size);
  37756. + ipsp->ips_iv_bits = ipsp->ips_iv_size * 8;
  37757. + }
  37758. +
  37759. + if ((error=ipsec_alg_enc_key_create(ipsp)) < 0)
  37760. + SENDERR(-error);
  37761. +
  37762. + if ((ixt_a=ipsp->ips_alg_auth)) {
  37763. + if ((error=ipsec_alg_auth_key_create(ipsp)) < 0)
  37764. + SENDERR(-error);
  37765. + } else
  37766. +
  37767. + switch(ipsp->ips_authalg) {
  37768. +# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  37769. + case AH_MD5: {
  37770. + MD5_CTX *ictx;
  37771. + MD5_CTX *octx;
  37772. +
  37773. + if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {
  37774. + KLIPS_PRINT(debug_pfkey,
  37775. + "ipsec_sa_init: "
  37776. + "incorrect authorisation key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37777. + ipsp->ips_key_bits_a,
  37778. + AHMD596_KLEN * 8);
  37779. + SENDERR(EINVAL);
  37780. + }
  37781. +
  37782. +# if KLIPS_DIVULGE_HMAC_KEY
  37783. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37784. + "ipsec_sa_init: "
  37785. + "hmac md5-96 key is 0x%08x %08x %08x %08x\n",
  37786. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+0)),
  37787. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+1)),
  37788. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+2)),
  37789. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+3)));
  37790. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37791. + ipsp->ips_auth_bits = AHMD596_ALEN * 8;
  37792. +
  37793. + /* save the pointer to the key material */
  37794. + akp = ipsp->ips_key_a;
  37795. + aks = ipsp->ips_key_a_size;
  37796. +
  37797. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37798. + "ipsec_sa_init: "
  37799. + "allocating %lu bytes for md5_ctx.\n",
  37800. + (unsigned long) sizeof(struct md5_ctx));
  37801. + if((ipsp->ips_key_a = (caddr_t)
  37802. + kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {
  37803. + ipsp->ips_key_a = akp;
  37804. + SENDERR(ENOMEM);
  37805. + }
  37806. + ipsp->ips_key_a_size = sizeof(struct md5_ctx);
  37807. +
  37808. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37809. + kb[i] = akp[i] ^ HMAC_IPAD;
  37810. + }
  37811. + for (; i < AHMD596_BLKLEN; i++) {
  37812. + kb[i] = HMAC_IPAD;
  37813. + }
  37814. +
  37815. + ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);
  37816. + osMD5Init(ictx);
  37817. + osMD5Update(ictx, kb, AHMD596_BLKLEN);
  37818. +
  37819. + for (i = 0; i < AHMD596_BLKLEN; i++) {
  37820. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37821. + }
  37822. +
  37823. + octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);
  37824. + osMD5Init(octx);
  37825. + osMD5Update(octx, kb, AHMD596_BLKLEN);
  37826. +
  37827. +# if KLIPS_DIVULGE_HMAC_KEY
  37828. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37829. + "ipsec_sa_init: "
  37830. + "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37831. + ((__u32*)ictx)[0],
  37832. + ((__u32*)ictx)[1],
  37833. + ((__u32*)ictx)[2],
  37834. + ((__u32*)ictx)[3],
  37835. + ((__u32*)octx)[0],
  37836. + ((__u32*)octx)[1],
  37837. + ((__u32*)octx)[2],
  37838. + ((__u32*)octx)[3] );
  37839. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37840. + /* paranoid */
  37841. + memset(akp, 0, aks);
  37842. + kfree(akp);
  37843. + break;
  37844. + }
  37845. +# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  37846. +# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  37847. + case AH_SHA: {
  37848. + SHA1_CTX *ictx;
  37849. + SHA1_CTX *octx;
  37850. +
  37851. + if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {
  37852. + KLIPS_PRINT(debug_pfkey,
  37853. + "ipsec_sa_init: "
  37854. + "incorrect authorisation key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37855. + ipsp->ips_key_bits_a,
  37856. + AHSHA196_KLEN * 8);
  37857. + SENDERR(EINVAL);
  37858. + }
  37859. +
  37860. +# if KLIPS_DIVULGE_HMAC_KEY
  37861. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37862. + "ipsec_sa_init: "
  37863. + "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",
  37864. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37865. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37866. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37867. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37868. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37869. + ipsp->ips_auth_bits = AHSHA196_ALEN * 8;
  37870. +
  37871. + /* save the pointer to the key material */
  37872. + akp = ipsp->ips_key_a;
  37873. + aks = ipsp->ips_key_a_size;
  37874. +
  37875. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37876. + "ipsec_sa_init: "
  37877. + "allocating %lu bytes for sha1_ctx.\n",
  37878. + (unsigned long) sizeof(struct sha1_ctx));
  37879. + if((ipsp->ips_key_a = (caddr_t)
  37880. + kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {
  37881. + ipsp->ips_key_a = akp;
  37882. + SENDERR(ENOMEM);
  37883. + }
  37884. + ipsp->ips_key_a_size = sizeof(struct sha1_ctx);
  37885. +
  37886. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37887. + kb[i] = akp[i] ^ HMAC_IPAD;
  37888. + }
  37889. + for (; i < AHMD596_BLKLEN; i++) {
  37890. + kb[i] = HMAC_IPAD;
  37891. + }
  37892. +
  37893. + ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);
  37894. + SHA1Init(ictx);
  37895. + SHA1Update(ictx, kb, AHSHA196_BLKLEN);
  37896. +
  37897. + for (i = 0; i < AHSHA196_BLKLEN; i++) {
  37898. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37899. + }
  37900. +
  37901. + octx = &((struct sha1_ctx*)(ipsp->ips_key_a))->octx;
  37902. + SHA1Init(octx);
  37903. + SHA1Update(octx, kb, AHSHA196_BLKLEN);
  37904. +
  37905. +# if KLIPS_DIVULGE_HMAC_KEY
  37906. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37907. + "ipsec_sa_init: "
  37908. + "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37909. + ((__u32*)ictx)[0],
  37910. + ((__u32*)ictx)[1],
  37911. + ((__u32*)ictx)[2],
  37912. + ((__u32*)ictx)[3],
  37913. + ((__u32*)octx)[0],
  37914. + ((__u32*)octx)[1],
  37915. + ((__u32*)octx)[2],
  37916. + ((__u32*)octx)[3] );
  37917. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37918. + memset(akp, 0, aks);
  37919. + kfree(akp);
  37920. + break;
  37921. + }
  37922. +# endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  37923. + case AH_NONE:
  37924. + break;
  37925. + default:
  37926. + KLIPS_PRINT(debug_pfkey,
  37927. + "ipsec_sa_init: "
  37928. + "authalg=%d support not available in the kernel.\n",
  37929. + ipsp->ips_authalg);
  37930. + SENDERR(EINVAL);
  37931. + }
  37932. + }
  37933. + break;
  37934. +#endif /* !CONFIG_KLIPS_ESP */
  37935. +#ifdef CONFIG_KLIPS_IPCOMP
  37936. + case IPPROTO_COMP:
  37937. + ipsp->ips_comp_adapt_tries = 0;
  37938. + ipsp->ips_comp_adapt_skip = 0;
  37939. + ipsp->ips_comp_ratio_cbytes = 0;
  37940. + ipsp->ips_comp_ratio_dbytes = 0;
  37941. + break;
  37942. +#endif /* CONFIG_KLIPS_IPCOMP */
  37943. + default:
  37944. + printk(KERN_ERR "KLIPS sa initialization: "
  37945. + "proto=%d unknown.\n",
  37946. + ipsp->ips_said.proto);
  37947. + SENDERR(EINVAL);
  37948. + }
  37949. +
  37950. + errlab:
  37951. + return(error);
  37952. +}
  37953. +
  37954. +
  37955. +
  37956. +/*
  37957. + * $Log: ipsec_sa.c,v $
  37958. + * Revision 1.30.2.2 2006/10/06 21:39:26 paul
  37959. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  37960. + * set. This is defined through autoconf.h which is included through the
  37961. + * linux kernel build macros.
  37962. + *
  37963. + * Revision 1.30.2.1 2006/04/20 16:33:07 mcr
  37964. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  37965. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  37966. + *
  37967. + * Revision 1.30 2005/05/24 01:02:35 mcr
  37968. + * some refactoring/simplification of situation where alg
  37969. + * is not found.
  37970. + *
  37971. + * Revision 1.29 2005/05/18 19:13:28 mcr
  37972. + * rename debug messages. make sure that algo not found is not
  37973. + * a debug message.
  37974. + *
  37975. + * Revision 1.28 2005/05/11 01:30:20 mcr
  37976. + * removed "poor-man"s OOP in favour of proper C structures.
  37977. + *
  37978. + * Revision 1.27 2005/04/29 05:10:22 mcr
  37979. + * removed from extraenous includes to make unit testing easier.
  37980. + *
  37981. + * Revision 1.26 2005/04/14 20:56:24 mcr
  37982. + * moved (pfkey_)ipsec_sa_init to ipsec_sa.c.
  37983. + *
  37984. + * Revision 1.25 2004/08/22 20:12:16 mcr
  37985. + * one more KLIPS_NAT->IPSEC_NAT.
  37986. + *
  37987. + * Revision 1.24 2004/07/10 19:11:18 mcr
  37988. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  37989. + *
  37990. + * Revision 1.23 2004/04/06 02:49:26 mcr
  37991. + * pullup of algo code from alg-branch.
  37992. + *
  37993. + * Revision 1.22.2.1 2003/12/22 15:25:52 jjo
  37994. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  37995. + *
  37996. + * Revision 1.22 2003/12/10 01:14:27 mcr
  37997. + * NAT-traversal patches to KLIPS.
  37998. + *
  37999. + * Revision 1.21 2003/10/31 02:27:55 mcr
  38000. + * pulled up port-selector patches and sa_id elimination.
  38001. + *
  38002. + * Revision 1.20.4.1 2003/10/29 01:30:41 mcr
  38003. + * elimited "struct sa_id".
  38004. + *
  38005. + * Revision 1.20 2003/02/06 01:50:34 rgb
  38006. + * Fixed initialisation bug for first sadb hash bucket that would only manifest itself on platforms where NULL != 0.
  38007. + *
  38008. + * Revision 1.19 2003/01/30 02:32:22 rgb
  38009. + *
  38010. + * Rename SAref table macro names for clarity.
  38011. + * Transmit error code through to caller from callee for better diagnosis of problems.
  38012. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  38013. + *
  38014. + * Revision 1.18 2002/10/12 23:11:53 dhr
  38015. + *
  38016. + * [KenB + DHR] more 64-bit cleanup
  38017. + *
  38018. + * Revision 1.17 2002/10/07 18:31:43 rgb
  38019. + * Move field width sanity checks to ipsec_sa.c
  38020. + *
  38021. + * Revision 1.16 2002/09/20 15:41:02 rgb
  38022. + * Re-wrote most of the SAref code to eliminate Entry pointers.
  38023. + * Added SAref code compiler directive switch.
  38024. + * Added a saref test function for testing macros.
  38025. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  38026. + * Split ipsec_sadb_cleanup from new funciton ipsec_sadb_free to avoid problem
  38027. + * of freeing newly created structures when clearing the reftable upon startup
  38028. + * to start from a known state.
  38029. + * Place all ipsec sadb globals into one struct.
  38030. + * Rework saref freelist.
  38031. + * Added memory allocation debugging.
  38032. + *
  38033. + * Revision 1.15 2002/09/20 05:01:44 rgb
  38034. + * Update copyright date.
  38035. + *
  38036. + * Revision 1.14 2002/08/13 19:01:25 mcr
  38037. + * patches from kenb to permit compilation of FreeSWAN on ia64.
  38038. + * des library patched to use proper DES_LONG type for ia64.
  38039. + *
  38040. + * Revision 1.13 2002/07/29 03:06:20 mcr
  38041. + * get rid of variable not used warnings.
  38042. + *
  38043. + * Revision 1.12 2002/07/26 08:48:31 rgb
  38044. + * Added SA ref table code.
  38045. + *
  38046. + * Revision 1.11 2002/06/04 16:48:49 rgb
  38047. + * Tidied up pointer code for processor independance.
  38048. + *
  38049. + * Revision 1.10 2002/05/23 07:16:17 rgb
  38050. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  38051. + * Pointer clean-up.
  38052. + * Added refcount code.
  38053. + * Convert "usecount" to "refcount" to remove ambiguity.
  38054. + *
  38055. + * Revision 1.9 2002/05/14 02:34:49 rgb
  38056. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  38057. + * with "put" usage in the kernel.
  38058. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  38059. + * ipsec_sa or ipsec_sa.
  38060. + * Added some preliminary refcount code.
  38061. + *
  38062. + * Revision 1.8 2002/04/24 07:55:32 mcr
  38063. + * #include patches and Makefiles for post-reorg compilation.
  38064. + *
  38065. + * Revision 1.7 2002/04/24 07:36:30 mcr
  38066. + * Moved from ./klips/net/ipsec/ipsec_sa.c,v
  38067. + *
  38068. + * Revision 1.6 2002/04/20 00:12:25 rgb
  38069. + * Added esp IV CBC attack fix, disabled.
  38070. + *
  38071. + * Revision 1.5 2002/01/29 17:17:56 mcr
  38072. + * moved include of ipsec_param.h to after include of linux/kernel.h
  38073. + * otherwise, it seems that some option that is set in ipsec_param.h
  38074. + * screws up something subtle in the include path to kernel.h, and
  38075. + * it complains on the snprintf() prototype.
  38076. + *
  38077. + * Revision 1.4 2002/01/29 04:00:52 mcr
  38078. + * more excise of kversions.h header.
  38079. + *
  38080. + * Revision 1.3 2002/01/29 02:13:18 mcr
  38081. + * introduction of ipsec_kversion.h means that include of
  38082. + * ipsec_param.h must preceed any decisions about what files to
  38083. + * include to deal with differences in kernel source.
  38084. + *
  38085. + * Revision 1.2 2001/11/26 09:16:15 rgb
  38086. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  38087. + *
  38088. + * Revision 1.1.2.2 2001/10/22 21:05:41 mcr
  38089. + * removed phony prototype for des_set_key.
  38090. + *
  38091. + * Revision 1.1.2.1 2001/09/25 02:24:57 mcr
  38092. + * struct tdb -> struct ipsec_sa.
  38093. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  38094. + * ipsec_xform.c removed. header file still contains useful things.
  38095. + *
  38096. + *
  38097. + *
  38098. + * CLONED from ipsec_xform.c:
  38099. + * Revision 1.53 2001/09/08 21:13:34 rgb
  38100. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  38101. + *
  38102. + * Revision 1.52 2001/06/14 19:35:11 rgb
  38103. + * Update copyright date.
  38104. + *
  38105. + * Revision 1.51 2001/05/30 08:14:03 rgb
  38106. + * Removed vestiges of esp-null transforms.
  38107. + *
  38108. + * Revision 1.50 2001/05/03 19:43:18 rgb
  38109. + * Initialise error return variable.
  38110. + * Update SENDERR macro.
  38111. + * Fix sign of error return code for ipsec_tdbcleanup().
  38112. + * Use more appropriate return code for ipsec_tdbwipe().
  38113. + *
  38114. + * Revision 1.49 2001/04/19 18:56:17 rgb
  38115. + * Fixed tdb table locking comments.
  38116. + *
  38117. + * Revision 1.48 2001/02/27 22:24:55 rgb
  38118. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  38119. + * Check for satoa() return codes.
  38120. + *
  38121. + * Revision 1.47 2000/11/06 04:32:08 rgb
  38122. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  38123. + *
  38124. + * Revision 1.46 2000/09/20 16:21:57 rgb
  38125. + * Cleaned up ident string alloc/free.
  38126. + *
  38127. + * Revision 1.45 2000/09/08 19:16:51 rgb
  38128. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  38129. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  38130. + *
  38131. + * Revision 1.44 2000/08/30 05:29:04 rgb
  38132. + * Compiler-define out no longer used tdb_init() in ipsec_xform.c.
  38133. + *
  38134. + * Revision 1.43 2000/08/18 21:30:41 rgb
  38135. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  38136. + *
  38137. + * Revision 1.42 2000/08/01 14:51:51 rgb
  38138. + * Removed _all_ remaining traces of DES.
  38139. + *
  38140. + * Revision 1.41 2000/07/28 14:58:31 rgb
  38141. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  38142. + *
  38143. + * Revision 1.40 2000/06/28 05:50:11 rgb
  38144. + * Actually set iv_bits.
  38145. + *
  38146. + * Revision 1.39 2000/05/10 23:11:09 rgb
  38147. + * Added netlink debugging output.
  38148. + * Added a cast to quiet down the ntohl bug.
  38149. + *
  38150. + * Revision 1.38 2000/05/10 19:18:42 rgb
  38151. + * Cast output of ntohl so that the broken prototype doesn't make our
  38152. + * compile noisy.
  38153. + *
  38154. + * Revision 1.37 2000/03/16 14:04:59 rgb
  38155. + * Hardwired CONFIG_IPSEC_PFKEYv2 on.
  38156. + *
  38157. + * Revision 1.36 2000/01/26 10:11:28 rgb
  38158. + * Fixed spacing in error text causing run-in words.
  38159. + *
  38160. + * Revision 1.35 2000/01/21 06:17:16 rgb
  38161. + * Tidied up compiler directive indentation for readability.
  38162. + * Added ictx,octx vars for simplification.(kravietz)
  38163. + * Added macros for HMAC padding magic numbers.(kravietz)
  38164. + * Fixed missing key length reporting bug.
  38165. + * Fixed bug in tdbwipe to return immediately on NULL tdbp passed in.
  38166. + *
  38167. + * Revision 1.34 1999/12/08 00:04:19 rgb
  38168. + * Fixed SA direction overwriting bug for netlink users.
  38169. + *
  38170. + * Revision 1.33 1999/12/01 22:16:44 rgb
  38171. + * Minor formatting changes in ESP MD5 initialisation.
  38172. + *
  38173. + * Revision 1.32 1999/11/25 09:06:36 rgb
  38174. + * Fixed error return messages, should be returning negative numbers.
  38175. + * Implemented SENDERR macro for propagating error codes.
  38176. + * Added debug message and separate error code for algorithms not compiled
  38177. + * in.
  38178. + *
  38179. + * Revision 1.31 1999/11/23 23:06:26 rgb
  38180. + * Sort out pfkey and freeswan headers, putting them in a library path.
  38181. + *
  38182. + * Revision 1.30 1999/11/18 04:09:20 rgb
  38183. + * Replaced all kernel version macros to shorter, readable form.
  38184. + *
  38185. + * Revision 1.29 1999/11/17 15:53:40 rgb
  38186. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  38187. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  38188. + * klips/net/ipsec/Makefile.
  38189. + *
  38190. + * Revision 1.28 1999/10/18 20:04:01 rgb
  38191. + * Clean-out unused cruft.
  38192. + *
  38193. + * Revision 1.27 1999/10/03 19:01:03 rgb
  38194. + * Spinlock support for 2.3.xx and 2.0.xx kernels.
  38195. + *
  38196. + * Revision 1.26 1999/10/01 16:22:24 rgb
  38197. + * Switch from assignment init. to functional init. of spinlocks.
  38198. + *
  38199. + * Revision 1.25 1999/10/01 15:44:54 rgb
  38200. + * Move spinlock header include to 2.1> scope.
  38201. + *
  38202. + * Revision 1.24 1999/10/01 00:03:46 rgb
  38203. + * Added tdb structure locking.
  38204. + * Minor formatting changes.
  38205. + * Add function to initialize tdb hash table.
  38206. + *
  38207. + * Revision 1.23 1999/05/25 22:42:12 rgb
  38208. + * Add deltdbchain() debugging.
  38209. + *
  38210. + * Revision 1.22 1999/05/25 21:24:31 rgb
  38211. + * Add debugging statements to deltdbchain().
  38212. + *
  38213. + * Revision 1.21 1999/05/25 03:51:48 rgb
  38214. + * Refix error return code.
  38215. + *
  38216. + * Revision 1.20 1999/05/25 03:34:07 rgb
  38217. + * Fix error return for flush.
  38218. + *
  38219. + * Revision 1.19 1999/05/09 03:25:37 rgb
  38220. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  38221. + *
  38222. + * Revision 1.18 1999/05/05 22:02:32 rgb
  38223. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  38224. + *
  38225. + * Revision 1.17 1999/04/29 15:20:16 rgb
  38226. + * Change gettdb parameter to a pointer to reduce stack loading and
  38227. + * facilitate parameter sanity checking.
  38228. + * Add sanity checking for null pointer arguments.
  38229. + * Add debugging instrumentation.
  38230. + * Add function deltdbchain() which will take care of unlinking,
  38231. + * zeroing and deleting a chain of tdbs.
  38232. + * Add a parameter to tdbcleanup to be able to delete a class of SAs.
  38233. + * tdbwipe now actually zeroes the tdb as well as any of its pointed
  38234. + * structures.
  38235. + *
  38236. + * Revision 1.16 1999/04/16 15:36:29 rgb
  38237. + * Fix cut-and-paste error causing a memory leak in IPIP TDB freeing.
  38238. + *
  38239. + * Revision 1.15 1999/04/11 00:29:01 henry
  38240. + * GPL boilerplate
  38241. + *
  38242. + * Revision 1.14 1999/04/06 04:54:28 rgb
  38243. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  38244. + * patch shell fixes.
  38245. + *
  38246. + * Revision 1.13 1999/02/19 18:23:01 rgb
  38247. + * Nix debug off compile warning.
  38248. + *
  38249. + * Revision 1.12 1999/02/17 16:52:16 rgb
  38250. + * Consolidate satoa()s for space and speed efficiency.
  38251. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  38252. + * Clean out unused cruft.
  38253. + * Ditch NET_IPIP dependancy.
  38254. + * Loop for 3des key setting.
  38255. + *
  38256. + * Revision 1.11 1999/01/26 02:09:05 rgb
  38257. + * Remove ah/esp/IPIP switching on include files.
  38258. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  38259. + * Removed dead code.
  38260. + * Clean up debug code when switched off.
  38261. + * Remove references to INET_GET_PROTOCOL.
  38262. + * Added code exclusion macros to reduce code from unused algorithms.
  38263. + *
  38264. + * Revision 1.10 1999/01/22 06:28:55 rgb
  38265. + * Cruft clean-out.
  38266. + * Put random IV generation in kernel.
  38267. + * Added algorithm switch code.
  38268. + * Enhanced debugging.
  38269. + * 64-bit clean-up.
  38270. + *
  38271. + * Revision 1.9 1998/11/30 13:22:55 rgb
  38272. + * Rationalised all the klips kernel file headers. They are much shorter
  38273. + * now and won't conflict under RH5.2.
  38274. + *
  38275. + * Revision 1.8 1998/11/25 04:59:06 rgb
  38276. + * Add conditionals for no IPIP tunnel code.
  38277. + * Delete commented out code.
  38278. + *
  38279. + * Revision 1.7 1998/10/31 06:50:41 rgb
  38280. + * Convert xform ASCII names to no spaces.
  38281. + * Fixed up comments in #endif directives.
  38282. + *
  38283. + * Revision 1.6 1998/10/19 14:44:28 rgb
  38284. + * Added inclusion of freeswan.h.
  38285. + * sa_id structure implemented and used: now includes protocol.
  38286. + *
  38287. + * Revision 1.5 1998/10/09 04:32:19 rgb
  38288. + * Added 'klips_debug' prefix to all klips printk debug statements.
  38289. + *
  38290. + * Revision 1.4 1998/08/12 00:11:31 rgb
  38291. + * Added new xform functions to the xform table.
  38292. + * Fixed minor debug output spelling error.
  38293. + *
  38294. + * Revision 1.3 1998/07/09 17:45:31 rgb
  38295. + * Clarify algorithm not available message.
  38296. + *
  38297. + * Revision 1.2 1998/06/23 03:00:51 rgb
  38298. + * Check for presence of IPIP protocol if it is setup one way (we don't
  38299. + * know what has been set up the other way and can only assume it will be
  38300. + * symmetrical with the exception of keys).
  38301. + *
  38302. + * Revision 1.1 1998/06/18 21:27:51 henry
  38303. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  38304. + * kernel-build scripts happier in the presence of symlinks
  38305. + *
  38306. + * Revision 1.3 1998/06/11 05:54:59 rgb
  38307. + * Added transform version string pointer to xformsw initialisations.
  38308. + *
  38309. + * Revision 1.2 1998/04/21 21:28:57 rgb
  38310. + * Rearrange debug switches to change on the fly debug output from user
  38311. + * space. Only kernel changes checked in at this time. radij.c was also
  38312. + * changed to temporarily remove buggy debugging code in rj_delete causing
  38313. + * an OOPS and hence, netlink device open errors.
  38314. + *
  38315. + * Revision 1.1 1998/04/09 03:06:13 henry
  38316. + * sources moved up from linux/net/ipsec
  38317. + *
  38318. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  38319. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  38320. + *
  38321. + * Revision 0.5 1997/06/03 04:24:48 ji
  38322. + * Added ESP-3DES-MD5-96
  38323. + *
  38324. + * Revision 0.4 1997/01/15 01:28:15 ji
  38325. + * Added new transforms.
  38326. + *
  38327. + * Revision 0.3 1996/11/20 14:39:04 ji
  38328. + * Minor cleanups.
  38329. + * Rationalized debugging code.
  38330. + *
  38331. + * Revision 0.2 1996/11/02 00:18:33 ji
  38332. + * First limited release.
  38333. + *
  38334. + *
  38335. + */
  38336. --- /dev/null Tue Mar 11 13:02:56 2003
  38337. +++ linux/net/ipsec/ipsec_sha1.c Mon Feb 9 13:51:03 2004
  38338. @@ -0,0 +1,219 @@
  38339. +/*
  38340. + * RCSID $Id: ipsec_sha1.c,v 1.9 2004/04/06 02:49:26 mcr Exp $
  38341. + */
  38342. +
  38343. +/*
  38344. + * The rest of the code is derived from sha1.c by Steve Reid, which is
  38345. + * public domain.
  38346. + * Minor cosmetic changes to accomodate it in the Linux kernel by ji.
  38347. + */
  38348. +
  38349. +#include <asm/byteorder.h>
  38350. +#include <linux/string.h>
  38351. +
  38352. +#include "openswan/ipsec_sha1.h"
  38353. +
  38354. +#if defined(rol)
  38355. +#undef rol
  38356. +#endif
  38357. +
  38358. +#define SHA1HANDSOFF
  38359. +
  38360. +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  38361. +
  38362. +/* blk0() and blk() perform the initial expand. */
  38363. +/* I got the idea of expanding during the round function from SSLeay */
  38364. +#ifdef __LITTLE_ENDIAN
  38365. +#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  38366. + |(rol(block->l[i],8)&0x00FF00FF))
  38367. +#else
  38368. +#define blk0(i) block->l[i]
  38369. +#endif
  38370. +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  38371. + ^block->l[(i+2)&15]^block->l[i&15],1))
  38372. +
  38373. +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  38374. +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38375. +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38376. +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  38377. +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  38378. +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  38379. +
  38380. +
  38381. +/* Hash a single 512-bit block. This is the core of the algorithm. */
  38382. +
  38383. +void SHA1Transform(__u32 state[5], __u8 buffer[64])
  38384. +{
  38385. +__u32 a, b, c, d, e;
  38386. +typedef union {
  38387. + unsigned char c[64];
  38388. + __u32 l[16];
  38389. +} CHAR64LONG16;
  38390. +CHAR64LONG16* block;
  38391. +#ifdef SHA1HANDSOFF
  38392. +static unsigned char workspace[64];
  38393. + block = (CHAR64LONG16*)workspace;
  38394. + memcpy(block, buffer, 64);
  38395. +#else
  38396. + block = (CHAR64LONG16*)buffer;
  38397. +#endif
  38398. + /* Copy context->state[] to working vars */
  38399. + a = state[0];
  38400. + b = state[1];
  38401. + c = state[2];
  38402. + d = state[3];
  38403. + e = state[4];
  38404. + /* 4 rounds of 20 operations each. Loop unrolled. */
  38405. + 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);
  38406. + 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);
  38407. + 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);
  38408. + 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);
  38409. + 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);
  38410. + 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);
  38411. + 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);
  38412. + 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);
  38413. + 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);
  38414. + 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);
  38415. + 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);
  38416. + 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);
  38417. + 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);
  38418. + 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);
  38419. + 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);
  38420. + 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);
  38421. + 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);
  38422. + 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);
  38423. + 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);
  38424. + 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);
  38425. + /* Add the working vars back into context.state[] */
  38426. + state[0] += a;
  38427. + state[1] += b;
  38428. + state[2] += c;
  38429. + state[3] += d;
  38430. + state[4] += e;
  38431. + /* Wipe variables */
  38432. + a = b = c = d = e = 0;
  38433. +}
  38434. +
  38435. +
  38436. +/* SHA1Init - Initialize new context */
  38437. +
  38438. +void SHA1Init(void *vcontext)
  38439. +{
  38440. + SHA1_CTX* context = vcontext;
  38441. +
  38442. + /* SHA1 initialization constants */
  38443. + context->state[0] = 0x67452301;
  38444. + context->state[1] = 0xEFCDAB89;
  38445. + context->state[2] = 0x98BADCFE;
  38446. + context->state[3] = 0x10325476;
  38447. + context->state[4] = 0xC3D2E1F0;
  38448. + context->count[0] = context->count[1] = 0;
  38449. +}
  38450. +
  38451. +
  38452. +/* Run your data through this. */
  38453. +
  38454. +void SHA1Update(void *vcontext, unsigned char* data, __u32 len)
  38455. +{
  38456. + SHA1_CTX* context = vcontext;
  38457. + __u32 i, j;
  38458. +
  38459. + j = context->count[0];
  38460. + if ((context->count[0] += len << 3) < j)
  38461. + context->count[1]++;
  38462. + context->count[1] += (len>>29);
  38463. + j = (j >> 3) & 63;
  38464. + if ((j + len) > 63) {
  38465. + memcpy(&context->buffer[j], data, (i = 64-j));
  38466. + SHA1Transform(context->state, context->buffer);
  38467. + for ( ; i + 63 < len; i += 64) {
  38468. + SHA1Transform(context->state, &data[i]);
  38469. + }
  38470. + j = 0;
  38471. + }
  38472. + else i = 0;
  38473. + memcpy(&context->buffer[j], &data[i], len - i);
  38474. +}
  38475. +
  38476. +
  38477. +/* Add padding and return the message digest. */
  38478. +
  38479. +void SHA1Final(unsigned char digest[20], void *vcontext)
  38480. +{
  38481. + __u32 i, j;
  38482. + unsigned char finalcount[8];
  38483. + SHA1_CTX* context = vcontext;
  38484. +
  38485. + for (i = 0; i < 8; i++) {
  38486. + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  38487. + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  38488. + }
  38489. + SHA1Update(context, (unsigned char *)"\200", 1);
  38490. + while ((context->count[0] & 504) != 448) {
  38491. + SHA1Update(context, (unsigned char *)"\0", 1);
  38492. + }
  38493. + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
  38494. + for (i = 0; i < 20; i++) {
  38495. + digest[i] = (unsigned char)
  38496. + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
  38497. + }
  38498. + /* Wipe variables */
  38499. + i = j = 0;
  38500. + memset(context->buffer, 0, 64);
  38501. + memset(context->state, 0, 20);
  38502. + memset(context->count, 0, 8);
  38503. + memset(&finalcount, 0, 8);
  38504. +#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
  38505. + SHA1Transform(context->state, context->buffer);
  38506. +#endif
  38507. +}
  38508. +
  38509. +
  38510. +/*
  38511. + * $Log: ipsec_sha1.c,v $
  38512. + * Revision 1.9 2004/04/06 02:49:26 mcr
  38513. + * pullup of algo code from alg-branch.
  38514. + *
  38515. + * Revision 1.8 2002/09/10 01:45:14 mcr
  38516. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  38517. + * the function prototypes would match, and could be placed
  38518. + * into a pointer to a function.
  38519. + *
  38520. + * Revision 1.7 2002/04/24 07:55:32 mcr
  38521. + * #include patches and Makefiles for post-reorg compilation.
  38522. + *
  38523. + * Revision 1.6 2002/04/24 07:36:30 mcr
  38524. + * Moved from ./klips/net/ipsec/ipsec_sha1.c,v
  38525. + *
  38526. + * Revision 1.5 1999/12/13 13:59:13 rgb
  38527. + * Quick fix to argument size to Update bugs.
  38528. + *
  38529. + * Revision 1.4 1999/04/11 00:29:00 henry
  38530. + * GPL boilerplate
  38531. + *
  38532. + * Revision 1.3 1999/04/06 04:54:27 rgb
  38533. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  38534. + * patch shell fixes.
  38535. + *
  38536. + * Revision 1.2 1999/01/22 06:55:50 rgb
  38537. + * 64-bit clean-up.
  38538. + *
  38539. + * Revision 1.1 1998/06/18 21:27:50 henry
  38540. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  38541. + * kernel-build scripts happier in the presence of symlinks
  38542. + *
  38543. + * Revision 1.2 1998/04/23 20:54:04 rgb
  38544. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  38545. + * verified.
  38546. + *
  38547. + * Revision 1.1 1998/04/09 03:06:11 henry
  38548. + * sources moved up from linux/net/ipsec
  38549. + *
  38550. + * Revision 1.1.1.1 1998/04/08 05:35:05 henry
  38551. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  38552. + *
  38553. + * Revision 0.4 1997/01/15 01:28:15 ji
  38554. + * New transform
  38555. + *
  38556. + *
  38557. + */
  38558. --- /dev/null Tue Mar 11 13:02:56 2003
  38559. +++ linux/net/ipsec/ipsec_snprintf.c Mon Feb 9 13:51:03 2004
  38560. @@ -0,0 +1,135 @@
  38561. +/*
  38562. + * @(#) ipsec_snprintf() function
  38563. + *
  38564. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  38565. + * 2001 Michael Richardson <mcr@freeswan.org>
  38566. + * Copyright (C) 2005 Michael Richardson <mcr@xelerance.com>
  38567. + *
  38568. + * This program is free software; you can redistribute it and/or modify it
  38569. + * under the terms of the GNU General Public License as published by the
  38570. + * Free Software Foundation; either version 2 of the License, or (at your
  38571. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  38572. + *
  38573. + * This program is distributed in the hope that it will be useful, but
  38574. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  38575. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  38576. + * for more details.
  38577. + *
  38578. + * Split out from ipsec_proc.c.
  38579. + */
  38580. +
  38581. +#ifndef AUTOCONF_INCLUDED
  38582. +#include <linux/config.h>
  38583. +#endif
  38584. +#include <linux/version.h>
  38585. +#define __NO_VERSION__
  38586. +#include <linux/module.h>
  38587. +#include <linux/kernel.h> /* printk() */
  38588. +
  38589. +#include "openswan/ipsec_kversion.h"
  38590. +#include "openswan/ipsec_param.h"
  38591. +
  38592. +#include <net/ip.h>
  38593. +
  38594. +#include "openswan/radij.h"
  38595. +
  38596. +#include "openswan/ipsec_life.h"
  38597. +#include "openswan/ipsec_stats.h"
  38598. +#include "openswan/ipsec_sa.h"
  38599. +
  38600. +#include "openswan/ipsec_encap.h"
  38601. +#include "openswan/ipsec_radij.h"
  38602. +#include "openswan/ipsec_xform.h"
  38603. +#include "openswan/ipsec_tunnel.h"
  38604. +#include "openswan/ipsec_xmit.h"
  38605. +
  38606. +#include "openswan/ipsec_rcv.h"
  38607. +#include "openswan/ipsec_ah.h"
  38608. +#include "openswan/ipsec_esp.h"
  38609. +#include "openswan/ipsec_kern24.h"
  38610. +
  38611. +#ifdef CONFIG_KLIPS_IPCOMP
  38612. +#include "openswan/ipcomp.h"
  38613. +#endif /* CONFIG_KLIPS_IPCOMP */
  38614. +
  38615. +#include "openswan/ipsec_proto.h"
  38616. +
  38617. +#include <pfkeyv2.h>
  38618. +#include <pfkey.h>
  38619. +
  38620. +/* ipsec_snprintf: like snprintf except
  38621. + * - size is signed and a negative value is treated as if it were 0
  38622. + * - the returned result is never negative --
  38623. + * an error generates a "?" or null output (depending on space).
  38624. + * (Our callers are too lazy to check for an error return.)
  38625. + *
  38626. + * @param buf String buffer
  38627. + * @param size Size of the string
  38628. + * @param fmt printf string
  38629. + * @param ... Variables to be displayed in fmt
  38630. + * @return int Return code
  38631. + */
  38632. +int ipsec_snprintf(char *buf, ssize_t size, const char *fmt, ...)
  38633. +{
  38634. + va_list args;
  38635. + int i;
  38636. + size_t possize = size < 0? 0 : size;
  38637. + va_start(args, fmt);
  38638. + i = vsnprintf(buf,possize,fmt,args);
  38639. + va_end(args);
  38640. + if (i < 0) {
  38641. + /* create empty output in place of error */
  38642. + i = 0;
  38643. + if (size > 0) {
  38644. + *buf = '\0';
  38645. + }
  38646. + }
  38647. + return i;
  38648. +}
  38649. +
  38650. +
  38651. +void ipsec_dmp_block(char *s, caddr_t bb, int len)
  38652. +{
  38653. + int i;
  38654. + unsigned char *b = bb;
  38655. +
  38656. + printk(KERN_INFO "klips_dmp: "
  38657. + "at %s, len=%d:\n", s, len);
  38658. +
  38659. + for(i = 0; i < len; i++ /*, c++*/) {
  38660. + if(!(i % 16)) {
  38661. + printk(KERN_INFO
  38662. + "klips_debug: @%03x:",
  38663. + i);
  38664. + }
  38665. + printk(" %02x", b[i]);
  38666. + if(!((i + 1) % 16)) {
  38667. + printk("\n");
  38668. + }
  38669. + }
  38670. + if(i % 16) {
  38671. + printk("\n");
  38672. + }
  38673. +}
  38674. +
  38675. +/*
  38676. + *
  38677. + * $Log: ipsec_snprintf.c,v $
  38678. + * Revision 1.3.2.1 2006/10/06 21:39:26 paul
  38679. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  38680. + * set. This is defined through autoconf.h which is included through the
  38681. + * linux kernel build macros.
  38682. + *
  38683. + * Revision 1.3 2005/04/29 05:10:22 mcr
  38684. + * removed from extraenous includes to make unit testing easier.
  38685. + *
  38686. + * Revision 1.2 2005/04/15 00:32:01 mcr
  38687. + * added ipsec_dmp_block routine.
  38688. + *
  38689. + *
  38690. + * Local Variables:
  38691. + * c-file-style: "linux"
  38692. + * End:
  38693. + *
  38694. + */
  38695. +
  38696. --- /dev/null Tue Mar 11 13:02:56 2003
  38697. +++ linux/net/ipsec/ipsec_tunnel.c Mon Feb 9 13:51:03 2004
  38698. @@ -0,0 +1,2878 @@
  38699. +/*
  38700. + * IPSEC Tunneling code. Heavily based on drivers/net/new_tunnel.c
  38701. + * Copyright (C) 1996, 1997 John Ioannidis.
  38702. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  38703. + *
  38704. + * This program is free software; you can redistribute it and/or modify it
  38705. + * under the terms of the GNU General Public License as published by the
  38706. + * Free Software Foundation; either version 2 of the License, or (at your
  38707. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  38708. + *
  38709. + * This program is distributed in the hope that it will be useful, but
  38710. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  38711. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  38712. + * for more details.
  38713. + */
  38714. +
  38715. +char ipsec_tunnel_c_version[] = "RCSID $Id: ipsec_tunnel.c,v 1.232.2.5 2006/10/06 21:39:26 paul Exp $";
  38716. +
  38717. +#define __NO_VERSION__
  38718. +#include <linux/module.h>
  38719. +#ifndef AUTOCONF_INCLUDED
  38720. +#include <linux/config.h>
  38721. +#endif /* for CONFIG_IP_FORWARD */
  38722. +#include <linux/version.h>
  38723. +#include <linux/kernel.h> /* printk() */
  38724. +
  38725. +#include "openswan/ipsec_param.h"
  38726. +
  38727. +#ifdef MALLOC_SLAB
  38728. +# include <linux/slab.h> /* kmalloc() */
  38729. +#else /* MALLOC_SLAB */
  38730. +# include <linux/malloc.h> /* kmalloc() */
  38731. +#endif /* MALLOC_SLAB */
  38732. +#include <linux/errno.h> /* error codes */
  38733. +#include <linux/types.h> /* size_t */
  38734. +#include <linux/interrupt.h> /* mark_bh */
  38735. +
  38736. +#include <net/tcp.h>
  38737. +#include <net/udp.h>
  38738. +#include <linux/skbuff.h>
  38739. +
  38740. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  38741. +#include <linux/etherdevice.h> /* eth_type_trans */
  38742. +#include <linux/ip.h> /* struct iphdr */
  38743. +#include <linux/skbuff.h>
  38744. +
  38745. +#include <openswan.h>
  38746. +
  38747. +#ifdef NET_21
  38748. +# include <linux/in6.h>
  38749. +# define ip_chk_addr inet_addr_type
  38750. +# define IS_MYADDR RTN_LOCAL
  38751. +# include <net/dst.h>
  38752. +# undef dev_kfree_skb
  38753. +# define dev_kfree_skb(a,b) kfree_skb(a)
  38754. +# define PHYSDEV_TYPE
  38755. +#endif /* NET_21 */
  38756. +
  38757. +#include <net/icmp.h> /* icmp_send() */
  38758. +#include <net/ip.h>
  38759. +#ifdef NETDEV_23
  38760. +# include <linux/netfilter_ipv4.h>
  38761. +#endif /* NETDEV_23 */
  38762. +
  38763. +#include <linux/if_arp.h>
  38764. +#include <net/arp.h>
  38765. +
  38766. +#include "openswan/ipsec_kversion.h"
  38767. +#include "openswan/radij.h"
  38768. +#include "openswan/ipsec_life.h"
  38769. +#include "openswan/ipsec_xform.h"
  38770. +#include "openswan/ipsec_eroute.h"
  38771. +#include "openswan/ipsec_encap.h"
  38772. +#include "openswan/ipsec_radij.h"
  38773. +#include "openswan/ipsec_sa.h"
  38774. +#include "openswan/ipsec_tunnel.h"
  38775. +#include "openswan/ipsec_xmit.h"
  38776. +#include "openswan/ipsec_ipe4.h"
  38777. +#include "openswan/ipsec_ah.h"
  38778. +#include "openswan/ipsec_esp.h"
  38779. +#include "openswan/ipsec_kern24.h"
  38780. +
  38781. +#include <pfkeyv2.h>
  38782. +#include <pfkey.h>
  38783. +
  38784. +#include "openswan/ipsec_proto.h"
  38785. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  38786. +#include <linux/udp.h>
  38787. +#endif
  38788. +
  38789. +static __u32 zeroes[64];
  38790. +
  38791. +#ifdef CONFIG_KLIPS_DEBUG
  38792. +int debug_tunnel = 0;
  38793. +#endif /* CONFIG_KLIPS_DEBUG */
  38794. +
  38795. +DEBUG_NO_STATIC int
  38796. +ipsec_tunnel_open(struct net_device *dev)
  38797. +{
  38798. + struct ipsecpriv *prv = dev->priv;
  38799. +
  38800. + /*
  38801. + * Can't open until attached.
  38802. + */
  38803. +
  38804. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  38805. + "klips_debug:ipsec_tunnel_open: "
  38806. + "dev = %s, prv->dev = %s\n",
  38807. + dev->name, prv->dev?prv->dev->name:"NONE");
  38808. +
  38809. + if (prv->dev == NULL)
  38810. + return -ENODEV;
  38811. +
  38812. + KLIPS_INC_USE;
  38813. + return 0;
  38814. +}
  38815. +
  38816. +DEBUG_NO_STATIC int
  38817. +ipsec_tunnel_close(struct net_device *dev)
  38818. +{
  38819. + KLIPS_DEC_USE;
  38820. + return 0;
  38821. +}
  38822. +
  38823. +#ifdef NETDEV_23
  38824. +static inline int ipsec_tunnel_xmit2(struct sk_buff *skb)
  38825. +{
  38826. +#ifdef NETDEV_25 /* 2.6 kernels */
  38827. + return dst_output(skb);
  38828. +#else
  38829. + return ip_send(skb);
  38830. +#endif
  38831. +}
  38832. +#endif /* NETDEV_23 */
  38833. +
  38834. +enum ipsec_xmit_value
  38835. +ipsec_tunnel_strip_hard_header(struct ipsec_xmit_state *ixs)
  38836. +{
  38837. + /* ixs->physdev->hard_header_len is unreliable and should not be used */
  38838. + ixs->hard_header_len = (unsigned char *)(ixs->iph) - ixs->skb->data;
  38839. +
  38840. + if(ixs->hard_header_len < 0) {
  38841. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38842. + "klips_error:ipsec_xmit_strip_hard_header: "
  38843. + "Negative hard_header_len (%d)?!\n", ixs->hard_header_len);
  38844. + ixs->stats->tx_dropped++;
  38845. + return IPSEC_XMIT_BADHHLEN;
  38846. + }
  38847. +
  38848. + /* while ixs->physdev->hard_header_len is unreliable and
  38849. + * should not be trusted, it accurate and required for ATM, GRE and
  38850. + * some other interfaces to work. Thanks to Willy Tarreau
  38851. + * <willy@w.ods.org>.
  38852. + */
  38853. + if(ixs->hard_header_len == 0) { /* no hard header present */
  38854. + ixs->hard_header_stripped = 1;
  38855. + ixs->hard_header_len = ixs->physdev->hard_header_len;
  38856. + }
  38857. +
  38858. +#ifdef CONFIG_KLIPS_DEBUG
  38859. + if (debug_tunnel & DB_TN_XMIT) {
  38860. + int i;
  38861. + char c;
  38862. +
  38863. + printk(KERN_INFO "klips_debug:ipsec_xmit_strip_hard_header: "
  38864. + ">>> skb->len=%ld hard_header_len:%d",
  38865. + (unsigned long int)ixs->skb->len, ixs->hard_header_len);
  38866. + c = ' ';
  38867. + for (i=0; i < ixs->hard_header_len; i++) {
  38868. + printk("%c%02x", c, ixs->skb->data[i]);
  38869. + c = ':';
  38870. + }
  38871. + printk(" \n");
  38872. + }
  38873. +#endif /* CONFIG_KLIPS_DEBUG */
  38874. +
  38875. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->iph);
  38876. +
  38877. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  38878. + "klips_debug:ipsec_xmit_strip_hard_header: "
  38879. + "Original head,tailroom: %d,%d\n",
  38880. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  38881. +
  38882. + return IPSEC_XMIT_OK;
  38883. +}
  38884. +
  38885. +enum ipsec_xmit_value
  38886. +ipsec_tunnel_SAlookup(struct ipsec_xmit_state *ixs)
  38887. +{
  38888. + unsigned int bypass;
  38889. +
  38890. + bypass = FALSE;
  38891. +
  38892. + /*
  38893. + * First things first -- look us up in the erouting tables.
  38894. + */
  38895. + ixs->matcher.sen_len = sizeof (struct sockaddr_encap);
  38896. + ixs->matcher.sen_family = AF_ENCAP;
  38897. + ixs->matcher.sen_type = SENT_IP4;
  38898. + ixs->matcher.sen_ip_src.s_addr = ixs->iph->saddr;
  38899. + ixs->matcher.sen_ip_dst.s_addr = ixs->iph->daddr;
  38900. + ixs->matcher.sen_proto = ixs->iph->protocol;
  38901. + ipsec_extract_ports(ixs->iph, &ixs->matcher);
  38902. +
  38903. + /*
  38904. + * The spinlock is to prevent any other process from accessing or deleting
  38905. + * the eroute while we are using and updating it.
  38906. + */
  38907. + spin_lock(&eroute_lock);
  38908. +
  38909. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  38910. +
  38911. + if(ixs->iph->protocol == IPPROTO_UDP) {
  38912. + struct udphdr *t = NULL;
  38913. +
  38914. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38915. + "klips_debug:udp port check: "
  38916. + "fragoff: %d len: %d>%ld \n",
  38917. + ntohs(ixs->iph->frag_off) & IP_OFFSET,
  38918. + (ixs->skb->len - ixs->hard_header_len),
  38919. + (unsigned long int) ((ixs->iph->ihl << 2) + sizeof(struct udphdr)));
  38920. +
  38921. + if((ntohs(ixs->iph->frag_off) & IP_OFFSET) == 0 &&
  38922. + ((ixs->skb->len - ixs->hard_header_len) >=
  38923. + ((ixs->iph->ihl << 2) + sizeof(struct udphdr))))
  38924. + {
  38925. + t =((struct udphdr*)((caddr_t)ixs->iph+(ixs->iph->ihl<<2)));
  38926. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38927. + "klips_debug:udp port in packet: "
  38928. + "port %d -> %d\n",
  38929. + ntohs(t->source), ntohs(t->dest));
  38930. + }
  38931. +
  38932. + ixs->sport=0; ixs->dport=0;
  38933. +
  38934. + if(ixs->skb->sk) {
  38935. +#ifdef NET_26
  38936. + struct udp_sock *us;
  38937. +
  38938. + us = (struct udp_sock *)ixs->skb->sk;
  38939. +
  38940. + ixs->sport = ntohs(us->inet.sport);
  38941. + ixs->dport = ntohs(us->inet.dport);
  38942. +#else
  38943. + ixs->sport = ntohs(ixs->skb->sk->sport);
  38944. + ixs->dport = ntohs(ixs->skb->sk->dport);
  38945. +#endif
  38946. +
  38947. + }
  38948. +
  38949. + if(t != NULL) {
  38950. + if(ixs->sport == 0) {
  38951. + ixs->sport = ntohs(t->source);
  38952. + }
  38953. + if(ixs->dport == 0) {
  38954. + ixs->dport = ntohs(t->dest);
  38955. + }
  38956. + }
  38957. + }
  38958. +
  38959. + /*
  38960. + * practically identical to above, but let's be careful about
  38961. + * tcp vs udp headers
  38962. + */
  38963. + if(ixs->iph->protocol == IPPROTO_TCP) {
  38964. + struct tcphdr *t = NULL;
  38965. +
  38966. + if((ntohs(ixs->iph->frag_off) & IP_OFFSET) == 0 &&
  38967. + ((ixs->skb->len - ixs->hard_header_len) >=
  38968. + ((ixs->iph->ihl << 2) + sizeof(struct tcphdr)))) {
  38969. + t =((struct tcphdr*)((caddr_t)ixs->iph+(ixs->iph->ihl<<2)));
  38970. + }
  38971. +
  38972. + ixs->sport=0; ixs->dport=0;
  38973. +
  38974. + if(ixs->skb->sk) {
  38975. +#ifdef NET_26
  38976. +#ifdef HAVE_INET_SK_SPORT
  38977. + ixs->sport = ntohs(inet_sk(ixs->skb->sk)->sport);
  38978. + ixs->dport = ntohs(inet_sk(ixs->skb->sk)->dport);
  38979. +#else
  38980. + struct tcp_tw_bucket *tw;
  38981. +
  38982. + tw = (struct tcp_tw_bucket *)ixs->skb->sk;
  38983. +
  38984. + ixs->sport = ntohs(tw->tw_sport);
  38985. + ixs->dport = ntohs(tw->tw_dport);
  38986. +#endif
  38987. +#else
  38988. + ixs->sport = ntohs(ixs->skb->sk->sport);
  38989. + ixs->dport = ntohs(ixs->skb->sk->dport);
  38990. +#endif
  38991. + }
  38992. +
  38993. + if(t != NULL) {
  38994. + if(ixs->sport == 0) {
  38995. + ixs->sport = ntohs(t->source);
  38996. + }
  38997. + if(ixs->dport == 0) {
  38998. + ixs->dport = ntohs(t->dest);
  38999. + }
  39000. + }
  39001. + }
  39002. +
  39003. + /* default to a %drop eroute */
  39004. + ixs->outgoing_said.proto = IPPROTO_INT;
  39005. + ixs->outgoing_said.spi = htonl(SPI_DROP);
  39006. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr = INADDR_ANY;
  39007. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39008. + "klips_debug:ipsec_xmit_SAlookup: "
  39009. + "checking for local udp/500 IKE packet "
  39010. + "saddr=%x, er=0p%p, daddr=%x, er_dst=%x, proto=%d sport=%d dport=%d\n",
  39011. + ntohl((unsigned int)ixs->iph->saddr),
  39012. + ixs->eroute,
  39013. + ntohl((unsigned int)ixs->iph->daddr),
  39014. + ixs->eroute ? ntohl((unsigned int)ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr) : 0,
  39015. + ixs->iph->protocol,
  39016. + ixs->sport,
  39017. + ixs->dport);
  39018. +
  39019. + /*
  39020. + * cheat for now...are we udp/500? If so, let it through
  39021. + * without interference since it is most likely an IKE packet.
  39022. + */
  39023. +
  39024. + if (ip_chk_addr((unsigned long)ixs->iph->saddr) == IS_MYADDR
  39025. + && (ixs->eroute==NULL
  39026. + || ixs->iph->daddr == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr
  39027. + || INADDR_ANY == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr)
  39028. + && (ixs->iph->protocol == IPPROTO_UDP &&
  39029. + (ixs->sport == 500 || ixs->sport == 4500))) {
  39030. + /* Whatever the eroute, this is an IKE message
  39031. + * from us (i.e. not being forwarded).
  39032. + * Furthermore, if there is a tunnel eroute,
  39033. + * the destination is the peer for this eroute.
  39034. + * So %pass the packet: modify the default %drop.
  39035. + */
  39036. +
  39037. + ixs->outgoing_said.spi = htonl(SPI_PASS);
  39038. + if(!(ixs->skb->sk) && ((ntohs(ixs->iph->frag_off) & IP_MF) != 0)) {
  39039. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39040. + "klips_debug:ipsec_xmit_SAlookup: "
  39041. + "local UDP/500 (probably IKE) passthrough: base fragment, rest of fragments will probably get filtered.\n");
  39042. + }
  39043. + bypass = TRUE;
  39044. + }
  39045. +
  39046. +#ifdef KLIPS_EXCEPT_DNS53
  39047. + /*
  39048. + *
  39049. + * if we are udp/53 or tcp/53, also let it through a %trap or %hold,
  39050. + * since it is DNS, but *also* follow the %trap.
  39051. + *
  39052. + * we do not do this for tunnels, only %trap's and %hold's.
  39053. + *
  39054. + */
  39055. +
  39056. + if (ip_chk_addr((unsigned long)ixs->iph->saddr) == IS_MYADDR
  39057. + && (ixs->eroute==NULL
  39058. + || ixs->iph->daddr == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr
  39059. + || INADDR_ANY == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr)
  39060. + && ((ixs->iph->protocol == IPPROTO_UDP
  39061. + || ixs->iph->protocol == IPPROTO_TCP)
  39062. + && ixs->dport == 53)) {
  39063. +
  39064. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39065. + "klips_debug:ipsec_xmit_SAlookup: "
  39066. + "possible DNS packet\n");
  39067. +
  39068. + if(ixs->eroute)
  39069. + {
  39070. + if(ixs->eroute->er_said.spi == htonl(SPI_TRAP)
  39071. + || ixs->eroute->er_said.spi == htonl(SPI_HOLD))
  39072. + {
  39073. + ixs->outgoing_said.spi = htonl(SPI_PASSTRAP);
  39074. + bypass = TRUE;
  39075. + }
  39076. + }
  39077. + else
  39078. + {
  39079. + ixs->outgoing_said.spi = htonl(SPI_PASSTRAP);
  39080. + bypass = TRUE;
  39081. + }
  39082. +
  39083. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39084. + "klips_debug:ipsec_xmit_SAlookup: "
  39085. + "bypass = %d\n", bypass);
  39086. +
  39087. + if(bypass
  39088. + && !(ixs->skb->sk)
  39089. + && ((ntohs(ixs->iph->frag_off) & IP_MF) != 0))
  39090. + {
  39091. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39092. + "klips_debug:ipsec_xmit_SAlookup: "
  39093. + "local port 53 (probably DNS) passthrough:"
  39094. + "base fragment, rest of fragments will "
  39095. + "probably get filtered.\n");
  39096. + }
  39097. + }
  39098. +#endif
  39099. +
  39100. + if (bypass==FALSE && ixs->eroute) {
  39101. + ixs->eroute->er_count++;
  39102. + ixs->eroute->er_lasttime = jiffies/HZ;
  39103. + if(ixs->eroute->er_said.proto==IPPROTO_INT
  39104. + && ixs->eroute->er_said.spi==htonl(SPI_HOLD))
  39105. + {
  39106. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39107. + "klips_debug:ipsec_xmit_SAlookup: "
  39108. + "shunt SA of HOLD: skb stored in HOLD.\n");
  39109. + if(ixs->eroute->er_last != NULL) {
  39110. + kfree_skb(ixs->eroute->er_last);
  39111. + }
  39112. + ixs->eroute->er_last = ixs->skb;
  39113. + ixs->skb = NULL;
  39114. + ixs->stats->tx_dropped++;
  39115. + spin_unlock(&eroute_lock);
  39116. + return IPSEC_XMIT_STOLEN;
  39117. + }
  39118. + ixs->outgoing_said = ixs->eroute->er_said;
  39119. + ixs->eroute_pid = ixs->eroute->er_pid;
  39120. +
  39121. + /* Copy of the ident for the TRAP/TRAPSUBNET eroutes */
  39122. + if(ixs->outgoing_said.proto==IPPROTO_INT
  39123. + && (ixs->outgoing_said.spi==htonl(SPI_TRAP)
  39124. + || (ixs->outgoing_said.spi==htonl(SPI_TRAPSUBNET)))) {
  39125. + int len;
  39126. +
  39127. + ixs->ips.ips_ident_s.type = ixs->eroute->er_ident_s.type;
  39128. + ixs->ips.ips_ident_s.id = ixs->eroute->er_ident_s.id;
  39129. + ixs->ips.ips_ident_s.len = ixs->eroute->er_ident_s.len;
  39130. + if (ixs->ips.ips_ident_s.len)
  39131. + {
  39132. + len = ixs->ips.ips_ident_s.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  39133. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39134. + "klips_debug:ipsec_xmit_SAlookup: "
  39135. + "allocating %d bytes for ident_s shunt SA of HOLD: skb stored in HOLD.\n",
  39136. + len);
  39137. + if ((ixs->ips.ips_ident_s.data = kmalloc(len, GFP_ATOMIC)) == NULL) {
  39138. + printk(KERN_WARNING "klips_debug:ipsec_xmit_SAlookup: "
  39139. + "Failed, tried to allocate %d bytes for source ident.\n",
  39140. + len);
  39141. + ixs->stats->tx_dropped++;
  39142. + spin_unlock(&eroute_lock);
  39143. + return IPSEC_XMIT_ERRMEMALLOC;
  39144. + }
  39145. + memcpy(ixs->ips.ips_ident_s.data, ixs->eroute->er_ident_s.data, len);
  39146. + }
  39147. + ixs->ips.ips_ident_d.type = ixs->eroute->er_ident_d.type;
  39148. + ixs->ips.ips_ident_d.id = ixs->eroute->er_ident_d.id;
  39149. + ixs->ips.ips_ident_d.len = ixs->eroute->er_ident_d.len;
  39150. + if (ixs->ips.ips_ident_d.len)
  39151. + {
  39152. + len = ixs->ips.ips_ident_d.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  39153. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39154. + "klips_debug:ipsec_xmit_SAlookup: "
  39155. + "allocating %d bytes for ident_d shunt SA of HOLD: skb stored in HOLD.\n",
  39156. + len);
  39157. + if ((ixs->ips.ips_ident_d.data = kmalloc(len, GFP_ATOMIC)) == NULL) {
  39158. + printk(KERN_WARNING "klips_debug:ipsec_xmit_SAlookup: "
  39159. + "Failed, tried to allocate %d bytes for dest ident.\n",
  39160. + len);
  39161. + ixs->stats->tx_dropped++;
  39162. + spin_unlock(&eroute_lock);
  39163. + return IPSEC_XMIT_ERRMEMALLOC;
  39164. + }
  39165. + memcpy(ixs->ips.ips_ident_d.data, ixs->eroute->er_ident_d.data, len);
  39166. + }
  39167. + }
  39168. + }
  39169. +
  39170. + spin_unlock(&eroute_lock);
  39171. + return IPSEC_XMIT_OK;
  39172. +}
  39173. +
  39174. +
  39175. +enum ipsec_xmit_value
  39176. +ipsec_tunnel_restore_hard_header(struct ipsec_xmit_state*ixs)
  39177. +{
  39178. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  39179. + "klips_debug:ipsec_xmit_restore_hard_header: "
  39180. + "After recursive xforms -- head,tailroom: %d,%d\n",
  39181. + skb_headroom(ixs->skb),
  39182. + skb_tailroom(ixs->skb));
  39183. +
  39184. + if(ixs->saved_header) {
  39185. + if(skb_headroom(ixs->skb) < ixs->hard_header_len) {
  39186. + printk(KERN_WARNING
  39187. + "klips_error:ipsec_xmit_restore_hard_header: "
  39188. + "tried to skb_push hhlen=%d, %d available. This should never happen, please report.\n",
  39189. + ixs->hard_header_len,
  39190. + skb_headroom(ixs->skb));
  39191. + ixs->stats->tx_errors++;
  39192. + return IPSEC_XMIT_PUSHPULLERR;
  39193. +
  39194. + }
  39195. + skb_push(ixs->skb, ixs->hard_header_len);
  39196. + {
  39197. + int i;
  39198. + for (i = 0; i < ixs->hard_header_len; i++) {
  39199. + ixs->skb->data[i] = ixs->saved_header[i];
  39200. + }
  39201. + }
  39202. + }
  39203. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  39204. + if (ixs->natt_type && ixs->natt_head) {
  39205. + struct iphdr *ipp = ixs->skb->nh.iph;
  39206. + struct udphdr *udp;
  39207. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39208. + "klips_debug:ipsec_tunnel_start_xmit: "
  39209. + "encapsuling packet into UDP (NAT-Traversal) (%d %d)\n",
  39210. + ixs->natt_type, ixs->natt_head);
  39211. +
  39212. + ixs->iphlen = ipp->ihl << 2;
  39213. + ipp->tot_len =
  39214. + htons(ntohs(ipp->tot_len) + ixs->natt_head);
  39215. + if(skb_tailroom(ixs->skb) < ixs->natt_head) {
  39216. + printk(KERN_WARNING "klips_error:ipsec_tunnel_start_xmit: "
  39217. + "tried to skb_put %d, %d available. "
  39218. + "This should never happen, please report.\n",
  39219. + ixs->natt_head,
  39220. + skb_tailroom(ixs->skb));
  39221. + ixs->stats->tx_errors++;
  39222. + return IPSEC_XMIT_ESPUDP;
  39223. + }
  39224. + skb_put(ixs->skb, ixs->natt_head);
  39225. +
  39226. + udp = (struct udphdr *)((char *)ipp + ixs->iphlen);
  39227. +
  39228. + /* move ESP hdr after UDP hdr */
  39229. + memmove((void *)((char *)udp + ixs->natt_head),
  39230. + (void *)(udp),
  39231. + ntohs(ipp->tot_len) - ixs->iphlen - ixs->natt_head);
  39232. +
  39233. + /* clear UDP & Non-IKE Markers (if any) */
  39234. + memset(udp, 0, ixs->natt_head);
  39235. +
  39236. + /* fill UDP with usefull informations ;-) */
  39237. + udp->source = htons(ixs->natt_sport);
  39238. + udp->dest = htons(ixs->natt_dport);
  39239. + udp->len = htons(ntohs(ipp->tot_len) - ixs->iphlen);
  39240. +
  39241. + /* set protocol */
  39242. + ipp->protocol = IPPROTO_UDP;
  39243. +
  39244. + /* fix IP checksum */
  39245. + ipp->check = 0;
  39246. + ipp->check = ip_fast_csum((unsigned char *)ipp, ipp->ihl);
  39247. + }
  39248. +#endif
  39249. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  39250. + "klips_debug:ipsec_xmit_restore_hard_header: "
  39251. + "With hard_header, final head,tailroom: %d,%d\n",
  39252. + skb_headroom(ixs->skb),
  39253. + skb_tailroom(ixs->skb));
  39254. +
  39255. + return IPSEC_XMIT_OK;
  39256. +}
  39257. +
  39258. +enum ipsec_xmit_value
  39259. +ipsec_tunnel_send(struct ipsec_xmit_state*ixs)
  39260. +{
  39261. +#ifdef NETDEV_25
  39262. + struct flowi fl;
  39263. +#endif
  39264. +
  39265. +#ifdef NET_21 /* 2.2 and 2.4 kernels */
  39266. + /* new route/dst cache code from James Morris */
  39267. + ixs->skb->dev = ixs->physdev;
  39268. +#ifdef NETDEV_25
  39269. + memset (&fl, 0x0, sizeof (struct flowi));
  39270. + fl.oif = ixs->physdev->iflink;
  39271. + fl.nl_u.ip4_u.daddr = ixs->skb->nh.iph->daddr;
  39272. + fl.nl_u.ip4_u.saddr = ixs->pass ? 0 : ixs->skb->nh.iph->saddr;
  39273. + fl.nl_u.ip4_u.tos = RT_TOS(ixs->skb->nh.iph->tos);
  39274. + fl.proto = ixs->skb->nh.iph->protocol;
  39275. + if ((ixs->error = ip_route_output_key(&ixs->route, &fl))) {
  39276. +#else
  39277. + /*skb_orphan(ixs->skb);*/
  39278. + if((ixs->error = ip_route_output(&ixs->route,
  39279. + ixs->skb->nh.iph->daddr,
  39280. + ixs->pass ? 0 : ixs->skb->nh.iph->saddr,
  39281. + RT_TOS(ixs->skb->nh.iph->tos),
  39282. + /* mcr->rgb: should this be 0 instead? */
  39283. + ixs->physdev->iflink))) {
  39284. +#endif
  39285. + ixs->stats->tx_errors++;
  39286. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39287. + "klips_debug:ipsec_xmit_send: "
  39288. + "ip_route_output failed with error code %d, rt->u.dst.dev=%s, dropped\n",
  39289. + ixs->error,
  39290. + ixs->route->u.dst.dev->name);
  39291. + return IPSEC_XMIT_ROUTEERR;
  39292. + }
  39293. + if(ixs->dev == ixs->route->u.dst.dev) {
  39294. + ip_rt_put(ixs->route);
  39295. + /* This is recursion, drop it. */
  39296. + ixs->stats->tx_errors++;
  39297. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39298. + "klips_debug:ipsec_xmit_send: "
  39299. + "suspect recursion, dev=rt->u.dst.dev=%s, dropped\n",
  39300. + ixs->dev->name);
  39301. + return IPSEC_XMIT_RECURSDETECT;
  39302. + }
  39303. + dst_release(ixs->skb->dst);
  39304. + ixs->skb->dst = &ixs->route->u.dst;
  39305. + ixs->stats->tx_bytes += ixs->skb->len;
  39306. + if(ixs->skb->len < ixs->skb->nh.raw - ixs->skb->data) {
  39307. + ixs->stats->tx_errors++;
  39308. + printk(KERN_WARNING
  39309. + "klips_error:ipsec_xmit_send: "
  39310. + "tried to __skb_pull nh-data=%ld, %d available. This should never happen, please report.\n",
  39311. + (unsigned long)(ixs->skb->nh.raw - ixs->skb->data),
  39312. + ixs->skb->len);
  39313. + return IPSEC_XMIT_PUSHPULLERR;
  39314. + }
  39315. + __skb_pull(ixs->skb, ixs->skb->nh.raw - ixs->skb->data);
  39316. +#ifdef SKB_RESET_NFCT
  39317. + if(!ixs->pass) {
  39318. + nf_conntrack_put(ixs->skb->nfct);
  39319. + ixs->skb->nfct = NULL;
  39320. + }
  39321. +#if defined(CONFIG_NETFILTER_DEBUG) && defined(HAVE_SKB_NF_DEBUG)
  39322. + ixs->skb->nf_debug = 0;
  39323. +#endif /* CONFIG_NETFILTER_DEBUG */
  39324. +#endif /* SKB_RESET_NFCT */
  39325. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39326. + "klips_debug:ipsec_xmit_send: "
  39327. + "...done, calling ip_send() on device:%s\n",
  39328. + ixs->skb->dev ? ixs->skb->dev->name : "NULL");
  39329. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->skb->nh.iph);
  39330. +#ifdef NETDEV_23 /* 2.4 kernels */
  39331. + {
  39332. + int err;
  39333. +
  39334. + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, ixs->skb, NULL, ixs->route->u.dst.dev,
  39335. + ipsec_tunnel_xmit2);
  39336. + if(err != NET_XMIT_SUCCESS && err != NET_XMIT_CN) {
  39337. + if(net_ratelimit())
  39338. + printk(KERN_ERR
  39339. + "klips_error:ipsec_xmit_send: "
  39340. + "ip_send() failed, err=%d\n",
  39341. + -err);
  39342. + ixs->stats->tx_errors++;
  39343. + ixs->stats->tx_aborted_errors++;
  39344. + ixs->skb = NULL;
  39345. + return IPSEC_XMIT_IPSENDFAILURE;
  39346. + }
  39347. + }
  39348. +#else /* NETDEV_23 */ /* 2.2 kernels */
  39349. + ip_send(ixs->skb);
  39350. +#endif /* NETDEV_23 */
  39351. +#else /* NET_21 */ /* 2.0 kernels */
  39352. + ixs->skb->arp = 1;
  39353. + /* ISDN/ASYNC PPP from Matjaz Godec. */
  39354. + /* skb->protocol = htons(ETH_P_IP); */
  39355. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39356. + "klips_debug:ipsec_xmit_send: "
  39357. + "...done, calling dev_queue_xmit() or ip_fragment().\n");
  39358. + IP_SEND(ixs->skb, ixs->physdev);
  39359. +#endif /* NET_21 */
  39360. + ixs->stats->tx_packets++;
  39361. +
  39362. + ixs->skb = NULL;
  39363. +
  39364. + return IPSEC_XMIT_OK;
  39365. +}
  39366. +
  39367. +void
  39368. +ipsec_tunnel_cleanup(struct ipsec_xmit_state*ixs)
  39369. +{
  39370. +#if defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE)
  39371. + netif_wake_queue(ixs->dev);
  39372. +#else /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  39373. + ixs->dev->tbusy = 0;
  39374. +#endif /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  39375. + if(ixs->saved_header) {
  39376. + kfree(ixs->saved_header);
  39377. + }
  39378. + if(ixs->skb) {
  39379. + dev_kfree_skb(ixs->skb, FREE_WRITE);
  39380. + }
  39381. + if(ixs->oskb) {
  39382. + dev_kfree_skb(ixs->oskb, FREE_WRITE);
  39383. + }
  39384. + if (ixs->ips.ips_ident_s.data) {
  39385. + kfree(ixs->ips.ips_ident_s.data);
  39386. + }
  39387. + if (ixs->ips.ips_ident_d.data) {
  39388. + kfree(ixs->ips.ips_ident_d.data);
  39389. + }
  39390. +}
  39391. +
  39392. +/*
  39393. + * This function assumes it is being called from dev_queue_xmit()
  39394. + * and that skb is filled properly by that function.
  39395. + */
  39396. +int
  39397. +ipsec_tunnel_start_xmit(struct sk_buff *skb, struct net_device *dev)
  39398. +{
  39399. + struct ipsec_xmit_state ixs_mem;
  39400. + struct ipsec_xmit_state *ixs = &ixs_mem;
  39401. + enum ipsec_xmit_value stat;
  39402. +
  39403. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  39404. + ixs->natt_type = 0, ixs->natt_head = 0;
  39405. + ixs->natt_sport = 0, ixs->natt_dport = 0;
  39406. +#endif
  39407. +
  39408. + memset((caddr_t)ixs, 0, sizeof(*ixs));
  39409. + ixs->oskb = NULL;
  39410. + ixs->saved_header = NULL; /* saved copy of the hard header */
  39411. + ixs->route = NULL;
  39412. + memset((caddr_t)&(ixs->ips), 0, sizeof(ixs->ips));
  39413. + ixs->dev = dev;
  39414. + ixs->skb = skb;
  39415. +
  39416. + stat = ipsec_xmit_sanity_check_dev(ixs);
  39417. + if(stat != IPSEC_XMIT_OK) {
  39418. + goto cleanup;
  39419. + }
  39420. +
  39421. + stat = ipsec_xmit_sanity_check_skb(ixs);
  39422. + if(stat != IPSEC_XMIT_OK) {
  39423. + goto cleanup;
  39424. + }
  39425. +
  39426. + stat = ipsec_tunnel_strip_hard_header(ixs);
  39427. + if(stat != IPSEC_XMIT_OK) {
  39428. + goto cleanup;
  39429. + }
  39430. +
  39431. + stat = ipsec_tunnel_SAlookup(ixs);
  39432. + if(stat != IPSEC_XMIT_OK) {
  39433. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39434. + "klips_debug:ipsec_tunnel_start_xmit: SAlookup failed: %d\n",
  39435. + stat);
  39436. + goto cleanup;
  39437. + }
  39438. +
  39439. + ixs->innersrc = ixs->iph->saddr;
  39440. + /* start encapsulation loop here XXX */
  39441. + do {
  39442. + stat = ipsec_xmit_encap_bundle(ixs);
  39443. + if(stat != IPSEC_XMIT_OK) {
  39444. + if(stat == IPSEC_XMIT_PASS) {
  39445. + goto bypass;
  39446. + }
  39447. +
  39448. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39449. + "klips_debug:ipsec_tunnel_start_xmit: encap_bundle failed: %d\n",
  39450. + stat);
  39451. + goto cleanup;
  39452. + }
  39453. +
  39454. + ixs->matcher.sen_ip_src.s_addr = ixs->iph->saddr;
  39455. + ixs->matcher.sen_ip_dst.s_addr = ixs->iph->daddr;
  39456. + ixs->matcher.sen_proto = ixs->iph->protocol;
  39457. + ipsec_extract_ports(ixs->iph, &ixs->matcher);
  39458. +
  39459. + spin_lock(&eroute_lock);
  39460. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  39461. + if(ixs->eroute) {
  39462. + ixs->outgoing_said = ixs->eroute->er_said;
  39463. + ixs->eroute_pid = ixs->eroute->er_pid;
  39464. + ixs->eroute->er_count++;
  39465. + ixs->eroute->er_lasttime = jiffies/HZ;
  39466. + }
  39467. + spin_unlock(&eroute_lock);
  39468. +
  39469. + KLIPS_PRINT((debug_tunnel & DB_TN_XMIT) &&
  39470. + /* ((ixs->orgdst != ixs->newdst) || (ixs->orgsrc != ixs->newsrc)) */
  39471. + (ixs->orgedst != ixs->outgoing_said.dst.u.v4.sin_addr.s_addr) &&
  39472. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr &&
  39473. + ixs->eroute,
  39474. + "klips_debug:ipsec_tunnel_start_xmit: "
  39475. + "We are recursing here.\n");
  39476. +
  39477. + } while(/*((ixs->orgdst != ixs->newdst) || (ixs->orgsrc != ixs->newsrc))*/
  39478. + (ixs->orgedst != ixs->outgoing_said.dst.u.v4.sin_addr.s_addr) &&
  39479. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr &&
  39480. + ixs->eroute);
  39481. +
  39482. + stat = ipsec_tunnel_restore_hard_header(ixs);
  39483. + if(stat != IPSEC_XMIT_OK) {
  39484. + goto cleanup;
  39485. + }
  39486. +
  39487. + bypass:
  39488. + stat = ipsec_tunnel_send(ixs);
  39489. +
  39490. + cleanup:
  39491. + ipsec_tunnel_cleanup(ixs);
  39492. +
  39493. + return 0;
  39494. +}
  39495. +
  39496. +DEBUG_NO_STATIC struct net_device_stats *
  39497. +ipsec_tunnel_get_stats(struct net_device *dev)
  39498. +{
  39499. + return &(((struct ipsecpriv *)(dev->priv))->mystats);
  39500. +}
  39501. +
  39502. +/*
  39503. + * Revectored calls.
  39504. + * For each of these calls, a field exists in our private structure.
  39505. + */
  39506. +
  39507. +DEBUG_NO_STATIC int
  39508. +ipsec_tunnel_hard_header(struct sk_buff *skb, struct net_device *dev,
  39509. + unsigned short type, void *daddr, void *saddr, unsigned len)
  39510. +{
  39511. + struct ipsecpriv *prv = dev->priv;
  39512. + struct net_device *tmp;
  39513. + int ret;
  39514. + struct net_device_stats *stats; /* This device's statistics */
  39515. +
  39516. + if(skb == NULL) {
  39517. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39518. + "klips_debug:ipsec_tunnel_hard_header: "
  39519. + "no skb...\n");
  39520. + return -ENODATA;
  39521. + }
  39522. +
  39523. + if(dev == NULL) {
  39524. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39525. + "klips_debug:ipsec_tunnel_hard_header: "
  39526. + "no device...\n");
  39527. + return -ENODEV;
  39528. + }
  39529. +
  39530. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39531. + "klips_debug:ipsec_tunnel_hard_header: "
  39532. + "skb->dev=%s dev=%s.\n",
  39533. + skb->dev ? skb->dev->name : "NULL",
  39534. + dev->name);
  39535. +
  39536. + if(prv == NULL) {
  39537. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39538. + "klips_debug:ipsec_tunnel_hard_header: "
  39539. + "no private space associated with dev=%s\n",
  39540. + dev->name ? dev->name : "NULL");
  39541. + return -ENODEV;
  39542. + }
  39543. +
  39544. + stats = (struct net_device_stats *) &(prv->mystats);
  39545. +
  39546. + if(prv->dev == NULL) {
  39547. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39548. + "klips_debug:ipsec_tunnel_hard_header: "
  39549. + "no physical device associated with dev=%s\n",
  39550. + dev->name ? dev->name : "NULL");
  39551. + stats->tx_dropped++;
  39552. + return -ENODEV;
  39553. + }
  39554. +
  39555. + /* check if we have to send a IPv6 packet. It might be a Router
  39556. + Solicitation, where the building of the packet happens in
  39557. + reverse order:
  39558. + 1. ll hdr,
  39559. + 2. IPv6 hdr,
  39560. + 3. ICMPv6 hdr
  39561. + -> skb->nh.raw is still uninitialized when this function is
  39562. + called!! If this is no IPv6 packet, we can print debugging
  39563. + messages, otherwise we skip all debugging messages and just
  39564. + build the ll header */
  39565. + if(type != ETH_P_IPV6) {
  39566. + /* execute this only, if we don't have to build the
  39567. + header for a IPv6 packet */
  39568. + if(!prv->hard_header) {
  39569. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39570. + "klips_debug:ipsec_tunnel_hard_header: "
  39571. + "physical device has been detached, packet dropped 0p%p->0p%p len=%d type=%d dev=%s->NULL ",
  39572. + saddr,
  39573. + daddr,
  39574. + len,
  39575. + type,
  39576. + dev->name);
  39577. +#ifdef NET_21
  39578. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39579. + "ip=%08x->%08x\n",
  39580. + (__u32)ntohl(skb->nh.iph->saddr),
  39581. + (__u32)ntohl(skb->nh.iph->daddr) );
  39582. +#else /* NET_21 */
  39583. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39584. + "ip=%08x->%08x\n",
  39585. + (__u32)ntohl(skb->ip_hdr->saddr),
  39586. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39587. +#endif /* NET_21 */
  39588. + stats->tx_dropped++;
  39589. + return -ENODEV;
  39590. + }
  39591. +
  39592. +#define da ((struct net_device *)(prv->dev))->dev_addr
  39593. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39594. + "klips_debug:ipsec_tunnel_hard_header: "
  39595. + "Revectored 0p%p->0p%p len=%d type=%d dev=%s->%s dev_addr=%02x:%02x:%02x:%02x:%02x:%02x ",
  39596. + saddr,
  39597. + daddr,
  39598. + len,
  39599. + type,
  39600. + dev->name,
  39601. + prv->dev->name,
  39602. + da[0], da[1], da[2], da[3], da[4], da[5]);
  39603. +#ifdef NET_21
  39604. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39605. + "ip=%08x->%08x\n",
  39606. + (__u32)ntohl(skb->nh.iph->saddr),
  39607. + (__u32)ntohl(skb->nh.iph->daddr) );
  39608. +#else /* NET_21 */
  39609. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39610. + "ip=%08x->%08x\n",
  39611. + (__u32)ntohl(skb->ip_hdr->saddr),
  39612. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39613. +#endif /* NET_21 */
  39614. + } else {
  39615. + KLIPS_PRINT(debug_tunnel,
  39616. + "klips_debug:ipsec_tunnel_hard_header: "
  39617. + "is IPv6 packet, skip debugging messages, only revector and build linklocal header.\n");
  39618. + }
  39619. + tmp = skb->dev;
  39620. + skb->dev = prv->dev;
  39621. + ret = prv->hard_header(skb, prv->dev, type, (void *)daddr, (void *)saddr, len);
  39622. + skb->dev = tmp;
  39623. + return ret;
  39624. +}
  39625. +
  39626. +DEBUG_NO_STATIC int
  39627. +#ifdef NET_21
  39628. +ipsec_tunnel_rebuild_header(struct sk_buff *skb)
  39629. +#else /* NET_21 */
  39630. +ipsec_tunnel_rebuild_header(void *buff, struct net_device *dev,
  39631. + unsigned long raddr, struct sk_buff *skb)
  39632. +#endif /* NET_21 */
  39633. +{
  39634. + struct ipsecpriv *prv = skb->dev->priv;
  39635. + struct net_device *tmp;
  39636. + int ret;
  39637. + struct net_device_stats *stats; /* This device's statistics */
  39638. +
  39639. + if(skb->dev == NULL) {
  39640. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39641. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39642. + "no device...");
  39643. + return -ENODEV;
  39644. + }
  39645. +
  39646. + if(prv == NULL) {
  39647. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39648. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39649. + "no private space associated with dev=%s",
  39650. + skb->dev->name ? skb->dev->name : "NULL");
  39651. + return -ENODEV;
  39652. + }
  39653. +
  39654. + stats = (struct net_device_stats *) &(prv->mystats);
  39655. +
  39656. + if(prv->dev == NULL) {
  39657. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39658. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39659. + "no physical device associated with dev=%s",
  39660. + skb->dev->name ? skb->dev->name : "NULL");
  39661. + stats->tx_dropped++;
  39662. + return -ENODEV;
  39663. + }
  39664. +
  39665. + if(!prv->rebuild_header) {
  39666. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39667. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39668. + "physical device has been detached, packet dropped skb->dev=%s->NULL ",
  39669. + skb->dev->name);
  39670. +#ifdef NET_21
  39671. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39672. + "ip=%08x->%08x\n",
  39673. + (__u32)ntohl(skb->nh.iph->saddr),
  39674. + (__u32)ntohl(skb->nh.iph->daddr) );
  39675. +#else /* NET_21 */
  39676. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39677. + "ip=%08x->%08x\n",
  39678. + (__u32)ntohl(skb->ip_hdr->saddr),
  39679. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39680. +#endif /* NET_21 */
  39681. + stats->tx_dropped++;
  39682. + return -ENODEV;
  39683. + }
  39684. +
  39685. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39686. + "klips_debug:ipsec_tunnel: "
  39687. + "Revectored rebuild_header dev=%s->%s ",
  39688. + skb->dev->name, prv->dev->name);
  39689. +#ifdef NET_21
  39690. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39691. + "ip=%08x->%08x\n",
  39692. + (__u32)ntohl(skb->nh.iph->saddr),
  39693. + (__u32)ntohl(skb->nh.iph->daddr) );
  39694. +#else /* NET_21 */
  39695. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39696. + "ip=%08x->%08x\n",
  39697. + (__u32)ntohl(skb->ip_hdr->saddr),
  39698. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39699. +#endif /* NET_21 */
  39700. + tmp = skb->dev;
  39701. + skb->dev = prv->dev;
  39702. +
  39703. +#ifdef NET_21
  39704. + ret = prv->rebuild_header(skb);
  39705. +#else /* NET_21 */
  39706. + ret = prv->rebuild_header(buff, prv->dev, raddr, skb);
  39707. +#endif /* NET_21 */
  39708. + skb->dev = tmp;
  39709. + return ret;
  39710. +}
  39711. +
  39712. +DEBUG_NO_STATIC int
  39713. +ipsec_tunnel_set_mac_address(struct net_device *dev, void *addr)
  39714. +{
  39715. + struct ipsecpriv *prv = dev->priv;
  39716. +
  39717. + struct net_device_stats *stats; /* This device's statistics */
  39718. +
  39719. + if(dev == NULL) {
  39720. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39721. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39722. + "no device...");
  39723. + return -ENODEV;
  39724. + }
  39725. +
  39726. + if(prv == NULL) {
  39727. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39728. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39729. + "no private space associated with dev=%s",
  39730. + dev->name ? dev->name : "NULL");
  39731. + return -ENODEV;
  39732. + }
  39733. +
  39734. + stats = (struct net_device_stats *) &(prv->mystats);
  39735. +
  39736. + if(prv->dev == NULL) {
  39737. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39738. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39739. + "no physical device associated with dev=%s",
  39740. + dev->name ? dev->name : "NULL");
  39741. + stats->tx_dropped++;
  39742. + return -ENODEV;
  39743. + }
  39744. +
  39745. + if(!prv->set_mac_address) {
  39746. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39747. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39748. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39749. + dev->name);
  39750. + return -ENODEV;
  39751. + }
  39752. +
  39753. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39754. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39755. + "Revectored dev=%s->%s addr=0p%p\n",
  39756. + dev->name, prv->dev->name, addr);
  39757. + return prv->set_mac_address(prv->dev, addr);
  39758. +
  39759. +}
  39760. +
  39761. +#ifndef NET_21
  39762. +DEBUG_NO_STATIC void
  39763. +ipsec_tunnel_cache_bind(struct hh_cache **hhp, struct net_device *dev,
  39764. + unsigned short htype, __u32 daddr)
  39765. +{
  39766. + struct ipsecpriv *prv = dev->priv;
  39767. +
  39768. + struct net_device_stats *stats; /* This device's statistics */
  39769. +
  39770. + if(dev == NULL) {
  39771. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39772. + "klips_debug:ipsec_tunnel_cache_bind: "
  39773. + "no device...");
  39774. + return;
  39775. + }
  39776. +
  39777. + if(prv == NULL) {
  39778. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39779. + "klips_debug:ipsec_tunnel_cache_bind: "
  39780. + "no private space associated with dev=%s",
  39781. + dev->name ? dev->name : "NULL");
  39782. + return;
  39783. + }
  39784. +
  39785. + stats = (struct net_device_stats *) &(prv->mystats);
  39786. +
  39787. + if(prv->dev == NULL) {
  39788. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39789. + "klips_debug:ipsec_tunnel_cache_bind: "
  39790. + "no physical device associated with dev=%s",
  39791. + dev->name ? dev->name : "NULL");
  39792. + stats->tx_dropped++;
  39793. + return;
  39794. + }
  39795. +
  39796. + if(!prv->header_cache_bind) {
  39797. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39798. + "klips_debug:ipsec_tunnel_cache_bind: "
  39799. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39800. + dev->name);
  39801. + stats->tx_dropped++;
  39802. + return;
  39803. + }
  39804. +
  39805. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39806. + "klips_debug:ipsec_tunnel_cache_bind: "
  39807. + "Revectored \n");
  39808. + prv->header_cache_bind(hhp, prv->dev, htype, daddr);
  39809. + return;
  39810. +}
  39811. +#endif /* !NET_21 */
  39812. +
  39813. +
  39814. +DEBUG_NO_STATIC void
  39815. +ipsec_tunnel_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
  39816. +{
  39817. + struct ipsecpriv *prv = dev->priv;
  39818. +
  39819. + struct net_device_stats *stats; /* This device's statistics */
  39820. +
  39821. + if(dev == NULL) {
  39822. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39823. + "klips_debug:ipsec_tunnel_cache_update: "
  39824. + "no device...");
  39825. + return;
  39826. + }
  39827. +
  39828. + if(prv == NULL) {
  39829. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39830. + "klips_debug:ipsec_tunnel_cache_update: "
  39831. + "no private space associated with dev=%s",
  39832. + dev->name ? dev->name : "NULL");
  39833. + return;
  39834. + }
  39835. +
  39836. + stats = (struct net_device_stats *) &(prv->mystats);
  39837. +
  39838. + if(prv->dev == NULL) {
  39839. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39840. + "klips_debug:ipsec_tunnel_cache_update: "
  39841. + "no physical device associated with dev=%s",
  39842. + dev->name ? dev->name : "NULL");
  39843. + stats->tx_dropped++;
  39844. + return;
  39845. + }
  39846. +
  39847. + if(!prv->header_cache_update) {
  39848. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39849. + "klips_debug:ipsec_tunnel_cache_update: "
  39850. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39851. + dev->name);
  39852. + return;
  39853. + }
  39854. +
  39855. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39856. + "klips_debug:ipsec_tunnel: "
  39857. + "Revectored cache_update\n");
  39858. + prv->header_cache_update(hh, prv->dev, haddr);
  39859. + return;
  39860. +}
  39861. +
  39862. +#ifdef NET_21
  39863. +DEBUG_NO_STATIC int
  39864. +ipsec_tunnel_neigh_setup(struct neighbour *n)
  39865. +{
  39866. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39867. + "klips_debug:ipsec_tunnel_neigh_setup:\n");
  39868. +
  39869. + if (n->nud_state == NUD_NONE) {
  39870. + n->ops = &arp_broken_ops;
  39871. + n->output = n->ops->output;
  39872. + }
  39873. + return 0;
  39874. +}
  39875. +
  39876. +DEBUG_NO_STATIC int
  39877. +ipsec_tunnel_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  39878. +{
  39879. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39880. + "klips_debug:ipsec_tunnel_neigh_setup_dev: "
  39881. + "setting up %s\n",
  39882. + dev ? dev->name : "NULL");
  39883. +
  39884. + if (p->tbl->family == AF_INET) {
  39885. + p->neigh_setup = ipsec_tunnel_neigh_setup;
  39886. + p->ucast_probes = 0;
  39887. + p->mcast_probes = 0;
  39888. + }
  39889. + return 0;
  39890. +}
  39891. +#endif /* NET_21 */
  39892. +
  39893. +/*
  39894. + * We call the attach routine to attach another device.
  39895. + */
  39896. +
  39897. +DEBUG_NO_STATIC int
  39898. +ipsec_tunnel_attach(struct net_device *dev, struct net_device *physdev)
  39899. +{
  39900. + int i;
  39901. + struct ipsecpriv *prv = dev->priv;
  39902. +
  39903. + if(dev == NULL) {
  39904. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39905. + "klips_debug:ipsec_tunnel_attach: "
  39906. + "no device...");
  39907. + return -ENODEV;
  39908. + }
  39909. +
  39910. + if(prv == NULL) {
  39911. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39912. + "klips_debug:ipsec_tunnel_attach: "
  39913. + "no private space associated with dev=%s",
  39914. + dev->name ? dev->name : "NULL");
  39915. + return -ENODATA;
  39916. + }
  39917. +
  39918. + prv->dev = physdev;
  39919. + prv->hard_start_xmit = physdev->hard_start_xmit;
  39920. + prv->get_stats = physdev->get_stats;
  39921. +
  39922. + if (physdev->hard_header) {
  39923. + prv->hard_header = physdev->hard_header;
  39924. + dev->hard_header = ipsec_tunnel_hard_header;
  39925. + } else
  39926. + dev->hard_header = NULL;
  39927. +
  39928. + if (physdev->rebuild_header) {
  39929. + prv->rebuild_header = physdev->rebuild_header;
  39930. + dev->rebuild_header = ipsec_tunnel_rebuild_header;
  39931. + } else
  39932. + dev->rebuild_header = NULL;
  39933. +
  39934. + if (physdev->set_mac_address) {
  39935. + prv->set_mac_address = physdev->set_mac_address;
  39936. + dev->set_mac_address = ipsec_tunnel_set_mac_address;
  39937. + } else
  39938. + dev->set_mac_address = NULL;
  39939. +
  39940. +#ifndef NET_21
  39941. + if (physdev->header_cache_bind) {
  39942. + prv->header_cache_bind = physdev->header_cache_bind;
  39943. + dev->header_cache_bind = ipsec_tunnel_cache_bind;
  39944. + } else
  39945. + dev->header_cache_bind = NULL;
  39946. +#endif /* !NET_21 */
  39947. +
  39948. + if (physdev->header_cache_update) {
  39949. + prv->header_cache_update = physdev->header_cache_update;
  39950. + dev->header_cache_update = ipsec_tunnel_cache_update;
  39951. + } else
  39952. + dev->header_cache_update = NULL;
  39953. +
  39954. + dev->hard_header_len = physdev->hard_header_len;
  39955. +
  39956. +#ifdef NET_21
  39957. +/* prv->neigh_setup = physdev->neigh_setup; */
  39958. + dev->neigh_setup = ipsec_tunnel_neigh_setup_dev;
  39959. +#endif /* NET_21 */
  39960. + dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */
  39961. + prv->mtu = physdev->mtu;
  39962. +
  39963. +#ifdef PHYSDEV_TYPE
  39964. + dev->type = physdev->type; /* ARPHRD_TUNNEL; */
  39965. +#endif /* PHYSDEV_TYPE */
  39966. +
  39967. + dev->addr_len = physdev->addr_len;
  39968. + for (i=0; i<dev->addr_len; i++) {
  39969. + dev->dev_addr[i] = physdev->dev_addr[i];
  39970. + }
  39971. +#ifdef CONFIG_KLIPS_DEBUG
  39972. + if(debug_tunnel & DB_TN_INIT) {
  39973. + printk(KERN_INFO "klips_debug:ipsec_tunnel_attach: "
  39974. + "physical device %s being attached has HW address: %2x",
  39975. + physdev->name, physdev->dev_addr[0]);
  39976. + for (i=1; i < physdev->addr_len; i++) {
  39977. + printk(":%02x", physdev->dev_addr[i]);
  39978. + }
  39979. + printk("\n");
  39980. + }
  39981. +#endif /* CONFIG_KLIPS_DEBUG */
  39982. +
  39983. + return 0;
  39984. +}
  39985. +
  39986. +/*
  39987. + * We call the detach routine to detach the ipsec tunnel from another device.
  39988. + */
  39989. +
  39990. +DEBUG_NO_STATIC int
  39991. +ipsec_tunnel_detach(struct net_device *dev)
  39992. +{
  39993. + int i;
  39994. + struct ipsecpriv *prv = dev->priv;
  39995. +
  39996. + if(dev == NULL) {
  39997. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39998. + "klips_debug:ipsec_tunnel_detach: "
  39999. + "no device...");
  40000. + return -ENODEV;
  40001. + }
  40002. +
  40003. + if(prv == NULL) {
  40004. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  40005. + "klips_debug:ipsec_tunnel_detach: "
  40006. + "no private space associated with dev=%s",
  40007. + dev->name ? dev->name : "NULL");
  40008. + return -ENODATA;
  40009. + }
  40010. +
  40011. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40012. + "klips_debug:ipsec_tunnel_detach: "
  40013. + "physical device %s being detached from virtual device %s\n",
  40014. + prv->dev ? prv->dev->name : "NULL",
  40015. + dev->name);
  40016. +
  40017. + ipsec_dev_put(prv->dev);
  40018. + prv->dev = NULL;
  40019. + prv->hard_start_xmit = NULL;
  40020. + prv->get_stats = NULL;
  40021. +
  40022. + prv->hard_header = NULL;
  40023. +#ifdef DETACH_AND_DOWN
  40024. + dev->hard_header = NULL;
  40025. +#endif /* DETACH_AND_DOWN */
  40026. +
  40027. + prv->rebuild_header = NULL;
  40028. +#ifdef DETACH_AND_DOWN
  40029. + dev->rebuild_header = NULL;
  40030. +#endif /* DETACH_AND_DOWN */
  40031. +
  40032. + prv->set_mac_address = NULL;
  40033. +#ifdef DETACH_AND_DOWN
  40034. + dev->set_mac_address = NULL;
  40035. +#endif /* DETACH_AND_DOWN */
  40036. +
  40037. +#ifndef NET_21
  40038. + prv->header_cache_bind = NULL;
  40039. +#ifdef DETACH_AND_DOWN
  40040. + dev->header_cache_bind = NULL;
  40041. +#endif /* DETACH_AND_DOWN */
  40042. +#endif /* !NET_21 */
  40043. +
  40044. + prv->header_cache_update = NULL;
  40045. +#ifdef DETACH_AND_DOWN
  40046. + dev->header_cache_update = NULL;
  40047. +#endif /* DETACH_AND_DOWN */
  40048. +
  40049. +#ifdef NET_21
  40050. +/* prv->neigh_setup = NULL; */
  40051. +#ifdef DETACH_AND_DOWN
  40052. + dev->neigh_setup = NULL;
  40053. +#endif /* DETACH_AND_DOWN */
  40054. +#endif /* NET_21 */
  40055. + dev->hard_header_len = 0;
  40056. +#ifdef DETACH_AND_DOWN
  40057. + dev->mtu = 0;
  40058. +#endif /* DETACH_AND_DOWN */
  40059. + prv->mtu = 0;
  40060. + for (i=0; i<MAX_ADDR_LEN; i++) {
  40061. + dev->dev_addr[i] = 0;
  40062. + }
  40063. + dev->addr_len = 0;
  40064. +#ifdef PHYSDEV_TYPE
  40065. + dev->type = ARPHRD_VOID; /* ARPHRD_TUNNEL; */
  40066. +#endif /* PHYSDEV_TYPE */
  40067. +
  40068. + return 0;
  40069. +}
  40070. +
  40071. +/*
  40072. + * We call the clear routine to detach all ipsec tunnels from other devices.
  40073. + */
  40074. +DEBUG_NO_STATIC int
  40075. +ipsec_tunnel_clear(void)
  40076. +{
  40077. + int i;
  40078. + struct net_device *ipsecdev = NULL, *prvdev;
  40079. + struct ipsecpriv *prv;
  40080. + int ret;
  40081. +
  40082. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40083. + "klips_debug:ipsec_tunnel_clear: .\n");
  40084. +
  40085. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40086. + ipsecdev = ipsecdevices[i];
  40087. + if(ipsecdev != NULL) {
  40088. + if((prv = (struct ipsecpriv *)(ipsecdev->priv))) {
  40089. + prvdev = (struct net_device *)(prv->dev);
  40090. + if(prvdev) {
  40091. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40092. + "klips_debug:ipsec_tunnel_clear: "
  40093. + "physical device for device %s is %s\n",
  40094. + ipsecdev->name, prvdev->name);
  40095. + if((ret = ipsec_tunnel_detach(ipsecdev))) {
  40096. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40097. + "klips_debug:ipsec_tunnel_clear: "
  40098. + "error %d detatching device %s from device %s.\n",
  40099. + ret, ipsecdev->name, prvdev->name);
  40100. + return ret;
  40101. + }
  40102. + }
  40103. + }
  40104. + }
  40105. + }
  40106. + return 0;
  40107. +}
  40108. +
  40109. +DEBUG_NO_STATIC int
  40110. +ipsec_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  40111. +{
  40112. + struct ipsectunnelconf *cf = (struct ipsectunnelconf *)&ifr->ifr_data;
  40113. + struct ipsecpriv *prv = dev->priv;
  40114. + struct net_device *them; /* physical device */
  40115. +#ifdef CONFIG_IP_ALIAS
  40116. + char *colon;
  40117. + char realphysname[IFNAMSIZ];
  40118. +#endif /* CONFIG_IP_ALIAS */
  40119. +
  40120. + if(dev == NULL) {
  40121. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40122. + "klips_debug:ipsec_tunnel_ioctl: "
  40123. + "device not supplied.\n");
  40124. + return -ENODEV;
  40125. + }
  40126. +
  40127. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40128. + "klips_debug:ipsec_tunnel_ioctl: "
  40129. + "tncfg service call #%d for dev=%s\n",
  40130. + cmd,
  40131. + dev->name ? dev->name : "NULL");
  40132. + switch (cmd) {
  40133. + /* attach a virtual ipsec? device to a physical device */
  40134. + case IPSEC_SET_DEV:
  40135. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40136. + "klips_debug:ipsec_tunnel_ioctl: "
  40137. + "calling ipsec_tunnel_attatch...\n");
  40138. +#ifdef CONFIG_IP_ALIAS
  40139. + /* If this is an IP alias interface, get its real physical name */
  40140. + strncpy(realphysname, cf->cf_name, IFNAMSIZ);
  40141. + realphysname[IFNAMSIZ-1] = 0;
  40142. + colon = strchr(realphysname, ':');
  40143. + if (colon) *colon = 0;
  40144. + them = ipsec_dev_get(realphysname);
  40145. +#else /* CONFIG_IP_ALIAS */
  40146. + them = ipsec_dev_get(cf->cf_name);
  40147. +#endif /* CONFIG_IP_ALIAS */
  40148. +
  40149. + if (them == NULL) {
  40150. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40151. + "klips_debug:ipsec_tunnel_ioctl: "
  40152. + "physical device %s requested is null\n",
  40153. + cf->cf_name);
  40154. + return -ENXIO;
  40155. + }
  40156. +
  40157. +#if 0
  40158. + if (them->flags & IFF_UP) {
  40159. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40160. + "klips_debug:ipsec_tunnel_ioctl: "
  40161. + "physical device %s requested is not up.\n",
  40162. + cf->cf_name);
  40163. + ipsec_dev_put(them);
  40164. + return -ENXIO;
  40165. + }
  40166. +#endif
  40167. +
  40168. + if (prv && prv->dev) {
  40169. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40170. + "klips_debug:ipsec_tunnel_ioctl: "
  40171. + "virtual device is already connected to %s.\n",
  40172. + prv->dev->name ? prv->dev->name : "NULL");
  40173. + ipsec_dev_put(them);
  40174. + return -EBUSY;
  40175. + }
  40176. + return ipsec_tunnel_attach(dev, them);
  40177. +
  40178. + case IPSEC_DEL_DEV:
  40179. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40180. + "klips_debug:ipsec_tunnel_ioctl: "
  40181. + "calling ipsec_tunnel_detatch.\n");
  40182. + if (! prv->dev) {
  40183. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40184. + "klips_debug:ipsec_tunnel_ioctl: "
  40185. + "physical device not connected.\n");
  40186. + return -ENODEV;
  40187. + }
  40188. + return ipsec_tunnel_detach(dev);
  40189. +
  40190. + case IPSEC_CLR_DEV:
  40191. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40192. + "klips_debug:ipsec_tunnel_ioctl: "
  40193. + "calling ipsec_tunnel_clear.\n");
  40194. + return ipsec_tunnel_clear();
  40195. +
  40196. + default:
  40197. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40198. + "klips_debug:ipsec_tunnel_ioctl: "
  40199. + "unknown command %d.\n",
  40200. + cmd);
  40201. + return -EOPNOTSUPP;
  40202. + }
  40203. +}
  40204. +
  40205. +struct net_device *ipsec_get_device(int inst)
  40206. +{
  40207. + struct net_device *ipsec_dev;
  40208. +
  40209. + ipsec_dev = NULL;
  40210. +
  40211. + if(inst < IPSEC_NUM_IF) {
  40212. + ipsec_dev = ipsecdevices[inst];
  40213. + }
  40214. +
  40215. + return ipsec_dev;
  40216. +}
  40217. +
  40218. +int
  40219. +ipsec_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  40220. +{
  40221. + struct net_device *dev = ptr;
  40222. + struct net_device *ipsec_dev;
  40223. + struct ipsecpriv *priv;
  40224. + int i;
  40225. +
  40226. + if (dev == NULL) {
  40227. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40228. + "klips_debug:ipsec_device_event: "
  40229. + "dev=NULL for event type %ld.\n",
  40230. + event);
  40231. + return(NOTIFY_DONE);
  40232. + }
  40233. +
  40234. + /* check for loopback devices */
  40235. + if (dev && (dev->flags & IFF_LOOPBACK)) {
  40236. + return(NOTIFY_DONE);
  40237. + }
  40238. +
  40239. + switch (event) {
  40240. + case NETDEV_DOWN:
  40241. + /* look very carefully at the scope of these compiler
  40242. + directives before changing anything... -- RGB */
  40243. +#ifdef NET_21
  40244. + case NETDEV_UNREGISTER:
  40245. + switch (event) {
  40246. + case NETDEV_DOWN:
  40247. +#endif /* NET_21 */
  40248. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40249. + "klips_debug:ipsec_device_event: "
  40250. + "NETDEV_DOWN dev=%s flags=%x\n",
  40251. + dev->name,
  40252. + dev->flags);
  40253. + if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) {
  40254. + printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n",
  40255. + dev->name);
  40256. + }
  40257. +#ifdef NET_21
  40258. + break;
  40259. + case NETDEV_UNREGISTER:
  40260. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40261. + "klips_debug:ipsec_device_event: "
  40262. + "NETDEV_UNREGISTER dev=%s flags=%x\n",
  40263. + dev->name,
  40264. + dev->flags);
  40265. + break;
  40266. + }
  40267. +#endif /* NET_21 */
  40268. +
  40269. + /* find the attached physical device and detach it. */
  40270. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40271. + ipsec_dev = ipsecdevices[i];
  40272. +
  40273. + if(ipsec_dev) {
  40274. + priv = (struct ipsecpriv *)(ipsec_dev->priv);
  40275. + if(priv) {
  40276. + ;
  40277. + if(((struct net_device *)(priv->dev)) == dev) {
  40278. + /* dev_close(ipsec_dev); */
  40279. + /* return */ ipsec_tunnel_detach(ipsec_dev);
  40280. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40281. + "klips_debug:ipsec_device_event: "
  40282. + "device '%s' has been detached.\n",
  40283. + ipsec_dev->name);
  40284. + break;
  40285. + }
  40286. + } else {
  40287. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40288. + "klips_debug:ipsec_device_event: "
  40289. + "device '%s' has no private data space!\n",
  40290. + ipsec_dev->name);
  40291. + }
  40292. + }
  40293. + }
  40294. + break;
  40295. + case NETDEV_UP:
  40296. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40297. + "klips_debug:ipsec_device_event: "
  40298. + "NETDEV_UP dev=%s\n",
  40299. + dev->name);
  40300. + break;
  40301. +#ifdef NET_21
  40302. + case NETDEV_REBOOT:
  40303. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40304. + "klips_debug:ipsec_device_event: "
  40305. + "NETDEV_REBOOT dev=%s\n",
  40306. + dev->name);
  40307. + break;
  40308. + case NETDEV_CHANGE:
  40309. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40310. + "klips_debug:ipsec_device_event: "
  40311. + "NETDEV_CHANGE dev=%s flags=%x\n",
  40312. + dev->name,
  40313. + dev->flags);
  40314. + break;
  40315. + case NETDEV_REGISTER:
  40316. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40317. + "klips_debug:ipsec_device_event: "
  40318. + "NETDEV_REGISTER dev=%s\n",
  40319. + dev->name);
  40320. + break;
  40321. + case NETDEV_CHANGEMTU:
  40322. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40323. + "klips_debug:ipsec_device_event: "
  40324. + "NETDEV_CHANGEMTU dev=%s to mtu=%d\n",
  40325. + dev->name,
  40326. + dev->mtu);
  40327. + break;
  40328. + case NETDEV_CHANGEADDR:
  40329. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40330. + "klips_debug:ipsec_device_event: "
  40331. + "NETDEV_CHANGEADDR dev=%s\n",
  40332. + dev->name);
  40333. + break;
  40334. + case NETDEV_GOING_DOWN:
  40335. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40336. + "klips_debug:ipsec_device_event: "
  40337. + "NETDEV_GOING_DOWN dev=%s\n",
  40338. + dev->name);
  40339. + break;
  40340. + case NETDEV_CHANGENAME:
  40341. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40342. + "klips_debug:ipsec_device_event: "
  40343. + "NETDEV_CHANGENAME dev=%s\n",
  40344. + dev->name);
  40345. + break;
  40346. +#endif /* NET_21 */
  40347. + default:
  40348. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40349. + "klips_debug:ipsec_device_event: "
  40350. + "event type %ld unrecognised for dev=%s\n",
  40351. + event,
  40352. + dev->name);
  40353. + break;
  40354. + }
  40355. + return NOTIFY_DONE;
  40356. +}
  40357. +
  40358. +/*
  40359. + * Called when an ipsec tunnel device is initialized.
  40360. + * The ipsec tunnel device structure is passed to us.
  40361. + */
  40362. +
  40363. +int
  40364. +ipsec_tunnel_init(struct net_device *dev)
  40365. +{
  40366. + int i;
  40367. +
  40368. + KLIPS_PRINT(debug_tunnel,
  40369. + "klips_debug:ipsec_tunnel_init: "
  40370. + "allocating %lu bytes initialising device: %s\n",
  40371. + (unsigned long) sizeof(struct ipsecpriv),
  40372. + dev->name ? dev->name : "NULL");
  40373. +
  40374. + /* Add our tunnel functions to the device */
  40375. + dev->open = ipsec_tunnel_open;
  40376. + dev->stop = ipsec_tunnel_close;
  40377. + dev->hard_start_xmit = ipsec_tunnel_start_xmit;
  40378. + dev->get_stats = ipsec_tunnel_get_stats;
  40379. +
  40380. + dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL);
  40381. + if (dev->priv == NULL)
  40382. + return -ENOMEM;
  40383. + memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv));
  40384. +
  40385. + for(i = 0; i < sizeof(zeroes); i++) {
  40386. + ((__u8*)(zeroes))[i] = 0;
  40387. + }
  40388. +
  40389. +#ifndef NET_21
  40390. + /* Initialize the tunnel device structure */
  40391. + for (i = 0; i < DEV_NUMBUFFS; i++)
  40392. + skb_queue_head_init(&dev->buffs[i]);
  40393. +#endif /* !NET_21 */
  40394. +
  40395. + dev->set_multicast_list = NULL;
  40396. + dev->do_ioctl = ipsec_tunnel_ioctl;
  40397. + dev->hard_header = NULL;
  40398. + dev->rebuild_header = NULL;
  40399. + dev->set_mac_address = NULL;
  40400. +#ifndef NET_21
  40401. + dev->header_cache_bind = NULL;
  40402. +#endif /* !NET_21 */
  40403. + dev->header_cache_update= NULL;
  40404. +
  40405. +#ifdef NET_21
  40406. +/* prv->neigh_setup = NULL; */
  40407. + dev->neigh_setup = ipsec_tunnel_neigh_setup_dev;
  40408. +#endif /* NET_21 */
  40409. + dev->hard_header_len = 0;
  40410. + dev->mtu = 0;
  40411. + dev->addr_len = 0;
  40412. + dev->type = ARPHRD_VOID; /* ARPHRD_TUNNEL; */ /* ARPHRD_ETHER; */
  40413. + dev->tx_queue_len = 10; /* Small queue */
  40414. + memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */
  40415. +
  40416. + /* New-style flags. */
  40417. + dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */;
  40418. +
  40419. +#if 0
  40420. +#ifdef NET_21
  40421. + dev_init_buffers(dev);
  40422. +#else /* NET_21 */
  40423. + dev->family = AF_INET;
  40424. + dev->pa_addr = 0;
  40425. + dev->pa_brdaddr = 0;
  40426. + dev->pa_mask = 0;
  40427. + dev->pa_alen = 4;
  40428. +#endif /* NET_21 */
  40429. +#endif
  40430. +
  40431. + /* We're done. Have I forgotten anything? */
  40432. + return 0;
  40433. +}
  40434. +
  40435. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  40436. +/* Module specific interface (but it links with the rest of IPSEC) */
  40437. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  40438. +
  40439. +int
  40440. +ipsec_tunnel_probe(struct net_device *dev)
  40441. +{
  40442. + ipsec_tunnel_init(dev);
  40443. + return 0;
  40444. +}
  40445. +
  40446. +struct net_device *ipsecdevices[IPSEC_NUM_IF];
  40447. +
  40448. +int
  40449. +ipsec_tunnel_init_devices(void)
  40450. +{
  40451. + int i;
  40452. + char name[IFNAMSIZ];
  40453. + struct net_device *dev_ipsec;
  40454. +
  40455. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40456. + "klips_debug:ipsec_tunnel_init_devices: "
  40457. + "creating and registering IPSEC_NUM_IF=%u devices, allocating %lu per device, IFNAMSIZ=%u.\n",
  40458. + IPSEC_NUM_IF,
  40459. + (unsigned long) (sizeof(struct net_device) + IFNAMSIZ),
  40460. + IFNAMSIZ);
  40461. +
  40462. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40463. + sprintf(name, IPSEC_DEV_FORMAT, i);
  40464. + dev_ipsec = (struct net_device*)kmalloc(sizeof(struct net_device), GFP_KERNEL);
  40465. + if (dev_ipsec == NULL) {
  40466. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40467. + "klips_debug:ipsec_tunnel_init_devices: "
  40468. + "failed to allocate memory for device %s, quitting device init.\n",
  40469. + name);
  40470. + return -ENOMEM;
  40471. + }
  40472. + memset((caddr_t)dev_ipsec, 0, sizeof(struct net_device));
  40473. +#ifdef NETDEV_23
  40474. + strncpy(dev_ipsec->name, name, sizeof(dev_ipsec->name));
  40475. +#else /* NETDEV_23 */
  40476. + dev_ipsec->name = (char*)kmalloc(IFNAMSIZ, GFP_KERNEL);
  40477. + if (dev_ipsec->name == NULL) {
  40478. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40479. + "klips_debug:ipsec_tunnel_init_devices: "
  40480. + "failed to allocate memory for device %s name, quitting device init.\n",
  40481. + name);
  40482. + return -ENOMEM;
  40483. + }
  40484. + memset((caddr_t)dev_ipsec->name, 0, IFNAMSIZ);
  40485. + strncpy(dev_ipsec->name, name, IFNAMSIZ);
  40486. +#endif /* NETDEV_23 */
  40487. + dev_ipsec->next = NULL;
  40488. + dev_ipsec->init = &ipsec_tunnel_probe;
  40489. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40490. + "klips_debug:ipsec_tunnel_init_devices: "
  40491. + "registering device %s\n",
  40492. + dev_ipsec->name);
  40493. +
  40494. + /* reference and hold the device reference */
  40495. + dev_hold(dev_ipsec);
  40496. + ipsecdevices[i]=dev_ipsec;
  40497. +
  40498. + if (register_netdev(dev_ipsec) != 0) {
  40499. + KLIPS_PRINT(1 || debug_tunnel & DB_TN_INIT,
  40500. + "klips_debug:ipsec_tunnel_init_devices: "
  40501. + "registering device %s failed, quitting device init.\n",
  40502. + dev_ipsec->name);
  40503. + return -EIO;
  40504. + } else {
  40505. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40506. + "klips_debug:ipsec_tunnel_init_devices: "
  40507. + "registering device %s succeeded, continuing...\n",
  40508. + dev_ipsec->name);
  40509. + }
  40510. + }
  40511. + return 0;
  40512. +}
  40513. +
  40514. +/* void */
  40515. +int
  40516. +ipsec_tunnel_cleanup_devices(void)
  40517. +{
  40518. + int error = 0;
  40519. + int i;
  40520. + struct net_device *dev_ipsec;
  40521. +
  40522. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40523. + dev_ipsec = ipsecdevices[i];
  40524. + if(dev_ipsec == NULL) {
  40525. + continue;
  40526. + }
  40527. +
  40528. + /* release reference */
  40529. + ipsecdevices[i]=NULL;
  40530. + ipsec_dev_put(dev_ipsec);
  40531. +
  40532. + KLIPS_PRINT(debug_tunnel, "Unregistering %s (refcnt=%d)\n",
  40533. + dev_ipsec->name,
  40534. + atomic_read(&dev_ipsec->refcnt));
  40535. + unregister_netdev(dev_ipsec);
  40536. + KLIPS_PRINT(debug_tunnel, "Unregisted %s\n", dev_ipsec->name);
  40537. +#ifndef NETDEV_23
  40538. + kfree(dev_ipsec->name);
  40539. + dev_ipsec->name=NULL;
  40540. +#endif /* !NETDEV_23 */
  40541. + kfree(dev_ipsec->priv);
  40542. + dev_ipsec->priv=NULL;
  40543. + }
  40544. + return error;
  40545. +}
  40546. +
  40547. +/*
  40548. + * $Log: ipsec_tunnel.c,v $
  40549. + * Revision 1.232.2.5 2006/10/06 21:39:26 paul
  40550. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  40551. + * set. This is defined through autoconf.h which is included through the
  40552. + * linux kernel build macros.
  40553. + *
  40554. + * Revision 1.232.2.4 2006/03/28 20:58:19 ken
  40555. + * Fix for KLIPS on 2.6.16 - need to include <net/arp.h> now
  40556. + *
  40557. + * Revision 1.232.2.3 2006/02/15 05:14:12 paul
  40558. + * 568: uninitialized struct in ipsec_tunnel.c coud break routing under 2.6 kernels
  40559. + * ipsec_tunnel_send() calls the entry point function of routing subsystem
  40560. + * (ip_route_output_key()) using a not fully initialized struct of type
  40561. + * struct flowi.
  40562. + * This will cause a failure in routing packets through an ipsec interface
  40563. + * when patches for multipath routing from http://www.ssi.bg/~ja/
  40564. + * are applied.
  40565. + *
  40566. + * Revision 1.232.2.2 2005/11/22 04:11:52 ken
  40567. + * Backport fixes for 2.6.14 kernels from HEAD
  40568. + *
  40569. + * Revision 1.232.2.1 2005/09/21 22:57:43 paul
  40570. + * pulled up compile fix for 2.6.13
  40571. + *
  40572. + * Revision 1.232 2005/06/04 16:06:06 mcr
  40573. + * better patch for nat-t rcv-device code.
  40574. + *
  40575. + * Revision 1.231 2005/05/21 03:28:51 mcr
  40576. + * make sure that port-500 hole is used for port-4500 as well.
  40577. + *
  40578. + * Revision 1.230 2005/05/11 01:42:04 mcr
  40579. + * removal of debugging showed useless/wrong variables used.
  40580. + *
  40581. + * Revision 1.229 2005/04/29 05:10:22 mcr
  40582. + * removed from extraenous includes to make unit testing easier.
  40583. + *
  40584. + * Revision 1.228 2005/01/26 00:50:35 mcr
  40585. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  40586. + * and make sure that NAT_TRAVERSAL is set as well to match
  40587. + * userspace compiles of code.
  40588. + *
  40589. + * Revision 1.227 2004/12/10 21:16:08 ken
  40590. + * 64bit fixes from Opteron port of KLIPS 2.6
  40591. + *
  40592. + * Revision 1.226 2004/12/04 07:11:23 mcr
  40593. + * fix for snmp SIOCPRIVATE use of snmpd.
  40594. + * http://bugs.xelerance.com/view.php?id=144
  40595. + *
  40596. + * Revision 1.225 2004/12/03 21:25:57 mcr
  40597. + * compile time fixes for running on 2.6.
  40598. + * still experimental.
  40599. + *
  40600. + * Revision 1.224 2004/08/14 03:28:24 mcr
  40601. + * fixed log comment to remove warning about embedded comment.
  40602. + *
  40603. + * Revision 1.223 2004/08/04 15:57:07 mcr
  40604. + * moved des .h files to include/des/ *
  40605. + * included 2.6 protocol specific things
  40606. + * started at NAT-T support, but it will require a kernel patch.
  40607. + *
  40608. + * Revision 1.222 2004/08/03 18:19:08 mcr
  40609. + * in 2.6, use "net_device" instead of #define device->net_device.
  40610. + * this probably breaks 2.0 compiles.
  40611. + *
  40612. + * Revision 1.221 2004/07/10 19:11:18 mcr
  40613. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  40614. + *
  40615. + * Revision 1.220 2004/04/06 02:49:26 mcr
  40616. + * pullup of algo code from alg-branch.
  40617. + *
  40618. + * Revision 1.219 2004/02/03 03:13:17 mcr
  40619. + * minor edits for readability, and error reporting.
  40620. + *
  40621. + * Revision 1.218 2004/01/27 20:29:20 mcr
  40622. + * fix for unregister_netdev() problem for underlying eth0.
  40623. + *
  40624. + * Revision 1.217 2003/12/10 01:14:27 mcr
  40625. + * NAT-traversal patches to KLIPS.
  40626. + *
  40627. + * Revision 1.216 2003/12/04 23:01:17 mcr
  40628. + * removed ipsec_netlink.h
  40629. + *
  40630. + * Revision 1.215 2003/12/04 16:35:16 ken
  40631. + * Fix for ATM devices where physdev->hard_header_len *is* correct
  40632. + *
  40633. + * Revision 1.214 2003/11/25 23:52:37 mcr
  40634. + * fix typo in patch - ixs-> needed.
  40635. + *
  40636. + * Revision 1.213 2003/11/24 18:25:49 mcr
  40637. + * patch from willy@w.ods.org to fix problems with ATM interfaces.
  40638. + *
  40639. + * Revision 1.212 2003/10/31 02:27:55 mcr
  40640. + * pulled up port-selector patches and sa_id elimination.
  40641. + *
  40642. + * Revision 1.211.2.2 2003/10/29 01:30:41 mcr
  40643. + * elimited "struct sa_id".
  40644. + *
  40645. + * Revision 1.211.2.1 2003/09/21 13:59:56 mcr
  40646. + * pre-liminary X.509 patch - does not yet pass tests.
  40647. + *
  40648. + * Revision 1.211 2003/09/10 16:46:30 mcr
  40649. + * patches for 2.4 backport/2.6 existence.
  40650. + *
  40651. + * Revision 1.210 2003/07/31 22:47:16 mcr
  40652. + * preliminary (untested by FS-team) 2.5 patches.
  40653. + *
  40654. + * Revision 1.209 2003/06/22 21:28:43 mcr
  40655. + * inability to unload module was caused by calls to dev_get
  40656. + * (ipsec_dev_get), to gather a device from a name. There is
  40657. + * simply no reason to look the devices up - they should be kept
  40658. + * in a nice array, ready for use.
  40659. + *
  40660. + * Revision 1.208 2003/06/22 21:25:07 mcr
  40661. + * all staticly counted ipsecXXX device support removed.
  40662. + *
  40663. + * Revision 1.207 2003/04/02 20:15:37 mcr
  40664. + * fix for PR#204 - do not clear connection tracking info if we
  40665. + * the packet is being sent in the clear.
  40666. + *
  40667. + * Revision 1.206 2003/02/12 19:32:51 rgb
  40668. + * Refactored file to:
  40669. + * ipsec_xmit.c
  40670. + * ipsec_xmit.h
  40671. + * ipsec_mast.c
  40672. + *
  40673. + * Revision 1.205 2003/02/06 17:47:00 rgb
  40674. + *
  40675. + * Remove unused ipsec_tunnel_lock() and ipsec_tunnel_unlock() code.
  40676. + * Refactor ipsec_tunnel_start_xmit() further into:
  40677. + * ipsec_xmit_sanity_check_dev()
  40678. + * ipsec_xmit_sanity_check_skb()
  40679. + * ipsec_xmit_strip_hard_header()
  40680. + * ipsec_xmit_restore_hard_header()
  40681. + * ipsec_xmit_send()
  40682. + * ipsec_xmit_cleanup()
  40683. + * and start a skeletal ipsec_mast_start_xmit() .
  40684. + *
  40685. + * Revision 1.204 2003/02/06 06:43:46 rgb
  40686. + *
  40687. + * Refactor ipsec_tunnel_start_xmit, bringing out:
  40688. + * ipsec_xmit_SAlookup
  40689. + * ipsec_xmit_encap_once
  40690. + * ipsec_xmit_encap_bundle
  40691. + *
  40692. + * Revision 1.203 2003/02/06 02:21:34 rgb
  40693. + *
  40694. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  40695. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  40696. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  40697. + *
  40698. + * Revision 1.202 2003/01/03 07:38:01 rgb
  40699. + *
  40700. + * Start to refactor ipsec_tunnel_start_xmit() by putting local variables
  40701. + * into struct ipsec_xmit_state and renaming a few variables to give more
  40702. + * unique or searchable names.
  40703. + *
  40704. + * Revision 1.201 2003/01/03 00:31:28 rgb
  40705. + *
  40706. + * Clean up memset usage, including fixing 2 places where keys were not
  40707. + * properly wiped.
  40708. + *
  40709. + * Revision 1.200 2002/12/06 02:24:02 mcr
  40710. + * patches for compiling against SUSE 8.1 kernels. Requires
  40711. + * an additional -DSUSE_LINUX_2_4_19_IS_STUPID.
  40712. + *
  40713. + * Revision 1.199 2002/10/12 23:11:53 dhr
  40714. + *
  40715. + * [KenB + DHR] more 64-bit cleanup
  40716. + *
  40717. + * Revision 1.198 2002/10/05 05:02:58 dhr
  40718. + *
  40719. + * C labels go on statements
  40720. + *
  40721. + * Revision 1.197 2002/09/20 05:01:50 rgb
  40722. + * Added compiler directive to switch on IP options and fix IP options bug.
  40723. + * Make ip->ihl treatment consistent using shifts rather than multiplications.
  40724. + * Check for large enough packet before accessing udp header for IKE bypass.
  40725. + * Added memory allocation debugging.
  40726. + * Fixed potential memory allocation failure-induced oops.
  40727. + *
  40728. + * Revision 1.196 2002/07/24 18:44:54 rgb
  40729. + * Type fiddling to tame ia64 compiler.
  40730. + *
  40731. + * Revision 1.195 2002/07/23 03:36:07 rgb
  40732. + * Fixed 2.2 device initialisation hang.
  40733. + *
  40734. + * Revision 1.194 2002/05/27 21:40:34 rgb
  40735. + * Set unused ipsec devices to ARPHRD_VOID to avoid confusing iproute2.
  40736. + * Cleaned up intermediate step to dynamic device allocation.
  40737. + *
  40738. + * Revision 1.193 2002/05/27 19:31:36 rgb
  40739. + * Convert to dynamic ipsec device allocation.
  40740. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  40741. + *
  40742. + * Revision 1.192 2002/05/23 07:14:28 rgb
  40743. + * Added refcount code.
  40744. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  40745. + *
  40746. + * Revision 1.191 2002/05/14 02:34:37 rgb
  40747. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  40748. + * ipsec_sa or ipsec_sa.
  40749. + *
  40750. + * Revision 1.190 2002/04/24 07:55:32 mcr
  40751. + * #include patches and Makefiles for post-reorg compilation.
  40752. + *
  40753. + * Revision 1.189 2002/04/24 07:36:32 mcr
  40754. + * Moved from ./klips/net/ipsec/ipsec_tunnel.c,v
  40755. + *
  40756. + * Revision 1.188 2002/04/20 00:12:25 rgb
  40757. + * Added esp IV CBC attack fix, disabled.
  40758. + *
  40759. + * Revision 1.187 2002/03/23 19:55:17 rgb
  40760. + * Fix for 2.2 local IKE fragmentation blackhole. Still won't work if
  40761. + * iptraf or another pcap app is running.
  40762. + *
  40763. + * Revision 1.186 2002/03/19 03:26:22 rgb
  40764. + * Applied DHR's tunnel patch to streamline IKE/specialSA processing.
  40765. + *
  40766. + * Revision 1.185 2002/02/20 04:13:05 rgb
  40767. + * Send back ICMP_PKT_FILTERED upon %reject.
  40768. + *
  40769. + * Revision 1.184 2002/01/29 17:17:56 mcr
  40770. + * moved include of ipsec_param.h to after include of linux/kernel.h
  40771. + * otherwise, it seems that some option that is set in ipsec_param.h
  40772. + * screws up something subtle in the include path to kernel.h, and
  40773. + * it complains on the snprintf() prototype.
  40774. + *
  40775. + * Revision 1.183 2002/01/29 04:00:53 mcr
  40776. + * more excise of kversions.h header.
  40777. + *
  40778. + * Revision 1.182 2002/01/29 02:13:18 mcr
  40779. + * introduction of ipsec_kversion.h means that include of
  40780. + * ipsec_param.h must preceed any decisions about what files to
  40781. + * include to deal with differences in kernel source.
  40782. + *
  40783. + * Revision 1.181 2002/01/07 20:00:33 rgb
  40784. + * Added IKE destination port debugging.
  40785. + *
  40786. + * Revision 1.180 2001/12/21 21:49:54 rgb
  40787. + * Fixed bug as a result of moving IKE bypass above %trap/%hold code.
  40788. + *
  40789. + * Revision 1.179 2001/12/19 21:08:14 rgb
  40790. + * Added transport protocol ports to ipsec_print_ip().
  40791. + * Update eroute info for non-SA targets.
  40792. + * Added obey DF code disabled.
  40793. + * Fixed formatting bugs in ipsec_tunnel_hard_header().
  40794. + *
  40795. + * Revision 1.178 2001/12/05 09:36:10 rgb
  40796. + * Moved the UDP/500 IKE check just above the %hold/%trap checks to avoid
  40797. + * IKE packets being stolen by the %hold (and returned to the sending KMd
  40798. + * in an ACQUIRE, ironically ;-).
  40799. + *
  40800. + * Revision 1.177 2001/11/26 09:23:50 rgb
  40801. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  40802. + *
  40803. + * Revision 1.170.2.1 2001/09/25 02:28:27 mcr
  40804. + * struct tdb -> struct ipsec_sa.
  40805. + * lifetime checks moved to common routines.
  40806. + * cleaned up includes.
  40807. + *
  40808. + * Revision 1.170.2.2 2001/10/22 21:08:01 mcr
  40809. + * include des.h, removed phony prototypes and fixed calling
  40810. + * conventions to match real prototypes.
  40811. + *
  40812. + * Revision 1.176 2001/11/09 18:32:31 rgb
  40813. + * Added Hans Schultz' fragmented UDP/500 IKE socket port selector.
  40814. + *
  40815. + * Revision 1.175 2001/11/06 20:47:00 rgb
  40816. + * Added Eric Espie's TRAPSUBNET fix, minus spin-lock-bh dabbling.
  40817. + *
  40818. + * Revision 1.174 2001/11/06 19:50:43 rgb
  40819. + * Moved IP_SEND, ICMP_SEND, DEV_QUEUE_XMIT macros to ipsec_tunnel.h for
  40820. + * use also by pfkey_v2_parser.c
  40821. + *
  40822. + * Revision 1.173 2001/10/29 21:53:44 henry
  40823. + * tone down the device-down message slightly, until we can make it smarter
  40824. + *
  40825. + * Revision 1.172 2001/10/26 04:59:37 rgb
  40826. + * Added a critical level syslog message if an ipsec device goes down.
  40827. + *
  40828. + * Revision 1.171 2001/10/18 04:45:21 rgb
  40829. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  40830. + * lib/freeswan.h version macros moved to lib/kversions.h.
  40831. + * Other compiler directive cleanups.
  40832. + *
  40833. + * Revision 1.170 2001/09/25 00:09:50 rgb
  40834. + * Added NetCelo's TRAPSUBNET code to convert a new type TRAPSUBNET into a
  40835. + * HOLD.
  40836. + *
  40837. + * Revision 1.169 2001/09/15 16:24:05 rgb
  40838. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  40839. + *
  40840. + * Revision 1.168 2001/09/14 16:58:37 rgb
  40841. + * Added support for storing the first and last packets through a HOLD.
  40842. + *
  40843. + * Revision 1.167 2001/09/08 21:13:33 rgb
  40844. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  40845. + *
  40846. + * Revision 1.166 2001/08/27 19:47:59 rgb
  40847. + * Clear tdb before usage.
  40848. + * Added comment: clear IF before calling routing?
  40849. + *
  40850. + * Revision 1.165 2001/07/03 01:23:53 rgb
  40851. + * Send back ICMP iff DF set, !ICMP, offset==0, sysctl_icmp, iph->tot_len >
  40852. + * emtu, and don't drop.
  40853. + *
  40854. + * Revision 1.164 2001/06/14 19:35:10 rgb
  40855. + * Update copyright date.
  40856. + *
  40857. + * Revision 1.163 2001/06/06 20:28:51 rgb
  40858. + * Added sanity checks for NULL skbs and devices.
  40859. + * Added more debugging output to various functions.
  40860. + * Removed redundant dev->priv argument to ipsec_tunnel_{at,de}tach().
  40861. + * Renamed ipsec_tunnel_attach() virtual and physical device arguments.
  40862. + * Corrected neigh_setup() device function assignment.
  40863. + * Keep valid pointers to ipsec_tunnel_*() on detach.
  40864. + * Set dev->type to the originally-initiallised value.
  40865. + *
  40866. + * Revision 1.162 2001/06/01 07:28:04 rgb
  40867. + * Added sanity checks for detached devices. Don't down virtual devices
  40868. + * to prevent packets going out in the clear if the detached device comes
  40869. + * back up.
  40870. + *
  40871. + * Revision 1.161 2001/05/30 08:14:52 rgb
  40872. + * Removed vestiges of esp-null transforms.
  40873. + * NetDev Notifier instrumentation to track down disappearing devices.
  40874. + *
  40875. + * Revision 1.160 2001/05/29 05:15:12 rgb
  40876. + * Added SS' PMTU patch which notifies sender if packet doesn't fit
  40877. + * physical MTU (if it wasn't ICMP) and then drops it.
  40878. + *
  40879. + * Revision 1.159 2001/05/27 06:12:12 rgb
  40880. + * Added structures for pid, packet count and last access time to eroute.
  40881. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  40882. + *
  40883. + * Revision 1.158 2001/05/24 05:39:33 rgb
  40884. + * Applied source zeroing to 2.2 ip_route_output() call as well to enable
  40885. + * PASS eroutes for opportunism.
  40886. + *
  40887. + * Revision 1.157 2001/05/23 22:35:28 rgb
  40888. + * 2.4 source override simplification.
  40889. + *
  40890. + * Revision 1.156 2001/05/23 21:41:31 rgb
  40891. + * Added error return code printing on ip_route_output().
  40892. + *
  40893. + * Revision 1.155 2001/05/23 05:09:13 rgb
  40894. + * Fixed incorrect ip_route_output() failure message.
  40895. + *
  40896. + * Revision 1.154 2001/05/21 14:53:31 rgb
  40897. + * Added debug statement for case when ip_route_output() fails, causing
  40898. + * packet to be dropped, but log looked ok.
  40899. + *
  40900. + * Revision 1.153 2001/05/19 02:37:54 rgb
  40901. + * Fixed missing comment termination.
  40902. + *
  40903. + * Revision 1.152 2001/05/19 02:35:50 rgb
  40904. + * Debug code optimisation for non-debug speed.
  40905. + * Kernel version compiler define comments.
  40906. + * 2.2 and 2.4 kernel ip_send device and ip debug output added.
  40907. + *
  40908. + * Revision 1.151 2001/05/18 16:17:35 rgb
  40909. + * Changed reference from "magic" to "shunt" SAs.
  40910. + *
  40911. + * Revision 1.150 2001/05/18 16:12:19 rgb
  40912. + * Changed UDP/500 bypass test from 3 nested ifs to one anded if.
  40913. + *
  40914. + * Revision 1.149 2001/05/16 04:39:33 rgb
  40915. + * Add default == eroute.dest to IKE bypass conditions for magic eroutes.
  40916. + *
  40917. + * Revision 1.148 2001/05/05 03:31:41 rgb
  40918. + * IP frag debugging updates and enhancements.
  40919. + *
  40920. + * Revision 1.147 2001/05/03 19:41:40 rgb
  40921. + * Added SS' skb_cow fix for 2.4.4.
  40922. + *
  40923. + * Revision 1.146 2001/04/30 19:28:16 rgb
  40924. + * Update for 2.4.4. ip_select_ident() now has 3 args.
  40925. + *
  40926. + * Revision 1.145 2001/04/23 14:56:10 rgb
  40927. + * Added spin_lock() check to prevent double-locking for multiple
  40928. + * transforms and hence kernel lock-ups with SMP kernels.
  40929. + *
  40930. + * Revision 1.144 2001/04/21 23:04:45 rgb
  40931. + * Define out skb->used for 2.4 kernels.
  40932. + * Check if soft expire has already been sent before sending another to
  40933. + * prevent ACQUIRE flooding.
  40934. + *
  40935. + * Revision 1.143 2001/03/16 07:37:21 rgb
  40936. + * Added comments to all #endifs.
  40937. + *
  40938. + * Revision 1.142 2001/02/28 05:03:27 rgb
  40939. + * Clean up and rationalise startup messages.
  40940. + *
  40941. + * Revision 1.141 2001/02/27 22:24:54 rgb
  40942. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  40943. + * Check for satoa() return codes.
  40944. + *
  40945. + * Revision 1.140 2001/02/27 06:40:12 rgb
  40946. + * Fixed TRAP->HOLD eroute byte order.
  40947. + *
  40948. + * Revision 1.139 2001/02/26 20:38:59 rgb
  40949. + * Added compiler defines for 2.4.x-specific code.
  40950. + *
  40951. + * Revision 1.138 2001/02/26 19:57:27 rgb
  40952. + * Implement magic SAs %drop, %reject, %trap, %hold, %pass as part
  40953. + * of the new SPD and to support opportunistic.
  40954. + * Drop sysctl_ipsec_{no_eroute_pass,opportunistic}, replaced by magic SAs.
  40955. + *
  40956. + * Revision 1.137 2001/02/19 22:29:49 rgb
  40957. + * Fixes for presence of active ipv6 segments which share ipsec physical
  40958. + * device (gg).
  40959. + *
  40960. + * Revision 1.136 2001/01/29 22:30:38 rgb
  40961. + * Fixed minor acquire debug printing bug.
  40962. + *
  40963. + * Revision 1.135 2001/01/29 22:19:45 rgb
  40964. + * Zero source address for 2.4 bypass route lookup.
  40965. + *
  40966. + * Revision 1.134 2001/01/23 20:19:49 rgb
  40967. + * 2.4 fix to remove removed is_clone member.
  40968. + *
  40969. + * Revision 1.133 2000/12/09 22:08:35 rgb
  40970. + * Fix NET_23 bug, should be NETDEV_23.
  40971. + *
  40972. + * Revision 1.132 2000/12/01 06:54:50 rgb
  40973. + * Fix for new 2.4 IP TTL default variable name.
  40974. + *
  40975. + * Revision 1.131 2000/11/09 20:52:15 rgb
  40976. + * More spinlock shuffling, locking earlier and unlocking later in rcv to
  40977. + * include ipcomp and prevent races, renaming some tdb variables that got
  40978. + * forgotten, moving some unlocks to include tdbs and adding a missing
  40979. + * unlock. Thanks to Svenning for some of these.
  40980. + *
  40981. + * Revision 1.130 2000/11/09 20:11:22 rgb
  40982. + * Minor shuffles to fix non-standard kernel config option selection.
  40983. + *
  40984. + * Revision 1.129 2000/11/06 04:32:49 rgb
  40985. + * Clean up debug printing.
  40986. + * Copy skb->protocol for all kernel versions.
  40987. + * Ditched spin_lock_irqsave in favour of spin_lock.
  40988. + * Disabled TTL decrement, done in ip_forward.
  40989. + * Added debug printing before pfkey_acquire().
  40990. + * Fixed printk-deltdbchain-spin_lock races (Svenning).
  40991. + * Use defaultTTL for 2.1+ kernels.
  40992. + * Add Svenning's adaptive content compression.
  40993. + * Fix up debug display arguments.
  40994. + *
  40995. + * Revision 1.128 2000/09/28 00:58:57 rgb
  40996. + * Moved the IKE passthrough check after the eroute lookup so we can pass
  40997. + * IKE through intermediate tunnels.
  40998. + *
  40999. + * Revision 1.127 2000/09/22 17:52:11 rgb
  41000. + * Fixed misleading ipcomp debug output.
  41001. + *
  41002. + * Revision 1.126 2000/09/22 04:22:56 rgb
  41003. + * Fixed dumb spi->cpi conversion error.
  41004. + *
  41005. + * Revision 1.125 2000/09/21 04:34:48 rgb
  41006. + * A few debug-specific things should be hidden under
  41007. + * CONFIG_IPSEC_DEBUG.(MB)
  41008. + * Improved ip_send() error handling.(MB)
  41009. + *
  41010. + * Revision 1.124 2000/09/21 03:40:58 rgb
  41011. + * Added more debugging to try and track down the cpi outward copy problem.
  41012. + *
  41013. + * Revision 1.123 2000/09/19 07:08:49 rgb
  41014. + * Added debugging to outgoing compression report.
  41015. + *
  41016. + * Revision 1.122 2000/09/18 19:21:26 henry
  41017. + * RGB-supplied fix for RH5.2 problem
  41018. + *
  41019. + * Revision 1.121 2000/09/17 21:05:09 rgb
  41020. + * Added tdb to skb_compress call to write in cpi.
  41021. + *
  41022. + * Revision 1.120 2000/09/17 16:57:16 rgb
  41023. + * Added Svenning's patch to remove restriction of ipcomp to innermost
  41024. + * transform.
  41025. + *
  41026. + * Revision 1.119 2000/09/15 11:37:01 rgb
  41027. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  41028. + * IPCOMP zlib deflate code.
  41029. + *
  41030. + * Revision 1.118 2000/09/15 04:57:16 rgb
  41031. + * Moved debug output after sanity check.
  41032. + * Added tos copy sysctl.
  41033. + *
  41034. + * Revision 1.117 2000/09/12 03:22:51 rgb
  41035. + * Converted ipsec_icmp, no_eroute_pass, opportunistic and #if0 debugs to
  41036. + * sysctl.
  41037. + *
  41038. + * Revision 1.116 2000/09/08 19:18:19 rgb
  41039. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  41040. + * Added outgoing opportunistic hook, ifdef'ed out.
  41041. + *
  41042. + * Revision 1.115 2000/08/30 05:27:29 rgb
  41043. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  41044. + * Kill remainder of tdb_xform, tdb_xdata, xformsw.
  41045. + *
  41046. + * Revision 1.114 2000/08/28 18:15:46 rgb
  41047. + * Added MB's nf-debug reset patch.
  41048. + *
  41049. + * Revision 1.113 2000/08/27 02:26:40 rgb
  41050. + * Send all no-eroute-bypass, pluto-bypass and passthrough packets through
  41051. + * fragmentation machinery for 2.0, 2.2 and 2.4 kernels.
  41052. + *
  41053. + * Revision 1.112 2000/08/20 21:37:33 rgb
  41054. + * Activated pfkey_expire() calls.
  41055. + * Added a hard/soft expiry parameter to pfkey_expire(). (Momchil)
  41056. + * Re-arranged the order of soft and hard expiry to conform to RFC2367.
  41057. + * Clean up references to CONFIG_IPSEC_PFKEYv2.
  41058. + *
  41059. + * Revision 1.111 2000/08/01 14:51:51 rgb
  41060. + * Removed _all_ remaining traces of DES.
  41061. + *
  41062. + * Revision 1.110 2000/07/28 14:58:31 rgb
  41063. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  41064. + *
  41065. + * Revision 1.109 2000/07/28 13:50:54 rgb
  41066. + * Changed enet_statistics to net_device_stats and added back compatibility
  41067. + * for pre-2.1.19.
  41068. + *
  41069. + * Revision 1.108 2000/05/16 03:03:11 rgb
  41070. + * Updates for 2.3.99pre8 from MB.
  41071. + *
  41072. + * Revision 1.107 2000/05/10 23:08:21 rgb
  41073. + * Print a debug warning about bogus packets received by the outgoing
  41074. + * processing machinery only when klipsdebug is not set to none.
  41075. + * Comment out the device initialisation informational messages.
  41076. + *
  41077. + * Revision 1.106 2000/05/10 19:17:14 rgb
  41078. + * Define an IP_SEND macro, intending to have all packet passthroughs
  41079. + * use fragmentation. This didn't quite work, but is a step in the
  41080. + * right direction.
  41081. + * Added buffer allocation debugging statements.
  41082. + * Added configure option to shut off no eroute passthrough.
  41083. + * Only check usetime against soft and hard limits if the tdb has been
  41084. + * used.
  41085. + * Cast output of ntohl so that the broken prototype doesn't make our
  41086. + * compile noisy.
  41087. + *
  41088. + * Revision 1.105 2000/03/22 16:15:37 rgb
  41089. + * Fixed renaming of dev_get (MB).
  41090. + *
  41091. + * Revision 1.104 2000/03/16 14:04:15 rgb
  41092. + * Indented headers for readability.
  41093. + * Fixed debug scope to enable compilation with debug off.
  41094. + * Added macros for ip_chk_addr and IS_MYADDR for identifying self.
  41095. + *
  41096. + * Revision 1.103 2000/03/16 07:11:07 rgb
  41097. + * Hardcode PF_KEYv2 support.
  41098. + * Fixed bug which allowed UDP/500 packet from another machine
  41099. + * through in the clear.
  41100. + * Added disabled skb->protocol fix for ISDN/ASYNC PPP from Matjaz Godec.
  41101. + *
  41102. + * Revision 1.102 2000/03/14 12:26:59 rgb
  41103. + * Added skb->nfct support for clearing netfilter conntrack bits (MB).
  41104. + *
  41105. + * Revision 1.101 2000/02/14 21:05:22 rgb
  41106. + * Added MB's netif_queue fix for kernels 2.3.43+.
  41107. + *
  41108. + * Revision 1.100 2000/01/26 10:04:57 rgb
  41109. + * Fixed noisy 2.0 printk arguments.
  41110. + *
  41111. + * Revision 1.99 2000/01/21 06:16:25 rgb
  41112. + * Added sanity checks on skb_push(), skb_pull() to prevent panics.
  41113. + * Switched to AF_ENCAP macro.
  41114. + * Shortened debug output per packet and re-arranging debug_tunnel
  41115. + * bitmap flags, while retaining necessary information to avoid
  41116. + * trampling the kernel print ring buffer.
  41117. + * Reformatted recursion switch code.
  41118. + * Changed all references to tdb_proto to tdb_said.proto for clarity.
  41119. + *
  41120. + * Revision 1.98 2000/01/13 08:09:31 rgb
  41121. + * Shuffled debug_tunnel switches to focus output.
  41122. + * Fixed outgoing recursion bug, limiting to recursing only if the remote
  41123. + * SG changes and if it is valid, ie. not passthrough.
  41124. + * Clarified a number of debug messages.
  41125. + *
  41126. + * Revision 1.97 2000/01/10 16:37:16 rgb
  41127. + * MB support for new ip_select_ident() upon disappearance of
  41128. + * ip_id_count in 2.3.36+.
  41129. + *
  41130. + * Revision 1.96 1999/12/31 14:59:08 rgb
  41131. + * MB fix to use new skb_copy_expand in kernel 2.3.35.
  41132. + *
  41133. + * Revision 1.95 1999/12/29 21:15:44 rgb
  41134. + * Fix tncfg to aliased device bug.
  41135. + *
  41136. + * Revision 1.94 1999/12/22 04:26:06 rgb
  41137. + * Converted all 'static' functions to 'DEBUG_NO_STATIC' to enable
  41138. + * debugging by providing external labels to all functions with debugging
  41139. + * turned on.
  41140. + *
  41141. + * Revision 1.93 1999/12/13 13:30:14 rgb
  41142. + * Changed MTU reports and HW address reporting back to debug only.
  41143. + *
  41144. + * Revision 1.92 1999/12/07 18:57:56 rgb
  41145. + * Fix PFKEY symbol compile error (SADB_*) without pfkey enabled.
  41146. + *
  41147. + * Revision 1.91 1999/12/01 22:15:36 rgb
  41148. + * Add checks for LARVAL and DEAD SAs.
  41149. + * Change state of SA from MATURE to DYING when a soft lifetime is
  41150. + * reached and print debug warning.
  41151. + *
  41152. + * Revision 1.90 1999/11/23 23:04:04 rgb
  41153. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  41154. + * Sort out pfkey and freeswan headers, putting them in a library path.
  41155. + *
  41156. + * Revision 1.89 1999/11/18 18:50:59 rgb
  41157. + * Changed all device registrations for static linking to
  41158. + * dynamic to reduce the number and size of patches.
  41159. + *
  41160. + * Revision 1.88 1999/11/18 04:09:19 rgb
  41161. + * Replaced all kernel version macros to shorter, readable form.
  41162. + *
  41163. + * Revision 1.87 1999/11/17 15:53:40 rgb
  41164. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  41165. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  41166. + * klips/net/ipsec/Makefile.
  41167. + *
  41168. + * Revision 1.86 1999/10/16 18:25:37 rgb
  41169. + * Moved SA lifetime expiry checks before packet processing.
  41170. + * Expire SA on replay counter rollover.
  41171. + *
  41172. + * Revision 1.85 1999/10/16 04:24:31 rgb
  41173. + * Add stats for time since last packet.
  41174. + *
  41175. + * Revision 1.84 1999/10/16 00:30:47 rgb
  41176. + * Added SA lifetime counting.
  41177. + *
  41178. + * Revision 1.83 1999/10/15 22:15:57 rgb
  41179. + * Clean out cruft.
  41180. + * Add debugging.
  41181. + *
  41182. + * Revision 1.82 1999/10/08 18:26:19 rgb
  41183. + * Fix 2.0.3x outgoing fragmented packet memory leak.
  41184. + *
  41185. + * Revision 1.81 1999/10/05 02:38:54 rgb
  41186. + * Lower the default mtu of virtual devices to 16260.
  41187. + *
  41188. + * Revision 1.80 1999/10/03 18:56:41 rgb
  41189. + * Spinlock support for 2.3.xx.
  41190. + * Don't forget to undo spinlocks on error!
  41191. + * Check for valid eroute before copying the structure.
  41192. + *
  41193. + * Revision 1.79 1999/10/01 15:44:53 rgb
  41194. + * Move spinlock header include to 2.1> scope.
  41195. + *
  41196. + * Revision 1.78 1999/10/01 00:02:43 rgb
  41197. + * Added tdb structure locking.
  41198. + * Added eroute structure locking.
  41199. + *
  41200. + * Revision 1.77 1999/09/30 02:52:29 rgb
  41201. + * Add Marc Boucher's Copy-On-Write code (same as ipsec_rcv.c).
  41202. + *
  41203. + * Revision 1.76 1999/09/25 19:31:27 rgb
  41204. + * Refine MSS hack to affect SYN, but not SYN+ACK packets.
  41205. + *
  41206. + * Revision 1.75 1999/09/24 22:52:38 rgb
  41207. + * Fix two things broken in 2.0.38 by trying to fix network notifiers.
  41208. + *
  41209. + * Revision 1.74 1999/09/24 00:30:37 rgb
  41210. + * Add test for changed source as well as destination to check for
  41211. + * recursion.
  41212. + *
  41213. + * Revision 1.73 1999/09/23 20:52:24 rgb
  41214. + * Add James Morris' MSS hack patch, disabled.
  41215. + *
  41216. + * Revision 1.72 1999/09/23 20:22:40 rgb
  41217. + * Enable, tidy and fix network notifier code.
  41218. + *
  41219. + * Revision 1.71 1999/09/23 18:09:05 rgb
  41220. + * Clean up 2.2.x fragmenting traces.
  41221. + * Disable dev->type switching, forcing ARPHRD_TUNNEL.
  41222. + *
  41223. + * Revision 1.70 1999/09/22 14:14:24 rgb
  41224. + * Add sanity checks for revectored calls to prevent calling a downed I/F.
  41225. + *
  41226. + * Revision 1.69 1999/09/21 15:00:57 rgb
  41227. + * Add Marc Boucher's packet size check.
  41228. + * Flesh out network device notifier code.
  41229. + *
  41230. + * Revision 1.68 1999/09/18 11:39:57 rgb
  41231. + * Start to add (disabled) netdevice notifier code.
  41232. + *
  41233. + * Revision 1.67 1999/09/17 23:44:40 rgb
  41234. + * Add a comment warning potential code hackers to stay away from mac.raw.
  41235. + *
  41236. + * Revision 1.66 1999/09/17 18:04:02 rgb
  41237. + * Add fix for unpredictable hard_header_len for ISDN folks (thanks MB).
  41238. + * Ditch TTL decrement in 2.2 (MB).
  41239. + *
  41240. + * Revision 1.65 1999/09/15 23:15:35 henry
  41241. + * Marc Boucher's PPP fixes
  41242. + *
  41243. + * Revision 1.64 1999/09/07 13:40:53 rgb
  41244. + * Ditch unreliable references to skb->mac.raw.
  41245. + *
  41246. + * Revision 1.63 1999/08/28 11:33:09 rgb
  41247. + * Check for null skb->mac pointer.
  41248. + *
  41249. + * Revision 1.62 1999/08/28 02:02:30 rgb
  41250. + * Add Marc Boucher's fix for properly dealing with skb->sk.
  41251. + *
  41252. + * Revision 1.61 1999/08/27 05:23:05 rgb
  41253. + * Clean up skb->data/raw/nh/h manipulation.
  41254. + * Add Marc Boucher's mods to aid tcpdump.
  41255. + * Add sanity checks to skb->raw/nh/h pointer copies in skb_copy_expand.
  41256. + * Re-order hard_header stripping -- might be able to remove it...
  41257. + *
  41258. + * Revision 1.60 1999/08/26 20:01:02 rgb
  41259. + * Tidy up compiler directives and macros.
  41260. + * Re-enable ICMP for tunnels where inner_dst != outer_dst.
  41261. + * Remove unnecessary skb->dev = physdev assignment affecting 2.2.x.
  41262. + *
  41263. + * Revision 1.59 1999/08/25 15:44:41 rgb
  41264. + * Clean up from 2.2.x instrumenting for compilation under 2.0.36.
  41265. + *
  41266. + * Revision 1.58 1999/08/25 15:00:54 rgb
  41267. + * Add dst cache code for 2.2.xx.
  41268. + * Add sanity check for skb packet header pointers.
  41269. + * Add/modify debugging instrumentation to *_start_xmit, *_hard_header and
  41270. + * *_rebuild_header.
  41271. + * Add neigh_* cache code.
  41272. + * Change dev->type back to ARPHRD_TUNNEL.
  41273. + *
  41274. + * Revision 1.57 1999/08/17 21:50:23 rgb
  41275. + * Fixed minor debug output bugs.
  41276. + * Regrouped error recovery exit code.
  41277. + * Added compiler directives to remove unwanted code and symbols.
  41278. + * Shut off ICMP messages: to be refined to only send ICMP to remote systems.
  41279. + * Add debugging code for output function addresses.
  41280. + * Fix minor bug in (possibly unused) header_cache_bind function.
  41281. + * Add device neighbour caching code.
  41282. + * Change dev->type from ARPHRD_TUNNEL to physdev->type.
  41283. + *
  41284. + * Revision 1.56 1999/08/03 17:22:56 rgb
  41285. + * Debug output clarification using KERN_* macros. Other inactive changes
  41286. + * added.
  41287. + *
  41288. + * Revision 1.55 1999/08/03 16:58:46 rgb
  41289. + * Fix skb_copy_expand size bug. Was getting incorrect size.
  41290. + *
  41291. + * Revision 1.54 1999/07/14 19:32:38 rgb
  41292. + * Fix oversize packet crash and ssh stalling in 2.2.x kernels.
  41293. + *
  41294. + * Revision 1.53 1999/06/10 15:44:02 rgb
  41295. + * Minor reformatting and clean-up.
  41296. + *
  41297. + * Revision 1.52 1999/05/09 03:25:36 rgb
  41298. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  41299. + *
  41300. + * Revision 1.51 1999/05/08 21:24:59 rgb
  41301. + * Add casting to silence the 2.2.x compile.
  41302. + *
  41303. + * Revision 1.50 1999/05/05 22:02:32 rgb
  41304. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  41305. + *
  41306. + * Revision 1.49 1999/04/29 15:18:52 rgb
  41307. + * Change gettdb parameter to a pointer to reduce stack loading and
  41308. + * facilitate parameter sanity checking.
  41309. + * Fix undetected bug that might have tried to access a null pointer.
  41310. + * Eliminate unnessessary usage of tdb_xform member to further switch
  41311. + * away from the transform switch to the algorithm switch.
  41312. + * Add return values to init and cleanup functions.
  41313. + *
  41314. + * Revision 1.48 1999/04/16 15:38:00 rgb
  41315. + * Minor rearrangement of freeing code to avoid memory leaks with impossible or
  41316. + * rare situations.
  41317. + *
  41318. + * Revision 1.47 1999/04/15 15:37:25 rgb
  41319. + * Forward check changes from POST1_00 branch.
  41320. + *
  41321. + * Revision 1.32.2.4 1999/04/13 21:00:18 rgb
  41322. + * Ditch 'things I wish I had known before...'.
  41323. + *
  41324. + * Revision 1.32.2.3 1999/04/13 20:34:38 rgb
  41325. + * Free skb after fragmentation.
  41326. + * Use stats more effectively.
  41327. + * Add I/F to mtu notch-down reporting.
  41328. + *
  41329. + * Revision 1.32.2.2 1999/04/02 04:26:14 rgb
  41330. + * Backcheck from HEAD, pre1.0.
  41331. + *
  41332. + * Revision 1.46 1999/04/11 00:29:00 henry
  41333. + * GPL boilerplate
  41334. + *
  41335. + * Revision 1.45 1999/04/07 15:42:01 rgb
  41336. + * Fix mtu/ping bug AGAIN!
  41337. + *
  41338. + * Revision 1.44 1999/04/06 04:54:27 rgb
  41339. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  41340. + * patch shell fixes.
  41341. + *
  41342. + * Revision 1.43 1999/04/04 03:57:07 rgb
  41343. + * ip_fragment() doesn't free the supplied skb. Freed.
  41344. + *
  41345. + * Revision 1.42 1999/04/01 23:27:15 rgb
  41346. + * Preload size of virtual mtu.
  41347. + *
  41348. + * Revision 1.41 1999/04/01 09:31:23 rgb
  41349. + * Invert meaning of ICMP PMTUD config option and clarify.
  41350. + * Code clean-up.
  41351. + *
  41352. + * Revision 1.40 1999/04/01 04:37:17 rgb
  41353. + * SSH stalling bug fix.
  41354. + *
  41355. + * Revision 1.39 1999/03/31 23:44:28 rgb
  41356. + * Don't send ICMP on DF and frag_off.
  41357. + *
  41358. + * Revision 1.38 1999/03/31 15:20:10 rgb
  41359. + * Quiet down debugging.
  41360. + *
  41361. + * Revision 1.37 1999/03/31 08:30:31 rgb
  41362. + * Add switch to shut off ICMP PMTUD packets.
  41363. + *
  41364. + * Revision 1.36 1999/03/31 05:44:47 rgb
  41365. + * Keep PMTU reduction private.
  41366. + *
  41367. + * Revision 1.35 1999/03/27 15:13:02 rgb
  41368. + * PMTU/fragmentation bug fix.
  41369. + *
  41370. + * Revision 1.34 1999/03/17 21:19:26 rgb
  41371. + * Fix kmalloc nonatomic bug.
  41372. + *
  41373. + * Revision 1.33 1999/03/17 15:38:42 rgb
  41374. + * Code clean-up.
  41375. + * ESP_NULL IV bug fix.
  41376. + *
  41377. + * Revision 1.32 1999/03/01 20:44:25 rgb
  41378. + * Code clean-up.
  41379. + * Memory leak bug fix.
  41380. + *
  41381. + * Revision 1.31 1999/02/27 00:02:09 rgb
  41382. + * Tune to report the MTU reduction once, rather than after every recursion
  41383. + * through the encapsulating code, preventing tcp stream stalling.
  41384. + *
  41385. + * Revision 1.30 1999/02/24 20:21:01 rgb
  41386. + * Reformat debug printk's.
  41387. + * Fix recursive encapsulation, dynamic MTU bugs and add debugging code.
  41388. + * Clean-up.
  41389. + *
  41390. + * Revision 1.29 1999/02/22 17:08:14 rgb
  41391. + * Fix recursive encapsulation code.
  41392. + *
  41393. + * Revision 1.28 1999/02/19 18:27:02 rgb
  41394. + * Improve DF, fragmentation and PMTU behaviour and add dynamic MTU discovery.
  41395. + *
  41396. + * Revision 1.27 1999/02/17 16:51:37 rgb
  41397. + * Clean out unused cruft.
  41398. + * Temporarily tone down volume of debug output.
  41399. + * Temporarily shut off fragment rejection.
  41400. + * Disabled temporary failed recursive encapsulation loop.
  41401. + *
  41402. + * Revision 1.26 1999/02/12 21:21:26 rgb
  41403. + * Move KLIPS_PRINT to ipsec_netlink.h for accessibility.
  41404. + *
  41405. + * Revision 1.25 1999/02/11 19:38:27 rgb
  41406. + * More clean-up.
  41407. + * Add sanity checking for skb_copy_expand() to prevent kernel panics on
  41408. + * skb_put() values out of range.
  41409. + * Fix head/tailroom calculation causing skb_put() out-of-range values.
  41410. + * Fix return values to prevent 'nonatomic alloc_skb' warnings.
  41411. + * Allocate new skb iff needed.
  41412. + * Added more debug statements.
  41413. + * Make headroom depend on structure, not hard-coded values.
  41414. + *
  41415. + * Revision 1.24 1999/02/10 23:20:33 rgb
  41416. + * Shut up annoying 'statement has no effect' compiler warnings with
  41417. + * debugging compiled out.
  41418. + *
  41419. + * Revision 1.23 1999/02/10 22:36:30 rgb
  41420. + * Clean-up obsolete, unused and messy code.
  41421. + * Converted most IPSEC_DEBUG statements to KLIPS_PRINT macros.
  41422. + * Rename ipsec_tunnel_do_xmit to ipsec_tunnel_start_xmit and eliminated
  41423. + * original ipsec_tunnel_start_xmit.
  41424. + * Send all packet with different inner and outer destinations directly to
  41425. + * the attached physical device, rather than back through ip_forward,
  41426. + * preventing disappearing routes problems.
  41427. + * Do sanity checking before investing too much CPU in allocating new
  41428. + * structures.
  41429. + * Fail on IP header options: We cannot process them yet.
  41430. + * Add some helpful comments.
  41431. + * Use virtual device for parameters instead of physical device.
  41432. + *
  41433. + * Revision 1.22 1999/02/10 03:03:02 rgb
  41434. + * Duh. Fixed the TTL bug: forgot to update the checksum.
  41435. + *
  41436. + * Revision 1.21 1999/02/09 23:17:53 rgb
  41437. + * Add structure members to ipsec_print_ip debug function.
  41438. + * Temporarily fix TTL bug preventing tunnel mode from functioning.
  41439. + *
  41440. + * Revision 1.20 1999/02/09 00:14:25 rgb
  41441. + * Add KLIPSPRINT macro. (Not used yet, though.)
  41442. + * Delete old ip_tunnel code (BADCODE).
  41443. + * Decrement TTL in outgoing packet.
  41444. + * Set TTL on new IPIP_TUNNEL to default, not existing packet TTL.
  41445. + * Delete ethernet only feature and fix hard-coded hard_header_len.
  41446. + *
  41447. + * Revision 1.19 1999/01/29 17:56:22 rgb
  41448. + * 64-bit re-fix submitted by Peter Onion.
  41449. + *
  41450. + * Revision 1.18 1999/01/28 22:43:24 rgb
  41451. + * Fixed bug in ipsec_print_ip that caused an OOPS, found by P.Onion.
  41452. + *
  41453. + * Revision 1.17 1999/01/26 02:08:16 rgb
  41454. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  41455. + * Removed dead code.
  41456. + *
  41457. + * Revision 1.16 1999/01/22 06:25:26 rgb
  41458. + * Cruft clean-out.
  41459. + * Added algorithm switch code.
  41460. + * 64-bit clean-up.
  41461. + * Passthrough on IPIP protocol, spi 0x0 fix.
  41462. + * Enhanced debugging.
  41463. + *
  41464. + * Revision 1.15 1998/12/01 13:22:04 rgb
  41465. + * Added support for debug printing of version info.
  41466. + *
  41467. + * Revision 1.14 1998/11/30 13:22:55 rgb
  41468. + * Rationalised all the klips kernel file headers. They are much shorter
  41469. + * now and won't conflict under RH5.2.
  41470. + *
  41471. + * Revision 1.13 1998/11/17 21:13:52 rgb
  41472. + * Put IKE port bypass debug output in user-switched debug statements.
  41473. + *
  41474. + * Revision 1.12 1998/11/13 13:20:25 rgb
  41475. + * Fixed ntohs bug in udp/500 hole for IKE.
  41476. + *
  41477. + * Revision 1.11 1998/11/10 08:01:19 rgb
  41478. + * Kill tcp/500 hole, keep udp/500 hole.
  41479. + *
  41480. + * Revision 1.10 1998/11/09 21:29:26 rgb
  41481. + * If no eroute is found, discard packet and incr. tx_error.
  41482. + *
  41483. + * Revision 1.9 1998/10/31 06:50:00 rgb
  41484. + * Add tcp/udp/500 bypass.
  41485. + * Fixed up comments in #endif directives.
  41486. + *
  41487. + * Revision 1.8 1998/10/27 00:34:31 rgb
  41488. + * Reformat debug output of IP headers.
  41489. + * Newlines added before calls to ipsec_print_ip.
  41490. + *
  41491. + * Revision 1.7 1998/10/19 14:44:28 rgb
  41492. + * Added inclusion of freeswan.h.
  41493. + * sa_id structure implemented and used: now includes protocol.
  41494. + *
  41495. + * Revision 1.6 1998/10/09 04:31:35 rgb
  41496. + * Added 'klips_debug' prefix to all klips printk debug statements.
  41497. + *
  41498. + * Revision 1.5 1998/08/28 03:09:51 rgb
  41499. + * Prevent kernel log spam with default route through ipsec.
  41500. + *
  41501. + * Revision 1.4 1998/08/05 22:23:09 rgb
  41502. + * Change setdev return code to ENXIO for a non-existant physical device.
  41503. + *
  41504. + * Revision 1.3 1998/07/29 20:41:11 rgb
  41505. + * Add ipsec_tunnel_clear to clear all tunnel attachments.
  41506. + *
  41507. + * Revision 1.2 1998/06/25 20:00:33 rgb
  41508. + * Clean up #endif comments.
  41509. + * Rename dev_ipsec to dev_ipsec0 for consistency.
  41510. + * Document ipsec device fields.
  41511. + * Make ipsec_tunnel_probe visible from rest of kernel for static linking.
  41512. + * Get debugging report for *every* ipsec device initialisation.
  41513. + * Comment out redundant code.
  41514. + *
  41515. + * Revision 1.1 1998/06/18 21:27:50 henry
  41516. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  41517. + * kernel-build scripts happier in the presence of symlinks
  41518. + *
  41519. + * Revision 1.8 1998/06/14 23:49:40 rgb
  41520. + * Clarify version reporting on module loading.
  41521. + *
  41522. + * Revision 1.7 1998/05/27 23:19:20 rgb
  41523. + * Added version reporting.
  41524. + *
  41525. + * Revision 1.6 1998/05/18 21:56:23 rgb
  41526. + * Clean up for numerical consistency of output and cleaning up debug code.
  41527. + *
  41528. + * Revision 1.5 1998/05/12 02:44:23 rgb
  41529. + * Clarifying 'no e-route to host' message.
  41530. + *
  41531. + * Revision 1.4 1998/04/30 15:34:35 rgb
  41532. + * Enclosed most remaining debugging statements in #ifdef's to make it quieter.
  41533. + *
  41534. + * Revision 1.3 1998/04/21 21:28:54 rgb
  41535. + * Rearrange debug switches to change on the fly debug output from user
  41536. + * space. Only kernel changes checked in at this time. radij.c was also
  41537. + * changed to temporarily remove buggy debugging code in rj_delete causing
  41538. + * an OOPS and hence, netlink device open errors.
  41539. + *
  41540. + * Revision 1.2 1998/04/12 22:03:24 rgb
  41541. + * Updated ESP-3DES-HMAC-MD5-96,
  41542. + * ESP-DES-HMAC-MD5-96,
  41543. + * AH-HMAC-MD5-96,
  41544. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  41545. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  41546. + *
  41547. + * Fixed eroute references in /proc/net/ipsec*.
  41548. + *
  41549. + * Started to patch module unloading memory leaks in ipsec_netlink and
  41550. + * radij tree unloading.
  41551. + *
  41552. + * Revision 1.1 1998/04/09 03:06:12 henry
  41553. + * sources moved up from linux/net/ipsec
  41554. + *
  41555. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  41556. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  41557. + *
  41558. + * Revision 0.5 1997/06/03 04:24:48 ji
  41559. + * Added transport mode.
  41560. + * Changed the way routing is done.
  41561. + * Lots of bug fixes.
  41562. + *
  41563. + * Revision 0.4 1997/01/15 01:28:15 ji
  41564. + * No changes.
  41565. + *
  41566. + * Revision 0.3 1996/11/20 14:39:04 ji
  41567. + * Minor cleanups.
  41568. + * Rationalized debugging code.
  41569. + *
  41570. + * Revision 0.2 1996/11/02 00:18:33 ji
  41571. + * First limited release.
  41572. + *
  41573. + * Local Variables:
  41574. + * c-style: linux
  41575. + * End:
  41576. + */
  41577. --- /dev/null Tue Mar 11 13:02:56 2003
  41578. +++ linux/net/ipsec/ipsec_xform.c Mon Feb 9 13:51:03 2004
  41579. @@ -0,0 +1,360 @@
  41580. +/*
  41581. + * Common routines for IPSEC transformations.
  41582. + * Copyright (C) 1996, 1997 John Ioannidis.
  41583. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  41584. + *
  41585. + * This program is free software; you can redistribute it and/or modify it
  41586. + * under the terms of the GNU General Public License as published by the
  41587. + * Free Software Foundation; either version 2 of the License, or (at your
  41588. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  41589. + *
  41590. + * This program is distributed in the hope that it will be useful, but
  41591. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  41592. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  41593. + * for more details.
  41594. + *
  41595. + * RCSID $Id: ipsec_xform.c,v 1.65.2.1 2006/10/06 21:39:26 paul Exp $
  41596. + */
  41597. +
  41598. +#ifndef AUTOCONF_INCLUDED
  41599. +#include <linux/config.h>
  41600. +#endif
  41601. +#include <linux/version.h>
  41602. +#include <linux/kernel.h> /* printk() */
  41603. +
  41604. +#include "freeswan/ipsec_param.h"
  41605. +
  41606. +#ifdef MALLOC_SLAB
  41607. +# include <linux/slab.h> /* kmalloc() */
  41608. +#else /* MALLOC_SLAB */
  41609. +# include <linux/malloc.h> /* kmalloc() */
  41610. +#endif /* MALLOC_SLAB */
  41611. +#include <linux/errno.h> /* error codes */
  41612. +#include <linux/types.h> /* size_t */
  41613. +#include <linux/interrupt.h> /* mark_bh */
  41614. +
  41615. +#include <linux/netdevice.h> /* struct device, and other headers */
  41616. +#include <linux/etherdevice.h> /* eth_type_trans */
  41617. +#include <linux/ip.h> /* struct iphdr */
  41618. +#include <linux/skbuff.h>
  41619. +#include <linux/random.h> /* get_random_bytes() */
  41620. +#include <freeswan.h>
  41621. +#ifdef SPINLOCK
  41622. +# ifdef SPINLOCK_23
  41623. +# include <linux/spinlock.h> /* *lock* */
  41624. +# else /* SPINLOCK_23 */
  41625. +# include <asm/spinlock.h> /* *lock* */
  41626. +# endif /* SPINLOCK_23 */
  41627. +#endif /* SPINLOCK */
  41628. +
  41629. +#include <net/ip.h>
  41630. +
  41631. +#include "freeswan/radij.h"
  41632. +#include "freeswan/ipsec_encap.h"
  41633. +#include "freeswan/ipsec_radij.h"
  41634. +#include "freeswan/ipsec_xform.h"
  41635. +#include "freeswan/ipsec_ipe4.h"
  41636. +#include "freeswan/ipsec_ah.h"
  41637. +#include "freeswan/ipsec_esp.h"
  41638. +
  41639. +#include <pfkeyv2.h>
  41640. +#include <pfkey.h>
  41641. +
  41642. +#ifdef CONFIG_KLIPS_DEBUG
  41643. +int debug_xform = 0;
  41644. +#endif /* CONFIG_KLIPS_DEBUG */
  41645. +
  41646. +#ifdef SPINLOCK
  41647. +spinlock_t tdb_lock = SPIN_LOCK_UNLOCKED;
  41648. +#else /* SPINLOCK */
  41649. +spinlock_t tdb_lock;
  41650. +#endif /* SPINLOCK */
  41651. +
  41652. +/*
  41653. + * $Log: ipsec_xform.c,v $
  41654. + * Revision 1.65.2.1 2006/10/06 21:39:26 paul
  41655. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  41656. + * set. This is defined through autoconf.h which is included through the
  41657. + * linux kernel build macros.
  41658. + *
  41659. + * Revision 1.65 2005/04/29 05:10:22 mcr
  41660. + * removed from extraenous includes to make unit testing easier.
  41661. + *
  41662. + * Revision 1.64 2004/07/10 19:11:18 mcr
  41663. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  41664. + *
  41665. + * Revision 1.63 2003/10/31 02:27:55 mcr
  41666. + * pulled up port-selector patches and sa_id elimination.
  41667. + *
  41668. + * Revision 1.62.30.1 2003/10/29 01:30:41 mcr
  41669. + * elimited "struct sa_id".
  41670. + *
  41671. + * Revision 1.62 2002/05/14 02:34:21 rgb
  41672. + * Delete stale code.
  41673. + *
  41674. + * Revision 1.61 2002/04/24 07:55:32 mcr
  41675. + * #include patches and Makefiles for post-reorg compilation.
  41676. + *
  41677. + * Revision 1.60 2002/04/24 07:36:33 mcr
  41678. + * Moved from ./klips/net/ipsec/ipsec_xform.c,v
  41679. + *
  41680. + * Revision 1.59 2002/03/29 15:01:36 rgb
  41681. + * Delete decommissioned code.
  41682. + *
  41683. + * Revision 1.58 2002/01/29 17:17:57 mcr
  41684. + * moved include of ipsec_param.h to after include of linux/kernel.h
  41685. + * otherwise, it seems that some option that is set in ipsec_param.h
  41686. + * screws up something subtle in the include path to kernel.h, and
  41687. + * it complains on the snprintf() prototype.
  41688. + *
  41689. + * Revision 1.57 2002/01/29 04:00:53 mcr
  41690. + * more excise of kversions.h header.
  41691. + *
  41692. + * Revision 1.56 2001/11/27 05:17:22 mcr
  41693. + * turn off the worst of the per-packet debugging.
  41694. + *
  41695. + * Revision 1.55 2001/11/26 09:23:50 rgb
  41696. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  41697. + *
  41698. + * Revision 1.54 2001/10/18 04:45:21 rgb
  41699. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  41700. + * lib/freeswan.h version macros moved to lib/kversions.h.
  41701. + * Other compiler directive cleanups.
  41702. + *
  41703. + * Revision 1.53 2001/09/08 21:13:34 rgb
  41704. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  41705. + *
  41706. + * Revision 1.52 2001/06/14 19:35:11 rgb
  41707. + * Update copyright date.
  41708. + *
  41709. + * Revision 1.51 2001/05/30 08:14:03 rgb
  41710. + * Removed vestiges of esp-null transforms.
  41711. + *
  41712. + * Revision 1.50 2001/05/03 19:43:18 rgb
  41713. + * Initialise error return variable.
  41714. + * Update SENDERR macro.
  41715. + * Fix sign of error return code for ipsec_tdbcleanup().
  41716. + * Use more appropriate return code for ipsec_tdbwipe().
  41717. + *
  41718. + * Revision 1.49 2001/04/19 18:56:17 rgb
  41719. + * Fixed tdb table locking comments.
  41720. + *
  41721. + * Revision 1.48 2001/02/27 22:24:55 rgb
  41722. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  41723. + * Check for satoa() return codes.
  41724. + *
  41725. + * Revision 1.47 2000/11/06 04:32:08 rgb
  41726. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  41727. + *
  41728. + * Revision 1.46 2000/09/20 16:21:57 rgb
  41729. + * Cleaned up ident string alloc/free.
  41730. + *
  41731. + * Revision 1.45 2000/09/08 19:16:51 rgb
  41732. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  41733. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  41734. + *
  41735. + * Revision 1.44 2000/08/30 05:29:04 rgb
  41736. + * Compiler-define out no longer used tdb_init() in ipsec_xform.c.
  41737. + *
  41738. + * Revision 1.43 2000/08/18 21:30:41 rgb
  41739. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  41740. + *
  41741. + * Revision 1.42 2000/08/01 14:51:51 rgb
  41742. + * Removed _all_ remaining traces of DES.
  41743. + *
  41744. + * Revision 1.41 2000/07/28 14:58:31 rgb
  41745. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  41746. + *
  41747. + * Revision 1.40 2000/06/28 05:50:11 rgb
  41748. + * Actually set iv_bits.
  41749. + *
  41750. + * Revision 1.39 2000/05/10 23:11:09 rgb
  41751. + * Added netlink debugging output.
  41752. + * Added a cast to quiet down the ntohl bug.
  41753. + *
  41754. + * Revision 1.38 2000/05/10 19:18:42 rgb
  41755. + * Cast output of ntohl so that the broken prototype doesn't make our
  41756. + * compile noisy.
  41757. + *
  41758. + * Revision 1.37 2000/03/16 14:04:59 rgb
  41759. + * Hardwired CONFIG_IPSEC_PFKEYv2 on.
  41760. + *
  41761. + * Revision 1.36 2000/01/26 10:11:28 rgb
  41762. + * Fixed spacing in error text causing run-in words.
  41763. + *
  41764. + * Revision 1.35 2000/01/21 06:17:16 rgb
  41765. + * Tidied up compiler directive indentation for readability.
  41766. + * Added ictx,octx vars for simplification.(kravietz)
  41767. + * Added macros for HMAC padding magic numbers.(kravietz)
  41768. + * Fixed missing key length reporting bug.
  41769. + * Fixed bug in tdbwipe to return immediately on NULL tdbp passed in.
  41770. + *
  41771. + * Revision 1.34 1999/12/08 00:04:19 rgb
  41772. + * Fixed SA direction overwriting bug for netlink users.
  41773. + *
  41774. + * Revision 1.33 1999/12/01 22:16:44 rgb
  41775. + * Minor formatting changes in ESP MD5 initialisation.
  41776. + *
  41777. + * Revision 1.32 1999/11/25 09:06:36 rgb
  41778. + * Fixed error return messages, should be returning negative numbers.
  41779. + * Implemented SENDERR macro for propagating error codes.
  41780. + * Added debug message and separate error code for algorithms not compiled
  41781. + * in.
  41782. + *
  41783. + * Revision 1.31 1999/11/23 23:06:26 rgb
  41784. + * Sort out pfkey and freeswan headers, putting them in a library path.
  41785. + *
  41786. + * Revision 1.30 1999/11/18 04:09:20 rgb
  41787. + * Replaced all kernel version macros to shorter, readable form.
  41788. + *
  41789. + * Revision 1.29 1999/11/17 15:53:40 rgb
  41790. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  41791. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  41792. + * klips/net/ipsec/Makefile.
  41793. + *
  41794. + * Revision 1.28 1999/10/18 20:04:01 rgb
  41795. + * Clean-out unused cruft.
  41796. + *
  41797. + * Revision 1.27 1999/10/03 19:01:03 rgb
  41798. + * Spinlock support for 2.3.xx and 2.0.xx kernels.
  41799. + *
  41800. + * Revision 1.26 1999/10/01 16:22:24 rgb
  41801. + * Switch from assignment init. to functional init. of spinlocks.
  41802. + *
  41803. + * Revision 1.25 1999/10/01 15:44:54 rgb
  41804. + * Move spinlock header include to 2.1> scope.
  41805. + *
  41806. + * Revision 1.24 1999/10/01 00:03:46 rgb
  41807. + * Added tdb structure locking.
  41808. + * Minor formatting changes.
  41809. + * Add function to initialize tdb hash table.
  41810. + *
  41811. + * Revision 1.23 1999/05/25 22:42:12 rgb
  41812. + * Add deltdbchain() debugging.
  41813. + *
  41814. + * Revision 1.22 1999/05/25 21:24:31 rgb
  41815. + * Add debugging statements to deltdbchain().
  41816. + *
  41817. + * Revision 1.21 1999/05/25 03:51:48 rgb
  41818. + * Refix error return code.
  41819. + *
  41820. + * Revision 1.20 1999/05/25 03:34:07 rgb
  41821. + * Fix error return for flush.
  41822. + *
  41823. + * Revision 1.19 1999/05/09 03:25:37 rgb
  41824. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  41825. + *
  41826. + * Revision 1.18 1999/05/05 22:02:32 rgb
  41827. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  41828. + *
  41829. + * Revision 1.17 1999/04/29 15:20:16 rgb
  41830. + * Change gettdb parameter to a pointer to reduce stack loading and
  41831. + * facilitate parameter sanity checking.
  41832. + * Add sanity checking for null pointer arguments.
  41833. + * Add debugging instrumentation.
  41834. + * Add function deltdbchain() which will take care of unlinking,
  41835. + * zeroing and deleting a chain of tdbs.
  41836. + * Add a parameter to tdbcleanup to be able to delete a class of SAs.
  41837. + * tdbwipe now actually zeroes the tdb as well as any of its pointed
  41838. + * structures.
  41839. + *
  41840. + * Revision 1.16 1999/04/16 15:36:29 rgb
  41841. + * Fix cut-and-paste error causing a memory leak in IPIP TDB freeing.
  41842. + *
  41843. + * Revision 1.15 1999/04/11 00:29:01 henry
  41844. + * GPL boilerplate
  41845. + *
  41846. + * Revision 1.14 1999/04/06 04:54:28 rgb
  41847. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  41848. + * patch shell fixes.
  41849. + *
  41850. + * Revision 1.13 1999/02/19 18:23:01 rgb
  41851. + * Nix debug off compile warning.
  41852. + *
  41853. + * Revision 1.12 1999/02/17 16:52:16 rgb
  41854. + * Consolidate satoa()s for space and speed efficiency.
  41855. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  41856. + * Clean out unused cruft.
  41857. + * Ditch NET_IPIP dependancy.
  41858. + * Loop for 3des key setting.
  41859. + *
  41860. + * Revision 1.11 1999/01/26 02:09:05 rgb
  41861. + * Remove ah/esp/IPIP switching on include files.
  41862. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  41863. + * Removed dead code.
  41864. + * Clean up debug code when switched off.
  41865. + * Remove references to INET_GET_PROTOCOL.
  41866. + * Added code exclusion macros to reduce code from unused algorithms.
  41867. + *
  41868. + * Revision 1.10 1999/01/22 06:28:55 rgb
  41869. + * Cruft clean-out.
  41870. + * Put random IV generation in kernel.
  41871. + * Added algorithm switch code.
  41872. + * Enhanced debugging.
  41873. + * 64-bit clean-up.
  41874. + *
  41875. + * Revision 1.9 1998/11/30 13:22:55 rgb
  41876. + * Rationalised all the klips kernel file headers. They are much shorter
  41877. + * now and won't conflict under RH5.2.
  41878. + *
  41879. + * Revision 1.8 1998/11/25 04:59:06 rgb
  41880. + * Add conditionals for no IPIP tunnel code.
  41881. + * Delete commented out code.
  41882. + *
  41883. + * Revision 1.7 1998/10/31 06:50:41 rgb
  41884. + * Convert xform ASCII names to no spaces.
  41885. + * Fixed up comments in #endif directives.
  41886. + *
  41887. + * Revision 1.6 1998/10/19 14:44:28 rgb
  41888. + * Added inclusion of freeswan.h.
  41889. + * sa_id structure implemented and used: now includes protocol.
  41890. + *
  41891. + * Revision 1.5 1998/10/09 04:32:19 rgb
  41892. + * Added 'klips_debug' prefix to all klips printk debug statements.
  41893. + *
  41894. + * Revision 1.4 1998/08/12 00:11:31 rgb
  41895. + * Added new xform functions to the xform table.
  41896. + * Fixed minor debug output spelling error.
  41897. + *
  41898. + * Revision 1.3 1998/07/09 17:45:31 rgb
  41899. + * Clarify algorithm not available message.
  41900. + *
  41901. + * Revision 1.2 1998/06/23 03:00:51 rgb
  41902. + * Check for presence of IPIP protocol if it is setup one way (we don't
  41903. + * know what has been set up the other way and can only assume it will be
  41904. + * symmetrical with the exception of keys).
  41905. + *
  41906. + * Revision 1.1 1998/06/18 21:27:51 henry
  41907. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  41908. + * kernel-build scripts happier in the presence of symlinks
  41909. + *
  41910. + * Revision 1.3 1998/06/11 05:54:59 rgb
  41911. + * Added transform version string pointer to xformsw initialisations.
  41912. + *
  41913. + * Revision 1.2 1998/04/21 21:28:57 rgb
  41914. + * Rearrange debug switches to change on the fly debug output from user
  41915. + * space. Only kernel changes checked in at this time. radij.c was also
  41916. + * changed to temporarily remove buggy debugging code in rj_delete causing
  41917. + * an OOPS and hence, netlink device open errors.
  41918. + *
  41919. + * Revision 1.1 1998/04/09 03:06:13 henry
  41920. + * sources moved up from linux/net/ipsec
  41921. + *
  41922. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  41923. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  41924. + *
  41925. + * Revision 0.5 1997/06/03 04:24:48 ji
  41926. + * Added ESP-3DES-MD5-96
  41927. + *
  41928. + * Revision 0.4 1997/01/15 01:28:15 ji
  41929. + * Added new transforms.
  41930. + *
  41931. + * Revision 0.3 1996/11/20 14:39:04 ji
  41932. + * Minor cleanups.
  41933. + * Rationalized debugging code.
  41934. + *
  41935. + * Revision 0.2 1996/11/02 00:18:33 ji
  41936. + * First limited release.
  41937. + *
  41938. + *
  41939. + */
  41940. --- /dev/null Tue Mar 11 13:02:56 2003
  41941. +++ linux/net/ipsec/ipsec_xmit.c Mon Feb 9 13:51:03 2004
  41942. @@ -0,0 +1,1850 @@
  41943. +/*
  41944. + * IPSEC Transmit code.
  41945. + * Copyright (C) 1996, 1997 John Ioannidis.
  41946. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  41947. + * Copyright (C) 2004-2005 Michael Richardson <mcr@xelerance.com>
  41948. + *
  41949. + * This program is free software; you can redistribute it and/or modify it
  41950. + * under the terms of the GNU General Public License as published by the
  41951. + * Free Software Foundation; either version 2 of the License, or (at your
  41952. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  41953. + *
  41954. + * This program is distributed in the hope that it will be useful, but
  41955. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  41956. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  41957. + * for more details.
  41958. + */
  41959. +
  41960. +char ipsec_xmit_c_version[] = "RCSID $Id: ipsec_xmit.c,v 1.20.2.8 2006/10/06 21:39:26 paul Exp $";
  41961. +
  41962. +#define __NO_VERSION__
  41963. +#include <linux/module.h>
  41964. +#ifndef AUTOCONF_INCLUDED
  41965. +#include <linux/config.h>
  41966. +#endif /* for CONFIG_IP_FORWARD */
  41967. +#include <linux/version.h>
  41968. +#include <linux/kernel.h> /* printk() */
  41969. +
  41970. +#include "openswan/ipsec_param.h"
  41971. +
  41972. +#ifdef MALLOC_SLAB
  41973. +# include <linux/slab.h> /* kmalloc() */
  41974. +#else /* MALLOC_SLAB */
  41975. +# include <linux/malloc.h> /* kmalloc() */
  41976. +#endif /* MALLOC_SLAB */
  41977. +#include <linux/errno.h> /* error codes */
  41978. +#include <linux/types.h> /* size_t */
  41979. +#include <linux/interrupt.h> /* mark_bh */
  41980. +
  41981. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  41982. +#include <linux/etherdevice.h> /* eth_type_trans */
  41983. +#include <linux/ip.h> /* struct iphdr */
  41984. +#include <linux/tcp.h> /* struct tcphdr */
  41985. +#include <linux/udp.h> /* struct udphdr */
  41986. +#include <linux/skbuff.h>
  41987. +#include <asm/uaccess.h>
  41988. +#include <asm/checksum.h>
  41989. +#include <openswan.h>
  41990. +#ifdef NET_21
  41991. +# define MSS_HACK_ /* experimental */
  41992. +# include <linux/in6.h>
  41993. +# include <net/dst.h>
  41994. +# define proto_priv cb
  41995. +#endif /* NET_21 */
  41996. +
  41997. +#include <net/icmp.h> /* icmp_send() */
  41998. +#include <net/ip.h>
  41999. +#ifdef NETDEV_23
  42000. +# include <linux/netfilter_ipv4.h>
  42001. +#endif /* NETDEV_23 */
  42002. +
  42003. +#include <linux/if_arp.h>
  42004. +#ifdef MSS_HACK
  42005. +# include <net/tcp.h> /* TCP options */
  42006. +#endif /* MSS_HACK */
  42007. +
  42008. +#include "openswan/radij.h"
  42009. +#include "openswan/ipsec_life.h"
  42010. +#include "openswan/ipsec_xform.h"
  42011. +#include "openswan/ipsec_eroute.h"
  42012. +#include "openswan/ipsec_encap.h"
  42013. +#include "openswan/ipsec_radij.h"
  42014. +#include "openswan/ipsec_xmit.h"
  42015. +#include "openswan/ipsec_sa.h"
  42016. +#include "openswan/ipsec_tunnel.h"
  42017. +#include "openswan/ipsec_ipe4.h"
  42018. +#include "openswan/ipsec_ah.h"
  42019. +#include "openswan/ipsec_esp.h"
  42020. +
  42021. +#ifdef CONFIG_KLIPS_IPCOMP
  42022. +#include "openswan/ipcomp.h"
  42023. +#endif /* CONFIG_KLIPS_IPCOMP */
  42024. +
  42025. +#include <pfkeyv2.h>
  42026. +#include <pfkey.h>
  42027. +
  42028. +#include "openswan/ipsec_proto.h"
  42029. +#include "openswan/ipsec_alg.h"
  42030. +
  42031. +
  42032. +/*
  42033. + * Stupid kernel API differences in APIs. Not only do some
  42034. + * kernels not have ip_select_ident, but some have differing APIs,
  42035. + * and SuSE has one with one parameter, but no way of checking to
  42036. + * see what is really what.
  42037. + */
  42038. +
  42039. +#ifdef SUSE_LINUX_2_4_19_IS_STUPID
  42040. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph)
  42041. +#else
  42042. +
  42043. +/* simplest case, nothing */
  42044. +#if !defined(IP_SELECT_IDENT)
  42045. +#define KLIPS_IP_SELECT_IDENT(iph, skb) do { iph->id = htons(ip_id_count++); } while(0)
  42046. +#endif
  42047. +
  42048. +/* kernels > 2.3.37-ish */
  42049. +#if defined(IP_SELECT_IDENT) && !defined(IP_SELECT_IDENT_NEW)
  42050. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst)
  42051. +#endif
  42052. +
  42053. +/* kernels > 2.4.2 */
  42054. +#if defined(IP_SELECT_IDENT) && defined(IP_SELECT_IDENT_NEW)
  42055. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst, NULL)
  42056. +#endif
  42057. +
  42058. +#endif /* SUSE_LINUX_2_4_19_IS_STUPID */
  42059. +
  42060. +
  42061. +
  42062. +#if defined(CONFIG_KLIPS_AH)
  42063. +static __u32 zeroes[64];
  42064. +#endif
  42065. +
  42066. +#ifdef CONFIG_KLIPS_DEBUG
  42067. +int sysctl_ipsec_debug_verbose = 0;
  42068. +#endif /* CONFIG_KLIPS_DEBUG */
  42069. +
  42070. +int ipsec_xmit_trap_count = 0;
  42071. +int ipsec_xmit_trap_sendcount = 0;
  42072. +
  42073. +int sysctl_ipsec_icmp = 0;
  42074. +int sysctl_ipsec_tos = 0;
  42075. +
  42076. +#ifdef CONFIG_KLIPS_DEBUG
  42077. +#define dmp(_x,_y,_z) if(debug_tunnel) ipsec_dmp_block(_x,_y,_z)
  42078. +#else /* CONFIG_KLIPS_DEBUG */
  42079. +#define dmp(_x, _y, _z)
  42080. +#endif /* CONFIG_KLIPS_DEBUG */
  42081. +
  42082. +
  42083. +#if !defined(SKB_COPY_EXPAND) || defined(KLIPS_UNIT_TESTS)
  42084. +/*
  42085. + * This is mostly skbuff.c:skb_copy().
  42086. + */
  42087. +struct sk_buff *
  42088. +skb_copy_expand(const struct sk_buff *skb, int headroom,
  42089. + int tailroom, int priority)
  42090. +{
  42091. + struct sk_buff *n;
  42092. + unsigned long offset;
  42093. +
  42094. + /*
  42095. + * Do sanity checking
  42096. + */
  42097. + if((headroom < 0) || (tailroom < 0) || ((headroom+tailroom) < 0)) {
  42098. + printk(KERN_WARNING
  42099. + "klips_error:skb_copy_expand: "
  42100. + "Illegal negative head,tailroom %d,%d\n",
  42101. + headroom,
  42102. + tailroom);
  42103. + return NULL;
  42104. + }
  42105. + /*
  42106. + * Allocate the copy buffer
  42107. + */
  42108. +
  42109. +#ifndef NET_21
  42110. + IS_SKB(skb);
  42111. +#endif /* !NET_21 */
  42112. +
  42113. +
  42114. + n=alloc_skb(skb->end - skb->head + headroom + tailroom, priority);
  42115. +
  42116. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42117. + "klips_debug:skb_copy_expand: "
  42118. + "allocating %d bytes, head=0p%p data=0p%p tail=0p%p end=0p%p end-head=%d tail-data=%d\n",
  42119. + skb->end - skb->head + headroom + tailroom,
  42120. + skb->head,
  42121. + skb->data,
  42122. + skb->tail,
  42123. + skb->end,
  42124. + skb->end - skb->head,
  42125. + skb->tail - skb->data);
  42126. +
  42127. + if(n==NULL)
  42128. + return NULL;
  42129. +
  42130. + /*
  42131. + * Shift between the two data areas in bytes
  42132. + */
  42133. +
  42134. + /* Set the data pointer */
  42135. + skb_reserve(n,skb->data-skb->head+headroom);
  42136. + /* Set the tail pointer and length */
  42137. + if(skb_tailroom(n) < skb->len) {
  42138. + printk(KERN_WARNING "klips_error:skb_copy_expand: "
  42139. + "tried to skb_put %ld, %d available. This should never happen, please report.\n",
  42140. + (unsigned long int)skb->len,
  42141. + skb_tailroom(n));
  42142. + ipsec_kfree_skb(n);
  42143. + return NULL;
  42144. + }
  42145. + skb_put(n,skb->len);
  42146. +
  42147. + offset=n->head + headroom - skb->head;
  42148. +
  42149. + /* Copy the bytes */
  42150. + memcpy(n->head + headroom, skb->head,skb->end-skb->head);
  42151. +#ifdef NET_21
  42152. + n->csum=skb->csum;
  42153. + n->priority=skb->priority;
  42154. + n->dst=dst_clone(skb->dst);
  42155. + if(skb->nh.raw)
  42156. + n->nh.raw=skb->nh.raw+offset;
  42157. +#ifndef NETDEV_23
  42158. + n->is_clone=0;
  42159. +#endif /* NETDEV_23 */
  42160. + atomic_set(&n->users, 1);
  42161. + n->destructor = NULL;
  42162. +#ifdef HAVE_SOCK_SECURITY
  42163. + n->security=skb->security;
  42164. +#endif
  42165. +#else /* NET_21 */
  42166. + n->link3=NULL;
  42167. + n->when=skb->when;
  42168. + if(skb->ip_hdr)
  42169. + n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset);
  42170. + n->saddr=skb->saddr;
  42171. + n->daddr=skb->daddr;
  42172. + n->raddr=skb->raddr;
  42173. + n->seq=skb->seq;
  42174. + n->end_seq=skb->end_seq;
  42175. + n->ack_seq=skb->ack_seq;
  42176. + n->acked=skb->acked;
  42177. + n->free=1;
  42178. + n->arp=skb->arp;
  42179. + n->tries=0;
  42180. + n->lock=0;
  42181. + n->users=0;
  42182. +#endif /* NET_21 */
  42183. + n->protocol=skb->protocol;
  42184. + n->list=NULL;
  42185. + n->sk=NULL;
  42186. + n->dev=skb->dev;
  42187. + if(skb->h.raw)
  42188. + n->h.raw=skb->h.raw+offset;
  42189. + if(skb->mac.raw)
  42190. + n->mac.raw=skb->mac.raw+offset;
  42191. + memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
  42192. +#ifndef NETDEV_23
  42193. + n->used=skb->used;
  42194. +#endif /* !NETDEV_23 */
  42195. + n->pkt_type=skb->pkt_type;
  42196. + n->stamp=skb->stamp;
  42197. +
  42198. +#ifndef NET_21
  42199. + IS_SKB(n);
  42200. +#endif /* !NET_21 */
  42201. + return n;
  42202. +}
  42203. +#endif /* !SKB_COPY_EXPAND */
  42204. +
  42205. +#ifdef CONFIG_KLIPS_DEBUG
  42206. +void
  42207. +ipsec_print_ip(struct iphdr *ip)
  42208. +{
  42209. + char buf[ADDRTOA_BUF];
  42210. +
  42211. + printk(KERN_INFO "klips_debug: IP:");
  42212. + printk(" ihl:%d", ip->ihl << 2);
  42213. + printk(" ver:%d", ip->version);
  42214. + printk(" tos:%d", ip->tos);
  42215. + printk(" tlen:%d", ntohs(ip->tot_len));
  42216. + printk(" id:%d", ntohs(ip->id));
  42217. + printk(" %s%s%sfrag_off:%d",
  42218. + ip->frag_off & __constant_htons(IP_CE) ? "CE " : "",
  42219. + ip->frag_off & __constant_htons(IP_DF) ? "DF " : "",
  42220. + ip->frag_off & __constant_htons(IP_MF) ? "MF " : "",
  42221. + (ntohs(ip->frag_off) & IP_OFFSET) << 3);
  42222. + printk(" ttl:%d", ip->ttl);
  42223. + printk(" proto:%d", ip->protocol);
  42224. + if(ip->protocol == IPPROTO_UDP)
  42225. + printk(" (UDP)");
  42226. + if(ip->protocol == IPPROTO_TCP)
  42227. + printk(" (TCP)");
  42228. + if(ip->protocol == IPPROTO_ICMP)
  42229. + printk(" (ICMP)");
  42230. + if(ip->protocol == IPPROTO_ESP)
  42231. + printk(" (ESP)");
  42232. + if(ip->protocol == IPPROTO_AH)
  42233. + printk(" (AH)");
  42234. + if(ip->protocol == IPPROTO_COMP)
  42235. + printk(" (COMP)");
  42236. + printk(" chk:%d", ntohs(ip->check));
  42237. + addrtoa(*((struct in_addr*)(&ip->saddr)), 0, buf, sizeof(buf));
  42238. + printk(" saddr:%s", buf);
  42239. + if(ip->protocol == IPPROTO_UDP)
  42240. + printk(":%d",
  42241. + ntohs(((struct udphdr*)((caddr_t)ip + (ip->ihl << 2)))->source));
  42242. + if(ip->protocol == IPPROTO_TCP)
  42243. + printk(":%d",
  42244. + ntohs(((struct tcphdr*)((caddr_t)ip + (ip->ihl << 2)))->source));
  42245. + addrtoa(*((struct in_addr*)(&ip->daddr)), 0, buf, sizeof(buf));
  42246. + printk(" daddr:%s", buf);
  42247. + if(ip->protocol == IPPROTO_UDP)
  42248. + printk(":%d",
  42249. + ntohs(((struct udphdr*)((caddr_t)ip + (ip->ihl << 2)))->dest));
  42250. + if(ip->protocol == IPPROTO_TCP)
  42251. + printk(":%d",
  42252. + ntohs(((struct tcphdr*)((caddr_t)ip + (ip->ihl << 2)))->dest));
  42253. + if(ip->protocol == IPPROTO_ICMP)
  42254. + printk(" type:code=%d:%d",
  42255. + ((struct icmphdr*)((caddr_t)ip + (ip->ihl << 2)))->type,
  42256. + ((struct icmphdr*)((caddr_t)ip + (ip->ihl << 2)))->code);
  42257. + printk("\n");
  42258. +
  42259. + if(sysctl_ipsec_debug_verbose) {
  42260. + __u8 *c;
  42261. + int len = ntohs(ip->tot_len) - ip->ihl*4;
  42262. +
  42263. + c = ((__u8*)ip) + ip->ihl*4;
  42264. + ipsec_dmp_block("ip_print", c, len);
  42265. + }
  42266. +}
  42267. +#endif /* CONFIG_KLIPS_DEBUG */
  42268. +
  42269. +#ifdef MSS_HACK
  42270. +/*
  42271. + * Issues:
  42272. + * 1) Fragments arriving in the tunnel should probably be rejected.
  42273. + * 2) How does this affect syncookies, mss_cache, dst cache ?
  42274. + * 3) Path MTU discovery handling needs to be reviewed. For example,
  42275. + * if we receive an ICMP 'packet too big' message from an intermediate
  42276. + * router specifying it's next hop MTU, our stack may process this and
  42277. + * adjust the MSS without taking our AH/ESP overheads into account.
  42278. + */
  42279. +
  42280. +
  42281. +/*
  42282. + * Recaclulate checksum using differences between changed datum,
  42283. + * borrowed from netfilter.
  42284. + */
  42285. +DEBUG_NO_STATIC u_int16_t
  42286. +ipsec_fast_csum(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck)
  42287. +{
  42288. + u_int32_t diffs[] = { oldvalinv, newval };
  42289. + return csum_fold(csum_partial((char *)diffs, sizeof(diffs),
  42290. + oldcheck^0xFFFF));
  42291. +}
  42292. +
  42293. +/*
  42294. + * Determine effective MSS.
  42295. + *
  42296. + * Note that we assume that there is always an MSS option for our own
  42297. + * SYN segments, which is mentioned in tcp_syn_build_options(), kernel 2.2.x.
  42298. + * This could change, and we should probably parse TCP options instead.
  42299. + *
  42300. + */
  42301. +DEBUG_NO_STATIC u_int8_t
  42302. +ipsec_adjust_mss(struct sk_buff *skb, struct tcphdr *tcph, u_int16_t mtu)
  42303. +{
  42304. + u_int16_t oldmss, newmss;
  42305. + u_int32_t *mssp;
  42306. + struct sock *sk = skb->sk;
  42307. +
  42308. + newmss = tcp_sync_mss(sk, mtu);
  42309. + printk(KERN_INFO "klips: setting mss to %u\n", newmss);
  42310. + mssp = (u_int32_t *)tcph + sizeof(struct tcphdr) / sizeof(u_int32_t);
  42311. + oldmss = ntohl(*mssp) & 0x0000FFFF;
  42312. + *mssp = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | newmss);
  42313. + tcph->check = ipsec_fast_csum(htons(~oldmss),
  42314. + htons(newmss), tcph->check);
  42315. + return 1;
  42316. +}
  42317. +#endif /* MSS_HACK */
  42318. +
  42319. +/*
  42320. + * Sanity checks
  42321. + */
  42322. +enum ipsec_xmit_value
  42323. +ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs)
  42324. +{
  42325. +
  42326. + if (ixs->dev == NULL) {
  42327. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42328. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42329. + "No device associated with skb!\n" );
  42330. + return IPSEC_XMIT_NODEV;
  42331. + }
  42332. +
  42333. + ixs->prv = ixs->dev->priv;
  42334. + if (ixs->prv == NULL) {
  42335. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42336. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42337. + "Device has no private structure!\n" );
  42338. + return IPSEC_XMIT_NOPRIVDEV;
  42339. + }
  42340. +
  42341. + ixs->physdev = ixs->prv->dev;
  42342. + if (ixs->physdev == NULL) {
  42343. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42344. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42345. + "Device is not attached to physical device!\n" );
  42346. + return IPSEC_XMIT_NOPHYSDEV;
  42347. + }
  42348. +
  42349. + ixs->physmtu = ixs->physdev->mtu;
  42350. + ixs->cur_mtu = ixs->physdev->mtu;
  42351. + ixs->stats = (struct net_device_stats *) &(ixs->prv->mystats);
  42352. +
  42353. + return IPSEC_XMIT_OK;
  42354. +}
  42355. +
  42356. +enum ipsec_xmit_value
  42357. +ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs)
  42358. +{
  42359. + /*
  42360. + * Return if there is nothing to do. (Does this ever happen?) XXX
  42361. + */
  42362. + if (ixs->skb == NULL) {
  42363. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42364. + "klips_error:ipsec_xmit_sanity_check_skb: "
  42365. + "Nothing to do!\n" );
  42366. + return IPSEC_XMIT_NOSKB;
  42367. + }
  42368. +
  42369. + /* if skb was cloned (most likely due to a packet sniffer such as
  42370. + tcpdump being momentarily attached to the interface), make
  42371. + a copy of our own to modify */
  42372. + if(skb_cloned(ixs->skb)) {
  42373. + if
  42374. +#ifdef SKB_COW_NEW
  42375. + (skb_cow(ixs->skb, skb_headroom(ixs->skb)) != 0)
  42376. +#else /* SKB_COW_NEW */
  42377. + ((ixs->skb = skb_cow(ixs->skb, skb_headroom(ixs->skb))) == NULL)
  42378. +#endif /* SKB_COW_NEW */
  42379. + {
  42380. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42381. + "klips_error:ipsec_xmit_sanity_check_skb: "
  42382. + "skb_cow failed to allocate buffer, dropping.\n" );
  42383. + ixs->stats->tx_dropped++;
  42384. + return IPSEC_XMIT_ERRSKBALLOC;
  42385. + }
  42386. + }
  42387. +
  42388. + ixs->iph = ixs->skb->nh.iph;
  42389. +
  42390. + /* sanity check for IP version as we can't handle IPv6 right now */
  42391. + if (ixs->iph->version != 4) {
  42392. + KLIPS_PRINT(debug_tunnel,
  42393. + "klips_debug:ipsec_xmit_sanity_check_skb: "
  42394. + "found IP Version %d but cannot process other IP versions than v4.\n",
  42395. + ixs->iph->version); /* XXX */
  42396. + ixs->stats->tx_dropped++;
  42397. + return IPSEC_XMIT_NOIPV6;
  42398. + }
  42399. +
  42400. +#if IPSEC_DISALLOW_IPOPTIONS
  42401. + if ((ixs->iph->ihl << 2) != sizeof (struct iphdr)) {
  42402. + KLIPS_PRINT(debug_tunnel,
  42403. + "klips_debug:ipsec_xmit_sanity_check_skb: "
  42404. + "cannot process IP header options yet. May be mal-formed packet.\n"); /* XXX */
  42405. + ixs->stats->tx_dropped++;
  42406. + return IPSEC_XMIT_NOIPOPTIONS;
  42407. + }
  42408. +#endif /* IPSEC_DISALLOW_IPOPTIONS */
  42409. +
  42410. +#ifndef NET_21
  42411. + if (ixs->iph->ttl <= 0) {
  42412. + /* Tell the sender its packet died... */
  42413. + ICMP_SEND(ixs->skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0, ixs->physdev);
  42414. +
  42415. + KLIPS_PRINT(debug_tunnel, "klips_debug:ipsec_xmit_sanity_check_skb: "
  42416. + "TTL=0, too many hops!\n");
  42417. + ixs->stats->tx_dropped++;
  42418. + return IPSEC_XMIT_TTLEXPIRED;
  42419. + }
  42420. +#endif /* !NET_21 */
  42421. +
  42422. + return IPSEC_XMIT_OK;
  42423. +}
  42424. +
  42425. +enum ipsec_xmit_value
  42426. +ipsec_xmit_encap_once(struct ipsec_xmit_state *ixs)
  42427. +{
  42428. +#ifdef CONFIG_KLIPS_ESP
  42429. + struct esphdr *espp;
  42430. + unsigned char *idat, *pad;
  42431. + int authlen = 0, padlen = 0, i;
  42432. +#endif /* !CONFIG_KLIPS_ESP */
  42433. +#ifdef CONFIG_KLIPS_AH
  42434. + struct iphdr ipo;
  42435. + struct ahhdr *ahp;
  42436. +#endif /* CONFIG_KLIPS_AH */
  42437. +#if defined(CONFIG_KLIPS_AUTH_HMAC_MD5) || defined(CONFIG_KLIPS_AUTH_HMAC_SHA1)
  42438. + union {
  42439. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42440. + MD5_CTX md5;
  42441. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42442. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42443. + SHA1_CTX sha1;
  42444. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42445. + } tctx;
  42446. + __u8 hash[AH_AMAX];
  42447. +#endif /* defined(CONFIG_KLIPS_AUTH_HMAC_MD5) || defined(CONFIG_KLIPS_AUTH_HMACn_SHA1) */
  42448. + int headroom = 0, tailroom = 0, ilen = 0, len = 0;
  42449. + unsigned char *dat;
  42450. + int blocksize = 8; /* XXX: should be inside ixs --jjo */
  42451. + struct ipsec_alg_enc *ixt_e = NULL;
  42452. + struct ipsec_alg_auth *ixt_a = NULL;
  42453. +
  42454. + ixs->iphlen = ixs->iph->ihl << 2;
  42455. + ixs->pyldsz = ntohs(ixs->iph->tot_len) - ixs->iphlen;
  42456. + ixs->sa_len = satot(&ixs->ipsp->ips_said, 0, ixs->sa_txt, SATOT_BUF);
  42457. + KLIPS_PRINT(debug_tunnel & DB_TN_OXFS,
  42458. + "klips_debug:ipsec_xmit_encap_once: "
  42459. + "calling output for <%s%s%s>, SA:%s\n",
  42460. + IPS_XFORM_NAME(ixs->ipsp),
  42461. + ixs->sa_len ? ixs->sa_txt : " (error)");
  42462. +
  42463. + switch(ixs->ipsp->ips_said.proto) {
  42464. +#ifdef CONFIG_KLIPS_AH
  42465. + case IPPROTO_AH:
  42466. + headroom += sizeof(struct ahhdr);
  42467. + break;
  42468. +#endif /* CONFIG_KLIPS_AH */
  42469. +
  42470. +#ifdef CONFIG_KLIPS_ESP
  42471. + case IPPROTO_ESP:
  42472. + ixt_e=ixs->ipsp->ips_alg_enc;
  42473. + if (ixt_e) {
  42474. + blocksize = ixt_e->ixt_common.ixt_blocksize;
  42475. + headroom += ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  42476. + } else {
  42477. + ixs->stats->tx_errors++;
  42478. + return IPSEC_XMIT_ESP_BADALG;
  42479. + }
  42480. +
  42481. + ixt_a=ixs->ipsp->ips_alg_auth;
  42482. + if (ixt_a) {
  42483. + tailroom += AHHMAC_HASHLEN;
  42484. + } else
  42485. + switch(ixs->ipsp->ips_authalg) {
  42486. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42487. + case AH_MD5:
  42488. + authlen = AHHMAC_HASHLEN;
  42489. + break;
  42490. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42491. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42492. + case AH_SHA:
  42493. + authlen = AHHMAC_HASHLEN;
  42494. + break;
  42495. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42496. + case AH_NONE:
  42497. + break;
  42498. + default:
  42499. + ixs->stats->tx_errors++;
  42500. + return IPSEC_XMIT_ESP_BADALG;
  42501. + }
  42502. + tailroom += blocksize != 1 ?
  42503. + ((blocksize - ((ixs->pyldsz + 2) % blocksize)) % blocksize) + 2 :
  42504. + ((4 - ((ixs->pyldsz + 2) % 4)) % 4) + 2;
  42505. + tailroom += authlen;
  42506. + break;
  42507. +#endif /* CONFIG_KLIPS_ESP */
  42508. +
  42509. +#ifdef CONFIG_KLIPS_IPIP
  42510. + case IPPROTO_IPIP:
  42511. + headroom += sizeof(struct iphdr);
  42512. + ixs->iphlen = sizeof(struct iphdr);
  42513. + break;
  42514. +#endif /* !CONFIG_KLIPS_IPIP */
  42515. +
  42516. +#ifdef CONFIG_KLIPS_IPCOMP
  42517. + case IPPROTO_COMP:
  42518. + break;
  42519. +#endif /* CONFIG_KLIPS_IPCOMP */
  42520. +
  42521. + default:
  42522. + ixs->stats->tx_errors++;
  42523. + return IPSEC_XMIT_BADPROTO;
  42524. + }
  42525. +
  42526. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42527. + "klips_debug:ipsec_xmit_encap_once: "
  42528. + "pushing %d bytes, putting %d, proto %d.\n",
  42529. + headroom, tailroom, ixs->ipsp->ips_said.proto);
  42530. + if(skb_headroom(ixs->skb) < headroom) {
  42531. + printk(KERN_WARNING
  42532. + "klips_error:ipsec_xmit_encap_once: "
  42533. + "tried to skb_push headroom=%d, %d available. This should never happen, please report.\n",
  42534. + headroom, skb_headroom(ixs->skb));
  42535. + ixs->stats->tx_errors++;
  42536. + return IPSEC_XMIT_ESP_PUSHPULLERR;
  42537. + }
  42538. +
  42539. + dat = skb_push(ixs->skb, headroom);
  42540. + ilen = ixs->skb->len - tailroom;
  42541. + if(skb_tailroom(ixs->skb) < tailroom) {
  42542. + printk(KERN_WARNING
  42543. + "klips_error:ipsec_xmit_encap_once: "
  42544. + "tried to skb_put %d, %d available. This should never happen, please report.\n",
  42545. + tailroom, skb_tailroom(ixs->skb));
  42546. + ixs->stats->tx_errors++;
  42547. + return IPSEC_XMIT_ESP_PUSHPULLERR;
  42548. + }
  42549. + skb_put(ixs->skb, tailroom);
  42550. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42551. + "klips_debug:ipsec_xmit_encap_once: "
  42552. + "head,tailroom: %d,%d before xform.\n",
  42553. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  42554. + len = ixs->skb->len;
  42555. + if(len > 0xfff0) {
  42556. + printk(KERN_WARNING "klips_error:ipsec_xmit_encap_once: "
  42557. + "tot_len (%d) > 65520. This should never happen, please report.\n",
  42558. + len);
  42559. + ixs->stats->tx_errors++;
  42560. + return IPSEC_XMIT_BADLEN;
  42561. + }
  42562. + memmove((void *)dat, (void *)(dat + headroom), ixs->iphlen);
  42563. + ixs->iph = (struct iphdr *)dat;
  42564. + ixs->iph->tot_len = htons(ixs->skb->len);
  42565. +
  42566. + switch(ixs->ipsp->ips_said.proto) {
  42567. +#ifdef CONFIG_KLIPS_ESP
  42568. + case IPPROTO_ESP:
  42569. + espp = (struct esphdr *)(dat + ixs->iphlen);
  42570. + espp->esp_spi = ixs->ipsp->ips_said.spi;
  42571. + espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  42572. +
  42573. + if (!ixt_e) {
  42574. + ixs->stats->tx_errors++;
  42575. + return IPSEC_XMIT_ESP_BADALG;
  42576. + }
  42577. +
  42578. + idat = dat + ixs->iphlen + headroom;
  42579. + ilen = len - (ixs->iphlen + headroom + authlen);
  42580. +
  42581. + /* Self-describing padding */
  42582. + pad = &dat[len - tailroom];
  42583. + padlen = tailroom - 2 - authlen;
  42584. + for (i = 0; i < padlen; i++) {
  42585. + pad[i] = i + 1;
  42586. + }
  42587. + dat[len - authlen - 2] = padlen;
  42588. +
  42589. + dat[len - authlen - 1] = ixs->iph->protocol;
  42590. + ixs->iph->protocol = IPPROTO_ESP;
  42591. +#ifdef CONFIG_KLIPS_DEBUG
  42592. + if(debug_tunnel & DB_TN_ENCAP) {
  42593. + dmp("pre-encrypt", dat, len);
  42594. + }
  42595. +#endif
  42596. +
  42597. + /*
  42598. + * Do all operations here:
  42599. + * copy IV->ESP, encrypt, update ips IV
  42600. + *
  42601. + */
  42602. + {
  42603. + int ret;
  42604. + memcpy(espp->esp_iv,
  42605. + ixs->ipsp->ips_iv,
  42606. + ixs->ipsp->ips_iv_size);
  42607. + ret=ipsec_alg_esp_encrypt(ixs->ipsp,
  42608. + idat, ilen, espp->esp_iv,
  42609. + IPSEC_ALG_ENCRYPT);
  42610. +
  42611. + prng_bytes(&ipsec_prng,
  42612. + (char *)ixs->ipsp->ips_iv,
  42613. + ixs->ipsp->ips_iv_size);
  42614. + }
  42615. +
  42616. + if (ixt_a) {
  42617. + ipsec_alg_sa_esp_hash(ixs->ipsp,
  42618. + (caddr_t)espp, len - ixs->iphlen - authlen,
  42619. + &(dat[len - authlen]), authlen);
  42620. +
  42621. + } else
  42622. + switch(ixs->ipsp->ips_authalg) {
  42623. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42624. + case AH_MD5:
  42625. + dmp("espp", (char*)espp, len - ixs->iphlen - authlen);
  42626. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42627. + dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  42628. + osMD5Update(&tctx.md5, (caddr_t)espp, len - ixs->iphlen - authlen);
  42629. + dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  42630. + osMD5Final(hash, &tctx.md5);
  42631. + dmp("ictx hash", (char*)&hash, sizeof(hash));
  42632. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42633. + dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  42634. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  42635. + dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  42636. + osMD5Final(hash, &tctx.md5);
  42637. + dmp("octx hash", (char*)&hash, sizeof(hash));
  42638. + memcpy(&(dat[len - authlen]), hash, authlen);
  42639. +
  42640. + /* paranoid */
  42641. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  42642. + memset((caddr_t)hash, 0, sizeof(*hash));
  42643. + break;
  42644. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42645. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42646. + case AH_SHA:
  42647. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42648. + SHA1Update(&tctx.sha1, (caddr_t)espp, len - ixs->iphlen - authlen);
  42649. + SHA1Final(hash, &tctx.sha1);
  42650. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42651. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  42652. + SHA1Final(hash, &tctx.sha1);
  42653. + memcpy(&(dat[len - authlen]), hash, authlen);
  42654. +
  42655. + /* paranoid */
  42656. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  42657. + memset((caddr_t)hash, 0, sizeof(*hash));
  42658. + break;
  42659. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42660. + case AH_NONE:
  42661. + break;
  42662. + default:
  42663. + ixs->stats->tx_errors++;
  42664. + return IPSEC_XMIT_AH_BADALG;
  42665. + }
  42666. +#ifdef NET_21
  42667. + ixs->skb->h.raw = (unsigned char*)espp;
  42668. +#endif /* NET_21 */
  42669. + break;
  42670. +#endif /* !CONFIG_KLIPS_ESP */
  42671. +#ifdef CONFIG_KLIPS_AH
  42672. + case IPPROTO_AH:
  42673. + ahp = (struct ahhdr *)(dat + ixs->iphlen);
  42674. + ahp->ah_spi = ixs->ipsp->ips_said.spi;
  42675. + ahp->ah_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  42676. + ahp->ah_rv = 0;
  42677. + ahp->ah_nh = ixs->iph->protocol;
  42678. + ahp->ah_hl = (headroom >> 2) - sizeof(__u64)/sizeof(__u32);
  42679. + ixs->iph->protocol = IPPROTO_AH;
  42680. + dmp("ahp", (char*)ahp, sizeof(*ahp));
  42681. +
  42682. + ipo = *ixs->iph;
  42683. + ipo.tos = 0;
  42684. + ipo.frag_off = 0;
  42685. + ipo.ttl = 0;
  42686. + ipo.check = 0;
  42687. + dmp("ipo", (char*)&ipo, sizeof(ipo));
  42688. +
  42689. + switch(ixs->ipsp->ips_authalg) {
  42690. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42691. + case AH_MD5:
  42692. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42693. + dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  42694. + osMD5Update(&tctx.md5, (unsigned char *)&ipo, sizeof (struct iphdr));
  42695. + dmp("ictx+ipo", (char*)&tctx.md5, sizeof(tctx.md5));
  42696. + osMD5Update(&tctx.md5, (unsigned char *)ahp, headroom - sizeof(ahp->ah_data));
  42697. + dmp("ictx+ahp", (char*)&tctx.md5, sizeof(tctx.md5));
  42698. + osMD5Update(&tctx.md5, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  42699. + dmp("ictx+zeroes", (char*)&tctx.md5, sizeof(tctx.md5));
  42700. + osMD5Update(&tctx.md5, dat + ixs->iphlen + headroom, len - ixs->iphlen - headroom);
  42701. + dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  42702. + osMD5Final(hash, &tctx.md5);
  42703. + dmp("ictx hash", (char*)&hash, sizeof(hash));
  42704. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42705. + dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  42706. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  42707. + dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  42708. + osMD5Final(hash, &tctx.md5);
  42709. + dmp("octx hash", (char*)&hash, sizeof(hash));
  42710. +
  42711. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  42712. +
  42713. + /* paranoid */
  42714. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  42715. + memset((caddr_t)hash, 0, sizeof(*hash));
  42716. + break;
  42717. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42718. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42719. + case AH_SHA:
  42720. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42721. + SHA1Update(&tctx.sha1, (unsigned char *)&ipo, sizeof (struct iphdr));
  42722. + SHA1Update(&tctx.sha1, (unsigned char *)ahp, headroom - sizeof(ahp->ah_data));
  42723. + SHA1Update(&tctx.sha1, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  42724. + SHA1Update(&tctx.sha1, dat + ixs->iphlen + headroom, len - ixs->iphlen - headroom);
  42725. + SHA1Final(hash, &tctx.sha1);
  42726. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42727. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  42728. + SHA1Final(hash, &tctx.sha1);
  42729. +
  42730. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  42731. +
  42732. + /* paranoid */
  42733. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  42734. + memset((caddr_t)hash, 0, sizeof(*hash));
  42735. + break;
  42736. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42737. + default:
  42738. + ixs->stats->tx_errors++;
  42739. + return IPSEC_XMIT_AH_BADALG;
  42740. + }
  42741. +#ifdef NET_21
  42742. + ixs->skb->h.raw = (unsigned char*)ahp;
  42743. +#endif /* NET_21 */
  42744. + break;
  42745. +#endif /* CONFIG_KLIPS_AH */
  42746. +#ifdef CONFIG_KLIPS_IPIP
  42747. + case IPPROTO_IPIP:
  42748. + ixs->iph->version = 4;
  42749. + switch(sysctl_ipsec_tos) {
  42750. + case 0:
  42751. +#ifdef NET_21
  42752. + ixs->iph->tos = ixs->skb->nh.iph->tos;
  42753. +#else /* NET_21 */
  42754. + ixs->iph->tos = ixs->skb->ip_hdr->tos;
  42755. +#endif /* NET_21 */
  42756. + break;
  42757. + case 1:
  42758. + ixs->iph->tos = 0;
  42759. + break;
  42760. + default:
  42761. + break;
  42762. + }
  42763. + ixs->iph->ttl = SYSCTL_IPSEC_DEFAULT_TTL;
  42764. + ixs->iph->frag_off = 0;
  42765. + ixs->iph->saddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_s))->sin_addr.s_addr;
  42766. + ixs->iph->daddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_d))->sin_addr.s_addr;
  42767. + ixs->iph->protocol = IPPROTO_IPIP;
  42768. + ixs->iph->ihl = sizeof(struct iphdr) >> 2;
  42769. +
  42770. + KLIPS_IP_SELECT_IDENT(ixs->iph, ixs->skb);
  42771. +
  42772. + ixs->newdst = (__u32)ixs->iph->daddr;
  42773. + ixs->newsrc = (__u32)ixs->iph->saddr;
  42774. +
  42775. +#ifdef NET_21
  42776. + ixs->skb->h.ipiph = ixs->skb->nh.iph;
  42777. +#endif /* NET_21 */
  42778. + break;
  42779. +#endif /* !CONFIG_KLIPS_IPIP */
  42780. +#ifdef CONFIG_KLIPS_IPCOMP
  42781. + case IPPROTO_COMP:
  42782. + {
  42783. + unsigned int flags = 0;
  42784. +#ifdef CONFIG_KLIPS_DEBUG
  42785. + unsigned int old_tot_len = ntohs(ixs->iph->tot_len);
  42786. +#endif /* CONFIG_KLIPS_DEBUG */
  42787. + ixs->ipsp->ips_comp_ratio_dbytes += ntohs(ixs->iph->tot_len);
  42788. +
  42789. + ixs->skb = skb_compress(ixs->skb, ixs->ipsp, &flags);
  42790. +
  42791. +#ifdef NET_21
  42792. + ixs->iph = ixs->skb->nh.iph;
  42793. +#else /* NET_21 */
  42794. + ixs->iph = ixs->skb->ip_hdr;
  42795. +#endif /* NET_21 */
  42796. +
  42797. + ixs->ipsp->ips_comp_ratio_cbytes += ntohs(ixs->iph->tot_len);
  42798. +
  42799. +#ifdef CONFIG_KLIPS_DEBUG
  42800. + if (debug_tunnel & DB_TN_CROUT)
  42801. + {
  42802. + if (old_tot_len > ntohs(ixs->iph->tot_len))
  42803. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42804. + "klips_debug:ipsec_xmit_encap_once: "
  42805. + "packet shrunk from %d to %d bytes after compression, cpi=%04x (should be from spi=%08x, spi&0xffff=%04x.\n",
  42806. + old_tot_len, ntohs(ixs->iph->tot_len),
  42807. + ntohs(((struct ipcomphdr*)(((char*)ixs->iph) + ((ixs->iph->ihl) << 2)))->ipcomp_cpi),
  42808. + ntohl(ixs->ipsp->ips_said.spi),
  42809. + (__u16)(ntohl(ixs->ipsp->ips_said.spi) & 0x0000ffff));
  42810. + else
  42811. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42812. + "klips_debug:ipsec_xmit_encap_once: "
  42813. + "packet did not compress (flags = %d).\n",
  42814. + flags);
  42815. + }
  42816. +#endif /* CONFIG_KLIPS_DEBUG */
  42817. + }
  42818. + break;
  42819. +#endif /* CONFIG_KLIPS_IPCOMP */
  42820. + default:
  42821. + ixs->stats->tx_errors++;
  42822. + return IPSEC_XMIT_BADPROTO;
  42823. + }
  42824. +
  42825. +#ifdef NET_21
  42826. + ixs->skb->nh.raw = ixs->skb->data;
  42827. +#else /* NET_21 */
  42828. + ixs->skb->ip_hdr = ixs->skb->h.iph = (struct iphdr *) ixs->skb->data;
  42829. +#endif /* NET_21 */
  42830. + ixs->iph->check = 0;
  42831. + ixs->iph->check = ip_fast_csum((unsigned char *)ixs->iph, ixs->iph->ihl);
  42832. +
  42833. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42834. + "klips_debug:ipsec_xmit_encap_once: "
  42835. + "after <%s%s%s>, SA:%s:\n",
  42836. + IPS_XFORM_NAME(ixs->ipsp),
  42837. + ixs->sa_len ? ixs->sa_txt : " (error)");
  42838. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->iph);
  42839. +
  42840. + ixs->ipsp->ips_life.ipl_bytes.ipl_count += len;
  42841. + ixs->ipsp->ips_life.ipl_bytes.ipl_last = len;
  42842. +
  42843. + if(!ixs->ipsp->ips_life.ipl_usetime.ipl_count) {
  42844. + ixs->ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  42845. + }
  42846. + ixs->ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  42847. + ixs->ipsp->ips_life.ipl_packets.ipl_count++;
  42848. +
  42849. + ixs->ipsp = ixs->ipsp->ips_onext;
  42850. +
  42851. + return IPSEC_XMIT_OK;
  42852. +}
  42853. +
  42854. +/*
  42855. + * If the IP packet (iph) is a carrying TCP/UDP, then set the encaps
  42856. + * source and destination ports to those from the TCP/UDP header.
  42857. + */
  42858. +void ipsec_extract_ports(struct iphdr * iph, struct sockaddr_encap * er)
  42859. +{
  42860. + struct udphdr *udp;
  42861. +
  42862. + switch (iph->protocol) {
  42863. + case IPPROTO_UDP:
  42864. + case IPPROTO_TCP:
  42865. + /*
  42866. + * The ports are at the same offsets in a TCP and UDP
  42867. + * header so hack it ...
  42868. + */
  42869. + udp = (struct udphdr*)(((char*)iph)+(iph->ihl<<2));
  42870. + er->sen_sport = udp->source;
  42871. + er->sen_dport = udp->dest;
  42872. + break;
  42873. + default:
  42874. + er->sen_sport = 0;
  42875. + er->sen_dport = 0;
  42876. + break;
  42877. + }
  42878. +}
  42879. +
  42880. +/*
  42881. + * A TRAP eroute is installed and we want to replace it with a HOLD
  42882. + * eroute.
  42883. + */
  42884. +static int create_hold_eroute(struct eroute *origtrap,
  42885. + struct sk_buff * skb, struct iphdr * iph,
  42886. + uint32_t eroute_pid)
  42887. +{
  42888. + struct eroute hold_eroute;
  42889. + ip_said hold_said;
  42890. + struct sk_buff *first, *last;
  42891. + int error;
  42892. +
  42893. + first = last = NULL;
  42894. + memset((caddr_t)&hold_eroute, 0, sizeof(hold_eroute));
  42895. + memset((caddr_t)&hold_said, 0, sizeof(hold_said));
  42896. +
  42897. + hold_said.proto = IPPROTO_INT;
  42898. + hold_said.spi = htonl(SPI_HOLD);
  42899. + hold_said.dst.u.v4.sin_addr.s_addr = INADDR_ANY;
  42900. +
  42901. + hold_eroute.er_eaddr.sen_len = sizeof(struct sockaddr_encap);
  42902. + hold_eroute.er_emask.sen_len = sizeof(struct sockaddr_encap);
  42903. + hold_eroute.er_eaddr.sen_family = AF_ENCAP;
  42904. + hold_eroute.er_emask.sen_family = AF_ENCAP;
  42905. + hold_eroute.er_eaddr.sen_type = SENT_IP4;
  42906. + hold_eroute.er_emask.sen_type = 255;
  42907. +
  42908. + hold_eroute.er_eaddr.sen_ip_src.s_addr = iph->saddr;
  42909. + hold_eroute.er_eaddr.sen_ip_dst.s_addr = iph->daddr;
  42910. + hold_eroute.er_emask.sen_ip_src.s_addr = INADDR_BROADCAST;
  42911. + hold_eroute.er_emask.sen_ip_dst.s_addr = INADDR_BROADCAST;
  42912. + hold_eroute.er_emask.sen_sport = 0;
  42913. + hold_eroute.er_emask.sen_dport = 0;
  42914. + hold_eroute.er_pid = eroute_pid;
  42915. + hold_eroute.er_count = 0;
  42916. + hold_eroute.er_lasttime = jiffies/HZ;
  42917. +
  42918. + /*
  42919. + * if it wasn't captured by a wildcard, then don't record it as
  42920. + * a wildcard.
  42921. + */
  42922. + if(origtrap->er_eaddr.sen_proto != 0) {
  42923. + hold_eroute.er_eaddr.sen_proto = iph->protocol;
  42924. +
  42925. + if((iph->protocol == IPPROTO_TCP ||
  42926. + iph->protocol == IPPROTO_UDP) &&
  42927. + (origtrap->er_eaddr.sen_sport != 0 ||
  42928. + origtrap->er_eaddr.sen_dport != 0)) {
  42929. +
  42930. + if(origtrap->er_eaddr.sen_sport != 0)
  42931. + hold_eroute.er_emask.sen_sport = ~0;
  42932. +
  42933. + if(origtrap->er_eaddr.sen_dport != 0)
  42934. + hold_eroute.er_emask.sen_dport = ~0;
  42935. +
  42936. + ipsec_extract_ports(iph, &hold_eroute.er_eaddr);
  42937. + }
  42938. + }
  42939. +
  42940. +#ifdef CONFIG_KLIPS_DEBUG
  42941. + if (debug_pfkey) {
  42942. + char buf1[64], buf2[64];
  42943. + subnettoa(hold_eroute.er_eaddr.sen_ip_src,
  42944. + hold_eroute.er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  42945. + subnettoa(hold_eroute.er_eaddr.sen_ip_dst,
  42946. + hold_eroute.er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  42947. + KLIPS_PRINT(debug_pfkey,
  42948. + "klips_debug:ipsec_tunnel_start_xmit: "
  42949. + "calling breakeroute and makeroute for %s:%d->%s:%d %d HOLD eroute.\n",
  42950. + buf1, ntohs(hold_eroute.er_eaddr.sen_sport),
  42951. + buf2, ntohs(hold_eroute.er_eaddr.sen_dport),
  42952. + hold_eroute.er_eaddr.sen_proto);
  42953. + }
  42954. +#endif /* CONFIG_KLIPS_DEBUG */
  42955. +
  42956. + if (ipsec_breakroute(&(hold_eroute.er_eaddr), &(hold_eroute.er_emask),
  42957. + &first, &last)) {
  42958. + KLIPS_PRINT(debug_pfkey,
  42959. + "klips_debug:ipsec_tunnel_start_xmit: "
  42960. + "HOLD breakeroute found nothing.\n");
  42961. + } else {
  42962. + KLIPS_PRINT(debug_pfkey,
  42963. + "klips_debug:ipsec_tunnel_start_xmit: "
  42964. + "HOLD breakroute deleted %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u %u\n",
  42965. + NIPQUAD(hold_eroute.er_eaddr.sen_ip_src),
  42966. + ntohs(hold_eroute.er_eaddr.sen_sport),
  42967. + NIPQUAD(hold_eroute.er_eaddr.sen_ip_dst),
  42968. + ntohs(hold_eroute.er_eaddr.sen_dport),
  42969. + hold_eroute.er_eaddr.sen_proto);
  42970. + }
  42971. + if (first != NULL)
  42972. + kfree_skb(first);
  42973. + if (last != NULL)
  42974. + kfree_skb(last);
  42975. +
  42976. + error = ipsec_makeroute(&(hold_eroute.er_eaddr),
  42977. + &(hold_eroute.er_emask),
  42978. + hold_said, eroute_pid, skb, NULL, NULL);
  42979. + if (error) {
  42980. + KLIPS_PRINT(debug_pfkey,
  42981. + "klips_debug:ipsec_tunnel_start_xmit: "
  42982. + "HOLD makeroute returned %d, failed.\n", error);
  42983. + } else {
  42984. + KLIPS_PRINT(debug_pfkey,
  42985. + "klips_debug:ipsec_tunnel_start_xmit: "
  42986. + "HOLD makeroute call successful.\n");
  42987. + }
  42988. + return (error == 0);
  42989. +}
  42990. +
  42991. +/*
  42992. + * upon entry to this function, ixs->skb should be setup
  42993. + * as follows:
  42994. + *
  42995. + * data = beginning of IP packet <- differs from ipsec_rcv().
  42996. + * nh.raw = beginning of IP packet.
  42997. + * h.raw = data after the IP packet.
  42998. + *
  42999. + */
  43000. +enum ipsec_xmit_value
  43001. +ipsec_xmit_encap_bundle(struct ipsec_xmit_state *ixs)
  43002. +{
  43003. + struct ipsec_alg_enc *ixt_e = NULL;
  43004. + struct ipsec_alg_auth *ixt_a = NULL;
  43005. + int blocksize = 8;
  43006. + enum ipsec_xmit_value bundle_stat = IPSEC_XMIT_OK;
  43007. +
  43008. + ixs->newdst = ixs->orgdst = ixs->iph->daddr;
  43009. + ixs->newsrc = ixs->orgsrc = ixs->iph->saddr;
  43010. + ixs->orgedst = ixs->outgoing_said.dst.u.v4.sin_addr.s_addr;
  43011. + ixs->iphlen = ixs->iph->ihl << 2;
  43012. + ixs->pyldsz = ntohs(ixs->iph->tot_len) - ixs->iphlen;
  43013. + ixs->max_headroom = ixs->max_tailroom = 0;
  43014. +
  43015. + if (ixs->outgoing_said.proto == IPPROTO_INT) {
  43016. + switch (ntohl(ixs->outgoing_said.spi)) {
  43017. + case SPI_DROP:
  43018. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43019. + "klips_debug:ipsec_xmit_encap_bundle: "
  43020. + "shunt SA of DROP or no eroute: dropping.\n");
  43021. + ixs->stats->tx_dropped++;
  43022. + break;
  43023. +
  43024. + case SPI_REJECT:
  43025. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43026. + "klips_debug:ipsec_xmit_encap_bundle: "
  43027. + "shunt SA of REJECT: notifying and dropping.\n");
  43028. + ICMP_SEND(ixs->skb,
  43029. + ICMP_DEST_UNREACH,
  43030. + ICMP_PKT_FILTERED,
  43031. + 0,
  43032. + ixs->physdev);
  43033. + ixs->stats->tx_dropped++;
  43034. + break;
  43035. +
  43036. + case SPI_PASS:
  43037. +#ifdef NET_21
  43038. + ixs->pass = 1;
  43039. +#endif /* NET_21 */
  43040. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43041. + "klips_debug:ipsec_xmit_encap_bundle: "
  43042. + "PASS: calling dev_queue_xmit\n");
  43043. + return IPSEC_XMIT_PASS;
  43044. + goto cleanup;
  43045. +
  43046. + case SPI_HOLD:
  43047. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43048. + "klips_debug:ipsec_xmit_encap_bundle: "
  43049. + "shunt SA of HOLD: this does not make sense here, dropping.\n");
  43050. + ixs->stats->tx_dropped++;
  43051. + break;
  43052. +
  43053. + case SPI_TRAP:
  43054. + case SPI_TRAPSUBNET:
  43055. + {
  43056. + struct sockaddr_in src, dst;
  43057. +#ifdef CONFIG_KLIPS_DEBUG
  43058. + char bufsrc[ADDRTOA_BUF], bufdst[ADDRTOA_BUF];
  43059. +#endif /* CONFIG_KLIPS_DEBUG */
  43060. +
  43061. + /* Signal all listening KMds with a PF_KEY ACQUIRE */
  43062. +
  43063. + memset(&src, 0, sizeof(src));
  43064. + memset(&dst, 0, sizeof(dst));
  43065. + src.sin_family = AF_INET;
  43066. + dst.sin_family = AF_INET;
  43067. + src.sin_addr.s_addr = ixs->iph->saddr;
  43068. + dst.sin_addr.s_addr = ixs->iph->daddr;
  43069. +
  43070. + ixs->ips.ips_transport_protocol = 0;
  43071. + src.sin_port = 0;
  43072. + dst.sin_port = 0;
  43073. +
  43074. + if(ixs->eroute->er_eaddr.sen_proto != 0) {
  43075. + ixs->ips.ips_transport_protocol = ixs->iph->protocol;
  43076. +
  43077. + if(ixs->eroute->er_eaddr.sen_sport != 0) {
  43078. + src.sin_port =
  43079. + (ixs->iph->protocol == IPPROTO_UDP
  43080. + ? ((struct udphdr*) (((caddr_t)ixs->iph) + (ixs->iph->ihl << 2)))->source
  43081. + : (ixs->iph->protocol == IPPROTO_TCP
  43082. + ? ((struct tcphdr*)((caddr_t)ixs->iph + (ixs->iph->ihl << 2)))->source
  43083. + : 0));
  43084. + }
  43085. + if(ixs->eroute->er_eaddr.sen_dport != 0) {
  43086. + dst.sin_port =
  43087. + (ixs->iph->protocol == IPPROTO_UDP
  43088. + ? ((struct udphdr*) (((caddr_t)ixs->iph) + (ixs->iph->ihl << 2)))->dest
  43089. + : (ixs->iph->protocol == IPPROTO_TCP
  43090. + ? ((struct tcphdr*)((caddr_t)ixs->iph + (ixs->iph->ihl << 2)))->dest
  43091. + : 0));
  43092. + }
  43093. + }
  43094. +
  43095. + ixs->ips.ips_addr_s = (struct sockaddr*)(&src);
  43096. + ixs->ips.ips_addr_d = (struct sockaddr*)(&dst);
  43097. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43098. + "klips_debug:ipsec_xmit_encap_bundle: "
  43099. + "SADB_ACQUIRE sent with src=%s:%d, dst=%s:%d, proto=%d.\n",
  43100. + addrtoa(((struct sockaddr_in*)(ixs->ips.ips_addr_s))->sin_addr, 0, bufsrc, sizeof(bufsrc)) <= ADDRTOA_BUF ? bufsrc : "BAD_ADDR",
  43101. + ntohs(((struct sockaddr_in*)(ixs->ips.ips_addr_s))->sin_port),
  43102. + addrtoa(((struct sockaddr_in*)(ixs->ips.ips_addr_d))->sin_addr, 0, bufdst, sizeof(bufdst)) <= ADDRTOA_BUF ? bufdst : "BAD_ADDR",
  43103. + ntohs(((struct sockaddr_in*)(ixs->ips.ips_addr_d))->sin_port),
  43104. + ixs->ips.ips_said.proto);
  43105. +
  43106. + /* increment count of total traps needed */
  43107. + ipsec_xmit_trap_count++;
  43108. +
  43109. + if (pfkey_acquire(&ixs->ips) == 0) {
  43110. +
  43111. + /* note that we succeeded */
  43112. + ipsec_xmit_trap_sendcount++;
  43113. +
  43114. + if (ixs->outgoing_said.spi==htonl(SPI_TRAPSUBNET)) {
  43115. + /*
  43116. + * The spinlock is to prevent any other
  43117. + * process from accessing or deleting
  43118. + * the eroute while we are using and
  43119. + * updating it.
  43120. + */
  43121. + spin_lock(&eroute_lock);
  43122. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  43123. + if(ixs->eroute) {
  43124. + ixs->eroute->er_said.spi = htonl(SPI_HOLD);
  43125. + ixs->eroute->er_first = ixs->skb;
  43126. + ixs->skb = NULL;
  43127. + }
  43128. + spin_unlock(&eroute_lock);
  43129. + } else if (create_hold_eroute(ixs->eroute,
  43130. + ixs->skb,
  43131. + ixs->iph,
  43132. + ixs->eroute_pid)) {
  43133. + ixs->skb = NULL;
  43134. + }
  43135. + /* whether or not the above succeeded, we continue */
  43136. +
  43137. + }
  43138. + ixs->stats->tx_dropped++;
  43139. + }
  43140. + default:
  43141. + /* XXX what do we do with an unknown shunt spi? */
  43142. + break;
  43143. + } /* switch (ntohl(ixs->outgoing_said.spi)) */
  43144. + return IPSEC_XMIT_STOLEN;
  43145. + } /* if (ixs->outgoing_said.proto == IPPROTO_INT) */
  43146. +
  43147. + /*
  43148. + The spinlock is to prevent any other process from
  43149. + accessing or deleting the ipsec_sa hash table or any of the
  43150. + ipsec_sa s while we are using and updating them.
  43151. +
  43152. + This is not optimal, but was relatively straightforward
  43153. + at the time. A better way to do it has been planned for
  43154. + more than a year, to lock the hash table and put reference
  43155. + counts on each ipsec_sa instead. This is not likely to happen
  43156. + in KLIPS1 unless a volunteer contributes it, but will be
  43157. + designed into KLIPS2.
  43158. + */
  43159. + spin_lock(&tdb_lock);
  43160. +
  43161. + ixs->ipsp = ipsec_sa_getbyid(&ixs->outgoing_said);
  43162. + ixs->sa_len = satot(&ixs->outgoing_said, 0, ixs->sa_txt, sizeof(ixs->sa_txt));
  43163. +
  43164. + if (ixs->ipsp == NULL) {
  43165. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43166. + "klips_debug:ipsec_xmit_encap_bundle: "
  43167. + "no ipsec_sa for SA%s: outgoing packet with no SA, dropped.\n",
  43168. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43169. + if(ixs->stats) {
  43170. + ixs->stats->tx_dropped++;
  43171. + }
  43172. + bundle_stat = IPSEC_XMIT_SAIDNOTFOUND;
  43173. + goto cleanup;
  43174. + }
  43175. +
  43176. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43177. + "klips_debug:ipsec_xmit_encap_bundle: "
  43178. + "found ipsec_sa -- SA:<%s%s%s> %s\n",
  43179. + IPS_XFORM_NAME(ixs->ipsp),
  43180. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43181. +
  43182. + /*
  43183. + * How much headroom do we need to be able to apply
  43184. + * all the grouped transforms?
  43185. + */
  43186. + ixs->ipsq = ixs->ipsp; /* save the head of the ipsec_sa chain */
  43187. + while (ixs->ipsp) {
  43188. + ixs->sa_len = satot(&ixs->ipsp->ips_said, 0, ixs->sa_txt, sizeof(ixs->sa_txt));
  43189. + if(ixs->sa_len == 0) {
  43190. + strcpy(ixs->sa_txt, "(error)");
  43191. + }
  43192. +
  43193. + /* If it is in larval state, drop the packet, we cannot process yet. */
  43194. + if(ixs->ipsp->ips_state == SADB_SASTATE_LARVAL) {
  43195. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43196. + "klips_debug:ipsec_xmit_encap_bundle: "
  43197. + "ipsec_sa in larval state for SA:<%s%s%s> %s, cannot be used yet, dropping packet.\n",
  43198. + IPS_XFORM_NAME(ixs->ipsp),
  43199. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43200. + if(ixs->stats) {
  43201. + ixs->stats->tx_errors++;
  43202. + }
  43203. + bundle_stat = IPSEC_XMIT_SAIDNOTLIVE;
  43204. + goto cleanup;
  43205. + }
  43206. +
  43207. + if(ixs->ipsp->ips_state == SADB_SASTATE_DEAD) {
  43208. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43209. + "klips_debug:ipsec_xmit_encap_bundle: "
  43210. + "ipsec_sa in dead state for SA:<%s%s%s> %s, can no longer be used, dropping packet.\n",
  43211. + IPS_XFORM_NAME(ixs->ipsp),
  43212. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43213. + ixs->stats->tx_errors++;
  43214. + bundle_stat = IPSEC_XMIT_SAIDNOTLIVE;
  43215. + goto cleanup;
  43216. + }
  43217. +
  43218. + /* If the replay window counter == -1, expire SA, it will roll */
  43219. + if(ixs->ipsp->ips_replaywin && ixs->ipsp->ips_replaywin_lastseq == -1) {
  43220. + pfkey_expire(ixs->ipsp, 1);
  43221. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43222. + "klips_debug:ipsec_xmit_encap_bundle: "
  43223. + "replay window counter rolled for SA:<%s%s%s> %s, packet dropped, expiring SA.\n",
  43224. + IPS_XFORM_NAME(ixs->ipsp),
  43225. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43226. + ipsec_sa_delchain(ixs->ipsp);
  43227. + ixs->stats->tx_errors++;
  43228. + bundle_stat = IPSEC_XMIT_REPLAYROLLED;
  43229. + goto cleanup;
  43230. + }
  43231. +
  43232. + /*
  43233. + * if this is the first time we are using this SA, mark start time,
  43234. + * and offset hard/soft counters by "now" for later checking.
  43235. + */
  43236. +#if 0
  43237. + if(ixs->ipsp->ips_life.ipl_usetime.count == 0) {
  43238. + ixs->ipsp->ips_life.ipl_usetime.count = jiffies;
  43239. + ixs->ipsp->ips_life.ipl_usetime.hard += jiffies;
  43240. + ixs->ipsp->ips_life.ipl_usetime.soft += jiffies;
  43241. + }
  43242. +#endif
  43243. +
  43244. +
  43245. + if(ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_bytes, "bytes", ixs->sa_txt,
  43246. + ipsec_life_countbased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43247. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_addtime, "addtime",ixs->sa_txt,
  43248. + ipsec_life_timebased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43249. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_usetime, "usetime",ixs->sa_txt,
  43250. + ipsec_life_timebased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43251. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_packets, "packets",ixs->sa_txt,
  43252. + ipsec_life_countbased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied) {
  43253. +
  43254. + ipsec_sa_delchain(ixs->ipsp);
  43255. + ixs->stats->tx_errors++;
  43256. + bundle_stat = IPSEC_XMIT_LIFETIMEFAILED;
  43257. + goto cleanup;
  43258. + }
  43259. +
  43260. +
  43261. + ixs->headroom = ixs->tailroom = 0;
  43262. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43263. + "klips_debug:ipsec_xmit_encap_bundle: "
  43264. + "calling room for <%s%s%s>, SA:%s\n",
  43265. + IPS_XFORM_NAME(ixs->ipsp),
  43266. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43267. + switch(ixs->ipsp->ips_said.proto) {
  43268. +#ifdef CONFIG_KLIPS_AH
  43269. + case IPPROTO_AH:
  43270. + ixs->headroom += sizeof(struct ahhdr);
  43271. + break;
  43272. +#endif /* CONFIG_KLIPS_AH */
  43273. +#ifdef CONFIG_KLIPS_ESP
  43274. + case IPPROTO_ESP:
  43275. + ixt_e=ixs->ipsp->ips_alg_enc;
  43276. + if (ixt_e) {
  43277. + blocksize = ixt_e->ixt_common.ixt_blocksize;
  43278. + ixs->headroom += ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  43279. + }
  43280. + else {
  43281. + ixs->stats->tx_errors++;
  43282. + bundle_stat = IPSEC_XMIT_ESP_BADALG;
  43283. + goto cleanup;
  43284. + }
  43285. +
  43286. + if ((ixt_a=ixs->ipsp->ips_alg_auth)) {
  43287. + ixs->tailroom += AHHMAC_HASHLEN;
  43288. + } else
  43289. + switch(ixs->ipsp->ips_authalg) {
  43290. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  43291. + case AH_MD5:
  43292. + ixs->tailroom += AHHMAC_HASHLEN;
  43293. + break;
  43294. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  43295. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  43296. + case AH_SHA:
  43297. + ixs->tailroom += AHHMAC_HASHLEN;
  43298. + break;
  43299. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  43300. + case AH_NONE:
  43301. + break;
  43302. + default:
  43303. + ixs->stats->tx_errors++;
  43304. + bundle_stat = IPSEC_XMIT_AH_BADALG;
  43305. + goto cleanup;
  43306. + }
  43307. + ixs->tailroom += blocksize != 1 ?
  43308. + ((blocksize - ((ixs->pyldsz + 2) % blocksize)) % blocksize) + 2 :
  43309. + ((4 - ((ixs->pyldsz + 2) % 4)) % 4) + 2;
  43310. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  43311. + if ((ixs->ipsp->ips_natt_type) && (!ixs->natt_type)) {
  43312. + ixs->natt_type = ixs->ipsp->ips_natt_type;
  43313. + ixs->natt_sport = ixs->ipsp->ips_natt_sport;
  43314. + ixs->natt_dport = ixs->ipsp->ips_natt_dport;
  43315. + switch (ixs->natt_type) {
  43316. + case ESPINUDP_WITH_NON_IKE:
  43317. + ixs->natt_head = sizeof(struct udphdr)+(2*sizeof(__u32));
  43318. + break;
  43319. +
  43320. + case ESPINUDP_WITH_NON_ESP:
  43321. + ixs->natt_head = sizeof(struct udphdr);
  43322. + break;
  43323. +
  43324. + default:
  43325. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT
  43326. + , "klips_xmit: invalid nat-t type %d"
  43327. + , ixs->natt_type);
  43328. + bundle_stat = IPSEC_XMIT_ESPUDP_BADTYPE;
  43329. + goto cleanup;
  43330. +
  43331. + break;
  43332. + }
  43333. + ixs->tailroom += ixs->natt_head;
  43334. + }
  43335. +#endif
  43336. + break;
  43337. +#endif /* !CONFIG_KLIPS_ESP */
  43338. +#ifdef CONFIG_KLIPS_IPIP
  43339. + case IPPROTO_IPIP:
  43340. + ixs->headroom += sizeof(struct iphdr);
  43341. + break;
  43342. +#endif /* !CONFIG_KLIPS_IPIP */
  43343. + case IPPROTO_COMP:
  43344. +#ifdef CONFIG_KLIPS_IPCOMP
  43345. + /*
  43346. + We can't predict how much the packet will
  43347. + shrink without doing the actual compression.
  43348. + We could do it here, if we were the first
  43349. + encapsulation in the chain. That might save
  43350. + us a skb_copy_expand, since we might fit
  43351. + into the existing skb then. However, this
  43352. + would be a bit unclean (and this hack has
  43353. + bit us once), so we better not do it. After
  43354. + all, the skb_copy_expand is cheap in
  43355. + comparison to the actual compression.
  43356. + At least we know the packet will not grow.
  43357. + */
  43358. + break;
  43359. +#endif /* CONFIG_KLIPS_IPCOMP */
  43360. + default:
  43361. + ixs->stats->tx_errors++;
  43362. + bundle_stat = IPSEC_XMIT_BADPROTO;
  43363. + goto cleanup;
  43364. + }
  43365. + ixs->ipsp = ixs->ipsp->ips_onext;
  43366. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43367. + "klips_debug:ipsec_xmit_encap_bundle: "
  43368. + "Required head,tailroom: %d,%d\n",
  43369. + ixs->headroom, ixs->tailroom);
  43370. + ixs->max_headroom += ixs->headroom;
  43371. + ixs->max_tailroom += ixs->tailroom;
  43372. + ixs->pyldsz += (ixs->headroom + ixs->tailroom);
  43373. + }
  43374. + ixs->ipsp = ixs->ipsq; /* restore the head of the ipsec_sa chain */
  43375. +
  43376. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43377. + "klips_debug:ipsec_xmit_encap_bundle: "
  43378. + "existing head,tailroom: %d,%d before applying xforms with head,tailroom: %d,%d .\n",
  43379. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb),
  43380. + ixs->max_headroom, ixs->max_tailroom);
  43381. +
  43382. + ixs->tot_headroom += ixs->max_headroom;
  43383. + ixs->tot_tailroom += ixs->max_tailroom;
  43384. +
  43385. + ixs->mtudiff = ixs->cur_mtu + ixs->tot_headroom + ixs->tot_tailroom - ixs->physmtu;
  43386. +
  43387. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43388. + "klips_debug:ipsec_xmit_encap_bundle: "
  43389. + "mtu:%d physmtu:%d tothr:%d tottr:%d mtudiff:%d ippkttotlen:%d\n",
  43390. + ixs->cur_mtu, ixs->physmtu,
  43391. + ixs->tot_headroom, ixs->tot_tailroom, ixs->mtudiff, ntohs(ixs->iph->tot_len));
  43392. + if(ixs->mtudiff > 0) {
  43393. + int newmtu = ixs->physmtu - (ixs->tot_headroom + ((ixs->tot_tailroom + 2) & ~7) + 5);
  43394. +
  43395. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43396. + "klips_info:ipsec_xmit_encap_bundle: "
  43397. + "dev %s mtu of %d decreased by %d to %d\n",
  43398. + ixs->dev ? ixs->dev->name : "ifX",
  43399. + ixs->cur_mtu,
  43400. + ixs->cur_mtu - newmtu,
  43401. + newmtu);
  43402. + ixs->cur_mtu = newmtu;
  43403. +
  43404. + /* this would seem to adjust the MTU of the route as well */
  43405. +#if 0
  43406. + ixs->skb->dst->pmtu = ixs->prv->mtu; /* RGB */
  43407. +#endif /* 0 */
  43408. + }
  43409. +
  43410. + /*
  43411. + If the sender is doing PMTU discovery, and the
  43412. + packet doesn't fit within ixs->prv->mtu, notify him
  43413. + (unless it was an ICMP packet, or it was not the
  43414. + zero-offset packet) and send it anyways.
  43415. +
  43416. + Note: buggy firewall configuration may prevent the
  43417. + ICMP packet from getting back.
  43418. + */
  43419. + if(sysctl_ipsec_icmp
  43420. + && ixs->cur_mtu < ntohs(ixs->iph->tot_len)
  43421. + && (ixs->iph->frag_off & __constant_htons(IP_DF)) ) {
  43422. + int notify = ixs->iph->protocol != IPPROTO_ICMP
  43423. + && (ixs->iph->frag_off & __constant_htons(IP_OFFSET)) == 0;
  43424. +
  43425. +#ifdef IPSEC_obey_DF
  43426. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43427. + "klips_debug:ipsec_xmit_encap_bundle: "
  43428. + "fragmentation needed and DF set; %sdropping packet\n",
  43429. + notify ? "sending ICMP and " : "");
  43430. + if (notify)
  43431. + ICMP_SEND(ixs->skb,
  43432. + ICMP_DEST_UNREACH,
  43433. + ICMP_FRAG_NEEDED,
  43434. + ixs->cur_mtu,
  43435. + ixs->physdev);
  43436. + ixs->stats->tx_errors++;
  43437. + bundle_stat = IPSEC_XMIT_CANNOTFRAG;
  43438. + goto cleanup;
  43439. +#else /* IPSEC_obey_DF */
  43440. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43441. + "klips_debug:ipsec_xmit_encap_bundle: "
  43442. + "fragmentation needed and DF set; %spassing packet\n",
  43443. + notify ? "sending ICMP and " : "");
  43444. + if (notify)
  43445. + ICMP_SEND(ixs->skb,
  43446. + ICMP_DEST_UNREACH,
  43447. + ICMP_FRAG_NEEDED,
  43448. + ixs->cur_mtu,
  43449. + ixs->physdev);
  43450. +#endif /* IPSEC_obey_DF */
  43451. + }
  43452. +
  43453. +#ifdef MSS_HACK
  43454. + /*
  43455. + * If this is a transport mode TCP packet with
  43456. + * SYN set, determine an effective MSS based on
  43457. + * AH/ESP overheads determined above.
  43458. + */
  43459. + if (ixs->iph->protocol == IPPROTO_TCP
  43460. + && ixs->outgoing_said.proto != IPPROTO_IPIP) {
  43461. + struct tcphdr *tcph = ixs->skb->h.th;
  43462. + if (tcph->syn && !tcph->ack) {
  43463. + if(!ipsec_adjust_mss(ixs->skb, tcph, ixs->cur_mtu)) {
  43464. + printk(KERN_WARNING
  43465. + "klips_warning:ipsec_xmit_encap_bundle: "
  43466. + "ipsec_adjust_mss() failed\n");
  43467. + ixs->stats->tx_errors++;
  43468. + bundle_stat = IPSEC_XMIT_MSSERR;
  43469. + goto cleanup;
  43470. + }
  43471. + }
  43472. + }
  43473. +#endif /* MSS_HACK */
  43474. +
  43475. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  43476. + if ((ixs->natt_type) && (ixs->outgoing_said.proto != IPPROTO_IPIP)) {
  43477. + /**
  43478. + * NAT-Traversal and Transport Mode:
  43479. + * we need to correct TCP/UDP checksum
  43480. + *
  43481. + * If we've got NAT-OA, we can fix checksum without recalculation.
  43482. + * If we don't we can zero udp checksum.
  43483. + */
  43484. + __u32 natt_oa = ixs->ipsp->ips_natt_oa ?
  43485. + ((struct sockaddr_in*)(ixs->ipsp->ips_natt_oa))->sin_addr.s_addr : 0;
  43486. + __u16 pkt_len = ixs->skb->tail - (unsigned char *)ixs->iph;
  43487. + __u16 data_len = pkt_len - (ixs->iph->ihl << 2);
  43488. + switch (ixs->iph->protocol) {
  43489. + case IPPROTO_TCP:
  43490. + if (data_len >= sizeof(struct tcphdr)) {
  43491. + struct tcphdr *tcp = (struct tcphdr *)((__u32 *)ixs->iph+ixs->iph->ihl);
  43492. + if (natt_oa) {
  43493. + __u32 buff[2] = { ~ixs->iph->daddr, natt_oa };
  43494. + KLIPS_PRINT(debug_tunnel,
  43495. + "klips_debug:ipsec_tunnel_start_xmit: "
  43496. + "NAT-T & TRANSPORT: "
  43497. + "fix TCP checksum using NAT-OA\n");
  43498. + tcp->check = csum_fold(
  43499. + csum_partial((unsigned char *)buff, sizeof(buff),
  43500. + tcp->check^0xffff));
  43501. + }
  43502. + else {
  43503. + KLIPS_PRINT(debug_tunnel,
  43504. + "klips_debug:ipsec_tunnel_start_xmit: "
  43505. + "NAT-T & TRANSPORT: do not recalc TCP checksum\n");
  43506. + }
  43507. + }
  43508. + else {
  43509. + KLIPS_PRINT(debug_tunnel,
  43510. + "klips_debug:ipsec_tunnel_start_xmit: "
  43511. + "NAT-T & TRANSPORT: can't fix TCP checksum\n");
  43512. + }
  43513. + break;
  43514. + case IPPROTO_UDP:
  43515. + if (data_len >= sizeof(struct udphdr)) {
  43516. + struct udphdr *udp = (struct udphdr *)((__u32 *)ixs->iph+ixs->iph->ihl);
  43517. + if (udp->check == 0) {
  43518. + KLIPS_PRINT(debug_tunnel,
  43519. + "klips_debug:ipsec_tunnel_start_xmit: "
  43520. + "NAT-T & TRANSPORT: UDP checksum already 0\n");
  43521. + }
  43522. + else if (natt_oa) {
  43523. + __u32 buff[2] = { ~ixs->iph->daddr, natt_oa };
  43524. + KLIPS_PRINT(debug_tunnel,
  43525. + "klips_debug:ipsec_tunnel_start_xmit: "
  43526. + "NAT-T & TRANSPORT: "
  43527. + "fix UDP checksum using NAT-OA\n");
  43528. + udp->check = csum_fold(
  43529. + csum_partial((unsigned char *)buff, sizeof(buff),
  43530. + udp->check^0xffff));
  43531. + }
  43532. + else {
  43533. + KLIPS_PRINT(debug_tunnel,
  43534. + "klips_debug:ipsec_tunnel_start_xmit: "
  43535. + "NAT-T & TRANSPORT: zero UDP checksum\n");
  43536. + udp->check = 0;
  43537. + }
  43538. + }
  43539. + else {
  43540. + KLIPS_PRINT(debug_tunnel,
  43541. + "klips_debug:ipsec_tunnel_start_xmit: "
  43542. + "NAT-T & TRANSPORT: can't fix UDP checksum\n");
  43543. + }
  43544. + break;
  43545. + default:
  43546. + KLIPS_PRINT(debug_tunnel,
  43547. + "klips_debug:ipsec_tunnel_start_xmit: "
  43548. + "NAT-T & TRANSPORT: non TCP/UDP packet -- do nothing\n");
  43549. + break;
  43550. + }
  43551. + }
  43552. +#endif /* CONFIG_IPSEC_NAT_TRAVERSAL */
  43553. +
  43554. + if(!ixs->hard_header_stripped && ixs->hard_header_len>0) {
  43555. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43556. + "klips_debug:ipsec_xmit_encap_bundle: "
  43557. + "allocating %d bytes for hardheader.\n",
  43558. + ixs->hard_header_len);
  43559. + if((ixs->saved_header = kmalloc(ixs->hard_header_len, GFP_ATOMIC)) == NULL) {
  43560. + printk(KERN_WARNING "klips_debug:ipsec_xmit_encap_bundle: "
  43561. + "Failed, tried to allocate %d bytes for temp hard_header.\n",
  43562. + ixs->hard_header_len);
  43563. + ixs->stats->tx_errors++;
  43564. + bundle_stat = IPSEC_XMIT_ERRMEMALLOC;
  43565. + goto cleanup;
  43566. + }
  43567. + {
  43568. + int i;
  43569. + for (i = 0; i < ixs->hard_header_len; i++) {
  43570. + ixs->saved_header[i] = ixs->skb->data[i];
  43571. + }
  43572. + }
  43573. + if(ixs->skb->len < ixs->hard_header_len) {
  43574. + printk(KERN_WARNING "klips_error:ipsec_xmit_encap_bundle: "
  43575. + "tried to skb_pull hhlen=%d, %d available. This should never happen, please report.\n",
  43576. + ixs->hard_header_len, (int)(ixs->skb->len));
  43577. + ixs->stats->tx_errors++;
  43578. + bundle_stat = IPSEC_XMIT_ESP_PUSHPULLERR;
  43579. + goto cleanup;
  43580. + }
  43581. + skb_pull(ixs->skb, ixs->hard_header_len);
  43582. + ixs->hard_header_stripped = 1;
  43583. +
  43584. +/* ixs->iph = (struct iphdr *) (ixs->skb->data); */
  43585. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43586. + "klips_debug:ipsec_xmit_encap_bundle: "
  43587. + "head,tailroom: %d,%d after hard_header stripped.\n",
  43588. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  43589. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_CROUT, ixs->iph);
  43590. + } else {
  43591. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43592. + "klips_debug:ipsec_xmit_encap_bundle: "
  43593. + "hard header already stripped.\n");
  43594. + }
  43595. +
  43596. + ixs->ll_headroom = (ixs->hard_header_len + 15) & ~15;
  43597. +
  43598. + if ((skb_headroom(ixs->skb) >= ixs->max_headroom + 2 * ixs->ll_headroom) &&
  43599. + (skb_tailroom(ixs->skb) >= ixs->max_tailroom)
  43600. +#ifndef NET_21
  43601. + && ixs->skb->free
  43602. +#endif /* !NET_21 */
  43603. + ) {
  43604. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43605. + "klips_debug:ipsec_xmit_encap_bundle: "
  43606. + "data fits in existing skb\n");
  43607. + } else {
  43608. + struct sk_buff* tskb;
  43609. +
  43610. + if(!ixs->oskb) {
  43611. + ixs->oskb = ixs->skb;
  43612. + }
  43613. +
  43614. + tskb = skb_copy_expand(ixs->skb,
  43615. + /* The need for 2 * link layer length here remains unexplained...RGB */
  43616. + ixs->max_headroom + 2 * ixs->ll_headroom,
  43617. + ixs->max_tailroom,
  43618. + GFP_ATOMIC);
  43619. +
  43620. + if(tskb && ixs->skb->sk) {
  43621. + skb_set_owner_w(tskb, ixs->skb->sk);
  43622. + }
  43623. +
  43624. + if(ixs->skb != ixs->oskb) {
  43625. + ipsec_kfree_skb(ixs->skb);
  43626. + }
  43627. + ixs->skb = tskb;
  43628. + if (!ixs->skb) {
  43629. + printk(KERN_WARNING
  43630. + "klips_debug:ipsec_xmit_encap_bundle: "
  43631. + "Failed, tried to allocate %d head and %d tailroom\n",
  43632. + ixs->max_headroom, ixs->max_tailroom);
  43633. + ixs->stats->tx_errors++;
  43634. + bundle_stat = IPSEC_XMIT_ERRSKBALLOC;
  43635. + goto cleanup;
  43636. + }
  43637. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43638. + "klips_debug:ipsec_xmit_encap_bundle: "
  43639. + "head,tailroom: %d,%d after allocation\n",
  43640. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  43641. + }
  43642. +#ifdef CONFIG_KLIPS_DEBUG
  43643. + if(debug_tunnel & DB_TN_ENCAP) {
  43644. + ipsec_print_ip(ixs->iph);
  43645. + }
  43646. +#endif
  43647. +
  43648. + /*
  43649. + * Apply grouped transforms to packet
  43650. + */
  43651. + while (ixs->ipsp) {
  43652. + enum ipsec_xmit_value encap_stat = IPSEC_XMIT_OK;
  43653. +
  43654. + encap_stat = ipsec_xmit_encap_once(ixs);
  43655. +#ifdef CONFIG_KLIPS_DEBUG
  43656. + if(debug_tunnel & DB_TN_ENCAP) {
  43657. + ipsec_print_ip(ixs->iph);
  43658. + }
  43659. +#endif
  43660. +
  43661. + if(encap_stat != IPSEC_XMIT_OK) {
  43662. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43663. + "klips_debug:ipsec_xmit_encap_bundle: encap_once failed: %d\n",
  43664. + encap_stat);
  43665. +
  43666. + bundle_stat = IPSEC_XMIT_ENCAPFAIL;
  43667. + goto cleanup;
  43668. + }
  43669. + }
  43670. +
  43671. + /* we are done with this SA */
  43672. + ipsec_sa_put(ixs->ipsp);
  43673. +
  43674. + /* end encapsulation loop here XXX */
  43675. + cleanup:
  43676. + spin_unlock(&tdb_lock);
  43677. + return bundle_stat;
  43678. +}
  43679. +
  43680. +/*
  43681. + * $Log: ipsec_xmit.c,v $
  43682. + * Revision 1.20.2.8 2006/10/06 21:39:26 paul
  43683. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  43684. + * set. This is defined through autoconf.h which is included through the
  43685. + * linux kernel build macros.
  43686. + *
  43687. + * Revision 1.20.2.7 2006/08/24 03:02:01 paul
  43688. + * Compile fixes for when CONFIG_KLIPS_DEBUG is not set. (bug #642)
  43689. + *
  43690. + * Revision 1.20.2.6 2006/07/07 22:09:49 paul
  43691. + * From: Bart Trojanowski <bart@xelerance.com>
  43692. + * Removing a left over '#else' that split another '#if/#endif' block in two.
  43693. + *
  43694. + * Revision 1.20.2.5 2006/07/07 15:43:17 paul
  43695. + * From: Bart Trojanowski <bart@xelerance.com>
  43696. + * improved protocol detection in ipsec_print_ip() -- a debug aid.
  43697. + *
  43698. + * Revision 1.20.2.4 2006/04/20 16:33:07 mcr
  43699. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  43700. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  43701. + *
  43702. + * Revision 1.20.2.3 2005/11/29 21:52:57 ken
  43703. + * Fix for #518 MTU issues
  43704. + *
  43705. + * Revision 1.20.2.2 2005/11/27 21:41:03 paul
  43706. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"in for klips as module.
  43707. + *
  43708. + * Revision 1.20.2.1 2005/08/27 23:40:00 paul
  43709. + * recommited HAVE_SOCK_SECURITY fixes for linux 2.6.13
  43710. + *
  43711. + * Revision 1.20 2005/07/12 15:39:27 paul
  43712. + * include asm/uaccess.h for VERIFY_WRITE
  43713. + *
  43714. + * Revision 1.19 2005/05/24 01:02:35 mcr
  43715. + * some refactoring/simplification of situation where alg
  43716. + * is not found.
  43717. + *
  43718. + * Revision 1.18 2005/05/23 23:52:33 mcr
  43719. + * adjust comments, add additional debugging.
  43720. + *
  43721. + * Revision 1.17 2005/05/23 22:57:23 mcr
  43722. + * removed explicit 3DES support.
  43723. + *
  43724. + * Revision 1.16 2005/05/21 03:29:15 mcr
  43725. + * fixed warning about unused zeroes if AH is off.
  43726. + *
  43727. + * Revision 1.15 2005/05/20 16:47:59 mcr
  43728. + * include asm/checksum.h to get ip_fast_csum macro.
  43729. + *
  43730. + * Revision 1.14 2005/05/11 01:43:03 mcr
  43731. + * removed "poor-man"s OOP in favour of proper C structures.
  43732. + *
  43733. + * Revision 1.13 2005/04/29 05:10:22 mcr
  43734. + * removed from extraenous includes to make unit testing easier.
  43735. + *
  43736. + * Revision 1.12 2005/04/15 01:28:34 mcr
  43737. + * use ipsec_dmp_block.
  43738. + *
  43739. + * Revision 1.11 2005/01/26 00:50:35 mcr
  43740. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  43741. + * and make sure that NAT_TRAVERSAL is set as well to match
  43742. + * userspace compiles of code.
  43743. + *
  43744. + * Revision 1.10 2004/09/13 17:55:21 ken
  43745. + * MD5* -> osMD5*
  43746. + *
  43747. + * Revision 1.9 2004/07/10 19:11:18 mcr
  43748. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  43749. + *
  43750. + * Revision 1.8 2004/04/06 02:49:26 mcr
  43751. + * pullup of algo code from alg-branch.
  43752. + *
  43753. + * Revision 1.7 2004/02/03 03:13:41 mcr
  43754. + * mark invalid encapsulation states.
  43755. + *
  43756. + * Revision 1.6.2.1 2003/12/22 15:25:52 jjo
  43757. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  43758. + *
  43759. + * Revision 1.6 2003/12/10 01:14:27 mcr
  43760. + * NAT-traversal patches to KLIPS.
  43761. + *
  43762. + * Revision 1.5 2003/10/31 02:27:55 mcr
  43763. + * pulled up port-selector patches and sa_id elimination.
  43764. + *
  43765. + * Revision 1.4.4.2 2003/10/29 01:37:39 mcr
  43766. + * when creating %hold from %trap, only make the %hold as
  43767. + * specific as the %trap was - so if the protocol and ports
  43768. + * were wildcards, then the %hold will be too.
  43769. + *
  43770. + * Revision 1.4.4.1 2003/09/21 13:59:56 mcr
  43771. + * pre-liminary X.509 patch - does not yet pass tests.
  43772. + *
  43773. + * Revision 1.4 2003/06/20 02:28:10 mcr
  43774. + * misstype of variable name, not detected by module build.
  43775. + *
  43776. + * Revision 1.3 2003/06/20 01:42:21 mcr
  43777. + * added counters to measure how many ACQUIREs we send to pluto,
  43778. + * and how many are successfully sent.
  43779. + *
  43780. + * Revision 1.2 2003/04/03 17:38:35 rgb
  43781. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  43782. + * Normalised coding style.
  43783. + * Simplified logic and reduced duplication of code.
  43784. + *
  43785. + * Revision 1.1 2003/02/12 19:31:23 rgb
  43786. + * Refactored from ipsec_tunnel.c
  43787. + *
  43788. + * Local Variables:
  43789. + * c-file-style: "linux"
  43790. + * End:
  43791. + *
  43792. + */
  43793. --- /dev/null Tue Mar 11 13:02:56 2003
  43794. +++ linux/net/ipsec/match586.S Mon Feb 9 13:51:03 2004
  43795. @@ -0,0 +1,357 @@
  43796. +/* match.s -- Pentium-optimized version of longest_match()
  43797. + * Written for zlib 1.1.2
  43798. + * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
  43799. + *
  43800. + * This is free software; you can redistribute it and/or modify it
  43801. + * under the terms of the GNU General Public License.
  43802. + */
  43803. +
  43804. +#ifndef NO_UNDERLINE
  43805. +#define match_init _ipcomp_match_init
  43806. +#define longest_match _ipcomp_longest_match
  43807. +#else
  43808. +#define match_init ipcomp_match_init
  43809. +#define longest_match ipcomp_longest_match
  43810. +#endif
  43811. +
  43812. +#define MAX_MATCH (258)
  43813. +#define MIN_MATCH (3)
  43814. +#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  43815. +#define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  43816. +
  43817. +/* stack frame offsets */
  43818. +
  43819. +#define wmask 0 /* local copy of s->wmask */
  43820. +#define window 4 /* local copy of s->window */
  43821. +#define windowbestlen 8 /* s->window + bestlen */
  43822. +#define chainlenscanend 12 /* high word: current chain len */
  43823. + /* low word: last bytes sought */
  43824. +#define scanstart 16 /* first two bytes of string */
  43825. +#define scanalign 20 /* dword-misalignment of string */
  43826. +#define nicematch 24 /* a good enough match size */
  43827. +#define bestlen 28 /* size of best match so far */
  43828. +#define scan 32 /* ptr to string wanting match */
  43829. +
  43830. +#define LocalVarsSize (36)
  43831. +/* saved ebx 36 */
  43832. +/* saved edi 40 */
  43833. +/* saved esi 44 */
  43834. +/* saved ebp 48 */
  43835. +/* return address 52 */
  43836. +#define deflatestate 56 /* the function arguments */
  43837. +#define curmatch 60
  43838. +
  43839. +/* Offsets for fields in the deflate_state structure. These numbers
  43840. + * are calculated from the definition of deflate_state, with the
  43841. + * assumption that the compiler will dword-align the fields. (Thus,
  43842. + * changing the definition of deflate_state could easily cause this
  43843. + * program to crash horribly, without so much as a warning at
  43844. + * compile time. Sigh.)
  43845. + */
  43846. +#define dsWSize 36
  43847. +#define dsWMask 44
  43848. +#define dsWindow 48
  43849. +#define dsPrev 56
  43850. +#define dsMatchLen 88
  43851. +#define dsPrevMatch 92
  43852. +#define dsStrStart 100
  43853. +#define dsMatchStart 104
  43854. +#define dsLookahead 108
  43855. +#define dsPrevLen 112
  43856. +#define dsMaxChainLen 116
  43857. +#define dsGoodMatch 132
  43858. +#define dsNiceMatch 136
  43859. +
  43860. +
  43861. +.file "match.S"
  43862. +
  43863. +.globl match_init, longest_match
  43864. +
  43865. +.text
  43866. +
  43867. +/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  43868. +
  43869. +longest_match:
  43870. +
  43871. +/* Save registers that the compiler may be using, and adjust %esp to */
  43872. +/* make room for our stack frame. */
  43873. +
  43874. + pushl %ebp
  43875. + pushl %edi
  43876. + pushl %esi
  43877. + pushl %ebx
  43878. + subl $LocalVarsSize, %esp
  43879. +
  43880. +/* Retrieve the function arguments. %ecx will hold cur_match */
  43881. +/* throughout the entire function. %edx will hold the pointer to the */
  43882. +/* deflate_state structure during the function's setup (before */
  43883. +/* entering the main loop). */
  43884. +
  43885. + movl deflatestate(%esp), %edx
  43886. + movl curmatch(%esp), %ecx
  43887. +
  43888. +/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  43889. +
  43890. + movl dsNiceMatch(%edx), %eax
  43891. + movl dsLookahead(%edx), %ebx
  43892. + cmpl %eax, %ebx
  43893. + jl LookaheadLess
  43894. + movl %eax, %ebx
  43895. +LookaheadLess: movl %ebx, nicematch(%esp)
  43896. +
  43897. +/* register Bytef *scan = s->window + s->strstart; */
  43898. +
  43899. + movl dsWindow(%edx), %esi
  43900. + movl %esi, window(%esp)
  43901. + movl dsStrStart(%edx), %ebp
  43902. + lea (%esi,%ebp), %edi
  43903. + movl %edi, scan(%esp)
  43904. +
  43905. +/* Determine how many bytes the scan ptr is off from being */
  43906. +/* dword-aligned. */
  43907. +
  43908. + movl %edi, %eax
  43909. + negl %eax
  43910. + andl $3, %eax
  43911. + movl %eax, scanalign(%esp)
  43912. +
  43913. +/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  43914. +/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  43915. +
  43916. + movl dsWSize(%edx), %eax
  43917. + subl $MIN_LOOKAHEAD, %eax
  43918. + subl %eax, %ebp
  43919. + jg LimitPositive
  43920. + xorl %ebp, %ebp
  43921. +LimitPositive:
  43922. +
  43923. +/* unsigned chain_length = s->max_chain_length; */
  43924. +/* if (s->prev_length >= s->good_match) { */
  43925. +/* chain_length >>= 2; */
  43926. +/* } */
  43927. +
  43928. + movl dsPrevLen(%edx), %eax
  43929. + movl dsGoodMatch(%edx), %ebx
  43930. + cmpl %ebx, %eax
  43931. + movl dsMaxChainLen(%edx), %ebx
  43932. + jl LastMatchGood
  43933. + shrl $2, %ebx
  43934. +LastMatchGood:
  43935. +
  43936. +/* chainlen is decremented once beforehand so that the function can */
  43937. +/* use the sign flag instead of the zero flag for the exit test. */
  43938. +/* It is then shifted into the high word, to make room for the scanend */
  43939. +/* scanend value, which it will always accompany. */
  43940. +
  43941. + decl %ebx
  43942. + shll $16, %ebx
  43943. +
  43944. +/* int best_len = s->prev_length; */
  43945. +
  43946. + movl dsPrevLen(%edx), %eax
  43947. + movl %eax, bestlen(%esp)
  43948. +
  43949. +/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  43950. +
  43951. + addl %eax, %esi
  43952. + movl %esi, windowbestlen(%esp)
  43953. +
  43954. +/* register ush scan_start = *(ushf*)scan; */
  43955. +/* register ush scan_end = *(ushf*)(scan+best_len-1); */
  43956. +
  43957. + movw (%edi), %bx
  43958. + movw %bx, scanstart(%esp)
  43959. + movw -1(%edi,%eax), %bx
  43960. + movl %ebx, chainlenscanend(%esp)
  43961. +
  43962. +/* Posf *prev = s->prev; */
  43963. +/* uInt wmask = s->w_mask; */
  43964. +
  43965. + movl dsPrev(%edx), %edi
  43966. + movl dsWMask(%edx), %edx
  43967. + mov %edx, wmask(%esp)
  43968. +
  43969. +/* Jump into the main loop. */
  43970. +
  43971. + jmp LoopEntry
  43972. +
  43973. +.balign 16
  43974. +
  43975. +/* do {
  43976. + * match = s->window + cur_match;
  43977. + * if (*(ushf*)(match+best_len-1) != scan_end ||
  43978. + * *(ushf*)match != scan_start) continue;
  43979. + * [...]
  43980. + * } while ((cur_match = prev[cur_match & wmask]) > limit
  43981. + * && --chain_length != 0);
  43982. + *
  43983. + * Here is the inner loop of the function. The function will spend the
  43984. + * majority of its time in this loop, and majority of that time will
  43985. + * be spent in the first ten instructions.
  43986. + *
  43987. + * Within this loop:
  43988. + * %ebx = chainlenscanend - i.e., ((chainlen << 16) | scanend)
  43989. + * %ecx = curmatch
  43990. + * %edx = curmatch & wmask
  43991. + * %esi = windowbestlen - i.e., (window + bestlen)
  43992. + * %edi = prev
  43993. + * %ebp = limit
  43994. + *
  43995. + * Two optimization notes on the choice of instructions:
  43996. + *
  43997. + * The first instruction uses a 16-bit address, which costs an extra,
  43998. + * unpairable cycle. This is cheaper than doing a 32-bit access and
  43999. + * zeroing the high word, due to the 3-cycle misalignment penalty which
  44000. + * would occur half the time. This also turns out to be cheaper than
  44001. + * doing two separate 8-bit accesses, as the memory is so rarely in the
  44002. + * L1 cache.
  44003. + *
  44004. + * The window buffer, however, apparently spends a lot of time in the
  44005. + * cache, and so it is faster to retrieve the word at the end of the
  44006. + * match string with two 8-bit loads. The instructions that test the
  44007. + * word at the beginning of the match string, however, are executed
  44008. + * much less frequently, and there it was cheaper to use 16-bit
  44009. + * instructions, which avoided the necessity of saving off and
  44010. + * subsequently reloading one of the other registers.
  44011. + */
  44012. +LookupLoop:
  44013. + /* 1 U & V */
  44014. + movw (%edi,%edx,2), %cx /* 2 U pipe */
  44015. + movl wmask(%esp), %edx /* 2 V pipe */
  44016. + cmpl %ebp, %ecx /* 3 U pipe */
  44017. + jbe LeaveNow /* 3 V pipe */
  44018. + subl $0x00010000, %ebx /* 4 U pipe */
  44019. + js LeaveNow /* 4 V pipe */
  44020. +LoopEntry: movb -1(%esi,%ecx), %al /* 5 U pipe */
  44021. + andl %ecx, %edx /* 5 V pipe */
  44022. + cmpb %bl, %al /* 6 U pipe */
  44023. + jnz LookupLoop /* 6 V pipe */
  44024. + movb (%esi,%ecx), %ah
  44025. + cmpb %bh, %ah
  44026. + jnz LookupLoop
  44027. + movl window(%esp), %eax
  44028. + movw (%eax,%ecx), %ax
  44029. + cmpw scanstart(%esp), %ax
  44030. + jnz LookupLoop
  44031. +
  44032. +/* Store the current value of chainlen. */
  44033. +
  44034. + movl %ebx, chainlenscanend(%esp)
  44035. +
  44036. +/* Point %edi to the string under scrutiny, and %esi to the string we */
  44037. +/* are hoping to match it up with. In actuality, %esi and %edi are */
  44038. +/* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  44039. +/* initialized to -(MAX_MATCH_8 - scanalign). */
  44040. +
  44041. + movl window(%esp), %esi
  44042. + movl scan(%esp), %edi
  44043. + addl %ecx, %esi
  44044. + movl scanalign(%esp), %eax
  44045. + movl $(-MAX_MATCH_8), %edx
  44046. + lea MAX_MATCH_8(%edi,%eax), %edi
  44047. + lea MAX_MATCH_8(%esi,%eax), %esi
  44048. +
  44049. +/* Test the strings for equality, 8 bytes at a time. At the end,
  44050. + * adjust %edx so that it is offset to the exact byte that mismatched.
  44051. + *
  44052. + * We already know at this point that the first three bytes of the
  44053. + * strings match each other, and they can be safely passed over before
  44054. + * starting the compare loop. So what this code does is skip over 0-3
  44055. + * bytes, as much as necessary in order to dword-align the %edi
  44056. + * pointer. (%esi will still be misaligned three times out of four.)
  44057. + *
  44058. + * It should be confessed that this loop usually does not represent
  44059. + * much of the total running time. Replacing it with a more
  44060. + * straightforward "rep cmpsb" would not drastically degrade
  44061. + * performance.
  44062. + */
  44063. +LoopCmps:
  44064. + movl (%esi,%edx), %eax
  44065. + movl (%edi,%edx), %ebx
  44066. + xorl %ebx, %eax
  44067. + jnz LeaveLoopCmps
  44068. + movl 4(%esi,%edx), %eax
  44069. + movl 4(%edi,%edx), %ebx
  44070. + xorl %ebx, %eax
  44071. + jnz LeaveLoopCmps4
  44072. + addl $8, %edx
  44073. + jnz LoopCmps
  44074. + jmp LenMaximum
  44075. +LeaveLoopCmps4: addl $4, %edx
  44076. +LeaveLoopCmps: testl $0x0000FFFF, %eax
  44077. + jnz LenLower
  44078. + addl $2, %edx
  44079. + shrl $16, %eax
  44080. +LenLower: subb $1, %al
  44081. + adcl $0, %edx
  44082. +
  44083. +/* Calculate the length of the match. If it is longer than MAX_MATCH, */
  44084. +/* then automatically accept it as the best possible match and leave. */
  44085. +
  44086. + lea (%edi,%edx), %eax
  44087. + movl scan(%esp), %edi
  44088. + subl %edi, %eax
  44089. + cmpl $MAX_MATCH, %eax
  44090. + jge LenMaximum
  44091. +
  44092. +/* If the length of the match is not longer than the best match we */
  44093. +/* have so far, then forget it and return to the lookup loop. */
  44094. +
  44095. + movl deflatestate(%esp), %edx
  44096. + movl bestlen(%esp), %ebx
  44097. + cmpl %ebx, %eax
  44098. + jg LongerMatch
  44099. + movl chainlenscanend(%esp), %ebx
  44100. + movl windowbestlen(%esp), %esi
  44101. + movl dsPrev(%edx), %edi
  44102. + movl wmask(%esp), %edx
  44103. + andl %ecx, %edx
  44104. + jmp LookupLoop
  44105. +
  44106. +/* s->match_start = cur_match; */
  44107. +/* best_len = len; */
  44108. +/* if (len >= nice_match) break; */
  44109. +/* scan_end = *(ushf*)(scan+best_len-1); */
  44110. +
  44111. +LongerMatch: movl nicematch(%esp), %ebx
  44112. + movl %eax, bestlen(%esp)
  44113. + movl %ecx, dsMatchStart(%edx)
  44114. + cmpl %ebx, %eax
  44115. + jge LeaveNow
  44116. + movl window(%esp), %esi
  44117. + addl %eax, %esi
  44118. + movl %esi, windowbestlen(%esp)
  44119. + movl chainlenscanend(%esp), %ebx
  44120. + movw -1(%edi,%eax), %bx
  44121. + movl dsPrev(%edx), %edi
  44122. + movl %ebx, chainlenscanend(%esp)
  44123. + movl wmask(%esp), %edx
  44124. + andl %ecx, %edx
  44125. + jmp LookupLoop
  44126. +
  44127. +/* Accept the current string, with the maximum possible length. */
  44128. +
  44129. +LenMaximum: movl deflatestate(%esp), %edx
  44130. + movl $MAX_MATCH, bestlen(%esp)
  44131. + movl %ecx, dsMatchStart(%edx)
  44132. +
  44133. +/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  44134. +/* return s->lookahead; */
  44135. +
  44136. +LeaveNow:
  44137. + movl deflatestate(%esp), %edx
  44138. + movl bestlen(%esp), %ebx
  44139. + movl dsLookahead(%edx), %eax
  44140. + cmpl %eax, %ebx
  44141. + jg LookaheadRet
  44142. + movl %ebx, %eax
  44143. +LookaheadRet:
  44144. +
  44145. +/* Restore the stack and return from whence we came. */
  44146. +
  44147. + addl $LocalVarsSize, %esp
  44148. + popl %ebx
  44149. + popl %esi
  44150. + popl %edi
  44151. + popl %ebp
  44152. +match_init: ret
  44153. --- /dev/null Tue Mar 11 13:02:56 2003
  44154. +++ linux/net/ipsec/match686.S Mon Feb 9 13:51:03 2004
  44155. @@ -0,0 +1,330 @@
  44156. +/* match.s -- Pentium-Pro-optimized version of longest_match()
  44157. + * Written for zlib 1.1.2
  44158. + * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
  44159. + *
  44160. + * This is free software; you can redistribute it and/or modify it
  44161. + * under the terms of the GNU General Public License.
  44162. + */
  44163. +
  44164. +#ifndef NO_UNDERLINE
  44165. +#define match_init _ipcomp_match_init
  44166. +#define longest_match _ipcomp_longest_match
  44167. +#else
  44168. +#define match_init ipcomp_match_init
  44169. +#define longest_match ipcomp_longest_match
  44170. +#endif
  44171. +
  44172. +#define MAX_MATCH (258)
  44173. +#define MIN_MATCH (3)
  44174. +#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  44175. +#define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  44176. +
  44177. +/* stack frame offsets */
  44178. +
  44179. +#define chainlenwmask 0 /* high word: current chain len */
  44180. + /* low word: s->wmask */
  44181. +#define window 4 /* local copy of s->window */
  44182. +#define windowbestlen 8 /* s->window + bestlen */
  44183. +#define scanstart 16 /* first two bytes of string */
  44184. +#define scanend 12 /* last two bytes of string */
  44185. +#define scanalign 20 /* dword-misalignment of string */
  44186. +#define nicematch 24 /* a good enough match size */
  44187. +#define bestlen 28 /* size of best match so far */
  44188. +#define scan 32 /* ptr to string wanting match */
  44189. +
  44190. +#define LocalVarsSize (36)
  44191. +/* saved ebx 36 */
  44192. +/* saved edi 40 */
  44193. +/* saved esi 44 */
  44194. +/* saved ebp 48 */
  44195. +/* return address 52 */
  44196. +#define deflatestate 56 /* the function arguments */
  44197. +#define curmatch 60
  44198. +
  44199. +/* Offsets for fields in the deflate_state structure. These numbers
  44200. + * are calculated from the definition of deflate_state, with the
  44201. + * assumption that the compiler will dword-align the fields. (Thus,
  44202. + * changing the definition of deflate_state could easily cause this
  44203. + * program to crash horribly, without so much as a warning at
  44204. + * compile time. Sigh.)
  44205. + */
  44206. +#define dsWSize 36
  44207. +#define dsWMask 44
  44208. +#define dsWindow 48
  44209. +#define dsPrev 56
  44210. +#define dsMatchLen 88
  44211. +#define dsPrevMatch 92
  44212. +#define dsStrStart 100
  44213. +#define dsMatchStart 104
  44214. +#define dsLookahead 108
  44215. +#define dsPrevLen 112
  44216. +#define dsMaxChainLen 116
  44217. +#define dsGoodMatch 132
  44218. +#define dsNiceMatch 136
  44219. +
  44220. +
  44221. +.file "match.S"
  44222. +
  44223. +.globl match_init, longest_match
  44224. +
  44225. +.text
  44226. +
  44227. +/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  44228. +
  44229. +longest_match:
  44230. +
  44231. +/* Save registers that the compiler may be using, and adjust %esp to */
  44232. +/* make room for our stack frame. */
  44233. +
  44234. + pushl %ebp
  44235. + pushl %edi
  44236. + pushl %esi
  44237. + pushl %ebx
  44238. + subl $LocalVarsSize, %esp
  44239. +
  44240. +/* Retrieve the function arguments. %ecx will hold cur_match */
  44241. +/* throughout the entire function. %edx will hold the pointer to the */
  44242. +/* deflate_state structure during the function's setup (before */
  44243. +/* entering the main loop). */
  44244. +
  44245. + movl deflatestate(%esp), %edx
  44246. + movl curmatch(%esp), %ecx
  44247. +
  44248. +/* uInt wmask = s->w_mask; */
  44249. +/* unsigned chain_length = s->max_chain_length; */
  44250. +/* if (s->prev_length >= s->good_match) { */
  44251. +/* chain_length >>= 2; */
  44252. +/* } */
  44253. +
  44254. + movl dsPrevLen(%edx), %eax
  44255. + movl dsGoodMatch(%edx), %ebx
  44256. + cmpl %ebx, %eax
  44257. + movl dsWMask(%edx), %eax
  44258. + movl dsMaxChainLen(%edx), %ebx
  44259. + jl LastMatchGood
  44260. + shrl $2, %ebx
  44261. +LastMatchGood:
  44262. +
  44263. +/* chainlen is decremented once beforehand so that the function can */
  44264. +/* use the sign flag instead of the zero flag for the exit test. */
  44265. +/* It is then shifted into the high word, to make room for the wmask */
  44266. +/* value, which it will always accompany. */
  44267. +
  44268. + decl %ebx
  44269. + shll $16, %ebx
  44270. + orl %eax, %ebx
  44271. + movl %ebx, chainlenwmask(%esp)
  44272. +
  44273. +/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  44274. +
  44275. + movl dsNiceMatch(%edx), %eax
  44276. + movl dsLookahead(%edx), %ebx
  44277. + cmpl %eax, %ebx
  44278. + jl LookaheadLess
  44279. + movl %eax, %ebx
  44280. +LookaheadLess: movl %ebx, nicematch(%esp)
  44281. +
  44282. +/* register Bytef *scan = s->window + s->strstart; */
  44283. +
  44284. + movl dsWindow(%edx), %esi
  44285. + movl %esi, window(%esp)
  44286. + movl dsStrStart(%edx), %ebp
  44287. + lea (%esi,%ebp), %edi
  44288. + movl %edi, scan(%esp)
  44289. +
  44290. +/* Determine how many bytes the scan ptr is off from being */
  44291. +/* dword-aligned. */
  44292. +
  44293. + movl %edi, %eax
  44294. + negl %eax
  44295. + andl $3, %eax
  44296. + movl %eax, scanalign(%esp)
  44297. +
  44298. +/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  44299. +/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  44300. +
  44301. + movl dsWSize(%edx), %eax
  44302. + subl $MIN_LOOKAHEAD, %eax
  44303. + subl %eax, %ebp
  44304. + jg LimitPositive
  44305. + xorl %ebp, %ebp
  44306. +LimitPositive:
  44307. +
  44308. +/* int best_len = s->prev_length; */
  44309. +
  44310. + movl dsPrevLen(%edx), %eax
  44311. + movl %eax, bestlen(%esp)
  44312. +
  44313. +/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  44314. +
  44315. + addl %eax, %esi
  44316. + movl %esi, windowbestlen(%esp)
  44317. +
  44318. +/* register ush scan_start = *(ushf*)scan; */
  44319. +/* register ush scan_end = *(ushf*)(scan+best_len-1); */
  44320. +/* Posf *prev = s->prev; */
  44321. +
  44322. + movzwl (%edi), %ebx
  44323. + movl %ebx, scanstart(%esp)
  44324. + movzwl -1(%edi,%eax), %ebx
  44325. + movl %ebx, scanend(%esp)
  44326. + movl dsPrev(%edx), %edi
  44327. +
  44328. +/* Jump into the main loop. */
  44329. +
  44330. + movl chainlenwmask(%esp), %edx
  44331. + jmp LoopEntry
  44332. +
  44333. +.balign 16
  44334. +
  44335. +/* do {
  44336. + * match = s->window + cur_match;
  44337. + * if (*(ushf*)(match+best_len-1) != scan_end ||
  44338. + * *(ushf*)match != scan_start) continue;
  44339. + * [...]
  44340. + * } while ((cur_match = prev[cur_match & wmask]) > limit
  44341. + * && --chain_length != 0);
  44342. + *
  44343. + * Here is the inner loop of the function. The function will spend the
  44344. + * majority of its time in this loop, and majority of that time will
  44345. + * be spent in the first ten instructions.
  44346. + *
  44347. + * Within this loop:
  44348. + * %ebx = scanend
  44349. + * %ecx = curmatch
  44350. + * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
  44351. + * %esi = windowbestlen - i.e., (window + bestlen)
  44352. + * %edi = prev
  44353. + * %ebp = limit
  44354. + */
  44355. +LookupLoop:
  44356. + andl %edx, %ecx
  44357. + movzwl (%edi,%ecx,2), %ecx
  44358. + cmpl %ebp, %ecx
  44359. + jbe LeaveNow
  44360. + subl $0x00010000, %edx
  44361. + js LeaveNow
  44362. +LoopEntry: movzwl -1(%esi,%ecx), %eax
  44363. + cmpl %ebx, %eax
  44364. + jnz LookupLoop
  44365. + movl window(%esp), %eax
  44366. + movzwl (%eax,%ecx), %eax
  44367. + cmpl scanstart(%esp), %eax
  44368. + jnz LookupLoop
  44369. +
  44370. +/* Store the current value of chainlen. */
  44371. +
  44372. + movl %edx, chainlenwmask(%esp)
  44373. +
  44374. +/* Point %edi to the string under scrutiny, and %esi to the string we */
  44375. +/* are hoping to match it up with. In actuality, %esi and %edi are */
  44376. +/* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  44377. +/* initialized to -(MAX_MATCH_8 - scanalign). */
  44378. +
  44379. + movl window(%esp), %esi
  44380. + movl scan(%esp), %edi
  44381. + addl %ecx, %esi
  44382. + movl scanalign(%esp), %eax
  44383. + movl $(-MAX_MATCH_8), %edx
  44384. + lea MAX_MATCH_8(%edi,%eax), %edi
  44385. + lea MAX_MATCH_8(%esi,%eax), %esi
  44386. +
  44387. +/* Test the strings for equality, 8 bytes at a time. At the end,
  44388. + * adjust %edx so that it is offset to the exact byte that mismatched.
  44389. + *
  44390. + * We already know at this point that the first three bytes of the
  44391. + * strings match each other, and they can be safely passed over before
  44392. + * starting the compare loop. So what this code does is skip over 0-3
  44393. + * bytes, as much as necessary in order to dword-align the %edi
  44394. + * pointer. (%esi will still be misaligned three times out of four.)
  44395. + *
  44396. + * It should be confessed that this loop usually does not represent
  44397. + * much of the total running time. Replacing it with a more
  44398. + * straightforward "rep cmpsb" would not drastically degrade
  44399. + * performance.
  44400. + */
  44401. +LoopCmps:
  44402. + movl (%esi,%edx), %eax
  44403. + xorl (%edi,%edx), %eax
  44404. + jnz LeaveLoopCmps
  44405. + movl 4(%esi,%edx), %eax
  44406. + xorl 4(%edi,%edx), %eax
  44407. + jnz LeaveLoopCmps4
  44408. + addl $8, %edx
  44409. + jnz LoopCmps
  44410. + jmp LenMaximum
  44411. +LeaveLoopCmps4: addl $4, %edx
  44412. +LeaveLoopCmps: testl $0x0000FFFF, %eax
  44413. + jnz LenLower
  44414. + addl $2, %edx
  44415. + shrl $16, %eax
  44416. +LenLower: subb $1, %al
  44417. + adcl $0, %edx
  44418. +
  44419. +/* Calculate the length of the match. If it is longer than MAX_MATCH, */
  44420. +/* then automatically accept it as the best possible match and leave. */
  44421. +
  44422. + lea (%edi,%edx), %eax
  44423. + movl scan(%esp), %edi
  44424. + subl %edi, %eax
  44425. + cmpl $MAX_MATCH, %eax
  44426. + jge LenMaximum
  44427. +
  44428. +/* If the length of the match is not longer than the best match we */
  44429. +/* have so far, then forget it and return to the lookup loop. */
  44430. +
  44431. + movl deflatestate(%esp), %edx
  44432. + movl bestlen(%esp), %ebx
  44433. + cmpl %ebx, %eax
  44434. + jg LongerMatch
  44435. + movl windowbestlen(%esp), %esi
  44436. + movl dsPrev(%edx), %edi
  44437. + movl scanend(%esp), %ebx
  44438. + movl chainlenwmask(%esp), %edx
  44439. + jmp LookupLoop
  44440. +
  44441. +/* s->match_start = cur_match; */
  44442. +/* best_len = len; */
  44443. +/* if (len >= nice_match) break; */
  44444. +/* scan_end = *(ushf*)(scan+best_len-1); */
  44445. +
  44446. +LongerMatch: movl nicematch(%esp), %ebx
  44447. + movl %eax, bestlen(%esp)
  44448. + movl %ecx, dsMatchStart(%edx)
  44449. + cmpl %ebx, %eax
  44450. + jge LeaveNow
  44451. + movl window(%esp), %esi
  44452. + addl %eax, %esi
  44453. + movl %esi, windowbestlen(%esp)
  44454. + movzwl -1(%edi,%eax), %ebx
  44455. + movl dsPrev(%edx), %edi
  44456. + movl %ebx, scanend(%esp)
  44457. + movl chainlenwmask(%esp), %edx
  44458. + jmp LookupLoop
  44459. +
  44460. +/* Accept the current string, with the maximum possible length. */
  44461. +
  44462. +LenMaximum: movl deflatestate(%esp), %edx
  44463. + movl $MAX_MATCH, bestlen(%esp)
  44464. + movl %ecx, dsMatchStart(%edx)
  44465. +
  44466. +/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  44467. +/* return s->lookahead; */
  44468. +
  44469. +LeaveNow:
  44470. + movl deflatestate(%esp), %edx
  44471. + movl bestlen(%esp), %ebx
  44472. + movl dsLookahead(%edx), %eax
  44473. + cmpl %eax, %ebx
  44474. + jg LookaheadRet
  44475. + movl %ebx, %eax
  44476. +LookaheadRet:
  44477. +
  44478. +/* Restore the stack and return from whence we came. */
  44479. +
  44480. + addl $LocalVarsSize, %esp
  44481. + popl %ebx
  44482. + popl %esi
  44483. + popl %edi
  44484. + popl %ebp
  44485. +match_init: ret
  44486. --- /dev/null Tue Mar 11 13:02:56 2003
  44487. +++ linux/net/ipsec/null/ipsec_alg_null.c Mon Feb 9 13:51:03 2004
  44488. @@ -0,0 +1,145 @@
  44489. +/*
  44490. + * ipsec_alg NULL cipher stubs
  44491. + *
  44492. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  44493. + *
  44494. + * $Id: ipsec_alg_null.c,v 1.1.2.1 2006/10/11 18:14:33 paul Exp $
  44495. + *
  44496. + * This program is free software; you can redistribute it and/or modify it
  44497. + * under the terms of the GNU General Public License as published by the
  44498. + * Free Software Foundation; either version 2 of the License, or (at your
  44499. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  44500. + *
  44501. + * This program is distributed in the hope that it will be useful, but
  44502. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  44503. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  44504. + * for more details.
  44505. + *
  44506. + */
  44507. +#include <linux/config.h>
  44508. +#include <linux/version.h>
  44509. +
  44510. +/*
  44511. + * special case: ipsec core modular with this static algo inside:
  44512. + * must avoid MODULE magic for this file
  44513. + */
  44514. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_NULL)
  44515. +#undef MODULE
  44516. +#endif
  44517. +
  44518. +#include <linux/module.h>
  44519. +#include <linux/init.h>
  44520. +
  44521. +#include <linux/kernel.h> /* printk() */
  44522. +#include <linux/errno.h> /* error codes */
  44523. +#include <linux/types.h> /* size_t */
  44524. +#include <linux/string.h>
  44525. +
  44526. +/* Check if __exit is defined, if not null it */
  44527. +#ifndef __exit
  44528. +#define __exit
  44529. +#endif
  44530. +
  44531. +/* Low freeswan header coupling */
  44532. +#include "openswan/ipsec_alg.h"
  44533. +
  44534. +#define ESP_NULL 11 /* from ipsec drafts */
  44535. +#define ESP_NULL_BLK_LEN 1
  44536. +
  44537. +MODULE_AUTHOR("JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>");
  44538. +static int debug_null=0;
  44539. +static int test_null=0;
  44540. +#ifdef module_param
  44541. +module_param(debug_null, int, 0600);
  44542. +module_param(test_null, int, 0600);
  44543. +#else
  44544. +MODULE_PARM(debug_null, "i");
  44545. +MODULE_PARM(test_null, "i");
  44546. +#endif
  44547. +
  44548. +typedef int null_context;
  44549. +
  44550. +struct null_eks{
  44551. + null_context null_ctx;
  44552. +};
  44553. +static int _null_set_key(struct ipsec_alg_enc *alg,
  44554. + __u8 * key_e, const __u8 * key,
  44555. + size_t keysize) {
  44556. + null_context *ctx=&((struct null_eks*)key_e)->null_ctx;
  44557. + if (debug_null > 0)
  44558. + printk(KERN_DEBUG "klips_debug:_null_set_key:"
  44559. + "key_e=%p key=%p keysize=%d\n",
  44560. + key_e, key, keysize);
  44561. + *ctx = 1;
  44562. + return 0;
  44563. +}
  44564. +static int _null_cbc_encrypt(struct ipsec_alg_enc *alg,
  44565. + __u8 * key_e, __u8 * in, int ilen, const __u8 * iv,
  44566. + int encrypt) {
  44567. + null_context *ctx=&((struct null_eks*)key_e)->null_ctx;
  44568. + if (debug_null > 0)
  44569. + printk(KERN_DEBUG "klips_debug:_null_cbc_encrypt:"
  44570. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  44571. + key_e, in, ilen, iv, encrypt);
  44572. + (*ctx)++;
  44573. + return ilen;
  44574. +}
  44575. +static struct ipsec_alg_enc ipsec_alg_NULL = {
  44576. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  44577. + ixt_refcnt: ATOMIC_INIT(0),
  44578. + ixt_name: "null",
  44579. + ixt_blocksize: ESP_NULL_BLK_LEN,
  44580. + ixt_support: {
  44581. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  44582. + ias_id: ESP_NULL,
  44583. + ias_ivlen: 0,
  44584. + ias_keyminbits: 0,
  44585. + ias_keymaxbits: 0,
  44586. + },
  44587. + },
  44588. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44589. + ixt_module: THIS_MODULE,
  44590. +#endif
  44591. + ixt_e_keylen: 0,
  44592. + ixt_e_ctx_size: sizeof(null_context),
  44593. + ixt_e_set_key: _null_set_key,
  44594. + ixt_e_cbc_encrypt:_null_cbc_encrypt,
  44595. +};
  44596. +
  44597. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44598. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_null_init )
  44599. +#else
  44600. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_null_init )
  44601. +#endif
  44602. +{
  44603. + int ret, test_ret;
  44604. + ret=register_ipsec_alg_enc(&ipsec_alg_NULL);
  44605. + printk("ipsec_null_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  44606. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44607. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44608. + ipsec_alg_NULL.ixt_common.ixt_name,
  44609. + ret);
  44610. + if (ret==0 && test_null) {
  44611. + test_ret=ipsec_alg_test(
  44612. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44613. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44614. + test_null);
  44615. + printk("ipsec_null_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  44616. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44617. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44618. + test_ret);
  44619. + }
  44620. + return ret;
  44621. +}
  44622. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44623. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_null_fini )
  44624. +#else
  44625. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_null_fini )
  44626. +#endif
  44627. +{
  44628. + unregister_ipsec_alg_enc(&ipsec_alg_NULL);
  44629. + return;
  44630. +}
  44631. +#ifdef MODULE_LICENSE
  44632. +MODULE_LICENSE("GPL");
  44633. +#endif
  44634. --- /dev/null Tue Mar 11 13:02:56 2003
  44635. +++ linux/net/ipsec/pfkey_v2.c Mon Feb 9 13:51:03 2004
  44636. @@ -0,0 +1,2011 @@
  44637. +/*
  44638. + * @(#) RFC2367 PF_KEYv2 Key management API domain socket I/F
  44639. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  44640. + *
  44641. + * This program is free software; you can redistribute it and/or modify it
  44642. + * under the terms of the GNU General Public License as published by the
  44643. + * Free Software Foundation; either version 2 of the License, or (at your
  44644. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  44645. + *
  44646. + * This program is distributed in the hope that it will be useful, but
  44647. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  44648. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  44649. + * for more details.
  44650. + *
  44651. + * RCSID $Id: pfkey_v2.c,v 1.97.2.10 2006/10/10 20:43:28 paul Exp $
  44652. + */
  44653. +
  44654. +/*
  44655. + * Template from /usr/src/linux-2.0.36/net/unix/af_unix.c.
  44656. + * Hints from /usr/src/linux-2.0.36/net/ipv4/udp.c.
  44657. + */
  44658. +
  44659. +#define __NO_VERSION__
  44660. +#include <linux/module.h>
  44661. +#include <linux/version.h>
  44662. +#ifndef AUTOCONF_INCLUDED
  44663. +#include <linux/config.h>
  44664. +#endif
  44665. +#include <linux/kernel.h>
  44666. +
  44667. +#include "openswan/ipsec_param.h"
  44668. +
  44669. +#include <linux/major.h>
  44670. +#include <linux/signal.h>
  44671. +#include <linux/sched.h>
  44672. +#include <linux/errno.h>
  44673. +#include <linux/string.h>
  44674. +#include <linux/stat.h>
  44675. +#include <linux/socket.h>
  44676. +#include <linux/un.h>
  44677. +#include <linux/fcntl.h>
  44678. +#include <linux/termios.h>
  44679. +#include <linux/socket.h>
  44680. +#include <linux/sockios.h>
  44681. +#include <linux/net.h> /* struct socket */
  44682. +#include <linux/in.h>
  44683. +#include <linux/fs.h>
  44684. +#ifdef MALLOC_SLAB
  44685. +# include <linux/slab.h> /* kmalloc() */
  44686. +#else /* MALLOC_SLAB */
  44687. +# include <linux/malloc.h> /* kmalloc() */
  44688. +#endif /* MALLOC_SLAB */
  44689. +#include <asm/segment.h>
  44690. +#include <linux/skbuff.h>
  44691. +#include <linux/netdevice.h>
  44692. +#include <net/sock.h> /* struct sock */
  44693. +#include <net/protocol.h>
  44694. +/* #include <net/tcp.h> */
  44695. +#include <net/af_unix.h>
  44696. +#ifdef CONFIG_PROC_FS
  44697. +# include <linux/proc_fs.h>
  44698. +#endif /* CONFIG_PROC_FS */
  44699. +
  44700. +#include <linux/types.h>
  44701. +
  44702. +#include <openswan.h>
  44703. +
  44704. +#include "openswan/radij.h"
  44705. +#include "openswan/ipsec_encap.h"
  44706. +#include "openswan/ipsec_sa.h"
  44707. +
  44708. +#include <pfkeyv2.h>
  44709. +#include <pfkey.h>
  44710. +
  44711. +#include "openswan/ipsec_proto.h"
  44712. +#include "openswan/ipsec_kern24.h"
  44713. +
  44714. +#ifdef CONFIG_KLIPS_DEBUG
  44715. +int debug_pfkey = 0;
  44716. +extern int sysctl_ipsec_debug_verbose;
  44717. +#endif /* CONFIG_KLIPS_DEBUG */
  44718. +
  44719. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  44720. +
  44721. +#ifndef SOCKOPS_WRAPPED
  44722. +#define SOCKOPS_WRAPPED(name) name
  44723. +#endif /* SOCKOPS_WRAPPED */
  44724. +
  44725. +#ifdef NET_26
  44726. +static rwlock_t pfkey_sock_lock = RW_LOCK_UNLOCKED;
  44727. +HLIST_HEAD(pfkey_sock_list);
  44728. +static DECLARE_WAIT_QUEUE_HEAD(pfkey_sock_wait);
  44729. +static atomic_t pfkey_sock_users = ATOMIC_INIT(0);
  44730. +#else
  44731. +struct sock *pfkey_sock_list = NULL;
  44732. +#endif
  44733. +
  44734. +struct supported_list *pfkey_supported_list[SADB_SATYPE_MAX+1];
  44735. +
  44736. +struct socket_list *pfkey_open_sockets = NULL;
  44737. +struct socket_list *pfkey_registered_sockets[SADB_SATYPE_MAX+1];
  44738. +
  44739. +int pfkey_msg_interp(struct sock *, struct sadb_msg *, struct sadb_msg **);
  44740. +
  44741. +DEBUG_NO_STATIC int pfkey_create(struct socket *sock, int protocol);
  44742. +DEBUG_NO_STATIC int pfkey_shutdown(struct socket *sock, int mode);
  44743. +DEBUG_NO_STATIC int pfkey_release(struct socket *sock);
  44744. +
  44745. +#ifdef NET_26
  44746. +DEBUG_NO_STATIC int pfkey_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len);
  44747. +DEBUG_NO_STATIC int pfkey_recvmsg(struct kiocb *kiocb, struct socket *sock, struct msghdr *msg
  44748. + , size_t size, int flags);
  44749. +#else
  44750. +DEBUG_NO_STATIC int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm);
  44751. +DEBUG_NO_STATIC int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm);
  44752. +#endif
  44753. +
  44754. +struct net_proto_family pfkey_family_ops = {
  44755. +#ifdef NETDEV_23
  44756. + .family = PF_KEY,
  44757. + .create = pfkey_create,
  44758. + .owner = THIS_MODULE,
  44759. +#else
  44760. + PF_KEY,
  44761. + pfkey_create
  44762. +#endif
  44763. +};
  44764. +
  44765. +struct proto_ops SOCKOPS_WRAPPED(pfkey_ops) = {
  44766. +#ifdef NETDEV_23
  44767. + family: PF_KEY,
  44768. + owner: THIS_MODULE,
  44769. + release: pfkey_release,
  44770. + bind: sock_no_bind,
  44771. + connect: sock_no_connect,
  44772. + socketpair: sock_no_socketpair,
  44773. + accept: sock_no_accept,
  44774. + getname: sock_no_getname,
  44775. + poll: datagram_poll,
  44776. + ioctl: sock_no_ioctl,
  44777. + listen: sock_no_listen,
  44778. + shutdown: pfkey_shutdown,
  44779. + setsockopt: sock_no_setsockopt,
  44780. + getsockopt: sock_no_getsockopt,
  44781. + sendmsg: pfkey_sendmsg,
  44782. + recvmsg: pfkey_recvmsg,
  44783. + mmap: sock_no_mmap,
  44784. +#else /* NETDEV_23 */
  44785. + PF_KEY,
  44786. + sock_no_dup,
  44787. + pfkey_release,
  44788. + sock_no_bind,
  44789. + sock_no_connect,
  44790. + sock_no_socketpair,
  44791. + sock_no_accept,
  44792. + sock_no_getname,
  44793. + datagram_poll,
  44794. + sock_no_ioctl,
  44795. + sock_no_listen,
  44796. + pfkey_shutdown,
  44797. + sock_no_setsockopt,
  44798. + sock_no_getsockopt,
  44799. + sock_no_fcntl,
  44800. + pfkey_sendmsg,
  44801. + pfkey_recvmsg
  44802. +#endif /* NETDEV_23 */
  44803. +};
  44804. +
  44805. +#ifdef NETDEV_23
  44806. +#include <linux/smp_lock.h>
  44807. +SOCKOPS_WRAP(pfkey, PF_KEY);
  44808. +#endif /* NETDEV_23 */
  44809. +
  44810. +#ifdef NET_26
  44811. +static void pfkey_sock_list_grab(void)
  44812. +{
  44813. + write_lock_bh(&pfkey_sock_lock);
  44814. +
  44815. + if (atomic_read(&pfkey_sock_users)) {
  44816. + DECLARE_WAITQUEUE(wait, current);
  44817. +
  44818. + add_wait_queue_exclusive(&pfkey_sock_wait, &wait);
  44819. + for(;;) {
  44820. + set_current_state(TASK_UNINTERRUPTIBLE);
  44821. + if (atomic_read(&pfkey_sock_users) == 0)
  44822. + break;
  44823. + write_unlock_bh(&pfkey_sock_lock);
  44824. + schedule();
  44825. + write_lock_bh(&pfkey_sock_lock);
  44826. + }
  44827. +
  44828. + __set_current_state(TASK_RUNNING);
  44829. + remove_wait_queue(&pfkey_sock_wait, &wait);
  44830. + }
  44831. +}
  44832. +
  44833. +static __inline__ void pfkey_sock_list_ungrab(void)
  44834. +{
  44835. + write_unlock_bh(&pfkey_sock_lock);
  44836. + wake_up(&pfkey_sock_wait);
  44837. +}
  44838. +
  44839. +static __inline__ void pfkey_lock_sock_list(void)
  44840. +{
  44841. + /* read_lock() synchronizes us to pfkey_table_grab */
  44842. +
  44843. + read_lock(&pfkey_sock_lock);
  44844. + atomic_inc(&pfkey_sock_users);
  44845. + read_unlock(&pfkey_sock_lock);
  44846. +}
  44847. +
  44848. +static __inline__ void pfkey_unlock_sock_list(void)
  44849. +{
  44850. + if (atomic_dec_and_test(&pfkey_sock_users))
  44851. + wake_up(&pfkey_sock_wait);
  44852. +}
  44853. +#endif
  44854. +
  44855. +int
  44856. +pfkey_list_remove_socket(struct socket *socketp, struct socket_list **sockets)
  44857. +{
  44858. + struct socket_list *socket_listp,*prev;
  44859. +
  44860. + if(!socketp) {
  44861. + KLIPS_PRINT(debug_pfkey,
  44862. + "klips_debug:pfkey_list_remove_socket: "
  44863. + "NULL socketp handed in, failed.\n");
  44864. + return -EINVAL;
  44865. + }
  44866. +
  44867. + if(!sockets) {
  44868. + KLIPS_PRINT(debug_pfkey,
  44869. + "klips_debug:pfkey_list_remove_socket: "
  44870. + "NULL sockets list handed in, failed.\n");
  44871. + return -EINVAL;
  44872. + }
  44873. +
  44874. + socket_listp = *sockets;
  44875. + prev = NULL;
  44876. +
  44877. + KLIPS_PRINT(debug_pfkey,
  44878. + "klips_debug:pfkey_list_remove_socket: "
  44879. + "removing sock=0p%p\n",
  44880. + socketp);
  44881. +
  44882. + while(socket_listp != NULL) {
  44883. + if(socket_listp->socketp == socketp) {
  44884. + if(prev != NULL) {
  44885. + prev->next = socket_listp->next;
  44886. + } else {
  44887. + *sockets = socket_listp->next;
  44888. + }
  44889. +
  44890. + kfree((void*)socket_listp);
  44891. +
  44892. + break;
  44893. + }
  44894. + prev = socket_listp;
  44895. + socket_listp = socket_listp->next;
  44896. + }
  44897. +
  44898. + return 0;
  44899. +}
  44900. +
  44901. +int
  44902. +pfkey_list_insert_socket(struct socket *socketp, struct socket_list **sockets)
  44903. +{
  44904. + struct socket_list *socket_listp;
  44905. +
  44906. + if(!socketp) {
  44907. + KLIPS_PRINT(debug_pfkey,
  44908. + "klips_debug:pfkey_list_insert_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_insert_socket: "
  44916. + "NULL sockets list handed in, failed.\n");
  44917. + return -EINVAL;
  44918. + }
  44919. +
  44920. + KLIPS_PRINT(debug_pfkey,
  44921. + "klips_debug:pfkey_list_insert_socket: "
  44922. + "allocating %lu bytes for socketp=0p%p\n",
  44923. + (unsigned long) sizeof(struct socket_list),
  44924. + socketp);
  44925. +
  44926. + if((socket_listp = (struct socket_list *)kmalloc(sizeof(struct socket_list), GFP_KERNEL)) == NULL) {
  44927. + KLIPS_PRINT(debug_pfkey,
  44928. + "klips_debug:pfkey_list_insert_socket: "
  44929. + "memory allocation error.\n");
  44930. + return -ENOMEM;
  44931. + }
  44932. +
  44933. + socket_listp->socketp = socketp;
  44934. + socket_listp->next = *sockets;
  44935. + *sockets = socket_listp;
  44936. +
  44937. + return 0;
  44938. +}
  44939. +
  44940. +int
  44941. +pfkey_list_remove_supported(struct ipsec_alg_supported *supported, struct supported_list **supported_list)
  44942. +{
  44943. + struct supported_list *supported_listp = *supported_list, *prev = NULL;
  44944. +
  44945. + if(!supported) {
  44946. + KLIPS_PRINT(debug_pfkey,
  44947. + "klips_debug:pfkey_list_remove_supported: "
  44948. + "NULL supported handed in, failed.\n");
  44949. + return -EINVAL;
  44950. + }
  44951. +
  44952. + if(!supported_list) {
  44953. + KLIPS_PRINT(debug_pfkey,
  44954. + "klips_debug:pfkey_list_remove_supported: "
  44955. + "NULL supported_list handed in, failed.\n");
  44956. + return -EINVAL;
  44957. + }
  44958. +
  44959. + KLIPS_PRINT(debug_pfkey,
  44960. + "klips_debug:pfkey_list_remove_supported: "
  44961. + "removing supported=0p%p\n",
  44962. + supported);
  44963. +
  44964. + while(supported_listp != NULL) {
  44965. + if(supported_listp->supportedp == supported) {
  44966. + if(prev != NULL) {
  44967. + prev->next = supported_listp->next;
  44968. + } else {
  44969. + *supported_list = supported_listp->next;
  44970. + }
  44971. +
  44972. + kfree((void*)supported_listp);
  44973. +
  44974. + break;
  44975. + }
  44976. + prev = supported_listp;
  44977. + supported_listp = supported_listp->next;
  44978. + }
  44979. +
  44980. + return 0;
  44981. +}
  44982. +
  44983. +int
  44984. +pfkey_list_insert_supported(struct ipsec_alg_supported *supported
  44985. + , struct supported_list **supported_list)
  44986. +{
  44987. + struct supported_list *supported_listp;
  44988. +
  44989. + if(!supported) {
  44990. + KLIPS_PRINT(debug_pfkey,
  44991. + "klips_debug:pfkey_list_insert_supported: "
  44992. + "NULL supported handed in, failed.\n");
  44993. + return -EINVAL;
  44994. + }
  44995. +
  44996. + if(!supported_list) {
  44997. + KLIPS_PRINT(debug_pfkey,
  44998. + "klips_debug:pfkey_list_insert_supported: "
  44999. + "NULL supported_list handed in, failed.\n");
  45000. + return -EINVAL;
  45001. + }
  45002. +
  45003. + KLIPS_PRINT(debug_pfkey,
  45004. + "klips_debug:pfkey_list_insert_supported: "
  45005. + "allocating %lu bytes for incoming, supported=0p%p, supported_list=0p%p\n",
  45006. + (unsigned long) sizeof(struct supported_list),
  45007. + supported,
  45008. + supported_list);
  45009. +
  45010. + supported_listp = (struct supported_list *)kmalloc(sizeof(struct supported_list), GFP_KERNEL);
  45011. +
  45012. + if(supported_listp == NULL)
  45013. + {
  45014. + KLIPS_PRINT(debug_pfkey,
  45015. + "klips_debug:pfkey_list_insert_supported: "
  45016. + "memory allocation error.\n");
  45017. + return -ENOMEM;
  45018. + }
  45019. +
  45020. + supported_listp->supportedp = supported;
  45021. + supported_listp->next = *supported_list;
  45022. + *supported_list = supported_listp;
  45023. + KLIPS_PRINT(debug_pfkey,
  45024. + "klips_debug:pfkey_list_insert_supported: "
  45025. + "outgoing, supported=0p%p, supported_list=0p%p\n",
  45026. + supported,
  45027. + supported_list);
  45028. +
  45029. + return 0;
  45030. +}
  45031. +
  45032. +#ifdef NET_26
  45033. +DEBUG_NO_STATIC void
  45034. +pfkey_insert_socket(struct sock *sk)
  45035. +{
  45036. + KLIPS_PRINT(debug_pfkey,
  45037. + "klips_debug:pfkey_insert_socket: "
  45038. + "sk=0p%p\n",
  45039. + sk);
  45040. + pfkey_sock_list_grab();
  45041. + sk_add_node(sk, &pfkey_sock_list);
  45042. + pfkey_sock_list_ungrab();
  45043. +}
  45044. +
  45045. +DEBUG_NO_STATIC void
  45046. +pfkey_remove_socket(struct sock *sk)
  45047. +{
  45048. + KLIPS_PRINT(debug_pfkey,
  45049. + "klips_debug:pfkey_remove_socket: 0p%p\n", sk);
  45050. + pfkey_sock_list_grab();
  45051. + sk_del_node_init(sk);
  45052. + pfkey_sock_list_ungrab();
  45053. + return;
  45054. +}
  45055. +#else
  45056. +
  45057. +DEBUG_NO_STATIC void
  45058. +pfkey_insert_socket(struct sock *sk)
  45059. +{
  45060. + KLIPS_PRINT(debug_pfkey,
  45061. + "klips_debug:pfkey_insert_socket: "
  45062. + "sk=0p%p\n",
  45063. + sk);
  45064. + cli();
  45065. + sk->next=pfkey_sock_list;
  45066. + pfkey_sock_list=sk;
  45067. + sti();
  45068. +}
  45069. +DEBUG_NO_STATIC void
  45070. +pfkey_remove_socket(struct sock *sk)
  45071. +{
  45072. + struct sock **s;
  45073. +
  45074. + s = NULL;
  45075. + KLIPS_PRINT(debug_pfkey,
  45076. + "klips_debug:pfkey_remove_socket: .\n");
  45077. +
  45078. + cli();
  45079. + s=&pfkey_sock_list;
  45080. +
  45081. + while(*s!=NULL) {
  45082. + if(*s==sk) {
  45083. + *s=sk->next;
  45084. + sk->next=NULL;
  45085. + sti();
  45086. + KLIPS_PRINT(debug_pfkey,
  45087. + "klips_debug:pfkey_remove_socket: "
  45088. + "succeeded.\n");
  45089. + return;
  45090. + }
  45091. + s=&((*s)->next);
  45092. + }
  45093. + sti();
  45094. +
  45095. + KLIPS_PRINT(debug_pfkey,
  45096. + "klips_debug:pfkey_remove_socket: "
  45097. + "not found.\n");
  45098. + return;
  45099. +}
  45100. +#endif
  45101. +
  45102. +DEBUG_NO_STATIC void
  45103. +pfkey_destroy_socket(struct sock *sk)
  45104. +{
  45105. + struct sk_buff *skb;
  45106. +
  45107. + KLIPS_PRINT(debug_pfkey,
  45108. + "klips_debug:pfkey_destroy_socket: 0p%p\n",sk);
  45109. + pfkey_remove_socket(sk);
  45110. +
  45111. + KLIPS_PRINT(debug_pfkey,
  45112. + "klips_debug:pfkey_destroy_socket: "
  45113. + "pfkey_remove_socket called, sk=0p%p\n",sk);
  45114. +
  45115. + KLIPS_PRINT(debug_pfkey,
  45116. + "klips_debug:pfkey_destroy_socket: "
  45117. + "sk(0p%p)->(&0p%p)receive_queue.{next=0p%p,prev=0p%p}.\n",
  45118. + sk,
  45119. + &(sk->sk_receive_queue),
  45120. + sk->sk_receive_queue.next,
  45121. + sk->sk_receive_queue.prev);
  45122. +
  45123. + while(sk && ((skb=skb_dequeue(&(sk->sk_receive_queue)))!=NULL)) {
  45124. +#ifdef CONFIG_KLIPS_DEBUG
  45125. + if(debug_pfkey && sysctl_ipsec_debug_verbose) {
  45126. + KLIPS_PRINT(debug_pfkey,
  45127. + "klips_debug:pfkey_destroy_socket: "
  45128. + "skb=0p%p dequeued.\n", skb);
  45129. + printk(KERN_INFO "klips_debug:pfkey_destroy_socket: "
  45130. + "pfkey_skb contents:");
  45131. + printk(" next:0p%p", skb->next);
  45132. + printk(" prev:0p%p", skb->prev);
  45133. + printk(" sk:0p%p", skb->sk);
  45134. + printk(" dev:0p%p", skb->dev);
  45135. + if(skb->dev) {
  45136. + if(skb->dev->name) {
  45137. + printk(" dev->name:%s", skb->dev->name);
  45138. + } else {
  45139. + printk(" dev->name:NULL?");
  45140. + }
  45141. + } else {
  45142. + printk(" dev:NULL");
  45143. + }
  45144. + printk(" h:0p%p", skb->h.raw);
  45145. + printk(" nh:0p%p", skb->nh.raw);
  45146. + printk(" mac:0p%p", skb->mac.raw);
  45147. + printk(" dst:0p%p", skb->dst);
  45148. + if(sysctl_ipsec_debug_verbose) {
  45149. + int i;
  45150. +
  45151. + printk(" cb");
  45152. + for(i=0; i<48; i++) {
  45153. + printk(":%2x", skb->cb[i]);
  45154. + }
  45155. + }
  45156. + printk(" len:%d", skb->len);
  45157. + printk(" csum:%d", skb->csum);
  45158. +#ifndef NETDEV_23
  45159. + printk(" used:%d", skb->used);
  45160. + printk(" is_clone:%d", skb->is_clone);
  45161. +#endif /* NETDEV_23 */
  45162. + printk(" cloned:%d", skb->cloned);
  45163. + printk(" pkt_type:%d", skb->pkt_type);
  45164. + printk(" ip_summed:%d", skb->ip_summed);
  45165. + printk(" priority:%d", skb->priority);
  45166. + printk(" protocol:%d", skb->protocol);
  45167. +#ifdef HAVE_SOCK_SECURITY
  45168. + printk(" security:%d", skb->security);
  45169. +#endif
  45170. + printk(" truesize:%d", skb->truesize);
  45171. + printk(" head:0p%p", skb->head);
  45172. + printk(" data:0p%p", skb->data);
  45173. + printk(" tail:0p%p", skb->tail);
  45174. + printk(" end:0p%p", skb->end);
  45175. + if(sysctl_ipsec_debug_verbose) {
  45176. + unsigned char* i;
  45177. + printk(" data");
  45178. + for(i = skb->head; i < skb->end; i++) {
  45179. + printk(":%2x", (unsigned char)(*(i)));
  45180. + }
  45181. + }
  45182. + printk(" destructor:0p%p", skb->destructor);
  45183. + printk("\n");
  45184. + }
  45185. +#endif /* CONFIG_KLIPS_DEBUG */
  45186. + KLIPS_PRINT(debug_pfkey,
  45187. + "klips_debug:pfkey_destroy_socket: "
  45188. + "skb=0p%p freed.\n",
  45189. + skb);
  45190. + ipsec_kfree_skb(skb);
  45191. + }
  45192. +
  45193. +#ifdef NET_26
  45194. + sock_set_flag(sk, SOCK_DEAD);
  45195. +#else
  45196. + sk->dead = 1;
  45197. +#endif
  45198. + sk_free(sk);
  45199. +
  45200. + KLIPS_PRINT(debug_pfkey,
  45201. + "klips_debug:pfkey_destroy_socket: destroyed.\n");
  45202. +}
  45203. +
  45204. +int
  45205. +pfkey_upmsg(struct socket *sock, struct sadb_msg *pfkey_msg)
  45206. +{
  45207. + int error = 0;
  45208. + struct sk_buff * skb = NULL;
  45209. + struct sock *sk;
  45210. +
  45211. + if(sock == NULL) {
  45212. + KLIPS_PRINT(debug_pfkey,
  45213. + "klips_debug:pfkey_upmsg: "
  45214. + "NULL socket passed in.\n");
  45215. + return -EINVAL;
  45216. + }
  45217. +
  45218. + if(pfkey_msg == NULL) {
  45219. + KLIPS_PRINT(debug_pfkey,
  45220. + "klips_debug:pfkey_upmsg: "
  45221. + "NULL pfkey_msg passed in.\n");
  45222. + return -EINVAL;
  45223. + }
  45224. +
  45225. + sk = sock->sk;
  45226. +
  45227. + if(sk == NULL) {
  45228. + KLIPS_PRINT(debug_pfkey,
  45229. + "klips_debug:pfkey_upmsg: "
  45230. + "NULL sock passed in.\n");
  45231. + return -EINVAL;
  45232. + }
  45233. +
  45234. + KLIPS_PRINT(debug_pfkey,
  45235. + "klips_debug:pfkey_upmsg: "
  45236. + "allocating %d bytes...\n",
  45237. + (int)(pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN));
  45238. + if(!(skb = alloc_skb(pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN, GFP_ATOMIC) )) {
  45239. + KLIPS_PRINT(debug_pfkey,
  45240. + "klips_debug:pfkey_upmsg: "
  45241. + "no buffers left to send up a message.\n");
  45242. + return -ENOBUFS;
  45243. + }
  45244. + KLIPS_PRINT(debug_pfkey,
  45245. + "klips_debug:pfkey_upmsg: "
  45246. + "...allocated at 0p%p.\n",
  45247. + skb);
  45248. +
  45249. + skb->dev = NULL;
  45250. +
  45251. + if(skb_tailroom(skb) < pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {
  45252. + printk(KERN_WARNING "klips_error:pfkey_upmsg: "
  45253. + "tried to skb_put %ld, %d available. This should never happen, please report.\n",
  45254. + (unsigned long int)pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN,
  45255. + skb_tailroom(skb));
  45256. + ipsec_kfree_skb(skb);
  45257. + return -ENOBUFS;
  45258. + }
  45259. + skb->h.raw = skb_put(skb, pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  45260. + memcpy(skb->h.raw, pfkey_msg, pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  45261. +
  45262. + if((error = sock_queue_rcv_skb(sk, skb)) < 0) {
  45263. + skb->sk=NULL;
  45264. + KLIPS_PRINT(debug_pfkey,
  45265. + "klips_debug:pfkey_upmsg: "
  45266. + "error=%d calling sock_queue_rcv_skb with skb=0p%p.\n",
  45267. + error,
  45268. + skb);
  45269. + ipsec_kfree_skb(skb);
  45270. + return error;
  45271. + }
  45272. + return error;
  45273. +}
  45274. +
  45275. +#ifdef NET_26_12_SKALLOC
  45276. +static struct proto key_proto = {
  45277. + .name = "KEY",
  45278. + .owner = THIS_MODULE,
  45279. + .obj_size = sizeof(struct sock),
  45280. +
  45281. +};
  45282. +#endif
  45283. +
  45284. +DEBUG_NO_STATIC int
  45285. +pfkey_create(struct socket *sock, int protocol)
  45286. +{
  45287. + struct sock *sk;
  45288. +
  45289. + if(sock == NULL) {
  45290. + KLIPS_PRINT(debug_pfkey,
  45291. + "klips_debug:pfkey_create: "
  45292. + "socket NULL.\n");
  45293. + return -EINVAL;
  45294. + }
  45295. +
  45296. + KLIPS_PRINT(debug_pfkey,
  45297. + "klips_debug:pfkey_create: "
  45298. + "sock=0p%p type:%d state:%d flags:%ld protocol:%d\n",
  45299. + sock,
  45300. + sock->type,
  45301. + (unsigned int)(sock->state),
  45302. + sock->flags, protocol);
  45303. +
  45304. + if(sock->type != SOCK_RAW) {
  45305. + KLIPS_PRINT(debug_pfkey,
  45306. + "klips_debug:pfkey_create: "
  45307. + "only SOCK_RAW supported.\n");
  45308. + return -ESOCKTNOSUPPORT;
  45309. + }
  45310. +
  45311. + if(protocol != PF_KEY_V2) {
  45312. + KLIPS_PRINT(debug_pfkey,
  45313. + "klips_debug:pfkey_create: "
  45314. + "protocol not PF_KEY_V2.\n");
  45315. + return -EPROTONOSUPPORT;
  45316. + }
  45317. +
  45318. + if((current->uid != 0)) {
  45319. + KLIPS_PRINT(debug_pfkey,
  45320. + "klips_debug:pfkey_create: "
  45321. + "must be root to open pfkey sockets.\n");
  45322. + return -EACCES;
  45323. + }
  45324. +
  45325. + sock->state = SS_UNCONNECTED;
  45326. +
  45327. + KLIPS_INC_USE;
  45328. +
  45329. +#ifdef NET_26
  45330. +#ifdef NET_26_12_SKALLOC
  45331. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
  45332. +#else
  45333. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, 1, NULL);
  45334. +#endif
  45335. +#else
  45336. + /* 2.4 interface */
  45337. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, 1);
  45338. +#endif
  45339. +
  45340. + if(sk == NULL)
  45341. + {
  45342. + KLIPS_PRINT(debug_pfkey,
  45343. + "klips_debug:pfkey_create: "
  45344. + "Out of memory trying to allocate.\n");
  45345. + KLIPS_DEC_USE;
  45346. + return -ENOMEM;
  45347. + }
  45348. +
  45349. + sock_init_data(sock, sk);
  45350. +
  45351. + sk->sk_destruct = NULL;
  45352. + sk->sk_reuse = 1;
  45353. + sock->ops = &pfkey_ops;
  45354. +
  45355. + sk->sk_family = PF_KEY;
  45356. +/* sk->num = protocol; */
  45357. + sk->sk_protocol = protocol;
  45358. + key_pid(sk) = current->pid;
  45359. + KLIPS_PRINT(debug_pfkey,
  45360. + "klips_debug:pfkey_create: "
  45361. + "sock->fasync_list=0p%p sk->sleep=0p%p.\n",
  45362. + sock->fasync_list,
  45363. + sk->sk_sleep);
  45364. +
  45365. + pfkey_insert_socket(sk);
  45366. + pfkey_list_insert_socket(sock, &pfkey_open_sockets);
  45367. +
  45368. + KLIPS_PRINT(debug_pfkey,
  45369. + "klips_debug:pfkey_create: "
  45370. + "Socket sock=0p%p sk=0p%p initialised.\n", sock, sk);
  45371. + return 0;
  45372. +}
  45373. +
  45374. +DEBUG_NO_STATIC int
  45375. +#ifdef NETDEV_23
  45376. +pfkey_release(struct socket *sock)
  45377. +#else /* NETDEV_23 */
  45378. +pfkey_release(struct socket *sock, struct socket *peersock)
  45379. +#endif /* NETDEV_23 */
  45380. +{
  45381. + struct sock *sk;
  45382. + int i;
  45383. +
  45384. + if(sock==NULL) {
  45385. + KLIPS_PRINT(debug_pfkey,
  45386. + "klips_debug:pfkey_release: "
  45387. + "No socket attached.\n");
  45388. + return 0; /* -EINVAL; */
  45389. + }
  45390. +
  45391. + sk=sock->sk;
  45392. +
  45393. + /* May not have data attached */
  45394. + if(sk==NULL) {
  45395. + KLIPS_PRINT(debug_pfkey,
  45396. + "klips_debug:pfkey_release: "
  45397. + "No sk attached to sock=0p%p.\n", sock);
  45398. + return 0; /* -EINVAL; */
  45399. + }
  45400. +
  45401. + KLIPS_PRINT(debug_pfkey,
  45402. + "klips_debug:pfkey_release: "
  45403. + "sock=0p%p sk=0p%p\n", sock, sk);
  45404. +
  45405. + if(sock_flag(sk, SOCK_DEAD))
  45406. + if(sk->sk_state_change) {
  45407. + sk->sk_state_change(sk);
  45408. + }
  45409. +
  45410. + sock->sk = NULL;
  45411. +
  45412. + /* Try to flush out this socket. Throw out buffers at least */
  45413. + pfkey_destroy_socket(sk);
  45414. + pfkey_list_remove_socket(sock, &pfkey_open_sockets);
  45415. + for(i = SADB_SATYPE_UNSPEC; i <= SADB_SATYPE_MAX; i++) {
  45416. + pfkey_list_remove_socket(sock, &(pfkey_registered_sockets[i]));
  45417. + }
  45418. +
  45419. + KLIPS_DEC_USE;
  45420. + KLIPS_PRINT(debug_pfkey,
  45421. + "klips_debug:pfkey_release: "
  45422. + "succeeded.\n");
  45423. +
  45424. + return 0;
  45425. +}
  45426. +
  45427. +DEBUG_NO_STATIC int
  45428. +pfkey_shutdown(struct socket *sock, int mode)
  45429. +{
  45430. + struct sock *sk;
  45431. +
  45432. + if(sock == NULL) {
  45433. + KLIPS_PRINT(debug_pfkey,
  45434. + "klips_debug:pfkey_shutdown: "
  45435. + "NULL socket passed in.\n");
  45436. + return -EINVAL;
  45437. + }
  45438. +
  45439. + sk=sock->sk;
  45440. +
  45441. + if(sk == NULL) {
  45442. + KLIPS_PRINT(debug_pfkey,
  45443. + "klips_debug:pfkey_shutdown: "
  45444. + "No sock attached to socket.\n");
  45445. + return -EINVAL;
  45446. + }
  45447. +
  45448. + KLIPS_PRINT(debug_pfkey,
  45449. + "klips_debug:pfkey_shutdown: "
  45450. + "mode=%x.\n", mode);
  45451. + mode++;
  45452. +
  45453. + if(mode&SEND_SHUTDOWN) {
  45454. + sk->sk_shutdown|=SEND_SHUTDOWN;
  45455. + sk->sk_state_change(sk);
  45456. + }
  45457. +
  45458. + if(mode&RCV_SHUTDOWN) {
  45459. + sk->sk_shutdown|=RCV_SHUTDOWN;
  45460. + sk->sk_state_change(sk);
  45461. + }
  45462. + return 0;
  45463. +}
  45464. +
  45465. +/*
  45466. + * Send PF_KEY data down.
  45467. + */
  45468. +
  45469. +DEBUG_NO_STATIC int
  45470. +#ifdef NET_26
  45471. +pfkey_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
  45472. +#else
  45473. +pfkey_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
  45474. +#endif
  45475. +{
  45476. + struct sock *sk;
  45477. + int error = 0;
  45478. + struct sadb_msg *pfkey_msg = NULL, *pfkey_reply = NULL;
  45479. +
  45480. + if(sock == NULL) {
  45481. + KLIPS_PRINT(debug_pfkey,
  45482. + "klips_debug:pfkey_sendmsg: "
  45483. + "Null socket passed in.\n");
  45484. + SENDERR(EINVAL);
  45485. + }
  45486. +
  45487. + sk = sock->sk;
  45488. +
  45489. + if(sk == NULL) {
  45490. + KLIPS_PRINT(debug_pfkey,
  45491. + "klips_debug:pfkey_sendmsg: "
  45492. + "Null sock passed in.\n");
  45493. + SENDERR(EINVAL);
  45494. + }
  45495. +
  45496. + if(msg == NULL) {
  45497. + KLIPS_PRINT(debug_pfkey,
  45498. + "klips_debug:pfkey_sendmsg: "
  45499. + "Null msghdr passed in.\n");
  45500. + SENDERR(EINVAL);
  45501. + }
  45502. +
  45503. + KLIPS_PRINT(debug_pfkey,
  45504. + "klips_debug:pfkey_sendmsg: .\n");
  45505. + if(sk->sk_err) {
  45506. + error = sock_error(sk);
  45507. + KLIPS_PRINT(debug_pfkey,
  45508. + "klips_debug:pfkey_sendmsg: "
  45509. + "sk->err is non-zero, returns %d.\n",
  45510. + error);
  45511. + SENDERR(-error);
  45512. + }
  45513. +
  45514. + if((current->uid != 0)) {
  45515. + KLIPS_PRINT(debug_pfkey,
  45516. + "klips_debug:pfkey_sendmsg: "
  45517. + "must be root to send messages to pfkey sockets.\n");
  45518. + SENDERR(EACCES);
  45519. + }
  45520. +
  45521. + if(msg->msg_control)
  45522. + {
  45523. + KLIPS_PRINT(debug_pfkey,
  45524. + "klips_debug:pfkey_sendmsg: "
  45525. + "can't set flags or set msg_control.\n");
  45526. + SENDERR(EINVAL);
  45527. + }
  45528. +
  45529. + if(sk->sk_shutdown & SEND_SHUTDOWN) {
  45530. + KLIPS_PRINT(debug_pfkey,
  45531. + "klips_debug:pfkey_sendmsg: "
  45532. + "shutdown.\n");
  45533. + send_sig(SIGPIPE, current, 0);
  45534. + SENDERR(EPIPE);
  45535. + }
  45536. +
  45537. + if(len < sizeof(struct sadb_msg)) {
  45538. + KLIPS_PRINT(debug_pfkey,
  45539. + "klips_debug:pfkey_sendmsg: "
  45540. + "bogus msg len of %d, too small.\n", (int)len);
  45541. + SENDERR(EMSGSIZE);
  45542. + }
  45543. +
  45544. + KLIPS_PRINT(debug_pfkey,
  45545. + "klips_debug:pfkey_sendmsg: "
  45546. + "allocating %d bytes for downward message.\n",
  45547. + (int)len);
  45548. + if((pfkey_msg = (struct sadb_msg*)kmalloc(len, GFP_KERNEL)) == NULL) {
  45549. + KLIPS_PRINT(debug_pfkey,
  45550. + "klips_debug:pfkey_sendmsg: "
  45551. + "memory allocation error.\n");
  45552. + SENDERR(ENOBUFS);
  45553. + }
  45554. +
  45555. + memcpy_fromiovec((void *)pfkey_msg, msg->msg_iov, len);
  45556. +
  45557. + if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {
  45558. + KLIPS_PRINT(1 || debug_pfkey,
  45559. + "klips_debug:pfkey_sendmsg: "
  45560. + "not PF_KEY_V2 msg, found %d, should be %d.\n",
  45561. + pfkey_msg->sadb_msg_version,
  45562. + PF_KEY_V2);
  45563. + kfree((void*)pfkey_msg);
  45564. + return -EINVAL;
  45565. + }
  45566. +
  45567. + if(len != pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {
  45568. + KLIPS_PRINT(debug_pfkey,
  45569. + "klips_debug:pfkey_sendmsg: "
  45570. + "bogus msg len of %d, not %d byte aligned.\n",
  45571. + (int)len, (int)IPSEC_PFKEYv2_ALIGN);
  45572. + SENDERR(EMSGSIZE);
  45573. + }
  45574. +
  45575. +#if 0
  45576. + /* This check is questionable, since a downward message could be
  45577. + the result of an ACQUIRE either from kernel (PID==0) or
  45578. + userspace (some other PID). */
  45579. + /* check PID */
  45580. + if(pfkey_msg->sadb_msg_pid != current->pid) {
  45581. + KLIPS_PRINT(debug_pfkey,
  45582. + "klips_debug:pfkey_sendmsg: "
  45583. + "pid (%d) does not equal sending process pid (%d).\n",
  45584. + pfkey_msg->sadb_msg_pid, current->pid);
  45585. + SENDERR(EINVAL);
  45586. + }
  45587. +#endif
  45588. +
  45589. + if(pfkey_msg->sadb_msg_reserved) {
  45590. + KLIPS_PRINT(debug_pfkey,
  45591. + "klips_debug:pfkey_sendmsg: "
  45592. + "reserved field must be zero, set to %d.\n",
  45593. + pfkey_msg->sadb_msg_reserved);
  45594. + SENDERR(EINVAL);
  45595. + }
  45596. +
  45597. + if((pfkey_msg->sadb_msg_type > SADB_MAX) || (!pfkey_msg->sadb_msg_type)){
  45598. + KLIPS_PRINT(debug_pfkey,
  45599. + "klips_debug:pfkey_sendmsg: "
  45600. + "msg type too large or small:%d.\n",
  45601. + pfkey_msg->sadb_msg_type);
  45602. + SENDERR(EINVAL);
  45603. + }
  45604. +
  45605. + KLIPS_PRINT(debug_pfkey,
  45606. + "klips_debug:pfkey_sendmsg: "
  45607. + "msg sent for parsing.\n");
  45608. +
  45609. + if((error = pfkey_msg_interp(sk, pfkey_msg, &pfkey_reply))) {
  45610. + struct socket_list *pfkey_socketsp;
  45611. +
  45612. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45613. + "pfkey_msg_parse returns %d.\n",
  45614. + error);
  45615. +
  45616. + if((pfkey_reply = (struct sadb_msg*)kmalloc(sizeof(struct sadb_msg), GFP_KERNEL)) == NULL) {
  45617. + KLIPS_PRINT(debug_pfkey,
  45618. + "klips_debug:pfkey_sendmsg: "
  45619. + "memory allocation error.\n");
  45620. + SENDERR(ENOBUFS);
  45621. + }
  45622. + memcpy((void*)pfkey_reply, (void*)pfkey_msg, sizeof(struct sadb_msg));
  45623. + pfkey_reply->sadb_msg_errno = -error;
  45624. + pfkey_reply->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  45625. +
  45626. + for(pfkey_socketsp = pfkey_open_sockets;
  45627. + pfkey_socketsp;
  45628. + pfkey_socketsp = pfkey_socketsp->next) {
  45629. + int error_upmsg = 0;
  45630. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45631. + "sending up error=%d message=0p%p to socket=0p%p.\n",
  45632. + error,
  45633. + pfkey_reply,
  45634. + pfkey_socketsp->socketp);
  45635. + if((error_upmsg = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  45636. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45637. + "sending up error message to socket=0p%p failed with error=%d.\n",
  45638. + pfkey_socketsp->socketp,
  45639. + error_upmsg);
  45640. + /* pfkey_msg_free(&pfkey_reply); */
  45641. + /* SENDERR(-error); */
  45642. + }
  45643. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45644. + "sending up error message to socket=0p%p succeeded.\n",
  45645. + pfkey_socketsp->socketp);
  45646. + }
  45647. +
  45648. + pfkey_msg_free(&pfkey_reply);
  45649. +
  45650. + SENDERR(-error);
  45651. + }
  45652. +
  45653. + errlab:
  45654. + if (pfkey_msg) {
  45655. + kfree((void*)pfkey_msg);
  45656. + }
  45657. +
  45658. + if(error) {
  45659. + return error;
  45660. + } else {
  45661. + return len;
  45662. + }
  45663. +}
  45664. +
  45665. +/*
  45666. + * Receive PF_KEY data up.
  45667. + */
  45668. +
  45669. +DEBUG_NO_STATIC int
  45670. +#ifdef NET_26
  45671. +pfkey_recvmsg(struct kiocb *kiocb
  45672. + , struct socket *sock
  45673. + , struct msghdr *msg
  45674. + , size_t size
  45675. + , int flags)
  45676. +#else
  45677. +pfkey_recvmsg(struct socket *sock
  45678. + , struct msghdr *msg
  45679. + , int size, int flags
  45680. + , struct scm_cookie *scm)
  45681. +#endif
  45682. +{
  45683. + struct sock *sk;
  45684. + int noblock = flags & MSG_DONTWAIT;
  45685. + struct sk_buff *skb;
  45686. + int error;
  45687. +
  45688. + if(sock == NULL) {
  45689. + KLIPS_PRINT(debug_pfkey,
  45690. + "klips_debug:pfkey_recvmsg: "
  45691. + "Null socket passed in.\n");
  45692. + return -EINVAL;
  45693. + }
  45694. +
  45695. + sk = sock->sk;
  45696. +
  45697. + if(sk == NULL) {
  45698. + KLIPS_PRINT(debug_pfkey,
  45699. + "klips_debug:pfkey_recvmsg: "
  45700. + "Null sock passed in for sock=0p%p.\n", sock);
  45701. + return -EINVAL;
  45702. + }
  45703. +
  45704. + if(msg == NULL) {
  45705. + KLIPS_PRINT(debug_pfkey,
  45706. + "klips_debug:pfkey_recvmsg: "
  45707. + "Null msghdr passed in for sock=0p%p, sk=0p%p.\n",
  45708. + sock, sk);
  45709. + return -EINVAL;
  45710. + }
  45711. +
  45712. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  45713. + "klips_debug:pfkey_recvmsg: sock=0p%p sk=0p%p msg=0p%p size=%d.\n",
  45714. + sock, sk, msg, (int)size);
  45715. + if(flags & ~MSG_PEEK) {
  45716. + KLIPS_PRINT(debug_pfkey,
  45717. + "klips_debug:pfkey_sendmsg: "
  45718. + "flags (%d) other than MSG_PEEK not supported.\n",
  45719. + flags);
  45720. + return -EOPNOTSUPP;
  45721. + }
  45722. +
  45723. + msg->msg_namelen = 0; /* sizeof(*ska); */
  45724. +
  45725. + if(sk->sk_err) {
  45726. + KLIPS_PRINT(debug_pfkey,
  45727. + "klips_debug:pfkey_sendmsg: "
  45728. + "sk->sk_err=%d.\n", sk->sk_err);
  45729. + return sock_error(sk);
  45730. + }
  45731. +
  45732. + if((skb = skb_recv_datagram(sk, flags, noblock, &error) ) == NULL) {
  45733. + return error;
  45734. + }
  45735. +
  45736. + if(size > skb->len) {
  45737. + size = skb->len;
  45738. + }
  45739. + else if(size <skb->len) {
  45740. + msg->msg_flags |= MSG_TRUNC;
  45741. + }
  45742. +
  45743. + skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
  45744. +#ifdef HAVE_TSTAMP
  45745. + sk->sk_stamp.tv_sec = skb->tstamp.off_sec;
  45746. + sk->sk_stamp.tv_usec = skb->tstamp.off_usec;
  45747. +#else
  45748. + sk->sk_stamp=skb->stamp;
  45749. +#endif
  45750. +
  45751. + skb_free_datagram(sk, skb);
  45752. + return size;
  45753. +}
  45754. +
  45755. +#ifdef CONFIG_PROC_FS
  45756. +#ifndef PROC_FS_2325
  45757. +DEBUG_NO_STATIC
  45758. +#endif /* PROC_FS_2325 */
  45759. +int
  45760. +pfkey_get_info(char *buffer, char **start, off_t offset, int length
  45761. +#ifndef PROC_NO_DUMMY
  45762. +, int dummy
  45763. +#endif /* !PROC_NO_DUMMY */
  45764. +)
  45765. +{
  45766. + const int max_content = length > 0? length-1 : 0; /* limit of useful snprintf output */
  45767. +#ifdef NET_26
  45768. + struct hlist_node *node;
  45769. +#endif
  45770. + off_t begin=0;
  45771. + int len=0;
  45772. + struct sock *sk;
  45773. +
  45774. +#ifdef CONFIG_KLIPS_DEBUG
  45775. + if(!sysctl_ipsec_debug_verbose) {
  45776. +#endif /* CONFIG_KLIPS_DEBUG */
  45777. + len += ipsec_snprintf(buffer, length,
  45778. + " sock pid socket next prev e n p sndbf Flags Type St\n");
  45779. +#ifdef CONFIG_KLIPS_DEBUG
  45780. + } else {
  45781. + len += ipsec_snprintf(buffer, length,
  45782. + " sock pid d sleep socket next prev e r z n p sndbf stamp Flags Type St\n");
  45783. + }
  45784. +#endif /* CONFIG_KLIPS_DEBUG */
  45785. +
  45786. + sk_for_each(sk, node, &pfkey_sock_list) {
  45787. +
  45788. +#ifdef CONFIG_KLIPS_DEBUG
  45789. + if(!sysctl_ipsec_debug_verbose) {
  45790. +#endif /* CONFIG_KLIPS_DEBUG */
  45791. + len += ipsec_snprintf(buffer+len, length-len,
  45792. + "%8p %5d %8p %d %d %5d %08lX %8X %2X\n",
  45793. + sk,
  45794. + key_pid(sk),
  45795. + sk->sk_socket,
  45796. + sk->sk_err,
  45797. + sk->sk_protocol,
  45798. + sk->sk_sndbuf,
  45799. + sk->sk_socket->flags,
  45800. + sk->sk_socket->type,
  45801. + sk->sk_socket->state);
  45802. +#ifdef CONFIG_KLIPS_DEBUG
  45803. + } else {
  45804. + len += ipsec_snprintf(buffer+len, length-len,
  45805. + "%8p %5d %d %8p %8p %d %d %d %d %5d %d.%06d %08lX %8X %2X\n",
  45806. + sk,
  45807. + key_pid(sk),
  45808. + sock_flag(sk, SOCK_DEAD),
  45809. + sk->sk_sleep,
  45810. + sk->sk_socket,
  45811. + sk->sk_err,
  45812. + sk->sk_reuse,
  45813. +#ifdef HAVE_SOCK_ZAPPED
  45814. + sock_flag(sk, SOCK_ZAPPED),
  45815. +#else
  45816. + sk->sk_zapped,
  45817. +#endif
  45818. + sk->sk_protocol,
  45819. + sk->sk_sndbuf,
  45820. + (unsigned int)sk->sk_stamp.tv_sec,
  45821. + (unsigned int)sk->sk_stamp.tv_usec,
  45822. + sk->sk_socket->flags,
  45823. + sk->sk_socket->type,
  45824. + sk->sk_socket->state);
  45825. + }
  45826. +#endif /* CONFIG_KLIPS_DEBUG */
  45827. +
  45828. + if (len >= max_content) {
  45829. + /* we've done all that can fit -- stop loop */
  45830. + len = max_content; /* truncate crap */
  45831. + break;
  45832. + } else {
  45833. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45834. +
  45835. + if (pos <= offset) {
  45836. + /* all is before first interesting character:
  45837. + * discard, but note where we are.
  45838. + */
  45839. + len = 0;
  45840. + begin = pos;
  45841. + }
  45842. + }
  45843. + }
  45844. +
  45845. + *start = buffer + (offset - begin); /* Start of wanted data */
  45846. + return len - (offset - begin);
  45847. +}
  45848. +
  45849. +#ifndef PROC_FS_2325
  45850. +DEBUG_NO_STATIC
  45851. +#endif /* PROC_FS_2325 */
  45852. +int
  45853. +pfkey_supported_get_info(char *buffer, char **start, off_t offset, int length
  45854. +#ifndef PROC_NO_DUMMY
  45855. +, int dummy
  45856. +#endif /* !PROC_NO_DUMMY */
  45857. +)
  45858. +{
  45859. + /* limit of useful snprintf output */
  45860. + const int max_content = length > 0? length-1 : 0;
  45861. + off_t begin=0;
  45862. + int len=0;
  45863. + int satype;
  45864. + struct supported_list *ps;
  45865. +
  45866. + len += ipsec_snprintf(buffer, length,
  45867. + "satype exttype alg_id ivlen minbits maxbits name\n");
  45868. +
  45869. + for(satype = SADB_SATYPE_UNSPEC; satype <= SADB_SATYPE_MAX; satype++) {
  45870. + ps = pfkey_supported_list[satype];
  45871. + while(ps) {
  45872. + struct ipsec_alg_supported *alg = ps->supportedp;
  45873. + unsigned char *n = alg->ias_name;
  45874. + if(n == NULL) n = "unknown";
  45875. +
  45876. + len += ipsec_snprintf(buffer+len, length-len,
  45877. + " %2d %2d %2d %3d %3d %3d %20s\n",
  45878. + satype,
  45879. + alg->ias_exttype,
  45880. + alg->ias_id,
  45881. + alg->ias_ivlen,
  45882. + alg->ias_keyminbits,
  45883. + alg->ias_keymaxbits,
  45884. + n);
  45885. +
  45886. + if (len >= max_content) {
  45887. + /* we've done all that can fit -- stop loop */
  45888. + len = max_content; /* truncate crap */
  45889. + break;
  45890. + } else {
  45891. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45892. +
  45893. + if (pos <= offset) {
  45894. + /* all is before first interesting character:
  45895. + * discard, but note where we are.
  45896. + */
  45897. + len = 0;
  45898. + begin = pos;
  45899. + }
  45900. + }
  45901. +
  45902. + ps = ps->next;
  45903. + }
  45904. + }
  45905. + *start = buffer + (offset - begin); /* Start of wanted data */
  45906. + return len - (offset - begin);
  45907. +}
  45908. +
  45909. +#ifndef PROC_FS_2325
  45910. +DEBUG_NO_STATIC
  45911. +#endif /* PROC_FS_2325 */
  45912. +int
  45913. +pfkey_registered_get_info(char *buffer, char **start, off_t offset, int length
  45914. +#ifndef PROC_NO_DUMMY
  45915. +, int dummy
  45916. +#endif /* !PROC_NO_DUMMY */
  45917. +)
  45918. +{
  45919. + const int max_content = length > 0? length-1 : 0; /* limit of useful snprintf output */
  45920. + off_t begin=0;
  45921. + int len=0;
  45922. + int satype;
  45923. + struct socket_list *pfkey_sockets;
  45924. +
  45925. + len += ipsec_snprintf(buffer, length,
  45926. + "satype socket pid sk\n");
  45927. +
  45928. + for(satype = SADB_SATYPE_UNSPEC; satype <= SADB_SATYPE_MAX; satype++) {
  45929. + pfkey_sockets = pfkey_registered_sockets[satype];
  45930. + while(pfkey_sockets) {
  45931. + len += ipsec_snprintf(buffer+len, length-len,
  45932. + " %2d %8p %5d %8p\n",
  45933. + satype,
  45934. + pfkey_sockets->socketp,
  45935. + key_pid(pfkey_sockets->socketp->sk),
  45936. + pfkey_sockets->socketp->sk);
  45937. +
  45938. + if (len >= max_content) {
  45939. + /* we've done all that can fit -- stop loop (could stop two) */
  45940. + len = max_content; /* truncate crap */
  45941. + break;
  45942. + } else {
  45943. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45944. +
  45945. + if (pos <= offset) {
  45946. + /* all is before first interesting character:
  45947. + * discard, but note where we are.
  45948. + */
  45949. + len = 0;
  45950. + begin = pos;
  45951. + }
  45952. + }
  45953. +
  45954. + pfkey_sockets = pfkey_sockets->next;
  45955. + }
  45956. + }
  45957. + *start = buffer + (offset - begin); /* Start of wanted data */
  45958. + return len - (offset - begin);
  45959. +}
  45960. +
  45961. +#ifndef PROC_FS_2325
  45962. +struct proc_dir_entry proc_net_pfkey =
  45963. +{
  45964. + 0,
  45965. + 6, "pf_key",
  45966. + S_IFREG | S_IRUGO, 1, 0, 0,
  45967. + 0, &proc_net_inode_operations,
  45968. + pfkey_get_info
  45969. +};
  45970. +struct proc_dir_entry proc_net_pfkey_supported =
  45971. +{
  45972. + 0,
  45973. + 16, "pf_key_supported",
  45974. + S_IFREG | S_IRUGO, 1, 0, 0,
  45975. + 0, &proc_net_inode_operations,
  45976. + pfkey_supported_get_info
  45977. +};
  45978. +struct proc_dir_entry proc_net_pfkey_registered =
  45979. +{
  45980. + 0,
  45981. + 17, "pf_key_registered",
  45982. + S_IFREG | S_IRUGO, 1, 0, 0,
  45983. + 0, &proc_net_inode_operations,
  45984. + pfkey_registered_get_info
  45985. +};
  45986. +#endif /* !PROC_FS_2325 */
  45987. +#endif /* CONFIG_PROC_FS */
  45988. +
  45989. +DEBUG_NO_STATIC int
  45990. +supported_add_all(int satype, struct ipsec_alg_supported supported[], int size)
  45991. +{
  45992. + int i;
  45993. + int error = 0;
  45994. +
  45995. + KLIPS_PRINT(debug_pfkey,
  45996. + "klips_debug:init_pfkey: "
  45997. + "sizeof(supported_init_<satype=%d>)[%d]/sizeof(struct ipsec_alg_supported)[%d]=%d.\n",
  45998. + satype,
  45999. + size,
  46000. + (int)sizeof(struct ipsec_alg_supported),
  46001. + (int)(size/sizeof(struct ipsec_alg_supported)));
  46002. +
  46003. + for(i = 0; i < size / sizeof(struct ipsec_alg_supported); i++) {
  46004. +
  46005. + unsigned char *n = supported[i].ias_name;
  46006. + if(n == NULL) n="unknown";
  46007. +
  46008. + KLIPS_PRINT(debug_pfkey,
  46009. + "klips_debug:init_pfkey: "
  46010. + "i=%d inserting satype=%d exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d name=%s.\n",
  46011. + i,
  46012. + satype,
  46013. + supported[i].ias_exttype,
  46014. + supported[i].ias_id,
  46015. + supported[i].ias_ivlen,
  46016. + supported[i].ias_keyminbits,
  46017. + supported[i].ias_keymaxbits,
  46018. + n);
  46019. +
  46020. + error |= pfkey_list_insert_supported(&(supported[i]),
  46021. + &(pfkey_supported_list[satype]));
  46022. + }
  46023. + return error;
  46024. +}
  46025. +
  46026. +DEBUG_NO_STATIC int
  46027. +supported_remove_all(int satype)
  46028. +{
  46029. + int error = 0;
  46030. + struct ipsec_alg_supported*supportedp;
  46031. +
  46032. + while(pfkey_supported_list[satype]) {
  46033. + unsigned char *n;
  46034. + supportedp = pfkey_supported_list[satype]->supportedp;
  46035. +
  46036. + n = supportedp->ias_name;
  46037. + if(n == NULL) n="unknown";
  46038. +
  46039. + KLIPS_PRINT(debug_pfkey,
  46040. + "klips_debug:init_pfkey: "
  46041. + "removing satype=%d exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d name=%s.\n",
  46042. + satype,
  46043. + supportedp->ias_exttype,
  46044. + supportedp->ias_id,
  46045. + supportedp->ias_ivlen,
  46046. + supportedp->ias_keyminbits,
  46047. + supportedp->ias_keymaxbits, n);
  46048. +
  46049. + error |= pfkey_list_remove_supported(supportedp,
  46050. + &(pfkey_supported_list[satype]));
  46051. + }
  46052. + return error;
  46053. +}
  46054. +
  46055. +int
  46056. +pfkey_init(void)
  46057. +{
  46058. + int error = 0;
  46059. + int i;
  46060. +
  46061. + static struct ipsec_alg_supported supported_init_ah[] = {
  46062. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  46063. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_MD5HMAC, 0, 128, 128},
  46064. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  46065. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  46066. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_SHA1HMAC, 0, 160, 160}
  46067. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  46068. + };
  46069. + static struct ipsec_alg_supported supported_init_esp[] = {
  46070. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  46071. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_MD5HMAC, 0, 128, 128},
  46072. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  46073. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  46074. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_SHA1HMAC, 0, 160, 160},
  46075. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  46076. +#ifdef CONFIG_KLIPS_ENC_3DES
  46077. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_EALG_3DESCBC, 64, 168, 168},
  46078. +#endif /* CONFIG_KLIPS_ENC_3DES */
  46079. + };
  46080. + static struct ipsec_alg_supported supported_init_ipip[] = {
  46081. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv4_in_IPv4, 0, 32, 32}
  46082. +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46083. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv6_in_IPv4, 0, 128, 32}
  46084. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv4_in_IPv6, 0, 32, 128}
  46085. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv6_in_IPv6, 0, 128, 128}
  46086. +#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  46087. + };
  46088. +#ifdef CONFIG_KLIPS_IPCOMP
  46089. + static struct ipsec_alg_supported supported_init_ipcomp[] = {
  46090. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_CALG_DEFLATE, 0, 1, 1}
  46091. + };
  46092. +#endif /* CONFIG_KLIPS_IPCOMP */
  46093. +
  46094. +#if 0
  46095. + printk(KERN_INFO
  46096. + "klips_info:pfkey_init: "
  46097. + "FreeS/WAN: initialising PF_KEYv2 domain sockets.\n");
  46098. +#endif
  46099. +
  46100. + for(i = SADB_SATYPE_UNSPEC; i <= SADB_SATYPE_MAX; i++) {
  46101. + pfkey_registered_sockets[i] = NULL;
  46102. + pfkey_supported_list[i] = NULL;
  46103. + }
  46104. +
  46105. + error |= supported_add_all(SADB_SATYPE_AH, supported_init_ah, sizeof(supported_init_ah));
  46106. + error |= supported_add_all(SADB_SATYPE_ESP, supported_init_esp, sizeof(supported_init_esp));
  46107. +#ifdef CONFIG_KLIPS_IPCOMP
  46108. + error |= supported_add_all(SADB_X_SATYPE_COMP, supported_init_ipcomp, sizeof(supported_init_ipcomp));
  46109. +#endif /* CONFIG_KLIPS_IPCOMP */
  46110. + error |= supported_add_all(SADB_X_SATYPE_IPIP, supported_init_ipip, sizeof(supported_init_ipip));
  46111. +
  46112. + error |= sock_register(&pfkey_family_ops);
  46113. +
  46114. +#ifdef CONFIG_PROC_FS
  46115. +# ifndef PROC_FS_2325
  46116. +# ifdef PROC_FS_21
  46117. + error |= proc_register(proc_net, &proc_net_pfkey);
  46118. + error |= proc_register(proc_net, &proc_net_pfkey_supported);
  46119. + error |= proc_register(proc_net, &proc_net_pfkey_registered);
  46120. +# else /* PROC_FS_21 */
  46121. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey);
  46122. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey_supported);
  46123. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey_registered);
  46124. +# endif /* PROC_FS_21 */
  46125. +# else /* !PROC_FS_2325 */
  46126. + proc_net_create ("pf_key", 0, pfkey_get_info);
  46127. + proc_net_create ("pf_key_supported", 0, pfkey_supported_get_info);
  46128. + proc_net_create ("pf_key_registered", 0, pfkey_registered_get_info);
  46129. +# endif /* !PROC_FS_2325 */
  46130. +#endif /* CONFIG_PROC_FS */
  46131. +
  46132. + return error;
  46133. +}
  46134. +
  46135. +int
  46136. +pfkey_cleanup(void)
  46137. +{
  46138. + int error = 0;
  46139. +
  46140. + printk(KERN_INFO "klips_info:pfkey_cleanup: "
  46141. + "shutting down PF_KEY domain sockets.\n");
  46142. + error |= sock_unregister(PF_KEY);
  46143. +
  46144. + error |= supported_remove_all(SADB_SATYPE_AH);
  46145. + error |= supported_remove_all(SADB_SATYPE_ESP);
  46146. +#ifdef CONFIG_KLIPS_IPCOMP
  46147. + error |= supported_remove_all(SADB_X_SATYPE_COMP);
  46148. +#endif /* CONFIG_KLIPS_IPCOMP */
  46149. + error |= supported_remove_all(SADB_X_SATYPE_IPIP);
  46150. +
  46151. +#ifdef CONFIG_PROC_FS
  46152. +# ifndef PROC_FS_2325
  46153. + if (proc_net_unregister(proc_net_pfkey.low_ino) != 0)
  46154. + printk("klips_debug:pfkey_cleanup: "
  46155. + "cannot unregister /proc/net/pf_key\n");
  46156. + if (proc_net_unregister(proc_net_pfkey_supported.low_ino) != 0)
  46157. + printk("klips_debug:pfkey_cleanup: "
  46158. + "cannot unregister /proc/net/pf_key_supported\n");
  46159. + if (proc_net_unregister(proc_net_pfkey_registered.low_ino) != 0)
  46160. + printk("klips_debug:pfkey_cleanup: "
  46161. + "cannot unregister /proc/net/pf_key_registered\n");
  46162. +# else /* !PROC_FS_2325 */
  46163. + proc_net_remove ("pf_key");
  46164. + proc_net_remove ("pf_key_supported");
  46165. + proc_net_remove ("pf_key_registered");
  46166. +# endif /* !PROC_FS_2325 */
  46167. +#endif /* CONFIG_PROC_FS */
  46168. +
  46169. + /* other module unloading cleanup happens here */
  46170. + return error;
  46171. +}
  46172. +
  46173. +#ifdef MODULE
  46174. +#if 0
  46175. +int
  46176. +init_module(void)
  46177. +{
  46178. + pfkey_init();
  46179. + return 0;
  46180. +}
  46181. +
  46182. +void
  46183. +cleanup_module(void)
  46184. +{
  46185. + pfkey_cleanup();
  46186. +}
  46187. +#endif /* 0 */
  46188. +#else /* MODULE */
  46189. +struct net_protocol;
  46190. +void pfkey_proto_init(struct net_protocol *pro)
  46191. +{
  46192. + pfkey_init();
  46193. +}
  46194. +#endif /* MODULE */
  46195. +
  46196. +/*
  46197. + * $Log: pfkey_v2.c,v $
  46198. + * Revision 1.97.2.10 2006/10/10 20:43:28 paul
  46199. + * Add family/create/owner for pfkey_family_ops. This fixes bug #671
  46200. + *
  46201. + * Revision 1.97.2.9 2006/10/06 21:39:26 paul
  46202. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  46203. + * set. This is defined through autoconf.h which is included through the
  46204. + * linux kernel build macros.
  46205. + *
  46206. + * Revision 1.97.2.8 2006/07/10 15:56:11 paul
  46207. + * Fix for bug #642 by Bart.
  46208. + *
  46209. + * Revision 1.97.2.7 2006/04/04 11:34:19 ken
  46210. + * Backport SMP fixes + #ifdef cleanup from #public
  46211. + *
  46212. + * Revision 1.97.2.6 2006/02/15 05:00:20 paul
  46213. + * Fix for crasher on 2.6.12+ with klips (mostly seen on redhat kernels)
  46214. + *
  46215. + * Revision 1.97.2.5 2005/11/22 04:11:52 ken
  46216. + * Backport fixes for 2.6.14 kernels from HEAD
  46217. + *
  46218. + * Revision 1.97.2.4 2005/09/14 16:40:45 mcr
  46219. + * pull up of compilation on 2.4
  46220. + *
  46221. + * Revision 1.97.2.3 2005/09/06 02:10:03 mcr
  46222. + * pulled up possible SMP-related compilation fix
  46223. + *
  46224. + * Revision 1.97.2.2 2005/08/28 01:21:12 paul
  46225. + * Undid Ken's gcc4 fix in version 1.94 since it breaks linking KLIPS on
  46226. + * SMP kernels.
  46227. + *
  46228. + * Revision 1.97.2.1 2005/08/27 23:40:00 paul
  46229. + * recommited HAVE_SOCK_SECURITY fixes for linux 2.6.13
  46230. + *
  46231. + * Revision 1.102 2005/09/14 16:37:23 mcr
  46232. + * fix to compile on 2.4.
  46233. + *
  46234. + * Revision 1.101 2005/09/06 01:42:25 mcr
  46235. + * removed additional SOCKOPS_WRAPPED code
  46236. + *
  46237. + * Revision 1.100 2005/08/30 18:10:15 mcr
  46238. + * remove SOCKOPS_WRAPPED() code, add proper locking to the
  46239. + * pfkey code. (cross fingers)
  46240. + *
  46241. + * Revision 1.99 2005/08/28 01:53:37 paul
  46242. + * Undid Ken's gcc4 fix in version 1.94 since it breaks linking KLIPS on SMP kernels.
  46243. + *
  46244. + * Revision 1.98 2005/08/27 23:07:21 paul
  46245. + * Somewhere between 2.6.12 and 2.6.13rc7 the unused security memnber in sk_buff
  46246. + * has been removed. This patch should fix compilation for both cases.
  46247. + *
  46248. + * Revision 1.97 2005/07/20 00:33:36 mcr
  46249. + * fixed typo in #ifdef for SKALLOC.
  46250. + *
  46251. + * Revision 1.96 2005/07/19 20:02:15 mcr
  46252. + * sk_alloc() interface change.
  46253. + *
  46254. + * Revision 1.95 2005/07/09 00:40:06 ken
  46255. + * Fix for GCC4 - it doesn't like the potential for duplicate declaration
  46256. + *
  46257. + * Revision 1.94 2005/07/09 00:14:04 ken
  46258. + * Casts for 64bit cleanliness
  46259. + *
  46260. + * Revision 1.93 2005/07/08 16:20:05 mcr
  46261. + * fix for 2.6.12 disapperance of sk_zapped field -> sock_flags.
  46262. + *
  46263. + * Revision 1.92 2005/05/21 03:29:39 mcr
  46264. + * fixed missing prototype definition.
  46265. + *
  46266. + * Revision 1.91 2005/05/11 01:43:45 mcr
  46267. + * removed "poor-man"s OOP in favour of proper C structures.
  46268. + *
  46269. + * Revision 1.90 2005/05/02 18:42:47 mcr
  46270. + * fix for cut&paste error with pfkey_v2.c "supported_name"
  46271. + *
  46272. + * Revision 1.89 2005/05/01 03:12:31 mcr
  46273. + * print name if it is available.
  46274. + *
  46275. + * Revision 1.88 2005/04/29 05:10:22 mcr
  46276. + * removed from extraenous includes to make unit testing easier.
  46277. + *
  46278. + * Revision 1.87 2005/04/15 19:57:10 mcr
  46279. + * make sure that address has 0p so that it will
  46280. + * sanitized.
  46281. + *
  46282. + * Revision 1.86 2005/04/08 18:28:36 mcr
  46283. + * some minor #ifdef simplification in pursuit of a possible bug.
  46284. + *
  46285. + * Revision 1.85 2004/12/03 21:25:57 mcr
  46286. + * compile time fixes for running on 2.6.
  46287. + * still experimental.
  46288. + *
  46289. + * Revision 1.84 2004/08/17 03:27:23 mcr
  46290. + * klips 2.6 edits.
  46291. + *
  46292. + * Revision 1.83 2004/08/04 15:57:07 mcr
  46293. + * moved des .h files to include/des/ *
  46294. + * included 2.6 protocol specific things
  46295. + * started at NAT-T support, but it will require a kernel patch.
  46296. + *
  46297. + * Revision 1.82 2004/07/10 19:11:18 mcr
  46298. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  46299. + *
  46300. + * Revision 1.81 2004/04/25 21:23:11 ken
  46301. + * Pull in dhr's changes from FreeS/WAN 2.06
  46302. + *
  46303. + * Revision 1.80 2004/04/06 02:49:26 mcr
  46304. + * pullup of algo code from alg-branch.
  46305. + *
  46306. + * Revision 1.79.4.1 2003/12/22 15:25:52 jjo
  46307. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  46308. + *
  46309. + * Revision 1.79 2003/10/31 02:27:55 mcr
  46310. + * pulled up port-selector patches and sa_id elimination.
  46311. + *
  46312. + * Revision 1.78.4.1 2003/10/29 01:30:41 mcr
  46313. + * elimited "struct sa_id".
  46314. + *
  46315. + * Revision 1.78 2003/04/03 17:38:09 rgb
  46316. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  46317. + *
  46318. + * Revision 1.77 2002/10/17 16:49:36 mcr
  46319. + * sock->ops should reference the unwrapped options so that
  46320. + * we get hacked in locking on SMP systems.
  46321. + *
  46322. + * Revision 1.76 2002/10/12 23:11:53 dhr
  46323. + *
  46324. + * [KenB + DHR] more 64-bit cleanup
  46325. + *
  46326. + * Revision 1.75 2002/09/20 05:01:57 rgb
  46327. + * Added memory allocation debugging.
  46328. + *
  46329. + * Revision 1.74 2002/09/19 02:42:50 mcr
  46330. + * do not define the pfkey_ops function for now.
  46331. + *
  46332. + * Revision 1.73 2002/09/17 17:29:23 mcr
  46333. + * #if 0 out some dead code - pfkey_ops is never used as written.
  46334. + *
  46335. + * Revision 1.72 2002/07/24 18:44:54 rgb
  46336. + * Type fiddling to tame ia64 compiler.
  46337. + *
  46338. + * Revision 1.71 2002/05/23 07:14:11 rgb
  46339. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  46340. + *
  46341. + * Revision 1.70 2002/04/24 07:55:32 mcr
  46342. + * #include patches and Makefiles for post-reorg compilation.
  46343. + *
  46344. + * Revision 1.69 2002/04/24 07:36:33 mcr
  46345. + * Moved from ./klips/net/ipsec/pfkey_v2.c,v
  46346. + *
  46347. + * Revision 1.68 2002/03/08 01:15:17 mcr
  46348. + * put some internal structure only debug messages behind
  46349. + * && sysctl_ipsec_debug_verbose.
  46350. + *
  46351. + * Revision 1.67 2002/01/29 17:17:57 mcr
  46352. + * moved include of ipsec_param.h to after include of linux/kernel.h
  46353. + * otherwise, it seems that some option that is set in ipsec_param.h
  46354. + * screws up something subtle in the include path to kernel.h, and
  46355. + * it complains on the snprintf() prototype.
  46356. + *
  46357. + * Revision 1.66 2002/01/29 04:00:54 mcr
  46358. + * more excise of kversions.h header.
  46359. + *
  46360. + * Revision 1.65 2002/01/29 02:13:18 mcr
  46361. + * introduction of ipsec_kversion.h means that include of
  46362. + * ipsec_param.h must preceed any decisions about what files to
  46363. + * include to deal with differences in kernel source.
  46364. + *
  46365. + * Revision 1.64 2001/11/26 09:23:51 rgb
  46366. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  46367. + *
  46368. + * Revision 1.61.2.1 2001/09/25 02:28:44 mcr
  46369. + * cleaned up includes.
  46370. + *
  46371. + * Revision 1.63 2001/11/12 19:38:00 rgb
  46372. + * Continue trying other sockets even if one fails and return only original
  46373. + * error.
  46374. + *
  46375. + * Revision 1.62 2001/10/18 04:45:22 rgb
  46376. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  46377. + * lib/freeswan.h version macros moved to lib/kversions.h.
  46378. + * Other compiler directive cleanups.
  46379. + *
  46380. + * Revision 1.61 2001/09/20 15:32:59 rgb
  46381. + * Min/max cleanup.
  46382. + *
  46383. + * Revision 1.60 2001/06/14 19:35:12 rgb
  46384. + * Update copyright date.
  46385. + *
  46386. + * Revision 1.59 2001/06/13 15:35:48 rgb
  46387. + * Fixed #endif comments.
  46388. + *
  46389. + * Revision 1.58 2001/05/04 16:37:24 rgb
  46390. + * Remove erroneous checking of return codes for proc_net_* in 2.4.
  46391. + *
  46392. + * Revision 1.57 2001/05/03 19:43:36 rgb
  46393. + * Initialise error return variable.
  46394. + * Check error return codes in startup and shutdown.
  46395. + * Standardise on SENDERR() macro.
  46396. + *
  46397. + * Revision 1.56 2001/04/21 23:05:07 rgb
  46398. + * Define out skb->used for 2.4 kernels.
  46399. + *
  46400. + * Revision 1.55 2001/02/28 05:03:28 rgb
  46401. + * Clean up and rationalise startup messages.
  46402. + *
  46403. + * Revision 1.54 2001/02/27 22:24:55 rgb
  46404. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  46405. + * Check for satoa() return codes.
  46406. + *
  46407. + * Revision 1.53 2001/02/27 06:48:18 rgb
  46408. + * Fixed pfkey socket unregister log message to reflect type and function.
  46409. + *
  46410. + * Revision 1.52 2001/02/26 22:34:38 rgb
  46411. + * Fix error return code that was getting overwritten by the error return
  46412. + * code of an upmsg.
  46413. + *
  46414. + * Revision 1.51 2001/01/30 23:42:47 rgb
  46415. + * Allow pfkey msgs from pid other than user context required for ACQUIRE
  46416. + * and subsequent ADD or UDATE.
  46417. + *
  46418. + * Revision 1.50 2001/01/23 20:22:59 rgb
  46419. + * 2.4 fix to remove removed is_clone member.
  46420. + *
  46421. + * Revision 1.49 2000/11/06 04:33:47 rgb
  46422. + * Changed non-exported functions to DEBUG_NO_STATIC.
  46423. + *
  46424. + * Revision 1.48 2000/09/29 19:47:41 rgb
  46425. + * Update copyright.
  46426. + *
  46427. + * Revision 1.47 2000/09/22 04:23:04 rgb
  46428. + * Added more debugging to pfkey_upmsg() call from pfkey_sendmsg() error.
  46429. + *
  46430. + * Revision 1.46 2000/09/21 04:20:44 rgb
  46431. + * Fixed array size off-by-one error. (Thanks Svenning!)
  46432. + *
  46433. + * Revision 1.45 2000/09/20 04:01:26 rgb
  46434. + * Changed static functions to DEBUG_NO_STATIC for revealing function names
  46435. + * in oopsen.
  46436. + *
  46437. + * Revision 1.44 2000/09/19 00:33:17 rgb
  46438. + * 2.0 fixes.
  46439. + *
  46440. + * Revision 1.43 2000/09/16 01:28:13 rgb
  46441. + * Fixed use of 0 in p format warning.
  46442. + *
  46443. + * Revision 1.42 2000/09/16 01:09:41 rgb
  46444. + * Fixed debug format warning for pointers that was expecting ints.
  46445. + *
  46446. + * Revision 1.41 2000/09/13 15:54:00 rgb
  46447. + * Rewrote pfkey_get_info(), added pfkey_{supported,registered}_get_info().
  46448. + * Moved supported algos add and remove to functions.
  46449. + *
  46450. + * Revision 1.40 2000/09/12 18:49:28 rgb
  46451. + * Added IPIP tunnel and IPCOMP register support.
  46452. + *
  46453. + * Revision 1.39 2000/09/12 03:23:49 rgb
  46454. + * Converted #if0 debugs to sysctl.
  46455. + * Removed debug_pfkey initialisations that prevented no_debug loading or
  46456. + * linking.
  46457. + *
  46458. + * Revision 1.38 2000/09/09 06:38:02 rgb
  46459. + * Return positive errno in pfkey_reply error message.
  46460. + *
  46461. + * Revision 1.37 2000/09/08 19:19:09 rgb
  46462. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  46463. + * Clean-up of long-unused crud...
  46464. + * Create pfkey error message on on failure.
  46465. + * Give pfkey_list_{insert,remove}_{socket,supported}() some error
  46466. + * checking.
  46467. + *
  46468. + * Revision 1.36 2000/09/01 18:49:38 rgb
  46469. + * Reap experimental NET_21_ bits.
  46470. + * Turned registered sockets list into an array of one list per satype.
  46471. + * Remove references to deprecated sklist_{insert,remove}_socket.
  46472. + * Removed leaking socket debugging code.
  46473. + * Removed duplicate pfkey_insert_socket in pfkey_create.
  46474. + * Removed all references to pfkey msg->msg_name, since it is not used for
  46475. + * pfkey.
  46476. + * Added a supported algorithms array lists, one per satype and registered
  46477. + * existing algorithms.
  46478. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  46479. + * list.
  46480. + * Only send pfkey_expire() messages to sockets registered for that satype.
  46481. + *
  46482. + * Revision 1.35 2000/08/24 17:03:00 rgb
  46483. + * Corrected message size error return code for PF_KEYv2.
  46484. + * Removed downward error prohibition.
  46485. + *
  46486. + * Revision 1.34 2000/08/21 16:32:26 rgb
  46487. + * Re-formatted for cosmetic consistency and readability.
  46488. + *
  46489. + * Revision 1.33 2000/08/20 21:38:24 rgb
  46490. + * Added a pfkey_reply parameter to pfkey_msg_interp(). (Momchil)
  46491. + * Extended the upward message initiation of pfkey_sendmsg(). (Momchil)
  46492. + *
  46493. + * Revision 1.32 2000/07/28 14:58:31 rgb
  46494. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  46495. + *
  46496. + * Revision 1.31 2000/05/16 03:04:00 rgb
  46497. + * Updates for 2.3.99pre8 from MB.
  46498. + *
  46499. + * Revision 1.30 2000/05/10 19:22:21 rgb
  46500. + * Use sklist private functions for 2.3.xx compatibility.
  46501. + *
  46502. + * Revision 1.29 2000/03/22 16:17:03 rgb
  46503. + * Fixed SOCKOPS_WRAPPED macro for SMP (MB).
  46504. + *
  46505. + * Revision 1.28 2000/02/21 19:30:45 rgb
  46506. + * Removed references to pkt_bridged for 2.3.47 compatibility.
  46507. + *
  46508. + * Revision 1.27 2000/02/14 21:07:00 rgb
  46509. + * Fixed /proc/net/pf-key legend spacing.
  46510. + *
  46511. + * Revision 1.26 2000/01/22 03:46:59 rgb
  46512. + * Fixed pfkey error return mechanism so that we are able to free the
  46513. + * local copy of the pfkey_msg, plugging a memory leak and silencing
  46514. + * the bad object free complaints.
  46515. + *
  46516. + * Revision 1.25 2000/01/21 06:19:44 rgb
  46517. + * Moved pfkey_list_remove_socket() calls to before MOD_USE_DEC_COUNT.
  46518. + * Added debugging to pfkey_upmsg.
  46519. + *
  46520. + * Revision 1.24 2000/01/10 16:38:23 rgb
  46521. + * MB fixups for 2.3.x.
  46522. + *
  46523. + * Revision 1.23 1999/12/09 23:22:16 rgb
  46524. + * Added more instrumentation for debugging 2.0 socket
  46525. + * selection/reading.
  46526. + * Removed erroneous 2.0 wait==NULL check bug in select.
  46527. + *
  46528. + * Revision 1.22 1999/12/08 20:32:16 rgb
  46529. + * Tidied up 2.0.xx support, after major pfkey work, eliminating
  46530. + * msg->msg_name twiddling in the process, since it is not defined
  46531. + * for PF_KEYv2.
  46532. + *
  46533. + * Revision 1.21 1999/12/01 22:17:19 rgb
  46534. + * Set skb->dev to zero on new skb in case it is a reused skb.
  46535. + * Added check for skb_put overflow and freeing to avoid upmsg on error.
  46536. + * Added check for wrong pfkey version and freeing to avoid upmsg on
  46537. + * error.
  46538. + * Shut off content dumping in pfkey_destroy.
  46539. + * Added debugging message for size of buffer allocated for upmsg.
  46540. + *
  46541. + * Revision 1.20 1999/11/27 12:11:00 rgb
  46542. + * Minor clean-up, enabling quiet operation of pfkey if desired.
  46543. + *
  46544. + * Revision 1.19 1999/11/25 19:04:21 rgb
  46545. + * Update proc_fs code for pfkey to use dynamic registration.
  46546. + *
  46547. + * Revision 1.18 1999/11/25 09:07:17 rgb
  46548. + * Implemented SENDERR macro for propagating error codes.
  46549. + * Fixed error return code bug.
  46550. + *
  46551. + * Revision 1.17 1999/11/23 23:07:20 rgb
  46552. + * Change name of pfkey_msg_parser to pfkey_msg_interp since it no longer
  46553. + * parses. (PJO)
  46554. + * Sort out pfkey and freeswan headers, putting them in a library path.
  46555. + *
  46556. + * Revision 1.16 1999/11/20 22:00:22 rgb
  46557. + * Moved socketlist type declarations and prototypes for shared use.
  46558. + * Renamed reformatted and generically extended for use by other socket
  46559. + * lists pfkey_{del,add}_open_socket to pfkey_list_{remove,insert}_socket.
  46560. + *
  46561. + * Revision 1.15 1999/11/18 04:15:09 rgb
  46562. + * Make pfkey_data_ready temporarily available for 2.2.x testing.
  46563. + * Clean up pfkey_destroy_socket() debugging statements.
  46564. + * Add Peter Onion's code to send messages up to all listening sockets.
  46565. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  46566. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  46567. + * klips/net/ipsec/Makefile.
  46568. + * Replaced all kernel version macros to shorter, readable form.
  46569. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  46570. + *
  46571. + * Revision 1.14 1999/11/17 16:01:00 rgb
  46572. + * Make pfkey_data_ready temporarily available for 2.2.x testing.
  46573. + * Clean up pfkey_destroy_socket() debugging statements.
  46574. + * Add Peter Onion's code to send messages up to all listening sockets.
  46575. + * Changed #include "../../../lib/freeswan.h" to #include <freeswan.h>
  46576. + * which works due to -Ilibfreeswan in the klips/net/ipsec/Makefile.
  46577. + *
  46578. + * Revision 1.13 1999/10/27 19:59:51 rgb
  46579. + * Removed af_unix comments that are no longer relevant.
  46580. + * Added debug prink statements.
  46581. + * Added to the /proc output in pfkey_get_info.
  46582. + * Made most functions non-static to enable oops tracing.
  46583. + * Re-enable skb dequeueing and freeing.
  46584. + * Fix skb_alloc() and skb_put() size bug in pfkey_upmsg().
  46585. + *
  46586. + * Revision 1.12 1999/10/26 17:05:42 rgb
  46587. + * Complete re-ordering based on proto_ops structure order.
  46588. + * Separated out proto_ops structures for 2.0.x and 2.2.x for clarity.
  46589. + * Simplification to use built-in socket ops where possible for 2.2.x.
  46590. + * Add shorter macros for compiler directives to visually clean-up.
  46591. + * Add lots of sk skb dequeueing debugging statements.
  46592. + * Added to the /proc output in pfkey_get_info.
  46593. + *
  46594. + * Revision 1.11 1999/09/30 02:55:10 rgb
  46595. + * Bogus skb detection.
  46596. + * Fix incorrect /proc/net/ipsec-eroute printk message.
  46597. + *
  46598. + * Revision 1.10 1999/09/21 15:22:13 rgb
  46599. + * Temporary fix while I figure out the right way to destroy sockets.
  46600. + *
  46601. + * Revision 1.9 1999/07/08 19:19:44 rgb
  46602. + * Fix pointer format warning.
  46603. + * Fix missing member error under 2.0.xx kernels.
  46604. + *
  46605. + * Revision 1.8 1999/06/13 07:24:04 rgb
  46606. + * Add more debugging.
  46607. + *
  46608. + * Revision 1.7 1999/06/10 05:24:17 rgb
  46609. + * Clarified compiler directives.
  46610. + * Renamed variables to reduce confusion.
  46611. + * Used sklist_*_socket() kernel functions to simplify 2.2.x socket support.
  46612. + * Added lots of sanity checking.
  46613. + *
  46614. + * Revision 1.6 1999/06/03 18:59:50 rgb
  46615. + * More updates to 2.2.x socket support. Almost works, oops at end of call.
  46616. + *
  46617. + * Revision 1.5 1999/05/25 22:44:05 rgb
  46618. + * Start fixing 2.2 sockets.
  46619. + *
  46620. + * Revision 1.4 1999/04/29 15:21:34 rgb
  46621. + * Move log to the end of the file.
  46622. + * Eliminate min/max redefinition in #include <net/tcp.h>.
  46623. + * Correct path for pfkey #includes
  46624. + * Standardise an error return method.
  46625. + * Add debugging instrumentation.
  46626. + * Move message type checking to pfkey_msg_parse().
  46627. + * Add check for errno incorrectly set.
  46628. + * Add check for valid PID.
  46629. + * Add check for reserved illegally set.
  46630. + * Add check for message out of bounds.
  46631. + *
  46632. + * Revision 1.3 1999/04/15 17:58:07 rgb
  46633. + * Add RCSID labels.
  46634. + *
  46635. + * Revision 1.2 1999/04/15 15:37:26 rgb
  46636. + * Forward check changes from POST1_00 branch.
  46637. + *
  46638. + * Revision 1.1.2.2 1999/04/13 20:37:12 rgb
  46639. + * Header Title correction.
  46640. + *
  46641. + * Revision 1.1.2.1 1999/03/26 20:58:55 rgb
  46642. + * Add pfkeyv2 support to KLIPS.
  46643. + *
  46644. + *
  46645. + * RFC 2367
  46646. + * PF_KEY_v2 Key Management API
  46647. + */
  46648. --- /dev/null Tue Mar 11 13:02:56 2003
  46649. +++ linux/net/ipsec/pfkey_v2_build.c Mon Feb 9 13:51:03 2004
  46650. @@ -0,0 +1,1581 @@
  46651. +/*
  46652. + * RFC2367 PF_KEYv2 Key management API message parser
  46653. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  46654. + *
  46655. + * This program is free software; you can redistribute it and/or modify it
  46656. + * under the terms of the GNU General Public License as published by the
  46657. + * Free Software Foundation; either version 2 of the License, or (at your
  46658. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  46659. + *
  46660. + * This program is distributed in the hope that it will be useful, but
  46661. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  46662. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  46663. + * for more details.
  46664. + *
  46665. + * RCSID $Id: pfkey_v2_build.c,v 1.51.8.1 2006/05/01 14:36:39 mcr Exp $
  46666. + */
  46667. +
  46668. +/*
  46669. + * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
  46670. + */
  46671. +
  46672. +char pfkey_v2_build_c_version[] = "$Id: pfkey_v2_build.c,v 1.51.8.1 2006/05/01 14:36:39 mcr Exp $";
  46673. +
  46674. +/*
  46675. + * Some ugly stuff to allow consistent debugging code for use in the
  46676. + * kernel and in user space
  46677. +*/
  46678. +
  46679. +#ifdef __KERNEL__
  46680. +
  46681. +# include <linux/kernel.h> /* for printk */
  46682. +
  46683. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  46684. +# ifdef MALLOC_SLAB
  46685. +# include <linux/slab.h> /* kmalloc() */
  46686. +# else /* MALLOC_SLAB */
  46687. +# include <linux/malloc.h> /* kmalloc() */
  46688. +# endif /* MALLOC_SLAB */
  46689. +# include <linux/errno.h> /* error codes */
  46690. +# include <linux/types.h> /* size_t */
  46691. +# include <linux/interrupt.h> /* mark_bh */
  46692. +
  46693. +# include <linux/netdevice.h> /* struct device, and other headers */
  46694. +# include <linux/etherdevice.h> /* eth_type_trans */
  46695. +# include <linux/ip.h> /* struct iphdr */
  46696. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46697. +# include <linux/ipv6.h> /* struct ipv6hdr */
  46698. +# endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  46699. +
  46700. +# define MALLOC(size) kmalloc(size, GFP_ATOMIC)
  46701. +# define FREE(obj) kfree(obj)
  46702. +# include <openswan.h>
  46703. +#else /* __KERNEL__ */
  46704. +
  46705. +# include <sys/types.h>
  46706. +# include <linux/types.h>
  46707. +# include <linux/errno.h>
  46708. +# include <malloc.h>
  46709. +# include <string.h> /* memset */
  46710. +
  46711. +# include <openswan.h>
  46712. +
  46713. +#endif /* __KERNEL__ */
  46714. +
  46715. +#include <pfkeyv2.h>
  46716. +#include <pfkey.h>
  46717. +
  46718. +#ifdef __KERNEL__
  46719. +#include "openswan/radij.h" /* rd_nodes */
  46720. +#include "openswan/ipsec_encap.h" /* sockaddr_encap */
  46721. +#endif /* __KERNEL__ */
  46722. +
  46723. +
  46724. +#include "openswan/ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
  46725. +#include "openswan/pfkey_debug.h"
  46726. +
  46727. +
  46728. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  46729. +
  46730. +void
  46731. +pfkey_extensions_init(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
  46732. +{
  46733. + int i;
  46734. +
  46735. + for (i = 0; i != SADB_EXT_MAX + 1; i++) {
  46736. + extensions[i] = NULL;
  46737. + }
  46738. +}
  46739. +
  46740. +void
  46741. +pfkey_extensions_free(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
  46742. +{
  46743. + int i;
  46744. +
  46745. + if(!extensions) {
  46746. + return;
  46747. + }
  46748. +
  46749. + if(extensions[0]) {
  46750. + memset(extensions[0], 0, sizeof(struct sadb_msg));
  46751. + FREE(extensions[0]);
  46752. + extensions[0] = NULL;
  46753. + }
  46754. +
  46755. + for (i = 1; i != SADB_EXT_MAX + 1; i++) {
  46756. + if(extensions[i]) {
  46757. + memset(extensions[i], 0, extensions[i]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  46758. + FREE(extensions[i]);
  46759. + extensions[i] = NULL;
  46760. + }
  46761. + }
  46762. +}
  46763. +
  46764. +void
  46765. +pfkey_msg_free(struct sadb_msg **pfkey_msg)
  46766. +{
  46767. + if(*pfkey_msg) {
  46768. + memset(*pfkey_msg, 0, (*pfkey_msg)->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  46769. + FREE(*pfkey_msg);
  46770. + *pfkey_msg = NULL;
  46771. + }
  46772. +}
  46773. +
  46774. +/* Default extension builders taken from the KLIPS code */
  46775. +
  46776. +int
  46777. +pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
  46778. + uint8_t msg_type,
  46779. + uint8_t satype,
  46780. + uint8_t msg_errno,
  46781. + uint32_t seq,
  46782. + uint32_t pid)
  46783. +{
  46784. + int error = 0;
  46785. + struct sadb_msg *pfkey_msg = (struct sadb_msg *)*pfkey_ext;
  46786. +
  46787. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46788. + "pfkey_msg_hdr_build:\n");
  46789. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46790. + "pfkey_msg_hdr_build: "
  46791. + "on_entry &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
  46792. + &pfkey_ext,
  46793. + pfkey_ext,
  46794. + *pfkey_ext);
  46795. + /* sanity checks... */
  46796. + if(pfkey_msg) {
  46797. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46798. + "pfkey_msg_hdr_build: "
  46799. + "why is pfkey_msg already pointing to something?\n");
  46800. + SENDERR(EINVAL);
  46801. + }
  46802. +
  46803. + if(!msg_type) {
  46804. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46805. + "pfkey_msg_hdr_build: "
  46806. + "msg type not set, must be non-zero..\n");
  46807. + SENDERR(EINVAL);
  46808. + }
  46809. +
  46810. + if(msg_type > SADB_MAX) {
  46811. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46812. + "pfkey_msg_hdr_build: "
  46813. + "msg type too large:%d.\n",
  46814. + msg_type);
  46815. + SENDERR(EINVAL);
  46816. + }
  46817. +
  46818. + if(satype > SADB_SATYPE_MAX) {
  46819. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46820. + "pfkey_msg_hdr_build: "
  46821. + "satype %d > max %d\n",
  46822. + satype, SADB_SATYPE_MAX);
  46823. + SENDERR(EINVAL);
  46824. + }
  46825. +
  46826. + pfkey_msg = (struct sadb_msg*)MALLOC(sizeof(struct sadb_msg));
  46827. + *pfkey_ext = (struct sadb_ext*)pfkey_msg;
  46828. +
  46829. + if(pfkey_msg == NULL) {
  46830. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46831. + "pfkey_msg_hdr_build: "
  46832. + "memory allocation failed\n");
  46833. + SENDERR(ENOMEM);
  46834. + }
  46835. + memset(pfkey_msg, 0, sizeof(struct sadb_msg));
  46836. +
  46837. + pfkey_msg->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  46838. +
  46839. + pfkey_msg->sadb_msg_type = msg_type;
  46840. + pfkey_msg->sadb_msg_satype = satype;
  46841. +
  46842. + pfkey_msg->sadb_msg_version = PF_KEY_V2;
  46843. + pfkey_msg->sadb_msg_errno = msg_errno;
  46844. + pfkey_msg->sadb_msg_reserved = 0;
  46845. + pfkey_msg->sadb_msg_seq = seq;
  46846. + pfkey_msg->sadb_msg_pid = pid;
  46847. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46848. + "pfkey_msg_hdr_build: "
  46849. + "on_exit &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
  46850. + &pfkey_ext,
  46851. + pfkey_ext,
  46852. + *pfkey_ext);
  46853. +errlab:
  46854. + return error;
  46855. +}
  46856. +
  46857. +int
  46858. +pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
  46859. + uint16_t exttype,
  46860. + uint32_t spi,
  46861. + uint8_t replay_window,
  46862. + uint8_t sa_state,
  46863. + uint8_t auth,
  46864. + uint8_t encrypt,
  46865. + uint32_t flags,
  46866. + uint32_t/*IPsecSAref_t*/ ref)
  46867. +{
  46868. + int error = 0;
  46869. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)*pfkey_ext;
  46870. +
  46871. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46872. + "pfkey_sa_build: "
  46873. + "spi=%08x replay=%d sa_state=%d auth=%d encrypt=%d flags=%d\n",
  46874. + ntohl(spi), /* in network order */
  46875. + replay_window,
  46876. + sa_state,
  46877. + auth,
  46878. + encrypt,
  46879. + flags);
  46880. + /* sanity checks... */
  46881. + if(pfkey_sa) {
  46882. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46883. + "pfkey_sa_build: "
  46884. + "why is pfkey_sa already pointing to something?\n");
  46885. + SENDERR(EINVAL);
  46886. + }
  46887. +
  46888. + if(exttype != SADB_EXT_SA &&
  46889. + exttype != SADB_X_EXT_SA2) {
  46890. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46891. + "pfkey_sa_build: "
  46892. + "invalid exttype=%d.\n",
  46893. + exttype);
  46894. + SENDERR(EINVAL);
  46895. + }
  46896. +
  46897. + if(replay_window > 64) {
  46898. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46899. + "pfkey_sa_build: "
  46900. + "replay window size: %d -- must be 0 <= size <= 64\n",
  46901. + replay_window);
  46902. + SENDERR(EINVAL);
  46903. + }
  46904. +
  46905. + if(auth > SADB_AALG_MAX) {
  46906. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46907. + "pfkey_sa_build: "
  46908. + "auth=%d > SADB_AALG_MAX=%d.\n",
  46909. + auth,
  46910. + SADB_AALG_MAX);
  46911. + SENDERR(EINVAL);
  46912. + }
  46913. +
  46914. +#if SADB_EALG_MAX < 255
  46915. + if(encrypt > SADB_EALG_MAX) {
  46916. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46917. + "pfkey_sa_build: "
  46918. + "encrypt=%d > SADB_EALG_MAX=%d.\n",
  46919. + encrypt,
  46920. + SADB_EALG_MAX);
  46921. + SENDERR(EINVAL);
  46922. + }
  46923. +#endif
  46924. +
  46925. + if(sa_state > SADB_SASTATE_MAX) {
  46926. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46927. + "pfkey_sa_build: "
  46928. + "sa_state=%d exceeds MAX=%d.\n",
  46929. + sa_state,
  46930. + SADB_SASTATE_MAX);
  46931. + SENDERR(EINVAL);
  46932. + }
  46933. +
  46934. + if(sa_state == SADB_SASTATE_DEAD) {
  46935. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46936. + "pfkey_sa_build: "
  46937. + "sa_state=%d is DEAD=%d is not allowed.\n",
  46938. + sa_state,
  46939. + SADB_SASTATE_DEAD);
  46940. + SENDERR(EINVAL);
  46941. + }
  46942. +
  46943. + if((IPSEC_SAREF_NULL != ref) && (ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
  46944. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46945. + "pfkey_sa_build: "
  46946. + "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
  46947. + ref,
  46948. + IPSEC_SAREF_NULL,
  46949. + IPSEC_SA_REF_TABLE_NUM_ENTRIES);
  46950. + SENDERR(EINVAL);
  46951. + }
  46952. +
  46953. + pfkey_sa = (struct sadb_sa*)MALLOC(sizeof(struct sadb_sa));
  46954. + *pfkey_ext = (struct sadb_ext*)pfkey_sa;
  46955. +
  46956. + if(pfkey_sa == NULL) {
  46957. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46958. + "pfkey_sa_build: "
  46959. + "memory allocation failed\n");
  46960. + SENDERR(ENOMEM);
  46961. + }
  46962. + memset(pfkey_sa, 0, sizeof(struct sadb_sa));
  46963. +
  46964. + pfkey_sa->sadb_sa_len = sizeof(*pfkey_sa) / IPSEC_PFKEYv2_ALIGN;
  46965. + pfkey_sa->sadb_sa_exttype = exttype;
  46966. + pfkey_sa->sadb_sa_spi = spi;
  46967. + pfkey_sa->sadb_sa_replay = replay_window;
  46968. + pfkey_sa->sadb_sa_state = sa_state;
  46969. + pfkey_sa->sadb_sa_auth = auth;
  46970. + pfkey_sa->sadb_sa_encrypt = encrypt;
  46971. + pfkey_sa->sadb_sa_flags = flags;
  46972. + pfkey_sa->sadb_x_sa_ref = ref;
  46973. +
  46974. +errlab:
  46975. + return error;
  46976. +}
  46977. +
  46978. +int
  46979. +pfkey_sa_build(struct sadb_ext ** pfkey_ext,
  46980. + uint16_t exttype,
  46981. + uint32_t spi,
  46982. + uint8_t replay_window,
  46983. + uint8_t sa_state,
  46984. + uint8_t auth,
  46985. + uint8_t encrypt,
  46986. + uint32_t flags)
  46987. +{
  46988. + return pfkey_sa_ref_build(pfkey_ext,
  46989. + exttype,
  46990. + spi,
  46991. + replay_window,
  46992. + sa_state,
  46993. + auth,
  46994. + encrypt,
  46995. + flags,
  46996. + IPSEC_SAREF_NULL);
  46997. +}
  46998. +
  46999. +int
  47000. +pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
  47001. + uint16_t exttype,
  47002. + uint32_t allocations,
  47003. + uint64_t bytes,
  47004. + uint64_t addtime,
  47005. + uint64_t usetime,
  47006. + uint32_t packets)
  47007. +{
  47008. + int error = 0;
  47009. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)*pfkey_ext;
  47010. +
  47011. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47012. + "pfkey_lifetime_build:\n");
  47013. + /* sanity checks... */
  47014. + if(pfkey_lifetime) {
  47015. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47016. + "pfkey_lifetime_build: "
  47017. + "why is pfkey_lifetime already pointing to something?\n");
  47018. + SENDERR(EINVAL);
  47019. + }
  47020. +
  47021. + if(exttype != SADB_EXT_LIFETIME_CURRENT &&
  47022. + exttype != SADB_EXT_LIFETIME_HARD &&
  47023. + exttype != SADB_EXT_LIFETIME_SOFT) {
  47024. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47025. + "pfkey_lifetime_build: "
  47026. + "invalid exttype=%d.\n",
  47027. + exttype);
  47028. + SENDERR(EINVAL);
  47029. + }
  47030. +
  47031. + pfkey_lifetime = (struct sadb_lifetime*)MALLOC(sizeof(struct sadb_lifetime));
  47032. + *pfkey_ext = (struct sadb_ext*) pfkey_lifetime;
  47033. +
  47034. + if(pfkey_lifetime == NULL) {
  47035. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47036. + "pfkey_lifetime_build: "
  47037. + "memory allocation failed\n");
  47038. + SENDERR(ENOMEM);
  47039. + }
  47040. + memset(pfkey_lifetime, 0, sizeof(struct sadb_lifetime));
  47041. +
  47042. + pfkey_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN;
  47043. + pfkey_lifetime->sadb_lifetime_exttype = exttype;
  47044. + pfkey_lifetime->sadb_lifetime_allocations = allocations;
  47045. + pfkey_lifetime->sadb_lifetime_bytes = bytes;
  47046. + pfkey_lifetime->sadb_lifetime_addtime = addtime;
  47047. + pfkey_lifetime->sadb_lifetime_usetime = usetime;
  47048. + pfkey_lifetime->sadb_x_lifetime_packets = packets;
  47049. +
  47050. +errlab:
  47051. + return error;
  47052. +}
  47053. +
  47054. +int
  47055. +pfkey_address_build(struct sadb_ext** pfkey_ext,
  47056. + uint16_t exttype,
  47057. + uint8_t proto,
  47058. + uint8_t prefixlen,
  47059. + struct sockaddr* address)
  47060. +{
  47061. + int error = 0;
  47062. + int saddr_len = 0;
  47063. + char ipaddr_txt[ADDRTOT_BUF + 6/*extra for port number*/];
  47064. + struct sadb_address *pfkey_address = (struct sadb_address *)*pfkey_ext;
  47065. +
  47066. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47067. + "pfkey_address_build: "
  47068. + "exttype=%d proto=%d prefixlen=%d\n",
  47069. + exttype,
  47070. + proto,
  47071. + prefixlen);
  47072. + /* sanity checks... */
  47073. + if(pfkey_address) {
  47074. + ERROR("pfkey_address_build: "
  47075. + "why is pfkey_address already pointing to something?\n");
  47076. + SENDERR(EINVAL);
  47077. + }
  47078. +
  47079. + if (!address) {
  47080. + ERROR("pfkey_address_build: " "address is NULL\n");
  47081. + SENDERR(EINVAL);
  47082. + }
  47083. +
  47084. + switch(exttype) {
  47085. + case SADB_EXT_ADDRESS_SRC:
  47086. + case SADB_EXT_ADDRESS_DST:
  47087. + case SADB_EXT_ADDRESS_PROXY:
  47088. + case SADB_X_EXT_ADDRESS_DST2:
  47089. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  47090. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  47091. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  47092. + case SADB_X_EXT_ADDRESS_DST_MASK:
  47093. +#ifdef NAT_TRAVERSAL
  47094. + case SADB_X_EXT_NAT_T_OA:
  47095. +#endif
  47096. + break;
  47097. + default:
  47098. + ERROR("pfkey_address_build: "
  47099. + "unrecognised ext_type=%d.\n",
  47100. + exttype);
  47101. + SENDERR(EINVAL);
  47102. + }
  47103. +
  47104. + switch(address->sa_family) {
  47105. + case AF_INET:
  47106. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47107. + "pfkey_address_build: "
  47108. + "found address family AF_INET.\n");
  47109. + saddr_len = sizeof(struct sockaddr_in);
  47110. + sprintf(ipaddr_txt, "%d.%d.%d.%d:%d"
  47111. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 0) & 0xFF
  47112. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 8) & 0xFF
  47113. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 16) & 0xFF
  47114. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 24) & 0xFF
  47115. + , ntohs(((struct sockaddr_in*)address)->sin_port));
  47116. + break;
  47117. + case AF_INET6:
  47118. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47119. + "pfkey_address_build: "
  47120. + "found address family AF_INET6.\n");
  47121. + saddr_len = sizeof(struct sockaddr_in6);
  47122. + sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x-%x"
  47123. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[0])
  47124. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[1])
  47125. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[2])
  47126. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[3])
  47127. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[4])
  47128. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[5])
  47129. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[6])
  47130. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[7])
  47131. + , ntohs(((struct sockaddr_in6*)address)->sin6_port));
  47132. + break;
  47133. + default:
  47134. + ERROR("pfkey_address_build: "
  47135. + "address->sa_family=%d not supported.\n",
  47136. + address->sa_family);
  47137. + SENDERR(EPFNOSUPPORT);
  47138. + }
  47139. +
  47140. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47141. + "pfkey_address_build: "
  47142. + "found address=%s.\n",
  47143. + ipaddr_txt);
  47144. + if(prefixlen != 0) {
  47145. + ERROR("pfkey_address_build: "
  47146. + "address prefixes not supported yet.\n");
  47147. + SENDERR(EAFNOSUPPORT); /* not supported yet */
  47148. + }
  47149. +
  47150. + /* allocate some memory for the extension */
  47151. + pfkey_address = (struct sadb_address*)
  47152. + MALLOC(ALIGN_N(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN));
  47153. + *pfkey_ext = (struct sadb_ext*)pfkey_address;
  47154. +
  47155. + if(pfkey_address == NULL ) {
  47156. + ERROR("pfkey_lifetime_build: "
  47157. + "memory allocation failed\n");
  47158. + SENDERR(ENOMEM);
  47159. + }
  47160. + memset(pfkey_address,
  47161. + 0,
  47162. + ALIGN_N(sizeof(struct sadb_address) + saddr_len,
  47163. + IPSEC_PFKEYv2_ALIGN));
  47164. +
  47165. + pfkey_address->sadb_address_len = DIVUP(sizeof(struct sadb_address) + saddr_len,
  47166. + IPSEC_PFKEYv2_ALIGN);
  47167. +
  47168. + pfkey_address->sadb_address_exttype = exttype;
  47169. + pfkey_address->sadb_address_proto = proto;
  47170. + pfkey_address->sadb_address_prefixlen = prefixlen;
  47171. + pfkey_address->sadb_address_reserved = 0;
  47172. +
  47173. + memcpy((char*)pfkey_address + sizeof(struct sadb_address),
  47174. + address,
  47175. + saddr_len);
  47176. +
  47177. +#if 0
  47178. + for(i = 0; i < sizeof(struct sockaddr_in) - offsetof(struct sockaddr_in, sin_zero); i++) {
  47179. + pfkey_address_s_ska.sin_zero[i] = 0;
  47180. + }
  47181. +#endif
  47182. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47183. + "pfkey_address_build: "
  47184. + "successful created len: %d.\n", pfkey_address->sadb_address_len);
  47185. +
  47186. + errlab:
  47187. + return error;
  47188. +}
  47189. +
  47190. +int
  47191. +pfkey_key_build(struct sadb_ext** pfkey_ext,
  47192. + uint16_t exttype,
  47193. + uint16_t key_bits,
  47194. + char* key)
  47195. +{
  47196. + int error = 0;
  47197. + struct sadb_key *pfkey_key = (struct sadb_key *)*pfkey_ext;
  47198. +
  47199. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47200. + "pfkey_key_build:\n");
  47201. + /* sanity checks... */
  47202. + if(pfkey_key) {
  47203. + ERROR("pfkey_key_build: "
  47204. + "why is pfkey_key already pointing to something?\n");
  47205. + SENDERR(EINVAL);
  47206. + }
  47207. +
  47208. + if(!key_bits) {
  47209. + ERROR("pfkey_key_build: "
  47210. + "key_bits is zero, it must be non-zero.\n");
  47211. + SENDERR(EINVAL);
  47212. + }
  47213. +
  47214. + if( !((exttype == SADB_EXT_KEY_AUTH) || (exttype == SADB_EXT_KEY_ENCRYPT))) {
  47215. + ERROR("pfkey_key_build: "
  47216. + "unsupported extension type=%d.\n",
  47217. + exttype);
  47218. + SENDERR(EINVAL);
  47219. + }
  47220. +
  47221. + pfkey_key = (struct sadb_key*)
  47222. + MALLOC(sizeof(struct sadb_key) +
  47223. + DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
  47224. +
  47225. + *pfkey_ext = (struct sadb_ext*)pfkey_key;
  47226. +
  47227. + if(pfkey_key == NULL) {
  47228. + ERROR("pfkey_key_build: "
  47229. + "memory allocation failed\n");
  47230. + SENDERR(ENOMEM);
  47231. + }
  47232. + memset(pfkey_key,
  47233. + 0,
  47234. + sizeof(struct sadb_key) +
  47235. + DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
  47236. +
  47237. + pfkey_key->sadb_key_len = DIVUP(sizeof(struct sadb_key) * IPSEC_PFKEYv2_ALIGN + key_bits,
  47238. + 64);
  47239. + pfkey_key->sadb_key_exttype = exttype;
  47240. + pfkey_key->sadb_key_bits = key_bits;
  47241. + pfkey_key->sadb_key_reserved = 0;
  47242. + memcpy((char*)pfkey_key + sizeof(struct sadb_key),
  47243. + key,
  47244. + DIVUP(key_bits, 8));
  47245. +
  47246. +errlab:
  47247. + return error;
  47248. +}
  47249. +
  47250. +int
  47251. +pfkey_ident_build(struct sadb_ext** pfkey_ext,
  47252. + uint16_t exttype,
  47253. + uint16_t ident_type,
  47254. + uint64_t ident_id,
  47255. + uint8_t ident_len,
  47256. + char* ident_string)
  47257. +{
  47258. + int error = 0;
  47259. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)*pfkey_ext;
  47260. + int data_len = ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  47261. +
  47262. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47263. + "pfkey_ident_build:\n");
  47264. + /* sanity checks... */
  47265. + if(pfkey_ident) {
  47266. + ERROR("pfkey_ident_build: "
  47267. + "why is pfkey_ident already pointing to something?\n");
  47268. + SENDERR(EINVAL);
  47269. + }
  47270. +
  47271. + if( ! ((exttype == SADB_EXT_IDENTITY_SRC) ||
  47272. + (exttype == SADB_EXT_IDENTITY_DST))) {
  47273. + ERROR("pfkey_ident_build: "
  47274. + "unsupported extension type=%d.\n",
  47275. + exttype);
  47276. + SENDERR(EINVAL);
  47277. + }
  47278. +
  47279. + if((ident_type == SADB_IDENTTYPE_RESERVED)) {
  47280. + ERROR("pfkey_ident_build: "
  47281. + "ident_type must be non-zero.\n");
  47282. + SENDERR(EINVAL);
  47283. + }
  47284. +
  47285. + if(ident_type > SADB_IDENTTYPE_MAX) {
  47286. + ERROR("pfkey_ident_build: "
  47287. + "identtype=%d out of range.\n",
  47288. + ident_type);
  47289. + SENDERR(EINVAL);
  47290. + }
  47291. +
  47292. + if(((ident_type == SADB_IDENTTYPE_PREFIX) ||
  47293. + (ident_type == SADB_IDENTTYPE_FQDN)) &&
  47294. + !ident_string) {
  47295. + ERROR("pfkey_ident_build: "
  47296. + "string required to allocate size of extension.\n");
  47297. + SENDERR(EINVAL);
  47298. + }
  47299. +
  47300. +#if 0
  47301. + if((ident_type == SADB_IDENTTYPE_USERFQDN) ) {
  47302. + }
  47303. +#endif
  47304. +
  47305. + pfkey_ident = (struct sadb_ident*)
  47306. + MALLOC(ident_len * IPSEC_PFKEYv2_ALIGN);
  47307. +
  47308. + *pfkey_ext = (struct sadb_ext*)pfkey_ident;
  47309. +
  47310. + if(pfkey_ident == NULL) {
  47311. + ERROR("pfkey_ident_build: "
  47312. + "memory allocation failed\n");
  47313. + SENDERR(ENOMEM);
  47314. + }
  47315. + memset(pfkey_ident, 0, ident_len * IPSEC_PFKEYv2_ALIGN);
  47316. +
  47317. + pfkey_ident->sadb_ident_len = ident_len;
  47318. + pfkey_ident->sadb_ident_exttype = exttype;
  47319. + pfkey_ident->sadb_ident_type = ident_type;
  47320. + pfkey_ident->sadb_ident_reserved = 0;
  47321. + pfkey_ident->sadb_ident_id = ident_id;
  47322. + memcpy((char*)pfkey_ident + sizeof(struct sadb_ident),
  47323. + ident_string,
  47324. + data_len);
  47325. +
  47326. +errlab:
  47327. + return error;
  47328. +}
  47329. +
  47330. +int
  47331. +pfkey_sens_build(struct sadb_ext** pfkey_ext,
  47332. + uint32_t dpd,
  47333. + uint8_t sens_level,
  47334. + uint8_t sens_len,
  47335. + uint64_t* sens_bitmap,
  47336. + uint8_t integ_level,
  47337. + uint8_t integ_len,
  47338. + uint64_t* integ_bitmap)
  47339. +{
  47340. + int error = 0;
  47341. + struct sadb_sens *pfkey_sens = (struct sadb_sens *)*pfkey_ext;
  47342. + int i;
  47343. + uint64_t* bitmap;
  47344. +
  47345. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47346. + "pfkey_sens_build:\n");
  47347. + /* sanity checks... */
  47348. + if(pfkey_sens) {
  47349. + ERROR("pfkey_sens_build: "
  47350. + "why is pfkey_sens already pointing to something?\n");
  47351. + SENDERR(EINVAL);
  47352. + }
  47353. +
  47354. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47355. + "pfkey_sens_build: "
  47356. + "Sorry, I can't build exttype=%d yet.\n",
  47357. + (*pfkey_ext)->sadb_ext_type);
  47358. + SENDERR(EINVAL); /* don't process these yet */
  47359. +
  47360. + pfkey_sens = (struct sadb_sens*)
  47361. + MALLOC(sizeof(struct sadb_sens) +
  47362. + (sens_len + integ_len) * sizeof(uint64_t));
  47363. +
  47364. + *pfkey_ext = (struct sadb_ext*)pfkey_sens;
  47365. +
  47366. + if(pfkey_sens == NULL) {
  47367. + ERROR("pfkey_sens_build: "
  47368. + "memory allocation failed\n");
  47369. + SENDERR(ENOMEM);
  47370. + }
  47371. + memset(pfkey_sens,
  47372. + 0,
  47373. + sizeof(struct sadb_sens) +
  47374. + (sens_len + integ_len) * sizeof(uint64_t));
  47375. +
  47376. + pfkey_sens->sadb_sens_len = (sizeof(struct sadb_sens) +
  47377. + (sens_len + integ_len) * sizeof(uint64_t)) / IPSEC_PFKEYv2_ALIGN;
  47378. + pfkey_sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
  47379. + pfkey_sens->sadb_sens_dpd = dpd;
  47380. + pfkey_sens->sadb_sens_sens_level = sens_level;
  47381. + pfkey_sens->sadb_sens_sens_len = sens_len;
  47382. + pfkey_sens->sadb_sens_integ_level = integ_level;
  47383. + pfkey_sens->sadb_sens_integ_len = integ_len;
  47384. + pfkey_sens->sadb_sens_reserved = 0;
  47385. +
  47386. + bitmap = (uint64_t*)((char*)pfkey_ext + sizeof(struct sadb_sens));
  47387. + for(i = 0; i < sens_len; i++) {
  47388. + *bitmap = sens_bitmap[i];
  47389. + bitmap++;
  47390. + }
  47391. + for(i = 0; i < integ_len; i++) {
  47392. + *bitmap = integ_bitmap[i];
  47393. + bitmap++;
  47394. + }
  47395. +
  47396. +errlab:
  47397. + return error;
  47398. +}
  47399. +
  47400. +int
  47401. +pfkey_prop_build(struct sadb_ext** pfkey_ext,
  47402. + uint8_t replay,
  47403. + unsigned int comb_num,
  47404. + struct sadb_comb* comb)
  47405. +{
  47406. + int error = 0;
  47407. + int i;
  47408. + struct sadb_prop *pfkey_prop = (struct sadb_prop *)*pfkey_ext;
  47409. + struct sadb_comb *combp;
  47410. +
  47411. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47412. + "pfkey_prop_build:\n");
  47413. + /* sanity checks... */
  47414. + if(pfkey_prop) {
  47415. + ERROR("pfkey_prop_build: "
  47416. + "why is pfkey_prop already pointing to something?\n");
  47417. + SENDERR(EINVAL);
  47418. + }
  47419. +
  47420. + pfkey_prop = (struct sadb_prop*)
  47421. + MALLOC(sizeof(struct sadb_prop) +
  47422. + comb_num * sizeof(struct sadb_comb));
  47423. +
  47424. + *pfkey_ext = (struct sadb_ext*)pfkey_prop;
  47425. +
  47426. + if(pfkey_prop == NULL) {
  47427. + ERROR("pfkey_prop_build: "
  47428. + "memory allocation failed\n");
  47429. + SENDERR(ENOMEM);
  47430. + }
  47431. + memset(pfkey_prop,
  47432. + 0,
  47433. + sizeof(struct sadb_prop) +
  47434. + comb_num * sizeof(struct sadb_comb));
  47435. +
  47436. + pfkey_prop->sadb_prop_len = (sizeof(struct sadb_prop) +
  47437. + comb_num * sizeof(struct sadb_comb)) / IPSEC_PFKEYv2_ALIGN;
  47438. +
  47439. + pfkey_prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
  47440. + pfkey_prop->sadb_prop_replay = replay;
  47441. +
  47442. + for(i=0; i<3; i++) {
  47443. + pfkey_prop->sadb_prop_reserved[i] = 0;
  47444. + }
  47445. +
  47446. + combp = (struct sadb_comb*)((char*)*pfkey_ext + sizeof(struct sadb_prop));
  47447. + for(i = 0; i < comb_num; i++) {
  47448. + memcpy (combp, &(comb[i]), sizeof(struct sadb_comb));
  47449. + combp++;
  47450. + }
  47451. +
  47452. +#if 0
  47453. + uint8_t sadb_comb_auth;
  47454. + uint8_t sadb_comb_encrypt;
  47455. + uint16_t sadb_comb_flags;
  47456. + uint16_t sadb_comb_auth_minbits;
  47457. + uint16_t sadb_comb_auth_maxbits;
  47458. + uint16_t sadb_comb_encrypt_minbits;
  47459. + uint16_t sadb_comb_encrypt_maxbits;
  47460. + uint32_t sadb_comb_reserved;
  47461. + uint32_t sadb_comb_soft_allocations;
  47462. + uint32_t sadb_comb_hard_allocations;
  47463. + uint64_t sadb_comb_soft_bytes;
  47464. + uint64_t sadb_comb_hard_bytes;
  47465. + uint64_t sadb_comb_soft_addtime;
  47466. + uint64_t sadb_comb_hard_addtime;
  47467. + uint64_t sadb_comb_soft_usetime;
  47468. + uint64_t sadb_comb_hard_usetime;
  47469. + uint32_t sadb_comb_soft_packets;
  47470. + uint32_t sadb_comb_hard_packets;
  47471. +#endif
  47472. +errlab:
  47473. + return error;
  47474. +}
  47475. +
  47476. +int
  47477. +pfkey_supported_build(struct sadb_ext** pfkey_ext,
  47478. + uint16_t exttype,
  47479. + unsigned int alg_num,
  47480. + struct sadb_alg* alg)
  47481. +{
  47482. + int error = 0;
  47483. + unsigned int i;
  47484. + struct sadb_supported *pfkey_supported = (struct sadb_supported *)*pfkey_ext;
  47485. + struct sadb_alg *pfkey_alg;
  47486. +
  47487. + /* sanity checks... */
  47488. + if(pfkey_supported) {
  47489. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47490. + "pfkey_supported_build: "
  47491. + "why is pfkey_supported already pointing to something?\n");
  47492. + SENDERR(EINVAL);
  47493. + }
  47494. +
  47495. + if( !((exttype == SADB_EXT_SUPPORTED_AUTH) || (exttype == SADB_EXT_SUPPORTED_ENCRYPT))) {
  47496. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47497. + "pfkey_supported_build: "
  47498. + "unsupported extension type=%d.\n",
  47499. + exttype);
  47500. + SENDERR(EINVAL);
  47501. + }
  47502. +
  47503. + pfkey_supported = (struct sadb_supported*)
  47504. + MALLOC(sizeof(struct sadb_supported) +
  47505. + alg_num *
  47506. + sizeof(struct sadb_alg));
  47507. +
  47508. + *pfkey_ext = (struct sadb_ext*)pfkey_supported;
  47509. +
  47510. + if(pfkey_supported == NULL) {
  47511. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47512. + "pfkey_supported_build: "
  47513. + "memory allocation failed\n");
  47514. + SENDERR(ENOMEM);
  47515. + }
  47516. + memset(pfkey_supported,
  47517. + 0,
  47518. + sizeof(struct sadb_supported) +
  47519. + alg_num *
  47520. + sizeof(struct sadb_alg));
  47521. +
  47522. + pfkey_supported->sadb_supported_len = (sizeof(struct sadb_supported) +
  47523. + alg_num *
  47524. + sizeof(struct sadb_alg)) /
  47525. + IPSEC_PFKEYv2_ALIGN;
  47526. + pfkey_supported->sadb_supported_exttype = exttype;
  47527. + pfkey_supported->sadb_supported_reserved = 0;
  47528. +
  47529. + pfkey_alg = (struct sadb_alg*)((char*)pfkey_supported + sizeof(struct sadb_supported));
  47530. + for(i = 0; i < alg_num; i++) {
  47531. + memcpy (pfkey_alg, &(alg[i]), sizeof(struct sadb_alg));
  47532. + pfkey_alg->sadb_alg_reserved = 0;
  47533. + pfkey_alg++;
  47534. + }
  47535. +
  47536. +#if 0
  47537. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47538. + "pfkey_supported_build: "
  47539. + "Sorry, I can't build exttype=%d yet.\n",
  47540. + (*pfkey_ext)->sadb_ext_type);
  47541. + SENDERR(EINVAL); /* don't process these yet */
  47542. +
  47543. + uint8_t sadb_alg_id;
  47544. + uint8_t sadb_alg_ivlen;
  47545. + uint16_t sadb_alg_minbits;
  47546. + uint16_t sadb_alg_maxbits;
  47547. + uint16_t sadb_alg_reserved;
  47548. +#endif
  47549. +errlab:
  47550. + return error;
  47551. +}
  47552. +
  47553. +int
  47554. +pfkey_spirange_build(struct sadb_ext** pfkey_ext,
  47555. + uint16_t exttype,
  47556. + uint32_t min, /* in network order */
  47557. + uint32_t max) /* in network order */
  47558. +{
  47559. + int error = 0;
  47560. + struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)*pfkey_ext;
  47561. +
  47562. + /* sanity checks... */
  47563. + if(pfkey_spirange) {
  47564. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47565. + "pfkey_spirange_build: "
  47566. + "why is pfkey_spirange already pointing to something?\n");
  47567. + SENDERR(EINVAL);
  47568. + }
  47569. +
  47570. + if(ntohl(max) < ntohl(min)) {
  47571. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47572. + "pfkey_spirange_build: "
  47573. + "minspi=%08x must be < maxspi=%08x.\n",
  47574. + ntohl(min),
  47575. + ntohl(max));
  47576. + SENDERR(EINVAL);
  47577. + }
  47578. +
  47579. + if(ntohl(min) <= 255) {
  47580. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47581. + "pfkey_spirange_build: "
  47582. + "minspi=%08x must be > 255.\n",
  47583. + ntohl(min));
  47584. + SENDERR(EEXIST);
  47585. + }
  47586. +
  47587. + pfkey_spirange = (struct sadb_spirange*)
  47588. + MALLOC(sizeof(struct sadb_spirange));
  47589. +
  47590. + *pfkey_ext = (struct sadb_ext*)pfkey_spirange;
  47591. +
  47592. + if(pfkey_spirange == NULL) {
  47593. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47594. + "pfkey_spirange_build: "
  47595. + "memory allocation failed\n");
  47596. + SENDERR(ENOMEM);
  47597. + }
  47598. + memset(pfkey_spirange,
  47599. + 0,
  47600. + sizeof(struct sadb_spirange));
  47601. +
  47602. + pfkey_spirange->sadb_spirange_len = sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN;
  47603. +
  47604. + pfkey_spirange->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
  47605. + pfkey_spirange->sadb_spirange_min = min;
  47606. + pfkey_spirange->sadb_spirange_max = max;
  47607. + pfkey_spirange->sadb_spirange_reserved = 0;
  47608. + errlab:
  47609. + return error;
  47610. +}
  47611. +
  47612. +int
  47613. +pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext)
  47614. +{
  47615. + int error = 0;
  47616. + struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)*pfkey_ext;
  47617. +
  47618. + /* sanity checks... */
  47619. + if(pfkey_x_kmprivate) {
  47620. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47621. + "pfkey_x_kmprivate_build: "
  47622. + "why is pfkey_x_kmprivate already pointing to something?\n");
  47623. + SENDERR(EINVAL);
  47624. + }
  47625. +
  47626. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
  47627. +
  47628. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47629. + "pfkey_x_kmprivate_build: "
  47630. + "Sorry, I can't build exttype=%d yet.\n",
  47631. + (*pfkey_ext)->sadb_ext_type);
  47632. + SENDERR(EINVAL); /* don't process these yet */
  47633. +
  47634. + pfkey_x_kmprivate = (struct sadb_x_kmprivate*)
  47635. + MALLOC(sizeof(struct sadb_x_kmprivate));
  47636. +
  47637. + *pfkey_ext = (struct sadb_ext*)pfkey_x_kmprivate;
  47638. +
  47639. + if(pfkey_x_kmprivate == NULL) {
  47640. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47641. + "pfkey_x_kmprivate_build: "
  47642. + "memory allocation failed\n");
  47643. + SENDERR(ENOMEM);
  47644. + }
  47645. + memset(pfkey_x_kmprivate,
  47646. + 0,
  47647. + sizeof(struct sadb_x_kmprivate));
  47648. +
  47649. + pfkey_x_kmprivate->sadb_x_kmprivate_len =
  47650. + sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN;
  47651. +
  47652. + pfkey_x_kmprivate->sadb_x_kmprivate_exttype = SADB_X_EXT_KMPRIVATE;
  47653. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
  47654. +errlab:
  47655. + return error;
  47656. +}
  47657. +
  47658. +int
  47659. +pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
  47660. + uint8_t satype)
  47661. +{
  47662. + int error = 0;
  47663. + int i;
  47664. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)*pfkey_ext;
  47665. +
  47666. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47667. + "pfkey_x_satype_build:\n");
  47668. + /* sanity checks... */
  47669. + if(pfkey_x_satype) {
  47670. + ERROR("pfkey_x_satype_build: "
  47671. + "why is pfkey_x_satype already pointing to something?\n");
  47672. + SENDERR(EINVAL);
  47673. + }
  47674. +
  47675. + if(!satype) {
  47676. + ERROR("pfkey_x_satype_build: "
  47677. + "SA type not set, must be non-zero.\n");
  47678. + SENDERR(EINVAL);
  47679. + }
  47680. +
  47681. + if(satype > SADB_SATYPE_MAX) {
  47682. + ERROR("pfkey_x_satype_build: "
  47683. + "satype %d > max %d\n",
  47684. + satype, SADB_SATYPE_MAX);
  47685. + SENDERR(EINVAL);
  47686. + }
  47687. +
  47688. + pfkey_x_satype = (struct sadb_x_satype*)
  47689. + MALLOC(sizeof(struct sadb_x_satype));
  47690. +
  47691. + *pfkey_ext = (struct sadb_ext*)pfkey_x_satype;
  47692. + if(pfkey_x_satype == NULL) {
  47693. + ERROR("pfkey_x_satype_build: "
  47694. + "memory allocation failed\n");
  47695. + SENDERR(ENOMEM);
  47696. + }
  47697. + memset(pfkey_x_satype,
  47698. + 0,
  47699. + sizeof(struct sadb_x_satype));
  47700. +
  47701. + pfkey_x_satype->sadb_x_satype_len = sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN;
  47702. +
  47703. + pfkey_x_satype->sadb_x_satype_exttype = SADB_X_EXT_SATYPE2;
  47704. + pfkey_x_satype->sadb_x_satype_satype = satype;
  47705. + for(i=0; i<3; i++) {
  47706. + pfkey_x_satype->sadb_x_satype_reserved[i] = 0;
  47707. + }
  47708. +
  47709. +errlab:
  47710. + return error;
  47711. +}
  47712. +
  47713. +int
  47714. +pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
  47715. + uint32_t tunnel,
  47716. + uint32_t netlink,
  47717. + uint32_t xform,
  47718. + uint32_t eroute,
  47719. + uint32_t spi,
  47720. + uint32_t radij,
  47721. + uint32_t esp,
  47722. + uint32_t ah,
  47723. + uint32_t rcv,
  47724. + uint32_t pfkey,
  47725. + uint32_t ipcomp,
  47726. + uint32_t verbose)
  47727. +{
  47728. + int error = 0;
  47729. + int i;
  47730. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)*pfkey_ext;
  47731. +
  47732. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47733. + "pfkey_x_debug_build:\n");
  47734. + /* sanity checks... */
  47735. + if(pfkey_x_debug) {
  47736. + ERROR("pfkey_x_debug_build: "
  47737. + "why is pfkey_x_debug already pointing to something?\n");
  47738. + SENDERR(EINVAL);
  47739. + }
  47740. +
  47741. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47742. + "pfkey_x_debug_build: "
  47743. + "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",
  47744. + tunnel, netlink, xform, eroute, spi, radij, esp, ah, rcv, pfkey, ipcomp, verbose);
  47745. +
  47746. + pfkey_x_debug = (struct sadb_x_debug*)
  47747. + MALLOC(sizeof(struct sadb_x_debug));
  47748. +
  47749. + *pfkey_ext = (struct sadb_ext*)pfkey_x_debug;
  47750. +
  47751. + if(pfkey_x_debug == NULL) {
  47752. + ERROR("pfkey_x_debug_build: "
  47753. + "memory allocation failed\n");
  47754. + SENDERR(ENOMEM);
  47755. + }
  47756. +#if 0
  47757. + memset(pfkey_x_debug,
  47758. + 0,
  47759. + sizeof(struct sadb_x_debug));
  47760. +#endif
  47761. +
  47762. + pfkey_x_debug->sadb_x_debug_len = sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN;
  47763. + pfkey_x_debug->sadb_x_debug_exttype = SADB_X_EXT_DEBUG;
  47764. +
  47765. + pfkey_x_debug->sadb_x_debug_tunnel = tunnel;
  47766. + pfkey_x_debug->sadb_x_debug_netlink = netlink;
  47767. + pfkey_x_debug->sadb_x_debug_xform = xform;
  47768. + pfkey_x_debug->sadb_x_debug_eroute = eroute;
  47769. + pfkey_x_debug->sadb_x_debug_spi = spi;
  47770. + pfkey_x_debug->sadb_x_debug_radij = radij;
  47771. + pfkey_x_debug->sadb_x_debug_esp = esp;
  47772. + pfkey_x_debug->sadb_x_debug_ah = ah;
  47773. + pfkey_x_debug->sadb_x_debug_rcv = rcv;
  47774. + pfkey_x_debug->sadb_x_debug_pfkey = pfkey;
  47775. + pfkey_x_debug->sadb_x_debug_ipcomp = ipcomp;
  47776. + pfkey_x_debug->sadb_x_debug_verbose = verbose;
  47777. +
  47778. + for(i=0; i<4; i++) {
  47779. + pfkey_x_debug->sadb_x_debug_reserved[i] = 0;
  47780. + }
  47781. +
  47782. +errlab:
  47783. + return error;
  47784. +}
  47785. +
  47786. +int
  47787. +pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
  47788. + uint8_t type)
  47789. +{
  47790. + int error = 0;
  47791. + int i;
  47792. + struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)*pfkey_ext;
  47793. +
  47794. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47795. + "pfkey_x_nat_t_type_build:\n");
  47796. + /* sanity checks... */
  47797. + if(pfkey_x_nat_t_type) {
  47798. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47799. + "pfkey_x_nat_t_type_build: "
  47800. + "why is pfkey_x_nat_t_type already pointing to something?\n");
  47801. + SENDERR(EINVAL);
  47802. + }
  47803. +
  47804. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47805. + "pfkey_x_nat_t_type_build: "
  47806. + "type=%d\n", type);
  47807. +
  47808. + pfkey_x_nat_t_type = (struct sadb_x_nat_t_type*)
  47809. + MALLOC(sizeof(struct sadb_x_nat_t_type));
  47810. +
  47811. + *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_type;
  47812. +
  47813. + if(pfkey_x_nat_t_type == NULL) {
  47814. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47815. + "pfkey_x_nat_t_type_build: "
  47816. + "memory allocation failed\n");
  47817. + SENDERR(ENOMEM);
  47818. + }
  47819. +
  47820. + pfkey_x_nat_t_type->sadb_x_nat_t_type_len = sizeof(struct sadb_x_nat_t_type) / IPSEC_PFKEYv2_ALIGN;
  47821. + pfkey_x_nat_t_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
  47822. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type = type;
  47823. + for(i=0; i<3; i++) {
  47824. + pfkey_x_nat_t_type->sadb_x_nat_t_type_reserved[i] = 0;
  47825. + }
  47826. +
  47827. +errlab:
  47828. + return error;
  47829. +}
  47830. +int
  47831. +pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
  47832. + uint16_t exttype,
  47833. + uint16_t port)
  47834. +{
  47835. + int error = 0;
  47836. + struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)*pfkey_ext;
  47837. +
  47838. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47839. + "pfkey_x_nat_t_port_build:\n");
  47840. + /* sanity checks... */
  47841. + if(pfkey_x_nat_t_port) {
  47842. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47843. + "pfkey_x_nat_t_port_build: "
  47844. + "why is pfkey_x_nat_t_port already pointing to something?\n");
  47845. + SENDERR(EINVAL);
  47846. + }
  47847. +
  47848. + switch(exttype) {
  47849. + case SADB_X_EXT_NAT_T_SPORT:
  47850. + case SADB_X_EXT_NAT_T_DPORT:
  47851. + break;
  47852. + default:
  47853. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47854. + "pfkey_nat_t_port_build: "
  47855. + "unrecognised ext_type=%d.\n",
  47856. + exttype);
  47857. + SENDERR(EINVAL);
  47858. + }
  47859. +
  47860. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47861. + "pfkey_x_nat_t_port_build: "
  47862. + "ext=%d, port=%d\n", exttype, port);
  47863. +
  47864. + pfkey_x_nat_t_port = (struct sadb_x_nat_t_port*)
  47865. + MALLOC(sizeof(struct sadb_x_nat_t_port));
  47866. +
  47867. + *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_port;
  47868. +
  47869. + if(pfkey_x_nat_t_port == NULL) {
  47870. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47871. + "pfkey_x_nat_t_port_build: "
  47872. + "memory allocation failed\n");
  47873. + SENDERR(ENOMEM);
  47874. + }
  47875. +
  47876. + pfkey_x_nat_t_port->sadb_x_nat_t_port_len = sizeof(struct sadb_x_nat_t_port) / IPSEC_PFKEYv2_ALIGN;
  47877. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype = exttype;
  47878. + pfkey_x_nat_t_port->sadb_x_nat_t_port_port = port;
  47879. + pfkey_x_nat_t_port->sadb_x_nat_t_port_reserved = 0;
  47880. +
  47881. +errlab:
  47882. + return error;
  47883. +}
  47884. +
  47885. +int pfkey_x_protocol_build(struct sadb_ext **pfkey_ext,
  47886. + uint8_t protocol)
  47887. +{
  47888. + int error = 0;
  47889. + struct sadb_protocol * p = (struct sadb_protocol *)*pfkey_ext;
  47890. + DEBUGGING(PF_KEY_DEBUG_BUILD,"pfkey_x_protocol_build: protocol=%u\n", protocol);
  47891. + /* sanity checks... */
  47892. + if (p != 0) {
  47893. + ERROR("pfkey_x_protocol_build: bogus protocol pointer\n");
  47894. + SENDERR(EINVAL);
  47895. + }
  47896. + if ((p = (struct sadb_protocol*)MALLOC(sizeof(*p))) == 0) {
  47897. + ERROR("pfkey_build: memory allocation failed\n");
  47898. + SENDERR(ENOMEM);
  47899. + }
  47900. + *pfkey_ext = (struct sadb_ext *)p;
  47901. + p->sadb_protocol_len = sizeof(*p) / sizeof(uint64_t);
  47902. + p->sadb_protocol_exttype = SADB_X_EXT_PROTOCOL;
  47903. + p->sadb_protocol_proto = protocol;
  47904. + p->sadb_protocol_flags = 0;
  47905. + p->sadb_protocol_reserved2 = 0;
  47906. + errlab:
  47907. + return error;
  47908. +}
  47909. +
  47910. +int
  47911. +pfkey_msg_build(struct sadb_msg **pfkey_msg, struct sadb_ext *extensions[], int dir)
  47912. +{
  47913. + int error = 0;
  47914. + unsigned ext;
  47915. + unsigned total_size;
  47916. + struct sadb_ext *pfkey_ext;
  47917. + int extensions_seen = 0;
  47918. +#ifndef __KERNEL__
  47919. + struct sadb_ext *extensions_check[SADB_EXT_MAX + 1];
  47920. +#endif
  47921. +
  47922. + if(!extensions[0]) {
  47923. + ERROR("pfkey_msg_build: "
  47924. + "extensions[0] must be specified (struct sadb_msg).\n");
  47925. + SENDERR(EINVAL);
  47926. + }
  47927. +
  47928. + /* figure out the total size for all the requested extensions */
  47929. + total_size = IPSEC_PFKEYv2_WORDS(sizeof(struct sadb_msg));
  47930. + for(ext = 1; ext <= SADB_EXT_MAX; ext++) {
  47931. + if(extensions[ext]) {
  47932. + total_size += (extensions[ext])->sadb_ext_len;
  47933. + }
  47934. + }
  47935. +
  47936. + /* allocate that much space */
  47937. + *pfkey_msg = (struct sadb_msg*)MALLOC(total_size * IPSEC_PFKEYv2_ALIGN);
  47938. + if(*pfkey_msg == NULL) {
  47939. + ERROR("pfkey_msg_build: "
  47940. + "memory allocation failed\n");
  47941. + SENDERR(ENOMEM);
  47942. + }
  47943. +
  47944. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47945. + "pfkey_msg_build: "
  47946. + "pfkey_msg=0p%p allocated %lu bytes, &(extensions[0])=0p%p\n",
  47947. + *pfkey_msg,
  47948. + (unsigned long)(total_size * IPSEC_PFKEYv2_ALIGN),
  47949. + &(extensions[0]));
  47950. +
  47951. + memcpy(*pfkey_msg,
  47952. + extensions[0],
  47953. + sizeof(struct sadb_msg));
  47954. + (*pfkey_msg)->sadb_msg_len = total_size;
  47955. + (*pfkey_msg)->sadb_msg_reserved = 0;
  47956. + extensions_seen = 1 ;
  47957. +
  47958. + /*
  47959. + * point pfkey_ext to immediately after the space for the header,
  47960. + * i.e. at the first extension location.
  47961. + */
  47962. + pfkey_ext = (struct sadb_ext*)(((char*)(*pfkey_msg)) + sizeof(struct sadb_msg));
  47963. +
  47964. + for(ext = 1; ext <= SADB_EXT_MAX; ext++) {
  47965. + /* copy from extension[ext] to buffer */
  47966. + if(extensions[ext]) {
  47967. + /* Is this type of extension permitted for this type of message? */
  47968. + if(!(extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type] &
  47969. + 1<<ext)) {
  47970. + ERROR("pfkey_msg_build: "
  47971. + "ext type %d not permitted, exts_perm=%08x, 1<<type=%08x\n",
  47972. + ext,
  47973. + extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
  47974. + 1<<ext);
  47975. + SENDERR(EINVAL);
  47976. + }
  47977. +
  47978. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47979. + "pfkey_msg_build: "
  47980. + "copying %lu bytes from extensions[%u] (type=%d)\n",
  47981. + (unsigned long)(extensions[ext]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN),
  47982. + ext,
  47983. + extensions[ext]->sadb_ext_type);
  47984. +
  47985. + memcpy(pfkey_ext,
  47986. + extensions[ext],
  47987. + (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  47988. + {
  47989. + char *pfkey_ext_c = (char *)pfkey_ext;
  47990. +
  47991. + pfkey_ext_c += (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN;
  47992. + pfkey_ext = (struct sadb_ext *)pfkey_ext_c;
  47993. + }
  47994. +
  47995. + /* Mark that we have seen this extension and remember the header location */
  47996. + extensions_seen |= ( 1 << ext );
  47997. + }
  47998. + }
  47999. +
  48000. + /* check required extensions */
  48001. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48002. + "pfkey_msg_build: "
  48003. + "extensions permitted=%08x, seen=%08x, required=%08x.\n",
  48004. + extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
  48005. + extensions_seen,
  48006. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]);
  48007. +
  48008. + if((extensions_seen &
  48009. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) !=
  48010. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) {
  48011. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48012. + "pfkey_msg_build: "
  48013. + "required extensions missing:%08x.\n",
  48014. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type] -
  48015. + (extensions_seen &
  48016. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) );
  48017. + SENDERR(EINVAL);
  48018. + }
  48019. +
  48020. +#ifndef __KERNEL__
  48021. +/*
  48022. + * this is silly, there is no need to reparse the message that we just built.
  48023. + *
  48024. + */
  48025. + if((error = pfkey_msg_parse(*pfkey_msg, NULL, extensions_check, dir))) {
  48026. + ERROR(
  48027. + "pfkey_msg_build: "
  48028. + "Trouble parsing newly built pfkey message, error=%d.\n",
  48029. + error);
  48030. + SENDERR(-error);
  48031. + }
  48032. +#endif
  48033. +
  48034. +errlab:
  48035. +
  48036. + return error;
  48037. +}
  48038. +
  48039. +/*
  48040. + * $Log: pfkey_v2_build.c,v $
  48041. + * Revision 1.51.8.1 2006/05/01 14:36:39 mcr
  48042. + * get rid of dead code.
  48043. + *
  48044. + * Revision 1.51 2004/10/03 01:26:36 mcr
  48045. + * fixes for gcc 3.4 compilation.
  48046. + *
  48047. + * Revision 1.50 2004/07/10 07:48:35 mcr
  48048. + * Moved from linux/lib/libfreeswan/pfkey_v2_build.c,v
  48049. + *
  48050. + * Revision 1.49 2004/04/12 02:59:06 mcr
  48051. + * erroneously moved pfkey_v2_build.c
  48052. + *
  48053. + * Revision 1.48 2004/04/09 18:00:40 mcr
  48054. + * Moved from linux/lib/libfreeswan/pfkey_v2_build.c,v
  48055. + *
  48056. + * Revision 1.47 2004/03/08 01:59:08 ken
  48057. + * freeswan.h -> openswan.h
  48058. + *
  48059. + * Revision 1.46 2003/12/10 01:20:19 mcr
  48060. + * NAT-traversal patches to KLIPS.
  48061. + *
  48062. + * Revision 1.45 2003/12/04 23:01:12 mcr
  48063. + * removed ipsec_netlink.h
  48064. + *
  48065. + * Revision 1.44 2003/10/31 02:27:12 mcr
  48066. + * pulled up port-selector patches and sa_id elimination.
  48067. + *
  48068. + * Revision 1.43.4.2 2003/10/29 01:11:32 mcr
  48069. + * added debugging for pfkey library.
  48070. + *
  48071. + * Revision 1.43.4.1 2003/09/21 13:59:44 mcr
  48072. + * pre-liminary X.509 patch - does not yet pass tests.
  48073. + *
  48074. + * Revision 1.43 2003/05/07 17:29:17 mcr
  48075. + * new function pfkey_debug_func added for us in debugging from
  48076. + * pfkey library.
  48077. + *
  48078. + * Revision 1.42 2003/01/30 02:32:09 rgb
  48079. + *
  48080. + * Rename SAref table macro names for clarity.
  48081. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  48082. + *
  48083. + * Revision 1.41 2002/12/13 18:16:02 mcr
  48084. + * restored sa_ref code
  48085. + *
  48086. + * Revision 1.40 2002/12/13 18:06:52 mcr
  48087. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  48088. + *
  48089. + * Revision 1.39 2002/12/13 17:43:28 mcr
  48090. + * commented out access to sadb_x_sa_ref for 2.xx branch
  48091. + *
  48092. + * Revision 1.38 2002/10/09 03:12:05 dhr
  48093. + *
  48094. + * [kenb+dhr] 64-bit fixes
  48095. + *
  48096. + * Revision 1.37 2002/09/20 15:40:39 rgb
  48097. + * Added new function pfkey_sa_ref_build() to accomodate saref parameter.
  48098. + *
  48099. + * Revision 1.36 2002/09/20 05:01:22 rgb
  48100. + * Generalise for platform independance: fix (ia64) using unsigned for sizes.
  48101. + *
  48102. + * Revision 1.35 2002/07/24 18:44:54 rgb
  48103. + * Type fiddling to tame ia64 compiler.
  48104. + *
  48105. + * Revision 1.34 2002/05/23 07:14:11 rgb
  48106. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  48107. + *
  48108. + * Revision 1.33 2002/04/24 07:55:32 mcr
  48109. + * #include patches and Makefiles for post-reorg compilation.
  48110. + *
  48111. + * Revision 1.32 2002/04/24 07:36:40 mcr
  48112. + * Moved from ./lib/pfkey_v2_build.c,v
  48113. + *
  48114. + * Revision 1.31 2002/01/29 22:25:35 rgb
  48115. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  48116. + *
  48117. + * Revision 1.30 2002/01/29 01:59:09 mcr
  48118. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  48119. + * updating of IPv6 structures to match latest in6.h version.
  48120. + * removed dead code from openswan.h that also duplicated kversions.h
  48121. + * code.
  48122. + *
  48123. + * Revision 1.29 2001/12/19 21:06:09 rgb
  48124. + * Added port numbers to pfkey_address_build() debugging.
  48125. + *
  48126. + * Revision 1.28 2001/11/06 19:47:47 rgb
  48127. + * Added packet parameter to lifetime and comb structures.
  48128. + *
  48129. + * Revision 1.27 2001/10/18 04:45:24 rgb
  48130. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  48131. + * lib/openswan.h version macros moved to lib/kversions.h.
  48132. + * Other compiler directive cleanups.
  48133. + *
  48134. + * Revision 1.26 2001/09/08 21:13:34 rgb
  48135. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  48136. + *
  48137. + * Revision 1.25 2001/06/14 19:35:16 rgb
  48138. + * Update copyright date.
  48139. + *
  48140. + * Revision 1.24 2001/03/20 03:49:45 rgb
  48141. + * Ditch superfluous debug_pfkey declaration.
  48142. + * Move misplaced openswan.h inclusion for kernel case.
  48143. + *
  48144. + * Revision 1.23 2001/03/16 07:41:50 rgb
  48145. + * Put openswan.h include before pluto includes.
  48146. + *
  48147. + * Revision 1.22 2001/02/27 22:24:56 rgb
  48148. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  48149. + * Check for satoa() return codes.
  48150. + *
  48151. + * Revision 1.21 2000/11/17 18:10:30 rgb
  48152. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  48153. + * network byte order since this is the way PF_KEYv2 stored spis.
  48154. + *
  48155. + * Revision 1.20 2000/10/12 00:02:39 rgb
  48156. + * Removed 'format, ##' nonsense from debug macros for RH7.0.
  48157. + *
  48158. + * Revision 1.19 2000/10/10 20:10:20 rgb
  48159. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  48160. + *
  48161. + * Revision 1.18 2000/09/12 18:59:54 rgb
  48162. + * Added Gerhard's IPv6 support to pfkey parts of libopenswan.
  48163. + *
  48164. + * Revision 1.17 2000/09/12 03:27:00 rgb
  48165. + * Moved DEBUGGING definition to compile kernel with debug off.
  48166. + *
  48167. + * Revision 1.16 2000/09/08 19:22:12 rgb
  48168. + * Fixed pfkey_prop_build() parameter to be only single indirection.
  48169. + * Fixed struct alg copy.
  48170. + *
  48171. + * Revision 1.15 2000/08/20 21:40:01 rgb
  48172. + * Added an address parameter sanity check to pfkey_address_build().
  48173. + *
  48174. + * Revision 1.14 2000/08/15 17:29:23 rgb
  48175. + * Fixes from SZI to untested pfkey_prop_build().
  48176. + *
  48177. + * Revision 1.13 2000/06/02 22:54:14 rgb
  48178. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  48179. + *
  48180. + * Revision 1.12 2000/05/10 19:24:01 rgb
  48181. + * Fleshed out sensitivity, proposal and supported extensions.
  48182. + *
  48183. + * Revision 1.11 2000/03/16 14:07:23 rgb
  48184. + * Renamed ALIGN macro to avoid fighting with others in kernel.
  48185. + *
  48186. + * Revision 1.10 2000/01/24 21:14:35 rgb
  48187. + * Added disabled pluto pfkey lib debug flag.
  48188. + *
  48189. + * Revision 1.9 2000/01/21 06:27:32 rgb
  48190. + * Added address cases for eroute flows.
  48191. + * Removed unused code.
  48192. + * Dropped unused argument to pfkey_x_satype_build().
  48193. + * Indented compiler directives for readability.
  48194. + * Added klipsdebug switching capability.
  48195. + * Fixed SADB_EXT_MAX bug not permitting last extension access.
  48196. + *
  48197. + * Revision 1.8 1999/12/29 21:17:41 rgb
  48198. + * Changed pfkey_msg_build() I/F to include a struct sadb_msg**
  48199. + * parameter for cleaner manipulation of extensions[] and to guard
  48200. + * against potential memory leaks.
  48201. + * Changed the I/F to pfkey_msg_free() for the same reason.
  48202. + *
  48203. + * Revision 1.7 1999/12/09 23:12:20 rgb
  48204. + * Removed unused cruft.
  48205. + * Added argument to pfkey_sa_build() to do eroutes.
  48206. + * Fixed exttype check in as yet unused pfkey_lifetime_build().
  48207. + *
  48208. + * Revision 1.6 1999/12/07 19:54:29 rgb
  48209. + * Removed static pluto debug flag.
  48210. + * Added functions for pfkey message and extensions initialisation
  48211. + * and cleanup.
  48212. + *
  48213. + * Revision 1.5 1999/12/01 22:20:06 rgb
  48214. + * Changed pfkey_sa_build to accept an SPI in network byte order.
  48215. + * Added <string.h> to quiet userspace compiler.
  48216. + * Moved pfkey_lib_debug variable into the library.
  48217. + * Removed SATYPE check from pfkey_msg_hdr_build so FLUSH will work.
  48218. + * Added extension assembly debugging.
  48219. + * Isolated assignment with brackets to be sure of scope.
  48220. + *
  48221. + * Revision 1.4 1999/11/27 11:57:35 rgb
  48222. + * Added ipv6 headers.
  48223. + * Remove over-zealous algorithm sanity checkers from pfkey_sa_build.
  48224. + * Debugging error messages added.
  48225. + * Fixed missing auth and encrypt assignment bug.
  48226. + * Add argument to pfkey_msg_parse() for direction.
  48227. + * Move parse-after-build check inside pfkey_msg_build().
  48228. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  48229. + * Add CVS log entry to bottom of file.
  48230. + *
  48231. + */
  48232. --- /dev/null Tue Mar 11 13:02:56 2003
  48233. +++ linux/net/ipsec/pfkey_v2_debug.c Mon Feb 9 13:51:03 2004
  48234. @@ -0,0 +1,181 @@
  48235. +/*
  48236. + * @(#) pfkey version 2 debugging messages
  48237. + *
  48238. + * Copyright (C) 2001 Richard Guy Briggs <rgb@openswan.org>
  48239. + * and Michael Richardson <mcr@openswan.org>
  48240. + *
  48241. + * This program is free software; you can redistribute it and/or modify it
  48242. + * under the terms of the GNU General Public License as published by the
  48243. + * Free Software Foundation; either version 2 of the License, or (at your
  48244. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  48245. + *
  48246. + * This program is distributed in the hope that it will be useful, but
  48247. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  48248. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  48249. + * for more details.
  48250. + *
  48251. + * RCSID $Id: pfkey_v2_debug.c,v 1.11 2005/04/06 17:45:16 mcr Exp $
  48252. + *
  48253. + */
  48254. +
  48255. +#ifdef __KERNEL__
  48256. +
  48257. +# include <linux/kernel.h> /* for printk */
  48258. +
  48259. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  48260. +# ifdef MALLOC_SLAB
  48261. +# include <linux/slab.h> /* kmalloc() */
  48262. +# else /* MALLOC_SLAB */
  48263. +# include <linux/malloc.h> /* kmalloc() */
  48264. +# endif /* MALLOC_SLAB */
  48265. +# include <linux/errno.h> /* error codes */
  48266. +# include <linux/types.h> /* size_t */
  48267. +# include <linux/interrupt.h> /* mark_bh */
  48268. +
  48269. +# include <linux/netdevice.h> /* struct device, and other headers */
  48270. +# include <linux/etherdevice.h> /* eth_type_trans */
  48271. +extern int debug_pfkey;
  48272. +
  48273. +#else /* __KERNEL__ */
  48274. +
  48275. +# include <sys/types.h>
  48276. +# include <linux/types.h>
  48277. +# include <linux/errno.h>
  48278. +
  48279. +#endif /* __KERNEL__ */
  48280. +
  48281. +#include "openswan.h"
  48282. +#include "pfkeyv2.h"
  48283. +#include "pfkey.h"
  48284. +
  48285. +/*
  48286. + * This file provides ASCII translations of PF_KEY magic numbers.
  48287. + *
  48288. + */
  48289. +
  48290. +static char *pfkey_sadb_ext_strings[]={
  48291. + "reserved", /* SADB_EXT_RESERVED 0 */
  48292. + "security-association", /* SADB_EXT_SA 1 */
  48293. + "lifetime-current", /* SADB_EXT_LIFETIME_CURRENT 2 */
  48294. + "lifetime-hard", /* SADB_EXT_LIFETIME_HARD 3 */
  48295. + "lifetime-soft", /* SADB_EXT_LIFETIME_SOFT 4 */
  48296. + "source-address", /* SADB_EXT_ADDRESS_SRC 5 */
  48297. + "destination-address", /* SADB_EXT_ADDRESS_DST 6 */
  48298. + "proxy-address", /* SADB_EXT_ADDRESS_PROXY 7 */
  48299. + "authentication-key", /* SADB_EXT_KEY_AUTH 8 */
  48300. + "cipher-key", /* SADB_EXT_KEY_ENCRYPT 9 */
  48301. + "source-identity", /* SADB_EXT_IDENTITY_SRC 10 */
  48302. + "destination-identity", /* SADB_EXT_IDENTITY_DST 11 */
  48303. + "sensitivity-label", /* SADB_EXT_SENSITIVITY 12 */
  48304. + "proposal", /* SADB_EXT_PROPOSAL 13 */
  48305. + "supported-auth", /* SADB_EXT_SUPPORTED_AUTH 14 */
  48306. + "supported-cipher", /* SADB_EXT_SUPPORTED_ENCRYPT 15 */
  48307. + "spi-range", /* SADB_EXT_SPIRANGE 16 */
  48308. + "X-kmpprivate", /* SADB_X_EXT_KMPRIVATE 17 */
  48309. + "X-satype2", /* SADB_X_EXT_SATYPE2 18 */
  48310. + "X-security-association", /* SADB_X_EXT_SA2 19 */
  48311. + "X-destination-address2", /* SADB_X_EXT_ADDRESS_DST2 20 */
  48312. + "X-source-flow-address", /* SADB_X_EXT_ADDRESS_SRC_FLOW 21 */
  48313. + "X-dest-flow-address", /* SADB_X_EXT_ADDRESS_DST_FLOW 22 */
  48314. + "X-source-mask", /* SADB_X_EXT_ADDRESS_SRC_MASK 23 */
  48315. + "X-dest-mask", /* SADB_X_EXT_ADDRESS_DST_MASK 24 */
  48316. + "X-set-debug", /* SADB_X_EXT_DEBUG 25 */
  48317. + /* NAT_TRAVERSAL */
  48318. + "X-NAT-T-type", /* SADB_X_EXT_NAT_T_TYPE 26 */
  48319. + "X-NAT-T-sport", /* SADB_X_EXT_NAT_T_SPORT 27 */
  48320. + "X-NAT-T-dport", /* SADB_X_EXT_NAT_T_DPORT 28 */
  48321. + "X-NAT-T-OA", /* SADB_X_EXT_NAT_T_OA 29 */
  48322. +};
  48323. +
  48324. +const char *
  48325. +pfkey_v2_sadb_ext_string(int ext)
  48326. +{
  48327. + if(ext <= SADB_EXT_MAX) {
  48328. + return pfkey_sadb_ext_strings[ext];
  48329. + } else {
  48330. + return "unknown-ext";
  48331. + }
  48332. +}
  48333. +
  48334. +
  48335. +static char *pfkey_sadb_type_strings[]={
  48336. + "reserved", /* SADB_RESERVED */
  48337. + "getspi", /* SADB_GETSPI */
  48338. + "update", /* SADB_UPDATE */
  48339. + "add", /* SADB_ADD */
  48340. + "delete", /* SADB_DELETE */
  48341. + "get", /* SADB_GET */
  48342. + "acquire", /* SADB_ACQUIRE */
  48343. + "register", /* SADB_REGISTER */
  48344. + "expire", /* SADB_EXPIRE */
  48345. + "flush", /* SADB_FLUSH */
  48346. + "dump", /* SADB_DUMP */
  48347. + "x-promisc", /* SADB_X_PROMISC */
  48348. + "x-pchange", /* SADB_X_PCHANGE */
  48349. + "x-groupsa", /* SADB_X_GRPSA */
  48350. + "x-addflow(eroute)", /* SADB_X_ADDFLOW */
  48351. + "x-delflow(eroute)", /* SADB_X_DELFLOW */
  48352. + "x-debug", /* SADB_X_DEBUG */
  48353. +};
  48354. +
  48355. +const char *
  48356. +pfkey_v2_sadb_type_string(int sadb_type)
  48357. +{
  48358. + if(sadb_type <= SADB_MAX) {
  48359. + return pfkey_sadb_type_strings[sadb_type];
  48360. + } else {
  48361. + return "unknown-sadb-type";
  48362. + }
  48363. +}
  48364. +
  48365. +
  48366. +
  48367. +
  48368. +/*
  48369. + * $Log: pfkey_v2_debug.c,v $
  48370. + * Revision 1.11 2005/04/06 17:45:16 mcr
  48371. + * always include NAT-T names.
  48372. + *
  48373. + * Revision 1.10 2004/07/10 07:48:35 mcr
  48374. + * Moved from linux/lib/libfreeswan/pfkey_v2_debug.c,v
  48375. + *
  48376. + * Revision 1.9 2004/03/08 01:59:08 ken
  48377. + * freeswan.h -> openswan.h
  48378. + *
  48379. + * Revision 1.8 2003/12/10 01:20:19 mcr
  48380. + * NAT-traversal patches to KLIPS.
  48381. + *
  48382. + * Revision 1.7 2002/09/20 05:01:26 rgb
  48383. + * Fixed limit inclusion error in both type and ext string conversion.
  48384. + *
  48385. + * Revision 1.6 2002/04/24 07:55:32 mcr
  48386. + * #include patches and Makefiles for post-reorg compilation.
  48387. + *
  48388. + * Revision 1.5 2002/04/24 07:36:40 mcr
  48389. + * Moved from ./lib/pfkey_v2_debug.c,v
  48390. + *
  48391. + * Revision 1.4 2002/01/29 22:25:36 rgb
  48392. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  48393. + *
  48394. + * Revision 1.3 2002/01/29 01:59:09 mcr
  48395. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  48396. + * updating of IPv6 structures to match latest in6.h version.
  48397. + * removed dead code from openswan.h that also duplicated kversions.h
  48398. + * code.
  48399. + *
  48400. + * Revision 1.2 2002/01/20 20:34:50 mcr
  48401. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  48402. + *
  48403. + * Revision 1.1 2001/11/27 05:30:06 mcr
  48404. + * initial set of debug strings for pfkey debugging.
  48405. + * this will eventually only be included for debug builds.
  48406. + *
  48407. + * Revision 1.1 2001/09/21 04:12:03 mcr
  48408. + * first compilable version.
  48409. + *
  48410. + *
  48411. + * Local variables:
  48412. + * c-file-style: "linux"
  48413. + * End:
  48414. + *
  48415. + */
  48416. --- /dev/null Tue Mar 11 13:02:56 2003
  48417. +++ linux/net/ipsec/pfkey_v2_ext_bits.c Mon Feb 9 13:51:03 2004
  48418. @@ -0,0 +1,814 @@
  48419. +/*
  48420. + * RFC2367 PF_KEYv2 Key management API message parser
  48421. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  48422. + *
  48423. + * This program is free software; you can redistribute it and/or modify it
  48424. + * under the terms of the GNU General Public License as published by the
  48425. + * Free Software Foundation; either version 2 of the License, or (at your
  48426. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  48427. + *
  48428. + * This program is distributed in the hope that it will be useful, but
  48429. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  48430. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  48431. + * for more details.
  48432. + *
  48433. + * RCSID $Id: pfkey_v2_ext_bits.c,v 1.22 2005/05/11 01:45:31 mcr Exp $
  48434. + */
  48435. +
  48436. +/*
  48437. + * Template from klips/net/ipsec/ipsec/ipsec_parse.c.
  48438. + */
  48439. +
  48440. +char pfkey_v2_ext_bits_c_version[] = "$Id: pfkey_v2_ext_bits.c,v 1.22 2005/05/11 01:45:31 mcr Exp $";
  48441. +
  48442. +/*
  48443. + * Some ugly stuff to allow consistent debugging code for use in the
  48444. + * kernel and in user space
  48445. +*/
  48446. +
  48447. +#ifdef __KERNEL__
  48448. +
  48449. +# include <linux/kernel.h> /* for printk */
  48450. +
  48451. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  48452. +# ifdef MALLOC_SLAB
  48453. +# include <linux/slab.h> /* kmalloc() */
  48454. +# else /* MALLOC_SLAB */
  48455. +# include <linux/malloc.h> /* kmalloc() */
  48456. +# endif /* MALLOC_SLAB */
  48457. +# include <linux/errno.h> /* error codes */
  48458. +# include <linux/types.h> /* size_t */
  48459. +# include <linux/interrupt.h> /* mark_bh */
  48460. +
  48461. +# include <linux/netdevice.h> /* struct device, and other headers */
  48462. +# include <linux/etherdevice.h> /* eth_type_trans */
  48463. +# include <linux/ip.h> /* struct iphdr */
  48464. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  48465. +# include <linux/ipv6.h>
  48466. +# endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  48467. +
  48468. +#else /* __KERNEL__ */
  48469. +
  48470. +# include <sys/types.h>
  48471. +# include <linux/types.h>
  48472. +# include <linux/errno.h>
  48473. +#endif
  48474. +
  48475. +#include <openswan.h>
  48476. +#include <pfkeyv2.h>
  48477. +#include <pfkey.h>
  48478. +
  48479. +unsigned int extensions_bitmaps[2/*in/out*/][2/*perm/req*/][SADB_EXTENSIONS_MAX] = {
  48480. +
  48481. +/* INBOUND EXTENSIONS */
  48482. +{
  48483. +
  48484. +/* PERMITTED IN */
  48485. +{
  48486. +/* SADB_RESERVED */
  48487. +0
  48488. +,
  48489. +/* SADB_GETSPI */
  48490. +1<<SADB_EXT_RESERVED
  48491. +| 1<<SADB_EXT_ADDRESS_SRC
  48492. +| 1<<SADB_EXT_ADDRESS_DST
  48493. +| 1<<SADB_EXT_ADDRESS_PROXY
  48494. +| 1<<SADB_EXT_SPIRANGE
  48495. +,
  48496. +/* SADB_UPDATE */
  48497. +1<<SADB_EXT_RESERVED
  48498. +| 1<<SADB_EXT_SA
  48499. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48500. +| 1<<SADB_EXT_LIFETIME_HARD
  48501. +| 1<<SADB_EXT_LIFETIME_SOFT
  48502. +| 1<<SADB_EXT_ADDRESS_SRC
  48503. +| 1<<SADB_EXT_ADDRESS_DST
  48504. +| 1<<SADB_EXT_ADDRESS_PROXY
  48505. +| 1<<SADB_EXT_KEY_AUTH
  48506. +| 1<<SADB_EXT_KEY_ENCRYPT
  48507. +| 1<<SADB_EXT_IDENTITY_SRC
  48508. +| 1<<SADB_EXT_IDENTITY_DST
  48509. +| 1<<SADB_EXT_SENSITIVITY
  48510. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48511. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48512. +,
  48513. +/* SADB_ADD */
  48514. +1<<SADB_EXT_RESERVED
  48515. +| 1<<SADB_EXT_SA
  48516. +| 1<<SADB_EXT_LIFETIME_HARD
  48517. +| 1<<SADB_EXT_LIFETIME_SOFT
  48518. +| 1<<SADB_EXT_ADDRESS_SRC
  48519. +| 1<<SADB_EXT_ADDRESS_DST
  48520. +| 1<<SADB_EXT_ADDRESS_PROXY
  48521. +| 1<<SADB_EXT_KEY_AUTH
  48522. +| 1<<SADB_EXT_KEY_ENCRYPT
  48523. +| 1<<SADB_EXT_IDENTITY_SRC
  48524. +| 1<<SADB_EXT_IDENTITY_DST
  48525. +| 1<<SADB_EXT_SENSITIVITY
  48526. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48527. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48528. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48529. +| 1<<SADB_X_EXT_NAT_T_OA
  48530. +,
  48531. +/* SADB_DELETE */
  48532. +1<<SADB_EXT_RESERVED
  48533. +| 1<<SADB_EXT_SA
  48534. +| 1<<SADB_EXT_ADDRESS_SRC
  48535. +| 1<<SADB_EXT_ADDRESS_DST
  48536. +,
  48537. +/* SADB_GET */
  48538. +1<<SADB_EXT_RESERVED
  48539. +| 1<<SADB_EXT_SA
  48540. +| 1<<SADB_EXT_ADDRESS_SRC
  48541. +| 1<<SADB_EXT_ADDRESS_DST
  48542. +,
  48543. +/* SADB_ACQUIRE */
  48544. +1<<SADB_EXT_RESERVED
  48545. +| 1<<SADB_EXT_ADDRESS_SRC
  48546. +| 1<<SADB_EXT_ADDRESS_DST
  48547. +| 1<<SADB_EXT_ADDRESS_PROXY
  48548. +| 1<<SADB_EXT_IDENTITY_SRC
  48549. +| 1<<SADB_EXT_IDENTITY_DST
  48550. +| 1<<SADB_EXT_SENSITIVITY
  48551. +| 1<<SADB_EXT_PROPOSAL
  48552. +,
  48553. +/* SADB_REGISTER */
  48554. +1<<SADB_EXT_RESERVED
  48555. +,
  48556. +/* SADB_EXPIRE */
  48557. +0
  48558. +,
  48559. +/* SADB_FLUSH */
  48560. +1<<SADB_EXT_RESERVED
  48561. +,
  48562. +/* SADB_DUMP */
  48563. +1<<SADB_EXT_RESERVED
  48564. +,
  48565. +/* SADB_X_PROMISC */
  48566. +1<<SADB_EXT_RESERVED
  48567. +| 1<<SADB_EXT_SA
  48568. +| 1<<SADB_EXT_LIFETIME_CURRENT
  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_EXT_PROPOSAL
  48580. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48581. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48582. +| 1<<SADB_EXT_SPIRANGE
  48583. +| 1<<SADB_X_EXT_KMPRIVATE
  48584. +| 1<<SADB_X_EXT_SATYPE2
  48585. +| 1<<SADB_X_EXT_SA2
  48586. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48587. +,
  48588. +/* SADB_X_PCHANGE */
  48589. +1<<SADB_EXT_RESERVED
  48590. +| 1<<SADB_EXT_SA
  48591. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48592. +| 1<<SADB_EXT_LIFETIME_HARD
  48593. +| 1<<SADB_EXT_LIFETIME_SOFT
  48594. +| 1<<SADB_EXT_ADDRESS_SRC
  48595. +| 1<<SADB_EXT_ADDRESS_DST
  48596. +| 1<<SADB_EXT_ADDRESS_PROXY
  48597. +| 1<<SADB_EXT_KEY_AUTH
  48598. +| 1<<SADB_EXT_KEY_ENCRYPT
  48599. +| 1<<SADB_EXT_IDENTITY_SRC
  48600. +| 1<<SADB_EXT_IDENTITY_DST
  48601. +| 1<<SADB_EXT_SENSITIVITY
  48602. +| 1<<SADB_EXT_PROPOSAL
  48603. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48604. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48605. +| 1<<SADB_EXT_SPIRANGE
  48606. +| 1<<SADB_X_EXT_KMPRIVATE
  48607. +| 1<<SADB_X_EXT_SATYPE2
  48608. +| 1<<SADB_X_EXT_SA2
  48609. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48610. +,
  48611. +/* SADB_X_GRPSA */
  48612. +1<<SADB_EXT_RESERVED
  48613. +| 1<<SADB_EXT_SA
  48614. +| 1<<SADB_EXT_ADDRESS_DST
  48615. +| 1<<SADB_X_EXT_SATYPE2
  48616. +| 1<<SADB_X_EXT_SA2
  48617. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48618. +,
  48619. +/* SADB_X_ADDFLOW */
  48620. +1<<SADB_EXT_RESERVED
  48621. +| 1<<SADB_EXT_SA
  48622. +| 1<<SADB_EXT_ADDRESS_SRC
  48623. +| 1<<SADB_EXT_ADDRESS_DST
  48624. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48625. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48626. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48627. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48628. +| 1<<SADB_EXT_IDENTITY_SRC
  48629. +| 1<<SADB_EXT_IDENTITY_DST
  48630. +| 1<<SADB_X_EXT_PROTOCOL
  48631. +,
  48632. +/* SADB_X_DELFLOW */
  48633. +1<<SADB_EXT_RESERVED
  48634. +| 1<<SADB_EXT_SA
  48635. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48636. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48637. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48638. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48639. +| 1<<SADB_EXT_IDENTITY_SRC
  48640. +| 1<<SADB_EXT_IDENTITY_DST
  48641. +| 1<<SADB_X_EXT_PROTOCOL
  48642. +,
  48643. +/* SADB_X_DEBUG */
  48644. +1<<SADB_EXT_RESERVED
  48645. +| 1<<SADB_X_EXT_DEBUG
  48646. +,
  48647. +/* SADB_X_NAT_T_NEW_MAPPING */
  48648. +1<<SADB_EXT_RESERVED
  48649. +| 1<<SADB_EXT_SA
  48650. +| 1<<SADB_EXT_ADDRESS_SRC
  48651. +| 1<<SADB_EXT_ADDRESS_DST
  48652. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48653. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48654. +},
  48655. +
  48656. +/* REQUIRED IN */
  48657. +{
  48658. +/* SADB_RESERVED */
  48659. +0
  48660. +,
  48661. +/* SADB_GETSPI */
  48662. +1<<SADB_EXT_RESERVED
  48663. +| 1<<SADB_EXT_ADDRESS_SRC
  48664. +| 1<<SADB_EXT_ADDRESS_DST
  48665. +| 1<<SADB_EXT_SPIRANGE
  48666. +,
  48667. +/* SADB_UPDATE */
  48668. +1<<SADB_EXT_RESERVED
  48669. +| 1<<SADB_EXT_SA
  48670. +| 1<<SADB_EXT_ADDRESS_SRC
  48671. +| 1<<SADB_EXT_ADDRESS_DST
  48672. +/*| 1<<SADB_EXT_KEY_AUTH*/
  48673. +/*| 1<<SADB_EXT_KEY_ENCRYPT*/
  48674. +,
  48675. +/* SADB_ADD */
  48676. +1<<SADB_EXT_RESERVED
  48677. +| 1<<SADB_EXT_SA
  48678. +| 1<<SADB_EXT_ADDRESS_SRC
  48679. +| 1<<SADB_EXT_ADDRESS_DST
  48680. +/*| 1<<SADB_EXT_KEY_AUTH*/
  48681. +/*| 1<<SADB_EXT_KEY_ENCRYPT*/
  48682. +,
  48683. +/* SADB_DELETE */
  48684. +1<<SADB_EXT_RESERVED
  48685. +| 1<<SADB_EXT_SA
  48686. +| 1<<SADB_EXT_ADDRESS_SRC
  48687. +| 1<<SADB_EXT_ADDRESS_DST
  48688. +,
  48689. +/* SADB_GET */
  48690. +1<<SADB_EXT_RESERVED
  48691. +| 1<<SADB_EXT_SA
  48692. +| 1<<SADB_EXT_ADDRESS_SRC
  48693. +| 1<<SADB_EXT_ADDRESS_DST
  48694. +,
  48695. +/* SADB_ACQUIRE */
  48696. +1<<SADB_EXT_RESERVED
  48697. +| 1<<SADB_EXT_ADDRESS_SRC
  48698. +| 1<<SADB_EXT_ADDRESS_DST
  48699. +| 1<<SADB_EXT_PROPOSAL
  48700. +,
  48701. +/* SADB_REGISTER */
  48702. +1<<SADB_EXT_RESERVED
  48703. +,
  48704. +/* SADB_EXPIRE */
  48705. +0
  48706. +,
  48707. +/* SADB_FLUSH */
  48708. +1<<SADB_EXT_RESERVED
  48709. +,
  48710. +/* SADB_DUMP */
  48711. +1<<SADB_EXT_RESERVED
  48712. +,
  48713. +/* SADB_X_PROMISC */
  48714. +1<<SADB_EXT_RESERVED
  48715. +| 1<<SADB_EXT_SA
  48716. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48717. +| 1<<SADB_EXT_LIFETIME_HARD
  48718. +| 1<<SADB_EXT_LIFETIME_SOFT
  48719. +| 1<<SADB_EXT_ADDRESS_SRC
  48720. +| 1<<SADB_EXT_ADDRESS_DST
  48721. +| 1<<SADB_EXT_ADDRESS_PROXY
  48722. +| 1<<SADB_EXT_KEY_AUTH
  48723. +| 1<<SADB_EXT_KEY_ENCRYPT
  48724. +| 1<<SADB_EXT_IDENTITY_SRC
  48725. +| 1<<SADB_EXT_IDENTITY_DST
  48726. +| 1<<SADB_EXT_SENSITIVITY
  48727. +| 1<<SADB_EXT_PROPOSAL
  48728. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48729. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48730. +| 1<<SADB_EXT_SPIRANGE
  48731. +| 1<<SADB_X_EXT_KMPRIVATE
  48732. +| 1<<SADB_X_EXT_SATYPE2
  48733. +| 1<<SADB_X_EXT_SA2
  48734. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48735. +,
  48736. +/* SADB_X_PCHANGE */
  48737. +1<<SADB_EXT_RESERVED
  48738. +| 1<<SADB_EXT_SA
  48739. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48740. +| 1<<SADB_EXT_LIFETIME_HARD
  48741. +| 1<<SADB_EXT_LIFETIME_SOFT
  48742. +| 1<<SADB_EXT_ADDRESS_SRC
  48743. +| 1<<SADB_EXT_ADDRESS_DST
  48744. +| 1<<SADB_EXT_ADDRESS_PROXY
  48745. +| 1<<SADB_EXT_KEY_AUTH
  48746. +| 1<<SADB_EXT_KEY_ENCRYPT
  48747. +| 1<<SADB_EXT_IDENTITY_SRC
  48748. +| 1<<SADB_EXT_IDENTITY_DST
  48749. +| 1<<SADB_EXT_SENSITIVITY
  48750. +| 1<<SADB_EXT_PROPOSAL
  48751. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48752. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48753. +| 1<<SADB_EXT_SPIRANGE
  48754. +| 1<<SADB_X_EXT_KMPRIVATE
  48755. +| 1<<SADB_X_EXT_SATYPE2
  48756. +| 1<<SADB_X_EXT_SA2
  48757. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48758. +,
  48759. +/* SADB_X_GRPSA */
  48760. +1<<SADB_EXT_RESERVED
  48761. +| 1<<SADB_EXT_SA
  48762. +| 1<<SADB_EXT_ADDRESS_DST
  48763. +/*| 1<<SADB_X_EXT_SATYPE2*/
  48764. +/*| 1<<SADB_X_EXT_SA2*/
  48765. +/*| 1<<SADB_X_EXT_ADDRESS_DST2*/
  48766. +,
  48767. +/* SADB_X_ADDFLOW */
  48768. +1<<SADB_EXT_RESERVED
  48769. +| 1<<SADB_EXT_SA
  48770. +| 1<<SADB_EXT_ADDRESS_DST
  48771. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48772. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48773. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48774. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48775. +,
  48776. +/* SADB_X_DELFLOW */
  48777. +1<<SADB_EXT_RESERVED
  48778. +/*| 1<<SADB_EXT_SA*/
  48779. +#if 0 /* SADB_X_CLREROUTE doesn't need all these... */
  48780. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48781. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48782. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48783. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48784. +#endif
  48785. +,
  48786. +/* SADB_X_DEBUG */
  48787. +1<<SADB_EXT_RESERVED
  48788. +| 1<<SADB_X_EXT_DEBUG
  48789. +,
  48790. +/* SADB_X_NAT_T_NEW_MAPPING */
  48791. +1<<SADB_EXT_RESERVED
  48792. +| 1<<SADB_EXT_SA
  48793. +| 1<<SADB_EXT_ADDRESS_SRC
  48794. +| 1<<SADB_EXT_ADDRESS_DST
  48795. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48796. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48797. +}
  48798. +
  48799. +},
  48800. +
  48801. +/* OUTBOUND EXTENSIONS */
  48802. +{
  48803. +
  48804. +/* PERMITTED OUT */
  48805. +{
  48806. +/* SADB_RESERVED */
  48807. +0
  48808. +,
  48809. +/* SADB_GETSPI */
  48810. +1<<SADB_EXT_RESERVED
  48811. +| 1<<SADB_EXT_SA
  48812. +| 1<<SADB_EXT_ADDRESS_SRC
  48813. +| 1<<SADB_EXT_ADDRESS_DST
  48814. +,
  48815. +/* SADB_UPDATE */
  48816. +1<<SADB_EXT_RESERVED
  48817. +| 1<<SADB_EXT_SA
  48818. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48819. +| 1<<SADB_EXT_LIFETIME_HARD
  48820. +| 1<<SADB_EXT_LIFETIME_SOFT
  48821. +| 1<<SADB_EXT_ADDRESS_SRC
  48822. +| 1<<SADB_EXT_ADDRESS_DST
  48823. +| 1<<SADB_EXT_ADDRESS_PROXY
  48824. +| 1<<SADB_EXT_IDENTITY_SRC
  48825. +| 1<<SADB_EXT_IDENTITY_DST
  48826. +| 1<<SADB_EXT_SENSITIVITY
  48827. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48828. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48829. +,
  48830. +/* SADB_ADD */
  48831. +1<<SADB_EXT_RESERVED
  48832. +| 1<<SADB_EXT_SA
  48833. +| 1<<SADB_EXT_LIFETIME_HARD
  48834. +| 1<<SADB_EXT_LIFETIME_SOFT
  48835. +| 1<<SADB_EXT_ADDRESS_SRC
  48836. +| 1<<SADB_EXT_ADDRESS_DST
  48837. +| 1<<SADB_EXT_IDENTITY_SRC
  48838. +| 1<<SADB_EXT_IDENTITY_DST
  48839. +| 1<<SADB_EXT_SENSITIVITY
  48840. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48841. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48842. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48843. +| 1<<SADB_X_EXT_NAT_T_OA
  48844. +,
  48845. +/* SADB_DELETE */
  48846. +1<<SADB_EXT_RESERVED
  48847. +| 1<<SADB_EXT_SA
  48848. +| 1<<SADB_EXT_ADDRESS_SRC
  48849. +| 1<<SADB_EXT_ADDRESS_DST
  48850. +,
  48851. +/* SADB_GET */
  48852. +1<<SADB_EXT_RESERVED
  48853. +| 1<<SADB_EXT_SA
  48854. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48855. +| 1<<SADB_EXT_LIFETIME_HARD
  48856. +| 1<<SADB_EXT_LIFETIME_SOFT
  48857. +| 1<<SADB_EXT_ADDRESS_SRC
  48858. +| 1<<SADB_EXT_ADDRESS_DST
  48859. +| 1<<SADB_EXT_ADDRESS_PROXY
  48860. +| 1<<SADB_EXT_KEY_AUTH
  48861. +| 1<<SADB_EXT_KEY_ENCRYPT
  48862. +| 1<<SADB_EXT_IDENTITY_SRC
  48863. +| 1<<SADB_EXT_IDENTITY_DST
  48864. +| 1<<SADB_EXT_SENSITIVITY
  48865. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48866. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48867. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48868. +| 1<<SADB_X_EXT_NAT_T_OA
  48869. +,
  48870. +/* SADB_ACQUIRE */
  48871. +1<<SADB_EXT_RESERVED
  48872. +| 1<<SADB_EXT_ADDRESS_SRC
  48873. +| 1<<SADB_EXT_ADDRESS_DST
  48874. +| 1<<SADB_EXT_ADDRESS_PROXY
  48875. +| 1<<SADB_EXT_IDENTITY_SRC
  48876. +| 1<<SADB_EXT_IDENTITY_DST
  48877. +| 1<<SADB_EXT_SENSITIVITY
  48878. +| 1<<SADB_EXT_PROPOSAL
  48879. +,
  48880. +/* SADB_REGISTER */
  48881. +1<<SADB_EXT_RESERVED
  48882. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48883. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48884. +,
  48885. +/* SADB_EXPIRE */
  48886. +1<<SADB_EXT_RESERVED
  48887. +| 1<<SADB_EXT_SA
  48888. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48889. +| 1<<SADB_EXT_LIFETIME_HARD
  48890. +| 1<<SADB_EXT_LIFETIME_SOFT
  48891. +| 1<<SADB_EXT_ADDRESS_SRC
  48892. +| 1<<SADB_EXT_ADDRESS_DST
  48893. +,
  48894. +/* SADB_FLUSH */
  48895. +1<<SADB_EXT_RESERVED
  48896. +,
  48897. +/* SADB_DUMP */
  48898. +1<<SADB_EXT_RESERVED
  48899. +| 1<<SADB_EXT_SA
  48900. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48901. +| 1<<SADB_EXT_LIFETIME_HARD
  48902. +| 1<<SADB_EXT_LIFETIME_SOFT
  48903. +| 1<<SADB_EXT_ADDRESS_SRC
  48904. +| 1<<SADB_EXT_ADDRESS_DST
  48905. +| 1<<SADB_EXT_ADDRESS_PROXY
  48906. +| 1<<SADB_EXT_KEY_AUTH
  48907. +| 1<<SADB_EXT_KEY_ENCRYPT
  48908. +| 1<<SADB_EXT_IDENTITY_SRC
  48909. +| 1<<SADB_EXT_IDENTITY_DST
  48910. +| 1<<SADB_EXT_SENSITIVITY
  48911. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48912. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48913. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48914. +| 1<<SADB_X_EXT_NAT_T_OA
  48915. +,
  48916. +/* SADB_X_PROMISC */
  48917. +1<<SADB_EXT_RESERVED
  48918. +| 1<<SADB_EXT_SA
  48919. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48920. +| 1<<SADB_EXT_LIFETIME_HARD
  48921. +| 1<<SADB_EXT_LIFETIME_SOFT
  48922. +| 1<<SADB_EXT_ADDRESS_SRC
  48923. +| 1<<SADB_EXT_ADDRESS_DST
  48924. +| 1<<SADB_EXT_ADDRESS_PROXY
  48925. +| 1<<SADB_EXT_KEY_AUTH
  48926. +| 1<<SADB_EXT_KEY_ENCRYPT
  48927. +| 1<<SADB_EXT_IDENTITY_SRC
  48928. +| 1<<SADB_EXT_IDENTITY_DST
  48929. +| 1<<SADB_EXT_SENSITIVITY
  48930. +| 1<<SADB_EXT_PROPOSAL
  48931. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48932. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48933. +| 1<<SADB_EXT_SPIRANGE
  48934. +| 1<<SADB_X_EXT_KMPRIVATE
  48935. +| 1<<SADB_X_EXT_SATYPE2
  48936. +| 1<<SADB_X_EXT_SA2
  48937. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48938. +,
  48939. +/* SADB_X_PCHANGE */
  48940. +1<<SADB_EXT_RESERVED
  48941. +| 1<<SADB_EXT_SA
  48942. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48943. +| 1<<SADB_EXT_LIFETIME_HARD
  48944. +| 1<<SADB_EXT_LIFETIME_SOFT
  48945. +| 1<<SADB_EXT_ADDRESS_SRC
  48946. +| 1<<SADB_EXT_ADDRESS_DST
  48947. +| 1<<SADB_EXT_ADDRESS_PROXY
  48948. +| 1<<SADB_EXT_KEY_AUTH
  48949. +| 1<<SADB_EXT_KEY_ENCRYPT
  48950. +| 1<<SADB_EXT_IDENTITY_SRC
  48951. +| 1<<SADB_EXT_IDENTITY_DST
  48952. +| 1<<SADB_EXT_SENSITIVITY
  48953. +| 1<<SADB_EXT_PROPOSAL
  48954. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48955. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48956. +| 1<<SADB_EXT_SPIRANGE
  48957. +| 1<<SADB_X_EXT_KMPRIVATE
  48958. +| 1<<SADB_X_EXT_SATYPE2
  48959. +| 1<<SADB_X_EXT_SA2
  48960. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48961. +,
  48962. +/* SADB_X_GRPSA */
  48963. +1<<SADB_EXT_RESERVED
  48964. +| 1<<SADB_EXT_SA
  48965. +| 1<<SADB_EXT_ADDRESS_DST
  48966. +| 1<<SADB_X_EXT_SATYPE2
  48967. +| 1<<SADB_X_EXT_SA2
  48968. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48969. +,
  48970. +/* SADB_X_ADDFLOW */
  48971. +1<<SADB_EXT_RESERVED
  48972. +| 1<<SADB_EXT_SA
  48973. +| 1<<SADB_EXT_ADDRESS_SRC
  48974. +| 1<<SADB_EXT_ADDRESS_DST
  48975. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48976. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48977. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48978. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48979. +| 1<<SADB_X_EXT_PROTOCOL
  48980. +,
  48981. +/* SADB_X_DELFLOW */
  48982. +1<<SADB_EXT_RESERVED
  48983. +| 1<<SADB_EXT_SA
  48984. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48985. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48986. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48987. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48988. +| 1<<SADB_X_EXT_PROTOCOL
  48989. +,
  48990. +/* SADB_X_DEBUG */
  48991. +1<<SADB_EXT_RESERVED
  48992. +| 1<<SADB_X_EXT_DEBUG
  48993. +,
  48994. +/* SADB_X_NAT_T_NEW_MAPPING */
  48995. +1<<SADB_EXT_RESERVED
  48996. +| 1<<SADB_EXT_SA
  48997. +| 1<<SADB_EXT_ADDRESS_SRC
  48998. +| 1<<SADB_EXT_ADDRESS_DST
  48999. +| 1<<SADB_X_EXT_NAT_T_SPORT
  49000. +| 1<<SADB_X_EXT_NAT_T_DPORT
  49001. +},
  49002. +
  49003. +/* REQUIRED OUT */
  49004. +{
  49005. +/* SADB_RESERVED */
  49006. +0
  49007. +,
  49008. +/* SADB_GETSPI */
  49009. +1<<SADB_EXT_RESERVED
  49010. +| 1<<SADB_EXT_SA
  49011. +| 1<<SADB_EXT_ADDRESS_SRC
  49012. +| 1<<SADB_EXT_ADDRESS_DST
  49013. +,
  49014. +/* SADB_UPDATE */
  49015. +1<<SADB_EXT_RESERVED
  49016. +| 1<<SADB_EXT_SA
  49017. +| 1<<SADB_EXT_ADDRESS_SRC
  49018. +| 1<<SADB_EXT_ADDRESS_DST
  49019. +,
  49020. +/* SADB_ADD */
  49021. +1<<SADB_EXT_RESERVED
  49022. +| 1<<SADB_EXT_SA
  49023. +| 1<<SADB_EXT_ADDRESS_SRC
  49024. +| 1<<SADB_EXT_ADDRESS_DST
  49025. +,
  49026. +/* SADB_DELETE */
  49027. +1<<SADB_EXT_RESERVED
  49028. +| 1<<SADB_EXT_SA
  49029. +| 1<<SADB_EXT_ADDRESS_SRC
  49030. +| 1<<SADB_EXT_ADDRESS_DST
  49031. +,
  49032. +/* SADB_GET */
  49033. +1<<SADB_EXT_RESERVED
  49034. +| 1<<SADB_EXT_SA
  49035. +| 1<<SADB_EXT_ADDRESS_SRC
  49036. +| 1<<SADB_EXT_ADDRESS_DST
  49037. +/* | 1<<SADB_EXT_KEY_AUTH */
  49038. +/* | 1<<SADB_EXT_KEY_ENCRYPT */
  49039. +,
  49040. +/* SADB_ACQUIRE */
  49041. +1<<SADB_EXT_RESERVED
  49042. +| 1<<SADB_EXT_ADDRESS_SRC
  49043. +| 1<<SADB_EXT_ADDRESS_DST
  49044. +| 1<<SADB_EXT_PROPOSAL
  49045. +,
  49046. +/* SADB_REGISTER */
  49047. +1<<SADB_EXT_RESERVED
  49048. +/* | 1<<SADB_EXT_SUPPORTED_AUTH
  49049. + | 1<<SADB_EXT_SUPPORTED_ENCRYPT */
  49050. +,
  49051. +/* SADB_EXPIRE */
  49052. +1<<SADB_EXT_RESERVED
  49053. +| 1<<SADB_EXT_SA
  49054. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49055. +/* | 1<<SADB_EXT_LIFETIME_HARD
  49056. + | 1<<SADB_EXT_LIFETIME_SOFT */
  49057. +| 1<<SADB_EXT_ADDRESS_SRC
  49058. +| 1<<SADB_EXT_ADDRESS_DST
  49059. +,
  49060. +/* SADB_FLUSH */
  49061. +1<<SADB_EXT_RESERVED
  49062. +,
  49063. +/* SADB_DUMP */
  49064. +1<<SADB_EXT_RESERVED
  49065. +| 1<<SADB_EXT_SA
  49066. +| 1<<SADB_EXT_ADDRESS_SRC
  49067. +| 1<<SADB_EXT_ADDRESS_DST
  49068. +| 1<<SADB_EXT_KEY_AUTH
  49069. +| 1<<SADB_EXT_KEY_ENCRYPT
  49070. +,
  49071. +/* SADB_X_PROMISC */
  49072. +1<<SADB_EXT_RESERVED
  49073. +| 1<<SADB_EXT_SA
  49074. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49075. +| 1<<SADB_EXT_LIFETIME_HARD
  49076. +| 1<<SADB_EXT_LIFETIME_SOFT
  49077. +| 1<<SADB_EXT_ADDRESS_SRC
  49078. +| 1<<SADB_EXT_ADDRESS_DST
  49079. +| 1<<SADB_EXT_ADDRESS_PROXY
  49080. +| 1<<SADB_EXT_KEY_AUTH
  49081. +| 1<<SADB_EXT_KEY_ENCRYPT
  49082. +| 1<<SADB_EXT_IDENTITY_SRC
  49083. +| 1<<SADB_EXT_IDENTITY_DST
  49084. +| 1<<SADB_EXT_SENSITIVITY
  49085. +| 1<<SADB_EXT_PROPOSAL
  49086. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49087. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49088. +| 1<<SADB_EXT_SPIRANGE
  49089. +| 1<<SADB_X_EXT_KMPRIVATE
  49090. +| 1<<SADB_X_EXT_SATYPE2
  49091. +| 1<<SADB_X_EXT_SA2
  49092. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49093. +,
  49094. +/* SADB_X_PCHANGE */
  49095. +1<<SADB_EXT_RESERVED
  49096. +| 1<<SADB_EXT_SA
  49097. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49098. +| 1<<SADB_EXT_LIFETIME_HARD
  49099. +| 1<<SADB_EXT_LIFETIME_SOFT
  49100. +| 1<<SADB_EXT_ADDRESS_SRC
  49101. +| 1<<SADB_EXT_ADDRESS_DST
  49102. +| 1<<SADB_EXT_ADDRESS_PROXY
  49103. +| 1<<SADB_EXT_KEY_AUTH
  49104. +| 1<<SADB_EXT_KEY_ENCRYPT
  49105. +| 1<<SADB_EXT_IDENTITY_SRC
  49106. +| 1<<SADB_EXT_IDENTITY_DST
  49107. +| 1<<SADB_EXT_SENSITIVITY
  49108. +| 1<<SADB_EXT_PROPOSAL
  49109. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49110. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49111. +| 1<<SADB_EXT_SPIRANGE
  49112. +| 1<<SADB_X_EXT_KMPRIVATE
  49113. +| 1<<SADB_X_EXT_SATYPE2
  49114. +| 1<<SADB_X_EXT_SA2
  49115. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49116. +,
  49117. +/* SADB_X_GRPSA */
  49118. +1<<SADB_EXT_RESERVED
  49119. +| 1<<SADB_EXT_SA
  49120. +| 1<<SADB_EXT_ADDRESS_DST
  49121. +,
  49122. +/* SADB_X_ADDFLOW */
  49123. +1<<SADB_EXT_RESERVED
  49124. +| 1<<SADB_EXT_SA
  49125. +| 1<<SADB_EXT_ADDRESS_DST
  49126. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49127. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49128. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49129. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49130. +,
  49131. +/* SADB_X_DELFLOW */
  49132. +1<<SADB_EXT_RESERVED
  49133. +/*| 1<<SADB_EXT_SA*/
  49134. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49135. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49136. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49137. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49138. +,
  49139. +/* SADB_X_DEBUG */
  49140. +1<<SADB_EXT_RESERVED
  49141. +| 1<<SADB_X_EXT_DEBUG
  49142. +,
  49143. +/* SADB_X_NAT_T_NEW_MAPPING */
  49144. +1<<SADB_EXT_RESERVED
  49145. +| 1<<SADB_EXT_SA
  49146. +| 1<<SADB_EXT_ADDRESS_SRC
  49147. +| 1<<SADB_EXT_ADDRESS_DST
  49148. +| 1<<SADB_X_EXT_NAT_T_SPORT
  49149. +| 1<<SADB_X_EXT_NAT_T_DPORT
  49150. +}
  49151. +}
  49152. +};
  49153. +
  49154. +/*
  49155. + * $Log: pfkey_v2_ext_bits.c,v $
  49156. + * Revision 1.22 2005/05/11 01:45:31 mcr
  49157. + * make pfkey.h standalone.
  49158. + *
  49159. + * Revision 1.21 2004/07/10 07:48:36 mcr
  49160. + * Moved from linux/lib/libfreeswan/pfkey_v2_ext_bits.c,v
  49161. + *
  49162. + * Revision 1.20 2004/03/08 01:59:08 ken
  49163. + * freeswan.h -> openswan.h
  49164. + *
  49165. + * Revision 1.19 2003/12/22 21:38:13 mcr
  49166. + * removed extraenous #endif.
  49167. + *
  49168. + * Revision 1.18 2003/12/22 19:34:41 mcr
  49169. + * added 0.6c NAT-T patch.
  49170. + *
  49171. + * Revision 1.17 2003/12/10 01:20:19 mcr
  49172. + * NAT-traversal patches to KLIPS.
  49173. + *
  49174. + * Revision 1.16 2003/10/31 02:27:12 mcr
  49175. + * pulled up port-selector patches and sa_id elimination.
  49176. + *
  49177. + * Revision 1.15.30.1 2003/09/21 13:59:44 mcr
  49178. + * pre-liminary X.509 patch - does not yet pass tests.
  49179. + *
  49180. + * Revision 1.15 2002/04/24 07:55:32 mcr
  49181. + * #include patches and Makefiles for post-reorg compilation.
  49182. + *
  49183. + * Revision 1.14 2002/04/24 07:36:40 mcr
  49184. + * Moved from ./lib/pfkey_v2_ext_bits.c,v
  49185. + *
  49186. + * Revision 1.13 2002/01/29 22:25:36 rgb
  49187. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  49188. + *
  49189. + * Revision 1.12 2002/01/29 01:59:10 mcr
  49190. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  49191. + * updating of IPv6 structures to match latest in6.h version.
  49192. + * removed dead code from openswan.h that also duplicated kversions.h
  49193. + * code.
  49194. + *
  49195. + * Revision 1.11 2001/10/18 04:45:24 rgb
  49196. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  49197. + * lib/openswan.h version macros moved to lib/kversions.h.
  49198. + * Other compiler directive cleanups.
  49199. + *
  49200. + * Revision 1.10 2001/09/08 21:13:35 rgb
  49201. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  49202. + *
  49203. + * Revision 1.9 2001/06/14 19:35:16 rgb
  49204. + * Update copyright date.
  49205. + *
  49206. + * Revision 1.8 2001/03/26 23:07:36 rgb
  49207. + * Remove requirement for auth and enc key from UPDATE.
  49208. + *
  49209. + * Revision 1.7 2000/09/12 22:35:37 rgb
  49210. + * Restructured to remove unused extensions from CLEARFLOW messages.
  49211. + *
  49212. + * Revision 1.6 2000/09/09 06:39:01 rgb
  49213. + * Added comments for clarity.
  49214. + *
  49215. + * Revision 1.5 2000/06/02 22:54:14 rgb
  49216. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  49217. + *
  49218. + * Revision 1.4 2000/01/21 06:27:56 rgb
  49219. + * Added address cases for eroute flows.
  49220. + * Added comments for each message type.
  49221. + * Added klipsdebug switching capability.
  49222. + * Fixed GRPSA bitfields.
  49223. + *
  49224. + * Revision 1.3 1999/12/01 22:20:27 rgb
  49225. + * Remove requirement for a proxy address in an incoming getspi message.
  49226. + *
  49227. + * Revision 1.2 1999/11/27 11:57:06 rgb
  49228. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  49229. + * Add CVS log entry to bottom of file.
  49230. + * Cleaned out unused bits.
  49231. + *
  49232. + */
  49233. --- /dev/null Tue Mar 11 13:02:56 2003
  49234. +++ linux/net/ipsec/pfkey_v2_ext_process.c Mon Feb 9 13:51:03 2004
  49235. @@ -0,0 +1,951 @@
  49236. +/*
  49237. + * @(#) RFC2367 PF_KEYv2 Key management API message parser
  49238. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  49239. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  49240. + *
  49241. + * This program is free software; you can redistribute it and/or modify it
  49242. + * under the terms of the GNU General Public License as published by the
  49243. + * Free Software Foundation; either version 2 of the License, or (at your
  49244. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  49245. + *
  49246. + * This program is distributed in the hope that it will be useful, but
  49247. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  49248. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  49249. + * for more details.
  49250. + *
  49251. + * RCSID $Id: pfkey_v2_ext_process.c,v 1.20.2.2 2006/10/06 21:39:26 paul Exp $
  49252. + */
  49253. +
  49254. +/*
  49255. + * Template from klips/net/ipsec/ipsec/ipsec_netlink.c.
  49256. + */
  49257. +
  49258. +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 $";
  49259. +
  49260. +#ifndef AUTOCONF_INCLUDED
  49261. +#include <linux/config.h>
  49262. +#endif
  49263. +#include <linux/version.h>
  49264. +#include <linux/kernel.h> /* printk() */
  49265. +
  49266. +#include "openswan/ipsec_param.h"
  49267. +
  49268. +#ifdef MALLOC_SLAB
  49269. +# include <linux/slab.h> /* kmalloc() */
  49270. +#else /* MALLOC_SLAB */
  49271. +# include <linux/malloc.h> /* kmalloc() */
  49272. +#endif /* MALLOC_SLAB */
  49273. +#include <linux/errno.h> /* error codes */
  49274. +#include <linux/types.h> /* size_t */
  49275. +#include <linux/interrupt.h> /* mark_bh */
  49276. +
  49277. +#include <linux/netdevice.h> /* struct device, and other headers */
  49278. +#include <linux/etherdevice.h> /* eth_type_trans */
  49279. +#include <linux/ip.h> /* struct iphdr */
  49280. +#include <linux/skbuff.h>
  49281. +
  49282. +#include <openswan.h>
  49283. +
  49284. +#include <crypto/des.h>
  49285. +
  49286. +#ifdef SPINLOCK
  49287. +# ifdef SPINLOCK_23
  49288. +# include <linux/spinlock.h> /* *lock* */
  49289. +# else /* SPINLOCK_23 */
  49290. +# include <asm/spinlock.h> /* *lock* */
  49291. +# endif /* SPINLOCK_23 */
  49292. +#endif /* SPINLOCK */
  49293. +#ifdef NET_21
  49294. +# include <linux/in6.h>
  49295. +# define ip_chk_addr inet_addr_type
  49296. +# define IS_MYADDR RTN_LOCAL
  49297. +#endif
  49298. +
  49299. +#include <net/ip.h>
  49300. +#ifdef NETLINK_SOCK
  49301. +# include <linux/netlink.h>
  49302. +#else
  49303. +# include <net/netlink.h>
  49304. +#endif
  49305. +
  49306. +#include <linux/random.h> /* get_random_bytes() */
  49307. +
  49308. +#include "openswan/radij.h"
  49309. +#include "openswan/ipsec_encap.h"
  49310. +#include "openswan/ipsec_sa.h"
  49311. +
  49312. +#include "openswan/ipsec_radij.h"
  49313. +#include "openswan/ipsec_xform.h"
  49314. +#include "openswan/ipsec_ah.h"
  49315. +#include "openswan/ipsec_esp.h"
  49316. +#include "openswan/ipsec_tunnel.h"
  49317. +#include "openswan/ipsec_rcv.h"
  49318. +#include "openswan/ipcomp.h"
  49319. +
  49320. +#include <pfkeyv2.h>
  49321. +#include <pfkey.h>
  49322. +
  49323. +#include "openswan/ipsec_proto.h"
  49324. +#include "openswan/ipsec_alg.h"
  49325. +
  49326. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  49327. +
  49328. +int
  49329. +pfkey_sa_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49330. +{
  49331. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)pfkey_ext;
  49332. + int error = 0;
  49333. + struct ipsec_sa* ipsp;
  49334. +
  49335. + KLIPS_PRINT(debug_pfkey,
  49336. + "klips_debug:pfkey_sa_process: .\n");
  49337. +
  49338. + if(!extr || !extr->ips) {
  49339. + KLIPS_PRINT(debug_pfkey,
  49340. + "klips_debug:pfkey_sa_process: "
  49341. + "extr or extr->ips is NULL, fatal\n");
  49342. + SENDERR(EINVAL);
  49343. + }
  49344. +
  49345. + switch(pfkey_ext->sadb_ext_type) {
  49346. + case SADB_EXT_SA:
  49347. + ipsp = extr->ips;
  49348. + break;
  49349. + case SADB_X_EXT_SA2:
  49350. + if(extr->ips2 == NULL) {
  49351. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49352. + }
  49353. + if(extr->ips2 == NULL) {
  49354. + SENDERR(-error);
  49355. + }
  49356. + ipsp = extr->ips2;
  49357. + break;
  49358. + default:
  49359. + KLIPS_PRINT(debug_pfkey,
  49360. + "klips_debug:pfkey_sa_process: "
  49361. + "invalid exttype=%d.\n",
  49362. + pfkey_ext->sadb_ext_type);
  49363. + SENDERR(EINVAL);
  49364. + }
  49365. +
  49366. + ipsp->ips_said.spi = pfkey_sa->sadb_sa_spi;
  49367. + ipsp->ips_replaywin = pfkey_sa->sadb_sa_replay;
  49368. + ipsp->ips_state = pfkey_sa->sadb_sa_state;
  49369. + ipsp->ips_flags = pfkey_sa->sadb_sa_flags;
  49370. + ipsp->ips_replaywin_lastseq = ipsp->ips_replaywin_bitmap = 0;
  49371. + ipsp->ips_ref_rel = pfkey_sa->sadb_x_sa_ref;
  49372. +
  49373. + switch(ipsp->ips_said.proto) {
  49374. + case IPPROTO_AH:
  49375. + ipsp->ips_authalg = pfkey_sa->sadb_sa_auth;
  49376. + ipsp->ips_encalg = SADB_EALG_NONE;
  49377. + break;
  49378. + case IPPROTO_ESP:
  49379. + ipsp->ips_authalg = pfkey_sa->sadb_sa_auth;
  49380. + ipsp->ips_encalg = pfkey_sa->sadb_sa_encrypt;
  49381. + ipsec_alg_sa_init(ipsp);
  49382. + break;
  49383. + case IPPROTO_IPIP:
  49384. + ipsp->ips_authalg = AH_NONE;
  49385. + ipsp->ips_encalg = ESP_NONE;
  49386. + break;
  49387. +#ifdef CONFIG_KLIPS_IPCOMP
  49388. + case IPPROTO_COMP:
  49389. + ipsp->ips_authalg = AH_NONE;
  49390. + ipsp->ips_encalg = pfkey_sa->sadb_sa_encrypt;
  49391. + break;
  49392. +#endif /* CONFIG_KLIPS_IPCOMP */
  49393. + case IPPROTO_INT:
  49394. + ipsp->ips_authalg = AH_NONE;
  49395. + ipsp->ips_encalg = ESP_NONE;
  49396. + break;
  49397. + case 0:
  49398. + break;
  49399. + default:
  49400. + KLIPS_PRINT(debug_pfkey,
  49401. + "klips_debug:pfkey_sa_process: "
  49402. + "unknown proto=%d.\n",
  49403. + ipsp->ips_said.proto);
  49404. + SENDERR(EINVAL);
  49405. + }
  49406. +
  49407. +errlab:
  49408. + return error;
  49409. +}
  49410. +
  49411. +int
  49412. +pfkey_lifetime_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49413. +{
  49414. + int error = 0;
  49415. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)pfkey_ext;
  49416. +
  49417. + KLIPS_PRINT(debug_pfkey,
  49418. + "klips_debug:pfkey_lifetime_process: .\n");
  49419. +
  49420. + if(!extr || !extr->ips) {
  49421. + KLIPS_PRINT(debug_pfkey,
  49422. + "klips_debug:pfkey_lifetime_process: "
  49423. + "extr or extr->ips is NULL, fatal\n");
  49424. + SENDERR(EINVAL);
  49425. + }
  49426. +
  49427. + switch(pfkey_lifetime->sadb_lifetime_exttype) {
  49428. + case SADB_EXT_LIFETIME_CURRENT:
  49429. + KLIPS_PRINT(debug_pfkey,
  49430. + "klips_debug:pfkey_lifetime_process: "
  49431. + "lifetime_current not supported yet.\n");
  49432. + SENDERR(EINVAL);
  49433. + break;
  49434. + case SADB_EXT_LIFETIME_HARD:
  49435. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_allocations,
  49436. + pfkey_lifetime->sadb_lifetime_allocations);
  49437. +
  49438. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_bytes,
  49439. + pfkey_lifetime->sadb_lifetime_bytes);
  49440. +
  49441. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_addtime,
  49442. + pfkey_lifetime->sadb_lifetime_addtime);
  49443. +
  49444. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_usetime,
  49445. + pfkey_lifetime->sadb_lifetime_usetime);
  49446. +
  49447. + break;
  49448. +
  49449. + case SADB_EXT_LIFETIME_SOFT:
  49450. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_allocations,
  49451. + pfkey_lifetime->sadb_lifetime_allocations);
  49452. +
  49453. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_bytes,
  49454. + pfkey_lifetime->sadb_lifetime_bytes);
  49455. +
  49456. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_addtime,
  49457. + pfkey_lifetime->sadb_lifetime_addtime);
  49458. +
  49459. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_usetime,
  49460. + pfkey_lifetime->sadb_lifetime_usetime);
  49461. +
  49462. + break;
  49463. + default:
  49464. + KLIPS_PRINT(debug_pfkey,
  49465. + "klips_debug:pfkey_lifetime_process: "
  49466. + "invalid exttype=%d.\n",
  49467. + pfkey_ext->sadb_ext_type);
  49468. + SENDERR(EINVAL);
  49469. + }
  49470. +
  49471. +errlab:
  49472. + return error;
  49473. +}
  49474. +
  49475. +int
  49476. +pfkey_address_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49477. +{
  49478. + int error = 0;
  49479. + int saddr_len = 0;
  49480. + char ipaddr_txt[ADDRTOA_BUF];
  49481. + unsigned char **sap;
  49482. + unsigned short * portp = 0;
  49483. + struct sadb_address *pfkey_address = (struct sadb_address *)pfkey_ext;
  49484. + struct sockaddr* s = (struct sockaddr*)((char*)pfkey_address + sizeof(*pfkey_address));
  49485. + struct ipsec_sa* ipsp;
  49486. +
  49487. + KLIPS_PRINT(debug_pfkey,
  49488. + "klips_debug:pfkey_address_process:\n");
  49489. +
  49490. + if(!extr || !extr->ips) {
  49491. + KLIPS_PRINT(debug_pfkey,
  49492. + "klips_debug:pfkey_address_process: "
  49493. + "extr or extr->ips is NULL, fatal\n");
  49494. + SENDERR(EINVAL);
  49495. + }
  49496. +
  49497. + switch(s->sa_family) {
  49498. + case AF_INET:
  49499. + saddr_len = sizeof(struct sockaddr_in);
  49500. + addrtoa(((struct sockaddr_in*)s)->sin_addr, 0, ipaddr_txt, sizeof(ipaddr_txt));
  49501. + KLIPS_PRINT(debug_pfkey,
  49502. + "klips_debug:pfkey_address_process: "
  49503. + "found address family=%d, AF_INET, %s.\n",
  49504. + s->sa_family,
  49505. + ipaddr_txt);
  49506. + break;
  49507. +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  49508. + case AF_INET6:
  49509. + saddr_len = sizeof(struct sockaddr_in6);
  49510. + break;
  49511. +#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  49512. + default:
  49513. + KLIPS_PRINT(debug_pfkey,
  49514. + "klips_debug:pfkey_address_process: "
  49515. + "s->sa_family=%d not supported.\n",
  49516. + s->sa_family);
  49517. + SENDERR(EPFNOSUPPORT);
  49518. + }
  49519. +
  49520. + switch(pfkey_address->sadb_address_exttype) {
  49521. + case SADB_EXT_ADDRESS_SRC:
  49522. + KLIPS_PRINT(debug_pfkey,
  49523. + "klips_debug:pfkey_address_process: "
  49524. + "found src address.\n");
  49525. + sap = (unsigned char **)&(extr->ips->ips_addr_s);
  49526. + extr->ips->ips_addr_s_size = saddr_len;
  49527. + break;
  49528. + case SADB_EXT_ADDRESS_DST:
  49529. + KLIPS_PRINT(debug_pfkey,
  49530. + "klips_debug:pfkey_address_process: "
  49531. + "found dst address.\n");
  49532. + sap = (unsigned char **)&(extr->ips->ips_addr_d);
  49533. + extr->ips->ips_addr_d_size = saddr_len;
  49534. + break;
  49535. + case SADB_EXT_ADDRESS_PROXY:
  49536. + KLIPS_PRINT(debug_pfkey,
  49537. + "klips_debug:pfkey_address_process: "
  49538. + "found proxy address.\n");
  49539. + sap = (unsigned char **)&(extr->ips->ips_addr_p);
  49540. + extr->ips->ips_addr_p_size = saddr_len;
  49541. + break;
  49542. + case SADB_X_EXT_ADDRESS_DST2:
  49543. + KLIPS_PRINT(debug_pfkey,
  49544. + "klips_debug:pfkey_address_process: "
  49545. + "found 2nd dst address.\n");
  49546. + if(extr->ips2 == NULL) {
  49547. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49548. + }
  49549. + if(extr->ips2 == NULL) {
  49550. + SENDERR(-error);
  49551. + }
  49552. + sap = (unsigned char **)&(extr->ips2->ips_addr_d);
  49553. + extr->ips2->ips_addr_d_size = saddr_len;
  49554. + break;
  49555. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  49556. + KLIPS_PRINT(debug_pfkey,
  49557. + "klips_debug:pfkey_address_process: "
  49558. + "found src flow address.\n");
  49559. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49560. + SENDERR(ENOMEM);
  49561. + }
  49562. + sap = (unsigned char **)&(extr->eroute->er_eaddr.sen_ip_src);
  49563. + portp = &(extr->eroute->er_eaddr.sen_sport);
  49564. + break;
  49565. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  49566. + KLIPS_PRINT(debug_pfkey,
  49567. + "klips_debug:pfkey_address_process: "
  49568. + "found dst flow address.\n");
  49569. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49570. + SENDERR(ENOMEM);
  49571. + }
  49572. + sap = (unsigned char **)&(extr->eroute->er_eaddr.sen_ip_dst);
  49573. + portp = &(extr->eroute->er_eaddr.sen_dport);
  49574. + break;
  49575. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  49576. + KLIPS_PRINT(debug_pfkey,
  49577. + "klips_debug:pfkey_address_process: "
  49578. + "found src mask address.\n");
  49579. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49580. + SENDERR(ENOMEM);
  49581. + }
  49582. + sap = (unsigned char **)&(extr->eroute->er_emask.sen_ip_src);
  49583. + portp = &(extr->eroute->er_emask.sen_sport);
  49584. + break;
  49585. + case SADB_X_EXT_ADDRESS_DST_MASK:
  49586. + KLIPS_PRINT(debug_pfkey,
  49587. + "klips_debug:pfkey_address_process: "
  49588. + "found dst mask address.\n");
  49589. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49590. + SENDERR(ENOMEM);
  49591. + }
  49592. + sap = (unsigned char **)&(extr->eroute->er_emask.sen_ip_dst);
  49593. + portp = &(extr->eroute->er_emask.sen_dport);
  49594. + break;
  49595. +#ifdef NAT_TRAVERSAL
  49596. + case SADB_X_EXT_NAT_T_OA:
  49597. + KLIPS_PRINT(debug_pfkey,
  49598. + "klips_debug:pfkey_address_process: "
  49599. + "found NAT-OA address.\n");
  49600. + sap = (unsigned char **)&(extr->ips->ips_natt_oa);
  49601. + extr->ips->ips_natt_oa_size = saddr_len;
  49602. + break;
  49603. +#endif
  49604. + default:
  49605. + KLIPS_PRINT(debug_pfkey,
  49606. + "klips_debug:pfkey_address_process: "
  49607. + "unrecognised ext_type=%d.\n",
  49608. + pfkey_address->sadb_address_exttype);
  49609. + SENDERR(EINVAL);
  49610. + }
  49611. +
  49612. + switch(pfkey_address->sadb_address_exttype) {
  49613. + case SADB_EXT_ADDRESS_SRC:
  49614. + case SADB_EXT_ADDRESS_DST:
  49615. + case SADB_EXT_ADDRESS_PROXY:
  49616. + case SADB_X_EXT_ADDRESS_DST2:
  49617. +#ifdef NAT_TRAVERSAL
  49618. + case SADB_X_EXT_NAT_T_OA:
  49619. +#endif
  49620. + KLIPS_PRINT(debug_pfkey,
  49621. + "klips_debug:pfkey_address_process: "
  49622. + "allocating %d bytes for saddr.\n",
  49623. + saddr_len);
  49624. + if(!(*sap = kmalloc(saddr_len, GFP_KERNEL))) {
  49625. + SENDERR(ENOMEM);
  49626. + }
  49627. + memcpy(*sap, s, saddr_len);
  49628. + break;
  49629. + default:
  49630. + if(s->sa_family != AF_INET) {
  49631. + KLIPS_PRINT(debug_pfkey,
  49632. + "klips_debug:pfkey_address_process: "
  49633. + "s->sa_family=%d not supported.\n",
  49634. + s->sa_family);
  49635. + SENDERR(EPFNOSUPPORT);
  49636. + }
  49637. + {
  49638. + unsigned long *ulsap = (unsigned long *)sap;
  49639. + *ulsap = ((struct sockaddr_in*)s)->sin_addr.s_addr;
  49640. + }
  49641. +
  49642. + if (portp != 0)
  49643. + *portp = ((struct sockaddr_in*)s)->sin_port;
  49644. +#ifdef CONFIG_KLIPS_DEBUG
  49645. + if(extr->eroute) {
  49646. + char buf1[64], buf2[64];
  49647. + if (debug_pfkey) {
  49648. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  49649. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  49650. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  49651. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  49652. + KLIPS_PRINT(debug_pfkey,
  49653. + "klips_debug:pfkey_address_parse: "
  49654. + "extr->eroute set to %s:%d->%s:%d\n",
  49655. + buf1,
  49656. + ntohs(extr->eroute->er_eaddr.sen_sport),
  49657. + buf2,
  49658. + ntohs(extr->eroute->er_eaddr.sen_dport));
  49659. + }
  49660. + }
  49661. +#endif /* CONFIG_KLIPS_DEBUG */
  49662. + }
  49663. +
  49664. + ipsp = extr->ips;
  49665. + switch(pfkey_address->sadb_address_exttype) {
  49666. + case SADB_X_EXT_ADDRESS_DST2:
  49667. + ipsp = extr->ips2;
  49668. + case SADB_EXT_ADDRESS_DST:
  49669. + if(s->sa_family == AF_INET) {
  49670. + ipsp->ips_said.dst.u.v4.sin_addr.s_addr = ((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr.s_addr;
  49671. + ipsp->ips_said.dst.u.v4.sin_family = AF_INET;
  49672. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,
  49673. + 0,
  49674. + ipaddr_txt,
  49675. + sizeof(ipaddr_txt));
  49676. + KLIPS_PRINT(debug_pfkey,
  49677. + "klips_debug:pfkey_address_process: "
  49678. + "ips_said.dst set to %s.\n",
  49679. + ipaddr_txt);
  49680. + } else {
  49681. + KLIPS_PRINT(debug_pfkey,
  49682. + "klips_debug:pfkey_address_process: "
  49683. + "uh, ips_said.dst doesn't do address family=%d yet, said will be invalid.\n",
  49684. + s->sa_family);
  49685. + }
  49686. + default:
  49687. + break;
  49688. + }
  49689. +
  49690. + /* XXX check if port!=0 */
  49691. +
  49692. + KLIPS_PRINT(debug_pfkey,
  49693. + "klips_debug:pfkey_address_process: successful.\n");
  49694. + errlab:
  49695. + return error;
  49696. +}
  49697. +
  49698. +int
  49699. +pfkey_key_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49700. +{
  49701. + int error = 0;
  49702. + struct sadb_key *pfkey_key = (struct sadb_key *)pfkey_ext;
  49703. +
  49704. + KLIPS_PRINT(debug_pfkey,
  49705. + "klips_debug:pfkey_key_process: .\n");
  49706. +
  49707. + if(!extr || !extr->ips) {
  49708. + KLIPS_PRINT(debug_pfkey,
  49709. + "klips_debug:pfkey_key_process: "
  49710. + "extr or extr->ips is NULL, fatal\n");
  49711. + SENDERR(EINVAL);
  49712. + }
  49713. +
  49714. + switch(pfkey_key->sadb_key_exttype) {
  49715. + case SADB_EXT_KEY_AUTH:
  49716. + KLIPS_PRINT(debug_pfkey,
  49717. + "klips_debug:pfkey_key_process: "
  49718. + "allocating %d bytes for authkey.\n",
  49719. + DIVUP(pfkey_key->sadb_key_bits, 8));
  49720. + if(!(extr->ips->ips_key_a = kmalloc(DIVUP(pfkey_key->sadb_key_bits, 8), GFP_KERNEL))) {
  49721. + KLIPS_PRINT(debug_pfkey,
  49722. + "klips_debug:pfkey_key_process: "
  49723. + "memory allocation error.\n");
  49724. + SENDERR(ENOMEM);
  49725. + }
  49726. + extr->ips->ips_key_bits_a = pfkey_key->sadb_key_bits;
  49727. + extr->ips->ips_key_a_size = DIVUP(pfkey_key->sadb_key_bits, 8);
  49728. + memcpy(extr->ips->ips_key_a,
  49729. + (char*)pfkey_key + sizeof(struct sadb_key),
  49730. + extr->ips->ips_key_a_size);
  49731. + break;
  49732. + case SADB_EXT_KEY_ENCRYPT: /* Key(s) */
  49733. + KLIPS_PRINT(debug_pfkey,
  49734. + "klips_debug:pfkey_key_process: "
  49735. + "allocating %d bytes for enckey.\n",
  49736. + DIVUP(pfkey_key->sadb_key_bits, 8));
  49737. + if(!(extr->ips->ips_key_e = kmalloc(DIVUP(pfkey_key->sadb_key_bits, 8), GFP_KERNEL))) {
  49738. + KLIPS_PRINT(debug_pfkey,
  49739. + "klips_debug:pfkey_key_process: "
  49740. + "memory allocation error.\n");
  49741. + SENDERR(ENOMEM);
  49742. + }
  49743. + extr->ips->ips_key_bits_e = pfkey_key->sadb_key_bits;
  49744. + extr->ips->ips_key_e_size = DIVUP(pfkey_key->sadb_key_bits, 8);
  49745. + memcpy(extr->ips->ips_key_e,
  49746. + (char*)pfkey_key + sizeof(struct sadb_key),
  49747. + extr->ips->ips_key_e_size);
  49748. + break;
  49749. + default:
  49750. + SENDERR(EINVAL);
  49751. + }
  49752. +
  49753. + KLIPS_PRINT(debug_pfkey,
  49754. + "klips_debug:pfkey_key_process: "
  49755. + "success.\n");
  49756. +errlab:
  49757. + return error;
  49758. +}
  49759. +
  49760. +int
  49761. +pfkey_ident_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49762. +{
  49763. + int error = 0;
  49764. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)pfkey_ext;
  49765. + int data_len;
  49766. +
  49767. + KLIPS_PRINT(debug_pfkey,
  49768. + "klips_debug:pfkey_ident_process: .\n");
  49769. +
  49770. + if(!extr || !extr->ips) {
  49771. + KLIPS_PRINT(debug_pfkey,
  49772. + "klips_debug:pfkey_ident_process: "
  49773. + "extr or extr->ips is NULL, fatal\n");
  49774. + SENDERR(EINVAL);
  49775. + }
  49776. +
  49777. + switch(pfkey_ident->sadb_ident_exttype) {
  49778. + case SADB_EXT_IDENTITY_SRC:
  49779. + data_len = pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  49780. +
  49781. + extr->ips->ips_ident_s.type = pfkey_ident->sadb_ident_type;
  49782. + extr->ips->ips_ident_s.id = pfkey_ident->sadb_ident_id;
  49783. + extr->ips->ips_ident_s.len = pfkey_ident->sadb_ident_len;
  49784. + if(data_len) {
  49785. + KLIPS_PRINT(debug_pfkey,
  49786. + "klips_debug:pfkey_ident_process: "
  49787. + "allocating %d bytes for ident_s.\n",
  49788. + data_len);
  49789. + if(!(extr->ips->ips_ident_s.data
  49790. + = kmalloc(data_len, GFP_KERNEL))) {
  49791. + SENDERR(ENOMEM);
  49792. + }
  49793. + memcpy(extr->ips->ips_ident_s.data,
  49794. + (char*)pfkey_ident + sizeof(struct sadb_ident),
  49795. + data_len);
  49796. + } else {
  49797. + extr->ips->ips_ident_s.data = NULL;
  49798. + }
  49799. + break;
  49800. + case SADB_EXT_IDENTITY_DST: /* Identity(ies) */
  49801. + data_len = pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  49802. +
  49803. + extr->ips->ips_ident_d.type = pfkey_ident->sadb_ident_type;
  49804. + extr->ips->ips_ident_d.id = pfkey_ident->sadb_ident_id;
  49805. + extr->ips->ips_ident_d.len = pfkey_ident->sadb_ident_len;
  49806. + if(data_len) {
  49807. + KLIPS_PRINT(debug_pfkey,
  49808. + "klips_debug:pfkey_ident_process: "
  49809. + "allocating %d bytes for ident_d.\n",
  49810. + data_len);
  49811. + if(!(extr->ips->ips_ident_d.data
  49812. + = kmalloc(data_len, GFP_KERNEL))) {
  49813. + SENDERR(ENOMEM);
  49814. + }
  49815. + memcpy(extr->ips->ips_ident_d.data,
  49816. + (char*)pfkey_ident + sizeof(struct sadb_ident),
  49817. + data_len);
  49818. + } else {
  49819. + extr->ips->ips_ident_d.data = NULL;
  49820. + }
  49821. + break;
  49822. + default:
  49823. + SENDERR(EINVAL);
  49824. + }
  49825. +errlab:
  49826. + return error;
  49827. +}
  49828. +
  49829. +int
  49830. +pfkey_sens_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49831. +{
  49832. + int error = 0;
  49833. +
  49834. + KLIPS_PRINT(debug_pfkey,
  49835. + "klips_debug:pfkey_sens_process: "
  49836. + "Sorry, I can't process exttype=%d yet.\n",
  49837. + pfkey_ext->sadb_ext_type);
  49838. + SENDERR(EINVAL); /* don't process these yet */
  49839. + errlab:
  49840. + return error;
  49841. +}
  49842. +
  49843. +int
  49844. +pfkey_prop_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49845. +{
  49846. + int error = 0;
  49847. +
  49848. + KLIPS_PRINT(debug_pfkey,
  49849. + "klips_debug:pfkey_prop_process: "
  49850. + "Sorry, I can't process exttype=%d yet.\n",
  49851. + pfkey_ext->sadb_ext_type);
  49852. + SENDERR(EINVAL); /* don't process these yet */
  49853. +
  49854. + errlab:
  49855. + return error;
  49856. +}
  49857. +
  49858. +int
  49859. +pfkey_supported_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49860. +{
  49861. + int error = 0;
  49862. +
  49863. + KLIPS_PRINT(debug_pfkey,
  49864. + "klips_debug:pfkey_supported_process: "
  49865. + "Sorry, I can't process exttype=%d yet.\n",
  49866. + pfkey_ext->sadb_ext_type);
  49867. + SENDERR(EINVAL); /* don't process these yet */
  49868. +
  49869. +errlab:
  49870. + return error;
  49871. +}
  49872. +
  49873. +int
  49874. +pfkey_spirange_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49875. +{
  49876. + int error = 0;
  49877. +
  49878. + KLIPS_PRINT(debug_pfkey,
  49879. + "klips_debug:pfkey_spirange_process: .\n");
  49880. +/* errlab: */
  49881. + return error;
  49882. +}
  49883. +
  49884. +int
  49885. +pfkey_x_kmprivate_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49886. +{
  49887. + int error = 0;
  49888. +
  49889. + KLIPS_PRINT(debug_pfkey,
  49890. + "klips_debug:pfkey_x_kmprivate_process: "
  49891. + "Sorry, I can't process exttype=%d yet.\n",
  49892. + pfkey_ext->sadb_ext_type);
  49893. + SENDERR(EINVAL); /* don't process these yet */
  49894. +
  49895. +errlab:
  49896. + return error;
  49897. +}
  49898. +
  49899. +int
  49900. +pfkey_x_satype_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49901. +{
  49902. + int error = 0;
  49903. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)pfkey_ext;
  49904. +
  49905. + KLIPS_PRINT(debug_pfkey,
  49906. + "klips_debug:pfkey_x_satype_process: .\n");
  49907. +
  49908. + if(!extr || !extr->ips) {
  49909. + KLIPS_PRINT(debug_pfkey,
  49910. + "klips_debug:pfkey_x_satype_process: "
  49911. + "extr or extr->ips is NULL, fatal\n");
  49912. + SENDERR(EINVAL);
  49913. + }
  49914. +
  49915. + if(extr->ips2 == NULL) {
  49916. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49917. + }
  49918. + if(extr->ips2 == NULL) {
  49919. + SENDERR(-error);
  49920. + }
  49921. + if(!(extr->ips2->ips_said.proto = satype2proto(pfkey_x_satype->sadb_x_satype_satype))) {
  49922. + KLIPS_PRINT(debug_pfkey,
  49923. + "klips_debug:pfkey_x_satype_process: "
  49924. + "proto lookup from satype=%d failed.\n",
  49925. + pfkey_x_satype->sadb_x_satype_satype);
  49926. + SENDERR(EINVAL);
  49927. + }
  49928. + KLIPS_PRINT(debug_pfkey,
  49929. + "klips_debug:pfkey_x_satype_process: "
  49930. + "protocol==%d decoded from satype==%d(%s).\n",
  49931. + extr->ips2->ips_said.proto,
  49932. + pfkey_x_satype->sadb_x_satype_satype,
  49933. + satype2name(pfkey_x_satype->sadb_x_satype_satype));
  49934. +
  49935. +errlab:
  49936. + return error;
  49937. +}
  49938. +
  49939. +
  49940. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  49941. +int
  49942. +pfkey_x_nat_t_type_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49943. +{
  49944. + int error = 0;
  49945. + struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)pfkey_ext;
  49946. +
  49947. + if(!pfkey_x_nat_t_type) {
  49948. + printk("klips_debug:pfkey_x_nat_t_type_process: "
  49949. + "null pointer passed in\n");
  49950. + SENDERR(EINVAL);
  49951. + }
  49952. +
  49953. + KLIPS_PRINT(debug_pfkey,
  49954. + "klips_debug:pfkey_x_nat_t_type_process: %d.\n",
  49955. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type);
  49956. +
  49957. + if(!extr || !extr->ips) {
  49958. + KLIPS_PRINT(debug_pfkey,
  49959. + "klips_debug:pfkey_nat_t_type_process: "
  49960. + "extr or extr->ips is NULL, fatal\n");
  49961. + SENDERR(EINVAL);
  49962. + }
  49963. +
  49964. + switch(pfkey_x_nat_t_type->sadb_x_nat_t_type_type) {
  49965. + case ESPINUDP_WITH_NON_IKE: /* with Non-IKE (older version) */
  49966. + case ESPINUDP_WITH_NON_ESP: /* with Non-ESP */
  49967. +
  49968. + extr->ips->ips_natt_type = pfkey_x_nat_t_type->sadb_x_nat_t_type_type;
  49969. + break;
  49970. + default:
  49971. + KLIPS_PRINT(debug_pfkey,
  49972. + "klips_debug:pfkey_x_nat_t_type_process: "
  49973. + "unknown type %d.\n",
  49974. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type);
  49975. + SENDERR(EINVAL);
  49976. + break;
  49977. + }
  49978. +
  49979. +errlab:
  49980. + return error;
  49981. +}
  49982. +
  49983. +int
  49984. +pfkey_x_nat_t_port_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49985. +{
  49986. + int error = 0;
  49987. + struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)pfkey_ext;
  49988. +
  49989. + if(!pfkey_x_nat_t_port) {
  49990. + printk("klips_debug:pfkey_x_nat_t_port_process: "
  49991. + "null pointer passed in\n");
  49992. + SENDERR(EINVAL);
  49993. + }
  49994. +
  49995. + KLIPS_PRINT(debug_pfkey,
  49996. + "klips_debug:pfkey_x_nat_t_port_process: %d/%d.\n",
  49997. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype,
  49998. + pfkey_x_nat_t_port->sadb_x_nat_t_port_port);
  49999. +
  50000. + if(!extr || !extr->ips) {
  50001. + KLIPS_PRINT(debug_pfkey,
  50002. + "klips_debug:pfkey_nat_t_type_process: "
  50003. + "extr or extr->ips is NULL, fatal\n");
  50004. + SENDERR(EINVAL);
  50005. + }
  50006. +
  50007. + switch(pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype) {
  50008. + case SADB_X_EXT_NAT_T_SPORT:
  50009. + extr->ips->ips_natt_sport = pfkey_x_nat_t_port->sadb_x_nat_t_port_port;
  50010. + break;
  50011. + case SADB_X_EXT_NAT_T_DPORT:
  50012. + extr->ips->ips_natt_dport = pfkey_x_nat_t_port->sadb_x_nat_t_port_port;
  50013. + break;
  50014. + default:
  50015. + KLIPS_PRINT(debug_pfkey,
  50016. + "klips_debug:pfkey_x_nat_t_port_process: "
  50017. + "unknown exttype %d.\n",
  50018. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype);
  50019. + SENDERR(EINVAL);
  50020. + break;
  50021. + }
  50022. +
  50023. +errlab:
  50024. + return error;
  50025. +}
  50026. +#endif
  50027. +
  50028. +int
  50029. +pfkey_x_debug_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  50030. +{
  50031. + int error = 0;
  50032. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)pfkey_ext;
  50033. +
  50034. + if(!pfkey_x_debug) {
  50035. + printk("klips_debug:pfkey_x_debug_process: "
  50036. + "null pointer passed in\n");
  50037. + SENDERR(EINVAL);
  50038. + }
  50039. +
  50040. + KLIPS_PRINT(debug_pfkey,
  50041. + "klips_debug:pfkey_x_debug_process: .\n");
  50042. +
  50043. +#ifdef CONFIG_KLIPS_DEBUG
  50044. + if(pfkey_x_debug->sadb_x_debug_netlink >>
  50045. + (sizeof(pfkey_x_debug->sadb_x_debug_netlink) * 8 - 1)) {
  50046. + pfkey_x_debug->sadb_x_debug_netlink &=
  50047. + ~(1 << (sizeof(pfkey_x_debug->sadb_x_debug_netlink) * 8 -1));
  50048. + debug_tunnel |= pfkey_x_debug->sadb_x_debug_tunnel;
  50049. + debug_netlink |= pfkey_x_debug->sadb_x_debug_netlink;
  50050. + debug_xform |= pfkey_x_debug->sadb_x_debug_xform;
  50051. + debug_eroute |= pfkey_x_debug->sadb_x_debug_eroute;
  50052. + debug_spi |= pfkey_x_debug->sadb_x_debug_spi;
  50053. + debug_radij |= pfkey_x_debug->sadb_x_debug_radij;
  50054. + debug_esp |= pfkey_x_debug->sadb_x_debug_esp;
  50055. + debug_ah |= pfkey_x_debug->sadb_x_debug_ah;
  50056. + debug_rcv |= pfkey_x_debug->sadb_x_debug_rcv;
  50057. + debug_pfkey |= pfkey_x_debug->sadb_x_debug_pfkey;
  50058. +#ifdef CONFIG_KLIPS_IPCOMP
  50059. + sysctl_ipsec_debug_ipcomp |= pfkey_x_debug->sadb_x_debug_ipcomp;
  50060. +#endif /* CONFIG_KLIPS_IPCOMP */
  50061. + sysctl_ipsec_debug_verbose |= pfkey_x_debug->sadb_x_debug_verbose;
  50062. + KLIPS_PRINT(debug_pfkey,
  50063. + "klips_debug:pfkey_x_debug_process: "
  50064. + "set\n");
  50065. + } else {
  50066. + KLIPS_PRINT(debug_pfkey,
  50067. + "klips_debug:pfkey_x_debug_process: "
  50068. + "unset\n");
  50069. + debug_tunnel &= pfkey_x_debug->sadb_x_debug_tunnel;
  50070. + debug_netlink &= pfkey_x_debug->sadb_x_debug_netlink;
  50071. + debug_xform &= pfkey_x_debug->sadb_x_debug_xform;
  50072. + debug_eroute &= pfkey_x_debug->sadb_x_debug_eroute;
  50073. + debug_spi &= pfkey_x_debug->sadb_x_debug_spi;
  50074. + debug_radij &= pfkey_x_debug->sadb_x_debug_radij;
  50075. + debug_esp &= pfkey_x_debug->sadb_x_debug_esp;
  50076. + debug_ah &= pfkey_x_debug->sadb_x_debug_ah;
  50077. + debug_rcv &= pfkey_x_debug->sadb_x_debug_rcv;
  50078. + debug_pfkey &= pfkey_x_debug->sadb_x_debug_pfkey;
  50079. +#ifdef CONFIG_KLIPS_IPCOMP
  50080. + sysctl_ipsec_debug_ipcomp &= pfkey_x_debug->sadb_x_debug_ipcomp;
  50081. +#endif /* CONFIG_KLIPS_IPCOMP */
  50082. + sysctl_ipsec_debug_verbose &= pfkey_x_debug->sadb_x_debug_verbose;
  50083. + }
  50084. +#else /* CONFIG_KLIPS_DEBUG */
  50085. + printk("klips_debug:pfkey_x_debug_process: "
  50086. + "debugging not enabled\n");
  50087. + SENDERR(EINVAL);
  50088. +#endif /* CONFIG_KLIPS_DEBUG */
  50089. +
  50090. +errlab:
  50091. + return error;
  50092. +}
  50093. +
  50094. +/*
  50095. + * $Log: pfkey_v2_ext_process.c,v $
  50096. + * Revision 1.20.2.2 2006/10/06 21:39:26 paul
  50097. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  50098. + * set. This is defined through autoconf.h which is included through the
  50099. + * linux kernel build macros.
  50100. + *
  50101. + * Revision 1.20.2.1 2006/04/20 16:33:07 mcr
  50102. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  50103. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  50104. + *
  50105. + * Revision 1.20 2005/04/29 05:10:22 mcr
  50106. + * removed from extraenous includes to make unit testing easier.
  50107. + *
  50108. + * Revision 1.19 2004/12/04 07:14:18 mcr
  50109. + * resolution to gcc3-ism was wrong. fixed to assign correct
  50110. + * variable.
  50111. + *
  50112. + * Revision 1.18 2004/12/03 21:25:57 mcr
  50113. + * compile time fixes for running on 2.6.
  50114. + * still experimental.
  50115. + *
  50116. + * Revision 1.17 2004/08/21 00:45:04 mcr
  50117. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  50118. + *
  50119. + * Revision 1.16 2004/07/10 19:11:18 mcr
  50120. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  50121. + *
  50122. + * Revision 1.15 2004/04/06 02:49:26 mcr
  50123. + * pullup of algo code from alg-branch.
  50124. + *
  50125. + * Revision 1.14 2004/02/03 03:13:59 mcr
  50126. + * no longer #ifdef out NON_ESP mode. That was a mistake.
  50127. + *
  50128. + * Revision 1.13 2003/12/15 18:13:12 mcr
  50129. + * when compiling with NAT traversal, don't assume that the
  50130. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  50131. + * is set.
  50132. + *
  50133. + * Revision 1.12.2.1 2003/12/22 15:25:52 jjo
  50134. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  50135. + *
  50136. + * Revision 1.12 2003/12/10 01:14:27 mcr
  50137. + * NAT-traversal patches to KLIPS.
  50138. + *
  50139. + * Revision 1.11 2003/10/31 02:27:55 mcr
  50140. + * pulled up port-selector patches and sa_id elimination.
  50141. + *
  50142. + * Revision 1.10.4.2 2003/10/29 01:30:41 mcr
  50143. + * elimited "struct sa_id".
  50144. + *
  50145. + * Revision 1.10.4.1 2003/09/21 13:59:56 mcr
  50146. + * pre-liminary X.509 patch - does not yet pass tests.
  50147. + *
  50148. + * Revision 1.10 2003/02/06 01:51:41 rgb
  50149. + * Removed no longer relevant comment
  50150. + *
  50151. + * Revision 1.9 2003/01/30 02:32:44 rgb
  50152. + *
  50153. + * Transmit error code through to caller from callee for better diagnosis of problems.
  50154. + *
  50155. + * Revision 1.8 2002/12/13 22:42:22 mcr
  50156. + * restored sa_ref code
  50157. + *
  50158. + * Revision 1.7 2002/12/13 22:40:48 mcr
  50159. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  50160. + *
  50161. + * Revision 1.6 2002/10/05 05:02:58 dhr
  50162. + *
  50163. + * C labels go on statements
  50164. + *
  50165. + * Revision 1.5 2002/09/20 15:41:08 rgb
  50166. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  50167. + * Added sadb_x_sa_ref to struct sadb_sa.
  50168. + *
  50169. + * Revision 1.4 2002/09/20 05:02:02 rgb
  50170. + * Added memory allocation debugging.
  50171. + *
  50172. + * Revision 1.3 2002/07/24 18:44:54 rgb
  50173. + * Type fiddling to tame ia64 compiler.
  50174. + *
  50175. + * Revision 1.2 2002/05/27 18:55:03 rgb
  50176. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  50177. + *
  50178. + * Revision 1.1 2002/05/14 02:33:51 rgb
  50179. + * Moved all the extension processing functions to pfkey_v2_ext_process.c.
  50180. + *
  50181. + *
  50182. + * Local variables:
  50183. + * c-file-style: "linux"
  50184. + * End:
  50185. + *
  50186. + */
  50187. --- /dev/null Tue Mar 11 13:02:56 2003
  50188. +++ linux/net/ipsec/pfkey_v2_parse.c Mon Feb 9 13:51:03 2004
  50189. @@ -0,0 +1,1846 @@
  50190. +/*
  50191. + * RFC2367 PF_KEYv2 Key management API message parser
  50192. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  50193. + *
  50194. + * This program is free software; you can redistribute it and/or modify it
  50195. + * under the terms of the GNU General Public License as published by the
  50196. + * Free Software Foundation; either version 2 of the License, or (at your
  50197. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  50198. + *
  50199. + * This program is distributed in the hope that it will be useful, but
  50200. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  50201. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  50202. + * for more details.
  50203. + *
  50204. + * RCSID $Id: pfkey_v2_parse.c,v 1.65 2005/04/06 17:46:05 mcr Exp $
  50205. + */
  50206. +
  50207. +/*
  50208. + * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
  50209. + */
  50210. +
  50211. +char pfkey_v2_parse_c_version[] = "$Id: pfkey_v2_parse.c,v 1.65 2005/04/06 17:46:05 mcr Exp $";
  50212. +
  50213. +/*
  50214. + * Some ugly stuff to allow consistent debugging code for use in the
  50215. + * kernel and in user space
  50216. +*/
  50217. +
  50218. +#ifdef __KERNEL__
  50219. +
  50220. +# include <linux/kernel.h> /* for printk */
  50221. +
  50222. +#include "openswan/ipsec_kversion.h" /* for malloc switch */
  50223. +
  50224. +# ifdef MALLOC_SLAB
  50225. +# include <linux/slab.h> /* kmalloc() */
  50226. +# else /* MALLOC_SLAB */
  50227. +# include <linux/malloc.h> /* kmalloc() */
  50228. +# endif /* MALLOC_SLAB */
  50229. +# include <linux/errno.h> /* error codes */
  50230. +# include <linux/types.h> /* size_t */
  50231. +# include <linux/interrupt.h> /* mark_bh */
  50232. +
  50233. +# include <linux/netdevice.h> /* struct device, and other headers */
  50234. +# include <linux/etherdevice.h> /* eth_type_trans */
  50235. +# include <linux/ip.h> /* struct iphdr */
  50236. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  50237. +# include <linux/ipv6.h> /* struct ipv6hdr */
  50238. +# endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  50239. +extern int debug_pfkey;
  50240. +
  50241. +# include <openswan.h>
  50242. +
  50243. +#include "openswan/ipsec_encap.h"
  50244. +
  50245. +#else /* __KERNEL__ */
  50246. +
  50247. +# include <sys/types.h>
  50248. +# include <linux/types.h>
  50249. +# include <linux/errno.h>
  50250. +
  50251. +# include <openswan.h>
  50252. +# include "constants.h"
  50253. +# include "programs/pluto/defs.h" /* for PRINTF_LIKE */
  50254. +
  50255. +#endif /* __KERNEL__ */
  50256. +
  50257. +
  50258. +#include <pfkeyv2.h>
  50259. +#include <pfkey.h>
  50260. +
  50261. +#include "openswan/ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
  50262. +
  50263. +/*
  50264. + * how to handle debugging for pfkey.
  50265. + */
  50266. +#include <openswan/pfkey_debug.h>
  50267. +
  50268. +unsigned int pfkey_lib_debug = PF_KEY_DEBUG_PARSE_NONE;
  50269. +void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
  50270. +void (*pfkey_error_func)(const char *message, ...) PRINTF_LIKE(1);
  50271. +
  50272. +
  50273. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  50274. +
  50275. +struct satype_tbl {
  50276. + uint8_t proto;
  50277. + uint8_t satype;
  50278. + char* name;
  50279. +} static satype_tbl[] = {
  50280. +#ifdef __KERNEL__
  50281. + { IPPROTO_ESP, SADB_SATYPE_ESP, "ESP" },
  50282. + { IPPROTO_AH, SADB_SATYPE_AH, "AH" },
  50283. + { IPPROTO_IPIP, SADB_X_SATYPE_IPIP, "IPIP" },
  50284. +#ifdef CONFIG_KLIPS_IPCOMP
  50285. + { IPPROTO_COMP, SADB_X_SATYPE_COMP, "COMP" },
  50286. +#endif /* CONFIG_KLIPS_IPCOMP */
  50287. + { IPPROTO_INT, SADB_X_SATYPE_INT, "INT" },
  50288. +#else /* __KERNEL__ */
  50289. + { SA_ESP, SADB_SATYPE_ESP, "ESP" },
  50290. + { SA_AH, SADB_SATYPE_AH, "AH" },
  50291. + { SA_IPIP, SADB_X_SATYPE_IPIP, "IPIP" },
  50292. + { SA_COMP, SADB_X_SATYPE_COMP, "COMP" },
  50293. + { SA_INT, SADB_X_SATYPE_INT, "INT" },
  50294. +#endif /* __KERNEL__ */
  50295. + { 0, 0, "UNKNOWN" }
  50296. +};
  50297. +
  50298. +uint8_t
  50299. +satype2proto(uint8_t satype)
  50300. +{
  50301. + int i =0;
  50302. +
  50303. + while(satype_tbl[i].satype != satype && satype_tbl[i].satype != 0) {
  50304. + i++;
  50305. + }
  50306. + return satype_tbl[i].proto;
  50307. +}
  50308. +
  50309. +uint8_t
  50310. +proto2satype(uint8_t proto)
  50311. +{
  50312. + int i = 0;
  50313. +
  50314. + while(satype_tbl[i].proto != proto && satype_tbl[i].proto != 0) {
  50315. + i++;
  50316. + }
  50317. + return satype_tbl[i].satype;
  50318. +}
  50319. +
  50320. +char*
  50321. +satype2name(uint8_t satype)
  50322. +{
  50323. + int i = 0;
  50324. +
  50325. + while(satype_tbl[i].satype != satype && satype_tbl[i].satype != 0) {
  50326. + i++;
  50327. + }
  50328. + return satype_tbl[i].name;
  50329. +}
  50330. +
  50331. +char*
  50332. +proto2name(uint8_t proto)
  50333. +{
  50334. + int i = 0;
  50335. +
  50336. + while(satype_tbl[i].proto != proto && satype_tbl[i].proto != 0) {
  50337. + i++;
  50338. + }
  50339. + return satype_tbl[i].name;
  50340. +}
  50341. +
  50342. +/* Default extension parsers taken from the KLIPS code */
  50343. +
  50344. +DEBUG_NO_STATIC int
  50345. +pfkey_sa_parse(struct sadb_ext *pfkey_ext)
  50346. +{
  50347. + int error = 0;
  50348. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)pfkey_ext;
  50349. +#if 0
  50350. + struct sadb_sa sav2;
  50351. +#endif
  50352. +
  50353. + /* sanity checks... */
  50354. + if(!pfkey_sa) {
  50355. + ERROR("pfkey_sa_parse: "
  50356. + "NULL pointer passed in.\n");
  50357. + SENDERR(EINVAL);
  50358. + }
  50359. +
  50360. +#if 0
  50361. + /* check if this structure is short, and if so, fix it up.
  50362. + * XXX this is NOT the way to do things.
  50363. + */
  50364. + if(pfkey_sa->sadb_sa_len == sizeof(struct sadb_sa_v1)/IPSEC_PFKEYv2_ALIGN) {
  50365. +
  50366. + /* yes, so clear out a temporary structure, and copy first */
  50367. + memset(&sav2, 0, sizeof(sav2));
  50368. + memcpy(&sav2, pfkey_sa, sizeof(struct sadb_sa_v1));
  50369. + sav2.sadb_x_sa_ref=-1;
  50370. + sav2.sadb_sa_len = sizeof(struct sadb_sa) / IPSEC_PFKEYv2_ALIGN;
  50371. +
  50372. + pfkey_sa = &sav2;
  50373. + }
  50374. +#endif
  50375. +
  50376. +
  50377. + if(pfkey_sa->sadb_sa_len != sizeof(struct sadb_sa) / IPSEC_PFKEYv2_ALIGN) {
  50378. + ERROR(
  50379. + "pfkey_sa_parse: "
  50380. + "length wrong pfkey_sa->sadb_sa_len=%d sizeof(struct sadb_sa)=%d.\n",
  50381. + pfkey_sa->sadb_sa_len,
  50382. + (int)sizeof(struct sadb_sa));
  50383. + SENDERR(EINVAL);
  50384. + }
  50385. +
  50386. +#if SADB_EALG_MAX < 255
  50387. + if(pfkey_sa->sadb_sa_encrypt > SADB_EALG_MAX) {
  50388. + ERROR(
  50389. + "pfkey_sa_parse: "
  50390. + "pfkey_sa->sadb_sa_encrypt=%d > SADB_EALG_MAX=%d.\n",
  50391. + pfkey_sa->sadb_sa_encrypt,
  50392. + SADB_EALG_MAX);
  50393. + SENDERR(EINVAL);
  50394. + }
  50395. +#endif
  50396. +
  50397. +#if SADB_AALG_MAX < 255
  50398. + if(pfkey_sa->sadb_sa_auth > SADB_AALG_MAX) {
  50399. + ERROR(
  50400. + "pfkey_sa_parse: "
  50401. + "pfkey_sa->sadb_sa_auth=%d > SADB_AALG_MAX=%d.\n",
  50402. + pfkey_sa->sadb_sa_auth,
  50403. + SADB_AALG_MAX);
  50404. + SENDERR(EINVAL);
  50405. + }
  50406. +#endif
  50407. +
  50408. +#if SADB_SASTATE_MAX < 255
  50409. + if(pfkey_sa->sadb_sa_state > SADB_SASTATE_MAX) {
  50410. + ERROR(
  50411. + "pfkey_sa_parse: "
  50412. + "state=%d exceeds MAX=%d.\n",
  50413. + pfkey_sa->sadb_sa_state,
  50414. + SADB_SASTATE_MAX);
  50415. + SENDERR(EINVAL);
  50416. + }
  50417. +#endif
  50418. +
  50419. + if(pfkey_sa->sadb_sa_state == SADB_SASTATE_DEAD) {
  50420. + ERROR(
  50421. + "pfkey_sa_parse: "
  50422. + "state=%d is DEAD=%d.\n",
  50423. + pfkey_sa->sadb_sa_state,
  50424. + SADB_SASTATE_DEAD);
  50425. + SENDERR(EINVAL);
  50426. + }
  50427. +
  50428. + if(pfkey_sa->sadb_sa_replay > 64) {
  50429. + ERROR(
  50430. + "pfkey_sa_parse: "
  50431. + "replay window size: %d -- must be 0 <= size <= 64\n",
  50432. + pfkey_sa->sadb_sa_replay);
  50433. + SENDERR(EINVAL);
  50434. + }
  50435. +
  50436. + if(! ((pfkey_sa->sadb_sa_exttype == SADB_EXT_SA) ||
  50437. + (pfkey_sa->sadb_sa_exttype == SADB_X_EXT_SA2)))
  50438. + {
  50439. + ERROR(
  50440. + "pfkey_sa_parse: "
  50441. + "unknown exttype=%d, expecting SADB_EXT_SA=%d or SADB_X_EXT_SA2=%d.\n",
  50442. + pfkey_sa->sadb_sa_exttype,
  50443. + SADB_EXT_SA,
  50444. + SADB_X_EXT_SA2);
  50445. + SENDERR(EINVAL);
  50446. + }
  50447. +
  50448. + if((IPSEC_SAREF_NULL != pfkey_sa->sadb_x_sa_ref) && (pfkey_sa->sadb_x_sa_ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
  50449. + ERROR(
  50450. + "pfkey_sa_parse: "
  50451. + "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
  50452. + pfkey_sa->sadb_x_sa_ref,
  50453. + IPSEC_SAREF_NULL,
  50454. + IPSEC_SA_REF_TABLE_NUM_ENTRIES);
  50455. + SENDERR(EINVAL);
  50456. + }
  50457. +
  50458. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50459. + "pfkey_sa_parse: "
  50460. + "successfully found len=%d exttype=%d(%s) spi=%08lx replay=%d state=%d auth=%d encrypt=%d flags=%d ref=%d.\n",
  50461. + pfkey_sa->sadb_sa_len,
  50462. + pfkey_sa->sadb_sa_exttype,
  50463. + pfkey_v2_sadb_ext_string(pfkey_sa->sadb_sa_exttype),
  50464. + (long unsigned int)ntohl(pfkey_sa->sadb_sa_spi),
  50465. + pfkey_sa->sadb_sa_replay,
  50466. + pfkey_sa->sadb_sa_state,
  50467. + pfkey_sa->sadb_sa_auth,
  50468. + pfkey_sa->sadb_sa_encrypt,
  50469. + pfkey_sa->sadb_sa_flags,
  50470. + pfkey_sa->sadb_x_sa_ref);
  50471. +
  50472. + errlab:
  50473. + return error;
  50474. +}
  50475. +
  50476. +DEBUG_NO_STATIC int
  50477. +pfkey_lifetime_parse(struct sadb_ext *pfkey_ext)
  50478. +{
  50479. + int error = 0;
  50480. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)pfkey_ext;
  50481. +
  50482. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  50483. + "pfkey_lifetime_parse:enter\n");
  50484. + /* sanity checks... */
  50485. + if(!pfkey_lifetime) {
  50486. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50487. + "pfkey_lifetime_parse: "
  50488. + "NULL pointer passed in.\n");
  50489. + SENDERR(EINVAL);
  50490. + }
  50491. +
  50492. + if(pfkey_lifetime->sadb_lifetime_len !=
  50493. + sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN) {
  50494. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50495. + "pfkey_lifetime_parse: "
  50496. + "length wrong pfkey_lifetime->sadb_lifetime_len=%d sizeof(struct sadb_lifetime)=%d.\n",
  50497. + pfkey_lifetime->sadb_lifetime_len,
  50498. + (int)sizeof(struct sadb_lifetime));
  50499. + SENDERR(EINVAL);
  50500. + }
  50501. +
  50502. + if((pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_HARD) &&
  50503. + (pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_SOFT) &&
  50504. + (pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_CURRENT)) {
  50505. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50506. + "pfkey_lifetime_parse: "
  50507. + "unexpected ext_type=%d.\n",
  50508. + pfkey_lifetime->sadb_lifetime_exttype);
  50509. + SENDERR(EINVAL);
  50510. + }
  50511. +
  50512. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50513. + "pfkey_lifetime_parse: "
  50514. + "life_type=%d(%s) alloc=%u bytes=%u add=%u use=%u pkts=%u.\n",
  50515. + pfkey_lifetime->sadb_lifetime_exttype,
  50516. + pfkey_v2_sadb_ext_string(pfkey_lifetime->sadb_lifetime_exttype),
  50517. + pfkey_lifetime->sadb_lifetime_allocations,
  50518. + (unsigned)pfkey_lifetime->sadb_lifetime_bytes,
  50519. + (unsigned)pfkey_lifetime->sadb_lifetime_addtime,
  50520. + (unsigned)pfkey_lifetime->sadb_lifetime_usetime,
  50521. + pfkey_lifetime->sadb_x_lifetime_packets);
  50522. +errlab:
  50523. + return error;
  50524. +}
  50525. +
  50526. +DEBUG_NO_STATIC int
  50527. +pfkey_address_parse(struct sadb_ext *pfkey_ext)
  50528. +{
  50529. + int error = 0;
  50530. + int saddr_len = 0;
  50531. + struct sadb_address *pfkey_address = (struct sadb_address *)pfkey_ext;
  50532. + struct sockaddr* s = (struct sockaddr*)((char*)pfkey_address + sizeof(*pfkey_address));
  50533. + char ipaddr_txt[ADDRTOT_BUF];
  50534. +
  50535. + /* sanity checks... */
  50536. + if(!pfkey_address) {
  50537. + ERROR(
  50538. + "pfkey_address_parse: "
  50539. + "NULL pointer passed in.\n");
  50540. + SENDERR(EINVAL);
  50541. + }
  50542. +
  50543. + if(pfkey_address->sadb_address_len <
  50544. + (sizeof(struct sadb_address) + sizeof(struct sockaddr))/
  50545. + IPSEC_PFKEYv2_ALIGN) {
  50546. + ERROR("pfkey_address_parse: "
  50547. + "size wrong 1 ext_len=%d, adr_ext_len=%d, saddr_len=%d.\n",
  50548. + pfkey_address->sadb_address_len,
  50549. + (int)sizeof(struct sadb_address),
  50550. + (int)sizeof(struct sockaddr));
  50551. + SENDERR(EINVAL);
  50552. + }
  50553. +
  50554. + if(pfkey_address->sadb_address_reserved) {
  50555. + ERROR("pfkey_address_parse: "
  50556. + "res=%d, must be zero.\n",
  50557. + pfkey_address->sadb_address_reserved);
  50558. + SENDERR(EINVAL);
  50559. + }
  50560. +
  50561. + switch(pfkey_address->sadb_address_exttype) {
  50562. + case SADB_EXT_ADDRESS_SRC:
  50563. + case SADB_EXT_ADDRESS_DST:
  50564. + case SADB_EXT_ADDRESS_PROXY:
  50565. + case SADB_X_EXT_ADDRESS_DST2:
  50566. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  50567. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  50568. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  50569. + case SADB_X_EXT_ADDRESS_DST_MASK:
  50570. +#ifdef NAT_TRAVERSAL
  50571. + case SADB_X_EXT_NAT_T_OA:
  50572. +#endif
  50573. + break;
  50574. + default:
  50575. + ERROR(
  50576. + "pfkey_address_parse: "
  50577. + "unexpected ext_type=%d.\n",
  50578. + pfkey_address->sadb_address_exttype);
  50579. + SENDERR(ENOPKG);
  50580. + }
  50581. +
  50582. + switch(s->sa_family) {
  50583. + case AF_INET:
  50584. + saddr_len = sizeof(struct sockaddr_in);
  50585. + sprintf(ipaddr_txt, "%d.%d.%d.%d"
  50586. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 0) & 0xFF
  50587. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 8) & 0xFF
  50588. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 16) & 0xFF
  50589. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 24) & 0xFF);
  50590. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50591. + "pfkey_address_parse: "
  50592. + "found exttype=%u(%s) family=%d(AF_INET) address=%s proto=%u port=%u.\n",
  50593. + pfkey_address->sadb_address_exttype,
  50594. + pfkey_v2_sadb_ext_string(pfkey_address->sadb_address_exttype),
  50595. + s->sa_family,
  50596. + ipaddr_txt,
  50597. + pfkey_address->sadb_address_proto,
  50598. + ntohs(((struct sockaddr_in*)s)->sin_port));
  50599. + break;
  50600. + case AF_INET6:
  50601. + saddr_len = sizeof(struct sockaddr_in6);
  50602. + sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x"
  50603. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[0])
  50604. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[1])
  50605. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[2])
  50606. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[3])
  50607. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[4])
  50608. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[5])
  50609. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[6])
  50610. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[7]));
  50611. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50612. + "pfkey_address_parse: "
  50613. + "found exttype=%u(%s) family=%d(AF_INET6) address=%s proto=%u port=%u.\n",
  50614. + pfkey_address->sadb_address_exttype,
  50615. + pfkey_v2_sadb_ext_string(pfkey_address->sadb_address_exttype),
  50616. + s->sa_family,
  50617. + ipaddr_txt,
  50618. + pfkey_address->sadb_address_proto,
  50619. + ((struct sockaddr_in6*)s)->sin6_port);
  50620. + break;
  50621. + default:
  50622. + ERROR(
  50623. + "pfkey_address_parse: "
  50624. + "s->sa_family=%d not supported.\n",
  50625. + s->sa_family);
  50626. + SENDERR(EPFNOSUPPORT);
  50627. + }
  50628. +
  50629. + if(pfkey_address->sadb_address_len !=
  50630. + DIVUP(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN)) {
  50631. + ERROR(
  50632. + "pfkey_address_parse: "
  50633. + "size wrong 2 ext_len=%d, adr_ext_len=%d, saddr_len=%d.\n",
  50634. + pfkey_address->sadb_address_len,
  50635. + (int)sizeof(struct sadb_address),
  50636. + saddr_len);
  50637. + SENDERR(EINVAL);
  50638. + }
  50639. +
  50640. + if(pfkey_address->sadb_address_prefixlen != 0) {
  50641. + ERROR(
  50642. + "pfkey_address_parse: "
  50643. + "address prefixes not supported yet.\n");
  50644. + SENDERR(EAFNOSUPPORT); /* not supported yet */
  50645. + }
  50646. +
  50647. + /* XXX check if port!=0 */
  50648. +
  50649. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  50650. + "pfkey_address_parse: successful.\n");
  50651. + errlab:
  50652. + return error;
  50653. +}
  50654. +
  50655. +DEBUG_NO_STATIC int
  50656. +pfkey_key_parse(struct sadb_ext *pfkey_ext)
  50657. +{
  50658. + int error = 0;
  50659. + struct sadb_key *pfkey_key = (struct sadb_key *)pfkey_ext;
  50660. +
  50661. + /* sanity checks... */
  50662. +
  50663. + if(!pfkey_key) {
  50664. + ERROR(
  50665. + "pfkey_key_parse: "
  50666. + "NULL pointer passed in.\n");
  50667. + SENDERR(EINVAL);
  50668. + }
  50669. +
  50670. + if(pfkey_key->sadb_key_len < sizeof(struct sadb_key) / IPSEC_PFKEYv2_ALIGN) {
  50671. + ERROR(
  50672. + "pfkey_key_parse: "
  50673. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50674. + pfkey_key->sadb_key_len,
  50675. + (int)sizeof(struct sadb_key));
  50676. + SENDERR(EINVAL);
  50677. + }
  50678. +
  50679. + if(!pfkey_key->sadb_key_bits) {
  50680. + ERROR(
  50681. + "pfkey_key_parse: "
  50682. + "key length set to zero, must be non-zero.\n");
  50683. + SENDERR(EINVAL);
  50684. + }
  50685. +
  50686. + if(pfkey_key->sadb_key_len !=
  50687. + DIVUP(sizeof(struct sadb_key) * OCTETBITS + pfkey_key->sadb_key_bits,
  50688. + PFKEYBITS)) {
  50689. + ERROR(
  50690. + "pfkey_key_parse: "
  50691. + "key length=%d does not agree with extension length=%d.\n",
  50692. + pfkey_key->sadb_key_bits,
  50693. + pfkey_key->sadb_key_len);
  50694. + SENDERR(EINVAL);
  50695. + }
  50696. +
  50697. + if(pfkey_key->sadb_key_reserved) {
  50698. + ERROR(
  50699. + "pfkey_key_parse: "
  50700. + "res=%d, must be zero.\n",
  50701. + pfkey_key->sadb_key_reserved);
  50702. + SENDERR(EINVAL);
  50703. + }
  50704. +
  50705. + if(! ( (pfkey_key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ||
  50706. + (pfkey_key->sadb_key_exttype == SADB_EXT_KEY_ENCRYPT))) {
  50707. + ERROR(
  50708. + "pfkey_key_parse: "
  50709. + "expecting extension type AUTH or ENCRYPT, got %d.\n",
  50710. + pfkey_key->sadb_key_exttype);
  50711. + SENDERR(EINVAL);
  50712. + }
  50713. +
  50714. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50715. + "pfkey_key_parse: "
  50716. + "success, found len=%d exttype=%d(%s) bits=%d reserved=%d.\n",
  50717. + pfkey_key->sadb_key_len,
  50718. + pfkey_key->sadb_key_exttype,
  50719. + pfkey_v2_sadb_ext_string(pfkey_key->sadb_key_exttype),
  50720. + pfkey_key->sadb_key_bits,
  50721. + pfkey_key->sadb_key_reserved);
  50722. +
  50723. +errlab:
  50724. + return error;
  50725. +}
  50726. +
  50727. +DEBUG_NO_STATIC int
  50728. +pfkey_ident_parse(struct sadb_ext *pfkey_ext)
  50729. +{
  50730. + int error = 0;
  50731. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)pfkey_ext;
  50732. +
  50733. + /* sanity checks... */
  50734. + if(pfkey_ident->sadb_ident_len < sizeof(struct sadb_ident) / IPSEC_PFKEYv2_ALIGN) {
  50735. + ERROR(
  50736. + "pfkey_ident_parse: "
  50737. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50738. + pfkey_ident->sadb_ident_len,
  50739. + (int)sizeof(struct sadb_ident));
  50740. + SENDERR(EINVAL);
  50741. + }
  50742. +
  50743. + if(pfkey_ident->sadb_ident_type > SADB_IDENTTYPE_MAX) {
  50744. + ERROR(
  50745. + "pfkey_ident_parse: "
  50746. + "ident_type=%d out of range, must be less than %d.\n",
  50747. + pfkey_ident->sadb_ident_type,
  50748. + SADB_IDENTTYPE_MAX);
  50749. + SENDERR(EINVAL);
  50750. + }
  50751. +
  50752. + if(pfkey_ident->sadb_ident_reserved) {
  50753. + ERROR(
  50754. + "pfkey_ident_parse: "
  50755. + "res=%d, must be zero.\n",
  50756. + pfkey_ident->sadb_ident_reserved);
  50757. + SENDERR(EINVAL);
  50758. + }
  50759. +
  50760. + /* string terminator/padding must be zero */
  50761. + if(pfkey_ident->sadb_ident_len > sizeof(struct sadb_ident) / IPSEC_PFKEYv2_ALIGN) {
  50762. + if(*((char*)pfkey_ident + pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - 1)) {
  50763. + ERROR(
  50764. + "pfkey_ident_parse: "
  50765. + "string padding must be zero, last is 0x%02x.\n",
  50766. + *((char*)pfkey_ident +
  50767. + pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - 1));
  50768. + SENDERR(EINVAL);
  50769. + }
  50770. + }
  50771. +
  50772. + if( ! ((pfkey_ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ||
  50773. + (pfkey_ident->sadb_ident_exttype == SADB_EXT_IDENTITY_DST))) {
  50774. + ERROR(
  50775. + "pfkey_key_parse: "
  50776. + "expecting extension type IDENTITY_SRC or IDENTITY_DST, got %d.\n",
  50777. + pfkey_ident->sadb_ident_exttype);
  50778. + SENDERR(EINVAL);
  50779. + }
  50780. +
  50781. +errlab:
  50782. + return error;
  50783. +}
  50784. +
  50785. +DEBUG_NO_STATIC int
  50786. +pfkey_sens_parse(struct sadb_ext *pfkey_ext)
  50787. +{
  50788. + int error = 0;
  50789. + struct sadb_sens *pfkey_sens = (struct sadb_sens *)pfkey_ext;
  50790. +
  50791. + /* sanity checks... */
  50792. + if(pfkey_sens->sadb_sens_len < sizeof(struct sadb_sens) / IPSEC_PFKEYv2_ALIGN) {
  50793. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50794. + "pfkey_sens_parse: "
  50795. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50796. + pfkey_sens->sadb_sens_len,
  50797. + (int)sizeof(struct sadb_sens));
  50798. + SENDERR(EINVAL);
  50799. + }
  50800. +
  50801. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50802. + "pfkey_sens_parse: "
  50803. + "Sorry, I can't parse exttype=%d yet.\n",
  50804. + pfkey_ext->sadb_ext_type);
  50805. +#if 0
  50806. + SENDERR(EINVAL); /* don't process these yet */
  50807. +#endif
  50808. +
  50809. +errlab:
  50810. + return error;
  50811. +}
  50812. +
  50813. +DEBUG_NO_STATIC int
  50814. +pfkey_prop_parse(struct sadb_ext *pfkey_ext)
  50815. +{
  50816. + int error = 0;
  50817. + int i, num_comb;
  50818. + struct sadb_prop *pfkey_prop = (struct sadb_prop *)pfkey_ext;
  50819. + struct sadb_comb *pfkey_comb = (struct sadb_comb *)((char*)pfkey_ext + sizeof(struct sadb_prop));
  50820. +
  50821. + /* sanity checks... */
  50822. + if((pfkey_prop->sadb_prop_len < sizeof(struct sadb_prop) / IPSEC_PFKEYv2_ALIGN) ||
  50823. + (((pfkey_prop->sadb_prop_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_prop)) % sizeof(struct sadb_comb))) {
  50824. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50825. + "pfkey_prop_parse: "
  50826. + "size wrong ext_len=%d, prop_ext_len=%d comb_ext_len=%d.\n",
  50827. + pfkey_prop->sadb_prop_len,
  50828. + (int)sizeof(struct sadb_prop),
  50829. + (int)sizeof(struct sadb_comb));
  50830. + SENDERR(EINVAL);
  50831. + }
  50832. +
  50833. + if(pfkey_prop->sadb_prop_replay > 64) {
  50834. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50835. + "pfkey_prop_parse: "
  50836. + "replay window size: %d -- must be 0 <= size <= 64\n",
  50837. + pfkey_prop->sadb_prop_replay);
  50838. + SENDERR(EINVAL);
  50839. + }
  50840. +
  50841. + for(i=0; i<3; i++) {
  50842. + if(pfkey_prop->sadb_prop_reserved[i]) {
  50843. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50844. + "pfkey_prop_parse: "
  50845. + "res[%d]=%d, must be zero.\n",
  50846. + i, pfkey_prop->sadb_prop_reserved[i]);
  50847. + SENDERR(EINVAL);
  50848. + }
  50849. + }
  50850. +
  50851. + num_comb = ((pfkey_prop->sadb_prop_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_prop)) / sizeof(struct sadb_comb);
  50852. +
  50853. + for(i = 0; i < num_comb; i++) {
  50854. + if(pfkey_comb->sadb_comb_auth > SADB_AALG_MAX) {
  50855. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50856. + "pfkey_prop_parse: "
  50857. + "pfkey_comb[%d]->sadb_comb_auth=%d > SADB_AALG_MAX=%d.\n",
  50858. + i,
  50859. + pfkey_comb->sadb_comb_auth,
  50860. + SADB_AALG_MAX);
  50861. + SENDERR(EINVAL);
  50862. + }
  50863. +
  50864. + if(pfkey_comb->sadb_comb_auth) {
  50865. + if(!pfkey_comb->sadb_comb_auth_minbits) {
  50866. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50867. + "pfkey_prop_parse: "
  50868. + "pfkey_comb[%d]->sadb_comb_auth_minbits=0, fatal.\n",
  50869. + i);
  50870. + SENDERR(EINVAL);
  50871. + }
  50872. + if(!pfkey_comb->sadb_comb_auth_maxbits) {
  50873. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50874. + "pfkey_prop_parse: "
  50875. + "pfkey_comb[%d]->sadb_comb_auth_maxbits=0, fatal.\n",
  50876. + i);
  50877. + SENDERR(EINVAL);
  50878. + }
  50879. + if(pfkey_comb->sadb_comb_auth_minbits > pfkey_comb->sadb_comb_auth_maxbits) {
  50880. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50881. + "pfkey_prop_parse: "
  50882. + "pfkey_comb[%d]->sadb_comb_auth_minbits=%d > maxbits=%d, fatal.\n",
  50883. + i,
  50884. + pfkey_comb->sadb_comb_auth_minbits,
  50885. + pfkey_comb->sadb_comb_auth_maxbits);
  50886. + SENDERR(EINVAL);
  50887. + }
  50888. + } else {
  50889. + if(pfkey_comb->sadb_comb_auth_minbits) {
  50890. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50891. + "pfkey_prop_parse: "
  50892. + "pfkey_comb[%d]->sadb_comb_auth_minbits=%d != 0, fatal.\n",
  50893. + i,
  50894. + pfkey_comb->sadb_comb_auth_minbits);
  50895. + SENDERR(EINVAL);
  50896. + }
  50897. + if(pfkey_comb->sadb_comb_auth_maxbits) {
  50898. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50899. + "pfkey_prop_parse: "
  50900. + "pfkey_comb[%d]->sadb_comb_auth_maxbits=%d != 0, fatal.\n",
  50901. + i,
  50902. + pfkey_comb->sadb_comb_auth_maxbits);
  50903. + SENDERR(EINVAL);
  50904. + }
  50905. + }
  50906. +
  50907. +#if SADB_EALG_MAX < 255
  50908. + if(pfkey_comb->sadb_comb_encrypt > SADB_EALG_MAX) {
  50909. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50910. + "pfkey_comb_parse: "
  50911. + "pfkey_comb[%d]->sadb_comb_encrypt=%d > SADB_EALG_MAX=%d.\n",
  50912. + i,
  50913. + pfkey_comb->sadb_comb_encrypt,
  50914. + SADB_EALG_MAX);
  50915. + SENDERR(EINVAL);
  50916. + }
  50917. +#endif
  50918. +
  50919. + if(pfkey_comb->sadb_comb_encrypt) {
  50920. + if(!pfkey_comb->sadb_comb_encrypt_minbits) {
  50921. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50922. + "pfkey_prop_parse: "
  50923. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=0, fatal.\n",
  50924. + i);
  50925. + SENDERR(EINVAL);
  50926. + }
  50927. + if(!pfkey_comb->sadb_comb_encrypt_maxbits) {
  50928. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50929. + "pfkey_prop_parse: "
  50930. + "pfkey_comb[%d]->sadb_comb_encrypt_maxbits=0, fatal.\n",
  50931. + i);
  50932. + SENDERR(EINVAL);
  50933. + }
  50934. + if(pfkey_comb->sadb_comb_encrypt_minbits > pfkey_comb->sadb_comb_encrypt_maxbits) {
  50935. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50936. + "pfkey_prop_parse: "
  50937. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=%d > maxbits=%d, fatal.\n",
  50938. + i,
  50939. + pfkey_comb->sadb_comb_encrypt_minbits,
  50940. + pfkey_comb->sadb_comb_encrypt_maxbits);
  50941. + SENDERR(EINVAL);
  50942. + }
  50943. + } else {
  50944. + if(pfkey_comb->sadb_comb_encrypt_minbits) {
  50945. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50946. + "pfkey_prop_parse: "
  50947. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=%d != 0, fatal.\n",
  50948. + i,
  50949. + pfkey_comb->sadb_comb_encrypt_minbits);
  50950. + SENDERR(EINVAL);
  50951. + }
  50952. + if(pfkey_comb->sadb_comb_encrypt_maxbits) {
  50953. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50954. + "pfkey_prop_parse: "
  50955. + "pfkey_comb[%d]->sadb_comb_encrypt_maxbits=%d != 0, fatal.\n",
  50956. + i,
  50957. + pfkey_comb->sadb_comb_encrypt_maxbits);
  50958. + SENDERR(EINVAL);
  50959. + }
  50960. + }
  50961. +
  50962. + /* XXX do sanity check on flags */
  50963. +
  50964. + if(pfkey_comb->sadb_comb_hard_allocations && pfkey_comb->sadb_comb_soft_allocations > pfkey_comb->sadb_comb_hard_allocations) {
  50965. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50966. + "pfkey_prop_parse: "
  50967. + "pfkey_comb[%d]->sadb_comb_soft_allocations=%d > hard_allocations=%d, fatal.\n",
  50968. + i,
  50969. + pfkey_comb->sadb_comb_soft_allocations,
  50970. + pfkey_comb->sadb_comb_hard_allocations);
  50971. + SENDERR(EINVAL);
  50972. + }
  50973. +
  50974. + if(pfkey_comb->sadb_comb_hard_bytes && pfkey_comb->sadb_comb_soft_bytes > pfkey_comb->sadb_comb_hard_bytes) {
  50975. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50976. + "pfkey_prop_parse: "
  50977. + "pfkey_comb[%d]->sadb_comb_soft_bytes=%Ld > hard_bytes=%Ld, fatal.\n",
  50978. + i,
  50979. + (unsigned long long int)pfkey_comb->sadb_comb_soft_bytes,
  50980. + (unsigned long long int)pfkey_comb->sadb_comb_hard_bytes);
  50981. + SENDERR(EINVAL);
  50982. + }
  50983. +
  50984. + if(pfkey_comb->sadb_comb_hard_addtime && pfkey_comb->sadb_comb_soft_addtime > pfkey_comb->sadb_comb_hard_addtime) {
  50985. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50986. + "pfkey_prop_parse: "
  50987. + "pfkey_comb[%d]->sadb_comb_soft_addtime=%Ld > hard_addtime=%Ld, fatal.\n",
  50988. + i,
  50989. + (unsigned long long int)pfkey_comb->sadb_comb_soft_addtime,
  50990. + (unsigned long long int)pfkey_comb->sadb_comb_hard_addtime);
  50991. + SENDERR(EINVAL);
  50992. + }
  50993. +
  50994. + if(pfkey_comb->sadb_comb_hard_usetime && pfkey_comb->sadb_comb_soft_usetime > pfkey_comb->sadb_comb_hard_usetime) {
  50995. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50996. + "pfkey_prop_parse: "
  50997. + "pfkey_comb[%d]->sadb_comb_soft_usetime=%Ld > hard_usetime=%Ld, fatal.\n",
  50998. + i,
  50999. + (unsigned long long int)pfkey_comb->sadb_comb_soft_usetime,
  51000. + (unsigned long long int)pfkey_comb->sadb_comb_hard_usetime);
  51001. + SENDERR(EINVAL);
  51002. + }
  51003. +
  51004. + if(pfkey_comb->sadb_x_comb_hard_packets && pfkey_comb->sadb_x_comb_soft_packets > pfkey_comb->sadb_x_comb_hard_packets) {
  51005. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51006. + "pfkey_prop_parse: "
  51007. + "pfkey_comb[%d]->sadb_x_comb_soft_packets=%d > hard_packets=%d, fatal.\n",
  51008. + i,
  51009. + pfkey_comb->sadb_x_comb_soft_packets,
  51010. + pfkey_comb->sadb_x_comb_hard_packets);
  51011. + SENDERR(EINVAL);
  51012. + }
  51013. +
  51014. + if(pfkey_comb->sadb_comb_reserved) {
  51015. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51016. + "pfkey_prop_parse: "
  51017. + "comb[%d].res=%d, must be zero.\n",
  51018. + i,
  51019. + pfkey_comb->sadb_comb_reserved);
  51020. + SENDERR(EINVAL);
  51021. + }
  51022. + pfkey_comb++;
  51023. + }
  51024. +
  51025. +errlab:
  51026. + return error;
  51027. +}
  51028. +
  51029. +DEBUG_NO_STATIC int
  51030. +pfkey_supported_parse(struct sadb_ext *pfkey_ext)
  51031. +{
  51032. + int error = 0;
  51033. + unsigned int i, num_alg;
  51034. + struct sadb_supported *pfkey_supported = (struct sadb_supported *)pfkey_ext;
  51035. + struct sadb_alg *pfkey_alg = (struct sadb_alg*)((char*)pfkey_ext + sizeof(struct sadb_supported));
  51036. +
  51037. + /* sanity checks... */
  51038. + if((pfkey_supported->sadb_supported_len <
  51039. + sizeof(struct sadb_supported) / IPSEC_PFKEYv2_ALIGN) ||
  51040. + (((pfkey_supported->sadb_supported_len * IPSEC_PFKEYv2_ALIGN) -
  51041. + sizeof(struct sadb_supported)) % sizeof(struct sadb_alg))) {
  51042. +
  51043. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51044. + "pfkey_supported_parse: "
  51045. + "size wrong ext_len=%d, supported_ext_len=%d alg_ext_len=%d.\n",
  51046. + pfkey_supported->sadb_supported_len,
  51047. + (int)sizeof(struct sadb_supported),
  51048. + (int)sizeof(struct sadb_alg));
  51049. + SENDERR(EINVAL);
  51050. + }
  51051. +
  51052. + if(pfkey_supported->sadb_supported_reserved) {
  51053. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51054. + "pfkey_supported_parse: "
  51055. + "res=%d, must be zero.\n",
  51056. + pfkey_supported->sadb_supported_reserved);
  51057. + SENDERR(EINVAL);
  51058. + }
  51059. +
  51060. + num_alg = ((pfkey_supported->sadb_supported_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_supported)) / sizeof(struct sadb_alg);
  51061. +
  51062. + for(i = 0; i < num_alg; i++) {
  51063. + /* process algo description */
  51064. + if(pfkey_alg->sadb_alg_reserved) {
  51065. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51066. + "pfkey_supported_parse: "
  51067. + "alg[%d], id=%d, ivlen=%d, minbits=%d, maxbits=%d, res=%d, must be zero.\n",
  51068. + i,
  51069. + pfkey_alg->sadb_alg_id,
  51070. + pfkey_alg->sadb_alg_ivlen,
  51071. + pfkey_alg->sadb_alg_minbits,
  51072. + pfkey_alg->sadb_alg_maxbits,
  51073. + pfkey_alg->sadb_alg_reserved);
  51074. + SENDERR(EINVAL);
  51075. + }
  51076. +
  51077. + /* XXX can alg_id auth/enc be determined from info given?
  51078. + Yes, but OpenBSD's method does not iteroperate with rfc2367.
  51079. + rgb, 2000-04-06 */
  51080. +
  51081. + switch(pfkey_supported->sadb_supported_exttype) {
  51082. + case SADB_EXT_SUPPORTED_AUTH:
  51083. + if(pfkey_alg->sadb_alg_id > SADB_AALG_MAX) {
  51084. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51085. + "pfkey_supported_parse: "
  51086. + "alg[%d], alg_id=%d > SADB_AALG_MAX=%d, fatal.\n",
  51087. + i,
  51088. + pfkey_alg->sadb_alg_id,
  51089. + SADB_AALG_MAX);
  51090. + SENDERR(EINVAL);
  51091. + }
  51092. + break;
  51093. + case SADB_EXT_SUPPORTED_ENCRYPT:
  51094. +#if SADB_EALG_MAX < 255
  51095. + if(pfkey_alg->sadb_alg_id > SADB_EALG_MAX) {
  51096. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51097. + "pfkey_supported_parse: "
  51098. + "alg[%d], alg_id=%d > SADB_EALG_MAX=%d, fatal.\n",
  51099. + i,
  51100. + pfkey_alg->sadb_alg_id,
  51101. + SADB_EALG_MAX);
  51102. + SENDERR(EINVAL);
  51103. + }
  51104. +#endif
  51105. + break;
  51106. + default:
  51107. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51108. + "pfkey_supported_parse: "
  51109. + "alg[%d], alg_id=%d > SADB_EALG_MAX=%d, fatal.\n",
  51110. + i,
  51111. + pfkey_alg->sadb_alg_id,
  51112. + SADB_EALG_MAX);
  51113. + SENDERR(EINVAL);
  51114. + }
  51115. + pfkey_alg++;
  51116. + }
  51117. +
  51118. + errlab:
  51119. + return error;
  51120. +}
  51121. +
  51122. +DEBUG_NO_STATIC int
  51123. +pfkey_spirange_parse(struct sadb_ext *pfkey_ext)
  51124. +{
  51125. + int error = 0;
  51126. + struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)pfkey_ext;
  51127. +
  51128. + /* sanity checks... */
  51129. + if(pfkey_spirange->sadb_spirange_len !=
  51130. + sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN) {
  51131. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51132. + "pfkey_spirange_parse: "
  51133. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51134. + pfkey_spirange->sadb_spirange_len,
  51135. + (int)sizeof(struct sadb_spirange));
  51136. + SENDERR(EINVAL);
  51137. + }
  51138. +
  51139. + if(pfkey_spirange->sadb_spirange_reserved) {
  51140. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51141. + "pfkey_spirange_parse: "
  51142. + "reserved=%d must be set to zero.\n",
  51143. + pfkey_spirange->sadb_spirange_reserved);
  51144. + SENDERR(EINVAL);
  51145. + }
  51146. +
  51147. + if(ntohl(pfkey_spirange->sadb_spirange_max) < ntohl(pfkey_spirange->sadb_spirange_min)) {
  51148. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51149. + "pfkey_spirange_parse: "
  51150. + "minspi=%08x must be < maxspi=%08x.\n",
  51151. + ntohl(pfkey_spirange->sadb_spirange_min),
  51152. + ntohl(pfkey_spirange->sadb_spirange_max));
  51153. + SENDERR(EINVAL);
  51154. + }
  51155. +
  51156. + if(ntohl(pfkey_spirange->sadb_spirange_min) <= 255) {
  51157. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51158. + "pfkey_spirange_parse: "
  51159. + "minspi=%08x must be > 255.\n",
  51160. + ntohl(pfkey_spirange->sadb_spirange_min));
  51161. + SENDERR(EEXIST);
  51162. + }
  51163. +
  51164. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51165. + "pfkey_spirange_parse: "
  51166. + "ext_len=%u ext_type=%u(%s) min=%u max=%u res=%u.\n",
  51167. + pfkey_spirange->sadb_spirange_len,
  51168. + pfkey_spirange->sadb_spirange_exttype,
  51169. + pfkey_v2_sadb_ext_string(pfkey_spirange->sadb_spirange_exttype),
  51170. + pfkey_spirange->sadb_spirange_min,
  51171. + pfkey_spirange->sadb_spirange_max,
  51172. + pfkey_spirange->sadb_spirange_reserved);
  51173. + errlab:
  51174. + return error;
  51175. +}
  51176. +
  51177. +DEBUG_NO_STATIC int
  51178. +pfkey_x_kmprivate_parse(struct sadb_ext *pfkey_ext)
  51179. +{
  51180. + int error = 0;
  51181. + struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)pfkey_ext;
  51182. +
  51183. + /* sanity checks... */
  51184. + if(pfkey_x_kmprivate->sadb_x_kmprivate_len <
  51185. + sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN) {
  51186. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51187. + "pfkey_x_kmprivate_parse: "
  51188. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51189. + pfkey_x_kmprivate->sadb_x_kmprivate_len,
  51190. + (int)sizeof(struct sadb_x_kmprivate));
  51191. + SENDERR(EINVAL);
  51192. + }
  51193. +
  51194. + if(pfkey_x_kmprivate->sadb_x_kmprivate_reserved) {
  51195. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51196. + "pfkey_x_kmprivate_parse: "
  51197. + "reserved=%d must be set to zero.\n",
  51198. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved);
  51199. + SENDERR(EINVAL);
  51200. + }
  51201. +
  51202. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51203. + "pfkey_x_kmprivate_parse: "
  51204. + "Sorry, I can't parse exttype=%d yet.\n",
  51205. + pfkey_ext->sadb_ext_type);
  51206. + SENDERR(EINVAL); /* don't process these yet */
  51207. +
  51208. +errlab:
  51209. + return error;
  51210. +}
  51211. +
  51212. +DEBUG_NO_STATIC int
  51213. +pfkey_x_satype_parse(struct sadb_ext *pfkey_ext)
  51214. +{
  51215. + int error = 0;
  51216. + int i;
  51217. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)pfkey_ext;
  51218. +
  51219. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51220. + "pfkey_x_satype_parse: enter\n");
  51221. + /* sanity checks... */
  51222. + if(pfkey_x_satype->sadb_x_satype_len !=
  51223. + sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN) {
  51224. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51225. + "pfkey_x_satype_parse: "
  51226. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51227. + pfkey_x_satype->sadb_x_satype_len,
  51228. + (int)sizeof(struct sadb_x_satype));
  51229. + SENDERR(EINVAL);
  51230. + }
  51231. +
  51232. + if(!pfkey_x_satype->sadb_x_satype_satype) {
  51233. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51234. + "pfkey_x_satype_parse: "
  51235. + "satype is zero, must be non-zero.\n");
  51236. + SENDERR(EINVAL);
  51237. + }
  51238. +
  51239. + if(pfkey_x_satype->sadb_x_satype_satype > SADB_SATYPE_MAX) {
  51240. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51241. + "pfkey_x_satype_parse: "
  51242. + "satype %d > max %d, invalid.\n",
  51243. + pfkey_x_satype->sadb_x_satype_satype, SADB_SATYPE_MAX);
  51244. + SENDERR(EINVAL);
  51245. + }
  51246. +
  51247. + if(!(satype2proto(pfkey_x_satype->sadb_x_satype_satype))) {
  51248. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51249. + "pfkey_x_satype_parse: "
  51250. + "proto lookup from satype=%d failed.\n",
  51251. + pfkey_x_satype->sadb_x_satype_satype);
  51252. + SENDERR(EINVAL);
  51253. + }
  51254. +
  51255. + for(i = 0; i < 3; i++) {
  51256. + if(pfkey_x_satype->sadb_x_satype_reserved[i]) {
  51257. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51258. + "pfkey_x_satype_parse: "
  51259. + "reserved[%d]=%d must be set to zero.\n",
  51260. + i, pfkey_x_satype->sadb_x_satype_reserved[i]);
  51261. + SENDERR(EINVAL);
  51262. + }
  51263. + }
  51264. +
  51265. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51266. + "pfkey_x_satype_parse: "
  51267. + "len=%u ext=%u(%s) satype=%u(%s) res=%u,%u,%u.\n",
  51268. + pfkey_x_satype->sadb_x_satype_len,
  51269. + pfkey_x_satype->sadb_x_satype_exttype,
  51270. + pfkey_v2_sadb_ext_string(pfkey_x_satype->sadb_x_satype_exttype),
  51271. + pfkey_x_satype->sadb_x_satype_satype,
  51272. + satype2name(pfkey_x_satype->sadb_x_satype_satype),
  51273. + pfkey_x_satype->sadb_x_satype_reserved[0],
  51274. + pfkey_x_satype->sadb_x_satype_reserved[1],
  51275. + pfkey_x_satype->sadb_x_satype_reserved[2]);
  51276. +errlab:
  51277. + return error;
  51278. +}
  51279. +
  51280. +DEBUG_NO_STATIC int
  51281. +pfkey_x_ext_debug_parse(struct sadb_ext *pfkey_ext)
  51282. +{
  51283. + int error = 0;
  51284. + int i;
  51285. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)pfkey_ext;
  51286. +
  51287. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51288. + "pfkey_x_debug_parse: enter\n");
  51289. + /* sanity checks... */
  51290. + if(pfkey_x_debug->sadb_x_debug_len !=
  51291. + sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN) {
  51292. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51293. + "pfkey_x_debug_parse: "
  51294. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51295. + pfkey_x_debug->sadb_x_debug_len,
  51296. + (int)sizeof(struct sadb_x_debug));
  51297. + SENDERR(EINVAL);
  51298. + }
  51299. +
  51300. + for(i = 0; i < 4; i++) {
  51301. + if(pfkey_x_debug->sadb_x_debug_reserved[i]) {
  51302. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51303. + "pfkey_x_debug_parse: "
  51304. + "reserved[%d]=%d must be set to zero.\n",
  51305. + i, pfkey_x_debug->sadb_x_debug_reserved[i]);
  51306. + SENDERR(EINVAL);
  51307. + }
  51308. + }
  51309. +
  51310. +errlab:
  51311. + return error;
  51312. +}
  51313. +
  51314. +DEBUG_NO_STATIC int
  51315. +pfkey_x_ext_protocol_parse(struct sadb_ext *pfkey_ext)
  51316. +{
  51317. + int error = 0;
  51318. + struct sadb_protocol *p = (struct sadb_protocol *)pfkey_ext;
  51319. +
  51320. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM, "pfkey_x_protocol_parse:\n");
  51321. + /* sanity checks... */
  51322. +
  51323. + if (p->sadb_protocol_len != sizeof(*p)/IPSEC_PFKEYv2_ALIGN) {
  51324. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51325. + "pfkey_x_protocol_parse: size wrong ext_len=%d, key_ext_len=%d.\n",
  51326. + p->sadb_protocol_len, (int)sizeof(*p));
  51327. + SENDERR(EINVAL);
  51328. + }
  51329. +
  51330. + if (p->sadb_protocol_reserved2 != 0) {
  51331. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51332. + "pfkey_protocol_parse: res=%d, must be zero.\n",
  51333. + p->sadb_protocol_reserved2);
  51334. + SENDERR(EINVAL);
  51335. + }
  51336. +
  51337. + errlab:
  51338. + return error;
  51339. +}
  51340. +
  51341. +#ifdef NAT_TRAVERSAL
  51342. +DEBUG_NO_STATIC int
  51343. +pfkey_x_ext_nat_t_type_parse(struct sadb_ext *pfkey_ext)
  51344. +{
  51345. + return 0;
  51346. +}
  51347. +DEBUG_NO_STATIC int
  51348. +pfkey_x_ext_nat_t_port_parse(struct sadb_ext *pfkey_ext)
  51349. +{
  51350. + return 0;
  51351. +}
  51352. +#endif
  51353. +
  51354. +#define DEFINEPARSER(NAME) static struct pf_key_ext_parsers_def NAME##_def={NAME, #NAME};
  51355. +
  51356. +DEFINEPARSER(pfkey_sa_parse);
  51357. +DEFINEPARSER(pfkey_lifetime_parse);
  51358. +DEFINEPARSER(pfkey_address_parse);
  51359. +DEFINEPARSER(pfkey_key_parse);
  51360. +DEFINEPARSER(pfkey_ident_parse);
  51361. +DEFINEPARSER(pfkey_sens_parse);
  51362. +DEFINEPARSER(pfkey_prop_parse);
  51363. +DEFINEPARSER(pfkey_supported_parse);
  51364. +DEFINEPARSER(pfkey_spirange_parse);
  51365. +DEFINEPARSER(pfkey_x_kmprivate_parse);
  51366. +DEFINEPARSER(pfkey_x_satype_parse);
  51367. +DEFINEPARSER(pfkey_x_ext_debug_parse);
  51368. +DEFINEPARSER(pfkey_x_ext_protocol_parse);
  51369. +#ifdef NAT_TRAVERSAL
  51370. +DEFINEPARSER(pfkey_x_ext_nat_t_type_parse);
  51371. +DEFINEPARSER(pfkey_x_ext_nat_t_port_parse);
  51372. +#endif
  51373. +
  51374. +struct pf_key_ext_parsers_def *ext_default_parsers[]=
  51375. +{
  51376. + NULL, /* pfkey_msg_parse, */
  51377. + &pfkey_sa_parse_def,
  51378. + &pfkey_lifetime_parse_def,
  51379. + &pfkey_lifetime_parse_def,
  51380. + &pfkey_lifetime_parse_def,
  51381. + &pfkey_address_parse_def,
  51382. + &pfkey_address_parse_def,
  51383. + &pfkey_address_parse_def,
  51384. + &pfkey_key_parse_def,
  51385. + &pfkey_key_parse_def,
  51386. + &pfkey_ident_parse_def,
  51387. + &pfkey_ident_parse_def,
  51388. + &pfkey_sens_parse_def,
  51389. + &pfkey_prop_parse_def,
  51390. + &pfkey_supported_parse_def,
  51391. + &pfkey_supported_parse_def,
  51392. + &pfkey_spirange_parse_def,
  51393. + &pfkey_x_kmprivate_parse_def,
  51394. + &pfkey_x_satype_parse_def,
  51395. + &pfkey_sa_parse_def,
  51396. + &pfkey_address_parse_def,
  51397. + &pfkey_address_parse_def,
  51398. + &pfkey_address_parse_def,
  51399. + &pfkey_address_parse_def,
  51400. + &pfkey_address_parse_def,
  51401. + &pfkey_x_ext_debug_parse_def,
  51402. + &pfkey_x_ext_protocol_parse_def
  51403. +#ifdef NAT_TRAVERSAL
  51404. + ,
  51405. + &pfkey_x_ext_nat_t_type_parse_def,
  51406. + &pfkey_x_ext_nat_t_port_parse_def,
  51407. + &pfkey_x_ext_nat_t_port_parse_def,
  51408. + &pfkey_address_parse_def
  51409. +#endif
  51410. +};
  51411. +
  51412. +int
  51413. +pfkey_msg_parse(struct sadb_msg *pfkey_msg,
  51414. + struct pf_key_ext_parsers_def *ext_parsers[],
  51415. + struct sadb_ext *extensions[],
  51416. + int dir)
  51417. +{
  51418. + int error = 0;
  51419. + int remain;
  51420. + struct sadb_ext *pfkey_ext;
  51421. + int extensions_seen = 0;
  51422. +
  51423. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51424. + "pfkey_msg_parse: "
  51425. + "parsing message ver=%d, type=%d(%s), errno=%d, satype=%d(%s), len=%d, res=%d, seq=%d, pid=%d.\n",
  51426. + pfkey_msg->sadb_msg_version,
  51427. + pfkey_msg->sadb_msg_type,
  51428. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type),
  51429. + pfkey_msg->sadb_msg_errno,
  51430. + pfkey_msg->sadb_msg_satype,
  51431. + satype2name(pfkey_msg->sadb_msg_satype),
  51432. + pfkey_msg->sadb_msg_len,
  51433. + pfkey_msg->sadb_msg_reserved,
  51434. + pfkey_msg->sadb_msg_seq,
  51435. + pfkey_msg->sadb_msg_pid);
  51436. +
  51437. + if(ext_parsers == NULL) ext_parsers = ext_default_parsers;
  51438. +
  51439. + pfkey_extensions_init(extensions);
  51440. +
  51441. + remain = pfkey_msg->sadb_msg_len;
  51442. + remain -= sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  51443. +
  51444. + pfkey_ext = (struct sadb_ext*)((char*)pfkey_msg +
  51445. + sizeof(struct sadb_msg));
  51446. +
  51447. + extensions[0] = (struct sadb_ext *) pfkey_msg;
  51448. +
  51449. +
  51450. + if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {
  51451. + ERROR("pfkey_msg_parse: "
  51452. + "not PF_KEY_V2 msg, found %d, should be %d.\n",
  51453. + pfkey_msg->sadb_msg_version,
  51454. + PF_KEY_V2);
  51455. + SENDERR(EINVAL);
  51456. + }
  51457. +
  51458. + if(!pfkey_msg->sadb_msg_type) {
  51459. + ERROR("pfkey_msg_parse: "
  51460. + "msg type not set, must be non-zero..\n");
  51461. + SENDERR(EINVAL);
  51462. + }
  51463. +
  51464. + if(pfkey_msg->sadb_msg_type > SADB_MAX) {
  51465. + ERROR("pfkey_msg_parse: "
  51466. + "msg type=%d > max=%d.\n",
  51467. + pfkey_msg->sadb_msg_type,
  51468. + SADB_MAX);
  51469. + SENDERR(EINVAL);
  51470. + }
  51471. +
  51472. + switch(pfkey_msg->sadb_msg_type) {
  51473. + case SADB_GETSPI:
  51474. + case SADB_UPDATE:
  51475. + case SADB_ADD:
  51476. + case SADB_DELETE:
  51477. + case SADB_GET:
  51478. + case SADB_X_GRPSA:
  51479. + case SADB_X_ADDFLOW:
  51480. + if(!satype2proto(pfkey_msg->sadb_msg_satype)) {
  51481. + ERROR("pfkey_msg_parse: "
  51482. + "satype %d conversion to proto failed for msg_type %d (%s).\n",
  51483. + pfkey_msg->sadb_msg_satype,
  51484. + pfkey_msg->sadb_msg_type,
  51485. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51486. + SENDERR(EINVAL);
  51487. + } else {
  51488. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51489. + "pfkey_msg_parse: "
  51490. + "satype %d(%s) conversion to proto gives %d for msg_type %d(%s).\n",
  51491. + pfkey_msg->sadb_msg_satype,
  51492. + satype2name(pfkey_msg->sadb_msg_satype),
  51493. + satype2proto(pfkey_msg->sadb_msg_satype),
  51494. + pfkey_msg->sadb_msg_type,
  51495. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51496. + }
  51497. + case SADB_ACQUIRE:
  51498. + case SADB_REGISTER:
  51499. + case SADB_EXPIRE:
  51500. + if(!pfkey_msg->sadb_msg_satype) {
  51501. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51502. + "pfkey_msg_parse: "
  51503. + "satype is zero, must be non-zero for msg_type %d(%s).\n",
  51504. + pfkey_msg->sadb_msg_type,
  51505. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51506. + SENDERR(EINVAL);
  51507. + }
  51508. + default:
  51509. + break;
  51510. + }
  51511. +
  51512. + /* errno must not be set in downward messages */
  51513. + /* this is not entirely true... a response to an ACQUIRE could return an error */
  51514. + if((dir == EXT_BITS_IN) && (pfkey_msg->sadb_msg_type != SADB_ACQUIRE) && pfkey_msg->sadb_msg_errno) {
  51515. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51516. + "pfkey_msg_parse: "
  51517. + "errno set to %d.\n",
  51518. + pfkey_msg->sadb_msg_errno);
  51519. + SENDERR(EINVAL);
  51520. + }
  51521. +
  51522. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51523. + "pfkey_msg_parse: "
  51524. + "remain=%d\n",
  51525. + remain
  51526. + );
  51527. +
  51528. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51529. + "pfkey_msg_parse: "
  51530. + "extensions permitted=%08x, required=%08x.\n",
  51531. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51532. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]);
  51533. +
  51534. + extensions_seen = 1;
  51535. +
  51536. + while( (remain * IPSEC_PFKEYv2_ALIGN) >= sizeof(struct sadb_ext) ) {
  51537. + /* Is there enough message left to support another extension header? */
  51538. + if(remain < pfkey_ext->sadb_ext_len) {
  51539. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51540. + "pfkey_msg_parse: "
  51541. + "remain %d less than ext len %d.\n",
  51542. + remain, pfkey_ext->sadb_ext_len);
  51543. + SENDERR(EINVAL);
  51544. + }
  51545. +
  51546. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51547. + "pfkey_msg_parse: "
  51548. + "parsing ext type=%d(%s) remain=%d.\n",
  51549. + pfkey_ext->sadb_ext_type,
  51550. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51551. + remain);
  51552. +
  51553. + /* Is the extension header type valid? */
  51554. + if((pfkey_ext->sadb_ext_type > SADB_EXT_MAX) || (!pfkey_ext->sadb_ext_type)) {
  51555. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51556. + "pfkey_msg_parse: "
  51557. + "ext type %d(%s) invalid, SADB_EXT_MAX=%d.\n",
  51558. + pfkey_ext->sadb_ext_type,
  51559. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51560. + SADB_EXT_MAX);
  51561. + SENDERR(EINVAL);
  51562. + }
  51563. +
  51564. + /* Have we already seen this type of extension? */
  51565. + if((extensions_seen & ( 1 << pfkey_ext->sadb_ext_type )) != 0)
  51566. + {
  51567. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51568. + "pfkey_msg_parse: "
  51569. + "ext type %d(%s) already seen.\n",
  51570. + pfkey_ext->sadb_ext_type,
  51571. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51572. + SENDERR(EINVAL);
  51573. + }
  51574. +
  51575. + /* Do I even know about this type of extension? */
  51576. + if(ext_parsers[pfkey_ext->sadb_ext_type]==NULL) {
  51577. + ERROR("pfkey_msg_parse: "
  51578. + "ext type %d(%s) unknown, ignoring.\n",
  51579. + pfkey_ext->sadb_ext_type,
  51580. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51581. + goto next_ext;
  51582. + }
  51583. +
  51584. + /* Is this type of extension permitted for this type of message? */
  51585. + if(!(extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type] &
  51586. + 1<<pfkey_ext->sadb_ext_type)) {
  51587. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51588. + "pfkey_msg_parse: "
  51589. + "ext type %d(%s) not permitted, exts_perm_in=%08x, 1<<type=%08x\n",
  51590. + pfkey_ext->sadb_ext_type,
  51591. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51592. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51593. + 1<<pfkey_ext->sadb_ext_type);
  51594. + SENDERR(EINVAL);
  51595. + }
  51596. +
  51597. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51598. + "pfkey_msg_parse: "
  51599. + "remain=%d ext_type=%d(%s) ext_len=%d parsing ext 0p%p with parser %s.\n",
  51600. + remain,
  51601. + pfkey_ext->sadb_ext_type,
  51602. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51603. + pfkey_ext->sadb_ext_len,
  51604. + pfkey_ext,
  51605. + ext_parsers[pfkey_ext->sadb_ext_type]->parser_name);
  51606. +
  51607. + /* Parse the extension */
  51608. + if((error =
  51609. + (*ext_parsers[pfkey_ext->sadb_ext_type]->parser)(pfkey_ext))) {
  51610. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51611. + "pfkey_msg_parse: "
  51612. + "extension parsing for type %d(%s) failed with error %d.\n",
  51613. + pfkey_ext->sadb_ext_type,
  51614. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51615. + error);
  51616. + SENDERR(-error);
  51617. + }
  51618. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51619. + "pfkey_msg_parse: "
  51620. + "Extension %d(%s) parsed.\n",
  51621. + pfkey_ext->sadb_ext_type,
  51622. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51623. +
  51624. + /* Mark that we have seen this extension and remember the header location */
  51625. + extensions_seen |= ( 1 << pfkey_ext->sadb_ext_type );
  51626. + extensions[pfkey_ext->sadb_ext_type] = pfkey_ext;
  51627. +
  51628. + next_ext:
  51629. + /* Calculate how much message remains */
  51630. + remain -= pfkey_ext->sadb_ext_len;
  51631. +
  51632. + if(!remain) {
  51633. + break;
  51634. + }
  51635. + /* Find the next extension header */
  51636. + pfkey_ext = (struct sadb_ext*)((char*)pfkey_ext +
  51637. + pfkey_ext->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  51638. + }
  51639. +
  51640. + if(remain) {
  51641. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51642. + "pfkey_msg_parse: "
  51643. + "unexpected remainder of %d.\n",
  51644. + remain);
  51645. + /* why is there still something remaining? */
  51646. + SENDERR(EINVAL);
  51647. + }
  51648. +
  51649. + /* check required extensions */
  51650. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51651. + "pfkey_msg_parse: "
  51652. + "extensions permitted=%08x, seen=%08x, required=%08x.\n",
  51653. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51654. + extensions_seen,
  51655. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]);
  51656. +
  51657. + /* don't check further if it is an error return message since it
  51658. + may not have a body */
  51659. + if(pfkey_msg->sadb_msg_errno) {
  51660. + SENDERR(-error);
  51661. + }
  51662. +
  51663. + if((extensions_seen &
  51664. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]) !=
  51665. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]) {
  51666. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51667. + "pfkey_msg_parse: "
  51668. + "required extensions missing:%08x.\n",
  51669. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type] -
  51670. + (extensions_seen &
  51671. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]));
  51672. + SENDERR(EINVAL);
  51673. + }
  51674. +
  51675. + if((dir == EXT_BITS_IN) && (pfkey_msg->sadb_msg_type == SADB_X_DELFLOW)
  51676. + && ((extensions_seen & SADB_X_EXT_ADDRESS_DELFLOW)
  51677. + != SADB_X_EXT_ADDRESS_DELFLOW)
  51678. + && (((extensions_seen & (1<<SADB_EXT_SA)) != (1<<SADB_EXT_SA))
  51679. + || ((((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_flags
  51680. + & SADB_X_SAFLAGS_CLEARFLOW)
  51681. + != SADB_X_SAFLAGS_CLEARFLOW))) {
  51682. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51683. + "pfkey_msg_parse: "
  51684. + "required SADB_X_DELFLOW extensions missing: either %08x must be present or %08x must be present with SADB_X_SAFLAGS_CLEARFLOW set.\n",
  51685. + SADB_X_EXT_ADDRESS_DELFLOW
  51686. + - (extensions_seen & SADB_X_EXT_ADDRESS_DELFLOW),
  51687. + (1<<SADB_EXT_SA) - (extensions_seen & (1<<SADB_EXT_SA)));
  51688. + SENDERR(EINVAL);
  51689. + }
  51690. +
  51691. + switch(pfkey_msg->sadb_msg_type) {
  51692. + case SADB_ADD:
  51693. + case SADB_UPDATE:
  51694. + /* check maturity */
  51695. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state !=
  51696. + SADB_SASTATE_MATURE) {
  51697. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51698. + "pfkey_msg_parse: "
  51699. + "state=%d for add or update should be MATURE=%d.\n",
  51700. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  51701. + SADB_SASTATE_MATURE);
  51702. + SENDERR(EINVAL);
  51703. + }
  51704. +
  51705. + /* check AH and ESP */
  51706. + switch(((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype) {
  51707. + case SADB_SATYPE_AH:
  51708. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51709. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_auth !=
  51710. + SADB_AALG_NONE)) {
  51711. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51712. + "pfkey_msg_parse: "
  51713. + "auth alg is zero, must be non-zero for AH SAs.\n");
  51714. + SENDERR(EINVAL);
  51715. + }
  51716. + if(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt !=
  51717. + SADB_EALG_NONE) {
  51718. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51719. + "pfkey_msg_parse: "
  51720. + "AH handed encalg=%d, must be zero.\n",
  51721. + ((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt);
  51722. + SENDERR(EINVAL);
  51723. + }
  51724. + break;
  51725. + case SADB_SATYPE_ESP:
  51726. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51727. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt !=
  51728. + SADB_EALG_NONE)) {
  51729. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51730. + "pfkey_msg_parse: "
  51731. + "encrypt alg=%d is zero, must be non-zero for ESP=%d SAs.\n",
  51732. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt,
  51733. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  51734. + SENDERR(EINVAL);
  51735. + }
  51736. + if((((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt ==
  51737. + SADB_EALG_NULL) &&
  51738. + (((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth ==
  51739. + SADB_AALG_NONE) ) {
  51740. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51741. + "pfkey_msg_parse: "
  51742. + "ESP handed encNULL+authNONE, illegal combination.\n");
  51743. + SENDERR(EINVAL);
  51744. + }
  51745. + break;
  51746. + case SADB_X_SATYPE_COMP:
  51747. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51748. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt !=
  51749. + SADB_EALG_NONE)) {
  51750. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51751. + "pfkey_msg_parse: "
  51752. + "encrypt alg=%d is zero, must be non-zero for COMP=%d SAs.\n",
  51753. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt,
  51754. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  51755. + SENDERR(EINVAL);
  51756. + }
  51757. + if(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth !=
  51758. + SADB_AALG_NONE) {
  51759. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51760. + "pfkey_msg_parse: "
  51761. + "COMP handed auth=%d, must be zero.\n",
  51762. + ((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth);
  51763. + SENDERR(EINVAL);
  51764. + }
  51765. + break;
  51766. + default:
  51767. + break;
  51768. + }
  51769. + if(ntohl(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_spi) <= 255) {
  51770. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51771. + "pfkey_msg_parse: "
  51772. + "spi=%08x must be > 255.\n",
  51773. + ntohl(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_spi));
  51774. + SENDERR(EINVAL);
  51775. + }
  51776. + default:
  51777. + break;
  51778. + }
  51779. +errlab:
  51780. +
  51781. + return error;
  51782. +}
  51783. +
  51784. +/*
  51785. + * $Log: pfkey_v2_parse.c,v $
  51786. + * Revision 1.65 2005/04/06 17:46:05 mcr
  51787. + * failure to recognize an extension is considered an error.
  51788. + * This could be a problem in the future, but we need some kind
  51789. + * of logging. This should be rate limited, probably.
  51790. + *
  51791. + * Revision 1.64 2005/01/26 00:50:35 mcr
  51792. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  51793. + * and make sure that NAT_TRAVERSAL is set as well to match
  51794. + * userspace compiles of code.
  51795. + *
  51796. + * Revision 1.63 2004/10/28 22:54:10 mcr
  51797. + * results from valgrind, thanks to: Harald Hoyer <harald@redhat.com>
  51798. + *
  51799. + * Revision 1.62 2004/10/03 01:26:36 mcr
  51800. + * fixes for gcc 3.4 compilation.
  51801. + *
  51802. + * Revision 1.61 2004/07/10 19:11:18 mcr
  51803. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  51804. + *
  51805. + * Revision 1.59 2004/04/18 03:03:49 mcr
  51806. + * renamed common include files from pluto directory.
  51807. + *
  51808. + * Revision 1.58 2004/03/08 01:59:08 ken
  51809. + * freeswan.h -> openswan.h
  51810. + *
  51811. + * Revision 1.57 2003/12/10 01:20:19 mcr
  51812. + * NAT-traversal patches to KLIPS.
  51813. + *
  51814. + * Revision 1.56 2003/12/04 23:01:12 mcr
  51815. + * removed ipsec_netlink.h
  51816. + *
  51817. + * Revision 1.55 2003/11/07 01:30:37 ken
  51818. + * Cast sizeof() to int to keep things 64bit clean
  51819. + *
  51820. + * Revision 1.54 2003/10/31 02:27:12 mcr
  51821. + * pulled up port-selector patches and sa_id elimination.
  51822. + *
  51823. + * Revision 1.53.20.2 2003/10/29 01:11:32 mcr
  51824. + * added debugging for pfkey library.
  51825. + *
  51826. + * Revision 1.53.20.1 2003/09/21 13:59:44 mcr
  51827. + * pre-liminary X.509 patch - does not yet pass tests.
  51828. + *
  51829. + * Revision 1.53 2003/01/30 02:32:09 rgb
  51830. + *
  51831. + * Rename SAref table macro names for clarity.
  51832. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  51833. + *
  51834. + * Revision 1.52 2002/12/30 06:53:07 mcr
  51835. + * deal with short SA structures... #if 0 out for now. Probably
  51836. + * not quite the right way.
  51837. + *
  51838. + * Revision 1.51 2002/12/13 18:16:02 mcr
  51839. + * restored sa_ref code
  51840. + *
  51841. + * Revision 1.50 2002/12/13 18:06:52 mcr
  51842. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  51843. + *
  51844. + * Revision 1.49 2002/10/05 05:02:58 dhr
  51845. + *
  51846. + * C labels go on statements
  51847. + *
  51848. + * Revision 1.48 2002/09/20 15:40:45 rgb
  51849. + * Added sadb_x_sa_ref to struct sadb_sa.
  51850. + *
  51851. + * Revision 1.47 2002/09/20 05:01:31 rgb
  51852. + * Fixed usage of pfkey_lib_debug.
  51853. + * Format for function declaration style consistency.
  51854. + * Added text labels to elucidate numeric values presented.
  51855. + * Re-organised debug output to reduce noise in output.
  51856. + *
  51857. + * Revision 1.46 2002/07/24 18:44:54 rgb
  51858. + * Type fiddling to tame ia64 compiler.
  51859. + *
  51860. + * Revision 1.45 2002/05/23 07:14:11 rgb
  51861. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  51862. + *
  51863. + * Revision 1.44 2002/04/24 07:55:32 mcr
  51864. + * #include patches and Makefiles for post-reorg compilation.
  51865. + *
  51866. + * Revision 1.43 2002/04/24 07:36:40 mcr
  51867. + * Moved from ./lib/pfkey_v2_parse.c,v
  51868. + *
  51869. + * Revision 1.42 2002/01/29 22:25:36 rgb
  51870. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  51871. + *
  51872. + * Revision 1.41 2002/01/29 01:59:10 mcr
  51873. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  51874. + * updating of IPv6 structures to match latest in6.h version.
  51875. + * removed dead code from openswan.h that also duplicated kversions.h
  51876. + * code.
  51877. + *
  51878. + * Revision 1.40 2002/01/20 20:34:50 mcr
  51879. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  51880. + *
  51881. + * Revision 1.39 2001/11/27 05:29:22 mcr
  51882. + * pfkey parses are now maintained by a structure
  51883. + * that includes their name for debug purposes.
  51884. + * DEBUGGING() macro changed so that it takes a debug
  51885. + * level so that pf_key() can use this to decode the
  51886. + * structures without innundanting humans.
  51887. + * Also uses pfkey_v2_sadb_ext_string() in messages.
  51888. + *
  51889. + * Revision 1.38 2001/11/06 19:47:47 rgb
  51890. + * Added packet parameter to lifetime and comb structures.
  51891. + *
  51892. + * Revision 1.37 2001/10/18 04:45:24 rgb
  51893. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  51894. + * lib/openswan.h version macros moved to lib/kversions.h.
  51895. + * Other compiler directive cleanups.
  51896. + *
  51897. + * Revision 1.36 2001/06/14 19:35:16 rgb
  51898. + * Update copyright date.
  51899. + *
  51900. + * Revision 1.35 2001/05/03 19:44:51 rgb
  51901. + * Standardise on SENDERR() macro.
  51902. + *
  51903. + * Revision 1.34 2001/03/16 07:41:51 rgb
  51904. + * Put openswan.h include before pluto includes.
  51905. + *
  51906. + * Revision 1.33 2001/02/27 07:13:51 rgb
  51907. + * Added satype2name() function.
  51908. + * Added text to default satype_tbl entry.
  51909. + * Added satype2name() conversions for most satype debug output.
  51910. + *
  51911. + * Revision 1.32 2001/02/26 20:01:09 rgb
  51912. + * Added internal IP protocol 61 for magic SAs.
  51913. + * Ditch unused sadb_satype2proto[], replaced by satype2proto().
  51914. + * Re-formatted debug output (split lines, consistent spacing).
  51915. + * Removed acquire, register and expire requirements for a known satype.
  51916. + * Changed message type checking to a switch structure.
  51917. + * Verify expected NULL auth for IPCOMP.
  51918. + * Enforced spi > 0x100 requirement, now that pass uses a magic SA for
  51919. + * appropriate message types.
  51920. + *
  51921. + * Revision 1.31 2000/12/01 07:09:00 rgb
  51922. + * Added ipcomp sanity check to require encalgo is set.
  51923. + *
  51924. + * Revision 1.30 2000/11/17 18:10:30 rgb
  51925. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  51926. + * network byte order since this is the way PF_KEYv2 stored spis.
  51927. + *
  51928. + * Revision 1.29 2000/10/12 00:02:39 rgb
  51929. + * Removed 'format, ##' nonsense from debug macros for RH7.0.
  51930. + *
  51931. + * Revision 1.28 2000/09/20 16:23:04 rgb
  51932. + * Remove over-paranoid extension check in the presence of sadb_msg_errno.
  51933. + *
  51934. + * Revision 1.27 2000/09/20 04:04:21 rgb
  51935. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  51936. + * oopsen.
  51937. + *
  51938. + * Revision 1.26 2000/09/15 11:37:02 rgb
  51939. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  51940. + * IPCOMP zlib deflate code.
  51941. + *
  51942. + * Revision 1.25 2000/09/12 22:35:37 rgb
  51943. + * Restructured to remove unused extensions from CLEARFLOW messages.
  51944. + *
  51945. + * Revision 1.24 2000/09/12 18:59:54 rgb
  51946. + * Added Gerhard's IPv6 support to pfkey parts of libopenswan.
  51947. + *
  51948. + * Revision 1.23 2000/09/12 03:27:00 rgb
  51949. + * Moved DEBUGGING definition to compile kernel with debug off.
  51950. + *
  51951. + * Revision 1.22 2000/09/09 06:39:27 rgb
  51952. + * Restrict pfkey errno check to downward messages only.
  51953. + *
  51954. + * Revision 1.21 2000/09/08 19:22:34 rgb
  51955. + * Enabled pfkey_sens_parse().
  51956. + * Added check for errno on downward acquire messages only.
  51957. + *
  51958. + * Revision 1.20 2000/09/01 18:48:23 rgb
  51959. + * Fixed reserved check bug and added debug output in
  51960. + * pfkey_supported_parse().
  51961. + * Fixed debug output label bug in pfkey_ident_parse().
  51962. + *
  51963. + * Revision 1.19 2000/08/27 01:55:26 rgb
  51964. + * Define OCTETBITS and PFKEYBITS to avoid using 'magic' numbers in code.
  51965. + *
  51966. + * Revision 1.18 2000/08/24 17:00:36 rgb
  51967. + * Ignore unknown extensions instead of failing.
  51968. + *
  51969. + * Revision 1.17 2000/06/02 22:54:14 rgb
  51970. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  51971. + *
  51972. + * Revision 1.16 2000/05/10 19:25:11 rgb
  51973. + * Fleshed out proposal and supported extensions.
  51974. + *
  51975. + * Revision 1.15 2000/01/24 21:15:31 rgb
  51976. + * Added disabled pluto pfkey lib debug flag.
  51977. + * Added algo debugging reporting.
  51978. + *
  51979. + * Revision 1.14 2000/01/22 23:24:29 rgb
  51980. + * Added new functions proto2satype() and satype2proto() and lookup
  51981. + * table satype_tbl. Also added proto2name() since it was easy.
  51982. + *
  51983. + * Revision 1.13 2000/01/21 09:43:59 rgb
  51984. + * Cast ntohl(spi) as (unsigned long int) to shut up compiler.
  51985. + *
  51986. + * Revision 1.12 2000/01/21 06:28:19 rgb
  51987. + * Added address cases for eroute flows.
  51988. + * Indented compiler directives for readability.
  51989. + * Added klipsdebug switching capability.
  51990. + *
  51991. + * Revision 1.11 1999/12/29 21:14:59 rgb
  51992. + * Fixed debug text cut and paste typo.
  51993. + *
  51994. + * Revision 1.10 1999/12/10 17:45:24 rgb
  51995. + * Added address debugging.
  51996. + *
  51997. + * Revision 1.9 1999/12/09 23:11:42 rgb
  51998. + * Ditched <string.h> include since we no longer use memset().
  51999. + * Use new pfkey_extensions_init() instead of memset().
  52000. + * Added check for SATYPE in pfkey_msg_build().
  52001. + * Tidy up comments and debugging comments.
  52002. + *
  52003. + * Revision 1.8 1999/12/07 19:55:26 rgb
  52004. + * Removed unused first argument from extension parsers.
  52005. + * Removed static pluto debug flag.
  52006. + * Moved message type and state checking to pfkey_msg_parse().
  52007. + * Changed print[fk] type from lx to x to quiet compiler.
  52008. + * Removed redundant remain check.
  52009. + * Changed __u* types to uint* to avoid use of asm/types.h and
  52010. + * sys/types.h in userspace code.
  52011. + *
  52012. + * Revision 1.7 1999/12/01 22:20:51 rgb
  52013. + * Moved pfkey_lib_debug variable into the library.
  52014. + * Added pfkey version check into header parsing.
  52015. + * Added check for SATYPE only for those extensions that require a
  52016. + * non-zero value.
  52017. + *
  52018. + * Revision 1.6 1999/11/27 11:58:05 rgb
  52019. + * Added ipv6 headers.
  52020. + * Moved sadb_satype2proto protocol lookup table from
  52021. + * klips/net/ipsec/pfkey_v2_parser.c.
  52022. + * Enable lifetime_current checking.
  52023. + * Debugging error messages added.
  52024. + * Add argument to pfkey_msg_parse() for direction.
  52025. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  52026. + * Add CVS log entry to bottom of file.
  52027. + * Moved auth and enc alg check to pfkey_msg_parse().
  52028. + * Enable accidentally disabled spirange parsing.
  52029. + * Moved protocol/algorithm checks from klips/net/ipsec/pfkey_v2_parser.c
  52030. + *
  52031. + * Local variables:
  52032. + * c-file-style: "linux"
  52033. + * End:
  52034. + *
  52035. + */
  52036. --- /dev/null Tue Mar 11 13:02:56 2003
  52037. +++ linux/net/ipsec/pfkey_v2_parser.c Mon Feb 9 13:51:03 2004
  52038. @@ -0,0 +1,3520 @@
  52039. +/*
  52040. + * @(#) RFC2367 PF_KEYv2 Key management API message parser
  52041. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  52042. + *
  52043. + * This program is free software; you can redistribute it and/or modify it
  52044. + * under the terms of the GNU General Public License as published by the
  52045. + * Free Software Foundation; either version 2 of the License, or (at your
  52046. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  52047. + *
  52048. + * This program is distributed in the hope that it will be useful, but
  52049. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  52050. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  52051. + * for more details.
  52052. + *
  52053. + * RCSID $Id: pfkey_v2_parser.c,v 1.134.2.2 2006/10/06 21:39:26 paul Exp $
  52054. + */
  52055. +
  52056. +/*
  52057. + * Template from klips/net/ipsec/ipsec/ipsec_netlink.c.
  52058. + */
  52059. +
  52060. +char pfkey_v2_parser_c_version[] = "$Id: pfkey_v2_parser.c,v 1.134.2.2 2006/10/06 21:39:26 paul Exp $";
  52061. +
  52062. +#ifndef AUTOCONF_INCLUDED
  52063. +#include <linux/config.h>
  52064. +#endif
  52065. +#include <linux/version.h>
  52066. +#include <linux/kernel.h> /* printk() */
  52067. +
  52068. +#include "openswan/ipsec_param.h"
  52069. +
  52070. +#ifdef MALLOC_SLAB
  52071. +# include <linux/slab.h> /* kmalloc() */
  52072. +#else /* MALLOC_SLAB */
  52073. +# include <linux/malloc.h> /* kmalloc() */
  52074. +#endif /* MALLOC_SLAB */
  52075. +#include <linux/errno.h> /* error codes */
  52076. +#include <linux/types.h> /* size_t */
  52077. +#include <linux/interrupt.h> /* mark_bh */
  52078. +
  52079. +#include <linux/netdevice.h> /* struct device, and other headers */
  52080. +#include <linux/etherdevice.h> /* eth_type_trans */
  52081. +#include <linux/ip.h> /* struct iphdr */
  52082. +#include <linux/skbuff.h>
  52083. +
  52084. +#include <openswan.h>
  52085. +
  52086. +#include <crypto/des.h>
  52087. +
  52088. +#ifdef SPINLOCK
  52089. +# ifdef SPINLOCK_23
  52090. +# include <linux/spinlock.h> /* *lock* */
  52091. +# else /* SPINLOCK_23 */
  52092. +# include <asm/spinlock.h> /* *lock* */
  52093. +# endif /* SPINLOCK_23 */
  52094. +#endif /* SPINLOCK */
  52095. +
  52096. +#include <linux/in6.h>
  52097. +#include <net/route.h>
  52098. +
  52099. +#include <net/ip.h>
  52100. +#ifdef NETLINK_SOCK
  52101. +# include <linux/netlink.h>
  52102. +#else
  52103. +# include <net/netlink.h>
  52104. +#endif
  52105. +
  52106. +#include <linux/random.h> /* get_random_bytes() */
  52107. +
  52108. +#include "openswan/radij.h"
  52109. +#include "openswan/ipsec_encap.h"
  52110. +#include "openswan/ipsec_sa.h"
  52111. +
  52112. +#include "openswan/ipsec_radij.h"
  52113. +#include "openswan/ipsec_xform.h"
  52114. +#include "openswan/ipsec_ah.h"
  52115. +#include "openswan/ipsec_esp.h"
  52116. +#include "openswan/ipsec_tunnel.h"
  52117. +#include "openswan/ipsec_rcv.h"
  52118. +#include "openswan/ipcomp.h"
  52119. +
  52120. +#include <pfkeyv2.h>
  52121. +#include <pfkey.h>
  52122. +
  52123. +#include "openswan/ipsec_proto.h"
  52124. +#include "openswan/ipsec_alg.h"
  52125. +
  52126. +#include "openswan/ipsec_kern24.h"
  52127. +
  52128. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  52129. +
  52130. +struct sklist_t {
  52131. + struct socket *sk;
  52132. + struct sklist_t* next;
  52133. +} pfkey_sklist_head, *pfkey_sklist, *pfkey_sklist_prev;
  52134. +
  52135. +__u32 pfkey_msg_seq = 0;
  52136. +
  52137. +
  52138. +#if 0
  52139. +#define DUMP_SAID dump_said(&extr->ips->ips_said, __LINE__)
  52140. +#define DUMP_SAID2 dump_said(&extr.ips->ips_said, __LINE__)
  52141. +static void dump_said(ip_said *s, int line)
  52142. +{
  52143. + char msa[SATOT_BUF];
  52144. + size_t msa_len;
  52145. +
  52146. + msa_len = satot(s, 0, msa, sizeof(msa));
  52147. +
  52148. + printk("line: %d msa: %s\n", line, msa);
  52149. +}
  52150. +#endif
  52151. +
  52152. +
  52153. +int
  52154. +pfkey_alloc_eroute(struct eroute** eroute)
  52155. +{
  52156. + int error = 0;
  52157. + if(*eroute) {
  52158. + KLIPS_PRINT(debug_pfkey,
  52159. + "klips_debug:pfkey_alloc_eroute: "
  52160. + "eroute struct already allocated\n");
  52161. + SENDERR(EEXIST);
  52162. + }
  52163. +
  52164. + if((*eroute = kmalloc(sizeof(**eroute), GFP_ATOMIC) ) == NULL) {
  52165. + KLIPS_PRINT(debug_pfkey,
  52166. + "klips_debug:pfkey_alloc_eroute: "
  52167. + "memory allocation error\n");
  52168. + SENDERR(ENOMEM);
  52169. + }
  52170. +
  52171. + KLIPS_PRINT(debug_pfkey,
  52172. + "klips_debug:pfkey_alloc_eroute: "
  52173. + "allocating %lu bytes for an eroute at 0p%p\n",
  52174. + (unsigned long) sizeof(**eroute), *eroute);
  52175. +
  52176. + memset((caddr_t)*eroute, 0, sizeof(**eroute));
  52177. + (*eroute)->er_eaddr.sen_len =
  52178. + (*eroute)->er_emask.sen_len = sizeof(struct sockaddr_encap);
  52179. + (*eroute)->er_eaddr.sen_family =
  52180. + (*eroute)->er_emask.sen_family = AF_ENCAP;
  52181. + (*eroute)->er_eaddr.sen_type = SENT_IP4;
  52182. + (*eroute)->er_emask.sen_type = 255;
  52183. + (*eroute)->er_pid = 0;
  52184. + (*eroute)->er_count = 0;
  52185. + (*eroute)->er_lasttime = jiffies/HZ;
  52186. +
  52187. + errlab:
  52188. + return(error);
  52189. +}
  52190. +
  52191. +DEBUG_NO_STATIC int
  52192. +pfkey_x_protocol_process(struct sadb_ext *pfkey_ext,
  52193. + struct pfkey_extracted_data *extr)
  52194. +{
  52195. + int error = 0;
  52196. + struct sadb_protocol * p = (struct sadb_protocol *)pfkey_ext;
  52197. +
  52198. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_protocol_process: %p\n", extr);
  52199. +
  52200. + if (extr == 0) {
  52201. + KLIPS_PRINT(debug_pfkey,
  52202. + "klips_debug:pfkey_x_protocol_process:"
  52203. + "extr is NULL, fatal\n");
  52204. + SENDERR(EINVAL);
  52205. + }
  52206. + if (extr->eroute == 0) {
  52207. + KLIPS_PRINT(debug_pfkey,
  52208. + "klips_debug:pfkey_x_protocol_process:"
  52209. + "extr->eroute is NULL, fatal\n");
  52210. + SENDERR(EINVAL);
  52211. + }
  52212. +
  52213. + extr->eroute->er_eaddr.sen_proto = p->sadb_protocol_proto;
  52214. + extr->eroute->er_emask.sen_proto = p->sadb_protocol_proto ? ~0:0;
  52215. + KLIPS_PRINT(debug_pfkey,
  52216. + "klips_debug:pfkey_x_protocol_process: protocol = %d.\n",
  52217. + p->sadb_protocol_proto);
  52218. + errlab:
  52219. + return error;
  52220. +}
  52221. +
  52222. +DEBUG_NO_STATIC int
  52223. +pfkey_ipsec_sa_init(struct ipsec_sa *ipsp)
  52224. +{
  52225. +
  52226. + return ipsec_sa_init(ipsp);
  52227. +}
  52228. +
  52229. +int
  52230. +pfkey_safe_build(int error, struct sadb_ext *extensions[SADB_MAX+1])
  52231. +{
  52232. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build: "
  52233. + "error=%d\n",
  52234. + error);
  52235. + if (!error) {
  52236. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build:"
  52237. + "success.\n");
  52238. + return 1;
  52239. + } else {
  52240. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build:"
  52241. + "caught error %d\n",
  52242. + error);
  52243. + pfkey_extensions_free(extensions);
  52244. + return 0;
  52245. + }
  52246. +}
  52247. +
  52248. +
  52249. +DEBUG_NO_STATIC int
  52250. +pfkey_getspi_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52251. +{
  52252. + int error = 0;
  52253. + ipsec_spi_t minspi = htonl(256), maxspi = htonl(-1L);
  52254. + int found_avail = 0;
  52255. + struct ipsec_sa *ipsq;
  52256. + char sa[SATOT_BUF];
  52257. + size_t sa_len;
  52258. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52259. + struct sadb_msg *pfkey_reply = NULL;
  52260. + struct socket_list *pfkey_socketsp;
  52261. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52262. +
  52263. + KLIPS_PRINT(debug_pfkey,
  52264. + "klips_debug:pfkey_getspi_parse: .\n");
  52265. +
  52266. + pfkey_extensions_init(extensions_reply);
  52267. +
  52268. + if(extr == NULL || extr->ips == NULL) {
  52269. + KLIPS_PRINT(debug_pfkey,
  52270. + "klips_debug:pfkey_getspi_parse: "
  52271. + "error, extr or extr->ipsec_sa pointer NULL\n");
  52272. + SENDERR(EINVAL);
  52273. + }
  52274. +
  52275. + if(extensions[SADB_EXT_SPIRANGE]) {
  52276. + minspi = ((struct sadb_spirange *)extensions[SADB_EXT_SPIRANGE])->sadb_spirange_min;
  52277. + maxspi = ((struct sadb_spirange *)extensions[SADB_EXT_SPIRANGE])->sadb_spirange_max;
  52278. + }
  52279. +
  52280. + if(maxspi == minspi) {
  52281. + extr->ips->ips_said.spi = maxspi;
  52282. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52283. + if(ipsq != NULL) {
  52284. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52285. + ipsec_sa_put(ipsq);
  52286. + KLIPS_PRINT(debug_pfkey,
  52287. + "klips_debug:pfkey_getspi_parse: "
  52288. + "EMT_GETSPI found an old ipsec_sa for SA: %s, delete it first.\n",
  52289. + sa_len ? sa : " (error)");
  52290. + SENDERR(EEXIST);
  52291. + } else {
  52292. + found_avail = 1;
  52293. + }
  52294. + } else {
  52295. + int i = 0;
  52296. + __u32 rand_val;
  52297. + __u32 spi_diff;
  52298. + while( ( i < (spi_diff = (ntohl(maxspi) - ntohl(minspi)))) && !found_avail ) {
  52299. + prng_bytes(&ipsec_prng, (char *) &(rand_val),
  52300. + ( (spi_diff < (2^8)) ? 1 :
  52301. + ( (spi_diff < (2^16)) ? 2 :
  52302. + ( (spi_diff < (2^24)) ? 3 :
  52303. + 4 ) ) ) );
  52304. + extr->ips->ips_said.spi = htonl(ntohl(minspi) +
  52305. + (rand_val %
  52306. + (spi_diff + 1)));
  52307. + i++;
  52308. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52309. + if(ipsq == NULL) {
  52310. + found_avail = 1;
  52311. + } else {
  52312. + ipsec_sa_put(ipsq);
  52313. + }
  52314. + }
  52315. + }
  52316. +
  52317. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52318. +
  52319. + if (!found_avail) {
  52320. + KLIPS_PRINT(debug_pfkey,
  52321. + "klips_debug:pfkey_getspi_parse: "
  52322. + "found an old ipsec_sa for SA: %s, delete it first.\n",
  52323. + sa_len ? sa : " (error)");
  52324. + SENDERR(EEXIST);
  52325. + }
  52326. +
  52327. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52328. + extr->ips->ips_flags |= EMT_INBOUND;
  52329. + }
  52330. +
  52331. + KLIPS_PRINT(debug_pfkey,
  52332. + "klips_debug:pfkey_getspi_parse: "
  52333. + "existing ipsec_sa not found (this is good) for SA: %s, %s-bound, allocating.\n",
  52334. + sa_len ? sa : " (error)",
  52335. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52336. +
  52337. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52338. + extr->ips->ips_rcvif = NULL;
  52339. + extr->ips->ips_life.ipl_addtime.ipl_count = jiffies/HZ;
  52340. +
  52341. + extr->ips->ips_state = SADB_SASTATE_LARVAL;
  52342. +
  52343. + if(!extr->ips->ips_life.ipl_allocations.ipl_count) {
  52344. + extr->ips->ips_life.ipl_allocations.ipl_count += 1;
  52345. + }
  52346. +
  52347. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52348. + SADB_GETSPI,
  52349. + satype,
  52350. + 0,
  52351. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52352. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52353. + extensions_reply)
  52354. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52355. + SADB_EXT_SA,
  52356. + extr->ips->ips_said.spi,
  52357. + 0,
  52358. + SADB_SASTATE_LARVAL,
  52359. + 0,
  52360. + 0,
  52361. + 0,
  52362. + extr->ips->ips_ref),
  52363. + extensions_reply)
  52364. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52365. + SADB_EXT_ADDRESS_SRC,
  52366. + 0, /*extr->ips->ips_said.proto,*/
  52367. + 0,
  52368. + extr->ips->ips_addr_s),
  52369. + extensions_reply)
  52370. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52371. + SADB_EXT_ADDRESS_DST,
  52372. + 0, /*extr->ips->ips_said.proto,*/
  52373. + 0,
  52374. + extr->ips->ips_addr_d),
  52375. + extensions_reply) )) {
  52376. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52377. + "failed to build the getspi reply message extensions\n");
  52378. + goto errlab;
  52379. + }
  52380. +
  52381. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52382. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52383. + "failed to build the getspi reply message\n");
  52384. + SENDERR(-error);
  52385. + }
  52386. + for(pfkey_socketsp = pfkey_open_sockets;
  52387. + pfkey_socketsp;
  52388. + pfkey_socketsp = pfkey_socketsp->next) {
  52389. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52390. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52391. + "sending up getspi reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52392. + satype,
  52393. + satype2name(satype),
  52394. + pfkey_socketsp->socketp,
  52395. + error);
  52396. + SENDERR(-error);
  52397. + }
  52398. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52399. + "sending up getspi reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52400. + satype,
  52401. + satype2name(satype),
  52402. + pfkey_socketsp->socketp);
  52403. + }
  52404. +
  52405. + if((error = ipsec_sa_add(extr->ips))) {
  52406. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52407. + "failed to add the larval SA=%s with error=%d.\n",
  52408. + sa_len ? sa : " (error)",
  52409. + error);
  52410. + SENDERR(-error);
  52411. + }
  52412. + extr->ips = NULL;
  52413. +
  52414. + KLIPS_PRINT(debug_pfkey,
  52415. + "klips_debug:pfkey_getspi_parse: "
  52416. + "successful for SA: %s\n",
  52417. + sa_len ? sa : " (error)");
  52418. +
  52419. + errlab:
  52420. + if (pfkey_reply) {
  52421. + pfkey_msg_free(&pfkey_reply);
  52422. + }
  52423. + pfkey_extensions_free(extensions_reply);
  52424. + return error;
  52425. +}
  52426. +
  52427. +DEBUG_NO_STATIC int
  52428. +pfkey_update_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52429. +{
  52430. + int error = 0;
  52431. + struct ipsec_sa* ipsq;
  52432. + char sa[SATOT_BUF];
  52433. + size_t sa_len;
  52434. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52435. + struct sadb_msg *pfkey_reply = NULL;
  52436. + struct socket_list *pfkey_socketsp;
  52437. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52438. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52439. + struct ipsec_sa *nat_t_ips_saved = NULL;
  52440. +#endif
  52441. + KLIPS_PRINT(debug_pfkey,
  52442. + "klips_debug:pfkey_update_parse: .\n");
  52443. +
  52444. + pfkey_extensions_init(extensions_reply);
  52445. +
  52446. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state != SADB_SASTATE_MATURE) {
  52447. + KLIPS_PRINT(debug_pfkey,
  52448. + "klips_debug:pfkey_update_parse: "
  52449. + "error, sa_state=%d must be MATURE=%d\n",
  52450. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  52451. + SADB_SASTATE_MATURE);
  52452. + SENDERR(EINVAL);
  52453. + }
  52454. +
  52455. + if(extr == NULL || extr->ips == NULL) {
  52456. + KLIPS_PRINT(debug_pfkey,
  52457. + "klips_debug:pfkey_update_parse: "
  52458. + "error, extr or extr->ips pointer NULL\n");
  52459. + SENDERR(EINVAL);
  52460. + }
  52461. +
  52462. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52463. +
  52464. + spin_lock_bh(&tdb_lock);
  52465. +
  52466. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52467. + if (ipsq == NULL) {
  52468. + spin_unlock_bh(&tdb_lock);
  52469. + KLIPS_PRINT(debug_pfkey,
  52470. + "klips_debug:pfkey_update_parse: "
  52471. + "reserved ipsec_sa for SA: %s not found. Call SADB_GETSPI first or call SADB_ADD instead.\n",
  52472. + sa_len ? sa : " (error)");
  52473. + SENDERR(ENOENT);
  52474. + }
  52475. +
  52476. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52477. + extr->ips->ips_flags |= EMT_INBOUND;
  52478. + }
  52479. +
  52480. + KLIPS_PRINT(debug_pfkey,
  52481. + "klips_debug:pfkey_update_parse: "
  52482. + "existing ipsec_sa found (this is good) for SA: %s, %s-bound, updating.\n",
  52483. + sa_len ? sa : " (error)",
  52484. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52485. +
  52486. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52487. + if (extr->ips->ips_natt_sport || extr->ips->ips_natt_dport) {
  52488. + KLIPS_PRINT(debug_pfkey,
  52489. + "klips_debug:pfkey_update_parse: only updating NAT-T ports "
  52490. + "(%u:%u -> %u:%u)\n",
  52491. + ipsq->ips_natt_sport, ipsq->ips_natt_dport,
  52492. + extr->ips->ips_natt_sport, extr->ips->ips_natt_dport);
  52493. +
  52494. + if (extr->ips->ips_natt_sport) {
  52495. + ipsq->ips_natt_sport = extr->ips->ips_natt_sport;
  52496. + if (ipsq->ips_addr_s->sa_family == AF_INET) {
  52497. + ((struct sockaddr_in *)(ipsq->ips_addr_s))->sin_port = htons(extr->ips->ips_natt_sport);
  52498. + }
  52499. + }
  52500. +
  52501. + if (extr->ips->ips_natt_dport) {
  52502. + ipsq->ips_natt_dport = extr->ips->ips_natt_dport;
  52503. + if (ipsq->ips_addr_d->sa_family == AF_INET) {
  52504. + ((struct sockaddr_in *)(ipsq->ips_addr_d))->sin_port = htons(extr->ips->ips_natt_dport);
  52505. + }
  52506. + }
  52507. +
  52508. + nat_t_ips_saved = extr->ips;
  52509. + extr->ips = ipsq;
  52510. + }
  52511. + else {
  52512. +#endif
  52513. +
  52514. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52515. + extr->ips->ips_rcvif = NULL;
  52516. + if ((error = pfkey_ipsec_sa_init(extr->ips))) {
  52517. + ipsec_sa_put(ipsq);
  52518. + spin_unlock_bh(&tdb_lock);
  52519. + KLIPS_PRINT(debug_pfkey,
  52520. + "klips_debug:pfkey_update_parse: "
  52521. + "not successful for SA: %s, deleting.\n",
  52522. + sa_len ? sa : " (error)");
  52523. + SENDERR(-error);
  52524. + }
  52525. +
  52526. + extr->ips->ips_life.ipl_addtime.ipl_count = ipsq->ips_life.ipl_addtime.ipl_count;
  52527. + ipsec_sa_put(ipsq);
  52528. + if((error = ipsec_sa_delchain(ipsq))) {
  52529. + spin_unlock_bh(&tdb_lock);
  52530. + KLIPS_PRINT(debug_pfkey,
  52531. + "klips_debug:pfkey_update_parse: "
  52532. + "error=%d, trouble deleting intermediate ipsec_sa for SA=%s.\n",
  52533. + error,
  52534. + sa_len ? sa : " (error)");
  52535. + SENDERR(-error);
  52536. + }
  52537. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52538. + }
  52539. +#endif
  52540. +
  52541. + spin_unlock_bh(&tdb_lock);
  52542. +
  52543. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52544. + SADB_UPDATE,
  52545. + satype,
  52546. + 0,
  52547. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52548. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52549. + extensions_reply)
  52550. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52551. + SADB_EXT_SA,
  52552. + extr->ips->ips_said.spi,
  52553. + extr->ips->ips_replaywin,
  52554. + extr->ips->ips_state,
  52555. + extr->ips->ips_authalg,
  52556. + extr->ips->ips_encalg,
  52557. + extr->ips->ips_flags,
  52558. + extr->ips->ips_ref),
  52559. + extensions_reply)
  52560. + /* The 3 lifetime extentions should only be sent if non-zero. */
  52561. + && (extensions[SADB_EXT_LIFETIME_HARD]
  52562. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  52563. + SADB_EXT_LIFETIME_HARD,
  52564. + extr->ips->ips_life.ipl_allocations.ipl_hard,
  52565. + extr->ips->ips_life.ipl_bytes.ipl_hard,
  52566. + extr->ips->ips_life.ipl_addtime.ipl_hard,
  52567. + extr->ips->ips_life.ipl_usetime.ipl_hard,
  52568. + extr->ips->ips_life.ipl_packets.ipl_hard),
  52569. + extensions_reply) : 1)
  52570. + && (extensions[SADB_EXT_LIFETIME_SOFT]
  52571. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  52572. + SADB_EXT_LIFETIME_SOFT,
  52573. + extr->ips->ips_life.ipl_allocations.ipl_count,
  52574. + extr->ips->ips_life.ipl_bytes.ipl_count,
  52575. + extr->ips->ips_life.ipl_addtime.ipl_count,
  52576. + extr->ips->ips_life.ipl_usetime.ipl_count,
  52577. + extr->ips->ips_life.ipl_packets.ipl_count),
  52578. + extensions_reply) : 1)
  52579. + && (extr->ips->ips_life.ipl_allocations.ipl_count
  52580. + || extr->ips->ips_life.ipl_bytes.ipl_count
  52581. + || extr->ips->ips_life.ipl_addtime.ipl_count
  52582. + || extr->ips->ips_life.ipl_usetime.ipl_count
  52583. + || extr->ips->ips_life.ipl_packets.ipl_count
  52584. +
  52585. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_CURRENT],
  52586. + SADB_EXT_LIFETIME_CURRENT,
  52587. + extr->ips->ips_life.ipl_allocations.ipl_count,
  52588. + extr->ips->ips_life.ipl_bytes.ipl_count,
  52589. + extr->ips->ips_life.ipl_addtime.ipl_count,
  52590. + extr->ips->ips_life.ipl_usetime.ipl_count,
  52591. + extr->ips->ips_life.ipl_packets.ipl_count),
  52592. + extensions_reply) : 1)
  52593. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52594. + SADB_EXT_ADDRESS_SRC,
  52595. + 0, /*extr->ips->ips_said.proto,*/
  52596. + 0,
  52597. + extr->ips->ips_addr_s),
  52598. + extensions_reply)
  52599. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52600. + SADB_EXT_ADDRESS_DST,
  52601. + 0, /*extr->ips->ips_said.proto,*/
  52602. + 0,
  52603. + extr->ips->ips_addr_d),
  52604. + extensions_reply)
  52605. + && (extr->ips->ips_ident_s.data
  52606. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  52607. + SADB_EXT_IDENTITY_SRC,
  52608. + extr->ips->ips_ident_s.type,
  52609. + extr->ips->ips_ident_s.id,
  52610. + extr->ips->ips_ident_s.len,
  52611. + extr->ips->ips_ident_s.data),
  52612. + extensions_reply) : 1)
  52613. + && (extr->ips->ips_ident_d.data
  52614. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  52615. + SADB_EXT_IDENTITY_DST,
  52616. + extr->ips->ips_ident_d.type,
  52617. + extr->ips->ips_ident_d.id,
  52618. + extr->ips->ips_ident_d.len,
  52619. + extr->ips->ips_ident_d.data),
  52620. + extensions_reply) : 1)
  52621. +#if 0
  52622. + /* FIXME: This won't work yet because I have not finished
  52623. + it. */
  52624. + && (extr->ips->ips_sens_
  52625. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  52626. + extr->ips->ips_sens_dpd,
  52627. + extr->ips->ips_sens_sens_level,
  52628. + extr->ips->ips_sens_sens_len,
  52629. + extr->ips->ips_sens_sens_bitmap,
  52630. + extr->ips->ips_sens_integ_level,
  52631. + extr->ips->ips_sens_integ_len,
  52632. + extr->ips->ips_sens_integ_bitmap),
  52633. + extensions_reply) : 1)
  52634. +#endif
  52635. + )) {
  52636. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52637. + "failed to build the update reply message extensions\n");
  52638. + SENDERR(-error);
  52639. + }
  52640. +
  52641. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52642. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52643. + "failed to build the update reply message\n");
  52644. + SENDERR(-error);
  52645. + }
  52646. + for(pfkey_socketsp = pfkey_open_sockets;
  52647. + pfkey_socketsp;
  52648. + pfkey_socketsp = pfkey_socketsp->next) {
  52649. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52650. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52651. + "sending up update reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52652. + satype,
  52653. + satype2name(satype),
  52654. + pfkey_socketsp->socketp,
  52655. + error);
  52656. + SENDERR(-error);
  52657. + }
  52658. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52659. + "sending up update reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52660. + satype,
  52661. + satype2name(satype),
  52662. + pfkey_socketsp->socketp);
  52663. + }
  52664. +
  52665. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52666. + if (nat_t_ips_saved) {
  52667. + /**
  52668. + * As we _really_ update existing SA, we keep tdbq and need to delete
  52669. + * parsed ips (nat_t_ips_saved, was extr->ips).
  52670. + *
  52671. + * goto errlab with extr->ips = nat_t_ips_saved will free it.
  52672. + */
  52673. +
  52674. + extr->ips = nat_t_ips_saved;
  52675. +
  52676. + error = 0;
  52677. + KLIPS_PRINT(debug_pfkey,
  52678. + "klips_debug:pfkey_update_parse (NAT-T ports): "
  52679. + "successful for SA: %s\n",
  52680. + sa_len ? sa : " (error)");
  52681. +
  52682. + goto errlab;
  52683. + }
  52684. +#endif
  52685. +
  52686. + if((error = ipsec_sa_add(extr->ips))) {
  52687. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52688. + "failed to update the mature SA=%s with error=%d.\n",
  52689. + sa_len ? sa : " (error)",
  52690. + error);
  52691. + SENDERR(-error);
  52692. + }
  52693. + extr->ips = NULL;
  52694. +
  52695. + KLIPS_PRINT(debug_pfkey,
  52696. + "klips_debug:pfkey_update_parse: "
  52697. + "successful for SA: %s\n",
  52698. + sa_len ? sa : " (error)");
  52699. +
  52700. + errlab:
  52701. + if (pfkey_reply) {
  52702. + pfkey_msg_free(&pfkey_reply);
  52703. + }
  52704. + pfkey_extensions_free(extensions_reply);
  52705. + return error;
  52706. +}
  52707. +
  52708. +DEBUG_NO_STATIC int
  52709. +pfkey_add_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52710. +{
  52711. + int error = 0;
  52712. + struct ipsec_sa* ipsq;
  52713. + char sa[SATOT_BUF];
  52714. + size_t sa_len;
  52715. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52716. + struct sadb_msg *pfkey_reply = NULL;
  52717. + struct socket_list *pfkey_socketsp;
  52718. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52719. +
  52720. + KLIPS_PRINT(debug_pfkey,
  52721. + "klips_debug:pfkey_add_parse: .\n");
  52722. +
  52723. + pfkey_extensions_init(extensions_reply);
  52724. +
  52725. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state != SADB_SASTATE_MATURE) {
  52726. + KLIPS_PRINT(debug_pfkey,
  52727. + "klips_debug:pfkey_add_parse: "
  52728. + "error, sa_state=%d must be MATURE=%d\n",
  52729. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  52730. + SADB_SASTATE_MATURE);
  52731. + SENDERR(EINVAL);
  52732. + }
  52733. +
  52734. + if(!extr || !extr->ips) {
  52735. + KLIPS_PRINT(debug_pfkey,
  52736. + "klips_debug:pfkey_add_parse: "
  52737. + "extr or extr->ips pointer NULL\n");
  52738. + SENDERR(EINVAL);
  52739. + }
  52740. +
  52741. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52742. +
  52743. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52744. + if(ipsq != NULL) {
  52745. + ipsec_sa_put(ipsq);
  52746. + KLIPS_PRINT(debug_pfkey,
  52747. + "klips_debug:pfkey_add_parse: "
  52748. + "found an old ipsec_sa for SA%s, delete it first.\n",
  52749. + sa_len ? sa : " (error)");
  52750. + SENDERR(EEXIST);
  52751. + }
  52752. +
  52753. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52754. + extr->ips->ips_flags |= EMT_INBOUND;
  52755. + }
  52756. +
  52757. + KLIPS_PRINT(debug_pfkey,
  52758. + "klips_debug:pfkey_add_parse: "
  52759. + "existing ipsec_sa not found (this is good) for SA%s, %s-bound, allocating.\n",
  52760. + sa_len ? sa : " (error)",
  52761. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52762. +
  52763. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52764. + extr->ips->ips_rcvif = NULL;
  52765. +
  52766. + if ((error = pfkey_ipsec_sa_init(extr->ips))) {
  52767. + KLIPS_PRINT(debug_pfkey,
  52768. + "klips_debug:pfkey_add_parse: "
  52769. + "not successful for SA: %s, deleting.\n",
  52770. + sa_len ? sa : " (error)");
  52771. + SENDERR(-error);
  52772. + }
  52773. +
  52774. + extr->ips->ips_life.ipl_addtime.ipl_count = jiffies / HZ;
  52775. + if(!extr->ips->ips_life.ipl_allocations.ipl_count) {
  52776. + extr->ips->ips_life.ipl_allocations.ipl_count += 1;
  52777. + }
  52778. +
  52779. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52780. + SADB_ADD,
  52781. + satype,
  52782. + 0,
  52783. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52784. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52785. + extensions_reply)
  52786. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52787. + SADB_EXT_SA,
  52788. + extr->ips->ips_said.spi,
  52789. + extr->ips->ips_replaywin,
  52790. + extr->ips->ips_state,
  52791. + extr->ips->ips_authalg,
  52792. + extr->ips->ips_encalg,
  52793. + extr->ips->ips_flags,
  52794. + extr->ips->ips_ref),
  52795. + extensions_reply)
  52796. + /* The 3 lifetime extentions should only be sent if non-zero. */
  52797. + && (extensions[SADB_EXT_LIFETIME_HARD]
  52798. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  52799. + SADB_EXT_LIFETIME_HARD,
  52800. + extr->ips->ips_life.ipl_allocations.ipl_hard,
  52801. + extr->ips->ips_life.ipl_bytes.ipl_hard,
  52802. + extr->ips->ips_life.ipl_addtime.ipl_hard,
  52803. + extr->ips->ips_life.ipl_usetime.ipl_hard,
  52804. + extr->ips->ips_life.ipl_packets.ipl_hard),
  52805. + extensions_reply) : 1)
  52806. + && (extensions[SADB_EXT_LIFETIME_SOFT]
  52807. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  52808. + SADB_EXT_LIFETIME_SOFT,
  52809. + extr->ips->ips_life.ipl_allocations.ipl_soft,
  52810. + extr->ips->ips_life.ipl_bytes.ipl_soft,
  52811. + extr->ips->ips_life.ipl_addtime.ipl_soft,
  52812. + extr->ips->ips_life.ipl_usetime.ipl_soft,
  52813. + extr->ips->ips_life.ipl_packets.ipl_soft),
  52814. + extensions_reply) : 1)
  52815. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52816. + SADB_EXT_ADDRESS_SRC,
  52817. + 0, /*extr->ips->ips_said.proto,*/
  52818. + 0,
  52819. + extr->ips->ips_addr_s),
  52820. + extensions_reply)
  52821. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52822. + SADB_EXT_ADDRESS_DST,
  52823. + 0, /*extr->ips->ips_said.proto,*/
  52824. + 0,
  52825. + extr->ips->ips_addr_d),
  52826. + extensions_reply)
  52827. + && (extr->ips->ips_ident_s.data
  52828. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  52829. + SADB_EXT_IDENTITY_SRC,
  52830. + extr->ips->ips_ident_s.type,
  52831. + extr->ips->ips_ident_s.id,
  52832. + extr->ips->ips_ident_s.len,
  52833. + extr->ips->ips_ident_s.data),
  52834. + extensions_reply) : 1)
  52835. + && (extr->ips->ips_ident_d.data
  52836. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  52837. + SADB_EXT_IDENTITY_DST,
  52838. + extr->ips->ips_ident_d.type,
  52839. + extr->ips->ips_ident_d.id,
  52840. + extr->ips->ips_ident_d.len,
  52841. + extr->ips->ips_ident_d.data),
  52842. + extensions_reply) : 1)
  52843. +#if 0
  52844. + /* FIXME: This won't work yet because I have not finished
  52845. + it. */
  52846. + && (extr->ips->ips_sens_
  52847. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  52848. + extr->ips->ips_sens_dpd,
  52849. + extr->ips->ips_sens_sens_level,
  52850. + extr->ips->ips_sens_sens_len,
  52851. + extr->ips->ips_sens_sens_bitmap,
  52852. + extr->ips->ips_sens_integ_level,
  52853. + extr->ips->ips_sens_integ_len,
  52854. + extr->ips->ips_sens_integ_bitmap),
  52855. + extensions_reply) : 1)
  52856. +#endif
  52857. + )) {
  52858. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52859. + "failed to build the add reply message extensions\n");
  52860. + SENDERR(-error);
  52861. + }
  52862. +
  52863. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52864. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52865. + "failed to build the add reply message\n");
  52866. + SENDERR(-error);
  52867. + }
  52868. + for(pfkey_socketsp = pfkey_open_sockets;
  52869. + pfkey_socketsp;
  52870. + pfkey_socketsp = pfkey_socketsp->next) {
  52871. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52872. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52873. + "sending up add reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52874. + satype,
  52875. + satype2name(satype),
  52876. + pfkey_socketsp->socketp,
  52877. + error);
  52878. + SENDERR(-error);
  52879. + }
  52880. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52881. + "sending up add reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52882. + satype,
  52883. + satype2name(satype),
  52884. + pfkey_socketsp->socketp);
  52885. + }
  52886. +
  52887. + if((error = ipsec_sa_add(extr->ips))) {
  52888. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52889. + "failed to add the mature SA=%s with error=%d.\n",
  52890. + sa_len ? sa : " (error)",
  52891. + error);
  52892. + SENDERR(-error);
  52893. + }
  52894. + extr->ips = NULL;
  52895. +
  52896. + KLIPS_PRINT(debug_pfkey,
  52897. + "klips_debug:pfkey_add_parse: "
  52898. + "successful for SA: %s\n",
  52899. + sa_len ? sa : " (error)");
  52900. +
  52901. + errlab:
  52902. + if (pfkey_reply) {
  52903. + pfkey_msg_free(&pfkey_reply);
  52904. + }
  52905. + pfkey_extensions_free(extensions_reply);
  52906. + return error;
  52907. +}
  52908. +
  52909. +DEBUG_NO_STATIC int
  52910. +pfkey_delete_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52911. +{
  52912. + struct ipsec_sa *ipsp;
  52913. + char sa[SATOT_BUF];
  52914. + size_t sa_len;
  52915. + int error = 0;
  52916. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52917. + struct sadb_msg *pfkey_reply = NULL;
  52918. + struct socket_list *pfkey_socketsp;
  52919. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52920. +
  52921. + KLIPS_PRINT(debug_pfkey,
  52922. + "klips_debug:pfkey_delete_parse: .\n");
  52923. +
  52924. + pfkey_extensions_init(extensions_reply);
  52925. +
  52926. + if(!extr || !extr->ips) {
  52927. + KLIPS_PRINT(debug_pfkey,
  52928. + "klips_debug:pfkey_delete_parse: "
  52929. + "extr or extr->ips pointer NULL, fatal\n");
  52930. + SENDERR(EINVAL);
  52931. + }
  52932. +
  52933. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52934. +
  52935. + spin_lock_bh(&tdb_lock);
  52936. +
  52937. + ipsp = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52938. + if (ipsp == NULL) {
  52939. + spin_unlock_bh(&tdb_lock);
  52940. + KLIPS_PRINT(debug_pfkey,
  52941. + "klips_debug:pfkey_delete_parse: "
  52942. + "ipsec_sa not found for SA:%s, could not delete.\n",
  52943. + sa_len ? sa : " (error)");
  52944. + SENDERR(ESRCH);
  52945. + }
  52946. +
  52947. + ipsec_sa_put(ipsp);
  52948. + if((error = ipsec_sa_delchain(ipsp))) {
  52949. + spin_unlock_bh(&tdb_lock);
  52950. + KLIPS_PRINT(debug_pfkey,
  52951. + "klips_debug:pfkey_delete_parse: "
  52952. + "error=%d returned trying to delete ipsec_sa for SA:%s.\n",
  52953. + error,
  52954. + sa_len ? sa : " (error)");
  52955. + SENDERR(-error);
  52956. + }
  52957. + spin_unlock_bh(&tdb_lock);
  52958. +
  52959. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52960. + SADB_DELETE,
  52961. + satype,
  52962. + 0,
  52963. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52964. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52965. + extensions_reply)
  52966. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52967. + SADB_EXT_SA,
  52968. + extr->ips->ips_said.spi,
  52969. + 0,
  52970. + 0,
  52971. + 0,
  52972. + 0,
  52973. + 0,
  52974. + extr->ips->ips_ref),
  52975. + extensions_reply)
  52976. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52977. + SADB_EXT_ADDRESS_SRC,
  52978. + 0, /*extr->ips->ips_said.proto,*/
  52979. + 0,
  52980. + extr->ips->ips_addr_s),
  52981. + extensions_reply)
  52982. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52983. + SADB_EXT_ADDRESS_DST,
  52984. + 0, /*extr->ips->ips_said.proto,*/
  52985. + 0,
  52986. + extr->ips->ips_addr_d),
  52987. + extensions_reply)
  52988. + )) {
  52989. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  52990. + "failed to build the delete reply message extensions\n");
  52991. + SENDERR(-error);
  52992. + }
  52993. +
  52994. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52995. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  52996. + "failed to build the delete reply message\n");
  52997. + SENDERR(-error);
  52998. + }
  52999. + for(pfkey_socketsp = pfkey_open_sockets;
  53000. + pfkey_socketsp;
  53001. + pfkey_socketsp = pfkey_socketsp->next) {
  53002. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53003. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53004. + "sending up delete reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53005. + satype,
  53006. + satype2name(satype),
  53007. + pfkey_socketsp->socketp,
  53008. + error);
  53009. + SENDERR(-error);
  53010. + }
  53011. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53012. + "sending up delete reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53013. + satype,
  53014. + satype2name(satype),
  53015. + pfkey_socketsp->socketp);
  53016. + }
  53017. +
  53018. + errlab:
  53019. + if (pfkey_reply) {
  53020. + pfkey_msg_free(&pfkey_reply);
  53021. + }
  53022. + pfkey_extensions_free(extensions_reply);
  53023. + return error;
  53024. +}
  53025. +
  53026. +DEBUG_NO_STATIC int
  53027. +pfkey_get_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53028. +{
  53029. + int error = 0;
  53030. + struct ipsec_sa *ipsp;
  53031. + char sa[SATOT_BUF];
  53032. + size_t sa_len;
  53033. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53034. + struct sadb_msg *pfkey_reply = NULL;
  53035. +
  53036. + KLIPS_PRINT(debug_pfkey,
  53037. + "klips_debug:pfkey_get_parse: .\n");
  53038. +
  53039. + pfkey_extensions_init(extensions_reply);
  53040. +
  53041. + if(!extr || !extr->ips) {
  53042. + KLIPS_PRINT(debug_pfkey,
  53043. + "klips_debug:pfkey_get_parse: "
  53044. + "extr or extr->ips pointer NULL, fatal\n");
  53045. + SENDERR(EINVAL);
  53046. + }
  53047. +
  53048. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  53049. +
  53050. + spin_lock_bh(&tdb_lock);
  53051. +
  53052. + ipsp = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53053. + if (ipsp == NULL) {
  53054. + spin_unlock_bh(&tdb_lock);
  53055. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53056. + "ipsec_sa not found for SA=%s, could not get.\n",
  53057. + sa_len ? sa : " (error)");
  53058. + SENDERR(ESRCH);
  53059. + }
  53060. +
  53061. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53062. + SADB_GET,
  53063. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype,
  53064. + 0,
  53065. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53066. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53067. + extensions_reply)
  53068. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53069. + SADB_EXT_SA,
  53070. + extr->ips->ips_said.spi,
  53071. + extr->ips->ips_replaywin,
  53072. + extr->ips->ips_state,
  53073. + extr->ips->ips_authalg,
  53074. + extr->ips->ips_encalg,
  53075. + extr->ips->ips_flags,
  53076. + extr->ips->ips_ref),
  53077. + extensions_reply)
  53078. + /* The 3 lifetime extentions should only be sent if non-zero. */
  53079. + && (ipsp->ips_life.ipl_allocations.ipl_count
  53080. + || ipsp->ips_life.ipl_bytes.ipl_count
  53081. + || ipsp->ips_life.ipl_addtime.ipl_count
  53082. + || ipsp->ips_life.ipl_usetime.ipl_count
  53083. + || ipsp->ips_life.ipl_packets.ipl_count
  53084. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_CURRENT],
  53085. + SADB_EXT_LIFETIME_CURRENT,
  53086. + ipsp->ips_life.ipl_allocations.ipl_count,
  53087. + ipsp->ips_life.ipl_bytes.ipl_count,
  53088. + ipsp->ips_life.ipl_addtime.ipl_count,
  53089. + ipsp->ips_life.ipl_usetime.ipl_count,
  53090. + ipsp->ips_life.ipl_packets.ipl_count),
  53091. + extensions_reply) : 1)
  53092. + && (ipsp->ips_life.ipl_allocations.ipl_hard
  53093. + || ipsp->ips_life.ipl_bytes.ipl_hard
  53094. + || ipsp->ips_life.ipl_addtime.ipl_hard
  53095. + || ipsp->ips_life.ipl_usetime.ipl_hard
  53096. + || ipsp->ips_life.ipl_packets.ipl_hard
  53097. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  53098. + SADB_EXT_LIFETIME_HARD,
  53099. + ipsp->ips_life.ipl_allocations.ipl_hard,
  53100. + ipsp->ips_life.ipl_bytes.ipl_hard,
  53101. + ipsp->ips_life.ipl_addtime.ipl_hard,
  53102. + ipsp->ips_life.ipl_usetime.ipl_hard,
  53103. + ipsp->ips_life.ipl_packets.ipl_hard),
  53104. + extensions_reply) : 1)
  53105. + && (ipsp->ips_life.ipl_allocations.ipl_soft
  53106. + || ipsp->ips_life.ipl_bytes.ipl_soft
  53107. + || ipsp->ips_life.ipl_addtime.ipl_soft
  53108. + || ipsp->ips_life.ipl_usetime.ipl_soft
  53109. + || ipsp->ips_life.ipl_packets.ipl_soft
  53110. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  53111. + SADB_EXT_LIFETIME_SOFT,
  53112. + ipsp->ips_life.ipl_allocations.ipl_soft,
  53113. + ipsp->ips_life.ipl_bytes.ipl_soft,
  53114. + ipsp->ips_life.ipl_addtime.ipl_soft,
  53115. + ipsp->ips_life.ipl_usetime.ipl_soft,
  53116. + ipsp->ips_life.ipl_packets.ipl_soft),
  53117. + extensions_reply) : 1)
  53118. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  53119. + SADB_EXT_ADDRESS_SRC,
  53120. + 0, /*extr->ips->ips_said.proto,*/
  53121. + 0,
  53122. + extr->ips->ips_addr_s),
  53123. + extensions_reply)
  53124. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53125. + SADB_EXT_ADDRESS_DST,
  53126. + 0, /*extr->ips->ips_said.proto,*/
  53127. + 0,
  53128. + extr->ips->ips_addr_d),
  53129. + extensions_reply)
  53130. + && (extr->ips->ips_addr_p
  53131. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_PROXY],
  53132. + SADB_EXT_ADDRESS_PROXY,
  53133. + 0, /*extr->ips->ips_said.proto,*/
  53134. + 0,
  53135. + extr->ips->ips_addr_p),
  53136. + extensions_reply) : 1)
  53137. +#if 0
  53138. + /* FIXME: This won't work yet because the keys are not
  53139. + stored directly in the ipsec_sa. They are stored as
  53140. + contexts. */
  53141. + && (extr->ips->ips_key_a_size
  53142. + ? pfkey_safe_build(error = pfkey_key_build(&extensions_reply[SADB_EXT_KEY_AUTH],
  53143. + SADB_EXT_KEY_AUTH,
  53144. + extr->ips->ips_key_a_size * 8,
  53145. + extr->ips->ips_key_a),
  53146. + extensions_reply) : 1)
  53147. + /* FIXME: This won't work yet because the keys are not
  53148. + stored directly in the ipsec_sa. They are stored as
  53149. + key schedules. */
  53150. + && (extr->ips->ips_key_e_size
  53151. + ? pfkey_safe_build(error = pfkey_key_build(&extensions_reply[SADB_EXT_KEY_ENCRYPT],
  53152. + SADB_EXT_KEY_ENCRYPT,
  53153. + extr->ips->ips_key_e_size * 8,
  53154. + extr->ips->ips_key_e),
  53155. + extensions_reply) : 1)
  53156. +#endif
  53157. + && (extr->ips->ips_ident_s.data
  53158. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  53159. + SADB_EXT_IDENTITY_SRC,
  53160. + extr->ips->ips_ident_s.type,
  53161. + extr->ips->ips_ident_s.id,
  53162. + extr->ips->ips_ident_s.len,
  53163. + extr->ips->ips_ident_s.data),
  53164. + extensions_reply) : 1)
  53165. + && (extr->ips->ips_ident_d.data
  53166. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  53167. + SADB_EXT_IDENTITY_DST,
  53168. + extr->ips->ips_ident_d.type,
  53169. + extr->ips->ips_ident_d.id,
  53170. + extr->ips->ips_ident_d.len,
  53171. + extr->ips->ips_ident_d.data),
  53172. + extensions_reply) : 1)
  53173. +#if 0
  53174. + /* FIXME: This won't work yet because I have not finished
  53175. + it. */
  53176. + && (extr->ips->ips_sens_
  53177. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  53178. + extr->ips->ips_sens_dpd,
  53179. + extr->ips->ips_sens_sens_level,
  53180. + extr->ips->ips_sens_sens_len,
  53181. + extr->ips->ips_sens_sens_bitmap,
  53182. + extr->ips->ips_sens_integ_level,
  53183. + extr->ips->ips_sens_integ_len,
  53184. + extr->ips->ips_sens_integ_bitmap),
  53185. + extensions_reply) : 1)
  53186. +#endif
  53187. + )) {
  53188. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53189. + "failed to build the get reply message extensions\n");
  53190. + ipsec_sa_put(ipsp);
  53191. + spin_unlock_bh(&tdb_lock);
  53192. + SENDERR(-error);
  53193. + }
  53194. +
  53195. + ipsec_sa_put(ipsp);
  53196. + spin_unlock_bh(&tdb_lock);
  53197. +
  53198. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53199. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53200. + "failed to build the get reply message\n");
  53201. + SENDERR(-error);
  53202. + }
  53203. +
  53204. + if((error = pfkey_upmsg(sk->sk_socket, pfkey_reply))) {
  53205. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53206. + "failed to send the get reply message\n");
  53207. + SENDERR(-error);
  53208. + }
  53209. +
  53210. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53211. + "succeeded in sending get reply message.\n");
  53212. +
  53213. + errlab:
  53214. + if (pfkey_reply) {
  53215. + pfkey_msg_free(&pfkey_reply);
  53216. + }
  53217. + pfkey_extensions_free(extensions_reply);
  53218. + return error;
  53219. +}
  53220. +
  53221. +DEBUG_NO_STATIC int
  53222. +pfkey_acquire_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53223. +{
  53224. + int error = 0;
  53225. + struct socket_list *pfkey_socketsp;
  53226. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53227. +
  53228. + KLIPS_PRINT(debug_pfkey,
  53229. + "klips_debug:pfkey_acquire_parse: .\n");
  53230. +
  53231. + /* XXX I don't know if we want an upper bound, since userspace may
  53232. + want to register itself for an satype > SADB_SATYPE_MAX. */
  53233. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53234. + KLIPS_PRINT(debug_pfkey,
  53235. + "klips_debug:pfkey_acquire_parse: "
  53236. + "SATYPE=%d invalid.\n",
  53237. + satype);
  53238. + SENDERR(EINVAL);
  53239. + }
  53240. +
  53241. + if(!(pfkey_registered_sockets[satype])) {
  53242. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53243. + "no sockets registered for SAtype=%d(%s).\n",
  53244. + satype,
  53245. + satype2name(satype));
  53246. + SENDERR(EPROTONOSUPPORT);
  53247. + }
  53248. +
  53249. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  53250. + pfkey_socketsp;
  53251. + pfkey_socketsp = pfkey_socketsp->next) {
  53252. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53253. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53254. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53255. + "sending up acquire reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53256. + satype,
  53257. + satype2name(satype),
  53258. + pfkey_socketsp->socketp,
  53259. + error);
  53260. + SENDERR(-error);
  53261. + }
  53262. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53263. + "sending up acquire reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53264. + satype,
  53265. + satype2name(satype),
  53266. + pfkey_socketsp->socketp);
  53267. + }
  53268. +
  53269. + errlab:
  53270. + return error;
  53271. +}
  53272. +
  53273. +DEBUG_NO_STATIC int
  53274. +pfkey_register_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53275. +{
  53276. + int error = 0;
  53277. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53278. +
  53279. + KLIPS_PRINT(debug_pfkey,
  53280. + "klips_debug:pfkey_register_parse: .\n");
  53281. +
  53282. + /* XXX I don't know if we want an upper bound, since userspace may
  53283. + want to register itself for an satype > SADB_SATYPE_MAX. */
  53284. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53285. + KLIPS_PRINT(debug_pfkey,
  53286. + "klips_debug:pfkey_register_parse: "
  53287. + "SATYPE=%d invalid.\n",
  53288. + satype);
  53289. + SENDERR(EINVAL);
  53290. + }
  53291. +
  53292. + if(!pfkey_list_insert_socket(sk->sk_socket,
  53293. + &(pfkey_registered_sockets[satype]))) {
  53294. + KLIPS_PRINT(debug_pfkey,
  53295. + "klips_debug:pfkey_register_parse: "
  53296. + "SATYPE=%02d(%s) successfully registered by KMd (pid=%d).\n",
  53297. + satype,
  53298. + satype2name(satype),
  53299. + key_pid(sk));
  53300. + };
  53301. +
  53302. + /* send up register msg with supported SATYPE algos */
  53303. +
  53304. + error=pfkey_register_reply(satype, (struct sadb_msg*)extensions[SADB_EXT_RESERVED]);
  53305. + errlab:
  53306. + return error;
  53307. +}
  53308. +
  53309. +int
  53310. +pfkey_register_reply(int satype, struct sadb_msg *sadb_msg)
  53311. +{
  53312. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53313. + struct sadb_msg *pfkey_reply = NULL;
  53314. + struct socket_list *pfkey_socketsp;
  53315. + struct supported_list *pfkey_supported_listp;
  53316. + unsigned int alg_num_a = 0, alg_num_e = 0;
  53317. + struct sadb_alg *alg_a = NULL, *alg_e = NULL, *alg_ap = NULL, *alg_ep = NULL;
  53318. + int error = 0;
  53319. +
  53320. + pfkey_extensions_init(extensions_reply);
  53321. +
  53322. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53323. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53324. + "SAtype=%d unspecified or unknown.\n",
  53325. + satype);
  53326. + SENDERR(EINVAL);
  53327. + }
  53328. + if(!(pfkey_registered_sockets[satype])) {
  53329. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53330. + "no sockets registered for SAtype=%d(%s).\n",
  53331. + satype,
  53332. + satype2name(satype));
  53333. + SENDERR(EPROTONOSUPPORT);
  53334. + }
  53335. + /* send up register msg with supported SATYPE algos */
  53336. + pfkey_supported_listp = pfkey_supported_list[satype];
  53337. + KLIPS_PRINT(debug_pfkey,
  53338. + "klips_debug:pfkey_register_reply: "
  53339. + "pfkey_supported_list[%d]=0p%p\n",
  53340. + satype,
  53341. + pfkey_supported_list[satype]);
  53342. + while(pfkey_supported_listp) {
  53343. + KLIPS_PRINT(debug_pfkey,
  53344. + "klips_debug:pfkey_register_reply: "
  53345. + "checking supported=0p%p\n",
  53346. + pfkey_supported_listp);
  53347. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_AUTH) {
  53348. + KLIPS_PRINT(debug_pfkey,
  53349. + "klips_debug:pfkey_register_reply: "
  53350. + "adding auth alg.\n");
  53351. + alg_num_a++;
  53352. + }
  53353. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_ENCRYPT) {
  53354. + KLIPS_PRINT(debug_pfkey,
  53355. + "klips_debug:pfkey_register_reply: "
  53356. + "adding encrypt alg.\n");
  53357. + alg_num_e++;
  53358. + }
  53359. + pfkey_supported_listp = pfkey_supported_listp->next;
  53360. + }
  53361. +
  53362. + if(alg_num_a) {
  53363. + KLIPS_PRINT(debug_pfkey,
  53364. + "klips_debug:pfkey_register_reply: "
  53365. + "allocating %lu bytes for auth algs.\n",
  53366. + (unsigned long) (alg_num_a * sizeof(struct sadb_alg)));
  53367. + if((alg_a = kmalloc(alg_num_a * sizeof(struct sadb_alg), GFP_ATOMIC) ) == NULL) {
  53368. + KLIPS_PRINT(debug_pfkey,
  53369. + "klips_debug:pfkey_register_reply: "
  53370. + "auth alg memory allocation error\n");
  53371. + SENDERR(ENOMEM);
  53372. + }
  53373. + alg_ap = alg_a;
  53374. + }
  53375. +
  53376. + if(alg_num_e) {
  53377. + KLIPS_PRINT(debug_pfkey,
  53378. + "klips_debug:pfkey_register_reply: "
  53379. + "allocating %lu bytes for enc algs.\n",
  53380. + (unsigned long) (alg_num_e * sizeof(struct sadb_alg)));
  53381. + if((alg_e = kmalloc(alg_num_e * sizeof(struct sadb_alg), GFP_ATOMIC) ) == NULL) {
  53382. + KLIPS_PRINT(debug_pfkey,
  53383. + "klips_debug:pfkey_register_reply: "
  53384. + "enc alg memory allocation error\n");
  53385. + SENDERR(ENOMEM);
  53386. + }
  53387. + alg_ep = alg_e;
  53388. + }
  53389. +
  53390. + pfkey_supported_listp = pfkey_supported_list[satype];
  53391. + while(pfkey_supported_listp) {
  53392. + if(alg_num_a) {
  53393. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_AUTH) {
  53394. + alg_ap->sadb_alg_id = pfkey_supported_listp->supportedp->ias_id;
  53395. + alg_ap->sadb_alg_ivlen = pfkey_supported_listp->supportedp->ias_ivlen;
  53396. + alg_ap->sadb_alg_minbits = pfkey_supported_listp->supportedp->ias_keyminbits;
  53397. + alg_ap->sadb_alg_maxbits = pfkey_supported_listp->supportedp->ias_keymaxbits;
  53398. + alg_ap->sadb_alg_reserved = 0;
  53399. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  53400. + "klips_debug:pfkey_register_reply: "
  53401. + "adding auth=0p%p\n",
  53402. + alg_ap);
  53403. + alg_ap++;
  53404. + }
  53405. + }
  53406. + if(alg_num_e) {
  53407. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_ENCRYPT) {
  53408. + alg_ep->sadb_alg_id = pfkey_supported_listp->supportedp->ias_id;
  53409. + alg_ep->sadb_alg_ivlen = pfkey_supported_listp->supportedp->ias_ivlen;
  53410. + alg_ep->sadb_alg_minbits = pfkey_supported_listp->supportedp->ias_keyminbits;
  53411. + alg_ep->sadb_alg_maxbits = pfkey_supported_listp->supportedp->ias_keymaxbits;
  53412. + alg_ep->sadb_alg_reserved = 0;
  53413. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  53414. + "klips_debug:pfkey_register_reply: "
  53415. + "adding encrypt=0p%p\n",
  53416. + alg_ep);
  53417. + alg_ep++;
  53418. + }
  53419. + }
  53420. + KLIPS_PRINT(debug_pfkey,
  53421. + "klips_debug:pfkey_register_reply: "
  53422. + "found satype=%d(%s) exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d.\n",
  53423. + satype,
  53424. + satype2name(satype),
  53425. + pfkey_supported_listp->supportedp->ias_exttype,
  53426. + pfkey_supported_listp->supportedp->ias_id,
  53427. + pfkey_supported_listp->supportedp->ias_ivlen,
  53428. + pfkey_supported_listp->supportedp->ias_keyminbits,
  53429. + pfkey_supported_listp->supportedp->ias_keymaxbits);
  53430. + pfkey_supported_listp = pfkey_supported_listp->next;
  53431. + }
  53432. +
  53433. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53434. + SADB_REGISTER,
  53435. + satype,
  53436. + 0,
  53437. + sadb_msg? sadb_msg->sadb_msg_seq : ++pfkey_msg_seq,
  53438. + sadb_msg? sadb_msg->sadb_msg_pid: current->pid),
  53439. + extensions_reply) &&
  53440. + (alg_num_a ? pfkey_safe_build(error = pfkey_supported_build(&extensions_reply[SADB_EXT_SUPPORTED_AUTH],
  53441. + SADB_EXT_SUPPORTED_AUTH,
  53442. + alg_num_a,
  53443. + alg_a),
  53444. + extensions_reply) : 1) &&
  53445. + (alg_num_e ? pfkey_safe_build(error = pfkey_supported_build(&extensions_reply[SADB_EXT_SUPPORTED_ENCRYPT],
  53446. + SADB_EXT_SUPPORTED_ENCRYPT,
  53447. + alg_num_e,
  53448. + alg_e),
  53449. + extensions_reply) : 1))) {
  53450. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53451. + "failed to build the register message extensions_reply\n");
  53452. + SENDERR(-error);
  53453. + }
  53454. +
  53455. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53456. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53457. + "failed to build the register message\n");
  53458. + SENDERR(-error);
  53459. + }
  53460. + /* this should go to all registered sockets for that satype only */
  53461. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  53462. + pfkey_socketsp;
  53463. + pfkey_socketsp = pfkey_socketsp->next) {
  53464. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53465. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53466. + "sending up acquire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53467. + satype,
  53468. + satype2name(satype),
  53469. + pfkey_socketsp->socketp,
  53470. + error);
  53471. + SENDERR(-error);
  53472. + }
  53473. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53474. + "sending up register message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53475. + satype,
  53476. + satype2name(satype),
  53477. + pfkey_socketsp->socketp);
  53478. + }
  53479. +
  53480. + errlab:
  53481. + if(alg_a) {
  53482. + kfree(alg_a);
  53483. + }
  53484. + if(alg_e) {
  53485. + kfree(alg_e);
  53486. + }
  53487. +
  53488. + if (pfkey_reply) {
  53489. + pfkey_msg_free(&pfkey_reply);
  53490. + }
  53491. + pfkey_extensions_free(extensions_reply);
  53492. + return error;
  53493. +}
  53494. +
  53495. +DEBUG_NO_STATIC int
  53496. +pfkey_expire_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53497. +{
  53498. + int error = 0;
  53499. + struct socket_list *pfkey_socketsp;
  53500. +#ifdef CONFIG_KLIPS_DEBUG
  53501. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53502. +#endif /* CONFIG_KLIPS_DEBUG */
  53503. +
  53504. + KLIPS_PRINT(debug_pfkey,
  53505. + "klips_debug:pfkey_expire_parse: .\n");
  53506. +
  53507. + if(pfkey_open_sockets) {
  53508. + for(pfkey_socketsp = pfkey_open_sockets;
  53509. + pfkey_socketsp;
  53510. + pfkey_socketsp = pfkey_socketsp->next) {
  53511. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53512. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53513. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire_parse: "
  53514. + "sending up expire reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53515. + satype,
  53516. + satype2name(satype),
  53517. + pfkey_socketsp->socketp,
  53518. + error);
  53519. + SENDERR(-error);
  53520. + }
  53521. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire_parse: "
  53522. + "sending up expire reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53523. + satype,
  53524. + satype2name(satype),
  53525. + pfkey_socketsp->socketp);
  53526. + }
  53527. + }
  53528. +
  53529. + errlab:
  53530. + return error;
  53531. +}
  53532. +
  53533. +DEBUG_NO_STATIC int
  53534. +pfkey_flush_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53535. +{
  53536. + int error = 0;
  53537. + struct socket_list *pfkey_socketsp;
  53538. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53539. + uint8_t proto = 0;
  53540. +
  53541. + KLIPS_PRINT(debug_pfkey,
  53542. + "klips_debug:pfkey_flush_parse: "
  53543. + "flushing type %d SAs\n",
  53544. + satype);
  53545. +
  53546. + if(satype && !(proto = satype2proto(satype))) {
  53547. + KLIPS_PRINT(debug_pfkey,
  53548. + "klips_debug:pfkey_flush_parse: "
  53549. + "satype %d lookup failed.\n",
  53550. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  53551. + SENDERR(EINVAL);
  53552. + }
  53553. +
  53554. + if ((error = ipsec_sadb_cleanup(proto))) {
  53555. + SENDERR(-error);
  53556. + }
  53557. +
  53558. + if(pfkey_open_sockets) {
  53559. + for(pfkey_socketsp = pfkey_open_sockets;
  53560. + pfkey_socketsp;
  53561. + pfkey_socketsp = pfkey_socketsp->next) {
  53562. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53563. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53564. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_flush_parse: "
  53565. + "sending up flush reply message for satype=%d(%s) (proto=%d) to socket=0p%p failed with error=%d.\n",
  53566. + satype,
  53567. + satype2name(satype),
  53568. + proto,
  53569. + pfkey_socketsp->socketp,
  53570. + error);
  53571. + SENDERR(-error);
  53572. + }
  53573. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_flush_parse: "
  53574. + "sending up flush reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53575. + satype,
  53576. + satype2name(satype),
  53577. + pfkey_socketsp->socketp);
  53578. + }
  53579. + }
  53580. +
  53581. + errlab:
  53582. + return error;
  53583. +}
  53584. +
  53585. +DEBUG_NO_STATIC int
  53586. +pfkey_dump_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53587. +{
  53588. + int error = 0;
  53589. +
  53590. + KLIPS_PRINT(debug_pfkey,
  53591. + "klips_debug:pfkey_dump_parse: .\n");
  53592. +
  53593. + SENDERR(ENOSYS);
  53594. + errlab:
  53595. + return error;
  53596. +}
  53597. +
  53598. +DEBUG_NO_STATIC int
  53599. +pfkey_x_promisc_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53600. +{
  53601. + int error = 0;
  53602. +
  53603. + KLIPS_PRINT(debug_pfkey,
  53604. + "klips_debug:pfkey_promisc_parse: .\n");
  53605. +
  53606. + SENDERR(ENOSYS);
  53607. + errlab:
  53608. + return error;
  53609. +}
  53610. +
  53611. +DEBUG_NO_STATIC int
  53612. +pfkey_x_pchange_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53613. +{
  53614. + int error = 0;
  53615. +
  53616. + KLIPS_PRINT(debug_pfkey,
  53617. + "klips_debug:pfkey_x_pchange_parse: .\n");
  53618. +
  53619. + SENDERR(ENOSYS);
  53620. + errlab:
  53621. + return error;
  53622. +}
  53623. +
  53624. +DEBUG_NO_STATIC int
  53625. +pfkey_x_grpsa_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53626. +{
  53627. + struct ipsec_sa *ips1p, *ips2p, *ipsp;
  53628. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53629. + struct sadb_msg *pfkey_reply = NULL;
  53630. + struct socket_list *pfkey_socketsp;
  53631. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53632. + char sa1[SATOT_BUF], sa2[SATOT_BUF];
  53633. + size_t sa_len1, sa_len2 = 0;
  53634. + int error = 0;
  53635. +
  53636. + KLIPS_PRINT(debug_pfkey,
  53637. + "klips_debug:pfkey_x_grpsa_parse: .\n");
  53638. +
  53639. + pfkey_extensions_init(extensions_reply);
  53640. +
  53641. + if(extr == NULL || extr->ips == NULL) {
  53642. + KLIPS_PRINT(debug_pfkey,
  53643. + "klips_debug:pfkey_x_grpsa_parse: "
  53644. + "extr or extr->ips is NULL, fatal.\n");
  53645. + SENDERR(EINVAL);
  53646. + }
  53647. +
  53648. + sa_len1 = satot(&extr->ips->ips_said, 0, sa1, sizeof(sa1));
  53649. + if(extr->ips2 != NULL) {
  53650. + sa_len2 = satot(&extr->ips2->ips_said, 0, sa2, sizeof(sa2));
  53651. + }
  53652. +
  53653. + spin_lock_bh(&tdb_lock);
  53654. +
  53655. + ips1p = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53656. + if(ips1p == NULL) {
  53657. + spin_unlock_bh(&tdb_lock);
  53658. + KLIPS_PRINT(debug_pfkey,
  53659. + "klips_debug:pfkey_x_grpsa_parse: "
  53660. + "reserved ipsec_sa for SA1: %s not found. Call SADB_ADD/UPDATE first.\n",
  53661. + sa_len1 ? sa1 : " (error)");
  53662. + SENDERR(ENOENT);
  53663. + }
  53664. + if(extr->ips2) { /* GRPSA */
  53665. + ips2p = ipsec_sa_getbyid(&(extr->ips2->ips_said));
  53666. + if(ips2p == NULL) {
  53667. + ipsec_sa_put(ips1p);
  53668. + spin_unlock_bh(&tdb_lock);
  53669. + KLIPS_PRINT(debug_pfkey,
  53670. + "klips_debug:pfkey_x_grpsa_parse: "
  53671. + "reserved ipsec_sa for SA2: %s not found. Call SADB_ADD/UPDATE first.\n",
  53672. + sa_len2 ? sa2 : " (error)");
  53673. + SENDERR(ENOENT);
  53674. + }
  53675. +
  53676. + /* Is either one already linked? */
  53677. + if(ips1p->ips_onext) {
  53678. + ipsec_sa_put(ips1p);
  53679. + ipsec_sa_put(ips2p);
  53680. + spin_unlock_bh(&tdb_lock);
  53681. + KLIPS_PRINT(debug_pfkey,
  53682. + "klips_debug:pfkey_x_grpsa_parse: "
  53683. + "ipsec_sa for SA: %s is already linked.\n",
  53684. + sa_len1 ? sa1 : " (error)");
  53685. + SENDERR(EEXIST);
  53686. + }
  53687. + if(ips2p->ips_inext) {
  53688. + ipsec_sa_put(ips1p);
  53689. + ipsec_sa_put(ips2p);
  53690. + spin_unlock_bh(&tdb_lock);
  53691. + KLIPS_PRINT(debug_pfkey,
  53692. + "klips_debug:pfkey_x_grpsa_parse: "
  53693. + "ipsec_sa for SA: %s is already linked.\n",
  53694. + sa_len2 ? sa2 : " (error)");
  53695. + SENDERR(EEXIST);
  53696. + }
  53697. +
  53698. + /* Is extr->ips already linked to extr->ips2? */
  53699. + ipsp = ips2p;
  53700. + while(ipsp) {
  53701. + if(ipsp == ips1p) {
  53702. + ipsec_sa_put(ips1p);
  53703. + ipsec_sa_put(ips2p);
  53704. + spin_unlock_bh(&tdb_lock);
  53705. + KLIPS_PRINT(debug_pfkey,
  53706. + "klips_debug:pfkey_x_grpsa_parse: "
  53707. + "ipsec_sa for SA: %s is already linked to %s.\n",
  53708. + sa_len1 ? sa1 : " (error)",
  53709. + sa_len2 ? sa2 : " (error)");
  53710. + SENDERR(EEXIST);
  53711. + }
  53712. + ipsp = ipsp->ips_onext;
  53713. + }
  53714. +
  53715. + /* link 'em */
  53716. + KLIPS_PRINT(debug_pfkey,
  53717. + "klips_debug:pfkey_x_grpsa_parse: "
  53718. + "linking ipsec_sa SA: %s with %s.\n",
  53719. + sa_len1 ? sa1 : " (error)",
  53720. + sa_len2 ? sa2 : " (error)");
  53721. + ips1p->ips_onext = ips2p;
  53722. + ips2p->ips_inext = ips1p;
  53723. + } else { /* UNGRPSA */
  53724. + ipsec_sa_put(ips1p);
  53725. + KLIPS_PRINT(debug_pfkey,
  53726. + "klips_debug:pfkey_x_grpsa_parse: "
  53727. + "unlinking ipsec_sa SA: %s.\n",
  53728. + sa_len1 ? sa1 : " (error)");
  53729. + while(ips1p->ips_onext) {
  53730. + ips1p = ips1p->ips_onext;
  53731. + }
  53732. + while(ips1p->ips_inext) {
  53733. + ipsp = ips1p;
  53734. + ips1p = ips1p->ips_inext;
  53735. + ipsec_sa_put(ips1p);
  53736. + ipsp->ips_inext = NULL;
  53737. + ipsec_sa_put(ipsp);
  53738. + ips1p->ips_onext = NULL;
  53739. + }
  53740. + }
  53741. +
  53742. + spin_unlock_bh(&tdb_lock);
  53743. +
  53744. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53745. + SADB_X_GRPSA,
  53746. + satype,
  53747. + 0,
  53748. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53749. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53750. + extensions_reply)
  53751. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53752. + SADB_EXT_SA,
  53753. + extr->ips->ips_said.spi,
  53754. + extr->ips->ips_replaywin,
  53755. + extr->ips->ips_state,
  53756. + extr->ips->ips_authalg,
  53757. + extr->ips->ips_encalg,
  53758. + extr->ips->ips_flags,
  53759. + extr->ips->ips_ref),
  53760. + extensions_reply)
  53761. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53762. + SADB_EXT_ADDRESS_DST,
  53763. + 0, /*extr->ips->ips_said.proto,*/
  53764. + 0,
  53765. + extr->ips->ips_addr_d),
  53766. + extensions_reply)
  53767. + && (extr->ips2
  53768. + ? (pfkey_safe_build(error = pfkey_x_satype_build(&extensions_reply[SADB_X_EXT_SATYPE2],
  53769. + ((struct sadb_x_satype*)extensions[SADB_X_EXT_SATYPE2])->sadb_x_satype_satype
  53770. + /* proto2satype(extr->ips2->ips_said.proto) */),
  53771. + extensions_reply)
  53772. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_X_EXT_SA2],
  53773. + SADB_X_EXT_SA2,
  53774. + extr->ips2->ips_said.spi,
  53775. + extr->ips2->ips_replaywin,
  53776. + extr->ips2->ips_state,
  53777. + extr->ips2->ips_authalg,
  53778. + extr->ips2->ips_encalg,
  53779. + extr->ips2->ips_flags,
  53780. + extr->ips2->ips_ref),
  53781. + extensions_reply)
  53782. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST2],
  53783. + SADB_X_EXT_ADDRESS_DST2,
  53784. + 0, /*extr->ips->ips_said.proto,*/
  53785. + 0,
  53786. + extr->ips2->ips_addr_d),
  53787. + extensions_reply) ) : 1 )
  53788. + )) {
  53789. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53790. + "failed to build the x_grpsa reply message extensions\n");
  53791. + SENDERR(-error);
  53792. + }
  53793. +
  53794. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53795. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53796. + "failed to build the x_grpsa reply message\n");
  53797. + SENDERR(-error);
  53798. + }
  53799. +
  53800. + for(pfkey_socketsp = pfkey_open_sockets;
  53801. + pfkey_socketsp;
  53802. + pfkey_socketsp = pfkey_socketsp->next) {
  53803. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53804. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53805. + "sending up x_grpsa reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53806. + satype,
  53807. + satype2name(satype),
  53808. + pfkey_socketsp->socketp,
  53809. + error);
  53810. + SENDERR(-error);
  53811. + }
  53812. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53813. + "sending up x_grpsa reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53814. + satype,
  53815. + satype2name(satype),
  53816. + pfkey_socketsp->socketp);
  53817. + }
  53818. +
  53819. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53820. + "succeeded in sending x_grpsa reply message.\n");
  53821. +
  53822. + errlab:
  53823. + if (pfkey_reply) {
  53824. + pfkey_msg_free(&pfkey_reply);
  53825. + }
  53826. + pfkey_extensions_free(extensions_reply);
  53827. + return error;
  53828. +}
  53829. +
  53830. +DEBUG_NO_STATIC int
  53831. +pfkey_x_addflow_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53832. +{
  53833. + int error = 0;
  53834. +#ifdef CONFIG_KLIPS_DEBUG
  53835. + char buf1[64], buf2[64];
  53836. +#endif /* CONFIG_KLIPS_DEBUG */
  53837. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53838. + struct sadb_msg *pfkey_reply = NULL;
  53839. + struct socket_list *pfkey_socketsp;
  53840. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53841. + ip_address srcflow, dstflow, srcmask, dstmask;
  53842. +
  53843. + KLIPS_PRINT(debug_pfkey,
  53844. + "klips_debug:pfkey_x_addflow_parse: .\n");
  53845. +
  53846. + pfkey_extensions_init(extensions_reply);
  53847. +
  53848. + memset((caddr_t)&srcflow, 0, sizeof(srcflow));
  53849. + memset((caddr_t)&dstflow, 0, sizeof(dstflow));
  53850. + memset((caddr_t)&srcmask, 0, sizeof(srcmask));
  53851. + memset((caddr_t)&dstmask, 0, sizeof(dstmask));
  53852. +
  53853. + if(!extr || !(extr->ips) || !(extr->eroute)) {
  53854. + KLIPS_PRINT(debug_pfkey,
  53855. + "klips_debug:pfkey_x_addflow_parse: "
  53856. + "missing extr, ipsec_sa or eroute data.\n");
  53857. + SENDERR(EINVAL);
  53858. + }
  53859. +
  53860. + srcflow.u.v4.sin_family = AF_INET;
  53861. + dstflow.u.v4.sin_family = AF_INET;
  53862. + srcmask.u.v4.sin_family = AF_INET;
  53863. + dstmask.u.v4.sin_family = AF_INET;
  53864. + srcflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_src;
  53865. + dstflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_dst;
  53866. + srcmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_src;
  53867. + dstmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_dst;
  53868. +
  53869. +#ifdef CONFIG_KLIPS_DEBUG
  53870. + if (debug_pfkey) {
  53871. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  53872. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  53873. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  53874. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  53875. + KLIPS_PRINT(debug_pfkey,
  53876. + "klips_debug:pfkey_x_addflow_parse: "
  53877. + "calling breakeroute and/or makeroute for %s->%s\n",
  53878. + buf1, buf2);
  53879. + }
  53880. +#endif /* CONFIG_KLIPS_DEBUG */
  53881. +
  53882. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_INFLOW) {
  53883. + struct ipsec_sa *ipsp, *ipsq;
  53884. + char sa[SATOT_BUF];
  53885. + size_t sa_len;
  53886. +
  53887. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53888. + if(ipsq == NULL) {
  53889. + KLIPS_PRINT(debug_pfkey,
  53890. + "klips_debug:pfkey_x_addflow_parse: "
  53891. + "ipsec_sa not found, cannot set incoming policy.\n");
  53892. + SENDERR(ENOENT);
  53893. + }
  53894. +
  53895. + ipsp = ipsq;
  53896. + while(ipsp && ipsp->ips_said.proto != IPPROTO_IPIP) {
  53897. + ipsp = ipsp->ips_inext;
  53898. + }
  53899. +
  53900. + if(ipsp == NULL) {
  53901. + ipsec_sa_put(ipsq);
  53902. + KLIPS_PRINT(debug_pfkey,
  53903. + "klips_debug:pfkey_x_addflow_parse: "
  53904. + "SA chain does not have an IPIP SA, cannot set incoming policy.\n");
  53905. + SENDERR(ENOENT);
  53906. + }
  53907. +
  53908. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  53909. +
  53910. + ipsp->ips_flags |= SADB_X_SAFLAGS_INFLOW;
  53911. + ipsp->ips_flow_s = srcflow;
  53912. + ipsp->ips_flow_d = dstflow;
  53913. + ipsp->ips_mask_s = srcmask;
  53914. + ipsp->ips_mask_d = dstmask;
  53915. +
  53916. + ipsec_sa_put(ipsq);
  53917. +
  53918. + KLIPS_PRINT(debug_pfkey,
  53919. + "klips_debug:pfkey_x_addflow_parse: "
  53920. + "inbound eroute, setting incoming policy information in IPIP ipsec_sa for SA: %s.\n",
  53921. + sa_len ? sa : " (error)");
  53922. + } else {
  53923. + struct sk_buff *first = NULL, *last = NULL;
  53924. +
  53925. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_REPLACEFLOW) {
  53926. + KLIPS_PRINT(debug_pfkey,
  53927. + "klips_debug:pfkey_x_addflow_parse: "
  53928. + "REPLACEFLOW flag set, calling breakeroute.\n");
  53929. + if ((error = ipsec_breakroute(&(extr->eroute->er_eaddr),
  53930. + &(extr->eroute->er_emask),
  53931. + &first, &last))) {
  53932. + KLIPS_PRINT(debug_pfkey,
  53933. + "klips_debug:pfkey_x_addflow_parse: "
  53934. + "breakeroute returned %d. first=0p%p, last=0p%p\n",
  53935. + error,
  53936. + first,
  53937. + last);
  53938. + if(first != NULL) {
  53939. + ipsec_kfree_skb(first);
  53940. + }
  53941. + if(last != NULL) {
  53942. + ipsec_kfree_skb(last);
  53943. + }
  53944. + SENDERR(-error);
  53945. + }
  53946. + }
  53947. +
  53948. + KLIPS_PRINT(debug_pfkey,
  53949. + "klips_debug:pfkey_x_addflow_parse: "
  53950. + "calling makeroute.\n");
  53951. +
  53952. + if ((error = ipsec_makeroute(&(extr->eroute->er_eaddr),
  53953. + &(extr->eroute->er_emask),
  53954. + extr->ips->ips_said,
  53955. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid,
  53956. + NULL,
  53957. + &(extr->ips->ips_ident_s),
  53958. + &(extr->ips->ips_ident_d)))) {
  53959. + KLIPS_PRINT(debug_pfkey,
  53960. + "klips_debug:pfkey_x_addflow_parse: "
  53961. + "makeroute returned %d.\n", error);
  53962. + SENDERR(-error);
  53963. + }
  53964. + if(first != NULL) {
  53965. + KLIPS_PRINT(debug_eroute,
  53966. + "klips_debug:pfkey_x_addflow_parse: "
  53967. + "first=0p%p HOLD packet re-injected.\n",
  53968. + first);
  53969. + DEV_QUEUE_XMIT(first, first->dev, SOPRI_NORMAL);
  53970. + }
  53971. + if(last != NULL) {
  53972. + KLIPS_PRINT(debug_eroute,
  53973. + "klips_debug:pfkey_x_addflow_parse: "
  53974. + "last=0p%p HOLD packet re-injected.\n",
  53975. + last);
  53976. + DEV_QUEUE_XMIT(last, last->dev, SOPRI_NORMAL);
  53977. + }
  53978. + }
  53979. +
  53980. + KLIPS_PRINT(debug_pfkey,
  53981. + "klips_debug:pfkey_x_addflow_parse: "
  53982. + "makeroute call successful.\n");
  53983. +
  53984. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53985. + SADB_X_ADDFLOW,
  53986. + satype,
  53987. + 0,
  53988. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53989. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53990. + extensions_reply)
  53991. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53992. + SADB_EXT_SA,
  53993. + extr->ips->ips_said.spi,
  53994. + extr->ips->ips_replaywin,
  53995. + extr->ips->ips_state,
  53996. + extr->ips->ips_authalg,
  53997. + extr->ips->ips_encalg,
  53998. + extr->ips->ips_flags,
  53999. + extr->ips->ips_ref),
  54000. + extensions_reply)
  54001. + && (extensions[SADB_EXT_ADDRESS_SRC]
  54002. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  54003. + SADB_EXT_ADDRESS_SRC,
  54004. + 0, /*extr->ips->ips_said.proto,*/
  54005. + 0,
  54006. + extr->ips->ips_addr_s),
  54007. + extensions_reply) : 1)
  54008. + && (extensions[SADB_EXT_ADDRESS_DST]
  54009. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  54010. + SADB_EXT_ADDRESS_DST,
  54011. + 0, /*extr->ips->ips_said.proto,*/
  54012. + 0,
  54013. + extr->ips->ips_addr_d),
  54014. + extensions_reply) : 1)
  54015. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_FLOW],
  54016. + SADB_X_EXT_ADDRESS_SRC_FLOW,
  54017. + 0, /*extr->ips->ips_said.proto,*/
  54018. + 0,
  54019. + (struct sockaddr*)&srcflow),
  54020. + extensions_reply)
  54021. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_FLOW],
  54022. + SADB_X_EXT_ADDRESS_DST_FLOW,
  54023. + 0, /*extr->ips->ips_said.proto,*/
  54024. + 0,
  54025. + (struct sockaddr*)&dstflow),
  54026. + extensions_reply)
  54027. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_MASK],
  54028. + SADB_X_EXT_ADDRESS_SRC_MASK,
  54029. + 0, /*extr->ips->ips_said.proto,*/
  54030. + 0,
  54031. + (struct sockaddr*)&srcmask),
  54032. + extensions_reply)
  54033. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_MASK],
  54034. + SADB_X_EXT_ADDRESS_DST_MASK,
  54035. + 0, /*extr->ips->ips_said.proto,*/
  54036. + 0,
  54037. + (struct sockaddr*)&dstmask),
  54038. + extensions_reply)
  54039. + )) {
  54040. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54041. + "failed to build the x_addflow reply message extensions\n");
  54042. + SENDERR(-error);
  54043. + }
  54044. +
  54045. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  54046. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54047. + "failed to build the x_addflow reply message\n");
  54048. + SENDERR(-error);
  54049. + }
  54050. +
  54051. + for(pfkey_socketsp = pfkey_open_sockets;
  54052. + pfkey_socketsp;
  54053. + pfkey_socketsp = pfkey_socketsp->next) {
  54054. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  54055. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54056. + "sending up x_addflow reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54057. + satype,
  54058. + satype2name(satype),
  54059. + pfkey_socketsp->socketp,
  54060. + error);
  54061. + SENDERR(-error);
  54062. + }
  54063. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54064. + "sending up x_addflow reply message for satype=%d(%s) (proto=%d) to socket=0p%p succeeded.\n",
  54065. + satype,
  54066. + satype2name(satype),
  54067. + extr->ips->ips_said.proto,
  54068. + pfkey_socketsp->socketp);
  54069. + }
  54070. +
  54071. + KLIPS_PRINT(debug_pfkey,
  54072. + "klips_debug:pfkey_x_addflow_parse: "
  54073. + "extr->ips cleaned up and freed.\n");
  54074. +
  54075. + errlab:
  54076. + if (pfkey_reply) {
  54077. + pfkey_msg_free(&pfkey_reply);
  54078. + }
  54079. + pfkey_extensions_free(extensions_reply);
  54080. + return error;
  54081. +}
  54082. +
  54083. +DEBUG_NO_STATIC int
  54084. +pfkey_x_delflow_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54085. +{
  54086. + int error = 0;
  54087. +#ifdef CONFIG_KLIPS_DEBUG
  54088. + char buf1[64], buf2[64];
  54089. +#endif /* CONFIG_KLIPS_DEBUG */
  54090. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  54091. + struct sadb_msg *pfkey_reply = NULL;
  54092. + struct socket_list *pfkey_socketsp;
  54093. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  54094. + ip_address srcflow, dstflow, srcmask, dstmask;
  54095. +
  54096. + KLIPS_PRINT(debug_pfkey,
  54097. + "klips_debug:pfkey_x_delflow_parse: .\n");
  54098. +
  54099. + pfkey_extensions_init(extensions_reply);
  54100. +
  54101. + memset((caddr_t)&srcflow, 0, sizeof(srcflow));
  54102. + memset((caddr_t)&dstflow, 0, sizeof(dstflow));
  54103. + memset((caddr_t)&srcmask, 0, sizeof(srcmask));
  54104. + memset((caddr_t)&dstmask, 0, sizeof(dstmask));
  54105. +
  54106. + if(!extr || !(extr->ips)) {
  54107. + KLIPS_PRINT(debug_pfkey,
  54108. + "klips_debug:pfkey_x_delflow_parse: "
  54109. + "extr, or extr->ips is NULL, fatal\n");
  54110. + SENDERR(EINVAL);
  54111. + }
  54112. +
  54113. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_CLEARFLOW) {
  54114. + KLIPS_PRINT(debug_pfkey,
  54115. + "klips_debug:pfkey_x_delflow_parse: "
  54116. + "CLEARFLOW flag set, calling cleareroutes.\n");
  54117. + if ((error = ipsec_cleareroutes()))
  54118. + KLIPS_PRINT(debug_pfkey,
  54119. + "klips_debug:pfkey_x_delflow_parse: "
  54120. + "cleareroutes returned %d.\n", error);
  54121. + SENDERR(-error);
  54122. + } else {
  54123. + struct sk_buff *first = NULL, *last = NULL;
  54124. +
  54125. + if(!(extr->eroute)) {
  54126. + KLIPS_PRINT(debug_pfkey,
  54127. + "klips_debug:pfkey_x_delflow_parse: "
  54128. + "extr->eroute is NULL, fatal.\n");
  54129. + SENDERR(EINVAL);
  54130. + }
  54131. +
  54132. + srcflow.u.v4.sin_family = AF_INET;
  54133. + dstflow.u.v4.sin_family = AF_INET;
  54134. + srcmask.u.v4.sin_family = AF_INET;
  54135. + dstmask.u.v4.sin_family = AF_INET;
  54136. + srcflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_src;
  54137. + dstflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_dst;
  54138. + srcmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_src;
  54139. + dstmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_dst;
  54140. +
  54141. +#ifdef CONFIG_KLIPS_DEBUG
  54142. + if (debug_pfkey) {
  54143. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  54144. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  54145. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  54146. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  54147. + KLIPS_PRINT(debug_pfkey,
  54148. + "klips_debug:pfkey_x_delflow_parse: "
  54149. + "calling breakeroute for %s->%s\n",
  54150. + buf1, buf2);
  54151. + }
  54152. +#endif /* CONFIG_KLIPS_DEBUG */
  54153. + error = ipsec_breakroute(&(extr->eroute->er_eaddr),
  54154. + &(extr->eroute->er_emask),
  54155. + &first, &last);
  54156. + if(error) {
  54157. + KLIPS_PRINT(debug_pfkey,
  54158. + "klips_debug:pfkey_x_delflow_parse: "
  54159. + "breakeroute returned %d. first=0p%p, last=0p%p\n",
  54160. + error,
  54161. + first,
  54162. + last);
  54163. + }
  54164. + if(first != NULL) {
  54165. + ipsec_kfree_skb(first);
  54166. + }
  54167. + if(last != NULL) {
  54168. + ipsec_kfree_skb(last);
  54169. + }
  54170. + if(error) {
  54171. + SENDERR(-error);
  54172. + }
  54173. + }
  54174. +
  54175. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  54176. + SADB_X_DELFLOW,
  54177. + satype,
  54178. + 0,
  54179. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  54180. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  54181. + extensions_reply)
  54182. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  54183. + SADB_EXT_SA,
  54184. + extr->ips->ips_said.spi,
  54185. + extr->ips->ips_replaywin,
  54186. + extr->ips->ips_state,
  54187. + extr->ips->ips_authalg,
  54188. + extr->ips->ips_encalg,
  54189. + extr->ips->ips_flags,
  54190. + extr->ips->ips_ref),
  54191. + extensions_reply)
  54192. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_FLOW],
  54193. + SADB_X_EXT_ADDRESS_SRC_FLOW,
  54194. + 0, /*extr->ips->ips_said.proto,*/
  54195. + 0,
  54196. + (struct sockaddr*)&srcflow),
  54197. + extensions_reply)
  54198. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_FLOW],
  54199. + SADB_X_EXT_ADDRESS_DST_FLOW,
  54200. + 0, /*extr->ips->ips_said.proto,*/
  54201. + 0,
  54202. + (struct sockaddr*)&dstflow),
  54203. + extensions_reply)
  54204. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_MASK],
  54205. + SADB_X_EXT_ADDRESS_SRC_MASK,
  54206. + 0, /*extr->ips->ips_said.proto,*/
  54207. + 0,
  54208. + (struct sockaddr*)&srcmask),
  54209. + extensions_reply)
  54210. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_MASK],
  54211. + SADB_X_EXT_ADDRESS_DST_MASK,
  54212. + 0, /*extr->ips->ips_said.proto,*/
  54213. + 0,
  54214. + (struct sockaddr*)&dstmask),
  54215. + extensions_reply)
  54216. + )) {
  54217. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54218. + "failed to build the x_delflow reply message extensions\n");
  54219. + SENDERR(-error);
  54220. + }
  54221. +
  54222. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  54223. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54224. + "failed to build the x_delflow reply message\n");
  54225. + SENDERR(-error);
  54226. + }
  54227. +
  54228. + for(pfkey_socketsp = pfkey_open_sockets;
  54229. + pfkey_socketsp;
  54230. + pfkey_socketsp = pfkey_socketsp->next) {
  54231. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  54232. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54233. + "sending up x_delflow reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54234. + satype,
  54235. + satype2name(satype),
  54236. + pfkey_socketsp->socketp,
  54237. + error);
  54238. + SENDERR(-error);
  54239. + }
  54240. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54241. + "sending up x_delflow reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  54242. + satype,
  54243. + satype2name(satype),
  54244. + pfkey_socketsp->socketp);
  54245. + }
  54246. +
  54247. + KLIPS_PRINT(debug_pfkey,
  54248. + "klips_debug:pfkey_x_delflow_parse: "
  54249. + "extr->ips cleaned up and freed.\n");
  54250. +
  54251. + errlab:
  54252. + if (pfkey_reply) {
  54253. + pfkey_msg_free(&pfkey_reply);
  54254. + }
  54255. + pfkey_extensions_free(extensions_reply);
  54256. + return error;
  54257. +}
  54258. +
  54259. +DEBUG_NO_STATIC int
  54260. +pfkey_x_msg_debug_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54261. +{
  54262. + int error = 0;
  54263. +
  54264. + KLIPS_PRINT(debug_pfkey,
  54265. + "klips_debug:pfkey_x_msg_debug_parse: .\n");
  54266. +
  54267. +/* errlab:*/
  54268. + return error;
  54269. +}
  54270. +
  54271. +/* pfkey_expire expects the ipsec_sa table to be locked before being called. */
  54272. +int
  54273. +pfkey_expire(struct ipsec_sa *ipsp, int hard)
  54274. +{
  54275. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54276. + struct sadb_msg *pfkey_msg = NULL;
  54277. + struct socket_list *pfkey_socketsp;
  54278. + int error = 0;
  54279. + uint8_t satype;
  54280. +
  54281. + pfkey_extensions_init(extensions);
  54282. +
  54283. + if(!(satype = proto2satype(ipsp->ips_said.proto))) {
  54284. + KLIPS_PRINT(debug_pfkey,
  54285. + "klips_debug:pfkey_expire: "
  54286. + "satype lookup for protocol %d lookup failed.\n",
  54287. + ipsp->ips_said.proto);
  54288. + SENDERR(EINVAL);
  54289. + }
  54290. +
  54291. + if(!pfkey_open_sockets) {
  54292. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54293. + "no sockets listening.\n");
  54294. + SENDERR(EPROTONOSUPPORT);
  54295. + }
  54296. +
  54297. + if (!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions[0],
  54298. + SADB_EXPIRE,
  54299. + satype,
  54300. + 0,
  54301. + ++pfkey_msg_seq,
  54302. + 0),
  54303. + extensions)
  54304. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions[SADB_EXT_SA],
  54305. + SADB_EXT_SA,
  54306. + ipsp->ips_said.spi,
  54307. + ipsp->ips_replaywin,
  54308. + ipsp->ips_state,
  54309. + ipsp->ips_authalg,
  54310. + ipsp->ips_encalg,
  54311. + ipsp->ips_flags,
  54312. + ipsp->ips_ref),
  54313. + extensions)
  54314. + && pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_CURRENT],
  54315. + SADB_EXT_LIFETIME_CURRENT,
  54316. + ipsp->ips_life.ipl_allocations.ipl_count,
  54317. + ipsp->ips_life.ipl_bytes.ipl_count,
  54318. + ipsp->ips_life.ipl_addtime.ipl_count,
  54319. + ipsp->ips_life.ipl_usetime.ipl_count,
  54320. + ipsp->ips_life.ipl_packets.ipl_count),
  54321. + extensions)
  54322. + && (hard ?
  54323. + pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_HARD],
  54324. + SADB_EXT_LIFETIME_HARD,
  54325. + ipsp->ips_life.ipl_allocations.ipl_hard,
  54326. + ipsp->ips_life.ipl_bytes.ipl_hard,
  54327. + ipsp->ips_life.ipl_addtime.ipl_hard,
  54328. + ipsp->ips_life.ipl_usetime.ipl_hard,
  54329. + ipsp->ips_life.ipl_packets.ipl_hard),
  54330. + extensions)
  54331. + : pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_SOFT],
  54332. + SADB_EXT_LIFETIME_SOFT,
  54333. + ipsp->ips_life.ipl_allocations.ipl_soft,
  54334. + ipsp->ips_life.ipl_bytes.ipl_soft,
  54335. + ipsp->ips_life.ipl_addtime.ipl_soft,
  54336. + ipsp->ips_life.ipl_usetime.ipl_soft,
  54337. + ipsp->ips_life.ipl_packets.ipl_soft),
  54338. + extensions))
  54339. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54340. + SADB_EXT_ADDRESS_SRC,
  54341. + 0, /* ipsp->ips_said.proto, */
  54342. + 0,
  54343. + ipsp->ips_addr_s),
  54344. + extensions)
  54345. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54346. + SADB_EXT_ADDRESS_DST,
  54347. + 0, /* ipsp->ips_said.proto, */
  54348. + 0,
  54349. + ipsp->ips_addr_d),
  54350. + extensions))) {
  54351. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54352. + "failed to build the expire message extensions\n");
  54353. + spin_unlock(&tdb_lock);
  54354. + goto errlab;
  54355. + }
  54356. +
  54357. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54358. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54359. + "failed to build the expire message\n");
  54360. + SENDERR(-error);
  54361. + }
  54362. +
  54363. + for(pfkey_socketsp = pfkey_open_sockets;
  54364. + pfkey_socketsp;
  54365. + pfkey_socketsp = pfkey_socketsp->next) {
  54366. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54367. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54368. + "sending up expire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54369. + satype,
  54370. + satype2name(satype),
  54371. + pfkey_socketsp->socketp,
  54372. + error);
  54373. + SENDERR(-error);
  54374. + }
  54375. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54376. + "sending up expire message for satype=%d(%s) (proto=%d) to socket=0p%p succeeded.\n",
  54377. + satype,
  54378. + satype2name(satype),
  54379. + ipsp->ips_said.proto,
  54380. + pfkey_socketsp->socketp);
  54381. + }
  54382. +
  54383. + errlab:
  54384. + if (pfkey_msg) {
  54385. + pfkey_msg_free(&pfkey_msg);
  54386. + }
  54387. + pfkey_extensions_free(extensions);
  54388. + return error;
  54389. +}
  54390. +
  54391. +int
  54392. +pfkey_acquire(struct ipsec_sa *ipsp)
  54393. +{
  54394. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54395. + struct sadb_msg *pfkey_msg = NULL;
  54396. + struct socket_list *pfkey_socketsp;
  54397. + int error = 0;
  54398. + struct sadb_comb comb[] = {
  54399. + /* auth; encrypt; flags; */
  54400. + /* auth_minbits; auth_maxbits; encrypt_minbits; encrypt_maxbits; */
  54401. + /* reserved; soft_allocations; hard_allocations; soft_bytes; hard_bytes; */
  54402. + /* soft_addtime; hard_addtime; soft_usetime; hard_usetime; */
  54403. + /* soft_packets; hard_packets; */
  54404. + { SADB_AALG_MD5HMAC, SADB_EALG_3DESCBC, SADB_SAFLAGS_PFS,
  54405. + 128, 128, 168, 168,
  54406. + 0, 0, 0, 0, 0,
  54407. + 57600, 86400, 57600, 86400,
  54408. + 0, 0 },
  54409. + { SADB_AALG_SHA1HMAC, SADB_EALG_3DESCBC, SADB_SAFLAGS_PFS,
  54410. + 160, 160, 168, 168,
  54411. + 0, 0, 0, 0, 0,
  54412. + 57600, 86400, 57600, 86400,
  54413. + 0, 0 }
  54414. + };
  54415. +
  54416. + /* XXX This should not be hard-coded. It should be taken from the spdb */
  54417. + uint8_t satype = SADB_SATYPE_ESP;
  54418. +
  54419. + pfkey_extensions_init(extensions);
  54420. +
  54421. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  54422. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire: "
  54423. + "SAtype=%d unspecified or unknown.\n",
  54424. + satype);
  54425. + SENDERR(EINVAL);
  54426. + }
  54427. +
  54428. + if(!(pfkey_registered_sockets[satype])) {
  54429. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54430. + "no sockets registered for SAtype=%d(%s).\n",
  54431. + satype,
  54432. + satype2name(satype));
  54433. + SENDERR(EPROTONOSUPPORT);
  54434. + }
  54435. +
  54436. + if (!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions[0],
  54437. + SADB_ACQUIRE,
  54438. + satype,
  54439. + 0,
  54440. + ++pfkey_msg_seq,
  54441. + 0),
  54442. + extensions)
  54443. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54444. + SADB_EXT_ADDRESS_SRC,
  54445. + ipsp->ips_transport_protocol,
  54446. + 0,
  54447. + ipsp->ips_addr_s),
  54448. + extensions)
  54449. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54450. + SADB_EXT_ADDRESS_DST,
  54451. + ipsp->ips_transport_protocol,
  54452. + 0,
  54453. + ipsp->ips_addr_d),
  54454. + extensions)
  54455. +#if 0
  54456. + && (ipsp->ips_addr_p
  54457. + ? pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_PROXY],
  54458. + SADB_EXT_ADDRESS_PROXY,
  54459. + ipsp->ips_transport_protocol,
  54460. + 0,
  54461. + ipsp->ips_addr_p),
  54462. + extensions) : 1)
  54463. +#endif
  54464. + && (ipsp->ips_ident_s.type != SADB_IDENTTYPE_RESERVED
  54465. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions[SADB_EXT_IDENTITY_SRC],
  54466. + SADB_EXT_IDENTITY_SRC,
  54467. + ipsp->ips_ident_s.type,
  54468. + ipsp->ips_ident_s.id,
  54469. + ipsp->ips_ident_s.len,
  54470. + ipsp->ips_ident_s.data),
  54471. + extensions) : 1)
  54472. +
  54473. + && (ipsp->ips_ident_d.type != SADB_IDENTTYPE_RESERVED
  54474. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions[SADB_EXT_IDENTITY_DST],
  54475. + SADB_EXT_IDENTITY_DST,
  54476. + ipsp->ips_ident_d.type,
  54477. + ipsp->ips_ident_d.id,
  54478. + ipsp->ips_ident_d.len,
  54479. + ipsp->ips_ident_d.data),
  54480. + extensions) : 1)
  54481. +#if 0
  54482. + /* FIXME: This won't work yet because I have not finished
  54483. + it. */
  54484. + && (ipsp->ips_sens_
  54485. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions[SADB_EXT_SENSITIVITY],
  54486. + ipsp->ips_sens_dpd,
  54487. + ipsp->ips_sens_sens_level,
  54488. + ipsp->ips_sens_sens_len,
  54489. + ipsp->ips_sens_sens_bitmap,
  54490. + ipsp->ips_sens_integ_level,
  54491. + ipsp->ips_sens_integ_len,
  54492. + ipsp->ips_sens_integ_bitmap),
  54493. + extensions) : 1)
  54494. +#endif
  54495. + && pfkey_safe_build(error = pfkey_prop_build(&extensions[SADB_EXT_PROPOSAL],
  54496. + 64, /* replay */
  54497. + sizeof(comb)/sizeof(struct sadb_comb),
  54498. + &(comb[0])),
  54499. + extensions)
  54500. + )) {
  54501. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54502. + "failed to build the acquire message extensions\n");
  54503. + SENDERR(-error);
  54504. + }
  54505. +
  54506. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54507. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54508. + "failed to build the acquire message\n");
  54509. + SENDERR(-error);
  54510. + }
  54511. +
  54512. +#if KLIPS_PFKEY_ACQUIRE_LOSSAGE > 0
  54513. + if(sysctl_ipsec_regress_pfkey_lossage) {
  54514. + return(0);
  54515. + }
  54516. +#endif
  54517. +
  54518. + /* this should go to all registered sockets for that satype only */
  54519. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  54520. + pfkey_socketsp;
  54521. + pfkey_socketsp = pfkey_socketsp->next) {
  54522. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54523. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54524. + "sending up acquire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54525. + satype,
  54526. + satype2name(satype),
  54527. + pfkey_socketsp->socketp,
  54528. + error);
  54529. + SENDERR(-error);
  54530. + }
  54531. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire: "
  54532. + "sending up acquire message for satype=%d(%s) to socket=0p%p succeeded.\n",
  54533. + satype,
  54534. + satype2name(satype),
  54535. + pfkey_socketsp->socketp);
  54536. + }
  54537. +
  54538. + errlab:
  54539. + if (pfkey_msg) {
  54540. + pfkey_msg_free(&pfkey_msg);
  54541. + }
  54542. + pfkey_extensions_free(extensions);
  54543. + return error;
  54544. +}
  54545. +
  54546. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54547. +int
  54548. +pfkey_nat_t_new_mapping(struct ipsec_sa *ipsp, struct sockaddr *ipaddr,
  54549. + __u16 sport)
  54550. +{
  54551. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54552. + struct sadb_msg *pfkey_msg = NULL;
  54553. + struct socket_list *pfkey_socketsp;
  54554. + int error = 0;
  54555. + uint8_t satype = (ipsp->ips_said.proto==IPPROTO_ESP) ? SADB_SATYPE_ESP : 0;
  54556. +
  54557. + /* Construct SADB_X_NAT_T_NEW_MAPPING message */
  54558. +
  54559. + pfkey_extensions_init(extensions);
  54560. +
  54561. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  54562. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54563. + "SAtype=%d unspecified or unknown.\n",
  54564. + satype);
  54565. + SENDERR(EINVAL);
  54566. + }
  54567. +
  54568. + if(!(pfkey_registered_sockets[satype])) {
  54569. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54570. + "no sockets registered for SAtype=%d(%s).\n",
  54571. + satype,
  54572. + satype2name(satype));
  54573. + SENDERR(EPROTONOSUPPORT);
  54574. + }
  54575. +
  54576. + if (!(pfkey_safe_build
  54577. + (error = pfkey_msg_hdr_build(&extensions[0], SADB_X_NAT_T_NEW_MAPPING,
  54578. + satype, 0, ++pfkey_msg_seq, 0), extensions)
  54579. + /* SA */
  54580. + && pfkey_safe_build
  54581. + (error = pfkey_sa_build(&extensions[SADB_EXT_SA],
  54582. + SADB_EXT_SA, ipsp->ips_said.spi, 0, 0, 0, 0, 0), extensions)
  54583. + /* ADDRESS_SRC = old addr */
  54584. + && pfkey_safe_build
  54585. + (error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54586. + SADB_EXT_ADDRESS_SRC, ipsp->ips_said.proto, 0, ipsp->ips_addr_s),
  54587. + extensions)
  54588. + /* NAT_T_SPORT = old port */
  54589. + && pfkey_safe_build
  54590. + (error = pfkey_x_nat_t_port_build(&extensions[SADB_X_EXT_NAT_T_SPORT],
  54591. + SADB_X_EXT_NAT_T_SPORT, ipsp->ips_natt_sport), extensions)
  54592. + /* ADDRESS_DST = new addr */
  54593. + && pfkey_safe_build
  54594. + (error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54595. + SADB_EXT_ADDRESS_DST, ipsp->ips_said.proto, 0, ipaddr), extensions)
  54596. + /* NAT_T_DPORT = new port */
  54597. + && pfkey_safe_build
  54598. + (error = pfkey_x_nat_t_port_build(&extensions[SADB_X_EXT_NAT_T_DPORT],
  54599. + SADB_X_EXT_NAT_T_DPORT, sport), extensions)
  54600. + )) {
  54601. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54602. + "failed to build the nat_t_new_mapping message extensions\n");
  54603. + SENDERR(-error);
  54604. + }
  54605. +
  54606. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54607. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54608. + "failed to build the nat_t_new_mapping message\n");
  54609. + SENDERR(-error);
  54610. + }
  54611. +
  54612. + /* this should go to all registered sockets for that satype only */
  54613. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  54614. + pfkey_socketsp;
  54615. + pfkey_socketsp = pfkey_socketsp->next) {
  54616. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54617. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54618. + "sending up nat_t_new_mapping message for satype=%d(%s) to socket=%p failed with error=%d.\n",
  54619. + satype,
  54620. + satype2name(satype),
  54621. + pfkey_socketsp->socketp,
  54622. + error);
  54623. + SENDERR(-error);
  54624. + }
  54625. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54626. + "sending up nat_t_new_mapping message for satype=%d(%s) to socket=%p succeeded.\n",
  54627. + satype,
  54628. + satype2name(satype),
  54629. + pfkey_socketsp->socketp);
  54630. + }
  54631. +
  54632. + errlab:
  54633. + if (pfkey_msg) {
  54634. + pfkey_msg_free(&pfkey_msg);
  54635. + }
  54636. + pfkey_extensions_free(extensions);
  54637. + return error;
  54638. +}
  54639. +
  54640. +DEBUG_NO_STATIC int
  54641. +pfkey_x_nat_t_new_mapping_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54642. +{
  54643. + /* SADB_X_NAT_T_NEW_MAPPING not used in kernel */
  54644. + return -EINVAL;
  54645. +}
  54646. +#endif
  54647. +
  54648. +DEBUG_NO_STATIC int (*ext_processors[SADB_EXT_MAX+1])(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr) =
  54649. +{
  54650. + NULL, /* pfkey_msg_process, */
  54651. + pfkey_sa_process,
  54652. + pfkey_lifetime_process,
  54653. + pfkey_lifetime_process,
  54654. + pfkey_lifetime_process,
  54655. + pfkey_address_process,
  54656. + pfkey_address_process,
  54657. + pfkey_address_process,
  54658. + pfkey_key_process,
  54659. + pfkey_key_process,
  54660. + pfkey_ident_process,
  54661. + pfkey_ident_process,
  54662. + pfkey_sens_process,
  54663. + pfkey_prop_process,
  54664. + pfkey_supported_process,
  54665. + pfkey_supported_process,
  54666. + pfkey_spirange_process,
  54667. + pfkey_x_kmprivate_process,
  54668. + pfkey_x_satype_process,
  54669. + pfkey_sa_process,
  54670. + pfkey_address_process,
  54671. + pfkey_address_process,
  54672. + pfkey_address_process,
  54673. + pfkey_address_process,
  54674. + pfkey_address_process,
  54675. + pfkey_x_debug_process,
  54676. + pfkey_x_protocol_process
  54677. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54678. + ,
  54679. + pfkey_x_nat_t_type_process,
  54680. + pfkey_x_nat_t_port_process,
  54681. + pfkey_x_nat_t_port_process,
  54682. + pfkey_address_process
  54683. +#endif
  54684. +};
  54685. +
  54686. +
  54687. +DEBUG_NO_STATIC int (*msg_parsers[SADB_MAX +1])(struct sock *sk, struct sadb_ext *extensions[], struct pfkey_extracted_data* extr)
  54688. + =
  54689. +{
  54690. + NULL, /* RESERVED */
  54691. + pfkey_getspi_parse,
  54692. + pfkey_update_parse,
  54693. + pfkey_add_parse,
  54694. + pfkey_delete_parse,
  54695. + pfkey_get_parse,
  54696. + pfkey_acquire_parse,
  54697. + pfkey_register_parse,
  54698. + pfkey_expire_parse,
  54699. + pfkey_flush_parse,
  54700. + pfkey_dump_parse,
  54701. + pfkey_x_promisc_parse,
  54702. + pfkey_x_pchange_parse,
  54703. + pfkey_x_grpsa_parse,
  54704. + pfkey_x_addflow_parse,
  54705. + pfkey_x_delflow_parse,
  54706. + pfkey_x_msg_debug_parse
  54707. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54708. + , pfkey_x_nat_t_new_mapping_parse
  54709. +#endif
  54710. +};
  54711. +
  54712. +int
  54713. +pfkey_build_reply(struct sadb_msg *pfkey_msg,
  54714. + struct pfkey_extracted_data *extr,
  54715. + struct sadb_msg **pfkey_reply)
  54716. +{
  54717. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54718. + int error = 0;
  54719. + int msg_type = pfkey_msg->sadb_msg_type;
  54720. + int seq = pfkey_msg->sadb_msg_seq;
  54721. +
  54722. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54723. + "building reply with type: %d\n",
  54724. + msg_type);
  54725. + pfkey_extensions_init(extensions);
  54726. + if (!extr || !extr->ips) {
  54727. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54728. + "bad ipsec_sa passed\n");
  54729. + return EINVAL;
  54730. + }
  54731. + error = pfkey_safe_build(pfkey_msg_hdr_build(&extensions[0],
  54732. + msg_type,
  54733. + proto2satype(extr->ips->ips_said.proto),
  54734. + 0,
  54735. + seq,
  54736. + pfkey_msg->sadb_msg_pid),
  54737. + extensions) &&
  54738. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54739. + 1 << SADB_EXT_SA)
  54740. + || pfkey_safe_build(pfkey_sa_ref_build(&extensions[SADB_EXT_SA],
  54741. + SADB_EXT_SA,
  54742. + extr->ips->ips_said.spi,
  54743. + extr->ips->ips_replaywin,
  54744. + extr->ips->ips_state,
  54745. + extr->ips->ips_authalg,
  54746. + extr->ips->ips_encalg,
  54747. + extr->ips->ips_flags,
  54748. + extr->ips->ips_ref),
  54749. + extensions)) &&
  54750. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54751. + 1 << SADB_EXT_LIFETIME_CURRENT)
  54752. + || pfkey_safe_build(pfkey_lifetime_build(&extensions
  54753. + [SADB_EXT_LIFETIME_CURRENT],
  54754. + SADB_EXT_LIFETIME_CURRENT,
  54755. + extr->ips->ips_life.ipl_allocations.ipl_count,
  54756. + extr->ips->ips_life.ipl_bytes.ipl_count,
  54757. + extr->ips->ips_life.ipl_addtime.ipl_count,
  54758. + extr->ips->ips_life.ipl_usetime.ipl_count,
  54759. + extr->ips->ips_life.ipl_packets.ipl_count),
  54760. + extensions)) &&
  54761. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54762. + 1 << SADB_EXT_ADDRESS_SRC)
  54763. + || pfkey_safe_build(pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54764. + SADB_EXT_ADDRESS_SRC,
  54765. + extr->ips->ips_said.proto,
  54766. + 0,
  54767. + extr->ips->ips_addr_s),
  54768. + extensions)) &&
  54769. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54770. + 1 << SADB_EXT_ADDRESS_DST)
  54771. + || pfkey_safe_build(pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54772. + SADB_EXT_ADDRESS_DST,
  54773. + extr->ips->ips_said.proto,
  54774. + 0,
  54775. + extr->ips->ips_addr_d),
  54776. + extensions));
  54777. +
  54778. + if (error == 0) {
  54779. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54780. + "building extensions failed\n");
  54781. + return EINVAL;
  54782. + }
  54783. +
  54784. + KLIPS_PRINT(debug_pfkey,
  54785. + "klips_debug:pfkey_build_reply: "
  54786. + "built extensions, proceed to build the message\n");
  54787. + KLIPS_PRINT(debug_pfkey,
  54788. + "klips_debug:pfkey_build_reply: "
  54789. + "extensions[1]=0p%p\n",
  54790. + extensions[1]);
  54791. + error = pfkey_msg_build(pfkey_reply, extensions, EXT_BITS_OUT);
  54792. + pfkey_extensions_free(extensions);
  54793. +
  54794. + return error;
  54795. +}
  54796. +
  54797. +int
  54798. +pfkey_msg_interp(struct sock *sk, struct sadb_msg *pfkey_msg,
  54799. + struct sadb_msg **pfkey_reply)
  54800. +{
  54801. + int error = 0;
  54802. + int i;
  54803. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54804. + struct pfkey_extracted_data extr = {NULL, NULL, NULL};
  54805. +
  54806. + pfkey_extensions_init(extensions);
  54807. + KLIPS_PRINT(debug_pfkey,
  54808. + "klips_debug:pfkey_msg_interp: "
  54809. + "parsing message ver=%d, type=%d, errno=%d, satype=%d(%s), len=%d, res=%d, seq=%d, pid=%d.\n",
  54810. + pfkey_msg->sadb_msg_version,
  54811. + pfkey_msg->sadb_msg_type,
  54812. + pfkey_msg->sadb_msg_errno,
  54813. + pfkey_msg->sadb_msg_satype,
  54814. + satype2name(pfkey_msg->sadb_msg_satype),
  54815. + pfkey_msg->sadb_msg_len,
  54816. + pfkey_msg->sadb_msg_reserved,
  54817. + pfkey_msg->sadb_msg_seq,
  54818. + pfkey_msg->sadb_msg_pid);
  54819. +
  54820. + extr.ips = ipsec_sa_alloc(&error); /* pass in error var by pointer */
  54821. + if(extr.ips == NULL) {
  54822. + KLIPS_PRINT(debug_pfkey,
  54823. + "klips_debug:pfkey_msg_interp: "
  54824. + "memory allocation error.\n");
  54825. + SENDERR(-error);
  54826. + }
  54827. +
  54828. + KLIPS_PRINT(debug_pfkey,
  54829. + "klips_debug:pfkey_msg_interp: "
  54830. + "allocated extr->ips=0p%p.\n",
  54831. + extr.ips);
  54832. +
  54833. + if(pfkey_msg->sadb_msg_satype > SADB_SATYPE_MAX) {
  54834. + KLIPS_PRINT(debug_pfkey,
  54835. + "klips_debug:pfkey_msg_interp: "
  54836. + "satype %d > max %d\n",
  54837. + pfkey_msg->sadb_msg_satype,
  54838. + SADB_SATYPE_MAX);
  54839. + SENDERR(EINVAL);
  54840. + }
  54841. +
  54842. + switch(pfkey_msg->sadb_msg_type) {
  54843. + case SADB_GETSPI:
  54844. + case SADB_UPDATE:
  54845. + case SADB_ADD:
  54846. + case SADB_DELETE:
  54847. + case SADB_X_GRPSA:
  54848. + case SADB_X_ADDFLOW:
  54849. + if(!(extr.ips->ips_said.proto = satype2proto(pfkey_msg->sadb_msg_satype))) {
  54850. + KLIPS_PRINT(debug_pfkey,
  54851. + "klips_debug:pfkey_msg_interp: "
  54852. + "satype %d lookup failed.\n",
  54853. + pfkey_msg->sadb_msg_satype);
  54854. + SENDERR(EINVAL);
  54855. + } else {
  54856. + KLIPS_PRINT(debug_pfkey,
  54857. + "klips_debug:pfkey_msg_interp: "
  54858. + "satype %d lookups to proto=%d.\n",
  54859. + pfkey_msg->sadb_msg_satype,
  54860. + extr.ips->ips_said.proto);
  54861. + }
  54862. + break;
  54863. + default:
  54864. + break;
  54865. + }
  54866. +
  54867. + /* The NULL below causes the default extension parsers to be used */
  54868. + /* Parse the extensions */
  54869. + if((error = pfkey_msg_parse(pfkey_msg, NULL, extensions, EXT_BITS_IN)))
  54870. + {
  54871. + KLIPS_PRINT(debug_pfkey,
  54872. + "klips_debug:pfkey_msg_interp: "
  54873. + "message parsing failed with error %d.\n",
  54874. + error);
  54875. + SENDERR(-error);
  54876. + }
  54877. +
  54878. + /* Process the extensions */
  54879. + for(i=1; i <= SADB_EXT_MAX;i++) {
  54880. + if(extensions[i] != NULL) {
  54881. + KLIPS_PRINT(debug_pfkey,
  54882. + "klips_debug:pfkey_msg_interp: "
  54883. + "processing ext %d 0p%p with processor 0p%p.\n",
  54884. + i, extensions[i], ext_processors[i]);
  54885. + if((error = ext_processors[i](extensions[i], &extr))) {
  54886. + KLIPS_PRINT(debug_pfkey,
  54887. + "klips_debug:pfkey_msg_interp: "
  54888. + "extension processing for type %d failed with error %d.\n",
  54889. + i,
  54890. + error);
  54891. + SENDERR(-error);
  54892. + }
  54893. +
  54894. + }
  54895. +
  54896. + }
  54897. +
  54898. + /* Parse the message types */
  54899. + KLIPS_PRINT(debug_pfkey,
  54900. + "klips_debug:pfkey_msg_interp: "
  54901. + "parsing message type %d(%s) with msg_parser 0p%p.\n",
  54902. + pfkey_msg->sadb_msg_type,
  54903. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type),
  54904. + msg_parsers[pfkey_msg->sadb_msg_type]);
  54905. + if((error = msg_parsers[pfkey_msg->sadb_msg_type](sk, extensions, &extr))) {
  54906. + KLIPS_PRINT(debug_pfkey,
  54907. + "klips_debug:pfkey_msg_interp: "
  54908. + "message parsing failed with error %d.\n",
  54909. + error);
  54910. + SENDERR(-error);
  54911. + }
  54912. +
  54913. +#if 0
  54914. + error = pfkey_build_reply(pfkey_msg, &extr, pfkey_reply);
  54915. + if (error) {
  54916. + *pfkey_reply = NULL;
  54917. + }
  54918. +#endif
  54919. + errlab:
  54920. + if(extr.ips != NULL) {
  54921. + ipsec_sa_wipe(extr.ips);
  54922. + }
  54923. + if(extr.ips2 != NULL) {
  54924. + ipsec_sa_wipe(extr.ips2);
  54925. + }
  54926. + if (extr.eroute != NULL) {
  54927. + kfree(extr.eroute);
  54928. + }
  54929. + return(error);
  54930. +}
  54931. +
  54932. +/*
  54933. + * $Log: pfkey_v2_parser.c,v $
  54934. + * Revision 1.134.2.2 2006/10/06 21:39:26 paul
  54935. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  54936. + * set. This is defined through autoconf.h which is included through the
  54937. + * linux kernel build macros.
  54938. + *
  54939. + * Revision 1.134.2.1 2006/05/01 14:37:25 mcr
  54940. + * ip_chk_addr -> inet_addr_type for more direct 2.4/2.6 support.
  54941. + *
  54942. + * Revision 1.134 2005/05/11 01:48:20 mcr
  54943. + * removed "poor-man"s OOP in favour of proper C structures.
  54944. + *
  54945. + * Revision 1.133 2005/04/29 05:10:22 mcr
  54946. + * removed from extraenous includes to make unit testing easier.
  54947. + *
  54948. + * Revision 1.132 2005/04/14 20:56:24 mcr
  54949. + * moved (pfkey_)ipsec_sa_init to ipsec_sa.c.
  54950. + *
  54951. + * Revision 1.131 2005/01/26 00:50:35 mcr
  54952. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  54953. + * and make sure that NAT_TRAVERSAL is set as well to match
  54954. + * userspace compiles of code.
  54955. + *
  54956. + * Revision 1.130 2004/09/08 17:21:36 ken
  54957. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  54958. + *
  54959. + * Revision 1.129 2004/09/06 18:36:30 mcr
  54960. + * if a protocol can not be found, then log it. This is not
  54961. + * debugging.
  54962. + *
  54963. + * Revision 1.128 2004/08/21 00:45:19 mcr
  54964. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  54965. + *
  54966. + * Revision 1.127 2004/08/20 21:45:45 mcr
  54967. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  54968. + * be 26sec compatible. But, some defines where changed.
  54969. + *
  54970. + * Revision 1.126 2004/08/17 03:27:23 mcr
  54971. + * klips 2.6 edits.
  54972. + *
  54973. + * Revision 1.125 2004/08/04 15:57:07 mcr
  54974. + * moved des .h files to include/des/ *
  54975. + * included 2.6 protocol specific things
  54976. + * started at NAT-T support, but it will require a kernel patch.
  54977. + *
  54978. + * Revision 1.124 2004/07/10 19:11:18 mcr
  54979. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  54980. + *
  54981. + * Revision 1.123 2004/04/06 02:49:26 mcr
  54982. + * pullup of algo code from alg-branch.
  54983. + *
  54984. + * Revision 1.122.2.2 2004/04/05 04:30:46 mcr
  54985. + * patches for alg-branch to compile/work with 2.x openswan
  54986. + *
  54987. + * Revision 1.122.2.1 2003/12/22 15:25:52 jjo
  54988. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  54989. + *
  54990. + * Revision 1.122 2003/12/10 01:14:27 mcr
  54991. + * NAT-traversal patches to KLIPS.
  54992. + *
  54993. + * Revision 1.121 2003/10/31 02:27:55 mcr
  54994. + * pulled up port-selector patches and sa_id elimination.
  54995. + *
  54996. + * Revision 1.120.4.2 2003/10/29 01:30:41 mcr
  54997. + * elimited "struct sa_id".
  54998. + *
  54999. + * Revision 1.120.4.1 2003/09/21 13:59:56 mcr
  55000. + * pre-liminary X.509 patch - does not yet pass tests.
  55001. + *
  55002. + * Revision 1.120 2003/04/03 17:38:09 rgb
  55003. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  55004. + *
  55005. + * Revision 1.119 2003/02/06 01:52:37 rgb
  55006. + * Removed no longer relevant comment
  55007. + *
  55008. + * Revision 1.118 2003/01/30 02:32:44 rgb
  55009. + *
  55010. + * Transmit error code through to caller from callee for better diagnosis of problems.
  55011. + *
  55012. + * Revision 1.117 2003/01/16 18:48:13 rgb
  55013. + *
  55014. + * Fixed sign bug in error return from an sa allocation call in
  55015. + * pfkey_msg_interp.
  55016. + *
  55017. + * Revision 1.116 2002/10/17 16:38:01 rgb
  55018. + * Change pfkey_alloc_eroute() to never static since its consumers
  55019. + * have been moved outside the file.
  55020. + *
  55021. + * Revision 1.115 2002/10/12 23:11:53 dhr
  55022. + *
  55023. + * [KenB + DHR] more 64-bit cleanup
  55024. + *
  55025. + * Revision 1.114 2002/10/05 05:02:58 dhr
  55026. + *
  55027. + * C labels go on statements
  55028. + *
  55029. + * Revision 1.113 2002/09/30 19:11:22 rgb
  55030. + * Turn on debugging for upgoing acquire messages to test for reliability.
  55031. + *
  55032. + * Revision 1.112 2002/09/20 15:41:16 rgb
  55033. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  55034. + * Added sadb_x_sa_ref to struct sadb_sa.
  55035. + * Added ref parameter to pfkey_sa_build().
  55036. + *
  55037. + * Revision 1.111 2002/09/20 05:02:08 rgb
  55038. + * Added memory allocation debugging.
  55039. + * Convert to switch to divulge hmac keys for debugging.
  55040. + * Added text labels to elucidate numeric values presented.
  55041. + *
  55042. + * Revision 1.110 2002/08/03 18:03:05 mcr
  55043. + * loop that checks for SPI's to have been already linked
  55044. + * fails to actually step to next pointer, but continuously
  55045. + * resets to head of list. Wrong pointer used.
  55046. + * test east-icmp-02 revealed this.
  55047. + *
  55048. + * Revision 1.109 2002/07/26 08:48:31 rgb
  55049. + * Added SA ref table code.
  55050. + *
  55051. + * Revision 1.108 2002/05/27 18:55:03 rgb
  55052. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  55053. + *
  55054. + * Revision 1.107 2002/05/23 07:16:08 rgb
  55055. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  55056. + * Pointer clean-up.
  55057. + * Added refcount code.
  55058. + *
  55059. + * Revision 1.106 2002/05/14 02:34:13 rgb
  55060. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  55061. + * with "put" usage in the kernel.
  55062. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  55063. + * ipsec_sa or ipsec_sa.
  55064. + * Moved all the extension parsing functions to pfkey_v2_ext_process.c.
  55065. + *
  55066. + * Revision 1.105 2002/04/24 07:55:32 mcr
  55067. + * #include patches and Makefiles for post-reorg compilation.
  55068. + *
  55069. + * Revision 1.104 2002/04/24 07:36:34 mcr
  55070. + * Moved from ./klips/net/ipsec/pfkey_v2_parser.c,v
  55071. + *
  55072. + * Revision 1.103 2002/04/20 00:12:25 rgb
  55073. + * Added esp IV CBC attack fix, disabled.
  55074. + *
  55075. + * Revision 1.102 2002/03/08 01:15:17 mcr
  55076. + * put some internal structure only debug messages behind
  55077. + * && sysctl_ipsec_debug_verbose.
  55078. + *
  55079. + * Revision 1.101 2002/01/29 17:17:57 mcr
  55080. + * moved include of ipsec_param.h to after include of linux/kernel.h
  55081. + * otherwise, it seems that some option that is set in ipsec_param.h
  55082. + * screws up something subtle in the include path to kernel.h, and
  55083. + * it complains on the snprintf() prototype.
  55084. + *
  55085. + * Revision 1.100 2002/01/29 04:00:54 mcr
  55086. + * more excise of kversions.h header.
  55087. + *
  55088. + * Revision 1.99 2002/01/29 02:13:19 mcr
  55089. + * introduction of ipsec_kversion.h means that include of
  55090. + * ipsec_param.h must preceed any decisions about what files to
  55091. + * include to deal with differences in kernel source.
  55092. + *
  55093. + * Revision 1.98 2002/01/12 02:57:57 mcr
  55094. + * first regression test causes acquire messages to be lost
  55095. + * 100% of the time. This is to help testing of pluto.
  55096. + *
  55097. + * Revision 1.97 2001/11/26 09:23:52 rgb
  55098. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  55099. + *
  55100. + * Revision 1.93.2.4 2001/10/23 04:20:27 mcr
  55101. + * parity was forced on wrong structure! prototypes help here.
  55102. + *
  55103. + * Revision 1.93.2.3 2001/10/22 21:14:59 mcr
  55104. + * include des.h, removed phony prototypes and fixed calling
  55105. + * conventions to match real prototypes.
  55106. + *
  55107. + * Revision 1.93.2.2 2001/10/15 05:39:03 mcr
  55108. + * %08lx is not the right format for u32. Use %08x. 64-bit safe? ha.
  55109. + *
  55110. + * Revision 1.93.2.1 2001/09/25 02:30:14 mcr
  55111. + * struct tdb -> struct ipsec_sa.
  55112. + * use new lifetime structure. common format routines for debug.
  55113. + *
  55114. + * Revision 1.96 2001/11/06 20:47:54 rgb
  55115. + * Fixed user context call to ipsec_dev_start_xmit() bug. Call
  55116. + * dev_queue_xmit() instead.
  55117. + *
  55118. + * Revision 1.95 2001/11/06 19:47:46 rgb
  55119. + * Added packet parameter to lifetime and comb structures.
  55120. + *
  55121. + * Revision 1.94 2001/10/18 04:45:23 rgb
  55122. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  55123. + * lib/freeswan.h version macros moved to lib/kversions.h.
  55124. + * Other compiler directive cleanups.
  55125. + *
  55126. + * Revision 1.93 2001/09/20 15:32:59 rgb
  55127. + * Min/max cleanup.
  55128. + *
  55129. + * Revision 1.92 2001/09/19 16:35:48 rgb
  55130. + * PF_KEY ident fix for getspi from NetCelo (puttdb duplication).
  55131. + *
  55132. + * Revision 1.91 2001/09/15 16:24:06 rgb
  55133. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  55134. + *
  55135. + * Revision 1.90 2001/09/14 16:58:38 rgb
  55136. + * Added support for storing the first and last packets through a HOLD.
  55137. + *
  55138. + * Revision 1.89 2001/09/08 21:14:07 rgb
  55139. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  55140. + * Better state coherency (error management) between pf_key and IKE daemon.
  55141. + * (NetCelo)
  55142. + *
  55143. + * Revision 1.88 2001/08/27 19:42:44 rgb
  55144. + * Fix memory leak of encrypt and auth structs in pfkey register.
  55145. + *
  55146. + * Revision 1.87 2001/07/06 19:50:46 rgb
  55147. + * Removed unused debugging code.
  55148. + * Added inbound policy checking code for IPIP SAs.
  55149. + *
  55150. + * Revision 1.86 2001/06/20 06:26:04 rgb
  55151. + * Changed missing SA errors from EEXIST to ENOENT and added debug output
  55152. + * for already linked SAs.
  55153. + *
  55154. + * Revision 1.85 2001/06/15 04:57:02 rgb
  55155. + * Remove single error return condition check and check for all errors in
  55156. + * the case of a replace eroute delete operation. This means that
  55157. + * applications must expect to be deleting something before replacing it
  55158. + * and if nothing is found, complain.
  55159. + *
  55160. + * Revision 1.84 2001/06/14 19:35:12 rgb
  55161. + * Update copyright date.
  55162. + *
  55163. + * Revision 1.83 2001/06/12 00:03:19 rgb
  55164. + * Silence debug set/unset under normal conditions.
  55165. + *
  55166. + * Revision 1.82 2001/05/30 08:14:04 rgb
  55167. + * Removed vestiges of esp-null transforms.
  55168. + *
  55169. + * Revision 1.81 2001/05/27 06:12:12 rgb
  55170. + * Added structures for pid, packet count and last access time to eroute.
  55171. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  55172. + *
  55173. + * Revision 1.80 2001/05/03 19:43:59 rgb
  55174. + * Check error return codes for all build function calls.
  55175. + * Standardise on SENDERR() macro.
  55176. + *
  55177. + * Revision 1.79 2001/04/20 21:09:16 rgb
  55178. + * Cleaned up fixed tdbwipes.
  55179. + * Free pfkey_reply and clean up extensions_reply for grpsa, addflow and
  55180. + * delflow (Per Cederqvist) plugging memleaks.
  55181. + *
  55182. + * Revision 1.78 2001/04/19 19:02:39 rgb
  55183. + * Fixed extr.tdb freeing, stealing it for getspi, update and add.
  55184. + * Refined a couple of spinlocks, fixed the one in update.
  55185. + *
  55186. + * Revision 1.77 2001/04/18 20:26:16 rgb
  55187. + * Wipe/free eroute and both tdbs from extr at end of pfkey_msg_interp()
  55188. + * instead of inside each message type parser. This fixes two memleaks.
  55189. + *
  55190. + * Revision 1.76 2001/04/17 23:51:18 rgb
  55191. + * Quiet down pfkey_x_debug_process().
  55192. + *
  55193. + * Revision 1.75 2001/03/29 01:55:05 rgb
  55194. + * Fixed pfkey key init memleak.
  55195. + * Fixed pfkey encryption key debug output.
  55196. + *
  55197. + * Revision 1.74 2001/03/27 05:29:14 rgb
  55198. + * Debug output cleanup/silencing.
  55199. + *
  55200. + * Revision 1.73 2001/02/28 05:03:28 rgb
  55201. + * Clean up and rationalise startup messages.
  55202. + *
  55203. + * Revision 1.72 2001/02/27 22:24:56 rgb
  55204. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  55205. + * Check for satoa() return codes.
  55206. + *
  55207. + * Revision 1.71 2001/02/27 06:59:30 rgb
  55208. + * Added satype2name() conversions most places satype is debug printed.
  55209. + *
  55210. + * Revision 1.70 2001/02/26 22:37:08 rgb
  55211. + * Fixed 'unknown proto' INT bug in new code.
  55212. + * Added satype to protocol debugging instrumentation.
  55213. + *
  55214. + * Revision 1.69 2001/02/26 19:57:51 rgb
  55215. + * Re-formatted debug output (split lines, consistent spacing).
  55216. + * Fixed as yet undetected FLUSH bug which called ipsec_tdbcleanup()
  55217. + * with an satype instead of proto.
  55218. + * Checked for satype consistency and fixed minor bugs.
  55219. + * Fixed undetected ungrpspi bug that tried to upmsg a second tdb.
  55220. + * Check for satype sanity in pfkey_expire().
  55221. + * Added satype sanity check to addflow.
  55222. + *
  55223. + * Revision 1.68 2001/02/12 23:14:40 rgb
  55224. + * Remove double spin lock in pfkey_expire().
  55225. + *
  55226. + * Revision 1.67 2001/01/31 19:23:40 rgb
  55227. + * Fixed double-unlock bug introduced by grpsa upmsg (found by Lars Heete).
  55228. + *
  55229. + * Revision 1.66 2001/01/29 22:20:04 rgb
  55230. + * Fix minor add upmsg lifetime bug.
  55231. + *
  55232. + * Revision 1.65 2001/01/24 06:12:33 rgb
  55233. + * Fixed address extension compile bugs just introduced.
  55234. + *
  55235. + * Revision 1.64 2001/01/24 00:31:15 rgb
  55236. + * Added upmsg for addflow/delflow.
  55237. + *
  55238. + * Revision 1.63 2001/01/23 22:02:55 rgb
  55239. + * Added upmsg to x_grpsa.
  55240. + * Fixed lifetimes extentions to add/update/get upmsg.
  55241. + *
  55242. + * Revision 1.62 2000/11/30 21:47:51 rgb
  55243. + * Fix error return bug after returning from pfkey_tdb_init().
  55244. + *
  55245. + * Revision 1.61 2000/11/17 18:10:29 rgb
  55246. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  55247. + * network byte order since this is the way PF_KEYv2 stored spis.
  55248. + *
  55249. + * Revision 1.60 2000/11/06 04:34:53 rgb
  55250. + * Changed non-exported functions to DEBUG_NO_STATIC.
  55251. + * Add Svenning's adaptive content compression.
  55252. + * Ditched spin_lock_irqsave in favour of spin_lock/_bh.
  55253. + * Fixed double unlock bug (Svenning).
  55254. + * Fixed pfkey_msg uninitialized bug in pfkey_{expire,acquire}().
  55255. + * Fixed incorrect extension type (prop) in pfkey)acquire().
  55256. + *
  55257. + * Revision 1.59 2000/10/11 15:25:12 rgb
  55258. + * Fixed IPCOMP disabled compile bug.
  55259. + *
  55260. + * Revision 1.58 2000/10/11 14:54:03 rgb
  55261. + * Fixed pfkey_acquire() satype to SADB_SATYPE_ESP and removed pfkey
  55262. + * protocol violations of setting pfkey_address_build() protocol parameter
  55263. + * to non-zero except in the case of pfkey_acquire().
  55264. + *
  55265. + * Revision 1.57 2000/10/10 20:10:18 rgb
  55266. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  55267. + *
  55268. + * Revision 1.56 2000/10/06 20:24:36 rgb
  55269. + * Fixes to pfkey_acquire to initialize extensions[] and use correct
  55270. + * ipproto.
  55271. + *
  55272. + * Revision 1.55 2000/10/03 03:20:57 rgb
  55273. + * Added brackets to get a?b:c scope right for pfkey_register reply.
  55274. + *
  55275. + * Revision 1.54 2000/09/29 19:49:30 rgb
  55276. + * As-yet-unused-bits cleanup.
  55277. + *
  55278. + * Revision 1.53 2000/09/28 00:35:45 rgb
  55279. + * Padded SATYPE printout in pfkey_register for vertical alignment.
  55280. + *
  55281. + * Revision 1.52 2000/09/20 16:21:58 rgb
  55282. + * Cleaned up ident string alloc/free.
  55283. + *
  55284. + * Revision 1.51 2000/09/20 04:04:20 rgb
  55285. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  55286. + * oopsen.
  55287. + *
  55288. + * Revision 1.50 2000/09/16 01:10:53 rgb
  55289. + * Fixed unused var warning with debug off.
  55290. + *
  55291. + * Revision 1.49 2000/09/15 11:37:02 rgb
  55292. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  55293. + * IPCOMP zlib deflate code.
  55294. + *
  55295. + * Revision 1.48 2000/09/15 04:57:57 rgb
  55296. + * Cleaned up existing IPCOMP code before svenning addition.
  55297. + * Initialize pfkey_reply and extensions_reply in case of early error in
  55298. + * message parsing functions (thanks Kai!).
  55299. + *
  55300. + * Revision 1.47 2000/09/13 08:02:56 rgb
  55301. + * Added KMd registration notification.
  55302. + *
  55303. + * Revision 1.46 2000/09/12 22:35:36 rgb
  55304. + * Restructured to remove unused extensions from CLEARFLOW messages.
  55305. + *
  55306. + * Revision 1.45 2000/09/12 03:24:23 rgb
  55307. + * Converted #if0 debugs to sysctl.
  55308. + *
  55309. + * Revision 1.44 2000/09/09 06:38:39 rgb
  55310. + * Correct SADB message type for update, add and delete.
  55311. + *
  55312. + * Revision 1.43 2000/09/08 19:19:56 rgb
  55313. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  55314. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  55315. + * Put in sanity checks in most msg type parsers to catch invalid satypes
  55316. + * and empty socket lists.
  55317. + * Moved spin-locks in pfkey_get_parse() to simplify.
  55318. + * Added pfkey_acquire().
  55319. + * Added upwards messages to update, add, delete, acquire_parse,
  55320. + * expire_parse and flush.
  55321. + * Fix pfkey_prop_build() parameter to be only single indirection.
  55322. + * Changed all replies to use pfkey_reply.
  55323. + * Check return code on puttdb() and deltdbchain() in getspi, update,
  55324. + * add, delete.
  55325. + * Fixed up all pfkey replies to open and registered sockets.
  55326. + *
  55327. + * Revision 1.42 2000/09/01 18:50:26 rgb
  55328. + * Added a supported algorithms array lists, one per satype and registered
  55329. + * existing algorithms.
  55330. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  55331. + * list.
  55332. + * Only send pfkey_expire() messages to sockets registered for that satype.
  55333. + * Added reply to pfkey_getspi_parse().
  55334. + * Added reply to pfkey_get_parse().
  55335. + * Fixed debug output label bug in pfkey_lifetime_process().
  55336. + * Cleaned up pfkey_sa_process a little.
  55337. + * Moved pfkey_safe_build() above message type parsers to make it available
  55338. + * for creating replies.
  55339. + * Added comments for future work in pfkey_acquire_parse().
  55340. + * Fleshed out guts of pfkey_register_parse().
  55341. + *
  55342. + * Revision 1.41 2000/08/24 16:58:11 rgb
  55343. + * Fixed key debugging variables.
  55344. + * Fixed error return code for a failed search.
  55345. + * Changed order of pfkey_get operations.
  55346. + *
  55347. + * Revision 1.40 2000/08/21 16:32:27 rgb
  55348. + * Re-formatted for cosmetic consistency and readability.
  55349. + *
  55350. + * Revision 1.39 2000/08/20 21:38:57 rgb
  55351. + * Bugfixes to as-yet-unused pfkey_update_parse() and
  55352. + * pfkey_register_parse(). (Momchil)
  55353. + * Added functions pfkey_safe_build(), pfkey_expire() and
  55354. + * pfkey_build_reply(). (Momchil)
  55355. + * Added a pfkey_reply parameter to pfkey_msg_interp(). (Momchil)
  55356. + *
  55357. + * Revision 1.38 2000/08/18 21:30:41 rgb
  55358. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  55359. + *
  55360. + * Revision 1.37 2000/08/18 18:18:02 rgb
  55361. + * Cosmetic and descriptive changes made to debug test.
  55362. + * getspi and update fixes from Momchil.
  55363. + *
  55364. + * Revision 1.36 2000/08/15 15:41:55 rgb
  55365. + * Fixed the (as yet unused and untested) pfkey_getspi() routine.
  55366. + *
  55367. + * Revision 1.35 2000/08/01 14:51:52 rgb
  55368. + * Removed _all_ remaining traces of DES.
  55369. + *
  55370. + * Revision 1.34 2000/07/28 14:58:32 rgb
  55371. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  55372. + *
  55373. + * Revision 1.33 2000/06/28 05:50:11 rgb
  55374. + * Actually set iv_bits.
  55375. + *
  55376. + * Revision 1.32 2000/05/30 18:36:56 rgb
  55377. + * Fix AH auth hash setup bug. This breaks interop with previous PF_KEY
  55378. + * FreeS/WAN, but fixes interop with other implementations.
  55379. + *
  55380. + * Revision 1.31 2000/03/16 14:05:48 rgb
  55381. + * Fixed brace scope preventing non-debug compile.
  55382. + * Added null parameter check for pfkey_x_debug().
  55383. + *
  55384. + * Revision 1.30 2000/01/22 23:21:13 rgb
  55385. + * Use new function satype2proto().
  55386. + *
  55387. + * Revision 1.29 2000/01/22 08:40:21 rgb
  55388. + * Invert condition to known value to avoid AF_INET6 in 2.0.36.
  55389. + *
  55390. + * Revision 1.28 2000/01/22 07:58:57 rgb
  55391. + * Fixed REPLACEFLOW bug, missing braces around KLIPS_PRINT *and* SENDERR.
  55392. + *
  55393. + * Revision 1.27 2000/01/22 03:48:01 rgb
  55394. + * Added extr pointer component debugging.
  55395. + *
  55396. + * Revision 1.26 2000/01/21 09:41:25 rgb
  55397. + * Changed a (void*) to (char*) cast to do proper pointer math.
  55398. + * Don't call tdbwipe if tdb2 is NULL.
  55399. + *
  55400. + * Revision 1.25 2000/01/21 06:21:01 rgb
  55401. + * Added address cases for eroute flows.
  55402. + * Tidied up compiler directive indentation for readability.
  55403. + * Added ictx,octx vars for simplification.
  55404. + * Added macros for HMAC padding magic numbers.
  55405. + * Converted from double tdb arguments to one structure (extr)
  55406. + * containing pointers to all temporary information structures
  55407. + * and checking for valid arguments to all ext processors and
  55408. + * msg type parsers.
  55409. + * Added spiungrp'ing.
  55410. + * Added klipsdebug switching capability.
  55411. + * Removed sa_process() check for zero protocol.
  55412. + * Added address case for DST2 for grouping.
  55413. + * Added/changed minor debugging instrumentation.
  55414. + * Fixed spigrp for single said, ungrouping case.
  55415. + * Added code to parse addflow and delflow messages.
  55416. + * Removed redundant statements duplicating tdbwipe() functionality
  55417. + * and causing double kfrees.
  55418. + * Permit addflow to have a protocol of 0.
  55419. + *
  55420. + * Revision 1.24 1999/12/09 23:23:00 rgb
  55421. + * Added check to pfkey_sa_process() to do eroutes.
  55422. + * Converted to DIVUP() macro.
  55423. + * Converted if() to switch() in pfkey_register_parse().
  55424. + * Use new pfkey_extensions_init() instead of memset().
  55425. + *
  55426. + * Revision 1.23 1999/12/01 22:18:13 rgb
  55427. + * Preset minspi and maxspi values in case and spirange extension is not
  55428. + * included and check for the presence of an spirange extension before
  55429. + * using it. Initialise tdb_sastate to LARVAL.
  55430. + * Fixed debugging output typo.
  55431. + * Fixed authentication context initialisation bugs (4 places).
  55432. + *
  55433. + * Revision 1.22 1999/11/27 11:53:08 rgb
  55434. + * Moved pfkey_msg_parse prototype to pfkey.h
  55435. + * Moved exts_permitted/required prototype to pfkey.h.
  55436. + * Moved sadb_satype2proto protocol lookup table to lib/pfkey_v2_parse.c.
  55437. + * Deleted SADB_X_EXT_SA2 code from pfkey_sa_process() since it will never
  55438. + * be called.
  55439. + * Moved protocol/algorithm checks to lib/pfkey_v2_parse.c
  55440. + * Debugging error messages added.
  55441. + * Enable lifetime_current checking.
  55442. + * Remove illegal requirement for SA extension to be present in an
  55443. + * originating GETSPI call.
  55444. + * Re-instate requirement for UPDATE or ADD message to be MATURE.
  55445. + * Add argument to pfkey_msg_parse() for direction.
  55446. + * Fixed IPIP dst address bug and purged redundant, leaky code.
  55447. + *
  55448. + * Revision 1.21 1999/11/24 05:24:20 rgb
  55449. + * hanged 'void*extensions' to 'struct sadb_ext*extensions'.
  55450. + * Fixed indention.
  55451. + * Ditched redundant replay check.
  55452. + * Fixed debug message text from 'parse' to 'process'.
  55453. + * Added more debug output.
  55454. + * Forgot to zero extensions array causing bug, fixed.
  55455. + *
  55456. + * Revision 1.20 1999/11/23 23:08:13 rgb
  55457. + * Move all common parsing code to lib/pfkey_v2_parse.c and rename
  55458. + * remaining bits to *_process. (PJO)
  55459. + * Add macros for dealing with alignment and rounding up more opaquely.
  55460. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  55461. + * Sort out pfkey and freeswan headers, putting them in a library path.
  55462. + * Corrected a couple of bugs in as-yet-inactive code.
  55463. + *
  55464. + * Revision 1.19 1999/11/20 22:01:10 rgb
  55465. + * Add more descriptive error messages for non-zero reserved fields.
  55466. + * Add more descriptive error message for spirange parsing.
  55467. + * Start on supported extension parsing.
  55468. + * Start on register and get message parsing.
  55469. + *
  55470. + * Revision 1.18 1999/11/18 04:09:20 rgb
  55471. + * Replaced all kernel version macros to shorter, readable form.
  55472. + *
  55473. + * Revision 1.17 1999/11/17 15:53:41 rgb
  55474. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  55475. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  55476. + * klips/net/ipsec/Makefile.
  55477. + *
  55478. + * Revision 1.16 1999/10/26 16:57:43 rgb
  55479. + * Add shorter macros for compiler directives to visually clean-up.
  55480. + * Give ipv6 code meaningful compiler directive.
  55481. + * Add comments to other #if 0 debug code.
  55482. + * Remove unused *_bh_atomic() calls.
  55483. + * Fix mis-placed spinlock.
  55484. + *
  55485. + * Revision 1.15 1999/10/16 18:27:10 rgb
  55486. + * Clean-up unused cruft.
  55487. + * Fix-up lifetime_allocations_c and lifetime_addtime_c initialisations.
  55488. + *
  55489. + * Revision 1.14 1999/10/08 18:37:34 rgb
  55490. + * Fix end-of-line spacing to sate whining PHMs.
  55491. + *
  55492. + * Revision 1.13 1999/10/03 18:49:12 rgb
  55493. + * Spinlock fixes for 2.0.xx and 2.3.xx.
  55494. + *
  55495. + * Revision 1.12 1999/10/01 15:44:54 rgb
  55496. + * Move spinlock header include to 2.1> scope.
  55497. + *
  55498. + * Revision 1.11 1999/10/01 00:05:45 rgb
  55499. + * Added tdb structure locking.
  55500. + * Use 'jiffies' instead of do_get_timeofday().
  55501. + * Fix lifetime assignments.
  55502. + *
  55503. + * Revision 1.10 1999/09/21 15:24:45 rgb
  55504. + * Rework spirange code to save entropy and prevent endless loops.
  55505. + *
  55506. + * Revision 1.9 1999/09/16 12:10:21 rgb
  55507. + * Minor fixes to random spi selection for correctness and entropy conservation.
  55508. + *
  55509. + * Revision 1.8 1999/05/25 22:54:46 rgb
  55510. + * Fix comparison that should be an assignment in an if.
  55511. + *
  55512. + * Revision 1.7 1999/05/09 03:25:37 rgb
  55513. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  55514. + *
  55515. + * Revision 1.6 1999/05/08 21:32:30 rgb
  55516. + * Fix error return reporting.
  55517. + *
  55518. + * Revision 1.5 1999/05/05 22:02:33 rgb
  55519. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  55520. + *
  55521. + * Revision 1.4 1999/04/29 15:22:40 rgb
  55522. + * Standardise an error return method.
  55523. + * Add debugging instrumentation.
  55524. + * Add check for existence of macros min/max.
  55525. + * Add extensions permitted/required in/out filters.
  55526. + * Add satype-to-protocol table.
  55527. + * Add a second tdb pointer to each parser to accomodate GRPSA.
  55528. + * Move AH & no_algo_set to GETSPI, UPDATE and ADD.
  55529. + * Add OOO window check.
  55530. + * Add support for IPPROTO_IPIP and hooks for IPPROTO_COMP.
  55531. + * Add timestamp to lifetime parse.
  55532. + * Fix address structure length checking bug.
  55533. + * Fix address structure allocation bug (forgot to kmalloc!).
  55534. + * Add checks for extension lengths.
  55535. + * Add checks for extension reserved illegal values.
  55536. + * Add check for spirange legal values.
  55537. + * Add an extension type for parsing a second satype, SA and
  55538. + * DST_ADDRESS.
  55539. + * Make changes to tdb_init() template to get pfkey_tdb_init(),
  55540. + * eliminating any mention of xformsw.
  55541. + * Implement getspi, update and grpsa (not tested).
  55542. + * Add stubs for as yet unimplemented message types.
  55543. + * Add table of message parsers to substitute for msg_parse switch.
  55544. + *
  55545. + * Revision 1.3 1999/04/15 17:58:07 rgb
  55546. + * Add RCSID labels.
  55547. + *
  55548. + * Revision 1.2 1999/04/15 15:37:26 rgb
  55549. + * Forward check changes from POST1_00 branch.
  55550. + *
  55551. + * Revision 1.1.2.1 1999/03/26 20:58:56 rgb
  55552. + * Add pfkeyv2 support to KLIPS.
  55553. + *
  55554. + * Local variables:
  55555. + * c-file-style: "linux"
  55556. + * End:
  55557. + *
  55558. + */
  55559. --- /dev/null Tue Mar 11 13:02:56 2003
  55560. +++ linux/net/ipsec/prng.c Mon Feb 9 13:51:03 2004
  55561. @@ -0,0 +1,201 @@
  55562. +/*
  55563. + * crypto-class pseudorandom number generator
  55564. + * currently uses same algorithm as RC4(TM), from Schneier 2nd ed p397
  55565. + * Copyright (C) 2002 Henry Spencer.
  55566. + *
  55567. + * This library is free software; you can redistribute it and/or modify it
  55568. + * under the terms of the GNU Library General Public License as published by
  55569. + * the Free Software Foundation; either version 2 of the License, or (at your
  55570. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  55571. + *
  55572. + * This library is distributed in the hope that it will be useful, but
  55573. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  55574. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  55575. + * License for more details.
  55576. + *
  55577. + * RCSID $Id: prng.c,v 1.7 2004/07/10 07:48:36 mcr Exp $
  55578. + */
  55579. +#include "openswan.h"
  55580. +
  55581. +/*
  55582. + - prng_init - initialize PRNG from a key
  55583. + */
  55584. +void
  55585. +prng_init(prng, key, keylen)
  55586. +struct prng *prng;
  55587. +const unsigned char *key;
  55588. +size_t keylen;
  55589. +{
  55590. + unsigned char k[256];
  55591. + int i, j;
  55592. + unsigned const char *p;
  55593. + unsigned const char *keyend = key + keylen;
  55594. + unsigned char t;
  55595. +
  55596. + for (i = 0; i <= 255; i++)
  55597. + prng->sbox[i] = i;
  55598. + p = key;
  55599. + for (i = 0; i <= 255; i++) {
  55600. + k[i] = *p++;
  55601. + if (p >= keyend)
  55602. + p = key;
  55603. + }
  55604. + j = 0;
  55605. + for (i = 0; i <= 255; i++) {
  55606. + j = (j + prng->sbox[i] + k[i]) & 0xff;
  55607. + t = prng->sbox[i];
  55608. + prng->sbox[i] = prng->sbox[j];
  55609. + prng->sbox[j] = t;
  55610. + k[i] = 0; /* clear out key memory */
  55611. + }
  55612. + prng->i = 0;
  55613. + prng->j = 0;
  55614. + prng->count = 0;
  55615. +}
  55616. +
  55617. +/*
  55618. + - prng_bytes - get some pseudorandom bytes from PRNG
  55619. + */
  55620. +void
  55621. +prng_bytes(prng, dst, dstlen)
  55622. +struct prng *prng;
  55623. +unsigned char *dst;
  55624. +size_t dstlen;
  55625. +{
  55626. + int i, j, t;
  55627. + unsigned char *p = dst;
  55628. + size_t remain = dstlen;
  55629. +# define MAX 4000000000ul
  55630. +
  55631. + while (remain > 0) {
  55632. + i = (prng->i + 1) & 0xff;
  55633. + prng->i = i;
  55634. + j = (prng->j + prng->sbox[i]) & 0xff;
  55635. + prng->j = j;
  55636. + t = prng->sbox[i];
  55637. + prng->sbox[i] = prng->sbox[j];
  55638. + prng->sbox[j] = t;
  55639. + t = (t + prng->sbox[i]) & 0xff;
  55640. + *p++ = prng->sbox[t];
  55641. + remain--;
  55642. + }
  55643. + if (prng->count < MAX - dstlen)
  55644. + prng->count += dstlen;
  55645. + else
  55646. + prng->count = MAX;
  55647. +}
  55648. +
  55649. +/*
  55650. + - prnt_count - how many bytes have been extracted from PRNG so far?
  55651. + */
  55652. +unsigned long
  55653. +prng_count(prng)
  55654. +struct prng *prng;
  55655. +{
  55656. + return prng->count;
  55657. +}
  55658. +
  55659. +/*
  55660. + - prng_final - clear out PRNG to ensure nothing left in memory
  55661. + */
  55662. +void
  55663. +prng_final(prng)
  55664. +struct prng *prng;
  55665. +{
  55666. + int i;
  55667. +
  55668. + for (i = 0; i <= 255; i++)
  55669. + prng->sbox[i] = 0;
  55670. + prng->i = 0;
  55671. + prng->j = 0;
  55672. + prng->count = 0; /* just for good measure */
  55673. +}
  55674. +
  55675. +
  55676. +
  55677. +#ifdef PRNG_MAIN
  55678. +
  55679. +#include <stdio.h>
  55680. +
  55681. +void regress();
  55682. +
  55683. +int
  55684. +main(argc, argv)
  55685. +int argc;
  55686. +char *argv[];
  55687. +{
  55688. + struct prng pr;
  55689. + unsigned char buf[100];
  55690. + unsigned char *p;
  55691. + size_t n;
  55692. +
  55693. + if (argc < 2) {
  55694. + fprintf(stderr, "Usage: %s {key|-r}\n", argv[0]);
  55695. + exit(2);
  55696. + }
  55697. +
  55698. + if (strcmp(argv[1], "-r") == 0) {
  55699. + regress();
  55700. + fprintf(stderr, "regress() returned?!?\n");
  55701. + exit(1);
  55702. + }
  55703. +
  55704. + prng_init(&pr, argv[1], strlen(argv[1]));
  55705. + prng_bytes(&pr, buf, 32);
  55706. + printf("0x");
  55707. + for (p = buf, n = 32; n > 0; p++, n--)
  55708. + printf("%02x", *p);
  55709. + printf("\n%lu bytes\n", prng_count(&pr));
  55710. + prng_final(&pr);
  55711. + exit(0);
  55712. +}
  55713. +
  55714. +void
  55715. +regress()
  55716. +{
  55717. + struct prng pr;
  55718. + unsigned char buf[100];
  55719. + unsigned char *p;
  55720. + size_t n;
  55721. + /* somewhat non-random sample key */
  55722. + unsigned char key[] = "here we go gathering nuts in May";
  55723. + /* first thirty bytes of output from that key */
  55724. + unsigned char good[] = "\x3f\x02\x8e\x4a\x2a\xea\x23\x18\x92\x7c"
  55725. + "\x09\x52\x83\x61\xaa\x26\xce\xbb\x9d\x71"
  55726. + "\x71\xe5\x10\x22\xaf\x60\x54\x8d\x5b\x28";
  55727. + int nzero, none;
  55728. + int show = 0;
  55729. +
  55730. + prng_init(&pr, key, strlen(key));
  55731. + prng_bytes(&pr, buf, sizeof(buf));
  55732. + for (p = buf, n = sizeof(buf); n > 0; p++, n--) {
  55733. + if (*p == 0)
  55734. + nzero++;
  55735. + if (*p == 255)
  55736. + none++;
  55737. + }
  55738. + if (nzero > 3 || none > 3) {
  55739. + fprintf(stderr, "suspiciously non-random output!\n");
  55740. + show = 1;
  55741. + }
  55742. + if (memcmp(buf, good, strlen(good)) != 0) {
  55743. + fprintf(stderr, "incorrect output!\n");
  55744. + show = 1;
  55745. + }
  55746. + if (show) {
  55747. + fprintf(stderr, "0x");
  55748. + for (p = buf, n = sizeof(buf); n > 0; p++, n--)
  55749. + fprintf(stderr, "%02x", *p);
  55750. + fprintf(stderr, "\n");
  55751. + exit(1);
  55752. + }
  55753. + if (prng_count(&pr) != sizeof(buf)) {
  55754. + fprintf(stderr, "got %u bytes, but count is %lu\n",
  55755. + sizeof(buf), prng_count(&pr));
  55756. + exit(1);
  55757. + }
  55758. + prng_final(&pr);
  55759. + exit(0);
  55760. +}
  55761. +
  55762. +#endif /* PRNG_MAIN */
  55763. --- /dev/null Tue Mar 11 13:02:56 2003
  55764. +++ linux/net/ipsec/radij.c Mon Feb 9 13:51:03 2004
  55765. @@ -0,0 +1,1237 @@
  55766. +char radij_c_version[] = "RCSID $Id: radij.c,v 1.48.2.1 2006/10/06 21:39:27 paul Exp $";
  55767. +
  55768. +/*
  55769. + * This file is defived from ${SRC}/sys/net/radix.c of BSD 4.4lite
  55770. + *
  55771. + * Variable and procedure names have been modified so that they don't
  55772. + * conflict with the original BSD code, as a small number of modifications
  55773. + * have been introduced and we may want to reuse this code in BSD.
  55774. + *
  55775. + * The `j' in `radij' is pronounced as a voiceless guttural (like a Greek
  55776. + * chi or a German ch sound (as `doch', not as in `milch'), or even a
  55777. + * spanish j as in Juan. It is not as far back in the throat like
  55778. + * the corresponding Hebrew sound, nor is it a soft breath like the English h.
  55779. + * It has nothing to do with the Dutch ij sound.
  55780. + *
  55781. + * Here is the appropriate copyright notice:
  55782. + */
  55783. +
  55784. +/*
  55785. + * Copyright (c) 1988, 1989, 1993
  55786. + * The Regents of the University of California. All rights reserved.
  55787. + *
  55788. + * Redistribution and use in source and binary forms, with or without
  55789. + * modification, are permitted provided that the following conditions
  55790. + * are met:
  55791. + * 1. Redistributions of source code must retain the above copyright
  55792. + * notice, this list of conditions and the following disclaimer.
  55793. + * 2. Redistributions in binary form must reproduce the above copyright
  55794. + * notice, this list of conditions and the following disclaimer in the
  55795. + * documentation and/or other materials provided with the distribution.
  55796. + * 3. All advertising materials mentioning features or use of this software
  55797. + * must display the following acknowledgement:
  55798. + * This product includes software developed by the University of
  55799. + * California, Berkeley and its contributors.
  55800. + * 4. Neither the name of the University nor the names of its contributors
  55801. + * may be used to endorse or promote products derived from this software
  55802. + * without specific prior written permission.
  55803. + *
  55804. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  55805. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55806. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55807. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  55808. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55809. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55810. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55811. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55812. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55813. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55814. + * SUCH DAMAGE.
  55815. + *
  55816. + * @(#)radix.c 8.2 (Berkeley) 1/4/94
  55817. + */
  55818. +
  55819. +/*
  55820. + * Routines to build and maintain radix trees for routing lookups.
  55821. + */
  55822. +
  55823. +#ifndef AUTOCONF_INCLUDED
  55824. +#include <linux/config.h>
  55825. +#endif
  55826. +#include <linux/version.h>
  55827. +#include <linux/kernel.h> /* printk() */
  55828. +
  55829. +#include "openswan/ipsec_param.h"
  55830. +
  55831. +#ifdef MALLOC_SLAB
  55832. +# include <linux/slab.h> /* kmalloc() */
  55833. +#else /* MALLOC_SLAB */
  55834. +# include <linux/malloc.h> /* kmalloc() */
  55835. +#endif /* MALLOC_SLAB */
  55836. +#include <linux/errno.h> /* error codes */
  55837. +#include <linux/types.h> /* size_t */
  55838. +#include <linux/interrupt.h> /* mark_bh */
  55839. +
  55840. +#include <linux/netdevice.h> /* struct device, and other headers */
  55841. +#include <linux/etherdevice.h> /* eth_type_trans */
  55842. +#include <linux/ip.h> /* struct iphdr */
  55843. +#include <linux/skbuff.h>
  55844. +#ifdef NET_21
  55845. +# include <linux/in6.h>
  55846. +#endif /* NET_21 */
  55847. +
  55848. +#include <net/ip.h>
  55849. +
  55850. +#include <openswan.h>
  55851. +
  55852. +#include "openswan/radij.h"
  55853. +#include "openswan/ipsec_encap.h"
  55854. +#include "openswan/ipsec_radij.h"
  55855. +
  55856. +int maj_keylen;
  55857. +struct radij_mask *rj_mkfreelist;
  55858. +struct radij_node_head *mask_rjhead;
  55859. +static int gotOddMasks;
  55860. +static char *maskedKey;
  55861. +static char *rj_zeroes, *rj_ones;
  55862. +
  55863. +#define rj_masktop (mask_rjhead->rnh_treetop)
  55864. +#ifdef Bcmp
  55865. +# undef Bcmp
  55866. +#endif /* Bcmp */
  55867. +#define Bcmp(a, b, l) (l == 0 ? 0 : memcmp((caddr_t)(b), (caddr_t)(a), (size_t)l))
  55868. +/*
  55869. + * The data structure for the keys is a radix tree with one way
  55870. + * branching removed. The index rj_b at an internal node n represents a bit
  55871. + * position to be tested. The tree is arranged so that all descendants
  55872. + * of a node n have keys whose bits all agree up to position rj_b - 1.
  55873. + * (We say the index of n is rj_b.)
  55874. + *
  55875. + * There is at least one descendant which has a one bit at position rj_b,
  55876. + * and at least one with a zero there.
  55877. + *
  55878. + * A route is determined by a pair of key and mask. We require that the
  55879. + * bit-wise logical and of the key and mask to be the key.
  55880. + * We define the index of a route to associated with the mask to be
  55881. + * the first bit number in the mask where 0 occurs (with bit number 0
  55882. + * representing the highest order bit).
  55883. + *
  55884. + * We say a mask is normal if every bit is 0, past the index of the mask.
  55885. + * If a node n has a descendant (k, m) with index(m) == index(n) == rj_b,
  55886. + * and m is a normal mask, then the route applies to every descendant of n.
  55887. + * If the index(m) < rj_b, this implies the trailing last few bits of k
  55888. + * before bit b are all 0, (and hence consequently true of every descendant
  55889. + * of n), so the route applies to all descendants of the node as well.
  55890. + *
  55891. + * The present version of the code makes no use of normal routes,
  55892. + * but similar logic shows that a non-normal mask m such that
  55893. + * index(m) <= index(n) could potentially apply to many children of n.
  55894. + * Thus, for each non-host route, we attach its mask to a list at an internal
  55895. + * node as high in the tree as we can go.
  55896. + */
  55897. +
  55898. +struct radij_node *
  55899. +rj_search(v_arg, head)
  55900. + void *v_arg;
  55901. + struct radij_node *head;
  55902. +{
  55903. + register struct radij_node *x;
  55904. + register caddr_t v;
  55905. +
  55906. + for (x = head, v = v_arg; x->rj_b >= 0;) {
  55907. + if (x->rj_bmask & v[x->rj_off])
  55908. + x = x->rj_r;
  55909. + else
  55910. + x = x->rj_l;
  55911. + }
  55912. + return (x);
  55913. +};
  55914. +
  55915. +struct radij_node *
  55916. +rj_search_m(v_arg, head, m_arg)
  55917. + struct radij_node *head;
  55918. + void *v_arg, *m_arg;
  55919. +{
  55920. + register struct radij_node *x;
  55921. + register caddr_t v = v_arg, m = m_arg;
  55922. +
  55923. + for (x = head; x->rj_b >= 0;) {
  55924. + if ((x->rj_bmask & m[x->rj_off]) &&
  55925. + (x->rj_bmask & v[x->rj_off]))
  55926. + x = x->rj_r;
  55927. + else
  55928. + x = x->rj_l;
  55929. + }
  55930. + return x;
  55931. +};
  55932. +
  55933. +int
  55934. +rj_refines(m_arg, n_arg)
  55935. + void *m_arg, *n_arg;
  55936. +{
  55937. + register caddr_t m = m_arg, n = n_arg;
  55938. + register caddr_t lim, lim2 = lim = n + *(u_char *)n;
  55939. + int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
  55940. + int masks_are_equal = 1;
  55941. +
  55942. + if (longer > 0)
  55943. + lim -= longer;
  55944. + while (n < lim) {
  55945. + if (*n & ~(*m))
  55946. + return 0;
  55947. + if (*n++ != *m++)
  55948. + masks_are_equal = 0;
  55949. +
  55950. + }
  55951. + while (n < lim2)
  55952. + if (*n++)
  55953. + return 0;
  55954. + if (masks_are_equal && (longer < 0))
  55955. + for (lim2 = m - longer; m < lim2; )
  55956. + if (*m++)
  55957. + return 1;
  55958. + return (!masks_are_equal);
  55959. +}
  55960. +
  55961. +
  55962. +struct radij_node *
  55963. +rj_match(v_arg, head)
  55964. + void *v_arg;
  55965. + struct radij_node_head *head;
  55966. +{
  55967. + caddr_t v = v_arg;
  55968. + register struct radij_node *t = head->rnh_treetop, *x;
  55969. + register caddr_t cp = v, cp2, cp3;
  55970. + caddr_t cplim, mstart;
  55971. + struct radij_node *saved_t, *top = t;
  55972. + int off = t->rj_off, vlen = *(u_char *)cp, matched_off;
  55973. +
  55974. + /*
  55975. + * Open code rj_search(v, top) to avoid overhead of extra
  55976. + * subroutine call.
  55977. + */
  55978. + for (; t->rj_b >= 0; ) {
  55979. + if (t->rj_bmask & cp[t->rj_off])
  55980. + t = t->rj_r;
  55981. + else
  55982. + t = t->rj_l;
  55983. + }
  55984. + /*
  55985. + * See if we match exactly as a host destination
  55986. + */
  55987. + KLIPS_PRINT(debug_radij,
  55988. + "klips_debug:rj_match: "
  55989. + "* See if we match exactly as a host destination\n");
  55990. +
  55991. + cp += off; cp2 = t->rj_key + off; cplim = v + vlen;
  55992. + for (; cp < cplim; cp++, cp2++)
  55993. + if (*cp != *cp2)
  55994. + goto on1;
  55995. + /*
  55996. + * This extra grot is in case we are explicitly asked
  55997. + * to look up the default. Ugh!
  55998. + */
  55999. + if ((t->rj_flags & RJF_ROOT) && t->rj_dupedkey)
  56000. + t = t->rj_dupedkey;
  56001. + return t;
  56002. +on1:
  56003. + matched_off = cp - v;
  56004. + saved_t = t;
  56005. + KLIPS_PRINT(debug_radij,
  56006. + "klips_debug:rj_match: "
  56007. + "** try to match a leaf, t=0p%p\n", t);
  56008. + do {
  56009. + if (t->rj_mask) {
  56010. + /*
  56011. + * Even if we don't match exactly as a hosts;
  56012. + * we may match if the leaf we wound up at is
  56013. + * a route to a net.
  56014. + */
  56015. + cp3 = matched_off + t->rj_mask;
  56016. + cp2 = matched_off + t->rj_key;
  56017. + for (; cp < cplim; cp++)
  56018. + if ((*cp2++ ^ *cp) & *cp3++)
  56019. + break;
  56020. + if (cp == cplim)
  56021. + return t;
  56022. + cp = matched_off + v;
  56023. + }
  56024. + } while ((t = t->rj_dupedkey));
  56025. + t = saved_t;
  56026. + /* start searching up the tree */
  56027. + KLIPS_PRINT(debug_radij,
  56028. + "klips_debug:rj_match: "
  56029. + "*** start searching up the tree, t=0p%p\n",
  56030. + t);
  56031. + do {
  56032. + register struct radij_mask *m;
  56033. +
  56034. + t = t->rj_p;
  56035. + KLIPS_PRINT(debug_radij,
  56036. + "klips_debug:rj_match: "
  56037. + "**** t=0p%p\n",
  56038. + t);
  56039. + if ((m = t->rj_mklist)) {
  56040. + /*
  56041. + * After doing measurements here, it may
  56042. + * turn out to be faster to open code
  56043. + * rj_search_m here instead of always
  56044. + * copying and masking.
  56045. + */
  56046. + /* off = min(t->rj_off, matched_off); */
  56047. + off = t->rj_off;
  56048. + if (matched_off < off)
  56049. + off = matched_off;
  56050. + mstart = maskedKey + off;
  56051. + do {
  56052. + cp2 = mstart;
  56053. + cp3 = m->rm_mask + off;
  56054. + KLIPS_PRINT(debug_radij,
  56055. + "klips_debug:rj_match: "
  56056. + "***** cp2=0p%p cp3=0p%p\n",
  56057. + cp2, cp3);
  56058. + for (cp = v + off; cp < cplim;)
  56059. + *cp2++ = *cp++ & *cp3++;
  56060. + x = rj_search(maskedKey, t);
  56061. + while (x && x->rj_mask != m->rm_mask)
  56062. + x = x->rj_dupedkey;
  56063. + if (x &&
  56064. + (Bcmp(mstart, x->rj_key + off,
  56065. + vlen - off) == 0))
  56066. + return x;
  56067. + } while ((m = m->rm_mklist));
  56068. + }
  56069. + } while (t != top);
  56070. + KLIPS_PRINT(debug_radij,
  56071. + "klips_debug:rj_match: "
  56072. + "***** not found.\n");
  56073. + return 0;
  56074. +};
  56075. +
  56076. +#ifdef RJ_DEBUG
  56077. +int rj_nodenum;
  56078. +struct radij_node *rj_clist;
  56079. +int rj_saveinfo;
  56080. +DEBUG_NO_STATIC void traverse(struct radij_node *);
  56081. +#ifdef RJ_DEBUG2
  56082. +int rj_debug = 1;
  56083. +#else
  56084. +int rj_debug = 0;
  56085. +#endif /* RJ_DEBUG2 */
  56086. +#endif /* RJ_DEBUG */
  56087. +
  56088. +struct radij_node *
  56089. +rj_newpair(v, b, nodes)
  56090. + void *v;
  56091. + int b;
  56092. + struct radij_node nodes[2];
  56093. +{
  56094. + register struct radij_node *tt = nodes, *t = tt + 1;
  56095. + t->rj_b = b; t->rj_bmask = 0x80 >> (b & 7);
  56096. + t->rj_l = tt; t->rj_off = b >> 3;
  56097. + tt->rj_b = -1; tt->rj_key = (caddr_t)v; tt->rj_p = t;
  56098. + tt->rj_flags = t->rj_flags = RJF_ACTIVE;
  56099. +#ifdef RJ_DEBUG
  56100. + tt->rj_info = rj_nodenum++; t->rj_info = rj_nodenum++;
  56101. + tt->rj_twin = t; tt->rj_ybro = rj_clist; rj_clist = tt;
  56102. +#endif /* RJ_DEBUG */
  56103. + return t;
  56104. +}
  56105. +
  56106. +struct radij_node *
  56107. +rj_insert(v_arg, head, dupentry, nodes)
  56108. + void *v_arg;
  56109. + struct radij_node_head *head;
  56110. + int *dupentry;
  56111. + struct radij_node nodes[2];
  56112. +{
  56113. + caddr_t v = v_arg;
  56114. + struct radij_node *top = head->rnh_treetop;
  56115. + int head_off = top->rj_off, vlen = (int)*((u_char *)v);
  56116. + register struct radij_node *t = rj_search(v_arg, top);
  56117. + register caddr_t cp = v + head_off;
  56118. + register int b;
  56119. + struct radij_node *tt;
  56120. + /*
  56121. + *find first bit at which v and t->rj_key differ
  56122. + */
  56123. + {
  56124. + register caddr_t cp2 = t->rj_key + head_off;
  56125. + register int cmp_res;
  56126. + caddr_t cplim = v + vlen;
  56127. +
  56128. + while (cp < cplim)
  56129. + if (*cp2++ != *cp++)
  56130. + goto on1;
  56131. + *dupentry = 1;
  56132. + return t;
  56133. +on1:
  56134. + *dupentry = 0;
  56135. + cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
  56136. + for (b = (cp - v) << 3; cmp_res; b--)
  56137. + cmp_res >>= 1;
  56138. + }
  56139. + {
  56140. + register struct radij_node *p, *x = top;
  56141. + cp = v;
  56142. + do {
  56143. + p = x;
  56144. + if (cp[x->rj_off] & x->rj_bmask)
  56145. + x = x->rj_r;
  56146. + else x = x->rj_l;
  56147. + } while (b > (unsigned) x->rj_b); /* x->rj_b < b && x->rj_b >= 0 */
  56148. +#ifdef RJ_DEBUG
  56149. + if (rj_debug)
  56150. + printk("klips_debug:rj_insert: Going In:\n"), traverse(p);
  56151. +#endif /* RJ_DEBUG */
  56152. + t = rj_newpair(v_arg, b, nodes); tt = t->rj_l;
  56153. + if ((cp[p->rj_off] & p->rj_bmask) == 0)
  56154. + p->rj_l = t;
  56155. + else
  56156. + p->rj_r = t;
  56157. + x->rj_p = t; t->rj_p = p; /* frees x, p as temp vars below */
  56158. + if ((cp[t->rj_off] & t->rj_bmask) == 0) {
  56159. + t->rj_r = x;
  56160. + } else {
  56161. + t->rj_r = tt; t->rj_l = x;
  56162. + }
  56163. +#ifdef RJ_DEBUG
  56164. + if (rj_debug)
  56165. + printk("klips_debug:rj_insert: Coming out:\n"), traverse(p);
  56166. +#endif /* RJ_DEBUG */
  56167. + }
  56168. + return (tt);
  56169. +}
  56170. +
  56171. +struct radij_node *
  56172. +rj_addmask(n_arg, search, skip)
  56173. + int search, skip;
  56174. + void *n_arg;
  56175. +{
  56176. + caddr_t netmask = (caddr_t)n_arg;
  56177. + register struct radij_node *x;
  56178. + register caddr_t cp, cplim;
  56179. + register int b, mlen, j;
  56180. + int maskduplicated;
  56181. +
  56182. + mlen = *(u_char *)netmask;
  56183. + if (search) {
  56184. + x = rj_search(netmask, rj_masktop);
  56185. + mlen = *(u_char *)netmask;
  56186. + if (Bcmp(netmask, x->rj_key, mlen) == 0)
  56187. + return (x);
  56188. + }
  56189. + R_Malloc(x, struct radij_node *, maj_keylen + 2 * sizeof (*x));
  56190. + if (x == 0)
  56191. + return (0);
  56192. + Bzero(x, maj_keylen + 2 * sizeof (*x));
  56193. + cp = (caddr_t)(x + 2);
  56194. + Bcopy(netmask, cp, mlen);
  56195. + netmask = cp;
  56196. + x = rj_insert(netmask, mask_rjhead, &maskduplicated, x);
  56197. + /*
  56198. + * Calculate index of mask.
  56199. + */
  56200. + cplim = netmask + mlen;
  56201. + for (cp = netmask + skip; cp < cplim; cp++)
  56202. + if (*(u_char *)cp != 0xff)
  56203. + break;
  56204. + b = (cp - netmask) << 3;
  56205. + if (cp != cplim) {
  56206. + if (*cp != 0) {
  56207. + gotOddMasks = 1;
  56208. + for (j = 0x80; j; b++, j >>= 1)
  56209. + if ((j & *cp) == 0)
  56210. + break;
  56211. + }
  56212. + }
  56213. + x->rj_b = -1 - b;
  56214. + return (x);
  56215. +}
  56216. +
  56217. +#if 0
  56218. +struct radij_node *
  56219. +#endif
  56220. +int
  56221. +rj_addroute(v_arg, n_arg, head, treenodes)
  56222. + void *v_arg, *n_arg;
  56223. + struct radij_node_head *head;
  56224. + struct radij_node treenodes[2];
  56225. +{
  56226. + caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
  56227. + register struct radij_node *t, *x=NULL, *tt;
  56228. + struct radij_node *saved_tt, *top = head->rnh_treetop;
  56229. + short b = 0, b_leaf;
  56230. + int mlen, keyduplicated;
  56231. + caddr_t cplim;
  56232. + struct radij_mask *m, **mp;
  56233. +
  56234. + /*
  56235. + * In dealing with non-contiguous masks, there may be
  56236. + * many different routes which have the same mask.
  56237. + * We will find it useful to have a unique pointer to
  56238. + * the mask to speed avoiding duplicate references at
  56239. + * nodes and possibly save time in calculating indices.
  56240. + */
  56241. + if (netmask) {
  56242. + x = rj_search(netmask, rj_masktop);
  56243. + mlen = *(u_char *)netmask;
  56244. + if (Bcmp(netmask, x->rj_key, mlen) != 0) {
  56245. + x = rj_addmask(netmask, 0, top->rj_off);
  56246. + if (x == 0)
  56247. + return -ENOMEM; /* (0) rgb */
  56248. + }
  56249. + netmask = x->rj_key;
  56250. + b = -1 - x->rj_b;
  56251. + }
  56252. + /*
  56253. + * Deal with duplicated keys: attach node to previous instance
  56254. + */
  56255. + saved_tt = tt = rj_insert(v, head, &keyduplicated, treenodes);
  56256. +#ifdef RJ_DEBUG
  56257. + printk("addkey: duplicated: %d\n", keyduplicated);
  56258. +#endif
  56259. + if (keyduplicated) {
  56260. + do {
  56261. + if (tt->rj_mask == netmask)
  56262. + return -EEXIST; /* -ENXIO; (0) rgb */
  56263. + t = tt;
  56264. + if (netmask == 0 ||
  56265. + (tt->rj_mask && rj_refines(netmask, tt->rj_mask)))
  56266. + break;
  56267. + } while ((tt = tt->rj_dupedkey));
  56268. + /*
  56269. + * If the mask is not duplicated, we wouldn't
  56270. + * find it among possible duplicate key entries
  56271. + * anyway, so the above test doesn't hurt.
  56272. + *
  56273. + * We sort the masks for a duplicated key the same way as
  56274. + * in a masklist -- most specific to least specific.
  56275. + * This may require the unfortunate nuisance of relocating
  56276. + * the head of the list.
  56277. + */
  56278. + if (tt && t == saved_tt) {
  56279. + struct radij_node *xx = x;
  56280. + /* link in at head of list */
  56281. + (tt = treenodes)->rj_dupedkey = t;
  56282. + tt->rj_flags = t->rj_flags;
  56283. + tt->rj_p = x = t->rj_p;
  56284. + if (x->rj_l == t) x->rj_l = tt; else x->rj_r = tt;
  56285. + saved_tt = tt; x = xx;
  56286. + } else {
  56287. + (tt = treenodes)->rj_dupedkey = t->rj_dupedkey;
  56288. + t->rj_dupedkey = tt;
  56289. + }
  56290. +#ifdef RJ_DEBUG
  56291. + t=tt+1; tt->rj_info = rj_nodenum++; t->rj_info = rj_nodenum++;
  56292. + tt->rj_twin = t; tt->rj_ybro = rj_clist; rj_clist = tt;
  56293. +#endif /* RJ_DEBUG */
  56294. + t = saved_tt;
  56295. + tt->rj_key = (caddr_t) v;
  56296. + tt->rj_b = -1;
  56297. + tt->rj_flags = t->rj_flags & ~RJF_ROOT;
  56298. + }
  56299. + /*
  56300. + * Put mask in tree.
  56301. + */
  56302. + if (netmask) {
  56303. + tt->rj_mask = netmask;
  56304. + tt->rj_b = x->rj_b;
  56305. + }
  56306. + t = saved_tt->rj_p;
  56307. + b_leaf = -1 - t->rj_b;
  56308. + if (t->rj_r == saved_tt) x = t->rj_l; else x = t->rj_r;
  56309. + /* Promote general routes from below */
  56310. + if (x->rj_b < 0) {
  56311. + if (x->rj_mask && (x->rj_b >= b_leaf) && x->rj_mklist == 0) {
  56312. + MKGet(m);
  56313. + if (m) {
  56314. + Bzero(m, sizeof *m);
  56315. + m->rm_b = x->rj_b;
  56316. + m->rm_mask = x->rj_mask;
  56317. + x->rj_mklist = t->rj_mklist = m;
  56318. + }
  56319. + }
  56320. + } else if (x->rj_mklist) {
  56321. + /*
  56322. + * Skip over masks whose index is > that of new node
  56323. + */
  56324. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist)
  56325. + if (m->rm_b >= b_leaf)
  56326. + break;
  56327. + t->rj_mklist = m; *mp = 0;
  56328. + }
  56329. + /* Add new route to highest possible ancestor's list */
  56330. + if ((netmask == 0) || (b > t->rj_b )) {
  56331. +#ifdef RJ_DEBUG
  56332. + printk("klips:radij.c: netmask = %p or b(%d)>t->rjb(%d)\n", netmask, b, t->rj_b);
  56333. +#endif
  56334. + return 0; /* tt rgb */ /* can't lift at all */
  56335. + }
  56336. + b_leaf = tt->rj_b;
  56337. + do {
  56338. + x = t;
  56339. + t = t->rj_p;
  56340. + } while (b <= t->rj_b && x != top);
  56341. + /*
  56342. + * Search through routes associated with node to
  56343. + * insert new route according to index.
  56344. + * For nodes of equal index, place more specific
  56345. + * masks first.
  56346. + */
  56347. + cplim = netmask + mlen;
  56348. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist) {
  56349. + if (m->rm_b < b_leaf)
  56350. + continue;
  56351. + if (m->rm_b > b_leaf)
  56352. + break;
  56353. + if (m->rm_mask == netmask) {
  56354. + m->rm_refs++;
  56355. + tt->rj_mklist = m;
  56356. +#ifdef RJ_DEBUG
  56357. + printk("klips:radij.c: m->rm_mask %p == netmask\n", netmask);
  56358. +#endif
  56359. + return 0; /* tt rgb */
  56360. + }
  56361. + if (rj_refines(netmask, m->rm_mask))
  56362. + break;
  56363. + }
  56364. + MKGet(m);
  56365. + if (m == 0) {
  56366. + printk("klips_debug:rj_addroute: "
  56367. + "Mask for route not entered\n");
  56368. + return 0; /* (tt) rgb */
  56369. + }
  56370. + Bzero(m, sizeof *m);
  56371. + m->rm_b = b_leaf;
  56372. + m->rm_mask = netmask;
  56373. + m->rm_mklist = *mp;
  56374. + *mp = m;
  56375. + tt->rj_mklist = m;
  56376. +#ifdef RJ_DEBUG
  56377. + printk("klips:radij.c: addroute done\n");
  56378. +#endif
  56379. + return 0; /* tt rgb */
  56380. +}
  56381. +
  56382. +int
  56383. +rj_delete(v_arg, netmask_arg, head, node)
  56384. + void *v_arg, *netmask_arg;
  56385. + struct radij_node_head *head;
  56386. + struct radij_node **node;
  56387. +{
  56388. + register struct radij_node *t, *p, *x, *tt;
  56389. + struct radij_mask *m, *saved_m, **mp;
  56390. + struct radij_node *dupedkey, *saved_tt, *top;
  56391. + caddr_t v, netmask;
  56392. + int b, head_off, vlen;
  56393. +
  56394. + v = v_arg;
  56395. + netmask = netmask_arg;
  56396. + x = head->rnh_treetop;
  56397. + tt = rj_search(v, x);
  56398. + head_off = x->rj_off;
  56399. + vlen = *(u_char *)v;
  56400. + saved_tt = tt;
  56401. + top = x;
  56402. + if (tt == 0 ||
  56403. + Bcmp(v + head_off, tt->rj_key + head_off, vlen - head_off))
  56404. + return -EFAULT; /* (0) rgb */
  56405. + /*
  56406. + * Delete our route from mask lists.
  56407. + */
  56408. + if ((dupedkey = tt->rj_dupedkey)) {
  56409. + if (netmask)
  56410. + netmask = rj_search(netmask, rj_masktop)->rj_key;
  56411. + while (tt->rj_mask != netmask)
  56412. + if ((tt = tt->rj_dupedkey) == 0)
  56413. + return -ENOENT; /* -ENXIO; (0) rgb */
  56414. + }
  56415. + if (tt->rj_mask == 0 || (saved_m = m = tt->rj_mklist) == 0)
  56416. + goto on1;
  56417. + if (m->rm_mask != tt->rj_mask) {
  56418. + printk("klips_debug:rj_delete: "
  56419. + "inconsistent annotation\n");
  56420. + goto on1;
  56421. + }
  56422. + if (--m->rm_refs >= 0)
  56423. + goto on1;
  56424. + b = -1 - tt->rj_b;
  56425. + t = saved_tt->rj_p;
  56426. + if (b > t->rj_b)
  56427. + goto on1; /* Wasn't lifted at all */
  56428. + do {
  56429. + x = t;
  56430. + t = t->rj_p;
  56431. + } while (b <= t->rj_b && x != top);
  56432. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist)
  56433. + if (m == saved_m) {
  56434. + *mp = m->rm_mklist;
  56435. + MKFree(m);
  56436. + break;
  56437. + }
  56438. + if (m == 0)
  56439. + printk("klips_debug:rj_delete: "
  56440. + "couldn't find our annotation\n");
  56441. +on1:
  56442. + /*
  56443. + * Eliminate us from tree
  56444. + */
  56445. + if (tt->rj_flags & RJF_ROOT)
  56446. + return -EFAULT; /* (0) rgb */
  56447. +#ifdef RJ_DEBUG
  56448. + /* Get us out of the creation list */
  56449. + for (t = rj_clist; t && t->rj_ybro != tt; t = t->rj_ybro) {}
  56450. + if (t) t->rj_ybro = tt->rj_ybro;
  56451. +#endif /* RJ_DEBUG */
  56452. + t = tt->rj_p;
  56453. + if (dupedkey) {
  56454. + if (tt == saved_tt) {
  56455. + x = dupedkey; x->rj_p = t;
  56456. + if (t->rj_l == tt) t->rj_l = x; else t->rj_r = x;
  56457. + } else {
  56458. + for (x = p = saved_tt; p && p->rj_dupedkey != tt;)
  56459. + p = p->rj_dupedkey;
  56460. + if (p) p->rj_dupedkey = tt->rj_dupedkey;
  56461. + else printk("klips_debug:rj_delete: "
  56462. + "couldn't find node that we started with\n");
  56463. + }
  56464. + t = tt + 1;
  56465. + if (t->rj_flags & RJF_ACTIVE) {
  56466. +#ifndef RJ_DEBUG
  56467. + *++x = *t; p = t->rj_p;
  56468. +#else
  56469. + b = t->rj_info; *++x = *t; t->rj_info = b; p = t->rj_p;
  56470. +#endif /* RJ_DEBUG */
  56471. + if (p->rj_l == t) p->rj_l = x; else p->rj_r = x;
  56472. + x->rj_l->rj_p = x; x->rj_r->rj_p = x;
  56473. + }
  56474. + goto out;
  56475. + }
  56476. + if (t->rj_l == tt) x = t->rj_r; else x = t->rj_l;
  56477. + p = t->rj_p;
  56478. + if (p->rj_r == t) p->rj_r = x; else p->rj_l = x;
  56479. + x->rj_p = p;
  56480. + /*
  56481. + * Demote routes attached to us.
  56482. + */
  56483. + if (t->rj_mklist) {
  56484. + if (x->rj_b >= 0) {
  56485. + for (mp = &x->rj_mklist; (m = *mp);)
  56486. + mp = &m->rm_mklist;
  56487. + *mp = t->rj_mklist;
  56488. + } else {
  56489. + for (m = t->rj_mklist; m;) {
  56490. + struct radij_mask *mm = m->rm_mklist;
  56491. + if (m == x->rj_mklist && (--(m->rm_refs) < 0)) {
  56492. + x->rj_mklist = 0;
  56493. + MKFree(m);
  56494. + } else
  56495. + printk("klips_debug:rj_delete: "
  56496. + "Orphaned Mask 0p%p at 0p%p\n", m, x);
  56497. + m = mm;
  56498. + }
  56499. + }
  56500. + }
  56501. + /*
  56502. + * We may be holding an active internal node in the tree.
  56503. + */
  56504. + x = tt + 1;
  56505. + if (t != x) {
  56506. +#ifndef RJ_DEBUG
  56507. + *t = *x;
  56508. +#else
  56509. + b = t->rj_info; *t = *x; t->rj_info = b;
  56510. +#endif /* RJ_DEBUG */
  56511. + t->rj_l->rj_p = t; t->rj_r->rj_p = t;
  56512. + p = x->rj_p;
  56513. + if (p->rj_l == x) p->rj_l = t; else p->rj_r = t;
  56514. + }
  56515. +out:
  56516. + tt->rj_flags &= ~RJF_ACTIVE;
  56517. + tt[1].rj_flags &= ~RJF_ACTIVE;
  56518. + *node = tt;
  56519. + return 0; /* (tt) rgb */
  56520. +}
  56521. +
  56522. +int
  56523. +rj_walktree(h, f, w)
  56524. + struct radij_node_head *h;
  56525. + register int (*f)(struct radij_node *,void *);
  56526. + void *w;
  56527. +{
  56528. + int error;
  56529. + struct radij_node *base, *next;
  56530. + register struct radij_node *rn;
  56531. +
  56532. + if(!h || !f /* || !w */) {
  56533. + return -ENODATA;
  56534. + }
  56535. +
  56536. + rn = h->rnh_treetop;
  56537. + /*
  56538. + * This gets complicated because we may delete the node
  56539. + * while applying the function f to it, so we need to calculate
  56540. + * the successor node in advance.
  56541. + */
  56542. + /* First time through node, go left */
  56543. + while (rn->rj_b >= 0)
  56544. + rn = rn->rj_l;
  56545. + for (;;) {
  56546. +#ifdef CONFIG_KLIPS_DEBUG
  56547. + if(debug_radij) {
  56548. + printk("klips_debug:rj_walktree: "
  56549. + "for: rn=0p%p rj_b=%d rj_flags=%x",
  56550. + rn,
  56551. + rn->rj_b,
  56552. + rn->rj_flags);
  56553. + rn->rj_b >= 0 ?
  56554. + printk(" node off=%x\n",
  56555. + rn->rj_off) :
  56556. + printk(" leaf key = %08x->%08x\n",
  56557. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56558. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56559. + ;
  56560. + }
  56561. +#endif /* CONFIG_KLIPS_DEBUG */
  56562. + base = rn;
  56563. + /* If at right child go back up, otherwise, go right */
  56564. + while (rn->rj_p->rj_r == rn && (rn->rj_flags & RJF_ROOT) == 0)
  56565. + rn = rn->rj_p;
  56566. + /* Find the next *leaf* since next node might vanish, too */
  56567. + for (rn = rn->rj_p->rj_r; rn->rj_b >= 0;)
  56568. + rn = rn->rj_l;
  56569. + next = rn;
  56570. +#ifdef CONFIG_KLIPS_DEBUG
  56571. + if(debug_radij) {
  56572. + printk("klips_debug:rj_walktree: "
  56573. + "processing leaves, rn=0p%p rj_b=%d rj_flags=%x",
  56574. + rn,
  56575. + rn->rj_b,
  56576. + rn->rj_flags);
  56577. + rn->rj_b >= 0 ?
  56578. + printk(" node off=%x\n",
  56579. + rn->rj_off) :
  56580. + printk(" leaf key = %08x->%08x\n",
  56581. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56582. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56583. + ;
  56584. + }
  56585. +#endif /* CONFIG_KLIPS_DEBUG */
  56586. + /* Process leaves */
  56587. + while ((rn = base)) {
  56588. + base = rn->rj_dupedkey;
  56589. +#ifdef CONFIG_KLIPS_DEBUG
  56590. + if(debug_radij) {
  56591. + printk("klips_debug:rj_walktree: "
  56592. + "while: base=0p%p rn=0p%p rj_b=%d rj_flags=%x",
  56593. + base,
  56594. + rn,
  56595. + rn->rj_b,
  56596. + rn->rj_flags);
  56597. + rn->rj_b >= 0 ?
  56598. + printk(" node off=%x\n",
  56599. + rn->rj_off) :
  56600. + printk(" leaf key = %08x->%08x\n",
  56601. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56602. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56603. + ;
  56604. + }
  56605. +#endif /* CONFIG_KLIPS_DEBUG */
  56606. + if (!(rn->rj_flags & RJF_ROOT) && (error = (*f)(rn, w)))
  56607. + return (-error);
  56608. + }
  56609. + rn = next;
  56610. + if (rn->rj_flags & RJF_ROOT)
  56611. + return (0);
  56612. + }
  56613. + /* NOTREACHED */
  56614. +}
  56615. +
  56616. +int
  56617. +rj_inithead(head, off)
  56618. + void **head;
  56619. + int off;
  56620. +{
  56621. + register struct radij_node_head *rnh;
  56622. + register struct radij_node *t, *tt, *ttt;
  56623. + if (*head)
  56624. + return (1);
  56625. + R_Malloc(rnh, struct radij_node_head *, sizeof (*rnh));
  56626. + if (rnh == NULL)
  56627. + return (0);
  56628. + Bzero(rnh, sizeof (*rnh));
  56629. + *head = rnh;
  56630. + t = rj_newpair(rj_zeroes, off, rnh->rnh_nodes);
  56631. + ttt = rnh->rnh_nodes + 2;
  56632. + t->rj_r = ttt;
  56633. + t->rj_p = t;
  56634. + tt = t->rj_l;
  56635. + tt->rj_flags = t->rj_flags = RJF_ROOT | RJF_ACTIVE;
  56636. + tt->rj_b = -1 - off;
  56637. + *ttt = *tt;
  56638. + ttt->rj_key = rj_ones;
  56639. + rnh->rnh_addaddr = rj_addroute;
  56640. + rnh->rnh_deladdr = rj_delete;
  56641. + rnh->rnh_matchaddr = rj_match;
  56642. + rnh->rnh_walktree = rj_walktree;
  56643. + rnh->rnh_treetop = t;
  56644. + return (1);
  56645. +}
  56646. +
  56647. +void
  56648. +rj_init()
  56649. +{
  56650. + char *cp, *cplim;
  56651. +
  56652. + if (maj_keylen == 0) {
  56653. + printk("klips_debug:rj_init: "
  56654. + "radij functions require maj_keylen be set\n");
  56655. + return;
  56656. + }
  56657. + R_Malloc(rj_zeroes, char *, 3 * maj_keylen);
  56658. + if (rj_zeroes == NULL)
  56659. + panic("rj_init");
  56660. + Bzero(rj_zeroes, 3 * maj_keylen);
  56661. + rj_ones = cp = rj_zeroes + maj_keylen;
  56662. + maskedKey = cplim = rj_ones + maj_keylen;
  56663. + while (cp < cplim)
  56664. + *cp++ = -1;
  56665. + if (rj_inithead((void **)&mask_rjhead, 0) == 0)
  56666. + panic("rj_init 2");
  56667. +}
  56668. +
  56669. +void
  56670. +rj_preorder(struct radij_node *rn, int l)
  56671. +{
  56672. + int i;
  56673. +
  56674. + if (rn == NULL){
  56675. + printk("klips_debug:rj_preorder: "
  56676. + "NULL pointer\n");
  56677. + return;
  56678. + }
  56679. +
  56680. + if (rn->rj_b >= 0){
  56681. + rj_preorder(rn->rj_l, l+1);
  56682. + rj_preorder(rn->rj_r, l+1);
  56683. + printk("klips_debug:");
  56684. + for (i=0; i<l; i++)
  56685. + printk("*");
  56686. + printk(" off = %d\n",
  56687. + rn->rj_off);
  56688. + } else {
  56689. + printk("klips_debug:");
  56690. + for (i=0; i<l; i++)
  56691. + printk("@");
  56692. + printk(" flags = %x",
  56693. + (u_int)rn->rj_flags);
  56694. + if (rn->rj_flags & RJF_ACTIVE) {
  56695. + printk(" @key=0p%p",
  56696. + rn->rj_key);
  56697. + printk(" key = %08x->%08x",
  56698. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56699. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr));
  56700. + printk(" @mask=0p%p",
  56701. + rn->rj_mask);
  56702. + if (rn->rj_mask)
  56703. + printk(" mask = %08x->%08x",
  56704. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_mask)->sen_ip_src.s_addr),
  56705. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_mask)->sen_ip_dst.s_addr));
  56706. + if (rn->rj_dupedkey)
  56707. + printk(" dupedkey = 0p%p",
  56708. + rn->rj_dupedkey);
  56709. + }
  56710. + printk("\n");
  56711. + }
  56712. +}
  56713. +
  56714. +#ifdef RJ_DEBUG
  56715. +DEBUG_NO_STATIC void traverse(struct radij_node *p)
  56716. +{
  56717. + rj_preorder(p, 0);
  56718. +}
  56719. +#endif /* RJ_DEBUG */
  56720. +
  56721. +void
  56722. +rj_dumptrees(void)
  56723. +{
  56724. + rj_preorder(rnh->rnh_treetop, 0);
  56725. +}
  56726. +
  56727. +void
  56728. +rj_free_mkfreelist(void)
  56729. +{
  56730. + struct radij_mask *mknp, *mknp2;
  56731. +
  56732. + mknp = rj_mkfreelist;
  56733. + while(mknp)
  56734. + {
  56735. + mknp2 = mknp;
  56736. + mknp = mknp->rm_mklist;
  56737. + kfree(mknp2);
  56738. + }
  56739. +}
  56740. +
  56741. +int
  56742. +radijcleartree(void)
  56743. +{
  56744. + return rj_walktree(rnh, ipsec_rj_walker_delete, NULL);
  56745. +}
  56746. +
  56747. +int
  56748. +radijcleanup(void)
  56749. +{
  56750. + int error = 0;
  56751. +
  56752. + error = radijcleartree();
  56753. +
  56754. + rj_free_mkfreelist();
  56755. +
  56756. +/* rj_walktree(mask_rjhead, ipsec_rj_walker_delete, NULL); */
  56757. + if(mask_rjhead) {
  56758. + kfree(mask_rjhead);
  56759. + }
  56760. +
  56761. + if(rj_zeroes) {
  56762. + kfree(rj_zeroes);
  56763. + }
  56764. +
  56765. + if(rnh) {
  56766. + kfree(rnh);
  56767. + }
  56768. +
  56769. + return error;
  56770. +}
  56771. +
  56772. +/*
  56773. + * $Log: radij.c,v $
  56774. + * Revision 1.48.2.1 2006/10/06 21:39:27 paul
  56775. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  56776. + * set. This is defined through autoconf.h which is included through the
  56777. + * linux kernel build macros.
  56778. + *
  56779. + * Revision 1.48 2005/04/29 05:10:22 mcr
  56780. + * removed from extraenous includes to make unit testing easier.
  56781. + *
  56782. + * Revision 1.47 2004/07/10 19:11:18 mcr
  56783. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  56784. + *
  56785. + * Revision 1.46 2004/04/06 02:49:26 mcr
  56786. + * pullup of algo code from alg-branch.
  56787. + *
  56788. + * Revision 1.45 2003/10/31 02:27:55 mcr
  56789. + * pulled up port-selector patches and sa_id elimination.
  56790. + *
  56791. + * Revision 1.44.30.1 2003/10/29 01:30:41 mcr
  56792. + * elimited "struct sa_id".
  56793. + *
  56794. + * Revision 1.44 2002/07/24 18:44:54 rgb
  56795. + * Type fiddling to tame ia64 compiler.
  56796. + *
  56797. + * Revision 1.43 2002/05/23 07:14:11 rgb
  56798. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  56799. + *
  56800. + * Revision 1.42 2002/04/24 07:55:32 mcr
  56801. + * #include patches and Makefiles for post-reorg compilation.
  56802. + *
  56803. + * Revision 1.41 2002/04/24 07:36:35 mcr
  56804. + * Moved from ./klips/net/ipsec/radij.c,v
  56805. + *
  56806. + * Revision 1.40 2002/01/29 17:17:58 mcr
  56807. + * moved include of ipsec_param.h to after include of linux/kernel.h
  56808. + * otherwise, it seems that some option that is set in ipsec_param.h
  56809. + * screws up something subtle in the include path to kernel.h, and
  56810. + * it complains on the snprintf() prototype.
  56811. + *
  56812. + * Revision 1.39 2002/01/29 04:00:55 mcr
  56813. + * more excise of kversions.h header.
  56814. + *
  56815. + * Revision 1.38 2002/01/29 02:13:19 mcr
  56816. + * introduction of ipsec_kversion.h means that include of
  56817. + * ipsec_param.h must preceed any decisions about what files to
  56818. + * include to deal with differences in kernel source.
  56819. + *
  56820. + * Revision 1.37 2001/10/18 04:45:23 rgb
  56821. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  56822. + * lib/freeswan.h version macros moved to lib/kversions.h.
  56823. + * Other compiler directive cleanups.
  56824. + *
  56825. + * Revision 1.36 2001/08/22 13:43:51 henry
  56826. + * eliminate the single use of min() to avoid problems with Linus changing it
  56827. + *
  56828. + * Revision 1.35 2001/06/15 04:57:29 rgb
  56829. + * Clarified error return codes.
  56830. + * Changed mask add already exists to EEXIST.
  56831. + * Changed mask delete did not exist to ENOENT.
  56832. + *
  56833. + * Revision 1.34 2001/05/03 19:44:26 rgb
  56834. + * Fix sign of error return codes for rj_addroute().
  56835. + *
  56836. + * Revision 1.33 2001/02/27 22:24:56 rgb
  56837. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  56838. + * Check for satoa() return codes.
  56839. + *
  56840. + * Revision 1.32 2001/02/27 06:23:15 rgb
  56841. + * Debug line splitting.
  56842. + *
  56843. + * Revision 1.31 2000/11/06 04:35:21 rgb
  56844. + * Clear table *before* releasing other items in radijcleanup.
  56845. + *
  56846. + * Revision 1.30 2000/09/20 04:07:40 rgb
  56847. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  56848. + * oopsen.
  56849. + *
  56850. + * Revision 1.29 2000/09/12 03:25:02 rgb
  56851. + * Moved radij_c_version printing to ipsec_version_get_info().
  56852. + *
  56853. + * Revision 1.28 2000/09/08 19:12:56 rgb
  56854. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  56855. + *
  56856. + * Revision 1.27 2000/07/28 14:58:32 rgb
  56857. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  56858. + *
  56859. + * Revision 1.26 2000/05/10 23:11:37 rgb
  56860. + * Comment out most of the startup version information.
  56861. + *
  56862. + * Revision 1.25 2000/01/21 06:21:47 rgb
  56863. + * Change return codes to negative on error.
  56864. + *
  56865. + * Revision 1.24 1999/11/18 04:09:20 rgb
  56866. + * Replaced all kernel version macros to shorter, readable form.
  56867. + *
  56868. + * Revision 1.23 1999/11/17 15:53:41 rgb
  56869. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  56870. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  56871. + * klips/net/ipsec/Makefile.
  56872. + *
  56873. + * Revision 1.22 1999/10/15 22:17:28 rgb
  56874. + * Modify radijcleanup() to call radijcleartree().
  56875. + *
  56876. + * Revision 1.21 1999/10/08 18:37:34 rgb
  56877. + * Fix end-of-line spacing to sate whining PHMs.
  56878. + *
  56879. + * Revision 1.20 1999/10/01 15:44:54 rgb
  56880. + * Move spinlock header include to 2.1> scope.
  56881. + *
  56882. + * Revision 1.19 1999/10/01 08:35:52 rgb
  56883. + * Add spinlock include to shut up compiler for 2.0.38.
  56884. + *
  56885. + * Revision 1.18 1999/09/23 18:02:52 rgb
  56886. + * De-alarm the search failure message so it doesn't sound so grave.
  56887. + *
  56888. + * Revision 1.17 1999/05/25 21:26:01 rgb
  56889. + * Fix rj_walktree() sanity checking bug.
  56890. + *
  56891. + * Revision 1.16 1999/05/09 03:25:38 rgb
  56892. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  56893. + *
  56894. + * Revision 1.15 1999/05/05 22:02:33 rgb
  56895. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  56896. + *
  56897. + * Revision 1.14 1999/04/29 15:24:15 rgb
  56898. + * Add sanity checking for null pointer arguments.
  56899. + * Standardise an error return method.
  56900. + *
  56901. + * Revision 1.13 1999/04/11 00:29:02 henry
  56902. + * GPL boilerplate
  56903. + *
  56904. + * Revision 1.12 1999/04/06 04:54:28 rgb
  56905. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  56906. + * patch shell fixes.
  56907. + *
  56908. + * Revision 1.11 1999/02/17 16:52:53 rgb
  56909. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  56910. + * Clean out unused cruft.
  56911. + *
  56912. + * Revision 1.10 1999/01/22 06:30:05 rgb
  56913. + * Cruft clean-out.
  56914. + * 64-bit clean-up.
  56915. + *
  56916. + * Revision 1.9 1998/12/01 13:22:04 rgb
  56917. + * Added support for debug printing of version info.
  56918. + *
  56919. + * Revision 1.8 1998/11/30 13:22:55 rgb
  56920. + * Rationalised all the klips kernel file headers. They are much shorter
  56921. + * now and won't conflict under RH5.2.
  56922. + *
  56923. + * Revision 1.7 1998/10/25 02:43:26 rgb
  56924. + * Change return type on rj_addroute and rj_delete and add and argument
  56925. + * to the latter to be able to transmit more infomation about errors.
  56926. + *
  56927. + * Revision 1.6 1998/10/19 14:30:06 rgb
  56928. + * Added inclusion of freeswan.h.
  56929. + *
  56930. + * Revision 1.5 1998/10/09 04:33:27 rgb
  56931. + * Added 'klips_debug' prefix to all klips printk debug statements.
  56932. + * Fixed output formatting slightly.
  56933. + *
  56934. + * Revision 1.4 1998/07/28 00:06:59 rgb
  56935. + * Add debug detail to tree traversing.
  56936. + *
  56937. + * Revision 1.3 1998/07/14 18:07:58 rgb
  56938. + * Add a routine to clear the eroute tree.
  56939. + *
  56940. + * Revision 1.2 1998/06/25 20:03:22 rgb
  56941. + * Cleanup #endif comments. Debug output for rj_init.
  56942. + *
  56943. + * Revision 1.1 1998/06/18 21:30:22 henry
  56944. + * move sources from klips/src to klips/net/ipsec to keep stupid kernel
  56945. + * build scripts happier about symlinks
  56946. + *
  56947. + * Revision 1.8 1998/05/25 20:34:15 rgb
  56948. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  56949. + *
  56950. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  56951. + * add ipsec_rj_walker_delete.
  56952. + *
  56953. + * Recover memory for eroute table on unload of module.
  56954. + *
  56955. + * Revision 1.7 1998/05/21 12:58:58 rgb
  56956. + * Moved 'extern' definitions to ipsec_radij.h to support /proc 3k limit fix.
  56957. + *
  56958. + * Revision 1.6 1998/04/23 20:57:29 rgb
  56959. + * Cleaned up compiler warnings for unused debugging functions.
  56960. + *
  56961. + * Revision 1.5 1998/04/22 16:51:38 rgb
  56962. + * Tidy up radij debug code from recent rash of modifications to debug code.
  56963. + *
  56964. + * Revision 1.4 1998/04/21 21:28:56 rgb
  56965. + * Rearrange debug switches to change on the fly debug output from user
  56966. + * space. Only kernel changes checked in at this time. radij.c was also
  56967. + * changed to temporarily remove buggy debugging code in rj_delete causing
  56968. + * an OOPS and hence, netlink device open errors.
  56969. + *
  56970. + * Revision 1.3 1998/04/14 17:30:37 rgb
  56971. + * Fix up compiling errors for radij tree memory reclamation.
  56972. + *
  56973. + * Revision 1.2 1998/04/12 22:03:25 rgb
  56974. + * Updated ESP-3DES-HMAC-MD5-96,
  56975. + * ESP-DES-HMAC-MD5-96,
  56976. + * AH-HMAC-MD5-96,
  56977. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  56978. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  56979. + *
  56980. + * Fixed eroute references in /proc/net/ipsec*.
  56981. + *
  56982. + * Started to patch module unloading memory leaks in ipsec_netlink and
  56983. + * radij tree unloading.
  56984. + *
  56985. + * Revision 1.1 1998/04/09 03:06:15 henry
  56986. + * sources moved up from linux/net/ipsec
  56987. + *
  56988. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  56989. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  56990. + *
  56991. + * Revision 0.4 1997/01/15 01:28:15 ji
  56992. + * No changes.
  56993. + *
  56994. + * Revision 0.3 1996/11/20 14:39:04 ji
  56995. + * Minor cleanups.
  56996. + * Rationalized debugging code.
  56997. + *
  56998. + * Revision 0.2 1996/11/02 00:18:33 ji
  56999. + * First limited release.
  57000. + *
  57001. + *
  57002. + */
  57003. --- /dev/null Tue Mar 11 13:02:56 2003
  57004. +++ linux/net/ipsec/rangetoa.c Mon Feb 9 13:51:03 2004
  57005. @@ -0,0 +1,60 @@
  57006. +/*
  57007. + * convert binary form of address range to ASCII
  57008. + * Copyright (C) 1998, 1999 Henry Spencer.
  57009. + *
  57010. + * This library is free software; you can redistribute it and/or modify it
  57011. + * under the terms of the GNU Library General Public License as published by
  57012. + * the Free Software Foundation; either version 2 of the License, or (at your
  57013. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57014. + *
  57015. + * This library is distributed in the hope that it will be useful, but
  57016. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57017. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57018. + * License for more details.
  57019. + *
  57020. + * RCSID $Id: rangetoa.c,v 1.9 2004/07/10 07:48:37 mcr Exp $
  57021. + */
  57022. +#include "openswan.h"
  57023. +
  57024. +/*
  57025. + - rangetoa - convert address range to ASCII
  57026. + */
  57027. +size_t /* space needed for full conversion */
  57028. +rangetoa(addrs, format, dst, dstlen)
  57029. +struct in_addr addrs[2];
  57030. +int format; /* character */
  57031. +char *dst; /* need not be valid if dstlen is 0 */
  57032. +size_t dstlen;
  57033. +{
  57034. + size_t len;
  57035. + size_t rest;
  57036. + int n;
  57037. + char *p;
  57038. +
  57039. + switch (format) {
  57040. + case 0:
  57041. + break;
  57042. + default:
  57043. + return 0;
  57044. + break;
  57045. + }
  57046. +
  57047. + len = addrtoa(addrs[0], 0, dst, dstlen);
  57048. + if (len < dstlen)
  57049. + for (p = dst + len - 1, n = 3; len < dstlen && n > 0;
  57050. + p++, len++, n--)
  57051. + *p = '.';
  57052. + else
  57053. + p = NULL;
  57054. + if (len < dstlen)
  57055. + rest = dstlen - len;
  57056. + else {
  57057. + if (dstlen > 0)
  57058. + *(dst + dstlen - 1) = '\0';
  57059. + rest = 0;
  57060. + }
  57061. +
  57062. + len += addrtoa(addrs[1], 0, p, rest);
  57063. +
  57064. + return len;
  57065. +}
  57066. --- /dev/null Tue Mar 11 13:02:56 2003
  57067. +++ linux/net/ipsec/satot.c Mon Feb 9 13:51:03 2004
  57068. @@ -0,0 +1,133 @@
  57069. +/*
  57070. + * convert from binary form of SA ID to text
  57071. + * Copyright (C) 2000, 2001 Henry Spencer.
  57072. + *
  57073. + * This library is free software; you can redistribute it and/or modify it
  57074. + * under the terms of the GNU Library General Public License as published by
  57075. + * the Free Software Foundation; either version 2 of the License, or (at your
  57076. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57077. + *
  57078. + * This library is distributed in the hope that it will be useful, but
  57079. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57080. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57081. + * License for more details.
  57082. + *
  57083. + * RCSID $Id: satot.c,v 1.13 2004/07/10 07:48:37 mcr Exp $
  57084. + */
  57085. +#include "openswan.h"
  57086. +
  57087. +static struct typename {
  57088. + char type;
  57089. + char *name;
  57090. +} typenames[] = {
  57091. + { SA_AH, "ah" },
  57092. + { SA_ESP, "esp" },
  57093. + { SA_IPIP, "tun" },
  57094. + { SA_COMP, "comp" },
  57095. + { SA_INT, "int" },
  57096. + { 0, NULL }
  57097. +};
  57098. +
  57099. +/*
  57100. + - satot - convert SA to text "ah507@1.2.3.4"
  57101. + */
  57102. +size_t /* space needed for full conversion */
  57103. +satot(sa, format, dst, dstlen)
  57104. +const ip_said *sa;
  57105. +int format; /* character */
  57106. +char *dst; /* need not be valid if dstlen is 0 */
  57107. +size_t dstlen;
  57108. +{
  57109. + size_t len = 0; /* 0 means "not recognized yet" */
  57110. + int base;
  57111. + int showversion; /* use delimiter to show IP version? */
  57112. + struct typename *tn;
  57113. + char *p;
  57114. + char *pre;
  57115. + char buf[10+1+ULTOT_BUF+ADDRTOT_BUF];
  57116. + char unk[10];
  57117. +
  57118. + switch (format) {
  57119. + case 0:
  57120. + base = 16;
  57121. + showversion = 1;
  57122. + break;
  57123. + case 'f':
  57124. + base = 17;
  57125. + showversion = 1;
  57126. + break;
  57127. + case 'x':
  57128. + base = 'x';
  57129. + showversion = 0;
  57130. + break;
  57131. + case 'd':
  57132. + base = 10;
  57133. + showversion = 0;
  57134. + break;
  57135. + default:
  57136. + return 0;
  57137. + break;
  57138. + }
  57139. +
  57140. + memset(buf, 0, sizeof(buf));
  57141. +
  57142. + pre = NULL;
  57143. + for (tn = typenames; tn->name != NULL; tn++)
  57144. + if (sa->proto == tn->type) {
  57145. + pre = tn->name;
  57146. + break; /* NOTE BREAK OUT */
  57147. + }
  57148. + if (pre == NULL) { /* unknown protocol */
  57149. + strcpy(unk, "unk");
  57150. + (void) ultot((unsigned char)sa->proto, 10, unk+strlen(unk),
  57151. + sizeof(unk)-strlen(unk));
  57152. + pre = unk;
  57153. + }
  57154. +
  57155. + if (strcmp(pre, PASSTHROUGHTYPE) == 0 &&
  57156. + sa->spi == PASSTHROUGHSPI &&
  57157. + isunspecaddr(&sa->dst)) {
  57158. + strcpy(buf, (addrtypeof(&sa->dst) == AF_INET) ?
  57159. + PASSTHROUGH4NAME :
  57160. + PASSTHROUGH6NAME);
  57161. + len = strlen(buf);
  57162. + }
  57163. +
  57164. + if (sa->proto == SA_INT) {
  57165. + switch (ntohl(sa->spi)) {
  57166. + case SPI_PASS: p = "%pass"; break;
  57167. + case SPI_DROP: p = "%drop"; break;
  57168. + case SPI_REJECT: p = "%reject"; break;
  57169. + case SPI_HOLD: p = "%hold"; break;
  57170. + case SPI_TRAP: p = "%trap"; break;
  57171. + case SPI_TRAPSUBNET: p = "%trapsubnet"; break;
  57172. + default: p = NULL; break;
  57173. + }
  57174. + if (p != NULL) {
  57175. + strcpy(buf, p);
  57176. + len = strlen(buf);
  57177. + }
  57178. + }
  57179. +
  57180. + if (len == 0) { /* general case needed */
  57181. + strcpy(buf, pre);
  57182. + len = strlen(buf);
  57183. + if (showversion) {
  57184. + *(buf+len) = (addrtypeof(&sa->dst) == AF_INET) ? '.' :
  57185. + ':';
  57186. + len++;
  57187. + *(buf+len) = '\0';
  57188. + }
  57189. + len += ultot(ntohl(sa->spi), base, buf+len, sizeof(buf)-len);
  57190. + *(buf+len-1) = '@';
  57191. + len += addrtot(&sa->dst, 0, buf+len, sizeof(buf)-len);
  57192. + *(buf+len) = '\0';
  57193. + }
  57194. +
  57195. + if (dst != NULL) {
  57196. + if (len > dstlen)
  57197. + *(buf+dstlen-1) = '\0';
  57198. + strcpy(dst, buf);
  57199. + }
  57200. + return len;
  57201. +}
  57202. --- /dev/null Tue Mar 11 13:02:56 2003
  57203. +++ linux/net/ipsec/subnetof.c Mon Feb 9 13:51:03 2004
  57204. @@ -0,0 +1,59 @@
  57205. +/*
  57206. + * minor network-address manipulation utilities
  57207. + * Copyright (C) 1998, 1999 Henry Spencer.
  57208. + *
  57209. + * This library is free software; you can redistribute it and/or modify it
  57210. + * under the terms of the GNU Library General Public License as published by
  57211. + * the Free Software Foundation; either version 2 of the License, or (at your
  57212. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57213. + *
  57214. + * This library is distributed in the hope that it will be useful, but
  57215. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57216. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57217. + * License for more details.
  57218. + *
  57219. + * RCSID $Id: subnetof.c,v 1.8 2004/07/10 07:48:37 mcr Exp $
  57220. + */
  57221. +#include "openswan.h"
  57222. +
  57223. +/*
  57224. + - subnetof - given address and mask, return subnet part
  57225. + */
  57226. +struct in_addr
  57227. +subnetof(addr, mask)
  57228. +struct in_addr addr;
  57229. +struct in_addr mask;
  57230. +{
  57231. + struct in_addr result;
  57232. +
  57233. + result.s_addr = addr.s_addr & mask.s_addr;
  57234. + return result;
  57235. +}
  57236. +
  57237. +/*
  57238. + - hostof - given address and mask, return host part
  57239. + */
  57240. +struct in_addr
  57241. +hostof(addr, mask)
  57242. +struct in_addr addr;
  57243. +struct in_addr mask;
  57244. +{
  57245. + struct in_addr result;
  57246. +
  57247. + result.s_addr = addr.s_addr & ~mask.s_addr;
  57248. + return result;
  57249. +}
  57250. +
  57251. +/*
  57252. + - broadcastof - given (network) address and mask, return broadcast address
  57253. + */
  57254. +struct in_addr
  57255. +broadcastof(addr, mask)
  57256. +struct in_addr addr;
  57257. +struct in_addr mask;
  57258. +{
  57259. + struct in_addr result;
  57260. +
  57261. + result.s_addr = addr.s_addr | ~mask.s_addr;
  57262. + return result;
  57263. +}
  57264. --- /dev/null Tue Mar 11 13:02:56 2003
  57265. +++ linux/net/ipsec/subnettoa.c Mon Feb 9 13:51:03 2004
  57266. @@ -0,0 +1,61 @@
  57267. +/*
  57268. + * convert binary form of subnet description to ASCII
  57269. + * Copyright (C) 1998, 1999 Henry Spencer.
  57270. + *
  57271. + * This library is free software; you can redistribute it and/or modify it
  57272. + * under the terms of the GNU Library General Public License as published by
  57273. + * the Free Software Foundation; either version 2 of the License, or (at your
  57274. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57275. + *
  57276. + * This library is distributed in the hope that it will be useful, but
  57277. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57278. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57279. + * License for more details.
  57280. + *
  57281. + * RCSID $Id: subnettoa.c,v 1.11 2004/07/10 07:48:37 mcr Exp $
  57282. + */
  57283. +#include "openswan.h"
  57284. +
  57285. +/*
  57286. + - subnettoa - convert address and mask to ASCII "addr/mask"
  57287. + * Output expresses the mask as a bit count if possible, else dotted decimal.
  57288. + */
  57289. +size_t /* space needed for full conversion */
  57290. +subnettoa(addr, mask, format, dst, dstlen)
  57291. +struct in_addr addr;
  57292. +struct in_addr mask;
  57293. +int format; /* character */
  57294. +char *dst; /* need not be valid if dstlen is 0 */
  57295. +size_t dstlen;
  57296. +{
  57297. + size_t len;
  57298. + size_t rest;
  57299. + int n;
  57300. + char *p;
  57301. +
  57302. + switch (format) {
  57303. + case 0:
  57304. + break;
  57305. + default:
  57306. + return 0;
  57307. + break;
  57308. + }
  57309. +
  57310. + len = addrtoa(addr, 0, dst, dstlen);
  57311. + if (len < dstlen) {
  57312. + dst[len - 1] = '/';
  57313. + p = dst + len;
  57314. + rest = dstlen - len;
  57315. + } else {
  57316. + p = NULL;
  57317. + rest = 0;
  57318. + }
  57319. +
  57320. + n = masktobits(mask);
  57321. + if (n >= 0)
  57322. + len += ultoa((unsigned long)n, 10, p, rest);
  57323. + else
  57324. + len += addrtoa(mask, 0, p, rest);
  57325. +
  57326. + return len;
  57327. +}
  57328. --- /dev/null Tue Mar 11 13:02:56 2003
  57329. +++ linux/net/ipsec/sysctl_net_ipsec.c Mon Feb 9 13:51:03 2004
  57330. @@ -0,0 +1,199 @@
  57331. +/*
  57332. + * sysctl interface to net IPSEC subsystem.
  57333. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  57334. + *
  57335. + * This program is free software; you can redistribute it and/or modify it
  57336. + * under the terms of the GNU General Public License as published by the
  57337. + * Free Software Foundation; either version 2 of the License, or (at your
  57338. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  57339. + *
  57340. + * This program is distributed in the hope that it will be useful, but
  57341. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57342. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  57343. + * for more details.
  57344. + *
  57345. + * RCSID $Id: sysctl_net_ipsec.c,v 1.17 2004/07/10 19:11:18 mcr Exp $
  57346. + */
  57347. +
  57348. +/* -*- linux-c -*-
  57349. + *
  57350. + * Initiated April 3, 1998, Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  57351. + */
  57352. +
  57353. +#include <linux/mm.h>
  57354. +#include <linux/sysctl.h>
  57355. +
  57356. +#include "openswan/ipsec_param.h"
  57357. +
  57358. +#ifdef CONFIG_SYSCTL
  57359. +
  57360. +#define NET_IPSEC 2112 /* Random number */
  57361. +#ifdef CONFIG_KLIPS_DEBUG
  57362. +extern int debug_ah;
  57363. +extern int debug_esp;
  57364. +extern int debug_tunnel;
  57365. +extern int debug_eroute;
  57366. +extern int debug_spi;
  57367. +extern int debug_radij;
  57368. +extern int debug_netlink;
  57369. +extern int debug_xform;
  57370. +extern int debug_rcv;
  57371. +extern int debug_pfkey;
  57372. +extern int sysctl_ipsec_debug_verbose;
  57373. +#ifdef CONFIG_KLIPS_IPCOMP
  57374. +extern int sysctl_ipsec_debug_ipcomp;
  57375. +#endif /* CONFIG_KLIPS_IPCOMP */
  57376. +#endif /* CONFIG_KLIPS_DEBUG */
  57377. +
  57378. +extern int sysctl_ipsec_icmp;
  57379. +extern int sysctl_ipsec_inbound_policy_check;
  57380. +extern int sysctl_ipsec_tos;
  57381. +int sysctl_ipsec_regress_pfkey_lossage;
  57382. +
  57383. +enum {
  57384. +#ifdef CONFIG_KLIPS_DEBUG
  57385. + NET_IPSEC_DEBUG_AH=1,
  57386. + NET_IPSEC_DEBUG_ESP=2,
  57387. + NET_IPSEC_DEBUG_TUNNEL=3,
  57388. + NET_IPSEC_DEBUG_EROUTE=4,
  57389. + NET_IPSEC_DEBUG_SPI=5,
  57390. + NET_IPSEC_DEBUG_RADIJ=6,
  57391. + NET_IPSEC_DEBUG_NETLINK=7,
  57392. + NET_IPSEC_DEBUG_XFORM=8,
  57393. + NET_IPSEC_DEBUG_RCV=9,
  57394. + NET_IPSEC_DEBUG_PFKEY=10,
  57395. + NET_IPSEC_DEBUG_VERBOSE=11,
  57396. + NET_IPSEC_DEBUG_IPCOMP=12,
  57397. +#endif /* CONFIG_KLIPS_DEBUG */
  57398. + NET_IPSEC_ICMP=13,
  57399. + NET_IPSEC_INBOUND_POLICY_CHECK=14,
  57400. + NET_IPSEC_TOS=15,
  57401. + NET_IPSEC_REGRESS_PFKEY_LOSSAGE=16,
  57402. +};
  57403. +
  57404. +static ctl_table ipsec_table[] = {
  57405. +#ifdef CONFIG_KLIPS_DEBUG
  57406. + { NET_IPSEC_DEBUG_AH, "debug_ah", &debug_ah,
  57407. + sizeof(int), 0644, NULL, &proc_dointvec},
  57408. + { NET_IPSEC_DEBUG_ESP, "debug_esp", &debug_esp,
  57409. + sizeof(int), 0644, NULL, &proc_dointvec},
  57410. + { NET_IPSEC_DEBUG_TUNNEL, "debug_tunnel", &debug_tunnel,
  57411. + sizeof(int), 0644, NULL, &proc_dointvec},
  57412. + { NET_IPSEC_DEBUG_EROUTE, "debug_eroute", &debug_eroute,
  57413. + sizeof(int), 0644, NULL, &proc_dointvec},
  57414. + { NET_IPSEC_DEBUG_SPI, "debug_spi", &debug_spi,
  57415. + sizeof(int), 0644, NULL, &proc_dointvec},
  57416. + { NET_IPSEC_DEBUG_RADIJ, "debug_radij", &debug_radij,
  57417. + sizeof(int), 0644, NULL, &proc_dointvec},
  57418. + { NET_IPSEC_DEBUG_NETLINK, "debug_netlink", &debug_netlink,
  57419. + sizeof(int), 0644, NULL, &proc_dointvec},
  57420. + { NET_IPSEC_DEBUG_XFORM, "debug_xform", &debug_xform,
  57421. + sizeof(int), 0644, NULL, &proc_dointvec},
  57422. + { NET_IPSEC_DEBUG_RCV, "debug_rcv", &debug_rcv,
  57423. + sizeof(int), 0644, NULL, &proc_dointvec},
  57424. + { NET_IPSEC_DEBUG_PFKEY, "debug_pfkey", &debug_pfkey,
  57425. + sizeof(int), 0644, NULL, &proc_dointvec},
  57426. + { NET_IPSEC_DEBUG_VERBOSE, "debug_verbose",&sysctl_ipsec_debug_verbose,
  57427. + sizeof(int), 0644, NULL, &proc_dointvec},
  57428. +#ifdef CONFIG_KLIPS_IPCOMP
  57429. + { NET_IPSEC_DEBUG_IPCOMP, "debug_ipcomp", &sysctl_ipsec_debug_ipcomp,
  57430. + sizeof(int), 0644, NULL, &proc_dointvec},
  57431. +#endif /* CONFIG_KLIPS_IPCOMP */
  57432. +
  57433. +#ifdef CONFIG_KLIPS_REGRESS
  57434. + { NET_IPSEC_REGRESS_PFKEY_LOSSAGE, "pfkey_lossage",
  57435. + &sysctl_ipsec_regress_pfkey_lossage,
  57436. + sizeof(int), 0644, NULL, &proc_dointvec},
  57437. +#endif /* CONFIG_KLIPS_REGRESS */
  57438. +
  57439. +#endif /* CONFIG_KLIPS_DEBUG */
  57440. + { NET_IPSEC_ICMP, "icmp", &sysctl_ipsec_icmp,
  57441. + sizeof(int), 0644, NULL, &proc_dointvec},
  57442. + { NET_IPSEC_INBOUND_POLICY_CHECK, "inbound_policy_check", &sysctl_ipsec_inbound_policy_check,
  57443. + sizeof(int), 0644, NULL, &proc_dointvec},
  57444. + { NET_IPSEC_TOS, "tos", &sysctl_ipsec_tos,
  57445. + sizeof(int), 0644, NULL, &proc_dointvec},
  57446. + {0}
  57447. +};
  57448. +
  57449. +static ctl_table ipsec_net_table[] = {
  57450. + { NET_IPSEC, "ipsec", NULL, 0, 0555, ipsec_table },
  57451. + { 0 }
  57452. +};
  57453. +
  57454. +static ctl_table ipsec_root_table[] = {
  57455. + { CTL_NET, "net", NULL, 0, 0555, ipsec_net_table },
  57456. + { 0 }
  57457. +};
  57458. +
  57459. +static struct ctl_table_header *ipsec_table_header;
  57460. +
  57461. +int ipsec_sysctl_register(void)
  57462. +{
  57463. + ipsec_table_header = register_sysctl_table(ipsec_root_table, 0);
  57464. + if (!ipsec_table_header) {
  57465. + return -ENOMEM;
  57466. + }
  57467. + return 0;
  57468. +}
  57469. +
  57470. +void ipsec_sysctl_unregister(void)
  57471. +{
  57472. + unregister_sysctl_table(ipsec_table_header);
  57473. +}
  57474. +
  57475. +#endif /* CONFIG_SYSCTL */
  57476. +
  57477. +/*
  57478. + * $Log: sysctl_net_ipsec.c,v $
  57479. + * Revision 1.17 2004/07/10 19:11:18 mcr
  57480. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  57481. + *
  57482. + * Revision 1.16 2004/04/06 02:49:26 mcr
  57483. + * pullup of algo code from alg-branch.
  57484. + *
  57485. + * Revision 1.15 2002/04/24 07:55:32 mcr
  57486. + * #include patches and Makefiles for post-reorg compilation.
  57487. + *
  57488. + * Revision 1.14 2002/04/24 07:36:35 mcr
  57489. + * Moved from ./klips/net/ipsec/sysctl_net_ipsec.c,v
  57490. + *
  57491. + * Revision 1.13 2002/01/12 02:58:32 mcr
  57492. + * first regression test causes acquire messages to be lost
  57493. + * 100% of the time. This is to help testing of pluto.
  57494. + *
  57495. + * Revision 1.12 2001/06/14 19:35:13 rgb
  57496. + * Update copyright date.
  57497. + *
  57498. + * Revision 1.11 2001/02/26 19:58:13 rgb
  57499. + * Drop sysctl_ipsec_{no_eroute_pass,opportunistic}, replaced by magic SAs.
  57500. + *
  57501. + * Revision 1.10 2000/09/16 01:50:15 rgb
  57502. + * Protect sysctl_ipsec_debug_ipcomp with compiler defines too so that the
  57503. + * linker won't blame rj_delete() for missing symbols. ;-> Damn statics...
  57504. + *
  57505. + * Revision 1.9 2000/09/15 23:17:51 rgb
  57506. + * Moved stuff around to compile with debug off.
  57507. + *
  57508. + * Revision 1.8 2000/09/15 11:37:02 rgb
  57509. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  57510. + * IPCOMP zlib deflate code.
  57511. + *
  57512. + * Revision 1.7 2000/09/15 07:37:15 rgb
  57513. + * Munged silly log comment that was causing a warning.
  57514. + *
  57515. + * Revision 1.6 2000/09/15 04:58:23 rgb
  57516. + * Added tos runtime switch.
  57517. + * Removed 'sysctl_ipsec_' prefix from /proc/sys/net/ipsec/ filenames.
  57518. + *
  57519. + * Revision 1.5 2000/09/12 03:25:28 rgb
  57520. + * Filled in and implemented sysctl.
  57521. + *
  57522. + * Revision 1.4 1999/04/11 00:29:03 henry
  57523. + * GPL boilerplate
  57524. + *
  57525. + * Revision 1.3 1999/04/06 04:54:29 rgb
  57526. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  57527. + * patch shell fixes.
  57528. + *
  57529. + */
  57530. --- /dev/null Tue Mar 11 13:02:56 2003
  57531. +++ linux/net/ipsec/trees.c Mon Feb 9 13:51:03 2004
  57532. @@ -0,0 +1,1214 @@
  57533. +/* trees.c -- output deflated data using Huffman coding
  57534. + * Copyright (C) 1995-2002 Jean-loup Gailly
  57535. + * For conditions of distribution and use, see copyright notice in zlib.h
  57536. + */
  57537. +
  57538. +/*
  57539. + * ALGORITHM
  57540. + *
  57541. + * The "deflation" process uses several Huffman trees. The more
  57542. + * common source values are represented by shorter bit sequences.
  57543. + *
  57544. + * Each code tree is stored in a compressed form which is itself
  57545. + * a Huffman encoding of the lengths of all the code strings (in
  57546. + * ascending order by source values). The actual code strings are
  57547. + * reconstructed from the lengths in the inflate process, as described
  57548. + * in the deflate specification.
  57549. + *
  57550. + * REFERENCES
  57551. + *
  57552. + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  57553. + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  57554. + *
  57555. + * Storer, James A.
  57556. + * Data Compression: Methods and Theory, pp. 49-50.
  57557. + * Computer Science Press, 1988. ISBN 0-7167-8156-5.
  57558. + *
  57559. + * Sedgewick, R.
  57560. + * Algorithms, p290.
  57561. + * Addison-Wesley, 1983. ISBN 0-201-06672-6.
  57562. + */
  57563. +
  57564. +/* @(#) $Id: trees.c,v 1.4 2004/07/10 07:48:39 mcr Exp $ */
  57565. +
  57566. +/* #define GEN_TREES_H */
  57567. +
  57568. +#include "deflate.h"
  57569. +
  57570. +#ifdef DEBUG
  57571. +# include <ctype.h>
  57572. +#endif
  57573. +
  57574. +/* ===========================================================================
  57575. + * Constants
  57576. + */
  57577. +
  57578. +#define MAX_BL_BITS 7
  57579. +/* Bit length codes must not exceed MAX_BL_BITS bits */
  57580. +
  57581. +#define END_BLOCK 256
  57582. +/* end of block literal code */
  57583. +
  57584. +#define REP_3_6 16
  57585. +/* repeat previous bit length 3-6 times (2 bits of repeat count) */
  57586. +
  57587. +#define REPZ_3_10 17
  57588. +/* repeat a zero length 3-10 times (3 bits of repeat count) */
  57589. +
  57590. +#define REPZ_11_138 18
  57591. +/* repeat a zero length 11-138 times (7 bits of repeat count) */
  57592. +
  57593. +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
  57594. + = {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};
  57595. +
  57596. +local const int extra_dbits[D_CODES] /* extra bits for each distance code */
  57597. + = {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};
  57598. +
  57599. +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
  57600. + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  57601. +
  57602. +local const uch bl_order[BL_CODES]
  57603. + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  57604. +/* The lengths of the bit length codes are sent in order of decreasing
  57605. + * probability, to avoid transmitting the lengths for unused bit length codes.
  57606. + */
  57607. +
  57608. +#define Buf_size (8 * 2*sizeof(char))
  57609. +/* Number of bits used within bi_buf. (bi_buf might be implemented on
  57610. + * more than 16 bits on some systems.)
  57611. + */
  57612. +
  57613. +/* ===========================================================================
  57614. + * Local data. These are initialized only once.
  57615. + */
  57616. +
  57617. +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
  57618. +
  57619. +#if defined(GEN_TREES_H) || !defined(STDC)
  57620. +/* non ANSI compilers may not accept trees.h */
  57621. +
  57622. +local ct_data static_ltree[L_CODES+2];
  57623. +/* The static literal tree. Since the bit lengths are imposed, there is no
  57624. + * need for the L_CODES extra codes used during heap construction. However
  57625. + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  57626. + * below).
  57627. + */
  57628. +
  57629. +local ct_data static_dtree[D_CODES];
  57630. +/* The static distance tree. (Actually a trivial tree since all codes use
  57631. + * 5 bits.)
  57632. + */
  57633. +
  57634. +uch _dist_code[DIST_CODE_LEN];
  57635. +/* Distance codes. The first 256 values correspond to the distances
  57636. + * 3 .. 258, the last 256 values correspond to the top 8 bits of
  57637. + * the 15 bit distances.
  57638. + */
  57639. +
  57640. +uch _length_code[MAX_MATCH-MIN_MATCH+1];
  57641. +/* length code for each normalized match length (0 == MIN_MATCH) */
  57642. +
  57643. +local int base_length[LENGTH_CODES];
  57644. +/* First normalized length for each code (0 = MIN_MATCH) */
  57645. +
  57646. +local int base_dist[D_CODES];
  57647. +/* First normalized distance for each code (0 = distance of 1) */
  57648. +
  57649. +#else
  57650. +# include "trees.h"
  57651. +#endif /* GEN_TREES_H */
  57652. +
  57653. +struct static_tree_desc_s {
  57654. + const ct_data *static_tree; /* static tree or NULL */
  57655. + const intf *extra_bits; /* extra bits for each code or NULL */
  57656. + int extra_base; /* base index for extra_bits */
  57657. + int elems; /* max number of elements in the tree */
  57658. + int max_length; /* max bit length for the codes */
  57659. +};
  57660. +
  57661. +local static_tree_desc static_l_desc =
  57662. +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
  57663. +
  57664. +local static_tree_desc static_d_desc =
  57665. +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
  57666. +
  57667. +local static_tree_desc static_bl_desc =
  57668. +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
  57669. +
  57670. +/* ===========================================================================
  57671. + * Local (static) routines in this file.
  57672. + */
  57673. +
  57674. +local void tr_static_init OF((void));
  57675. +local void init_block OF((deflate_state *s));
  57676. +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
  57677. +local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
  57678. +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
  57679. +local void build_tree OF((deflate_state *s, tree_desc *desc));
  57680. +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
  57681. +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
  57682. +local int build_bl_tree OF((deflate_state *s));
  57683. +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
  57684. + int blcodes));
  57685. +local void compress_block OF((deflate_state *s, const ct_data *ltree,
  57686. + const ct_data *dtree));
  57687. +local void set_data_type OF((deflate_state *s));
  57688. +local unsigned bi_reverse OF((unsigned value, int length));
  57689. +local void bi_windup OF((deflate_state *s));
  57690. +local void bi_flush OF((deflate_state *s));
  57691. +local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
  57692. + int header));
  57693. +
  57694. +#ifdef GEN_TREES_H
  57695. +local void gen_trees_header OF((void));
  57696. +#endif
  57697. +
  57698. +#ifndef DEBUG
  57699. +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
  57700. + /* Send a code of the given tree. c and tree must not have side effects */
  57701. +
  57702. +#else /* DEBUG */
  57703. +# define send_code(s, c, tree) \
  57704. + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
  57705. + send_bits(s, tree[c].Code, tree[c].Len); }
  57706. +#endif
  57707. +
  57708. +/* ===========================================================================
  57709. + * Output a short LSB first on the stream.
  57710. + * IN assertion: there is enough room in pendingBuf.
  57711. + */
  57712. +#define put_short(s, w) { \
  57713. + put_byte(s, (uch)((w) & 0xff)); \
  57714. + put_byte(s, (uch)((ush)(w) >> 8)); \
  57715. +}
  57716. +
  57717. +/* ===========================================================================
  57718. + * Send a value on a given number of bits.
  57719. + * IN assertion: length <= 16 and value fits in length bits.
  57720. + */
  57721. +#ifdef DEBUG
  57722. +local void send_bits OF((deflate_state *s, int value, int length));
  57723. +
  57724. +local void send_bits(s, value, length)
  57725. + deflate_state *s;
  57726. + int value; /* value to send */
  57727. + int length; /* number of bits */
  57728. +{
  57729. + Tracevv((stderr," l %2d v %4x ", length, value));
  57730. + Assert(length > 0 && length <= 15, "invalid length");
  57731. + s->bits_sent += (ulg)length;
  57732. +
  57733. + /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  57734. + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  57735. + * unused bits in value.
  57736. + */
  57737. + if (s->bi_valid > (int)Buf_size - length) {
  57738. + s->bi_buf |= (value << s->bi_valid);
  57739. + put_short(s, s->bi_buf);
  57740. + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
  57741. + s->bi_valid += length - Buf_size;
  57742. + } else {
  57743. + s->bi_buf |= value << s->bi_valid;
  57744. + s->bi_valid += length;
  57745. + }
  57746. +}
  57747. +#else /* !DEBUG */
  57748. +
  57749. +#define send_bits(s, value, length) \
  57750. +{ int len = length;\
  57751. + if (s->bi_valid > (int)Buf_size - len) {\
  57752. + int val = value;\
  57753. + s->bi_buf |= (val << s->bi_valid);\
  57754. + put_short(s, s->bi_buf);\
  57755. + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
  57756. + s->bi_valid += len - Buf_size;\
  57757. + } else {\
  57758. + s->bi_buf |= (value) << s->bi_valid;\
  57759. + s->bi_valid += len;\
  57760. + }\
  57761. +}
  57762. +#endif /* DEBUG */
  57763. +
  57764. +
  57765. +#define MAX(a,b) (a >= b ? a : b)
  57766. +/* the arguments must not have side effects */
  57767. +
  57768. +/* ===========================================================================
  57769. + * Initialize the various 'constant' tables.
  57770. + */
  57771. +local void tr_static_init()
  57772. +{
  57773. +#if defined(GEN_TREES_H) || !defined(STDC)
  57774. + static int static_init_done = 0;
  57775. + int n; /* iterates over tree elements */
  57776. + int bits; /* bit counter */
  57777. + int length; /* length value */
  57778. + int code; /* code value */
  57779. + int dist; /* distance index */
  57780. + ush bl_count[MAX_BITS+1];
  57781. + /* number of codes at each bit length for an optimal tree */
  57782. +
  57783. + if (static_init_done) return;
  57784. +
  57785. + /* For some embedded targets, global variables are not initialized: */
  57786. + static_l_desc.static_tree = static_ltree;
  57787. + static_l_desc.extra_bits = extra_lbits;
  57788. + static_d_desc.static_tree = static_dtree;
  57789. + static_d_desc.extra_bits = extra_dbits;
  57790. + static_bl_desc.extra_bits = extra_blbits;
  57791. +
  57792. + /* Initialize the mapping length (0..255) -> length code (0..28) */
  57793. + length = 0;
  57794. + for (code = 0; code < LENGTH_CODES-1; code++) {
  57795. + base_length[code] = length;
  57796. + for (n = 0; n < (1<<extra_lbits[code]); n++) {
  57797. + _length_code[length++] = (uch)code;
  57798. + }
  57799. + }
  57800. + Assert (length == 256, "tr_static_init: length != 256");
  57801. + /* Note that the length 255 (match length 258) can be represented
  57802. + * in two different ways: code 284 + 5 bits or code 285, so we
  57803. + * overwrite length_code[255] to use the best encoding:
  57804. + */
  57805. + _length_code[length-1] = (uch)code;
  57806. +
  57807. + /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  57808. + dist = 0;
  57809. + for (code = 0 ; code < 16; code++) {
  57810. + base_dist[code] = dist;
  57811. + for (n = 0; n < (1<<extra_dbits[code]); n++) {
  57812. + _dist_code[dist++] = (uch)code;
  57813. + }
  57814. + }
  57815. + Assert (dist == 256, "tr_static_init: dist != 256");
  57816. + dist >>= 7; /* from now on, all distances are divided by 128 */
  57817. + for ( ; code < D_CODES; code++) {
  57818. + base_dist[code] = dist << 7;
  57819. + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  57820. + _dist_code[256 + dist++] = (uch)code;
  57821. + }
  57822. + }
  57823. + Assert (dist == 256, "tr_static_init: 256+dist != 512");
  57824. +
  57825. + /* Construct the codes of the static literal tree */
  57826. + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  57827. + n = 0;
  57828. + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
  57829. + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
  57830. + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
  57831. + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
  57832. + /* Codes 286 and 287 do not exist, but we must include them in the
  57833. + * tree construction to get a canonical Huffman tree (longest code
  57834. + * all ones)
  57835. + */
  57836. + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
  57837. +
  57838. + /* The static distance tree is trivial: */
  57839. + for (n = 0; n < D_CODES; n++) {
  57840. + static_dtree[n].Len = 5;
  57841. + static_dtree[n].Code = bi_reverse((unsigned)n, 5);
  57842. + }
  57843. + static_init_done = 1;
  57844. +
  57845. +# ifdef GEN_TREES_H
  57846. + gen_trees_header();
  57847. +# endif
  57848. +#endif /* defined(GEN_TREES_H) || !defined(STDC) */
  57849. +}
  57850. +
  57851. +/* ===========================================================================
  57852. + * Genererate the file trees.h describing the static trees.
  57853. + */
  57854. +#ifdef GEN_TREES_H
  57855. +# ifndef DEBUG
  57856. +# include <stdio.h>
  57857. +# endif
  57858. +
  57859. +# define SEPARATOR(i, last, width) \
  57860. + ((i) == (last)? "\n};\n\n" : \
  57861. + ((i) % (width) == (width)-1 ? ",\n" : ", "))
  57862. +
  57863. +void gen_trees_header()
  57864. +{
  57865. + FILE *header = fopen("trees.h", "w");
  57866. + int i;
  57867. +
  57868. + Assert (header != NULL, "Can't open trees.h");
  57869. + fprintf(header,
  57870. + "/* header created automatically with -DGEN_TREES_H */\n\n");
  57871. +
  57872. + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
  57873. + for (i = 0; i < L_CODES+2; i++) {
  57874. + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
  57875. + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
  57876. + }
  57877. +
  57878. + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
  57879. + for (i = 0; i < D_CODES; i++) {
  57880. + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
  57881. + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
  57882. + }
  57883. +
  57884. + fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
  57885. + for (i = 0; i < DIST_CODE_LEN; i++) {
  57886. + fprintf(header, "%2u%s", _dist_code[i],
  57887. + SEPARATOR(i, DIST_CODE_LEN-1, 20));
  57888. + }
  57889. +
  57890. + fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
  57891. + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
  57892. + fprintf(header, "%2u%s", _length_code[i],
  57893. + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
  57894. + }
  57895. +
  57896. + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
  57897. + for (i = 0; i < LENGTH_CODES; i++) {
  57898. + fprintf(header, "%1u%s", base_length[i],
  57899. + SEPARATOR(i, LENGTH_CODES-1, 20));
  57900. + }
  57901. +
  57902. + fprintf(header, "local const int base_dist[D_CODES] = {\n");
  57903. + for (i = 0; i < D_CODES; i++) {
  57904. + fprintf(header, "%5u%s", base_dist[i],
  57905. + SEPARATOR(i, D_CODES-1, 10));
  57906. + }
  57907. +
  57908. + fclose(header);
  57909. +}
  57910. +#endif /* GEN_TREES_H */
  57911. +
  57912. +/* ===========================================================================
  57913. + * Initialize the tree data structures for a new zlib stream.
  57914. + */
  57915. +void _tr_init(s)
  57916. + deflate_state *s;
  57917. +{
  57918. + tr_static_init();
  57919. +
  57920. + s->l_desc.dyn_tree = s->dyn_ltree;
  57921. + s->l_desc.stat_desc = &static_l_desc;
  57922. +
  57923. + s->d_desc.dyn_tree = s->dyn_dtree;
  57924. + s->d_desc.stat_desc = &static_d_desc;
  57925. +
  57926. + s->bl_desc.dyn_tree = s->bl_tree;
  57927. + s->bl_desc.stat_desc = &static_bl_desc;
  57928. +
  57929. + s->bi_buf = 0;
  57930. + s->bi_valid = 0;
  57931. + s->last_eob_len = 8; /* enough lookahead for inflate */
  57932. +#ifdef DEBUG
  57933. + s->compressed_len = 0L;
  57934. + s->bits_sent = 0L;
  57935. +#endif
  57936. +
  57937. + /* Initialize the first block of the first file: */
  57938. + init_block(s);
  57939. +}
  57940. +
  57941. +/* ===========================================================================
  57942. + * Initialize a new block.
  57943. + */
  57944. +local void init_block(s)
  57945. + deflate_state *s;
  57946. +{
  57947. + int n; /* iterates over tree elements */
  57948. +
  57949. + /* Initialize the trees. */
  57950. + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
  57951. + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
  57952. + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
  57953. +
  57954. + s->dyn_ltree[END_BLOCK].Freq = 1;
  57955. + s->opt_len = s->static_len = 0L;
  57956. + s->last_lit = s->matches = 0;
  57957. +}
  57958. +
  57959. +#define SMALLEST 1
  57960. +/* Index within the heap array of least frequent node in the Huffman tree */
  57961. +
  57962. +
  57963. +/* ===========================================================================
  57964. + * Remove the smallest element from the heap and recreate the heap with
  57965. + * one less element. Updates heap and heap_len.
  57966. + */
  57967. +#define pqremove(s, tree, top) \
  57968. +{\
  57969. + top = s->heap[SMALLEST]; \
  57970. + s->heap[SMALLEST] = s->heap[s->heap_len--]; \
  57971. + pqdownheap(s, tree, SMALLEST); \
  57972. +}
  57973. +
  57974. +/* ===========================================================================
  57975. + * Compares to subtrees, using the tree depth as tie breaker when
  57976. + * the subtrees have equal frequency. This minimizes the worst case length.
  57977. + */
  57978. +#define smaller(tree, n, m, depth) \
  57979. + (tree[n].Freq < tree[m].Freq || \
  57980. + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
  57981. +
  57982. +/* ===========================================================================
  57983. + * Restore the heap property by moving down the tree starting at node k,
  57984. + * exchanging a node with the smallest of its two sons if necessary, stopping
  57985. + * when the heap property is re-established (each father smaller than its
  57986. + * two sons).
  57987. + */
  57988. +local void pqdownheap(s, tree, k)
  57989. + deflate_state *s;
  57990. + ct_data *tree; /* the tree to restore */
  57991. + int k; /* node to move down */
  57992. +{
  57993. + int v = s->heap[k];
  57994. + int j = k << 1; /* left son of k */
  57995. + while (j <= s->heap_len) {
  57996. + /* Set j to the smallest of the two sons: */
  57997. + if (j < s->heap_len &&
  57998. + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
  57999. + j++;
  58000. + }
  58001. + /* Exit if v is smaller than both sons */
  58002. + if (smaller(tree, v, s->heap[j], s->depth)) break;
  58003. +
  58004. + /* Exchange v with the smallest son */
  58005. + s->heap[k] = s->heap[j]; k = j;
  58006. +
  58007. + /* And continue down the tree, setting j to the left son of k */
  58008. + j <<= 1;
  58009. + }
  58010. + s->heap[k] = v;
  58011. +}
  58012. +
  58013. +/* ===========================================================================
  58014. + * Compute the optimal bit lengths for a tree and update the total bit length
  58015. + * for the current block.
  58016. + * IN assertion: the fields freq and dad are set, heap[heap_max] and
  58017. + * above are the tree nodes sorted by increasing frequency.
  58018. + * OUT assertions: the field len is set to the optimal bit length, the
  58019. + * array bl_count contains the frequencies for each bit length.
  58020. + * The length opt_len is updated; static_len is also updated if stree is
  58021. + * not null.
  58022. + */
  58023. +local void gen_bitlen(s, desc)
  58024. + deflate_state *s;
  58025. + tree_desc *desc; /* the tree descriptor */
  58026. +{
  58027. + ct_data *tree = desc->dyn_tree;
  58028. + int max_code = desc->max_code;
  58029. + const ct_data *stree = desc->stat_desc->static_tree;
  58030. + const intf *extra = desc->stat_desc->extra_bits;
  58031. + int base = desc->stat_desc->extra_base;
  58032. + int max_length = desc->stat_desc->max_length;
  58033. + int h; /* heap index */
  58034. + int n, m; /* iterate over the tree elements */
  58035. + int bits; /* bit length */
  58036. + int xbits; /* extra bits */
  58037. + ush f; /* frequency */
  58038. + int overflow = 0; /* number of elements with bit length too large */
  58039. +
  58040. + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
  58041. +
  58042. + /* In a first pass, compute the optimal bit lengths (which may
  58043. + * overflow in the case of the bit length tree).
  58044. + */
  58045. + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
  58046. +
  58047. + for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
  58048. + n = s->heap[h];
  58049. + bits = tree[tree[n].Dad].Len + 1;
  58050. + if (bits > max_length) bits = max_length, overflow++;
  58051. + tree[n].Len = (ush)bits;
  58052. + /* We overwrite tree[n].Dad which is no longer needed */
  58053. +
  58054. + if (n > max_code) continue; /* not a leaf node */
  58055. +
  58056. + s->bl_count[bits]++;
  58057. + xbits = 0;
  58058. + if (n >= base) xbits = extra[n-base];
  58059. + f = tree[n].Freq;
  58060. + s->opt_len += (ulg)f * (bits + xbits);
  58061. + if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
  58062. + }
  58063. + if (overflow == 0) return;
  58064. +
  58065. + Trace((stderr,"\nbit length overflow\n"));
  58066. + /* This happens for example on obj2 and pic of the Calgary corpus */
  58067. +
  58068. + /* Find the first bit length which could increase: */
  58069. + do {
  58070. + bits = max_length-1;
  58071. + while (s->bl_count[bits] == 0) bits--;
  58072. + s->bl_count[bits]--; /* move one leaf down the tree */
  58073. + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
  58074. + s->bl_count[max_length]--;
  58075. + /* The brother of the overflow item also moves one step up,
  58076. + * but this does not affect bl_count[max_length]
  58077. + */
  58078. + overflow -= 2;
  58079. + } while (overflow > 0);
  58080. +
  58081. + /* Now recompute all bit lengths, scanning in increasing frequency.
  58082. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  58083. + * lengths instead of fixing only the wrong ones. This idea is taken
  58084. + * from 'ar' written by Haruhiko Okumura.)
  58085. + */
  58086. + for (bits = max_length; bits != 0; bits--) {
  58087. + n = s->bl_count[bits];
  58088. + while (n != 0) {
  58089. + m = s->heap[--h];
  58090. + if (m > max_code) continue;
  58091. + if (tree[m].Len != (unsigned) bits) {
  58092. + Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  58093. + s->opt_len += ((long)bits - (long)tree[m].Len)
  58094. + *(long)tree[m].Freq;
  58095. + tree[m].Len = (ush)bits;
  58096. + }
  58097. + n--;
  58098. + }
  58099. + }
  58100. +}
  58101. +
  58102. +/* ===========================================================================
  58103. + * Generate the codes for a given tree and bit counts (which need not be
  58104. + * optimal).
  58105. + * IN assertion: the array bl_count contains the bit length statistics for
  58106. + * the given tree and the field len is set for all tree elements.
  58107. + * OUT assertion: the field code is set for all tree elements of non
  58108. + * zero code length.
  58109. + */
  58110. +local void gen_codes (tree, max_code, bl_count)
  58111. + ct_data *tree; /* the tree to decorate */
  58112. + int max_code; /* largest code with non zero frequency */
  58113. + ushf *bl_count; /* number of codes at each bit length */
  58114. +{
  58115. + ush next_code[MAX_BITS+1]; /* next code value for each bit length */
  58116. + ush code = 0; /* running code value */
  58117. + int bits; /* bit index */
  58118. + int n; /* code index */
  58119. +
  58120. + /* The distribution counts are first used to generate the code values
  58121. + * without bit reversal.
  58122. + */
  58123. + for (bits = 1; bits <= MAX_BITS; bits++) {
  58124. + next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  58125. + }
  58126. + /* Check that the bit counts in bl_count are consistent. The last code
  58127. + * must be all ones.
  58128. + */
  58129. + Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  58130. + "inconsistent bit counts");
  58131. + Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  58132. +
  58133. + for (n = 0; n <= max_code; n++) {
  58134. + int len = tree[n].Len;
  58135. + if (len == 0) continue;
  58136. + /* Now reverse the bits */
  58137. + tree[n].Code = bi_reverse(next_code[len]++, len);
  58138. +
  58139. + Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  58140. + n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  58141. + }
  58142. +}
  58143. +
  58144. +/* ===========================================================================
  58145. + * Construct one Huffman tree and assigns the code bit strings and lengths.
  58146. + * Update the total bit length for the current block.
  58147. + * IN assertion: the field freq is set for all tree elements.
  58148. + * OUT assertions: the fields len and code are set to the optimal bit length
  58149. + * and corresponding code. The length opt_len is updated; static_len is
  58150. + * also updated if stree is not null. The field max_code is set.
  58151. + */
  58152. +local void build_tree(s, desc)
  58153. + deflate_state *s;
  58154. + tree_desc *desc; /* the tree descriptor */
  58155. +{
  58156. + ct_data *tree = desc->dyn_tree;
  58157. + const ct_data *stree = desc->stat_desc->static_tree;
  58158. + int elems = desc->stat_desc->elems;
  58159. + int n, m; /* iterate over heap elements */
  58160. + int max_code = -1; /* largest code with non zero frequency */
  58161. + int node; /* new node being created */
  58162. +
  58163. + /* Construct the initial heap, with least frequent element in
  58164. + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  58165. + * heap[0] is not used.
  58166. + */
  58167. + s->heap_len = 0, s->heap_max = HEAP_SIZE;
  58168. +
  58169. + for (n = 0; n < elems; n++) {
  58170. + if (tree[n].Freq != 0) {
  58171. + s->heap[++(s->heap_len)] = max_code = n;
  58172. + s->depth[n] = 0;
  58173. + } else {
  58174. + tree[n].Len = 0;
  58175. + }
  58176. + }
  58177. +
  58178. + /* The pkzip format requires that at least one distance code exists,
  58179. + * and that at least one bit should be sent even if there is only one
  58180. + * possible code. So to avoid special checks later on we force at least
  58181. + * two codes of non zero frequency.
  58182. + */
  58183. + while (s->heap_len < 2) {
  58184. + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
  58185. + tree[node].Freq = 1;
  58186. + s->depth[node] = 0;
  58187. + s->opt_len--; if (stree) s->static_len -= stree[node].Len;
  58188. + /* node is 0 or 1 so it does not have extra bits */
  58189. + }
  58190. + desc->max_code = max_code;
  58191. +
  58192. + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  58193. + * establish sub-heaps of increasing lengths:
  58194. + */
  58195. + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
  58196. +
  58197. + /* Construct the Huffman tree by repeatedly combining the least two
  58198. + * frequent nodes.
  58199. + */
  58200. + node = elems; /* next internal node of the tree */
  58201. + do {
  58202. + pqremove(s, tree, n); /* n = node of least frequency */
  58203. + m = s->heap[SMALLEST]; /* m = node of next least frequency */
  58204. +
  58205. + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
  58206. + s->heap[--(s->heap_max)] = m;
  58207. +
  58208. + /* Create a new node father of n and m */
  58209. + tree[node].Freq = tree[n].Freq + tree[m].Freq;
  58210. + s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
  58211. + tree[n].Dad = tree[m].Dad = (ush)node;
  58212. +#ifdef DUMP_BL_TREE
  58213. + if (tree == s->bl_tree) {
  58214. + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
  58215. + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
  58216. + }
  58217. +#endif
  58218. + /* and insert the new node in the heap */
  58219. + s->heap[SMALLEST] = node++;
  58220. + pqdownheap(s, tree, SMALLEST);
  58221. +
  58222. + } while (s->heap_len >= 2);
  58223. +
  58224. + s->heap[--(s->heap_max)] = s->heap[SMALLEST];
  58225. +
  58226. + /* At this point, the fields freq and dad are set. We can now
  58227. + * generate the bit lengths.
  58228. + */
  58229. + gen_bitlen(s, (tree_desc *)desc);
  58230. +
  58231. + /* The field len is now set, we can generate the bit codes */
  58232. + gen_codes ((ct_data *)tree, max_code, s->bl_count);
  58233. +}
  58234. +
  58235. +/* ===========================================================================
  58236. + * Scan a literal or distance tree to determine the frequencies of the codes
  58237. + * in the bit length tree.
  58238. + */
  58239. +local void scan_tree (s, tree, max_code)
  58240. + deflate_state *s;
  58241. + ct_data *tree; /* the tree to be scanned */
  58242. + int max_code; /* and its largest code of non zero frequency */
  58243. +{
  58244. + int n; /* iterates over all tree elements */
  58245. + int prevlen = -1; /* last emitted length */
  58246. + int curlen; /* length of current code */
  58247. + int nextlen = tree[0].Len; /* length of next code */
  58248. + int count = 0; /* repeat count of the current code */
  58249. + int max_count = 7; /* max repeat count */
  58250. + int min_count = 4; /* min repeat count */
  58251. +
  58252. + if (nextlen == 0) max_count = 138, min_count = 3;
  58253. + tree[max_code+1].Len = (ush)0xffff; /* guard */
  58254. +
  58255. + for (n = 0; n <= max_code; n++) {
  58256. + curlen = nextlen; nextlen = tree[n+1].Len;
  58257. + if (++count < max_count && curlen == nextlen) {
  58258. + continue;
  58259. + } else if (count < min_count) {
  58260. + s->bl_tree[curlen].Freq += count;
  58261. + } else if (curlen != 0) {
  58262. + if (curlen != prevlen) s->bl_tree[curlen].Freq++;
  58263. + s->bl_tree[REP_3_6].Freq++;
  58264. + } else if (count <= 10) {
  58265. + s->bl_tree[REPZ_3_10].Freq++;
  58266. + } else {
  58267. + s->bl_tree[REPZ_11_138].Freq++;
  58268. + }
  58269. + count = 0; prevlen = curlen;
  58270. + if (nextlen == 0) {
  58271. + max_count = 138, min_count = 3;
  58272. + } else if (curlen == nextlen) {
  58273. + max_count = 6, min_count = 3;
  58274. + } else {
  58275. + max_count = 7, min_count = 4;
  58276. + }
  58277. + }
  58278. +}
  58279. +
  58280. +/* ===========================================================================
  58281. + * Send a literal or distance tree in compressed form, using the codes in
  58282. + * bl_tree.
  58283. + */
  58284. +local void send_tree (s, tree, max_code)
  58285. + deflate_state *s;
  58286. + ct_data *tree; /* the tree to be scanned */
  58287. + int max_code; /* and its largest code of non zero frequency */
  58288. +{
  58289. + int n; /* iterates over all tree elements */
  58290. + int prevlen = -1; /* last emitted length */
  58291. + int curlen; /* length of current code */
  58292. + int nextlen = tree[0].Len; /* length of next code */
  58293. + int count = 0; /* repeat count of the current code */
  58294. + int max_count = 7; /* max repeat count */
  58295. + int min_count = 4; /* min repeat count */
  58296. +
  58297. + /* tree[max_code+1].Len = -1; */ /* guard already set */
  58298. + if (nextlen == 0) max_count = 138, min_count = 3;
  58299. +
  58300. + for (n = 0; n <= max_code; n++) {
  58301. + curlen = nextlen; nextlen = tree[n+1].Len;
  58302. + if (++count < max_count && curlen == nextlen) {
  58303. + continue;
  58304. + } else if (count < min_count) {
  58305. + do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
  58306. +
  58307. + } else if (curlen != 0) {
  58308. + if (curlen != prevlen) {
  58309. + send_code(s, curlen, s->bl_tree); count--;
  58310. + }
  58311. + Assert(count >= 3 && count <= 6, " 3_6?");
  58312. + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
  58313. +
  58314. + } else if (count <= 10) {
  58315. + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
  58316. +
  58317. + } else {
  58318. + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
  58319. + }
  58320. + count = 0; prevlen = curlen;
  58321. + if (nextlen == 0) {
  58322. + max_count = 138, min_count = 3;
  58323. + } else if (curlen == nextlen) {
  58324. + max_count = 6, min_count = 3;
  58325. + } else {
  58326. + max_count = 7, min_count = 4;
  58327. + }
  58328. + }
  58329. +}
  58330. +
  58331. +/* ===========================================================================
  58332. + * Construct the Huffman tree for the bit lengths and return the index in
  58333. + * bl_order of the last bit length code to send.
  58334. + */
  58335. +local int build_bl_tree(s)
  58336. + deflate_state *s;
  58337. +{
  58338. + int max_blindex; /* index of last bit length code of non zero freq */
  58339. +
  58340. + /* Determine the bit length frequencies for literal and distance trees */
  58341. + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
  58342. + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
  58343. +
  58344. + /* Build the bit length tree: */
  58345. + build_tree(s, (tree_desc *)(&(s->bl_desc)));
  58346. + /* opt_len now includes the length of the tree representations, except
  58347. + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  58348. + */
  58349. +
  58350. + /* Determine the number of bit length codes to send. The pkzip format
  58351. + * requires that at least 4 bit length codes be sent. (appnote.txt says
  58352. + * 3 but the actual value used is 4.)
  58353. + */
  58354. + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  58355. + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
  58356. + }
  58357. + /* Update opt_len to include the bit length tree and counts */
  58358. + s->opt_len += 3*(max_blindex+1) + 5+5+4;
  58359. + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  58360. + s->opt_len, s->static_len));
  58361. +
  58362. + return max_blindex;
  58363. +}
  58364. +
  58365. +/* ===========================================================================
  58366. + * Send the header for a block using dynamic Huffman trees: the counts, the
  58367. + * lengths of the bit length codes, the literal tree and the distance tree.
  58368. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  58369. + */
  58370. +local void send_all_trees(s, lcodes, dcodes, blcodes)
  58371. + deflate_state *s;
  58372. + int lcodes, dcodes, blcodes; /* number of codes for each tree */
  58373. +{
  58374. + int rank; /* index in bl_order */
  58375. +
  58376. + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  58377. + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  58378. + "too many codes");
  58379. + Tracev((stderr, "\nbl counts: "));
  58380. + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
  58381. + send_bits(s, dcodes-1, 5);
  58382. + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
  58383. + for (rank = 0; rank < blcodes; rank++) {
  58384. + Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  58385. + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
  58386. + }
  58387. + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  58388. +
  58389. + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
  58390. + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  58391. +
  58392. + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
  58393. + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  58394. +}
  58395. +
  58396. +/* ===========================================================================
  58397. + * Send a stored block
  58398. + */
  58399. +void _tr_stored_block(s, buf, stored_len, eof)
  58400. + deflate_state *s;
  58401. + charf *buf; /* input block */
  58402. + ulg stored_len; /* length of input block */
  58403. + int eof; /* true if this is the last block for a file */
  58404. +{
  58405. + send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
  58406. +#ifdef DEBUG
  58407. + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
  58408. + s->compressed_len += (stored_len + 4) << 3;
  58409. +#endif
  58410. + copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
  58411. +}
  58412. +
  58413. +/* ===========================================================================
  58414. + * Send one empty static block to give enough lookahead for inflate.
  58415. + * This takes 10 bits, of which 7 may remain in the bit buffer.
  58416. + * The current inflate code requires 9 bits of lookahead. If the
  58417. + * last two codes for the previous block (real code plus EOB) were coded
  58418. + * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  58419. + * the last real code. In this case we send two empty static blocks instead
  58420. + * of one. (There are no problems if the previous block is stored or fixed.)
  58421. + * To simplify the code, we assume the worst case of last real code encoded
  58422. + * on one bit only.
  58423. + */
  58424. +void _tr_align(s)
  58425. + deflate_state *s;
  58426. +{
  58427. + send_bits(s, STATIC_TREES<<1, 3);
  58428. + send_code(s, END_BLOCK, static_ltree);
  58429. +#ifdef DEBUG
  58430. + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
  58431. +#endif
  58432. + bi_flush(s);
  58433. + /* Of the 10 bits for the empty block, we have already sent
  58434. + * (10 - bi_valid) bits. The lookahead for the last real code (before
  58435. + * the EOB of the previous block) was thus at least one plus the length
  58436. + * of the EOB plus what we have just sent of the empty static block.
  58437. + */
  58438. + if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
  58439. + send_bits(s, STATIC_TREES<<1, 3);
  58440. + send_code(s, END_BLOCK, static_ltree);
  58441. +#ifdef DEBUG
  58442. + s->compressed_len += 10L;
  58443. +#endif
  58444. + bi_flush(s);
  58445. + }
  58446. + s->last_eob_len = 7;
  58447. +}
  58448. +
  58449. +/* ===========================================================================
  58450. + * Determine the best encoding for the current block: dynamic trees, static
  58451. + * trees or store, and output the encoded block to the zip file.
  58452. + */
  58453. +void _tr_flush_block(s, buf, stored_len, eof)
  58454. + deflate_state *s;
  58455. + charf *buf; /* input block, or NULL if too old */
  58456. + ulg stored_len; /* length of input block */
  58457. + int eof; /* true if this is the last block for a file */
  58458. +{
  58459. + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  58460. + int max_blindex = 0; /* index of last bit length code of non zero freq */
  58461. +
  58462. + /* Build the Huffman trees unless a stored block is forced */
  58463. + if (s->level > 0) {
  58464. +
  58465. + /* Check if the file is ascii or binary */
  58466. + if (s->data_type == Z_UNKNOWN) set_data_type(s);
  58467. +
  58468. + /* Construct the literal and distance trees */
  58469. + build_tree(s, (tree_desc *)(&(s->l_desc)));
  58470. + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  58471. + s->static_len));
  58472. +
  58473. + build_tree(s, (tree_desc *)(&(s->d_desc)));
  58474. + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  58475. + s->static_len));
  58476. + /* At this point, opt_len and static_len are the total bit lengths of
  58477. + * the compressed block data, excluding the tree representations.
  58478. + */
  58479. +
  58480. + /* Build the bit length tree for the above two trees, and get the index
  58481. + * in bl_order of the last bit length code to send.
  58482. + */
  58483. + max_blindex = build_bl_tree(s);
  58484. +
  58485. + /* Determine the best encoding. Compute first the block length in bytes*/
  58486. + opt_lenb = (s->opt_len+3+7)>>3;
  58487. + static_lenb = (s->static_len+3+7)>>3;
  58488. +
  58489. + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  58490. + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  58491. + s->last_lit));
  58492. +
  58493. + if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
  58494. +
  58495. + } else {
  58496. + Assert(buf != (char*)0, "lost buf");
  58497. + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  58498. + }
  58499. +
  58500. +#ifdef FORCE_STORED
  58501. + if (buf != (char*)0) { /* force stored block */
  58502. +#else
  58503. + if (stored_len+4 <= opt_lenb && buf != (char*)0) {
  58504. + /* 4: two words for the lengths */
  58505. +#endif
  58506. + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  58507. + * Otherwise we can't have processed more than WSIZE input bytes since
  58508. + * the last block flush, because compression would have been
  58509. + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  58510. + * transform a block into a stored block.
  58511. + */
  58512. + _tr_stored_block(s, buf, stored_len, eof);
  58513. +
  58514. +#ifdef FORCE_STATIC
  58515. + } else if (static_lenb >= 0) { /* force static trees */
  58516. +#else
  58517. + } else if (static_lenb == opt_lenb) {
  58518. +#endif
  58519. + send_bits(s, (STATIC_TREES<<1)+eof, 3);
  58520. + compress_block(s, static_ltree, static_dtree);
  58521. +#ifdef DEBUG
  58522. + s->compressed_len += 3 + s->static_len;
  58523. +#endif
  58524. + } else {
  58525. + send_bits(s, (DYN_TREES<<1)+eof, 3);
  58526. + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
  58527. + max_blindex+1);
  58528. + compress_block(s, s->dyn_ltree, s->dyn_dtree);
  58529. +#ifdef DEBUG
  58530. + s->compressed_len += 3 + s->opt_len;
  58531. +#endif
  58532. + }
  58533. + Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  58534. + /* The above check is made mod 2^32, for files larger than 512 MB
  58535. + * and uLong implemented on 32 bits.
  58536. + */
  58537. + init_block(s);
  58538. +
  58539. + if (eof) {
  58540. + bi_windup(s);
  58541. +#ifdef DEBUG
  58542. + s->compressed_len += 7; /* align on byte boundary */
  58543. +#endif
  58544. + }
  58545. + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  58546. + s->compressed_len-7*eof));
  58547. +}
  58548. +
  58549. +/* ===========================================================================
  58550. + * Save the match info and tally the frequency counts. Return true if
  58551. + * the current block must be flushed.
  58552. + */
  58553. +int _tr_tally (s, dist, lc)
  58554. + deflate_state *s;
  58555. + unsigned dist; /* distance of matched string */
  58556. + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  58557. +{
  58558. + s->d_buf[s->last_lit] = (ush)dist;
  58559. + s->l_buf[s->last_lit++] = (uch)lc;
  58560. + if (dist == 0) {
  58561. + /* lc is the unmatched char */
  58562. + s->dyn_ltree[lc].Freq++;
  58563. + } else {
  58564. + s->matches++;
  58565. + /* Here, lc is the match length - MIN_MATCH */
  58566. + dist--; /* dist = match distance - 1 */
  58567. + Assert((ush)dist < (ush)MAX_DIST(s) &&
  58568. + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  58569. + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  58570. +
  58571. + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
  58572. + s->dyn_dtree[d_code(dist)].Freq++;
  58573. + }
  58574. +
  58575. +#ifdef TRUNCATE_BLOCK
  58576. + /* Try to guess if it is profitable to stop the current block here */
  58577. + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
  58578. + /* Compute an upper bound for the compressed length */
  58579. + ulg out_length = (ulg)s->last_lit*8L;
  58580. + ulg in_length = (ulg)((long)s->strstart - s->block_start);
  58581. + int dcode;
  58582. + for (dcode = 0; dcode < D_CODES; dcode++) {
  58583. + out_length += (ulg)s->dyn_dtree[dcode].Freq *
  58584. + (5L+extra_dbits[dcode]);
  58585. + }
  58586. + out_length >>= 3;
  58587. + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  58588. + s->last_lit, in_length, out_length,
  58589. + 100L - out_length*100L/in_length));
  58590. + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
  58591. + }
  58592. +#endif
  58593. + return (s->last_lit == s->lit_bufsize-1);
  58594. + /* We avoid equality with lit_bufsize because of wraparound at 64K
  58595. + * on 16 bit machines and because stored blocks are restricted to
  58596. + * 64K-1 bytes.
  58597. + */
  58598. +}
  58599. +
  58600. +/* ===========================================================================
  58601. + * Send the block data compressed using the given Huffman trees
  58602. + */
  58603. +local void compress_block(s, ltree, dtree)
  58604. + deflate_state *s;
  58605. + const ct_data *ltree; /* literal tree */
  58606. + const ct_data *dtree; /* distance tree */
  58607. +{
  58608. + unsigned dist; /* distance of matched string */
  58609. + int lc; /* match length or unmatched char (if dist == 0) */
  58610. + unsigned lx = 0; /* running index in l_buf */
  58611. + unsigned code; /* the code to send */
  58612. + int extra; /* number of extra bits to send */
  58613. +
  58614. + if (s->last_lit != 0) do {
  58615. + dist = s->d_buf[lx];
  58616. + lc = s->l_buf[lx++];
  58617. + if (dist == 0) {
  58618. + send_code(s, lc, ltree); /* send a literal byte */
  58619. + Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  58620. + } else {
  58621. + /* Here, lc is the match length - MIN_MATCH */
  58622. + code = _length_code[lc];
  58623. + send_code(s, code+LITERALS+1, ltree); /* send the length code */
  58624. + extra = extra_lbits[code];
  58625. + if (extra != 0) {
  58626. + lc -= base_length[code];
  58627. + send_bits(s, lc, extra); /* send the extra length bits */
  58628. + }
  58629. + dist--; /* dist is now the match distance - 1 */
  58630. + code = d_code(dist);
  58631. + Assert (code < D_CODES, "bad d_code");
  58632. +
  58633. + send_code(s, code, dtree); /* send the distance code */
  58634. + extra = extra_dbits[code];
  58635. + if (extra != 0) {
  58636. + dist -= base_dist[code];
  58637. + send_bits(s, dist, extra); /* send the extra distance bits */
  58638. + }
  58639. + } /* literal or match pair ? */
  58640. +
  58641. + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  58642. + Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
  58643. +
  58644. + } while (lx < s->last_lit);
  58645. +
  58646. + send_code(s, END_BLOCK, ltree);
  58647. + s->last_eob_len = ltree[END_BLOCK].Len;
  58648. +}
  58649. +
  58650. +/* ===========================================================================
  58651. + * Set the data type to ASCII or BINARY, using a crude approximation:
  58652. + * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  58653. + * IN assertion: the fields freq of dyn_ltree are set and the total of all
  58654. + * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  58655. + */
  58656. +local void set_data_type(s)
  58657. + deflate_state *s;
  58658. +{
  58659. + int n = 0;
  58660. + unsigned ascii_freq = 0;
  58661. + unsigned bin_freq = 0;
  58662. + while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
  58663. + while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
  58664. + while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
  58665. + s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
  58666. +}
  58667. +
  58668. +/* ===========================================================================
  58669. + * Reverse the first len bits of a code, using straightforward code (a faster
  58670. + * method would use a table)
  58671. + * IN assertion: 1 <= len <= 15
  58672. + */
  58673. +local unsigned bi_reverse(code, len)
  58674. + unsigned code; /* the value to invert */
  58675. + int len; /* its bit length */
  58676. +{
  58677. + register unsigned res = 0;
  58678. + do {
  58679. + res |= code & 1;
  58680. + code >>= 1, res <<= 1;
  58681. + } while (--len > 0);
  58682. + return res >> 1;
  58683. +}
  58684. +
  58685. +/* ===========================================================================
  58686. + * Flush the bit buffer, keeping at most 7 bits in it.
  58687. + */
  58688. +local void bi_flush(s)
  58689. + deflate_state *s;
  58690. +{
  58691. + if (s->bi_valid == 16) {
  58692. + put_short(s, s->bi_buf);
  58693. + s->bi_buf = 0;
  58694. + s->bi_valid = 0;
  58695. + } else if (s->bi_valid >= 8) {
  58696. + put_byte(s, (Byte)s->bi_buf);
  58697. + s->bi_buf >>= 8;
  58698. + s->bi_valid -= 8;
  58699. + }
  58700. +}
  58701. +
  58702. +/* ===========================================================================
  58703. + * Flush the bit buffer and align the output on a byte boundary
  58704. + */
  58705. +local void bi_windup(s)
  58706. + deflate_state *s;
  58707. +{
  58708. + if (s->bi_valid > 8) {
  58709. + put_short(s, s->bi_buf);
  58710. + } else if (s->bi_valid > 0) {
  58711. + put_byte(s, (Byte)s->bi_buf);
  58712. + }
  58713. + s->bi_buf = 0;
  58714. + s->bi_valid = 0;
  58715. +#ifdef DEBUG
  58716. + s->bits_sent = (s->bits_sent+7) & ~7;
  58717. +#endif
  58718. +}
  58719. +
  58720. +/* ===========================================================================
  58721. + * Copy a stored block, storing first the length and its
  58722. + * one's complement if requested.
  58723. + */
  58724. +local void copy_block(s, buf, len, header)
  58725. + deflate_state *s;
  58726. + charf *buf; /* the input data */
  58727. + unsigned len; /* its length */
  58728. + int header; /* true if block header must be written */
  58729. +{
  58730. + bi_windup(s); /* align on byte boundary */
  58731. + s->last_eob_len = 8; /* enough lookahead for inflate */
  58732. +
  58733. + if (header) {
  58734. + put_short(s, (ush)len);
  58735. + put_short(s, (ush)~len);
  58736. +#ifdef DEBUG
  58737. + s->bits_sent += 2*16;
  58738. +#endif
  58739. + }
  58740. +#ifdef DEBUG
  58741. + s->bits_sent += (ulg)len<<3;
  58742. +#endif
  58743. + while (len--) {
  58744. + put_byte(s, *buf++);
  58745. + }
  58746. +}
  58747. --- /dev/null Tue Mar 11 13:02:56 2003
  58748. +++ linux/net/ipsec/trees.h Mon Feb 9 13:51:03 2004
  58749. @@ -0,0 +1,128 @@
  58750. +/* header created automatically with -DGEN_TREES_H */
  58751. +
  58752. +local const ct_data static_ltree[L_CODES+2] = {
  58753. +{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
  58754. +{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
  58755. +{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
  58756. +{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
  58757. +{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
  58758. +{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
  58759. +{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
  58760. +{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
  58761. +{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
  58762. +{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
  58763. +{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
  58764. +{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
  58765. +{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
  58766. +{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
  58767. +{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
  58768. +{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
  58769. +{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
  58770. +{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
  58771. +{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
  58772. +{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
  58773. +{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
  58774. +{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
  58775. +{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
  58776. +{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
  58777. +{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
  58778. +{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
  58779. +{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
  58780. +{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
  58781. +{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
  58782. +{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
  58783. +{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
  58784. +{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
  58785. +{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
  58786. +{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
  58787. +{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
  58788. +{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
  58789. +{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
  58790. +{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
  58791. +{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
  58792. +{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
  58793. +{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
  58794. +{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
  58795. +{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
  58796. +{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
  58797. +{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
  58798. +{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
  58799. +{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
  58800. +{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
  58801. +{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
  58802. +{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
  58803. +{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
  58804. +{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
  58805. +{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
  58806. +{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
  58807. +{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
  58808. +{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
  58809. +{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
  58810. +{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
  58811. +};
  58812. +
  58813. +local const ct_data static_dtree[D_CODES] = {
  58814. +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
  58815. +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
  58816. +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
  58817. +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
  58818. +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
  58819. +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
  58820. +};
  58821. +
  58822. +const uch _dist_code[DIST_CODE_LEN] = {
  58823. + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
  58824. + 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
  58825. +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  58826. +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  58827. +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
  58828. +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  58829. +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58830. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58831. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58832. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
  58833. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58834. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58835. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
  58836. +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
  58837. +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58838. +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  58839. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  58840. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
  58841. +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  58842. +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58843. +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58844. +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58845. +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58846. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58847. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58848. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
  58849. +};
  58850. +
  58851. +const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
  58852. + 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  58853. +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  58854. +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  58855. +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  58856. +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  58857. +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  58858. +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58859. +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58860. +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  58861. +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  58862. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  58863. +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  58864. +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
  58865. +};
  58866. +
  58867. +local const int base_length[LENGTH_CODES] = {
  58868. +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
  58869. +64, 80, 96, 112, 128, 160, 192, 224, 0
  58870. +};
  58871. +
  58872. +local const int base_dist[D_CODES] = {
  58873. + 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
  58874. + 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
  58875. + 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
  58876. +};
  58877. +
  58878. --- /dev/null Tue Mar 11 13:02:56 2003
  58879. +++ linux/net/ipsec/ultoa.c Mon Feb 9 13:51:03 2004
  58880. @@ -0,0 +1,66 @@
  58881. +/*
  58882. + * convert unsigned long to ASCII
  58883. + * Copyright (C) 1998, 1999 Henry Spencer.
  58884. + *
  58885. + * This library is free software; you can redistribute it and/or modify it
  58886. + * under the terms of the GNU Library General Public License as published by
  58887. + * the Free Software Foundation; either version 2 of the License, or (at your
  58888. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  58889. + *
  58890. + * This library is distributed in the hope that it will be useful, but
  58891. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  58892. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  58893. + * License for more details.
  58894. + *
  58895. + * RCSID $Id: ultoa.c,v 1.10 2004/07/10 07:48:37 mcr Exp $
  58896. + */
  58897. +#include "openswan.h"
  58898. +
  58899. +/*
  58900. + - ultoa - convert unsigned long to decimal ASCII
  58901. + */
  58902. +size_t /* length required for full conversion */
  58903. +ultoa(n, base, dst, dstlen)
  58904. +unsigned long n;
  58905. +int base;
  58906. +char *dst; /* need not be valid if dstlen is 0 */
  58907. +size_t dstlen;
  58908. +{
  58909. + char buf[3*sizeof(unsigned long) + 1];
  58910. + char *bufend = buf + sizeof(buf);
  58911. + size_t len;
  58912. + char *p;
  58913. + static char hex[] = "0123456789abcdef";
  58914. +
  58915. + p = bufend;
  58916. + *--p = '\0';
  58917. + if (base == 10) {
  58918. + do {
  58919. + *--p = n%10 + '0';
  58920. + n /= 10;
  58921. + } while (n != 0);
  58922. + } else if (base == 16) {
  58923. + do {
  58924. + *--p = hex[n&0xf];
  58925. + n >>= 4;
  58926. + } while (n != 0);
  58927. + *--p = 'x';
  58928. + *--p = '0';
  58929. + } else if (base == 8) {
  58930. + do {
  58931. + *--p = (n&07) + '0';
  58932. + n >>= 3;
  58933. + } while (n != 0);
  58934. + *--p = '0';
  58935. + } else
  58936. + *--p = '?';
  58937. +
  58938. + len = bufend - p;
  58939. +
  58940. + if (dstlen > 0) {
  58941. + if (len > dstlen)
  58942. + *(p + dstlen - 1) = '\0';
  58943. + strcpy(dst, p);
  58944. + }
  58945. + return len;
  58946. +}
  58947. --- /dev/null Tue Mar 11 13:02:56 2003
  58948. +++ linux/net/ipsec/ultot.c Mon Feb 9 13:51:03 2004
  58949. @@ -0,0 +1,82 @@
  58950. +/*
  58951. + * convert unsigned long to text
  58952. + * Copyright (C) 2000 Henry Spencer.
  58953. + *
  58954. + * This library is free software; you can redistribute it and/or modify it
  58955. + * under the terms of the GNU Library General Public License as published by
  58956. + * the Free Software Foundation; either version 2 of the License, or (at your
  58957. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  58958. + *
  58959. + * This library is distributed in the hope that it will be useful, but
  58960. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  58961. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  58962. + * License for more details.
  58963. + *
  58964. + * RCSID $Id: ultot.c,v 1.5 2004/07/10 07:48:37 mcr Exp $
  58965. + */
  58966. +#include "openswan.h"
  58967. +
  58968. +/*
  58969. + - ultot - convert unsigned long to text
  58970. + */
  58971. +size_t /* length required for full conversion */
  58972. +ultot(n, base, dst, dstlen)
  58973. +unsigned long n;
  58974. +int base;
  58975. +char *dst; /* need not be valid if dstlen is 0 */
  58976. +size_t dstlen;
  58977. +{
  58978. + char buf[3*sizeof(unsigned long) + 1];
  58979. + char *bufend = buf + sizeof(buf);
  58980. + size_t len;
  58981. + char *p;
  58982. + static char hex[] = "0123456789abcdef";
  58983. +# define HEX32 (32/4)
  58984. +
  58985. + p = bufend;
  58986. + *--p = '\0';
  58987. + switch (base) {
  58988. + case 10:
  58989. + case 'd':
  58990. + do {
  58991. + *--p = n%10 + '0';
  58992. + n /= 10;
  58993. + } while (n != 0);
  58994. + break;
  58995. + case 16:
  58996. + case 17:
  58997. + case 'x':
  58998. + do {
  58999. + *--p = hex[n&0xf];
  59000. + n >>= 4;
  59001. + } while (n != 0);
  59002. + if (base == 17)
  59003. + while (bufend - p < HEX32 + 1)
  59004. + *--p = '0';
  59005. + if (base == 'x') {
  59006. + *--p = 'x';
  59007. + *--p = '0';
  59008. + }
  59009. + break;
  59010. + case 8:
  59011. + case 'o':
  59012. + do {
  59013. + *--p = (n&07) + '0';
  59014. + n >>= 3;
  59015. + } while (n != 0);
  59016. + if (base == 'o')
  59017. + *--p = '0';
  59018. + break;
  59019. + default:
  59020. + return 0;
  59021. + break;
  59022. + }
  59023. +
  59024. + len = bufend - p;
  59025. + if (dstlen > 0) {
  59026. + if (len > dstlen)
  59027. + *(p + dstlen - 1) = '\0';
  59028. + strcpy(dst, p);
  59029. + }
  59030. + return len;
  59031. +}
  59032. --- /dev/null Tue Mar 11 13:02:56 2003
  59033. +++ linux/net/ipsec/version.c Mon Feb 9 13:51:03 2004
  59034. @@ -0,0 +1,44 @@
  59035. +/*
  59036. + * return IPsec version information
  59037. + * Copyright (C) 2001 Henry Spencer.
  59038. + *
  59039. + * This library is free software; you can redistribute it and/or modify it
  59040. + * under the terms of the GNU Library General Public License as published by
  59041. + * the Free Software Foundation; either version 2 of the License, or (at your
  59042. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  59043. + *
  59044. + * This library is distributed in the hope that it will be useful, but
  59045. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  59046. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  59047. + * License for more details.
  59048. + *
  59049. + * RCSID $Id: version.in.c,v 1.2 2004/04/14 05:09:46 ken Exp $
  59050. + */
  59051. +
  59052. +#ifdef __KERNEL__
  59053. +#include <linux/netdevice.h>
  59054. +#endif
  59055. +
  59056. +#include "openswan.h"
  59057. +
  59058. +#define V "2.4.7" /* substituted in by Makefile */
  59059. +static const char openswan_number[] = V;
  59060. +static const char openswan_string[] = "Openswan " V;
  59061. +
  59062. +/*
  59063. + - ipsec_version_code - return IPsec version number/code, as string
  59064. + */
  59065. +const char *
  59066. +ipsec_version_code()
  59067. +{
  59068. + return openswan_number;
  59069. +}
  59070. +
  59071. +/*
  59072. + - ipsec_version_string - return full version string
  59073. + */
  59074. +const char *
  59075. +ipsec_version_string()
  59076. +{
  59077. + return openswan_string;
  59078. +}
  59079. --- /dev/null Tue Mar 11 13:02:56 2003
  59080. +++ linux/net/ipsec/zutil.c Mon Feb 9 13:51:03 2004
  59081. @@ -0,0 +1,227 @@
  59082. +/* zutil.c -- target dependent utility functions for the compression library
  59083. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  59084. + * For conditions of distribution and use, see copyright notice in zlib.h
  59085. + */
  59086. +
  59087. +/* @(#) $Id: zutil.c,v 1.5 2004/07/10 07:48:40 mcr Exp $ */
  59088. +
  59089. +#include <zlib/zutil.h>
  59090. +
  59091. +#define MY_ZCALLOC
  59092. +
  59093. +struct internal_state {int dummy;}; /* for buggy compilers */
  59094. +
  59095. +#ifndef STDC
  59096. +extern void exit OF((int));
  59097. +#endif
  59098. +
  59099. +const char *z_errmsg[10] = {
  59100. +"need dictionary", /* Z_NEED_DICT 2 */
  59101. +"stream end", /* Z_STREAM_END 1 */
  59102. +"", /* Z_OK 0 */
  59103. +"file error", /* Z_ERRNO (-1) */
  59104. +"stream error", /* Z_STREAM_ERROR (-2) */
  59105. +"data error", /* Z_DATA_ERROR (-3) */
  59106. +"insufficient memory", /* Z_MEM_ERROR (-4) */
  59107. +"buffer error", /* Z_BUF_ERROR (-5) */
  59108. +"incompatible version",/* Z_VERSION_ERROR (-6) */
  59109. +""};
  59110. +
  59111. +
  59112. +const char * ZEXPORT zlibVersion()
  59113. +{
  59114. + return ZLIB_VERSION;
  59115. +}
  59116. +
  59117. +#ifdef DEBUG
  59118. +
  59119. +# ifndef verbose
  59120. +# define verbose 0
  59121. +# endif
  59122. +int z_verbose = verbose;
  59123. +
  59124. +void z_error (m)
  59125. + char *m;
  59126. +{
  59127. + fprintf(stderr, "%s\n", m);
  59128. + exit(1);
  59129. +}
  59130. +#endif
  59131. +
  59132. +/* exported to allow conversion of error code to string for compress() and
  59133. + * uncompress()
  59134. + */
  59135. +const char * ZEXPORT zError(err)
  59136. + int err;
  59137. +{
  59138. + return ERR_MSG(err);
  59139. +}
  59140. +
  59141. +
  59142. +#ifndef HAVE_MEMCPY
  59143. +
  59144. +void zmemcpy(dest, source, len)
  59145. + Bytef* dest;
  59146. + const Bytef* source;
  59147. + uInt len;
  59148. +{
  59149. + if (len == 0) return;
  59150. + do {
  59151. + *dest++ = *source++; /* ??? to be unrolled */
  59152. + } while (--len != 0);
  59153. +}
  59154. +
  59155. +int zmemcmp(s1, s2, len)
  59156. + const Bytef* s1;
  59157. + const Bytef* s2;
  59158. + uInt len;
  59159. +{
  59160. + uInt j;
  59161. +
  59162. + for (j = 0; j < len; j++) {
  59163. + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  59164. + }
  59165. + return 0;
  59166. +}
  59167. +
  59168. +void zmemzero(dest, len)
  59169. + Bytef* dest;
  59170. + uInt len;
  59171. +{
  59172. + if (len == 0) return;
  59173. + do {
  59174. + *dest++ = 0; /* ??? to be unrolled */
  59175. + } while (--len != 0);
  59176. +}
  59177. +#endif
  59178. +
  59179. +#ifdef __TURBOC__
  59180. +#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
  59181. +/* Small and medium model in Turbo C are for now limited to near allocation
  59182. + * with reduced MAX_WBITS and MAX_MEM_LEVEL
  59183. + */
  59184. +# define MY_ZCALLOC
  59185. +
  59186. +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  59187. + * and farmalloc(64K) returns a pointer with an offset of 8, so we
  59188. + * must fix the pointer. Warning: the pointer must be put back to its
  59189. + * original form in order to free it, use zcfree().
  59190. + */
  59191. +
  59192. +#define MAX_PTR 10
  59193. +/* 10*64K = 640K */
  59194. +
  59195. +local int next_ptr = 0;
  59196. +
  59197. +typedef struct ptr_table_s {
  59198. + voidpf org_ptr;
  59199. + voidpf new_ptr;
  59200. +} ptr_table;
  59201. +
  59202. +local ptr_table table[MAX_PTR];
  59203. +/* This table is used to remember the original form of pointers
  59204. + * to large buffers (64K). Such pointers are normalized with a zero offset.
  59205. + * Since MSDOS is not a preemptive multitasking OS, this table is not
  59206. + * protected from concurrent access. This hack doesn't work anyway on
  59207. + * a protected system like OS/2. Use Microsoft C instead.
  59208. + */
  59209. +
  59210. +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  59211. +{
  59212. + voidpf buf = opaque; /* just to make some compilers happy */
  59213. + ulg bsize = (ulg)items*size;
  59214. +
  59215. + /* If we allocate less than 65520 bytes, we assume that farmalloc
  59216. + * will return a usable pointer which doesn't have to be normalized.
  59217. + */
  59218. + if (bsize < 65520L) {
  59219. + buf = farmalloc(bsize);
  59220. + if (*(ush*)&buf != 0) return buf;
  59221. + } else {
  59222. + buf = farmalloc(bsize + 16L);
  59223. + }
  59224. + if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  59225. + table[next_ptr].org_ptr = buf;
  59226. +
  59227. + /* Normalize the pointer to seg:0 */
  59228. + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  59229. + *(ush*)&buf = 0;
  59230. + table[next_ptr++].new_ptr = buf;
  59231. + return buf;
  59232. +}
  59233. +
  59234. +void zcfree (voidpf opaque, voidpf ptr)
  59235. +{
  59236. + int n;
  59237. + if (*(ush*)&ptr != 0) { /* object < 64K */
  59238. + farfree(ptr);
  59239. + return;
  59240. + }
  59241. + /* Find the original pointer */
  59242. + for (n = 0; n < next_ptr; n++) {
  59243. + if (ptr != table[n].new_ptr) continue;
  59244. +
  59245. + farfree(table[n].org_ptr);
  59246. + while (++n < next_ptr) {
  59247. + table[n-1] = table[n];
  59248. + }
  59249. + next_ptr--;
  59250. + return;
  59251. + }
  59252. + ptr = opaque; /* just to make some compilers happy */
  59253. + Assert(0, "zcfree: ptr not found");
  59254. +}
  59255. +#endif
  59256. +#endif /* __TURBOC__ */
  59257. +
  59258. +
  59259. +#if defined(M_I86) && !defined(__32BIT__)
  59260. +/* Microsoft C in 16-bit mode */
  59261. +
  59262. +# define MY_ZCALLOC
  59263. +
  59264. +#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  59265. +# define _halloc halloc
  59266. +# define _hfree hfree
  59267. +#endif
  59268. +
  59269. +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  59270. +{
  59271. + if (opaque) opaque = 0; /* to make compiler happy */
  59272. + return _halloc((long)items, size);
  59273. +}
  59274. +
  59275. +void zcfree (voidpf opaque, voidpf ptr)
  59276. +{
  59277. + if (opaque) opaque = 0; /* to make compiler happy */
  59278. + _hfree(ptr);
  59279. +}
  59280. +
  59281. +#endif /* MSC */
  59282. +
  59283. +
  59284. +#ifndef MY_ZCALLOC /* Any system without a special alloc function */
  59285. +
  59286. +#ifndef STDC
  59287. +extern voidp calloc OF((uInt items, uInt size));
  59288. +extern void free OF((voidpf ptr));
  59289. +#endif
  59290. +
  59291. +voidpf zcalloc (opaque, items, size)
  59292. + voidpf opaque;
  59293. + unsigned items;
  59294. + unsigned size;
  59295. +{
  59296. + if (opaque) items += size - size; /* make compiler happy */
  59297. + return (voidpf)calloc(items, size);
  59298. +}
  59299. +
  59300. +void zcfree (opaque, ptr)
  59301. + voidpf opaque;
  59302. + voidpf ptr;
  59303. +{
  59304. + free(ptr);
  59305. + if (opaque) return; /* make compiler happy */
  59306. +}
  59307. +
  59308. +#endif /* MY_ZCALLOC */
  59309. --- swan26/net/ipv4/af_inet.c.orig Wed Jun 16 01:18:58 2004
  59310. +++ swan26/net/ipv4/af_inet.c Fri Aug 13 23:09:27 2004
  59311. @@ -1169,6 +1169,18 @@
  59312. #if defined(CONFIG_IP_MROUTE)
  59313. ip_mr_init();
  59314. #endif
  59315. +
  59316. +#if defined(CONFIG_KLIPS)
  59317. + {
  59318. + extern int ipsec_klips_init(void);
  59319. + /*
  59320. + * Initialise AF_INET ESP and AH protocol support including
  59321. + * e-routing and SA tables
  59322. + */
  59323. + ipsec_klips_init();
  59324. + }
  59325. +#endif /* CONFIG_IPSEC */
  59326. +
  59327. /*
  59328. * Initialise per-cpu ipv4 mibs
  59329. */
  59330. --- /dev/null Fri May 10 13:59:54 2002
  59331. +++ linux/net/ipsec/Makefile.ver Sun Jul 28 22:10:40 2002
  59332. @@ -0,0 +1 @@
  59333. +IPSECVERSION=2.4.7