2
1

linux-2.6.21.5-openswan-2.4.8.kernel-2.6-klips.patch 1.8 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382
  1. packaging/utils/kernelpatch 2.6
  2. --- /dev/null Tue Mar 11 13:02:56 2003
  3. +++ linux/README.openswan-2 Mon Feb 9 13:51:03 2004
  4. @@ -0,0 +1,112 @@
  5. +*
  6. +* RCSID $Id: README.openswan-2,v 1.1 2003/12/10 01:07:49 mcr Exp $
  7. +*
  8. +
  9. + ****************************************
  10. + * IPSEC for Linux, Release 2.xx series *
  11. + ****************************************
  12. +
  13. +
  14. +
  15. +1. Files
  16. +
  17. +The contents of linux/net/ipsec/ (see below) join the linux kernel source tree.
  18. +as provided for higher up.
  19. +
  20. +The programs/ directory contains the user-level utilities which you need
  21. +to run IPSEC. See the top-level top/INSTALL to compile and install them.
  22. +
  23. +The testing/ directory contains test scripts.
  24. +
  25. +The doc/ directory contains -- what else -- documentation.
  26. +
  27. +1.1. Kernel files
  28. +
  29. +The following are found in net/ipsec/:
  30. +
  31. +Makefile The Makefile
  32. +Config.in The configuration script for make menuconfig
  33. +defconfig Configuration defaults for first time.
  34. +
  35. +radij.c General-purpose radix-tree operations
  36. +
  37. +ipsec_ipcomp.c IPCOMP encapsulate/decapsulate code.
  38. +ipsec_ah.c Authentication Header (AH) encapsulate/decapsulate code.
  39. +ipsec_esp.c Encapsulated Security Payload (ESP) encap/decap code.
  40. +
  41. +pfkey_v2.c PF_KEYv2 socket interface code.
  42. +pfkey_v2_parser.c PF_KEYv2 message parsing and processing code.
  43. +
  44. +ipsec_init.c Initialization code, /proc interface.
  45. +ipsec_radij.c Interface with the radix tree code.
  46. +ipsec_netlink.c Interface with the netlink code.
  47. +ipsec_xform.c Routines and structures common to transforms.
  48. +ipsec_tunnel.c The outgoing packet processing code.
  49. +ipsec_rcv.c The incoming packet processing code.
  50. +ipsec_md5c.c Somewhat modified RSADSI MD5 C code.
  51. +ipsec_sha1.c Somewhat modified Steve Reid SHA-1 C code.
  52. +
  53. +sysctl_net_ipsec.c /proc/sys/net/ipsec/* variable definitions.
  54. +
  55. +version.c symbolic link to project version.
  56. +
  57. +radij.h Headers for radij.c
  58. +
  59. +ipcomp.h Headers used by IPCOMP code.
  60. +
  61. +ipsec_radij.h Interface with the radix tree code.
  62. +ipsec_netlink.h Headers used by the netlink interface.
  63. +ipsec_encap.h Headers defining encapsulation structures.
  64. +ipsec_xform.h Transform headers.
  65. +ipsec_tunnel.h Headers used by tunneling code.
  66. +ipsec_ipe4.h Headers for the IP-in-IP code.
  67. +ipsec_ah.h Headers common to AH transforms.
  68. +ipsec_md5h.h RSADSI MD5 headers.
  69. +ipsec_sha1.h SHA-1 headers.
  70. +ipsec_esp.h Headers common to ESP transfroms.
  71. +ipsec_rcv.h Headers for incoming packet processing code.
  72. +
  73. +1.2. User-level files.
  74. +
  75. +The following are found in utils/:
  76. +
  77. +eroute.c Create an "extended route" source code
  78. +spi.c Set up Security Associations source code
  79. +spigrp.c Link SPIs together source code.
  80. +tncfg.c Configure the tunneling features of the virtual interface
  81. + source code
  82. +klipsdebug.c Set/reset klips debugging features source code.
  83. +version.c symbolic link to project version.
  84. +
  85. +eroute.8 Create an "extended route" manual page
  86. +spi.8 Set up Security Associations manual page
  87. +spigrp.8 Link SPIs together manual page
  88. +tncfg.8 Configure the tunneling features of the virtual interface
  89. + manual page
  90. +klipsdebug.8 Set/reset klips debugging features manual page
  91. +
  92. +eroute.5 /proc/net/ipsec_eroute format manual page
  93. +spi.5 /proc/net/ipsec_spi format manual page
  94. +spigrp.5 /proc/net/ipsec_spigrp format manual page
  95. +tncfg.5 /proc/net/ipsec_tncfg format manual page
  96. +klipsdebug.5 /proc/net/ipsec_klipsdebug format manual page
  97. +version.5 /proc/net/ipsec_version format manual page
  98. +pf_key.5 /proc/net/pf_key format manual page
  99. +
  100. +Makefile Utilities makefile.
  101. +
  102. +*.8 Manpages for the respective utils.
  103. +
  104. +
  105. +1.3. Test files
  106. +
  107. +The test scripts are locate in testing/ and and documentation is found
  108. +at doc/src/umltesting.html. Automated testing via "make check" is available
  109. +provided that the User-Mode-Linux patches are available.
  110. +
  111. +*
  112. +* $Log: README.openswan-2,v $
  113. +* Revision 1.1 2003/12/10 01:07:49 mcr
  114. +* documentation for additions.
  115. +*
  116. +*
  117. --- /dev/null Tue Mar 11 13:02:56 2003
  118. +++ linux/crypto/ciphers/aes/test_main.c Mon Feb 9 13:51:03 2004
  119. @@ -0,0 +1,41 @@
  120. +#include <stdio.h>
  121. +#include <string.h>
  122. +#include <sys/types.h>
  123. +#include "aes_cbc.h"
  124. +#define AES_BLOCK_SIZE 16
  125. +#define KEY_SIZE 128 /* bits */
  126. +#define KEY "1234567890123456"
  127. +#define STR "hola guaso como estaisss ... 012"
  128. +#define STRSZ (sizeof(STR)-1)
  129. +
  130. +#define EMT_AESCBC_BLKLEN AES_BLOCK_SIZE
  131. +#define AES_CONTEXT_T aes_context
  132. +#define EMT_ESPAES_KEY_SZ 16
  133. +int pretty_print(const unsigned char *buf, int count) {
  134. + int i=0;
  135. + for (;i<count;i++) {
  136. + if (i%8==0) putchar(' ');
  137. + if (i%16==0) putchar('\n');
  138. + printf ("%02hhx ", buf[i]);
  139. + }
  140. + putchar('\n');
  141. + return i;
  142. +}
  143. +//#define SIZE STRSZ/2
  144. +#define SIZE STRSZ
  145. +int main() {
  146. + int ret;
  147. + char buf0[SIZE+1], buf1[SIZE+1];
  148. + char IV[AES_BLOCK_SIZE]="\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0";
  149. + aes_context ac;
  150. + AES_set_key(&ac, KEY, KEY_SIZE);
  151. + //pretty_print((char *)&ac.aes_e_key, sizeof(ac.aes_e_key));
  152. + memset(buf0, 0, sizeof (buf0));
  153. + memset(buf1, 0, sizeof (buf1));
  154. + ret=AES_cbc_encrypt(&ac, STR, buf0, SIZE, IV, 1);
  155. + pretty_print(buf0, SIZE);
  156. + printf("size=%d ret=%d\n%s\n", SIZE, ret, buf0);
  157. + ret=AES_cbc_encrypt(&ac, buf0, buf1, SIZE, IV, 0);
  158. + printf("size=%d ret=%d\n%s\n", SIZE, ret, buf1);
  159. + return 0;
  160. +}
  161. --- /dev/null Tue Mar 11 13:02:56 2003
  162. +++ linux/crypto/ciphers/aes/test_main_mac.c Mon Feb 9 13:51:03 2004
  163. @@ -0,0 +1,30 @@
  164. +#include <stdio.h>
  165. +#include <sys/types.h>
  166. +#include <string.h>
  167. +#include "aes.h"
  168. +#include "aes_xcbc_mac.h"
  169. +#define STR "Hola guasssso c|mo estais ...012"
  170. +void print_hash(const __u8 *hash) {
  171. + printf("%08x %08x %08x %08x\n",
  172. + *(__u32*)(&hash[0]),
  173. + *(__u32*)(&hash[4]),
  174. + *(__u32*)(&hash[8]),
  175. + *(__u32*)(&hash[12]));
  176. +}
  177. +int main(int argc, char *argv[]) {
  178. + aes_block key= { 0xdeadbeef, 0xceedcaca, 0xcafebabe, 0xff010204 };
  179. + __u8 hash[16];
  180. + char *str = argv[1];
  181. + aes_context_mac ctx;
  182. + if (str==NULL) {
  183. + fprintf(stderr, "pasame el str\n");
  184. + return 255;
  185. + }
  186. + AES_xcbc_mac_set_key(&ctx, (__u8 *)&key, sizeof(key));
  187. + AES_xcbc_mac_hash(&ctx, str, strlen(str), hash);
  188. + print_hash(hash);
  189. + str[2]='x';
  190. + AES_xcbc_mac_hash(&ctx, str, strlen(str), hash);
  191. + print_hash(hash);
  192. + return 0;
  193. +}
  194. --- /dev/null Tue Mar 11 13:02:56 2003
  195. +++ linux/include/crypto/aes.h Mon Feb 9 13:51:03 2004
  196. @@ -0,0 +1,97 @@
  197. +// I retain copyright in this code but I encourage its free use provided
  198. +// that I don't carry any responsibility for the results. I am especially
  199. +// happy to see it used in free and open source software. If you do use
  200. +// it I would appreciate an acknowledgement of its origin in the code or
  201. +// the product that results and I would also appreciate knowing a little
  202. +// about the use to which it is being put. I am grateful to Frank Yellin
  203. +// for some ideas that are used in this implementation.
  204. +//
  205. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  206. +//
  207. +// This is an implementation of the AES encryption algorithm (Rijndael)
  208. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  209. +// to provide both fixed and dynamic block and key lengths and can also
  210. +// run with either big or little endian internal byte order (see aes.h).
  211. +// It inputs block and key lengths in bytes with the legal values being
  212. +// 16, 24 and 32.
  213. +
  214. +/*
  215. + * Modified by Jari Ruusu, May 1 2001
  216. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  217. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  218. + * - Major name space cleanup: Names visible to outside now begin
  219. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  220. + * - Removed C++ and DLL support as part of name space cleanup.
  221. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  222. + * - Merged precomputed constant tables to aes.c file.
  223. + * - Removed data alignment restrictions for portability reasons.
  224. + * - Made block and key lengths accept bit count (128/192/256)
  225. + * as well byte count (16/24/32).
  226. + * - Removed all error checks. This change also eliminated the need
  227. + * to preinitialize the context struct to zero.
  228. + * - Removed some totally unused constants.
  229. + */
  230. +
  231. +#ifndef _AES_H
  232. +#define _AES_H
  233. +
  234. +#if defined(__linux__) && defined(__KERNEL__)
  235. +# include <linux/types.h>
  236. +#else
  237. +# include <sys/types.h>
  238. +#endif
  239. +
  240. +// CONFIGURATION OPTIONS (see also aes.c)
  241. +//
  242. +// Define AES_BLOCK_SIZE to set the cipher block size (16, 24 or 32) or
  243. +// leave this undefined for dynamically variable block size (this will
  244. +// result in much slower code).
  245. +// IMPORTANT NOTE: AES_BLOCK_SIZE is in BYTES (16, 24, 32 or undefined). If
  246. +// left undefined a slower version providing variable block length is compiled
  247. +
  248. +#define AES_BLOCK_SIZE 16
  249. +
  250. +// The number of key schedule words for different block and key lengths
  251. +// allowing for method of computation which requires the length to be a
  252. +// multiple of the key length
  253. +//
  254. +// Nk = 4 6 8
  255. +// -------------
  256. +// Nb = 4 | 60 60 64
  257. +// 6 | 96 90 96
  258. +// 8 | 120 120 120
  259. +
  260. +#if !defined(AES_BLOCK_SIZE) || (AES_BLOCK_SIZE == 32)
  261. +#define AES_KS_LENGTH 120
  262. +#define AES_RC_LENGTH 29
  263. +#else
  264. +#define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
  265. +#define AES_RC_LENGTH (9 * AES_BLOCK_SIZE) / 8 - 8
  266. +#endif
  267. +
  268. +typedef struct
  269. +{
  270. + u_int32_t aes_Nkey; // the number of words in the key input block
  271. + u_int32_t aes_Nrnd; // the number of cipher rounds
  272. + u_int32_t aes_e_key[AES_KS_LENGTH]; // the encryption key schedule
  273. + u_int32_t aes_d_key[AES_KS_LENGTH]; // the decryption key schedule
  274. +#if !defined(AES_BLOCK_SIZE)
  275. + u_int32_t aes_Ncol; // the number of columns in the cipher state
  276. +#endif
  277. +} aes_context;
  278. +
  279. +// THE CIPHER INTERFACE
  280. +
  281. +#if !defined(AES_BLOCK_SIZE)
  282. +extern void aes_set_blk(aes_context *, const int);
  283. +#endif
  284. +extern void aes_set_key(aes_context *, const unsigned char [], const int, const int);
  285. +extern void aes_encrypt(const aes_context *, const unsigned char [], unsigned char []);
  286. +extern void aes_decrypt(const aes_context *, const unsigned char [], unsigned char []);
  287. +
  288. +// The block length inputs to aes_set_block and aes_set_key are in numbers
  289. +// of bytes or bits. The calls to subroutines must be made in the above
  290. +// order but multiple calls can be made without repeating earlier calls
  291. +// if their parameters have not changed.
  292. +
  293. +#endif // _AES_H
  294. --- /dev/null Tue Mar 11 13:02:56 2003
  295. +++ linux/include/crypto/aes_cbc.h Mon Feb 9 13:51:03 2004
  296. @@ -0,0 +1,4 @@
  297. +/* Glue header */
  298. +#include "aes.h"
  299. +int AES_set_key(aes_context *aes_ctx, const u_int8_t * key, int keysize);
  300. +int AES_cbc_encrypt(aes_context *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt);
  301. --- /dev/null Tue Mar 11 13:02:56 2003
  302. +++ linux/include/crypto/aes_xcbc_mac.h Mon Feb 9 13:51:03 2004
  303. @@ -0,0 +1,12 @@
  304. +#ifndef _AES_XCBC_MAC_H
  305. +#define _AES_XCBC_MAC_H
  306. +
  307. +typedef u_int32_t aes_block[4];
  308. +typedef struct {
  309. + aes_context ctx_k1;
  310. + aes_block k2;
  311. + aes_block k3;
  312. +} aes_context_mac;
  313. +int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen);
  314. +int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]);
  315. +#endif /* _AES_XCBC_MAC_H */
  316. --- /dev/null Tue Mar 11 13:02:56 2003
  317. +++ linux/include/crypto/cbc_generic.h Mon Feb 9 13:51:03 2004
  318. @@ -0,0 +1,110 @@
  319. +#ifndef _CBC_GENERIC_H
  320. +#define _CBC_GENERIC_H
  321. +/*
  322. + * CBC macro helpers
  323. + *
  324. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  325. + *
  326. + * This program is free software; you can redistribute it and/or modify it
  327. + * under the terms of the GNU General Public License as published by the
  328. + * Free Software Foundation; either version 2 of the License, or (at your
  329. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  330. + *
  331. + * This program is distributed in the hope that it will be useful, but
  332. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  333. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  334. + * for more details.
  335. + *
  336. + */
  337. +
  338. +/*
  339. + * Heavily inspired in loop_AES
  340. + */
  341. +#define CBC_IMPL_BLK16(name, ctx_type, addr_type, enc_func, dec_func) \
  342. +int name(ctx_type *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
  343. + int ret=ilen, pos; \
  344. + const u_int32_t *iv_i; \
  345. + if ((ilen) % 16) return 0; \
  346. + if (encrypt) { \
  347. + pos=0; \
  348. + while(pos<ilen) { \
  349. + if (pos==0) \
  350. + iv_i=(const u_int32_t*) iv; \
  351. + else \
  352. + iv_i=(const u_int32_t*) (out-16); \
  353. + *((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
  354. + *((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
  355. + *((u_int32_t *)(&out[ 8])) = iv_i[2]^*((const u_int32_t *)(&in[ 8])); \
  356. + *((u_int32_t *)(&out[12])) = iv_i[3]^*((const u_int32_t *)(&in[12])); \
  357. + enc_func(ctx, (addr_type) out, (addr_type) out); \
  358. + in+=16; \
  359. + out+=16; \
  360. + pos+=16; \
  361. + } \
  362. + } else { \
  363. + pos=ilen-16; \
  364. + in+=pos; \
  365. + out+=pos; \
  366. + while(pos>=0) { \
  367. + dec_func(ctx, (const addr_type) in, (addr_type) out); \
  368. + if (pos==0) \
  369. + iv_i=(const u_int32_t*) (iv); \
  370. + else \
  371. + iv_i=(const u_int32_t*) (in-16); \
  372. + *((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
  373. + *((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
  374. + *((u_int32_t *)(&out[ 8])) ^= iv_i[2]; \
  375. + *((u_int32_t *)(&out[12])) ^= iv_i[3]; \
  376. + in-=16; \
  377. + out-=16; \
  378. + pos-=16; \
  379. + } \
  380. + } \
  381. + return ret; \
  382. +}
  383. +#define CBC_IMPL_BLK8(name, ctx_type, addr_type, enc_func, dec_func) \
  384. +int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
  385. + int ret=ilen, pos; \
  386. + const u_int32_t *iv_i; \
  387. + if ((ilen) % 8) return 0; \
  388. + if (encrypt) { \
  389. + pos=0; \
  390. + while(pos<ilen) { \
  391. + if (pos==0) \
  392. + iv_i=(const u_int32_t*) iv; \
  393. + else \
  394. + iv_i=(const u_int32_t*) (out-8); \
  395. + *((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
  396. + *((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
  397. + enc_func(ctx, (addr_type)out, (addr_type)out); \
  398. + in+=8; \
  399. + out+=8; \
  400. + pos+=8; \
  401. + } \
  402. + } else { \
  403. + pos=ilen-8; \
  404. + in+=pos; \
  405. + out+=pos; \
  406. + while(pos>=0) { \
  407. + dec_func(ctx, (const addr_type)in, (addr_type)out); \
  408. + if (pos==0) \
  409. + iv_i=(const u_int32_t*) (iv); \
  410. + else \
  411. + iv_i=(const u_int32_t*) (in-8); \
  412. + *((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
  413. + *((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
  414. + in-=8; \
  415. + out-=8; \
  416. + pos-=8; \
  417. + } \
  418. + } \
  419. + return ret; \
  420. +}
  421. +#define CBC_DECL(name, ctx_type) \
  422. +int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt)
  423. +/*
  424. +Eg.:
  425. +CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
  426. +CBC_DECL(AES_cbc_encrypt, aes_context);
  427. +*/
  428. +#endif /* _CBC_GENERIC_H */
  429. --- /dev/null Tue Mar 11 13:02:56 2003
  430. +++ linux/include/crypto/des.h Mon Feb 9 13:51:03 2004
  431. @@ -0,0 +1,298 @@
  432. +/* crypto/des/des.org */
  433. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  434. + * All rights reserved.
  435. + *
  436. + * This package is an SSL implementation written
  437. + * by Eric Young (eay@cryptsoft.com).
  438. + * The implementation was written so as to conform with Netscapes SSL.
  439. + *
  440. + * This library is free for commercial and non-commercial use as long as
  441. + * the following conditions are aheared to. The following conditions
  442. + * apply to all code found in this distribution, be it the RC4, RSA,
  443. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  444. + * included with this distribution is covered by the same copyright terms
  445. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  446. + *
  447. + * Copyright remains Eric Young's, and as such any Copyright notices in
  448. + * the code are not to be removed.
  449. + * If this package is used in a product, Eric Young should be given attribution
  450. + * as the author of the parts of the library used.
  451. + * This can be in the form of a textual message at program startup or
  452. + * in documentation (online or textual) provided with the package.
  453. + *
  454. + * Redistribution and use in source and binary forms, with or without
  455. + * modification, are permitted provided that the following conditions
  456. + * are met:
  457. + * 1. Redistributions of source code must retain the copyright
  458. + * notice, this list of conditions and the following disclaimer.
  459. + * 2. Redistributions in binary form must reproduce the above copyright
  460. + * notice, this list of conditions and the following disclaimer in the
  461. + * documentation and/or other materials provided with the distribution.
  462. + * 3. All advertising materials mentioning features or use of this software
  463. + * must display the following acknowledgement:
  464. + * "This product includes cryptographic software written by
  465. + * Eric Young (eay@cryptsoft.com)"
  466. + * The word 'cryptographic' can be left out if the rouines from the library
  467. + * being used are not cryptographic related :-).
  468. + * 4. If you include any Windows specific code (or a derivative thereof) from
  469. + * the apps directory (application code) you must include an acknowledgement:
  470. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  471. + *
  472. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  473. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  474. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  475. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  476. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  477. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  478. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  479. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  480. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  481. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  482. + * SUCH DAMAGE.
  483. + *
  484. + * The licence and distribution terms for any publically available version or
  485. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  486. + * copied and put under another distribution licence
  487. + * [including the GNU Public Licence.]
  488. + */
  489. +
  490. +/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  491. + *
  492. + * Always modify des.org since des.h is automatically generated from
  493. + * it during SSLeay configuration.
  494. + *
  495. + * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  496. + */
  497. +
  498. +#ifndef HEADER_DES_H
  499. +#define HEADER_DES_H
  500. +
  501. +#ifdef __cplusplus
  502. +extern "C" {
  503. +#endif
  504. +
  505. +
  506. +/* If this is set to 'unsigned int' on a DEC Alpha, this gives about a
  507. + * %20 speed up (longs are 8 bytes, int's are 4). */
  508. +/* Must be unsigned int on ia64/Itanium or DES breaks badly */
  509. +
  510. +#ifdef __KERNEL__
  511. +#include <linux/types.h>
  512. +#else
  513. +#include <sys/types.h>
  514. +#endif
  515. +
  516. +#ifndef DES_LONG
  517. +#define DES_LONG u_int32_t
  518. +#endif
  519. +
  520. +typedef unsigned char des_cblock[8];
  521. +typedef struct { des_cblock ks; } des_key_schedule[16];
  522. +
  523. +#define DES_KEY_SZ (sizeof(des_cblock))
  524. +#define DES_SCHEDULE_SZ (sizeof(des_key_schedule))
  525. +
  526. +#define DES_ENCRYPT 1
  527. +#define DES_DECRYPT 0
  528. +
  529. +#define DES_CBC_MODE 0
  530. +#define DES_PCBC_MODE 1
  531. +
  532. +#define des_ecb2_encrypt(i,o,k1,k2,e) \
  533. + des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
  534. +
  535. +#define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
  536. + des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
  537. +
  538. +#define des_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
  539. + des_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
  540. +
  541. +#define des_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
  542. + des_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
  543. +
  544. +#define C_Block des_cblock
  545. +#define Key_schedule des_key_schedule
  546. +#ifdef KERBEROS
  547. +#define ENCRYPT DES_ENCRYPT
  548. +#define DECRYPT DES_DECRYPT
  549. +#endif
  550. +#define KEY_SZ DES_KEY_SZ
  551. +#define string_to_key des_string_to_key
  552. +#define read_pw_string des_read_pw_string
  553. +#define random_key des_random_key
  554. +#define pcbc_encrypt des_pcbc_encrypt
  555. +#define set_key des_set_key
  556. +#define key_sched des_key_sched
  557. +#define ecb_encrypt des_ecb_encrypt
  558. +#define cbc_encrypt des_cbc_encrypt
  559. +#define ncbc_encrypt des_ncbc_encrypt
  560. +#define xcbc_encrypt des_xcbc_encrypt
  561. +#define cbc_cksum des_cbc_cksum
  562. +#define quad_cksum des_quad_cksum
  563. +
  564. +/* For compatibility with the MIT lib - eay 20/05/92 */
  565. +typedef des_key_schedule bit_64;
  566. +#define des_fixup_key_parity des_set_odd_parity
  567. +#define des_check_key_parity check_parity
  568. +
  569. +extern int des_check_key; /* defaults to false */
  570. +extern int des_rw_mode; /* defaults to DES_PCBC_MODE */
  571. +
  572. +/* The next line is used to disable full ANSI prototypes, if your
  573. + * compiler has problems with the prototypes, make sure this line always
  574. + * evaluates to true :-) */
  575. +#if defined(MSDOS) || defined(__STDC__)
  576. +#undef NOPROTO
  577. +#endif
  578. +#ifndef NOPROTO
  579. +char *des_options(void);
  580. +void des_ecb3_encrypt(des_cblock *input,des_cblock *output,
  581. + des_key_schedule ks1,des_key_schedule ks2,
  582. + des_key_schedule ks3, int enc);
  583. +DES_LONG des_cbc_cksum(des_cblock *input,des_cblock *output,
  584. + long length,des_key_schedule schedule,des_cblock *ivec);
  585. +void des_cbc_encrypt(des_cblock *input,des_cblock *output,long length,
  586. + des_key_schedule schedule,des_cblock *ivec,int enc);
  587. +void des_ncbc_encrypt(des_cblock *input,des_cblock *output,long length,
  588. + des_key_schedule schedule,des_cblock *ivec,int enc);
  589. +void des_xcbc_encrypt(des_cblock *input,des_cblock *output,long length,
  590. + des_key_schedule schedule,des_cblock *ivec,
  591. + des_cblock *inw,des_cblock *outw,int enc);
  592. +void des_cfb_encrypt(unsigned char *in,unsigned char *out,int numbits,
  593. + long length,des_key_schedule schedule,des_cblock *ivec,int enc);
  594. +void des_ecb_encrypt(des_cblock *input,des_cblock *output,
  595. + des_key_schedule ks,int enc);
  596. +void des_encrypt(DES_LONG *data,des_key_schedule ks, int enc);
  597. +void des_encrypt2(DES_LONG *data,des_key_schedule ks, int enc);
  598. +void des_encrypt3(DES_LONG *data, des_key_schedule ks1,
  599. + des_key_schedule ks2, des_key_schedule ks3);
  600. +void des_decrypt3(DES_LONG *data, des_key_schedule ks1,
  601. + des_key_schedule ks2, des_key_schedule ks3);
  602. +void des_ede3_cbc_encrypt(des_cblock *input, des_cblock *output,
  603. + long length, des_key_schedule ks1, des_key_schedule ks2,
  604. + des_key_schedule ks3, des_cblock *ivec, int enc);
  605. +void des_ede3_cfb64_encrypt(unsigned char *in, unsigned char *out,
  606. + long length, des_key_schedule ks1, des_key_schedule ks2,
  607. + des_key_schedule ks3, des_cblock *ivec, int *num, int enc);
  608. +void des_ede3_ofb64_encrypt(unsigned char *in, unsigned char *out,
  609. + long length, des_key_schedule ks1, des_key_schedule ks2,
  610. + des_key_schedule ks3, des_cblock *ivec, int *num);
  611. +
  612. +void des_xwhite_in2out(des_cblock (*des_key), des_cblock (*in_white),
  613. + des_cblock (*out_white));
  614. +
  615. +int des_enc_read(int fd,char *buf,int len,des_key_schedule sched,
  616. + des_cblock *iv);
  617. +int des_enc_write(int fd,char *buf,int len,des_key_schedule sched,
  618. + des_cblock *iv);
  619. +char *des_fcrypt(const char *buf,const char *salt, char *ret);
  620. +#ifdef PERL5
  621. +char *des_crypt(const char *buf,const char *salt);
  622. +#else
  623. +/* some stupid compilers complain because I have declared char instead
  624. + * of const char */
  625. +#ifndef __KERNEL__
  626. +#ifdef HEADER_DES_LOCL_H
  627. +char *crypt(const char *buf,const char *salt);
  628. +#else /* HEADER_DES_LOCL_H */
  629. +char *crypt(void);
  630. +#endif /* HEADER_DES_LOCL_H */
  631. +#endif /* __KERNEL__ */
  632. +#endif /* PERL5 */
  633. +void des_ofb_encrypt(unsigned char *in,unsigned char *out,
  634. + int numbits,long length,des_key_schedule schedule,des_cblock *ivec);
  635. +void des_pcbc_encrypt(des_cblock *input,des_cblock *output,long length,
  636. + des_key_schedule schedule,des_cblock *ivec,int enc);
  637. +DES_LONG des_quad_cksum(des_cblock *input,des_cblock *output,
  638. + long length,int out_count,des_cblock *seed);
  639. +void des_random_seed(des_cblock key);
  640. +void des_random_key(des_cblock ret);
  641. +int des_read_password(des_cblock *key,char *prompt,int verify);
  642. +int des_read_2passwords(des_cblock *key1,des_cblock *key2,
  643. + char *prompt,int verify);
  644. +int des_read_pw_string(char *buf,int length,char *prompt,int verify);
  645. +void des_set_odd_parity(des_cblock *key);
  646. +int des_is_weak_key(des_cblock *key);
  647. +int des_set_key(des_cblock *key,des_key_schedule schedule);
  648. +int des_key_sched(des_cblock *key,des_key_schedule schedule);
  649. +void des_string_to_key(char *str,des_cblock *key);
  650. +void des_string_to_2keys(char *str,des_cblock *key1,des_cblock *key2);
  651. +void des_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
  652. + des_key_schedule schedule, des_cblock *ivec, int *num, int enc);
  653. +void des_ofb64_encrypt(unsigned char *in, unsigned char *out, long length,
  654. + des_key_schedule schedule, des_cblock *ivec, int *num);
  655. +int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify);
  656. +
  657. +/* Extra functions from Mark Murray <mark@grondar.za> */
  658. +/* The following functions are not in the normal unix build or the
  659. + * SSLeay build. When using the SSLeay build, use RAND_seed()
  660. + * and RAND_bytes() instead. */
  661. +int des_new_random_key(des_cblock *key);
  662. +void des_init_random_number_generator(des_cblock *key);
  663. +void des_set_random_generator_seed(des_cblock *key);
  664. +void des_set_sequence_number(des_cblock new_sequence_number);
  665. +void des_generate_random_block(des_cblock *block);
  666. +
  667. +#else
  668. +
  669. +char *des_options();
  670. +void des_ecb3_encrypt();
  671. +DES_LONG des_cbc_cksum();
  672. +void des_cbc_encrypt();
  673. +void des_ncbc_encrypt();
  674. +void des_xcbc_encrypt();
  675. +void des_cfb_encrypt();
  676. +void des_ede3_cfb64_encrypt();
  677. +void des_ede3_ofb64_encrypt();
  678. +void des_ecb_encrypt();
  679. +void des_encrypt();
  680. +void des_encrypt2();
  681. +void des_encrypt3();
  682. +void des_decrypt3();
  683. +void des_ede3_cbc_encrypt();
  684. +int des_enc_read();
  685. +int des_enc_write();
  686. +char *des_fcrypt();
  687. +#ifdef PERL5
  688. +char *des_crypt();
  689. +#else
  690. +char *crypt();
  691. +#endif
  692. +void des_ofb_encrypt();
  693. +void des_pcbc_encrypt();
  694. +DES_LONG des_quad_cksum();
  695. +void des_random_seed();
  696. +void des_random_key();
  697. +int des_read_password();
  698. +int des_read_2passwords();
  699. +int des_read_pw_string();
  700. +void des_set_odd_parity();
  701. +int des_is_weak_key();
  702. +int des_set_key();
  703. +int des_key_sched();
  704. +void des_string_to_key();
  705. +void des_string_to_2keys();
  706. +void des_cfb64_encrypt();
  707. +void des_ofb64_encrypt();
  708. +int des_read_pw();
  709. +void des_xwhite_in2out();
  710. +
  711. +/* Extra functions from Mark Murray <mark@grondar.za> */
  712. +/* The following functions are not in the normal unix build or the
  713. + * SSLeay build. When using the SSLeay build, use RAND_seed()
  714. + * and RAND_bytes() instead. */
  715. +#ifdef FreeBSD
  716. +int des_new_random_key();
  717. +void des_init_random_number_generator();
  718. +void des_set_random_generator_seed();
  719. +void des_set_sequence_number();
  720. +void des_generate_random_block();
  721. +#endif
  722. +
  723. +#endif
  724. +
  725. +#ifdef __cplusplus
  726. +}
  727. +#endif
  728. +
  729. +#endif
  730. --- /dev/null Tue Mar 11 13:02:56 2003
  731. +++ linux/include/des/des_locl.h Mon Feb 9 13:51:03 2004
  732. @@ -0,0 +1,515 @@
  733. +/* crypto/des/des_locl.org */
  734. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  735. + * All rights reserved.
  736. + *
  737. + * This package is an SSL implementation written
  738. + * by Eric Young (eay@cryptsoft.com).
  739. + * The implementation was written so as to conform with Netscapes SSL.
  740. + *
  741. + * This library is free for commercial and non-commercial use as long as
  742. + * the following conditions are aheared to. The following conditions
  743. + * apply to all code found in this distribution, be it the RC4, RSA,
  744. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  745. + * included with this distribution is covered by the same copyright terms
  746. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  747. + *
  748. + * Copyright remains Eric Young's, and as such any Copyright notices in
  749. + * the code are not to be removed.
  750. + * If this package is used in a product, Eric Young should be given attribution
  751. + * as the author of the parts of the library used.
  752. + * This can be in the form of a textual message at program startup or
  753. + * in documentation (online or textual) provided with the package.
  754. + *
  755. + * Redistribution and use in source and binary forms, with or without
  756. + * modification, are permitted provided that the following conditions
  757. + * are met:
  758. + * 1. Redistributions of source code must retain the copyright
  759. + * notice, this list of conditions and the following disclaimer.
  760. + * 2. Redistributions in binary form must reproduce the above copyright
  761. + * notice, this list of conditions and the following disclaimer in the
  762. + * documentation and/or other materials provided with the distribution.
  763. + * 3. All advertising materials mentioning features or use of this software
  764. + * must display the following acknowledgement:
  765. + * "This product includes cryptographic software written by
  766. + * Eric Young (eay@cryptsoft.com)"
  767. + * The word 'cryptographic' can be left out if the rouines from the library
  768. + * being used are not cryptographic related :-).
  769. + * 4. If you include any Windows specific code (or a derivative thereof) from
  770. + * the apps directory (application code) you must include an acknowledgement:
  771. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  772. + *
  773. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  774. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  775. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  776. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  777. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  778. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  779. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  780. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  781. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  782. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  783. + * SUCH DAMAGE.
  784. + *
  785. + * The licence and distribution terms for any publically available version or
  786. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  787. + * copied and put under another distribution licence
  788. + * [including the GNU Public Licence.]
  789. + */
  790. +
  791. +/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  792. + *
  793. + * Always modify des_locl.org since des_locl.h is automatically generated from
  794. + * it during SSLeay configuration.
  795. + *
  796. + * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  797. + */
  798. +
  799. +#ifndef HEADER_DES_LOCL_H
  800. +#define HEADER_DES_LOCL_H
  801. +
  802. +#if defined(WIN32) || defined(WIN16)
  803. +#ifndef MSDOS
  804. +#define MSDOS
  805. +#endif
  806. +#endif
  807. +
  808. +#include "crypto/des.h"
  809. +
  810. +#ifndef DES_DEFAULT_OPTIONS
  811. +/* the following is tweaked from a config script, that is why it is a
  812. + * protected undef/define */
  813. +#ifndef DES_PTR
  814. +#define DES_PTR
  815. +#endif
  816. +
  817. +/* This helps C compiler generate the correct code for multiple functional
  818. + * units. It reduces register dependancies at the expense of 2 more
  819. + * registers */
  820. +#ifndef DES_RISC1
  821. +#define DES_RISC1
  822. +#endif
  823. +
  824. +#ifndef DES_RISC2
  825. +#undef DES_RISC2
  826. +#endif
  827. +
  828. +#if defined(DES_RISC1) && defined(DES_RISC2)
  829. +YOU SHOULD NOT HAVE BOTH DES_RISC1 AND DES_RISC2 DEFINED!!!!!
  830. +#endif
  831. +
  832. +/* Unroll the inner loop, this sometimes helps, sometimes hinders.
  833. + * Very mucy CPU dependant */
  834. +#ifndef DES_UNROLL
  835. +#define DES_UNROLL
  836. +#endif
  837. +
  838. +/* These default values were supplied by
  839. + * Peter Gutman <pgut001@cs.auckland.ac.nz>
  840. + * They are only used if nothing else has been defined */
  841. +#if !defined(DES_PTR) && !defined(DES_RISC1) && !defined(DES_RISC2) && !defined(DES_UNROLL)
  842. +/* Special defines which change the way the code is built depending on the
  843. + CPU and OS. For SGI machines you can use _MIPS_SZLONG (32 or 64) to find
  844. + even newer MIPS CPU's, but at the moment one size fits all for
  845. + optimization options. Older Sparc's work better with only UNROLL, but
  846. + there's no way to tell at compile time what it is you're running on */
  847. +
  848. +#if defined( sun ) /* Newer Sparc's */
  849. + #define DES_PTR
  850. + #define DES_RISC1
  851. + #define DES_UNROLL
  852. +#elif defined( __ultrix ) /* Older MIPS */
  853. + #define DES_PTR
  854. + #define DES_RISC2
  855. + #define DES_UNROLL
  856. +#elif defined( __osf1__ ) /* Alpha */
  857. + #define DES_PTR
  858. + #define DES_RISC2
  859. +#elif defined ( _AIX ) /* RS6000 */
  860. + /* Unknown */
  861. +#elif defined( __hpux ) /* HP-PA */
  862. + /* Unknown */
  863. +#elif defined( __aux ) /* 68K */
  864. + /* Unknown */
  865. +#elif defined( __dgux ) /* 88K (but P6 in latest boxes) */
  866. + #define DES_UNROLL
  867. +#elif defined( __sgi ) /* Newer MIPS */
  868. + #define DES_PTR
  869. + #define DES_RISC2
  870. + #define DES_UNROLL
  871. +#elif defined( i386 ) /* x86 boxes, should be gcc */
  872. + #define DES_PTR
  873. + #define DES_RISC1
  874. + #define DES_UNROLL
  875. +#endif /* Systems-specific speed defines */
  876. +#endif
  877. +
  878. +#endif /* DES_DEFAULT_OPTIONS */
  879. +
  880. +#ifdef MSDOS /* Visual C++ 2.1 (Windows NT/95) */
  881. +#include <stdlib.h>
  882. +#include <errno.h>
  883. +#include <time.h>
  884. +#include <io.h>
  885. +#ifndef RAND
  886. +#define RAND
  887. +#endif
  888. +#undef NOPROTO
  889. +#endif
  890. +
  891. +#if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS)
  892. +#ifndef __KERNEL__
  893. +#include <string.h>
  894. +#else
  895. +#include <linux/string.h>
  896. +#endif
  897. +#endif
  898. +
  899. +#ifndef RAND
  900. +#define RAND
  901. +#endif
  902. +
  903. +#ifdef linux
  904. +#undef RAND
  905. +#endif
  906. +
  907. +#ifdef MSDOS
  908. +#define getpid() 2
  909. +#define RAND
  910. +#undef NOPROTO
  911. +#endif
  912. +
  913. +#if defined(NOCONST)
  914. +#define const
  915. +#endif
  916. +
  917. +#ifdef __STDC__
  918. +#undef NOPROTO
  919. +#endif
  920. +
  921. +#ifdef RAND
  922. +#define srandom(s) srand(s)
  923. +#define random rand
  924. +#endif
  925. +
  926. +#define ITERATIONS 16
  927. +#define HALF_ITERATIONS 8
  928. +
  929. +/* used in des_read and des_write */
  930. +#define MAXWRITE (1024*16)
  931. +#define BSIZE (MAXWRITE+4)
  932. +
  933. +#define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
  934. + l|=((DES_LONG)(*((c)++)))<< 8L, \
  935. + l|=((DES_LONG)(*((c)++)))<<16L, \
  936. + l|=((DES_LONG)(*((c)++)))<<24L)
  937. +
  938. +/* NOTE - c is not incremented as per c2l */
  939. +#define c2ln(c,l1,l2,n) { \
  940. + c+=n; \
  941. + l1=l2=0; \
  942. + switch (n) { \
  943. + case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
  944. + case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
  945. + case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
  946. + case 5: l2|=((DES_LONG)(*(--(c)))); \
  947. + case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
  948. + case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
  949. + case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
  950. + case 1: l1|=((DES_LONG)(*(--(c)))); \
  951. + } \
  952. + }
  953. +
  954. +#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  955. + *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  956. + *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  957. + *((c)++)=(unsigned char)(((l)>>24L)&0xff))
  958. +
  959. +/* replacements for htonl and ntohl since I have no idea what to do
  960. + * when faced with machines with 8 byte longs. */
  961. +#define HDRSIZE 4
  962. +
  963. +#define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
  964. + l|=((DES_LONG)(*((c)++)))<<16L, \
  965. + l|=((DES_LONG)(*((c)++)))<< 8L, \
  966. + l|=((DES_LONG)(*((c)++))))
  967. +
  968. +#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
  969. + *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  970. + *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  971. + *((c)++)=(unsigned char)(((l) )&0xff))
  972. +
  973. +/* NOTE - c is not incremented as per l2c */
  974. +#define l2cn(l1,l2,c,n) { \
  975. + c+=n; \
  976. + switch (n) { \
  977. + case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
  978. + case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
  979. + case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
  980. + case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
  981. + case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
  982. + case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
  983. + case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
  984. + case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
  985. + } \
  986. + }
  987. +
  988. +#if defined(WIN32)
  989. +#define ROTATE(a,n) (_lrotr(a,n))
  990. +#else
  991. +#define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
  992. +#endif
  993. +
  994. +/* Don't worry about the LOAD_DATA() stuff, that is used by
  995. + * fcrypt() to add it's little bit to the front */
  996. +
  997. +#ifdef DES_FCRYPT
  998. +
  999. +#define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
  1000. + { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
  1001. +
  1002. +#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
  1003. + t=R^(R>>16L); \
  1004. + u=t&E0; t&=E1; \
  1005. + tmp=(u<<16); u^=R^s[S ]; u^=tmp; \
  1006. + tmp=(t<<16); t^=R^s[S+1]; t^=tmp
  1007. +#else
  1008. +#define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
  1009. +#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
  1010. + u=R^s[S ]; \
  1011. + t=R^s[S+1]
  1012. +#endif
  1013. +
  1014. +/* The changes to this macro may help or hinder, depending on the
  1015. + * compiler and the achitecture. gcc2 always seems to do well :-).
  1016. + * Inspired by Dana How <how@isl.stanford.edu>
  1017. + * DO NOT use the alternative version on machines with 8 byte longs.
  1018. + * It does not seem to work on the Alpha, even when DES_LONG is 4
  1019. + * bytes, probably an issue of accessing non-word aligned objects :-( */
  1020. +#ifdef DES_PTR
  1021. +
  1022. +/* It recently occured to me that 0^0^0^0^0^0^0 == 0, so there
  1023. + * is no reason to not xor all the sub items together. This potentially
  1024. + * saves a register since things can be xored directly into L */
  1025. +
  1026. +#if defined(DES_RISC1) || defined(DES_RISC2)
  1027. +#ifdef DES_RISC1
  1028. +#define D_ENCRYPT(LL,R,S) { \
  1029. + unsigned int u1,u2,u3; \
  1030. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1031. + u2=(int)u>>8L; \
  1032. + u1=(int)u&0xfc; \
  1033. + u2&=0xfc; \
  1034. + t=ROTATE(t,4); \
  1035. + u>>=16L; \
  1036. + LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
  1037. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
  1038. + u3=(int)(u>>8L); \
  1039. + u1=(int)u&0xfc; \
  1040. + u3&=0xfc; \
  1041. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+u1); \
  1042. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+u3); \
  1043. + u2=(int)t>>8L; \
  1044. + u1=(int)t&0xfc; \
  1045. + u2&=0xfc; \
  1046. + t>>=16L; \
  1047. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
  1048. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
  1049. + u3=(int)t>>8L; \
  1050. + u1=(int)t&0xfc; \
  1051. + u3&=0xfc; \
  1052. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+u1); \
  1053. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+u3); }
  1054. +#endif
  1055. +#ifdef DES_RISC2
  1056. +#define D_ENCRYPT(LL,R,S) { \
  1057. + unsigned int u1,u2,s1,s2; \
  1058. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1059. + u2=(int)u>>8L; \
  1060. + u1=(int)u&0xfc; \
  1061. + u2&=0xfc; \
  1062. + t=ROTATE(t,4); \
  1063. + LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
  1064. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
  1065. + s1=(int)(u>>16L); \
  1066. + s2=(int)(u>>24L); \
  1067. + s1&=0xfc; \
  1068. + s2&=0xfc; \
  1069. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+s1); \
  1070. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+s2); \
  1071. + u2=(int)t>>8L; \
  1072. + u1=(int)t&0xfc; \
  1073. + u2&=0xfc; \
  1074. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
  1075. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
  1076. + s1=(int)(t>>16L); \
  1077. + s2=(int)(t>>24L); \
  1078. + s1&=0xfc; \
  1079. + s2&=0xfc; \
  1080. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+s1); \
  1081. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+s2); }
  1082. +#endif
  1083. +#else
  1084. +#define D_ENCRYPT(LL,R,S) { \
  1085. + LOAD_DATA_tmp(R,S,u,t,E0,E1); \
  1086. + t=ROTATE(t,4); \
  1087. + LL^= \
  1088. + *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))^ \
  1089. + *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8L)&0xfc))^ \
  1090. + *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16L)&0xfc))^ \
  1091. + *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24L)&0xfc))^ \
  1092. + *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))^ \
  1093. + *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8L)&0xfc))^ \
  1094. + *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16L)&0xfc))^ \
  1095. + *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24L)&0xfc)); }
  1096. +#endif
  1097. +
  1098. +#else /* original version */
  1099. +
  1100. +#if defined(DES_RISC1) || defined(DES_RISC2)
  1101. +#ifdef DES_RISC1
  1102. +#define D_ENCRYPT(LL,R,S) {\
  1103. + unsigned int u1,u2,u3; \
  1104. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1105. + u>>=2L; \
  1106. + t=ROTATE(t,6); \
  1107. + u2=(int)u>>8L; \
  1108. + u1=(int)u&0x3f; \
  1109. + u2&=0x3f; \
  1110. + u>>=16L; \
  1111. + LL^=des_SPtrans[0][u1]; \
  1112. + LL^=des_SPtrans[2][u2]; \
  1113. + u3=(int)u>>8L; \
  1114. + u1=(int)u&0x3f; \
  1115. + u3&=0x3f; \
  1116. + LL^=des_SPtrans[4][u1]; \
  1117. + LL^=des_SPtrans[6][u3]; \
  1118. + u2=(int)t>>8L; \
  1119. + u1=(int)t&0x3f; \
  1120. + u2&=0x3f; \
  1121. + t>>=16L; \
  1122. + LL^=des_SPtrans[1][u1]; \
  1123. + LL^=des_SPtrans[3][u2]; \
  1124. + u3=(int)t>>8L; \
  1125. + u1=(int)t&0x3f; \
  1126. + u3&=0x3f; \
  1127. + LL^=des_SPtrans[5][u1]; \
  1128. + LL^=des_SPtrans[7][u3]; }
  1129. +#endif
  1130. +#ifdef DES_RISC2
  1131. +#define D_ENCRYPT(LL,R,S) {\
  1132. + unsigned int u1,u2,s1,s2; \
  1133. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1134. + u>>=2L; \
  1135. + t=ROTATE(t,6); \
  1136. + u2=(int)u>>8L; \
  1137. + u1=(int)u&0x3f; \
  1138. + u2&=0x3f; \
  1139. + LL^=des_SPtrans[0][u1]; \
  1140. + LL^=des_SPtrans[2][u2]; \
  1141. + s1=(int)u>>16L; \
  1142. + s2=(int)u>>24L; \
  1143. + s1&=0x3f; \
  1144. + s2&=0x3f; \
  1145. + LL^=des_SPtrans[4][s1]; \
  1146. + LL^=des_SPtrans[6][s2]; \
  1147. + u2=(int)t>>8L; \
  1148. + u1=(int)t&0x3f; \
  1149. + u2&=0x3f; \
  1150. + LL^=des_SPtrans[1][u1]; \
  1151. + LL^=des_SPtrans[3][u2]; \
  1152. + s1=(int)t>>16; \
  1153. + s2=(int)t>>24L; \
  1154. + s1&=0x3f; \
  1155. + s2&=0x3f; \
  1156. + LL^=des_SPtrans[5][s1]; \
  1157. + LL^=des_SPtrans[7][s2]; }
  1158. +#endif
  1159. +
  1160. +#else
  1161. +
  1162. +#define D_ENCRYPT(LL,R,S) {\
  1163. + LOAD_DATA_tmp(R,S,u,t,E0,E1); \
  1164. + t=ROTATE(t,4); \
  1165. + LL^=\
  1166. + des_SPtrans[0][(u>> 2L)&0x3f]^ \
  1167. + des_SPtrans[2][(u>>10L)&0x3f]^ \
  1168. + des_SPtrans[4][(u>>18L)&0x3f]^ \
  1169. + des_SPtrans[6][(u>>26L)&0x3f]^ \
  1170. + des_SPtrans[1][(t>> 2L)&0x3f]^ \
  1171. + des_SPtrans[3][(t>>10L)&0x3f]^ \
  1172. + des_SPtrans[5][(t>>18L)&0x3f]^ \
  1173. + des_SPtrans[7][(t>>26L)&0x3f]; }
  1174. +#endif
  1175. +#endif
  1176. +
  1177. + /* IP and FP
  1178. + * The problem is more of a geometric problem that random bit fiddling.
  1179. + 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
  1180. + 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
  1181. + 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
  1182. + 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
  1183. +
  1184. + 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
  1185. + 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
  1186. + 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
  1187. + 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
  1188. +
  1189. + The output has been subject to swaps of the form
  1190. + 0 1 -> 3 1 but the odd and even bits have been put into
  1191. + 2 3 2 0
  1192. + different words. The main trick is to remember that
  1193. + t=((l>>size)^r)&(mask);
  1194. + r^=t;
  1195. + l^=(t<<size);
  1196. + can be used to swap and move bits between words.
  1197. +
  1198. + So l = 0 1 2 3 r = 16 17 18 19
  1199. + 4 5 6 7 20 21 22 23
  1200. + 8 9 10 11 24 25 26 27
  1201. + 12 13 14 15 28 29 30 31
  1202. + becomes (for size == 2 and mask == 0x3333)
  1203. + t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
  1204. + 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
  1205. + 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
  1206. + 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
  1207. +
  1208. + Thanks for hints from Richard Outerbridge - he told me IP&FP
  1209. + could be done in 15 xor, 10 shifts and 5 ands.
  1210. + When I finally started to think of the problem in 2D
  1211. + I first got ~42 operations without xors. When I remembered
  1212. + how to use xors :-) I got it to its final state.
  1213. + */
  1214. +#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  1215. + (b)^=(t),\
  1216. + (a)^=((t)<<(n)))
  1217. +
  1218. +#define IP(l,r) \
  1219. + { \
  1220. + register DES_LONG tt; \
  1221. + PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
  1222. + PERM_OP(l,r,tt,16,0x0000ffffL); \
  1223. + PERM_OP(r,l,tt, 2,0x33333333L); \
  1224. + PERM_OP(l,r,tt, 8,0x00ff00ffL); \
  1225. + PERM_OP(r,l,tt, 1,0x55555555L); \
  1226. + }
  1227. +
  1228. +#define FP(l,r) \
  1229. + { \
  1230. + register DES_LONG tt; \
  1231. + PERM_OP(l,r,tt, 1,0x55555555L); \
  1232. + PERM_OP(r,l,tt, 8,0x00ff00ffL); \
  1233. + PERM_OP(l,r,tt, 2,0x33333333L); \
  1234. + PERM_OP(r,l,tt,16,0x0000ffffL); \
  1235. + PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
  1236. + }
  1237. +
  1238. +extern const DES_LONG des_SPtrans[8][64];
  1239. +
  1240. +#ifndef NOPROTO
  1241. +void fcrypt_body(DES_LONG *out,des_key_schedule ks,
  1242. + DES_LONG Eswap0, DES_LONG Eswap1);
  1243. +#else
  1244. +void fcrypt_body();
  1245. +#endif
  1246. +
  1247. +#endif
  1248. --- /dev/null Tue Mar 11 13:02:56 2003
  1249. +++ linux/include/des/des_ver.h Mon Feb 9 13:51:03 2004
  1250. @@ -0,0 +1,60 @@
  1251. +/* crypto/des/des_ver.h */
  1252. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1253. + * All rights reserved.
  1254. + *
  1255. + * This package is an SSL implementation written
  1256. + * by Eric Young (eay@cryptsoft.com).
  1257. + * The implementation was written so as to conform with Netscapes SSL.
  1258. + *
  1259. + * This library is free for commercial and non-commercial use as long as
  1260. + * the following conditions are aheared to. The following conditions
  1261. + * apply to all code found in this distribution, be it the RC4, RSA,
  1262. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1263. + * included with this distribution is covered by the same copyright terms
  1264. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1265. + *
  1266. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1267. + * the code are not to be removed.
  1268. + * If this package is used in a product, Eric Young should be given attribution
  1269. + * as the author of the parts of the library used.
  1270. + * This can be in the form of a textual message at program startup or
  1271. + * in documentation (online or textual) provided with the package.
  1272. + *
  1273. + * Redistribution and use in source and binary forms, with or without
  1274. + * modification, are permitted provided that the following conditions
  1275. + * are met:
  1276. + * 1. Redistributions of source code must retain the copyright
  1277. + * notice, this list of conditions and the following disclaimer.
  1278. + * 2. Redistributions in binary form must reproduce the above copyright
  1279. + * notice, this list of conditions and the following disclaimer in the
  1280. + * documentation and/or other materials provided with the distribution.
  1281. + * 3. All advertising materials mentioning features or use of this software
  1282. + * must display the following acknowledgement:
  1283. + * "This product includes cryptographic software written by
  1284. + * Eric Young (eay@cryptsoft.com)"
  1285. + * The word 'cryptographic' can be left out if the rouines from the library
  1286. + * being used are not cryptographic related :-).
  1287. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1288. + * the apps directory (application code) you must include an acknowledgement:
  1289. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1290. + *
  1291. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1292. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1293. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1294. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1295. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1296. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1297. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1298. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1299. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1300. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1301. + * SUCH DAMAGE.
  1302. + *
  1303. + * The licence and distribution terms for any publically available version or
  1304. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1305. + * copied and put under another distribution licence
  1306. + * [including the GNU Public Licence.]
  1307. + */
  1308. +
  1309. +extern char *DES_version; /* SSLeay version string */
  1310. +extern char *libdes_version; /* old libdes version string */
  1311. --- /dev/null Tue Mar 11 13:02:56 2003
  1312. +++ linux/include/des/podd.h Mon Feb 9 13:51:03 2004
  1313. @@ -0,0 +1,75 @@
  1314. +/* crypto/des/podd.h */
  1315. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1316. + * All rights reserved.
  1317. + *
  1318. + * This package is an SSL implementation written
  1319. + * by Eric Young (eay@cryptsoft.com).
  1320. + * The implementation was written so as to conform with Netscapes SSL.
  1321. + *
  1322. + * This library is free for commercial and non-commercial use as long as
  1323. + * the following conditions are aheared to. The following conditions
  1324. + * apply to all code found in this distribution, be it the RC4, RSA,
  1325. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1326. + * included with this distribution is covered by the same copyright terms
  1327. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1328. + *
  1329. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1330. + * the code are not to be removed.
  1331. + * If this package is used in a product, Eric Young should be given attribution
  1332. + * as the author of the parts of the library used.
  1333. + * This can be in the form of a textual message at program startup or
  1334. + * in documentation (online or textual) provided with the package.
  1335. + *
  1336. + * Redistribution and use in source and binary forms, with or without
  1337. + * modification, are permitted provided that the following conditions
  1338. + * are met:
  1339. + * 1. Redistributions of source code must retain the copyright
  1340. + * notice, this list of conditions and the following disclaimer.
  1341. + * 2. Redistributions in binary form must reproduce the above copyright
  1342. + * notice, this list of conditions and the following disclaimer in the
  1343. + * documentation and/or other materials provided with the distribution.
  1344. + * 3. All advertising materials mentioning features or use of this software
  1345. + * must display the following acknowledgement:
  1346. + * "This product includes cryptographic software written by
  1347. + * Eric Young (eay@cryptsoft.com)"
  1348. + * The word 'cryptographic' can be left out if the rouines from the library
  1349. + * being used are not cryptographic related :-).
  1350. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1351. + * the apps directory (application code) you must include an acknowledgement:
  1352. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1353. + *
  1354. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1355. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1356. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1357. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1358. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1359. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1360. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1361. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1362. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1363. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1364. + * SUCH DAMAGE.
  1365. + *
  1366. + * The licence and distribution terms for any publically available version or
  1367. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1368. + * copied and put under another distribution licence
  1369. + * [including the GNU Public Licence.]
  1370. + */
  1371. +
  1372. +static const unsigned char odd_parity[256]={
  1373. + 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
  1374. + 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
  1375. + 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
  1376. + 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
  1377. + 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
  1378. + 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
  1379. + 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
  1380. +112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
  1381. +128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
  1382. +145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
  1383. +161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
  1384. +176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
  1385. +193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
  1386. +208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
  1387. +224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
  1388. +241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254};
  1389. --- /dev/null Tue Mar 11 13:02:56 2003
  1390. +++ linux/include/des/sk.h Mon Feb 9 13:51:03 2004
  1391. @@ -0,0 +1,204 @@
  1392. +/* crypto/des/sk.h */
  1393. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1394. + * All rights reserved.
  1395. + *
  1396. + * This package is an SSL implementation written
  1397. + * by Eric Young (eay@cryptsoft.com).
  1398. + * The implementation was written so as to conform with Netscapes SSL.
  1399. + *
  1400. + * This library is free for commercial and non-commercial use as long as
  1401. + * the following conditions are aheared to. The following conditions
  1402. + * apply to all code found in this distribution, be it the RC4, RSA,
  1403. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1404. + * included with this distribution is covered by the same copyright terms
  1405. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1406. + *
  1407. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1408. + * the code are not to be removed.
  1409. + * If this package is used in a product, Eric Young should be given attribution
  1410. + * as the author of the parts of the library used.
  1411. + * This can be in the form of a textual message at program startup or
  1412. + * in documentation (online or textual) provided with the package.
  1413. + *
  1414. + * Redistribution and use in source and binary forms, with or without
  1415. + * modification, are permitted provided that the following conditions
  1416. + * are met:
  1417. + * 1. Redistributions of source code must retain the copyright
  1418. + * notice, this list of conditions and the following disclaimer.
  1419. + * 2. Redistributions in binary form must reproduce the above copyright
  1420. + * notice, this list of conditions and the following disclaimer in the
  1421. + * documentation and/or other materials provided with the distribution.
  1422. + * 3. All advertising materials mentioning features or use of this software
  1423. + * must display the following acknowledgement:
  1424. + * "This product includes cryptographic software written by
  1425. + * Eric Young (eay@cryptsoft.com)"
  1426. + * The word 'cryptographic' can be left out if the rouines from the library
  1427. + * being used are not cryptographic related :-).
  1428. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1429. + * the apps directory (application code) you must include an acknowledgement:
  1430. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1431. + *
  1432. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1433. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1434. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1435. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1436. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1437. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1438. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1439. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1440. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1441. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1442. + * SUCH DAMAGE.
  1443. + *
  1444. + * The licence and distribution terms for any publically available version or
  1445. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1446. + * copied and put under another distribution licence
  1447. + * [including the GNU Public Licence.]
  1448. + */
  1449. +
  1450. +static const DES_LONG des_skb[8][64]={
  1451. +{
  1452. +/* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  1453. +0x00000000L,0x00000010L,0x20000000L,0x20000010L,
  1454. +0x00010000L,0x00010010L,0x20010000L,0x20010010L,
  1455. +0x00000800L,0x00000810L,0x20000800L,0x20000810L,
  1456. +0x00010800L,0x00010810L,0x20010800L,0x20010810L,
  1457. +0x00000020L,0x00000030L,0x20000020L,0x20000030L,
  1458. +0x00010020L,0x00010030L,0x20010020L,0x20010030L,
  1459. +0x00000820L,0x00000830L,0x20000820L,0x20000830L,
  1460. +0x00010820L,0x00010830L,0x20010820L,0x20010830L,
  1461. +0x00080000L,0x00080010L,0x20080000L,0x20080010L,
  1462. +0x00090000L,0x00090010L,0x20090000L,0x20090010L,
  1463. +0x00080800L,0x00080810L,0x20080800L,0x20080810L,
  1464. +0x00090800L,0x00090810L,0x20090800L,0x20090810L,
  1465. +0x00080020L,0x00080030L,0x20080020L,0x20080030L,
  1466. +0x00090020L,0x00090030L,0x20090020L,0x20090030L,
  1467. +0x00080820L,0x00080830L,0x20080820L,0x20080830L,
  1468. +0x00090820L,0x00090830L,0x20090820L,0x20090830L,
  1469. +},{
  1470. +/* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
  1471. +0x00000000L,0x02000000L,0x00002000L,0x02002000L,
  1472. +0x00200000L,0x02200000L,0x00202000L,0x02202000L,
  1473. +0x00000004L,0x02000004L,0x00002004L,0x02002004L,
  1474. +0x00200004L,0x02200004L,0x00202004L,0x02202004L,
  1475. +0x00000400L,0x02000400L,0x00002400L,0x02002400L,
  1476. +0x00200400L,0x02200400L,0x00202400L,0x02202400L,
  1477. +0x00000404L,0x02000404L,0x00002404L,0x02002404L,
  1478. +0x00200404L,0x02200404L,0x00202404L,0x02202404L,
  1479. +0x10000000L,0x12000000L,0x10002000L,0x12002000L,
  1480. +0x10200000L,0x12200000L,0x10202000L,0x12202000L,
  1481. +0x10000004L,0x12000004L,0x10002004L,0x12002004L,
  1482. +0x10200004L,0x12200004L,0x10202004L,0x12202004L,
  1483. +0x10000400L,0x12000400L,0x10002400L,0x12002400L,
  1484. +0x10200400L,0x12200400L,0x10202400L,0x12202400L,
  1485. +0x10000404L,0x12000404L,0x10002404L,0x12002404L,
  1486. +0x10200404L,0x12200404L,0x10202404L,0x12202404L,
  1487. +},{
  1488. +/* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
  1489. +0x00000000L,0x00000001L,0x00040000L,0x00040001L,
  1490. +0x01000000L,0x01000001L,0x01040000L,0x01040001L,
  1491. +0x00000002L,0x00000003L,0x00040002L,0x00040003L,
  1492. +0x01000002L,0x01000003L,0x01040002L,0x01040003L,
  1493. +0x00000200L,0x00000201L,0x00040200L,0x00040201L,
  1494. +0x01000200L,0x01000201L,0x01040200L,0x01040201L,
  1495. +0x00000202L,0x00000203L,0x00040202L,0x00040203L,
  1496. +0x01000202L,0x01000203L,0x01040202L,0x01040203L,
  1497. +0x08000000L,0x08000001L,0x08040000L,0x08040001L,
  1498. +0x09000000L,0x09000001L,0x09040000L,0x09040001L,
  1499. +0x08000002L,0x08000003L,0x08040002L,0x08040003L,
  1500. +0x09000002L,0x09000003L,0x09040002L,0x09040003L,
  1501. +0x08000200L,0x08000201L,0x08040200L,0x08040201L,
  1502. +0x09000200L,0x09000201L,0x09040200L,0x09040201L,
  1503. +0x08000202L,0x08000203L,0x08040202L,0x08040203L,
  1504. +0x09000202L,0x09000203L,0x09040202L,0x09040203L,
  1505. +},{
  1506. +/* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
  1507. +0x00000000L,0x00100000L,0x00000100L,0x00100100L,
  1508. +0x00000008L,0x00100008L,0x00000108L,0x00100108L,
  1509. +0x00001000L,0x00101000L,0x00001100L,0x00101100L,
  1510. +0x00001008L,0x00101008L,0x00001108L,0x00101108L,
  1511. +0x04000000L,0x04100000L,0x04000100L,0x04100100L,
  1512. +0x04000008L,0x04100008L,0x04000108L,0x04100108L,
  1513. +0x04001000L,0x04101000L,0x04001100L,0x04101100L,
  1514. +0x04001008L,0x04101008L,0x04001108L,0x04101108L,
  1515. +0x00020000L,0x00120000L,0x00020100L,0x00120100L,
  1516. +0x00020008L,0x00120008L,0x00020108L,0x00120108L,
  1517. +0x00021000L,0x00121000L,0x00021100L,0x00121100L,
  1518. +0x00021008L,0x00121008L,0x00021108L,0x00121108L,
  1519. +0x04020000L,0x04120000L,0x04020100L,0x04120100L,
  1520. +0x04020008L,0x04120008L,0x04020108L,0x04120108L,
  1521. +0x04021000L,0x04121000L,0x04021100L,0x04121100L,
  1522. +0x04021008L,0x04121008L,0x04021108L,0x04121108L,
  1523. +},{
  1524. +/* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  1525. +0x00000000L,0x10000000L,0x00010000L,0x10010000L,
  1526. +0x00000004L,0x10000004L,0x00010004L,0x10010004L,
  1527. +0x20000000L,0x30000000L,0x20010000L,0x30010000L,
  1528. +0x20000004L,0x30000004L,0x20010004L,0x30010004L,
  1529. +0x00100000L,0x10100000L,0x00110000L,0x10110000L,
  1530. +0x00100004L,0x10100004L,0x00110004L,0x10110004L,
  1531. +0x20100000L,0x30100000L,0x20110000L,0x30110000L,
  1532. +0x20100004L,0x30100004L,0x20110004L,0x30110004L,
  1533. +0x00001000L,0x10001000L,0x00011000L,0x10011000L,
  1534. +0x00001004L,0x10001004L,0x00011004L,0x10011004L,
  1535. +0x20001000L,0x30001000L,0x20011000L,0x30011000L,
  1536. +0x20001004L,0x30001004L,0x20011004L,0x30011004L,
  1537. +0x00101000L,0x10101000L,0x00111000L,0x10111000L,
  1538. +0x00101004L,0x10101004L,0x00111004L,0x10111004L,
  1539. +0x20101000L,0x30101000L,0x20111000L,0x30111000L,
  1540. +0x20101004L,0x30101004L,0x20111004L,0x30111004L,
  1541. +},{
  1542. +/* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
  1543. +0x00000000L,0x08000000L,0x00000008L,0x08000008L,
  1544. +0x00000400L,0x08000400L,0x00000408L,0x08000408L,
  1545. +0x00020000L,0x08020000L,0x00020008L,0x08020008L,
  1546. +0x00020400L,0x08020400L,0x00020408L,0x08020408L,
  1547. +0x00000001L,0x08000001L,0x00000009L,0x08000009L,
  1548. +0x00000401L,0x08000401L,0x00000409L,0x08000409L,
  1549. +0x00020001L,0x08020001L,0x00020009L,0x08020009L,
  1550. +0x00020401L,0x08020401L,0x00020409L,0x08020409L,
  1551. +0x02000000L,0x0A000000L,0x02000008L,0x0A000008L,
  1552. +0x02000400L,0x0A000400L,0x02000408L,0x0A000408L,
  1553. +0x02020000L,0x0A020000L,0x02020008L,0x0A020008L,
  1554. +0x02020400L,0x0A020400L,0x02020408L,0x0A020408L,
  1555. +0x02000001L,0x0A000001L,0x02000009L,0x0A000009L,
  1556. +0x02000401L,0x0A000401L,0x02000409L,0x0A000409L,
  1557. +0x02020001L,0x0A020001L,0x02020009L,0x0A020009L,
  1558. +0x02020401L,0x0A020401L,0x02020409L,0x0A020409L,
  1559. +},{
  1560. +/* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
  1561. +0x00000000L,0x00000100L,0x00080000L,0x00080100L,
  1562. +0x01000000L,0x01000100L,0x01080000L,0x01080100L,
  1563. +0x00000010L,0x00000110L,0x00080010L,0x00080110L,
  1564. +0x01000010L,0x01000110L,0x01080010L,0x01080110L,
  1565. +0x00200000L,0x00200100L,0x00280000L,0x00280100L,
  1566. +0x01200000L,0x01200100L,0x01280000L,0x01280100L,
  1567. +0x00200010L,0x00200110L,0x00280010L,0x00280110L,
  1568. +0x01200010L,0x01200110L,0x01280010L,0x01280110L,
  1569. +0x00000200L,0x00000300L,0x00080200L,0x00080300L,
  1570. +0x01000200L,0x01000300L,0x01080200L,0x01080300L,
  1571. +0x00000210L,0x00000310L,0x00080210L,0x00080310L,
  1572. +0x01000210L,0x01000310L,0x01080210L,0x01080310L,
  1573. +0x00200200L,0x00200300L,0x00280200L,0x00280300L,
  1574. +0x01200200L,0x01200300L,0x01280200L,0x01280300L,
  1575. +0x00200210L,0x00200310L,0x00280210L,0x00280310L,
  1576. +0x01200210L,0x01200310L,0x01280210L,0x01280310L,
  1577. +},{
  1578. +/* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
  1579. +0x00000000L,0x04000000L,0x00040000L,0x04040000L,
  1580. +0x00000002L,0x04000002L,0x00040002L,0x04040002L,
  1581. +0x00002000L,0x04002000L,0x00042000L,0x04042000L,
  1582. +0x00002002L,0x04002002L,0x00042002L,0x04042002L,
  1583. +0x00000020L,0x04000020L,0x00040020L,0x04040020L,
  1584. +0x00000022L,0x04000022L,0x00040022L,0x04040022L,
  1585. +0x00002020L,0x04002020L,0x00042020L,0x04042020L,
  1586. +0x00002022L,0x04002022L,0x00042022L,0x04042022L,
  1587. +0x00000800L,0x04000800L,0x00040800L,0x04040800L,
  1588. +0x00000802L,0x04000802L,0x00040802L,0x04040802L,
  1589. +0x00002800L,0x04002800L,0x00042800L,0x04042800L,
  1590. +0x00002802L,0x04002802L,0x00042802L,0x04042802L,
  1591. +0x00000820L,0x04000820L,0x00040820L,0x04040820L,
  1592. +0x00000822L,0x04000822L,0x00040822L,0x04040822L,
  1593. +0x00002820L,0x04002820L,0x00042820L,0x04042820L,
  1594. +0x00002822L,0x04002822L,0x00042822L,0x04042822L,
  1595. +}};
  1596. --- /dev/null Tue Mar 11 13:02:56 2003
  1597. +++ linux/include/des/spr.h Mon Feb 9 13:51:03 2004
  1598. @@ -0,0 +1,204 @@
  1599. +/* crypto/des/spr.h */
  1600. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1601. + * All rights reserved.
  1602. + *
  1603. + * This package is an SSL implementation written
  1604. + * by Eric Young (eay@cryptsoft.com).
  1605. + * The implementation was written so as to conform with Netscapes SSL.
  1606. + *
  1607. + * This library is free for commercial and non-commercial use as long as
  1608. + * the following conditions are aheared to. The following conditions
  1609. + * apply to all code found in this distribution, be it the RC4, RSA,
  1610. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1611. + * included with this distribution is covered by the same copyright terms
  1612. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1613. + *
  1614. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1615. + * the code are not to be removed.
  1616. + * If this package is used in a product, Eric Young should be given attribution
  1617. + * as the author of the parts of the library used.
  1618. + * This can be in the form of a textual message at program startup or
  1619. + * in documentation (online or textual) provided with the package.
  1620. + *
  1621. + * Redistribution and use in source and binary forms, with or without
  1622. + * modification, are permitted provided that the following conditions
  1623. + * are met:
  1624. + * 1. Redistributions of source code must retain the copyright
  1625. + * notice, this list of conditions and the following disclaimer.
  1626. + * 2. Redistributions in binary form must reproduce the above copyright
  1627. + * notice, this list of conditions and the following disclaimer in the
  1628. + * documentation and/or other materials provided with the distribution.
  1629. + * 3. All advertising materials mentioning features or use of this software
  1630. + * must display the following acknowledgement:
  1631. + * "This product includes cryptographic software written by
  1632. + * Eric Young (eay@cryptsoft.com)"
  1633. + * The word 'cryptographic' can be left out if the rouines from the library
  1634. + * being used are not cryptographic related :-).
  1635. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1636. + * the apps directory (application code) you must include an acknowledgement:
  1637. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1638. + *
  1639. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1640. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1641. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1642. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1643. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1644. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1645. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1646. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1647. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1648. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1649. + * SUCH DAMAGE.
  1650. + *
  1651. + * The licence and distribution terms for any publically available version or
  1652. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1653. + * copied and put under another distribution licence
  1654. + * [including the GNU Public Licence.]
  1655. + */
  1656. +
  1657. +const DES_LONG des_SPtrans[8][64]={
  1658. +{
  1659. +/* nibble 0 */
  1660. +0x02080800L, 0x00080000L, 0x02000002L, 0x02080802L,
  1661. +0x02000000L, 0x00080802L, 0x00080002L, 0x02000002L,
  1662. +0x00080802L, 0x02080800L, 0x02080000L, 0x00000802L,
  1663. +0x02000802L, 0x02000000L, 0x00000000L, 0x00080002L,
  1664. +0x00080000L, 0x00000002L, 0x02000800L, 0x00080800L,
  1665. +0x02080802L, 0x02080000L, 0x00000802L, 0x02000800L,
  1666. +0x00000002L, 0x00000800L, 0x00080800L, 0x02080002L,
  1667. +0x00000800L, 0x02000802L, 0x02080002L, 0x00000000L,
  1668. +0x00000000L, 0x02080802L, 0x02000800L, 0x00080002L,
  1669. +0x02080800L, 0x00080000L, 0x00000802L, 0x02000800L,
  1670. +0x02080002L, 0x00000800L, 0x00080800L, 0x02000002L,
  1671. +0x00080802L, 0x00000002L, 0x02000002L, 0x02080000L,
  1672. +0x02080802L, 0x00080800L, 0x02080000L, 0x02000802L,
  1673. +0x02000000L, 0x00000802L, 0x00080002L, 0x00000000L,
  1674. +0x00080000L, 0x02000000L, 0x02000802L, 0x02080800L,
  1675. +0x00000002L, 0x02080002L, 0x00000800L, 0x00080802L,
  1676. +},{
  1677. +/* nibble 1 */
  1678. +0x40108010L, 0x00000000L, 0x00108000L, 0x40100000L,
  1679. +0x40000010L, 0x00008010L, 0x40008000L, 0x00108000L,
  1680. +0x00008000L, 0x40100010L, 0x00000010L, 0x40008000L,
  1681. +0x00100010L, 0x40108000L, 0x40100000L, 0x00000010L,
  1682. +0x00100000L, 0x40008010L, 0x40100010L, 0x00008000L,
  1683. +0x00108010L, 0x40000000L, 0x00000000L, 0x00100010L,
  1684. +0x40008010L, 0x00108010L, 0x40108000L, 0x40000010L,
  1685. +0x40000000L, 0x00100000L, 0x00008010L, 0x40108010L,
  1686. +0x00100010L, 0x40108000L, 0x40008000L, 0x00108010L,
  1687. +0x40108010L, 0x00100010L, 0x40000010L, 0x00000000L,
  1688. +0x40000000L, 0x00008010L, 0x00100000L, 0x40100010L,
  1689. +0x00008000L, 0x40000000L, 0x00108010L, 0x40008010L,
  1690. +0x40108000L, 0x00008000L, 0x00000000L, 0x40000010L,
  1691. +0x00000010L, 0x40108010L, 0x00108000L, 0x40100000L,
  1692. +0x40100010L, 0x00100000L, 0x00008010L, 0x40008000L,
  1693. +0x40008010L, 0x00000010L, 0x40100000L, 0x00108000L,
  1694. +},{
  1695. +/* nibble 2 */
  1696. +0x04000001L, 0x04040100L, 0x00000100L, 0x04000101L,
  1697. +0x00040001L, 0x04000000L, 0x04000101L, 0x00040100L,
  1698. +0x04000100L, 0x00040000L, 0x04040000L, 0x00000001L,
  1699. +0x04040101L, 0x00000101L, 0x00000001L, 0x04040001L,
  1700. +0x00000000L, 0x00040001L, 0x04040100L, 0x00000100L,
  1701. +0x00000101L, 0x04040101L, 0x00040000L, 0x04000001L,
  1702. +0x04040001L, 0x04000100L, 0x00040101L, 0x04040000L,
  1703. +0x00040100L, 0x00000000L, 0x04000000L, 0x00040101L,
  1704. +0x04040100L, 0x00000100L, 0x00000001L, 0x00040000L,
  1705. +0x00000101L, 0x00040001L, 0x04040000L, 0x04000101L,
  1706. +0x00000000L, 0x04040100L, 0x00040100L, 0x04040001L,
  1707. +0x00040001L, 0x04000000L, 0x04040101L, 0x00000001L,
  1708. +0x00040101L, 0x04000001L, 0x04000000L, 0x04040101L,
  1709. +0x00040000L, 0x04000100L, 0x04000101L, 0x00040100L,
  1710. +0x04000100L, 0x00000000L, 0x04040001L, 0x00000101L,
  1711. +0x04000001L, 0x00040101L, 0x00000100L, 0x04040000L,
  1712. +},{
  1713. +/* nibble 3 */
  1714. +0x00401008L, 0x10001000L, 0x00000008L, 0x10401008L,
  1715. +0x00000000L, 0x10400000L, 0x10001008L, 0x00400008L,
  1716. +0x10401000L, 0x10000008L, 0x10000000L, 0x00001008L,
  1717. +0x10000008L, 0x00401008L, 0x00400000L, 0x10000000L,
  1718. +0x10400008L, 0x00401000L, 0x00001000L, 0x00000008L,
  1719. +0x00401000L, 0x10001008L, 0x10400000L, 0x00001000L,
  1720. +0x00001008L, 0x00000000L, 0x00400008L, 0x10401000L,
  1721. +0x10001000L, 0x10400008L, 0x10401008L, 0x00400000L,
  1722. +0x10400008L, 0x00001008L, 0x00400000L, 0x10000008L,
  1723. +0x00401000L, 0x10001000L, 0x00000008L, 0x10400000L,
  1724. +0x10001008L, 0x00000000L, 0x00001000L, 0x00400008L,
  1725. +0x00000000L, 0x10400008L, 0x10401000L, 0x00001000L,
  1726. +0x10000000L, 0x10401008L, 0x00401008L, 0x00400000L,
  1727. +0x10401008L, 0x00000008L, 0x10001000L, 0x00401008L,
  1728. +0x00400008L, 0x00401000L, 0x10400000L, 0x10001008L,
  1729. +0x00001008L, 0x10000000L, 0x10000008L, 0x10401000L,
  1730. +},{
  1731. +/* nibble 4 */
  1732. +0x08000000L, 0x00010000L, 0x00000400L, 0x08010420L,
  1733. +0x08010020L, 0x08000400L, 0x00010420L, 0x08010000L,
  1734. +0x00010000L, 0x00000020L, 0x08000020L, 0x00010400L,
  1735. +0x08000420L, 0x08010020L, 0x08010400L, 0x00000000L,
  1736. +0x00010400L, 0x08000000L, 0x00010020L, 0x00000420L,
  1737. +0x08000400L, 0x00010420L, 0x00000000L, 0x08000020L,
  1738. +0x00000020L, 0x08000420L, 0x08010420L, 0x00010020L,
  1739. +0x08010000L, 0x00000400L, 0x00000420L, 0x08010400L,
  1740. +0x08010400L, 0x08000420L, 0x00010020L, 0x08010000L,
  1741. +0x00010000L, 0x00000020L, 0x08000020L, 0x08000400L,
  1742. +0x08000000L, 0x00010400L, 0x08010420L, 0x00000000L,
  1743. +0x00010420L, 0x08000000L, 0x00000400L, 0x00010020L,
  1744. +0x08000420L, 0x00000400L, 0x00000000L, 0x08010420L,
  1745. +0x08010020L, 0x08010400L, 0x00000420L, 0x00010000L,
  1746. +0x00010400L, 0x08010020L, 0x08000400L, 0x00000420L,
  1747. +0x00000020L, 0x00010420L, 0x08010000L, 0x08000020L,
  1748. +},{
  1749. +/* nibble 5 */
  1750. +0x80000040L, 0x00200040L, 0x00000000L, 0x80202000L,
  1751. +0x00200040L, 0x00002000L, 0x80002040L, 0x00200000L,
  1752. +0x00002040L, 0x80202040L, 0x00202000L, 0x80000000L,
  1753. +0x80002000L, 0x80000040L, 0x80200000L, 0x00202040L,
  1754. +0x00200000L, 0x80002040L, 0x80200040L, 0x00000000L,
  1755. +0x00002000L, 0x00000040L, 0x80202000L, 0x80200040L,
  1756. +0x80202040L, 0x80200000L, 0x80000000L, 0x00002040L,
  1757. +0x00000040L, 0x00202000L, 0x00202040L, 0x80002000L,
  1758. +0x00002040L, 0x80000000L, 0x80002000L, 0x00202040L,
  1759. +0x80202000L, 0x00200040L, 0x00000000L, 0x80002000L,
  1760. +0x80000000L, 0x00002000L, 0x80200040L, 0x00200000L,
  1761. +0x00200040L, 0x80202040L, 0x00202000L, 0x00000040L,
  1762. +0x80202040L, 0x00202000L, 0x00200000L, 0x80002040L,
  1763. +0x80000040L, 0x80200000L, 0x00202040L, 0x00000000L,
  1764. +0x00002000L, 0x80000040L, 0x80002040L, 0x80202000L,
  1765. +0x80200000L, 0x00002040L, 0x00000040L, 0x80200040L,
  1766. +},{
  1767. +/* nibble 6 */
  1768. +0x00004000L, 0x00000200L, 0x01000200L, 0x01000004L,
  1769. +0x01004204L, 0x00004004L, 0x00004200L, 0x00000000L,
  1770. +0x01000000L, 0x01000204L, 0x00000204L, 0x01004000L,
  1771. +0x00000004L, 0x01004200L, 0x01004000L, 0x00000204L,
  1772. +0x01000204L, 0x00004000L, 0x00004004L, 0x01004204L,
  1773. +0x00000000L, 0x01000200L, 0x01000004L, 0x00004200L,
  1774. +0x01004004L, 0x00004204L, 0x01004200L, 0x00000004L,
  1775. +0x00004204L, 0x01004004L, 0x00000200L, 0x01000000L,
  1776. +0x00004204L, 0x01004000L, 0x01004004L, 0x00000204L,
  1777. +0x00004000L, 0x00000200L, 0x01000000L, 0x01004004L,
  1778. +0x01000204L, 0x00004204L, 0x00004200L, 0x00000000L,
  1779. +0x00000200L, 0x01000004L, 0x00000004L, 0x01000200L,
  1780. +0x00000000L, 0x01000204L, 0x01000200L, 0x00004200L,
  1781. +0x00000204L, 0x00004000L, 0x01004204L, 0x01000000L,
  1782. +0x01004200L, 0x00000004L, 0x00004004L, 0x01004204L,
  1783. +0x01000004L, 0x01004200L, 0x01004000L, 0x00004004L,
  1784. +},{
  1785. +/* nibble 7 */
  1786. +0x20800080L, 0x20820000L, 0x00020080L, 0x00000000L,
  1787. +0x20020000L, 0x00800080L, 0x20800000L, 0x20820080L,
  1788. +0x00000080L, 0x20000000L, 0x00820000L, 0x00020080L,
  1789. +0x00820080L, 0x20020080L, 0x20000080L, 0x20800000L,
  1790. +0x00020000L, 0x00820080L, 0x00800080L, 0x20020000L,
  1791. +0x20820080L, 0x20000080L, 0x00000000L, 0x00820000L,
  1792. +0x20000000L, 0x00800000L, 0x20020080L, 0x20800080L,
  1793. +0x00800000L, 0x00020000L, 0x20820000L, 0x00000080L,
  1794. +0x00800000L, 0x00020000L, 0x20000080L, 0x20820080L,
  1795. +0x00020080L, 0x20000000L, 0x00000000L, 0x00820000L,
  1796. +0x20800080L, 0x20020080L, 0x20020000L, 0x00800080L,
  1797. +0x20820000L, 0x00000080L, 0x00800080L, 0x20020000L,
  1798. +0x20820080L, 0x00800000L, 0x20800000L, 0x20000080L,
  1799. +0x00820000L, 0x00020080L, 0x20020080L, 0x20800000L,
  1800. +0x00000080L, 0x20820000L, 0x00820080L, 0x00000000L,
  1801. +0x20000000L, 0x20800080L, 0x00020000L, 0x00820080L,
  1802. +}};
  1803. --- /dev/null Tue Mar 11 13:02:56 2003
  1804. +++ linux/include/mast.h Mon Feb 9 13:51:03 2004
  1805. @@ -0,0 +1,33 @@
  1806. +struct mast_callbacks {
  1807. + int (*packet_encap)(struct device *mast, void *context,
  1808. + struct sk_buff *skb, int flowref);
  1809. + int (*link_inquire)(struct device *mast, void *context);
  1810. +};
  1811. +
  1812. +
  1813. +struct device *mast_init (int family,
  1814. + struct mast_callbacks *callbacks,
  1815. + unsigned int flags,
  1816. + unsigned int desired_unit,
  1817. + unsigned int max_flowref,
  1818. + void *context);
  1819. +
  1820. +int mast_destroy(struct device *mast);
  1821. +
  1822. +int mast_recv(struct device *mast, struct sk_buff *skb, int flowref);
  1823. +
  1824. +/* free this skb as being useless, increment failure count. */
  1825. +int mast_toast(struct device *mast, struct sk_buff *skb, int flowref);
  1826. +
  1827. +int mast_linkstat (struct device *mast, int flowref,
  1828. + int status);
  1829. +
  1830. +int mast_setreference (struct device *mast,
  1831. + int defaultSA);
  1832. +
  1833. +int mast_setneighbor (struct device *mast,
  1834. + struct sockaddr *source,
  1835. + struct sockaddr *destination,
  1836. + int flowref);
  1837. +
  1838. +
  1839. --- /dev/null Tue Mar 11 13:02:56 2003
  1840. +++ linux/include/openswan.h Mon Feb 9 13:51:03 2004
  1841. @@ -0,0 +1,518 @@
  1842. +#ifndef _OPENSWAN_H
  1843. +/*
  1844. + * header file for FreeS/WAN library functions
  1845. + * Copyright (C) 1998, 1999, 2000 Henry Spencer.
  1846. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs
  1847. + *
  1848. + * This library is free software; you can redistribute it and/or modify it
  1849. + * under the terms of the GNU Library General Public License as published by
  1850. + * the Free Software Foundation; either version 2 of the License, or (at your
  1851. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  1852. + *
  1853. + * This library is distributed in the hope that it will be useful, but
  1854. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  1855. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  1856. + * License for more details.
  1857. + *
  1858. + * RCSID $Id: openswan.h,v 1.93 2005/04/14 20:21:51 mcr Exp $
  1859. + */
  1860. +#define _OPENSWAN_H /* seen it, no need to see it again */
  1861. +
  1862. +/* you'd think this should be builtin to compiler... */
  1863. +#ifndef TRUE
  1864. +#define TRUE 1
  1865. +#endif
  1866. +
  1867. +#ifndef FALSE
  1868. +#define FALSE 0
  1869. +#endif
  1870. +
  1871. +
  1872. +
  1873. +/*
  1874. + * We've just got to have some datatypes defined... And annoyingly, just
  1875. + * where we get them depends on whether we're in userland or not.
  1876. + */
  1877. +/* things that need to come from one place or the other, depending */
  1878. +#ifdef __KERNEL__
  1879. +#include <linux/types.h>
  1880. +#include <linux/socket.h>
  1881. +#include <linux/in.h>
  1882. +#include <linux/string.h>
  1883. +#include <linux/ctype.h>
  1884. +#define user_assert(foo) /*nothing*/
  1885. +#else
  1886. +#include <sys/types.h>
  1887. +#include <netinet/in.h>
  1888. +#include <string.h>
  1889. +#include <ctype.h>
  1890. +#include <assert.h>
  1891. +#define user_assert(foo) assert(foo)
  1892. +#include <stdio.h>
  1893. +
  1894. +# define uint8_t u_int8_t
  1895. +# define uint16_t u_int16_t
  1896. +# define uint32_t u_int32_t
  1897. +# define uint64_t u_int64_t
  1898. +
  1899. +
  1900. +# define DEBUG_NO_STATIC static
  1901. +
  1902. +#endif
  1903. +
  1904. +#include <openswan/ipsec_param.h>
  1905. +
  1906. +
  1907. +/*
  1908. + * Grab the kernel version to see if we have NET_21, and therefore
  1909. + * IPv6. Some of this is repeated from ipsec_kversions.h. Of course,
  1910. + * we aren't really testing if the kernel has IPv6, but rather if the
  1911. + * the include files do.
  1912. + */
  1913. +#include <linux/version.h>
  1914. +#ifndef KERNEL_VERSION
  1915. +#define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  1916. +#endif
  1917. +
  1918. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
  1919. +#define NET_21
  1920. +#endif
  1921. +
  1922. +#ifndef IPPROTO_COMP
  1923. +# define IPPROTO_COMP 108
  1924. +#endif /* !IPPROTO_COMP */
  1925. +
  1926. +#ifndef IPPROTO_INT
  1927. +# define IPPROTO_INT 61
  1928. +#endif /* !IPPROTO_INT */
  1929. +
  1930. +#ifdef CONFIG_KLIPS_DEBUG
  1931. +#ifndef DEBUG_NO_STATIC
  1932. +# define DEBUG_NO_STATIC
  1933. +#endif
  1934. +#else /* CONFIG_KLIPS_DEBUG */
  1935. +#ifndef DEBUG_NO_STATIC
  1936. +# define DEBUG_NO_STATIC static
  1937. +#endif
  1938. +#endif /* CONFIG_KLIPS_DEBUG */
  1939. +
  1940. +#if !defined(ESPINUDP_WITH_NON_IKE)
  1941. +#define ESPINUDP_WITH_NON_IKE 1 /* draft-ietf-ipsec-nat-t-ike-00/01 */
  1942. +#define ESPINUDP_WITH_NON_ESP 2 /* draft-ietf-ipsec-nat-t-ike-02 */
  1943. +#endif
  1944. +
  1945. +/*
  1946. + * Basic data types for the address-handling functions.
  1947. + * ip_address and ip_subnet are supposed to be opaque types; do not
  1948. + * use their definitions directly, they are subject to change!
  1949. + */
  1950. +
  1951. +/* first, some quick fakes in case we're on an old system with no IPv6 */
  1952. +#ifndef s6_addr16
  1953. +struct in6_addr {
  1954. + union
  1955. + {
  1956. + __u8 u6_addr8[16];
  1957. + __u16 u6_addr16[8];
  1958. + __u32 u6_addr32[4];
  1959. + } in6_u;
  1960. +#define s6_addr in6_u.u6_addr8
  1961. +#define s6_addr16 in6_u.u6_addr16
  1962. +#define s6_addr32 in6_u.u6_addr32
  1963. +};
  1964. +struct sockaddr_in6 {
  1965. + unsigned short int sin6_family; /* AF_INET6 */
  1966. + __u16 sin6_port; /* Transport layer port # */
  1967. + __u32 sin6_flowinfo; /* IPv6 flow information */
  1968. + struct in6_addr sin6_addr; /* IPv6 address */
  1969. + __u32 sin6_scope_id; /* scope id (new in RFC2553) */
  1970. +};
  1971. +#endif /* !s6_addr16 */
  1972. +
  1973. +/* then the main types */
  1974. +typedef struct {
  1975. + union {
  1976. + struct sockaddr_in v4;
  1977. + struct sockaddr_in6 v6;
  1978. + } u;
  1979. +} ip_address;
  1980. +typedef struct {
  1981. + ip_address addr;
  1982. + int maskbits;
  1983. +} ip_subnet;
  1984. +
  1985. +/* and the SA ID stuff */
  1986. +#ifdef __KERNEL__
  1987. +typedef __u32 ipsec_spi_t;
  1988. +#else
  1989. +typedef u_int32_t ipsec_spi_t;
  1990. +#endif
  1991. +typedef struct { /* to identify an SA, we need: */
  1992. + ip_address dst; /* A. destination host */
  1993. + ipsec_spi_t spi; /* B. 32-bit SPI, assigned by dest. host */
  1994. +# define SPI_PASS 256 /* magic values... */
  1995. +# define SPI_DROP 257 /* ...for use... */
  1996. +# define SPI_REJECT 258 /* ...with SA_INT */
  1997. +# define SPI_HOLD 259
  1998. +# define SPI_TRAP 260
  1999. +# define SPI_TRAPSUBNET 261
  2000. + int proto; /* C. protocol */
  2001. +# define SA_ESP 50 /* IPPROTO_ESP */
  2002. +# define SA_AH 51 /* IPPROTO_AH */
  2003. +# define SA_IPIP 4 /* IPPROTO_IPIP */
  2004. +# define SA_COMP 108 /* IPPROTO_COMP */
  2005. +# define SA_INT 61 /* IANA reserved for internal use */
  2006. +} ip_said;
  2007. +
  2008. +/* misc */
  2009. +typedef const char *err_t; /* error message, or NULL for success */
  2010. +struct prng { /* pseudo-random-number-generator guts */
  2011. + unsigned char sbox[256];
  2012. + int i, j;
  2013. + unsigned long count;
  2014. +};
  2015. +
  2016. +
  2017. +/*
  2018. + * definitions for user space, taken from freeswan/ipsec_sa.h
  2019. + */
  2020. +typedef uint32_t IPsecSAref_t;
  2021. +
  2022. +#define IPSEC_SA_REF_FIELD_WIDTH (8 * sizeof(IPsecSAref_t))
  2023. +
  2024. +#define IPsecSAref2NFmark(x) ((x) << (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  2025. +#define NFmark2IPsecSAref(x) ((x) >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  2026. +
  2027. +#define IPSEC_SAREF_NULL (~((IPsecSAref_t)0))
  2028. +
  2029. +/* GCC magic for use in function definitions! */
  2030. +#ifdef GCC_LINT
  2031. +# define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
  2032. +# define NEVER_RETURNS __attribute__ ((noreturn))
  2033. +# define UNUSED __attribute__ ((unused))
  2034. +# define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
  2035. +#else
  2036. +# define PRINTF_LIKE(n) /* ignore */
  2037. +# define NEVER_RETURNS /* ignore */
  2038. +# define UNUSED /* ignore */
  2039. +# define BLANK_FORMAT ""
  2040. +#endif
  2041. +
  2042. +
  2043. +
  2044. +
  2045. +
  2046. +/*
  2047. + * new IPv6-compatible functions
  2048. + */
  2049. +
  2050. +/* text conversions */
  2051. +err_t ttoul(const char *src, size_t srclen, int format, unsigned long *dst);
  2052. +size_t ultot(unsigned long src, int format, char *buf, size_t buflen);
  2053. +#define ULTOT_BUF (22+1) /* holds 64 bits in octal */
  2054. +err_t ttoaddr(const char *src, size_t srclen, int af, ip_address *dst);
  2055. +err_t tnatoaddr(const char *src, size_t srclen, int af, ip_address *dst);
  2056. +size_t addrtot(const ip_address *src, int format, char *buf, size_t buflen);
  2057. +/* RFC 1886 old IPv6 reverse-lookup format is the bulkiest */
  2058. +#define ADDRTOT_BUF (32*2 + 3 + 1 + 3 + 1 + 1)
  2059. +err_t ttosubnet(const char *src, size_t srclen, int af, ip_subnet *dst);
  2060. +size_t subnettot(const ip_subnet *src, int format, char *buf, size_t buflen);
  2061. +#define SUBNETTOT_BUF (ADDRTOT_BUF + 1 + 3)
  2062. +size_t subnetporttot(const ip_subnet *src, int format, char *buf, size_t buflen);
  2063. +#define SUBNETPROTOTOT_BUF (SUBNETTOTO_BUF + ULTOT_BUF)
  2064. +err_t ttosa(const char *src, size_t srclen, ip_said *dst);
  2065. +size_t satot(const ip_said *src, int format, char *bufptr, size_t buflen);
  2066. +#define SATOT_BUF (5 + ULTOA_BUF + 1 + ADDRTOT_BUF)
  2067. +err_t ttodata(const char *src, size_t srclen, int base, char *buf,
  2068. + size_t buflen, size_t *needed);
  2069. +err_t ttodatav(const char *src, size_t srclen, int base,
  2070. + char *buf, size_t buflen, size_t *needed,
  2071. + char *errp, size_t errlen, unsigned int flags);
  2072. +#define TTODATAV_BUF 40 /* ttodatav's largest non-literal message */
  2073. +#define TTODATAV_IGNORESPACE (1<<1) /* ignore spaces in base64 encodings*/
  2074. +#define TTODATAV_SPACECOUNTS 0 /* do not ignore spaces in base64 */
  2075. +
  2076. +size_t datatot(const char *src, size_t srclen, int format, char *buf,
  2077. + size_t buflen);
  2078. +size_t keyblobtoid(const unsigned char *src, size_t srclen, char *dst,
  2079. + size_t dstlen);
  2080. +size_t splitkeytoid(const unsigned char *e, size_t elen, const unsigned char *m,
  2081. + size_t mlen, char *dst, size_t dstlen);
  2082. +#define KEYID_BUF 10 /* up to 9 text digits plus NUL */
  2083. +err_t ttoprotoport(char *src, size_t src_len, u_int8_t *proto, u_int16_t *port,
  2084. + int *has_port_wildcard);
  2085. +
  2086. +/* initializations */
  2087. +void initsaid(const ip_address *addr, ipsec_spi_t spi, int proto, ip_said *dst);
  2088. +err_t loopbackaddr(int af, ip_address *dst);
  2089. +err_t unspecaddr(int af, ip_address *dst);
  2090. +err_t anyaddr(int af, ip_address *dst);
  2091. +err_t initaddr(const unsigned char *src, size_t srclen, int af, ip_address *dst);
  2092. +err_t initsubnet(const ip_address *addr, int maskbits, int clash, ip_subnet *dst);
  2093. +err_t addrtosubnet(const ip_address *addr, ip_subnet *dst);
  2094. +
  2095. +/* misc. conversions and related */
  2096. +err_t rangetosubnet(const ip_address *from, const ip_address *to, ip_subnet *dst);
  2097. +int addrtypeof(const ip_address *src);
  2098. +int subnettypeof(const ip_subnet *src);
  2099. +size_t addrlenof(const ip_address *src);
  2100. +size_t addrbytesptr(const ip_address *src, const unsigned char **dst);
  2101. +size_t addrbytesof(const ip_address *src, unsigned char *dst, size_t dstlen);
  2102. +int masktocount(const ip_address *src);
  2103. +void networkof(const ip_subnet *src, ip_address *dst);
  2104. +void maskof(const ip_subnet *src, ip_address *dst);
  2105. +
  2106. +/* tests */
  2107. +int sameaddr(const ip_address *a, const ip_address *b);
  2108. +int addrcmp(const ip_address *a, const ip_address *b);
  2109. +int samesubnet(const ip_subnet *a, const ip_subnet *b);
  2110. +int addrinsubnet(const ip_address *a, const ip_subnet *s);
  2111. +int subnetinsubnet(const ip_subnet *a, const ip_subnet *b);
  2112. +int subnetishost(const ip_subnet *s);
  2113. +int samesaid(const ip_said *a, const ip_said *b);
  2114. +int sameaddrtype(const ip_address *a, const ip_address *b);
  2115. +int samesubnettype(const ip_subnet *a, const ip_subnet *b);
  2116. +int isanyaddr(const ip_address *src);
  2117. +int isunspecaddr(const ip_address *src);
  2118. +int isloopbackaddr(const ip_address *src);
  2119. +
  2120. +/* low-level grot */
  2121. +int portof(const ip_address *src);
  2122. +void setportof(int port, ip_address *dst);
  2123. +struct sockaddr *sockaddrof(ip_address *src);
  2124. +size_t sockaddrlenof(const ip_address *src);
  2125. +
  2126. +/* PRNG */
  2127. +void prng_init(struct prng *prng, const unsigned char *key, size_t keylen);
  2128. +void prng_bytes(struct prng *prng, unsigned char *dst, size_t dstlen);
  2129. +unsigned long prng_count(struct prng *prng);
  2130. +void prng_final(struct prng *prng);
  2131. +
  2132. +/* odds and ends */
  2133. +const char *ipsec_version_code(void);
  2134. +const char *ipsec_version_string(void);
  2135. +const char **ipsec_copyright_notice(void);
  2136. +
  2137. +const char *dns_string_rr(int rr, char *buf, int bufsize);
  2138. +const char *dns_string_datetime(time_t seconds,
  2139. + char *buf,
  2140. + int bufsize);
  2141. +
  2142. +
  2143. +/*
  2144. + * old functions, to be deleted eventually
  2145. + */
  2146. +
  2147. +/* unsigned long */
  2148. +const char * /* NULL for success, else string literal */
  2149. +atoul(
  2150. + const char *src,
  2151. + size_t srclen, /* 0 means strlen(src) */
  2152. + int base, /* 0 means figure it out */
  2153. + unsigned long *resultp
  2154. +);
  2155. +size_t /* space needed for full conversion */
  2156. +ultoa(
  2157. + unsigned long n,
  2158. + int base,
  2159. + char *dst,
  2160. + size_t dstlen
  2161. +);
  2162. +#define ULTOA_BUF 21 /* just large enough for largest result, */
  2163. + /* assuming 64-bit unsigned long! */
  2164. +
  2165. +/* Internet addresses */
  2166. +const char * /* NULL for success, else string literal */
  2167. +atoaddr(
  2168. + const char *src,
  2169. + size_t srclen, /* 0 means strlen(src) */
  2170. + struct in_addr *addr
  2171. +);
  2172. +size_t /* space needed for full conversion */
  2173. +addrtoa(
  2174. + struct in_addr addr,
  2175. + int format, /* character; 0 means default */
  2176. + char *dst,
  2177. + size_t dstlen
  2178. +);
  2179. +#define ADDRTOA_BUF 16 /* just large enough for largest result */
  2180. +
  2181. +/* subnets */
  2182. +const char * /* NULL for success, else string literal */
  2183. +atosubnet(
  2184. + const char *src,
  2185. + size_t srclen, /* 0 means strlen(src) */
  2186. + struct in_addr *addr,
  2187. + struct in_addr *mask
  2188. +);
  2189. +size_t /* space needed for full conversion */
  2190. +subnettoa(
  2191. + struct in_addr addr,
  2192. + struct in_addr mask,
  2193. + int format, /* character; 0 means default */
  2194. + char *dst,
  2195. + size_t dstlen
  2196. +);
  2197. +#define SUBNETTOA_BUF 32 /* large enough for worst case result */
  2198. +
  2199. +/* ranges */
  2200. +const char * /* NULL for success, else string literal */
  2201. +atoasr(
  2202. + const char *src,
  2203. + size_t srclen, /* 0 means strlen(src) */
  2204. + char *type, /* 'a', 's', 'r' */
  2205. + struct in_addr *addrs /* two-element array */
  2206. +);
  2207. +size_t /* space needed for full conversion */
  2208. +rangetoa(
  2209. + struct in_addr *addrs, /* two-element array */
  2210. + int format, /* character; 0 means default */
  2211. + char *dst,
  2212. + size_t dstlen
  2213. +);
  2214. +#define RANGETOA_BUF 34 /* large enough for worst case result */
  2215. +
  2216. +/* data types for SA conversion functions */
  2217. +
  2218. +/* generic data, e.g. keys */
  2219. +const char * /* NULL for success, else string literal */
  2220. +atobytes(
  2221. + const char *src,
  2222. + size_t srclen, /* 0 means strlen(src) */
  2223. + char *dst,
  2224. + size_t dstlen,
  2225. + size_t *lenp /* NULL means don't bother telling me */
  2226. +);
  2227. +size_t /* 0 failure, else true size */
  2228. +bytestoa(
  2229. + const char *src,
  2230. + size_t srclen,
  2231. + int format, /* character; 0 means default */
  2232. + char *dst,
  2233. + size_t dstlen
  2234. +);
  2235. +
  2236. +/* old versions of generic-data functions; deprecated */
  2237. +size_t /* 0 failure, else true size */
  2238. +atodata(
  2239. + const char *src,
  2240. + size_t srclen, /* 0 means strlen(src) */
  2241. + char *dst,
  2242. + size_t dstlen
  2243. +);
  2244. +size_t /* 0 failure, else true size */
  2245. +datatoa(
  2246. + const char *src,
  2247. + size_t srclen,
  2248. + int format, /* character; 0 means default */
  2249. + char *dst,
  2250. + size_t dstlen
  2251. +);
  2252. +
  2253. +/* part extraction and special addresses */
  2254. +struct in_addr
  2255. +subnetof(
  2256. + struct in_addr addr,
  2257. + struct in_addr mask
  2258. +);
  2259. +struct in_addr
  2260. +hostof(
  2261. + struct in_addr addr,
  2262. + struct in_addr mask
  2263. +);
  2264. +struct in_addr
  2265. +broadcastof(
  2266. + struct in_addr addr,
  2267. + struct in_addr mask
  2268. +);
  2269. +
  2270. +/* mask handling */
  2271. +int
  2272. +goodmask(
  2273. + struct in_addr mask
  2274. +);
  2275. +int
  2276. +masktobits(
  2277. + struct in_addr mask
  2278. +);
  2279. +struct in_addr
  2280. +bitstomask(
  2281. + int n
  2282. +);
  2283. +
  2284. +
  2285. +
  2286. +/*
  2287. + * general utilities
  2288. + */
  2289. +
  2290. +#ifndef __KERNEL__
  2291. +/* option pickup from files (userland only because of use of FILE) */
  2292. +const char *optionsfrom(const char *filename, int *argcp, char ***argvp,
  2293. + int optind, FILE *errorreport);
  2294. +
  2295. +/* sanitize a string */
  2296. +extern size_t sanitize_string(char *buf, size_t size);
  2297. +
  2298. +#endif
  2299. +
  2300. +
  2301. +/*
  2302. + * ENUM of klips debugging values. Not currently used in klips.
  2303. + * debug flag is actually 32 -bits, but only one bit is ever used,
  2304. + * so we can actually pack it all into a single 32-bit word.
  2305. + */
  2306. +enum klips_debug_flags {
  2307. + KDF_VERBOSE = 0,
  2308. + KDF_XMIT = 1,
  2309. + KDF_NETLINK = 2, /* obsolete */
  2310. + KDF_XFORM = 3,
  2311. + KDF_EROUTE = 4,
  2312. + KDF_SPI = 5,
  2313. + KDF_RADIJ = 6,
  2314. + KDF_ESP = 7,
  2315. + KDF_AH = 8, /* obsolete */
  2316. + KDF_RCV = 9,
  2317. + KDF_TUNNEL = 10,
  2318. + KDF_PFKEY = 11,
  2319. + KDF_COMP = 12
  2320. +};
  2321. +
  2322. +
  2323. +/*
  2324. + * Debugging levels for pfkey_lib_debug
  2325. + */
  2326. +#define PF_KEY_DEBUG_PARSE_NONE 0
  2327. +#define PF_KEY_DEBUG_PARSE_PROBLEM 1
  2328. +#define PF_KEY_DEBUG_PARSE_STRUCT 2
  2329. +#define PF_KEY_DEBUG_PARSE_FLOW 4
  2330. +#define PF_KEY_DEBUG_BUILD 8
  2331. +#define PF_KEY_DEBUG_PARSE_MAX 15
  2332. +
  2333. +extern unsigned int pfkey_lib_debug; /* bits selecting what to report */
  2334. +
  2335. +/*
  2336. + * pluto and lwdnsq need to know the maximum size of the commands to,
  2337. + * and replies from lwdnsq.
  2338. + */
  2339. +
  2340. +#define LWDNSQ_CMDBUF_LEN 1024
  2341. +#define LWDNSQ_RESULT_LEN_MAX 4096
  2342. +
  2343. +
  2344. +/* syntax for passthrough SA */
  2345. +#ifndef PASSTHROUGHNAME
  2346. +#define PASSTHROUGHNAME "%passthrough"
  2347. +#define PASSTHROUGH4NAME "%passthrough4"
  2348. +#define PASSTHROUGH6NAME "%passthrough6"
  2349. +#define PASSTHROUGHIS "tun0@0.0.0.0"
  2350. +#define PASSTHROUGH4IS "tun0@0.0.0.0"
  2351. +#define PASSTHROUGH6IS "tun0@::"
  2352. +#define PASSTHROUGHTYPE "tun"
  2353. +#define PASSTHROUGHSPI 0
  2354. +#define PASSTHROUGHDST 0
  2355. +#endif
  2356. +
  2357. +
  2358. +
  2359. +#endif /* _OPENSWAN_H */
  2360. --- /dev/null Tue Mar 11 13:02:56 2003
  2361. +++ linux/include/openswan/ipcomp.h Mon Feb 9 13:51:03 2004
  2362. @@ -0,0 +1,61 @@
  2363. +/*
  2364. + * IPCOMP zlib interface code.
  2365. + * Copyright (C) 2000 Svenning Soerensen <svenning@post5.tele.dk>
  2366. + * Copyright (C) 2000, 2001 Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  2367. + *
  2368. + * This program is free software; you can redistribute it and/or modify it
  2369. + * under the terms of the GNU General Public License as published by the
  2370. + * Free Software Foundation; either version 2 of the License, or (at your
  2371. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2372. + *
  2373. + * This program is distributed in the hope that it will be useful, but
  2374. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2375. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2376. + * for more details.
  2377. +
  2378. + RCSID $Id: ipcomp.h,v 1.14 2004/07/10 19:08:41 mcr Exp $
  2379. +
  2380. + */
  2381. +
  2382. +/* SSS */
  2383. +
  2384. +#ifndef _IPCOMP_H
  2385. +#define _IPCOMP_H
  2386. +
  2387. +/* Prefix all global deflate symbols with "ipcomp_" to avoid collisions with ppp_deflate & ext2comp */
  2388. +#ifndef IPCOMP_PREFIX
  2389. +#define IPCOMP_PREFIX
  2390. +#endif /* IPCOMP_PREFIX */
  2391. +
  2392. +#ifndef IPPROTO_COMP
  2393. +#define IPPROTO_COMP 108
  2394. +#endif /* IPPROTO_COMP */
  2395. +
  2396. +#ifdef CONFIG_KLIPS_DEBUG
  2397. +extern int sysctl_ipsec_debug_ipcomp;
  2398. +#endif /* CONFIG_KLIPS_DEBUG */
  2399. +
  2400. +struct ipcomphdr { /* IPCOMP header */
  2401. + __u8 ipcomp_nh; /* Next header (protocol) */
  2402. + __u8 ipcomp_flags; /* Reserved, must be 0 */
  2403. + __u16 ipcomp_cpi; /* Compression Parameter Index */
  2404. +};
  2405. +
  2406. +extern struct inet_protocol comp_protocol;
  2407. +extern int sysctl_ipsec_debug_ipcomp;
  2408. +
  2409. +#define IPCOMP_UNCOMPRESSABLE 0x000000001
  2410. +#define IPCOMP_COMPRESSIONERROR 0x000000002
  2411. +#define IPCOMP_PARMERROR 0x000000004
  2412. +#define IPCOMP_DECOMPRESSIONERROR 0x000000008
  2413. +
  2414. +#define IPCOMP_ADAPT_INITIAL_TRIES 8
  2415. +#define IPCOMP_ADAPT_INITIAL_SKIP 4
  2416. +#define IPCOMP_ADAPT_SUBSEQ_TRIES 2
  2417. +#define IPCOMP_ADAPT_SUBSEQ_SKIP 8
  2418. +
  2419. +/* Function prototypes */
  2420. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  2421. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  2422. +
  2423. +#endif /* _IPCOMP_H */
  2424. --- /dev/null Tue Mar 11 13:02:56 2003
  2425. +++ linux/include/openswan/ipsec_ah.h Mon Feb 9 13:51:03 2004
  2426. @@ -0,0 +1,200 @@
  2427. +/*
  2428. + * Authentication Header declarations
  2429. + * Copyright (C) 1996, 1997 John Ioannidis.
  2430. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  2431. + *
  2432. + * This program is free software; you can redistribute it and/or modify it
  2433. + * under the terms of the GNU General Public License as published by the
  2434. + * Free Software Foundation; either version 2 of the License, or (at your
  2435. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2436. + *
  2437. + * This program is distributed in the hope that it will be useful, but
  2438. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2439. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2440. + * for more details.
  2441. + *
  2442. + * RCSID $Id: ipsec_ah.h,v 1.26 2004/09/13 02:22:10 mcr Exp $
  2443. + */
  2444. +
  2445. +#include "ipsec_md5h.h"
  2446. +#include "ipsec_sha1.h"
  2447. +
  2448. +#ifndef IPPROTO_AH
  2449. +#define IPPROTO_AH 51
  2450. +#endif /* IPPROTO_AH */
  2451. +
  2452. +#include "ipsec_auth.h"
  2453. +
  2454. +#ifdef __KERNEL__
  2455. +
  2456. +extern struct inet_protocol ah_protocol;
  2457. +
  2458. +struct options;
  2459. +
  2460. +struct ahhdr /* Generic AH header */
  2461. +{
  2462. + __u8 ah_nh; /* Next header (protocol) */
  2463. + __u8 ah_hl; /* AH length, in 32-bit words */
  2464. + __u16 ah_rv; /* reserved, must be 0 */
  2465. + __u32 ah_spi; /* Security Parameters Index */
  2466. + __u32 ah_rpl; /* Replay prevention */
  2467. + __u8 ah_data[AHHMAC_HASHLEN];/* Authentication hash */
  2468. +};
  2469. +#define AH_BASIC_LEN 8 /* basic AH header is 8 bytes, nh,hl,rv,spi
  2470. + * and the ah_hl, says how many bytes after that
  2471. + * to cover. */
  2472. +
  2473. +extern struct xform_functions ah_xform_funcs[];
  2474. +
  2475. +#ifdef CONFIG_KLIPS_DEBUG
  2476. +extern int debug_ah;
  2477. +#endif /* CONFIG_KLIPS_DEBUG */
  2478. +#endif /* __KERNEL__ */
  2479. +
  2480. +/*
  2481. + * $Log: ipsec_ah.h,v $
  2482. + * Revision 1.26 2004/09/13 02:22:10 mcr
  2483. + * #define inet_protocol if necessary.
  2484. + *
  2485. + * Revision 1.25 2004/09/06 18:35:41 mcr
  2486. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  2487. + * so adjust for that.
  2488. + *
  2489. + * Revision 1.24 2004/07/10 19:08:41 mcr
  2490. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  2491. + *
  2492. + * Revision 1.23 2004/04/05 19:55:04 mcr
  2493. + * Moved from linux/include/freeswan/ipsec_ah.h,v
  2494. + *
  2495. + * Revision 1.22 2004/04/05 19:41:05 mcr
  2496. + * merged alg-branch code.
  2497. + *
  2498. + * Revision 1.21 2003/12/13 19:10:16 mcr
  2499. + * refactored rcv and xmit code - same as FS 2.05.
  2500. + *
  2501. + * Revision 1.22 2003/12/11 20:14:58 mcr
  2502. + * refactored the xmit code, to move all encapsulation
  2503. + * code into protocol functions. Note that all functions
  2504. + * are essentially done by a single function, which is probably
  2505. + * wrong.
  2506. + * the rcv_functions structures are renamed xform_functions.
  2507. + *
  2508. + * Revision 1.21 2003/12/06 21:21:19 mcr
  2509. + * split up receive path into per-transform files, for
  2510. + * easier later removal.
  2511. + *
  2512. + * Revision 1.20.8.1 2003/12/22 15:25:52 jjo
  2513. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  2514. + *
  2515. + * Revision 1.20 2003/02/06 02:21:34 rgb
  2516. + *
  2517. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  2518. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  2519. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  2520. + *
  2521. + * Revision 1.19 2002/09/16 21:19:13 mcr
  2522. + * fixes for west-ah-icmp-01 - length of AH header must be
  2523. + * calculated properly, and next_header field properly copied.
  2524. + *
  2525. + * Revision 1.18 2002/05/14 02:37:02 rgb
  2526. + * Change reference from _TDB to _IPSA.
  2527. + *
  2528. + * Revision 1.17 2002/04/24 07:36:46 mcr
  2529. + * Moved from ./klips/net/ipsec/ipsec_ah.h,v
  2530. + *
  2531. + * Revision 1.16 2002/02/20 01:27:06 rgb
  2532. + * Ditched a pile of structs only used by the old Netlink interface.
  2533. + *
  2534. + * Revision 1.15 2001/12/11 02:35:57 rgb
  2535. + * Change "struct net_device" to "struct device" for 2.2 compatibility.
  2536. + *
  2537. + * Revision 1.14 2001/11/26 09:23:47 rgb
  2538. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  2539. + *
  2540. + * Revision 1.13.2.1 2001/09/25 02:18:24 mcr
  2541. + * replace "struct device" with "struct netdevice"
  2542. + *
  2543. + * Revision 1.13 2001/06/14 19:35:08 rgb
  2544. + * Update copyright date.
  2545. + *
  2546. + * Revision 1.12 2000/09/12 03:21:20 rgb
  2547. + * Cleared out unused htonq.
  2548. + *
  2549. + * Revision 1.11 2000/09/08 19:12:55 rgb
  2550. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  2551. + *
  2552. + * Revision 1.10 2000/01/21 06:13:10 rgb
  2553. + * Tidied up spacing.
  2554. + * Added macros for HMAC padding magic numbers.(kravietz)
  2555. + *
  2556. + * Revision 1.9 1999/12/07 18:16:23 rgb
  2557. + * Fixed comments at end of #endif lines.
  2558. + *
  2559. + * Revision 1.8 1999/04/11 00:28:56 henry
  2560. + * GPL boilerplate
  2561. + *
  2562. + * Revision 1.7 1999/04/06 04:54:25 rgb
  2563. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  2564. + * patch shell fixes.
  2565. + *
  2566. + * Revision 1.6 1999/01/26 02:06:01 rgb
  2567. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  2568. + *
  2569. + * Revision 1.5 1999/01/22 06:17:49 rgb
  2570. + * Updated macro comments.
  2571. + * Added context types to support algorithm switch code.
  2572. + * 64-bit clean-up -- converting 'u long long' to __u64.
  2573. + *
  2574. + * Revision 1.4 1998/07/14 15:54:56 rgb
  2575. + * Add #ifdef __KERNEL__ to protect kernel-only structures.
  2576. + *
  2577. + * Revision 1.3 1998/06/30 18:05:16 rgb
  2578. + * Comment out references to htonq.
  2579. + *
  2580. + * Revision 1.2 1998/06/25 19:33:46 rgb
  2581. + * Add prototype for protocol receive function.
  2582. + * Rearrange for more logical layout.
  2583. + *
  2584. + * Revision 1.1 1998/06/18 21:27:43 henry
  2585. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  2586. + * kernel-build scripts happier in the presence of symlinks
  2587. + *
  2588. + * Revision 1.4 1998/05/18 22:28:43 rgb
  2589. + * Disable key printing facilities from /proc/net/ipsec_*.
  2590. + *
  2591. + * Revision 1.3 1998/04/21 21:29:07 rgb
  2592. + * Rearrange debug switches to change on the fly debug output from user
  2593. + * space. Only kernel changes checked in at this time. radij.c was also
  2594. + * changed to temporarily remove buggy debugging code in rj_delete causing
  2595. + * an OOPS and hence, netlink device open errors.
  2596. + *
  2597. + * Revision 1.2 1998/04/12 22:03:17 rgb
  2598. + * Updated ESP-3DES-HMAC-MD5-96,
  2599. + * ESP-DES-HMAC-MD5-96,
  2600. + * AH-HMAC-MD5-96,
  2601. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  2602. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  2603. + *
  2604. + * Fixed eroute references in /proc/net/ipsec*.
  2605. + *
  2606. + * Started to patch module unloading memory leaks in ipsec_netlink and
  2607. + * radij tree unloading.
  2608. + *
  2609. + * Revision 1.1 1998/04/09 03:05:55 henry
  2610. + * sources moved up from linux/net/ipsec
  2611. + *
  2612. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  2613. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  2614. + *
  2615. + * Revision 0.4 1997/01/15 01:28:15 ji
  2616. + * Added definitions for new AH transforms.
  2617. + *
  2618. + * Revision 0.3 1996/11/20 14:35:48 ji
  2619. + * Minor Cleanup.
  2620. + * Rationalized debugging code.
  2621. + *
  2622. + * Revision 0.2 1996/11/02 00:18:33 ji
  2623. + * First limited release.
  2624. + *
  2625. + *
  2626. + */
  2627. --- /dev/null Tue Mar 11 13:02:56 2003
  2628. +++ linux/include/openswan/ipsec_alg.h Mon Feb 9 13:51:03 2004
  2629. @@ -0,0 +1,248 @@
  2630. +/*
  2631. + * Modular extensions service and registration functions interface
  2632. + *
  2633. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  2634. + *
  2635. + * ipsec_alg.h,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  2636. + *
  2637. + */
  2638. +/*
  2639. + * This program is free software; you can redistribute it and/or modify it
  2640. + * under the terms of the GNU General Public License as published by the
  2641. + * Free Software Foundation; either version 2 of the License, or (at your
  2642. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2643. + *
  2644. + * This program is distributed in the hope that it will be useful, but
  2645. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2646. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2647. + * for more details.
  2648. + *
  2649. + */
  2650. +#ifndef IPSEC_ALG_H
  2651. +#define IPSEC_ALG_H
  2652. +
  2653. +/*
  2654. + * gcc >= 3.2 has removed __FUNCTION__, replaced by C99 __func__
  2655. + * *BUT* its a compiler variable.
  2656. + */
  2657. +#if (__GNUC__ >= 3)
  2658. +#ifndef __FUNCTION__
  2659. +#define __FUNCTION__ __func__
  2660. +#endif
  2661. +#endif
  2662. +
  2663. +/* Version 0.8.1-0 */
  2664. +#define IPSEC_ALG_VERSION 0x00080100
  2665. +
  2666. +#include <linux/types.h>
  2667. +#include <linux/list.h>
  2668. +#include <asm/atomic.h>
  2669. +#include <pfkey.h>
  2670. +
  2671. +/*
  2672. + * The following structs are used via pointers in ipsec_alg object to
  2673. + * avoid ipsec_alg.h coupling with freeswan headers, thus simplifying
  2674. + * module development
  2675. + */
  2676. +struct ipsec_sa;
  2677. +struct esp;
  2678. +
  2679. +/**************************************
  2680. + *
  2681. + * Main registration object
  2682. + *
  2683. + *************************************/
  2684. +#define IPSEC_ALG_VERSION_QUAD(v) \
  2685. + (v>>24),((v>>16)&0xff),((v>>8)&0xff),(v&0xff)
  2686. +/*
  2687. + * Main ipsec_alg objects: "OOPrograming wannabe"
  2688. + * Hierachy (carefully handled with _minimal_ cast'ing):
  2689. + *
  2690. + * ipsec_alg+
  2691. + * +->ipsec_alg_enc (ixt_alg_type=SADB_EXT_SUPPORTED_ENCRYPT)
  2692. + * +->ipsec_alg_auth (ixt_alg_type=SADB_EXT_SUPPORTED_AUTH)
  2693. + */
  2694. +
  2695. +/***************************************************************
  2696. + *
  2697. + * INTERFACE object: struct ipsec_alg
  2698. + *
  2699. + ***************************************************************/
  2700. +
  2701. +#define ixt_alg_type ixt_support.ias_exttype
  2702. +#define ixt_alg_id ixt_support.ias_id
  2703. +
  2704. +#define IPSEC_ALG_ST_SUPP 0x01
  2705. +#define IPSEC_ALG_ST_REGISTERED 0x02
  2706. +#define IPSEC_ALG_ST_EXCL 0x04
  2707. +struct ipsec_alg {
  2708. + unsigned ixt_version; /* only allow this version (or 'near')*/ \
  2709. + struct list_head ixt_list; /* dlinked list */ \
  2710. + struct module *ixt_module; /* THIS_MODULE */ \
  2711. + unsigned ixt_state; /* state flags */ \
  2712. + atomic_t ixt_refcnt; /* ref. count when pointed from ipsec_sa */ \
  2713. + char ixt_name[16]; /* descriptive short name, eg. "3des" */ \
  2714. + void *ixt_data; /* private for algo implementation */ \
  2715. + uint8_t ixt_blocksize; /* blocksize in bytes */ \
  2716. +
  2717. + struct ipsec_alg_supported ixt_support;
  2718. +};
  2719. +/*
  2720. + * Note the const in cbc_encrypt IV arg:
  2721. + * some ciphers like to toast passed IV (eg. 3DES): make a local IV copy
  2722. + */
  2723. +struct ipsec_alg_enc {
  2724. + struct ipsec_alg ixt_common;
  2725. + unsigned ixt_e_keylen; /* raw key length in bytes */
  2726. + unsigned ixt_e_ctx_size; /* sa_p->key_e_size */
  2727. + int (*ixt_e_set_key)(struct ipsec_alg_enc *alg, __u8 *key_e, const __u8 *key, size_t keysize);
  2728. + __u8 *(*ixt_e_new_key)(struct ipsec_alg_enc *alg, const __u8 *key, size_t keysize);
  2729. + void (*ixt_e_destroy_key)(struct ipsec_alg_enc *alg, __u8 *key_e);
  2730. + int (*ixt_e_cbc_encrypt)(struct ipsec_alg_enc *alg, __u8 *key_e, __u8 *in, int ilen, const __u8 *iv, int encrypt);
  2731. +};
  2732. +struct ipsec_alg_auth {
  2733. + struct ipsec_alg ixt_common;
  2734. + unsigned ixt_a_keylen; /* raw key length in bytes */
  2735. + unsigned ixt_a_ctx_size; /* sa_p->key_a_size */
  2736. + unsigned ixt_a_authlen; /* 'natural' auth. hash len (bytes) */
  2737. + int (*ixt_a_hmac_set_key)(struct ipsec_alg_auth *alg, __u8 *key_a, const __u8 *key, int keylen);
  2738. + int (*ixt_a_hmac_hash)(struct ipsec_alg_auth *alg, __u8 *key_a, const __u8 *dat, int len, __u8 *hash, int hashlen);
  2739. +};
  2740. +/*
  2741. + * These are _copies_ of SADB_EXT_SUPPORTED_{AUTH,ENCRYPT},
  2742. + * to avoid header coupling for true constants
  2743. + * about headers ... "cp is your friend" --Linus
  2744. + */
  2745. +#define IPSEC_ALG_TYPE_AUTH 14
  2746. +#define IPSEC_ALG_TYPE_ENCRYPT 15
  2747. +
  2748. +/***************************************************************
  2749. + *
  2750. + * INTERFACE for module loading,testing, and unloading
  2751. + *
  2752. + ***************************************************************/
  2753. +/* - registration calls */
  2754. +int register_ipsec_alg(struct ipsec_alg *);
  2755. +int unregister_ipsec_alg(struct ipsec_alg *);
  2756. +/* - optional (simple test) for algos */
  2757. +int ipsec_alg_test(unsigned alg_type, unsigned alg_id, int testparm);
  2758. +/* inline wrappers (usefull for type validation */
  2759. +static inline int register_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {
  2760. + return register_ipsec_alg((struct ipsec_alg*)ixt);
  2761. +}
  2762. +static inline int unregister_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {
  2763. + return unregister_ipsec_alg((struct ipsec_alg*)ixt);
  2764. +}
  2765. +static inline int register_ipsec_alg_auth(struct ipsec_alg_auth *ixt) {
  2766. + return register_ipsec_alg((struct ipsec_alg*)ixt);
  2767. +}
  2768. +static inline int unregister_ipsec_alg_auth(struct ipsec_alg_auth *ixt) {
  2769. + return unregister_ipsec_alg((struct ipsec_alg*)ixt);
  2770. +}
  2771. +
  2772. +/*****************************************************************
  2773. + *
  2774. + * INTERFACE for ENC services: key creation, encrypt function
  2775. + *
  2776. + *****************************************************************/
  2777. +
  2778. +#define IPSEC_ALG_ENCRYPT 1
  2779. +#define IPSEC_ALG_DECRYPT 0
  2780. +
  2781. +/* encryption key context creation function */
  2782. +int ipsec_alg_enc_key_create(struct ipsec_sa *sa_p);
  2783. +/*
  2784. + * ipsec_alg_esp_encrypt(): encrypt ilen bytes in idat returns
  2785. + * 0 or ERR<0
  2786. + */
  2787. +int ipsec_alg_esp_encrypt(struct ipsec_sa *sa_p, __u8 *idat, int ilen, const __u8 *iv, int action);
  2788. +
  2789. +/***************************************************************
  2790. + *
  2791. + * INTERFACE for AUTH services: key creation, hash functions
  2792. + *
  2793. + ***************************************************************/
  2794. +int ipsec_alg_auth_key_create(struct ipsec_sa *sa_p);
  2795. +int ipsec_alg_sa_esp_hash(const struct ipsec_sa *sa_p, const __u8 *espp, int len, __u8 *hash, int hashlen) ;
  2796. +#define ipsec_alg_sa_esp_update(c,k,l) ipsec_alg_sa_esp_hash(c,k,l,NULL,0)
  2797. +
  2798. +/* only called from ipsec_init.c */
  2799. +int ipsec_alg_init(void);
  2800. +
  2801. +/* algo module glue for static algos */
  2802. +void ipsec_alg_static_init(void);
  2803. +typedef int (*ipsec_alg_init_func_t) (void);
  2804. +
  2805. +/**********************************************
  2806. + *
  2807. + * INTERFACE for ipsec_sa init and wipe
  2808. + *
  2809. + **********************************************/
  2810. +
  2811. +/* returns true if ipsec_sa has ipsec_alg obj attached */
  2812. +/*
  2813. + * Initializes ipsec_sa's ipsec_alg object, using already loaded
  2814. + * proto, authalg, encalg.; links ipsec_alg objects (enc, auth)
  2815. + */
  2816. +int ipsec_alg_sa_init(struct ipsec_sa *sa_p);
  2817. +/*
  2818. + * Destroys ipsec_sa's ipsec_alg object
  2819. + * unlinking ipsec_alg objects
  2820. + */
  2821. +int ipsec_alg_sa_wipe(struct ipsec_sa *sa_p);
  2822. +
  2823. +#define IPSEC_ALG_MODULE_INIT_MOD( func_name ) \
  2824. + static int func_name(void); \
  2825. + module_init(func_name); \
  2826. + static int __init func_name(void)
  2827. +#define IPSEC_ALG_MODULE_EXIT_MOD( func_name ) \
  2828. + static void func_name(void); \
  2829. + module_exit(func_name); \
  2830. + static void __exit func_name(void)
  2831. +
  2832. +#define IPSEC_ALG_MODULE_INIT_STATIC( func_name ) \
  2833. + extern int func_name(void); \
  2834. + int func_name(void)
  2835. +#define IPSEC_ALG_MODULE_EXIT_STATIC( func_name ) \
  2836. + extern void func_name(void); \
  2837. + void func_name(void)
  2838. +
  2839. +/**********************************************
  2840. + *
  2841. + * 2.2 backport for some 2.4 useful module stuff
  2842. + *
  2843. + **********************************************/
  2844. +#ifdef MODULE
  2845. +#ifndef THIS_MODULE
  2846. +#define THIS_MODULE (&__this_module)
  2847. +#endif
  2848. +#ifndef module_init
  2849. +typedef int (*__init_module_func_t)(void);
  2850. +typedef void (*__cleanup_module_func_t)(void);
  2851. +
  2852. +#define module_init(x) \
  2853. + int init_module(void) __attribute__((alias(#x))); \
  2854. + static inline __init_module_func_t __init_module_inline(void) \
  2855. + { return x; }
  2856. +#define module_exit(x) \
  2857. + void cleanup_module(void) __attribute__((alias(#x))); \
  2858. + static inline __cleanup_module_func_t __cleanup_module_inline(void) \
  2859. + { return x; }
  2860. +#endif
  2861. +#define IPSEC_ALG_MODULE_INIT( func_name ) IPSEC_ALG_MODULE_INIT_MOD( func_name )
  2862. +#define IPSEC_ALG_MODULE_EXIT( func_name ) IPSEC_ALG_MODULE_EXIT_MOD( func_name )
  2863. +
  2864. +#else /* not MODULE */
  2865. +#ifndef THIS_MODULE
  2866. +#define THIS_MODULE NULL
  2867. +#endif
  2868. +/*
  2869. + * I only want module_init() magic
  2870. + * when algo.c file *is THE MODULE*, in all other
  2871. + * cases, initialization is called explicitely from ipsec_alg_init()
  2872. + */
  2873. +#define IPSEC_ALG_MODULE_INIT( func_name ) IPSEC_ALG_MODULE_INIT_STATIC(func_name)
  2874. +#define IPSEC_ALG_MODULE_EXIT( func_name ) IPSEC_ALG_MODULE_EXIT_STATIC(func_name)
  2875. +#endif
  2876. +
  2877. +#endif /* IPSEC_ALG_H */
  2878. --- /dev/null Tue Mar 11 13:02:56 2003
  2879. +++ linux/include/openswan/ipsec_alg_3des.h Mon Feb 9 13:51:03 2004
  2880. @@ -0,0 +1,12 @@
  2881. +struct TripleDES_context {
  2882. + des_key_schedule s1;
  2883. + des_key_schedule s2;
  2884. + des_key_schedule s3;
  2885. +};
  2886. +typedef struct TripleDES_context TripleDES_context;
  2887. +
  2888. +#define ESP_3DES_KEY_SZ 3*(sizeof(des_cblock))
  2889. +#define ESP_3DES_CBC_BLK_LEN 8
  2890. +
  2891. +
  2892. +
  2893. --- /dev/null Tue Mar 11 13:02:56 2003
  2894. +++ linux/include/openswan/ipsec_auth.h Mon Feb 9 13:51:03 2004
  2895. @@ -0,0 +1,100 @@
  2896. +/*
  2897. + * Authentication Header declarations
  2898. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  2899. + *
  2900. + * This program is free software; you can redistribute it and/or modify it
  2901. + * under the terms of the GNU General Public License as published by the
  2902. + * Free Software Foundation; either version 2 of the License, or (at your
  2903. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2904. + *
  2905. + * This program is distributed in the hope that it will be useful, but
  2906. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2907. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2908. + * for more details.
  2909. + *
  2910. + * RCSID $Id: ipsec_auth.h,v 1.3 2004/04/06 02:49:08 mcr Exp $
  2911. + */
  2912. +
  2913. +#include "ipsec_md5h.h"
  2914. +#include "ipsec_sha1.h"
  2915. +
  2916. +#ifndef IPSEC_AUTH_H
  2917. +#define IPSEC_AUTH_H
  2918. +
  2919. +#define AH_FLENGTH 12 /* size of fixed part */
  2920. +#define AHMD5_KMAX 64 /* MD5 max 512 bits key */
  2921. +#define AHMD5_AMAX 12 /* MD5 96 bits of authenticator */
  2922. +
  2923. +#define AHMD596_KLEN 16 /* MD5 128 bits key */
  2924. +#define AHSHA196_KLEN 20 /* SHA1 160 bits key */
  2925. +
  2926. +#define AHMD596_ALEN 16 /* MD5 128 bits authentication length */
  2927. +#define AHSHA196_ALEN 20 /* SHA1 160 bits authentication length */
  2928. +
  2929. +#define AHMD596_BLKLEN 64 /* MD5 block length */
  2930. +#define AHSHA196_BLKLEN 64 /* SHA1 block length */
  2931. +#define AHSHA2_256_BLKLEN 64 /* SHA2-256 block length */
  2932. +#define AHSHA2_384_BLKLEN 128 /* SHA2-384 block length (?) */
  2933. +#define AHSHA2_512_BLKLEN 128 /* SHA2-512 block length */
  2934. +
  2935. +#define AH_BLKLEN_MAX 128 /* keep up to date! */
  2936. +
  2937. +
  2938. +#define AH_AMAX AHSHA196_ALEN /* keep up to date! */
  2939. +#define AHHMAC_HASHLEN 12 /* authenticator length of 96bits */
  2940. +#define AHHMAC_RPLLEN 4 /* 32 bit replay counter */
  2941. +
  2942. +#define DB_AH_PKTRX 0x0001
  2943. +#define DB_AH_PKTRX2 0x0002
  2944. +#define DB_AH_DMP 0x0004
  2945. +#define DB_AH_IPSA 0x0010
  2946. +#define DB_AH_XF 0x0020
  2947. +#define DB_AH_INAU 0x0040
  2948. +#define DB_AH_REPLAY 0x0100
  2949. +
  2950. +#ifdef __KERNEL__
  2951. +
  2952. +/* General HMAC algorithm is described in RFC 2104 */
  2953. +
  2954. +#define HMAC_IPAD 0x36
  2955. +#define HMAC_OPAD 0x5C
  2956. +
  2957. +struct md5_ctx {
  2958. + MD5_CTX ictx; /* context after H(K XOR ipad) */
  2959. + MD5_CTX octx; /* context after H(K XOR opad) */
  2960. +};
  2961. +
  2962. +struct sha1_ctx {
  2963. + SHA1_CTX ictx; /* context after H(K XOR ipad) */
  2964. + SHA1_CTX octx; /* context after H(K XOR opad) */
  2965. +};
  2966. +
  2967. +struct auth_alg {
  2968. + void (*init)(void *ctx);
  2969. + void (*update)(void *ctx, unsigned char *bytes, __u32 len);
  2970. + void (*final)(unsigned char *hash, void *ctx);
  2971. + int hashlen;
  2972. +};
  2973. +
  2974. +struct options;
  2975. +
  2976. +#endif /* __KERNEL__ */
  2977. +#endif /* IPSEC_AUTH_H */
  2978. +
  2979. +/*
  2980. + * $Log: ipsec_auth.h,v $
  2981. + * Revision 1.3 2004/04/06 02:49:08 mcr
  2982. + * pullup of algo code from alg-branch.
  2983. + *
  2984. + * Revision 1.2 2004/04/05 19:55:04 mcr
  2985. + * Moved from linux/include/freeswan/ipsec_auth.h,v
  2986. + *
  2987. + * Revision 1.1 2003/12/13 19:10:16 mcr
  2988. + * refactored rcv and xmit code - same as FS 2.05.
  2989. + *
  2990. + * Revision 1.1 2003/12/06 21:21:19 mcr
  2991. + * split up receive path into per-transform files, for
  2992. + * easier later removal.
  2993. + *
  2994. + *
  2995. + */
  2996. --- /dev/null Tue Mar 11 13:02:56 2003
  2997. +++ linux/include/openswan/ipsec_encap.h Mon Feb 9 13:51:03 2004
  2998. @@ -0,0 +1,149 @@
  2999. +/*
  3000. + * declarations relevant to encapsulation-like operations
  3001. + * Copyright (C) 1996, 1997 John Ioannidis.
  3002. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  3003. + *
  3004. + * This program is free software; you can redistribute it and/or modify it
  3005. + * under the terms of the GNU General Public License as published by the
  3006. + * Free Software Foundation; either version 2 of the License, or (at your
  3007. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3008. + *
  3009. + * This program is distributed in the hope that it will be useful, but
  3010. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3011. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3012. + * for more details.
  3013. + *
  3014. + * RCSID $Id: ipsec_encap.h,v 1.19 2004/04/05 19:55:04 mcr Exp $
  3015. + */
  3016. +
  3017. +#ifndef _IPSEC_ENCAP_H_
  3018. +
  3019. +#define SENT_IP4 16 /* data is two struct in_addr + proto + ports*/
  3020. + /* (2 * sizeof(struct in_addr)) */
  3021. + /* sizeof(struct sockaddr_encap)
  3022. + - offsetof(struct sockaddr_encap, Sen.Sip4.Src) */
  3023. +
  3024. +struct sockaddr_encap
  3025. +{
  3026. + __u8 sen_len; /* length */
  3027. + __u8 sen_family; /* AF_ENCAP */
  3028. + __u16 sen_type; /* see SENT_* */
  3029. + union
  3030. + {
  3031. + struct /* SENT_IP4 */
  3032. + {
  3033. + struct in_addr Src;
  3034. + struct in_addr Dst;
  3035. + __u8 Proto;
  3036. + __u16 Sport;
  3037. + __u16 Dport;
  3038. + } Sip4;
  3039. + } Sen;
  3040. +};
  3041. +
  3042. +#define sen_ip_src Sen.Sip4.Src
  3043. +#define sen_ip_dst Sen.Sip4.Dst
  3044. +#define sen_proto Sen.Sip4.Proto
  3045. +#define sen_sport Sen.Sip4.Sport
  3046. +#define sen_dport Sen.Sip4.Dport
  3047. +
  3048. +#ifndef AF_ENCAP
  3049. +#define AF_ENCAP 26
  3050. +#endif /* AF_ENCAP */
  3051. +
  3052. +#define _IPSEC_ENCAP_H_
  3053. +#endif /* _IPSEC_ENCAP_H_ */
  3054. +
  3055. +/*
  3056. + * $Log: ipsec_encap.h,v $
  3057. + * Revision 1.19 2004/04/05 19:55:04 mcr
  3058. + * Moved from linux/include/freeswan/ipsec_encap.h,v
  3059. + *
  3060. + * Revision 1.18 2003/10/31 02:27:05 mcr
  3061. + * pulled up port-selector patches and sa_id elimination.
  3062. + *
  3063. + * Revision 1.17.30.1 2003/09/21 13:59:38 mcr
  3064. + * pre-liminary X.509 patch - does not yet pass tests.
  3065. + *
  3066. + * Revision 1.17 2002/04/24 07:36:46 mcr
  3067. + * Moved from ./klips/net/ipsec/ipsec_encap.h,v
  3068. + *
  3069. + * Revision 1.16 2001/11/26 09:23:47 rgb
  3070. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3071. + *
  3072. + * Revision 1.15.2.1 2001/09/25 02:18:54 mcr
  3073. + * struct eroute moved to ipsec_eroute.h
  3074. + *
  3075. + * Revision 1.15 2001/09/14 16:58:36 rgb
  3076. + * Added support for storing the first and last packets through a HOLD.
  3077. + *
  3078. + * Revision 1.14 2001/09/08 21:13:31 rgb
  3079. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  3080. + *
  3081. + * Revision 1.13 2001/06/14 19:35:08 rgb
  3082. + * Update copyright date.
  3083. + *
  3084. + * Revision 1.12 2001/05/27 06:12:10 rgb
  3085. + * Added structures for pid, packet count and last access time to eroute.
  3086. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  3087. + *
  3088. + * Revision 1.11 2000/09/08 19:12:56 rgb
  3089. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  3090. + *
  3091. + * Revision 1.10 2000/03/22 16:15:36 rgb
  3092. + * Fixed renaming of dev_get (MB).
  3093. + *
  3094. + * Revision 1.9 2000/01/21 06:13:26 rgb
  3095. + * Added a macro for AF_ENCAP
  3096. + *
  3097. + * Revision 1.8 1999/12/31 14:56:55 rgb
  3098. + * MB fix for 2.3 dev-use-count.
  3099. + *
  3100. + * Revision 1.7 1999/11/18 04:09:18 rgb
  3101. + * Replaced all kernel version macros to shorter, readable form.
  3102. + *
  3103. + * Revision 1.6 1999/09/24 00:34:13 rgb
  3104. + * Add Marc Boucher's support for 2.3.xx+.
  3105. + *
  3106. + * Revision 1.5 1999/04/11 00:28:57 henry
  3107. + * GPL boilerplate
  3108. + *
  3109. + * Revision 1.4 1999/04/06 04:54:25 rgb
  3110. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  3111. + * patch shell fixes.
  3112. + *
  3113. + * Revision 1.3 1998/10/19 14:44:28 rgb
  3114. + * Added inclusion of freeswan.h.
  3115. + * sa_id structure implemented and used: now includes protocol.
  3116. + *
  3117. + * Revision 1.2 1998/07/14 18:19:33 rgb
  3118. + * Added #ifdef __KERNEL__ directives to restrict scope of header.
  3119. + *
  3120. + * Revision 1.1 1998/06/18 21:27:44 henry
  3121. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  3122. + * kernel-build scripts happier in the presence of symlinks
  3123. + *
  3124. + * Revision 1.2 1998/04/21 21:29:10 rgb
  3125. + * Rearrange debug switches to change on the fly debug output from user
  3126. + * space. Only kernel changes checked in at this time. radij.c was also
  3127. + * changed to temporarily remove buggy debugging code in rj_delete causing
  3128. + * an OOPS and hence, netlink device open errors.
  3129. + *
  3130. + * Revision 1.1 1998/04/09 03:05:58 henry
  3131. + * sources moved up from linux/net/ipsec
  3132. + *
  3133. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  3134. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  3135. + *
  3136. + * Revision 0.4 1997/01/15 01:28:15 ji
  3137. + * Minor cosmetic changes.
  3138. + *
  3139. + * Revision 0.3 1996/11/20 14:35:48 ji
  3140. + * Minor Cleanup.
  3141. + * Rationalized debugging code.
  3142. + *
  3143. + * Revision 0.2 1996/11/02 00:18:33 ji
  3144. + * First limited release.
  3145. + *
  3146. + *
  3147. + */
  3148. --- /dev/null Tue Mar 11 13:02:56 2003
  3149. +++ linux/include/openswan/ipsec_eroute.h Mon Feb 9 13:51:03 2004
  3150. @@ -0,0 +1,112 @@
  3151. +/*
  3152. + * @(#) declarations of eroute structures
  3153. + *
  3154. + * Copyright (C) 1996, 1997 John Ioannidis.
  3155. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  3156. + * Copyright (C) 2001 Michael Richardson <mcr@freeswan.org>
  3157. + *
  3158. + * This program is free software; you can redistribute it and/or modify it
  3159. + * under the terms of the GNU General Public License as published by the
  3160. + * Free Software Foundation; either version 2 of the License, or (at your
  3161. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3162. + *
  3163. + * This program is distributed in the hope that it will be useful, but
  3164. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3165. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3166. + * for more details.
  3167. + *
  3168. + * RCSID $Id: ipsec_eroute.h,v 1.5 2004/04/05 19:55:05 mcr Exp $
  3169. + *
  3170. + * derived from ipsec_encap.h 1.15 on 2001/9/18 by mcr.
  3171. + *
  3172. + */
  3173. +
  3174. +#ifndef _IPSEC_EROUTE_H_
  3175. +
  3176. +#include "radij.h"
  3177. +#include "ipsec_encap.h"
  3178. +#include "ipsec_radij.h"
  3179. +
  3180. +/*
  3181. + * The "type" is really part of the address as far as the routing
  3182. + * system is concerned. By using only one bit in the type field
  3183. + * for each type, we sort-of make sure that different types of
  3184. + * encapsulation addresses won't be matched against the wrong type.
  3185. + */
  3186. +
  3187. +/*
  3188. + * An entry in the radix tree
  3189. + */
  3190. +
  3191. +struct rjtentry
  3192. +{
  3193. + struct radij_node rd_nodes[2]; /* tree glue, and other values */
  3194. +#define rd_key(r) ((struct sockaddr_encap *)((r)->rd_nodes->rj_key))
  3195. +#define rd_mask(r) ((struct sockaddr_encap *)((r)->rd_nodes->rj_mask))
  3196. + short rd_flags;
  3197. + short rd_count;
  3198. +};
  3199. +
  3200. +struct ident
  3201. +{
  3202. + __u16 type; /* identity type */
  3203. + __u64 id; /* identity id */
  3204. + __u8 len; /* identity len */
  3205. + caddr_t data; /* identity data */
  3206. +};
  3207. +
  3208. +/*
  3209. + * An encapsulation route consists of a pointer to a
  3210. + * radix tree entry and a SAID (a destination_address/SPI/protocol triple).
  3211. + */
  3212. +
  3213. +struct eroute
  3214. +{
  3215. + struct rjtentry er_rjt;
  3216. + ip_said er_said;
  3217. + uint32_t er_pid;
  3218. + uint32_t er_count;
  3219. + uint64_t er_lasttime;
  3220. + struct sockaddr_encap er_eaddr; /* MCR get rid of _encap, it is silly*/
  3221. + struct sockaddr_encap er_emask;
  3222. + struct ident er_ident_s;
  3223. + struct ident er_ident_d;
  3224. + struct sk_buff* er_first;
  3225. + struct sk_buff* er_last;
  3226. +};
  3227. +
  3228. +#define er_dst er_said.dst
  3229. +#define er_spi er_said.spi
  3230. +
  3231. +#define _IPSEC_EROUTE_H_
  3232. +#endif /* _IPSEC_EROUTE_H_ */
  3233. +
  3234. +/*
  3235. + * $Log: ipsec_eroute.h,v $
  3236. + * Revision 1.5 2004/04/05 19:55:05 mcr
  3237. + * Moved from linux/include/freeswan/ipsec_eroute.h,v
  3238. + *
  3239. + * Revision 1.4 2003/10/31 02:27:05 mcr
  3240. + * pulled up port-selector patches and sa_id elimination.
  3241. + *
  3242. + * Revision 1.3.30.2 2003/10/29 01:10:19 mcr
  3243. + * elimited "struct sa_id"
  3244. + *
  3245. + * Revision 1.3.30.1 2003/09/21 13:59:38 mcr
  3246. + * pre-liminary X.509 patch - does not yet pass tests.
  3247. + *
  3248. + * Revision 1.3 2002/04/24 07:36:46 mcr
  3249. + * Moved from ./klips/net/ipsec/ipsec_eroute.h,v
  3250. + *
  3251. + * Revision 1.2 2001/11/26 09:16:13 rgb
  3252. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3253. + *
  3254. + * Revision 1.1.2.1 2001/09/25 02:18:54 mcr
  3255. + * struct eroute moved to ipsec_eroute.h
  3256. + *
  3257. + *
  3258. + * Local variables:
  3259. + * c-file-style: "linux"
  3260. + * End:
  3261. + *
  3262. + */
  3263. --- /dev/null Tue Mar 11 13:02:56 2003
  3264. +++ linux/include/openswan/ipsec_errs.h Mon Feb 9 13:51:03 2004
  3265. @@ -0,0 +1,53 @@
  3266. +/*
  3267. + * @(#) definition of ipsec_errs structure
  3268. + *
  3269. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  3270. + * and Michael Richardson <mcr@freeswan.org>
  3271. + *
  3272. + * This program is free software; you can redistribute it and/or modify it
  3273. + * under the terms of the GNU General Public License as published by the
  3274. + * Free Software Foundation; either version 2 of the License, or (at your
  3275. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3276. + *
  3277. + * This program is distributed in the hope that it will be useful, but
  3278. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3279. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3280. + * for more details.
  3281. + *
  3282. + * RCSID $Id: ipsec_errs.h,v 1.4 2004/04/05 19:55:05 mcr Exp $
  3283. + *
  3284. + */
  3285. +
  3286. +/*
  3287. + * This file describes the errors/statistics that FreeSWAN collects.
  3288. + *
  3289. + */
  3290. +
  3291. +struct ipsec_errs {
  3292. + __u32 ips_alg_errs; /* number of algorithm errors */
  3293. + __u32 ips_auth_errs; /* # of authentication errors */
  3294. + __u32 ips_encsize_errs; /* # of encryption size errors*/
  3295. + __u32 ips_encpad_errs; /* # of encryption pad errors*/
  3296. + __u32 ips_replaywin_errs; /* # of pkt sequence errors */
  3297. +};
  3298. +
  3299. +/*
  3300. + * $Log: ipsec_errs.h,v $
  3301. + * Revision 1.4 2004/04/05 19:55:05 mcr
  3302. + * Moved from linux/include/freeswan/ipsec_errs.h,v
  3303. + *
  3304. + * Revision 1.3 2002/04/24 07:36:46 mcr
  3305. + * Moved from ./klips/net/ipsec/ipsec_errs.h,v
  3306. + *
  3307. + * Revision 1.2 2001/11/26 09:16:13 rgb
  3308. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3309. + *
  3310. + * Revision 1.1.2.1 2001/09/25 02:25:57 mcr
  3311. + * lifetime structure created and common functions created.
  3312. + *
  3313. + *
  3314. + * Local variables:
  3315. + * c-file-style: "linux"
  3316. + * End:
  3317. + *
  3318. + */
  3319. --- /dev/null Tue Mar 11 13:02:56 2003
  3320. +++ linux/include/openswan/ipsec_esp.h Mon Feb 9 13:51:03 2004
  3321. @@ -0,0 +1,157 @@
  3322. +/*
  3323. + * Copyright (C) 1996, 1997 John Ioannidis.
  3324. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  3325. + *
  3326. + * This program is free software; you can redistribute it and/or modify it
  3327. + * under the terms of the GNU General Public License as published by the
  3328. + * Free Software Foundation; either version 2 of the License, or (at your
  3329. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3330. + *
  3331. + * This program is distributed in the hope that it will be useful, but
  3332. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3333. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3334. + * for more details.
  3335. + *
  3336. + * RCSID $Id: ipsec_esp.h,v 1.28 2004/09/13 02:22:10 mcr Exp $
  3337. + */
  3338. +
  3339. +#include "openswan/ipsec_md5h.h"
  3340. +#include "openswan/ipsec_sha1.h"
  3341. +
  3342. +#include "crypto/des.h"
  3343. +
  3344. +#ifndef IPPROTO_ESP
  3345. +#define IPPROTO_ESP 50
  3346. +#endif /* IPPROTO_ESP */
  3347. +
  3348. +#define ESP_HEADER_LEN 8 /* 64 bits header (spi+rpl)*/
  3349. +
  3350. +#define EMT_ESPDESCBC_ULEN 20 /* coming from user mode */
  3351. +#define EMT_ESPDES_KMAX 64 /* 512 bit secret key enough? */
  3352. +#define EMT_ESPDES_KEY_SZ 8 /* 56 bit secret key with parity = 64 bits */
  3353. +#define EMT_ESP3DES_KEY_SZ 24 /* 168 bit secret key with parity = 192 bits */
  3354. +#define EMT_ESPDES_IV_SZ 8 /* IV size */
  3355. +#define ESP_DESCBC_BLKLEN 8 /* DES-CBC block size */
  3356. +
  3357. +#define ESP_IV_MAXSZ 16 /* This is _critical_ */
  3358. +#define ESP_IV_MAXSZ_INT (ESP_IV_MAXSZ/sizeof(int))
  3359. +
  3360. +#define DB_ES_PKTRX 0x0001
  3361. +#define DB_ES_PKTRX2 0x0002
  3362. +#define DB_ES_IPSA 0x0010
  3363. +#define DB_ES_XF 0x0020
  3364. +#define DB_ES_IPAD 0x0040
  3365. +#define DB_ES_INAU 0x0080
  3366. +#define DB_ES_OINFO 0x0100
  3367. +#define DB_ES_OINFO2 0x0200
  3368. +#define DB_ES_OH 0x0400
  3369. +#define DB_ES_REPLAY 0x0800
  3370. +
  3371. +#ifdef __KERNEL__
  3372. +struct des_eks {
  3373. + des_key_schedule ks;
  3374. +};
  3375. +
  3376. +extern struct inet_protocol esp_protocol;
  3377. +
  3378. +struct options;
  3379. +
  3380. +struct esphdr
  3381. +{
  3382. + __u32 esp_spi; /* Security Parameters Index */
  3383. + __u32 esp_rpl; /* Replay counter */
  3384. + __u8 esp_iv[8]; /* iv */
  3385. +};
  3386. +
  3387. +extern struct xform_functions esp_xform_funcs[];
  3388. +
  3389. +#ifdef CONFIG_KLIPS_DEBUG
  3390. +extern int debug_esp;
  3391. +#endif /* CONFIG_KLIPS_DEBUG */
  3392. +#endif /* __KERNEL__ */
  3393. +
  3394. +/*
  3395. + * $Log: ipsec_esp.h,v $
  3396. + * Revision 1.28 2004/09/13 02:22:10 mcr
  3397. + * #define inet_protocol if necessary.
  3398. + *
  3399. + * Revision 1.27 2004/09/06 18:35:41 mcr
  3400. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  3401. + * so adjust for that.
  3402. + *
  3403. + * Revision 1.26 2004/07/10 19:08:41 mcr
  3404. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  3405. + *
  3406. + * Revision 1.25 2004/04/06 02:49:08 mcr
  3407. + * pullup of algo code from alg-branch.
  3408. + *
  3409. + * Revision 1.24 2004/04/05 19:55:05 mcr
  3410. + * Moved from linux/include/freeswan/ipsec_esp.h,v
  3411. + *
  3412. + * Revision 1.23 2004/04/05 19:41:05 mcr
  3413. + * merged alg-branch code.
  3414. + *
  3415. + * Revision 1.22 2003/12/13 19:10:16 mcr
  3416. + * refactored rcv and xmit code - same as FS 2.05.
  3417. + *
  3418. + * Revision 1.23 2003/12/11 20:14:58 mcr
  3419. + * refactored the xmit code, to move all encapsulation
  3420. + * code into protocol functions. Note that all functions
  3421. + * are essentially done by a single function, which is probably
  3422. + * wrong.
  3423. + * the rcv_functions structures are renamed xform_functions.
  3424. + *
  3425. + * Revision 1.22 2003/12/06 21:21:19 mcr
  3426. + * split up receive path into per-transform files, for
  3427. + * easier later removal.
  3428. + *
  3429. + * Revision 1.21.8.1 2003/12/22 15:25:52 jjo
  3430. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  3431. + *
  3432. + * Revision 1.21 2003/02/06 02:21:34 rgb
  3433. + *
  3434. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  3435. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  3436. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  3437. + *
  3438. + * Revision 1.20 2002/05/14 02:37:02 rgb
  3439. + * Change reference from _TDB to _IPSA.
  3440. + *
  3441. + * Revision 1.19 2002/04/24 07:55:32 mcr
  3442. + * #include patches and Makefiles for post-reorg compilation.
  3443. + *
  3444. + * Revision 1.18 2002/04/24 07:36:46 mcr
  3445. + * Moved from ./klips/net/ipsec/ipsec_esp.h,v
  3446. + *
  3447. + * Revision 1.17 2002/02/20 01:27:07 rgb
  3448. + * Ditched a pile of structs only used by the old Netlink interface.
  3449. + *
  3450. + * Revision 1.16 2001/12/11 02:35:57 rgb
  3451. + * Change "struct net_device" to "struct device" for 2.2 compatibility.
  3452. + *
  3453. + * Revision 1.15 2001/11/26 09:23:48 rgb
  3454. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3455. + *
  3456. + * Revision 1.14.2.3 2001/10/23 04:16:42 mcr
  3457. + * get definition of des_key_schedule from des.h
  3458. + *
  3459. + * Revision 1.14.2.2 2001/10/22 20:33:13 mcr
  3460. + * use "des_key_schedule" structure instead of cooking our own.
  3461. + *
  3462. + * Revision 1.14.2.1 2001/09/25 02:18:25 mcr
  3463. + * replace "struct device" with "struct netdevice"
  3464. + *
  3465. + * Revision 1.14 2001/06/14 19:35:08 rgb
  3466. + * Update copyright date.
  3467. + *
  3468. + * Revision 1.13 2000/09/08 19:12:56 rgb
  3469. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  3470. + *
  3471. + * Revision 1.12 2000/08/01 14:51:50 rgb
  3472. + * Removed _all_ remaining traces of DES.
  3473. + *
  3474. + * Revision 1.11 2000/01/10 16:36:20 rgb
  3475. + * Ditch last of EME option flags, including initiator.
  3476. + *
  3477. + *
  3478. + */
  3479. --- /dev/null Tue Mar 11 13:02:56 2003
  3480. +++ linux/include/openswan/ipsec_ipcomp.h Mon Feb 9 13:51:03 2004
  3481. @@ -0,0 +1,94 @@
  3482. +/*
  3483. + * IP compression header declations
  3484. + *
  3485. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  3486. + *
  3487. + * This program is free software; you can redistribute it and/or modify it
  3488. + * under the terms of the GNU General Public License as published by the
  3489. + * Free Software Foundation; either version 2 of the License, or (at your
  3490. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3491. + *
  3492. + * This program is distributed in the hope that it will be useful, but
  3493. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3494. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3495. + * for more details.
  3496. + *
  3497. + * RCSID $Id: ipsec_ipcomp.h,v 1.4 2004/07/10 19:08:41 mcr Exp $
  3498. + */
  3499. +
  3500. +#ifndef IPSEC_IPCOMP_H
  3501. +#define IPSEC_IPCOMP_H
  3502. +
  3503. +#include "openswan/ipsec_auth.h"
  3504. +
  3505. +/* Prefix all global deflate symbols with "ipcomp_" to avoid collisions with ppp_deflate & ext2comp */
  3506. +#ifndef IPCOMP_PREFIX
  3507. +#define IPCOMP_PREFIX
  3508. +#endif /* IPCOMP_PREFIX */
  3509. +
  3510. +#ifndef IPPROTO_COMP
  3511. +#define IPPROTO_COMP 108
  3512. +#endif /* IPPROTO_COMP */
  3513. +
  3514. +#ifdef CONFIG_KLIPS_DEBUG
  3515. +extern int sysctl_ipsec_debug_ipcomp;
  3516. +#endif /* CONFIG_KLIPS_DEBUG */
  3517. +
  3518. +struct ipcomphdr { /* IPCOMP header */
  3519. + __u8 ipcomp_nh; /* Next header (protocol) */
  3520. + __u8 ipcomp_flags; /* Reserved, must be 0 */
  3521. + __u16 ipcomp_cpi; /* Compression Parameter Index */
  3522. +};
  3523. +
  3524. +extern struct inet_protocol comp_protocol;
  3525. +extern int sysctl_ipsec_debug_ipcomp;
  3526. +
  3527. +#define IPCOMP_UNCOMPRESSABLE 0x000000001
  3528. +#define IPCOMP_COMPRESSIONERROR 0x000000002
  3529. +#define IPCOMP_PARMERROR 0x000000004
  3530. +#define IPCOMP_DECOMPRESSIONERROR 0x000000008
  3531. +
  3532. +#define IPCOMP_ADAPT_INITIAL_TRIES 8
  3533. +#define IPCOMP_ADAPT_INITIAL_SKIP 4
  3534. +#define IPCOMP_ADAPT_SUBSEQ_TRIES 2
  3535. +#define IPCOMP_ADAPT_SUBSEQ_SKIP 8
  3536. +
  3537. +/* Function prototypes */
  3538. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  3539. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  3540. +
  3541. +extern struct xform_functions ipcomp_xform_funcs[];
  3542. +
  3543. +#endif /* IPSEC_IPCOMP_H */
  3544. +
  3545. +/*
  3546. + * $Log: ipsec_ipcomp.h,v $
  3547. + * Revision 1.4 2004/07/10 19:08:41 mcr
  3548. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  3549. + *
  3550. + * Revision 1.3 2004/04/06 02:49:08 mcr
  3551. + * pullup of algo code from alg-branch.
  3552. + *
  3553. + * Revision 1.2 2004/04/05 19:55:05 mcr
  3554. + * Moved from linux/include/freeswan/ipsec_ipcomp.h,v
  3555. + *
  3556. + * Revision 1.1 2003/12/13 19:10:16 mcr
  3557. + * refactored rcv and xmit code - same as FS 2.05.
  3558. + *
  3559. + * Revision 1.2 2003/12/11 20:14:58 mcr
  3560. + * refactored the xmit code, to move all encapsulation
  3561. + * code into protocol functions. Note that all functions
  3562. + * are essentially done by a single function, which is probably
  3563. + * wrong.
  3564. + * the rcv_functions structures are renamed xform_functions.
  3565. + *
  3566. + * Revision 1.1 2003/12/06 21:21:19 mcr
  3567. + * split up receive path into per-transform files, for
  3568. + * easier later removal.
  3569. + *
  3570. + *
  3571. + *
  3572. + */
  3573. +
  3574. +
  3575. +
  3576. --- /dev/null Tue Mar 11 13:02:56 2003
  3577. +++ linux/include/openswan/ipsec_ipe4.h Mon Feb 9 13:51:03 2004
  3578. @@ -0,0 +1,68 @@
  3579. +/*
  3580. + * IP-in-IP Header declarations
  3581. + * Copyright (C) 1996, 1997 John Ioannidis.
  3582. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  3583. + *
  3584. + * This program is free software; you can redistribute it and/or modify it
  3585. + * under the terms of the GNU General Public License as published by the
  3586. + * Free Software Foundation; either version 2 of the License, or (at your
  3587. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3588. + *
  3589. + * This program is distributed in the hope that it will be useful, but
  3590. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3591. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3592. + * for more details.
  3593. + *
  3594. + * RCSID $Id: ipsec_ipe4.h,v 1.6 2004/04/05 19:55:05 mcr Exp $
  3595. + */
  3596. +
  3597. +/* The packet header is an IP header! */
  3598. +
  3599. +struct ipe4_xdata /* transform table data */
  3600. +{
  3601. + struct in_addr i4_src;
  3602. + struct in_addr i4_dst;
  3603. +};
  3604. +
  3605. +#define EMT_IPE4_ULEN 8 /* coming from user mode */
  3606. +
  3607. +
  3608. +/*
  3609. + * $Log: ipsec_ipe4.h,v $
  3610. + * Revision 1.6 2004/04/05 19:55:05 mcr
  3611. + * Moved from linux/include/freeswan/ipsec_ipe4.h,v
  3612. + *
  3613. + * Revision 1.5 2002/04/24 07:36:46 mcr
  3614. + * Moved from ./klips/net/ipsec/ipsec_ipe4.h,v
  3615. + *
  3616. + * Revision 1.4 2001/06/14 19:35:08 rgb
  3617. + * Update copyright date.
  3618. + *
  3619. + * Revision 1.3 1999/04/11 00:28:57 henry
  3620. + * GPL boilerplate
  3621. + *
  3622. + * Revision 1.2 1999/04/06 04:54:25 rgb
  3623. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  3624. + * patch shell fixes.
  3625. + *
  3626. + * Revision 1.1 1998/06/18 21:27:47 henry
  3627. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  3628. + * kernel-build scripts happier in the presence of symlinks
  3629. + *
  3630. + * Revision 1.1 1998/04/09 03:06:07 henry
  3631. + * sources moved up from linux/net/ipsec
  3632. + *
  3633. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  3634. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  3635. + *
  3636. + * Revision 0.4 1997/01/15 01:28:15 ji
  3637. + * No changes.
  3638. + *
  3639. + * Revision 0.3 1996/11/20 14:48:53 ji
  3640. + * Release update only.
  3641. + *
  3642. + * Revision 0.2 1996/11/02 00:18:33 ji
  3643. + * First limited release.
  3644. + *
  3645. + *
  3646. + */
  3647. --- /dev/null Tue Mar 11 13:02:56 2003
  3648. +++ linux/include/openswan/ipsec_ipip.h Mon Feb 9 13:51:03 2004
  3649. @@ -0,0 +1,45 @@
  3650. +/*
  3651. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  3652. + *
  3653. + * This program is free software; you can redistribute it and/or modify it
  3654. + * under the terms of the GNU General Public License as published by the
  3655. + * Free Software Foundation; either version 2 of the License, or (at your
  3656. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3657. + *
  3658. + * This program is distributed in the hope that it will be useful, but
  3659. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3660. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3661. + * for more details.
  3662. + *
  3663. + * RCSID $Id: ipsec_ipip.h,v 1.2 2004/04/05 19:55:05 mcr Exp $
  3664. + */
  3665. +
  3666. +#ifndef _IPSEC_IPIP_H_
  3667. +
  3668. +#ifndef IPPROTO_IPIP
  3669. +#define IPPROTO_IPIP 4
  3670. +#endif /* IPPROTO_ESP */
  3671. +
  3672. +extern struct xform_functions ipip_xform_funcs[];
  3673. +
  3674. +#define _IPSEC_IPIP_H_
  3675. +
  3676. +#endif /* _IPSEC_IPIP_H_ */
  3677. +
  3678. +/*
  3679. + * $Log: ipsec_ipip.h,v $
  3680. + * Revision 1.2 2004/04/05 19:55:05 mcr
  3681. + * Moved from linux/include/freeswan/ipsec_ipip.h,v
  3682. + *
  3683. + * Revision 1.1 2003/12/13 19:10:16 mcr
  3684. + * refactored rcv and xmit code - same as FS 2.05.
  3685. + *
  3686. + * Revision 1.1 2003/12/11 20:14:58 mcr
  3687. + * refactored the xmit code, to move all encapsulation
  3688. + * code into protocol functions. Note that all functions
  3689. + * are essentially done by a single function, which is probably
  3690. + * wrong.
  3691. + * the rcv_functions structures are renamed xform_functions.
  3692. + *
  3693. + *
  3694. + */
  3695. --- /dev/null Tue Mar 11 13:02:56 2003
  3696. +++ linux/include/openswan/ipsec_kern24.h Mon Feb 9 13:51:03 2004
  3697. @@ -0,0 +1,61 @@
  3698. +/*
  3699. + * @(#) routines to makes kernel 2.4 compatible with 2.6 usage.
  3700. + *
  3701. + * Copyright (C) 2004 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  3702. + *
  3703. + * This program is free software; you can redistribute it and/or modify it
  3704. + * under the terms of the GNU General Public License as published by the
  3705. + * Free Software Foundation; either version 2 of the License, or (at your
  3706. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3707. + *
  3708. + * This program is distributed in the hope that it will be useful, but
  3709. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3710. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3711. + * for more details.
  3712. + *
  3713. + * RCSID $Id: ipsec_kern24.h,v 1.4 2005/05/20 03:19:18 mcr Exp $
  3714. + */
  3715. +
  3716. +#ifndef _IPSEC_KERN24_H
  3717. +
  3718. +#ifndef NET_26
  3719. +#define sk_receive_queue receive_queue
  3720. +#define sk_destruct destruct
  3721. +#define sk_reuse reuse
  3722. +#define sk_zapped zapped
  3723. +#define sk_family family
  3724. +#define sk_protocol protocol
  3725. +#define sk_protinfo protinfo
  3726. +#define sk_sleep sleep
  3727. +#define sk_state_change state_change
  3728. +#define sk_shutdown shutdown
  3729. +#define sk_err err
  3730. +#define sk_stamp stamp
  3731. +#define sk_socket socket
  3732. +#define sk_sndbuf sndbuf
  3733. +#define sock_flag(sk, flag) sk->dead
  3734. +#define sk_for_each(sk, node, plist) for(sk=*plist; sk!=NULL; sk = sk->next)
  3735. +#endif
  3736. +
  3737. +/* deal with 2.4 vs 2.6 issues with module counts */
  3738. +
  3739. +/* in 2.6, all refcounts are maintained *outside* of the
  3740. + * module to deal with race conditions.
  3741. + */
  3742. +
  3743. +#ifdef NET_26
  3744. +#define KLIPS_INC_USE /* nothing */
  3745. +#define KLIPS_DEC_USE /* nothing */
  3746. +
  3747. +#else
  3748. +#define KLIPS_INC_USE MOD_INC_USE_COUNT
  3749. +#define KLIPS_DEC_USE MOD_DEC_USE_COUNT
  3750. +#endif
  3751. +
  3752. +extern int printk_ratelimit(void);
  3753. +
  3754. +
  3755. +#define _IPSEC_KERN24_H 1
  3756. +
  3757. +#endif /* _IPSEC_KERN24_H */
  3758. +
  3759. --- /dev/null Tue Mar 11 13:02:56 2003
  3760. +++ linux/include/openswan/ipsec_kversion.h Mon Feb 9 13:51:03 2004
  3761. @@ -0,0 +1,352 @@
  3762. +#ifndef _OPENSWAN_KVERSIONS_H
  3763. +/*
  3764. + * header file for FreeS/WAN library functions
  3765. + * Copyright (C) 1998, 1999, 2000 Henry Spencer.
  3766. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs
  3767. + *
  3768. + * This library is free software; you can redistribute it and/or modify it
  3769. + * under the terms of the GNU Library General Public License as published by
  3770. + * the Free Software Foundation; either version 2 of the License, or (at your
  3771. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  3772. + *
  3773. + * This library is distributed in the hope that it will be useful, but
  3774. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3775. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  3776. + * License for more details.
  3777. + *
  3778. + * RCSID $Id: ipsec_kversion.h,v 1.15.2.11 2007/02/20 03:53:16 paul Exp $
  3779. + */
  3780. +#define _OPENSWAN_KVERSIONS_H /* seen it, no need to see it again */
  3781. +
  3782. +/*
  3783. + * this file contains a series of atomic defines that depend upon
  3784. + * kernel version numbers. The kernel versions are arranged
  3785. + * in version-order number (which is often not chronological)
  3786. + * and each clause enables or disables a feature.
  3787. + */
  3788. +
  3789. +/*
  3790. + * First, assorted kernel-version-dependent trickery.
  3791. + */
  3792. +#include <linux/version.h>
  3793. +#ifndef KERNEL_VERSION
  3794. +#define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  3795. +#endif
  3796. +
  3797. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
  3798. +#define HEADER_CACHE_BIND_21
  3799. +#error "KLIPS is no longer supported on Linux 2.0. Sorry"
  3800. +#endif
  3801. +
  3802. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
  3803. +#define SPINLOCK
  3804. +#define PROC_FS_21
  3805. +#define NETLINK_SOCK
  3806. +#define NET_21
  3807. +#endif
  3808. +
  3809. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,19)
  3810. +#define net_device_stats enet_statistics
  3811. +#endif
  3812. +
  3813. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  3814. +#define SPINLOCK_23
  3815. +#define NETDEV_23
  3816. +# ifndef CONFIG_IP_ALIAS
  3817. +# define CONFIG_IP_ALIAS
  3818. +# endif
  3819. +#include <linux/socket.h>
  3820. +#include <linux/skbuff.h>
  3821. +#include <linux/netlink.h>
  3822. +# ifdef NETLINK_XFRM
  3823. +# define NETDEV_25
  3824. +# endif
  3825. +#endif
  3826. +
  3827. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,25)
  3828. +#define PROC_FS_2325
  3829. +#undef PROC_FS_21
  3830. +#endif
  3831. +
  3832. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,30)
  3833. +#define PROC_NO_DUMMY
  3834. +#endif
  3835. +
  3836. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,35)
  3837. +#define SKB_COPY_EXPAND
  3838. +#endif
  3839. +
  3840. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,37)
  3841. +#define IP_SELECT_IDENT
  3842. +#endif
  3843. +
  3844. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,50)) && defined(CONFIG_NETFILTER)
  3845. +#define SKB_RESET_NFCT
  3846. +#endif
  3847. +
  3848. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,2)
  3849. +#define IP_SELECT_IDENT_NEW
  3850. +#endif
  3851. +
  3852. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4)
  3853. +#define IPH_is_SKB_PULLED
  3854. +#define SKB_COW_NEW
  3855. +#define PROTO_HANDLER_SINGLE_PARM
  3856. +#define IP_FRAGMENT_LINEARIZE 1
  3857. +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4) */
  3858. +# ifdef REDHAT_BOGOSITY
  3859. +# define IP_SELECT_IDENT_NEW
  3860. +# define IPH_is_SKB_PULLED
  3861. +# define SKB_COW_NEW
  3862. +# define PROTO_HANDLER_SINGLE_PARM
  3863. +# endif /* REDHAT_BOGOSITY */
  3864. +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4) */
  3865. +
  3866. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,9)
  3867. +#define MALLOC_SLAB
  3868. +#define LINUX_KERNEL_HAS_SNPRINTF
  3869. +#endif
  3870. +
  3871. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  3872. +#define HAVE_NETDEV_PRINTK 1
  3873. +#define NET_26
  3874. +#endif
  3875. +
  3876. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
  3877. +#define NEED_INET_PROTOCOL
  3878. +#endif
  3879. +
  3880. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
  3881. +#define HAVE_SOCK_ZAPPED
  3882. +#define NET_26_12_SKALLOC
  3883. +#endif
  3884. +
  3885. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
  3886. +#define HAVE_SOCK_SECURITY
  3887. +/* skb->nf_debug disappared completely in 2.6.13 */
  3888. +#define HAVE_SKB_NF_DEBUG
  3889. +#endif
  3890. +
  3891. +#define SYSCTL_IPSEC_DEFAULT_TTL sysctl_ip_default_ttl
  3892. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
  3893. +/* skb->stamp changed to skb->tstamp in 2.6.14 */
  3894. +#define HAVE_TSTAMP
  3895. +#define HAVE_INET_SK_SPORT
  3896. +#undef SYSCTL_IPSEC_DEFAULT_TTL
  3897. +#define SYSCTL_IPSEC_DEFAULT_TTL IPSEC_DEFAULT_TTL
  3898. +#else
  3899. +#define HAVE_SKB_LIST
  3900. +#endif
  3901. +
  3902. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
  3903. +#define HAVE_NEW_SKB_LINEARIZE
  3904. +#endif
  3905. +
  3906. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  3907. +/* skb->nfmark changed to skb->mark in 2.6.20 */
  3908. +#define nfmark mark
  3909. +#endif
  3910. +
  3911. +#ifdef NET_21
  3912. +# include <linux/in6.h>
  3913. +#else
  3914. + /* old kernel in.h has some IPv6 stuff, but not quite enough */
  3915. +# define s6_addr16 s6_addr
  3916. +# define AF_INET6 10
  3917. +# define uint8_t __u8
  3918. +# define uint16_t __u16
  3919. +# define uint32_t __u32
  3920. +# define uint64_t __u64
  3921. +#endif
  3922. +
  3923. +#ifdef NET_21
  3924. +# define ipsec_kfree_skb(a) kfree_skb(a)
  3925. +#else /* NET_21 */
  3926. +# define ipsec_kfree_skb(a) kfree_skb(a, FREE_WRITE)
  3927. +#endif /* NET_21 */
  3928. +
  3929. +#ifdef NETDEV_23
  3930. +#if 0
  3931. +#ifndef NETDEV_25
  3932. +#define device net_device
  3933. +#endif
  3934. +#endif
  3935. +# define ipsec_dev_get dev_get_by_name
  3936. +# define __ipsec_dev_get __dev_get_by_name
  3937. +# define ipsec_dev_put(x) dev_put(x)
  3938. +# define __ipsec_dev_put(x) __dev_put(x)
  3939. +# define ipsec_dev_hold(x) dev_hold(x)
  3940. +#else /* NETDEV_23 */
  3941. +# define ipsec_dev_get dev_get
  3942. +# define __ipsec_dev_put(x)
  3943. +# define ipsec_dev_put(x)
  3944. +# define ipsec_dev_hold(x)
  3945. +#endif /* NETDEV_23 */
  3946. +
  3947. +#ifndef SPINLOCK
  3948. +# include <linux/bios32.h>
  3949. + /* simulate spin locks and read/write locks */
  3950. + typedef struct {
  3951. + volatile char lock;
  3952. + } spinlock_t;
  3953. +
  3954. + typedef struct {
  3955. + volatile unsigned int lock;
  3956. + } rwlock_t;
  3957. +
  3958. +# define spin_lock_init(x) { (x)->lock = 0;}
  3959. +# define rw_lock_init(x) { (x)->lock = 0; }
  3960. +
  3961. +# define spin_lock(x) { while ((x)->lock) barrier(); (x)->lock=1;}
  3962. +# define spin_lock_irq(x) { cli(); spin_lock(x);}
  3963. +# define spin_lock_irqsave(x,flags) { save_flags(flags); spin_lock_irq(x);}
  3964. +
  3965. +# define spin_unlock(x) { (x)->lock=0;}
  3966. +# define spin_unlock_irq(x) { spin_unlock(x); sti();}
  3967. +# define spin_unlock_irqrestore(x,flags) { spin_unlock(x); restore_flags(flags);}
  3968. +
  3969. +# define read_lock(x) spin_lock(x)
  3970. +# define read_lock_irq(x) spin_lock_irq(x)
  3971. +# define read_lock_irqsave(x,flags) spin_lock_irqsave(x,flags)
  3972. +
  3973. +# define read_unlock(x) spin_unlock(x)
  3974. +# define read_unlock_irq(x) spin_unlock_irq(x)
  3975. +# define read_unlock_irqrestore(x,flags) spin_unlock_irqrestore(x,flags)
  3976. +
  3977. +# define write_lock(x) spin_lock(x)
  3978. +# define write_lock_irq(x) spin_lock_irq(x)
  3979. +# define write_lock_irqsave(x,flags) spin_lock_irqsave(x,flags)
  3980. +
  3981. +# define write_unlock(x) spin_unlock(x)
  3982. +# define write_unlock_irq(x) spin_unlock_irq(x)
  3983. +# define write_unlock_irqrestore(x,flags) spin_unlock_irqrestore(x,flags)
  3984. +#endif /* !SPINLOCK */
  3985. +
  3986. +#ifndef SPINLOCK_23
  3987. +# define spin_lock_bh(x) spin_lock_irq(x)
  3988. +# define spin_unlock_bh(x) spin_unlock_irq(x)
  3989. +
  3990. +# define read_lock_bh(x) read_lock_irq(x)
  3991. +# define read_unlock_bh(x) read_unlock_irq(x)
  3992. +
  3993. +# define write_lock_bh(x) write_lock_irq(x)
  3994. +# define write_unlock_bh(x) write_unlock_irq(x)
  3995. +#endif /* !SPINLOCK_23 */
  3996. +
  3997. +#ifndef HAVE_NETDEV_PRINTK
  3998. +#define netdev_printk(sevlevel, netdev, msglevel, format, arg...) \
  3999. + printk(sevlevel "%s: " format , netdev->name , ## arg)
  4000. +#endif
  4001. +
  4002. +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
  4003. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  4004. +#include "openswan/ipsec_kern24.h"
  4005. +#else
  4006. +#error "kernels before 2.4 are not supported at this time"
  4007. +#endif
  4008. +#endif
  4009. +
  4010. +
  4011. +#endif /* _OPENSWAN_KVERSIONS_H */
  4012. +
  4013. +/*
  4014. + * $Log: ipsec_kversion.h,v $
  4015. + * Revision 1.15.2.11 2007/02/20 03:53:16 paul
  4016. + * Added comment, made layout consistent with other checks.
  4017. + *
  4018. + * Revision 1.15.2.10 2007/02/16 19:08:12 paul
  4019. + * Fix for compiling on 2.6.20 (nfmark is now called mark in sk_buff)
  4020. + *
  4021. + * Revision 1.15.2.9 2006/07/29 05:00:40 paul
  4022. + * Added HAVE_NEW_SKB_LINEARIZE for 2.6.18+ kernels where skb_linearize
  4023. + * only takes 1 argument.
  4024. + *
  4025. + * Revision 1.15.2.8 2006/05/01 14:31:52 mcr
  4026. + * FREESWAN->OPENSWAN in #ifdef.
  4027. + *
  4028. + * Revision 1.15.2.7 2006/01/11 02:02:59 mcr
  4029. + * updated patches and DEFAULT_TTL code to work
  4030. + *
  4031. + * Revision 1.15.2.6 2006/01/03 19:25:02 ken
  4032. + * Remove duplicated #ifdef for TTL fix - bad patch
  4033. + *
  4034. + * Revision 1.15.2.5 2006/01/03 18:06:33 ken
  4035. + * Fix for missing sysctl default ttl
  4036. + *
  4037. + * Revision 1.15.2.4 2005/11/27 21:40:14 paul
  4038. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"
  4039. + * in for klips as module.
  4040. + *
  4041. + * Revision 1.15.2.3 2005/11/22 04:11:52 ken
  4042. + * Backport fixes for 2.6.14 kernels from HEAD
  4043. + *
  4044. + * Revision 1.15.2.2 2005/09/01 01:57:19 paul
  4045. + * michael's fixes for 2.6.13 from head
  4046. + *
  4047. + * Revision 1.15.2.1 2005/08/27 23:13:48 paul
  4048. + * Fix for:
  4049. + * 7 weeks ago: [NET]: Remove unused security member in sk_buff
  4050. + * changeset 4280: 328ea53f5fee
  4051. + * parent 4279: beb0afb0e3f8
  4052. + * author: Thomas Graf <tgraf@suug.ch>
  4053. + * date: Tue Jul 5 21:12:44 2005
  4054. + * files: include/linux/skbuff.h include/linux/tc_ematch/tc_em_meta.h net/core/skbuff.c net/ipv4/ip_output.c net/ipv6/ip6_output.c net/sched/em_meta.c
  4055. + *
  4056. + * This should fix compilation on 2.6.13(rc) kernels
  4057. + *
  4058. + * Revision 1.15 2005/07/19 20:02:15 mcr
  4059. + * sk_alloc() interface change.
  4060. + *
  4061. + * Revision 1.14 2005/07/08 16:20:05 mcr
  4062. + * fix for 2.6.12 disapperance of sk_zapped field -> sock_flags.
  4063. + *
  4064. + * Revision 1.13 2005/05/20 03:19:18 mcr
  4065. + * modifications for use on 2.4.30 kernel, with backported
  4066. + * printk_ratelimit(). all warnings removed.
  4067. + *
  4068. + * Revision 1.12 2005/04/13 22:46:21 mcr
  4069. + * note that KLIPS does not work on Linux 2.0.
  4070. + *
  4071. + * Revision 1.11 2004/09/13 02:22:26 mcr
  4072. + * #define inet_protocol if necessary.
  4073. + *
  4074. + * Revision 1.10 2004/08/03 18:17:15 mcr
  4075. + * in 2.6, use "net_device" instead of #define device->net_device.
  4076. + * this probably breaks 2.0 compiles.
  4077. + *
  4078. + * Revision 1.9 2004/04/05 19:55:05 mcr
  4079. + * Moved from linux/include/freeswan/ipsec_kversion.h,v
  4080. + *
  4081. + * Revision 1.8 2003/12/13 19:10:16 mcr
  4082. + * refactored rcv and xmit code - same as FS 2.05.
  4083. + *
  4084. + * Revision 1.7 2003/07/31 22:48:08 mcr
  4085. + * derive NET25-ness from presence of NETLINK_XFRM macro.
  4086. + *
  4087. + * Revision 1.6 2003/06/24 20:22:32 mcr
  4088. + * added new global: ipsecdevices[] so that we can keep track of
  4089. + * the ipsecX devices. They will be referenced with dev_hold(),
  4090. + * so 2.2 may need this as well.
  4091. + *
  4092. + * Revision 1.5 2003/04/03 17:38:09 rgb
  4093. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  4094. + *
  4095. + * Revision 1.4 2002/04/24 07:36:46 mcr
  4096. + * Moved from ./klips/net/ipsec/ipsec_kversion.h,v
  4097. + *
  4098. + * Revision 1.3 2002/04/12 03:21:17 mcr
  4099. + * three parameter version of ip_select_ident appears first
  4100. + * in 2.4.2 (RH7.1) not 2.4.4.
  4101. + *
  4102. + * Revision 1.2 2002/03/08 21:35:22 rgb
  4103. + * Defined LINUX_KERNEL_HAS_SNPRINTF to shut up compiler warnings after
  4104. + * 2.4.9. (Andreas Piesk).
  4105. + *
  4106. + * Revision 1.1 2002/01/29 02:11:42 mcr
  4107. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  4108. + * updating of IPv6 structures to match latest in6.h version.
  4109. + * removed dead code from freeswan.h that also duplicated kversions.h
  4110. + * code.
  4111. + *
  4112. + *
  4113. + */
  4114. --- /dev/null Tue Mar 11 13:02:56 2003
  4115. +++ linux/include/openswan/ipsec_life.h Mon Feb 9 13:51:03 2004
  4116. @@ -0,0 +1,112 @@
  4117. +/*
  4118. + * Definitions relevant to IPSEC lifetimes
  4119. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4120. + * and Michael Richardson <mcr@freeswan.org>
  4121. + *
  4122. + * This program is free software; you can redistribute it and/or modify it
  4123. + * under the terms of the GNU General Public License as published by the
  4124. + * Free Software Foundation; either version 2 of the License, or (at your
  4125. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4126. + *
  4127. + * This program is distributed in the hope that it will be useful, but
  4128. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4129. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4130. + * for more details.
  4131. + *
  4132. + * RCSID $Id: ipsec_life.h,v 1.4 2004/04/05 19:55:05 mcr Exp $
  4133. + *
  4134. + * This file derived from ipsec_xform.h on 2001/9/18 by mcr.
  4135. + *
  4136. + */
  4137. +
  4138. +/*
  4139. + * This file describes the book keeping fields for the
  4140. + * IPsec Security Association Structure. ("ipsec_sa")
  4141. + *
  4142. + * This structure is never allocated directly by kernel code,
  4143. + * (it is always a static/auto or is part of a structure)
  4144. + * so it does not have a reference count.
  4145. + *
  4146. + */
  4147. +
  4148. +#ifndef _IPSEC_LIFE_H_
  4149. +
  4150. +/*
  4151. + * _count is total count.
  4152. + * _hard is hard limit (kill SA after this number)
  4153. + * _soft is soft limit (try to renew SA after this number)
  4154. + * _last is used in some special cases.
  4155. + *
  4156. + */
  4157. +
  4158. +struct ipsec_lifetime64
  4159. +{
  4160. + __u64 ipl_count;
  4161. + __u64 ipl_soft;
  4162. + __u64 ipl_hard;
  4163. + __u64 ipl_last;
  4164. +};
  4165. +
  4166. +struct ipsec_lifetimes
  4167. +{
  4168. + /* number of bytes processed */
  4169. + struct ipsec_lifetime64 ipl_bytes;
  4170. +
  4171. + /* number of packets processed */
  4172. + struct ipsec_lifetime64 ipl_packets;
  4173. +
  4174. + /* time since SA was added */
  4175. + struct ipsec_lifetime64 ipl_addtime;
  4176. +
  4177. + /* time since SA was first used */
  4178. + struct ipsec_lifetime64 ipl_usetime;
  4179. +
  4180. + /* from rfc2367:
  4181. + * For CURRENT, the number of different connections,
  4182. + * endpoints, or flows that the association has been
  4183. + * allocated towards. For HARD and SOFT, the number of
  4184. + * these the association may be allocated towards
  4185. + * before it expires. The concept of a connection,
  4186. + * flow, or endpoint is system specific.
  4187. + *
  4188. + * mcr(2001-9-18) it is unclear what purpose these serve for FreeSWAN.
  4189. + * They are maintained for PF_KEY compatibility.
  4190. + */
  4191. + struct ipsec_lifetime64 ipl_allocations;
  4192. +};
  4193. +
  4194. +enum ipsec_life_alive {
  4195. + ipsec_life_harddied = -1,
  4196. + ipsec_life_softdied = 0,
  4197. + ipsec_life_okay = 1
  4198. +};
  4199. +
  4200. +enum ipsec_life_type {
  4201. + ipsec_life_timebased = 1,
  4202. + ipsec_life_countbased= 0
  4203. +};
  4204. +
  4205. +#define _IPSEC_LIFE_H_
  4206. +#endif /* _IPSEC_LIFE_H_ */
  4207. +
  4208. +
  4209. +/*
  4210. + * $Log: ipsec_life.h,v $
  4211. + * Revision 1.4 2004/04/05 19:55:05 mcr
  4212. + * Moved from linux/include/freeswan/ipsec_life.h,v
  4213. + *
  4214. + * Revision 1.3 2002/04/24 07:36:46 mcr
  4215. + * Moved from ./klips/net/ipsec/ipsec_life.h,v
  4216. + *
  4217. + * Revision 1.2 2001/11/26 09:16:14 rgb
  4218. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  4219. + *
  4220. + * Revision 1.1.2.1 2001/09/25 02:25:58 mcr
  4221. + * lifetime structure created and common functions created.
  4222. + *
  4223. + *
  4224. + * Local variables:
  4225. + * c-file-style: "linux"
  4226. + * End:
  4227. + *
  4228. + */
  4229. --- /dev/null Tue Mar 11 13:02:56 2003
  4230. +++ linux/include/openswan/ipsec_md5h.h Mon Feb 9 13:51:03 2004
  4231. @@ -0,0 +1,143 @@
  4232. +/*
  4233. + * RCSID $Id: ipsec_md5h.h,v 1.10 2004/09/08 17:21:35 ken Exp $
  4234. + */
  4235. +
  4236. +/*
  4237. + * The rest of this file is Copyright RSA DSI. See the following comments
  4238. + * for the full Copyright notice.
  4239. + */
  4240. +
  4241. +#ifndef _IPSEC_MD5H_H_
  4242. +#define _IPSEC_MD5H_H_
  4243. +
  4244. +/* GLOBAL.H - RSAREF types and constants
  4245. + */
  4246. +
  4247. +/* PROTOTYPES should be set to one if and only if the compiler supports
  4248. + function argument prototyping.
  4249. + The following makes PROTOTYPES default to 0 if it has not already
  4250. + been defined with C compiler flags.
  4251. + */
  4252. +#ifndef PROTOTYPES
  4253. +#define PROTOTYPES 1
  4254. +#endif /* !PROTOTYPES */
  4255. +
  4256. +/* POINTER defines a generic pointer type */
  4257. +typedef __u8 *POINTER;
  4258. +
  4259. +/* UINT2 defines a two byte word */
  4260. +typedef __u16 UINT2;
  4261. +
  4262. +/* UINT4 defines a four byte word */
  4263. +typedef __u32 UINT4;
  4264. +
  4265. +/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  4266. + If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  4267. + returns an empty list.
  4268. + */
  4269. +
  4270. +#if PROTOTYPES
  4271. +#define PROTO_LIST(list) list
  4272. +#else /* PROTOTYPES */
  4273. +#define PROTO_LIST(list) ()
  4274. +#endif /* PROTOTYPES */
  4275. +
  4276. +
  4277. +/* MD5.H - header file for MD5C.C
  4278. + */
  4279. +
  4280. +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  4281. +rights reserved.
  4282. +
  4283. +License to copy and use this software is granted provided that it
  4284. +is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  4285. +Algorithm" in all material mentioning or referencing this software
  4286. +or this function.
  4287. +
  4288. +License is also granted to make and use derivative works provided
  4289. +that such works are identified as "derived from the RSA Data
  4290. +Security, Inc. MD5 Message-Digest Algorithm" in all material
  4291. +mentioning or referencing the derived work.
  4292. +
  4293. +RSA Data Security, Inc. makes no representations concerning either
  4294. +the merchantability of this software or the suitability of this
  4295. +software for any particular purpose. It is provided "as is"
  4296. +without express or implied warranty of any kind.
  4297. +
  4298. +These notices must be retained in any copies of any part of this
  4299. +documentation and/or software.
  4300. + */
  4301. +
  4302. +/* MD5 context. */
  4303. +typedef struct {
  4304. + UINT4 state[4]; /* state (ABCD) */
  4305. + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  4306. + unsigned char buffer[64]; /* input buffer */
  4307. +} MD5_CTX;
  4308. +
  4309. +void osMD5Init PROTO_LIST ((void *));
  4310. +void osMD5Update PROTO_LIST
  4311. + ((void *, unsigned char *, __u32));
  4312. +void osMD5Final PROTO_LIST ((unsigned char [16], void *));
  4313. +
  4314. +#endif /* _IPSEC_MD5H_H_ */
  4315. +
  4316. +/*
  4317. + * $Log: ipsec_md5h.h,v $
  4318. + * Revision 1.10 2004/09/08 17:21:35 ken
  4319. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  4320. + *
  4321. + * Revision 1.9 2004/04/05 19:55:05 mcr
  4322. + * Moved from linux/include/freeswan/ipsec_md5h.h,v
  4323. + *
  4324. + * Revision 1.8 2002/09/10 01:45:09 mcr
  4325. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  4326. + * the function prototypes would match, and could be placed
  4327. + * into a pointer to a function.
  4328. + *
  4329. + * Revision 1.7 2002/04/24 07:36:46 mcr
  4330. + * Moved from ./klips/net/ipsec/ipsec_md5h.h,v
  4331. + *
  4332. + * Revision 1.6 1999/12/13 13:59:13 rgb
  4333. + * Quick fix to argument size to Update bugs.
  4334. + *
  4335. + * Revision 1.5 1999/12/07 18:16:23 rgb
  4336. + * Fixed comments at end of #endif lines.
  4337. + *
  4338. + * Revision 1.4 1999/04/06 04:54:26 rgb
  4339. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  4340. + * patch shell fixes.
  4341. + *
  4342. + * Revision 1.3 1999/01/22 06:19:58 rgb
  4343. + * 64-bit clean-up.
  4344. + *
  4345. + * Revision 1.2 1998/11/30 13:22:54 rgb
  4346. + * Rationalised all the klips kernel file headers. They are much shorter
  4347. + * now and won't conflict under RH5.2.
  4348. + *
  4349. + * Revision 1.1 1998/06/18 21:27:48 henry
  4350. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  4351. + * kernel-build scripts happier in the presence of symlinks
  4352. + *
  4353. + * Revision 1.2 1998/04/23 20:54:03 rgb
  4354. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  4355. + * verified.
  4356. + *
  4357. + * Revision 1.1 1998/04/09 03:04:21 henry
  4358. + * sources moved up from linux/net/ipsec
  4359. + * these two include files modified not to include others except in kernel
  4360. + *
  4361. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  4362. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  4363. + *
  4364. + * Revision 0.4 1997/01/15 01:28:15 ji
  4365. + * No changes.
  4366. + *
  4367. + * Revision 0.3 1996/11/20 14:48:53 ji
  4368. + * Release update only.
  4369. + *
  4370. + * Revision 0.2 1996/11/02 00:18:33 ji
  4371. + * First limited release.
  4372. + *
  4373. + *
  4374. + */
  4375. --- /dev/null Tue Mar 11 13:02:56 2003
  4376. +++ linux/include/openswan/ipsec_param.h Mon Feb 9 13:51:03 2004
  4377. @@ -0,0 +1,387 @@
  4378. +/*
  4379. + * @(#) Openswan tunable paramaters
  4380. + *
  4381. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4382. + * and Michael Richardson <mcr@freeswan.org>
  4383. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  4384. + *
  4385. + * This program is free software; you can redistribute it and/or modify it
  4386. + * under the terms of the GNU General Public License as published by the
  4387. + * Free Software Foundation; either version 2 of the License, or (at your
  4388. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4389. + *
  4390. + * This program is distributed in the hope that it will be useful, but
  4391. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4392. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4393. + * for more details.
  4394. + *
  4395. + * RCSID $Id: ipsec_param.h,v 1.29.6.3 2006/05/01 14:32:31 mcr Exp $
  4396. + *
  4397. + */
  4398. +
  4399. +/*
  4400. + * This file provides a set of #define's which may be tuned by various
  4401. + * people/configurations. It keeps all compile-time tunables in one place.
  4402. + *
  4403. + * This file should be included before all other IPsec kernel-only files.
  4404. + *
  4405. + */
  4406. +
  4407. +#ifndef _IPSEC_PARAM_H_
  4408. +
  4409. +#ifdef __KERNEL__
  4410. +#include "ipsec_kversion.h"
  4411. +
  4412. +/* Set number of ipsecX virtual devices here. */
  4413. +/* This must be < exp(field width of IPSEC_DEV_FORMAT) */
  4414. +/* It must also be reasonable so as not to overload the memory and CPU */
  4415. +/* constraints of the host. */
  4416. +#define IPSEC_NUM_IF 4
  4417. +/* The field width must be < IF_NAM_SIZ - strlen("ipsec") - 1. */
  4418. +/* With "ipsec" being 5 characters, that means 10 is the max field width */
  4419. +/* but machine memory and CPU constraints are not likely to tollerate */
  4420. +/* more than 3 digits. The default is one digit. */
  4421. +/* Update: userland scripts get upset if they can't find "ipsec0", so */
  4422. +/* for now, no "0"-padding should be used (which would have been helpful */
  4423. +/* to make text-searches work */
  4424. +#define IPSEC_DEV_FORMAT "ipsec%d"
  4425. +/* For, say, 500 virtual ipsec devices, I would recommend: */
  4426. +/* #define IPSEC_NUM_IF 500 */
  4427. +/* #define IPSEC_DEV_FORMAT "ipsec%03d" */
  4428. +/* Note that the "interfaces=" line in /etc/ipsec.conf would be, um, challenging. */
  4429. +
  4430. +/* use dynamic ipsecX device allocation */
  4431. +#ifndef CONFIG_KLIPS_DYNDEV
  4432. +#define CONFIG_KLIPS_DYNDEV 1
  4433. +#endif /* CONFIG_KLIPS_DYNDEV */
  4434. +
  4435. +
  4436. +#ifdef CONFIG_KLIPS_BIGGATE
  4437. +# define SADB_HASHMOD 8069
  4438. +#else /* CONFIG_KLIPS_BIGGATE */
  4439. +# define SADB_HASHMOD 257
  4440. +#endif /* CONFIG_KLIPS_BIGGATE */
  4441. +#endif /* __KERNEL__ */
  4442. +
  4443. +/*
  4444. + * This is for the SA reference table. This number is related to the
  4445. + * maximum number of SAs that KLIPS can concurrently deal with, plus enough
  4446. + * space for keeping expired SAs around.
  4447. + *
  4448. + * TABLE_MAX_WIDTH is the number of bits that we will use.
  4449. + * MAIN_TABLE_WIDTH is the number of bits used for the primary index table.
  4450. + *
  4451. + */
  4452. +#ifndef IPSEC_SA_REF_TABLE_IDX_WIDTH
  4453. +# define IPSEC_SA_REF_TABLE_IDX_WIDTH 16
  4454. +#endif
  4455. +
  4456. +#ifndef IPSEC_SA_REF_MAINTABLE_IDX_WIDTH
  4457. +# define IPSEC_SA_REF_MAINTABLE_IDX_WIDTH 4
  4458. +#endif
  4459. +
  4460. +#ifndef IPSEC_SA_REF_FREELIST_NUM_ENTRIES
  4461. +# define IPSEC_SA_REF_FREELIST_NUM_ENTRIES 256
  4462. +#endif
  4463. +
  4464. +#ifndef IPSEC_SA_REF_CODE
  4465. +# define IPSEC_SA_REF_CODE 1
  4466. +#endif
  4467. +
  4468. +#ifdef __KERNEL__
  4469. +/* This is defined for 2.4, but not 2.2.... */
  4470. +#ifndef ARPHRD_VOID
  4471. +# define ARPHRD_VOID 0xFFFF
  4472. +#endif
  4473. +
  4474. +/* always turn on IPIP mode */
  4475. +#ifndef CONFIG_KLIPS_IPIP
  4476. +#define CONFIG_KLIPS_IPIP 1
  4477. +#endif
  4478. +
  4479. +/*
  4480. + * Worry about PROC_FS stuff
  4481. + */
  4482. +#if defined(PROC_FS_2325)
  4483. +/* kernel 2.4 */
  4484. +# define IPSEC_PROC_LAST_ARG ,int *eof,void *data
  4485. +# define IPSEC_PROCFS_DEBUG_NO_STATIC
  4486. +# define IPSEC_PROC_SUBDIRS
  4487. +#else
  4488. +/* kernel <2.4 */
  4489. +# define IPSEC_PROCFS_DEBUG_NO_STATIC DEBUG_NO_STATIC
  4490. +
  4491. +# ifndef PROC_NO_DUMMY
  4492. +# define IPSEC_PROC_LAST_ARG , int dummy
  4493. +# else
  4494. +# define IPSEC_PROC_LAST_ARG
  4495. +# endif /* !PROC_NO_DUMMY */
  4496. +#endif /* PROC_FS_2325 */
  4497. +
  4498. +#if !defined(LINUX_KERNEL_HAS_SNPRINTF)
  4499. +/* GNU CPP specific! */
  4500. +# define snprintf(buf, len, fmt...) sprintf(buf, ##fmt)
  4501. +#endif /* !LINUX_KERNEL_HAS_SNPRINTF */
  4502. +
  4503. +#ifdef SPINLOCK
  4504. +# ifdef SPINLOCK_23
  4505. +# include <linux/spinlock.h> /* *lock* */
  4506. +# else /* SPINLOCK_23 */
  4507. +# include <asm/spinlock.h> /* *lock* */
  4508. +# endif /* SPINLOCK_23 */
  4509. +#endif /* SPINLOCK */
  4510. +
  4511. +#ifndef KLIPS_FIXES_DES_PARITY
  4512. +# define KLIPS_FIXES_DES_PARITY 1
  4513. +#endif /* !KLIPS_FIXES_DES_PARITY */
  4514. +
  4515. +/* we don't really want to print these unless there are really big problems */
  4516. +#ifndef KLIPS_DIVULGE_CYPHER_KEY
  4517. +# define KLIPS_DIVULGE_CYPHER_KEY 0
  4518. +#endif /* !KLIPS_DIVULGE_CYPHER_KEY */
  4519. +
  4520. +#ifndef KLIPS_DIVULGE_HMAC_KEY
  4521. +# define KLIPS_DIVULGE_HMAC_KEY 0
  4522. +#endif /* !KLIPS_DIVULGE_HMAC_KEY */
  4523. +
  4524. +#ifndef IPSEC_DISALLOW_IPOPTIONS
  4525. +# define IPSEC_DISALLOW_IPOPTIONS 1
  4526. +#endif /* !KLIPS_DIVULGE_HMAC_KEY */
  4527. +
  4528. +/* extra toggles for regression testing */
  4529. +#ifdef CONFIG_KLIPS_REGRESS
  4530. +
  4531. +/*
  4532. + * should pfkey_acquire() become 100% lossy?
  4533. + *
  4534. + */
  4535. +extern int sysctl_ipsec_regress_pfkey_lossage;
  4536. +#ifndef KLIPS_PFKEY_ACQUIRE_LOSSAGE
  4537. +# ifdef CONFIG_KLIPS_PFKEY_ACQUIRE_LOSSAGE
  4538. +# define KLIPS_PFKEY_ACQUIRE_LOSSAGE 100
  4539. +# endif /* CONFIG_KLIPS_PFKEY_ACQUIRE_LOSSAGE */
  4540. +#else
  4541. +#define KLIPS_PFKEY_ACQUIRE_LOSSAGE 0
  4542. +#endif /* KLIPS_PFKEY_ACQUIRE_LOSSAGE */
  4543. +
  4544. +#else /* CONFIG_KLIPS_REGRESS */
  4545. +#define KLIPS_PFKEY_ACQUIRE_LOSSAGE 0
  4546. +
  4547. +#endif /* CONFIG_KLIPS_REGRESS */
  4548. +
  4549. +
  4550. +/*
  4551. + * debugging routines.
  4552. + */
  4553. +#define KLIPS_ERROR(flag, format, args...) if(printk_ratelimit() || flag) printk(KERN_ERR "KLIPS " format, ## args)
  4554. +#ifdef CONFIG_KLIPS_DEBUG
  4555. +extern void ipsec_print_ip(struct iphdr *ip);
  4556. +
  4557. + #define KLIPS_PRINT(flag, format, args...) \
  4558. + ((flag) ? printk(KERN_INFO format , ## args) : 0)
  4559. + #define KLIPS_PRINTMORE(flag, format, args...) \
  4560. + ((flag) ? printk(format , ## args) : 0)
  4561. + #define KLIPS_IP_PRINT(flag, ip) \
  4562. + ((flag) ? ipsec_print_ip(ip) : 0)
  4563. +#else /* CONFIG_KLIPS_DEBUG */
  4564. + #define KLIPS_PRINT(flag, format, args...) do ; while(0)
  4565. + #define KLIPS_PRINTMORE(flag, format, args...) do ; while(0)
  4566. + #define KLIPS_IP_PRINT(flag, ip) do ; while(0)
  4567. +#endif /* CONFIG_KLIPS_DEBUG */
  4568. +
  4569. +
  4570. +/*
  4571. + * Stupid kernel API differences in APIs. Not only do some
  4572. + * kernels not have ip_select_ident, but some have differing APIs,
  4573. + * and SuSE has one with one parameter, but no way of checking to
  4574. + * see what is really what.
  4575. + */
  4576. +
  4577. +#ifdef SUSE_LINUX_2_4_19_IS_STUPID
  4578. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph)
  4579. +#else
  4580. +
  4581. +/* simplest case, nothing */
  4582. +#if !defined(IP_SELECT_IDENT)
  4583. +#define KLIPS_IP_SELECT_IDENT(iph, skb) do { iph->id = htons(ip_id_count++); } while(0)
  4584. +#endif
  4585. +
  4586. +/* kernels > 2.3.37-ish */
  4587. +#if defined(IP_SELECT_IDENT) && !defined(IP_SELECT_IDENT_NEW)
  4588. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst)
  4589. +#endif
  4590. +
  4591. +/* kernels > 2.4.2 */
  4592. +#if defined(IP_SELECT_IDENT) && defined(IP_SELECT_IDENT_NEW)
  4593. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst, NULL)
  4594. +#endif
  4595. +
  4596. +#endif /* SUSE_LINUX_2_4_19_IS_STUPID */
  4597. +
  4598. +/*
  4599. + * make klips fail test:east-espiv-01.
  4600. + * exploit is at testing/attacks/espiv
  4601. + *
  4602. + */
  4603. +#define KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK 0
  4604. +
  4605. +
  4606. +/* IP_FRAGMENT_LINEARIZE is set in freeswan.h if Kernel > 2.4.4 */
  4607. +#ifndef IP_FRAGMENT_LINEARIZE
  4608. +# define IP_FRAGMENT_LINEARIZE 0
  4609. +#endif /* IP_FRAGMENT_LINEARIZE */
  4610. +#endif /* __KERNEL__ */
  4611. +
  4612. +#ifdef NEED_INET_PROTOCOL
  4613. +#define inet_protocol net_protocol
  4614. +#endif
  4615. +
  4616. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && CONFIG_IPSEC_NAT_TRAVERSAL
  4617. +#define NAT_TRAVERSAL 1
  4618. +#else
  4619. +/* let people either #undef, or #define = 0 it */
  4620. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  4621. +#undef CONFIG_IPSEC_NAT_TRAVERSAL
  4622. +#endif
  4623. +#endif
  4624. +
  4625. +#ifndef IPSEC_DEFAULT_TTL
  4626. +#define IPSEC_DEFAULT_TTL 64
  4627. +#endif
  4628. +
  4629. +#define _IPSEC_PARAM_H_
  4630. +#endif /* _IPSEC_PARAM_H_ */
  4631. +
  4632. +/*
  4633. + * $Log: ipsec_param.h,v $
  4634. + * Revision 1.29.6.3 2006/05/01 14:32:31 mcr
  4635. + * added KLIPS_ERROR and make sure that things work without CONFIG_KLIPS_REGRESS.
  4636. + *
  4637. + * Revision 1.29.6.2 2005/11/27 21:40:14 paul
  4638. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"
  4639. + * in for klips as module.
  4640. + *
  4641. + * Revision 1.29.6.1 2005/08/12 16:24:18 ken
  4642. + * Pull in NAT-T compile logic from HEAD
  4643. + *
  4644. + * Revision 1.29 2005/01/26 00:50:35 mcr
  4645. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  4646. + * and make sure that NAT_TRAVERSAL is set as well to match
  4647. + * userspace compiles of code.
  4648. + *
  4649. + * Revision 1.28 2004/09/13 15:50:15 mcr
  4650. + * spell NEED_INET properly, not NET_INET.
  4651. + *
  4652. + * Revision 1.27 2004/09/13 02:21:45 mcr
  4653. + * always turn on IPIP mode.
  4654. + * #define inet_protocol if necessary.
  4655. + *
  4656. + * Revision 1.26 2004/08/17 03:25:43 mcr
  4657. + * freeswan->openswan.
  4658. + *
  4659. + * Revision 1.25 2004/07/10 19:08:41 mcr
  4660. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  4661. + *
  4662. + * Revision 1.24 2004/04/05 19:55:06 mcr
  4663. + * Moved from linux/include/freeswan/ipsec_param.h,v
  4664. + *
  4665. + * Revision 1.23 2003/12/13 19:10:16 mcr
  4666. + * refactored rcv and xmit code - same as FS 2.05.
  4667. + *
  4668. + * Revision 1.22 2003/10/31 02:27:05 mcr
  4669. + * pulled up port-selector patches and sa_id elimination.
  4670. + *
  4671. + * Revision 1.21.4.1 2003/10/29 01:10:19 mcr
  4672. + * elimited "struct sa_id"
  4673. + *
  4674. + * Revision 1.21 2003/04/03 17:38:18 rgb
  4675. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  4676. + * Change indentation for readability.
  4677. + *
  4678. + * Revision 1.20 2003/03/14 08:09:26 rgb
  4679. + * Fixed up CONFIG_IPSEC_DYNDEV definitions.
  4680. + *
  4681. + * Revision 1.19 2003/01/30 02:31:43 rgb
  4682. + *
  4683. + * Rename SAref table macro names for clarity.
  4684. + *
  4685. + * Revision 1.18 2002/09/30 19:06:26 rgb
  4686. + * Reduce default table to 16 bits width.
  4687. + *
  4688. + * Revision 1.17 2002/09/20 15:40:29 rgb
  4689. + * Define switch to activate new SAref code.
  4690. + * Prefix macros with "IPSEC_".
  4691. + * Rework saref freelist.
  4692. + * Restrict some bits to kernel context for use to klips utils.
  4693. + *
  4694. + * Revision 1.16 2002/09/20 05:00:31 rgb
  4695. + * Define switch to divulge hmac keys for debugging.
  4696. + * Added IPOPTIONS switch.
  4697. + *
  4698. + * Revision 1.15 2002/09/19 02:34:24 mcr
  4699. + * define IPSEC_PROC_SUBDIRS if we are 2.4, and use that in ipsec_proc.c
  4700. + * to decide if we are to create /proc/net/ipsec/.
  4701. + *
  4702. + * Revision 1.14 2002/08/30 01:20:54 mcr
  4703. + * reorganized 2.0/2.2/2.4 procfs support macro so match
  4704. + * 2.4 values/typedefs.
  4705. + *
  4706. + * Revision 1.13 2002/07/28 22:03:28 mcr
  4707. + * added some documentation to SA_REF_*
  4708. + * turned on fix for ESPIV attack, now that we have the attack code.
  4709. + *
  4710. + * Revision 1.12 2002/07/26 08:48:31 rgb
  4711. + * Added SA ref table code.
  4712. + *
  4713. + * Revision 1.11 2002/07/23 02:57:45 rgb
  4714. + * Define ARPHRD_VOID for < 2.4 kernels.
  4715. + *
  4716. + * Revision 1.10 2002/05/27 21:37:28 rgb
  4717. + * Set the defaults sanely for those adventurous enough to try more than 1
  4718. + * digit of ipsec devices.
  4719. + *
  4720. + * Revision 1.9 2002/05/27 18:56:07 rgb
  4721. + * Convert to dynamic ipsec device allocation.
  4722. + *
  4723. + * Revision 1.8 2002/04/24 07:36:47 mcr
  4724. + * Moved from ./klips/net/ipsec/ipsec_param.h,v
  4725. + *
  4726. + * Revision 1.7 2002/04/20 00:12:25 rgb
  4727. + * Added esp IV CBC attack fix, disabled.
  4728. + *
  4729. + * Revision 1.6 2002/01/29 02:11:42 mcr
  4730. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  4731. + * updating of IPv6 structures to match latest in6.h version.
  4732. + * removed dead code from freeswan.h that also duplicated kversions.h
  4733. + * code.
  4734. + *
  4735. + * Revision 1.5 2002/01/28 19:22:01 mcr
  4736. + * by default, turn off LINEARIZE option
  4737. + * (let kversions.h turn it on)
  4738. + *
  4739. + * Revision 1.4 2002/01/20 20:19:36 mcr
  4740. + * renamed option to IP_FRAGMENT_LINEARIZE.
  4741. + *
  4742. + * Revision 1.3 2002/01/12 02:57:25 mcr
  4743. + * first regression test causes acquire messages to be lost
  4744. + * 100% of the time. This is to help testing of pluto.
  4745. + *
  4746. + * Revision 1.2 2001/11/26 09:16:14 rgb
  4747. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  4748. + *
  4749. + * Revision 1.1.2.3 2001/10/23 04:40:16 mcr
  4750. + * added #define for DIVULGING session keys in debug output.
  4751. + *
  4752. + * Revision 1.1.2.2 2001/10/22 20:53:25 mcr
  4753. + * added a define to control forcing of DES parity.
  4754. + *
  4755. + * Revision 1.1.2.1 2001/09/25 02:20:19 mcr
  4756. + * many common kernel configuration questions centralized.
  4757. + * more things remain that should be moved from freeswan.h.
  4758. + *
  4759. + *
  4760. + * Local variables:
  4761. + * c-file-style: "linux"
  4762. + * End:
  4763. + *
  4764. + */
  4765. --- /dev/null Tue Mar 11 13:02:56 2003
  4766. +++ linux/include/openswan/ipsec_policy.h Mon Feb 9 13:51:03 2004
  4767. @@ -0,0 +1,217 @@
  4768. +#ifndef _IPSEC_POLICY_H
  4769. +/*
  4770. + * policy interface file between pluto and applications
  4771. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  4772. + *
  4773. + * This library is free software; you can redistribute it and/or modify it
  4774. + * under the terms of the GNU Library General Public License as published by
  4775. + * the Free Software Foundation; either version 2 of the License, or (at your
  4776. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  4777. + *
  4778. + * This library is distributed in the hope that it will be useful, but
  4779. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4780. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  4781. + * License for more details.
  4782. + *
  4783. + * RCSID $Id: ipsec_policy.h,v 1.7.6.1 2005/07/26 01:53:07 ken Exp $
  4784. + */
  4785. +#define _IPSEC_POLICY_H /* seen it, no need to see it again */
  4786. +
  4787. +
  4788. +/*
  4789. + * this file defines an interface between an application (or rather an
  4790. + * application library) and a key/policy daemon. It provides for inquiries
  4791. + * as to the current state of a connected socket, as well as for general
  4792. + * questions.
  4793. + *
  4794. + * In general, the interface is defined as a series of functional interfaces,
  4795. + * and the policy messages should be internal. However, because this is in
  4796. + * fact an ABI between pieces of the system that may get compiled and revised
  4797. + * seperately, this ABI must be public and revision controlled.
  4798. + *
  4799. + * It is expected that the daemon will always support previous versions.
  4800. + */
  4801. +
  4802. +#define IPSEC_POLICY_MSG_REVISION (unsigned)200305061
  4803. +
  4804. +enum ipsec_policy_command {
  4805. + IPSEC_CMD_QUERY_FD = 1,
  4806. + IPSEC_CMD_QUERY_HOSTPAIR = 2,
  4807. + IPSEC_CMD_QUERY_DSTONLY = 3,
  4808. +};
  4809. +
  4810. +struct ipsec_policy_msg_head {
  4811. + u_int32_t ipm_version;
  4812. + u_int32_t ipm_msg_len;
  4813. + u_int32_t ipm_msg_type;
  4814. + u_int32_t ipm_msg_seq;
  4815. +};
  4816. +
  4817. +enum ipsec_privacy_quality {
  4818. + IPSEC_PRIVACY_NONE = 0,
  4819. + IPSEC_PRIVACY_INTEGRAL = 4, /* not private at all. AH-like */
  4820. + IPSEC_PRIVACY_UNKNOWN = 8, /* something is claimed, but details unavail */
  4821. + IPSEC_PRIVACY_ROT13 = 12, /* trivially breakable, i.e. 1DES */
  4822. + IPSEC_PRIVACY_GAK = 16, /* known eavesdroppers */
  4823. + IPSEC_PRIVACY_PRIVATE = 32, /* secure for at least a decade */
  4824. + IPSEC_PRIVACY_STRONG = 64, /* ridiculously secure */
  4825. + IPSEC_PRIVACY_TORTOISE = 192, /* even stronger, but very slow */
  4826. + IPSEC_PRIVACY_OTP = 224, /* some kind of *true* one time pad */
  4827. +};
  4828. +
  4829. +enum ipsec_bandwidth_quality {
  4830. + IPSEC_QOS_UNKNOWN = 0, /* unknown bandwidth */
  4831. + IPSEC_QOS_INTERACTIVE = 16, /* reasonably moderate jitter, moderate fast.
  4832. + Good enough for telnet/ssh. */
  4833. + IPSEC_QOS_VOIP = 32, /* faster crypto, predicable jitter */
  4834. + IPSEC_QOS_FTP = 64, /* higher throughput crypto, perhaps hardware
  4835. + offloaded, but latency/jitter may be bad */
  4836. + IPSEC_QOS_WIRESPEED = 128, /* expect to be able to fill your pipe */
  4837. +};
  4838. +
  4839. +/* moved from programs/pluto/constants.h */
  4840. +/* IPsec AH transform values
  4841. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.3
  4842. + * and in http://www.iana.org/assignments/isakmp-registry
  4843. + */
  4844. +enum ipsec_authentication_algo {
  4845. + AH_MD5=2,
  4846. + AH_SHA=3,
  4847. + AH_DES=4,
  4848. + AH_SHA2_256=5,
  4849. + AH_SHA2_384=6,
  4850. + AH_SHA2_512=7
  4851. +};
  4852. +
  4853. +/* IPsec ESP transform values
  4854. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.4
  4855. + * and from http://www.iana.org/assignments/isakmp-registry
  4856. + */
  4857. +
  4858. +enum ipsec_cipher_algo {
  4859. + ESP_reserved=0,
  4860. + ESP_DES_IV64=1,
  4861. + ESP_DES=2,
  4862. + ESP_3DES=3,
  4863. + ESP_RC5=4,
  4864. + ESP_IDEA=5,
  4865. + ESP_CAST=6,
  4866. + ESP_BLOWFISH=7,
  4867. + ESP_3IDEA=8,
  4868. + ESP_DES_IV32=9,
  4869. + ESP_RC4=10,
  4870. + ESP_NULL=11,
  4871. + ESP_AES=12, /* 128 bit AES */
  4872. +};
  4873. +
  4874. +/* IPCOMP transform values
  4875. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.5
  4876. + */
  4877. +
  4878. +enum ipsec_comp_algo {
  4879. + IPCOMP_OUI= 1,
  4880. + IPCOMP_DEFLATE= 2,
  4881. + IPCOMP_LZS= 3,
  4882. + IPCOMP_V42BIS= 4
  4883. +};
  4884. +
  4885. +/* Identification type values
  4886. + * RFC 2407 The Internet IP security Domain of Interpretation for ISAKMP 4.6.2.1
  4887. + */
  4888. +
  4889. +enum ipsec_id_type {
  4890. + ID_IMPOSSIBLE= (-2), /* private to Pluto */
  4891. + ID_MYID= (-1), /* private to Pluto */
  4892. + ID_NONE= 0, /* private to Pluto */
  4893. + ID_IPV4_ADDR= 1,
  4894. + ID_FQDN= 2,
  4895. + ID_USER_FQDN= 3,
  4896. + ID_IPV4_ADDR_SUBNET= 4,
  4897. + ID_IPV6_ADDR= 5,
  4898. + ID_IPV6_ADDR_SUBNET= 6,
  4899. + ID_IPV4_ADDR_RANGE= 7,
  4900. + ID_IPV6_ADDR_RANGE= 8,
  4901. + ID_DER_ASN1_DN= 9,
  4902. + ID_DER_ASN1_GN= 10,
  4903. + ID_KEY_ID= 11
  4904. +};
  4905. +
  4906. +/* Certificate type values
  4907. + * RFC 2408 ISAKMP, chapter 3.9
  4908. + */
  4909. +enum ipsec_cert_type {
  4910. + CERT_NONE= 0, /* none, or guess from file contents */
  4911. + CERT_PKCS7_WRAPPED_X509= 1, /* self-signed certificate from disk */
  4912. + CERT_PGP= 2,
  4913. + CERT_DNS_SIGNED_KEY= 3, /* KEY RR from DNS */
  4914. + CERT_X509_SIGNATURE= 4,
  4915. + CERT_X509_KEY_EXCHANGE= 5,
  4916. + CERT_KERBEROS_TOKENS= 6,
  4917. + CERT_CRL= 7,
  4918. + CERT_ARL= 8,
  4919. + CERT_SPKI= 9,
  4920. + CERT_X509_ATTRIBUTE= 10,
  4921. + CERT_RAW_RSA= 11, /* raw RSA from config file */
  4922. +};
  4923. +
  4924. +/* a SIG record in ASCII */
  4925. +struct ipsec_dns_sig {
  4926. + char fqdn[256];
  4927. + char dns_sig[768]; /* empty string if not signed */
  4928. +};
  4929. +
  4930. +struct ipsec_raw_key {
  4931. + char id_name[256];
  4932. + char fs_keyid[8];
  4933. +};
  4934. +
  4935. +struct ipsec_identity {
  4936. + enum ipsec_id_type ii_type;
  4937. + enum ipsec_cert_type ii_format;
  4938. + union {
  4939. + struct ipsec_dns_sig ipsec_dns_signed;
  4940. + /* some thing for PGP */
  4941. + /* some thing for PKIX */
  4942. + struct ipsec_raw_key ipsec_raw_key;
  4943. + } ii_credential;
  4944. +};
  4945. +
  4946. +#define IPSEC_MAX_CREDENTIALS 32
  4947. +
  4948. +struct ipsec_policy_cmd_query {
  4949. + struct ipsec_policy_msg_head head;
  4950. +
  4951. + /* Query section */
  4952. + ip_address query_local; /* us */
  4953. + ip_address query_remote; /* them */
  4954. + u_int8_t proto; /* TCP, ICMP, etc. */
  4955. + u_short src_port, dst_port;
  4956. +
  4957. + /* Answer section */
  4958. + enum ipsec_privacy_quality strength;
  4959. + enum ipsec_bandwidth_quality bandwidth;
  4960. + enum ipsec_authentication_algo auth_detail;
  4961. + enum ipsec_cipher_algo esp_detail;
  4962. + enum ipsec_comp_algo comp_detail;
  4963. +
  4964. + int credential_count;
  4965. +
  4966. + struct ipsec_identity credentials[IPSEC_MAX_CREDENTIALS];
  4967. +};
  4968. +
  4969. +#define IPSEC_POLICY_SOCKET "/var/run/pluto/pluto.info"
  4970. +
  4971. +/* prototypes */
  4972. +extern err_t ipsec_policy_lookup(int fd, struct ipsec_policy_cmd_query *result);
  4973. +extern err_t ipsec_policy_init(void);
  4974. +extern err_t ipsec_policy_final(void);
  4975. +extern err_t ipsec_policy_readmsg(int policysock,
  4976. + unsigned char *buf, size_t buflen);
  4977. +extern err_t ipsec_policy_sendrecv(unsigned char *buf, size_t buflen);
  4978. +extern err_t ipsec_policy_cgilookup(struct ipsec_policy_cmd_query *result);
  4979. +
  4980. +
  4981. +extern const char *ipsec_policy_version_code(void);
  4982. +extern const char *ipsec_policy_version_string(void);
  4983. +
  4984. +#endif /* _IPSEC_POLICY_H */
  4985. --- /dev/null Tue Mar 11 13:02:56 2003
  4986. +++ linux/include/openswan/ipsec_proto.h Mon Feb 9 13:51:03 2004
  4987. @@ -0,0 +1,199 @@
  4988. +/*
  4989. + * @(#) prototypes for FreeSWAN functions
  4990. + *
  4991. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4992. + * and Michael Richardson <mcr@freeswan.org>
  4993. + *
  4994. + * This program is free software; you can redistribute it and/or modify it
  4995. + * under the terms of the GNU General Public License as published by the
  4996. + * Free Software Foundation; either version 2 of the License, or (at your
  4997. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4998. + *
  4999. + * This program is distributed in the hope that it will be useful, but
  5000. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5001. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5002. + * for more details.
  5003. + *
  5004. + * RCSID $Id: ipsec_proto.h,v 1.14 2005/04/29 04:50:03 mcr Exp $
  5005. + *
  5006. + */
  5007. +
  5008. +#ifndef _IPSEC_PROTO_H_
  5009. +
  5010. +#include "ipsec_param.h"
  5011. +
  5012. +/*
  5013. + * This file is a kernel only file that declares prototypes for
  5014. + * all intra-module function calls and global data structures.
  5015. + *
  5016. + * Include this file last.
  5017. + *
  5018. + */
  5019. +
  5020. +/* forward references */
  5021. +enum ipsec_direction;
  5022. +enum ipsec_life_type;
  5023. +struct ipsec_lifetime64;
  5024. +struct ident;
  5025. +struct sockaddr_encap;
  5026. +struct ipsec_sa;
  5027. +
  5028. +/* ipsec_init.c */
  5029. +extern struct prng ipsec_prng;
  5030. +
  5031. +/* ipsec_sa.c */
  5032. +extern struct ipsec_sa *ipsec_sadb_hash[SADB_HASHMOD];
  5033. +extern spinlock_t tdb_lock;
  5034. +extern int ipsec_sadb_init(void);
  5035. +extern int ipsec_sadb_cleanup(__u8);
  5036. +
  5037. +extern struct ipsec_sa *ipsec_sa_alloc(int*error);
  5038. +
  5039. +
  5040. +extern struct ipsec_sa *ipsec_sa_getbyid(ip_said *);
  5041. +extern int ipsec_sa_put(struct ipsec_sa *);
  5042. +extern /* void */ int ipsec_sa_del(struct ipsec_sa *);
  5043. +extern /* void */ int ipsec_sa_delchain(struct ipsec_sa *);
  5044. +extern /* void */ int ipsec_sa_add(struct ipsec_sa *);
  5045. +
  5046. +extern int ipsec_sa_init(struct ipsec_sa *ipsp);
  5047. +extern int ipsec_sa_wipe(struct ipsec_sa *ipsp);
  5048. +
  5049. +/* debug declarations */
  5050. +
  5051. +/* ipsec_proc.c */
  5052. +extern int ipsec_proc_init(void);
  5053. +extern void ipsec_proc_cleanup(void);
  5054. +
  5055. +/* ipsec_rcv.c */
  5056. +extern int ipsec_rcv(struct sk_buff *skb);
  5057. +extern int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type);
  5058. +
  5059. +/* ipsec_xmit.c */
  5060. +struct ipsec_xmit_state;
  5061. +extern enum ipsec_xmit_value ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs);
  5062. +extern enum ipsec_xmit_value ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs);
  5063. +extern void ipsec_print_ip(struct iphdr *ip);
  5064. +
  5065. +
  5066. +
  5067. +/* ipsec_radij.c */
  5068. +extern int ipsec_makeroute(struct sockaddr_encap *ea,
  5069. + struct sockaddr_encap *em,
  5070. + ip_said said,
  5071. + uint32_t pid,
  5072. + struct sk_buff *skb,
  5073. + struct ident *ident_s,
  5074. + struct ident *ident_d);
  5075. +
  5076. +extern int ipsec_breakroute(struct sockaddr_encap *ea,
  5077. + struct sockaddr_encap *em,
  5078. + struct sk_buff **first,
  5079. + struct sk_buff **last);
  5080. +
  5081. +int ipsec_radijinit(void);
  5082. +int ipsec_cleareroutes(void);
  5083. +int ipsec_radijcleanup(void);
  5084. +
  5085. +/* ipsec_life.c */
  5086. +extern enum ipsec_life_alive ipsec_lifetime_check(struct ipsec_lifetime64 *il64,
  5087. + const char *lifename,
  5088. + const char *saname,
  5089. + enum ipsec_life_type ilt,
  5090. + enum ipsec_direction idir,
  5091. + struct ipsec_sa *ips);
  5092. +
  5093. +
  5094. +extern int ipsec_lifetime_format(char *buffer,
  5095. + int buflen,
  5096. + char *lifename,
  5097. + enum ipsec_life_type timebaselife,
  5098. + struct ipsec_lifetime64 *lifetime);
  5099. +
  5100. +extern void ipsec_lifetime_update_hard(struct ipsec_lifetime64 *lifetime,
  5101. + __u64 newvalue);
  5102. +
  5103. +extern void ipsec_lifetime_update_soft(struct ipsec_lifetime64 *lifetime,
  5104. + __u64 newvalue);
  5105. +
  5106. +/* ipsec_snprintf.c */
  5107. +extern int ipsec_snprintf(char * buf, ssize_t size, const char *fmt, ...);
  5108. +extern void ipsec_dmp_block(char *s, caddr_t bb, int len);
  5109. +
  5110. +
  5111. +/* ipsec_alg.c */
  5112. +extern int ipsec_alg_init(void);
  5113. +
  5114. +
  5115. +#ifdef CONFIG_KLIPS_DEBUG
  5116. +
  5117. +extern int debug_xform;
  5118. +extern int debug_eroute;
  5119. +extern int debug_spi;
  5120. +extern int debug_netlink;
  5121. +
  5122. +#endif /* CONFIG_KLIPS_DEBUG */
  5123. +
  5124. +
  5125. +
  5126. +
  5127. +#define _IPSEC_PROTO_H
  5128. +#endif /* _IPSEC_PROTO_H_ */
  5129. +
  5130. +/*
  5131. + * $Log: ipsec_proto.h,v $
  5132. + * Revision 1.14 2005/04/29 04:50:03 mcr
  5133. + * prototypes for xmit and alg code.
  5134. + *
  5135. + * Revision 1.13 2005/04/17 03:46:07 mcr
  5136. + * added prototypes for ipsec_rcv() routines.
  5137. + *
  5138. + * Revision 1.12 2005/04/14 20:28:37 mcr
  5139. + * added additional prototypes.
  5140. + *
  5141. + * Revision 1.11 2005/04/14 01:16:28 mcr
  5142. + * add prototypes for snprintf.
  5143. + *
  5144. + * Revision 1.10 2005/04/13 22:47:28 mcr
  5145. + * make sure that forward references are available.
  5146. + *
  5147. + * Revision 1.9 2004/07/10 19:08:41 mcr
  5148. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5149. + *
  5150. + * Revision 1.8 2004/04/05 19:55:06 mcr
  5151. + * Moved from linux/include/freeswan/ipsec_proto.h,v
  5152. + *
  5153. + * Revision 1.7 2003/10/31 02:27:05 mcr
  5154. + * pulled up port-selector patches and sa_id elimination.
  5155. + *
  5156. + * Revision 1.6.30.1 2003/10/29 01:10:19 mcr
  5157. + * elimited "struct sa_id"
  5158. + *
  5159. + * Revision 1.6 2002/05/23 07:13:48 rgb
  5160. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  5161. + *
  5162. + * Revision 1.5 2002/05/14 02:36:40 rgb
  5163. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  5164. + * with "put" usage in the kernel.
  5165. + *
  5166. + * Revision 1.4 2002/04/24 07:36:47 mcr
  5167. + * Moved from ./klips/net/ipsec/ipsec_proto.h,v
  5168. + *
  5169. + * Revision 1.3 2002/04/20 00:12:25 rgb
  5170. + * Added esp IV CBC attack fix, disabled.
  5171. + *
  5172. + * Revision 1.2 2001/11/26 09:16:15 rgb
  5173. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5174. + *
  5175. + * Revision 1.1.2.1 2001/09/25 02:21:01 mcr
  5176. + * ipsec_proto.h created to keep prototypes rather than deal with
  5177. + * cyclic dependancies of structures and prototypes in .h files.
  5178. + *
  5179. + *
  5180. + *
  5181. + * Local variables:
  5182. + * c-file-style: "linux"
  5183. + * End:
  5184. + *
  5185. + */
  5186. +
  5187. --- /dev/null Tue Mar 11 13:02:56 2003
  5188. +++ linux/include/openswan/ipsec_radij.h Mon Feb 9 13:51:03 2004
  5189. @@ -0,0 +1,179 @@
  5190. +/*
  5191. + * @(#) Definitions relevant to the IPSEC <> radij tree interfacing
  5192. + * Copyright (C) 1996, 1997 John Ioannidis.
  5193. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  5194. + *
  5195. + * This program is free software; you can redistribute it and/or modify it
  5196. + * under the terms of the GNU General Public License as published by the
  5197. + * Free Software Foundation; either version 2 of the License, or (at your
  5198. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5199. + *
  5200. + * This program is distributed in the hope that it will be useful, but
  5201. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5202. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5203. + * for more details.
  5204. + *
  5205. + * RCSID $Id: ipsec_radij.h,v 1.22 2004/07/10 19:08:41 mcr Exp $
  5206. + */
  5207. +
  5208. +#ifndef _IPSEC_RADIJ_H
  5209. +
  5210. +#include <openswan.h>
  5211. +
  5212. +int ipsec_walk(char *);
  5213. +
  5214. +int ipsec_rj_walker_procprint(struct radij_node *, void *);
  5215. +int ipsec_rj_walker_delete(struct radij_node *, void *);
  5216. +
  5217. +/* This structure is used to pass information between
  5218. + * ipsec_eroute_get_info and ipsec_rj_walker_procprint
  5219. + * (through rj_walktree) and between calls of ipsec_rj_walker_procprint.
  5220. + */
  5221. +struct wsbuf
  5222. +{
  5223. + /* from caller of ipsec_eroute_get_info: */
  5224. + char *const buffer; /* start of buffer provided */
  5225. + const int length; /* length of buffer provided */
  5226. + const off_t offset; /* file position of first character of interest */
  5227. + /* accumulated by ipsec_rj_walker_procprint: */
  5228. + int len; /* number of character filled into buffer */
  5229. + off_t begin; /* file position contained in buffer[0] (<=offset) */
  5230. +};
  5231. +
  5232. +extern struct radij_node_head *rnh;
  5233. +extern spinlock_t eroute_lock;
  5234. +
  5235. +struct eroute * ipsec_findroute(struct sockaddr_encap *);
  5236. +
  5237. +#define O1(x) (int)(((x)>>24)&0xff)
  5238. +#define O2(x) (int)(((x)>>16)&0xff)
  5239. +#define O3(x) (int)(((x)>>8)&0xff)
  5240. +#define O4(x) (int)(((x))&0xff)
  5241. +
  5242. +#ifdef CONFIG_KLIPS_DEBUG
  5243. +extern int debug_radij;
  5244. +void rj_dumptrees(void);
  5245. +
  5246. +#define DB_RJ_DUMPTREES 0x0001
  5247. +#define DB_RJ_FINDROUTE 0x0002
  5248. +#endif /* CONFIG_KLIPS_DEBUG */
  5249. +
  5250. +#define _IPSEC_RADIJ_H
  5251. +#endif
  5252. +
  5253. +/*
  5254. + * $Log: ipsec_radij.h,v $
  5255. + * Revision 1.22 2004/07/10 19:08:41 mcr
  5256. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5257. + *
  5258. + * Revision 1.21 2004/04/29 11:06:42 ken
  5259. + * Last bits from 2.06 procfs updates
  5260. + *
  5261. + * Revision 1.20 2004/04/06 02:49:08 mcr
  5262. + * pullup of algo code from alg-branch.
  5263. + *
  5264. + * Revision 1.19 2004/04/05 19:55:06 mcr
  5265. + * Moved from linux/include/freeswan/ipsec_radij.h,v
  5266. + *
  5267. + * Revision 1.18 2002/04/24 07:36:47 mcr
  5268. + * Moved from ./klips/net/ipsec/ipsec_radij.h,v
  5269. + *
  5270. + * Revision 1.17 2001/11/26 09:23:49 rgb
  5271. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5272. + *
  5273. + * Revision 1.16.2.1 2001/09/25 02:21:17 mcr
  5274. + * ipsec_proto.h created to keep prototypes rather than deal with
  5275. + * cyclic dependancies of structures and prototypes in .h files.
  5276. + *
  5277. + * Revision 1.16 2001/09/15 16:24:04 rgb
  5278. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  5279. + *
  5280. + * Revision 1.15 2001/09/14 16:58:37 rgb
  5281. + * Added support for storing the first and last packets through a HOLD.
  5282. + *
  5283. + * Revision 1.14 2001/09/08 21:13:32 rgb
  5284. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  5285. + *
  5286. + * Revision 1.13 2001/06/14 19:35:09 rgb
  5287. + * Update copyright date.
  5288. + *
  5289. + * Revision 1.12 2001/05/27 06:12:11 rgb
  5290. + * Added structures for pid, packet count and last access time to eroute.
  5291. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  5292. + *
  5293. + * Revision 1.11 2000/09/08 19:12:56 rgb
  5294. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  5295. + *
  5296. + * Revision 1.10 1999/11/17 15:53:39 rgb
  5297. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  5298. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  5299. + * klips/net/ipsec/Makefile.
  5300. + *
  5301. + * Revision 1.9 1999/10/01 00:01:23 rgb
  5302. + * Added eroute structure locking.
  5303. + *
  5304. + * Revision 1.8 1999/04/11 00:28:59 henry
  5305. + * GPL boilerplate
  5306. + *
  5307. + * Revision 1.7 1999/04/06 04:54:26 rgb
  5308. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  5309. + * patch shell fixes.
  5310. + *
  5311. + * Revision 1.6 1999/01/22 06:23:26 rgb
  5312. + * Cruft clean-out.
  5313. + *
  5314. + * Revision 1.5 1998/10/25 02:42:08 rgb
  5315. + * Change return type on ipsec_breakroute and ipsec_makeroute and add an
  5316. + * argument to be able to transmit more infomation about errors.
  5317. + *
  5318. + * Revision 1.4 1998/10/19 14:44:29 rgb
  5319. + * Added inclusion of freeswan.h.
  5320. + * sa_id structure implemented and used: now includes protocol.
  5321. + *
  5322. + * Revision 1.3 1998/07/28 00:03:31 rgb
  5323. + * Comment out temporary inet_nto4u() kluge.
  5324. + *
  5325. + * Revision 1.2 1998/07/14 18:22:00 rgb
  5326. + * Add function to clear the eroute table.
  5327. + *
  5328. + * Revision 1.1 1998/06/18 21:27:49 henry
  5329. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  5330. + * kernel-build scripts happier in the presence of symlinks
  5331. + *
  5332. + * Revision 1.5 1998/05/25 20:30:38 rgb
  5333. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  5334. + *
  5335. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  5336. + * add ipsec_rj_walker_delete.
  5337. + *
  5338. + * Revision 1.4 1998/05/21 13:02:56 rgb
  5339. + * Imported definitions from ipsec_radij.c and radij.c to support /proc 3k
  5340. + * limit fix.
  5341. + *
  5342. + * Revision 1.3 1998/04/21 21:29:09 rgb
  5343. + * Rearrange debug switches to change on the fly debug output from user
  5344. + * space. Only kernel changes checked in at this time. radij.c was also
  5345. + * changed to temporarily remove buggy debugging code in rj_delete causing
  5346. + * an OOPS and hence, netlink device open errors.
  5347. + *
  5348. + * Revision 1.2 1998/04/14 17:30:39 rgb
  5349. + * Fix up compiling errors for radij tree memory reclamation.
  5350. + *
  5351. + * Revision 1.1 1998/04/09 03:06:10 henry
  5352. + * sources moved up from linux/net/ipsec
  5353. + *
  5354. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  5355. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  5356. + *
  5357. + * Revision 0.4 1997/01/15 01:28:15 ji
  5358. + * No changes.
  5359. + *
  5360. + * Revision 0.3 1996/11/20 14:39:04 ji
  5361. + * Minor cleanups.
  5362. + * Rationalized debugging code.
  5363. + *
  5364. + * Revision 0.2 1996/11/02 00:18:33 ji
  5365. + * First limited release.
  5366. + *
  5367. + *
  5368. + */
  5369. --- /dev/null Tue Mar 11 13:02:56 2003
  5370. +++ linux/include/openswan/ipsec_rcv.h Mon Feb 9 13:51:03 2004
  5371. @@ -0,0 +1,199 @@
  5372. +/*
  5373. + *
  5374. + * Copyright (C) 1996, 1997 John Ioannidis.
  5375. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  5376. + *
  5377. + * This program is free software; you can redistribute it and/or modify it
  5378. + * under the terms of the GNU General Public License as published by the
  5379. + * Free Software Foundation; either version 2 of the License, or (at your
  5380. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5381. + *
  5382. + * This program is distributed in the hope that it will be useful, but
  5383. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5384. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5385. + * for more details.
  5386. + *
  5387. + * RCSID $Id: ipsec_rcv.h,v 1.28.2.2 2006/10/06 21:39:26 paul Exp $
  5388. + */
  5389. +
  5390. +#ifndef IPSEC_RCV_H
  5391. +#define IPSEC_RCV_H
  5392. +
  5393. +#include "openswan/ipsec_auth.h"
  5394. +
  5395. +#define DB_RX_PKTRX 0x0001
  5396. +#define DB_RX_PKTRX2 0x0002
  5397. +#define DB_RX_DMP 0x0004
  5398. +#define DB_RX_IPSA 0x0010
  5399. +#define DB_RX_XF 0x0020
  5400. +#define DB_RX_IPAD 0x0040
  5401. +#define DB_RX_INAU 0x0080
  5402. +#define DB_RX_OINFO 0x0100
  5403. +#define DB_RX_OINFO2 0x0200
  5404. +#define DB_RX_OH 0x0400
  5405. +#define DB_RX_REPLAY 0x0800
  5406. +
  5407. +#ifdef __KERNEL__
  5408. +/* struct options; */
  5409. +
  5410. +#define __NO_VERSION__
  5411. +#ifndef AUTOCONF_INCLUDED
  5412. +#include <linux/config.h> /* for CONFIG_IP_FORWARD */
  5413. +#endif
  5414. +#ifdef CONFIG_MODULES
  5415. +#include <linux/module.h>
  5416. +#endif
  5417. +#include <linux/version.h>
  5418. +#include <openswan.h>
  5419. +
  5420. +#define IPSEC_BIRTH_TEMPLATE_MAXLEN 256
  5421. +
  5422. +struct ipsec_birth_reply {
  5423. + int packet_template_len;
  5424. + unsigned char packet_template[IPSEC_BIRTH_TEMPLATE_MAXLEN];
  5425. +};
  5426. +
  5427. +extern struct ipsec_birth_reply ipsec_ipv4_birth_packet;
  5428. +extern struct ipsec_birth_reply ipsec_ipv6_birth_packet;
  5429. +
  5430. +enum ipsec_rcv_value {
  5431. + IPSEC_RCV_LASTPROTO=1,
  5432. + IPSEC_RCV_OK=0,
  5433. + IPSEC_RCV_BADPROTO=-1,
  5434. + IPSEC_RCV_BADLEN=-2,
  5435. + IPSEC_RCV_ESP_BADALG=-3,
  5436. + IPSEC_RCV_3DES_BADBLOCKING=-4,
  5437. + IPSEC_RCV_ESP_DECAPFAIL=-5,
  5438. + IPSEC_RCV_DECAPFAIL=-6,
  5439. + IPSEC_RCV_SAIDNOTFOUND=-7,
  5440. + IPSEC_RCV_IPCOMPALONE=-8,
  5441. + IPSEC_RCV_IPCOMPFAILED=-10,
  5442. + IPSEC_RCV_SAIDNOTLIVE=-11,
  5443. + IPSEC_RCV_FAILEDINBOUND=-12,
  5444. + IPSEC_RCV_LIFETIMEFAILED=-13,
  5445. + IPSEC_RCV_BADAUTH=-14,
  5446. + IPSEC_RCV_REPLAYFAILED=-15,
  5447. + IPSEC_RCV_AUTHFAILED=-16,
  5448. + IPSEC_RCV_REPLAYROLLED=-17,
  5449. + IPSEC_RCV_BAD_DECRYPT=-18
  5450. +};
  5451. +
  5452. +struct ipsec_rcv_state {
  5453. + struct sk_buff *skb;
  5454. + struct net_device_stats *stats;
  5455. + struct iphdr *ipp; /* the IP header */
  5456. + struct ipsec_sa *ipsp; /* current SA being processed */
  5457. + int len; /* length of packet */
  5458. + int ilen; /* length of inner payload (-authlen) */
  5459. + int authlen; /* how big is the auth data at end */
  5460. + int hard_header_len; /* layer 2 size */
  5461. + int iphlen; /* how big is IP header */
  5462. + struct auth_alg *authfuncs;
  5463. + ip_said said;
  5464. + char sa[SATOT_BUF];
  5465. + size_t sa_len;
  5466. + __u8 next_header;
  5467. + __u8 hash[AH_AMAX];
  5468. + char ipsaddr_txt[ADDRTOA_BUF];
  5469. + char ipdaddr_txt[ADDRTOA_BUF];
  5470. + __u8 *octx;
  5471. + __u8 *ictx;
  5472. + int ictx_len;
  5473. + int octx_len;
  5474. + union {
  5475. + struct {
  5476. + struct esphdr *espp;
  5477. + } espstuff;
  5478. + struct {
  5479. + struct ahhdr *ahp;
  5480. + } ahstuff;
  5481. + struct {
  5482. + struct ipcomphdr *compp;
  5483. + } ipcompstuff;
  5484. + } protostuff;
  5485. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  5486. + __u8 natt_type;
  5487. + __u16 natt_sport;
  5488. + __u16 natt_dport;
  5489. + int natt_len;
  5490. +#endif
  5491. +};
  5492. +
  5493. +extern int
  5494. +#ifdef PROTO_HANDLER_SINGLE_PARM
  5495. +ipsec_rcv(struct sk_buff *skb);
  5496. +#else /* PROTO_HANDLER_SINGLE_PARM */
  5497. +ipsec_rcv(struct sk_buff *skb,
  5498. + unsigned short xlen);
  5499. +#endif /* PROTO_HANDLER_SINGLE_PARM */
  5500. +
  5501. +#ifdef CONFIG_KLIPS_DEBUG
  5502. +extern int debug_rcv;
  5503. +#define ipsec_rcv_dmp(_x,_y, _z) if (debug_rcv && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  5504. +#else
  5505. +#define ipsec_rcv_dmp(_x,_y, _z) do {} while(0)
  5506. +#endif /* CONFIG_KLIPS_DEBUG */
  5507. +
  5508. +extern int sysctl_ipsec_inbound_policy_check;
  5509. +#endif /* __KERNEL__ */
  5510. +
  5511. +extern int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type);
  5512. +
  5513. +
  5514. +#endif /* IPSEC_RCV_H */
  5515. +
  5516. +/*
  5517. + * $Log: ipsec_rcv.h,v $
  5518. + * Revision 1.28.2.2 2006/10/06 21:39:26 paul
  5519. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  5520. + * set. This is defined through autoconf.h which is included through the
  5521. + * linux kernel build macros.
  5522. + *
  5523. + * Revision 1.28.2.1 2006/07/10 15:52:20 paul
  5524. + * Fix for bug #642 by Bart Trojanowski
  5525. + *
  5526. + * Revision 1.28 2005/05/11 00:59:45 mcr
  5527. + * do not call debug routines if !defined KLIPS_DEBUG.
  5528. + *
  5529. + * Revision 1.27 2005/04/29 04:59:46 mcr
  5530. + * use ipsec_dmp_block.
  5531. + *
  5532. + * Revision 1.26 2005/04/13 22:48:35 mcr
  5533. + * added comments, and removed some log.
  5534. + * removed Linux 2.0 support.
  5535. + *
  5536. + * Revision 1.25 2005/04/08 18:25:37 mcr
  5537. + * prototype klips26 encap receive function
  5538. + *
  5539. + * Revision 1.24 2004/08/20 21:45:37 mcr
  5540. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  5541. + * be 26sec compatible. But, some defines where changed.
  5542. + *
  5543. + * Revision 1.23 2004/08/03 18:17:40 mcr
  5544. + * in 2.6, use "net_device" instead of #define device->net_device.
  5545. + * this probably breaks 2.0 compiles.
  5546. + *
  5547. + * Revision 1.22 2004/07/10 19:08:41 mcr
  5548. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5549. + *
  5550. + * Revision 1.21 2004/04/06 02:49:08 mcr
  5551. + * pullup of algo code from alg-branch.
  5552. + *
  5553. + * Revision 1.20 2004/04/05 19:55:06 mcr
  5554. + * Moved from linux/include/freeswan/ipsec_rcv.h,v
  5555. + *
  5556. + * Revision 1.19 2003/12/15 18:13:09 mcr
  5557. + * when compiling with NAT traversal, don't assume that the
  5558. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  5559. + * is set.
  5560. + *
  5561. + * history elided 2005-04-12.
  5562. + *
  5563. + * Local Variables:
  5564. + * c-basic-offset:8
  5565. + * c-style:linux
  5566. + * End:
  5567. + *
  5568. + */
  5569. +
  5570. +
  5571. --- /dev/null Tue Mar 11 13:02:56 2003
  5572. +++ linux/include/openswan/ipsec_sa.h Mon Feb 9 13:51:03 2004
  5573. @@ -0,0 +1,355 @@
  5574. +/*
  5575. + * @(#) Definitions of IPsec Security Association (ipsec_sa)
  5576. + *
  5577. + * Copyright (C) 2001, 2002, 2003
  5578. + * Richard Guy Briggs <rgb@freeswan.org>
  5579. + * and Michael Richardson <mcr@freeswan.org>
  5580. + *
  5581. + * This program is free software; you can redistribute it and/or modify it
  5582. + * under the terms of the GNU General Public License as published by the
  5583. + * Free Software Foundation; either version 2 of the License, or (at your
  5584. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5585. + *
  5586. + * This program is distributed in the hope that it will be useful, but
  5587. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5588. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5589. + * for more details.
  5590. + *
  5591. + * RCSID $Id: ipsec_sa.h,v 1.23 2005/05/11 01:18:59 mcr Exp $
  5592. + *
  5593. + * This file derived from ipsec_xform.h on 2001/9/18 by mcr.
  5594. + *
  5595. + */
  5596. +
  5597. +/*
  5598. + * This file describes the IPsec Security Association Structure.
  5599. + *
  5600. + * This structure keeps track of a single transform that may be done
  5601. + * to a set of packets. It can describe applying the transform or
  5602. + * apply the reverse. (e.g. compression vs expansion). However, it
  5603. + * only describes one at a time. To describe both, two structures would
  5604. + * be used, but since the sides of the transform are performed
  5605. + * on different machines typically it is usual to have only one side
  5606. + * of each association.
  5607. + *
  5608. + */
  5609. +
  5610. +#ifndef _IPSEC_SA_H_
  5611. +
  5612. +#ifdef __KERNEL__
  5613. +#include "openswan/ipsec_stats.h"
  5614. +#include "openswan/ipsec_life.h"
  5615. +#include "openswan/ipsec_eroute.h"
  5616. +#endif /* __KERNEL__ */
  5617. +#include "openswan/ipsec_param.h"
  5618. +
  5619. +#include "pfkeyv2.h"
  5620. +
  5621. +
  5622. +/* SAs are held in a table.
  5623. + * Entries in this table are referenced by IPsecSAref_t values.
  5624. + * IPsecSAref_t values are conceptually subscripts. Because
  5625. + * we want to allocate the table piece-meal, the subscripting
  5626. + * is implemented with two levels, a bit like paged virtual memory.
  5627. + * This representation mechanism is known as an Iliffe Vector.
  5628. + *
  5629. + * The Main table (AKA the refTable) consists of 2^IPSEC_SA_REF_MAINTABLE_IDX_WIDTH
  5630. + * pointers to subtables.
  5631. + * Each subtable has 2^IPSEC_SA_REF_SUBTABLE_IDX_WIDTH entries, each of which
  5632. + * is a pointer to an SA.
  5633. + *
  5634. + * An IPsecSAref_t contains either an exceptional value (signified by the
  5635. + * high-order bit being on) or a reference to a table entry. A table entry
  5636. + * reference has the subtable subscript in the low-order
  5637. + * IPSEC_SA_REF_SUBTABLE_IDX_WIDTH bits and the Main table subscript
  5638. + * in the next lowest IPSEC_SA_REF_MAINTABLE_IDX_WIDTH bits.
  5639. + *
  5640. + * The Maintable entry for an IPsecSAref_t x, a pointer to its subtable, is
  5641. + * IPsecSAref2table(x). It is of type struct IPsecSArefSubTable *.
  5642. + *
  5643. + * The pointer to the SA for x is IPsecSAref2SA(x). It is of type
  5644. + * struct ipsec_sa*. The macro definition clearly shows the two-level
  5645. + * access needed to find the SA pointer.
  5646. + *
  5647. + * The Maintable is allocated when IPsec is initialized.
  5648. + * Each subtable is allocated when needed, but the first is allocated
  5649. + * when IPsec is initialized.
  5650. + *
  5651. + * IPsecSAref_t is designed to be smaller than an NFmark so that
  5652. + * they can be stored in NFmarks and still leave a few bits for other
  5653. + * purposes. The spare bits are in the low order of the NFmark
  5654. + * but in the high order of the IPsecSAref_t, so conversion is required.
  5655. + * We pick the upper bits of NFmark on the theory that they are less likely to
  5656. + * interfere with more pedestrian uses of nfmark.
  5657. + */
  5658. +
  5659. +
  5660. +typedef unsigned short int IPsecRefTableUnusedCount;
  5661. +
  5662. +#define IPSEC_SA_REF_TABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH)
  5663. +
  5664. +#ifdef __KERNEL__
  5665. +#if ((IPSEC_SA_REF_TABLE_IDX_WIDTH - (1 + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)) < 0)
  5666. +#error "IPSEC_SA_REF_TABLE_IDX_WIDTH("IPSEC_SA_REF_TABLE_IDX_WIDTH") MUST be < 1 + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH("IPSEC_SA_REF_MAINTABLE_IDX_WIDTH")"
  5667. +#endif
  5668. +
  5669. +#define IPSEC_SA_REF_SUBTABLE_IDX_WIDTH (IPSEC_SA_REF_TABLE_IDX_WIDTH - IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)
  5670. +
  5671. +#define IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)
  5672. +#define IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5673. +
  5674. +#ifdef CONFIG_NETFILTER
  5675. +#define IPSEC_SA_REF_HOST_FIELD(x) ((struct sk_buff*)(x))->nfmark
  5676. +#define IPSEC_SA_REF_HOST_FIELD_TYPE typeof(IPSEC_SA_REF_HOST_FIELD(NULL))
  5677. +#else /* CONFIG_NETFILTER */
  5678. +/* just make it work for now, it doesn't matter, since there is no nfmark */
  5679. +#define IPSEC_SA_REF_HOST_FIELD_TYPE unsigned long
  5680. +#endif /* CONFIG_NETFILTER */
  5681. +#define IPSEC_SA_REF_HOST_FIELD_WIDTH (8 * sizeof(IPSEC_SA_REF_HOST_FIELD_TYPE))
  5682. +#define IPSEC_SA_REF_FIELD_WIDTH (8 * sizeof(IPsecSAref_t))
  5683. +
  5684. +#define IPSEC_SA_REF_MASK (IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  5685. +#define IPSEC_SA_REF_TABLE_MASK ((IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)) << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5686. +#define IPSEC_SA_REF_ENTRY_MASK (IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_SUBTABLE_IDX_WIDTH))
  5687. +
  5688. +#define IPsecSAref2table(x) (((x) & IPSEC_SA_REF_TABLE_MASK) >> IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5689. +#define IPsecSAref2entry(x) ((x) & IPSEC_SA_REF_ENTRY_MASK)
  5690. +#define IPsecSArefBuild(x,y) (((x) << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH) + (y))
  5691. +
  5692. +#define IPsecSAref2SA(x) (ipsec_sadb.refTable[IPsecSAref2table(x)]->entry[IPsecSAref2entry(x)])
  5693. +#define IPsecSA2SAref(x) ((x)->ips_ref)
  5694. +
  5695. +#define EMT_INBOUND 0x01 /* SA direction, 1=inbound */
  5696. +
  5697. +/* 'struct ipsec_sa' should be 64bit aligned when allocated. */
  5698. +struct ipsec_sa
  5699. +{
  5700. + IPsecSAref_t ips_ref; /* reference table entry number */
  5701. + atomic_t ips_refcount; /* reference count for this struct */
  5702. + struct ipsec_sa *ips_hnext; /* next in hash chain */
  5703. + struct ipsec_sa *ips_inext; /* pointer to next xform */
  5704. + struct ipsec_sa *ips_onext; /* pointer to prev xform */
  5705. +
  5706. + struct ifnet *ips_rcvif; /* related rcv encap interface */
  5707. +
  5708. + ip_said ips_said; /* SA ID */
  5709. +
  5710. + __u32 ips_seq; /* seq num of msg that initiated this SA */
  5711. + __u32 ips_pid; /* PID of process that initiated this SA */
  5712. + __u8 ips_authalg; /* auth algorithm for this SA */
  5713. + __u8 ips_encalg; /* enc algorithm for this SA */
  5714. +
  5715. + struct ipsec_stats ips_errs;
  5716. +
  5717. + __u8 ips_replaywin; /* replay window size */
  5718. + enum sadb_sastate ips_state; /* state of SA */
  5719. + __u32 ips_replaywin_lastseq; /* last pkt sequence num */
  5720. + __u64 ips_replaywin_bitmap; /* bitmap of received pkts */
  5721. + __u32 ips_replaywin_maxdiff; /* max pkt sequence difference */
  5722. +
  5723. + __u32 ips_flags; /* generic xform flags */
  5724. +
  5725. +
  5726. + struct ipsec_lifetimes ips_life; /* lifetime records */
  5727. +
  5728. + /* selector information */
  5729. + __u8 ips_transport_protocol; /* protocol for this SA, if ports are involved */
  5730. + struct sockaddr*ips_addr_s; /* src sockaddr */
  5731. + struct sockaddr*ips_addr_d; /* dst sockaddr */
  5732. + struct sockaddr*ips_addr_p; /* proxy sockaddr */
  5733. + __u16 ips_addr_s_size;
  5734. + __u16 ips_addr_d_size;
  5735. + __u16 ips_addr_p_size;
  5736. + ip_address ips_flow_s;
  5737. + ip_address ips_flow_d;
  5738. + ip_address ips_mask_s;
  5739. + ip_address ips_mask_d;
  5740. +
  5741. + __u16 ips_key_bits_a; /* size of authkey in bits */
  5742. + __u16 ips_auth_bits; /* size of authenticator in bits */
  5743. + __u16 ips_key_bits_e; /* size of enckey in bits */
  5744. + __u16 ips_iv_bits; /* size of IV in bits */
  5745. + __u8 ips_iv_size;
  5746. + __u16 ips_key_a_size;
  5747. + __u16 ips_key_e_size;
  5748. +
  5749. + caddr_t ips_key_a; /* authentication key */
  5750. + caddr_t ips_key_e; /* encryption key */
  5751. + caddr_t ips_iv; /* Initialisation Vector */
  5752. +
  5753. + struct ident ips_ident_s; /* identity src */
  5754. + struct ident ips_ident_d; /* identity dst */
  5755. +
  5756. + /* these are included even if CONFIG_KLIPS_IPCOMP is off */
  5757. + __u16 ips_comp_adapt_tries; /* ipcomp self-adaption tries */
  5758. + __u16 ips_comp_adapt_skip; /* ipcomp self-adaption to-skip */
  5759. + __u64 ips_comp_ratio_cbytes; /* compressed bytes */
  5760. + __u64 ips_comp_ratio_dbytes; /* decompressed (or uncompressed) bytes */
  5761. +
  5762. + /* these are included even if CONFIG_IPSEC_NAT_TRAVERSAL is off */
  5763. + __u8 ips_natt_type;
  5764. + __u8 ips_natt_reserved[3];
  5765. + __u16 ips_natt_sport;
  5766. + __u16 ips_natt_dport;
  5767. +
  5768. + struct sockaddr *ips_natt_oa;
  5769. + __u16 ips_natt_oa_size;
  5770. + __u16 ips_natt_reserved2;
  5771. +
  5772. +#if 0
  5773. + __u32 ips_sens_dpd;
  5774. + __u8 ips_sens_sens_level;
  5775. + __u8 ips_sens_sens_len;
  5776. + __u64* ips_sens_sens_bitmap;
  5777. + __u8 ips_sens_integ_level;
  5778. + __u8 ips_sens_integ_len;
  5779. + __u64* ips_sens_integ_bitmap;
  5780. +#endif
  5781. + struct ipsec_alg_enc *ips_alg_enc;
  5782. + struct ipsec_alg_auth *ips_alg_auth;
  5783. + IPsecSAref_t ips_ref_rel;
  5784. +};
  5785. +
  5786. +struct IPsecSArefSubTable
  5787. +{
  5788. + struct ipsec_sa* entry[IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES];
  5789. +};
  5790. +
  5791. +struct ipsec_sadb {
  5792. + struct IPsecSArefSubTable* refTable[IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES];
  5793. + IPsecSAref_t refFreeList[IPSEC_SA_REF_FREELIST_NUM_ENTRIES];
  5794. + int refFreeListHead;
  5795. + int refFreeListTail;
  5796. + IPsecSAref_t refFreeListCont;
  5797. + IPsecSAref_t said_hash[SADB_HASHMOD];
  5798. + spinlock_t sadb_lock;
  5799. +};
  5800. +
  5801. +extern struct ipsec_sadb ipsec_sadb;
  5802. +
  5803. +extern int ipsec_SAref_recycle(void);
  5804. +extern int ipsec_SArefSubTable_alloc(unsigned table);
  5805. +extern int ipsec_saref_freelist_init(void);
  5806. +extern int ipsec_sadb_init(void);
  5807. +extern struct ipsec_sa *ipsec_sa_alloc(int*error); /* pass in error var by pointer */
  5808. +extern IPsecSAref_t ipsec_SAref_alloc(int*erorr); /* pass in error var by pointer */
  5809. +extern int ipsec_sa_free(struct ipsec_sa* ips);
  5810. +extern int ipsec_sa_put(struct ipsec_sa *ips);
  5811. +extern int ipsec_sa_add(struct ipsec_sa *ips);
  5812. +extern int ipsec_sa_del(struct ipsec_sa *ips);
  5813. +extern int ipsec_sa_delchain(struct ipsec_sa *ips);
  5814. +extern int ipsec_sadb_cleanup(__u8 proto);
  5815. +extern int ipsec_sadb_free(void);
  5816. +extern int ipsec_sa_wipe(struct ipsec_sa *ips);
  5817. +#endif /* __KERNEL__ */
  5818. +
  5819. +enum ipsec_direction {
  5820. + ipsec_incoming = 1,
  5821. + ipsec_outgoing = 2
  5822. +};
  5823. +
  5824. +#define _IPSEC_SA_H_
  5825. +#endif /* _IPSEC_SA_H_ */
  5826. +
  5827. +/*
  5828. + * $Log: ipsec_sa.h,v $
  5829. + * Revision 1.23 2005/05/11 01:18:59 mcr
  5830. + * do not change structure based upon options, to avoid
  5831. + * too many #ifdef.
  5832. + *
  5833. + * Revision 1.22 2005/04/14 01:17:09 mcr
  5834. + * change sadb_state to an enum.
  5835. + *
  5836. + * Revision 1.21 2004/08/20 21:45:37 mcr
  5837. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  5838. + * be 26sec compatible. But, some defines where changed.
  5839. + *
  5840. + * Revision 1.20 2004/07/10 19:08:41 mcr
  5841. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5842. + *
  5843. + * Revision 1.19 2004/04/05 19:55:06 mcr
  5844. + * Moved from linux/include/freeswan/ipsec_sa.h,v
  5845. + *
  5846. + * Revision 1.18 2004/04/05 19:41:05 mcr
  5847. + * merged alg-branch code.
  5848. + *
  5849. + * Revision 1.17.2.1 2003/12/22 15:25:52 jjo
  5850. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  5851. + *
  5852. + * Revision 1.17 2003/12/10 01:20:06 mcr
  5853. + * NAT-traversal patches to KLIPS.
  5854. + *
  5855. + * Revision 1.16 2003/10/31 02:27:05 mcr
  5856. + * pulled up port-selector patches and sa_id elimination.
  5857. + *
  5858. + * Revision 1.15.4.1 2003/10/29 01:10:19 mcr
  5859. + * elimited "struct sa_id"
  5860. + *
  5861. + * Revision 1.15 2003/05/11 00:53:09 mcr
  5862. + * IPsecSAref_t and macros were moved to freeswan.h.
  5863. + *
  5864. + * Revision 1.14 2003/02/12 19:31:55 rgb
  5865. + * Fixed bug in "file seen" machinery.
  5866. + * Updated copyright year.
  5867. + *
  5868. + * Revision 1.13 2003/01/30 02:31:52 rgb
  5869. + *
  5870. + * Re-wrote comments describing SAref system for accuracy.
  5871. + * Rename SAref table macro names for clarity.
  5872. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  5873. + * Transmit error code through to caller from callee for better diagnosis of problems.
  5874. + * Enclose all macro arguments in parens to avoid any possible obscrure bugs.
  5875. + *
  5876. + * Revision 1.12 2002/10/07 18:31:19 rgb
  5877. + * Change comment to reflect the flexible nature of the main and sub-table widths.
  5878. + * Added a counter for the number of unused entries in each subtable.
  5879. + * Further break up host field type macro to host field.
  5880. + * Move field width sanity checks to ipsec_sa.c
  5881. + * Define a mask for an entire saref.
  5882. + *
  5883. + * Revision 1.11 2002/09/20 15:40:33 rgb
  5884. + * Re-write most of the SAref macros and types to eliminate any pointer references to Entrys.
  5885. + * Fixed SAref/nfmark macros.
  5886. + * Rework saref freeslist.
  5887. + * Place all ipsec sadb globals into one struct.
  5888. + * Restrict some bits to kernel context for use to klips utils.
  5889. + *
  5890. + * Revision 1.10 2002/09/20 05:00:34 rgb
  5891. + * Update copyright date.
  5892. + *
  5893. + * Revision 1.9 2002/09/17 17:19:29 mcr
  5894. + * make it compile even if there is no netfilter - we lost
  5895. + * functionality, but it works, especially on 2.2.
  5896. + *
  5897. + * Revision 1.8 2002/07/28 22:59:53 mcr
  5898. + * clarified/expanded one comment.
  5899. + *
  5900. + * Revision 1.7 2002/07/26 08:48:31 rgb
  5901. + * Added SA ref table code.
  5902. + *
  5903. + * Revision 1.6 2002/05/31 17:27:48 rgb
  5904. + * Comment fix.
  5905. + *
  5906. + * Revision 1.5 2002/05/27 18:55:03 rgb
  5907. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  5908. + *
  5909. + * Revision 1.4 2002/05/23 07:13:36 rgb
  5910. + * Convert "usecount" to "refcount" to remove ambiguity.
  5911. + *
  5912. + * Revision 1.3 2002/04/24 07:36:47 mcr
  5913. + * Moved from ./klips/net/ipsec/ipsec_sa.h,v
  5914. + *
  5915. + * Revision 1.2 2001/11/26 09:16:15 rgb
  5916. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5917. + *
  5918. + * Revision 1.1.2.1 2001/09/25 02:24:58 mcr
  5919. + * struct tdb -> struct ipsec_sa.
  5920. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  5921. + * ipsec_xform.c removed. header file still contains useful things.
  5922. + *
  5923. + *
  5924. + * Local variables:
  5925. + * c-file-style: "linux"
  5926. + * End:
  5927. + *
  5928. + */
  5929. --- /dev/null Tue Mar 11 13:02:56 2003
  5930. +++ linux/include/openswan/ipsec_sha1.h Mon Feb 9 13:51:03 2004
  5931. @@ -0,0 +1,79 @@
  5932. +/*
  5933. + * RCSID $Id: ipsec_sha1.h,v 1.8 2004/04/05 19:55:07 mcr Exp $
  5934. + */
  5935. +
  5936. +/*
  5937. + * Here is the original comment from the distribution:
  5938. +
  5939. +SHA-1 in C
  5940. +By Steve Reid <steve@edmweb.com>
  5941. +100% Public Domain
  5942. +
  5943. + * Adapted for use by the IPSEC code by John Ioannidis
  5944. + */
  5945. +
  5946. +
  5947. +#ifndef _IPSEC_SHA1_H_
  5948. +#define _IPSEC_SHA1_H_
  5949. +
  5950. +typedef struct
  5951. +{
  5952. + __u32 state[5];
  5953. + __u32 count[2];
  5954. + __u8 buffer[64];
  5955. +} SHA1_CTX;
  5956. +
  5957. +void SHA1Transform(__u32 state[5], __u8 buffer[64]);
  5958. +void SHA1Init(void *context);
  5959. +void SHA1Update(void *context, unsigned char *data, __u32 len);
  5960. +void SHA1Final(unsigned char digest[20], void *context);
  5961. +
  5962. +
  5963. +#endif /* _IPSEC_SHA1_H_ */
  5964. +
  5965. +/*
  5966. + * $Log: ipsec_sha1.h,v $
  5967. + * Revision 1.8 2004/04/05 19:55:07 mcr
  5968. + * Moved from linux/include/freeswan/ipsec_sha1.h,v
  5969. + *
  5970. + * Revision 1.7 2002/09/10 01:45:09 mcr
  5971. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  5972. + * the function prototypes would match, and could be placed
  5973. + * into a pointer to a function.
  5974. + *
  5975. + * Revision 1.6 2002/04/24 07:36:47 mcr
  5976. + * Moved from ./klips/net/ipsec/ipsec_sha1.h,v
  5977. + *
  5978. + * Revision 1.5 1999/12/13 13:59:13 rgb
  5979. + * Quick fix to argument size to Update bugs.
  5980. + *
  5981. + * Revision 1.4 1999/12/07 18:16:23 rgb
  5982. + * Fixed comments at end of #endif lines.
  5983. + *
  5984. + * Revision 1.3 1999/04/06 04:54:27 rgb
  5985. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  5986. + * patch shell fixes.
  5987. + *
  5988. + * Revision 1.2 1998/11/30 13:22:54 rgb
  5989. + * Rationalised all the klips kernel file headers. They are much shorter
  5990. + * now and won't conflict under RH5.2.
  5991. + *
  5992. + * Revision 1.1 1998/06/18 21:27:50 henry
  5993. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  5994. + * kernel-build scripts happier in the presence of symlinks
  5995. + *
  5996. + * Revision 1.2 1998/04/23 20:54:05 rgb
  5997. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  5998. + * verified.
  5999. + *
  6000. + * Revision 1.1 1998/04/09 03:04:21 henry
  6001. + * sources moved up from linux/net/ipsec
  6002. + * these two include files modified not to include others except in kernel
  6003. + *
  6004. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  6005. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  6006. + *
  6007. + * Revision 0.4 1997/01/15 01:28:15 ji
  6008. + * New transform
  6009. + *
  6010. + */
  6011. --- /dev/null Tue Mar 11 13:02:56 2003
  6012. +++ linux/include/openswan/ipsec_stats.h Mon Feb 9 13:51:03 2004
  6013. @@ -0,0 +1,76 @@
  6014. +/*
  6015. + * @(#) definition of ipsec_stats structure
  6016. + *
  6017. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  6018. + * and Michael Richardson <mcr@freeswan.org>
  6019. + *
  6020. + * This program is free software; you can redistribute it and/or modify it
  6021. + * under the terms of the GNU General Public License as published by the
  6022. + * Free Software Foundation; either version 2 of the License, or (at your
  6023. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6024. + *
  6025. + * This program is distributed in the hope that it will be useful, but
  6026. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6027. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6028. + * for more details.
  6029. + *
  6030. + * RCSID $Id: ipsec_stats.h,v 1.7 2005/04/14 01:17:45 mcr Exp $
  6031. + *
  6032. + */
  6033. +
  6034. +/*
  6035. + * This file describes the errors/statistics that FreeSWAN collects.
  6036. + */
  6037. +
  6038. +#ifndef _IPSEC_STATS_H_
  6039. +
  6040. +struct ipsec_stats {
  6041. + __u32 ips_alg_errs; /* number of algorithm errors */
  6042. + __u32 ips_auth_errs; /* # of authentication errors */
  6043. + __u32 ips_encsize_errs; /* # of encryption size errors*/
  6044. + __u32 ips_encpad_errs; /* # of encryption pad errors*/
  6045. + __u32 ips_replaywin_errs; /* # of pkt sequence errors */
  6046. +};
  6047. +
  6048. +#define _IPSEC_STATS_H_
  6049. +#endif /* _IPSEC_STATS_H_ */
  6050. +
  6051. +/*
  6052. + * $Log: ipsec_stats.h,v $
  6053. + * Revision 1.7 2005/04/14 01:17:45 mcr
  6054. + * add prototypes for snprintf.
  6055. + *
  6056. + * Revision 1.6 2004/04/05 19:55:07 mcr
  6057. + * Moved from linux/include/freeswan/ipsec_stats.h,v
  6058. + *
  6059. + * Revision 1.5 2004/04/05 19:41:05 mcr
  6060. + * merged alg-branch code.
  6061. + *
  6062. + * Revision 1.4 2004/03/28 20:27:19 paul
  6063. + * Included tested and confirmed fixes mcr made and dhr verified for
  6064. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  6065. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  6066. + * dhr. (thanks dhr!)
  6067. + *
  6068. + * Revision 1.4 2004/03/24 01:58:31 mcr
  6069. + * sprintf->snprintf for formatting into proc buffer.
  6070. + *
  6071. + * Revision 1.3.34.1 2004/04/05 04:30:46 mcr
  6072. + * patches for alg-branch to compile/work with 2.x openswan
  6073. + *
  6074. + * Revision 1.3 2002/04/24 07:36:47 mcr
  6075. + * Moved from ./klips/net/ipsec/ipsec_stats.h,v
  6076. + *
  6077. + * Revision 1.2 2001/11/26 09:16:16 rgb
  6078. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  6079. + *
  6080. + * Revision 1.1.2.1 2001/09/25 02:27:00 mcr
  6081. + * statistics moved to seperate structure.
  6082. + *
  6083. + *
  6084. + *
  6085. + * Local variables:
  6086. + * c-file-style: "linux"
  6087. + * End:
  6088. + *
  6089. + */
  6090. --- /dev/null Tue Mar 11 13:02:56 2003
  6091. +++ linux/include/openswan/ipsec_tunnel.h Mon Feb 9 13:51:03 2004
  6092. @@ -0,0 +1,280 @@
  6093. +/*
  6094. + * IPSEC tunneling code
  6095. + * Copyright (C) 1996, 1997 John Ioannidis.
  6096. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  6097. + *
  6098. + * This program is free software; you can redistribute it and/or modify it
  6099. + * under the terms of the GNU General Public License as published by the
  6100. + * Free Software Foundation; either version 2 of the License, or (at your
  6101. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6102. + *
  6103. + * This program is distributed in the hope that it will be useful, but
  6104. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6105. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6106. + * for more details.
  6107. + *
  6108. + * RCSID $Id: ipsec_tunnel.h,v 1.33 2005/06/04 16:06:05 mcr Exp $
  6109. + */
  6110. +
  6111. +
  6112. +#ifdef NET_21
  6113. +# define DEV_QUEUE_XMIT(skb, device, pri) {\
  6114. + skb->dev = device; \
  6115. + neigh_compat_output(skb); \
  6116. + /* skb->dst->output(skb); */ \
  6117. + }
  6118. +# define ICMP_SEND(skb_in, type, code, info, dev) \
  6119. + icmp_send(skb_in, type, code, htonl(info))
  6120. +# define IP_SEND(skb, dev) \
  6121. + ip_send(skb);
  6122. +#else /* NET_21 */
  6123. +# define DEV_QUEUE_XMIT(skb, device, pri) {\
  6124. + dev_queue_xmit(skb, device, pri); \
  6125. + }
  6126. +# define ICMP_SEND(skb_in, type, code, info, dev) \
  6127. + icmp_send(skb_in, type, code, info, dev)
  6128. +# define IP_SEND(skb, dev) \
  6129. + if(ntohs(iph->tot_len) > physmtu) { \
  6130. + ip_fragment(NULL, skb, dev, 0); \
  6131. + ipsec_kfree_skb(skb); \
  6132. + } else { \
  6133. + dev_queue_xmit(skb, dev, SOPRI_NORMAL); \
  6134. + }
  6135. +#endif /* NET_21 */
  6136. +
  6137. +
  6138. +/*
  6139. + * Heavily based on drivers/net/new_tunnel.c. Lots
  6140. + * of ideas also taken from the 2.1.x version of drivers/net/shaper.c
  6141. + */
  6142. +
  6143. +struct ipsectunnelconf
  6144. +{
  6145. + __u32 cf_cmd;
  6146. + union
  6147. + {
  6148. + char cfu_name[12];
  6149. + } cf_u;
  6150. +#define cf_name cf_u.cfu_name
  6151. +};
  6152. +
  6153. +#define IPSEC_SET_DEV (SIOCDEVPRIVATE)
  6154. +#define IPSEC_DEL_DEV (SIOCDEVPRIVATE + 1)
  6155. +#define IPSEC_CLR_DEV (SIOCDEVPRIVATE + 2)
  6156. +
  6157. +#ifdef __KERNEL__
  6158. +#include <linux/version.h>
  6159. +#ifndef KERNEL_VERSION
  6160. +# define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  6161. +#endif
  6162. +struct ipsecpriv
  6163. +{
  6164. + struct sk_buff_head sendq;
  6165. + struct net_device *dev;
  6166. + struct wait_queue *wait_queue;
  6167. + char locked;
  6168. + int (*hard_start_xmit) (struct sk_buff *skb,
  6169. + struct net_device *dev);
  6170. + int (*hard_header) (struct sk_buff *skb,
  6171. + struct net_device *dev,
  6172. + unsigned short type,
  6173. + void *daddr,
  6174. + void *saddr,
  6175. + unsigned len);
  6176. +#ifdef NET_21
  6177. + int (*rebuild_header)(struct sk_buff *skb);
  6178. +#else /* NET_21 */
  6179. + int (*rebuild_header)(void *buff, struct net_device *dev,
  6180. + unsigned long raddr, struct sk_buff *skb);
  6181. +#endif /* NET_21 */
  6182. + int (*set_mac_address)(struct net_device *dev, void *addr);
  6183. +#ifndef NET_21
  6184. + void (*header_cache_bind)(struct hh_cache **hhp, struct net_device *dev,
  6185. + unsigned short htype, __u32 daddr);
  6186. +#endif /* !NET_21 */
  6187. + void (*header_cache_update)(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr);
  6188. + struct net_device_stats *(*get_stats)(struct net_device *dev);
  6189. + struct net_device_stats mystats;
  6190. + int mtu; /* What is the desired MTU? */
  6191. +};
  6192. +
  6193. +extern char ipsec_tunnel_c_version[];
  6194. +
  6195. +extern struct net_device *ipsecdevices[IPSEC_NUM_IF];
  6196. +
  6197. +int ipsec_tunnel_init_devices(void);
  6198. +
  6199. +/* void */ int ipsec_tunnel_cleanup_devices(void);
  6200. +
  6201. +extern /* void */ int ipsec_init(void);
  6202. +
  6203. +extern int ipsec_tunnel_start_xmit(struct sk_buff *skb, struct net_device *dev);
  6204. +extern struct net_device *ipsec_get_device(int inst);
  6205. +
  6206. +#ifdef CONFIG_KLIPS_DEBUG
  6207. +extern int debug_tunnel;
  6208. +extern int sysctl_ipsec_debug_verbose;
  6209. +#endif /* CONFIG_KLIPS_DEBUG */
  6210. +#endif /* __KERNEL__ */
  6211. +
  6212. +#ifdef CONFIG_KLIPS_DEBUG
  6213. +#define DB_TN_INIT 0x0001
  6214. +#define DB_TN_PROCFS 0x0002
  6215. +#define DB_TN_XMIT 0x0010
  6216. +#define DB_TN_OHDR 0x0020
  6217. +#define DB_TN_CROUT 0x0040
  6218. +#define DB_TN_OXFS 0x0080
  6219. +#define DB_TN_REVEC 0x0100
  6220. +#define DB_TN_ENCAP 0x0200
  6221. +#endif /* CONFIG_KLIPS_DEBUG */
  6222. +
  6223. +/*
  6224. + * $Log: ipsec_tunnel.h,v $
  6225. + * Revision 1.33 2005/06/04 16:06:05 mcr
  6226. + * better patch for nat-t rcv-device code.
  6227. + *
  6228. + * Revision 1.32 2005/05/21 03:18:35 mcr
  6229. + * added additional debug flag tunnelling.
  6230. + *
  6231. + * Revision 1.31 2004/08/03 18:18:02 mcr
  6232. + * in 2.6, use "net_device" instead of #define device->net_device.
  6233. + * this probably breaks 2.0 compiles.
  6234. + *
  6235. + * Revision 1.30 2004/07/10 19:08:41 mcr
  6236. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6237. + *
  6238. + * Revision 1.29 2004/04/05 19:55:07 mcr
  6239. + * Moved from linux/include/freeswan/ipsec_tunnel.h,v
  6240. + *
  6241. + * Revision 1.28 2003/06/24 20:22:32 mcr
  6242. + * added new global: ipsecdevices[] so that we can keep track of
  6243. + * the ipsecX devices. They will be referenced with dev_hold(),
  6244. + * so 2.2 may need this as well.
  6245. + *
  6246. + * Revision 1.27 2003/04/03 17:38:09 rgb
  6247. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  6248. + *
  6249. + * Revision 1.26 2003/02/12 19:32:20 rgb
  6250. + * Updated copyright year.
  6251. + *
  6252. + * Revision 1.25 2002/05/27 18:56:07 rgb
  6253. + * Convert to dynamic ipsec device allocation.
  6254. + *
  6255. + * Revision 1.24 2002/04/24 07:36:48 mcr
  6256. + * Moved from ./klips/net/ipsec/ipsec_tunnel.h,v
  6257. + *
  6258. + * Revision 1.23 2001/11/06 19:50:44 rgb
  6259. + * Moved IP_SEND, ICMP_SEND, DEV_QUEUE_XMIT macros to ipsec_tunnel.h for
  6260. + * use also by pfkey_v2_parser.c
  6261. + *
  6262. + * Revision 1.22 2001/09/15 16:24:05 rgb
  6263. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  6264. + *
  6265. + * Revision 1.21 2001/06/14 19:35:10 rgb
  6266. + * Update copyright date.
  6267. + *
  6268. + * Revision 1.20 2000/09/15 11:37:02 rgb
  6269. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  6270. + * IPCOMP zlib deflate code.
  6271. + *
  6272. + * Revision 1.19 2000/09/08 19:12:56 rgb
  6273. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  6274. + *
  6275. + * Revision 1.18 2000/07/28 13:50:54 rgb
  6276. + * Changed enet_statistics to net_device_stats and added back compatibility
  6277. + * for pre-2.1.19.
  6278. + *
  6279. + * Revision 1.17 1999/11/19 01:12:15 rgb
  6280. + * Purge unneeded proc_info prototypes, now that static linking uses
  6281. + * dynamic proc_info registration.
  6282. + *
  6283. + * Revision 1.16 1999/11/18 18:51:00 rgb
  6284. + * Changed all device registrations for static linking to
  6285. + * dynamic to reduce the number and size of patches.
  6286. + *
  6287. + * Revision 1.15 1999/11/18 04:14:21 rgb
  6288. + * Replaced all kernel version macros to shorter, readable form.
  6289. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  6290. + * Added Marc Boucher's 2.3.25 proc patches.
  6291. + *
  6292. + * Revision 1.14 1999/05/25 02:50:10 rgb
  6293. + * Fix kernel version macros for 2.0.x static linking.
  6294. + *
  6295. + * Revision 1.13 1999/05/25 02:41:06 rgb
  6296. + * Add ipsec_klipsdebug support for static linking.
  6297. + *
  6298. + * Revision 1.12 1999/05/05 22:02:32 rgb
  6299. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  6300. + *
  6301. + * Revision 1.11 1999/04/29 15:19:50 rgb
  6302. + * Add return values to init and cleanup functions.
  6303. + *
  6304. + * Revision 1.10 1999/04/16 16:02:39 rgb
  6305. + * Bump up macro to 4 ipsec I/Fs.
  6306. + *
  6307. + * Revision 1.9 1999/04/15 15:37:25 rgb
  6308. + * Forward check changes from POST1_00 branch.
  6309. + *
  6310. + * Revision 1.5.2.1 1999/04/02 04:26:14 rgb
  6311. + * Backcheck from HEAD, pre1.0.
  6312. + *
  6313. + * Revision 1.8 1999/04/11 00:29:01 henry
  6314. + * GPL boilerplate
  6315. + *
  6316. + * Revision 1.7 1999/04/06 04:54:28 rgb
  6317. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  6318. + * patch shell fixes.
  6319. + *
  6320. + * Revision 1.6 1999/03/31 05:44:48 rgb
  6321. + * Keep PMTU reduction private.
  6322. + *
  6323. + * Revision 1.5 1999/02/10 22:31:20 rgb
  6324. + * Change rebuild_header member to reflect generality of link layer.
  6325. + *
  6326. + * Revision 1.4 1998/12/01 13:22:04 rgb
  6327. + * Added support for debug printing of version info.
  6328. + *
  6329. + * Revision 1.3 1998/07/29 20:42:46 rgb
  6330. + * Add a macro for clearing all tunnel devices.
  6331. + * Rearrange structures and declarations for sharing with userspace.
  6332. + *
  6333. + * Revision 1.2 1998/06/25 20:01:45 rgb
  6334. + * Make prototypes available for ipsec_init and ipsec proc_dir_entries
  6335. + * for static linking.
  6336. + *
  6337. + * Revision 1.1 1998/06/18 21:27:50 henry
  6338. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  6339. + * kernel-build scripts happier in the presence of symlinks
  6340. + *
  6341. + * Revision 1.3 1998/05/18 21:51:50 rgb
  6342. + * Added macros for num of I/F's and a procfs debug switch.
  6343. + *
  6344. + * Revision 1.2 1998/04/21 21:29:09 rgb
  6345. + * Rearrange debug switches to change on the fly debug output from user
  6346. + * space. Only kernel changes checked in at this time. radij.c was also
  6347. + * changed to temporarily remove buggy debugging code in rj_delete causing
  6348. + * an OOPS and hence, netlink device open errors.
  6349. + *
  6350. + * Revision 1.1 1998/04/09 03:06:13 henry
  6351. + * sources moved up from linux/net/ipsec
  6352. + *
  6353. + * Revision 1.1.1.1 1998/04/08 05:35:05 henry
  6354. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  6355. + *
  6356. + * Revision 0.5 1997/06/03 04:24:48 ji
  6357. + * Added transport mode.
  6358. + * Changed the way routing is done.
  6359. + * Lots of bug fixes.
  6360. + *
  6361. + * Revision 0.4 1997/01/15 01:28:15 ji
  6362. + * No changes.
  6363. + *
  6364. + * Revision 0.3 1996/11/20 14:39:04 ji
  6365. + * Minor cleanups.
  6366. + * Rationalized debugging code.
  6367. + *
  6368. + * Revision 0.2 1996/11/02 00:18:33 ji
  6369. + * First limited release.
  6370. + *
  6371. + *
  6372. + */
  6373. --- /dev/null Tue Mar 11 13:02:56 2003
  6374. +++ linux/include/openswan/ipsec_xform.h Mon Feb 9 13:51:03 2004
  6375. @@ -0,0 +1,257 @@
  6376. +/*
  6377. + * Definitions relevant to IPSEC transformations
  6378. + * Copyright (C) 1996, 1997 John Ioannidis.
  6379. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  6380. + * COpyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  6381. + *
  6382. + * This program is free software; you can redistribute it and/or modify it
  6383. + * under the terms of the GNU General Public License as published by the
  6384. + * Free Software Foundation; either version 2 of the License, or (at your
  6385. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6386. + *
  6387. + * This program is distributed in the hope that it will be useful, but
  6388. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6389. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6390. + * for more details.
  6391. + *
  6392. + * RCSID $Id: ipsec_xform.h,v 1.41 2004/07/10 19:08:41 mcr Exp $
  6393. + */
  6394. +
  6395. +#ifndef _IPSEC_XFORM_H_
  6396. +
  6397. +#include <openswan.h>
  6398. +
  6399. +#define XF_NONE 0 /* No transform set */
  6400. +#define XF_IP4 1 /* IPv4 inside IPv4 */
  6401. +#define XF_AHMD5 2 /* AH MD5 */
  6402. +#define XF_AHSHA 3 /* AH SHA */
  6403. +#define XF_ESP3DES 5 /* ESP DES3-CBC */
  6404. +#define XF_AHHMACMD5 6 /* AH-HMAC-MD5 with opt replay prot */
  6405. +#define XF_AHHMACSHA1 7 /* AH-HMAC-SHA1 with opt replay prot */
  6406. +#define XF_ESP3DESMD5 9 /* triple DES, HMAC-MD-5, 128-bits of authentication */
  6407. +#define XF_ESP3DESMD596 10 /* triple DES, HMAC-MD-5, 96-bits of authentication */
  6408. +#define XF_ESPNULLMD596 12 /* NULL, HMAC-MD-5 with 96-bits of authentication */
  6409. +#define XF_ESPNULLSHA196 13 /* NULL, HMAC-SHA-1 with 96-bits of authentication */
  6410. +#define XF_ESP3DESSHA196 14 /* triple DES, HMAC-SHA-1, 96-bits of authentication */
  6411. +#define XF_IP6 15 /* IPv6 inside IPv6 */
  6412. +#define XF_COMPDEFLATE 16 /* IPCOMP deflate */
  6413. +
  6414. +#define XF_CLR 126 /* Clear SA table */
  6415. +#define XF_DEL 127 /* Delete SA */
  6416. +
  6417. +/* IPsec AH transform values
  6418. + * RFC 2407
  6419. + * draft-ietf-ipsec-doi-tc-mib-02.txt
  6420. + */
  6421. +
  6422. +#define AH_NONE 0
  6423. +#define AH_MD5 2
  6424. +#define AH_SHA 3
  6425. +/* draft-ietf-ipsec-ciph-aes-cbc-03.txt */
  6426. +#define AH_SHA2_256 5
  6427. +#define AH_SHA2_384 6
  6428. +#define AH_SHA2_512 7
  6429. +#define AH_RIPEMD 8
  6430. +#define AH_MAX 15
  6431. +
  6432. +/* IPsec ESP transform values */
  6433. +
  6434. +#define ESP_NONE 0
  6435. +#define ESP_DES 2
  6436. +#define ESP_3DES 3
  6437. +#define ESP_RC5 4
  6438. +#define ESP_IDEA 5
  6439. +#define ESP_CAST 6
  6440. +#define ESP_BLOWFISH 7
  6441. +#define ESP_3IDEA 8
  6442. +#define ESP_RC4 10
  6443. +#define ESP_NULL 11
  6444. +#define ESP_AES 12
  6445. +
  6446. +/* as draft-ietf-ipsec-ciph-aes-cbc-02.txt */
  6447. +#define ESP_MARS 249
  6448. +#define ESP_RC6 250
  6449. +#define ESP_SERPENT 252
  6450. +#define ESP_TWOFISH 253
  6451. +
  6452. +/* IPCOMP transform values */
  6453. +
  6454. +#define IPCOMP_NONE 0
  6455. +#define IPCOMP_OUI 1
  6456. +#define IPCOMP_DEFLAT 2
  6457. +#define IPCOMP_LZS 3
  6458. +#define IPCOMP_V42BIS 4
  6459. +
  6460. +#define XFT_AUTH 0x0001
  6461. +#define XFT_CONF 0x0100
  6462. +
  6463. +/* available if CONFIG_KLIPS_DEBUG is defined */
  6464. +#define DB_XF_INIT 0x0001
  6465. +
  6466. +#define PROTO2TXT(x) \
  6467. + (x) == IPPROTO_AH ? "AH" : \
  6468. + (x) == IPPROTO_ESP ? "ESP" : \
  6469. + (x) == IPPROTO_IPIP ? "IPIP" : \
  6470. + (x) == IPPROTO_COMP ? "COMP" : \
  6471. + "UNKNOWN_proto"
  6472. +static inline const char *enc_name_id (unsigned id) {
  6473. + static char buf[16];
  6474. + snprintf(buf, sizeof(buf), "_ID%d", id);
  6475. + return buf;
  6476. +}
  6477. +static inline const char *auth_name_id (unsigned id) {
  6478. + static char buf[16];
  6479. + snprintf(buf, sizeof(buf), "_ID%d", id);
  6480. + return buf;
  6481. +}
  6482. +#define IPS_XFORM_NAME(x) \
  6483. + PROTO2TXT((x)->ips_said.proto), \
  6484. + (x)->ips_said.proto == IPPROTO_COMP ? \
  6485. + ((x)->ips_encalg == SADB_X_CALG_DEFLATE ? \
  6486. + "_DEFLATE" : "_UNKNOWN_comp") : \
  6487. + (x)->ips_encalg == ESP_NONE ? "" : \
  6488. + (x)->ips_encalg == ESP_3DES ? "_3DES" : \
  6489. + (x)->ips_encalg == ESP_AES ? "_AES" : \
  6490. + (x)->ips_encalg == ESP_SERPENT ? "_SERPENT" : \
  6491. + (x)->ips_encalg == ESP_TWOFISH ? "_TWOFISH" : \
  6492. + enc_name_id(x->ips_encalg)/* "_UNKNOWN_encr" */, \
  6493. + (x)->ips_authalg == AH_NONE ? "" : \
  6494. + (x)->ips_authalg == AH_MD5 ? "_HMAC_MD5" : \
  6495. + (x)->ips_authalg == AH_SHA ? "_HMAC_SHA1" : \
  6496. + (x)->ips_authalg == AH_SHA2_256 ? "_HMAC_SHA2_256" : \
  6497. + (x)->ips_authalg == AH_SHA2_384 ? "_HMAC_SHA2_384" : \
  6498. + (x)->ips_authalg == AH_SHA2_512 ? "_HMAC_SHA2_512" : \
  6499. + auth_name_id(x->ips_authalg) /* "_UNKNOWN_auth" */ \
  6500. +
  6501. +#ifdef __KERNEL__
  6502. +struct ipsec_rcv_state;
  6503. +struct ipsec_xmit_state;
  6504. +
  6505. +struct xform_functions {
  6506. + enum ipsec_rcv_value (*rcv_checks)(struct ipsec_rcv_state *irs,
  6507. + struct sk_buff *skb);
  6508. + enum ipsec_rcv_value (*rcv_decrypt)(struct ipsec_rcv_state *irs);
  6509. +
  6510. + enum ipsec_rcv_value (*rcv_setup_auth)(struct ipsec_rcv_state *irs,
  6511. + struct sk_buff *skb,
  6512. + __u32 *replay,
  6513. + unsigned char **authenticator);
  6514. + enum ipsec_rcv_value (*rcv_calc_auth)(struct ipsec_rcv_state *irs,
  6515. + struct sk_buff *skb);
  6516. +
  6517. + enum ipsec_xmit_value (*xmit_setup)(struct ipsec_xmit_state *ixs);
  6518. + enum ipsec_xmit_value (*xmit_encrypt)(struct ipsec_xmit_state *ixs);
  6519. +
  6520. + enum ipsec_xmit_value (*xmit_setup_auth)(struct ipsec_xmit_state *ixs,
  6521. + struct sk_buff *skb,
  6522. + __u32 *replay,
  6523. + unsigned char **authenticator);
  6524. + enum ipsec_xmit_value (*xmit_calc_auth)(struct ipsec_xmit_state *ixs,
  6525. + struct sk_buff *skb);
  6526. + int xmit_headroom;
  6527. + int xmit_needtailroom;
  6528. +};
  6529. +
  6530. +#endif /* __KERNEL__ */
  6531. +
  6532. +#ifdef CONFIG_KLIPS_DEBUG
  6533. +extern void ipsec_dmp(char *s, caddr_t bb, int len);
  6534. +#else /* CONFIG_KLIPS_DEBUG */
  6535. +#define ipsec_dmp(_x, _y, _z)
  6536. +#endif /* CONFIG_KLIPS_DEBUG */
  6537. +
  6538. +
  6539. +#define _IPSEC_XFORM_H_
  6540. +#endif /* _IPSEC_XFORM_H_ */
  6541. +
  6542. +/*
  6543. + * $Log: ipsec_xform.h,v $
  6544. + * Revision 1.41 2004/07/10 19:08:41 mcr
  6545. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6546. + *
  6547. + * Revision 1.40 2004/04/06 02:49:08 mcr
  6548. + * pullup of algo code from alg-branch.
  6549. + *
  6550. + * Revision 1.39 2004/04/05 19:55:07 mcr
  6551. + * Moved from linux/include/freeswan/ipsec_xform.h,v
  6552. + *
  6553. + * Revision 1.38 2004/04/05 19:41:05 mcr
  6554. + * merged alg-branch code.
  6555. + *
  6556. + * Revision 1.37 2003/12/13 19:10:16 mcr
  6557. + * refactored rcv and xmit code - same as FS 2.05.
  6558. + *
  6559. + * Revision 1.36.34.1 2003/12/22 15:25:52 jjo
  6560. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  6561. + *
  6562. + * Revision 1.36 2002/04/24 07:36:48 mcr
  6563. + * Moved from ./klips/net/ipsec/ipsec_xform.h,v
  6564. + *
  6565. + * Revision 1.35 2001/11/26 09:23:51 rgb
  6566. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  6567. + *
  6568. + * Revision 1.33.2.1 2001/09/25 02:24:58 mcr
  6569. + * struct tdb -> struct ipsec_sa.
  6570. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  6571. + * ipsec_xform.c removed. header file still contains useful things.
  6572. + *
  6573. + * Revision 1.34 2001/11/06 19:47:17 rgb
  6574. + * Changed lifetime_packets to uint32 from uint64.
  6575. + *
  6576. + * Revision 1.33 2001/09/08 21:13:34 rgb
  6577. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  6578. + *
  6579. + * Revision 1.32 2001/07/06 07:40:01 rgb
  6580. + * Reformatted for readability.
  6581. + * Added inbound policy checking fields for use with IPIP SAs.
  6582. + *
  6583. + * Revision 1.31 2001/06/14 19:35:11 rgb
  6584. + * Update copyright date.
  6585. + *
  6586. + * Revision 1.30 2001/05/30 08:14:03 rgb
  6587. + * Removed vestiges of esp-null transforms.
  6588. + *
  6589. + * Revision 1.29 2001/01/30 23:42:47 rgb
  6590. + * Allow pfkey msgs from pid other than user context required for ACQUIRE
  6591. + * and subsequent ADD or UDATE.
  6592. + *
  6593. + * Revision 1.28 2000/11/06 04:30:40 rgb
  6594. + * Add Svenning's adaptive content compression.
  6595. + *
  6596. + * Revision 1.27 2000/09/19 00:38:25 rgb
  6597. + * Fixed algorithm name bugs introduced for ipcomp.
  6598. + *
  6599. + * Revision 1.26 2000/09/17 21:36:48 rgb
  6600. + * Added proto2txt macro.
  6601. + *
  6602. + * Revision 1.25 2000/09/17 18:56:47 rgb
  6603. + * Added IPCOMP support.
  6604. + *
  6605. + * Revision 1.24 2000/09/12 19:34:12 rgb
  6606. + * Defined XF_IP6 from Gerhard for ipv6 tunnel support.
  6607. + *
  6608. + * Revision 1.23 2000/09/12 03:23:14 rgb
  6609. + * Cleaned out now unused tdb_xform and tdb_xdata members of struct tdb.
  6610. + *
  6611. + * Revision 1.22 2000/09/08 19:12:56 rgb
  6612. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  6613. + *
  6614. + * Revision 1.21 2000/09/01 18:32:43 rgb
  6615. + * Added (disabled) sensitivity members to tdb struct.
  6616. + *
  6617. + * Revision 1.20 2000/08/30 05:31:01 rgb
  6618. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  6619. + * Kill remainder of tdb_xform, tdb_xdata, xformsw.
  6620. + *
  6621. + * Revision 1.19 2000/08/01 14:51:52 rgb
  6622. + * Removed _all_ remaining traces of DES.
  6623. + *
  6624. + * Revision 1.18 2000/01/21 06:17:45 rgb
  6625. + * Tidied up spacing.
  6626. + *
  6627. + *
  6628. + * Local variables:
  6629. + * c-file-style: "linux"
  6630. + * End:
  6631. + *
  6632. + */
  6633. --- /dev/null Tue Mar 11 13:02:56 2003
  6634. +++ linux/include/openswan/ipsec_xmit.h Mon Feb 9 13:51:03 2004
  6635. @@ -0,0 +1,198 @@
  6636. +/*
  6637. + * IPSEC tunneling code
  6638. + * Copyright (C) 1996, 1997 John Ioannidis.
  6639. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  6640. + *
  6641. + * This program is free software; you can redistribute it and/or modify it
  6642. + * under the terms of the GNU General Public License as published by the
  6643. + * Free Software Foundation; either version 2 of the License, or (at your
  6644. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6645. + *
  6646. + * This program is distributed in the hope that it will be useful, but
  6647. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6648. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6649. + * for more details.
  6650. + *
  6651. + * RCSID $Id: ipsec_xmit.h,v 1.14 2005/05/11 01:00:26 mcr Exp $
  6652. + */
  6653. +
  6654. +#include "openswan/ipsec_sa.h"
  6655. +
  6656. +enum ipsec_xmit_value
  6657. +{
  6658. + IPSEC_XMIT_STOLEN=2,
  6659. + IPSEC_XMIT_PASS=1,
  6660. + IPSEC_XMIT_OK=0,
  6661. + IPSEC_XMIT_ERRMEMALLOC=-1,
  6662. + IPSEC_XMIT_ESP_BADALG=-2,
  6663. + IPSEC_XMIT_BADPROTO=-3,
  6664. + IPSEC_XMIT_ESP_PUSHPULLERR=-4,
  6665. + IPSEC_XMIT_BADLEN=-5,
  6666. + IPSEC_XMIT_AH_BADALG=-6,
  6667. + IPSEC_XMIT_SAIDNOTFOUND=-7,
  6668. + IPSEC_XMIT_SAIDNOTLIVE=-8,
  6669. + IPSEC_XMIT_REPLAYROLLED=-9,
  6670. + IPSEC_XMIT_LIFETIMEFAILED=-10,
  6671. + IPSEC_XMIT_CANNOTFRAG=-11,
  6672. + IPSEC_XMIT_MSSERR=-12,
  6673. + IPSEC_XMIT_ERRSKBALLOC=-13,
  6674. + IPSEC_XMIT_ENCAPFAIL=-14,
  6675. + IPSEC_XMIT_NODEV=-15,
  6676. + IPSEC_XMIT_NOPRIVDEV=-16,
  6677. + IPSEC_XMIT_NOPHYSDEV=-17,
  6678. + IPSEC_XMIT_NOSKB=-18,
  6679. + IPSEC_XMIT_NOIPV6=-19,
  6680. + IPSEC_XMIT_NOIPOPTIONS=-20,
  6681. + IPSEC_XMIT_TTLEXPIRED=-21,
  6682. + IPSEC_XMIT_BADHHLEN=-22,
  6683. + IPSEC_XMIT_PUSHPULLERR=-23,
  6684. + IPSEC_XMIT_ROUTEERR=-24,
  6685. + IPSEC_XMIT_RECURSDETECT=-25,
  6686. + IPSEC_XMIT_IPSENDFAILURE=-26,
  6687. + IPSEC_XMIT_ESPUDP=-27,
  6688. + IPSEC_XMIT_ESPUDP_BADTYPE=-28,
  6689. +};
  6690. +
  6691. +struct ipsec_xmit_state
  6692. +{
  6693. + struct sk_buff *skb; /* working skb pointer */
  6694. + struct net_device *dev; /* working dev pointer */
  6695. + struct ipsecpriv *prv; /* Our device' private space */
  6696. + struct sk_buff *oskb; /* Original skb pointer */
  6697. + struct net_device_stats *stats; /* This device's statistics */
  6698. + struct iphdr *iph; /* Our new IP header */
  6699. + __u32 newdst; /* The other SG's IP address */
  6700. + __u32 orgdst; /* Original IP destination address */
  6701. + __u32 orgedst; /* 1st SG's IP address */
  6702. + __u32 newsrc; /* The new source SG's IP address */
  6703. + __u32 orgsrc; /* Original IP source address */
  6704. + __u32 innersrc; /* Innermost IP source address */
  6705. + int iphlen; /* IP header length */
  6706. + int pyldsz; /* upper protocol payload size */
  6707. + int headroom;
  6708. + int tailroom;
  6709. + int authlen;
  6710. + int max_headroom; /* The extra header space needed */
  6711. + int max_tailroom; /* The extra stuffing needed */
  6712. + int ll_headroom; /* The extra link layer hard_header space needed */
  6713. + int tot_headroom; /* The total header space needed */
  6714. + int tot_tailroom; /* The totalstuffing needed */
  6715. + __u8 *saved_header; /* saved copy of the hard header */
  6716. + unsigned short sport, dport;
  6717. +
  6718. + struct sockaddr_encap matcher; /* eroute search key */
  6719. + struct eroute *eroute;
  6720. + struct ipsec_sa *ipsp, *ipsq; /* ipsec_sa pointers */
  6721. + char sa_txt[SATOT_BUF];
  6722. + size_t sa_len;
  6723. + int hard_header_stripped; /* has the hard header been removed yet? */
  6724. + int hard_header_len;
  6725. + struct net_device *physdev;
  6726. +/* struct device *virtdev; */
  6727. + short physmtu;
  6728. + short cur_mtu; /* copy of prv->mtu, cause prv may == NULL */
  6729. + short mtudiff;
  6730. +#ifdef NET_21
  6731. + struct rtable *route;
  6732. +#endif /* NET_21 */
  6733. + ip_said outgoing_said;
  6734. +#ifdef NET_21
  6735. + int pass;
  6736. +#endif /* NET_21 */
  6737. + int error;
  6738. + uint32_t eroute_pid;
  6739. + struct ipsec_sa ips;
  6740. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  6741. + uint8_t natt_type;
  6742. + uint8_t natt_head;
  6743. + uint16_t natt_sport;
  6744. + uint16_t natt_dport;
  6745. +#endif
  6746. +};
  6747. +
  6748. +enum ipsec_xmit_value
  6749. +ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs);
  6750. +
  6751. +enum ipsec_xmit_value
  6752. +ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs);
  6753. +
  6754. +enum ipsec_xmit_value
  6755. +ipsec_xmit_encap_bundle(struct ipsec_xmit_state *ixs);
  6756. +
  6757. +extern void ipsec_extract_ports(struct iphdr * iph, struct sockaddr_encap * er);
  6758. +
  6759. +
  6760. +extern int ipsec_xmit_trap_count;
  6761. +extern int ipsec_xmit_trap_sendcount;
  6762. +
  6763. +#ifdef CONFIG_KLIPS_DEBUG
  6764. +extern int debug_tunnel;
  6765. +
  6766. +#define debug_xmit debug_tunnel
  6767. +
  6768. +#define ipsec_xmit_dmp(_x,_y, _z) if (debug_xmit && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  6769. +#else
  6770. +#define ipsec_xmit_dmp(_x,_y, _z) do {} while(0)
  6771. +
  6772. +#endif /* CONFIG_KLIPS_DEBUG */
  6773. +
  6774. +extern int sysctl_ipsec_debug_verbose;
  6775. +extern int sysctl_ipsec_icmp;
  6776. +extern int sysctl_ipsec_tos;
  6777. +
  6778. +
  6779. +/*
  6780. + * $Log: ipsec_xmit.h,v $
  6781. + * Revision 1.14 2005/05/11 01:00:26 mcr
  6782. + * do not call debug routines if !defined KLIPS_DEBUG.
  6783. + *
  6784. + * Revision 1.13 2005/04/29 05:01:38 mcr
  6785. + * use ipsec_dmp_block.
  6786. + * added cur_mtu to ixs instead of using ixs->dev.
  6787. + *
  6788. + * Revision 1.12 2004/08/20 21:45:37 mcr
  6789. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  6790. + * be 26sec compatible. But, some defines where changed.
  6791. + *
  6792. + * Revision 1.11 2004/08/03 18:18:21 mcr
  6793. + * in 2.6, use "net_device" instead of #define device->net_device.
  6794. + * this probably breaks 2.0 compiles.
  6795. + *
  6796. + * Revision 1.10 2004/07/10 19:08:41 mcr
  6797. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6798. + *
  6799. + * Revision 1.9 2004/04/06 02:49:08 mcr
  6800. + * pullup of algo code from alg-branch.
  6801. + *
  6802. + * Revision 1.8 2004/04/05 19:55:07 mcr
  6803. + * Moved from linux/include/freeswan/ipsec_xmit.h,v
  6804. + *
  6805. + * Revision 1.7 2004/02/03 03:11:40 mcr
  6806. + * new xmit type if the UDP encapsulation is wrong.
  6807. + *
  6808. + * Revision 1.6 2003/12/13 19:10:16 mcr
  6809. + * refactored rcv and xmit code - same as FS 2.05.
  6810. + *
  6811. + * Revision 1.5 2003/12/10 01:20:06 mcr
  6812. + * NAT-traversal patches to KLIPS.
  6813. + *
  6814. + * Revision 1.4 2003/12/06 16:37:04 mcr
  6815. + * 1.4.7a X.509 patch applied.
  6816. + *
  6817. + * Revision 1.3 2003/10/31 02:27:05 mcr
  6818. + * pulled up port-selector patches and sa_id elimination.
  6819. + *
  6820. + * Revision 1.2.4.2 2003/10/29 01:10:19 mcr
  6821. + * elimited "struct sa_id"
  6822. + *
  6823. + * Revision 1.2.4.1 2003/09/21 13:59:38 mcr
  6824. + * pre-liminary X.509 patch - does not yet pass tests.
  6825. + *
  6826. + * Revision 1.2 2003/06/20 01:42:13 mcr
  6827. + * added counters to measure how many ACQUIREs we send to pluto,
  6828. + * and how many are successfully sent.
  6829. + *
  6830. + * Revision 1.1 2003/02/12 19:31:03 rgb
  6831. + * Refactored from ipsec_tunnel.c
  6832. + *
  6833. + */
  6834. --- /dev/null Tue Mar 11 13:02:56 2003
  6835. +++ linux/include/openswan/passert.h Mon Feb 9 13:51:03 2004
  6836. @@ -0,0 +1,75 @@
  6837. +/*
  6838. + * sanitize a string into a printable format.
  6839. + *
  6840. + * Copyright (C) 1998-2002 D. Hugh Redelmeier.
  6841. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  6842. + *
  6843. + * This library is free software; you can redistribute it and/or modify it
  6844. + * under the terms of the GNU Library General Public License as published by
  6845. + * the Free Software Foundation; either version 2 of the License, or (at your
  6846. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  6847. + *
  6848. + * This library is distributed in the hope that it will be useful, but
  6849. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6850. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  6851. + * License for more details.
  6852. + *
  6853. + * RCSID $Id: passert.h,v 1.7 2004/10/21 18:44:42 mcr Exp $
  6854. + */
  6855. +
  6856. +#include "openswan.h"
  6857. +
  6858. +#ifndef _OPENSWAN_PASSERT_H
  6859. +#define _OPENSWAN_PASSERT_H
  6860. +/* our versions of assert: log result */
  6861. +
  6862. +#ifdef DEBUG
  6863. +
  6864. +typedef void (*openswan_passert_fail_t)(const char *pred_str,
  6865. + const char *file_str,
  6866. + unsigned long line_no) NEVER_RETURNS;
  6867. +
  6868. +openswan_passert_fail_t openswan_passert_fail;
  6869. +
  6870. +extern void pexpect_log(const char *pred_str
  6871. + , const char *file_str, unsigned long line_no);
  6872. +
  6873. +# define impossible() do { \
  6874. + if(openswan_passert_fail) { \
  6875. + (*openswan_passert_fail)("impossible", __FILE__, __LINE__); \
  6876. + }} while(0)
  6877. +
  6878. +extern void switch_fail(int n
  6879. + , const char *file_str, unsigned long line_no) NEVER_RETURNS;
  6880. +
  6881. +# define bad_case(n) switch_fail((int) n, __FILE__, __LINE__)
  6882. +
  6883. +# define passert(pred) do { \
  6884. + if (!(pred)) \
  6885. + if(openswan_passert_fail) { \
  6886. + (*openswan_passert_fail)(#pred, __FILE__, __LINE__); \
  6887. + } \
  6888. + } while(0)
  6889. +
  6890. +# define pexpect(pred) do { \
  6891. + if (!(pred)) \
  6892. + pexpect_log(#pred, __FILE__, __LINE__); \
  6893. + } while(0)
  6894. +
  6895. +/* assert that an err_t is NULL; evaluate exactly once */
  6896. +# define happy(x) { \
  6897. + err_t ugh = x; \
  6898. + if (ugh != NULL) \
  6899. + if(openswan_passert_fail) { (*openswan_passert_fail)(ugh, __FILE__, __LINE__); } \
  6900. + }
  6901. +
  6902. +#else /*!DEBUG*/
  6903. +
  6904. +# define impossible() abort()
  6905. +# define bad_case(n) abort()
  6906. +# define passert(pred) { } /* do nothing */
  6907. +# define happy(x) { (void) x; } /* evaluate non-judgementally */
  6908. +
  6909. +#endif /*!DEBUG*/
  6910. +
  6911. +#endif /* _OPENSWAN_PASSERT_H */
  6912. --- /dev/null Tue Mar 11 13:02:56 2003
  6913. +++ linux/include/openswan/pfkey_debug.h Mon Feb 9 13:51:03 2004
  6914. @@ -0,0 +1,54 @@
  6915. +/*
  6916. + * sanitize a string into a printable format.
  6917. + *
  6918. + * Copyright (C) 1998-2002 D. Hugh Redelmeier.
  6919. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  6920. + *
  6921. + * This library is free software; you can redistribute it and/or modify it
  6922. + * under the terms of the GNU Library General Public License as published by
  6923. + * the Free Software Foundation; either version 2 of the License, or (at your
  6924. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  6925. + *
  6926. + * This library is distributed in the hope that it will be useful, but
  6927. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6928. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  6929. + * License for more details.
  6930. + *
  6931. + * RCSID $Id: pfkey_debug.h,v 1.3 2004/04/05 19:55:07 mcr Exp $
  6932. + */
  6933. +
  6934. +#ifndef _FREESWAN_PFKEY_DEBUG_H
  6935. +#define _FREESWAN_PFKEY_DEBUG_H
  6936. +
  6937. +#ifdef __KERNEL__
  6938. +
  6939. +/* note, kernel version ignores pfkey levels */
  6940. +# define DEBUGGING(level,args...) \
  6941. + KLIPS_PRINT(debug_pfkey, "klips_debug:" args)
  6942. +
  6943. +# define ERROR(args...) printk(KERN_ERR "klips:" args)
  6944. +
  6945. +#else
  6946. +
  6947. +extern unsigned int pfkey_lib_debug;
  6948. +
  6949. +extern void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
  6950. +extern void (*pfkey_error_func)(const char *message, ...) PRINTF_LIKE(1);
  6951. +
  6952. +#define DEBUGGING(level,args...) if(pfkey_lib_debug & level) { \
  6953. + if(pfkey_debug_func != NULL) { \
  6954. + (*pfkey_debug_func)("pfkey_lib_debug:" args); \
  6955. + } else { \
  6956. + printf("pfkey_lib_debug:" args); \
  6957. + } }
  6958. +
  6959. +#define ERROR(args...) if(pfkey_error_func != NULL) { \
  6960. + (*pfkey_error_func)("pfkey_lib_debug:" args); \
  6961. + }
  6962. +
  6963. +# define MALLOC(size) malloc(size)
  6964. +# define FREE(obj) free(obj)
  6965. +
  6966. +#endif
  6967. +
  6968. +#endif
  6969. --- /dev/null Tue Mar 11 13:02:56 2003
  6970. +++ linux/include/openswan/radij.h Mon Feb 9 13:51:03 2004
  6971. @@ -0,0 +1,280 @@
  6972. +/*
  6973. + * RCSID $Id: radij.h,v 1.13 2004/04/05 19:55:08 mcr Exp $
  6974. + */
  6975. +
  6976. +/*
  6977. + * This file is defived from ${SRC}/sys/net/radix.h of BSD 4.4lite
  6978. + *
  6979. + * Variable and procedure names have been modified so that they don't
  6980. + * conflict with the original BSD code, as a small number of modifications
  6981. + * have been introduced and we may want to reuse this code in BSD.
  6982. + *
  6983. + * The `j' in `radij' is pronounced as a voiceless guttural (like a Greek
  6984. + * chi or a German ch sound (as `doch', not as in `milch'), or even a
  6985. + * spanish j as in Juan. It is not as far back in the throat like
  6986. + * the corresponding Hebrew sound, nor is it a soft breath like the English h.
  6987. + * It has nothing to do with the Dutch ij sound.
  6988. + *
  6989. + * Here is the appropriate copyright notice:
  6990. + */
  6991. +
  6992. +/*
  6993. + * Copyright (c) 1988, 1989, 1993
  6994. + * The Regents of the University of California. All rights reserved.
  6995. + *
  6996. + * Redistribution and use in source and binary forms, with or without
  6997. + * modification, are permitted provided that the following conditions
  6998. + * are met:
  6999. + * 1. Redistributions of source code must retain the above copyright
  7000. + * notice, this list of conditions and the following disclaimer.
  7001. + * 2. Redistributions in binary form must reproduce the above copyright
  7002. + * notice, this list of conditions and the following disclaimer in the
  7003. + * documentation and/or other materials provided with the distribution.
  7004. + * 3. All advertising materials mentioning features or use of this software
  7005. + * must display the following acknowledgement:
  7006. + * This product includes software developed by the University of
  7007. + * California, Berkeley and its contributors.
  7008. + * 4. Neither the name of the University nor the names of its contributors
  7009. + * may be used to endorse or promote products derived from this software
  7010. + * without specific prior written permission.
  7011. + *
  7012. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  7013. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  7014. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  7015. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  7016. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  7017. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  7018. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  7019. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  7020. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  7021. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  7022. + * SUCH DAMAGE.
  7023. + *
  7024. + * @(#)radix.h 8.1 (Berkeley) 6/10/93
  7025. + */
  7026. +
  7027. +#ifndef _RADIJ_H_
  7028. +#define _RADIJ_H_
  7029. +
  7030. +/*
  7031. +#define RJ_DEBUG
  7032. +*/
  7033. +
  7034. +#ifdef __KERNEL__
  7035. +
  7036. +#ifndef __P
  7037. +#ifdef __STDC__
  7038. +#define __P(x) x
  7039. +#else
  7040. +#define __P(x) ()
  7041. +#endif
  7042. +#endif
  7043. +
  7044. +/*
  7045. + * Radix search tree node layout.
  7046. + */
  7047. +
  7048. +struct radij_node
  7049. +{
  7050. + struct radij_mask *rj_mklist; /* list of masks contained in subtree */
  7051. + struct radij_node *rj_p; /* parent */
  7052. + short rj_b; /* bit offset; -1-index(netmask) */
  7053. + char rj_bmask; /* node: mask for bit test*/
  7054. + u_char rj_flags; /* enumerated next */
  7055. +#define RJF_NORMAL 1 /* leaf contains normal route */
  7056. +#define RJF_ROOT 2 /* leaf is root leaf for tree */
  7057. +#define RJF_ACTIVE 4 /* This node is alive (for rtfree) */
  7058. + union {
  7059. + struct { /* leaf only data: */
  7060. + caddr_t rj_Key; /* object of search */
  7061. + caddr_t rj_Mask; /* netmask, if present */
  7062. + struct radij_node *rj_Dupedkey;
  7063. + } rj_leaf;
  7064. + struct { /* node only data: */
  7065. + int rj_Off; /* where to start compare */
  7066. + struct radij_node *rj_L;/* progeny */
  7067. + struct radij_node *rj_R;/* progeny */
  7068. + }rj_node;
  7069. + } rj_u;
  7070. +#ifdef RJ_DEBUG
  7071. + int rj_info;
  7072. + struct radij_node *rj_twin;
  7073. + struct radij_node *rj_ybro;
  7074. +#endif
  7075. +};
  7076. +
  7077. +#define rj_dupedkey rj_u.rj_leaf.rj_Dupedkey
  7078. +#define rj_key rj_u.rj_leaf.rj_Key
  7079. +#define rj_mask rj_u.rj_leaf.rj_Mask
  7080. +#define rj_off rj_u.rj_node.rj_Off
  7081. +#define rj_l rj_u.rj_node.rj_L
  7082. +#define rj_r rj_u.rj_node.rj_R
  7083. +
  7084. +/*
  7085. + * Annotations to tree concerning potential routes applying to subtrees.
  7086. + */
  7087. +
  7088. +extern struct radij_mask {
  7089. + short rm_b; /* bit offset; -1-index(netmask) */
  7090. + char rm_unused; /* cf. rj_bmask */
  7091. + u_char rm_flags; /* cf. rj_flags */
  7092. + struct radij_mask *rm_mklist; /* more masks to try */
  7093. + caddr_t rm_mask; /* the mask */
  7094. + int rm_refs; /* # of references to this struct */
  7095. +} *rj_mkfreelist;
  7096. +
  7097. +#define MKGet(m) {\
  7098. + if (rj_mkfreelist) {\
  7099. + m = rj_mkfreelist; \
  7100. + rj_mkfreelist = (m)->rm_mklist; \
  7101. + } else \
  7102. + R_Malloc(m, struct radij_mask *, sizeof (*(m))); }\
  7103. +
  7104. +#define MKFree(m) { (m)->rm_mklist = rj_mkfreelist; rj_mkfreelist = (m);}
  7105. +
  7106. +struct radij_node_head {
  7107. + struct radij_node *rnh_treetop;
  7108. + int rnh_addrsize; /* permit, but not require fixed keys */
  7109. + int rnh_pktsize; /* permit, but not require fixed keys */
  7110. +#if 0
  7111. + struct radij_node *(*rnh_addaddr) /* add based on sockaddr */
  7112. + __P((void *v, void *mask,
  7113. + struct radij_node_head *head, struct radij_node nodes[]));
  7114. +#endif
  7115. + int (*rnh_addaddr) /* add based on sockaddr */
  7116. + __P((void *v, void *mask,
  7117. + struct radij_node_head *head, struct radij_node nodes[]));
  7118. + struct radij_node *(*rnh_addpkt) /* add based on packet hdr */
  7119. + __P((void *v, void *mask,
  7120. + struct radij_node_head *head, struct radij_node nodes[]));
  7121. +#if 0
  7122. + struct radij_node *(*rnh_deladdr) /* remove based on sockaddr */
  7123. + __P((void *v, void *mask, struct radij_node_head *head));
  7124. +#endif
  7125. + int (*rnh_deladdr) /* remove based on sockaddr */
  7126. + __P((void *v, void *mask, struct radij_node_head *head, struct radij_node **node));
  7127. + struct radij_node *(*rnh_delpkt) /* remove based on packet hdr */
  7128. + __P((void *v, void *mask, struct radij_node_head *head));
  7129. + struct radij_node *(*rnh_matchaddr) /* locate based on sockaddr */
  7130. + __P((void *v, struct radij_node_head *head));
  7131. + struct radij_node *(*rnh_matchpkt) /* locate based on packet hdr */
  7132. + __P((void *v, struct radij_node_head *head));
  7133. + int (*rnh_walktree) /* traverse tree */
  7134. + __P((struct radij_node_head *head, int (*f)(struct radij_node *rn, void *w), void *w));
  7135. + struct radij_node rnh_nodes[3]; /* empty tree for common case */
  7136. +};
  7137. +
  7138. +
  7139. +#define Bcmp(a, b, n) memcmp(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
  7140. +#define Bcopy(a, b, n) memmove(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
  7141. +#define Bzero(p, n) memset((caddr_t)(p), 0, (unsigned)(n))
  7142. +#define R_Malloc(p, t, n) ((p = (t) kmalloc((size_t)(n), GFP_ATOMIC)), Bzero((p),(n)))
  7143. +#define Free(p) kfree((caddr_t)p);
  7144. +
  7145. +void rj_init __P((void));
  7146. +int rj_inithead __P((void **, int));
  7147. +int rj_refines __P((void *, void *));
  7148. +int rj_walktree __P((struct radij_node_head *head, int (*f)(struct radij_node *rn, void *w), void *w));
  7149. +struct radij_node
  7150. + *rj_addmask __P((void *, int, int)) /* , rgb */ ;
  7151. +int /* * */ rj_addroute __P((void *, void *, struct radij_node_head *,
  7152. + struct radij_node [2])) /* , rgb */ ;
  7153. +int /* * */ rj_delete __P((void *, void *, struct radij_node_head *, struct radij_node **)) /* , rgb */ ;
  7154. +struct radij_node /* rgb */
  7155. + *rj_insert __P((void *, struct radij_node_head *, int *,
  7156. + struct radij_node [2])),
  7157. + *rj_match __P((void *, struct radij_node_head *)),
  7158. + *rj_newpair __P((void *, int, struct radij_node[2])),
  7159. + *rj_search __P((void *, struct radij_node *)),
  7160. + *rj_search_m __P((void *, struct radij_node *, void *));
  7161. +
  7162. +void rj_deltree(struct radij_node_head *);
  7163. +void rj_delnodes(struct radij_node *);
  7164. +void rj_free_mkfreelist(void);
  7165. +int radijcleartree(void);
  7166. +int radijcleanup(void);
  7167. +
  7168. +extern struct radij_node_head *mask_rjhead;
  7169. +extern int maj_keylen;
  7170. +#endif /* __KERNEL__ */
  7171. +
  7172. +#endif /* _RADIJ_H_ */
  7173. +
  7174. +
  7175. +/*
  7176. + * $Log: radij.h,v $
  7177. + * Revision 1.13 2004/04/05 19:55:08 mcr
  7178. + * Moved from linux/include/freeswan/radij.h,v
  7179. + *
  7180. + * Revision 1.12 2002/04/24 07:36:48 mcr
  7181. + * Moved from ./klips/net/ipsec/radij.h,v
  7182. + *
  7183. + * Revision 1.11 2001/09/20 15:33:00 rgb
  7184. + * Min/max cleanup.
  7185. + *
  7186. + * Revision 1.10 1999/11/18 04:09:20 rgb
  7187. + * Replaced all kernel version macros to shorter, readable form.
  7188. + *
  7189. + * Revision 1.9 1999/05/05 22:02:33 rgb
  7190. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  7191. + *
  7192. + * Revision 1.8 1999/04/29 15:24:58 rgb
  7193. + * Add check for existence of macros min/max.
  7194. + *
  7195. + * Revision 1.7 1999/04/11 00:29:02 henry
  7196. + * GPL boilerplate
  7197. + *
  7198. + * Revision 1.6 1999/04/06 04:54:29 rgb
  7199. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  7200. + * patch shell fixes.
  7201. + *
  7202. + * Revision 1.5 1999/01/22 06:30:32 rgb
  7203. + * 64-bit clean-up.
  7204. + *
  7205. + * Revision 1.4 1998/11/30 13:22:55 rgb
  7206. + * Rationalised all the klips kernel file headers. They are much shorter
  7207. + * now and won't conflict under RH5.2.
  7208. + *
  7209. + * Revision 1.3 1998/10/25 02:43:27 rgb
  7210. + * Change return type on rj_addroute and rj_delete and add and argument
  7211. + * to the latter to be able to transmit more infomation about errors.
  7212. + *
  7213. + * Revision 1.2 1998/07/14 18:09:51 rgb
  7214. + * Add a routine to clear eroute table.
  7215. + * Added #ifdef __KERNEL__ directives to restrict scope of header.
  7216. + *
  7217. + * Revision 1.1 1998/06/18 21:30:22 henry
  7218. + * move sources from klips/src to klips/net/ipsec to keep stupid kernel
  7219. + * build scripts happier about symlinks
  7220. + *
  7221. + * Revision 1.4 1998/05/25 20:34:16 rgb
  7222. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  7223. + *
  7224. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  7225. + * add ipsec_rj_walker_delete.
  7226. + *
  7227. + * Recover memory for eroute table on unload of module.
  7228. + *
  7229. + * Revision 1.3 1998/04/22 16:51:37 rgb
  7230. + * Tidy up radij debug code from recent rash of modifications to debug code.
  7231. + *
  7232. + * Revision 1.2 1998/04/14 17:30:38 rgb
  7233. + * Fix up compiling errors for radij tree memory reclamation.
  7234. + *
  7235. + * Revision 1.1 1998/04/09 03:06:16 henry
  7236. + * sources moved up from linux/net/ipsec
  7237. + *
  7238. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  7239. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  7240. + *
  7241. + * Revision 0.4 1997/01/15 01:28:15 ji
  7242. + * No changes.
  7243. + *
  7244. + * Revision 0.3 1996/11/20 14:44:45 ji
  7245. + * Release update only.
  7246. + *
  7247. + * Revision 0.2 1996/11/02 00:18:33 ji
  7248. + * First limited release.
  7249. + *
  7250. + *
  7251. + */
  7252. --- /dev/null Tue Mar 11 13:02:56 2003
  7253. +++ linux/include/pfkey.h Mon Feb 9 13:51:03 2004
  7254. @@ -0,0 +1,529 @@
  7255. +/*
  7256. + * FreeS/WAN specific PF_KEY headers
  7257. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  7258. + *
  7259. + * This program is free software; you can redistribute it and/or modify it
  7260. + * under the terms of the GNU General Public License as published by the
  7261. + * Free Software Foundation; either version 2 of the License, or (at your
  7262. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  7263. + *
  7264. + * This program is distributed in the hope that it will be useful, but
  7265. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  7266. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  7267. + * for more details.
  7268. + *
  7269. + * RCSID $Id: pfkey.h,v 1.49 2005/05/11 00:57:29 mcr Exp $
  7270. + */
  7271. +
  7272. +#ifndef __NET_IPSEC_PF_KEY_H
  7273. +#define __NET_IPSEC_PF_KEY_H
  7274. +#ifdef __KERNEL__
  7275. +extern struct proto_ops pfkey_proto_ops;
  7276. +typedef struct sock pfkey_sock;
  7277. +extern int debug_pfkey;
  7278. +
  7279. +extern /* void */ int pfkey_init(void);
  7280. +extern /* void */ int pfkey_cleanup(void);
  7281. +
  7282. +struct socket_list
  7283. +{
  7284. + struct socket *socketp;
  7285. + struct socket_list *next;
  7286. +};
  7287. +extern int pfkey_list_insert_socket(struct socket*, struct socket_list**);
  7288. +extern int pfkey_list_remove_socket(struct socket*, struct socket_list**);
  7289. +extern struct socket_list *pfkey_open_sockets;
  7290. +extern struct socket_list *pfkey_registered_sockets[];
  7291. +
  7292. +struct ipsec_alg_supported
  7293. +{
  7294. + uint16_t ias_exttype;
  7295. + uint8_t ias_id;
  7296. + uint8_t ias_ivlen;
  7297. + uint16_t ias_keyminbits;
  7298. + uint16_t ias_keymaxbits;
  7299. + char *ias_name;
  7300. +};
  7301. +
  7302. +extern struct supported_list *pfkey_supported_list[];
  7303. +struct supported_list
  7304. +{
  7305. + struct ipsec_alg_supported *supportedp;
  7306. + struct supported_list *next;
  7307. +};
  7308. +extern int pfkey_list_insert_supported(struct ipsec_alg_supported*, struct supported_list**);
  7309. +extern int pfkey_list_remove_supported(struct ipsec_alg_supported*, struct supported_list**);
  7310. +
  7311. +struct sockaddr_key
  7312. +{
  7313. + uint16_t key_family; /* PF_KEY */
  7314. + uint16_t key_pad; /* not used */
  7315. + uint32_t key_pid; /* process ID */
  7316. +};
  7317. +
  7318. +struct pfkey_extracted_data
  7319. +{
  7320. + struct ipsec_sa* ips;
  7321. + struct ipsec_sa* ips2;
  7322. + struct eroute *eroute;
  7323. +};
  7324. +
  7325. +/* forward reference */
  7326. +struct sadb_ext;
  7327. +struct sadb_msg;
  7328. +struct sockaddr;
  7329. +struct sadb_comb;
  7330. +struct sadb_sadb;
  7331. +struct sadb_alg;
  7332. +
  7333. +extern int
  7334. +pfkey_alloc_eroute(struct eroute** eroute);
  7335. +
  7336. +extern int
  7337. +pfkey_sa_process(struct sadb_ext *pfkey_ext,
  7338. + struct pfkey_extracted_data* extr);
  7339. +
  7340. +extern int
  7341. +pfkey_lifetime_process(struct sadb_ext *pfkey_ext,
  7342. + struct pfkey_extracted_data* extr);
  7343. +
  7344. +extern int
  7345. +pfkey_address_process(struct sadb_ext *pfkey_ext,
  7346. + struct pfkey_extracted_data* extr);
  7347. +
  7348. +extern int
  7349. +pfkey_key_process(struct sadb_ext *pfkey_ext,
  7350. + struct pfkey_extracted_data* extr);
  7351. +
  7352. +extern int
  7353. +pfkey_ident_process(struct sadb_ext *pfkey_ext,
  7354. + struct pfkey_extracted_data* extr);
  7355. +
  7356. +extern int
  7357. +pfkey_sens_process(struct sadb_ext *pfkey_ext,
  7358. + struct pfkey_extracted_data* extr);
  7359. +
  7360. +extern int
  7361. +pfkey_prop_process(struct sadb_ext *pfkey_ext,
  7362. + struct pfkey_extracted_data* extr);
  7363. +
  7364. +extern int
  7365. +pfkey_supported_process(struct sadb_ext *pfkey_ext,
  7366. + struct pfkey_extracted_data* extr);
  7367. +
  7368. +extern int
  7369. +pfkey_spirange_process(struct sadb_ext *pfkey_ext,
  7370. + struct pfkey_extracted_data* extr);
  7371. +
  7372. +extern int
  7373. +pfkey_x_kmprivate_process(struct sadb_ext *pfkey_ext,
  7374. + struct pfkey_extracted_data* extr);
  7375. +
  7376. +extern int
  7377. +pfkey_x_satype_process(struct sadb_ext *pfkey_ext,
  7378. + struct pfkey_extracted_data* extr);
  7379. +
  7380. +extern int
  7381. +pfkey_x_debug_process(struct sadb_ext *pfkey_ext,
  7382. + struct pfkey_extracted_data* extr);
  7383. +
  7384. +extern int pfkey_upmsg(struct socket *, struct sadb_msg *);
  7385. +extern int pfkey_expire(struct ipsec_sa *, int);
  7386. +extern int pfkey_acquire(struct ipsec_sa *);
  7387. +#else /* ! __KERNEL__ */
  7388. +
  7389. +extern void (*pfkey_debug_func)(const char *message, ...);
  7390. +extern void (*pfkey_error_func)(const char *message, ...);
  7391. +extern void pfkey_print(struct sadb_msg *msg, FILE *out);
  7392. +
  7393. +
  7394. +#endif /* __KERNEL__ */
  7395. +
  7396. +extern uint8_t satype2proto(uint8_t satype);
  7397. +extern uint8_t proto2satype(uint8_t proto);
  7398. +extern char* satype2name(uint8_t satype);
  7399. +extern char* proto2name(uint8_t proto);
  7400. +
  7401. +struct key_opt
  7402. +{
  7403. + uint32_t key_pid; /* process ID */
  7404. + struct sock *sk;
  7405. +};
  7406. +
  7407. +#define key_pid(sk) ((struct key_opt*)&((sk)->sk_protinfo))->key_pid
  7408. +
  7409. +/* XXX-mcr this is not an alignment, this is because the count is in 64-bit
  7410. + * words.
  7411. + */
  7412. +#define IPSEC_PFKEYv2_ALIGN (sizeof(uint64_t)/sizeof(uint8_t))
  7413. +#define BITS_PER_OCTET 8
  7414. +#define OCTETBITS 8
  7415. +#define PFKEYBITS 64
  7416. +#define DIVUP(x,y) ((x + y -1) / y) /* divide, rounding upwards */
  7417. +#define ALIGN_N(x,y) (DIVUP(x,y) * y) /* align on y boundary */
  7418. +
  7419. +#define IPSEC_PFKEYv2_LEN(x) ((x) * IPSEC_PFKEYv2_ALIGN)
  7420. +#define IPSEC_PFKEYv2_WORDS(x) ((x) / IPSEC_PFKEYv2_ALIGN)
  7421. +
  7422. +
  7423. +#define PFKEYv2_MAX_MSGSIZE 4096
  7424. +
  7425. +/*
  7426. + * PF_KEYv2 permitted and required extensions in and out bitmaps
  7427. + */
  7428. +struct pf_key_ext_parsers_def {
  7429. + int (*parser)(struct sadb_ext*);
  7430. + char *parser_name;
  7431. +};
  7432. +
  7433. +
  7434. +#define SADB_EXTENSIONS_MAX 31
  7435. +extern unsigned int extensions_bitmaps[2/*in/out*/][2/*perm/req*/][SADB_EXTENSIONS_MAX];
  7436. +#define EXT_BITS_IN 0
  7437. +#define EXT_BITS_OUT 1
  7438. +#define EXT_BITS_PERM 0
  7439. +#define EXT_BITS_REQ 1
  7440. +
  7441. +extern void pfkey_extensions_init(struct sadb_ext *extensions[]);
  7442. +extern void pfkey_extensions_free(struct sadb_ext *extensions[]);
  7443. +extern void pfkey_msg_free(struct sadb_msg **pfkey_msg);
  7444. +
  7445. +extern int pfkey_msg_parse(struct sadb_msg *pfkey_msg,
  7446. + struct pf_key_ext_parsers_def *ext_parsers[],
  7447. + struct sadb_ext **extensions,
  7448. + int dir);
  7449. +
  7450. +extern int pfkey_register_reply(int satype, struct sadb_msg *sadb_msg);
  7451. +
  7452. +/*
  7453. + * PF_KEYv2 build function prototypes
  7454. + */
  7455. +
  7456. +int
  7457. +pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
  7458. + uint8_t msg_type,
  7459. + uint8_t satype,
  7460. + uint8_t msg_errno,
  7461. + uint32_t seq,
  7462. + uint32_t pid);
  7463. +
  7464. +int
  7465. +pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
  7466. + uint16_t exttype,
  7467. + uint32_t spi, /* in network order */
  7468. + uint8_t replay_window,
  7469. + uint8_t sa_state,
  7470. + uint8_t auth,
  7471. + uint8_t encrypt,
  7472. + uint32_t flags,
  7473. + uint32_t/*IPsecSAref_t*/ ref);
  7474. +
  7475. +int
  7476. +pfkey_sa_build(struct sadb_ext ** pfkey_ext,
  7477. + uint16_t exttype,
  7478. + uint32_t spi, /* in network order */
  7479. + uint8_t replay_window,
  7480. + uint8_t sa_state,
  7481. + uint8_t auth,
  7482. + uint8_t encrypt,
  7483. + uint32_t flags);
  7484. +
  7485. +int
  7486. +pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
  7487. + uint16_t exttype,
  7488. + uint32_t allocations,
  7489. + uint64_t bytes,
  7490. + uint64_t addtime,
  7491. + uint64_t usetime,
  7492. + uint32_t packets);
  7493. +
  7494. +int
  7495. +pfkey_address_build(struct sadb_ext** pfkey_ext,
  7496. + uint16_t exttype,
  7497. + uint8_t proto,
  7498. + uint8_t prefixlen,
  7499. + struct sockaddr* address);
  7500. +
  7501. +int
  7502. +pfkey_key_build(struct sadb_ext** pfkey_ext,
  7503. + uint16_t exttype,
  7504. + uint16_t key_bits,
  7505. + char* key);
  7506. +
  7507. +int
  7508. +pfkey_ident_build(struct sadb_ext** pfkey_ext,
  7509. + uint16_t exttype,
  7510. + uint16_t ident_type,
  7511. + uint64_t ident_id,
  7512. + uint8_t ident_len,
  7513. + char* ident_string);
  7514. +
  7515. +#ifdef __KERNEL__
  7516. +extern int pfkey_nat_t_new_mapping(struct ipsec_sa *, struct sockaddr *, __u16);
  7517. +extern int pfkey_x_nat_t_type_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr);
  7518. +extern int pfkey_x_nat_t_port_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr);
  7519. +#endif /* __KERNEL__ */
  7520. +int
  7521. +pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
  7522. + uint8_t type);
  7523. +int
  7524. +pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
  7525. + uint16_t exttype,
  7526. + uint16_t port);
  7527. +
  7528. +int
  7529. +pfkey_sens_build(struct sadb_ext** pfkey_ext,
  7530. + uint32_t dpd,
  7531. + uint8_t sens_level,
  7532. + uint8_t sens_len,
  7533. + uint64_t* sens_bitmap,
  7534. + uint8_t integ_level,
  7535. + uint8_t integ_len,
  7536. + uint64_t* integ_bitmap);
  7537. +
  7538. +int pfkey_x_protocol_build(struct sadb_ext **, uint8_t);
  7539. +
  7540. +
  7541. +int
  7542. +pfkey_prop_build(struct sadb_ext** pfkey_ext,
  7543. + uint8_t replay,
  7544. + unsigned int comb_num,
  7545. + struct sadb_comb* comb);
  7546. +
  7547. +int
  7548. +pfkey_supported_build(struct sadb_ext** pfkey_ext,
  7549. + uint16_t exttype,
  7550. + unsigned int alg_num,
  7551. + struct sadb_alg* alg);
  7552. +
  7553. +int
  7554. +pfkey_spirange_build(struct sadb_ext** pfkey_ext,
  7555. + uint16_t exttype,
  7556. + uint32_t min,
  7557. + uint32_t max);
  7558. +
  7559. +int
  7560. +pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext);
  7561. +
  7562. +int
  7563. +pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
  7564. + uint8_t satype);
  7565. +
  7566. +int
  7567. +pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
  7568. + uint32_t tunnel,
  7569. + uint32_t netlink,
  7570. + uint32_t xform,
  7571. + uint32_t eroute,
  7572. + uint32_t spi,
  7573. + uint32_t radij,
  7574. + uint32_t esp,
  7575. + uint32_t ah,
  7576. + uint32_t rcv,
  7577. + uint32_t pfkey,
  7578. + uint32_t ipcomp,
  7579. + uint32_t verbose);
  7580. +
  7581. +int
  7582. +pfkey_msg_build(struct sadb_msg** pfkey_msg,
  7583. + struct sadb_ext* extensions[],
  7584. + int dir);
  7585. +
  7586. +/* in pfkey_v2_debug.c - routines to decode numbers -> strings */
  7587. +const char *
  7588. +pfkey_v2_sadb_ext_string(int extnum);
  7589. +
  7590. +const char *
  7591. +pfkey_v2_sadb_type_string(int sadb_type);
  7592. +
  7593. +
  7594. +#endif /* __NET_IPSEC_PF_KEY_H */
  7595. +
  7596. +/*
  7597. + * $Log: pfkey.h,v $
  7598. + * Revision 1.49 2005/05/11 00:57:29 mcr
  7599. + * rename struct supported -> struct ipsec_alg_supported.
  7600. + * make pfkey.h more standalone.
  7601. + *
  7602. + * Revision 1.48 2005/05/01 03:12:50 mcr
  7603. + * include name of algorithm in datastructure.
  7604. + *
  7605. + * Revision 1.47 2004/08/21 00:44:14 mcr
  7606. + * simplify definition of nat_t related prototypes.
  7607. + *
  7608. + * Revision 1.46 2004/08/04 16:27:22 mcr
  7609. + * 2.6 sk_ options.
  7610. + *
  7611. + * Revision 1.45 2004/04/06 02:49:00 mcr
  7612. + * pullup of algo code from alg-branch.
  7613. + *
  7614. + * Revision 1.44 2003/12/10 01:20:01 mcr
  7615. + * NAT-traversal patches to KLIPS.
  7616. + *
  7617. + * Revision 1.43 2003/10/31 02:26:44 mcr
  7618. + * pulled up port-selector patches.
  7619. + *
  7620. + * Revision 1.42.2.2 2003/10/29 01:09:32 mcr
  7621. + * added debugging for pfkey library.
  7622. + *
  7623. + * Revision 1.42.2.1 2003/09/21 13:59:34 mcr
  7624. + * pre-liminary X.509 patch - does not yet pass tests.
  7625. + *
  7626. + * Revision 1.42 2003/08/25 22:08:19 mcr
  7627. + * removed pfkey_proto_init() from pfkey.h for 2.6 support.
  7628. + *
  7629. + * Revision 1.41 2003/05/07 17:28:57 mcr
  7630. + * new function pfkey_debug_func added for us in debugging from
  7631. +
  7632. + * pfkey library.
  7633. + *
  7634. + * Revision 1.40 2003/01/30 02:31:34 rgb
  7635. + *
  7636. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  7637. + *
  7638. + * Revision 1.39 2002/09/20 15:40:21 rgb
  7639. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  7640. + * Added ref parameter to pfkey_sa_build().
  7641. + * Cleaned out unused cruft.
  7642. + *
  7643. + * Revision 1.38 2002/05/14 02:37:24 rgb
  7644. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  7645. + * ipsec_sa or ipsec_sa.
  7646. + * Added function prototypes for the functions moved to
  7647. + * pfkey_v2_ext_process.c.
  7648. + *
  7649. + * Revision 1.37 2002/04/24 07:36:49 mcr
  7650. + * Moved from ./lib/pfkey.h,v
  7651. + *
  7652. + * Revision 1.36 2002/01/20 20:34:49 mcr
  7653. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  7654. + *
  7655. + * Revision 1.35 2001/11/27 05:27:47 mcr
  7656. + * pfkey parses are now maintained by a structure
  7657. + * that includes their name for debug purposes.
  7658. + *
  7659. + * Revision 1.34 2001/11/26 09:23:53 rgb
  7660. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  7661. + *
  7662. + * Revision 1.33 2001/11/06 19:47:47 rgb
  7663. + * Added packet parameter to lifetime and comb structures.
  7664. + *
  7665. + * Revision 1.32 2001/09/08 21:13:34 rgb
  7666. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  7667. + *
  7668. + * Revision 1.31 2001/06/14 19:35:16 rgb
  7669. + * Update copyright date.
  7670. + *
  7671. + * Revision 1.30 2001/02/27 07:04:52 rgb
  7672. + * Added satype2name prototype.
  7673. + *
  7674. + * Revision 1.29 2001/02/26 19:59:33 rgb
  7675. + * Ditch unused sadb_satype2proto[], replaced by satype2proto().
  7676. + *
  7677. + * Revision 1.28 2000/10/10 20:10:19 rgb
  7678. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  7679. + *
  7680. + * Revision 1.27 2000/09/21 04:20:45 rgb
  7681. + * Fixed array size off-by-one error. (Thanks Svenning!)
  7682. + *
  7683. + * Revision 1.26 2000/09/12 03:26:05 rgb
  7684. + * Added pfkey_acquire prototype.
  7685. + *
  7686. + * Revision 1.25 2000/09/08 19:21:28 rgb
  7687. + * Fix pfkey_prop_build() parameter to be only single indirection.
  7688. + *
  7689. + * Revision 1.24 2000/09/01 18:46:42 rgb
  7690. + * Added a supported algorithms array lists, one per satype and registered
  7691. + * existing algorithms.
  7692. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  7693. + * list.
  7694. + *
  7695. + * Revision 1.23 2000/08/27 01:55:26 rgb
  7696. + * Define OCTETBITS and PFKEYBITS to avoid using 'magic' numbers in code.
  7697. + *
  7698. + * Revision 1.22 2000/08/20 21:39:23 rgb
  7699. + * Added kernel prototypes for kernel funcitions pfkey_upmsg() and
  7700. + * pfkey_expire().
  7701. + *
  7702. + * Revision 1.21 2000/08/15 17:29:23 rgb
  7703. + * Fixes from SZI to untested pfkey_prop_build().
  7704. + *
  7705. + * Revision 1.20 2000/05/10 20:14:19 rgb
  7706. + * Fleshed out sensitivity, proposal and supported extensions.
  7707. + *
  7708. + * Revision 1.19 2000/03/16 14:07:23 rgb
  7709. + * Renamed ALIGN macro to avoid fighting with others in kernel.
  7710. + *
  7711. + * Revision 1.18 2000/01/22 23:24:06 rgb
  7712. + * Added prototypes for proto2satype(), satype2proto() and proto2name().
  7713. + *
  7714. + * Revision 1.17 2000/01/21 06:26:59 rgb
  7715. + * Converted from double tdb arguments to one structure (extr)
  7716. + * containing pointers to all temporary information structures.
  7717. + * Added klipsdebug switching capability.
  7718. + * Dropped unused argument to pfkey_x_satype_build().
  7719. + *
  7720. + * Revision 1.16 1999/12/29 21:17:41 rgb
  7721. + * Changed pfkey_msg_build() I/F to include a struct sadb_msg**
  7722. + * parameter for cleaner manipulation of extensions[] and to guard
  7723. + * against potential memory leaks.
  7724. + * Changed the I/F to pfkey_msg_free() for the same reason.
  7725. + *
  7726. + * Revision 1.15 1999/12/09 23:12:54 rgb
  7727. + * Added macro for BITS_PER_OCTET.
  7728. + * Added argument to pfkey_sa_build() to do eroutes.
  7729. + *
  7730. + * Revision 1.14 1999/12/08 20:33:25 rgb
  7731. + * Changed sa_family_t to uint16_t for 2.0.xx compatibility.
  7732. + *
  7733. + * Revision 1.13 1999/12/07 19:53:40 rgb
  7734. + * Removed unused first argument from extension parsers.
  7735. + * Changed __u* types to uint* to avoid use of asm/types.h and
  7736. + * sys/types.h in userspace code.
  7737. + * Added function prototypes for pfkey message and extensions
  7738. + * initialisation and cleanup.
  7739. + *
  7740. + * Revision 1.12 1999/12/01 22:19:38 rgb
  7741. + * Change pfkey_sa_build to accept an SPI in network byte order.
  7742. + *
  7743. + * Revision 1.11 1999/11/27 11:55:26 rgb
  7744. + * Added extern sadb_satype2proto to enable moving protocol lookup table
  7745. + * to lib/pfkey_v2_parse.c.
  7746. + * Delete unused, moved typedefs.
  7747. + * Add argument to pfkey_msg_parse() for direction.
  7748. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  7749. + *
  7750. + * Revision 1.10 1999/11/23 22:29:21 rgb
  7751. + * This file has been moved in the distribution from klips/net/ipsec to
  7752. + * lib.
  7753. + * Add macros for dealing with alignment and rounding up more opaquely.
  7754. + * The uint<n>_t type defines have been moved to freeswan.h to avoid
  7755. + * chicken-and-egg problems.
  7756. + * Add macros for dealing with alignment and rounding up more opaque.
  7757. + * Added prototypes for using extention header bitmaps.
  7758. + * Added prototypes of all the build functions.
  7759. + *
  7760. + * Revision 1.9 1999/11/20 21:59:48 rgb
  7761. + * Moved socketlist type declarations and prototypes for shared use.
  7762. + * Slightly modified scope of sockaddr_key declaration.
  7763. + *
  7764. + * Revision 1.8 1999/11/17 14:34:25 rgb
  7765. + * Protect sa_family_t from being used in userspace with GLIBC<2.
  7766. + *
  7767. + * Revision 1.7 1999/10/27 19:40:35 rgb
  7768. + * Add a maximum PFKEY packet size macro.
  7769. + *
  7770. + * Revision 1.6 1999/10/26 16:58:58 rgb
  7771. + * Created a sockaddr_key and key_opt socket extension structures.
  7772. + *
  7773. + * Revision 1.5 1999/06/10 05:24:41 rgb
  7774. + * Renamed variables to reduce confusion.
  7775. + *
  7776. + * Revision 1.4 1999/04/29 15:21:11 rgb
  7777. + * Add pfkey support to debugging.
  7778. + * Add return values to init and cleanup functions.
  7779. + *
  7780. + * Revision 1.3 1999/04/15 17:58:07 rgb
  7781. + * Add RCSID labels.
  7782. + *
  7783. + */
  7784. --- /dev/null Tue Mar 11 13:02:56 2003
  7785. +++ linux/include/pfkeyv2.h Mon Feb 9 13:51:03 2004
  7786. @@ -0,0 +1,472 @@
  7787. +/*
  7788. + * RCSID $Id: pfkeyv2.h,v 1.31 2005/04/14 01:14:54 mcr Exp $
  7789. + */
  7790. +
  7791. +/*
  7792. +RFC 2367 PF_KEY Key Management API July 1998
  7793. +
  7794. +
  7795. +Appendix D: Sample Header File
  7796. +
  7797. +This file defines structures and symbols for the PF_KEY Version 2
  7798. +key management interface. It was written at the U.S. Naval Research
  7799. +Laboratory. This file is in the public domain. The authors ask that
  7800. +you leave this credit intact on any copies of this file.
  7801. +*/
  7802. +#ifndef __PFKEY_V2_H
  7803. +#define __PFKEY_V2_H 1
  7804. +
  7805. +#define PF_KEY_V2 2
  7806. +#define PFKEYV2_REVISION 199806L
  7807. +
  7808. +#define SADB_RESERVED 0
  7809. +#define SADB_GETSPI 1
  7810. +#define SADB_UPDATE 2
  7811. +#define SADB_ADD 3
  7812. +#define SADB_DELETE 4
  7813. +#define SADB_GET 5
  7814. +#define SADB_ACQUIRE 6
  7815. +#define SADB_REGISTER 7
  7816. +#define SADB_EXPIRE 8
  7817. +#define SADB_FLUSH 9
  7818. +#define SADB_DUMP 10
  7819. +#define SADB_X_PROMISC 11
  7820. +#define SADB_X_PCHANGE 12
  7821. +#define SADB_X_GRPSA 13
  7822. +#define SADB_X_ADDFLOW 14
  7823. +#define SADB_X_DELFLOW 15
  7824. +#define SADB_X_DEBUG 16
  7825. +#define SADB_X_NAT_T_NEW_MAPPING 17
  7826. +#define SADB_MAX 17
  7827. +
  7828. +struct sadb_msg {
  7829. + uint8_t sadb_msg_version;
  7830. + uint8_t sadb_msg_type;
  7831. + uint8_t sadb_msg_errno;
  7832. + uint8_t sadb_msg_satype;
  7833. + uint16_t sadb_msg_len;
  7834. + uint16_t sadb_msg_reserved;
  7835. + uint32_t sadb_msg_seq;
  7836. + uint32_t sadb_msg_pid;
  7837. +};
  7838. +
  7839. +struct sadb_ext {
  7840. + uint16_t sadb_ext_len;
  7841. + uint16_t sadb_ext_type;
  7842. +};
  7843. +
  7844. +struct sadb_sa {
  7845. + uint16_t sadb_sa_len;
  7846. + uint16_t sadb_sa_exttype;
  7847. + uint32_t sadb_sa_spi;
  7848. + uint8_t sadb_sa_replay;
  7849. + uint8_t sadb_sa_state;
  7850. + uint8_t sadb_sa_auth;
  7851. + uint8_t sadb_sa_encrypt;
  7852. + uint32_t sadb_sa_flags;
  7853. + uint32_t /*IPsecSAref_t*/ sadb_x_sa_ref; /* 32 bits */
  7854. + uint8_t sadb_x_reserved[4];
  7855. +};
  7856. +
  7857. +struct sadb_sa_v1 {
  7858. + uint16_t sadb_sa_len;
  7859. + uint16_t sadb_sa_exttype;
  7860. + uint32_t sadb_sa_spi;
  7861. + uint8_t sadb_sa_replay;
  7862. + uint8_t sadb_sa_state;
  7863. + uint8_t sadb_sa_auth;
  7864. + uint8_t sadb_sa_encrypt;
  7865. + uint32_t sadb_sa_flags;
  7866. +};
  7867. +
  7868. +struct sadb_lifetime {
  7869. + uint16_t sadb_lifetime_len;
  7870. + uint16_t sadb_lifetime_exttype;
  7871. + uint32_t sadb_lifetime_allocations;
  7872. + uint64_t sadb_lifetime_bytes;
  7873. + uint64_t sadb_lifetime_addtime;
  7874. + uint64_t sadb_lifetime_usetime;
  7875. + uint32_t sadb_x_lifetime_packets;
  7876. + uint32_t sadb_x_lifetime_reserved;
  7877. +};
  7878. +
  7879. +struct sadb_address {
  7880. + uint16_t sadb_address_len;
  7881. + uint16_t sadb_address_exttype;
  7882. + uint8_t sadb_address_proto;
  7883. + uint8_t sadb_address_prefixlen;
  7884. + uint16_t sadb_address_reserved;
  7885. +};
  7886. +
  7887. +struct sadb_key {
  7888. + uint16_t sadb_key_len;
  7889. + uint16_t sadb_key_exttype;
  7890. + uint16_t sadb_key_bits;
  7891. + uint16_t sadb_key_reserved;
  7892. +};
  7893. +
  7894. +struct sadb_ident {
  7895. + uint16_t sadb_ident_len;
  7896. + uint16_t sadb_ident_exttype;
  7897. + uint16_t sadb_ident_type;
  7898. + uint16_t sadb_ident_reserved;
  7899. + uint64_t sadb_ident_id;
  7900. +};
  7901. +
  7902. +struct sadb_sens {
  7903. + uint16_t sadb_sens_len;
  7904. + uint16_t sadb_sens_exttype;
  7905. + uint32_t sadb_sens_dpd;
  7906. + uint8_t sadb_sens_sens_level;
  7907. + uint8_t sadb_sens_sens_len;
  7908. + uint8_t sadb_sens_integ_level;
  7909. + uint8_t sadb_sens_integ_len;
  7910. + uint32_t sadb_sens_reserved;
  7911. +};
  7912. +
  7913. +struct sadb_prop {
  7914. + uint16_t sadb_prop_len;
  7915. + uint16_t sadb_prop_exttype;
  7916. + uint8_t sadb_prop_replay;
  7917. + uint8_t sadb_prop_reserved[3];
  7918. +};
  7919. +
  7920. +struct sadb_comb {
  7921. + uint8_t sadb_comb_auth;
  7922. + uint8_t sadb_comb_encrypt;
  7923. + uint16_t sadb_comb_flags;
  7924. + uint16_t sadb_comb_auth_minbits;
  7925. + uint16_t sadb_comb_auth_maxbits;
  7926. + uint16_t sadb_comb_encrypt_minbits;
  7927. + uint16_t sadb_comb_encrypt_maxbits;
  7928. + uint32_t sadb_comb_reserved;
  7929. + uint32_t sadb_comb_soft_allocations;
  7930. + uint32_t sadb_comb_hard_allocations;
  7931. + uint64_t sadb_comb_soft_bytes;
  7932. + uint64_t sadb_comb_hard_bytes;
  7933. + uint64_t sadb_comb_soft_addtime;
  7934. + uint64_t sadb_comb_hard_addtime;
  7935. + uint64_t sadb_comb_soft_usetime;
  7936. + uint64_t sadb_comb_hard_usetime;
  7937. + uint32_t sadb_x_comb_soft_packets;
  7938. + uint32_t sadb_x_comb_hard_packets;
  7939. +};
  7940. +
  7941. +struct sadb_supported {
  7942. + uint16_t sadb_supported_len;
  7943. + uint16_t sadb_supported_exttype;
  7944. + uint32_t sadb_supported_reserved;
  7945. +};
  7946. +
  7947. +struct sadb_alg {
  7948. + uint8_t sadb_alg_id;
  7949. + uint8_t sadb_alg_ivlen;
  7950. + uint16_t sadb_alg_minbits;
  7951. + uint16_t sadb_alg_maxbits;
  7952. + uint16_t sadb_alg_reserved;
  7953. +};
  7954. +
  7955. +struct sadb_spirange {
  7956. + uint16_t sadb_spirange_len;
  7957. + uint16_t sadb_spirange_exttype;
  7958. + uint32_t sadb_spirange_min;
  7959. + uint32_t sadb_spirange_max;
  7960. + uint32_t sadb_spirange_reserved;
  7961. +};
  7962. +
  7963. +struct sadb_x_kmprivate {
  7964. + uint16_t sadb_x_kmprivate_len;
  7965. + uint16_t sadb_x_kmprivate_exttype;
  7966. + uint32_t sadb_x_kmprivate_reserved;
  7967. +};
  7968. +
  7969. +struct sadb_x_satype {
  7970. + uint16_t sadb_x_satype_len;
  7971. + uint16_t sadb_x_satype_exttype;
  7972. + uint8_t sadb_x_satype_satype;
  7973. + uint8_t sadb_x_satype_reserved[3];
  7974. +};
  7975. +
  7976. +struct sadb_x_policy {
  7977. + uint16_t sadb_x_policy_len;
  7978. + uint16_t sadb_x_policy_exttype;
  7979. + uint16_t sadb_x_policy_type;
  7980. + uint8_t sadb_x_policy_dir;
  7981. + uint8_t sadb_x_policy_reserved;
  7982. + uint32_t sadb_x_policy_id;
  7983. + uint32_t sadb_x_policy_reserved2;
  7984. +};
  7985. +
  7986. +struct sadb_x_debug {
  7987. + uint16_t sadb_x_debug_len;
  7988. + uint16_t sadb_x_debug_exttype;
  7989. + uint32_t sadb_x_debug_tunnel;
  7990. + uint32_t sadb_x_debug_netlink;
  7991. + uint32_t sadb_x_debug_xform;
  7992. + uint32_t sadb_x_debug_eroute;
  7993. + uint32_t sadb_x_debug_spi;
  7994. + uint32_t sadb_x_debug_radij;
  7995. + uint32_t sadb_x_debug_esp;
  7996. + uint32_t sadb_x_debug_ah;
  7997. + uint32_t sadb_x_debug_rcv;
  7998. + uint32_t sadb_x_debug_pfkey;
  7999. + uint32_t sadb_x_debug_ipcomp;
  8000. + uint32_t sadb_x_debug_verbose;
  8001. + uint8_t sadb_x_debug_reserved[4];
  8002. +};
  8003. +
  8004. +struct sadb_x_nat_t_type {
  8005. + uint16_t sadb_x_nat_t_type_len;
  8006. + uint16_t sadb_x_nat_t_type_exttype;
  8007. + uint8_t sadb_x_nat_t_type_type;
  8008. + uint8_t sadb_x_nat_t_type_reserved[3];
  8009. +};
  8010. +struct sadb_x_nat_t_port {
  8011. + uint16_t sadb_x_nat_t_port_len;
  8012. + uint16_t sadb_x_nat_t_port_exttype;
  8013. + uint16_t sadb_x_nat_t_port_port;
  8014. + uint16_t sadb_x_nat_t_port_reserved;
  8015. +};
  8016. +
  8017. +/*
  8018. + * A protocol structure for passing through the transport level
  8019. + * protocol. It contains more fields than are actually used/needed
  8020. + * but it is this way to be compatible with the structure used in
  8021. + * OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/sys/net/pfkeyv2.h)
  8022. + */
  8023. +struct sadb_protocol {
  8024. + uint16_t sadb_protocol_len;
  8025. + uint16_t sadb_protocol_exttype;
  8026. + uint8_t sadb_protocol_proto;
  8027. + uint8_t sadb_protocol_direction;
  8028. + uint8_t sadb_protocol_flags;
  8029. + uint8_t sadb_protocol_reserved2;
  8030. +};
  8031. +
  8032. +#define SADB_EXT_RESERVED 0
  8033. +#define SADB_EXT_SA 1
  8034. +#define SADB_EXT_LIFETIME_CURRENT 2
  8035. +#define SADB_EXT_LIFETIME_HARD 3
  8036. +#define SADB_EXT_LIFETIME_SOFT 4
  8037. +#define SADB_EXT_ADDRESS_SRC 5
  8038. +#define SADB_EXT_ADDRESS_DST 6
  8039. +#define SADB_EXT_ADDRESS_PROXY 7
  8040. +#define SADB_EXT_KEY_AUTH 8
  8041. +#define SADB_EXT_KEY_ENCRYPT 9
  8042. +#define SADB_EXT_IDENTITY_SRC 10
  8043. +#define SADB_EXT_IDENTITY_DST 11
  8044. +#define SADB_EXT_SENSITIVITY 12
  8045. +#define SADB_EXT_PROPOSAL 13
  8046. +#define SADB_EXT_SUPPORTED_AUTH 14
  8047. +#define SADB_EXT_SUPPORTED_ENCRYPT 15
  8048. +#define SADB_EXT_SPIRANGE 16
  8049. +#define SADB_X_EXT_KMPRIVATE 17
  8050. +#define SADB_X_EXT_SATYPE2 18
  8051. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8052. +#define SADB_X_EXT_POLICY 18
  8053. +#endif
  8054. +#define SADB_X_EXT_SA2 19
  8055. +#define SADB_X_EXT_ADDRESS_DST2 20
  8056. +#define SADB_X_EXT_ADDRESS_SRC_FLOW 21
  8057. +#define SADB_X_EXT_ADDRESS_DST_FLOW 22
  8058. +#define SADB_X_EXT_ADDRESS_SRC_MASK 23
  8059. +#define SADB_X_EXT_ADDRESS_DST_MASK 24
  8060. +#define SADB_X_EXT_DEBUG 25
  8061. +#define SADB_X_EXT_PROTOCOL 26
  8062. +#define SADB_X_EXT_NAT_T_TYPE 27
  8063. +#define SADB_X_EXT_NAT_T_SPORT 28
  8064. +#define SADB_X_EXT_NAT_T_DPORT 29
  8065. +#define SADB_X_EXT_NAT_T_OA 30
  8066. +#define SADB_EXT_MAX 30
  8067. +
  8068. +/* SADB_X_DELFLOW required over and above SADB_X_SAFLAGS_CLEARFLOW */
  8069. +#define SADB_X_EXT_ADDRESS_DELFLOW \
  8070. + ( (1<<SADB_X_EXT_ADDRESS_SRC_FLOW) \
  8071. + | (1<<SADB_X_EXT_ADDRESS_DST_FLOW) \
  8072. + | (1<<SADB_X_EXT_ADDRESS_SRC_MASK) \
  8073. + | (1<<SADB_X_EXT_ADDRESS_DST_MASK))
  8074. +
  8075. +#define SADB_SATYPE_UNSPEC 0
  8076. +#define SADB_SATYPE_AH 2
  8077. +#define SADB_SATYPE_ESP 3
  8078. +#define SADB_SATYPE_RSVP 5
  8079. +#define SADB_SATYPE_OSPFV2 6
  8080. +#define SADB_SATYPE_RIPV2 7
  8081. +#define SADB_SATYPE_MIP 8
  8082. +#define SADB_X_SATYPE_IPIP 9
  8083. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8084. +#define SADB_X_SATYPE_IPCOMP 9 /* ICK! */
  8085. +#endif
  8086. +#define SADB_X_SATYPE_COMP 10
  8087. +#define SADB_X_SATYPE_INT 11
  8088. +#define SADB_SATYPE_MAX 11
  8089. +
  8090. +enum sadb_sastate {
  8091. + SADB_SASTATE_LARVAL=0,
  8092. + SADB_SASTATE_MATURE=1,
  8093. + SADB_SASTATE_DYING=2,
  8094. + SADB_SASTATE_DEAD=3
  8095. +};
  8096. +#define SADB_SASTATE_MAX 3
  8097. +
  8098. +#define SADB_SAFLAGS_PFS 1
  8099. +#define SADB_X_SAFLAGS_REPLACEFLOW 2
  8100. +#define SADB_X_SAFLAGS_CLEARFLOW 4
  8101. +#define SADB_X_SAFLAGS_INFLOW 8
  8102. +
  8103. +/* not obvious, but these are the same values as used in isakmp,
  8104. + * and in freeswan/ipsec_policy.h. If you need to add any, they
  8105. + * should be added as according to
  8106. + * http://www.iana.org/assignments/isakmp-registry
  8107. + *
  8108. + * and if not, then please try to use a private-use value, and
  8109. + * consider asking IANA to assign a value.
  8110. + */
  8111. +#define SADB_AALG_NONE 0
  8112. +#define SADB_AALG_MD5HMAC 2
  8113. +#define SADB_AALG_SHA1HMAC 3
  8114. +#define SADB_X_AALG_SHA2_256HMAC 5
  8115. +#define SADB_X_AALG_SHA2_384HMAC 6
  8116. +#define SADB_X_AALG_SHA2_512HMAC 7
  8117. +#define SADB_X_AALG_RIPEMD160HMAC 8
  8118. +#define SADB_X_AALG_NULL 251 /* kame */
  8119. +#define SADB_AALG_MAX 251
  8120. +
  8121. +#define SADB_EALG_NONE 0
  8122. +#define SADB_EALG_DESCBC 2
  8123. +#define SADB_EALG_3DESCBC 3
  8124. +#define SADB_X_EALG_CASTCBC 6
  8125. +#define SADB_X_EALG_BLOWFISHCBC 7
  8126. +#define SADB_EALG_NULL 11
  8127. +#define SADB_X_EALG_AESCBC 12
  8128. +#define SADB_EALG_MAX 255
  8129. +
  8130. +#define SADB_X_CALG_NONE 0
  8131. +#define SADB_X_CALG_OUI 1
  8132. +#define SADB_X_CALG_DEFLATE 2
  8133. +#define SADB_X_CALG_LZS 3
  8134. +#define SADB_X_CALG_V42BIS 4
  8135. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8136. +#define SADB_X_CALG_LZJH 4
  8137. +#endif
  8138. +#define SADB_X_CALG_MAX 4
  8139. +
  8140. +#define SADB_X_TALG_NONE 0
  8141. +#define SADB_X_TALG_IPv4_in_IPv4 1
  8142. +#define SADB_X_TALG_IPv6_in_IPv4 2
  8143. +#define SADB_X_TALG_IPv4_in_IPv6 3
  8144. +#define SADB_X_TALG_IPv6_in_IPv6 4
  8145. +#define SADB_X_TALG_MAX 4
  8146. +
  8147. +
  8148. +#define SADB_IDENTTYPE_RESERVED 0
  8149. +#define SADB_IDENTTYPE_PREFIX 1
  8150. +#define SADB_IDENTTYPE_FQDN 2
  8151. +#define SADB_IDENTTYPE_USERFQDN 3
  8152. +#define SADB_X_IDENTTYPE_CONNECTION 4
  8153. +#define SADB_IDENTTYPE_MAX 4
  8154. +
  8155. +#define SADB_KEY_FLAGS_MAX 0
  8156. +#endif /* __PFKEY_V2_H */
  8157. +
  8158. +/*
  8159. + * $Log: pfkeyv2.h,v $
  8160. + * Revision 1.31 2005/04/14 01:14:54 mcr
  8161. + * change sadb_state to an enum.
  8162. + *
  8163. + * Revision 1.30 2004/04/06 02:49:00 mcr
  8164. + * pullup of algo code from alg-branch.
  8165. + *
  8166. + * Revision 1.29 2003/12/22 21:35:58 mcr
  8167. + * new patches from Dr{Who}.
  8168. + *
  8169. + * Revision 1.28 2003/12/22 19:33:15 mcr
  8170. + * added 0.6c NAT-T patch.
  8171. + *
  8172. + * Revision 1.27 2003/12/10 01:20:01 mcr
  8173. + * NAT-traversal patches to KLIPS.
  8174. + *
  8175. + * Revision 1.26 2003/10/31 02:26:44 mcr
  8176. + * pulled up port-selector patches.
  8177. + *
  8178. + * Revision 1.25.4.1 2003/09/21 13:59:34 mcr
  8179. + * pre-liminary X.509 patch - does not yet pass tests.
  8180. + *
  8181. + * Revision 1.25 2003/07/31 23:59:17 mcr
  8182. + * re-introduce kernel 2.6 duplicate values for now.
  8183. + * hope to get them changed!
  8184. + *
  8185. + * Revision 1.24 2003/07/31 22:55:27 mcr
  8186. + * added some definitions to keep pfkeyv2.h files in sync.
  8187. + *
  8188. + * Revision 1.23 2003/05/11 00:43:48 mcr
  8189. + * added comment about origin of values used
  8190. + *
  8191. + * Revision 1.22 2003/01/30 02:31:34 rgb
  8192. + *
  8193. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  8194. + *
  8195. + * Revision 1.21 2002/12/16 19:26:49 mcr
  8196. + * added definition of FS 1.xx sadb structure
  8197. + *
  8198. + * Revision 1.20 2002/09/20 15:40:25 rgb
  8199. + * Added sadb_x_sa_ref to struct sadb_sa.
  8200. + *
  8201. + * Revision 1.19 2002/04/24 07:36:49 mcr
  8202. + * Moved from ./lib/pfkeyv2.h,v
  8203. + *
  8204. + * Revision 1.18 2001/11/06 19:47:47 rgb
  8205. + * Added packet parameter to lifetime and comb structures.
  8206. + *
  8207. + * Revision 1.17 2001/09/08 21:13:35 rgb
  8208. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  8209. + *
  8210. + * Revision 1.16 2001/07/06 19:49:46 rgb
  8211. + * Added SADB_X_SAFLAGS_INFLOW for supporting incoming policy checks.
  8212. + *
  8213. + * Revision 1.15 2001/02/26 20:00:43 rgb
  8214. + * Added internal IP protocol 61 for magic SAs.
  8215. + *
  8216. + * Revision 1.14 2001/02/08 18:51:05 rgb
  8217. + * Include RFC document title and appendix subsection title.
  8218. + *
  8219. + * Revision 1.13 2000/10/10 20:10:20 rgb
  8220. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  8221. + *
  8222. + * Revision 1.12 2000/09/15 06:41:50 rgb
  8223. + * Added V42BIS constant.
  8224. + *
  8225. + * Revision 1.11 2000/09/12 22:35:37 rgb
  8226. + * Restructured to remove unused extensions from CLEARFLOW messages.
  8227. + *
  8228. + * Revision 1.10 2000/09/12 18:50:09 rgb
  8229. + * Added IPIP tunnel types as algo support.
  8230. + *
  8231. + * Revision 1.9 2000/08/21 16:47:19 rgb
  8232. + * Added SADB_X_CALG_* macros for IPCOMP.
  8233. + *
  8234. + * Revision 1.8 2000/08/09 20:43:34 rgb
  8235. + * Fixed bitmask value for SADB_X_SAFLAGS_CLEAREROUTE.
  8236. + *
  8237. + * Revision 1.7 2000/01/21 06:28:37 rgb
  8238. + * Added flow add/delete message type macros.
  8239. + * Added flow address extension type macros.
  8240. + * Tidied up spacing.
  8241. + * Added klipsdebug switching capability.
  8242. + *
  8243. + * Revision 1.6 1999/11/27 11:56:08 rgb
  8244. + * Add SADB_X_SATYPE_COMP for compression, eventually.
  8245. + *
  8246. + * Revision 1.5 1999/11/23 22:23:16 rgb
  8247. + * This file has been moved in the distribution from klips/net/ipsec to
  8248. + * lib.
  8249. + *
  8250. + * Revision 1.4 1999/04/29 15:23:29 rgb
  8251. + * Add GRPSA support.
  8252. + * Add support for a second SATYPE, SA and DST_ADDRESS.
  8253. + * Add IPPROTO_IPIP support.
  8254. + *
  8255. + * Revision 1.3 1999/04/15 17:58:08 rgb
  8256. + * Add RCSID labels.
  8257. + *
  8258. + */
  8259. --- /dev/null Tue Mar 11 13:02:56 2003
  8260. +++ linux/include/zlib/zconf.h Mon Feb 9 13:51:03 2004
  8261. @@ -0,0 +1,309 @@
  8262. +/* zconf.h -- configuration of the zlib compression library
  8263. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  8264. + * For conditions of distribution and use, see copyright notice in zlib.h
  8265. + */
  8266. +
  8267. +/* @(#) $Id: zconf.h,v 1.4 2004/07/10 07:48:40 mcr Exp $ */
  8268. +
  8269. +#ifndef _ZCONF_H
  8270. +#define _ZCONF_H
  8271. +
  8272. +/*
  8273. + * If you *really* need a unique prefix for all types and library functions,
  8274. + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
  8275. + */
  8276. +#ifdef IPCOMP_PREFIX
  8277. +# define deflateInit_ ipcomp_deflateInit_
  8278. +# define deflate ipcomp_deflate
  8279. +# define deflateEnd ipcomp_deflateEnd
  8280. +# define inflateInit_ ipcomp_inflateInit_
  8281. +# define inflate ipcomp_inflate
  8282. +# define inflateEnd ipcomp_inflateEnd
  8283. +# define deflateInit2_ ipcomp_deflateInit2_
  8284. +# define deflateSetDictionary ipcomp_deflateSetDictionary
  8285. +# define deflateCopy ipcomp_deflateCopy
  8286. +# define deflateReset ipcomp_deflateReset
  8287. +# define deflateParams ipcomp_deflateParams
  8288. +# define inflateInit2_ ipcomp_inflateInit2_
  8289. +# define inflateSetDictionary ipcomp_inflateSetDictionary
  8290. +# define inflateSync ipcomp_inflateSync
  8291. +# define inflateSyncPoint ipcomp_inflateSyncPoint
  8292. +# define inflateReset ipcomp_inflateReset
  8293. +# define compress ipcomp_compress
  8294. +# define compress2 ipcomp_compress2
  8295. +# define uncompress ipcomp_uncompress
  8296. +# define adler32 ipcomp_adler32
  8297. +# define crc32 ipcomp_crc32
  8298. +# define get_crc_table ipcomp_get_crc_table
  8299. +/* SSS: these also need to be prefixed to avoid clash with ppp_deflate and ext2compression */
  8300. +# define inflate_blocks ipcomp_deflate_blocks
  8301. +# define inflate_blocks_free ipcomp_deflate_blocks_free
  8302. +# define inflate_blocks_new ipcomp_inflate_blocks_new
  8303. +# define inflate_blocks_reset ipcomp_inflate_blocks_reset
  8304. +# define inflate_blocks_sync_point ipcomp_inflate_blocks_sync_point
  8305. +# define inflate_set_dictionary ipcomp_inflate_set_dictionary
  8306. +# define inflate_codes ipcomp_inflate_codes
  8307. +# define inflate_codes_free ipcomp_inflate_codes_free
  8308. +# define inflate_codes_new ipcomp_inflate_codes_new
  8309. +# define inflate_fast ipcomp_inflate_fast
  8310. +# define inflate_trees_bits ipcomp_inflate_trees_bits
  8311. +# define inflate_trees_dynamic ipcomp_inflate_trees_dynamic
  8312. +# define inflate_trees_fixed ipcomp_inflate_trees_fixed
  8313. +# define inflate_flush ipcomp_inflate_flush
  8314. +# define inflate_mask ipcomp_inflate_mask
  8315. +# define _dist_code _ipcomp_dist_code
  8316. +# define _length_code _ipcomp_length_code
  8317. +# define _tr_align _ipcomp_tr_align
  8318. +# define _tr_flush_block _ipcomp_tr_flush_block
  8319. +# define _tr_init _ipcomp_tr_init
  8320. +# define _tr_stored_block _ipcomp_tr_stored_block
  8321. +# define _tr_tally _ipcomp_tr_tally
  8322. +# define zError ipcomp_zError
  8323. +# define z_errmsg ipcomp_z_errmsg
  8324. +# define zlibVersion ipcomp_zlibVersion
  8325. +# define match_init ipcomp_match_init
  8326. +# define longest_match ipcomp_longest_match
  8327. +#endif
  8328. +
  8329. +#ifdef Z_PREFIX
  8330. +# define Byte z_Byte
  8331. +# define uInt z_uInt
  8332. +# define uLong z_uLong
  8333. +# define Bytef z_Bytef
  8334. +# define charf z_charf
  8335. +# define intf z_intf
  8336. +# define uIntf z_uIntf
  8337. +# define uLongf z_uLongf
  8338. +# define voidpf z_voidpf
  8339. +# define voidp z_voidp
  8340. +#endif
  8341. +
  8342. +#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  8343. +# define WIN32
  8344. +#endif
  8345. +#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
  8346. +# ifndef __32BIT__
  8347. +# define __32BIT__
  8348. +# endif
  8349. +#endif
  8350. +#if defined(__MSDOS__) && !defined(MSDOS)
  8351. +# define MSDOS
  8352. +#endif
  8353. +
  8354. +/*
  8355. + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  8356. + * than 64k bytes at a time (needed on systems with 16-bit int).
  8357. + */
  8358. +#if defined(MSDOS) && !defined(__32BIT__)
  8359. +# define MAXSEG_64K
  8360. +#endif
  8361. +#ifdef MSDOS
  8362. +# define UNALIGNED_OK
  8363. +#endif
  8364. +
  8365. +#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
  8366. +# define STDC
  8367. +#endif
  8368. +#if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
  8369. +# ifndef STDC
  8370. +# define STDC
  8371. +# endif
  8372. +#endif
  8373. +
  8374. +#ifndef STDC
  8375. +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
  8376. +# define const
  8377. +# endif
  8378. +#endif
  8379. +
  8380. +/* Some Mac compilers merge all .h files incorrectly: */
  8381. +#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
  8382. +# define NO_DUMMY_DECL
  8383. +#endif
  8384. +
  8385. +/* Old Borland C incorrectly complains about missing returns: */
  8386. +#if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
  8387. +# define NEED_DUMMY_RETURN
  8388. +#endif
  8389. +
  8390. +
  8391. +/* Maximum value for memLevel in deflateInit2 */
  8392. +#ifndef MAX_MEM_LEVEL
  8393. +# ifdef MAXSEG_64K
  8394. +# define MAX_MEM_LEVEL 8
  8395. +# else
  8396. +# define MAX_MEM_LEVEL 9
  8397. +# endif
  8398. +#endif
  8399. +
  8400. +/* Maximum value for windowBits in deflateInit2 and inflateInit2.
  8401. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
  8402. + * created by gzip. (Files created by minigzip can still be extracted by
  8403. + * gzip.)
  8404. + */
  8405. +#ifndef MAX_WBITS
  8406. +# define MAX_WBITS 15 /* 32K LZ77 window */
  8407. +#endif
  8408. +
  8409. +/* The memory requirements for deflate are (in bytes):
  8410. + (1 << (windowBits+2)) + (1 << (memLevel+9))
  8411. + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  8412. + plus a few kilobytes for small objects. For example, if you want to reduce
  8413. + the default memory requirements from 256K to 128K, compile with
  8414. + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
  8415. + Of course this will generally degrade compression (there's no free lunch).
  8416. +
  8417. + The memory requirements for inflate are (in bytes) 1 << windowBits
  8418. + that is, 32K for windowBits=15 (default value) plus a few kilobytes
  8419. + for small objects.
  8420. +*/
  8421. +
  8422. + /* Type declarations */
  8423. +
  8424. +#ifndef OF /* function prototypes */
  8425. +# ifdef STDC
  8426. +# define OF(args) args
  8427. +# else
  8428. +# define OF(args) ()
  8429. +# endif
  8430. +#endif
  8431. +
  8432. +/* The following definitions for FAR are needed only for MSDOS mixed
  8433. + * model programming (small or medium model with some far allocations).
  8434. + * This was tested only with MSC; for other MSDOS compilers you may have
  8435. + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
  8436. + * just define FAR to be empty.
  8437. + */
  8438. +#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
  8439. + /* MSC small or medium model */
  8440. +# define SMALL_MEDIUM
  8441. +# ifdef _MSC_VER
  8442. +# define FAR _far
  8443. +# else
  8444. +# define FAR far
  8445. +# endif
  8446. +#endif
  8447. +#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
  8448. +# ifndef __32BIT__
  8449. +# define SMALL_MEDIUM
  8450. +# define FAR _far
  8451. +# endif
  8452. +#endif
  8453. +
  8454. +/* Compile with -DZLIB_DLL for Windows DLL support */
  8455. +#if defined(ZLIB_DLL)
  8456. +# if defined(_WINDOWS) || defined(WINDOWS)
  8457. +# ifdef FAR
  8458. +# undef FAR
  8459. +# endif
  8460. +# include <windows.h>
  8461. +# define ZEXPORT WINAPI
  8462. +# ifdef WIN32
  8463. +# define ZEXPORTVA WINAPIV
  8464. +# else
  8465. +# define ZEXPORTVA FAR _cdecl _export
  8466. +# endif
  8467. +# endif
  8468. +# if defined (__BORLANDC__)
  8469. +# if (__BORLANDC__ >= 0x0500) && defined (WIN32)
  8470. +# include <windows.h>
  8471. +# define ZEXPORT __declspec(dllexport) WINAPI
  8472. +# define ZEXPORTRVA __declspec(dllexport) WINAPIV
  8473. +# else
  8474. +# if defined (_Windows) && defined (__DLL__)
  8475. +# define ZEXPORT _export
  8476. +# define ZEXPORTVA _export
  8477. +# endif
  8478. +# endif
  8479. +# endif
  8480. +#endif
  8481. +
  8482. +#if defined (__BEOS__)
  8483. +# if defined (ZLIB_DLL)
  8484. +# define ZEXTERN extern __declspec(dllexport)
  8485. +# else
  8486. +# define ZEXTERN extern __declspec(dllimport)
  8487. +# endif
  8488. +#endif
  8489. +
  8490. +#ifndef ZEXPORT
  8491. +# define ZEXPORT
  8492. +#endif
  8493. +#ifndef ZEXPORTVA
  8494. +# define ZEXPORTVA
  8495. +#endif
  8496. +#ifndef ZEXTERN
  8497. +# define ZEXTERN extern
  8498. +#endif
  8499. +
  8500. +#ifndef FAR
  8501. +# define FAR
  8502. +#endif
  8503. +
  8504. +#if !defined(MACOS) && !defined(TARGET_OS_MAC)
  8505. +typedef unsigned char Byte; /* 8 bits */
  8506. +#endif
  8507. +typedef unsigned int uInt; /* 16 bits or more */
  8508. +typedef unsigned long uLong; /* 32 bits or more */
  8509. +
  8510. +#ifdef SMALL_MEDIUM
  8511. + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
  8512. +# define Bytef Byte FAR
  8513. +#else
  8514. + typedef Byte FAR Bytef;
  8515. +#endif
  8516. +typedef char FAR charf;
  8517. +typedef int FAR intf;
  8518. +typedef uInt FAR uIntf;
  8519. +typedef uLong FAR uLongf;
  8520. +
  8521. +#ifdef STDC
  8522. + typedef void FAR *voidpf;
  8523. + typedef void *voidp;
  8524. +#else
  8525. + typedef Byte FAR *voidpf;
  8526. + typedef Byte *voidp;
  8527. +#endif
  8528. +
  8529. +#ifdef HAVE_UNISTD_H
  8530. +# include <sys/types.h> /* for off_t */
  8531. +# include <unistd.h> /* for SEEK_* and off_t */
  8532. +# define z_off_t off_t
  8533. +#endif
  8534. +#ifndef SEEK_SET
  8535. +# define SEEK_SET 0 /* Seek from beginning of file. */
  8536. +# define SEEK_CUR 1 /* Seek from current position. */
  8537. +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
  8538. +#endif
  8539. +#ifndef z_off_t
  8540. +# define z_off_t long
  8541. +#endif
  8542. +
  8543. +/* MVS linker does not support external names larger than 8 bytes */
  8544. +#if defined(__MVS__)
  8545. +# pragma map(deflateInit_,"DEIN")
  8546. +# pragma map(deflateInit2_,"DEIN2")
  8547. +# pragma map(deflateEnd,"DEEND")
  8548. +# pragma map(inflateInit_,"ININ")
  8549. +# pragma map(inflateInit2_,"ININ2")
  8550. +# pragma map(inflateEnd,"INEND")
  8551. +# pragma map(inflateSync,"INSY")
  8552. +# pragma map(inflateSetDictionary,"INSEDI")
  8553. +# pragma map(inflate_blocks,"INBL")
  8554. +# pragma map(inflate_blocks_new,"INBLNE")
  8555. +# pragma map(inflate_blocks_free,"INBLFR")
  8556. +# pragma map(inflate_blocks_reset,"INBLRE")
  8557. +# pragma map(inflate_codes_free,"INCOFR")
  8558. +# pragma map(inflate_codes,"INCO")
  8559. +# pragma map(inflate_fast,"INFA")
  8560. +# pragma map(inflate_flush,"INFLU")
  8561. +# pragma map(inflate_mask,"INMA")
  8562. +# pragma map(inflate_set_dictionary,"INSEDI2")
  8563. +# pragma map(ipcomp_inflate_copyright,"INCOPY")
  8564. +# pragma map(inflate_trees_bits,"INTRBI")
  8565. +# pragma map(inflate_trees_dynamic,"INTRDY")
  8566. +# pragma map(inflate_trees_fixed,"INTRFI")
  8567. +# pragma map(inflate_trees_free,"INTRFR")
  8568. +#endif
  8569. +
  8570. +#endif /* _ZCONF_H */
  8571. --- /dev/null Tue Mar 11 13:02:56 2003
  8572. +++ linux/include/zlib/zlib.h Mon Feb 9 13:51:03 2004
  8573. @@ -0,0 +1,893 @@
  8574. +/* zlib.h -- interface of the 'zlib' general purpose compression library
  8575. + version 1.1.4, March 11th, 2002
  8576. +
  8577. + Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
  8578. +
  8579. + This software is provided 'as-is', without any express or implied
  8580. + warranty. In no event will the authors be held liable for any damages
  8581. + arising from the use of this software.
  8582. +
  8583. + Permission is granted to anyone to use this software for any purpose,
  8584. + including commercial applications, and to alter it and redistribute it
  8585. + freely, subject to the following restrictions:
  8586. +
  8587. + 1. The origin of this software must not be misrepresented; you must not
  8588. + claim that you wrote the original software. If you use this software
  8589. + in a product, an acknowledgment in the product documentation would be
  8590. + appreciated but is not required.
  8591. + 2. Altered source versions must be plainly marked as such, and must not be
  8592. + misrepresented as being the original software.
  8593. + 3. This notice may not be removed or altered from any source distribution.
  8594. +
  8595. + Jean-loup Gailly Mark Adler
  8596. + jloup@gzip.org madler@alumni.caltech.edu
  8597. +
  8598. +
  8599. + The data format used by the zlib library is described by RFCs (Request for
  8600. + Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  8601. + (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  8602. +*/
  8603. +
  8604. +#ifndef _ZLIB_H
  8605. +#define _ZLIB_H
  8606. +
  8607. +#include "zconf.h"
  8608. +
  8609. +#ifdef __cplusplus
  8610. +extern "C" {
  8611. +#endif
  8612. +
  8613. +#define ZLIB_VERSION "1.1.4"
  8614. +
  8615. +/*
  8616. + The 'zlib' compression library provides in-memory compression and
  8617. + decompression functions, including integrity checks of the uncompressed
  8618. + data. This version of the library supports only one compression method
  8619. + (deflation) but other algorithms will be added later and will have the same
  8620. + stream interface.
  8621. +
  8622. + Compression can be done in a single step if the buffers are large
  8623. + enough (for example if an input file is mmap'ed), or can be done by
  8624. + repeated calls of the compression function. In the latter case, the
  8625. + application must provide more input and/or consume the output
  8626. + (providing more output space) before each call.
  8627. +
  8628. + The library also supports reading and writing files in gzip (.gz) format
  8629. + with an interface similar to that of stdio.
  8630. +
  8631. + The library does not install any signal handler. The decoder checks
  8632. + the consistency of the compressed data, so the library should never
  8633. + crash even in case of corrupted input.
  8634. +*/
  8635. +
  8636. +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
  8637. +typedef void (*free_func) OF((voidpf opaque, voidpf address));
  8638. +
  8639. +struct internal_state;
  8640. +
  8641. +typedef struct z_stream_s {
  8642. + Bytef *next_in; /* next input byte */
  8643. + uInt avail_in; /* number of bytes available at next_in */
  8644. + uLong total_in; /* total nb of input bytes read so far */
  8645. +
  8646. + Bytef *next_out; /* next output byte should be put there */
  8647. + uInt avail_out; /* remaining free space at next_out */
  8648. + uLong total_out; /* total nb of bytes output so far */
  8649. +
  8650. + const char *msg; /* last error message, NULL if no error */
  8651. + struct internal_state FAR *state; /* not visible by applications */
  8652. +
  8653. + alloc_func zalloc; /* used to allocate the internal state */
  8654. + free_func zfree; /* used to free the internal state */
  8655. + voidpf opaque; /* private data object passed to zalloc and zfree */
  8656. +
  8657. + int data_type; /* best guess about the data type: ascii or binary */
  8658. + uLong adler; /* adler32 value of the uncompressed data */
  8659. + uLong reserved; /* reserved for future use */
  8660. +} z_stream;
  8661. +
  8662. +typedef z_stream FAR *z_streamp;
  8663. +
  8664. +/*
  8665. + The application must update next_in and avail_in when avail_in has
  8666. + dropped to zero. It must update next_out and avail_out when avail_out
  8667. + has dropped to zero. The application must initialize zalloc, zfree and
  8668. + opaque before calling the init function. All other fields are set by the
  8669. + compression library and must not be updated by the application.
  8670. +
  8671. + The opaque value provided by the application will be passed as the first
  8672. + parameter for calls of zalloc and zfree. This can be useful for custom
  8673. + memory management. The compression library attaches no meaning to the
  8674. + opaque value.
  8675. +
  8676. + zalloc must return Z_NULL if there is not enough memory for the object.
  8677. + If zlib is used in a multi-threaded application, zalloc and zfree must be
  8678. + thread safe.
  8679. +
  8680. + On 16-bit systems, the functions zalloc and zfree must be able to allocate
  8681. + exactly 65536 bytes, but will not be required to allocate more than this
  8682. + if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  8683. + pointers returned by zalloc for objects of exactly 65536 bytes *must*
  8684. + have their offset normalized to zero. The default allocation function
  8685. + provided by this library ensures this (see zutil.c). To reduce memory
  8686. + requirements and avoid any allocation of 64K objects, at the expense of
  8687. + compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  8688. +
  8689. + The fields total_in and total_out can be used for statistics or
  8690. + progress reports. After compression, total_in holds the total size of
  8691. + the uncompressed data and may be saved for use in the decompressor
  8692. + (particularly if the decompressor wants to decompress everything in
  8693. + a single step).
  8694. +*/
  8695. +
  8696. + /* constants */
  8697. +
  8698. +#define Z_NO_FLUSH 0
  8699. +#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
  8700. +#define Z_SYNC_FLUSH 2
  8701. +#define Z_FULL_FLUSH 3
  8702. +#define Z_FINISH 4
  8703. +/* Allowed flush values; see deflate() below for details */
  8704. +
  8705. +#define Z_OK 0
  8706. +#define Z_STREAM_END 1
  8707. +#define Z_NEED_DICT 2
  8708. +#define Z_ERRNO (-1)
  8709. +#define Z_STREAM_ERROR (-2)
  8710. +#define Z_DATA_ERROR (-3)
  8711. +#define Z_MEM_ERROR (-4)
  8712. +#define Z_BUF_ERROR (-5)
  8713. +#define Z_VERSION_ERROR (-6)
  8714. +/* Return codes for the compression/decompression functions. Negative
  8715. + * values are errors, positive values are used for special but normal events.
  8716. + */
  8717. +
  8718. +#define Z_NO_COMPRESSION 0
  8719. +#define Z_BEST_SPEED 1
  8720. +#define Z_BEST_COMPRESSION 9
  8721. +#define Z_DEFAULT_COMPRESSION (-1)
  8722. +/* compression levels */
  8723. +
  8724. +#define Z_FILTERED 1
  8725. +#define Z_HUFFMAN_ONLY 2
  8726. +#define Z_DEFAULT_STRATEGY 0
  8727. +/* compression strategy; see deflateInit2() below for details */
  8728. +
  8729. +#define Z_BINARY 0
  8730. +#define Z_ASCII 1
  8731. +#define Z_UNKNOWN 2
  8732. +/* Possible values of the data_type field */
  8733. +
  8734. +#define Z_DEFLATED 8
  8735. +/* The deflate compression method (the only one supported in this version) */
  8736. +
  8737. +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
  8738. +
  8739. +#define zlib_version zlibVersion()
  8740. +/* for compatibility with versions < 1.0.2 */
  8741. +
  8742. + /* basic functions */
  8743. +
  8744. +ZEXTERN const char * ZEXPORT zlibVersion OF((void));
  8745. +/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
  8746. + If the first character differs, the library code actually used is
  8747. + not compatible with the zlib.h header file used by the application.
  8748. + This check is automatically made by deflateInit and inflateInit.
  8749. + */
  8750. +
  8751. +/*
  8752. +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
  8753. +
  8754. + Initializes the internal stream state for compression. The fields
  8755. + zalloc, zfree and opaque must be initialized before by the caller.
  8756. + If zalloc and zfree are set to Z_NULL, deflateInit updates them to
  8757. + use default allocation functions.
  8758. +
  8759. + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
  8760. + 1 gives best speed, 9 gives best compression, 0 gives no compression at
  8761. + all (the input data is simply copied a block at a time).
  8762. + Z_DEFAULT_COMPRESSION requests a default compromise between speed and
  8763. + compression (currently equivalent to level 6).
  8764. +
  8765. + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  8766. + enough memory, Z_STREAM_ERROR if level is not a valid compression level,
  8767. + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
  8768. + with the version assumed by the caller (ZLIB_VERSION).
  8769. + msg is set to null if there is no error message. deflateInit does not
  8770. + perform any compression: this will be done by deflate().
  8771. +*/
  8772. +
  8773. +
  8774. +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
  8775. +/*
  8776. + deflate compresses as much data as possible, and stops when the input
  8777. + buffer becomes empty or the output buffer becomes full. It may introduce some
  8778. + output latency (reading input without producing any output) except when
  8779. + forced to flush.
  8780. +
  8781. + The detailed semantics are as follows. deflate performs one or both of the
  8782. + following actions:
  8783. +
  8784. + - Compress more input starting at next_in and update next_in and avail_in
  8785. + accordingly. If not all input can be processed (because there is not
  8786. + enough room in the output buffer), next_in and avail_in are updated and
  8787. + processing will resume at this point for the next call of deflate().
  8788. +
  8789. + - Provide more output starting at next_out and update next_out and avail_out
  8790. + accordingly. This action is forced if the parameter flush is non zero.
  8791. + Forcing flush frequently degrades the compression ratio, so this parameter
  8792. + should be set only when necessary (in interactive applications).
  8793. + Some output may be provided even if flush is not set.
  8794. +
  8795. + Before the call of deflate(), the application should ensure that at least
  8796. + one of the actions is possible, by providing more input and/or consuming
  8797. + more output, and updating avail_in or avail_out accordingly; avail_out
  8798. + should never be zero before the call. The application can consume the
  8799. + compressed output when it wants, for example when the output buffer is full
  8800. + (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
  8801. + and with zero avail_out, it must be called again after making room in the
  8802. + output buffer because there might be more output pending.
  8803. +
  8804. + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
  8805. + flushed to the output buffer and the output is aligned on a byte boundary, so
  8806. + that the decompressor can get all input data available so far. (In particular
  8807. + avail_in is zero after the call if enough output space has been provided
  8808. + before the call.) Flushing may degrade compression for some compression
  8809. + algorithms and so it should be used only when necessary.
  8810. +
  8811. + If flush is set to Z_FULL_FLUSH, all output is flushed as with
  8812. + Z_SYNC_FLUSH, and the compression state is reset so that decompression can
  8813. + restart from this point if previous compressed data has been damaged or if
  8814. + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
  8815. + the compression.
  8816. +
  8817. + If deflate returns with avail_out == 0, this function must be called again
  8818. + with the same value of the flush parameter and more output space (updated
  8819. + avail_out), until the flush is complete (deflate returns with non-zero
  8820. + avail_out).
  8821. +
  8822. + If the parameter flush is set to Z_FINISH, pending input is processed,
  8823. + pending output is flushed and deflate returns with Z_STREAM_END if there
  8824. + was enough output space; if deflate returns with Z_OK, this function must be
  8825. + called again with Z_FINISH and more output space (updated avail_out) but no
  8826. + more input data, until it returns with Z_STREAM_END or an error. After
  8827. + deflate has returned Z_STREAM_END, the only possible operations on the
  8828. + stream are deflateReset or deflateEnd.
  8829. +
  8830. + Z_FINISH can be used immediately after deflateInit if all the compression
  8831. + is to be done in a single step. In this case, avail_out must be at least
  8832. + 0.1% larger than avail_in plus 12 bytes. If deflate does not return
  8833. + Z_STREAM_END, then it must be called again as described above.
  8834. +
  8835. + deflate() sets strm->adler to the adler32 checksum of all input read
  8836. + so far (that is, total_in bytes).
  8837. +
  8838. + deflate() may update data_type if it can make a good guess about
  8839. + the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
  8840. + binary. This field is only for information purposes and does not affect
  8841. + the compression algorithm in any manner.
  8842. +
  8843. + deflate() returns Z_OK if some progress has been made (more input
  8844. + processed or more output produced), Z_STREAM_END if all input has been
  8845. + consumed and all output has been produced (only when flush is set to
  8846. + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
  8847. + if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
  8848. + (for example avail_in or avail_out was zero).
  8849. +*/
  8850. +
  8851. +
  8852. +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
  8853. +/*
  8854. + All dynamically allocated data structures for this stream are freed.
  8855. + This function discards any unprocessed input and does not flush any
  8856. + pending output.
  8857. +
  8858. + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
  8859. + stream state was inconsistent, Z_DATA_ERROR if the stream was freed
  8860. + prematurely (some input or output was discarded). In the error case,
  8861. + msg may be set but then points to a static string (which must not be
  8862. + deallocated).
  8863. +*/
  8864. +
  8865. +
  8866. +/*
  8867. +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
  8868. +
  8869. + Initializes the internal stream state for decompression. The fields
  8870. + next_in, avail_in, zalloc, zfree and opaque must be initialized before by
  8871. + the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
  8872. + value depends on the compression method), inflateInit determines the
  8873. + compression method from the zlib header and allocates all data structures
  8874. + accordingly; otherwise the allocation will be deferred to the first call of
  8875. + inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
  8876. + use default allocation functions.
  8877. +
  8878. + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
  8879. + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
  8880. + version assumed by the caller. msg is set to null if there is no error
  8881. + message. inflateInit does not perform any decompression apart from reading
  8882. + the zlib header if present: this will be done by inflate(). (So next_in and
  8883. + avail_in may be modified, but next_out and avail_out are unchanged.)
  8884. +*/
  8885. +
  8886. +
  8887. +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
  8888. +/*
  8889. + inflate decompresses as much data as possible, and stops when the input
  8890. + buffer becomes empty or the output buffer becomes full. It may some
  8891. + introduce some output latency (reading input without producing any output)
  8892. + except when forced to flush.
  8893. +
  8894. + The detailed semantics are as follows. inflate performs one or both of the
  8895. + following actions:
  8896. +
  8897. + - Decompress more input starting at next_in and update next_in and avail_in
  8898. + accordingly. If not all input can be processed (because there is not
  8899. + enough room in the output buffer), next_in is updated and processing
  8900. + will resume at this point for the next call of inflate().
  8901. +
  8902. + - Provide more output starting at next_out and update next_out and avail_out
  8903. + accordingly. inflate() provides as much output as possible, until there
  8904. + is no more input data or no more space in the output buffer (see below
  8905. + about the flush parameter).
  8906. +
  8907. + Before the call of inflate(), the application should ensure that at least
  8908. + one of the actions is possible, by providing more input and/or consuming
  8909. + more output, and updating the next_* and avail_* values accordingly.
  8910. + The application can consume the uncompressed output when it wants, for
  8911. + example when the output buffer is full (avail_out == 0), or after each
  8912. + call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  8913. + must be called again after making room in the output buffer because there
  8914. + might be more output pending.
  8915. +
  8916. + If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
  8917. + output as possible to the output buffer. The flushing behavior of inflate is
  8918. + not specified for values of the flush parameter other than Z_SYNC_FLUSH
  8919. + and Z_FINISH, but the current implementation actually flushes as much output
  8920. + as possible anyway.
  8921. +
  8922. + inflate() should normally be called until it returns Z_STREAM_END or an
  8923. + error. However if all decompression is to be performed in a single step
  8924. + (a single call of inflate), the parameter flush should be set to
  8925. + Z_FINISH. In this case all pending input is processed and all pending
  8926. + output is flushed; avail_out must be large enough to hold all the
  8927. + uncompressed data. (The size of the uncompressed data may have been saved
  8928. + by the compressor for this purpose.) The next operation on this stream must
  8929. + be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  8930. + is never required, but can be used to inform inflate that a faster routine
  8931. + may be used for the single inflate() call.
  8932. +
  8933. + If a preset dictionary is needed at this point (see inflateSetDictionary
  8934. + below), inflate sets strm-adler to the adler32 checksum of the
  8935. + dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
  8936. + it sets strm->adler to the adler32 checksum of all output produced
  8937. + so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
  8938. + an error code as described below. At the end of the stream, inflate()
  8939. + checks that its computed adler32 checksum is equal to that saved by the
  8940. + compressor and returns Z_STREAM_END only if the checksum is correct.
  8941. +
  8942. + inflate() returns Z_OK if some progress has been made (more input processed
  8943. + or more output produced), Z_STREAM_END if the end of the compressed data has
  8944. + been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  8945. + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  8946. + corrupted (input stream not conforming to the zlib format or incorrect
  8947. + adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
  8948. + (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
  8949. + enough memory, Z_BUF_ERROR if no progress is possible or if there was not
  8950. + enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
  8951. + case, the application may then call inflateSync to look for a good
  8952. + compression block.
  8953. +*/
  8954. +
  8955. +
  8956. +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
  8957. +/*
  8958. + All dynamically allocated data structures for this stream are freed.
  8959. + This function discards any unprocessed input and does not flush any
  8960. + pending output.
  8961. +
  8962. + inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  8963. + was inconsistent. In the error case, msg may be set but then points to a
  8964. + static string (which must not be deallocated).
  8965. +*/
  8966. +
  8967. + /* Advanced functions */
  8968. +
  8969. +/*
  8970. + The following functions are needed only in some special applications.
  8971. +*/
  8972. +
  8973. +/*
  8974. +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
  8975. + int level,
  8976. + int method,
  8977. + int windowBits,
  8978. + int memLevel,
  8979. + int strategy));
  8980. +
  8981. + This is another version of deflateInit with more compression options. The
  8982. + fields next_in, zalloc, zfree and opaque must be initialized before by
  8983. + the caller.
  8984. +
  8985. + The method parameter is the compression method. It must be Z_DEFLATED in
  8986. + this version of the library.
  8987. +
  8988. + The windowBits parameter is the base two logarithm of the window size
  8989. + (the size of the history buffer). It should be in the range 8..15 for this
  8990. + version of the library. Larger values of this parameter result in better
  8991. + compression at the expense of memory usage. The default value is 15 if
  8992. + deflateInit is used instead.
  8993. +
  8994. + The memLevel parameter specifies how much memory should be allocated
  8995. + for the internal compression state. memLevel=1 uses minimum memory but
  8996. + is slow and reduces compression ratio; memLevel=9 uses maximum memory
  8997. + for optimal speed. The default value is 8. See zconf.h for total memory
  8998. + usage as a function of windowBits and memLevel.
  8999. +
  9000. + The strategy parameter is used to tune the compression algorithm. Use the
  9001. + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
  9002. + filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
  9003. + string match). Filtered data consists mostly of small values with a
  9004. + somewhat random distribution. In this case, the compression algorithm is
  9005. + tuned to compress them better. The effect of Z_FILTERED is to force more
  9006. + Huffman coding and less string matching; it is somewhat intermediate
  9007. + between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
  9008. + the compression ratio but not the correctness of the compressed output even
  9009. + if it is not set appropriately.
  9010. +
  9011. + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9012. + memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
  9013. + method). msg is set to null if there is no error message. deflateInit2 does
  9014. + not perform any compression: this will be done by deflate().
  9015. +*/
  9016. +
  9017. +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
  9018. + const Bytef *dictionary,
  9019. + uInt dictLength));
  9020. +/*
  9021. + Initializes the compression dictionary from the given byte sequence
  9022. + without producing any compressed output. This function must be called
  9023. + immediately after deflateInit, deflateInit2 or deflateReset, before any
  9024. + call of deflate. The compressor and decompressor must use exactly the same
  9025. + dictionary (see inflateSetDictionary).
  9026. +
  9027. + The dictionary should consist of strings (byte sequences) that are likely
  9028. + to be encountered later in the data to be compressed, with the most commonly
  9029. + used strings preferably put towards the end of the dictionary. Using a
  9030. + dictionary is most useful when the data to be compressed is short and can be
  9031. + predicted with good accuracy; the data can then be compressed better than
  9032. + with the default empty dictionary.
  9033. +
  9034. + Depending on the size of the compression data structures selected by
  9035. + deflateInit or deflateInit2, a part of the dictionary may in effect be
  9036. + discarded, for example if the dictionary is larger than the window size in
  9037. + deflate or deflate2. Thus the strings most likely to be useful should be
  9038. + put at the end of the dictionary, not at the front.
  9039. +
  9040. + Upon return of this function, strm->adler is set to the Adler32 value
  9041. + of the dictionary; the decompressor may later use this value to determine
  9042. + which dictionary has been used by the compressor. (The Adler32 value
  9043. + applies to the whole dictionary even if only a subset of the dictionary is
  9044. + actually used by the compressor.)
  9045. +
  9046. + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
  9047. + parameter is invalid (such as NULL dictionary) or the stream state is
  9048. + inconsistent (for example if deflate has already been called for this stream
  9049. + or if the compression method is bsort). deflateSetDictionary does not
  9050. + perform any compression: this will be done by deflate().
  9051. +*/
  9052. +
  9053. +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
  9054. + z_streamp source));
  9055. +/*
  9056. + Sets the destination stream as a complete copy of the source stream.
  9057. +
  9058. + This function can be useful when several compression strategies will be
  9059. + tried, for example when there are several ways of pre-processing the input
  9060. + data with a filter. The streams that will be discarded should then be freed
  9061. + by calling deflateEnd. Note that deflateCopy duplicates the internal
  9062. + compression state which can be quite large, so this strategy is slow and
  9063. + can consume lots of memory.
  9064. +
  9065. + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  9066. + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  9067. + (such as zalloc being NULL). msg is left unchanged in both source and
  9068. + destination.
  9069. +*/
  9070. +
  9071. +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
  9072. +/*
  9073. + This function is equivalent to deflateEnd followed by deflateInit,
  9074. + but does not free and reallocate all the internal compression state.
  9075. + The stream will keep the same compression level and any other attributes
  9076. + that may have been set by deflateInit2.
  9077. +
  9078. + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  9079. + stream state was inconsistent (such as zalloc or state being NULL).
  9080. +*/
  9081. +
  9082. +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
  9083. + int level,
  9084. + int strategy));
  9085. +/*
  9086. + Dynamically update the compression level and compression strategy. The
  9087. + interpretation of level and strategy is as in deflateInit2. This can be
  9088. + used to switch between compression and straight copy of the input data, or
  9089. + to switch to a different kind of input data requiring a different
  9090. + strategy. If the compression level is changed, the input available so far
  9091. + is compressed with the old level (and may be flushed); the new level will
  9092. + take effect only at the next call of deflate().
  9093. +
  9094. + Before the call of deflateParams, the stream state must be set as for
  9095. + a call of deflate(), since the currently available input may have to
  9096. + be compressed and flushed. In particular, strm->avail_out must be non-zero.
  9097. +
  9098. + deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
  9099. + stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
  9100. + if strm->avail_out was zero.
  9101. +*/
  9102. +
  9103. +/*
  9104. +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
  9105. + int windowBits));
  9106. +
  9107. + This is another version of inflateInit with an extra parameter. The
  9108. + fields next_in, avail_in, zalloc, zfree and opaque must be initialized
  9109. + before by the caller.
  9110. +
  9111. + The windowBits parameter is the base two logarithm of the maximum window
  9112. + size (the size of the history buffer). It should be in the range 8..15 for
  9113. + this version of the library. The default value is 15 if inflateInit is used
  9114. + instead. If a compressed stream with a larger window size is given as
  9115. + input, inflate() will return with the error code Z_DATA_ERROR instead of
  9116. + trying to allocate a larger window.
  9117. +
  9118. + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9119. + memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
  9120. + memLevel). msg is set to null if there is no error message. inflateInit2
  9121. + does not perform any decompression apart from reading the zlib header if
  9122. + present: this will be done by inflate(). (So next_in and avail_in may be
  9123. + modified, but next_out and avail_out are unchanged.)
  9124. +*/
  9125. +
  9126. +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
  9127. + const Bytef *dictionary,
  9128. + uInt dictLength));
  9129. +/*
  9130. + Initializes the decompression dictionary from the given uncompressed byte
  9131. + sequence. This function must be called immediately after a call of inflate
  9132. + if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
  9133. + can be determined from the Adler32 value returned by this call of
  9134. + inflate. The compressor and decompressor must use exactly the same
  9135. + dictionary (see deflateSetDictionary).
  9136. +
  9137. + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  9138. + parameter is invalid (such as NULL dictionary) or the stream state is
  9139. + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  9140. + expected one (incorrect Adler32 value). inflateSetDictionary does not
  9141. + perform any decompression: this will be done by subsequent calls of
  9142. + inflate().
  9143. +*/
  9144. +
  9145. +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
  9146. +/*
  9147. + Skips invalid compressed data until a full flush point (see above the
  9148. + description of deflate with Z_FULL_FLUSH) can be found, or until all
  9149. + available input is skipped. No output is provided.
  9150. +
  9151. + inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  9152. + if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  9153. + or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  9154. + case, the application may save the current current value of total_in which
  9155. + indicates where valid compressed data was found. In the error case, the
  9156. + application may repeatedly call inflateSync, providing more input each time,
  9157. + until success or end of the input data.
  9158. +*/
  9159. +
  9160. +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
  9161. +/*
  9162. + This function is equivalent to inflateEnd followed by inflateInit,
  9163. + but does not free and reallocate all the internal decompression state.
  9164. + The stream will keep attributes that may have been set by inflateInit2.
  9165. +
  9166. + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  9167. + stream state was inconsistent (such as zalloc or state being NULL).
  9168. +*/
  9169. +
  9170. +
  9171. + /* utility functions */
  9172. +
  9173. +/*
  9174. + The following utility functions are implemented on top of the
  9175. + basic stream-oriented functions. To simplify the interface, some
  9176. + default options are assumed (compression level and memory usage,
  9177. + standard memory allocation functions). The source code of these
  9178. + utility functions can easily be modified if you need special options.
  9179. +*/
  9180. +
  9181. +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
  9182. + const Bytef *source, uLong sourceLen));
  9183. +/*
  9184. + Compresses the source buffer into the destination buffer. sourceLen is
  9185. + the byte length of the source buffer. Upon entry, destLen is the total
  9186. + size of the destination buffer, which must be at least 0.1% larger than
  9187. + sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
  9188. + compressed buffer.
  9189. + This function can be used to compress a whole file at once if the
  9190. + input file is mmap'ed.
  9191. + compress returns Z_OK if success, Z_MEM_ERROR if there was not
  9192. + enough memory, Z_BUF_ERROR if there was not enough room in the output
  9193. + buffer.
  9194. +*/
  9195. +
  9196. +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
  9197. + const Bytef *source, uLong sourceLen,
  9198. + int level));
  9199. +/*
  9200. + Compresses the source buffer into the destination buffer. The level
  9201. + parameter has the same meaning as in deflateInit. sourceLen is the byte
  9202. + length of the source buffer. Upon entry, destLen is the total size of the
  9203. + destination buffer, which must be at least 0.1% larger than sourceLen plus
  9204. + 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  9205. +
  9206. + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9207. + memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  9208. + Z_STREAM_ERROR if the level parameter is invalid.
  9209. +*/
  9210. +
  9211. +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
  9212. + const Bytef *source, uLong sourceLen));
  9213. +/*
  9214. + Decompresses the source buffer into the destination buffer. sourceLen is
  9215. + the byte length of the source buffer. Upon entry, destLen is the total
  9216. + size of the destination buffer, which must be large enough to hold the
  9217. + entire uncompressed data. (The size of the uncompressed data must have
  9218. + been saved previously by the compressor and transmitted to the decompressor
  9219. + by some mechanism outside the scope of this compression library.)
  9220. + Upon exit, destLen is the actual size of the compressed buffer.
  9221. + This function can be used to decompress a whole file at once if the
  9222. + input file is mmap'ed.
  9223. +
  9224. + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  9225. + enough memory, Z_BUF_ERROR if there was not enough room in the output
  9226. + buffer, or Z_DATA_ERROR if the input data was corrupted.
  9227. +*/
  9228. +
  9229. +
  9230. +typedef voidp gzFile;
  9231. +
  9232. +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
  9233. +/*
  9234. + Opens a gzip (.gz) file for reading or writing. The mode parameter
  9235. + is as in fopen ("rb" or "wb") but can also include a compression level
  9236. + ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
  9237. + Huffman only compression as in "wb1h". (See the description
  9238. + of deflateInit2 for more information about the strategy parameter.)
  9239. +
  9240. + gzopen can be used to read a file which is not in gzip format; in this
  9241. + case gzread will directly read from the file without decompression.
  9242. +
  9243. + gzopen returns NULL if the file could not be opened or if there was
  9244. + insufficient memory to allocate the (de)compression state; errno
  9245. + can be checked to distinguish the two cases (if errno is zero, the
  9246. + zlib error is Z_MEM_ERROR). */
  9247. +
  9248. +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
  9249. +/*
  9250. + gzdopen() associates a gzFile with the file descriptor fd. File
  9251. + descriptors are obtained from calls like open, dup, creat, pipe or
  9252. + fileno (in the file has been previously opened with fopen).
  9253. + The mode parameter is as in gzopen.
  9254. + The next call of gzclose on the returned gzFile will also close the
  9255. + file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
  9256. + descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
  9257. + gzdopen returns NULL if there was insufficient memory to allocate
  9258. + the (de)compression state.
  9259. +*/
  9260. +
  9261. +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
  9262. +/*
  9263. + Dynamically update the compression level or strategy. See the description
  9264. + of deflateInit2 for the meaning of these parameters.
  9265. + gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
  9266. + opened for writing.
  9267. +*/
  9268. +
  9269. +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
  9270. +/*
  9271. + Reads the given number of uncompressed bytes from the compressed file.
  9272. + If the input file was not in gzip format, gzread copies the given number
  9273. + of bytes into the buffer.
  9274. + gzread returns the number of uncompressed bytes actually read (0 for
  9275. + end of file, -1 for error). */
  9276. +
  9277. +ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
  9278. + const voidp buf, unsigned len));
  9279. +/*
  9280. + Writes the given number of uncompressed bytes into the compressed file.
  9281. + gzwrite returns the number of uncompressed bytes actually written
  9282. + (0 in case of error).
  9283. +*/
  9284. +
  9285. +ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
  9286. +/*
  9287. + Converts, formats, and writes the args to the compressed file under
  9288. + control of the format string, as in fprintf. gzprintf returns the number of
  9289. + uncompressed bytes actually written (0 in case of error).
  9290. +*/
  9291. +
  9292. +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
  9293. +/*
  9294. + Writes the given null-terminated string to the compressed file, excluding
  9295. + the terminating null character.
  9296. + gzputs returns the number of characters written, or -1 in case of error.
  9297. +*/
  9298. +
  9299. +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
  9300. +/*
  9301. + Reads bytes from the compressed file until len-1 characters are read, or
  9302. + a newline character is read and transferred to buf, or an end-of-file
  9303. + condition is encountered. The string is then terminated with a null
  9304. + character.
  9305. + gzgets returns buf, or Z_NULL in case of error.
  9306. +*/
  9307. +
  9308. +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
  9309. +/*
  9310. + Writes c, converted to an unsigned char, into the compressed file.
  9311. + gzputc returns the value that was written, or -1 in case of error.
  9312. +*/
  9313. +
  9314. +ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
  9315. +/*
  9316. + Reads one byte from the compressed file. gzgetc returns this byte
  9317. + or -1 in case of end of file or error.
  9318. +*/
  9319. +
  9320. +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
  9321. +/*
  9322. + Flushes all pending output into the compressed file. The parameter
  9323. + flush is as in the deflate() function. The return value is the zlib
  9324. + error number (see function gzerror below). gzflush returns Z_OK if
  9325. + the flush parameter is Z_FINISH and all output could be flushed.
  9326. + gzflush should be called only when strictly necessary because it can
  9327. + degrade compression.
  9328. +*/
  9329. +
  9330. +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
  9331. + z_off_t offset, int whence));
  9332. +/*
  9333. + Sets the starting position for the next gzread or gzwrite on the
  9334. + given compressed file. The offset represents a number of bytes in the
  9335. + uncompressed data stream. The whence parameter is defined as in lseek(2);
  9336. + the value SEEK_END is not supported.
  9337. + If the file is opened for reading, this function is emulated but can be
  9338. + extremely slow. If the file is opened for writing, only forward seeks are
  9339. + supported; gzseek then compresses a sequence of zeroes up to the new
  9340. + starting position.
  9341. +
  9342. + gzseek returns the resulting offset location as measured in bytes from
  9343. + the beginning of the uncompressed stream, or -1 in case of error, in
  9344. + particular if the file is opened for writing and the new starting position
  9345. + would be before the current position.
  9346. +*/
  9347. +
  9348. +ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
  9349. +/*
  9350. + Rewinds the given file. This function is supported only for reading.
  9351. +
  9352. + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
  9353. +*/
  9354. +
  9355. +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
  9356. +/*
  9357. + Returns the starting position for the next gzread or gzwrite on the
  9358. + given compressed file. This position represents a number of bytes in the
  9359. + uncompressed data stream.
  9360. +
  9361. + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
  9362. +*/
  9363. +
  9364. +ZEXTERN int ZEXPORT gzeof OF((gzFile file));
  9365. +/*
  9366. + Returns 1 when EOF has previously been detected reading the given
  9367. + input stream, otherwise zero.
  9368. +*/
  9369. +
  9370. +ZEXTERN int ZEXPORT gzclose OF((gzFile file));
  9371. +/*
  9372. + Flushes all pending output if necessary, closes the compressed file
  9373. + and deallocates all the (de)compression state. The return value is the zlib
  9374. + error number (see function gzerror below).
  9375. +*/
  9376. +
  9377. +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
  9378. +/*
  9379. + Returns the error message for the last error which occurred on the
  9380. + given compressed file. errnum is set to zlib error number. If an
  9381. + error occurred in the file system and not in the compression library,
  9382. + errnum is set to Z_ERRNO and the application may consult errno
  9383. + to get the exact error code.
  9384. +*/
  9385. +
  9386. + /* checksum functions */
  9387. +
  9388. +/*
  9389. + These functions are not related to compression but are exported
  9390. + anyway because they might be useful in applications using the
  9391. + compression library.
  9392. +*/
  9393. +
  9394. +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
  9395. +
  9396. +/*
  9397. + Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  9398. + return the updated checksum. If buf is NULL, this function returns
  9399. + the required initial value for the checksum.
  9400. + An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  9401. + much faster. Usage example:
  9402. +
  9403. + uLong adler = adler32(0L, Z_NULL, 0);
  9404. +
  9405. + while (read_buffer(buffer, length) != EOF) {
  9406. + adler = adler32(adler, buffer, length);
  9407. + }
  9408. + if (adler != original_adler) error();
  9409. +*/
  9410. +
  9411. +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
  9412. +/*
  9413. + Update a running crc with the bytes buf[0..len-1] and return the updated
  9414. + crc. If buf is NULL, this function returns the required initial value
  9415. + for the crc. Pre- and post-conditioning (one's complement) is performed
  9416. + within this function so it shouldn't be done by the application.
  9417. + Usage example:
  9418. +
  9419. + uLong crc = crc32(0L, Z_NULL, 0);
  9420. +
  9421. + while (read_buffer(buffer, length) != EOF) {
  9422. + crc = crc32(crc, buffer, length);
  9423. + }
  9424. + if (crc != original_crc) error();
  9425. +*/
  9426. +
  9427. +
  9428. + /* various hacks, don't look :) */
  9429. +
  9430. +/* deflateInit and inflateInit are macros to allow checking the zlib version
  9431. + * and the compiler's view of z_stream:
  9432. + */
  9433. +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
  9434. + const char *version, int stream_size));
  9435. +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
  9436. + const char *version, int stream_size));
  9437. +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
  9438. + int windowBits, int memLevel,
  9439. + int strategy, const char *version,
  9440. + int stream_size));
  9441. +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
  9442. + const char *version, int stream_size));
  9443. +#define deflateInit(strm, level) \
  9444. + deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
  9445. +#define inflateInit(strm) \
  9446. + inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
  9447. +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
  9448. + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
  9449. + (strategy), ZLIB_VERSION, sizeof(z_stream))
  9450. +#define inflateInit2(strm, windowBits) \
  9451. + inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
  9452. +
  9453. +
  9454. +#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
  9455. + struct internal_state {int dummy;}; /* hack for buggy compilers */
  9456. +#endif
  9457. +
  9458. +ZEXTERN const char * ZEXPORT zError OF((int err));
  9459. +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
  9460. +ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
  9461. +
  9462. +#ifdef __cplusplus
  9463. +}
  9464. +#endif
  9465. +
  9466. +#endif /* _ZLIB_H */
  9467. --- /dev/null Tue Mar 11 13:02:56 2003
  9468. +++ linux/include/zlib/zutil.h Mon Feb 9 13:51:03 2004
  9469. @@ -0,0 +1,225 @@
  9470. +/* zutil.h -- internal interface and configuration of the compression library
  9471. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  9472. + * For conditions of distribution and use, see copyright notice in zlib.h
  9473. + */
  9474. +
  9475. +/* WARNING: this file should *not* be used by applications. It is
  9476. + part of the implementation of the compression library and is
  9477. + subject to change. Applications should only use zlib.h.
  9478. + */
  9479. +
  9480. +/* @(#) $Id: zutil.h,v 1.4 2002/04/24 07:36:48 mcr Exp $ */
  9481. +
  9482. +#ifndef _Z_UTIL_H
  9483. +#define _Z_UTIL_H
  9484. +
  9485. +#include "zlib.h"
  9486. +
  9487. +#include <linux/string.h>
  9488. +#define HAVE_MEMCPY
  9489. +
  9490. +#if 0 // #ifdef STDC
  9491. +# include <stddef.h>
  9492. +# include <string.h>
  9493. +# include <stdlib.h>
  9494. +#endif
  9495. +#ifndef __KERNEL__
  9496. +#ifdef NO_ERRNO_H
  9497. + extern int errno;
  9498. +#else
  9499. +# include <errno.h>
  9500. +#endif
  9501. +#endif
  9502. +
  9503. +#ifndef local
  9504. +# define local static
  9505. +#endif
  9506. +/* compile with -Dlocal if your debugger can't find static symbols */
  9507. +
  9508. +typedef unsigned char uch;
  9509. +typedef uch FAR uchf;
  9510. +typedef unsigned short ush;
  9511. +typedef ush FAR ushf;
  9512. +typedef unsigned long ulg;
  9513. +
  9514. +extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
  9515. +/* (size given to avoid silly warnings with Visual C++) */
  9516. +
  9517. +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  9518. +
  9519. +#define ERR_RETURN(strm,err) \
  9520. + return (strm->msg = ERR_MSG(err), (err))
  9521. +/* To be used only when the state is known to be valid */
  9522. +
  9523. + /* common constants */
  9524. +
  9525. +#ifndef DEF_WBITS
  9526. +# define DEF_WBITS MAX_WBITS
  9527. +#endif
  9528. +/* default windowBits for decompression. MAX_WBITS is for compression only */
  9529. +
  9530. +#if MAX_MEM_LEVEL >= 8
  9531. +# define DEF_MEM_LEVEL 8
  9532. +#else
  9533. +# define DEF_MEM_LEVEL MAX_MEM_LEVEL
  9534. +#endif
  9535. +/* default memLevel */
  9536. +
  9537. +#define STORED_BLOCK 0
  9538. +#define STATIC_TREES 1
  9539. +#define DYN_TREES 2
  9540. +/* The three kinds of block type */
  9541. +
  9542. +#define MIN_MATCH 3
  9543. +#define MAX_MATCH 258
  9544. +/* The minimum and maximum match lengths */
  9545. +
  9546. +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
  9547. +
  9548. + /* target dependencies */
  9549. +
  9550. +#ifdef MSDOS
  9551. +# define OS_CODE 0x00
  9552. +# if defined(__TURBOC__) || defined(__BORLANDC__)
  9553. +# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
  9554. + /* Allow compilation with ANSI keywords only enabled */
  9555. + void _Cdecl farfree( void *block );
  9556. + void *_Cdecl farmalloc( unsigned long nbytes );
  9557. +# else
  9558. +# include <alloc.h>
  9559. +# endif
  9560. +# else /* MSC or DJGPP */
  9561. +# include <malloc.h>
  9562. +# endif
  9563. +#endif
  9564. +
  9565. +#ifdef OS2
  9566. +# define OS_CODE 0x06
  9567. +#endif
  9568. +
  9569. +#ifdef WIN32 /* Window 95 & Windows NT */
  9570. +# define OS_CODE 0x0b
  9571. +#endif
  9572. +
  9573. +#if defined(VAXC) || defined(VMS)
  9574. +# define OS_CODE 0x02
  9575. +# define F_OPEN(name, mode) \
  9576. + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
  9577. +#endif
  9578. +
  9579. +#ifdef AMIGA
  9580. +# define OS_CODE 0x01
  9581. +#endif
  9582. +
  9583. +#if defined(ATARI) || defined(atarist)
  9584. +# define OS_CODE 0x05
  9585. +#endif
  9586. +
  9587. +#if defined(MACOS) || defined(TARGET_OS_MAC)
  9588. +# define OS_CODE 0x07
  9589. +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  9590. +# include <unix.h> /* for fdopen */
  9591. +# else
  9592. +# ifndef fdopen
  9593. +# define fdopen(fd,mode) NULL /* No fdopen() */
  9594. +# endif
  9595. +# endif
  9596. +#endif
  9597. +
  9598. +#ifdef __50SERIES /* Prime/PRIMOS */
  9599. +# define OS_CODE 0x0F
  9600. +#endif
  9601. +
  9602. +#ifdef TOPS20
  9603. +# define OS_CODE 0x0a
  9604. +#endif
  9605. +
  9606. +#if defined(_BEOS_) || defined(RISCOS)
  9607. +# define fdopen(fd,mode) NULL /* No fdopen() */
  9608. +#endif
  9609. +
  9610. +#if (defined(_MSC_VER) && (_MSC_VER > 600))
  9611. +# define fdopen(fd,type) _fdopen(fd,type)
  9612. +#endif
  9613. +
  9614. +
  9615. + /* Common defaults */
  9616. +
  9617. +#ifndef OS_CODE
  9618. +# define OS_CODE 0x03 /* assume Unix */
  9619. +#endif
  9620. +
  9621. +#ifndef F_OPEN
  9622. +# define F_OPEN(name, mode) fopen((name), (mode))
  9623. +#endif
  9624. +
  9625. + /* functions */
  9626. +
  9627. +#ifdef HAVE_STRERROR
  9628. + extern char *strerror OF((int));
  9629. +# define zstrerror(errnum) strerror(errnum)
  9630. +#else
  9631. +# define zstrerror(errnum) ""
  9632. +#endif
  9633. +
  9634. +#if defined(pyr)
  9635. +# define NO_MEMCPY
  9636. +#endif
  9637. +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
  9638. + /* Use our own functions for small and medium model with MSC <= 5.0.
  9639. + * You may have to use the same strategy for Borland C (untested).
  9640. + * The __SC__ check is for Symantec.
  9641. + */
  9642. +# define NO_MEMCPY
  9643. +#endif
  9644. +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
  9645. +# define HAVE_MEMCPY
  9646. +#endif
  9647. +#ifdef HAVE_MEMCPY
  9648. +# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
  9649. +# define zmemcpy _fmemcpy
  9650. +# define zmemcmp _fmemcmp
  9651. +# define zmemzero(dest, len) _fmemset(dest, 0, len)
  9652. +# else
  9653. +# define zmemcpy memcpy
  9654. +# define zmemcmp memcmp
  9655. +# define zmemzero(dest, len) memset(dest, 0, len)
  9656. +# endif
  9657. +#else
  9658. + extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
  9659. + extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
  9660. + extern void zmemzero OF((Bytef* dest, uInt len));
  9661. +#endif
  9662. +
  9663. +/* Diagnostic functions */
  9664. +#ifdef DEBUG
  9665. +# include <stdio.h>
  9666. + extern int z_verbose;
  9667. + extern void z_error OF((char *m));
  9668. +# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
  9669. +# define Trace(x) {if (z_verbose>=0) fprintf x ;}
  9670. +# define Tracev(x) {if (z_verbose>0) fprintf x ;}
  9671. +# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
  9672. +# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
  9673. +# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
  9674. +#else
  9675. +# define Assert(cond,msg)
  9676. +# define Trace(x)
  9677. +# define Tracev(x)
  9678. +# define Tracevv(x)
  9679. +# define Tracec(c,x)
  9680. +# define Tracecv(c,x)
  9681. +#endif
  9682. +
  9683. +
  9684. +typedef uLong (ZEXPORT *check_func) OF((uLong check, const Bytef *buf,
  9685. + uInt len));
  9686. +voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
  9687. +void zcfree OF((voidpf opaque, voidpf ptr));
  9688. +
  9689. +#define ZALLOC(strm, items, size) \
  9690. + (*((strm)->zalloc))((strm)->opaque, (items), (size))
  9691. +#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
  9692. +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
  9693. +
  9694. +#endif /* _Z_UTIL_H */
  9695. --- /dev/null Tue Mar 11 13:02:56 2003
  9696. +++ linux/lib/libfreeswan/Makefile.objs Mon Feb 9 13:51:03 2004
  9697. @@ -0,0 +1,21 @@
  9698. +obj-y += satot.o
  9699. +obj-y += addrtot.o
  9700. +obj-y += ultot.o
  9701. +obj-y += addrtypeof.o
  9702. +obj-y += anyaddr.o
  9703. +obj-y += initaddr.o
  9704. +obj-y += ultoa.o
  9705. +obj-y += addrtoa.o
  9706. +obj-y += subnettoa.o
  9707. +obj-y += subnetof.o
  9708. +obj-y += goodmask.o
  9709. +obj-y += datatot.o
  9710. +obj-y += rangetoa.o
  9711. +obj-y += prng.o
  9712. +obj-y += pfkey_v2_parse.o
  9713. +obj-y += pfkey_v2_build.o
  9714. +obj-y += pfkey_v2_debug.o
  9715. +obj-y += pfkey_v2_ext_bits.o
  9716. +
  9717. +#version.c: ${LIBFREESWANDIR}/version.in.c ${OPENSWANSRCDIR}/Makefile.ver
  9718. +# sed '/"/s/xxx/$(IPSECVERSION)/' ${LIBFREESWANDIR}/version.in.c >$@
  9719. --- /dev/null Tue Mar 11 13:02:56 2003
  9720. +++ linux/lib/zlib/Makefile Mon Feb 9 13:51:03 2004
  9721. @@ -0,0 +1,118 @@
  9722. +# (kernel) Makefile for IPCOMP zlib deflate code
  9723. +# Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  9724. +# Copyright (C) 2000 Svenning Soerensen
  9725. +#
  9726. +# This program is free software; you can redistribute it and/or modify it
  9727. +# under the terms of the GNU General Public License as published by the
  9728. +# Free Software Foundation; either version 2 of the License, or (at your
  9729. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9730. +#
  9731. +# This program is distributed in the hope that it will be useful, but
  9732. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9733. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9734. +# for more details.
  9735. +#
  9736. +# RCSID $Id: Makefile,v 1.9 2002/04/24 07:55:32 mcr Exp $
  9737. +#
  9738. +
  9739. +
  9740. +
  9741. +include ../Makefile.inc
  9742. +
  9743. +
  9744. +
  9745. +ifndef TOPDIR
  9746. +TOPDIR := /usr/src/linux
  9747. +endif
  9748. +
  9749. +
  9750. +L_TARGET := zlib.a
  9751. +
  9752. +obj-y :=
  9753. +
  9754. +include Makefile.objs
  9755. +
  9756. +EXTRA_CFLAGS += $(KLIPSCOMPILE)
  9757. +
  9758. +EXTRA_CFLAGS += -Wall
  9759. +#EXTRA_CFLAGS += -Wconversion
  9760. +#EXTRA_CFLAGS += -Wmissing-prototypes
  9761. +EXTRA_CFLAGS += -Wpointer-arith
  9762. +#EXTRA_CFLAGS += -Wcast-qual
  9763. +#EXTRA_CFLAGS += -Wmissing-declarations
  9764. +EXTRA_CFLAGS += -Wstrict-prototypes
  9765. +#EXTRA_CFLAGS += -pedantic
  9766. +#EXTRA_CFLAGS += -W
  9767. +#EXTRA_CFLAGS += -Wwrite-strings
  9768. +EXTRA_CFLAGS += -Wbad-function-cast
  9769. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  9770. +
  9771. +.S.o:
  9772. + $(CC) -D__ASSEMBLY__ -DNO_UNDERLINE -traditional -c $< -o $*.o
  9773. +
  9774. +asm-obj-$(CONFIG_M586) += match586.o
  9775. +asm-obj-$(CONFIG_M586TSC) += match586.o
  9776. +asm-obj-$(CONFIG_M586MMX) += match586.o
  9777. +asm-obj-$(CONFIG_M686) += match686.o
  9778. +asm-obj-$(CONFIG_MPENTIUMIII) += match686.o
  9779. +asm-obj-$(CONFIG_MPENTIUM4) += match686.o
  9780. +asm-obj-$(CONFIG_MK6) += match586.o
  9781. +asm-obj-$(CONFIG_MK7) += match686.o
  9782. +asm-obj-$(CONFIG_MCRUSOE) += match586.o
  9783. +asm-obj-$(CONFIG_MWINCHIPC6) += match586.o
  9784. +asm-obj-$(CONFIG_MWINCHIP2) += match686.o
  9785. +asm-obj-$(CONFIG_MWINCHIP3D) += match686.o
  9786. +
  9787. +obj-y += $(asm-obj-y)
  9788. +ifneq ($(strip $(asm-obj-y)),)
  9789. + EXTRA_CFLAGS += -DASMV
  9790. +endif
  9791. +
  9792. +active-objs := $(sort $(obj-y) $(obj-m))
  9793. +L_OBJS := $(obj-y)
  9794. +M_OBJS := $(obj-m)
  9795. +MIX_OBJS := $(filter $(export-objs), $(active-objs))
  9796. +
  9797. +include $(TOPDIR)/Rules.make
  9798. +
  9799. +$(obj-y) : $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h
  9800. +
  9801. +
  9802. +clean:
  9803. + -rm -f *.o *.a
  9804. +
  9805. +checkprograms:
  9806. +programs: $(L_TARGET)
  9807. +
  9808. +#
  9809. +# $Log: Makefile,v $
  9810. +# Revision 1.9 2002/04/24 07:55:32 mcr
  9811. +# #include patches and Makefiles for post-reorg compilation.
  9812. +#
  9813. +# Revision 1.8 2002/04/24 07:36:44 mcr
  9814. +# Moved from ./zlib/Makefile,v
  9815. +#
  9816. +# Revision 1.7 2002/03/27 23:34:35 mcr
  9817. +# added programs: target
  9818. +#
  9819. +# Revision 1.6 2001/12/05 20:19:08 henry
  9820. +# use new compile-control variable
  9821. +#
  9822. +# Revision 1.5 2001/11/27 16:38:08 mcr
  9823. +# added new "checkprograms" target to deal with programs that
  9824. +# are required for "make check", but that may not be ready to
  9825. +# build for every user due to external dependancies.
  9826. +#
  9827. +# Revision 1.4 2001/10/24 14:46:24 henry
  9828. +# Makefile.inc
  9829. +#
  9830. +# Revision 1.3 2001/04/21 23:05:24 rgb
  9831. +# Update asm directives for 2.4 style makefiles.
  9832. +#
  9833. +# Revision 1.2 2001/01/29 22:22:00 rgb
  9834. +# Convert to 2.4 new style with back compat.
  9835. +#
  9836. +# Revision 1.1.1.1 2000/09/29 18:51:33 rgb
  9837. +# zlib_beginnings
  9838. +#
  9839. +#
  9840. --- /dev/null Tue Mar 11 13:02:56 2003
  9841. +++ linux/lib/zlib/Makefile.objs Mon Feb 9 13:51:03 2004
  9842. @@ -0,0 +1,27 @@
  9843. +obj-$(CONFIG_IPSEC_IPCOMP) += adler32.o
  9844. +obj-$(CONFIG_IPSEC_IPCOMP) += deflate.o
  9845. +obj-$(CONFIG_IPSEC_IPCOMP) += infblock.o
  9846. +obj-$(CONFIG_IPSEC_IPCOMP) += infcodes.o
  9847. +obj-$(CONFIG_IPSEC_IPCOMP) += inffast.o
  9848. +obj-$(CONFIG_IPSEC_IPCOMP) += inflate.o
  9849. +obj-$(CONFIG_IPSEC_IPCOMP) += inftrees.o
  9850. +obj-$(CONFIG_IPSEC_IPCOMP) += infutil.o
  9851. +obj-$(CONFIG_IPSEC_IPCOMP) += trees.o
  9852. +obj-$(CONFIG_IPSEC_IPCOMP) += zutil.o
  9853. +
  9854. +asm-obj-$(CONFIG_M586) += ${LIBZLIBSRCDIR}/match586.o
  9855. +asm-obj-$(CONFIG_M586TSC) += ${LIBZLIBSRCDIR}/match586.o
  9856. +asm-obj-$(CONFIG_M586MMX) += ${LIBZLIBSRCDIR}/match586.o
  9857. +asm-obj-$(CONFIG_M686) += ${LIBZLIBSRCDIR}/match686.o
  9858. +asm-obj-$(CONFIG_MPENTIUMIII) += ${LIBZLIBSRCDIR}/match686.o
  9859. +asm-obj-$(CONFIG_MPENTIUM4) += ${LIBZLIBSRCDIR}/match686.o
  9860. +asm-obj-$(CONFIG_MK6) += ${LIBZLIBSRCDIR}/match586.o
  9861. +asm-obj-$(CONFIG_MK7) += ${LIBZLIBSRCDIR}/match686.o
  9862. +asm-obj-$(CONFIG_MCRUSOE) += ${LIBZLIBSRCDIR}/match586.o
  9863. +asm-obj-$(CONFIG_MWINCHIPC6) += ${LIBZLIBSRCDIR}/match586.o
  9864. +asm-obj-$(CONFIG_MWINCHIP2) += ${LIBZLIBSRCDIR}/match686.o
  9865. +asm-obj-$(CONFIG_MWINCHIP3D) += ${LIBZLIBSRCDIR}/match686.o
  9866. +
  9867. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  9868. +
  9869. +
  9870. --- swan26/net/Kconfig.preipsec 2005-09-01 18:15:19.000000000 -0400
  9871. +++ swan26/net/Kconfig 2005-09-03 16:51:17.000000000 -0400
  9872. @@ -215,2 +215,6 @@
  9873. +if INET
  9874. +source "net/ipsec/Kconfig"
  9875. +endif # if INET
  9876. +
  9877. endif # if NET
  9878. --- /distros/kernel/linux-2.6.3-rc4/net/Makefile Mon Feb 16 21:22:12 2004
  9879. +++ ref26/net/Makefile Thu Feb 19 21:02:25 2004
  9880. @@ -42,3 +42,6 @@
  9881. ifeq ($(CONFIG_NET),y)
  9882. obj-$(CONFIG_SYSCTL) += sysctl_net.o
  9883. endif
  9884. +
  9885. +obj-$(CONFIG_KLIPS) += ipsec/
  9886. +
  9887. --- /dev/null Tue Mar 11 13:02:56 2003
  9888. +++ linux/net/ipsec/Kconfig Mon Feb 9 13:51:03 2004
  9889. @@ -0,0 +1,161 @@
  9890. +#
  9891. +# IPSEC configuration
  9892. +# Copyright (C) 2004 Michael Richardson <mcr@freeswan.org>
  9893. +#
  9894. +# This program is free software; you can redistribute it and/or modify it
  9895. +# under the terms of the GNU General Public License as published by the
  9896. +# Free Software Foundation; either version 2 of the License, or (at your
  9897. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9898. +#
  9899. +# This program is distributed in the hope that it will be useful, but
  9900. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9901. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9902. +# for more details.
  9903. +#
  9904. +# RCSID $Id: Kconfig,v 1.6.2.2 2006/10/11 18:14:33 paul Exp $
  9905. +
  9906. +config KLIPS
  9907. + tristate "Openswan IPsec (KLIPS26)"
  9908. + default n
  9909. + help
  9910. + KLIPS is the Openswan (www.openswan.org) Kernel Level IP Security
  9911. + system. It is extensively tested, and has interoperated with
  9912. + many other systems.
  9913. + It provides "ipsecX" devices on which one can do firewalling.
  9914. + The userland, is compatible with both KLIPS and 26sec.
  9915. +
  9916. +menu "KLIPS options"
  9917. + depends on KLIPS
  9918. +
  9919. +config KLIPS_ESP
  9920. + bool 'Encapsulating Security Payload - ESP ("VPN")'
  9921. + default y
  9922. + help
  9923. + This option provides support for the IPSEC Encapsulation Security
  9924. + Payload (IP protocol 50) which provides packet layer content
  9925. + hiding, and content authentication.
  9926. + It is recommended to enable this. RFC2406
  9927. +
  9928. +config KLIPS_AH
  9929. + bool 'Authentication Header - AH'
  9930. + default n
  9931. + help
  9932. + This option provides support for the IPSEC Authentication Header
  9933. + (IP protocol 51) which provides packet layer sender and content
  9934. + authentication. It does not provide for confidentiality.
  9935. + It is not recommended to enable this. RFC2402
  9936. +
  9937. +config KLIPS_AUTH_HMAC_MD5
  9938. + bool 'HMAC-MD5 authentication algorithm'
  9939. + default y
  9940. + help
  9941. + The HMAC-MD5 algorithm is used by ESP (and AH) to guarantee packet
  9942. + integrity. There is little reason not to include it.
  9943. +
  9944. +config KLIPS_AUTH_HMAC_SHA1
  9945. + bool 'HMAC-SHA1 authentication algorithm'
  9946. + default y
  9947. + help
  9948. + The HMAC-SHA1 algorithm is used by ESP (and AH) to guarantee packet
  9949. + integrity. SHA1 is a little slower than MD5, but is said to be
  9950. + a bit more secure. There is little reason not to include it.
  9951. +
  9952. +config KLIPS_ENC_CRYPTOAPI
  9953. + bool 'CryptoAPI algorithm interface'
  9954. + default n
  9955. + help
  9956. + Enable the algorithm interface to make all CryptoAPI 1.0 algorithms
  9957. + available to KLIPS.
  9958. +
  9959. +config KLIPS_ENC_1DES
  9960. + bool 'Include 1DES with CryptoAPI'
  9961. + default n
  9962. + depends on KLIPS_ENC_CRYPTOAPI
  9963. + help
  9964. + The CryptoAPI interface does not include support for every algorithm
  9965. + yet, and one that it doesn't support by default is the VERY WEAK
  9966. + 1DES. Select this if you are terminally stupid.
  9967. +
  9968. +config KLIPS_ENC_3DES
  9969. + bool '3DES encryption algorithm'
  9970. + default y
  9971. + help
  9972. + The 3DES algorithm is used by ESP to provide for packet privacy.
  9973. + 3DES is 3-repeats of the DES algorithm. 3DES is widely supported,
  9974. + and analyzed and is considered very secure. 1DES is not supported.
  9975. +
  9976. +config KLIPS_ENC_AES
  9977. + bool 'AES encryption algorithm'
  9978. + default y
  9979. + help
  9980. + The AES algorithm is used by ESP to provide for packet privacy.
  9981. + AES the NIST replacement for DES. AES is being widely analyzed,
  9982. + and is very fast.
  9983. +
  9984. +config KLIPS_ENC_NULL
  9985. + bool 'NULL NON-encryption algorithm'
  9986. + default n
  9987. + help
  9988. + NON encryption algo , maybe useful for ESP auth only scenarios
  9989. + (eg: with NAT-T), see RFC 2410.
  9990. +
  9991. +config KLIPS_IPCOMP
  9992. + bool 'IP compression'
  9993. + default y
  9994. + help
  9995. + The IPcomp protocol is used prior to ESP to make the packet
  9996. + smaller. Once encrypted, compression will fail, so any link
  9997. + layer efforts (e.g. PPP) will not work.
  9998. +
  9999. +config KLIPS_DEBUG
  10000. + bool 'IPsec debugging'
  10001. + default y
  10002. + help
  10003. + KLIPS includes a lot of debugging code. Unless there is a real
  10004. + tangible benefit to removing this code, it should be left in place.
  10005. + Debugging connections without access to kernel level debugging is
  10006. + essentially impossible. Leave this on.
  10007. +
  10008. +endmenu
  10009. +
  10010. +#
  10011. +#
  10012. +# $Log: Kconfig,v $
  10013. +# Revision 1.6.2.2 2006/10/11 18:14:33 paul
  10014. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  10015. +# per default.
  10016. +#
  10017. +# Revision 1.6.2.1 2006/04/20 16:33:06 mcr
  10018. +# remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  10019. +# Fix in-kernel module compilation. Sub-makefiles do not work.
  10020. +#
  10021. +# Revision 1.6 2005/05/18 20:55:27 mcr
  10022. +# default cryptoapi to n.
  10023. +#
  10024. +# Revision 1.5 2005/05/11 01:23:25 mcr
  10025. +# added 1DES option to cryptoapi.
  10026. +#
  10027. +# Revision 1.4 2005/04/29 05:29:54 mcr
  10028. +# add option to include cryptoapi algorithms.
  10029. +#
  10030. +# Revision 1.3 2004/08/17 03:27:23 mcr
  10031. +# klips 2.6 edits.
  10032. +#
  10033. +# Revision 1.2 2004/08/14 03:27:39 mcr
  10034. +# 2.6 kernel build/configuration files.
  10035. +#
  10036. +# Revision 1.1 2004/08/14 02:47:55 mcr
  10037. +# kernel build/config patches
  10038. +#
  10039. +# Revision 1.3 2004/02/24 17:17:04 mcr
  10040. +# s/CONFIG_IPSEC/CONFIG_KLIPS/ as 26sec uses "CONFIG_IPSEC" to
  10041. +# turn it on/off as well.
  10042. +#
  10043. +# Revision 1.2 2004/02/22 06:50:42 mcr
  10044. +# kernel 2.6 port - merged with 2.4 code.
  10045. +#
  10046. +# Revision 1.1.2.1 2004/02/20 02:07:53 mcr
  10047. +# module configuration for KLIPS 2.6
  10048. +#
  10049. +#
  10050. +
  10051. --- /dev/null Tue Mar 11 13:02:56 2003
  10052. +++ linux/net/ipsec/Makefile Mon Feb 9 13:51:03 2004
  10053. @@ -0,0 +1,195 @@
  10054. +# Makefile for KLIPS kernel code as a module for 2.6 kernels
  10055. +#
  10056. +# Makefile for KLIPS kernel code as a module
  10057. +# Copyright (C) 1998, 1999, 2000,2001 Richard Guy Briggs.
  10058. +# Copyright (C) 2002-2004 Michael Richardson <mcr@freeswan.org>
  10059. +#
  10060. +# This program is free software; you can redistribute it and/or modify it
  10061. +# under the terms of the GNU General Public License as published by the
  10062. +# Free Software Foundation; either version 2 of the License, or (at your
  10063. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  10064. +#
  10065. +# This program is distributed in the hope that it will be useful, but
  10066. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10067. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10068. +# for more details.
  10069. +#
  10070. +# RCSID $Id: Makefile.fs2_6,v 1.8.2.2 2006/10/11 18:14:33 paul Exp $
  10071. +#
  10072. +# Note! Dependencies are done automagically by 'make dep', which also
  10073. +# removes any old dependencies. DON'T put your own dependencies here
  10074. +# unless it's something special (ie not a .c file).
  10075. +#
  10076. +
  10077. +OPENSWANSRCDIR?=.
  10078. +KLIPS_TOP?=.
  10079. +
  10080. +-include ${OPENSWANSRCDIR}/Makefile.ver
  10081. +
  10082. +base-klips-objs :=
  10083. +
  10084. +base-klips-objs+= ipsec_init.o ipsec_sa.o ipsec_radij.o radij.o
  10085. +base-klips-objs+= ipsec_life.o ipsec_proc.o
  10086. +base-klips-objs+= ipsec_tunnel.o ipsec_xmit.o ipsec_rcv.o ipsec_ipip.o
  10087. +base-klips-objs+= ipsec_snprintf.o
  10088. +base-klips-objs+= sysctl_net_ipsec.o
  10089. +base-klips-objs+= pfkey_v2.o pfkey_v2_parser.o pfkey_v2_ext_process.o
  10090. +base-klips-objs+= version.o
  10091. +
  10092. +base-klips-objs+= satot.o
  10093. +base-klips-objs+= addrtot.o
  10094. +base-klips-objs+= ultot.o
  10095. +base-klips-objs+= addrtypeof.o
  10096. +base-klips-objs+= anyaddr.o
  10097. +base-klips-objs+= initaddr.o
  10098. +base-klips-objs+= ultoa.o
  10099. +base-klips-objs+= addrtoa.o
  10100. +base-klips-objs+= subnettoa.o
  10101. +base-klips-objs+= subnetof.o
  10102. +base-klips-objs+= goodmask.o
  10103. +base-klips-objs+= datatot.o
  10104. +base-klips-objs+= rangetoa.o
  10105. +base-klips-objs+= prng.o
  10106. +base-klips-objs+= pfkey_v2_parse.o
  10107. +base-klips-objs+= pfkey_v2_build.o
  10108. +base-klips-objs+= pfkey_v2_debug.o
  10109. +base-klips-objs+= pfkey_v2_ext_bits.o
  10110. +base-klips-objs+= version.o
  10111. +
  10112. +obj-${CONFIG_KLIPS} += ipsec.o
  10113. +
  10114. +ipsec-objs += ${base-klips-objs}
  10115. +
  10116. +ipsec-$(CONFIG_KLIPS_ESP) += ipsec_esp.o
  10117. +ipsec-$(CONFIG_KLIPS_IPCOMP) += ipsec_ipcomp.o
  10118. +ipsec-$(CONFIG_KLIPS_AUTH_HMAC_MD5) += ipsec_md5c.o
  10119. +ipsec-$(CONFIG_KLIPS_AUTH_HMAC_SHA1) += ipsec_sha1.o
  10120. +
  10121. +# AH, if you really think you need it.
  10122. +ipsec-$(CONFIG_KLIPS_AH) += ipsec_ah.o
  10123. +
  10124. +ipsec-y += ipsec_alg.o
  10125. +
  10126. +# include code from DES subdir
  10127. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/ipsec_alg_3des.o
  10128. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/cbc_enc.o
  10129. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/ecb_enc.o
  10130. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/set_key.o
  10131. +
  10132. +ifeq ($(strip ${SUBARCH}),)
  10133. +SUBARCH:=${ARCH}
  10134. +endif
  10135. +
  10136. +# the assembly version expects frame pointers, which are
  10137. +# optional in many kernel builds. If you want speed, you should
  10138. +# probably use cryptoapi code instead.
  10139. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  10140. +ifeq (${USEASSEMBLY},i386y)
  10141. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/dx86unix.o
  10142. +else
  10143. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/des_enc.o
  10144. +endif
  10145. +
  10146. +# include code from AES subdir
  10147. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/ipsec_alg_aes.o
  10148. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes_xcbc_mac.o
  10149. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes_cbc.o
  10150. +
  10151. +ifeq ($(strip ${SUBARCH}),)
  10152. +SUBARCH:=${ARCH}
  10153. +endif
  10154. +
  10155. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  10156. +ifeq (${USEASSEMBLY},i386y)
  10157. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes-i586.o
  10158. +else
  10159. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes.o
  10160. +endif
  10161. +
  10162. +crypto-$(CONFIG_KLIPS_ENC_NULL) += null/ipsec_alg_null.o
  10163. +
  10164. +ipsec-y += ${crypto-y}
  10165. +
  10166. +ipsec-$(CONFIG_KLIPS_ENC_CRYPTOAPI) += ipsec_alg_cryptoapi.o
  10167. +
  10168. +# IPcomp stuff
  10169. +base-ipcomp-objs := ipcomp.o
  10170. +base-ipcomp-objs += adler32.o
  10171. +base-ipcomp-objs += deflate.o
  10172. +base-ipcomp-objs += infblock.o
  10173. +base-ipcomp-objs += infcodes.o
  10174. +base-ipcomp-objs += inffast.o
  10175. +base-ipcomp-objs += inflate.o
  10176. +base-ipcomp-objs += inftrees.o
  10177. +base-ipcomp-objs += infutil.o
  10178. +base-ipcomp-objs += trees.o
  10179. +base-ipcomp-objs += zutil.o
  10180. +asm-ipcomp-obj-$(CONFIG_M586) += match586.o
  10181. +asm-ipcomp-obj-$(CONFIG_M586TSC) += match586.o
  10182. +asm-ipcomp-obj-$(CONFIG_M586MMX) += match586.o
  10183. +asm-ipcomp-obj-$(CONFIG_M686) += match686.o
  10184. +asm-ipcomp-obj-$(CONFIG_MPENTIUMIII) += match686.o
  10185. +asm-ipcomp-obj-$(CONFIG_MPENTIUM4) += match686.o
  10186. +asm-ipcomp-obj-$(CONFIG_MK6) += match586.o
  10187. +asm-ipcomp-obj-$(CONFIG_MK7) += match686.o
  10188. +asm-ipcomp-obj-$(CONFIG_MCRUSOE) += match586.o
  10189. +asm-ipcomp-obj-$(CONFIG_MWINCHIPC6) += match586.o
  10190. +asm-ipcomp-obj-$(CONFIG_MWINCHIP2) += match686.o
  10191. +asm-ipcomp-obj-$(CONFIG_MWINCHIP3D) += match686.o
  10192. +base-ipcomp-objs += ${asm-ipcomp-obj-y}
  10193. +
  10194. +ipsec-$(CONFIG_KLIPS_IPCOMP) += ${base-ipcomp-objs}
  10195. +
  10196. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  10197. +
  10198. +#
  10199. +# $Log: Makefile.fs2_6,v $
  10200. +# Revision 1.8.2.2 2006/10/11 18:14:33 paul
  10201. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  10202. +# per default.
  10203. +#
  10204. +# Revision 1.8.2.1 2006/04/20 16:33:06 mcr
  10205. +# remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  10206. +# Fix in-kernel module compilation. Sub-makefiles do not work.
  10207. +#
  10208. +# Revision 1.8 2005/05/11 03:15:42 mcr
  10209. +# adjusted makefiles to sanely build modules properly.
  10210. +#
  10211. +# Revision 1.7 2005/04/13 22:52:12 mcr
  10212. +# moved KLIPS specific snprintf() wrapper to seperate file.
  10213. +#
  10214. +# Revision 1.6 2004/08/22 05:02:03 mcr
  10215. +# organized symbols such that it is easier to build modules.
  10216. +#
  10217. +# Revision 1.5 2004/08/18 01:43:56 mcr
  10218. +# adjusted makefile enumation so that it can be used by module
  10219. +# wrapper.
  10220. +#
  10221. +# Revision 1.4 2004/08/17 03:27:23 mcr
  10222. +# klips 2.6 edits.
  10223. +#
  10224. +# Revision 1.3 2004/08/04 16:50:13 mcr
  10225. +# removed duplicate definition of dx86unix.o
  10226. +#
  10227. +# Revision 1.2 2004/08/03 18:21:09 mcr
  10228. +# only set KLIPS_TOP and OPENSWANSRCDIR if not already set.
  10229. +#
  10230. +# Revision 1.1 2004/07/26 15:02:22 mcr
  10231. +# makefile for KLIPS module for 2.6.
  10232. +#
  10233. +# Revision 1.3 2004/02/24 17:17:04 mcr
  10234. +# s/CONFIG_IPSEC/CONFIG_KLIPS/ as 26sec uses "CONFIG_IPSEC" to
  10235. +# turn it on/off as well.
  10236. +#
  10237. +# Revision 1.2 2004/02/22 06:50:42 mcr
  10238. +# kernel 2.6 port - merged with 2.4 code.
  10239. +#
  10240. +# Revision 1.1.2.1 2004/02/20 02:07:53 mcr
  10241. +# module configuration for KLIPS 2.6
  10242. +#
  10243. +#
  10244. +# Local Variables:
  10245. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  10246. +# End Variables:
  10247. +#
  10248. +
  10249. --- /dev/null Tue Mar 11 13:02:56 2003
  10250. +++ linux/net/ipsec/README-zlib Mon Feb 9 13:51:03 2004
  10251. @@ -0,0 +1,147 @@
  10252. +zlib 1.1.4 is a general purpose data compression library. All the code
  10253. +is thread safe. The data format used by the zlib library
  10254. +is described by RFCs (Request for Comments) 1950 to 1952 in the files
  10255. +http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate
  10256. +format) and rfc1952.txt (gzip format). These documents are also available in
  10257. +other formats from ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html
  10258. +
  10259. +All functions of the compression library are documented in the file zlib.h
  10260. +(volunteer to write man pages welcome, contact jloup@gzip.org). A usage
  10261. +example of the library is given in the file example.c which also tests that
  10262. +the library is working correctly. Another example is given in the file
  10263. +minigzip.c. The compression library itself is composed of all source files
  10264. +except example.c and minigzip.c.
  10265. +
  10266. +To compile all files and run the test program, follow the instructions
  10267. +given at the top of Makefile. In short "make test; make install"
  10268. +should work for most machines. For Unix: "./configure; make test; make install"
  10269. +For MSDOS, use one of the special makefiles such as Makefile.msc.
  10270. +For VMS, use Make_vms.com or descrip.mms.
  10271. +
  10272. +Questions about zlib should be sent to <zlib@gzip.org>, or to
  10273. +Gilles Vollant <info@winimage.com> for the Windows DLL version.
  10274. +The zlib home page is http://www.zlib.org or http://www.gzip.org/zlib/
  10275. +Before reporting a problem, please check this site to verify that
  10276. +you have the latest version of zlib; otherwise get the latest version and
  10277. +check whether the problem still exists or not.
  10278. +
  10279. +PLEASE read the zlib FAQ http://www.gzip.org/zlib/zlib_faq.html
  10280. +before asking for help.
  10281. +
  10282. +Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
  10283. +issue of Dr. Dobb's Journal; a copy of the article is available in
  10284. +http://dogma.net/markn/articles/zlibtool/zlibtool.htm
  10285. +
  10286. +The changes made in version 1.1.4 are documented in the file ChangeLog.
  10287. +The only changes made since 1.1.3 are bug corrections:
  10288. +
  10289. +- ZFREE was repeated on same allocation on some error conditions.
  10290. + This creates a security problem described in
  10291. + http://www.zlib.org/advisory-2002-03-11.txt
  10292. +- Returned incorrect error (Z_MEM_ERROR) on some invalid data
  10293. +- Avoid accesses before window for invalid distances with inflate window
  10294. + less than 32K.
  10295. +- force windowBits > 8 to avoid a bug in the encoder for a window size
  10296. + of 256 bytes. (A complete fix will be available in 1.1.5).
  10297. +
  10298. +The beta version 1.1.5beta includes many more changes. A new official
  10299. +version 1.1.5 will be released as soon as extensive testing has been
  10300. +completed on it.
  10301. +
  10302. +
  10303. +Unsupported third party contributions are provided in directory "contrib".
  10304. +
  10305. +A Java implementation of zlib is available in the Java Development Kit
  10306. +http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html
  10307. +See the zlib home page http://www.zlib.org for details.
  10308. +
  10309. +A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk>
  10310. +is in the CPAN (Comprehensive Perl Archive Network) sites
  10311. +http://www.cpan.org/modules/by-module/Compress/
  10312. +
  10313. +A Python interface to zlib written by A.M. Kuchling <amk@magnet.com>
  10314. +is available in Python 1.5 and later versions, see
  10315. +http://www.python.org/doc/lib/module-zlib.html
  10316. +
  10317. +A zlib binding for TCL written by Andreas Kupries <a.kupries@westend.com>
  10318. +is availlable at http://www.westend.com/~kupries/doc/trf/man/man.html
  10319. +
  10320. +An experimental package to read and write files in .zip format,
  10321. +written on top of zlib by Gilles Vollant <info@winimage.com>, is
  10322. +available at http://www.winimage.com/zLibDll/unzip.html
  10323. +and also in the contrib/minizip directory of zlib.
  10324. +
  10325. +
  10326. +Notes for some targets:
  10327. +
  10328. +- To build a Windows DLL version, include in a DLL project zlib.def, zlib.rc
  10329. + and all .c files except example.c and minigzip.c; compile with -DZLIB_DLL
  10330. + The zlib DLL support was initially done by Alessandro Iacopetti and is
  10331. + now maintained by Gilles Vollant <info@winimage.com>. Check the zlib DLL
  10332. + home page at http://www.winimage.com/zLibDll
  10333. +
  10334. + From Visual Basic, you can call the DLL functions which do not take
  10335. + a structure as argument: compress, uncompress and all gz* functions.
  10336. + See contrib/visual-basic.txt for more information, or get
  10337. + http://www.tcfb.com/dowseware/cmp-z-it.zip
  10338. +
  10339. +- For 64-bit Irix, deflate.c must be compiled without any optimization.
  10340. + With -O, one libpng test fails. The test works in 32 bit mode (with
  10341. + the -n32 compiler flag). The compiler bug has been reported to SGI.
  10342. +
  10343. +- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1
  10344. + it works when compiled with cc.
  10345. +
  10346. +- on Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1
  10347. + is necessary to get gzprintf working correctly. This is done by configure.
  10348. +
  10349. +- zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works
  10350. + with other compilers. Use "make test" to check your compiler.
  10351. +
  10352. +- gzdopen is not supported on RISCOS, BEOS and by some Mac compilers.
  10353. +
  10354. +- For Turbo C the small model is supported only with reduced performance to
  10355. + avoid any far allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
  10356. +
  10357. +- For PalmOs, see http://www.cs.uit.no/~perm/PASTA/pilot/software.html
  10358. + Per Harald Myrvang <perm@stud.cs.uit.no>
  10359. +
  10360. +
  10361. +Acknowledgments:
  10362. +
  10363. + The deflate format used by zlib was defined by Phil Katz. The deflate
  10364. + and zlib specifications were written by L. Peter Deutsch. Thanks to all the
  10365. + people who reported problems and suggested various improvements in zlib;
  10366. + they are too numerous to cite here.
  10367. +
  10368. +Copyright notice:
  10369. +
  10370. + (C) 1995-2002 Jean-loup Gailly and Mark Adler
  10371. +
  10372. + This software is provided 'as-is', without any express or implied
  10373. + warranty. In no event will the authors be held liable for any damages
  10374. + arising from the use of this software.
  10375. +
  10376. + Permission is granted to anyone to use this software for any purpose,
  10377. + including commercial applications, and to alter it and redistribute it
  10378. + freely, subject to the following restrictions:
  10379. +
  10380. + 1. The origin of this software must not be misrepresented; you must not
  10381. + claim that you wrote the original software. If you use this software
  10382. + in a product, an acknowledgment in the product documentation would be
  10383. + appreciated but is not required.
  10384. + 2. Altered source versions must be plainly marked as such, and must not be
  10385. + misrepresented as being the original software.
  10386. + 3. This notice may not be removed or altered from any source distribution.
  10387. +
  10388. + Jean-loup Gailly Mark Adler
  10389. + jloup@gzip.org madler@alumni.caltech.edu
  10390. +
  10391. +If you use the zlib library in a product, we would appreciate *not*
  10392. +receiving lengthy legal documents to sign. The sources are provided
  10393. +for free but without warranty of any kind. The library has been
  10394. +entirely written by Jean-loup Gailly and Mark Adler; it does not
  10395. +include third-party code.
  10396. +
  10397. +If you redistribute modified sources, we would appreciate that you include
  10398. +in the file ChangeLog history information documenting your changes.
  10399. --- /dev/null Tue Mar 11 13:02:56 2003
  10400. +++ linux/net/ipsec/README-zlib.freeswan Mon Feb 9 13:51:03 2004
  10401. @@ -0,0 +1,13 @@
  10402. +The only changes made to these files for use in FreeS/WAN are:
  10403. +
  10404. + - In zconf.h, macros are defined to prefix global symbols with "ipcomp_"
  10405. + (or "_ipcomp"), when compiled with -DIPCOMP_PREFIX.
  10406. + - The copyright strings are defined local (static)
  10407. +
  10408. + The above changes are made to avoid name collisions with ppp_deflate
  10409. + and ext2compr.
  10410. +
  10411. + - Files not needed for FreeS/WAN have been removed
  10412. +
  10413. + See the "README" file for information about where to obtain the complete
  10414. + zlib package.
  10415. --- /dev/null Tue Mar 11 13:02:56 2003
  10416. +++ linux/net/ipsec/addrtoa.c Mon Feb 9 13:51:03 2004
  10417. @@ -0,0 +1,67 @@
  10418. +/*
  10419. + * addresses to ASCII
  10420. + * Copyright (C) 1998, 1999 Henry Spencer.
  10421. + *
  10422. + * This library is free software; you can redistribute it and/or modify it
  10423. + * under the terms of the GNU Library General Public License as published by
  10424. + * the Free Software Foundation; either version 2 of the License, or (at your
  10425. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10426. + *
  10427. + * This library is distributed in the hope that it will be useful, but
  10428. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10429. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10430. + * License for more details.
  10431. + *
  10432. + * RCSID $Id: addrtoa.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  10433. + */
  10434. +#include "openswan.h"
  10435. +
  10436. +#define NBYTES 4 /* bytes in an address */
  10437. +#define PERBYTE 4 /* three digits plus a dot or NUL */
  10438. +#define BUFLEN (NBYTES*PERBYTE)
  10439. +
  10440. +#if BUFLEN != ADDRTOA_BUF
  10441. +#error "ADDRTOA_BUF in openswan.h inconsistent with addrtoa() code"
  10442. +#endif
  10443. +
  10444. +/*
  10445. + - addrtoa - convert binary address to ASCII dotted decimal
  10446. + */
  10447. +size_t /* space needed for full conversion */
  10448. +addrtoa(addr, format, dst, dstlen)
  10449. +struct in_addr addr;
  10450. +int format; /* character */
  10451. +char *dst; /* need not be valid if dstlen is 0 */
  10452. +size_t dstlen;
  10453. +{
  10454. + unsigned long a = ntohl(addr.s_addr);
  10455. + int i;
  10456. + size_t n;
  10457. + unsigned long byte;
  10458. + char buf[BUFLEN];
  10459. + char *p;
  10460. +
  10461. + switch (format) {
  10462. + case 0:
  10463. + break;
  10464. + default:
  10465. + return 0;
  10466. + break;
  10467. + }
  10468. +
  10469. + p = buf;
  10470. + for (i = NBYTES-1; i >= 0; i--) {
  10471. + byte = (a >> (i*8)) & 0xff;
  10472. + p += ultoa(byte, 10, p, PERBYTE);
  10473. + if (i != 0)
  10474. + *(p-1) = '.';
  10475. + }
  10476. + n = p - buf;
  10477. +
  10478. + if (dstlen > 0) {
  10479. + if (n > dstlen)
  10480. + buf[dstlen - 1] = '\0';
  10481. + strcpy(dst, buf);
  10482. + }
  10483. + return n;
  10484. +}
  10485. --- /dev/null Tue Mar 11 13:02:56 2003
  10486. +++ linux/net/ipsec/addrtot.c Mon Feb 9 13:51:03 2004
  10487. @@ -0,0 +1,423 @@
  10488. +/*
  10489. + * addresses to text
  10490. + * Copyright (C) 2000 Henry Spencer.
  10491. + *
  10492. + * This library is free software; you can redistribute it and/or modify it
  10493. + * under the terms of the GNU Library General Public License as published by
  10494. + * the Free Software Foundation; either version 2 of the License, or (at your
  10495. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10496. + *
  10497. + * This library is distributed in the hope that it will be useful, but
  10498. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10499. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10500. + * License for more details.
  10501. + *
  10502. + * RCSID $Id: addrtot.c,v 1.22.2.1 2005/11/17 22:30:49 paul Exp $
  10503. + */
  10504. +
  10505. +#if defined(__KERNEL__) && defined(__HAVE_ARCH_STRSTR)
  10506. +#include <linux/string.h>
  10507. +#endif
  10508. +
  10509. +#include "openswan.h"
  10510. +
  10511. +#define IP4BYTES 4 /* bytes in an IPv4 address */
  10512. +#define PERBYTE 4 /* three digits plus a dot or NUL */
  10513. +#define IP6BYTES 16 /* bytes in an IPv6 address */
  10514. +
  10515. +/* forwards */
  10516. +static size_t normal4(const unsigned char *s, size_t len, char *b, char **dp);
  10517. +static size_t normal6(const unsigned char *s, size_t len, char *b, char **dp, int squish);
  10518. +static size_t reverse4(const unsigned char *s, size_t len, char *b, char **dp);
  10519. +static size_t reverse6(const unsigned char *s, size_t len, char *b, char **dp);
  10520. +
  10521. +#if defined(__KERNEL__) && !defined(__HAVE_ARCH_STRSTR)
  10522. +#define strstr ipsec_strstr
  10523. +/*
  10524. + * Find the first occurrence of find in s.
  10525. + * (from NetBSD 1.6's /src/lib/libc/string/strstr.c)
  10526. + */
  10527. +static char *
  10528. +strstr(s, find)
  10529. + const char *s, *find;
  10530. +{
  10531. + char c, sc;
  10532. + size_t len;
  10533. +
  10534. + if ((c = *find++) != 0) {
  10535. + len = strlen(find);
  10536. + do {
  10537. + do {
  10538. + if ((sc = *s++) == 0)
  10539. + return (NULL);
  10540. + } while (sc != c);
  10541. + } while (strncmp(s, find, len) != 0);
  10542. + s--;
  10543. + }
  10544. + /* LINTED interface specification */
  10545. + return ((char *)s);
  10546. +}
  10547. +#endif
  10548. +
  10549. +/*
  10550. + - addrtot - convert binary address to text (dotted decimal or IPv6 string)
  10551. + */
  10552. +size_t /* space needed for full conversion */
  10553. +addrtot(src, format, dst, dstlen)
  10554. +const ip_address *src;
  10555. +int format; /* character */
  10556. +char *dst; /* need not be valid if dstlen is 0 */
  10557. +size_t dstlen;
  10558. +{
  10559. + const unsigned char *b;
  10560. + size_t n;
  10561. + char buf[1+ADDRTOT_BUF+1]; /* :address: */
  10562. + char *p;
  10563. + int t = addrtypeof(src);
  10564. +# define TF(t, f) (((t)<<8) | (f))
  10565. +
  10566. + n = addrbytesptr(src, &b);
  10567. + if (n == 0) {
  10568. + bad:
  10569. + dst[0]='\0';
  10570. + strncat(dst, "<invalid>", dstlen);
  10571. + return sizeof("<invalid>");
  10572. + }
  10573. +
  10574. + switch (TF(t, format)) {
  10575. + case TF(AF_INET, 0):
  10576. + n = normal4(b, n, buf, &p);
  10577. + break;
  10578. + case TF(AF_INET6, 0):
  10579. + n = normal6(b, n, buf, &p, 1);
  10580. + break;
  10581. + case TF(AF_INET, 'Q'):
  10582. + n = normal4(b, n, buf, &p);
  10583. + break;
  10584. + case TF(AF_INET6, 'Q'):
  10585. + n = normal6(b, n, buf, &p, 0);
  10586. + break;
  10587. + case TF(AF_INET, 'r'):
  10588. + n = reverse4(b, n, buf, &p);
  10589. + break;
  10590. + case TF(AF_INET6, 'r'):
  10591. + n = reverse6(b, n, buf, &p);
  10592. + break;
  10593. + default: /* including (AF_INET, 'R') */
  10594. + goto bad;
  10595. + break;
  10596. + }
  10597. +
  10598. + if (dstlen > 0) {
  10599. + if (dstlen < n)
  10600. + p[dstlen - 1] = '\0';
  10601. + strcpy(dst, p);
  10602. + }
  10603. + return n;
  10604. +}
  10605. +
  10606. +/*
  10607. + - normal4 - normal IPv4 address-text conversion
  10608. + */
  10609. +static size_t /* size of text, including NUL */
  10610. +normal4(srcp, srclen, buf, dstp)
  10611. +const unsigned char *srcp;
  10612. +size_t srclen;
  10613. +char *buf; /* guaranteed large enough */
  10614. +char **dstp; /* where to put result pointer */
  10615. +{
  10616. + int i;
  10617. + char *p;
  10618. +
  10619. + if (srclen != IP4BYTES) /* "can't happen" */
  10620. + return 0;
  10621. + p = buf;
  10622. + for (i = 0; i < IP4BYTES; i++) {
  10623. + p += ultot(srcp[i], 10, p, PERBYTE);
  10624. + if (i != IP4BYTES - 1)
  10625. + *(p-1) = '.'; /* overwrites the NUL */
  10626. + }
  10627. + *dstp = buf;
  10628. + return p - buf;
  10629. +}
  10630. +
  10631. +/*
  10632. + - normal6 - normal IPv6 address-text conversion
  10633. + */
  10634. +static size_t /* size of text, including NUL */
  10635. +normal6(srcp, srclen, buf, dstp, squish)
  10636. +const unsigned char *srcp;
  10637. +size_t srclen;
  10638. +char *buf; /* guaranteed large enough, plus 2 */
  10639. +char **dstp; /* where to put result pointer */
  10640. +int squish; /* whether to squish out 0:0 */
  10641. +{
  10642. + int i;
  10643. + unsigned long piece;
  10644. + char *p;
  10645. + char *q;
  10646. +
  10647. + if (srclen != IP6BYTES) /* "can't happen" */
  10648. + return 0;
  10649. + p = buf;
  10650. + *p++ = ':';
  10651. + for (i = 0; i < IP6BYTES/2; i++) {
  10652. + piece = (srcp[2*i] << 8) + srcp[2*i + 1];
  10653. + p += ultot(piece, 16, p, 5); /* 5 = abcd + NUL */
  10654. + *(p-1) = ':'; /* overwrites the NUL */
  10655. + }
  10656. + *p = '\0';
  10657. + q = strstr(buf, ":0:0:");
  10658. + if (squish && q != NULL) { /* zero squishing is possible */
  10659. + p = q + 1;
  10660. + while (*p == '0' && *(p+1) == ':')
  10661. + p += 2;
  10662. + q++;
  10663. + *q++ = ':'; /* overwrite first 0 */
  10664. + while (*p != '\0')
  10665. + *q++ = *p++;
  10666. + *q = '\0';
  10667. + if (!(*(q-1) == ':' && *(q-2) == ':'))
  10668. + *--q = '\0'; /* strip final : unless :: */
  10669. + p = buf;
  10670. + if (!(*p == ':' && *(p+1) == ':'))
  10671. + p++; /* skip initial : unless :: */
  10672. + } else {
  10673. + q = p;
  10674. + *--q = '\0'; /* strip final : */
  10675. + p = buf + 1; /* skip initial : */
  10676. + }
  10677. + *dstp = p;
  10678. + return q - p + 1;
  10679. +}
  10680. +
  10681. +/*
  10682. + - reverse4 - IPv4 reverse-lookup conversion
  10683. + */
  10684. +static size_t /* size of text, including NUL */
  10685. +reverse4(srcp, srclen, buf, dstp)
  10686. +const unsigned char *srcp;
  10687. +size_t srclen;
  10688. +char *buf; /* guaranteed large enough */
  10689. +char **dstp; /* where to put result pointer */
  10690. +{
  10691. + int i;
  10692. + char *p;
  10693. +
  10694. + if (srclen != IP4BYTES) /* "can't happen" */
  10695. + return 0;
  10696. + p = buf;
  10697. + for (i = IP4BYTES-1; i >= 0; i--) {
  10698. + p += ultot(srcp[i], 10, p, PERBYTE);
  10699. + *(p-1) = '.'; /* overwrites the NUL */
  10700. + }
  10701. + strcpy(p, "IN-ADDR.ARPA.");
  10702. + *dstp = buf;
  10703. + return strlen(buf) + 1;
  10704. +}
  10705. +
  10706. +/*
  10707. + - reverse6 - IPv6 reverse-lookup conversion (RFC 1886)
  10708. + * A trifle inefficient, really shouldn't use ultot...
  10709. + */
  10710. +static size_t /* size of text, including NUL */
  10711. +reverse6(srcp, srclen, buf, dstp)
  10712. +const unsigned char *srcp;
  10713. +size_t srclen;
  10714. +char *buf; /* guaranteed large enough */
  10715. +char **dstp; /* where to put result pointer */
  10716. +{
  10717. + int i;
  10718. + unsigned long piece;
  10719. + char *p;
  10720. +
  10721. + if (srclen != IP6BYTES) /* "can't happen" */
  10722. + return 0;
  10723. + p = buf;
  10724. + for (i = IP6BYTES-1; i >= 0; i--) {
  10725. + piece = srcp[i];
  10726. + p += ultot(piece&0xf, 16, p, 2);
  10727. + *(p-1) = '.';
  10728. + p += ultot(piece>>4, 16, p, 2);
  10729. + *(p-1) = '.';
  10730. + }
  10731. + strcpy(p, "IP6.ARPA.");
  10732. + *dstp = buf;
  10733. + return strlen(buf) + 1;
  10734. +}
  10735. +
  10736. +/*
  10737. + - reverse6 - modern IPv6 reverse-lookup conversion (RFC 2874)
  10738. + * this version removed as it was obsoleted in the end.
  10739. + */
  10740. +
  10741. +#ifdef ADDRTOT_MAIN
  10742. +
  10743. +#include <stdio.h>
  10744. +#include <sys/socket.h>
  10745. +#include <netinet/in.h>
  10746. +#include <arpa/inet.h>
  10747. +
  10748. +void regress(void);
  10749. +
  10750. +int
  10751. +main(int argc, char *argv[])
  10752. +{
  10753. + if (argc < 2) {
  10754. + fprintf(stderr, "Usage: %s {addr|net/mask|begin...end|-r}\n",
  10755. + argv[0]);
  10756. + exit(2);
  10757. + }
  10758. +
  10759. + if (strcmp(argv[1], "-r") == 0) {
  10760. + regress();
  10761. + fprintf(stderr, "regress() returned?!?\n");
  10762. + exit(1);
  10763. + }
  10764. + exit(0);
  10765. +}
  10766. +
  10767. +struct rtab {
  10768. + char *input;
  10769. + char format;
  10770. + char *output; /* NULL means error expected */
  10771. +} rtab[] = {
  10772. + {"1.2.3.0", 0, "1.2.3.0"},
  10773. + {"1:2::3:4", 0, "1:2::3:4"},
  10774. + {"1:2::3:4", 'Q', "1:2:0:0:0:0:3:4"},
  10775. + {"1:2:0:0:3:4:0:0", 0, "1:2::3:4:0:0"},
  10776. + {"1.2.3.4", 'r' , "4.3.2.1.IN-ADDR.ARPA."},
  10777. + /* 0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  10778. + {"1:2::3:4", 'r', "4.0.0.0.3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.1.0.0.0.IP6.ARPA."},
  10779. + {NULL, 0, NULL}
  10780. +};
  10781. +
  10782. +void
  10783. +regress()
  10784. +{
  10785. + struct rtab *r;
  10786. + int status = 0;
  10787. + ip_address a;
  10788. + char in[100];
  10789. + char buf[100];
  10790. + const char *oops;
  10791. + size_t n;
  10792. +
  10793. + for (r = rtab; r->input != NULL; r++) {
  10794. + strcpy(in, r->input);
  10795. +
  10796. + /* convert it *to* internal format */
  10797. + oops = ttoaddr(in, strlen(in), 0, &a);
  10798. +
  10799. + /* now convert it back */
  10800. +
  10801. + n = addrtot(&a, r->format, buf, sizeof(buf));
  10802. +
  10803. + if (n == 0 && r->output == NULL)
  10804. + {} /* okay, error expected */
  10805. +
  10806. + else if (n == 0) {
  10807. + printf("`%s' atoasr failed\n", r->input);
  10808. + status = 1;
  10809. +
  10810. + } else if (r->output == NULL) {
  10811. + printf("`%s' atoasr succeeded unexpectedly '%c'\n",
  10812. + r->input, r->format);
  10813. + status = 1;
  10814. + } else {
  10815. + if (strcasecmp(r->output, buf) != 0) {
  10816. + printf("`%s' '%c' gave `%s', expected `%s'\n",
  10817. + r->input, r->format, buf, r->output);
  10818. + status = 1;
  10819. + }
  10820. + }
  10821. + }
  10822. + exit(status);
  10823. +}
  10824. +
  10825. +#endif /* ADDRTOT_MAIN */
  10826. +
  10827. +/*
  10828. + * $Log: addrtot.c,v $
  10829. + * Revision 1.22.2.1 2005/11/17 22:30:49 paul
  10830. + * pull up strstr fix from head.
  10831. + *
  10832. + * Revision 1.22 2005/05/20 16:47:40 mcr
  10833. + * make strstr static if we need it.
  10834. + *
  10835. + * Revision 1.21 2005/03/21 00:35:12 mcr
  10836. + * test for strstr properly
  10837. + *
  10838. + * Revision 1.20 2004/11/09 22:52:20 mcr
  10839. + * until we figure out which kernels have strsep and which
  10840. + * do not (UML does not under certain circumstances), then
  10841. + * let's just provide our own.
  10842. + *
  10843. + * Revision 1.19 2004/10/08 16:30:33 mcr
  10844. + * pull-up of initial crypto-offload work.
  10845. + *
  10846. + * Revision 1.18 2004/09/18 19:33:08 mcr
  10847. + * use an appropriate kernel happy ifdef for strstr.
  10848. + *
  10849. + * Revision 1.17 2004/09/15 21:49:02 mcr
  10850. + * use local copy of strstr() if this is going in the kernel.
  10851. + * Not clear why this worked before, or why this shows up
  10852. + * for modules only.
  10853. + *
  10854. + * Revision 1.16 2004/07/10 07:43:47 mcr
  10855. + * Moved from linux/lib/libfreeswan/addrtot.c,v
  10856. + *
  10857. + * Revision 1.15 2004/04/11 17:39:25 mcr
  10858. + * removed internal.h requirements.
  10859. + *
  10860. + * Revision 1.14 2004/03/08 01:59:08 ken
  10861. + * freeswan.h -> openswan.h
  10862. + *
  10863. + * Revision 1.13 2004/01/05 23:21:05 mcr
  10864. + * if the address type is invalid, then return length of <invalid>
  10865. + * string!
  10866. + *
  10867. + * Revision 1.12 2003/12/30 06:42:48 mcr
  10868. + * added $Log: addrtot.c,v $
  10869. + * added Revision 1.22.2.1 2005/11/17 22:30:49 paul
  10870. + * added pull up strstr fix from head.
  10871. + * added
  10872. + * added Revision 1.22 2005/05/20 16:47:40 mcr
  10873. + * added make strstr static if we need it.
  10874. + * added
  10875. + * added Revision 1.21 2005/03/21 00:35:12 mcr
  10876. + * added test for strstr properly
  10877. + * added
  10878. + * added Revision 1.20 2004/11/09 22:52:20 mcr
  10879. + * added until we figure out which kernels have strsep and which
  10880. + * added do not (UML does not under certain circumstances), then
  10881. + * added let's just provide our own.
  10882. + * added
  10883. + * added Revision 1.19 2004/10/08 16:30:33 mcr
  10884. + * added pull-up of initial crypto-offload work.
  10885. + * added
  10886. + * added Revision 1.18 2004/09/18 19:33:08 mcr
  10887. + * added use an appropriate kernel happy ifdef for strstr.
  10888. + * added
  10889. + * added Revision 1.17 2004/09/15 21:49:02 mcr
  10890. + * added use local copy of strstr() if this is going in the kernel.
  10891. + * added Not clear why this worked before, or why this shows up
  10892. + * added for modules only.
  10893. + * added
  10894. + * added Revision 1.16 2004/07/10 07:43:47 mcr
  10895. + * added Moved from linux/lib/libfreeswan/addrtot.c,v
  10896. + * added
  10897. + * added Revision 1.15 2004/04/11 17:39:25 mcr
  10898. + * added removed internal.h requirements.
  10899. + * added
  10900. + * added Revision 1.14 2004/03/08 01:59:08 ken
  10901. + * added freeswan.h -> openswan.h
  10902. + * added
  10903. + * added Revision 1.13 2004/01/05 23:21:05 mcr
  10904. + * added if the address type is invalid, then return length of <invalid>
  10905. + * added string!
  10906. + * added
  10907. + *
  10908. + *
  10909. + */
  10910. +
  10911. --- /dev/null Tue Mar 11 13:02:56 2003
  10912. +++ linux/net/ipsec/addrtypeof.c Mon Feb 9 13:51:03 2004
  10913. @@ -0,0 +1,93 @@
  10914. +/*
  10915. + * extract parts of an ip_address
  10916. + * Copyright (C) 2000 Henry Spencer.
  10917. + *
  10918. + * This library is free software; you can redistribute it and/or modify it
  10919. + * under the terms of the GNU Library General Public License as published by
  10920. + * the Free Software Foundation; either version 2 of the License, or (at your
  10921. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10922. + *
  10923. + * This library is distributed in the hope that it will be useful, but
  10924. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10925. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10926. + * License for more details.
  10927. + *
  10928. + * RCSID $Id: addrtypeof.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  10929. + */
  10930. +#include "openswan.h"
  10931. +
  10932. +/*
  10933. + - addrtypeof - get the type of an ip_address
  10934. + */
  10935. +int
  10936. +addrtypeof(src)
  10937. +const ip_address *src;
  10938. +{
  10939. + return src->u.v4.sin_family;
  10940. +}
  10941. +
  10942. +/*
  10943. + - addrbytesptr - get pointer to the address bytes of an ip_address
  10944. + */
  10945. +size_t /* 0 for error */
  10946. +addrbytesptr(src, dstp)
  10947. +const ip_address *src;
  10948. +const unsigned char **dstp; /* NULL means just a size query */
  10949. +{
  10950. + const unsigned char *p;
  10951. + size_t n;
  10952. +
  10953. + switch (src->u.v4.sin_family) {
  10954. + case AF_INET:
  10955. + p = (const unsigned char *)&src->u.v4.sin_addr.s_addr;
  10956. + n = 4;
  10957. + break;
  10958. + case AF_INET6:
  10959. + p = (const unsigned char *)&src->u.v6.sin6_addr;
  10960. + n = 16;
  10961. + break;
  10962. + default:
  10963. + return 0;
  10964. + break;
  10965. + }
  10966. +
  10967. + if (dstp != NULL)
  10968. + *dstp = p;
  10969. + return n;
  10970. +}
  10971. +
  10972. +/*
  10973. + - addrlenof - get length of the address bytes of an ip_address
  10974. + */
  10975. +size_t /* 0 for error */
  10976. +addrlenof(src)
  10977. +const ip_address *src;
  10978. +{
  10979. + return addrbytesptr(src, NULL);
  10980. +}
  10981. +
  10982. +/*
  10983. + - addrbytesof - get the address bytes of an ip_address
  10984. + */
  10985. +size_t /* 0 for error */
  10986. +addrbytesof(src, dst, dstlen)
  10987. +const ip_address *src;
  10988. +unsigned char *dst;
  10989. +size_t dstlen;
  10990. +{
  10991. + const unsigned char *p;
  10992. + size_t n;
  10993. + size_t ncopy;
  10994. +
  10995. + n = addrbytesptr(src, &p);
  10996. + if (n == 0)
  10997. + return 0;
  10998. +
  10999. + if (dstlen > 0) {
  11000. + ncopy = n;
  11001. + if (ncopy > dstlen)
  11002. + ncopy = dstlen;
  11003. + memcpy(dst, p, ncopy);
  11004. + }
  11005. + return n;
  11006. +}
  11007. --- /dev/null Tue Mar 11 13:02:56 2003
  11008. +++ linux/net/ipsec/adler32.c Mon Feb 9 13:51:03 2004
  11009. @@ -0,0 +1,49 @@
  11010. +/* adler32.c -- compute the Adler-32 checksum of a data stream
  11011. + * Copyright (C) 1995-2002 Mark Adler
  11012. + * For conditions of distribution and use, see copyright notice in zlib.h
  11013. + */
  11014. +
  11015. +/* @(#) $Id: adler32.c,v 1.6 2004/07/10 19:11:18 mcr Exp $ */
  11016. +
  11017. +#include <zlib/zlib.h>
  11018. +#include <zlib/zconf.h>
  11019. +
  11020. +#define BASE 65521L /* largest prime smaller than 65536 */
  11021. +#define NMAX 5552
  11022. +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  11023. +
  11024. +#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  11025. +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  11026. +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  11027. +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  11028. +#define DO16(buf) DO8(buf,0); DO8(buf,8);
  11029. +
  11030. +/* ========================================================================= */
  11031. +uLong ZEXPORT adler32(adler, buf, len)
  11032. + uLong adler;
  11033. + const Bytef *buf;
  11034. + uInt len;
  11035. +{
  11036. + unsigned long s1 = adler & 0xffff;
  11037. + unsigned long s2 = (adler >> 16) & 0xffff;
  11038. + int k;
  11039. +
  11040. + if (buf == Z_NULL) return 1L;
  11041. +
  11042. + while (len > 0) {
  11043. + k = len < NMAX ? len : NMAX;
  11044. + len -= k;
  11045. + while (k >= 16) {
  11046. + DO16(buf);
  11047. + buf += 16;
  11048. + k -= 16;
  11049. + }
  11050. + if (k != 0) do {
  11051. + s1 += *buf++;
  11052. + s2 += s1;
  11053. + } while (--k);
  11054. + s1 %= BASE;
  11055. + s2 %= BASE;
  11056. + }
  11057. + return (s2 << 16) | s1;
  11058. +}
  11059. --- /dev/null Tue Mar 11 13:02:56 2003
  11060. +++ linux/net/ipsec/aes/Makefile Mon Feb 9 13:51:03 2004
  11061. @@ -0,0 +1,59 @@
  11062. +# Makefile for KLIPS 3DES kernel code as a module for 2.6 kernels
  11063. +#
  11064. +# Makefile for KLIPS kernel code as a module
  11065. +# Copyright (C) 2002-2004 Michael Richardson <mcr@xelerance.com>
  11066. +#
  11067. +# This program is free software; you can redistribute it and/or modify it
  11068. +# under the terms of the GNU General Public License as published by the
  11069. +# Free Software Foundation; either version 2 of the License, or (at your
  11070. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11071. +#
  11072. +# This program is distributed in the hope that it will be useful, but
  11073. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11074. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11075. +# for more details.
  11076. +#
  11077. +# RCSID $Id: Makefile.fs2_6,v 1.1.10.1 2005/08/12 16:10:05 ken Exp $
  11078. +#
  11079. +# Note! Dependencies are done automagically by 'make dep', which also
  11080. +# removes any old dependencies. DON'T put your own dependencies here
  11081. +# unless it's something special (ie not a .c file).
  11082. +#
  11083. +
  11084. +obj-$(CONFIG_KLIPS_ENC_AES) += ipsec_alg_aes.o
  11085. +obj-$(CONFIG_KLIPS_ENC_AES) += aes_xcbc_mac.o
  11086. +obj-$(CONFIG_KLIPS_ENC_AES) += aes_cbc.o
  11087. +
  11088. +ifeq ($(strip ${SUBARCH}),)
  11089. +SUBARCH:=${ARCH}
  11090. +endif
  11091. +
  11092. +# the assembly version expects frame pointers, which are
  11093. +# optional in many kernel builds. If you want speed, you should
  11094. +# probably use cryptoapi code instead.
  11095. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  11096. +ifeq (${USEASSEMBLY},i386y)
  11097. +obj-$(CONFIG_KLIPS_ENC_AES) += aes-i586.o
  11098. +else
  11099. +obj-$(CONFIG_KLIPS_ENC_AES) += aes.o
  11100. +endif
  11101. +
  11102. +
  11103. +#
  11104. +# $Log: Makefile.fs2_6,v $
  11105. +# Revision 1.1.10.1 2005/08/12 16:10:05 ken
  11106. +# do not use assembly code with there are no frame pointers
  11107. +#
  11108. +# Revision 1.2 2005/08/12 14:13:58 mcr
  11109. +# do not use assembly code with there are no frame pointers,
  11110. +# as it does not have the right linkages.
  11111. +#
  11112. +# Revision 1.1 2004/08/17 03:31:34 mcr
  11113. +# klips 2.6 edits.
  11114. +#
  11115. +#
  11116. +# Local Variables:
  11117. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  11118. +# End Variables:
  11119. +#
  11120. +
  11121. --- /dev/null Tue Mar 11 13:02:56 2003
  11122. +++ linux/net/ipsec/aes/aes-i586.S Mon Feb 9 13:51:03 2004
  11123. @@ -0,0 +1,892 @@
  11124. +//
  11125. +// Copyright (c) 2001, Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK.
  11126. +// All rights reserved.
  11127. +//
  11128. +// TERMS
  11129. +//
  11130. +// Redistribution and use in source and binary forms, with or without
  11131. +// modification, are permitted subject to the following conditions:
  11132. +//
  11133. +// 1. Redistributions of source code must retain the above copyright
  11134. +// notice, this list of conditions and the following disclaimer.
  11135. +//
  11136. +// 2. Redistributions in binary form must reproduce the above copyright
  11137. +// notice, this list of conditions and the following disclaimer in the
  11138. +// documentation and/or other materials provided with the distribution.
  11139. +//
  11140. +// 3. The copyright holder's name must not be used to endorse or promote
  11141. +// any products derived from this software without his specific prior
  11142. +// written permission.
  11143. +//
  11144. +// This software is provided 'as is' with no express or implied warranties
  11145. +// of correctness or fitness for purpose.
  11146. +
  11147. +// Modified by Jari Ruusu, December 24 2001
  11148. +// - Converted syntax to GNU CPP/assembler syntax
  11149. +// - C programming interface converted back to "old" API
  11150. +// - Minor portability cleanups and speed optimizations
  11151. +
  11152. +// An AES (Rijndael) implementation for the Pentium. This version only
  11153. +// implements the standard AES block length (128 bits, 16 bytes). This code
  11154. +// does not preserve the eax, ecx or edx registers or the artihmetic status
  11155. +// flags. However, the ebx, esi, edi, and ebp registers are preserved across
  11156. +// calls.
  11157. +
  11158. +// void aes_set_key(aes_context *cx, const unsigned char key[], const int key_len, const int f)
  11159. +// void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  11160. +// void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  11161. +
  11162. +#if defined(USE_UNDERLINE)
  11163. +# define aes_set_key _aes_set_key
  11164. +# define aes_encrypt _aes_encrypt
  11165. +# define aes_decrypt _aes_decrypt
  11166. +#endif
  11167. +#if !defined(ALIGN32BYTES)
  11168. +# define ALIGN32BYTES 32
  11169. +#endif
  11170. +
  11171. + .file "aes-i586.S"
  11172. + .globl aes_set_key
  11173. + .globl aes_encrypt
  11174. + .globl aes_decrypt
  11175. +
  11176. +#define tlen 1024 // length of each of 4 'xor' arrays (256 32-bit words)
  11177. +
  11178. +// offsets to parameters with one register pushed onto stack
  11179. +
  11180. +#define ctx 8 // AES context structure
  11181. +#define in_blk 12 // input byte array address parameter
  11182. +#define out_blk 16 // output byte array address parameter
  11183. +
  11184. +// offsets in context structure
  11185. +
  11186. +#define nkey 0 // key length, size 4
  11187. +#define nrnd 4 // number of rounds, size 4
  11188. +#define ekey 8 // encryption key schedule base address, size 256
  11189. +#define dkey 264 // decryption key schedule base address, size 256
  11190. +
  11191. +// This macro performs a forward encryption cycle. It is entered with
  11192. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  11193. +// exits with the final values in the same registers.
  11194. +
  11195. +#define fwd_rnd(p1,p2) \
  11196. + mov %ebx,(%esp) ;\
  11197. + movzbl %al,%edx ;\
  11198. + mov %eax,%ecx ;\
  11199. + mov p2(%ebp),%eax ;\
  11200. + mov %edi,4(%esp) ;\
  11201. + mov p2+12(%ebp),%edi ;\
  11202. + xor p1(,%edx,4),%eax ;\
  11203. + movzbl %ch,%edx ;\
  11204. + shr $16,%ecx ;\
  11205. + mov p2+4(%ebp),%ebx ;\
  11206. + xor p1+tlen(,%edx,4),%edi ;\
  11207. + movzbl %cl,%edx ;\
  11208. + movzbl %ch,%ecx ;\
  11209. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  11210. + mov %esi,%ecx ;\
  11211. + mov p1+2*tlen(,%edx,4),%esi ;\
  11212. + movzbl %cl,%edx ;\
  11213. + xor p1(,%edx,4),%esi ;\
  11214. + movzbl %ch,%edx ;\
  11215. + shr $16,%ecx ;\
  11216. + xor p1+tlen(,%edx,4),%ebx ;\
  11217. + movzbl %cl,%edx ;\
  11218. + movzbl %ch,%ecx ;\
  11219. + xor p1+2*tlen(,%edx,4),%eax ;\
  11220. + mov (%esp),%edx ;\
  11221. + xor p1+3*tlen(,%ecx,4),%edi ;\
  11222. + movzbl %dl,%ecx ;\
  11223. + xor p2+8(%ebp),%esi ;\
  11224. + xor p1(,%ecx,4),%ebx ;\
  11225. + movzbl %dh,%ecx ;\
  11226. + shr $16,%edx ;\
  11227. + xor p1+tlen(,%ecx,4),%eax ;\
  11228. + movzbl %dl,%ecx ;\
  11229. + movzbl %dh,%edx ;\
  11230. + xor p1+2*tlen(,%ecx,4),%edi ;\
  11231. + mov 4(%esp),%ecx ;\
  11232. + xor p1+3*tlen(,%edx,4),%esi ;\
  11233. + movzbl %cl,%edx ;\
  11234. + xor p1(,%edx,4),%edi ;\
  11235. + movzbl %ch,%edx ;\
  11236. + shr $16,%ecx ;\
  11237. + xor p1+tlen(,%edx,4),%esi ;\
  11238. + movzbl %cl,%edx ;\
  11239. + movzbl %ch,%ecx ;\
  11240. + xor p1+2*tlen(,%edx,4),%ebx ;\
  11241. + xor p1+3*tlen(,%ecx,4),%eax
  11242. +
  11243. +// This macro performs an inverse encryption cycle. It is entered with
  11244. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  11245. +// exits with the final values in the same registers.
  11246. +
  11247. +#define inv_rnd(p1,p2) \
  11248. + movzbl %al,%edx ;\
  11249. + mov %ebx,(%esp) ;\
  11250. + mov %eax,%ecx ;\
  11251. + mov p2(%ebp),%eax ;\
  11252. + mov %edi,4(%esp) ;\
  11253. + mov p2+4(%ebp),%ebx ;\
  11254. + xor p1(,%edx,4),%eax ;\
  11255. + movzbl %ch,%edx ;\
  11256. + shr $16,%ecx ;\
  11257. + mov p2+12(%ebp),%edi ;\
  11258. + xor p1+tlen(,%edx,4),%ebx ;\
  11259. + movzbl %cl,%edx ;\
  11260. + movzbl %ch,%ecx ;\
  11261. + xor p1+3*tlen(,%ecx,4),%edi ;\
  11262. + mov %esi,%ecx ;\
  11263. + mov p1+2*tlen(,%edx,4),%esi ;\
  11264. + movzbl %cl,%edx ;\
  11265. + xor p1(,%edx,4),%esi ;\
  11266. + movzbl %ch,%edx ;\
  11267. + shr $16,%ecx ;\
  11268. + xor p1+tlen(,%edx,4),%edi ;\
  11269. + movzbl %cl,%edx ;\
  11270. + movzbl %ch,%ecx ;\
  11271. + xor p1+2*tlen(,%edx,4),%eax ;\
  11272. + mov (%esp),%edx ;\
  11273. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  11274. + movzbl %dl,%ecx ;\
  11275. + xor p2+8(%ebp),%esi ;\
  11276. + xor p1(,%ecx,4),%ebx ;\
  11277. + movzbl %dh,%ecx ;\
  11278. + shr $16,%edx ;\
  11279. + xor p1+tlen(,%ecx,4),%esi ;\
  11280. + movzbl %dl,%ecx ;\
  11281. + movzbl %dh,%edx ;\
  11282. + xor p1+2*tlen(,%ecx,4),%edi ;\
  11283. + mov 4(%esp),%ecx ;\
  11284. + xor p1+3*tlen(,%edx,4),%eax ;\
  11285. + movzbl %cl,%edx ;\
  11286. + xor p1(,%edx,4),%edi ;\
  11287. + movzbl %ch,%edx ;\
  11288. + shr $16,%ecx ;\
  11289. + xor p1+tlen(,%edx,4),%eax ;\
  11290. + movzbl %cl,%edx ;\
  11291. + movzbl %ch,%ecx ;\
  11292. + xor p1+2*tlen(,%edx,4),%ebx ;\
  11293. + xor p1+3*tlen(,%ecx,4),%esi
  11294. +
  11295. +// AES (Rijndael) Encryption Subroutine
  11296. +
  11297. + .text
  11298. + .align ALIGN32BYTES
  11299. +aes_encrypt:
  11300. + push %ebp
  11301. + mov ctx(%esp),%ebp // pointer to context
  11302. + mov in_blk(%esp),%ecx
  11303. + push %ebx
  11304. + push %esi
  11305. + push %edi
  11306. + mov nrnd(%ebp),%edx // number of rounds
  11307. + lea ekey+16(%ebp),%ebp // key pointer
  11308. +
  11309. +// input four columns and xor in first round key
  11310. +
  11311. + mov (%ecx),%eax
  11312. + mov 4(%ecx),%ebx
  11313. + mov 8(%ecx),%esi
  11314. + mov 12(%ecx),%edi
  11315. + xor -16(%ebp),%eax
  11316. + xor -12(%ebp),%ebx
  11317. + xor -8(%ebp),%esi
  11318. + xor -4(%ebp),%edi
  11319. +
  11320. + sub $8,%esp // space for register saves on stack
  11321. +
  11322. + sub $10,%edx
  11323. + je aes_15
  11324. + add $32,%ebp
  11325. + sub $2,%edx
  11326. + je aes_13
  11327. + add $32,%ebp
  11328. +
  11329. + fwd_rnd(aes_ft_tab,-64) // 14 rounds for 256-bit key
  11330. + fwd_rnd(aes_ft_tab,-48)
  11331. +aes_13: fwd_rnd(aes_ft_tab,-32) // 12 rounds for 192-bit key
  11332. + fwd_rnd(aes_ft_tab,-16)
  11333. +aes_15: fwd_rnd(aes_ft_tab,0) // 10 rounds for 128-bit key
  11334. + fwd_rnd(aes_ft_tab,16)
  11335. + fwd_rnd(aes_ft_tab,32)
  11336. + fwd_rnd(aes_ft_tab,48)
  11337. + fwd_rnd(aes_ft_tab,64)
  11338. + fwd_rnd(aes_ft_tab,80)
  11339. + fwd_rnd(aes_ft_tab,96)
  11340. + fwd_rnd(aes_ft_tab,112)
  11341. + fwd_rnd(aes_ft_tab,128)
  11342. + fwd_rnd(aes_fl_tab,144) // last round uses a different table
  11343. +
  11344. +// move final values to the output array.
  11345. +
  11346. + mov out_blk+20(%esp),%ebp
  11347. + add $8,%esp
  11348. + mov %eax,(%ebp)
  11349. + mov %ebx,4(%ebp)
  11350. + mov %esi,8(%ebp)
  11351. + mov %edi,12(%ebp)
  11352. + pop %edi
  11353. + pop %esi
  11354. + pop %ebx
  11355. + pop %ebp
  11356. + ret
  11357. +
  11358. +
  11359. +// AES (Rijndael) Decryption Subroutine
  11360. +
  11361. + .align ALIGN32BYTES
  11362. +aes_decrypt:
  11363. + push %ebp
  11364. + mov ctx(%esp),%ebp // pointer to context
  11365. + mov in_blk(%esp),%ecx
  11366. + push %ebx
  11367. + push %esi
  11368. + push %edi
  11369. + mov nrnd(%ebp),%edx // number of rounds
  11370. + lea dkey+16(%ebp),%ebp // key pointer
  11371. +
  11372. +// input four columns and xor in first round key
  11373. +
  11374. + mov (%ecx),%eax
  11375. + mov 4(%ecx),%ebx
  11376. + mov 8(%ecx),%esi
  11377. + mov 12(%ecx),%edi
  11378. + xor -16(%ebp),%eax
  11379. + xor -12(%ebp),%ebx
  11380. + xor -8(%ebp),%esi
  11381. + xor -4(%ebp),%edi
  11382. +
  11383. + sub $8,%esp // space for register saves on stack
  11384. +
  11385. + sub $10,%edx
  11386. + je aes_25
  11387. + add $32,%ebp
  11388. + sub $2,%edx
  11389. + je aes_23
  11390. + add $32,%ebp
  11391. +
  11392. + inv_rnd(aes_it_tab,-64) // 14 rounds for 256-bit key
  11393. + inv_rnd(aes_it_tab,-48)
  11394. +aes_23: inv_rnd(aes_it_tab,-32) // 12 rounds for 192-bit key
  11395. + inv_rnd(aes_it_tab,-16)
  11396. +aes_25: inv_rnd(aes_it_tab,0) // 10 rounds for 128-bit key
  11397. + inv_rnd(aes_it_tab,16)
  11398. + inv_rnd(aes_it_tab,32)
  11399. + inv_rnd(aes_it_tab,48)
  11400. + inv_rnd(aes_it_tab,64)
  11401. + inv_rnd(aes_it_tab,80)
  11402. + inv_rnd(aes_it_tab,96)
  11403. + inv_rnd(aes_it_tab,112)
  11404. + inv_rnd(aes_it_tab,128)
  11405. + inv_rnd(aes_il_tab,144) // last round uses a different table
  11406. +
  11407. +// move final values to the output array.
  11408. +
  11409. + mov out_blk+20(%esp),%ebp
  11410. + add $8,%esp
  11411. + mov %eax,(%ebp)
  11412. + mov %ebx,4(%ebp)
  11413. + mov %esi,8(%ebp)
  11414. + mov %edi,12(%ebp)
  11415. + pop %edi
  11416. + pop %esi
  11417. + pop %ebx
  11418. + pop %ebp
  11419. + ret
  11420. +
  11421. +// AES (Rijndael) Key Schedule Subroutine
  11422. +
  11423. +// input/output parameters
  11424. +
  11425. +#define aes_cx 12 // AES context
  11426. +#define in_key 16 // key input array address
  11427. +#define key_ln 20 // key length, bytes (16,24,32) or bits (128,192,256)
  11428. +#define ed_flg 24 // 0=create both encr/decr keys, 1=create encr key only
  11429. +
  11430. +// offsets for locals
  11431. +
  11432. +#define cnt -4
  11433. +#define kpf -8
  11434. +#define slen 8
  11435. +
  11436. +// This macro performs a column mixing operation on an input 32-bit
  11437. +// word to give a 32-bit result. It uses each of the 4 bytes in the
  11438. +// the input column to index 4 different tables of 256 32-bit words
  11439. +// that are xored together to form the output value.
  11440. +
  11441. +#define mix_col(p1) \
  11442. + movzbl %bl,%ecx ;\
  11443. + mov p1(,%ecx,4),%eax ;\
  11444. + movzbl %bh,%ecx ;\
  11445. + ror $16,%ebx ;\
  11446. + xor p1+tlen(,%ecx,4),%eax ;\
  11447. + movzbl %bl,%ecx ;\
  11448. + xor p1+2*tlen(,%ecx,4),%eax ;\
  11449. + movzbl %bh,%ecx ;\
  11450. + xor p1+3*tlen(,%ecx,4),%eax
  11451. +
  11452. +// Key Schedule Macros
  11453. +
  11454. +#define ksc4(p1) \
  11455. + rol $24,%ebx ;\
  11456. + mix_col(aes_fl_tab) ;\
  11457. + ror $8,%ebx ;\
  11458. + xor 4*p1+aes_rcon_tab,%eax ;\
  11459. + xor %eax,%esi ;\
  11460. + xor %esi,%ebp ;\
  11461. + mov %esi,16*p1(%edi) ;\
  11462. + mov %ebp,16*p1+4(%edi) ;\
  11463. + xor %ebp,%edx ;\
  11464. + xor %edx,%ebx ;\
  11465. + mov %edx,16*p1+8(%edi) ;\
  11466. + mov %ebx,16*p1+12(%edi)
  11467. +
  11468. +#define ksc6(p1) \
  11469. + rol $24,%ebx ;\
  11470. + mix_col(aes_fl_tab) ;\
  11471. + ror $8,%ebx ;\
  11472. + xor 4*p1+aes_rcon_tab,%eax ;\
  11473. + xor 24*p1-24(%edi),%eax ;\
  11474. + mov %eax,24*p1(%edi) ;\
  11475. + xor 24*p1-20(%edi),%eax ;\
  11476. + mov %eax,24*p1+4(%edi) ;\
  11477. + xor %eax,%esi ;\
  11478. + xor %esi,%ebp ;\
  11479. + mov %esi,24*p1+8(%edi) ;\
  11480. + mov %ebp,24*p1+12(%edi) ;\
  11481. + xor %ebp,%edx ;\
  11482. + xor %edx,%ebx ;\
  11483. + mov %edx,24*p1+16(%edi) ;\
  11484. + mov %ebx,24*p1+20(%edi)
  11485. +
  11486. +#define ksc8(p1) \
  11487. + rol $24,%ebx ;\
  11488. + mix_col(aes_fl_tab) ;\
  11489. + ror $8,%ebx ;\
  11490. + xor 4*p1+aes_rcon_tab,%eax ;\
  11491. + xor 32*p1-32(%edi),%eax ;\
  11492. + mov %eax,32*p1(%edi) ;\
  11493. + xor 32*p1-28(%edi),%eax ;\
  11494. + mov %eax,32*p1+4(%edi) ;\
  11495. + xor 32*p1-24(%edi),%eax ;\
  11496. + mov %eax,32*p1+8(%edi) ;\
  11497. + xor 32*p1-20(%edi),%eax ;\
  11498. + mov %eax,32*p1+12(%edi) ;\
  11499. + push %ebx ;\
  11500. + mov %eax,%ebx ;\
  11501. + mix_col(aes_fl_tab) ;\
  11502. + pop %ebx ;\
  11503. + xor %eax,%esi ;\
  11504. + xor %esi,%ebp ;\
  11505. + mov %esi,32*p1+16(%edi) ;\
  11506. + mov %ebp,32*p1+20(%edi) ;\
  11507. + xor %ebp,%edx ;\
  11508. + xor %edx,%ebx ;\
  11509. + mov %edx,32*p1+24(%edi) ;\
  11510. + mov %ebx,32*p1+28(%edi)
  11511. +
  11512. + .align ALIGN32BYTES
  11513. +aes_set_key:
  11514. + pushfl
  11515. + push %ebp
  11516. + mov %esp,%ebp
  11517. + sub $slen,%esp
  11518. + push %ebx
  11519. + push %esi
  11520. + push %edi
  11521. +
  11522. + mov aes_cx(%ebp),%edx // edx -> AES context
  11523. +
  11524. + mov key_ln(%ebp),%ecx // key length
  11525. + cmpl $128,%ecx
  11526. + jb aes_30
  11527. + shr $3,%ecx
  11528. +aes_30: cmpl $32,%ecx
  11529. + je aes_32
  11530. + cmpl $24,%ecx
  11531. + je aes_32
  11532. + mov $16,%ecx
  11533. +aes_32: shr $2,%ecx
  11534. + mov %ecx,nkey(%edx)
  11535. +
  11536. + lea 6(%ecx),%eax // 10/12/14 for 4/6/8 32-bit key length
  11537. + mov %eax,nrnd(%edx)
  11538. +
  11539. + mov in_key(%ebp),%esi // key input array
  11540. + lea ekey(%edx),%edi // key position in AES context
  11541. + cld
  11542. + push %ebp
  11543. + mov %ecx,%eax // save key length in eax
  11544. + rep ; movsl // words in the key schedule
  11545. + mov -4(%esi),%ebx // put some values in registers
  11546. + mov -8(%esi),%edx // to allow faster code
  11547. + mov -12(%esi),%ebp
  11548. + mov -16(%esi),%esi
  11549. +
  11550. + cmpl $4,%eax // jump on key size
  11551. + je aes_36
  11552. + cmpl $6,%eax
  11553. + je aes_35
  11554. +
  11555. + ksc8(0)
  11556. + ksc8(1)
  11557. + ksc8(2)
  11558. + ksc8(3)
  11559. + ksc8(4)
  11560. + ksc8(5)
  11561. + ksc8(6)
  11562. + jmp aes_37
  11563. +aes_35: ksc6(0)
  11564. + ksc6(1)
  11565. + ksc6(2)
  11566. + ksc6(3)
  11567. + ksc6(4)
  11568. + ksc6(5)
  11569. + ksc6(6)
  11570. + ksc6(7)
  11571. + jmp aes_37
  11572. +aes_36: ksc4(0)
  11573. + ksc4(1)
  11574. + ksc4(2)
  11575. + ksc4(3)
  11576. + ksc4(4)
  11577. + ksc4(5)
  11578. + ksc4(6)
  11579. + ksc4(7)
  11580. + ksc4(8)
  11581. + ksc4(9)
  11582. +aes_37: pop %ebp
  11583. + mov aes_cx(%ebp),%edx // edx -> AES context
  11584. + cmpl $0,ed_flg(%ebp)
  11585. + jne aes_39
  11586. +
  11587. +// compile decryption key schedule from encryption schedule - reverse
  11588. +// order and do mix_column operation on round keys except first and last
  11589. +
  11590. + mov nrnd(%edx),%eax // kt = cx->d_key + nc * cx->Nrnd
  11591. + shl $2,%eax
  11592. + lea dkey(%edx,%eax,4),%edi
  11593. + lea ekey(%edx),%esi // kf = cx->e_key
  11594. +
  11595. + movsl // copy first round key (unmodified)
  11596. + movsl
  11597. + movsl
  11598. + movsl
  11599. + sub $32,%edi
  11600. + movl $1,cnt(%ebp)
  11601. +aes_38: // do mix column on each column of
  11602. + lodsl // each round key
  11603. + mov %eax,%ebx
  11604. + mix_col(aes_im_tab)
  11605. + stosl
  11606. + lodsl
  11607. + mov %eax,%ebx
  11608. + mix_col(aes_im_tab)
  11609. + stosl
  11610. + lodsl
  11611. + mov %eax,%ebx
  11612. + mix_col(aes_im_tab)
  11613. + stosl
  11614. + lodsl
  11615. + mov %eax,%ebx
  11616. + mix_col(aes_im_tab)
  11617. + stosl
  11618. + sub $32,%edi
  11619. +
  11620. + incl cnt(%ebp)
  11621. + mov cnt(%ebp),%eax
  11622. + cmp nrnd(%edx),%eax
  11623. + jb aes_38
  11624. +
  11625. + movsl // copy last round key (unmodified)
  11626. + movsl
  11627. + movsl
  11628. + movsl
  11629. +aes_39: pop %edi
  11630. + pop %esi
  11631. + pop %ebx
  11632. + mov %ebp,%esp
  11633. + pop %ebp
  11634. + popfl
  11635. + ret
  11636. +
  11637. +
  11638. +// finite field multiplies by {02}, {04} and {08}
  11639. +
  11640. +#define f2(x) ((x<<1)^(((x>>7)&1)*0x11b))
  11641. +#define f4(x) ((x<<2)^(((x>>6)&1)*0x11b)^(((x>>6)&2)*0x11b))
  11642. +#define f8(x) ((x<<3)^(((x>>5)&1)*0x11b)^(((x>>5)&2)*0x11b)^(((x>>5)&4)*0x11b))
  11643. +
  11644. +// finite field multiplies required in table generation
  11645. +
  11646. +#define f3(x) (f2(x) ^ x)
  11647. +#define f9(x) (f8(x) ^ x)
  11648. +#define fb(x) (f8(x) ^ f2(x) ^ x)
  11649. +#define fd(x) (f8(x) ^ f4(x) ^ x)
  11650. +#define fe(x) (f8(x) ^ f4(x) ^ f2(x))
  11651. +
  11652. +// These defines generate the forward table entries
  11653. +
  11654. +#define u0(x) ((f3(x) << 24) | (x << 16) | (x << 8) | f2(x))
  11655. +#define u1(x) ((x << 24) | (x << 16) | (f2(x) << 8) | f3(x))
  11656. +#define u2(x) ((x << 24) | (f2(x) << 16) | (f3(x) << 8) | x)
  11657. +#define u3(x) ((f2(x) << 24) | (f3(x) << 16) | (x << 8) | x)
  11658. +
  11659. +// These defines generate the inverse table entries
  11660. +
  11661. +#define v0(x) ((fb(x) << 24) | (fd(x) << 16) | (f9(x) << 8) | fe(x))
  11662. +#define v1(x) ((fd(x) << 24) | (f9(x) << 16) | (fe(x) << 8) | fb(x))
  11663. +#define v2(x) ((f9(x) << 24) | (fe(x) << 16) | (fb(x) << 8) | fd(x))
  11664. +#define v3(x) ((fe(x) << 24) | (fb(x) << 16) | (fd(x) << 8) | f9(x))
  11665. +
  11666. +// These defines generate entries for the last round tables
  11667. +
  11668. +#define w0(x) (x)
  11669. +#define w1(x) (x << 8)
  11670. +#define w2(x) (x << 16)
  11671. +#define w3(x) (x << 24)
  11672. +
  11673. +// macro to generate inverse mix column tables (needed for the key schedule)
  11674. +
  11675. +#define im_data0(p1) \
  11676. + .long p1(0x00),p1(0x01),p1(0x02),p1(0x03),p1(0x04),p1(0x05),p1(0x06),p1(0x07) ;\
  11677. + .long p1(0x08),p1(0x09),p1(0x0a),p1(0x0b),p1(0x0c),p1(0x0d),p1(0x0e),p1(0x0f) ;\
  11678. + .long p1(0x10),p1(0x11),p1(0x12),p1(0x13),p1(0x14),p1(0x15),p1(0x16),p1(0x17) ;\
  11679. + .long p1(0x18),p1(0x19),p1(0x1a),p1(0x1b),p1(0x1c),p1(0x1d),p1(0x1e),p1(0x1f)
  11680. +#define im_data1(p1) \
  11681. + .long p1(0x20),p1(0x21),p1(0x22),p1(0x23),p1(0x24),p1(0x25),p1(0x26),p1(0x27) ;\
  11682. + .long p1(0x28),p1(0x29),p1(0x2a),p1(0x2b),p1(0x2c),p1(0x2d),p1(0x2e),p1(0x2f) ;\
  11683. + .long p1(0x30),p1(0x31),p1(0x32),p1(0x33),p1(0x34),p1(0x35),p1(0x36),p1(0x37) ;\
  11684. + .long p1(0x38),p1(0x39),p1(0x3a),p1(0x3b),p1(0x3c),p1(0x3d),p1(0x3e),p1(0x3f)
  11685. +#define im_data2(p1) \
  11686. + .long p1(0x40),p1(0x41),p1(0x42),p1(0x43),p1(0x44),p1(0x45),p1(0x46),p1(0x47) ;\
  11687. + .long p1(0x48),p1(0x49),p1(0x4a),p1(0x4b),p1(0x4c),p1(0x4d),p1(0x4e),p1(0x4f) ;\
  11688. + .long p1(0x50),p1(0x51),p1(0x52),p1(0x53),p1(0x54),p1(0x55),p1(0x56),p1(0x57) ;\
  11689. + .long p1(0x58),p1(0x59),p1(0x5a),p1(0x5b),p1(0x5c),p1(0x5d),p1(0x5e),p1(0x5f)
  11690. +#define im_data3(p1) \
  11691. + .long p1(0x60),p1(0x61),p1(0x62),p1(0x63),p1(0x64),p1(0x65),p1(0x66),p1(0x67) ;\
  11692. + .long p1(0x68),p1(0x69),p1(0x6a),p1(0x6b),p1(0x6c),p1(0x6d),p1(0x6e),p1(0x6f) ;\
  11693. + .long p1(0x70),p1(0x71),p1(0x72),p1(0x73),p1(0x74),p1(0x75),p1(0x76),p1(0x77) ;\
  11694. + .long p1(0x78),p1(0x79),p1(0x7a),p1(0x7b),p1(0x7c),p1(0x7d),p1(0x7e),p1(0x7f)
  11695. +#define im_data4(p1) \
  11696. + .long p1(0x80),p1(0x81),p1(0x82),p1(0x83),p1(0x84),p1(0x85),p1(0x86),p1(0x87) ;\
  11697. + .long p1(0x88),p1(0x89),p1(0x8a),p1(0x8b),p1(0x8c),p1(0x8d),p1(0x8e),p1(0x8f) ;\
  11698. + .long p1(0x90),p1(0x91),p1(0x92),p1(0x93),p1(0x94),p1(0x95),p1(0x96),p1(0x97) ;\
  11699. + .long p1(0x98),p1(0x99),p1(0x9a),p1(0x9b),p1(0x9c),p1(0x9d),p1(0x9e),p1(0x9f)
  11700. +#define im_data5(p1) \
  11701. + .long p1(0xa0),p1(0xa1),p1(0xa2),p1(0xa3),p1(0xa4),p1(0xa5),p1(0xa6),p1(0xa7) ;\
  11702. + .long p1(0xa8),p1(0xa9),p1(0xaa),p1(0xab),p1(0xac),p1(0xad),p1(0xae),p1(0xaf) ;\
  11703. + .long p1(0xb0),p1(0xb1),p1(0xb2),p1(0xb3),p1(0xb4),p1(0xb5),p1(0xb6),p1(0xb7) ;\
  11704. + .long p1(0xb8),p1(0xb9),p1(0xba),p1(0xbb),p1(0xbc),p1(0xbd),p1(0xbe),p1(0xbf)
  11705. +#define im_data6(p1) \
  11706. + .long p1(0xc0),p1(0xc1),p1(0xc2),p1(0xc3),p1(0xc4),p1(0xc5),p1(0xc6),p1(0xc7) ;\
  11707. + .long p1(0xc8),p1(0xc9),p1(0xca),p1(0xcb),p1(0xcc),p1(0xcd),p1(0xce),p1(0xcf) ;\
  11708. + .long p1(0xd0),p1(0xd1),p1(0xd2),p1(0xd3),p1(0xd4),p1(0xd5),p1(0xd6),p1(0xd7) ;\
  11709. + .long p1(0xd8),p1(0xd9),p1(0xda),p1(0xdb),p1(0xdc),p1(0xdd),p1(0xde),p1(0xdf)
  11710. +#define im_data7(p1) \
  11711. + .long p1(0xe0),p1(0xe1),p1(0xe2),p1(0xe3),p1(0xe4),p1(0xe5),p1(0xe6),p1(0xe7) ;\
  11712. + .long p1(0xe8),p1(0xe9),p1(0xea),p1(0xeb),p1(0xec),p1(0xed),p1(0xee),p1(0xef) ;\
  11713. + .long p1(0xf0),p1(0xf1),p1(0xf2),p1(0xf3),p1(0xf4),p1(0xf5),p1(0xf6),p1(0xf7) ;\
  11714. + .long p1(0xf8),p1(0xf9),p1(0xfa),p1(0xfb),p1(0xfc),p1(0xfd),p1(0xfe),p1(0xff)
  11715. +
  11716. +// S-box data - 256 entries
  11717. +
  11718. +#define sb_data0(p1) \
  11719. + .long p1(0x63),p1(0x7c),p1(0x77),p1(0x7b),p1(0xf2),p1(0x6b),p1(0x6f),p1(0xc5) ;\
  11720. + .long p1(0x30),p1(0x01),p1(0x67),p1(0x2b),p1(0xfe),p1(0xd7),p1(0xab),p1(0x76) ;\
  11721. + .long p1(0xca),p1(0x82),p1(0xc9),p1(0x7d),p1(0xfa),p1(0x59),p1(0x47),p1(0xf0) ;\
  11722. + .long p1(0xad),p1(0xd4),p1(0xa2),p1(0xaf),p1(0x9c),p1(0xa4),p1(0x72),p1(0xc0)
  11723. +#define sb_data1(p1) \
  11724. + .long p1(0xb7),p1(0xfd),p1(0x93),p1(0x26),p1(0x36),p1(0x3f),p1(0xf7),p1(0xcc) ;\
  11725. + .long p1(0x34),p1(0xa5),p1(0xe5),p1(0xf1),p1(0x71),p1(0xd8),p1(0x31),p1(0x15) ;\
  11726. + .long p1(0x04),p1(0xc7),p1(0x23),p1(0xc3),p1(0x18),p1(0x96),p1(0x05),p1(0x9a) ;\
  11727. + .long p1(0x07),p1(0x12),p1(0x80),p1(0xe2),p1(0xeb),p1(0x27),p1(0xb2),p1(0x75)
  11728. +#define sb_data2(p1) \
  11729. + .long p1(0x09),p1(0x83),p1(0x2c),p1(0x1a),p1(0x1b),p1(0x6e),p1(0x5a),p1(0xa0) ;\
  11730. + .long p1(0x52),p1(0x3b),p1(0xd6),p1(0xb3),p1(0x29),p1(0xe3),p1(0x2f),p1(0x84) ;\
  11731. + .long p1(0x53),p1(0xd1),p1(0x00),p1(0xed),p1(0x20),p1(0xfc),p1(0xb1),p1(0x5b) ;\
  11732. + .long p1(0x6a),p1(0xcb),p1(0xbe),p1(0x39),p1(0x4a),p1(0x4c),p1(0x58),p1(0xcf)
  11733. +#define sb_data3(p1) \
  11734. + .long p1(0xd0),p1(0xef),p1(0xaa),p1(0xfb),p1(0x43),p1(0x4d),p1(0x33),p1(0x85) ;\
  11735. + .long p1(0x45),p1(0xf9),p1(0x02),p1(0x7f),p1(0x50),p1(0x3c),p1(0x9f),p1(0xa8) ;\
  11736. + .long p1(0x51),p1(0xa3),p1(0x40),p1(0x8f),p1(0x92),p1(0x9d),p1(0x38),p1(0xf5) ;\
  11737. + .long p1(0xbc),p1(0xb6),p1(0xda),p1(0x21),p1(0x10),p1(0xff),p1(0xf3),p1(0xd2)
  11738. +#define sb_data4(p1) \
  11739. + .long p1(0xcd),p1(0x0c),p1(0x13),p1(0xec),p1(0x5f),p1(0x97),p1(0x44),p1(0x17) ;\
  11740. + .long p1(0xc4),p1(0xa7),p1(0x7e),p1(0x3d),p1(0x64),p1(0x5d),p1(0x19),p1(0x73) ;\
  11741. + .long p1(0x60),p1(0x81),p1(0x4f),p1(0xdc),p1(0x22),p1(0x2a),p1(0x90),p1(0x88) ;\
  11742. + .long p1(0x46),p1(0xee),p1(0xb8),p1(0x14),p1(0xde),p1(0x5e),p1(0x0b),p1(0xdb)
  11743. +#define sb_data5(p1) \
  11744. + .long p1(0xe0),p1(0x32),p1(0x3a),p1(0x0a),p1(0x49),p1(0x06),p1(0x24),p1(0x5c) ;\
  11745. + .long p1(0xc2),p1(0xd3),p1(0xac),p1(0x62),p1(0x91),p1(0x95),p1(0xe4),p1(0x79) ;\
  11746. + .long p1(0xe7),p1(0xc8),p1(0x37),p1(0x6d),p1(0x8d),p1(0xd5),p1(0x4e),p1(0xa9) ;\
  11747. + .long p1(0x6c),p1(0x56),p1(0xf4),p1(0xea),p1(0x65),p1(0x7a),p1(0xae),p1(0x08)
  11748. +#define sb_data6(p1) \
  11749. + .long p1(0xba),p1(0x78),p1(0x25),p1(0x2e),p1(0x1c),p1(0xa6),p1(0xb4),p1(0xc6) ;\
  11750. + .long p1(0xe8),p1(0xdd),p1(0x74),p1(0x1f),p1(0x4b),p1(0xbd),p1(0x8b),p1(0x8a) ;\
  11751. + .long p1(0x70),p1(0x3e),p1(0xb5),p1(0x66),p1(0x48),p1(0x03),p1(0xf6),p1(0x0e) ;\
  11752. + .long p1(0x61),p1(0x35),p1(0x57),p1(0xb9),p1(0x86),p1(0xc1),p1(0x1d),p1(0x9e)
  11753. +#define sb_data7(p1) \
  11754. + .long p1(0xe1),p1(0xf8),p1(0x98),p1(0x11),p1(0x69),p1(0xd9),p1(0x8e),p1(0x94) ;\
  11755. + .long p1(0x9b),p1(0x1e),p1(0x87),p1(0xe9),p1(0xce),p1(0x55),p1(0x28),p1(0xdf) ;\
  11756. + .long p1(0x8c),p1(0xa1),p1(0x89),p1(0x0d),p1(0xbf),p1(0xe6),p1(0x42),p1(0x68) ;\
  11757. + .long p1(0x41),p1(0x99),p1(0x2d),p1(0x0f),p1(0xb0),p1(0x54),p1(0xbb),p1(0x16)
  11758. +
  11759. +// Inverse S-box data - 256 entries
  11760. +
  11761. +#define ib_data0(p1) \
  11762. + .long p1(0x52),p1(0x09),p1(0x6a),p1(0xd5),p1(0x30),p1(0x36),p1(0xa5),p1(0x38) ;\
  11763. + .long p1(0xbf),p1(0x40),p1(0xa3),p1(0x9e),p1(0x81),p1(0xf3),p1(0xd7),p1(0xfb) ;\
  11764. + .long p1(0x7c),p1(0xe3),p1(0x39),p1(0x82),p1(0x9b),p1(0x2f),p1(0xff),p1(0x87) ;\
  11765. + .long p1(0x34),p1(0x8e),p1(0x43),p1(0x44),p1(0xc4),p1(0xde),p1(0xe9),p1(0xcb)
  11766. +#define ib_data1(p1) \
  11767. + .long p1(0x54),p1(0x7b),p1(0x94),p1(0x32),p1(0xa6),p1(0xc2),p1(0x23),p1(0x3d) ;\
  11768. + .long p1(0xee),p1(0x4c),p1(0x95),p1(0x0b),p1(0x42),p1(0xfa),p1(0xc3),p1(0x4e) ;\
  11769. + .long p1(0x08),p1(0x2e),p1(0xa1),p1(0x66),p1(0x28),p1(0xd9),p1(0x24),p1(0xb2) ;\
  11770. + .long p1(0x76),p1(0x5b),p1(0xa2),p1(0x49),p1(0x6d),p1(0x8b),p1(0xd1),p1(0x25)
  11771. +#define ib_data2(p1) \
  11772. + .long p1(0x72),p1(0xf8),p1(0xf6),p1(0x64),p1(0x86),p1(0x68),p1(0x98),p1(0x16) ;\
  11773. + .long p1(0xd4),p1(0xa4),p1(0x5c),p1(0xcc),p1(0x5d),p1(0x65),p1(0xb6),p1(0x92) ;\
  11774. + .long p1(0x6c),p1(0x70),p1(0x48),p1(0x50),p1(0xfd),p1(0xed),p1(0xb9),p1(0xda) ;\
  11775. + .long p1(0x5e),p1(0x15),p1(0x46),p1(0x57),p1(0xa7),p1(0x8d),p1(0x9d),p1(0x84)
  11776. +#define ib_data3(p1) \
  11777. + .long p1(0x90),p1(0xd8),p1(0xab),p1(0x00),p1(0x8c),p1(0xbc),p1(0xd3),p1(0x0a) ;\
  11778. + .long p1(0xf7),p1(0xe4),p1(0x58),p1(0x05),p1(0xb8),p1(0xb3),p1(0x45),p1(0x06) ;\
  11779. + .long p1(0xd0),p1(0x2c),p1(0x1e),p1(0x8f),p1(0xca),p1(0x3f),p1(0x0f),p1(0x02) ;\
  11780. + .long p1(0xc1),p1(0xaf),p1(0xbd),p1(0x03),p1(0x01),p1(0x13),p1(0x8a),p1(0x6b)
  11781. +#define ib_data4(p1) \
  11782. + .long p1(0x3a),p1(0x91),p1(0x11),p1(0x41),p1(0x4f),p1(0x67),p1(0xdc),p1(0xea) ;\
  11783. + .long p1(0x97),p1(0xf2),p1(0xcf),p1(0xce),p1(0xf0),p1(0xb4),p1(0xe6),p1(0x73) ;\
  11784. + .long p1(0x96),p1(0xac),p1(0x74),p1(0x22),p1(0xe7),p1(0xad),p1(0x35),p1(0x85) ;\
  11785. + .long p1(0xe2),p1(0xf9),p1(0x37),p1(0xe8),p1(0x1c),p1(0x75),p1(0xdf),p1(0x6e)
  11786. +#define ib_data5(p1) \
  11787. + .long p1(0x47),p1(0xf1),p1(0x1a),p1(0x71),p1(0x1d),p1(0x29),p1(0xc5),p1(0x89) ;\
  11788. + .long p1(0x6f),p1(0xb7),p1(0x62),p1(0x0e),p1(0xaa),p1(0x18),p1(0xbe),p1(0x1b) ;\
  11789. + .long p1(0xfc),p1(0x56),p1(0x3e),p1(0x4b),p1(0xc6),p1(0xd2),p1(0x79),p1(0x20) ;\
  11790. + .long p1(0x9a),p1(0xdb),p1(0xc0),p1(0xfe),p1(0x78),p1(0xcd),p1(0x5a),p1(0xf4)
  11791. +#define ib_data6(p1) \
  11792. + .long p1(0x1f),p1(0xdd),p1(0xa8),p1(0x33),p1(0x88),p1(0x07),p1(0xc7),p1(0x31) ;\
  11793. + .long p1(0xb1),p1(0x12),p1(0x10),p1(0x59),p1(0x27),p1(0x80),p1(0xec),p1(0x5f) ;\
  11794. + .long p1(0x60),p1(0x51),p1(0x7f),p1(0xa9),p1(0x19),p1(0xb5),p1(0x4a),p1(0x0d) ;\
  11795. + .long p1(0x2d),p1(0xe5),p1(0x7a),p1(0x9f),p1(0x93),p1(0xc9),p1(0x9c),p1(0xef)
  11796. +#define ib_data7(p1) \
  11797. + .long p1(0xa0),p1(0xe0),p1(0x3b),p1(0x4d),p1(0xae),p1(0x2a),p1(0xf5),p1(0xb0) ;\
  11798. + .long p1(0xc8),p1(0xeb),p1(0xbb),p1(0x3c),p1(0x83),p1(0x53),p1(0x99),p1(0x61) ;\
  11799. + .long p1(0x17),p1(0x2b),p1(0x04),p1(0x7e),p1(0xba),p1(0x77),p1(0xd6),p1(0x26) ;\
  11800. + .long p1(0xe1),p1(0x69),p1(0x14),p1(0x63),p1(0x55),p1(0x21),p1(0x0c),p1(0x7d)
  11801. +
  11802. +// The rcon_table (needed for the key schedule)
  11803. +//
  11804. +// Here is original Dr Brian Gladman's source code:
  11805. +// _rcon_tab:
  11806. +// %assign x 1
  11807. +// %rep 29
  11808. +// dd x
  11809. +// %assign x f2(x)
  11810. +// %endrep
  11811. +//
  11812. +// Here is precomputed output (it's more portable this way):
  11813. +
  11814. + .align ALIGN32BYTES
  11815. +aes_rcon_tab:
  11816. + .long 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
  11817. + .long 0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f
  11818. + .long 0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4
  11819. + .long 0xb3,0x7d,0xfa,0xef,0xc5
  11820. +
  11821. +// The forward xor tables
  11822. +
  11823. + .align ALIGN32BYTES
  11824. +aes_ft_tab:
  11825. + sb_data0(u0)
  11826. + sb_data1(u0)
  11827. + sb_data2(u0)
  11828. + sb_data3(u0)
  11829. + sb_data4(u0)
  11830. + sb_data5(u0)
  11831. + sb_data6(u0)
  11832. + sb_data7(u0)
  11833. +
  11834. + sb_data0(u1)
  11835. + sb_data1(u1)
  11836. + sb_data2(u1)
  11837. + sb_data3(u1)
  11838. + sb_data4(u1)
  11839. + sb_data5(u1)
  11840. + sb_data6(u1)
  11841. + sb_data7(u1)
  11842. +
  11843. + sb_data0(u2)
  11844. + sb_data1(u2)
  11845. + sb_data2(u2)
  11846. + sb_data3(u2)
  11847. + sb_data4(u2)
  11848. + sb_data5(u2)
  11849. + sb_data6(u2)
  11850. + sb_data7(u2)
  11851. +
  11852. + sb_data0(u3)
  11853. + sb_data1(u3)
  11854. + sb_data2(u3)
  11855. + sb_data3(u3)
  11856. + sb_data4(u3)
  11857. + sb_data5(u3)
  11858. + sb_data6(u3)
  11859. + sb_data7(u3)
  11860. +
  11861. + .align ALIGN32BYTES
  11862. +aes_fl_tab:
  11863. + sb_data0(w0)
  11864. + sb_data1(w0)
  11865. + sb_data2(w0)
  11866. + sb_data3(w0)
  11867. + sb_data4(w0)
  11868. + sb_data5(w0)
  11869. + sb_data6(w0)
  11870. + sb_data7(w0)
  11871. +
  11872. + sb_data0(w1)
  11873. + sb_data1(w1)
  11874. + sb_data2(w1)
  11875. + sb_data3(w1)
  11876. + sb_data4(w1)
  11877. + sb_data5(w1)
  11878. + sb_data6(w1)
  11879. + sb_data7(w1)
  11880. +
  11881. + sb_data0(w2)
  11882. + sb_data1(w2)
  11883. + sb_data2(w2)
  11884. + sb_data3(w2)
  11885. + sb_data4(w2)
  11886. + sb_data5(w2)
  11887. + sb_data6(w2)
  11888. + sb_data7(w2)
  11889. +
  11890. + sb_data0(w3)
  11891. + sb_data1(w3)
  11892. + sb_data2(w3)
  11893. + sb_data3(w3)
  11894. + sb_data4(w3)
  11895. + sb_data5(w3)
  11896. + sb_data6(w3)
  11897. + sb_data7(w3)
  11898. +
  11899. +// The inverse xor tables
  11900. +
  11901. + .align ALIGN32BYTES
  11902. +aes_it_tab:
  11903. + ib_data0(v0)
  11904. + ib_data1(v0)
  11905. + ib_data2(v0)
  11906. + ib_data3(v0)
  11907. + ib_data4(v0)
  11908. + ib_data5(v0)
  11909. + ib_data6(v0)
  11910. + ib_data7(v0)
  11911. +
  11912. + ib_data0(v1)
  11913. + ib_data1(v1)
  11914. + ib_data2(v1)
  11915. + ib_data3(v1)
  11916. + ib_data4(v1)
  11917. + ib_data5(v1)
  11918. + ib_data6(v1)
  11919. + ib_data7(v1)
  11920. +
  11921. + ib_data0(v2)
  11922. + ib_data1(v2)
  11923. + ib_data2(v2)
  11924. + ib_data3(v2)
  11925. + ib_data4(v2)
  11926. + ib_data5(v2)
  11927. + ib_data6(v2)
  11928. + ib_data7(v2)
  11929. +
  11930. + ib_data0(v3)
  11931. + ib_data1(v3)
  11932. + ib_data2(v3)
  11933. + ib_data3(v3)
  11934. + ib_data4(v3)
  11935. + ib_data5(v3)
  11936. + ib_data6(v3)
  11937. + ib_data7(v3)
  11938. +
  11939. + .align ALIGN32BYTES
  11940. +aes_il_tab:
  11941. + ib_data0(w0)
  11942. + ib_data1(w0)
  11943. + ib_data2(w0)
  11944. + ib_data3(w0)
  11945. + ib_data4(w0)
  11946. + ib_data5(w0)
  11947. + ib_data6(w0)
  11948. + ib_data7(w0)
  11949. +
  11950. + ib_data0(w1)
  11951. + ib_data1(w1)
  11952. + ib_data2(w1)
  11953. + ib_data3(w1)
  11954. + ib_data4(w1)
  11955. + ib_data5(w1)
  11956. + ib_data6(w1)
  11957. + ib_data7(w1)
  11958. +
  11959. + ib_data0(w2)
  11960. + ib_data1(w2)
  11961. + ib_data2(w2)
  11962. + ib_data3(w2)
  11963. + ib_data4(w2)
  11964. + ib_data5(w2)
  11965. + ib_data6(w2)
  11966. + ib_data7(w2)
  11967. +
  11968. + ib_data0(w3)
  11969. + ib_data1(w3)
  11970. + ib_data2(w3)
  11971. + ib_data3(w3)
  11972. + ib_data4(w3)
  11973. + ib_data5(w3)
  11974. + ib_data6(w3)
  11975. + ib_data7(w3)
  11976. +
  11977. +// The inverse mix column tables
  11978. +
  11979. + .align ALIGN32BYTES
  11980. +aes_im_tab:
  11981. + im_data0(v0)
  11982. + im_data1(v0)
  11983. + im_data2(v0)
  11984. + im_data3(v0)
  11985. + im_data4(v0)
  11986. + im_data5(v0)
  11987. + im_data6(v0)
  11988. + im_data7(v0)
  11989. +
  11990. + im_data0(v1)
  11991. + im_data1(v1)
  11992. + im_data2(v1)
  11993. + im_data3(v1)
  11994. + im_data4(v1)
  11995. + im_data5(v1)
  11996. + im_data6(v1)
  11997. + im_data7(v1)
  11998. +
  11999. + im_data0(v2)
  12000. + im_data1(v2)
  12001. + im_data2(v2)
  12002. + im_data3(v2)
  12003. + im_data4(v2)
  12004. + im_data5(v2)
  12005. + im_data6(v2)
  12006. + im_data7(v2)
  12007. +
  12008. + im_data0(v3)
  12009. + im_data1(v3)
  12010. + im_data2(v3)
  12011. + im_data3(v3)
  12012. + im_data4(v3)
  12013. + im_data5(v3)
  12014. + im_data6(v3)
  12015. + im_data7(v3)
  12016. --- /dev/null Tue Mar 11 13:02:56 2003
  12017. +++ linux/net/ipsec/aes/aes.c Mon Feb 9 13:51:03 2004
  12018. @@ -0,0 +1,1415 @@
  12019. +// I retain copyright in this code but I encourage its free use provided
  12020. +// that I don't carry any responsibility for the results. I am especially
  12021. +// happy to see it used in free and open source software. If you do use
  12022. +// it I would appreciate an acknowledgement of its origin in the code or
  12023. +// the product that results and I would also appreciate knowing a little
  12024. +// about the use to which it is being put. I am grateful to Frank Yellin
  12025. +// for some ideas that are used in this implementation.
  12026. +//
  12027. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  12028. +//
  12029. +// This is an implementation of the AES encryption algorithm (Rijndael)
  12030. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  12031. +// to provide both fixed and dynamic block and key lengths and can also
  12032. +// run with either big or little endian internal byte order (see aes.h).
  12033. +// It inputs block and key lengths in bytes with the legal values being
  12034. +// 16, 24 and 32.
  12035. +
  12036. +/*
  12037. + * Modified by Jari Ruusu, May 1 2001
  12038. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  12039. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  12040. + * - Major name space cleanup: Names visible to outside now begin
  12041. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  12042. + * - Removed C++ and DLL support as part of name space cleanup.
  12043. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  12044. + * - Merged precomputed constant tables to aes.c file.
  12045. + * - Removed data alignment restrictions for portability reasons.
  12046. + * - Made block and key lengths accept bit count (128/192/256)
  12047. + * as well byte count (16/24/32).
  12048. + * - Removed all error checks. This change also eliminated the need
  12049. + * to preinitialize the context struct to zero.
  12050. + * - Removed some totally unused constants.
  12051. + */
  12052. +
  12053. +#include "crypto/aes.h"
  12054. +
  12055. +// CONFIGURATION OPTIONS (see also aes.h)
  12056. +//
  12057. +// 1. Define UNROLL for full loop unrolling in encryption and decryption.
  12058. +// 2. Define PARTIAL_UNROLL to unroll two loops in encryption and decryption.
  12059. +// 3. Define FIXED_TABLES for compiled rather than dynamic tables.
  12060. +// 4. Define FF_TABLES to use tables for field multiplies and inverses.
  12061. +// Do not enable this without understanding stack space requirements.
  12062. +// 5. Define ARRAYS to use arrays to hold the local state block. If this
  12063. +// is not defined, individually declared 32-bit words are used.
  12064. +// 6. Define FAST_VARIABLE if a high speed variable block implementation
  12065. +// is needed (essentially three separate fixed block size code sequences)
  12066. +// 7. Define either ONE_TABLE or FOUR_TABLES for a fast table driven
  12067. +// version using 1 table (2 kbytes of table space) or 4 tables (8
  12068. +// kbytes of table space) for higher speed.
  12069. +// 8. Define either ONE_LR_TABLE or FOUR_LR_TABLES for a further speed
  12070. +// increase by using tables for the last rounds but with more table
  12071. +// space (2 or 8 kbytes extra).
  12072. +// 9. If neither ONE_TABLE nor FOUR_TABLES is defined, a compact but
  12073. +// slower version is provided.
  12074. +// 10. If fast decryption key scheduling is needed define ONE_IM_TABLE
  12075. +// or FOUR_IM_TABLES for higher speed (2 or 8 kbytes extra).
  12076. +
  12077. +#define UNROLL
  12078. +//#define PARTIAL_UNROLL
  12079. +
  12080. +#define FIXED_TABLES
  12081. +//#define FF_TABLES
  12082. +//#define ARRAYS
  12083. +#define FAST_VARIABLE
  12084. +
  12085. +//#define ONE_TABLE
  12086. +#define FOUR_TABLES
  12087. +
  12088. +//#define ONE_LR_TABLE
  12089. +#define FOUR_LR_TABLES
  12090. +
  12091. +//#define ONE_IM_TABLE
  12092. +#define FOUR_IM_TABLES
  12093. +
  12094. +#if defined(UNROLL) && defined (PARTIAL_UNROLL)
  12095. +#error both UNROLL and PARTIAL_UNROLL are defined
  12096. +#endif
  12097. +
  12098. +#if defined(ONE_TABLE) && defined (FOUR_TABLES)
  12099. +#error both ONE_TABLE and FOUR_TABLES are defined
  12100. +#endif
  12101. +
  12102. +#if defined(ONE_LR_TABLE) && defined (FOUR_LR_TABLES)
  12103. +#error both ONE_LR_TABLE and FOUR_LR_TABLES are defined
  12104. +#endif
  12105. +
  12106. +#if defined(ONE_IM_TABLE) && defined (FOUR_IM_TABLES)
  12107. +#error both ONE_IM_TABLE and FOUR_IM_TABLES are defined
  12108. +#endif
  12109. +
  12110. +#if defined(AES_BLOCK_SIZE) && AES_BLOCK_SIZE != 16 && AES_BLOCK_SIZE != 24 && AES_BLOCK_SIZE != 32
  12111. +#error an illegal block size has been specified
  12112. +#endif
  12113. +
  12114. +// upr(x,n): rotates bytes within words by n positions, moving bytes
  12115. +// to higher index positions with wrap around into low positions
  12116. +// ups(x,n): moves bytes by n positions to higher index positions in
  12117. +// words but without wrap around
  12118. +// bval(x,n): extracts a byte from a word
  12119. +
  12120. +#define upr(x,n) (((x) << 8 * (n)) | ((x) >> (32 - 8 * (n))))
  12121. +#define ups(x,n) ((x) << 8 * (n))
  12122. +#define bval(x,n) ((unsigned char)((x) >> 8 * (n)))
  12123. +#define bytes2word(b0, b1, b2, b3) \
  12124. + ((u_int32_t)(b3) << 24 | (u_int32_t)(b2) << 16 | (u_int32_t)(b1) << 8 | (b0))
  12125. +
  12126. +
  12127. +/* little endian processor without data alignment restrictions: AES_LE_OK */
  12128. +/* original code: i386 */
  12129. +#if defined(i386) || defined(_I386) || defined(__i386__) || defined(__i386)
  12130. +#define AES_LE_OK 1
  12131. +/* added (tested): alpha --jjo */
  12132. +#elif defined(__alpha__)|| defined (__alpha)
  12133. +#define AES_LE_OK 1
  12134. +/* added (tested): ia64 --jjo */
  12135. +#elif defined(__ia64__)|| defined (__ia64)
  12136. +#define AES_LE_OK 1
  12137. +#endif
  12138. +
  12139. +#ifdef AES_LE_OK
  12140. +/* little endian processor without data alignment restrictions */
  12141. +#define word_in(x) *(u_int32_t*)(x)
  12142. +#define const_word_in(x) *(const u_int32_t*)(x)
  12143. +#define word_out(x,v) *(u_int32_t*)(x) = (v)
  12144. +#define const_word_out(x,v) *(const u_int32_t*)(x) = (v)
  12145. +#else
  12146. +/* slower but generic big endian or with data alignment restrictions */
  12147. +/* some additional "const" touches to stop "gcc -Wcast-qual" complains --jjo */
  12148. +#define word_in(x) ((u_int32_t)(((unsigned char *)(x))[0])|((u_int32_t)(((unsigned char *)(x))[1])<<8)|((u_int32_t)(((unsigned char *)(x))[2])<<16)|((u_int32_t)(((unsigned char *)(x))[3])<<24))
  12149. +#define const_word_in(x) ((const u_int32_t)(((const unsigned char *)(x))[0])|((const u_int32_t)(((const unsigned char *)(x))[1])<<8)|((const u_int32_t)(((const unsigned char *)(x))[2])<<16)|((const u_int32_t)(((const unsigned char *)(x))[3])<<24))
  12150. +#define word_out(x,v) ((unsigned char *)(x))[0]=(v),((unsigned char *)(x))[1]=((v)>>8),((unsigned char *)(x))[2]=((v)>>16),((unsigned char *)(x))[3]=((v)>>24)
  12151. +#define const_word_out(x,v) ((const unsigned char *)(x))[0]=(v),((const unsigned char *)(x))[1]=((v)>>8),((const unsigned char *)(x))[2]=((v)>>16),((const unsigned char *)(x))[3]=((v)>>24)
  12152. +#endif
  12153. +
  12154. +// Disable at least some poor combinations of options
  12155. +
  12156. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  12157. +#define FIXED_TABLES
  12158. +#undef UNROLL
  12159. +#undef ONE_LR_TABLE
  12160. +#undef FOUR_LR_TABLES
  12161. +#undef ONE_IM_TABLE
  12162. +#undef FOUR_IM_TABLES
  12163. +#elif !defined(FOUR_TABLES)
  12164. +#ifdef FOUR_LR_TABLES
  12165. +#undef FOUR_LR_TABLES
  12166. +#define ONE_LR_TABLE
  12167. +#endif
  12168. +#ifdef FOUR_IM_TABLES
  12169. +#undef FOUR_IM_TABLES
  12170. +#define ONE_IM_TABLE
  12171. +#endif
  12172. +#elif !defined(AES_BLOCK_SIZE)
  12173. +#if defined(UNROLL)
  12174. +#define PARTIAL_UNROLL
  12175. +#undef UNROLL
  12176. +#endif
  12177. +#endif
  12178. +
  12179. +// the finite field modular polynomial and elements
  12180. +
  12181. +#define ff_poly 0x011b
  12182. +#define ff_hi 0x80
  12183. +
  12184. +// multiply four bytes in GF(2^8) by 'x' {02} in parallel
  12185. +
  12186. +#define m1 0x80808080
  12187. +#define m2 0x7f7f7f7f
  12188. +#define m3 0x0000001b
  12189. +#define FFmulX(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * m3))
  12190. +
  12191. +// The following defines provide alternative definitions of FFmulX that might
  12192. +// give improved performance if a fast 32-bit multiply is not available. Note
  12193. +// that a temporary variable u needs to be defined where FFmulX is used.
  12194. +
  12195. +// #define FFmulX(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
  12196. +// #define m4 0x1b1b1b1b
  12197. +// #define FFmulX(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
  12198. +
  12199. +// perform column mix operation on four bytes in parallel
  12200. +
  12201. +#define fwd_mcol(x) (f2 = FFmulX(x), f2 ^ upr(x ^ f2,3) ^ upr(x,2) ^ upr(x,1))
  12202. +
  12203. +#if defined(FIXED_TABLES)
  12204. +
  12205. +// the S-Box table
  12206. +
  12207. +static const unsigned char s_box[256] =
  12208. +{
  12209. + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  12210. + 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  12211. + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  12212. + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  12213. + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  12214. + 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  12215. + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  12216. + 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  12217. + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  12218. + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  12219. + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  12220. + 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  12221. + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  12222. + 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  12223. + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  12224. + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  12225. + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  12226. + 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  12227. + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  12228. + 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  12229. + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  12230. + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  12231. + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  12232. + 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  12233. + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  12234. + 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  12235. + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  12236. + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  12237. + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  12238. + 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  12239. + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  12240. + 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  12241. +};
  12242. +
  12243. +// the inverse S-Box table
  12244. +
  12245. +static const unsigned char inv_s_box[256] =
  12246. +{
  12247. + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
  12248. + 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  12249. + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
  12250. + 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  12251. + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d,
  12252. + 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  12253. + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2,
  12254. + 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  12255. + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
  12256. + 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  12257. + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda,
  12258. + 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  12259. + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
  12260. + 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  12261. + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
  12262. + 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  12263. + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea,
  12264. + 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  12265. + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85,
  12266. + 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  12267. + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
  12268. + 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  12269. + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20,
  12270. + 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  12271. + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31,
  12272. + 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  12273. + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
  12274. + 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  12275. + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0,
  12276. + 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  12277. + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26,
  12278. + 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  12279. +};
  12280. +
  12281. +#define w0(p) 0x000000##p
  12282. +
  12283. +// Number of elements required in this table for different
  12284. +// block and key lengths is:
  12285. +//
  12286. +// Nk = 4 6 8
  12287. +// ----------
  12288. +// Nb = 4 | 10 8 7
  12289. +// 6 | 19 12 11
  12290. +// 8 | 29 19 14
  12291. +//
  12292. +// this table can be a table of bytes if the key schedule
  12293. +// code is adjusted accordingly
  12294. +
  12295. +static const u_int32_t rcon_tab[29] =
  12296. +{
  12297. + w0(01), w0(02), w0(04), w0(08),
  12298. + w0(10), w0(20), w0(40), w0(80),
  12299. + w0(1b), w0(36), w0(6c), w0(d8),
  12300. + w0(ab), w0(4d), w0(9a), w0(2f),
  12301. + w0(5e), w0(bc), w0(63), w0(c6),
  12302. + w0(97), w0(35), w0(6a), w0(d4),
  12303. + w0(b3), w0(7d), w0(fa), w0(ef),
  12304. + w0(c5)
  12305. +};
  12306. +
  12307. +#undef w0
  12308. +
  12309. +#define r0(p,q,r,s) 0x##p##q##r##s
  12310. +#define r1(p,q,r,s) 0x##q##r##s##p
  12311. +#define r2(p,q,r,s) 0x##r##s##p##q
  12312. +#define r3(p,q,r,s) 0x##s##p##q##r
  12313. +#define w0(p) 0x000000##p
  12314. +#define w1(p) 0x0000##p##00
  12315. +#define w2(p) 0x00##p##0000
  12316. +#define w3(p) 0x##p##000000
  12317. +
  12318. +#if defined(FIXED_TABLES) && (defined(ONE_TABLE) || defined(FOUR_TABLES))
  12319. +
  12320. +// data for forward tables (other than last round)
  12321. +
  12322. +#define f_table \
  12323. + r(a5,63,63,c6), r(84,7c,7c,f8), r(99,77,77,ee), r(8d,7b,7b,f6),\
  12324. + r(0d,f2,f2,ff), r(bd,6b,6b,d6), r(b1,6f,6f,de), r(54,c5,c5,91),\
  12325. + r(50,30,30,60), r(03,01,01,02), r(a9,67,67,ce), r(7d,2b,2b,56),\
  12326. + r(19,fe,fe,e7), r(62,d7,d7,b5), r(e6,ab,ab,4d), r(9a,76,76,ec),\
  12327. + r(45,ca,ca,8f), r(9d,82,82,1f), r(40,c9,c9,89), r(87,7d,7d,fa),\
  12328. + r(15,fa,fa,ef), r(eb,59,59,b2), r(c9,47,47,8e), r(0b,f0,f0,fb),\
  12329. + r(ec,ad,ad,41), r(67,d4,d4,b3), r(fd,a2,a2,5f), r(ea,af,af,45),\
  12330. + r(bf,9c,9c,23), r(f7,a4,a4,53), r(96,72,72,e4), r(5b,c0,c0,9b),\
  12331. + r(c2,b7,b7,75), r(1c,fd,fd,e1), r(ae,93,93,3d), r(6a,26,26,4c),\
  12332. + r(5a,36,36,6c), r(41,3f,3f,7e), r(02,f7,f7,f5), r(4f,cc,cc,83),\
  12333. + r(5c,34,34,68), r(f4,a5,a5,51), r(34,e5,e5,d1), r(08,f1,f1,f9),\
  12334. + r(93,71,71,e2), r(73,d8,d8,ab), r(53,31,31,62), r(3f,15,15,2a),\
  12335. + r(0c,04,04,08), r(52,c7,c7,95), r(65,23,23,46), r(5e,c3,c3,9d),\
  12336. + r(28,18,18,30), r(a1,96,96,37), r(0f,05,05,0a), r(b5,9a,9a,2f),\
  12337. + r(09,07,07,0e), r(36,12,12,24), r(9b,80,80,1b), r(3d,e2,e2,df),\
  12338. + r(26,eb,eb,cd), r(69,27,27,4e), r(cd,b2,b2,7f), r(9f,75,75,ea),\
  12339. + r(1b,09,09,12), r(9e,83,83,1d), r(74,2c,2c,58), r(2e,1a,1a,34),\
  12340. + r(2d,1b,1b,36), r(b2,6e,6e,dc), r(ee,5a,5a,b4), r(fb,a0,a0,5b),\
  12341. + r(f6,52,52,a4), r(4d,3b,3b,76), r(61,d6,d6,b7), r(ce,b3,b3,7d),\
  12342. + r(7b,29,29,52), r(3e,e3,e3,dd), r(71,2f,2f,5e), r(97,84,84,13),\
  12343. + r(f5,53,53,a6), r(68,d1,d1,b9), r(00,00,00,00), r(2c,ed,ed,c1),\
  12344. + r(60,20,20,40), r(1f,fc,fc,e3), r(c8,b1,b1,79), r(ed,5b,5b,b6),\
  12345. + r(be,6a,6a,d4), r(46,cb,cb,8d), r(d9,be,be,67), r(4b,39,39,72),\
  12346. + r(de,4a,4a,94), r(d4,4c,4c,98), r(e8,58,58,b0), r(4a,cf,cf,85),\
  12347. + r(6b,d0,d0,bb), r(2a,ef,ef,c5), r(e5,aa,aa,4f), r(16,fb,fb,ed),\
  12348. + r(c5,43,43,86), r(d7,4d,4d,9a), r(55,33,33,66), r(94,85,85,11),\
  12349. + r(cf,45,45,8a), r(10,f9,f9,e9), r(06,02,02,04), r(81,7f,7f,fe),\
  12350. + r(f0,50,50,a0), r(44,3c,3c,78), r(ba,9f,9f,25), r(e3,a8,a8,4b),\
  12351. + r(f3,51,51,a2), r(fe,a3,a3,5d), r(c0,40,40,80), r(8a,8f,8f,05),\
  12352. + r(ad,92,92,3f), r(bc,9d,9d,21), r(48,38,38,70), r(04,f5,f5,f1),\
  12353. + r(df,bc,bc,63), r(c1,b6,b6,77), r(75,da,da,af), r(63,21,21,42),\
  12354. + r(30,10,10,20), r(1a,ff,ff,e5), r(0e,f3,f3,fd), r(6d,d2,d2,bf),\
  12355. + r(4c,cd,cd,81), r(14,0c,0c,18), r(35,13,13,26), r(2f,ec,ec,c3),\
  12356. + r(e1,5f,5f,be), r(a2,97,97,35), r(cc,44,44,88), r(39,17,17,2e),\
  12357. + r(57,c4,c4,93), r(f2,a7,a7,55), r(82,7e,7e,fc), r(47,3d,3d,7a),\
  12358. + r(ac,64,64,c8), r(e7,5d,5d,ba), r(2b,19,19,32), r(95,73,73,e6),\
  12359. + r(a0,60,60,c0), r(98,81,81,19), r(d1,4f,4f,9e), r(7f,dc,dc,a3),\
  12360. + r(66,22,22,44), r(7e,2a,2a,54), r(ab,90,90,3b), r(83,88,88,0b),\
  12361. + r(ca,46,46,8c), r(29,ee,ee,c7), r(d3,b8,b8,6b), r(3c,14,14,28),\
  12362. + r(79,de,de,a7), r(e2,5e,5e,bc), r(1d,0b,0b,16), r(76,db,db,ad),\
  12363. + r(3b,e0,e0,db), r(56,32,32,64), r(4e,3a,3a,74), r(1e,0a,0a,14),\
  12364. + r(db,49,49,92), r(0a,06,06,0c), r(6c,24,24,48), r(e4,5c,5c,b8),\
  12365. + r(5d,c2,c2,9f), r(6e,d3,d3,bd), r(ef,ac,ac,43), r(a6,62,62,c4),\
  12366. + r(a8,91,91,39), r(a4,95,95,31), r(37,e4,e4,d3), r(8b,79,79,f2),\
  12367. + r(32,e7,e7,d5), r(43,c8,c8,8b), r(59,37,37,6e), r(b7,6d,6d,da),\
  12368. + r(8c,8d,8d,01), r(64,d5,d5,b1), r(d2,4e,4e,9c), r(e0,a9,a9,49),\
  12369. + r(b4,6c,6c,d8), r(fa,56,56,ac), r(07,f4,f4,f3), r(25,ea,ea,cf),\
  12370. + r(af,65,65,ca), r(8e,7a,7a,f4), r(e9,ae,ae,47), r(18,08,08,10),\
  12371. + r(d5,ba,ba,6f), r(88,78,78,f0), r(6f,25,25,4a), r(72,2e,2e,5c),\
  12372. + r(24,1c,1c,38), r(f1,a6,a6,57), r(c7,b4,b4,73), r(51,c6,c6,97),\
  12373. + r(23,e8,e8,cb), r(7c,dd,dd,a1), r(9c,74,74,e8), r(21,1f,1f,3e),\
  12374. + r(dd,4b,4b,96), r(dc,bd,bd,61), r(86,8b,8b,0d), r(85,8a,8a,0f),\
  12375. + r(90,70,70,e0), r(42,3e,3e,7c), r(c4,b5,b5,71), r(aa,66,66,cc),\
  12376. + r(d8,48,48,90), r(05,03,03,06), r(01,f6,f6,f7), r(12,0e,0e,1c),\
  12377. + r(a3,61,61,c2), r(5f,35,35,6a), r(f9,57,57,ae), r(d0,b9,b9,69),\
  12378. + r(91,86,86,17), r(58,c1,c1,99), r(27,1d,1d,3a), r(b9,9e,9e,27),\
  12379. + r(38,e1,e1,d9), r(13,f8,f8,eb), r(b3,98,98,2b), r(33,11,11,22),\
  12380. + r(bb,69,69,d2), r(70,d9,d9,a9), r(89,8e,8e,07), r(a7,94,94,33),\
  12381. + r(b6,9b,9b,2d), r(22,1e,1e,3c), r(92,87,87,15), r(20,e9,e9,c9),\
  12382. + r(49,ce,ce,87), r(ff,55,55,aa), r(78,28,28,50), r(7a,df,df,a5),\
  12383. + r(8f,8c,8c,03), r(f8,a1,a1,59), r(80,89,89,09), r(17,0d,0d,1a),\
  12384. + r(da,bf,bf,65), r(31,e6,e6,d7), r(c6,42,42,84), r(b8,68,68,d0),\
  12385. + r(c3,41,41,82), r(b0,99,99,29), r(77,2d,2d,5a), r(11,0f,0f,1e),\
  12386. + r(cb,b0,b0,7b), r(fc,54,54,a8), r(d6,bb,bb,6d), r(3a,16,16,2c)
  12387. +
  12388. +// data for inverse tables (other than last round)
  12389. +
  12390. +#define i_table \
  12391. + r(50,a7,f4,51), r(53,65,41,7e), r(c3,a4,17,1a), r(96,5e,27,3a),\
  12392. + r(cb,6b,ab,3b), r(f1,45,9d,1f), r(ab,58,fa,ac), r(93,03,e3,4b),\
  12393. + r(55,fa,30,20), r(f6,6d,76,ad), r(91,76,cc,88), r(25,4c,02,f5),\
  12394. + r(fc,d7,e5,4f), r(d7,cb,2a,c5), r(80,44,35,26), r(8f,a3,62,b5),\
  12395. + r(49,5a,b1,de), r(67,1b,ba,25), r(98,0e,ea,45), r(e1,c0,fe,5d),\
  12396. + r(02,75,2f,c3), r(12,f0,4c,81), r(a3,97,46,8d), r(c6,f9,d3,6b),\
  12397. + r(e7,5f,8f,03), r(95,9c,92,15), r(eb,7a,6d,bf), r(da,59,52,95),\
  12398. + r(2d,83,be,d4), r(d3,21,74,58), r(29,69,e0,49), r(44,c8,c9,8e),\
  12399. + r(6a,89,c2,75), r(78,79,8e,f4), r(6b,3e,58,99), r(dd,71,b9,27),\
  12400. + r(b6,4f,e1,be), r(17,ad,88,f0), r(66,ac,20,c9), r(b4,3a,ce,7d),\
  12401. + r(18,4a,df,63), r(82,31,1a,e5), r(60,33,51,97), r(45,7f,53,62),\
  12402. + r(e0,77,64,b1), r(84,ae,6b,bb), r(1c,a0,81,fe), r(94,2b,08,f9),\
  12403. + r(58,68,48,70), r(19,fd,45,8f), r(87,6c,de,94), r(b7,f8,7b,52),\
  12404. + r(23,d3,73,ab), r(e2,02,4b,72), r(57,8f,1f,e3), r(2a,ab,55,66),\
  12405. + r(07,28,eb,b2), r(03,c2,b5,2f), r(9a,7b,c5,86), r(a5,08,37,d3),\
  12406. + r(f2,87,28,30), r(b2,a5,bf,23), r(ba,6a,03,02), r(5c,82,16,ed),\
  12407. + r(2b,1c,cf,8a), r(92,b4,79,a7), r(f0,f2,07,f3), r(a1,e2,69,4e),\
  12408. + r(cd,f4,da,65), r(d5,be,05,06), r(1f,62,34,d1), r(8a,fe,a6,c4),\
  12409. + r(9d,53,2e,34), r(a0,55,f3,a2), r(32,e1,8a,05), r(75,eb,f6,a4),\
  12410. + r(39,ec,83,0b), r(aa,ef,60,40), r(06,9f,71,5e), r(51,10,6e,bd),\
  12411. + r(f9,8a,21,3e), r(3d,06,dd,96), r(ae,05,3e,dd), r(46,bd,e6,4d),\
  12412. + r(b5,8d,54,91), r(05,5d,c4,71), r(6f,d4,06,04), r(ff,15,50,60),\
  12413. + r(24,fb,98,19), r(97,e9,bd,d6), r(cc,43,40,89), r(77,9e,d9,67),\
  12414. + r(bd,42,e8,b0), r(88,8b,89,07), r(38,5b,19,e7), r(db,ee,c8,79),\
  12415. + r(47,0a,7c,a1), r(e9,0f,42,7c), r(c9,1e,84,f8), r(00,00,00,00),\
  12416. + r(83,86,80,09), r(48,ed,2b,32), r(ac,70,11,1e), r(4e,72,5a,6c),\
  12417. + r(fb,ff,0e,fd), r(56,38,85,0f), r(1e,d5,ae,3d), r(27,39,2d,36),\
  12418. + r(64,d9,0f,0a), r(21,a6,5c,68), r(d1,54,5b,9b), r(3a,2e,36,24),\
  12419. + r(b1,67,0a,0c), r(0f,e7,57,93), r(d2,96,ee,b4), r(9e,91,9b,1b),\
  12420. + r(4f,c5,c0,80), r(a2,20,dc,61), r(69,4b,77,5a), r(16,1a,12,1c),\
  12421. + r(0a,ba,93,e2), r(e5,2a,a0,c0), r(43,e0,22,3c), r(1d,17,1b,12),\
  12422. + r(0b,0d,09,0e), r(ad,c7,8b,f2), r(b9,a8,b6,2d), r(c8,a9,1e,14),\
  12423. + r(85,19,f1,57), r(4c,07,75,af), r(bb,dd,99,ee), r(fd,60,7f,a3),\
  12424. + r(9f,26,01,f7), r(bc,f5,72,5c), r(c5,3b,66,44), r(34,7e,fb,5b),\
  12425. + r(76,29,43,8b), r(dc,c6,23,cb), r(68,fc,ed,b6), r(63,f1,e4,b8),\
  12426. + r(ca,dc,31,d7), r(10,85,63,42), r(40,22,97,13), r(20,11,c6,84),\
  12427. + r(7d,24,4a,85), r(f8,3d,bb,d2), r(11,32,f9,ae), r(6d,a1,29,c7),\
  12428. + r(4b,2f,9e,1d), r(f3,30,b2,dc), r(ec,52,86,0d), r(d0,e3,c1,77),\
  12429. + r(6c,16,b3,2b), r(99,b9,70,a9), r(fa,48,94,11), r(22,64,e9,47),\
  12430. + r(c4,8c,fc,a8), r(1a,3f,f0,a0), r(d8,2c,7d,56), r(ef,90,33,22),\
  12431. + r(c7,4e,49,87), r(c1,d1,38,d9), r(fe,a2,ca,8c), r(36,0b,d4,98),\
  12432. + r(cf,81,f5,a6), r(28,de,7a,a5), r(26,8e,b7,da), r(a4,bf,ad,3f),\
  12433. + r(e4,9d,3a,2c), r(0d,92,78,50), r(9b,cc,5f,6a), r(62,46,7e,54),\
  12434. + r(c2,13,8d,f6), r(e8,b8,d8,90), r(5e,f7,39,2e), r(f5,af,c3,82),\
  12435. + r(be,80,5d,9f), r(7c,93,d0,69), r(a9,2d,d5,6f), r(b3,12,25,cf),\
  12436. + r(3b,99,ac,c8), r(a7,7d,18,10), r(6e,63,9c,e8), r(7b,bb,3b,db),\
  12437. + r(09,78,26,cd), r(f4,18,59,6e), r(01,b7,9a,ec), r(a8,9a,4f,83),\
  12438. + r(65,6e,95,e6), r(7e,e6,ff,aa), r(08,cf,bc,21), r(e6,e8,15,ef),\
  12439. + r(d9,9b,e7,ba), r(ce,36,6f,4a), r(d4,09,9f,ea), r(d6,7c,b0,29),\
  12440. + r(af,b2,a4,31), r(31,23,3f,2a), r(30,94,a5,c6), r(c0,66,a2,35),\
  12441. + r(37,bc,4e,74), r(a6,ca,82,fc), r(b0,d0,90,e0), r(15,d8,a7,33),\
  12442. + r(4a,98,04,f1), r(f7,da,ec,41), r(0e,50,cd,7f), r(2f,f6,91,17),\
  12443. + r(8d,d6,4d,76), r(4d,b0,ef,43), r(54,4d,aa,cc), r(df,04,96,e4),\
  12444. + r(e3,b5,d1,9e), r(1b,88,6a,4c), r(b8,1f,2c,c1), r(7f,51,65,46),\
  12445. + r(04,ea,5e,9d), r(5d,35,8c,01), r(73,74,87,fa), r(2e,41,0b,fb),\
  12446. + r(5a,1d,67,b3), r(52,d2,db,92), r(33,56,10,e9), r(13,47,d6,6d),\
  12447. + r(8c,61,d7,9a), r(7a,0c,a1,37), r(8e,14,f8,59), r(89,3c,13,eb),\
  12448. + r(ee,27,a9,ce), r(35,c9,61,b7), r(ed,e5,1c,e1), r(3c,b1,47,7a),\
  12449. + r(59,df,d2,9c), r(3f,73,f2,55), r(79,ce,14,18), r(bf,37,c7,73),\
  12450. + r(ea,cd,f7,53), r(5b,aa,fd,5f), r(14,6f,3d,df), r(86,db,44,78),\
  12451. + r(81,f3,af,ca), r(3e,c4,68,b9), r(2c,34,24,38), r(5f,40,a3,c2),\
  12452. + r(72,c3,1d,16), r(0c,25,e2,bc), r(8b,49,3c,28), r(41,95,0d,ff),\
  12453. + r(71,01,a8,39), r(de,b3,0c,08), r(9c,e4,b4,d8), r(90,c1,56,64),\
  12454. + r(61,84,cb,7b), r(70,b6,32,d5), r(74,5c,6c,48), r(42,57,b8,d0)
  12455. +
  12456. +// generate the required tables in the desired endian format
  12457. +
  12458. +#undef r
  12459. +#define r r0
  12460. +
  12461. +#if defined(ONE_TABLE)
  12462. +static const u_int32_t ft_tab[256] =
  12463. + { f_table };
  12464. +#elif defined(FOUR_TABLES)
  12465. +static const u_int32_t ft_tab[4][256] =
  12466. +{ { f_table },
  12467. +#undef r
  12468. +#define r r1
  12469. + { f_table },
  12470. +#undef r
  12471. +#define r r2
  12472. + { f_table },
  12473. +#undef r
  12474. +#define r r3
  12475. + { f_table }
  12476. +};
  12477. +#endif
  12478. +
  12479. +#undef r
  12480. +#define r r0
  12481. +#if defined(ONE_TABLE)
  12482. +static const u_int32_t it_tab[256] =
  12483. + { i_table };
  12484. +#elif defined(FOUR_TABLES)
  12485. +static const u_int32_t it_tab[4][256] =
  12486. +{ { i_table },
  12487. +#undef r
  12488. +#define r r1
  12489. + { i_table },
  12490. +#undef r
  12491. +#define r r2
  12492. + { i_table },
  12493. +#undef r
  12494. +#define r r3
  12495. + { i_table }
  12496. +};
  12497. +#endif
  12498. +
  12499. +#endif
  12500. +
  12501. +#if defined(FIXED_TABLES) && (defined(ONE_LR_TABLE) || defined(FOUR_LR_TABLES))
  12502. +
  12503. +// data for inverse tables (last round)
  12504. +
  12505. +#define li_table \
  12506. + w(52), w(09), w(6a), w(d5), w(30), w(36), w(a5), w(38),\
  12507. + w(bf), w(40), w(a3), w(9e), w(81), w(f3), w(d7), w(fb),\
  12508. + w(7c), w(e3), w(39), w(82), w(9b), w(2f), w(ff), w(87),\
  12509. + w(34), w(8e), w(43), w(44), w(c4), w(de), w(e9), w(cb),\
  12510. + w(54), w(7b), w(94), w(32), w(a6), w(c2), w(23), w(3d),\
  12511. + w(ee), w(4c), w(95), w(0b), w(42), w(fa), w(c3), w(4e),\
  12512. + w(08), w(2e), w(a1), w(66), w(28), w(d9), w(24), w(b2),\
  12513. + w(76), w(5b), w(a2), w(49), w(6d), w(8b), w(d1), w(25),\
  12514. + w(72), w(f8), w(f6), w(64), w(86), w(68), w(98), w(16),\
  12515. + w(d4), w(a4), w(5c), w(cc), w(5d), w(65), w(b6), w(92),\
  12516. + w(6c), w(70), w(48), w(50), w(fd), w(ed), w(b9), w(da),\
  12517. + w(5e), w(15), w(46), w(57), w(a7), w(8d), w(9d), w(84),\
  12518. + w(90), w(d8), w(ab), w(00), w(8c), w(bc), w(d3), w(0a),\
  12519. + w(f7), w(e4), w(58), w(05), w(b8), w(b3), w(45), w(06),\
  12520. + w(d0), w(2c), w(1e), w(8f), w(ca), w(3f), w(0f), w(02),\
  12521. + w(c1), w(af), w(bd), w(03), w(01), w(13), w(8a), w(6b),\
  12522. + w(3a), w(91), w(11), w(41), w(4f), w(67), w(dc), w(ea),\
  12523. + w(97), w(f2), w(cf), w(ce), w(f0), w(b4), w(e6), w(73),\
  12524. + w(96), w(ac), w(74), w(22), w(e7), w(ad), w(35), w(85),\
  12525. + w(e2), w(f9), w(37), w(e8), w(1c), w(75), w(df), w(6e),\
  12526. + w(47), w(f1), w(1a), w(71), w(1d), w(29), w(c5), w(89),\
  12527. + w(6f), w(b7), w(62), w(0e), w(aa), w(18), w(be), w(1b),\
  12528. + w(fc), w(56), w(3e), w(4b), w(c6), w(d2), w(79), w(20),\
  12529. + w(9a), w(db), w(c0), w(fe), w(78), w(cd), w(5a), w(f4),\
  12530. + w(1f), w(dd), w(a8), w(33), w(88), w(07), w(c7), w(31),\
  12531. + w(b1), w(12), w(10), w(59), w(27), w(80), w(ec), w(5f),\
  12532. + w(60), w(51), w(7f), w(a9), w(19), w(b5), w(4a), w(0d),\
  12533. + w(2d), w(e5), w(7a), w(9f), w(93), w(c9), w(9c), w(ef),\
  12534. + w(a0), w(e0), w(3b), w(4d), w(ae), w(2a), w(f5), w(b0),\
  12535. + w(c8), w(eb), w(bb), w(3c), w(83), w(53), w(99), w(61),\
  12536. + w(17), w(2b), w(04), w(7e), w(ba), w(77), w(d6), w(26),\
  12537. + w(e1), w(69), w(14), w(63), w(55), w(21), w(0c), w(7d),
  12538. +
  12539. +// generate the required tables in the desired endian format
  12540. +
  12541. +#undef r
  12542. +#define r(p,q,r,s) w0(q)
  12543. +#if defined(ONE_LR_TABLE)
  12544. +static const u_int32_t fl_tab[256] =
  12545. + { f_table };
  12546. +#elif defined(FOUR_LR_TABLES)
  12547. +static const u_int32_t fl_tab[4][256] =
  12548. +{ { f_table },
  12549. +#undef r
  12550. +#define r(p,q,r,s) w1(q)
  12551. + { f_table },
  12552. +#undef r
  12553. +#define r(p,q,r,s) w2(q)
  12554. + { f_table },
  12555. +#undef r
  12556. +#define r(p,q,r,s) w3(q)
  12557. + { f_table }
  12558. +};
  12559. +#endif
  12560. +
  12561. +#undef w
  12562. +#define w w0
  12563. +#if defined(ONE_LR_TABLE)
  12564. +static const u_int32_t il_tab[256] =
  12565. + { li_table };
  12566. +#elif defined(FOUR_LR_TABLES)
  12567. +static const u_int32_t il_tab[4][256] =
  12568. +{ { li_table },
  12569. +#undef w
  12570. +#define w w1
  12571. + { li_table },
  12572. +#undef w
  12573. +#define w w2
  12574. + { li_table },
  12575. +#undef w
  12576. +#define w w3
  12577. + { li_table }
  12578. +};
  12579. +#endif
  12580. +
  12581. +#endif
  12582. +
  12583. +#if defined(FIXED_TABLES) && (defined(ONE_IM_TABLE) || defined(FOUR_IM_TABLES))
  12584. +
  12585. +#define m_table \
  12586. + r(00,00,00,00), r(0b,0d,09,0e), r(16,1a,12,1c), r(1d,17,1b,12),\
  12587. + r(2c,34,24,38), r(27,39,2d,36), r(3a,2e,36,24), r(31,23,3f,2a),\
  12588. + r(58,68,48,70), r(53,65,41,7e), r(4e,72,5a,6c), r(45,7f,53,62),\
  12589. + r(74,5c,6c,48), r(7f,51,65,46), r(62,46,7e,54), r(69,4b,77,5a),\
  12590. + r(b0,d0,90,e0), r(bb,dd,99,ee), r(a6,ca,82,fc), r(ad,c7,8b,f2),\
  12591. + r(9c,e4,b4,d8), r(97,e9,bd,d6), r(8a,fe,a6,c4), r(81,f3,af,ca),\
  12592. + r(e8,b8,d8,90), r(e3,b5,d1,9e), r(fe,a2,ca,8c), r(f5,af,c3,82),\
  12593. + r(c4,8c,fc,a8), r(cf,81,f5,a6), r(d2,96,ee,b4), r(d9,9b,e7,ba),\
  12594. + r(7b,bb,3b,db), r(70,b6,32,d5), r(6d,a1,29,c7), r(66,ac,20,c9),\
  12595. + r(57,8f,1f,e3), r(5c,82,16,ed), r(41,95,0d,ff), r(4a,98,04,f1),\
  12596. + r(23,d3,73,ab), r(28,de,7a,a5), r(35,c9,61,b7), r(3e,c4,68,b9),\
  12597. + r(0f,e7,57,93), r(04,ea,5e,9d), r(19,fd,45,8f), r(12,f0,4c,81),\
  12598. + r(cb,6b,ab,3b), r(c0,66,a2,35), r(dd,71,b9,27), r(d6,7c,b0,29),\
  12599. + r(e7,5f,8f,03), r(ec,52,86,0d), r(f1,45,9d,1f), r(fa,48,94,11),\
  12600. + r(93,03,e3,4b), r(98,0e,ea,45), r(85,19,f1,57), r(8e,14,f8,59),\
  12601. + r(bf,37,c7,73), r(b4,3a,ce,7d), r(a9,2d,d5,6f), r(a2,20,dc,61),\
  12602. + r(f6,6d,76,ad), r(fd,60,7f,a3), r(e0,77,64,b1), r(eb,7a,6d,bf),\
  12603. + r(da,59,52,95), r(d1,54,5b,9b), r(cc,43,40,89), r(c7,4e,49,87),\
  12604. + r(ae,05,3e,dd), r(a5,08,37,d3), r(b8,1f,2c,c1), r(b3,12,25,cf),\
  12605. + r(82,31,1a,e5), r(89,3c,13,eb), r(94,2b,08,f9), r(9f,26,01,f7),\
  12606. + r(46,bd,e6,4d), r(4d,b0,ef,43), r(50,a7,f4,51), r(5b,aa,fd,5f),\
  12607. + r(6a,89,c2,75), r(61,84,cb,7b), r(7c,93,d0,69), r(77,9e,d9,67),\
  12608. + r(1e,d5,ae,3d), r(15,d8,a7,33), r(08,cf,bc,21), r(03,c2,b5,2f),\
  12609. + r(32,e1,8a,05), r(39,ec,83,0b), r(24,fb,98,19), r(2f,f6,91,17),\
  12610. + r(8d,d6,4d,76), r(86,db,44,78), r(9b,cc,5f,6a), r(90,c1,56,64),\
  12611. + r(a1,e2,69,4e), r(aa,ef,60,40), r(b7,f8,7b,52), r(bc,f5,72,5c),\
  12612. + r(d5,be,05,06), r(de,b3,0c,08), r(c3,a4,17,1a), r(c8,a9,1e,14),\
  12613. + r(f9,8a,21,3e), r(f2,87,28,30), r(ef,90,33,22), r(e4,9d,3a,2c),\
  12614. + r(3d,06,dd,96), r(36,0b,d4,98), r(2b,1c,cf,8a), r(20,11,c6,84),\
  12615. + r(11,32,f9,ae), r(1a,3f,f0,a0), r(07,28,eb,b2), r(0c,25,e2,bc),\
  12616. + r(65,6e,95,e6), r(6e,63,9c,e8), r(73,74,87,fa), r(78,79,8e,f4),\
  12617. + r(49,5a,b1,de), r(42,57,b8,d0), r(5f,40,a3,c2), r(54,4d,aa,cc),\
  12618. + r(f7,da,ec,41), r(fc,d7,e5,4f), r(e1,c0,fe,5d), r(ea,cd,f7,53),\
  12619. + r(db,ee,c8,79), r(d0,e3,c1,77), r(cd,f4,da,65), r(c6,f9,d3,6b),\
  12620. + r(af,b2,a4,31), r(a4,bf,ad,3f), r(b9,a8,b6,2d), r(b2,a5,bf,23),\
  12621. + r(83,86,80,09), r(88,8b,89,07), r(95,9c,92,15), r(9e,91,9b,1b),\
  12622. + r(47,0a,7c,a1), r(4c,07,75,af), r(51,10,6e,bd), r(5a,1d,67,b3),\
  12623. + r(6b,3e,58,99), r(60,33,51,97), r(7d,24,4a,85), r(76,29,43,8b),\
  12624. + r(1f,62,34,d1), r(14,6f,3d,df), r(09,78,26,cd), r(02,75,2f,c3),\
  12625. + r(33,56,10,e9), r(38,5b,19,e7), r(25,4c,02,f5), r(2e,41,0b,fb),\
  12626. + r(8c,61,d7,9a), r(87,6c,de,94), r(9a,7b,c5,86), r(91,76,cc,88),\
  12627. + r(a0,55,f3,a2), r(ab,58,fa,ac), r(b6,4f,e1,be), r(bd,42,e8,b0),\
  12628. + r(d4,09,9f,ea), r(df,04,96,e4), r(c2,13,8d,f6), r(c9,1e,84,f8),\
  12629. + r(f8,3d,bb,d2), r(f3,30,b2,dc), r(ee,27,a9,ce), r(e5,2a,a0,c0),\
  12630. + r(3c,b1,47,7a), r(37,bc,4e,74), r(2a,ab,55,66), r(21,a6,5c,68),\
  12631. + r(10,85,63,42), r(1b,88,6a,4c), r(06,9f,71,5e), r(0d,92,78,50),\
  12632. + r(64,d9,0f,0a), r(6f,d4,06,04), r(72,c3,1d,16), r(79,ce,14,18),\
  12633. + r(48,ed,2b,32), r(43,e0,22,3c), r(5e,f7,39,2e), r(55,fa,30,20),\
  12634. + r(01,b7,9a,ec), r(0a,ba,93,e2), r(17,ad,88,f0), r(1c,a0,81,fe),\
  12635. + r(2d,83,be,d4), r(26,8e,b7,da), r(3b,99,ac,c8), r(30,94,a5,c6),\
  12636. + r(59,df,d2,9c), r(52,d2,db,92), r(4f,c5,c0,80), r(44,c8,c9,8e),\
  12637. + r(75,eb,f6,a4), r(7e,e6,ff,aa), r(63,f1,e4,b8), r(68,fc,ed,b6),\
  12638. + r(b1,67,0a,0c), r(ba,6a,03,02), r(a7,7d,18,10), r(ac,70,11,1e),\
  12639. + r(9d,53,2e,34), r(96,5e,27,3a), r(8b,49,3c,28), r(80,44,35,26),\
  12640. + r(e9,0f,42,7c), r(e2,02,4b,72), r(ff,15,50,60), r(f4,18,59,6e),\
  12641. + r(c5,3b,66,44), r(ce,36,6f,4a), r(d3,21,74,58), r(d8,2c,7d,56),\
  12642. + r(7a,0c,a1,37), r(71,01,a8,39), r(6c,16,b3,2b), r(67,1b,ba,25),\
  12643. + r(56,38,85,0f), r(5d,35,8c,01), r(40,22,97,13), r(4b,2f,9e,1d),\
  12644. + r(22,64,e9,47), r(29,69,e0,49), r(34,7e,fb,5b), r(3f,73,f2,55),\
  12645. + r(0e,50,cd,7f), r(05,5d,c4,71), r(18,4a,df,63), r(13,47,d6,6d),\
  12646. + r(ca,dc,31,d7), r(c1,d1,38,d9), r(dc,c6,23,cb), r(d7,cb,2a,c5),\
  12647. + r(e6,e8,15,ef), r(ed,e5,1c,e1), r(f0,f2,07,f3), r(fb,ff,0e,fd),\
  12648. + r(92,b4,79,a7), r(99,b9,70,a9), r(84,ae,6b,bb), r(8f,a3,62,b5),\
  12649. + r(be,80,5d,9f), r(b5,8d,54,91), r(a8,9a,4f,83), r(a3,97,46,8d)
  12650. +
  12651. +#undef r
  12652. +#define r r0
  12653. +
  12654. +#if defined(ONE_IM_TABLE)
  12655. +static const u_int32_t im_tab[256] =
  12656. + { m_table };
  12657. +#elif defined(FOUR_IM_TABLES)
  12658. +static const u_int32_t im_tab[4][256] =
  12659. +{ { m_table },
  12660. +#undef r
  12661. +#define r r1
  12662. + { m_table },
  12663. +#undef r
  12664. +#define r r2
  12665. + { m_table },
  12666. +#undef r
  12667. +#define r r3
  12668. + { m_table }
  12669. +};
  12670. +#endif
  12671. +
  12672. +#endif
  12673. +
  12674. +#else
  12675. +
  12676. +static int tab_gen = 0;
  12677. +
  12678. +static unsigned char s_box[256]; // the S box
  12679. +static unsigned char inv_s_box[256]; // the inverse S box
  12680. +static u_int32_t rcon_tab[AES_RC_LENGTH]; // table of round constants
  12681. +
  12682. +#if defined(ONE_TABLE)
  12683. +static u_int32_t ft_tab[256];
  12684. +static u_int32_t it_tab[256];
  12685. +#elif defined(FOUR_TABLES)
  12686. +static u_int32_t ft_tab[4][256];
  12687. +static u_int32_t it_tab[4][256];
  12688. +#endif
  12689. +
  12690. +#if defined(ONE_LR_TABLE)
  12691. +static u_int32_t fl_tab[256];
  12692. +static u_int32_t il_tab[256];
  12693. +#elif defined(FOUR_LR_TABLES)
  12694. +static u_int32_t fl_tab[4][256];
  12695. +static u_int32_t il_tab[4][256];
  12696. +#endif
  12697. +
  12698. +#if defined(ONE_IM_TABLE)
  12699. +static u_int32_t im_tab[256];
  12700. +#elif defined(FOUR_IM_TABLES)
  12701. +static u_int32_t im_tab[4][256];
  12702. +#endif
  12703. +
  12704. +// Generate the tables for the dynamic table option
  12705. +
  12706. +#if !defined(FF_TABLES)
  12707. +
  12708. +// It will generally be sensible to use tables to compute finite
  12709. +// field multiplies and inverses but where memory is scarse this
  12710. +// code might sometimes be better.
  12711. +
  12712. +// return 2 ^ (n - 1) where n is the bit number of the highest bit
  12713. +// set in x with x in the range 1 < x < 0x00000200. This form is
  12714. +// used so that locals within FFinv can be bytes rather than words
  12715. +
  12716. +static unsigned char hibit(const u_int32_t x)
  12717. +{ unsigned char r = (unsigned char)((x >> 1) | (x >> 2));
  12718. +
  12719. + r |= (r >> 2);
  12720. + r |= (r >> 4);
  12721. + return (r + 1) >> 1;
  12722. +}
  12723. +
  12724. +// return the inverse of the finite field element x
  12725. +
  12726. +static unsigned char FFinv(const unsigned char x)
  12727. +{ unsigned char p1 = x, p2 = 0x1b, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  12728. +
  12729. + if(x < 2) return x;
  12730. +
  12731. + for(;;)
  12732. + {
  12733. + if(!n1) return v1;
  12734. +
  12735. + while(n2 >= n1)
  12736. + {
  12737. + n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
  12738. + }
  12739. +
  12740. + if(!n2) return v2;
  12741. +
  12742. + while(n1 >= n2)
  12743. + {
  12744. + n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
  12745. + }
  12746. + }
  12747. +}
  12748. +
  12749. +// define the finite field multiplies required for Rijndael
  12750. +
  12751. +#define FFmul02(x) ((((x) & 0x7f) << 1) ^ ((x) & 0x80 ? 0x1b : 0))
  12752. +#define FFmul03(x) ((x) ^ FFmul02(x))
  12753. +#define FFmul09(x) ((x) ^ FFmul02(FFmul02(FFmul02(x))))
  12754. +#define FFmul0b(x) ((x) ^ FFmul02((x) ^ FFmul02(FFmul02(x))))
  12755. +#define FFmul0d(x) ((x) ^ FFmul02(FFmul02((x) ^ FFmul02(x))))
  12756. +#define FFmul0e(x) FFmul02((x) ^ FFmul02((x) ^ FFmul02(x)))
  12757. +
  12758. +#else
  12759. +
  12760. +#define FFinv(x) ((x) ? pow[255 - log[x]]: 0)
  12761. +
  12762. +#define FFmul02(x) (x ? pow[log[x] + 0x19] : 0)
  12763. +#define FFmul03(x) (x ? pow[log[x] + 0x01] : 0)
  12764. +#define FFmul09(x) (x ? pow[log[x] + 0xc7] : 0)
  12765. +#define FFmul0b(x) (x ? pow[log[x] + 0x68] : 0)
  12766. +#define FFmul0d(x) (x ? pow[log[x] + 0xee] : 0)
  12767. +#define FFmul0e(x) (x ? pow[log[x] + 0xdf] : 0)
  12768. +
  12769. +#endif
  12770. +
  12771. +// The forward and inverse affine transformations used in the S-box
  12772. +
  12773. +#define fwd_affine(x) \
  12774. + (w = (u_int32_t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(unsigned char)(w^(w>>8)))
  12775. +
  12776. +#define inv_affine(x) \
  12777. + (w = (u_int32_t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(unsigned char)(w^(w>>8)))
  12778. +
  12779. +static void gen_tabs(void)
  12780. +{ u_int32_t i, w;
  12781. +
  12782. +#if defined(FF_TABLES)
  12783. +
  12784. + unsigned char pow[512], log[256];
  12785. +
  12786. + // log and power tables for GF(2^8) finite field with
  12787. + // 0x011b as modular polynomial - the simplest primitive
  12788. + // root is 0x03, used here to generate the tables
  12789. +
  12790. + i = 0; w = 1;
  12791. + do
  12792. + {
  12793. + pow[i] = (unsigned char)w;
  12794. + pow[i + 255] = (unsigned char)w;
  12795. + log[w] = (unsigned char)i++;
  12796. + w ^= (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  12797. + }
  12798. + while (w != 1);
  12799. +
  12800. +#endif
  12801. +
  12802. + for(i = 0, w = 1; i < AES_RC_LENGTH; ++i)
  12803. + {
  12804. + rcon_tab[i] = bytes2word(w, 0, 0, 0);
  12805. + w = (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  12806. + }
  12807. +
  12808. + for(i = 0; i < 256; ++i)
  12809. + { unsigned char b;
  12810. +
  12811. + s_box[i] = b = fwd_affine(FFinv((unsigned char)i));
  12812. +
  12813. + w = bytes2word(b, 0, 0, 0);
  12814. +#if defined(ONE_LR_TABLE)
  12815. + fl_tab[i] = w;
  12816. +#elif defined(FOUR_LR_TABLES)
  12817. + fl_tab[0][i] = w;
  12818. + fl_tab[1][i] = upr(w,1);
  12819. + fl_tab[2][i] = upr(w,2);
  12820. + fl_tab[3][i] = upr(w,3);
  12821. +#endif
  12822. + w = bytes2word(FFmul02(b), b, b, FFmul03(b));
  12823. +#if defined(ONE_TABLE)
  12824. + ft_tab[i] = w;
  12825. +#elif defined(FOUR_TABLES)
  12826. + ft_tab[0][i] = w;
  12827. + ft_tab[1][i] = upr(w,1);
  12828. + ft_tab[2][i] = upr(w,2);
  12829. + ft_tab[3][i] = upr(w,3);
  12830. +#endif
  12831. + inv_s_box[i] = b = FFinv(inv_affine((unsigned char)i));
  12832. +
  12833. + w = bytes2word(b, 0, 0, 0);
  12834. +#if defined(ONE_LR_TABLE)
  12835. + il_tab[i] = w;
  12836. +#elif defined(FOUR_LR_TABLES)
  12837. + il_tab[0][i] = w;
  12838. + il_tab[1][i] = upr(w,1);
  12839. + il_tab[2][i] = upr(w,2);
  12840. + il_tab[3][i] = upr(w,3);
  12841. +#endif
  12842. + w = bytes2word(FFmul0e(b), FFmul09(b), FFmul0d(b), FFmul0b(b));
  12843. +#if defined(ONE_TABLE)
  12844. + it_tab[i] = w;
  12845. +#elif defined(FOUR_TABLES)
  12846. + it_tab[0][i] = w;
  12847. + it_tab[1][i] = upr(w,1);
  12848. + it_tab[2][i] = upr(w,2);
  12849. + it_tab[3][i] = upr(w,3);
  12850. +#endif
  12851. +#if defined(ONE_IM_TABLE)
  12852. + im_tab[b] = w;
  12853. +#elif defined(FOUR_IM_TABLES)
  12854. + im_tab[0][b] = w;
  12855. + im_tab[1][b] = upr(w,1);
  12856. + im_tab[2][b] = upr(w,2);
  12857. + im_tab[3][b] = upr(w,3);
  12858. +#endif
  12859. +
  12860. + }
  12861. +}
  12862. +
  12863. +#endif
  12864. +
  12865. +#define no_table(x,box,vf,rf,c) bytes2word( \
  12866. + box[bval(vf(x,0,c),rf(0,c))], \
  12867. + box[bval(vf(x,1,c),rf(1,c))], \
  12868. + box[bval(vf(x,2,c),rf(2,c))], \
  12869. + box[bval(vf(x,3,c),rf(3,c))])
  12870. +
  12871. +#define one_table(x,op,tab,vf,rf,c) \
  12872. + ( tab[bval(vf(x,0,c),rf(0,c))] \
  12873. + ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
  12874. + ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
  12875. + ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
  12876. +
  12877. +#define four_tables(x,tab,vf,rf,c) \
  12878. + ( tab[0][bval(vf(x,0,c),rf(0,c))] \
  12879. + ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
  12880. + ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
  12881. + ^ tab[3][bval(vf(x,3,c),rf(3,c))])
  12882. +
  12883. +#define vf1(x,r,c) (x)
  12884. +#define rf1(r,c) (r)
  12885. +#define rf2(r,c) ((r-c)&3)
  12886. +
  12887. +#if defined(FOUR_LR_TABLES)
  12888. +#define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
  12889. +#elif defined(ONE_LR_TABLE)
  12890. +#define ls_box(x,c) one_table(x,upr,fl_tab,vf1,rf2,c)
  12891. +#else
  12892. +#define ls_box(x,c) no_table(x,s_box,vf1,rf2,c)
  12893. +#endif
  12894. +
  12895. +#if defined(FOUR_IM_TABLES)
  12896. +#define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
  12897. +#elif defined(ONE_IM_TABLE)
  12898. +#define inv_mcol(x) one_table(x,upr,im_tab,vf1,rf1,0)
  12899. +#else
  12900. +#define inv_mcol(x) \
  12901. + (f9 = (x),f2 = FFmulX(f9), f4 = FFmulX(f2), f8 = FFmulX(f4), f9 ^= f8, \
  12902. + f2 ^= f4 ^ f8 ^ upr(f2 ^ f9,3) ^ upr(f4 ^ f9,2) ^ upr(f9,1))
  12903. +#endif
  12904. +
  12905. +// Subroutine to set the block size (if variable) in bytes, legal
  12906. +// values being 16, 24 and 32.
  12907. +
  12908. +#if defined(AES_BLOCK_SIZE)
  12909. +#define nc (AES_BLOCK_SIZE / 4)
  12910. +#else
  12911. +#define nc (cx->aes_Ncol)
  12912. +
  12913. +void aes_set_blk(aes_context *cx, int n_bytes)
  12914. +{
  12915. +#if !defined(FIXED_TABLES)
  12916. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  12917. +#endif
  12918. +
  12919. + switch(n_bytes) {
  12920. + case 32: /* bytes */
  12921. + case 256: /* bits */
  12922. + nc = 8;
  12923. + break;
  12924. + case 24: /* bytes */
  12925. + case 192: /* bits */
  12926. + nc = 6;
  12927. + break;
  12928. + case 16: /* bytes */
  12929. + case 128: /* bits */
  12930. + default:
  12931. + nc = 4;
  12932. + break;
  12933. + }
  12934. +}
  12935. +
  12936. +#endif
  12937. +
  12938. +// Initialise the key schedule from the user supplied key. The key
  12939. +// length is now specified in bytes - 16, 24 or 32 as appropriate.
  12940. +// This corresponds to bit lengths of 128, 192 and 256 bits, and
  12941. +// to Nk values of 4, 6 and 8 respectively.
  12942. +
  12943. +#define mx(t,f) (*t++ = inv_mcol(*f),f++)
  12944. +#define cp(t,f) *t++ = *f++
  12945. +
  12946. +#if AES_BLOCK_SIZE == 16
  12947. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  12948. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  12949. +#elif AES_BLOCK_SIZE == 24
  12950. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  12951. + cp(d,s); cp(d,s)
  12952. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  12953. + mx(d,s); mx(d,s)
  12954. +#elif AES_BLOCK_SIZE == 32
  12955. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  12956. + cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  12957. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  12958. + mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  12959. +#else
  12960. +
  12961. +#define cpy(d,s) \
  12962. +switch(nc) \
  12963. +{ case 8: cp(d,s); cp(d,s); \
  12964. + case 6: cp(d,s); cp(d,s); \
  12965. + case 4: cp(d,s); cp(d,s); \
  12966. + cp(d,s); cp(d,s); \
  12967. +}
  12968. +
  12969. +#define mix(d,s) \
  12970. +switch(nc) \
  12971. +{ case 8: mx(d,s); mx(d,s); \
  12972. + case 6: mx(d,s); mx(d,s); \
  12973. + case 4: mx(d,s); mx(d,s); \
  12974. + mx(d,s); mx(d,s); \
  12975. +}
  12976. +
  12977. +#endif
  12978. +
  12979. +void aes_set_key(aes_context *cx, const unsigned char in_key[], int n_bytes, const int f)
  12980. +{ u_int32_t *kf, *kt, rci;
  12981. +
  12982. +#if !defined(FIXED_TABLES)
  12983. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  12984. +#endif
  12985. +
  12986. + switch(n_bytes) {
  12987. + case 32: /* bytes */
  12988. + case 256: /* bits */
  12989. + cx->aes_Nkey = 8;
  12990. + break;
  12991. + case 24: /* bytes */
  12992. + case 192: /* bits */
  12993. + cx->aes_Nkey = 6;
  12994. + break;
  12995. + case 16: /* bytes */
  12996. + case 128: /* bits */
  12997. + default:
  12998. + cx->aes_Nkey = 4;
  12999. + break;
  13000. + }
  13001. +
  13002. + cx->aes_Nrnd = (cx->aes_Nkey > nc ? cx->aes_Nkey : nc) + 6;
  13003. +
  13004. + cx->aes_e_key[0] = const_word_in(in_key );
  13005. + cx->aes_e_key[1] = const_word_in(in_key + 4);
  13006. + cx->aes_e_key[2] = const_word_in(in_key + 8);
  13007. + cx->aes_e_key[3] = const_word_in(in_key + 12);
  13008. +
  13009. + kf = cx->aes_e_key;
  13010. + kt = kf + nc * (cx->aes_Nrnd + 1) - cx->aes_Nkey;
  13011. + rci = 0;
  13012. +
  13013. + switch(cx->aes_Nkey)
  13014. + {
  13015. + case 4: do
  13016. + { kf[4] = kf[0] ^ ls_box(kf[3],3) ^ rcon_tab[rci++];
  13017. + kf[5] = kf[1] ^ kf[4];
  13018. + kf[6] = kf[2] ^ kf[5];
  13019. + kf[7] = kf[3] ^ kf[6];
  13020. + kf += 4;
  13021. + }
  13022. + while(kf < kt);
  13023. + break;
  13024. +
  13025. + case 6: cx->aes_e_key[4] = const_word_in(in_key + 16);
  13026. + cx->aes_e_key[5] = const_word_in(in_key + 20);
  13027. + do
  13028. + { kf[ 6] = kf[0] ^ ls_box(kf[5],3) ^ rcon_tab[rci++];
  13029. + kf[ 7] = kf[1] ^ kf[ 6];
  13030. + kf[ 8] = kf[2] ^ kf[ 7];
  13031. + kf[ 9] = kf[3] ^ kf[ 8];
  13032. + kf[10] = kf[4] ^ kf[ 9];
  13033. + kf[11] = kf[5] ^ kf[10];
  13034. + kf += 6;
  13035. + }
  13036. + while(kf < kt);
  13037. + break;
  13038. +
  13039. + case 8: cx->aes_e_key[4] = const_word_in(in_key + 16);
  13040. + cx->aes_e_key[5] = const_word_in(in_key + 20);
  13041. + cx->aes_e_key[6] = const_word_in(in_key + 24);
  13042. + cx->aes_e_key[7] = const_word_in(in_key + 28);
  13043. + do
  13044. + { kf[ 8] = kf[0] ^ ls_box(kf[7],3) ^ rcon_tab[rci++];
  13045. + kf[ 9] = kf[1] ^ kf[ 8];
  13046. + kf[10] = kf[2] ^ kf[ 9];
  13047. + kf[11] = kf[3] ^ kf[10];
  13048. + kf[12] = kf[4] ^ ls_box(kf[11],0);
  13049. + kf[13] = kf[5] ^ kf[12];
  13050. + kf[14] = kf[6] ^ kf[13];
  13051. + kf[15] = kf[7] ^ kf[14];
  13052. + kf += 8;
  13053. + }
  13054. + while (kf < kt);
  13055. + break;
  13056. + }
  13057. +
  13058. + if(!f)
  13059. + { u_int32_t i;
  13060. +
  13061. + kt = cx->aes_d_key + nc * cx->aes_Nrnd;
  13062. + kf = cx->aes_e_key;
  13063. +
  13064. + cpy(kt, kf); kt -= 2 * nc;
  13065. +
  13066. + for(i = 1; i < cx->aes_Nrnd; ++i)
  13067. + {
  13068. +#if defined(ONE_TABLE) || defined(FOUR_TABLES)
  13069. +#if !defined(ONE_IM_TABLE) && !defined(FOUR_IM_TABLES)
  13070. + u_int32_t f2, f4, f8, f9;
  13071. +#endif
  13072. + mix(kt, kf);
  13073. +#else
  13074. + cpy(kt, kf);
  13075. +#endif
  13076. + kt -= 2 * nc;
  13077. + }
  13078. +
  13079. + cpy(kt, kf);
  13080. + }
  13081. +}
  13082. +
  13083. +// y = output word, x = input word, r = row, c = column
  13084. +// for r = 0, 1, 2 and 3 = column accessed for row r
  13085. +
  13086. +#if defined(ARRAYS)
  13087. +#define s(x,c) x[c]
  13088. +#else
  13089. +#define s(x,c) x##c
  13090. +#endif
  13091. +
  13092. +// I am grateful to Frank Yellin for the following constructions
  13093. +// which, given the column (c) of the output state variable that
  13094. +// is being computed, return the input state variables which are
  13095. +// needed for each row (r) of the state
  13096. +
  13097. +// For the fixed block size options, compilers reduce these two
  13098. +// expressions to fixed variable references. For variable block
  13099. +// size code conditional clauses will sometimes be returned
  13100. +
  13101. +#define unused 77 // Sunset Strip
  13102. +
  13103. +#define fwd_var(x,r,c) \
  13104. + ( r==0 ? \
  13105. + ( c==0 ? s(x,0) \
  13106. + : c==1 ? s(x,1) \
  13107. + : c==2 ? s(x,2) \
  13108. + : c==3 ? s(x,3) \
  13109. + : c==4 ? s(x,4) \
  13110. + : c==5 ? s(x,5) \
  13111. + : c==6 ? s(x,6) \
  13112. + : s(x,7)) \
  13113. + : r==1 ? \
  13114. + ( c==0 ? s(x,1) \
  13115. + : c==1 ? s(x,2) \
  13116. + : c==2 ? s(x,3) \
  13117. + : c==3 ? nc==4 ? s(x,0) : s(x,4) \
  13118. + : c==4 ? s(x,5) \
  13119. + : c==5 ? nc==8 ? s(x,6) : s(x,0) \
  13120. + : c==6 ? s(x,7) \
  13121. + : s(x,0)) \
  13122. + : r==2 ? \
  13123. + ( c==0 ? nc==8 ? s(x,3) : s(x,2) \
  13124. + : c==1 ? nc==8 ? s(x,4) : s(x,3) \
  13125. + : c==2 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  13126. + : c==3 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  13127. + : c==4 ? nc==8 ? s(x,7) : s(x,0) \
  13128. + : c==5 ? nc==8 ? s(x,0) : s(x,1) \
  13129. + : c==6 ? s(x,1) \
  13130. + : s(x,2)) \
  13131. + : \
  13132. + ( c==0 ? nc==8 ? s(x,4) : s(x,3) \
  13133. + : c==1 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  13134. + : c==2 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  13135. + : c==3 ? nc==4 ? s(x,2) : nc==8 ? s(x,7) : s(x,0) \
  13136. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  13137. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  13138. + : c==6 ? s(x,2) \
  13139. + : s(x,3)))
  13140. +
  13141. +#define inv_var(x,r,c) \
  13142. + ( r==0 ? \
  13143. + ( c==0 ? s(x,0) \
  13144. + : c==1 ? s(x,1) \
  13145. + : c==2 ? s(x,2) \
  13146. + : c==3 ? s(x,3) \
  13147. + : c==4 ? s(x,4) \
  13148. + : c==5 ? s(x,5) \
  13149. + : c==6 ? s(x,6) \
  13150. + : s(x,7)) \
  13151. + : r==1 ? \
  13152. + ( c==0 ? nc==4 ? s(x,3) : nc==8 ? s(x,7) : s(x,5) \
  13153. + : c==1 ? s(x,0) \
  13154. + : c==2 ? s(x,1) \
  13155. + : c==3 ? s(x,2) \
  13156. + : c==4 ? s(x,3) \
  13157. + : c==5 ? s(x,4) \
  13158. + : c==6 ? s(x,5) \
  13159. + : s(x,6)) \
  13160. + : r==2 ? \
  13161. + ( c==0 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  13162. + : c==1 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  13163. + : c==2 ? nc==8 ? s(x,7) : s(x,0) \
  13164. + : c==3 ? nc==8 ? s(x,0) : s(x,1) \
  13165. + : c==4 ? nc==8 ? s(x,1) : s(x,2) \
  13166. + : c==5 ? nc==8 ? s(x,2) : s(x,3) \
  13167. + : c==6 ? s(x,3) \
  13168. + : s(x,4)) \
  13169. + : \
  13170. + ( c==0 ? nc==4 ? s(x,1) : nc==8 ? s(x,4) : s(x,3) \
  13171. + : c==1 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  13172. + : c==2 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  13173. + : c==3 ? nc==8 ? s(x,7) : s(x,0) \
  13174. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  13175. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  13176. + : c==6 ? s(x,2) \
  13177. + : s(x,3)))
  13178. +
  13179. +#define si(y,x,k,c) s(y,c) = const_word_in(x + 4 * c) ^ k[c]
  13180. +#define so(y,x,c) word_out(y + 4 * c, s(x,c))
  13181. +
  13182. +#if defined(FOUR_TABLES)
  13183. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,ft_tab,fwd_var,rf1,c)
  13184. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,it_tab,inv_var,rf1,c)
  13185. +#elif defined(ONE_TABLE)
  13186. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,ft_tab,fwd_var,rf1,c)
  13187. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,it_tab,inv_var,rf1,c)
  13188. +#else
  13189. +#define fwd_rnd(y,x,k,c) s(y,c) = fwd_mcol(no_table(x,s_box,fwd_var,rf1,c)) ^ (k)[c]
  13190. +#define inv_rnd(y,x,k,c) s(y,c) = inv_mcol(no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c])
  13191. +#endif
  13192. +
  13193. +#if defined(FOUR_LR_TABLES)
  13194. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,fl_tab,fwd_var,rf1,c)
  13195. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,il_tab,inv_var,rf1,c)
  13196. +#elif defined(ONE_LR_TABLE)
  13197. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,fl_tab,fwd_var,rf1,c)
  13198. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,il_tab,inv_var,rf1,c)
  13199. +#else
  13200. +#define fwd_lrnd(y,x,k,c) s(y,c) = no_table(x,s_box,fwd_var,rf1,c) ^ (k)[c]
  13201. +#define inv_lrnd(y,x,k,c) s(y,c) = no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c]
  13202. +#endif
  13203. +
  13204. +#if AES_BLOCK_SIZE == 16
  13205. +
  13206. +#if defined(ARRAYS)
  13207. +#define locals(y,x) x[4],y[4]
  13208. +#else
  13209. +#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
  13210. +// the following defines prevent the compiler requiring the declaration
  13211. +// of generated but unused variables in the fwd_var and inv_var macros
  13212. +#define b04 unused
  13213. +#define b05 unused
  13214. +#define b06 unused
  13215. +#define b07 unused
  13216. +#define b14 unused
  13217. +#define b15 unused
  13218. +#define b16 unused
  13219. +#define b17 unused
  13220. +#endif
  13221. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13222. + s(y,2) = s(x,2); s(y,3) = s(x,3);
  13223. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
  13224. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
  13225. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
  13226. +
  13227. +#elif AES_BLOCK_SIZE == 24
  13228. +
  13229. +#if defined(ARRAYS)
  13230. +#define locals(y,x) x[6],y[6]
  13231. +#else
  13232. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5, \
  13233. + y##0,y##1,y##2,y##3,y##4,y##5
  13234. +#define b06 unused
  13235. +#define b07 unused
  13236. +#define b16 unused
  13237. +#define b17 unused
  13238. +#endif
  13239. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13240. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  13241. + s(y,4) = s(x,4); s(y,5) = s(x,5);
  13242. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); \
  13243. + si(y,x,k,3); si(y,x,k,4); si(y,x,k,5)
  13244. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); \
  13245. + so(y,x,3); so(y,x,4); so(y,x,5)
  13246. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); \
  13247. + rm(y,x,k,3); rm(y,x,k,4); rm(y,x,k,5)
  13248. +#else
  13249. +
  13250. +#if defined(ARRAYS)
  13251. +#define locals(y,x) x[8],y[8]
  13252. +#else
  13253. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5,x##6,x##7, \
  13254. + y##0,y##1,y##2,y##3,y##4,y##5,y##6,y##7
  13255. +#endif
  13256. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13257. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  13258. + s(y,4) = s(x,4); s(y,5) = s(x,5); \
  13259. + s(y,6) = s(x,6); s(y,7) = s(x,7);
  13260. +
  13261. +#if AES_BLOCK_SIZE == 32
  13262. +
  13263. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3); \
  13264. + si(y,x,k,4); si(y,x,k,5); si(y,x,k,6); si(y,x,k,7)
  13265. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3); \
  13266. + so(y,x,4); so(y,x,5); so(y,x,6); so(y,x,7)
  13267. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3); \
  13268. + rm(y,x,k,4); rm(y,x,k,5); rm(y,x,k,6); rm(y,x,k,7)
  13269. +#else
  13270. +
  13271. +#define state_in(y,x,k) \
  13272. +switch(nc) \
  13273. +{ case 8: si(y,x,k,7); si(y,x,k,6); \
  13274. + case 6: si(y,x,k,5); si(y,x,k,4); \
  13275. + case 4: si(y,x,k,3); si(y,x,k,2); \
  13276. + si(y,x,k,1); si(y,x,k,0); \
  13277. +}
  13278. +
  13279. +#define state_out(y,x) \
  13280. +switch(nc) \
  13281. +{ case 8: so(y,x,7); so(y,x,6); \
  13282. + case 6: so(y,x,5); so(y,x,4); \
  13283. + case 4: so(y,x,3); so(y,x,2); \
  13284. + so(y,x,1); so(y,x,0); \
  13285. +}
  13286. +
  13287. +#if defined(FAST_VARIABLE)
  13288. +
  13289. +#define round(rm,y,x,k) \
  13290. +switch(nc) \
  13291. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  13292. + rm(y,x,k,5); rm(y,x,k,4); \
  13293. + rm(y,x,k,3); rm(y,x,k,2); \
  13294. + rm(y,x,k,1); rm(y,x,k,0); \
  13295. + break; \
  13296. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  13297. + rm(y,x,k,3); rm(y,x,k,2); \
  13298. + rm(y,x,k,1); rm(y,x,k,0); \
  13299. + break; \
  13300. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  13301. + rm(y,x,k,1); rm(y,x,k,0); \
  13302. + break; \
  13303. +}
  13304. +#else
  13305. +
  13306. +#define round(rm,y,x,k) \
  13307. +switch(nc) \
  13308. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  13309. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  13310. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  13311. + rm(y,x,k,1); rm(y,x,k,0); \
  13312. +}
  13313. +
  13314. +#endif
  13315. +
  13316. +#endif
  13317. +#endif
  13318. +
  13319. +void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  13320. +{ u_int32_t locals(b0, b1);
  13321. + const u_int32_t *kp = cx->aes_e_key;
  13322. +
  13323. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  13324. + u_int32_t f2;
  13325. +#endif
  13326. +
  13327. + state_in(b0, in_blk, kp); kp += nc;
  13328. +
  13329. +#if defined(UNROLL)
  13330. +
  13331. + switch(cx->aes_Nrnd)
  13332. + {
  13333. + case 14: round(fwd_rnd, b1, b0, kp );
  13334. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13335. + case 12: round(fwd_rnd, b1, b0, kp );
  13336. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13337. + case 10: round(fwd_rnd, b1, b0, kp );
  13338. + round(fwd_rnd, b0, b1, kp + nc);
  13339. + round(fwd_rnd, b1, b0, kp + 2 * nc);
  13340. + round(fwd_rnd, b0, b1, kp + 3 * nc);
  13341. + round(fwd_rnd, b1, b0, kp + 4 * nc);
  13342. + round(fwd_rnd, b0, b1, kp + 5 * nc);
  13343. + round(fwd_rnd, b1, b0, kp + 6 * nc);
  13344. + round(fwd_rnd, b0, b1, kp + 7 * nc);
  13345. + round(fwd_rnd, b1, b0, kp + 8 * nc);
  13346. + round(fwd_lrnd, b0, b1, kp + 9 * nc);
  13347. + }
  13348. +
  13349. +#elif defined(PARTIAL_UNROLL)
  13350. + { u_int32_t rnd;
  13351. +
  13352. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  13353. + {
  13354. + round(fwd_rnd, b1, b0, kp);
  13355. + round(fwd_rnd, b0, b1, kp + nc); kp += 2 * nc;
  13356. + }
  13357. +
  13358. + round(fwd_rnd, b1, b0, kp);
  13359. + round(fwd_lrnd, b0, b1, kp + nc);
  13360. + }
  13361. +#else
  13362. + { u_int32_t rnd;
  13363. +
  13364. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  13365. + {
  13366. + round(fwd_rnd, b1, b0, kp);
  13367. + l_copy(b0, b1); kp += nc;
  13368. + }
  13369. +
  13370. + round(fwd_lrnd, b0, b1, kp);
  13371. + }
  13372. +#endif
  13373. +
  13374. + state_out(out_blk, b0);
  13375. +}
  13376. +
  13377. +void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  13378. +{ u_int32_t locals(b0, b1);
  13379. + const u_int32_t *kp = cx->aes_d_key;
  13380. +
  13381. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  13382. + u_int32_t f2, f4, f8, f9;
  13383. +#endif
  13384. +
  13385. + state_in(b0, in_blk, kp); kp += nc;
  13386. +
  13387. +#if defined(UNROLL)
  13388. +
  13389. + switch(cx->aes_Nrnd)
  13390. + {
  13391. + case 14: round(inv_rnd, b1, b0, kp );
  13392. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13393. + case 12: round(inv_rnd, b1, b0, kp );
  13394. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13395. + case 10: round(inv_rnd, b1, b0, kp );
  13396. + round(inv_rnd, b0, b1, kp + nc);
  13397. + round(inv_rnd, b1, b0, kp + 2 * nc);
  13398. + round(inv_rnd, b0, b1, kp + 3 * nc);
  13399. + round(inv_rnd, b1, b0, kp + 4 * nc);
  13400. + round(inv_rnd, b0, b1, kp + 5 * nc);
  13401. + round(inv_rnd, b1, b0, kp + 6 * nc);
  13402. + round(inv_rnd, b0, b1, kp + 7 * nc);
  13403. + round(inv_rnd, b1, b0, kp + 8 * nc);
  13404. + round(inv_lrnd, b0, b1, kp + 9 * nc);
  13405. + }
  13406. +
  13407. +#elif defined(PARTIAL_UNROLL)
  13408. + { u_int32_t rnd;
  13409. +
  13410. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  13411. + {
  13412. + round(inv_rnd, b1, b0, kp);
  13413. + round(inv_rnd, b0, b1, kp + nc); kp += 2 * nc;
  13414. + }
  13415. +
  13416. + round(inv_rnd, b1, b0, kp);
  13417. + round(inv_lrnd, b0, b1, kp + nc);
  13418. + }
  13419. +#else
  13420. + { u_int32_t rnd;
  13421. +
  13422. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  13423. + {
  13424. + round(inv_rnd, b1, b0, kp);
  13425. + l_copy(b0, b1); kp += nc;
  13426. + }
  13427. +
  13428. + round(inv_lrnd, b0, b1, kp);
  13429. + }
  13430. +#endif
  13431. +
  13432. + state_out(out_blk, b0);
  13433. +}
  13434. --- /dev/null Tue Mar 11 13:02:56 2003
  13435. +++ linux/net/ipsec/aes/aes_cbc.c Mon Feb 9 13:51:03 2004
  13436. @@ -0,0 +1,46 @@
  13437. +/*
  13438. +// I retain copyright in this code but I encourage its free use provided
  13439. +// that I don't carry any responsibility for the results. I am especially
  13440. +// happy to see it used in free and open source software. If you do use
  13441. +// it I would appreciate an acknowledgement of its origin in the code or
  13442. +// the product that results and I would also appreciate knowing a little
  13443. +// about the use to which it is being put. I am grateful to Frank Yellin
  13444. +// for some ideas that are used in this implementation.
  13445. +//
  13446. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  13447. +//
  13448. +// This is an implementation of the AES encryption algorithm (Rijndael)
  13449. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  13450. +// to provide both fixed and dynamic block and key lengths and can also
  13451. +// run with either big or little endian internal byte order (see aes.h).
  13452. +// It inputs block and key lengths in bytes with the legal values being
  13453. +// 16, 24 and 32.
  13454. +*
  13455. +*/
  13456. +
  13457. +#ifdef __KERNEL__
  13458. +#include <linux/types.h>
  13459. +#else
  13460. +#include <sys/types.h>
  13461. +#endif
  13462. +#include "crypto/aes_cbc.h"
  13463. +#include "crypto/cbc_generic.h"
  13464. +
  13465. +/* returns bool success */
  13466. +int AES_set_key(aes_context *aes_ctx, const u_int8_t *key, int keysize) {
  13467. + aes_set_key(aes_ctx, key, keysize, 0);
  13468. + return 1;
  13469. +}
  13470. +CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
  13471. +
  13472. +
  13473. +/*
  13474. + * $Log: aes_cbc.c,v $
  13475. + * Revision 1.2 2004/07/10 07:48:40 mcr
  13476. + * Moved from linux/crypto/ciphers/aes/aes_cbc.c,v
  13477. + *
  13478. + * Revision 1.1 2004/04/06 02:48:12 mcr
  13479. + * pullup of AES cipher from alg-branch.
  13480. + *
  13481. + *
  13482. + */
  13483. --- /dev/null Tue Mar 11 13:02:56 2003
  13484. +++ linux/net/ipsec/aes/aes_xcbc_mac.c Mon Feb 9 13:51:03 2004
  13485. @@ -0,0 +1,67 @@
  13486. +#ifdef __KERNEL__
  13487. +#include <linux/types.h>
  13488. +#include <linux/kernel.h>
  13489. +#define DEBUG(x)
  13490. +#else
  13491. +#include <stdio.h>
  13492. +#include <sys/types.h>
  13493. +#define DEBUG(x) x
  13494. +#endif
  13495. +
  13496. +#include "crypto/aes.h"
  13497. +#include "crypto/aes_xcbc_mac.h"
  13498. +
  13499. +int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen)
  13500. +{
  13501. + int ret=1;
  13502. + aes_block kn[3] = {
  13503. + { 0x01010101, 0x01010101, 0x01010101, 0x01010101 },
  13504. + { 0x02020202, 0x02020202, 0x02020202, 0x02020202 },
  13505. + { 0x03030303, 0x03030303, 0x03030303, 0x03030303 },
  13506. + };
  13507. + aes_set_key(&ctxm->ctx_k1, key, keylen, 0);
  13508. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[0], (u_int8_t *) kn[0]);
  13509. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[1], (u_int8_t *) ctxm->k2);
  13510. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[2], (u_int8_t *) ctxm->k3);
  13511. + aes_set_key(&ctxm->ctx_k1, (u_int8_t *) kn[0], 16, 0);
  13512. + return ret;
  13513. +}
  13514. +static void do_pad_xor(u_int8_t *out, const u_int8_t *in, int len) {
  13515. + int pos=0;
  13516. + for (pos=1; pos <= 16; pos++, in++, out++) {
  13517. + if (pos <= len)
  13518. + *out ^= *in;
  13519. + if (pos > len) {
  13520. + DEBUG(printf("put 0x80 at pos=%d\n", pos));
  13521. + *out ^= 0x80;
  13522. + break;
  13523. + }
  13524. + }
  13525. +}
  13526. +static void xor_block(aes_block res, const aes_block op) {
  13527. + res[0] ^= op[0];
  13528. + res[1] ^= op[1];
  13529. + res[2] ^= op[2];
  13530. + res[3] ^= op[3];
  13531. +}
  13532. +int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]) {
  13533. + int ret=ilen;
  13534. + u_int32_t out[4] = { 0, 0, 0, 0 };
  13535. + for (; ilen > 16 ; ilen-=16) {
  13536. + xor_block(out, (const u_int32_t*) &in[0]);
  13537. + aes_encrypt(&ctxm->ctx_k1, in, (u_int8_t *)&out[0]);
  13538. + in+=16;
  13539. + }
  13540. + do_pad_xor((u_int8_t *)&out, in, ilen);
  13541. + if (ilen==16) {
  13542. + DEBUG(printf("using k3\n"));
  13543. + xor_block(out, ctxm->k3);
  13544. + }
  13545. + else
  13546. + {
  13547. + DEBUG(printf("using k2\n"));
  13548. + xor_block(out, ctxm->k2);
  13549. + }
  13550. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *)out, hash);
  13551. + return ret;
  13552. +}
  13553. --- /dev/null Tue Mar 11 13:02:56 2003
  13554. +++ linux/net/ipsec/aes/ipsec_alg_aes.c Mon Feb 9 13:51:03 2004
  13555. @@ -0,0 +1,296 @@
  13556. +/*
  13557. + * ipsec_alg AES cipher stubs
  13558. + *
  13559. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  13560. + *
  13561. + * ipsec_alg_aes.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  13562. + *
  13563. + * This program is free software; you can redistribute it and/or modify it
  13564. + * under the terms of the GNU General Public License as published by the
  13565. + * Free Software Foundation; either version 2 of the License, or (at your
  13566. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  13567. + *
  13568. + * This program is distributed in the hope that it will be useful, but
  13569. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13570. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13571. + * for more details.
  13572. + *
  13573. + * Fixes by:
  13574. + * PK: Pawel Krawczyk <kravietz@aba.krakow.pl>
  13575. + * Fixes list:
  13576. + * PK: make XCBC comply with latest draft (keylength)
  13577. + *
  13578. + */
  13579. +#ifndef AUTOCONF_INCLUDED
  13580. +#include <linux/config.h>
  13581. +#endif
  13582. +#include <linux/version.h>
  13583. +
  13584. +/*
  13585. + * special case: ipsec core modular with this static algo inside:
  13586. + * must avoid MODULE magic for this file
  13587. + */
  13588. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_AES)
  13589. +#undef MODULE
  13590. +#endif
  13591. +
  13592. +#include <linux/module.h>
  13593. +#include <linux/init.h>
  13594. +
  13595. +#include <linux/kernel.h> /* printk() */
  13596. +#include <linux/errno.h> /* error codes */
  13597. +#include <linux/types.h> /* size_t */
  13598. +#include <linux/string.h>
  13599. +
  13600. +/* Check if __exit is defined, if not null it */
  13601. +#ifndef __exit
  13602. +#define __exit
  13603. +#endif
  13604. +
  13605. +/* Low freeswan header coupling */
  13606. +#include "openswan/ipsec_alg.h"
  13607. +#include "crypto/aes_cbc.h"
  13608. +
  13609. +#define CONFIG_KLIPS_ENC_AES_MAC 1
  13610. +
  13611. +#define AES_CONTEXT_T aes_context
  13612. +static int debug_aes=0;
  13613. +static int test_aes=0;
  13614. +static int excl_aes=0;
  13615. +static int keyminbits=0;
  13616. +static int keymaxbits=0;
  13617. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13618. +MODULE_AUTHOR("JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>");
  13619. +#ifdef module_param
  13620. +module_param(debug_aes,int,0600)
  13621. +module_param(test_aes,int,0600)
  13622. +module_param(excl_aes,int,0600)
  13623. +module_param(keyminbits,int,0600)
  13624. +module_param(keymaxbits,int,0600)
  13625. +#else
  13626. +MODULE_PARM(debug_aes, "i");
  13627. +MODULE_PARM(test_aes, "i");
  13628. +MODULE_PARM(excl_aes, "i");
  13629. +MODULE_PARM(keyminbits, "i");
  13630. +MODULE_PARM(keymaxbits, "i");
  13631. +#endif
  13632. +#endif
  13633. +
  13634. +#if CONFIG_KLIPS_ENC_AES_MAC
  13635. +#include "crypto/aes_xcbc_mac.h"
  13636. +
  13637. +/*
  13638. + * Not IANA number yet (draft-ietf-ipsec-ciph-aes-xcbc-mac-00.txt).
  13639. + * We use 9 for non-modular algorithm and none for modular, thus
  13640. + * forcing user to specify one on module load. -kravietz
  13641. + */
  13642. +#ifdef MODULE
  13643. +static int auth_id=0;
  13644. +#else
  13645. +static int auth_id=9;
  13646. +#endif
  13647. +#ifdef module_param
  13648. +module_param(auth_id, int, 0600);
  13649. +#else
  13650. +MODULE_PARM(auth_id, "i");
  13651. +#endif
  13652. +#endif
  13653. +
  13654. +#define ESP_AES 12 /* truely _constant_ :) */
  13655. +
  13656. +/* 128, 192 or 256 */
  13657. +#define ESP_AES_KEY_SZ_MIN 16 /* 128 bit secret key */
  13658. +#define ESP_AES_KEY_SZ_MAX 32 /* 256 bit secret key */
  13659. +#define ESP_AES_CBC_BLK_LEN 16 /* AES-CBC block size */
  13660. +
  13661. +/* Values according to draft-ietf-ipsec-ciph-aes-xcbc-mac-02.txt
  13662. + * -kravietz
  13663. + */
  13664. +#define ESP_AES_MAC_KEY_SZ 16 /* 128 bit MAC key */
  13665. +#define ESP_AES_MAC_BLK_LEN 16 /* 128 bit block */
  13666. +
  13667. +static int _aes_set_key(struct ipsec_alg_enc *alg,
  13668. + __u8 * key_e, const __u8 * key,
  13669. + size_t keysize)
  13670. +{
  13671. + int ret;
  13672. + AES_CONTEXT_T *ctx=(AES_CONTEXT_T*)key_e;
  13673. + ret=AES_set_key(ctx, key, keysize)!=0? 0: -EINVAL;
  13674. + if (debug_aes > 0)
  13675. + printk(KERN_DEBUG "klips_debug:_aes_set_key:"
  13676. + "ret=%d key_e=%p key=%p keysize=%ld\n",
  13677. + ret, key_e, key, (unsigned long int) keysize);
  13678. + return ret;
  13679. +}
  13680. +
  13681. +static int _aes_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e,
  13682. + __u8 * in, int ilen, const __u8 * iv,
  13683. + int encrypt)
  13684. +{
  13685. + AES_CONTEXT_T *ctx=(AES_CONTEXT_T*)key_e;
  13686. + if (debug_aes > 0)
  13687. + printk(KERN_DEBUG "klips_debug:_aes_cbc_encrypt:"
  13688. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  13689. + key_e, in, ilen, iv, encrypt);
  13690. + return AES_cbc_encrypt(ctx, in, in, ilen, iv, encrypt);
  13691. +}
  13692. +#if CONFIG_KLIPS_ENC_AES_MAC
  13693. +static int _aes_mac_set_key(struct ipsec_alg_auth *alg, __u8 * key_a, const __u8 * key, int keylen) {
  13694. + aes_context_mac *ctxm=(aes_context_mac *)key_a;
  13695. + return AES_xcbc_mac_set_key(ctxm, key, keylen)? 0 : -EINVAL;
  13696. +}
  13697. +static int _aes_mac_hash(struct ipsec_alg_auth *alg, __u8 * key_a, const __u8 * dat, int len, __u8 * hash, int hashlen) {
  13698. + int ret;
  13699. + char hash_buf[16];
  13700. + aes_context_mac *ctxm=(aes_context_mac *)key_a;
  13701. + ret=AES_xcbc_mac_hash(ctxm, dat, len, hash_buf);
  13702. + memcpy(hash, hash_buf, hashlen);
  13703. + return ret;
  13704. +}
  13705. +static struct ipsec_alg_auth ipsec_alg_AES_MAC = {
  13706. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  13707. + ixt_refcnt: ATOMIC_INIT(0),
  13708. + ixt_name: "aes_mac",
  13709. + ixt_blocksize: ESP_AES_MAC_BLK_LEN,
  13710. + ixt_support: {
  13711. + ias_exttype: IPSEC_ALG_TYPE_AUTH,
  13712. + ias_id: 0,
  13713. + ias_keyminbits: ESP_AES_MAC_KEY_SZ*8,
  13714. + ias_keymaxbits: ESP_AES_MAC_KEY_SZ*8,
  13715. + },
  13716. + },
  13717. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13718. + ixt_module: THIS_MODULE,
  13719. +#endif
  13720. + ixt_a_keylen: ESP_AES_MAC_KEY_SZ,
  13721. + ixt_a_ctx_size: sizeof(aes_context_mac),
  13722. + ixt_a_hmac_set_key: _aes_mac_set_key,
  13723. + ixt_a_hmac_hash:_aes_mac_hash,
  13724. +};
  13725. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13726. +static struct ipsec_alg_enc ipsec_alg_AES = {
  13727. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  13728. + ixt_refcnt: ATOMIC_INIT(0),
  13729. + ixt_name: "aes",
  13730. + ixt_blocksize: ESP_AES_CBC_BLK_LEN,
  13731. + ixt_support: {
  13732. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  13733. + ias_id: ESP_AES,
  13734. + ias_keyminbits: ESP_AES_KEY_SZ_MIN*8,
  13735. + ias_keymaxbits: ESP_AES_KEY_SZ_MAX*8,
  13736. + },
  13737. + },
  13738. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13739. + ixt_module: THIS_MODULE,
  13740. +#endif
  13741. + ixt_e_keylen: ESP_AES_KEY_SZ_MAX,
  13742. + ixt_e_ctx_size: sizeof(AES_CONTEXT_T),
  13743. + ixt_e_set_key: _aes_set_key,
  13744. + ixt_e_cbc_encrypt:_aes_cbc_encrypt,
  13745. +};
  13746. +
  13747. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13748. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_aes_init )
  13749. +#else
  13750. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_aes_init )
  13751. +#endif
  13752. +{
  13753. + int ret, test_ret;
  13754. +
  13755. + if (keyminbits)
  13756. + ipsec_alg_AES.ixt_common.ixt_support.ias_keyminbits=keyminbits;
  13757. + if (keymaxbits) {
  13758. + ipsec_alg_AES.ixt_common.ixt_support.ias_keymaxbits=keymaxbits;
  13759. + if (keymaxbits*8>ipsec_alg_AES.ixt_common.ixt_support.ias_keymaxbits)
  13760. + ipsec_alg_AES.ixt_e_keylen=keymaxbits*8;
  13761. + }
  13762. + if (excl_aes) ipsec_alg_AES.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  13763. + ret=register_ipsec_alg_enc(&ipsec_alg_AES);
  13764. + printk("ipsec_aes_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  13765. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype,
  13766. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13767. + ipsec_alg_AES.ixt_common.ixt_name,
  13768. + ret);
  13769. + if (ret==0 && test_aes) {
  13770. + test_ret=ipsec_alg_test(
  13771. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype ,
  13772. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13773. + test_aes);
  13774. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  13775. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype ,
  13776. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13777. + test_ret);
  13778. + }
  13779. +#if CONFIG_KLIPS_ENC_AES_MAC
  13780. + if (auth_id!=0){
  13781. + int ret;
  13782. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id=auth_id;
  13783. + ret=register_ipsec_alg_auth(&ipsec_alg_AES_MAC);
  13784. + printk("ipsec_aes_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  13785. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13786. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13787. + ipsec_alg_AES_MAC.ixt_common.ixt_name,
  13788. + ret);
  13789. + if (ret==0 && test_aes) {
  13790. + test_ret=ipsec_alg_test(
  13791. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13792. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13793. + test_aes);
  13794. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  13795. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13796. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13797. + test_ret);
  13798. + }
  13799. + } else {
  13800. + printk(KERN_DEBUG "klips_debug: experimental ipsec_alg_AES_MAC not registered [Ok] (auth_id=%d)\n", auth_id);
  13801. + }
  13802. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13803. + return ret;
  13804. +}
  13805. +
  13806. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13807. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_aes_fini )
  13808. +#else
  13809. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_aes_fini )
  13810. +#endif
  13811. +{
  13812. +#if CONFIG_KLIPS_ENC_AES_MAC
  13813. + if (auth_id) unregister_ipsec_alg_auth(&ipsec_alg_AES_MAC);
  13814. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13815. + unregister_ipsec_alg_enc(&ipsec_alg_AES);
  13816. + return;
  13817. +}
  13818. +#ifdef MODULE_LICENSE
  13819. +MODULE_LICENSE("GPL");
  13820. +#endif
  13821. +
  13822. +#if 0 /* +NOT_YET */
  13823. +#ifndef MODULE
  13824. +/*
  13825. + * This is intended for static module setups, currently
  13826. + * doesn't work for modular ipsec.o with static algos inside
  13827. + */
  13828. +static int setup_keybits(const char *str)
  13829. +{
  13830. + unsigned aux;
  13831. + char *end;
  13832. +
  13833. + aux = simple_strtoul(str,&end,0);
  13834. + if (aux != 128 && aux != 192 && aux != 256)
  13835. + return 0;
  13836. + keyminbits = aux;
  13837. +
  13838. + if (*end == 0 || *end != ',')
  13839. + return 1;
  13840. + str=end+1;
  13841. + aux = simple_strtoul(str, NULL, 0);
  13842. + if (aux != 128 && aux != 192 && aux != 256)
  13843. + return 0;
  13844. + if (aux >= keyminbits)
  13845. + keymaxbits = aux;
  13846. + return 1;
  13847. +}
  13848. +__setup("ipsec_aes_keybits=", setup_keybits);
  13849. +#endif
  13850. +#endif
  13851. +
  13852. --- /dev/null Tue Mar 11 13:02:56 2003
  13853. +++ linux/net/ipsec/alg/Config.alg_aes.in Mon Feb 9 13:51:03 2004
  13854. @@ -0,0 +1,3 @@
  13855. +if [ "$CONFIG_IPSEC_ALG" = "y" ]; then
  13856. + tristate ' AES encryption algorithm' CONFIG_IPSEC_ENC_AES
  13857. +fi
  13858. --- /dev/null Tue Mar 11 13:02:56 2003
  13859. +++ linux/net/ipsec/alg/Config.alg_cryptoapi.in Mon Feb 9 13:51:03 2004
  13860. @@ -0,0 +1,6 @@
  13861. +if [ "$CONFIG_IPSEC_ALG" = "y" ]; then
  13862. + dep_tristate ' CRYPTOAPI ciphers support (needs cryptoapi patch)' CONFIG_IPSEC_ALG_CRYPTOAPI $CONFIG_CRYPTO
  13863. + if [ "$CONFIG_IPSEC_ALG_CRYPTOAPI" != "n" ]; then
  13864. + bool ' CRYPTOAPI proprietary ciphers ' CONFIG_IPSEC_ALG_NON_LIBRE
  13865. + fi
  13866. +fi
  13867. --- /dev/null Tue Mar 11 13:02:56 2003
  13868. +++ linux/net/ipsec/alg/Config.in Mon Feb 9 13:51:03 2004
  13869. @@ -0,0 +1,3 @@
  13870. +#Placeholder
  13871. +source net/ipsec/alg/Config.alg_aes.in
  13872. +source net/ipsec/alg/Config.alg_cryptoapi.in
  13873. --- /dev/null Tue Mar 11 13:02:56 2003
  13874. +++ linux/net/ipsec/alg/Makefile Mon Feb 9 13:51:03 2004
  13875. @@ -0,0 +1,112 @@
  13876. +# Makefile,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  13877. +ifeq ($(strip $(KLIPSMODULE)),)
  13878. +FREESWANSRCDIR=.
  13879. +else
  13880. +FREESWANSRCDIR=../../../..
  13881. +endif
  13882. +ifeq ($(strip $(KLIPS_TOP)),)
  13883. +KLIPS_TOP=../../..
  13884. +override EXTRA_CFLAGS += -I$(KLIPS_TOP)/include
  13885. +endif
  13886. +
  13887. +ifeq ($(CONFIG_IPSEC_DEBUG),y)
  13888. +override EXTRA_CFLAGS += -g
  13889. +endif
  13890. +
  13891. +# LIBCRYPTO normally comes as an argument from "parent" Makefile
  13892. +# (this applies both to FS' "make module" and eg. Linux' "make modules"
  13893. +# But make dep doest follow same evaluations, so we need this default:
  13894. +LIBCRYPTO=$(TOPDIR)/lib/libcrypto
  13895. +
  13896. +override EXTRA_CFLAGS += -I$(LIBCRYPTO)/include
  13897. +override EXTRA_CFLAGS += -Wall -Wpointer-arith -Wstrict-prototypes
  13898. +
  13899. +MOD_LIST_NAME := NET_MISC_MODULES
  13900. +
  13901. +#O_TARGET := static_init.o
  13902. +
  13903. +subdir- :=
  13904. +subdir-n :=
  13905. +subdir-y :=
  13906. +subdir-m :=
  13907. +
  13908. +obj-y := static_init.o
  13909. +
  13910. +ARCH_ASM-y :=
  13911. +ARCH_ASM-$(CONFIG_M586) := i586
  13912. +ARCH_ASM-$(CONFIG_M586TSC) := i586
  13913. +ARCH_ASM-$(CONFIG_M586MMX) := i586
  13914. +ARCH_ASM-$(CONFIG_MK6) := i586
  13915. +ARCH_ASM-$(CONFIG_M686) := i686
  13916. +ARCH_ASM-$(CONFIG_MPENTIUMIII) := i686
  13917. +ARCH_ASM-$(CONFIG_MPENTIUM4) := i686
  13918. +ARCH_ASM-$(CONFIG_MK7) := i686
  13919. +ARCH_ASM-$(CONFIG_MCRUSOE) := i586
  13920. +ARCH_ASM-$(CONFIG_MWINCHIPC6) := i586
  13921. +ARCH_ASM-$(CONFIG_MWINCHIP2) := i586
  13922. +ARCH_ASM-$(CONFIG_MWINCHIP3D) := i586
  13923. +ARCH_ASM-$(CONFIG_USERMODE) := i586
  13924. +
  13925. +ARCH_ASM :=$(ARCH_ASM-y)
  13926. +ifdef NO_ASM
  13927. +ARCH_ASM :=
  13928. +endif
  13929. +
  13930. +# The algorithm makefiles may put dependences, short-circuit them
  13931. +null:
  13932. +
  13933. +makefiles=$(filter-out %.preipsec, $(wildcard Makefile.alg_*))
  13934. +ifneq ($(makefiles),)
  13935. +#include Makefile.alg_aes
  13936. +#include Makefile.alg_aes-opt
  13937. +include $(makefiles)
  13938. +endif
  13939. +
  13940. +# These rules translate from new to old makefile rules
  13941. +# Translate to Rules.make lists.
  13942. +multi-used := $(filter $(list-multi), $(obj-y) $(obj-m))
  13943. +multi-objs := $(foreach m, $(multi-used), $($(basename $(m))-objs))
  13944. +active-objs := $(sort $(multi-objs) $(obj-y) $(obj-m))
  13945. +O_OBJS := $(obj-y)
  13946. +M_OBJS := $(obj-m)
  13947. +MIX_OBJS := $(filter $(export-objs), $(active-objs))
  13948. +#OX_OBJS := $(export-objs)
  13949. +SUB_DIRS := $(subdir-y)
  13950. +ALL_SUB_DIRS := $(subdir-y) $(subdir-m)
  13951. +MOD_SUB_DIRS := $(subdir-m)
  13952. +
  13953. +
  13954. +static_init_mod.o: $(obj-y)
  13955. + rm -f $@
  13956. + $(LD) $(LD_EXTRAFLAGS) $(obj-y) -r -o $@
  13957. +
  13958. +perlasm: ../../../crypto/ciphers/des/asm/perlasm
  13959. + ln -sf $? $@
  13960. +
  13961. +$(obj-y) $(obj-m): $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h $(KLIPS_TOP)/include/freeswan/ipsec_alg.h
  13962. +$(alg_obj-y) $(alg_obj-m): perlasm $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h $(KLIPS_TOP)/include/freeswan/ipsec_alg.h
  13963. +
  13964. +
  13965. +all_alg_modules: perlasm $(ALG_MODULES)
  13966. + @echo "ALG_MODULES=$(ALG_MODULES)"
  13967. +
  13968. +
  13969. +#
  13970. +# Construct alg. init. function: call ipsec_ALGO_init() for every static algo
  13971. +# Needed when there are static algos (with static or modular ipsec.o)
  13972. +#
  13973. +static_init.c: $(TOPDIR)/include/linux/autoconf.h Makefile $(makefiles) scripts/mk-static_init.c.sh
  13974. + @echo "Re-creating $@"
  13975. + $(SHELL) scripts/mk-static_init.c.sh $(static_init-func-y) > $@
  13976. +
  13977. +clean:
  13978. + @for i in $(ALG_SUBDIRS);do test -d $$i && make -C $$i clean;done;exit 0
  13979. + @find . -type l -exec rm -f {} \;
  13980. + -rm -f perlasm
  13981. + -rm -rf $(ALG_SUBDIRS)
  13982. + -rm -f *.o static_init.c
  13983. +
  13984. +ifdef TOPDIR
  13985. +include $(TOPDIR)/Rules.make
  13986. +endif
  13987. +
  13988. --- /dev/null Tue Mar 11 13:02:56 2003
  13989. +++ linux/net/ipsec/alg/Makefile.alg_aes Mon Feb 9 13:51:03 2004
  13990. @@ -0,0 +1,18 @@
  13991. +MOD_AES := ipsec_aes.o
  13992. +
  13993. +ALG_MODULES += $(MOD_AES)
  13994. +ALG_SUBDIRS += libaes
  13995. +
  13996. +obj-$(CONFIG_IPSEC_ALG_AES) += $(MOD_AES)
  13997. +static_init-func-$(CONFIG_IPSEC_ALG_AES)+= ipsec_aes_init
  13998. +alg_obj-$(CONFIG_IPSEC_ALG_AES) += ipsec_alg_aes.o
  13999. +
  14000. +AES_OBJS := ipsec_alg_aes.o $(LIBCRYPTO)/libaes/libaes.a
  14001. +
  14002. +
  14003. +$(MOD_AES): $(AES_OBJS)
  14004. + $(LD) $(EXTRA_LDFLAGS) -r $(AES_OBJS) -o $@
  14005. +
  14006. +$(LIBCRYPTO)/libaes/libaes.a:
  14007. + $(MAKE) -C $(LIBCRYPTO)/libaes CC='$(CC)' 'ARCH_ASM=$(ARCH_ASM)' CFLAGS='$(CFLAGS) $(EXTRA_CFLAGS)' libaes.a
  14008. +
  14009. --- /dev/null Tue Mar 11 13:02:56 2003
  14010. +++ linux/net/ipsec/alg/Makefile.alg_cryptoapi Mon Feb 9 13:51:03 2004
  14011. @@ -0,0 +1,14 @@
  14012. +MOD_CRYPTOAPI := ipsec_cryptoapi.o
  14013. +
  14014. +ifneq ($(wildcard $(TOPDIR)/include/linux/crypto.h),)
  14015. +ALG_MODULES += $(MOD_CRYPTOAPI)
  14016. +obj-$(CONFIG_IPSEC_ALG_CRYPTOAPI) += $(MOD_CRYPTOAPI)
  14017. +static_init-func-$(CONFIG_IPSEC_ALG_CRYPTOAPI)+= ipsec_cryptoapi_init
  14018. +alg_obj-$(CONFIG_IPSEC_ALG_CRYPTOAPI) += ipsec_alg_cryptoapi.o
  14019. +else
  14020. +$(warning "Linux CryptoAPI (2.4.22+ or 2.6.x) not found, not building ipsec_cryptoapi.o")
  14021. +endif
  14022. +
  14023. +CRYPTOAPI_OBJS := ipsec_alg_cryptoapi.o
  14024. +$(MOD_CRYPTOAPI): $(CRYPTOAPI_OBJS)
  14025. + $(LD) -r $(CRYPTOAPI_OBJS) -o $@
  14026. --- /dev/null Tue Mar 11 13:02:56 2003
  14027. +++ linux/net/ipsec/alg/ipsec_alg_cryptoapi.c Mon Feb 9 13:51:03 2004
  14028. @@ -0,0 +1,442 @@
  14029. +/*
  14030. + * ipsec_alg to linux cryptoapi GLUE
  14031. + *
  14032. + * Authors: CODE.ar TEAM
  14033. + * Harpo MAxx <harpo@linuxmendoza.org.ar>
  14034. + * JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  14035. + * Luciano Ruete <docemeses@softhome.net>
  14036. + *
  14037. + * ipsec_alg_cryptoapi.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  14038. + *
  14039. + * This program is free software; you can redistribute it and/or modify it
  14040. + * under the terms of the GNU General Public License as published by the
  14041. + * Free Software Foundation; either version 2 of the License, or (at your
  14042. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  14043. + *
  14044. + * This program is distributed in the hope that it will be useful, but
  14045. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14046. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14047. + * for more details.
  14048. + *
  14049. + * Example usage:
  14050. + * modinfo -p ipsec_cryptoapi (quite useful info, including supported algos)
  14051. + * modprobe ipsec_cryptoapi
  14052. + * modprobe ipsec_cryptoapi test=1
  14053. + * modprobe ipsec_cryptoapi excl=1 (exclusive cipher/algo)
  14054. + * modprobe ipsec_cryptoapi noauto=1 aes=1 twofish=1 (only these ciphers)
  14055. + * modprobe ipsec_cryptoapi aes=128,128 (force these keylens)
  14056. + * modprobe ipsec_cryptoapi des_ede3=0 (everything but 3DES)
  14057. + */
  14058. +#ifndef AUTOCONF_INCLUDED
  14059. +#include <linux/config.h>
  14060. +#endif
  14061. +#include <linux/version.h>
  14062. +
  14063. +/*
  14064. + * special case: ipsec core modular with this static algo inside:
  14065. + * must avoid MODULE magic for this file
  14066. + */
  14067. +#if CONFIG_IPSEC_MODULE && CONFIG_IPSEC_ALG_CRYPTOAPI
  14068. +#undef MODULE
  14069. +#endif
  14070. +
  14071. +#include <linux/module.h>
  14072. +#include <linux/init.h>
  14073. +
  14074. +#include <linux/kernel.h> /* printk() */
  14075. +#include <linux/errno.h> /* error codes */
  14076. +#include <linux/types.h> /* size_t */
  14077. +#include <linux/string.h>
  14078. +
  14079. +/* Check if __exit is defined, if not null it */
  14080. +#ifndef __exit
  14081. +#define __exit
  14082. +#endif
  14083. +
  14084. +/* warn the innocent */
  14085. +#if !defined (CONFIG_CRYPTO) && !defined (CONFIG_CRYPTO_MODULE)
  14086. +#warning "No linux CryptoAPI found, install 2.4.22+ or 2.6.x"
  14087. +#define NO_CRYPTOAPI_SUPPORT
  14088. +#endif
  14089. +/* Low freeswan header coupling */
  14090. +#include "openswan/ipsec_alg.h"
  14091. +
  14092. +#include <linux/crypto.h>
  14093. +#ifdef CRYPTO_API_VERSION_CODE
  14094. +#warning "Old CryptoAPI is not supported. Only linux-2.4.22+ or linux-2.6.x are supported"
  14095. +#define NO_CRYPTOAPI_SUPPORT
  14096. +#endif
  14097. +
  14098. +#ifdef NO_CRYPTOAPI_SUPPORT
  14099. +#warning "Building an unusable module :P"
  14100. +/* Catch old CryptoAPI by not allowing module to load */
  14101. +IPSEC_ALG_MODULE_INIT( ipsec_cryptoapi_init )
  14102. +{
  14103. + printk(KERN_WARNING "ipsec_cryptoapi.o was not built on stock Linux CryptoAPI (2.4.22+ or 2.6.x), not loading.\n");
  14104. + return -EINVAL;
  14105. +}
  14106. +#else
  14107. +#include <asm/scatterlist.h>
  14108. +#include <asm/pgtable.h>
  14109. +#include <linux/mm.h>
  14110. +
  14111. +#define CIPHERNAME_AES "aes"
  14112. +#define CIPHERNAME_3DES "des3_ede"
  14113. +#define CIPHERNAME_BLOWFISH "blowfish"
  14114. +#define CIPHERNAME_CAST "cast5"
  14115. +#define CIPHERNAME_SERPENT "serpent"
  14116. +#define CIPHERNAME_TWOFISH "twofish"
  14117. +
  14118. +#define ESP_3DES 3
  14119. +#define ESP_AES 12
  14120. +#define ESP_BLOWFISH 7 /* truely _constant_ :) */
  14121. +#define ESP_CAST 6 /* quite constant :) */
  14122. +#define ESP_SERPENT 252 /* from ipsec drafts */
  14123. +#define ESP_TWOFISH 253 /* from ipsec drafts */
  14124. +
  14125. +#define AH_MD5 2
  14126. +#define AH_SHA 3
  14127. +#define DIGESTNAME_MD5 "md5"
  14128. +#define DIGESTNAME_SHA1 "sha1"
  14129. +
  14130. +MODULE_AUTHOR("Juanjo Ciarlante, Harpo MAxx, Luciano Ruete");
  14131. +static int debug=0;
  14132. +static int test=0;
  14133. +static int excl=0;
  14134. +static int noauto = 0;
  14135. +
  14136. +static int des_ede3[] = {-1, -1};
  14137. +static int aes[] = {-1, -1};
  14138. +static int blowfish[] = {-1, -1};
  14139. +static int cast[] = {-1, -1};
  14140. +static int serpent[] = {-1, -1};
  14141. +static int twofish[] = {-1, -1};
  14142. +
  14143. +#ifdef module_param
  14144. +module_param(debug,int,0600);
  14145. +module_param(test,int,0600);
  14146. +module_param(ebug,int,0600);
  14147. +
  14148. +module_param(noauto,int,0600);
  14149. +module_param(ebug,int,0600);
  14150. +
  14151. +module_param_array(des_ede3,int,NULL,0);
  14152. +module_param(aes,int,NULL,0);
  14153. +module_param(blowfish,int,NULL,0);
  14154. +module_param(cast,int,NULL,0);
  14155. +module_param(serpent,int,NULL,0);
  14156. +module_param(twofish,int,NULL,0);
  14157. +#else
  14158. +MODULE_PARM(debug, "i");
  14159. +MODULE_PARM(test, "i");
  14160. +MODULE_PARM(excl, "i");
  14161. +
  14162. +MODULE_PARM(noauto,"i");
  14163. +
  14164. +MODULE_PARM(des_ede3,"1-2i");
  14165. +MODULE_PARM(aes,"1-2i");
  14166. +MODULE_PARM(blowfish,"1-2i");
  14167. +MODULE_PARM(cast,"1-2i");
  14168. +MODULE_PARM(serpent,"1-2i");
  14169. +MODULE_PARM(twofish,"1-2i");
  14170. +#endif
  14171. +
  14172. +MODULE_PARM_DESC(noauto, "Dont try all known algos, just setup enabled ones");
  14173. +
  14174. +MODULE_PARM_DESC(des_ede3, "0: disable | 1: force_enable | min,max: dontuse");
  14175. +MODULE_PARM_DESC(aes, "0: disable | 1: force_enable | min,max: keybitlens");
  14176. +MODULE_PARM_DESC(blowfish, "0: disable | 1: force_enable | min,max: keybitlens");
  14177. +MODULE_PARM_DESC(cast, "0: disable | 1: force_enable | min,max: keybitlens");
  14178. +MODULE_PARM_DESC(serpent, "0: disable | 1: force_enable | min,max: keybitlens");
  14179. +MODULE_PARM_DESC(twofish, "0: disable | 1: force_enable | min,max: keybitlens");
  14180. +
  14181. +struct ipsec_alg_capi_cipher {
  14182. + const char *ciphername; /* cryptoapi's ciphername */
  14183. + unsigned blocksize;
  14184. + unsigned short minbits;
  14185. + unsigned short maxbits;
  14186. + int *parm; /* lkm param for this cipher */
  14187. + struct ipsec_alg_enc alg; /* note it's not a pointer */
  14188. +};
  14189. +static struct ipsec_alg_capi_cipher alg_capi_carray[] = {
  14190. + { CIPHERNAME_AES , 16, 128, 256, aes , { ixt_alg_id: ESP_AES, }},
  14191. + { CIPHERNAME_TWOFISH , 16, 128, 256, twofish, { ixt_alg_id: ESP_TWOFISH, }},
  14192. + { CIPHERNAME_SERPENT , 16, 128, 256, serpent, { ixt_alg_id: ESP_SERPENT, }},
  14193. + { CIPHERNAME_CAST , 8, 128, 128, cast , { ixt_alg_id: ESP_CAST, }},
  14194. + { CIPHERNAME_BLOWFISH , 8, 96, 448, blowfish,{ ixt_alg_id: ESP_BLOWFISH, }},
  14195. + { CIPHERNAME_3DES , 8, 192, 192, des_ede3,{ ixt_alg_id: ESP_3DES, }},
  14196. + { NULL, 0, 0, 0, NULL, {} }
  14197. +};
  14198. +#ifdef NOT_YET
  14199. +struct ipsec_alg_capi_digest {
  14200. + const char *digestname; /* cryptoapi's digestname */
  14201. + struct digest_implementation *di;
  14202. + struct ipsec_alg_auth alg; /* note it's not a pointer */
  14203. +};
  14204. +static struct ipsec_alg_capi_cipher alg_capi_darray[] = {
  14205. + { DIGESTNAME_MD5, NULL, { ixt_alg_id: AH_MD5, }},
  14206. + { DIGESTNAME_SHA1, NULL, { ixt_alg_id: AH_SHA, }},
  14207. + { NULL, NULL, {} }
  14208. +};
  14209. +#endif
  14210. +/*
  14211. + * "generic" linux cryptoapi setup_cipher() function
  14212. + */
  14213. +int setup_cipher(const char *ciphername)
  14214. +{
  14215. + return crypto_alg_available(ciphername, 0);
  14216. +}
  14217. +
  14218. +/*
  14219. + * setups ipsec_alg_capi_cipher "hyper" struct components, calling
  14220. + * register_ipsec_alg for cointaned ipsec_alg object
  14221. + */
  14222. +static void _capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e);
  14223. +static __u8 * _capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen);
  14224. +static int _capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt);
  14225. +
  14226. +static int
  14227. +setup_ipsec_alg_capi_cipher(struct ipsec_alg_capi_cipher *cptr)
  14228. +{
  14229. + int ret;
  14230. + cptr->alg.ixt_version = IPSEC_ALG_VERSION;
  14231. + cptr->alg.ixt_module = THIS_MODULE;
  14232. + atomic_set (& cptr->alg.ixt_refcnt, 0);
  14233. + strncpy (cptr->alg.ixt_name , cptr->ciphername, sizeof (cptr->alg.ixt_name));
  14234. +
  14235. + cptr->alg.ixt_blocksize=cptr->blocksize;
  14236. + cptr->alg.ixt_keyminbits=cptr->minbits;
  14237. + cptr->alg.ixt_keymaxbits=cptr->maxbits;
  14238. + cptr->alg.ixt_state = 0;
  14239. + if (excl) cptr->alg.ixt_state |= IPSEC_ALG_ST_EXCL;
  14240. + cptr->alg.ixt_e_keylen=cptr->alg.ixt_keymaxbits/8;
  14241. + cptr->alg.ixt_e_ctx_size = 0;
  14242. + cptr->alg.ixt_alg_type = IPSEC_ALG_TYPE_ENCRYPT;
  14243. + cptr->alg.ixt_e_new_key = _capi_new_key;
  14244. + cptr->alg.ixt_e_destroy_key = _capi_destroy_key;
  14245. + cptr->alg.ixt_e_cbc_encrypt = _capi_cbc_encrypt;
  14246. + cptr->alg.ixt_data = cptr;
  14247. +
  14248. + ret=register_ipsec_alg_enc(&cptr->alg);
  14249. + printk("setup_ipsec_alg_capi_cipher(): "
  14250. + "alg_type=%d alg_id=%d name=%s "
  14251. + "keyminbits=%d keymaxbits=%d, ret=%d\n",
  14252. + cptr->alg.ixt_alg_type,
  14253. + cptr->alg.ixt_alg_id,
  14254. + cptr->alg.ixt_name,
  14255. + cptr->alg.ixt_keyminbits,
  14256. + cptr->alg.ixt_keymaxbits,
  14257. + ret);
  14258. + return ret;
  14259. +}
  14260. +/*
  14261. + * called in ipsec_sa_wipe() time, will destroy key contexts
  14262. + * and do 1 unbind()
  14263. + */
  14264. +static void
  14265. +_capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e)
  14266. +{
  14267. + struct crypto_tfm *tfm=(struct crypto_tfm*)key_e;
  14268. +
  14269. + if (debug > 0)
  14270. + printk(KERN_DEBUG "klips_debug: _capi_destroy_key:"
  14271. + "name=%s key_e=%p \n",
  14272. + alg->ixt_name, key_e);
  14273. + if (!key_e) {
  14274. + printk(KERN_ERR "klips_debug: _capi_destroy_key:"
  14275. + "name=%s NULL key_e!\n",
  14276. + alg->ixt_name);
  14277. + return;
  14278. + }
  14279. + crypto_free_tfm(tfm);
  14280. +}
  14281. +
  14282. +/*
  14283. + * create new key context, need alg->ixt_data to know which
  14284. + * (of many) cipher inside this module is the target
  14285. + */
  14286. +static __u8 *
  14287. +_capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen)
  14288. +{
  14289. + struct ipsec_alg_capi_cipher *cptr;
  14290. + struct crypto_tfm *tfm=NULL;
  14291. +
  14292. + cptr = alg->ixt_data;
  14293. + if (!cptr) {
  14294. + printk(KERN_ERR "_capi_new_key(): "
  14295. + "NULL ixt_data (?!) for \"%s\" algo\n"
  14296. + , alg->ixt_name);
  14297. + goto err;
  14298. + }
  14299. + if (debug > 0)
  14300. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  14301. + "name=%s cptr=%p key=%p keysize=%d\n",
  14302. + alg->ixt_name, cptr, key, keylen);
  14303. +
  14304. + /*
  14305. + * alloc tfm
  14306. + */
  14307. + tfm = crypto_alloc_tfm(cptr->ciphername, CRYPTO_TFM_MODE_CBC);
  14308. + if (!tfm) {
  14309. + printk(KERN_ERR "_capi_new_key(): "
  14310. + "NULL tfm for \"%s\" cryptoapi (\"%s\") algo\n"
  14311. + , alg->ixt_name, cptr->ciphername);
  14312. + goto err;
  14313. + }
  14314. + if (crypto_cipher_setkey(tfm, key, keylen) < 0) {
  14315. + printk(KERN_ERR "_capi_new_key(): "
  14316. + "failed new_key() for \"%s\" cryptoapi algo (keylen=%d)\n"
  14317. + , alg->ixt_name, keylen);
  14318. + crypto_free_tfm(tfm);
  14319. + tfm=NULL;
  14320. + }
  14321. +err:
  14322. + if (debug > 0)
  14323. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  14324. + "name=%s key=%p keylen=%d tfm=%p\n",
  14325. + alg->ixt_name, key, keylen, tfm);
  14326. + return (__u8 *) tfm;
  14327. +}
  14328. +/*
  14329. + * core encryption function: will use cx->ci to call actual cipher's
  14330. + * cbc function
  14331. + */
  14332. +static int
  14333. +_capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt) {
  14334. + int error =0;
  14335. + struct crypto_tfm *tfm=(struct crypto_tfm *)key_e;
  14336. + struct scatterlist sg = {
  14337. + .page = virt_to_page(in),
  14338. + .offset = (unsigned long)(in) % PAGE_SIZE,
  14339. + .length=ilen,
  14340. + };
  14341. + if (debug > 1)
  14342. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  14343. + "key_e=%p "
  14344. + "in=%p out=%p ilen=%d iv=%p encrypt=%d\n"
  14345. + , key_e
  14346. + , in, in, ilen, iv, encrypt);
  14347. + crypto_cipher_set_iv(tfm, iv, crypto_tfm_alg_ivsize(tfm));
  14348. + if (encrypt)
  14349. + error = crypto_cipher_encrypt (tfm, &sg, &sg, ilen);
  14350. + else
  14351. + error = crypto_cipher_decrypt (tfm, &sg, &sg, ilen);
  14352. + if (debug > 1)
  14353. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  14354. + "error=%d\n"
  14355. + , error);
  14356. + return (error<0)? error : ilen;
  14357. +}
  14358. +/*
  14359. + * main initialization loop: for each cipher in list, do
  14360. + * 1) setup cryptoapi cipher else continue
  14361. + * 2) register ipsec_alg object
  14362. + */
  14363. +static int
  14364. +setup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14365. +{
  14366. + struct ipsec_alg_capi_cipher *cptr;
  14367. + /* foreach cipher in list ... */
  14368. + for (cptr=clist;cptr->ciphername;cptr++) {
  14369. + /*
  14370. + * see if cipher has been disabled (0) or
  14371. + * if noauto set and not enabled (1)
  14372. + */
  14373. + if (cptr->parm[0] == 0 || (noauto && cptr->parm[0] < 0)) {
  14374. + if (debug>0)
  14375. + printk(KERN_INFO "setup_cipher_list(): "
  14376. + "ciphername=%s skipped at user request: "
  14377. + "noauto=%d parm[0]=%d parm[1]=%d\n"
  14378. + , cptr->ciphername
  14379. + , noauto
  14380. + , cptr->parm[0]
  14381. + , cptr->parm[1]);
  14382. + continue;
  14383. + }
  14384. + /*
  14385. + * use a local ci to avoid touching cptr->ci,
  14386. + * if register ipsec_alg success then bind cipher
  14387. + */
  14388. + if( setup_cipher(cptr->ciphername) ) {
  14389. + if (debug > 0)
  14390. + printk(KERN_DEBUG "klips_debug:"
  14391. + "setup_cipher_list():"
  14392. + "ciphername=%s found\n"
  14393. + , cptr->ciphername);
  14394. + if (setup_ipsec_alg_capi_cipher(cptr) == 0) {
  14395. +
  14396. +
  14397. + } else {
  14398. + printk(KERN_ERR "klips_debug:"
  14399. + "setup_cipher_list():"
  14400. + "ciphername=%s failed ipsec_alg_register\n"
  14401. + , cptr->ciphername);
  14402. + }
  14403. + } else {
  14404. + if (debug>0)
  14405. + printk(KERN_INFO "setup_cipher_list(): lookup for ciphername=%s: not found \n",
  14406. + cptr->ciphername);
  14407. + }
  14408. + }
  14409. + return 0;
  14410. +}
  14411. +/*
  14412. + * deregister ipsec_alg objects and unbind ciphers
  14413. + */
  14414. +static int
  14415. +unsetup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14416. +{
  14417. + struct ipsec_alg_capi_cipher *cptr;
  14418. + /* foreach cipher in list ... */
  14419. + for (cptr=clist;cptr->ciphername;cptr++) {
  14420. + if (cptr->alg.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  14421. + unregister_ipsec_alg_enc(&cptr->alg);
  14422. + }
  14423. + }
  14424. + return 0;
  14425. +}
  14426. +/*
  14427. + * test loop for registered algos
  14428. + */
  14429. +static int
  14430. +test_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14431. +{
  14432. + int test_ret;
  14433. + struct ipsec_alg_capi_cipher *cptr;
  14434. + /* foreach cipher in list ... */
  14435. + for (cptr=clist;cptr->ciphername;cptr++) {
  14436. + if (cptr->alg.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  14437. + test_ret=ipsec_alg_test(
  14438. + cptr->alg.ixt_alg_type,
  14439. + cptr->alg.ixt_alg_id,
  14440. + test);
  14441. + printk("test_cipher_list(alg_type=%d alg_id=%d): test_ret=%d\n",
  14442. + cptr->alg.ixt_alg_type,
  14443. + cptr->alg.ixt_alg_id,
  14444. + test_ret);
  14445. + }
  14446. + }
  14447. + return 0;
  14448. +}
  14449. +
  14450. +IPSEC_ALG_MODULE_INIT( ipsec_cryptoapi_init )
  14451. +{
  14452. + int ret, test_ret;
  14453. + if ((ret=setup_cipher_list(alg_capi_carray)) < 0)
  14454. + return -EPROTONOSUPPORT;
  14455. + if (ret==0 && test) {
  14456. + test_ret=test_cipher_list(alg_capi_carray);
  14457. + }
  14458. + return ret;
  14459. +}
  14460. +IPSEC_ALG_MODULE_EXIT( ipsec_cryptoapi_fini )
  14461. +{
  14462. + unsetup_cipher_list(alg_capi_carray);
  14463. + return;
  14464. +}
  14465. +#ifdef MODULE_LICENSE
  14466. +MODULE_LICENSE("GPL");
  14467. +#endif
  14468. +
  14469. +EXPORT_NO_SYMBOLS;
  14470. +#endif /* NO_CRYPTOAPI_SUPPORT */
  14471. --- /dev/null Tue Mar 11 13:02:56 2003
  14472. +++ linux/net/ipsec/alg/scripts/mk-static_init.c.sh Mon Feb 9 13:51:03 2004
  14473. @@ -0,0 +1,18 @@
  14474. +#!/bin/sh
  14475. +cat << EOF
  14476. +#include <linux/kernel.h>
  14477. +#include <linux/list.h>
  14478. +#include "freeswan/ipsec_alg.h"
  14479. +$(for i in $*; do
  14480. + test -z "$i" && continue
  14481. + echo "extern int $i(void);"
  14482. +done)
  14483. +void ipsec_alg_static_init(void){
  14484. + int __attribute__ ((unused)) err=0;
  14485. +$(for i in $*; do
  14486. + test -z "$i" && continue
  14487. + echo " if ((err=$i()) < 0)"
  14488. + echo " printk(KERN_WARNING \"$i() returned %d\", err);"
  14489. +done)
  14490. +}
  14491. +EOF
  14492. --- /dev/null Tue Mar 11 13:02:56 2003
  14493. +++ linux/net/ipsec/anyaddr.c Mon Feb 9 13:51:03 2004
  14494. @@ -0,0 +1,148 @@
  14495. +/*
  14496. + * special addresses
  14497. + * Copyright (C) 2000 Henry Spencer.
  14498. + *
  14499. + * This library is free software; you can redistribute it and/or modify it
  14500. + * under the terms of the GNU Library General Public License as published by
  14501. + * the Free Software Foundation; either version 2 of the License, or (at your
  14502. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  14503. + *
  14504. + * This library is distributed in the hope that it will be useful, but
  14505. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14506. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  14507. + * License for more details.
  14508. + *
  14509. + * RCSID $Id: anyaddr.c,v 1.10.10.1 2006/11/24 05:55:46 paul Exp $
  14510. + */
  14511. +#include "openswan.h"
  14512. +
  14513. +/* these are mostly fallbacks for the no-IPv6-support-in-library case */
  14514. +#ifndef IN6ADDR_ANY_INIT
  14515. +#define IN6ADDR_ANY_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
  14516. +#endif
  14517. +#ifndef IN6ADDR_LOOPBACK_INIT
  14518. +#define IN6ADDR_LOOPBACK_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
  14519. +#endif
  14520. +
  14521. +static struct in6_addr v6any = IN6ADDR_ANY_INIT;
  14522. +static struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;
  14523. +
  14524. +/*
  14525. + - anyaddr - initialize to the any-address value
  14526. + */
  14527. +err_t /* NULL for success, else string literal */
  14528. +anyaddr(af, dst)
  14529. +int af; /* address family */
  14530. +ip_address *dst;
  14531. +{
  14532. + uint32_t v4any = htonl(INADDR_ANY);
  14533. +
  14534. + switch (af) {
  14535. + case AF_INET:
  14536. + return initaddr((unsigned char *)&v4any, sizeof(v4any), af, dst);
  14537. + break;
  14538. + case AF_INET6:
  14539. + return initaddr((unsigned char *)&v6any, sizeof(v6any), af, dst);
  14540. + break;
  14541. + default:
  14542. + return "unknown address family in anyaddr/unspecaddr";
  14543. + break;
  14544. + }
  14545. +}
  14546. +
  14547. +/*
  14548. + - unspecaddr - initialize to the unspecified-address value
  14549. + */
  14550. +err_t /* NULL for success, else string literal */
  14551. +unspecaddr(af, dst)
  14552. +int af; /* address family */
  14553. +ip_address *dst;
  14554. +{
  14555. + return anyaddr(af, dst);
  14556. +}
  14557. +
  14558. +/*
  14559. + - loopbackaddr - initialize to the loopback-address value
  14560. + */
  14561. +err_t /* NULL for success, else string literal */
  14562. +loopbackaddr(af, dst)
  14563. +int af; /* address family */
  14564. +ip_address *dst;
  14565. +{
  14566. + uint32_t v4loop = htonl(INADDR_LOOPBACK);
  14567. +
  14568. + switch (af) {
  14569. + case AF_INET:
  14570. + return initaddr((unsigned char *)&v4loop, sizeof(v4loop), af, dst);
  14571. + break;
  14572. + case AF_INET6:
  14573. + return initaddr((unsigned char *)&v6loop, sizeof(v6loop), af, dst);
  14574. + break;
  14575. + default:
  14576. + return "unknown address family in loopbackaddr";
  14577. + break;
  14578. + }
  14579. +}
  14580. +
  14581. +/*
  14582. + - isanyaddr - test for the any-address value
  14583. + */
  14584. +int
  14585. +isanyaddr(src)
  14586. +const ip_address *src;
  14587. +{
  14588. + uint32_t v4any = htonl(INADDR_ANY);
  14589. + int cmp;
  14590. +
  14591. + switch (src->u.v4.sin_family) {
  14592. + case AF_INET:
  14593. + cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4any, sizeof(v4any));
  14594. + break;
  14595. + case AF_INET6:
  14596. + cmp = memcmp(&src->u.v6.sin6_addr, &v6any, sizeof(v6any));
  14597. + break;
  14598. + case 0:
  14599. + /* a zeroed structure is considered any address */
  14600. + return 1;
  14601. + default:
  14602. + return 0;
  14603. + break;
  14604. + }
  14605. +
  14606. + return (cmp == 0) ? 1 : 0;
  14607. +}
  14608. +
  14609. +/*
  14610. + - isunspecaddr - test for the unspecified-address value
  14611. + */
  14612. +int
  14613. +isunspecaddr(src)
  14614. +const ip_address *src;
  14615. +{
  14616. + return isanyaddr(src);
  14617. +}
  14618. +
  14619. +/*
  14620. + - isloopbackaddr - test for the loopback-address value
  14621. + */
  14622. +int
  14623. +isloopbackaddr(src)
  14624. +const ip_address *src;
  14625. +{
  14626. + uint32_t v4loop = htonl(INADDR_LOOPBACK);
  14627. + int cmp;
  14628. +
  14629. + switch (src->u.v4.sin_family) {
  14630. + case AF_INET:
  14631. + cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4loop, sizeof(v4loop));
  14632. + break;
  14633. + case AF_INET6:
  14634. + cmp = memcmp(&src->u.v6.sin6_addr, &v6loop, sizeof(v6loop));
  14635. + break;
  14636. + default:
  14637. + return 0;
  14638. + break;
  14639. + }
  14640. +
  14641. + return (cmp == 0) ? 1 : 0;
  14642. +}
  14643. --- /dev/null Tue Mar 11 13:02:56 2003
  14644. +++ linux/net/ipsec/datatot.c Mon Feb 9 13:51:03 2004
  14645. @@ -0,0 +1,234 @@
  14646. +/*
  14647. + * convert from binary data (e.g. key) to text form
  14648. + * Copyright (C) 2000 Henry Spencer.
  14649. + *
  14650. + * This library is free software; you can redistribute it and/or modify it
  14651. + * under the terms of the GNU Library General Public License as published by
  14652. + * the Free Software Foundation; either version 2 of the License, or (at your
  14653. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  14654. + *
  14655. + * This library is distributed in the hope that it will be useful, but
  14656. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14657. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  14658. + * License for more details.
  14659. + *
  14660. + * RCSID $Id: datatot.c,v 1.7 2005/04/14 20:48:43 mcr Exp $
  14661. + */
  14662. +#include "openswan.h"
  14663. +
  14664. +static void convert(const char *src, size_t nreal, int format, char *out);
  14665. +
  14666. +/*
  14667. + - datatot - convert data bytes to text
  14668. + */
  14669. +size_t /* true length (with NUL) for success */
  14670. +datatot(src, srclen, format, dst, dstlen)
  14671. +const char *src;
  14672. +size_t srclen;
  14673. +int format; /* character indicating what format */
  14674. +char *dst; /* need not be valid if dstlen is 0 */
  14675. +size_t dstlen;
  14676. +{
  14677. + size_t inblocksize; /* process this many bytes at a time */
  14678. + size_t outblocksize; /* producing this many */
  14679. + size_t breakevery; /* add a _ every this many (0 means don't) */
  14680. + size_t sincebreak; /* output bytes since last _ */
  14681. + char breakchar; /* character used to break between groups */
  14682. + char inblock[10]; /* enough for any format */
  14683. + char outblock[10]; /* enough for any format */
  14684. + char fake[1]; /* fake output area for dstlen == 0 */
  14685. + size_t needed; /* return value */
  14686. + char *stop; /* where the terminating NUL will go */
  14687. + size_t ntodo; /* remaining input */
  14688. + size_t nreal;
  14689. + char *out;
  14690. + char *prefix;
  14691. +
  14692. + breakevery = 0;
  14693. + breakchar = '_';
  14694. +
  14695. + switch (format) {
  14696. + case 0:
  14697. + case 'h':
  14698. + format = 'x';
  14699. + breakevery = 8;
  14700. + /* FALLTHROUGH */
  14701. + case 'x':
  14702. + inblocksize = 1;
  14703. + outblocksize = 2;
  14704. + prefix = "0x";
  14705. + break;
  14706. + case ':':
  14707. + format = 'x';
  14708. + breakevery = 2;
  14709. + breakchar = ':';
  14710. + /* FALLTHROUGH */
  14711. + case 16:
  14712. + inblocksize = 1;
  14713. + outblocksize = 2;
  14714. + prefix = "";
  14715. + format = 'x';
  14716. + break;
  14717. + case 's':
  14718. + inblocksize = 3;
  14719. + outblocksize = 4;
  14720. + prefix = "0s";
  14721. + break;
  14722. + case 64: /* beware, equals ' ' */
  14723. + inblocksize = 3;
  14724. + outblocksize = 4;
  14725. + prefix = "";
  14726. + format = 's';
  14727. + break;
  14728. + default:
  14729. + return 0;
  14730. + break;
  14731. + }
  14732. +
  14733. + user_assert(inblocksize < sizeof(inblock));
  14734. + user_assert(outblocksize < sizeof(outblock));
  14735. + user_assert(breakevery % outblocksize == 0);
  14736. +
  14737. + if (srclen == 0)
  14738. + return 0;
  14739. + ntodo = srclen;
  14740. +
  14741. + if (dstlen == 0) { /* dispose of awkward special case */
  14742. + dst = fake;
  14743. + dstlen = 1;
  14744. + }
  14745. + stop = dst + dstlen - 1;
  14746. +
  14747. + nreal = strlen(prefix);
  14748. + needed = nreal; /* for starters */
  14749. + if (dstlen <= nreal) { /* prefix won't fit */
  14750. + strncpy(dst, prefix, dstlen - 1);
  14751. + dst += dstlen - 1;
  14752. + } else {
  14753. + strcpy(dst, prefix);
  14754. + dst += nreal;
  14755. + }
  14756. +
  14757. + user_assert(dst <= stop);
  14758. + sincebreak = 0;
  14759. +
  14760. + while (ntodo > 0) {
  14761. + if (ntodo < inblocksize) { /* incomplete input */
  14762. + memset(inblock, 0, sizeof(inblock));
  14763. + memcpy(inblock, src, ntodo);
  14764. + src = inblock;
  14765. + nreal = ntodo;
  14766. + ntodo = inblocksize;
  14767. + } else
  14768. + nreal = inblocksize;
  14769. + out = (outblocksize > stop - dst) ? outblock : dst;
  14770. +
  14771. + convert(src, nreal, format, out);
  14772. + needed += outblocksize;
  14773. + sincebreak += outblocksize;
  14774. + if (dst < stop) {
  14775. + if (out != dst) {
  14776. + user_assert(outblocksize > stop - dst);
  14777. + memcpy(dst, out, stop - dst);
  14778. + dst = stop;
  14779. + } else
  14780. + dst += outblocksize;
  14781. + }
  14782. +
  14783. + src += inblocksize;
  14784. + ntodo -= inblocksize;
  14785. + if (breakevery != 0 && sincebreak >= breakevery && ntodo > 0) {
  14786. + if (dst < stop)
  14787. + *dst++ = breakchar;
  14788. + needed++;
  14789. + sincebreak = 0;
  14790. + }
  14791. + }
  14792. +
  14793. + user_assert(dst <= stop);
  14794. + *dst++ = '\0';
  14795. + needed++;
  14796. +
  14797. + return needed;
  14798. +}
  14799. +
  14800. +/*
  14801. + - convert - convert one input block to one output block
  14802. + */
  14803. +static void
  14804. +convert(src, nreal, format, out)
  14805. +const char *src;
  14806. +size_t nreal; /* how much of the input block is real */
  14807. +int format;
  14808. +char *out;
  14809. +{
  14810. + static char hex[] = "0123456789abcdef";
  14811. + static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  14812. + "abcdefghijklmnopqrstuvwxyz"
  14813. + "0123456789+/";
  14814. + unsigned char c;
  14815. + unsigned char c1, c2, c3;
  14816. +
  14817. + user_assert(nreal > 0);
  14818. + switch (format) {
  14819. + case 'x':
  14820. + user_assert(nreal == 1);
  14821. + c = (unsigned char)*src;
  14822. + *out++ = hex[c >> 4];
  14823. + *out++ = hex[c & 0xf];
  14824. + break;
  14825. + case 's':
  14826. + c1 = (unsigned char)*src++;
  14827. + c2 = (unsigned char)*src++;
  14828. + c3 = (unsigned char)*src++;
  14829. + *out++ = base64[c1 >> 2]; /* top 6 bits of c1 */
  14830. + c = (c1 & 0x3) << 4; /* bottom 2 of c1... */
  14831. + c |= c2 >> 4; /* ...top 4 of c2 */
  14832. + *out++ = base64[c];
  14833. + if (nreal == 1)
  14834. + *out++ = '=';
  14835. + else {
  14836. + c = (c2 & 0xf) << 2; /* bottom 4 of c2... */
  14837. + c |= c3 >> 6; /* ...top 2 of c3 */
  14838. + *out++ = base64[c];
  14839. + }
  14840. + if (nreal <= 2)
  14841. + *out++ = '=';
  14842. + else
  14843. + *out++ = base64[c3 & 0x3f]; /* bottom 6 of c3 */
  14844. + break;
  14845. + default:
  14846. + user_assert(nreal == 0); /* unknown format */
  14847. + break;
  14848. + }
  14849. +}
  14850. +
  14851. +/*
  14852. + - datatoa - convert data to ASCII
  14853. + * backward-compatibility synonym for datatot
  14854. + */
  14855. +size_t /* true length (with NUL) for success */
  14856. +datatoa(src, srclen, format, dst, dstlen)
  14857. +const char *src;
  14858. +size_t srclen;
  14859. +int format; /* character indicating what format */
  14860. +char *dst; /* need not be valid if dstlen is 0 */
  14861. +size_t dstlen;
  14862. +{
  14863. + return datatot(src, srclen, format, dst, dstlen);
  14864. +}
  14865. +
  14866. +/*
  14867. + - bytestoa - convert data bytes to ASCII
  14868. + * backward-compatibility synonym for datatot
  14869. + */
  14870. +size_t /* true length (with NUL) for success */
  14871. +bytestoa(src, srclen, format, dst, dstlen)
  14872. +const char *src;
  14873. +size_t srclen;
  14874. +int format; /* character indicating what format */
  14875. +char *dst; /* need not be valid if dstlen is 0 */
  14876. +size_t dstlen;
  14877. +{
  14878. + return datatot(src, srclen, format, dst, dstlen);
  14879. +}
  14880. --- /dev/null Tue Mar 11 13:02:56 2003
  14881. +++ linux/net/ipsec/defconfig Mon Feb 9 13:51:03 2004
  14882. @@ -0,0 +1,148 @@
  14883. +
  14884. +#
  14885. +# RCSID $Id: defconfig,v 1.28.2.1 2006/10/11 18:14:33 paul Exp $
  14886. +#
  14887. +
  14888. +#
  14889. +# FreeS/WAN IPSec implementation, KLIPS kernel config defaults
  14890. +#
  14891. +
  14892. +#
  14893. +# First, lets override stuff already set or not in the kernel config.
  14894. +#
  14895. +# We can't even think about leaving this off...
  14896. +CONFIG_INET=y
  14897. +
  14898. +#
  14899. +# This must be on for subnet protection.
  14900. +CONFIG_IP_FORWARD=y
  14901. +
  14902. +# Shut off IPSEC masquerading if it has been enabled, since it will
  14903. +# break the compile. IPPROTO_ESP and IPPROTO_AH were included in
  14904. +# net/ipv4/ip_masq.c when they should have gone into include/linux/in.h.
  14905. +CONFIG_IP_MASQUERADE_IPSEC=n
  14906. +
  14907. +#
  14908. +# Next, lets set the recommended FreeS/WAN configuration.
  14909. +#
  14910. +
  14911. +# To config as static (preferred), 'y'. To config as module, 'm'.
  14912. +CONFIG_KLIPS=m
  14913. +
  14914. +# To do tunnel mode IPSec, this must be enabled.
  14915. +CONFIG_KLIPS_IPIP=y
  14916. +
  14917. +# To enable authentication, say 'y'. (Highly recommended)
  14918. +CONFIG_KLIPS_AH=y
  14919. +
  14920. +# Authentication algorithm(s):
  14921. +CONFIG_KLIPS_AUTH_HMAC_MD5=y
  14922. +CONFIG_KLIPS_AUTH_HMAC_SHA1=y
  14923. +
  14924. +# To enable encryption, say 'y'. (Highly recommended)
  14925. +CONFIG_KLIPS_ESP=y
  14926. +
  14927. +# modular algo extensions (and new ALGOs)
  14928. +CONFIG_KLIPS_ALG=y
  14929. +
  14930. +# Encryption algorithm(s):
  14931. +CONFIG_KLIPS_ENC_3DES=y
  14932. +CONFIG_KLIPS_ENC_AES=y
  14933. +# CONFIG_KLIPS_ENC_NULL=y
  14934. +
  14935. +# Use CryptoAPI for ALG? - by default, no.
  14936. +CONFIG_KLIPS_ENC_CRYPTOAPI=n
  14937. +
  14938. +# IP Compression: new, probably still has minor bugs.
  14939. +CONFIG_KLIPS_IPCOMP=y
  14940. +
  14941. +# To enable userspace-switchable KLIPS debugging, say 'y'.
  14942. +CONFIG_KLIPS_DEBUG=y
  14943. +
  14944. +# NAT Traversal
  14945. +CONFIG_IPSEC_NAT_TRAVERSAL=y
  14946. +
  14947. +#
  14948. +#
  14949. +# $Log: defconfig,v $
  14950. +# Revision 1.28.2.1 2006/10/11 18:14:33 paul
  14951. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  14952. +# per default.
  14953. +#
  14954. +# Revision 1.28 2005/05/11 03:15:42 mcr
  14955. +# adjusted makefiles to sanely build modules properly.
  14956. +#
  14957. +# Revision 1.27 2005/03/20 03:00:05 mcr
  14958. +# default configuration should enable NAT_TRAVERSAL.
  14959. +#
  14960. +# Revision 1.26 2004/07/10 19:11:18 mcr
  14961. +# CONFIG_IPSEC -> CONFIG_KLIPS.
  14962. +#
  14963. +# Revision 1.25 2004/07/05 01:03:53 mcr
  14964. +# fix for adding cryptoapi code.
  14965. +# keep it off for now, since UMLs do not have it yet.
  14966. +#
  14967. +# Revision 1.24 2004/04/06 02:49:25 mcr
  14968. +# pullup of algo code from alg-branch.
  14969. +#
  14970. +# Revision 1.23.2.2 2004/04/05 04:30:46 mcr
  14971. +# patches for alg-branch to compile/work with 2.x openswan
  14972. +#
  14973. +# Revision 1.23.2.1 2003/12/22 15:25:52 jjo
  14974. +# . Merged algo-0.8.1-rc11-test1 into alg-branch
  14975. +#
  14976. +# Revision 1.23 2003/12/10 01:14:27 mcr
  14977. +# NAT-traversal patches to KLIPS.
  14978. +#
  14979. +# Revision 1.22 2003/02/24 19:37:27 mcr
  14980. +# changed default compilation mode to static.
  14981. +#
  14982. +# Revision 1.21 2002/04/24 07:36:27 mcr
  14983. +# Moved from ./klips/net/ipsec/defconfig,v
  14984. +#
  14985. +# Revision 1.20 2002/04/02 04:07:40 mcr
  14986. +# default build is now 'm'odule for KLIPS
  14987. +#
  14988. +# Revision 1.19 2002/03/08 18:57:17 rgb
  14989. +# Added a blank line at the beginning of the file to make it easier for
  14990. +# other projects to patch ./arch/i386/defconfig, for example
  14991. +# LIDS+grSecurity requested by Jason Pattie.
  14992. +#
  14993. +# Revision 1.18 2000/11/30 17:26:56 rgb
  14994. +# Cleaned out unused options and enabled ipcomp by default.
  14995. +#
  14996. +# Revision 1.17 2000/09/15 11:37:01 rgb
  14997. +# Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  14998. +# IPCOMP zlib deflate code.
  14999. +#
  15000. +# Revision 1.16 2000/09/08 19:12:55 rgb
  15001. +# Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  15002. +#
  15003. +# Revision 1.15 2000/05/24 19:37:13 rgb
  15004. +# *** empty log message ***
  15005. +#
  15006. +# Revision 1.14 2000/05/11 21:14:57 henry
  15007. +# just commenting the FOOBAR=y lines out is not enough
  15008. +#
  15009. +# Revision 1.13 2000/05/10 20:17:58 rgb
  15010. +# Comment out netlink defaults, which are no longer needed.
  15011. +#
  15012. +# Revision 1.12 2000/05/10 19:13:38 rgb
  15013. +# Added configure option to shut off no eroute passthrough.
  15014. +#
  15015. +# Revision 1.11 2000/03/16 07:09:46 rgb
  15016. +# Hardcode PF_KEYv2 support.
  15017. +# Disable IPSEC_ICMP by default.
  15018. +# Remove DES config option from defaults file.
  15019. +#
  15020. +# Revision 1.10 2000/01/11 03:09:42 rgb
  15021. +# Added a default of 'y' to PF_KEYv2 keying I/F.
  15022. +#
  15023. +# Revision 1.9 1999/05/08 21:23:12 rgb
  15024. +# Added support for 2.2.x kernels.
  15025. +#
  15026. +# Revision 1.8 1999/04/06 04:54:25 rgb
  15027. +# Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  15028. +# patch shell fixes.
  15029. +#
  15030. +#
  15031. --- /dev/null Tue Mar 11 13:02:56 2003
  15032. +++ linux/net/ipsec/deflate.c Mon Feb 9 13:51:03 2004
  15033. @@ -0,0 +1,1351 @@
  15034. +/* deflate.c -- compress data using the deflation algorithm
  15035. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  15036. + * For conditions of distribution and use, see copyright notice in zlib.h
  15037. + */
  15038. +
  15039. +/*
  15040. + * ALGORITHM
  15041. + *
  15042. + * The "deflation" process depends on being able to identify portions
  15043. + * of the input text which are identical to earlier input (within a
  15044. + * sliding window trailing behind the input currently being processed).
  15045. + *
  15046. + * The most straightforward technique turns out to be the fastest for
  15047. + * most input files: try all possible matches and select the longest.
  15048. + * The key feature of this algorithm is that insertions into the string
  15049. + * dictionary are very simple and thus fast, and deletions are avoided
  15050. + * completely. Insertions are performed at each input character, whereas
  15051. + * string matches are performed only when the previous match ends. So it
  15052. + * is preferable to spend more time in matches to allow very fast string
  15053. + * insertions and avoid deletions. The matching algorithm for small
  15054. + * strings is inspired from that of Rabin & Karp. A brute force approach
  15055. + * is used to find longer strings when a small match has been found.
  15056. + * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  15057. + * (by Leonid Broukhis).
  15058. + * A previous version of this file used a more sophisticated algorithm
  15059. + * (by Fiala and Greene) which is guaranteed to run in linear amortized
  15060. + * time, but has a larger average cost, uses more memory and is patented.
  15061. + * However the F&G algorithm may be faster for some highly redundant
  15062. + * files if the parameter max_chain_length (described below) is too large.
  15063. + *
  15064. + * ACKNOWLEDGEMENTS
  15065. + *
  15066. + * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  15067. + * I found it in 'freeze' written by Leonid Broukhis.
  15068. + * Thanks to many people for bug reports and testing.
  15069. + *
  15070. + * REFERENCES
  15071. + *
  15072. + * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  15073. + * Available in ftp://ds.internic.net/rfc/rfc1951.txt
  15074. + *
  15075. + * A description of the Rabin and Karp algorithm is given in the book
  15076. + * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  15077. + *
  15078. + * Fiala,E.R., and Greene,D.H.
  15079. + * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  15080. + *
  15081. + */
  15082. +
  15083. +/* @(#) $Id: deflate.c,v 1.4 2004/07/10 07:48:37 mcr Exp $ */
  15084. +
  15085. +#include "deflate.h"
  15086. +
  15087. +local const char deflate_copyright[] =
  15088. + " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly ";
  15089. +/*
  15090. + If you use the zlib library in a product, an acknowledgment is welcome
  15091. + in the documentation of your product. If for some reason you cannot
  15092. + include such an acknowledgment, I would appreciate that you keep this
  15093. + copyright string in the executable of your product.
  15094. + */
  15095. +
  15096. +/* ===========================================================================
  15097. + * Function prototypes.
  15098. + */
  15099. +typedef enum {
  15100. + need_more, /* block not completed, need more input or more output */
  15101. + block_done, /* block flush performed */
  15102. + finish_started, /* finish started, need only more output at next deflate */
  15103. + finish_done /* finish done, accept no more input or output */
  15104. +} block_state;
  15105. +
  15106. +typedef block_state (*compress_func) OF((deflate_state *s, int flush));
  15107. +/* Compression function. Returns the block state after the call. */
  15108. +
  15109. +local void fill_window OF((deflate_state *s));
  15110. +local block_state deflate_stored OF((deflate_state *s, int flush));
  15111. +local block_state deflate_fast OF((deflate_state *s, int flush));
  15112. +local block_state deflate_slow OF((deflate_state *s, int flush));
  15113. +local void lm_init OF((deflate_state *s));
  15114. +local void putShortMSB OF((deflate_state *s, uInt b));
  15115. +local void flush_pending OF((z_streamp strm));
  15116. +local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
  15117. +#ifdef ASMV
  15118. + void match_init OF((void)); /* asm code initialization */
  15119. + uInt longest_match OF((deflate_state *s, IPos cur_match));
  15120. +#else
  15121. +local uInt longest_match OF((deflate_state *s, IPos cur_match));
  15122. +#endif
  15123. +
  15124. +#ifdef DEBUG
  15125. +local void check_match OF((deflate_state *s, IPos start, IPos match,
  15126. + int length));
  15127. +#endif
  15128. +
  15129. +/* ===========================================================================
  15130. + * Local data
  15131. + */
  15132. +
  15133. +#define NIL 0
  15134. +/* Tail of hash chains */
  15135. +
  15136. +#ifndef TOO_FAR
  15137. +# define TOO_FAR 4096
  15138. +#endif
  15139. +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  15140. +
  15141. +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  15142. +/* Minimum amount of lookahead, except at the end of the input file.
  15143. + * See deflate.c for comments about the MIN_MATCH+1.
  15144. + */
  15145. +
  15146. +/* Values for max_lazy_match, good_match and max_chain_length, depending on
  15147. + * the desired pack level (0..9). The values given below have been tuned to
  15148. + * exclude worst case performance for pathological files. Better values may be
  15149. + * found for specific files.
  15150. + */
  15151. +typedef struct config_s {
  15152. + ush good_length; /* reduce lazy search above this match length */
  15153. + ush max_lazy; /* do not perform lazy search above this match length */
  15154. + ush nice_length; /* quit search above this match length */
  15155. + ush max_chain;
  15156. + compress_func func;
  15157. +} config;
  15158. +
  15159. +local const config configuration_table[10] = {
  15160. +/* good lazy nice chain */
  15161. +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  15162. +/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
  15163. +/* 2 */ {4, 5, 16, 8, deflate_fast},
  15164. +/* 3 */ {4, 6, 32, 32, deflate_fast},
  15165. +
  15166. +/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
  15167. +/* 5 */ {8, 16, 32, 32, deflate_slow},
  15168. +/* 6 */ {8, 16, 128, 128, deflate_slow},
  15169. +/* 7 */ {8, 32, 128, 256, deflate_slow},
  15170. +/* 8 */ {32, 128, 258, 1024, deflate_slow},
  15171. +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
  15172. +
  15173. +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  15174. + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  15175. + * meaning.
  15176. + */
  15177. +
  15178. +#define EQUAL 0
  15179. +/* result of memcmp for equal strings */
  15180. +
  15181. +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
  15182. +
  15183. +/* ===========================================================================
  15184. + * Update a hash value with the given input byte
  15185. + * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  15186. + * input characters, so that a running hash key can be computed from the
  15187. + * previous key instead of complete recalculation each time.
  15188. + */
  15189. +#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  15190. +
  15191. +
  15192. +/* ===========================================================================
  15193. + * Insert string str in the dictionary and set match_head to the previous head
  15194. + * of the hash chain (the most recent string with same hash key). Return
  15195. + * the previous length of the hash chain.
  15196. + * If this file is compiled with -DFASTEST, the compression level is forced
  15197. + * to 1, and no hash chains are maintained.
  15198. + * IN assertion: all calls to to INSERT_STRING are made with consecutive
  15199. + * input characters and the first MIN_MATCH bytes of str are valid
  15200. + * (except for the last MIN_MATCH-1 bytes of the input file).
  15201. + */
  15202. +#ifdef FASTEST
  15203. +#define INSERT_STRING(s, str, match_head) \
  15204. + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  15205. + match_head = s->head[s->ins_h], \
  15206. + s->head[s->ins_h] = (Pos)(str))
  15207. +#else
  15208. +#define INSERT_STRING(s, str, match_head) \
  15209. + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  15210. + s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
  15211. + s->head[s->ins_h] = (Pos)(str))
  15212. +#endif
  15213. +
  15214. +/* ===========================================================================
  15215. + * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  15216. + * prev[] will be initialized on the fly.
  15217. + */
  15218. +#define CLEAR_HASH(s) \
  15219. + s->head[s->hash_size-1] = NIL; \
  15220. + zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  15221. +
  15222. +/* ========================================================================= */
  15223. +int ZEXPORT deflateInit_(strm, level, version, stream_size)
  15224. + z_streamp strm;
  15225. + int level;
  15226. + const char *version;
  15227. + int stream_size;
  15228. +{
  15229. + return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
  15230. + Z_DEFAULT_STRATEGY, version, stream_size);
  15231. + /* To do: ignore strm->next_in if we use it as window */
  15232. +}
  15233. +
  15234. +/* ========================================================================= */
  15235. +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
  15236. + version, stream_size)
  15237. + z_streamp strm;
  15238. + int level;
  15239. + int method;
  15240. + int windowBits;
  15241. + int memLevel;
  15242. + int strategy;
  15243. + const char *version;
  15244. + int stream_size;
  15245. +{
  15246. + deflate_state *s;
  15247. + int noheader = 0;
  15248. + static const char* my_version = ZLIB_VERSION;
  15249. +
  15250. + ushf *overlay;
  15251. + /* We overlay pending_buf and d_buf+l_buf. This works since the average
  15252. + * output size for (length,distance) codes is <= 24 bits.
  15253. + */
  15254. +
  15255. + if (version == Z_NULL || version[0] != my_version[0] ||
  15256. + stream_size != sizeof(z_stream)) {
  15257. + return Z_VERSION_ERROR;
  15258. + }
  15259. + if (strm == Z_NULL) return Z_STREAM_ERROR;
  15260. +
  15261. + strm->msg = Z_NULL;
  15262. + if (strm->zalloc == Z_NULL) {
  15263. + return Z_STREAM_ERROR;
  15264. +/* strm->zalloc = zcalloc;
  15265. + strm->opaque = (voidpf)0;*/
  15266. + }
  15267. + if (strm->zfree == Z_NULL) return Z_STREAM_ERROR; /* strm->zfree = zcfree; */
  15268. +
  15269. + if (level == Z_DEFAULT_COMPRESSION) level = 6;
  15270. +#ifdef FASTEST
  15271. + level = 1;
  15272. +#endif
  15273. +
  15274. + if (windowBits < 0) { /* undocumented feature: suppress zlib header */
  15275. + noheader = 1;
  15276. + windowBits = -windowBits;
  15277. + }
  15278. + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  15279. + windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
  15280. + strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  15281. + return Z_STREAM_ERROR;
  15282. + }
  15283. + s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  15284. + if (s == Z_NULL) return Z_MEM_ERROR;
  15285. + strm->state = (struct internal_state FAR *)s;
  15286. + s->strm = strm;
  15287. +
  15288. + s->noheader = noheader;
  15289. + s->w_bits = windowBits;
  15290. + s->w_size = 1 << s->w_bits;
  15291. + s->w_mask = s->w_size - 1;
  15292. +
  15293. + s->hash_bits = memLevel + 7;
  15294. + s->hash_size = 1 << s->hash_bits;
  15295. + s->hash_mask = s->hash_size - 1;
  15296. + s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  15297. +
  15298. + s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  15299. + s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
  15300. + s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
  15301. +
  15302. + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  15303. +
  15304. + overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  15305. + s->pending_buf = (uchf *) overlay;
  15306. + s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  15307. +
  15308. + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  15309. + s->pending_buf == Z_NULL) {
  15310. + strm->msg = ERR_MSG(Z_MEM_ERROR);
  15311. + deflateEnd (strm);
  15312. + return Z_MEM_ERROR;
  15313. + }
  15314. + s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  15315. + s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  15316. +
  15317. + s->level = level;
  15318. + s->strategy = strategy;
  15319. + s->method = (Byte)method;
  15320. +
  15321. + return deflateReset(strm);
  15322. +}
  15323. +
  15324. +/* ========================================================================= */
  15325. +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
  15326. + z_streamp strm;
  15327. + const Bytef *dictionary;
  15328. + uInt dictLength;
  15329. +{
  15330. + deflate_state *s;
  15331. + uInt length = dictLength;
  15332. + uInt n;
  15333. + IPos hash_head = 0;
  15334. +
  15335. + if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
  15336. + strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
  15337. +
  15338. + s = strm->state;
  15339. + strm->adler = adler32(strm->adler, dictionary, dictLength);
  15340. +
  15341. + if (length < MIN_MATCH) return Z_OK;
  15342. + if (length > MAX_DIST(s)) {
  15343. + length = MAX_DIST(s);
  15344. +#ifndef USE_DICT_HEAD
  15345. + dictionary += dictLength - length; /* use the tail of the dictionary */
  15346. +#endif
  15347. + }
  15348. + zmemcpy(s->window, dictionary, length);
  15349. + s->strstart = length;
  15350. + s->block_start = (long)length;
  15351. +
  15352. + /* Insert all strings in the hash table (except for the last two bytes).
  15353. + * s->lookahead stays null, so s->ins_h will be recomputed at the next
  15354. + * call of fill_window.
  15355. + */
  15356. + s->ins_h = s->window[0];
  15357. + UPDATE_HASH(s, s->ins_h, s->window[1]);
  15358. + for (n = 0; n <= length - MIN_MATCH; n++) {
  15359. + INSERT_STRING(s, n, hash_head);
  15360. + }
  15361. + if (hash_head) hash_head = 0; /* to make compiler happy */
  15362. + return Z_OK;
  15363. +}
  15364. +
  15365. +/* ========================================================================= */
  15366. +int ZEXPORT deflateReset (strm)
  15367. + z_streamp strm;
  15368. +{
  15369. + deflate_state *s;
  15370. +
  15371. + if (strm == Z_NULL || strm->state == Z_NULL ||
  15372. + strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
  15373. +
  15374. + strm->total_in = strm->total_out = 0;
  15375. + strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  15376. + strm->data_type = Z_UNKNOWN;
  15377. +
  15378. + s = (deflate_state *)strm->state;
  15379. + s->pending = 0;
  15380. + s->pending_out = s->pending_buf;
  15381. +
  15382. + if (s->noheader < 0) {
  15383. + s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
  15384. + }
  15385. + s->status = s->noheader ? BUSY_STATE : INIT_STATE;
  15386. + strm->adler = 1;
  15387. + s->last_flush = Z_NO_FLUSH;
  15388. +
  15389. + _tr_init(s);
  15390. + lm_init(s);
  15391. +
  15392. + return Z_OK;
  15393. +}
  15394. +
  15395. +/* ========================================================================= */
  15396. +int ZEXPORT deflateParams(strm, level, strategy)
  15397. + z_streamp strm;
  15398. + int level;
  15399. + int strategy;
  15400. +{
  15401. + deflate_state *s;
  15402. + compress_func func;
  15403. + int err = Z_OK;
  15404. +
  15405. + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  15406. + s = strm->state;
  15407. +
  15408. + if (level == Z_DEFAULT_COMPRESSION) {
  15409. + level = 6;
  15410. + }
  15411. + if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  15412. + return Z_STREAM_ERROR;
  15413. + }
  15414. + func = configuration_table[s->level].func;
  15415. +
  15416. + if (func != configuration_table[level].func && strm->total_in != 0) {
  15417. + /* Flush the last buffer: */
  15418. + err = deflate(strm, Z_PARTIAL_FLUSH);
  15419. + }
  15420. + if (s->level != level) {
  15421. + s->level = level;
  15422. + s->max_lazy_match = configuration_table[level].max_lazy;
  15423. + s->good_match = configuration_table[level].good_length;
  15424. + s->nice_match = configuration_table[level].nice_length;
  15425. + s->max_chain_length = configuration_table[level].max_chain;
  15426. + }
  15427. + s->strategy = strategy;
  15428. + return err;
  15429. +}
  15430. +
  15431. +/* =========================================================================
  15432. + * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  15433. + * IN assertion: the stream state is correct and there is enough room in
  15434. + * pending_buf.
  15435. + */
  15436. +local void putShortMSB (s, b)
  15437. + deflate_state *s;
  15438. + uInt b;
  15439. +{
  15440. + put_byte(s, (Byte)(b >> 8));
  15441. + put_byte(s, (Byte)(b & 0xff));
  15442. +}
  15443. +
  15444. +/* =========================================================================
  15445. + * Flush as much pending output as possible. All deflate() output goes
  15446. + * through this function so some applications may wish to modify it
  15447. + * to avoid allocating a large strm->next_out buffer and copying into it.
  15448. + * (See also read_buf()).
  15449. + */
  15450. +local void flush_pending(strm)
  15451. + z_streamp strm;
  15452. +{
  15453. + unsigned len = strm->state->pending;
  15454. +
  15455. + if (len > strm->avail_out) len = strm->avail_out;
  15456. + if (len == 0) return;
  15457. +
  15458. + zmemcpy(strm->next_out, strm->state->pending_out, len);
  15459. + strm->next_out += len;
  15460. + strm->state->pending_out += len;
  15461. + strm->total_out += len;
  15462. + strm->avail_out -= len;
  15463. + strm->state->pending -= len;
  15464. + if (strm->state->pending == 0) {
  15465. + strm->state->pending_out = strm->state->pending_buf;
  15466. + }
  15467. +}
  15468. +
  15469. +/* ========================================================================= */
  15470. +int ZEXPORT deflate (strm, flush)
  15471. + z_streamp strm;
  15472. + int flush;
  15473. +{
  15474. + int old_flush; /* value of flush param for previous deflate call */
  15475. + deflate_state *s;
  15476. +
  15477. + if (strm == Z_NULL || strm->state == Z_NULL ||
  15478. + flush > Z_FINISH || flush < 0) {
  15479. + return Z_STREAM_ERROR;
  15480. + }
  15481. + s = strm->state;
  15482. +
  15483. + if (strm->next_out == Z_NULL ||
  15484. + (strm->next_in == Z_NULL && strm->avail_in != 0) ||
  15485. + (s->status == FINISH_STATE && flush != Z_FINISH)) {
  15486. + ERR_RETURN(strm, Z_STREAM_ERROR);
  15487. + }
  15488. + if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  15489. +
  15490. + s->strm = strm; /* just in case */
  15491. + old_flush = s->last_flush;
  15492. + s->last_flush = flush;
  15493. +
  15494. + /* Write the zlib header */
  15495. + if (s->status == INIT_STATE) {
  15496. +
  15497. + uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  15498. + uInt level_flags = (s->level-1) >> 1;
  15499. +
  15500. + if (level_flags > 3) level_flags = 3;
  15501. + header |= (level_flags << 6);
  15502. + if (s->strstart != 0) header |= PRESET_DICT;
  15503. + header += 31 - (header % 31);
  15504. +
  15505. + s->status = BUSY_STATE;
  15506. + putShortMSB(s, header);
  15507. +
  15508. + /* Save the adler32 of the preset dictionary: */
  15509. + if (s->strstart != 0) {
  15510. + putShortMSB(s, (uInt)(strm->adler >> 16));
  15511. + putShortMSB(s, (uInt)(strm->adler & 0xffff));
  15512. + }
  15513. + strm->adler = 1L;
  15514. + }
  15515. +
  15516. + /* Flush as much pending output as possible */
  15517. + if (s->pending != 0) {
  15518. + flush_pending(strm);
  15519. + if (strm->avail_out == 0) {
  15520. + /* Since avail_out is 0, deflate will be called again with
  15521. + * more output space, but possibly with both pending and
  15522. + * avail_in equal to zero. There won't be anything to do,
  15523. + * but this is not an error situation so make sure we
  15524. + * return OK instead of BUF_ERROR at next call of deflate:
  15525. + */
  15526. + s->last_flush = -1;
  15527. + return Z_OK;
  15528. + }
  15529. +
  15530. + /* Make sure there is something to do and avoid duplicate consecutive
  15531. + * flushes. For repeated and useless calls with Z_FINISH, we keep
  15532. + * returning Z_STREAM_END instead of Z_BUFF_ERROR.
  15533. + */
  15534. + } else if (strm->avail_in == 0 && flush <= old_flush &&
  15535. + flush != Z_FINISH) {
  15536. + ERR_RETURN(strm, Z_BUF_ERROR);
  15537. + }
  15538. +
  15539. + /* User must not provide more input after the first FINISH: */
  15540. + if (s->status == FINISH_STATE && strm->avail_in != 0) {
  15541. + ERR_RETURN(strm, Z_BUF_ERROR);
  15542. + }
  15543. +
  15544. + /* Start a new block or continue the current one.
  15545. + */
  15546. + if (strm->avail_in != 0 || s->lookahead != 0 ||
  15547. + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  15548. + block_state bstate;
  15549. +
  15550. + bstate = (*(configuration_table[s->level].func))(s, flush);
  15551. +
  15552. + if (bstate == finish_started || bstate == finish_done) {
  15553. + s->status = FINISH_STATE;
  15554. + }
  15555. + if (bstate == need_more || bstate == finish_started) {
  15556. + if (strm->avail_out == 0) {
  15557. + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  15558. + }
  15559. + return Z_OK;
  15560. + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  15561. + * of deflate should use the same flush parameter to make sure
  15562. + * that the flush is complete. So we don't have to output an
  15563. + * empty block here, this will be done at next call. This also
  15564. + * ensures that for a very small output buffer, we emit at most
  15565. + * one empty block.
  15566. + */
  15567. + }
  15568. + if (bstate == block_done) {
  15569. + if (flush == Z_PARTIAL_FLUSH) {
  15570. + _tr_align(s);
  15571. + } else { /* FULL_FLUSH or SYNC_FLUSH */
  15572. + _tr_stored_block(s, (char*)0, 0L, 0);
  15573. + /* For a full flush, this empty block will be recognized
  15574. + * as a special marker by inflate_sync().
  15575. + */
  15576. + if (flush == Z_FULL_FLUSH) {
  15577. + CLEAR_HASH(s); /* forget history */
  15578. + }
  15579. + }
  15580. + flush_pending(strm);
  15581. + if (strm->avail_out == 0) {
  15582. + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  15583. + return Z_OK;
  15584. + }
  15585. + }
  15586. + }
  15587. + Assert(strm->avail_out > 0, "bug2");
  15588. +
  15589. + if (flush != Z_FINISH) return Z_OK;
  15590. + if (s->noheader) return Z_STREAM_END;
  15591. +
  15592. + /* Write the zlib trailer (adler32) */
  15593. + putShortMSB(s, (uInt)(strm->adler >> 16));
  15594. + putShortMSB(s, (uInt)(strm->adler & 0xffff));
  15595. + flush_pending(strm);
  15596. + /* If avail_out is zero, the application will call deflate again
  15597. + * to flush the rest.
  15598. + */
  15599. + s->noheader = -1; /* write the trailer only once! */
  15600. + return s->pending != 0 ? Z_OK : Z_STREAM_END;
  15601. +}
  15602. +
  15603. +/* ========================================================================= */
  15604. +int ZEXPORT deflateEnd (strm)
  15605. + z_streamp strm;
  15606. +{
  15607. + int status;
  15608. +
  15609. + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  15610. +
  15611. + status = strm->state->status;
  15612. + if (status != INIT_STATE && status != BUSY_STATE &&
  15613. + status != FINISH_STATE) {
  15614. + return Z_STREAM_ERROR;
  15615. + }
  15616. +
  15617. + /* Deallocate in reverse order of allocations: */
  15618. + TRY_FREE(strm, strm->state->pending_buf);
  15619. + TRY_FREE(strm, strm->state->head);
  15620. + TRY_FREE(strm, strm->state->prev);
  15621. + TRY_FREE(strm, strm->state->window);
  15622. +
  15623. + ZFREE(strm, strm->state);
  15624. + strm->state = Z_NULL;
  15625. +
  15626. + return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  15627. +}
  15628. +
  15629. +/* =========================================================================
  15630. + * Copy the source state to the destination state.
  15631. + * To simplify the source, this is not supported for 16-bit MSDOS (which
  15632. + * doesn't have enough memory anyway to duplicate compression states).
  15633. + */
  15634. +int ZEXPORT deflateCopy (dest, source)
  15635. + z_streamp dest;
  15636. + z_streamp source;
  15637. +{
  15638. +#ifdef MAXSEG_64K
  15639. + return Z_STREAM_ERROR;
  15640. +#else
  15641. + deflate_state *ds;
  15642. + deflate_state *ss;
  15643. + ushf *overlay;
  15644. +
  15645. +
  15646. + if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
  15647. + return Z_STREAM_ERROR;
  15648. + }
  15649. +
  15650. + ss = source->state;
  15651. +
  15652. + *dest = *source;
  15653. +
  15654. + ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
  15655. + if (ds == Z_NULL) return Z_MEM_ERROR;
  15656. + dest->state = (struct internal_state FAR *) ds;
  15657. + *ds = *ss;
  15658. + ds->strm = dest;
  15659. +
  15660. + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
  15661. + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
  15662. + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
  15663. + overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
  15664. + ds->pending_buf = (uchf *) overlay;
  15665. +
  15666. + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
  15667. + ds->pending_buf == Z_NULL) {
  15668. + deflateEnd (dest);
  15669. + return Z_MEM_ERROR;
  15670. + }
  15671. + /* following zmemcpy do not work for 16-bit MSDOS */
  15672. + zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  15673. + zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
  15674. + zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
  15675. + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  15676. +
  15677. + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  15678. + ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
  15679. + ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
  15680. +
  15681. + ds->l_desc.dyn_tree = ds->dyn_ltree;
  15682. + ds->d_desc.dyn_tree = ds->dyn_dtree;
  15683. + ds->bl_desc.dyn_tree = ds->bl_tree;
  15684. +
  15685. + return Z_OK;
  15686. +#endif
  15687. +}
  15688. +
  15689. +/* ===========================================================================
  15690. + * Read a new buffer from the current input stream, update the adler32
  15691. + * and total number of bytes read. All deflate() input goes through
  15692. + * this function so some applications may wish to modify it to avoid
  15693. + * allocating a large strm->next_in buffer and copying from it.
  15694. + * (See also flush_pending()).
  15695. + */
  15696. +local int read_buf(strm, buf, size)
  15697. + z_streamp strm;
  15698. + Bytef *buf;
  15699. + unsigned size;
  15700. +{
  15701. + unsigned len = strm->avail_in;
  15702. +
  15703. + if (len > size) len = size;
  15704. + if (len == 0) return 0;
  15705. +
  15706. + strm->avail_in -= len;
  15707. +
  15708. + if (!strm->state->noheader) {
  15709. + strm->adler = adler32(strm->adler, strm->next_in, len);
  15710. + }
  15711. + zmemcpy(buf, strm->next_in, len);
  15712. + strm->next_in += len;
  15713. + strm->total_in += len;
  15714. +
  15715. + return (int)len;
  15716. +}
  15717. +
  15718. +/* ===========================================================================
  15719. + * Initialize the "longest match" routines for a new zlib stream
  15720. + */
  15721. +local void lm_init (s)
  15722. + deflate_state *s;
  15723. +{
  15724. + s->window_size = (ulg)2L*s->w_size;
  15725. +
  15726. + CLEAR_HASH(s);
  15727. +
  15728. + /* Set the default configuration parameters:
  15729. + */
  15730. + s->max_lazy_match = configuration_table[s->level].max_lazy;
  15731. + s->good_match = configuration_table[s->level].good_length;
  15732. + s->nice_match = configuration_table[s->level].nice_length;
  15733. + s->max_chain_length = configuration_table[s->level].max_chain;
  15734. +
  15735. + s->strstart = 0;
  15736. + s->block_start = 0L;
  15737. + s->lookahead = 0;
  15738. + s->match_length = s->prev_length = MIN_MATCH-1;
  15739. + s->match_available = 0;
  15740. + s->ins_h = 0;
  15741. +#ifdef ASMV
  15742. + match_init(); /* initialize the asm code */
  15743. +#endif
  15744. +}
  15745. +
  15746. +/* ===========================================================================
  15747. + * Set match_start to the longest match starting at the given string and
  15748. + * return its length. Matches shorter or equal to prev_length are discarded,
  15749. + * in which case the result is equal to prev_length and match_start is
  15750. + * garbage.
  15751. + * IN assertions: cur_match is the head of the hash chain for the current
  15752. + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  15753. + * OUT assertion: the match length is not greater than s->lookahead.
  15754. + */
  15755. +#ifndef ASMV
  15756. +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  15757. + * match.S. The code will be functionally equivalent.
  15758. + */
  15759. +#ifndef FASTEST
  15760. +local uInt longest_match(s, cur_match)
  15761. + deflate_state *s;
  15762. + IPos cur_match; /* current match */
  15763. +{
  15764. + unsigned chain_length = s->max_chain_length;/* max hash chain length */
  15765. + register Bytef *scan = s->window + s->strstart; /* current string */
  15766. + register Bytef *match; /* matched string */
  15767. + register int len; /* length of current match */
  15768. + int best_len = s->prev_length; /* best match length so far */
  15769. + int nice_match = s->nice_match; /* stop if match long enough */
  15770. + IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  15771. + s->strstart - (IPos)MAX_DIST(s) : NIL;
  15772. + /* Stop when cur_match becomes <= limit. To simplify the code,
  15773. + * we prevent matches with the string of window index 0.
  15774. + */
  15775. + Posf *prev = s->prev;
  15776. + uInt wmask = s->w_mask;
  15777. +
  15778. +#ifdef UNALIGNED_OK
  15779. + /* Compare two bytes at a time. Note: this is not always beneficial.
  15780. + * Try with and without -DUNALIGNED_OK to check.
  15781. + */
  15782. + register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  15783. + register ush scan_start = *(ushf*)scan;
  15784. + register ush scan_end = *(ushf*)(scan+best_len-1);
  15785. +#else
  15786. + register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  15787. + register Byte scan_end1 = scan[best_len-1];
  15788. + register Byte scan_end = scan[best_len];
  15789. +#endif
  15790. +
  15791. + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  15792. + * It is easy to get rid of this optimization if necessary.
  15793. + */
  15794. + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  15795. +
  15796. + /* Do not waste too much time if we already have a good match: */
  15797. + if (s->prev_length >= s->good_match) {
  15798. + chain_length >>= 2;
  15799. + }
  15800. + /* Do not look for matches beyond the end of the input. This is necessary
  15801. + * to make deflate deterministic.
  15802. + */
  15803. + if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  15804. +
  15805. + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  15806. +
  15807. + do {
  15808. + Assert(cur_match < s->strstart, "no future");
  15809. + match = s->window + cur_match;
  15810. +
  15811. + /* Skip to next match if the match length cannot increase
  15812. + * or if the match length is less than 2:
  15813. + */
  15814. +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  15815. + /* This code assumes sizeof(unsigned short) == 2. Do not use
  15816. + * UNALIGNED_OK if your compiler uses a different size.
  15817. + */
  15818. + if (*(ushf*)(match+best_len-1) != scan_end ||
  15819. + *(ushf*)match != scan_start) continue;
  15820. +
  15821. + /* It is not necessary to compare scan[2] and match[2] since they are
  15822. + * always equal when the other bytes match, given that the hash keys
  15823. + * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  15824. + * strstart+3, +5, ... up to strstart+257. We check for insufficient
  15825. + * lookahead only every 4th comparison; the 128th check will be made
  15826. + * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  15827. + * necessary to put more guard bytes at the end of the window, or
  15828. + * to check more often for insufficient lookahead.
  15829. + */
  15830. + Assert(scan[2] == match[2], "scan[2]?");
  15831. + scan++, match++;
  15832. + do {
  15833. + } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15834. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15835. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15836. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15837. + scan < strend);
  15838. + /* The funny "do {}" generates better code on most compilers */
  15839. +
  15840. + /* Here, scan <= window+strstart+257 */
  15841. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15842. + if (*scan == *match) scan++;
  15843. +
  15844. + len = (MAX_MATCH - 1) - (int)(strend-scan);
  15845. + scan = strend - (MAX_MATCH-1);
  15846. +
  15847. +#else /* UNALIGNED_OK */
  15848. +
  15849. + if (match[best_len] != scan_end ||
  15850. + match[best_len-1] != scan_end1 ||
  15851. + *match != *scan ||
  15852. + *++match != scan[1]) continue;
  15853. +
  15854. + /* The check at best_len-1 can be removed because it will be made
  15855. + * again later. (This heuristic is not always a win.)
  15856. + * It is not necessary to compare scan[2] and match[2] since they
  15857. + * are always equal when the other bytes match, given that
  15858. + * the hash keys are equal and that HASH_BITS >= 8.
  15859. + */
  15860. + scan += 2, match++;
  15861. + Assert(*scan == *match, "match[2]?");
  15862. +
  15863. + /* We check for insufficient lookahead only every 8th comparison;
  15864. + * the 256th check will be made at strstart+258.
  15865. + */
  15866. + do {
  15867. + } while (*++scan == *++match && *++scan == *++match &&
  15868. + *++scan == *++match && *++scan == *++match &&
  15869. + *++scan == *++match && *++scan == *++match &&
  15870. + *++scan == *++match && *++scan == *++match &&
  15871. + scan < strend);
  15872. +
  15873. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15874. +
  15875. + len = MAX_MATCH - (int)(strend - scan);
  15876. + scan = strend - MAX_MATCH;
  15877. +
  15878. +#endif /* UNALIGNED_OK */
  15879. +
  15880. + if (len > best_len) {
  15881. + s->match_start = cur_match;
  15882. + best_len = len;
  15883. + if (len >= nice_match) break;
  15884. +#ifdef UNALIGNED_OK
  15885. + scan_end = *(ushf*)(scan+best_len-1);
  15886. +#else
  15887. + scan_end1 = scan[best_len-1];
  15888. + scan_end = scan[best_len];
  15889. +#endif
  15890. + }
  15891. + } while ((cur_match = prev[cur_match & wmask]) > limit
  15892. + && --chain_length != 0);
  15893. +
  15894. + if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  15895. + return s->lookahead;
  15896. +}
  15897. +
  15898. +#else /* FASTEST */
  15899. +/* ---------------------------------------------------------------------------
  15900. + * Optimized version for level == 1 only
  15901. + */
  15902. +local uInt longest_match(s, cur_match)
  15903. + deflate_state *s;
  15904. + IPos cur_match; /* current match */
  15905. +{
  15906. + register Bytef *scan = s->window + s->strstart; /* current string */
  15907. + register Bytef *match; /* matched string */
  15908. + register int len; /* length of current match */
  15909. + register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  15910. +
  15911. + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  15912. + * It is easy to get rid of this optimization if necessary.
  15913. + */
  15914. + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  15915. +
  15916. + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  15917. +
  15918. + Assert(cur_match < s->strstart, "no future");
  15919. +
  15920. + match = s->window + cur_match;
  15921. +
  15922. + /* Return failure if the match length is less than 2:
  15923. + */
  15924. + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  15925. +
  15926. + /* The check at best_len-1 can be removed because it will be made
  15927. + * again later. (This heuristic is not always a win.)
  15928. + * It is not necessary to compare scan[2] and match[2] since they
  15929. + * are always equal when the other bytes match, given that
  15930. + * the hash keys are equal and that HASH_BITS >= 8.
  15931. + */
  15932. + scan += 2, match += 2;
  15933. + Assert(*scan == *match, "match[2]?");
  15934. +
  15935. + /* We check for insufficient lookahead only every 8th comparison;
  15936. + * the 256th check will be made at strstart+258.
  15937. + */
  15938. + do {
  15939. + } while (*++scan == *++match && *++scan == *++match &&
  15940. + *++scan == *++match && *++scan == *++match &&
  15941. + *++scan == *++match && *++scan == *++match &&
  15942. + *++scan == *++match && *++scan == *++match &&
  15943. + scan < strend);
  15944. +
  15945. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15946. +
  15947. + len = MAX_MATCH - (int)(strend - scan);
  15948. +
  15949. + if (len < MIN_MATCH) return MIN_MATCH - 1;
  15950. +
  15951. + s->match_start = cur_match;
  15952. + return len <= s->lookahead ? len : s->lookahead;
  15953. +}
  15954. +#endif /* FASTEST */
  15955. +#endif /* ASMV */
  15956. +
  15957. +#ifdef DEBUG
  15958. +/* ===========================================================================
  15959. + * Check that the match at match_start is indeed a match.
  15960. + */
  15961. +local void check_match(s, start, match, length)
  15962. + deflate_state *s;
  15963. + IPos start, match;
  15964. + int length;
  15965. +{
  15966. + /* check that the match is indeed a match */
  15967. + if (zmemcmp(s->window + match,
  15968. + s->window + start, length) != EQUAL) {
  15969. + fprintf(stderr, " start %u, match %u, length %d\n",
  15970. + start, match, length);
  15971. + do {
  15972. + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  15973. + } while (--length != 0);
  15974. + z_error("invalid match");
  15975. + }
  15976. + if (z_verbose > 1) {
  15977. + fprintf(stderr,"\\[%d,%d]", start-match, length);
  15978. + do { putc(s->window[start++], stderr); } while (--length != 0);
  15979. + }
  15980. +}
  15981. +#else
  15982. +# define check_match(s, start, match, length)
  15983. +#endif
  15984. +
  15985. +/* ===========================================================================
  15986. + * Fill the window when the lookahead becomes insufficient.
  15987. + * Updates strstart and lookahead.
  15988. + *
  15989. + * IN assertion: lookahead < MIN_LOOKAHEAD
  15990. + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  15991. + * At least one byte has been read, or avail_in == 0; reads are
  15992. + * performed for at least two bytes (required for the zip translate_eol
  15993. + * option -- not supported here).
  15994. + */
  15995. +local void fill_window(s)
  15996. + deflate_state *s;
  15997. +{
  15998. + register unsigned n, m;
  15999. + register Posf *p;
  16000. + unsigned more; /* Amount of free space at the end of the window. */
  16001. + uInt wsize = s->w_size;
  16002. +
  16003. + do {
  16004. + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  16005. +
  16006. + /* Deal with !@#$% 64K limit: */
  16007. + if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  16008. + more = wsize;
  16009. +
  16010. + } else if (more == (unsigned)(-1)) {
  16011. + /* Very unlikely, but possible on 16 bit machine if strstart == 0
  16012. + * and lookahead == 1 (input done one byte at time)
  16013. + */
  16014. + more--;
  16015. +
  16016. + /* If the window is almost full and there is insufficient lookahead,
  16017. + * move the upper half to the lower one to make room in the upper half.
  16018. + */
  16019. + } else if (s->strstart >= wsize+MAX_DIST(s)) {
  16020. +
  16021. + zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
  16022. + s->match_start -= wsize;
  16023. + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  16024. + s->block_start -= (long) wsize;
  16025. +
  16026. + /* Slide the hash table (could be avoided with 32 bit values
  16027. + at the expense of memory usage). We slide even when level == 0
  16028. + to keep the hash table consistent if we switch back to level > 0
  16029. + later. (Using level 0 permanently is not an optimal usage of
  16030. + zlib, so we don't care about this pathological case.)
  16031. + */
  16032. + n = s->hash_size;
  16033. + p = &s->head[n];
  16034. + do {
  16035. + m = *--p;
  16036. + *p = (Pos)(m >= wsize ? m-wsize : NIL);
  16037. + } while (--n);
  16038. +
  16039. + n = wsize;
  16040. +#ifndef FASTEST
  16041. + p = &s->prev[n];
  16042. + do {
  16043. + m = *--p;
  16044. + *p = (Pos)(m >= wsize ? m-wsize : NIL);
  16045. + /* If n is not on any hash chain, prev[n] is garbage but
  16046. + * its value will never be used.
  16047. + */
  16048. + } while (--n);
  16049. +#endif
  16050. + more += wsize;
  16051. + }
  16052. + if (s->strm->avail_in == 0) return;
  16053. +
  16054. + /* If there was no sliding:
  16055. + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  16056. + * more == window_size - lookahead - strstart
  16057. + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  16058. + * => more >= window_size - 2*WSIZE + 2
  16059. + * In the BIG_MEM or MMAP case (not yet supported),
  16060. + * window_size == input_size + MIN_LOOKAHEAD &&
  16061. + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  16062. + * Otherwise, window_size == 2*WSIZE so more >= 2.
  16063. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  16064. + */
  16065. + Assert(more >= 2, "more < 2");
  16066. +
  16067. + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  16068. + s->lookahead += n;
  16069. +
  16070. + /* Initialize the hash value now that we have some input: */
  16071. + if (s->lookahead >= MIN_MATCH) {
  16072. + s->ins_h = s->window[s->strstart];
  16073. + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  16074. +#if MIN_MATCH != 3
  16075. + Call UPDATE_HASH() MIN_MATCH-3 more times
  16076. +#endif
  16077. + }
  16078. + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  16079. + * but this is not important since only literal bytes will be emitted.
  16080. + */
  16081. +
  16082. + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  16083. +}
  16084. +
  16085. +/* ===========================================================================
  16086. + * Flush the current block, with given end-of-file flag.
  16087. + * IN assertion: strstart is set to the end of the current match.
  16088. + */
  16089. +#define FLUSH_BLOCK_ONLY(s, eof) { \
  16090. + _tr_flush_block(s, (s->block_start >= 0L ? \
  16091. + (charf *)&s->window[(unsigned)s->block_start] : \
  16092. + (charf *)Z_NULL), \
  16093. + (ulg)((long)s->strstart - s->block_start), \
  16094. + (eof)); \
  16095. + s->block_start = s->strstart; \
  16096. + flush_pending(s->strm); \
  16097. + Tracev((stderr,"[FLUSH]")); \
  16098. +}
  16099. +
  16100. +/* Same but force premature exit if necessary. */
  16101. +#define FLUSH_BLOCK(s, eof) { \
  16102. + FLUSH_BLOCK_ONLY(s, eof); \
  16103. + if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  16104. +}
  16105. +
  16106. +/* ===========================================================================
  16107. + * Copy without compression as much as possible from the input stream, return
  16108. + * the current block state.
  16109. + * This function does not insert new strings in the dictionary since
  16110. + * uncompressible data is probably not useful. This function is used
  16111. + * only for the level=0 compression option.
  16112. + * NOTE: this function should be optimized to avoid extra copying from
  16113. + * window to pending_buf.
  16114. + */
  16115. +local block_state deflate_stored(s, flush)
  16116. + deflate_state *s;
  16117. + int flush;
  16118. +{
  16119. + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  16120. + * to pending_buf_size, and each stored block has a 5 byte header:
  16121. + */
  16122. + ulg max_block_size = 0xffff;
  16123. + ulg max_start;
  16124. +
  16125. + if (max_block_size > s->pending_buf_size - 5) {
  16126. + max_block_size = s->pending_buf_size - 5;
  16127. + }
  16128. +
  16129. + /* Copy as much as possible from input to output: */
  16130. + for (;;) {
  16131. + /* Fill the window as much as possible: */
  16132. + if (s->lookahead <= 1) {
  16133. +
  16134. + Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  16135. + s->block_start >= (long)s->w_size, "slide too late");
  16136. +
  16137. + fill_window(s);
  16138. + if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  16139. +
  16140. + if (s->lookahead == 0) break; /* flush the current block */
  16141. + }
  16142. + Assert(s->block_start >= 0L, "block gone");
  16143. +
  16144. + s->strstart += s->lookahead;
  16145. + s->lookahead = 0;
  16146. +
  16147. + /* Emit a stored block if pending_buf will be full: */
  16148. + max_start = s->block_start + max_block_size;
  16149. + if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  16150. + /* strstart == 0 is possible when wraparound on 16-bit machine */
  16151. + s->lookahead = (uInt)(s->strstart - max_start);
  16152. + s->strstart = (uInt)max_start;
  16153. + FLUSH_BLOCK(s, 0);
  16154. + }
  16155. + /* Flush if we may have to slide, otherwise block_start may become
  16156. + * negative and the data will be gone:
  16157. + */
  16158. + if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  16159. + FLUSH_BLOCK(s, 0);
  16160. + }
  16161. + }
  16162. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16163. + return flush == Z_FINISH ? finish_done : block_done;
  16164. +}
  16165. +
  16166. +/* ===========================================================================
  16167. + * Compress as much as possible from the input stream, return the current
  16168. + * block state.
  16169. + * This function does not perform lazy evaluation of matches and inserts
  16170. + * new strings in the dictionary only for unmatched strings or for short
  16171. + * matches. It is used only for the fast compression options.
  16172. + */
  16173. +local block_state deflate_fast(s, flush)
  16174. + deflate_state *s;
  16175. + int flush;
  16176. +{
  16177. + IPos hash_head = NIL; /* head of the hash chain */
  16178. + int bflush; /* set if current block must be flushed */
  16179. +
  16180. + for (;;) {
  16181. + /* Make sure that we always have enough lookahead, except
  16182. + * at the end of the input file. We need MAX_MATCH bytes
  16183. + * for the next match, plus MIN_MATCH bytes to insert the
  16184. + * string following the next match.
  16185. + */
  16186. + if (s->lookahead < MIN_LOOKAHEAD) {
  16187. + fill_window(s);
  16188. + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  16189. + return need_more;
  16190. + }
  16191. + if (s->lookahead == 0) break; /* flush the current block */
  16192. + }
  16193. +
  16194. + /* Insert the string window[strstart .. strstart+2] in the
  16195. + * dictionary, and set hash_head to the head of the hash chain:
  16196. + */
  16197. + if (s->lookahead >= MIN_MATCH) {
  16198. + INSERT_STRING(s, s->strstart, hash_head);
  16199. + }
  16200. +
  16201. + /* Find the longest match, discarding those <= prev_length.
  16202. + * At this point we have always match_length < MIN_MATCH
  16203. + */
  16204. + if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  16205. + /* To simplify the code, we prevent matches with the string
  16206. + * of window index 0 (in particular we have to avoid a match
  16207. + * of the string with itself at the start of the input file).
  16208. + */
  16209. + if (s->strategy != Z_HUFFMAN_ONLY) {
  16210. + s->match_length = longest_match (s, hash_head);
  16211. + }
  16212. + /* longest_match() sets match_start */
  16213. + }
  16214. + if (s->match_length >= MIN_MATCH) {
  16215. + check_match(s, s->strstart, s->match_start, s->match_length);
  16216. +
  16217. + _tr_tally_dist(s, s->strstart - s->match_start,
  16218. + s->match_length - MIN_MATCH, bflush);
  16219. +
  16220. + s->lookahead -= s->match_length;
  16221. +
  16222. + /* Insert new strings in the hash table only if the match length
  16223. + * is not too large. This saves time but degrades compression.
  16224. + */
  16225. +#ifndef FASTEST
  16226. + if (s->match_length <= s->max_insert_length &&
  16227. + s->lookahead >= MIN_MATCH) {
  16228. + s->match_length--; /* string at strstart already in hash table */
  16229. + do {
  16230. + s->strstart++;
  16231. + INSERT_STRING(s, s->strstart, hash_head);
  16232. + /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  16233. + * always MIN_MATCH bytes ahead.
  16234. + */
  16235. + } while (--s->match_length != 0);
  16236. + s->strstart++;
  16237. + } else
  16238. +#endif
  16239. + {
  16240. + s->strstart += s->match_length;
  16241. + s->match_length = 0;
  16242. + s->ins_h = s->window[s->strstart];
  16243. + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  16244. +#if MIN_MATCH != 3
  16245. + Call UPDATE_HASH() MIN_MATCH-3 more times
  16246. +#endif
  16247. + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  16248. + * matter since it will be recomputed at next deflate call.
  16249. + */
  16250. + }
  16251. + } else {
  16252. + /* No match, output a literal byte */
  16253. + Tracevv((stderr,"%c", s->window[s->strstart]));
  16254. + _tr_tally_lit (s, s->window[s->strstart], bflush);
  16255. + s->lookahead--;
  16256. + s->strstart++;
  16257. + }
  16258. + if (bflush) FLUSH_BLOCK(s, 0);
  16259. + }
  16260. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16261. + return flush == Z_FINISH ? finish_done : block_done;
  16262. +}
  16263. +
  16264. +/* ===========================================================================
  16265. + * Same as above, but achieves better compression. We use a lazy
  16266. + * evaluation for matches: a match is finally adopted only if there is
  16267. + * no better match at the next window position.
  16268. + */
  16269. +local block_state deflate_slow(s, flush)
  16270. + deflate_state *s;
  16271. + int flush;
  16272. +{
  16273. + IPos hash_head = NIL; /* head of hash chain */
  16274. + int bflush; /* set if current block must be flushed */
  16275. +
  16276. + /* Process the input block. */
  16277. + for (;;) {
  16278. + /* Make sure that we always have enough lookahead, except
  16279. + * at the end of the input file. We need MAX_MATCH bytes
  16280. + * for the next match, plus MIN_MATCH bytes to insert the
  16281. + * string following the next match.
  16282. + */
  16283. + if (s->lookahead < MIN_LOOKAHEAD) {
  16284. + fill_window(s);
  16285. + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  16286. + return need_more;
  16287. + }
  16288. + if (s->lookahead == 0) break; /* flush the current block */
  16289. + }
  16290. +
  16291. + /* Insert the string window[strstart .. strstart+2] in the
  16292. + * dictionary, and set hash_head to the head of the hash chain:
  16293. + */
  16294. + if (s->lookahead >= MIN_MATCH) {
  16295. + INSERT_STRING(s, s->strstart, hash_head);
  16296. + }
  16297. +
  16298. + /* Find the longest match, discarding those <= prev_length.
  16299. + */
  16300. + s->prev_length = s->match_length, s->prev_match = s->match_start;
  16301. + s->match_length = MIN_MATCH-1;
  16302. +
  16303. + if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  16304. + s->strstart - hash_head <= MAX_DIST(s)) {
  16305. + /* To simplify the code, we prevent matches with the string
  16306. + * of window index 0 (in particular we have to avoid a match
  16307. + * of the string with itself at the start of the input file).
  16308. + */
  16309. + if (s->strategy != Z_HUFFMAN_ONLY) {
  16310. + s->match_length = longest_match (s, hash_head);
  16311. + }
  16312. + /* longest_match() sets match_start */
  16313. +
  16314. + if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
  16315. + (s->match_length == MIN_MATCH &&
  16316. + s->strstart - s->match_start > TOO_FAR))) {
  16317. +
  16318. + /* If prev_match is also MIN_MATCH, match_start is garbage
  16319. + * but we will ignore the current match anyway.
  16320. + */
  16321. + s->match_length = MIN_MATCH-1;
  16322. + }
  16323. + }
  16324. + /* If there was a match at the previous step and the current
  16325. + * match is not better, output the previous match:
  16326. + */
  16327. + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  16328. + uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  16329. + /* Do not insert strings in hash table beyond this. */
  16330. +
  16331. + check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  16332. +
  16333. + _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  16334. + s->prev_length - MIN_MATCH, bflush);
  16335. +
  16336. + /* Insert in hash table all strings up to the end of the match.
  16337. + * strstart-1 and strstart are already inserted. If there is not
  16338. + * enough lookahead, the last two strings are not inserted in
  16339. + * the hash table.
  16340. + */
  16341. + s->lookahead -= s->prev_length-1;
  16342. + s->prev_length -= 2;
  16343. + do {
  16344. + if (++s->strstart <= max_insert) {
  16345. + INSERT_STRING(s, s->strstart, hash_head);
  16346. + }
  16347. + } while (--s->prev_length != 0);
  16348. + s->match_available = 0;
  16349. + s->match_length = MIN_MATCH-1;
  16350. + s->strstart++;
  16351. +
  16352. + if (bflush) FLUSH_BLOCK(s, 0);
  16353. +
  16354. + } else if (s->match_available) {
  16355. + /* If there was no match at the previous position, output a
  16356. + * single literal. If there was a match but the current match
  16357. + * is longer, truncate the previous match to a single literal.
  16358. + */
  16359. + Tracevv((stderr,"%c", s->window[s->strstart-1]));
  16360. + _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  16361. + if (bflush) {
  16362. + FLUSH_BLOCK_ONLY(s, 0);
  16363. + }
  16364. + s->strstart++;
  16365. + s->lookahead--;
  16366. + if (s->strm->avail_out == 0) return need_more;
  16367. + } else {
  16368. + /* There is no previous match to compare with, wait for
  16369. + * the next step to decide.
  16370. + */
  16371. + s->match_available = 1;
  16372. + s->strstart++;
  16373. + s->lookahead--;
  16374. + }
  16375. + }
  16376. + Assert (flush != Z_NO_FLUSH, "no flush?");
  16377. + if (s->match_available) {
  16378. + Tracevv((stderr,"%c", s->window[s->strstart-1]));
  16379. + _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  16380. + s->match_available = 0;
  16381. + }
  16382. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16383. + return flush == Z_FINISH ? finish_done : block_done;
  16384. +}
  16385. --- /dev/null Tue Mar 11 13:02:56 2003
  16386. +++ linux/net/ipsec/deflate.h Mon Feb 9 13:51:03 2004
  16387. @@ -0,0 +1,318 @@
  16388. +/* deflate.h -- internal compression state
  16389. + * Copyright (C) 1995-2002 Jean-loup Gailly
  16390. + * For conditions of distribution and use, see copyright notice in zlib.h
  16391. + */
  16392. +
  16393. +/* WARNING: this file should *not* be used by applications. It is
  16394. + part of the implementation of the compression library and is
  16395. + subject to change. Applications should only use zlib.h.
  16396. + */
  16397. +
  16398. +/* @(#) $Id: deflate.h,v 1.5 2004/07/10 07:48:38 mcr Exp $ */
  16399. +
  16400. +#ifndef _DEFLATE_H
  16401. +#define _DEFLATE_H
  16402. +
  16403. +#include "zlib/zutil.h"
  16404. +
  16405. +/* ===========================================================================
  16406. + * Internal compression state.
  16407. + */
  16408. +
  16409. +#define LENGTH_CODES 29
  16410. +/* number of length codes, not counting the special END_BLOCK code */
  16411. +
  16412. +#define LITERALS 256
  16413. +/* number of literal bytes 0..255 */
  16414. +
  16415. +#define L_CODES (LITERALS+1+LENGTH_CODES)
  16416. +/* number of Literal or Length codes, including the END_BLOCK code */
  16417. +
  16418. +#define D_CODES 30
  16419. +/* number of distance codes */
  16420. +
  16421. +#define BL_CODES 19
  16422. +/* number of codes used to transfer the bit lengths */
  16423. +
  16424. +#define HEAP_SIZE (2*L_CODES+1)
  16425. +/* maximum heap size */
  16426. +
  16427. +#define MAX_BITS 15
  16428. +/* All codes must not exceed MAX_BITS bits */
  16429. +
  16430. +#define INIT_STATE 42
  16431. +#define BUSY_STATE 113
  16432. +#define FINISH_STATE 666
  16433. +/* Stream status */
  16434. +
  16435. +
  16436. +/* Data structure describing a single value and its code string. */
  16437. +typedef struct ct_data_s {
  16438. + union {
  16439. + ush freq; /* frequency count */
  16440. + ush code; /* bit string */
  16441. + } fc;
  16442. + union {
  16443. + ush dad; /* father node in Huffman tree */
  16444. + ush len; /* length of bit string */
  16445. + } dl;
  16446. +} FAR ct_data;
  16447. +
  16448. +#define Freq fc.freq
  16449. +#define Code fc.code
  16450. +#define Dad dl.dad
  16451. +#define Len dl.len
  16452. +
  16453. +typedef struct static_tree_desc_s static_tree_desc;
  16454. +
  16455. +typedef struct tree_desc_s {
  16456. + ct_data *dyn_tree; /* the dynamic tree */
  16457. + int max_code; /* largest code with non zero frequency */
  16458. + static_tree_desc *stat_desc; /* the corresponding static tree */
  16459. +} FAR tree_desc;
  16460. +
  16461. +typedef ush Pos;
  16462. +typedef Pos FAR Posf;
  16463. +typedef unsigned IPos;
  16464. +
  16465. +/* A Pos is an index in the character window. We use short instead of int to
  16466. + * save space in the various tables. IPos is used only for parameter passing.
  16467. + */
  16468. +
  16469. +typedef struct internal_state {
  16470. + z_streamp strm; /* pointer back to this zlib stream */
  16471. + int status; /* as the name implies */
  16472. + Bytef *pending_buf; /* output still pending */
  16473. + ulg pending_buf_size; /* size of pending_buf */
  16474. + Bytef *pending_out; /* next pending byte to output to the stream */
  16475. + int pending; /* nb of bytes in the pending buffer */
  16476. + int noheader; /* suppress zlib header and adler32 */
  16477. + Byte data_type; /* UNKNOWN, BINARY or ASCII */
  16478. + Byte method; /* STORED (for zip only) or DEFLATED */
  16479. + int last_flush; /* value of flush param for previous deflate call */
  16480. +
  16481. + /* used by deflate.c: */
  16482. +
  16483. + uInt w_size; /* LZ77 window size (32K by default) */
  16484. + uInt w_bits; /* log2(w_size) (8..16) */
  16485. + uInt w_mask; /* w_size - 1 */
  16486. +
  16487. + Bytef *window;
  16488. + /* Sliding window. Input bytes are read into the second half of the window,
  16489. + * and move to the first half later to keep a dictionary of at least wSize
  16490. + * bytes. With this organization, matches are limited to a distance of
  16491. + * wSize-MAX_MATCH bytes, but this ensures that IO is always
  16492. + * performed with a length multiple of the block size. Also, it limits
  16493. + * the window size to 64K, which is quite useful on MSDOS.
  16494. + * To do: use the user input buffer as sliding window.
  16495. + */
  16496. +
  16497. + ulg window_size;
  16498. + /* Actual size of window: 2*wSize, except when the user input buffer
  16499. + * is directly used as sliding window.
  16500. + */
  16501. +
  16502. + Posf *prev;
  16503. + /* Link to older string with same hash index. To limit the size of this
  16504. + * array to 64K, this link is maintained only for the last 32K strings.
  16505. + * An index in this array is thus a window index modulo 32K.
  16506. + */
  16507. +
  16508. + Posf *head; /* Heads of the hash chains or NIL. */
  16509. +
  16510. + uInt ins_h; /* hash index of string to be inserted */
  16511. + uInt hash_size; /* number of elements in hash table */
  16512. + uInt hash_bits; /* log2(hash_size) */
  16513. + uInt hash_mask; /* hash_size-1 */
  16514. +
  16515. + uInt hash_shift;
  16516. + /* Number of bits by which ins_h must be shifted at each input
  16517. + * step. It must be such that after MIN_MATCH steps, the oldest
  16518. + * byte no longer takes part in the hash key, that is:
  16519. + * hash_shift * MIN_MATCH >= hash_bits
  16520. + */
  16521. +
  16522. + long block_start;
  16523. + /* Window position at the beginning of the current output block. Gets
  16524. + * negative when the window is moved backwards.
  16525. + */
  16526. +
  16527. + uInt match_length; /* length of best match */
  16528. + IPos prev_match; /* previous match */
  16529. + int match_available; /* set if previous match exists */
  16530. + uInt strstart; /* start of string to insert */
  16531. + uInt match_start; /* start of matching string */
  16532. + uInt lookahead; /* number of valid bytes ahead in window */
  16533. +
  16534. + uInt prev_length;
  16535. + /* Length of the best match at previous step. Matches not greater than this
  16536. + * are discarded. This is used in the lazy match evaluation.
  16537. + */
  16538. +
  16539. + uInt max_chain_length;
  16540. + /* To speed up deflation, hash chains are never searched beyond this
  16541. + * length. A higher limit improves compression ratio but degrades the
  16542. + * speed.
  16543. + */
  16544. +
  16545. + uInt max_lazy_match;
  16546. + /* Attempt to find a better match only when the current match is strictly
  16547. + * smaller than this value. This mechanism is used only for compression
  16548. + * levels >= 4.
  16549. + */
  16550. +# define max_insert_length max_lazy_match
  16551. + /* Insert new strings in the hash table only if the match length is not
  16552. + * greater than this length. This saves time but degrades compression.
  16553. + * max_insert_length is used only for compression levels <= 3.
  16554. + */
  16555. +
  16556. + int level; /* compression level (1..9) */
  16557. + int strategy; /* favor or force Huffman coding*/
  16558. +
  16559. + uInt good_match;
  16560. + /* Use a faster search when the previous match is longer than this */
  16561. +
  16562. + int nice_match; /* Stop searching when current match exceeds this */
  16563. +
  16564. + /* used by trees.c: */
  16565. + /* Didn't use ct_data typedef below to supress compiler warning */
  16566. + struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  16567. + struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  16568. + struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  16569. +
  16570. + struct tree_desc_s l_desc; /* desc. for literal tree */
  16571. + struct tree_desc_s d_desc; /* desc. for distance tree */
  16572. + struct tree_desc_s bl_desc; /* desc. for bit length tree */
  16573. +
  16574. + ush bl_count[MAX_BITS+1];
  16575. + /* number of codes at each bit length for an optimal tree */
  16576. +
  16577. + int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  16578. + int heap_len; /* number of elements in the heap */
  16579. + int heap_max; /* element of largest frequency */
  16580. + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  16581. + * The same heap array is used to build all trees.
  16582. + */
  16583. +
  16584. + uch depth[2*L_CODES+1];
  16585. + /* Depth of each subtree used as tie breaker for trees of equal frequency
  16586. + */
  16587. +
  16588. + uchf *l_buf; /* buffer for literals or lengths */
  16589. +
  16590. + uInt lit_bufsize;
  16591. + /* Size of match buffer for literals/lengths. There are 4 reasons for
  16592. + * limiting lit_bufsize to 64K:
  16593. + * - frequencies can be kept in 16 bit counters
  16594. + * - if compression is not successful for the first block, all input
  16595. + * data is still in the window so we can still emit a stored block even
  16596. + * when input comes from standard input. (This can also be done for
  16597. + * all blocks if lit_bufsize is not greater than 32K.)
  16598. + * - if compression is not successful for a file smaller than 64K, we can
  16599. + * even emit a stored file instead of a stored block (saving 5 bytes).
  16600. + * This is applicable only for zip (not gzip or zlib).
  16601. + * - creating new Huffman trees less frequently may not provide fast
  16602. + * adaptation to changes in the input data statistics. (Take for
  16603. + * example a binary file with poorly compressible code followed by
  16604. + * a highly compressible string table.) Smaller buffer sizes give
  16605. + * fast adaptation but have of course the overhead of transmitting
  16606. + * trees more frequently.
  16607. + * - I can't count above 4
  16608. + */
  16609. +
  16610. + uInt last_lit; /* running index in l_buf */
  16611. +
  16612. + ushf *d_buf;
  16613. + /* Buffer for distances. To simplify the code, d_buf and l_buf have
  16614. + * the same number of elements. To use different lengths, an extra flag
  16615. + * array would be necessary.
  16616. + */
  16617. +
  16618. + ulg opt_len; /* bit length of current block with optimal trees */
  16619. + ulg static_len; /* bit length of current block with static trees */
  16620. + uInt matches; /* number of string matches in current block */
  16621. + int last_eob_len; /* bit length of EOB code for last block */
  16622. +
  16623. +#ifdef DEBUG
  16624. + ulg compressed_len; /* total bit length of compressed file mod 2^32 */
  16625. + ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
  16626. +#endif
  16627. +
  16628. + ush bi_buf;
  16629. + /* Output buffer. bits are inserted starting at the bottom (least
  16630. + * significant bits).
  16631. + */
  16632. + int bi_valid;
  16633. + /* Number of valid bits in bi_buf. All bits above the last valid bit
  16634. + * are always zero.
  16635. + */
  16636. +
  16637. +} FAR deflate_state;
  16638. +
  16639. +/* Output a byte on the stream.
  16640. + * IN assertion: there is enough room in pending_buf.
  16641. + */
  16642. +#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
  16643. +
  16644. +
  16645. +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  16646. +/* Minimum amount of lookahead, except at the end of the input file.
  16647. + * See deflate.c for comments about the MIN_MATCH+1.
  16648. + */
  16649. +
  16650. +#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
  16651. +/* In order to simplify the code, particularly on 16 bit machines, match
  16652. + * distances are limited to MAX_DIST instead of WSIZE.
  16653. + */
  16654. +
  16655. + /* in trees.c */
  16656. +void _tr_init OF((deflate_state *s));
  16657. +int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
  16658. +void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
  16659. + int eof));
  16660. +void _tr_align OF((deflate_state *s));
  16661. +void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
  16662. + int eof));
  16663. +
  16664. +#define d_code(dist) \
  16665. + ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
  16666. +/* Mapping from a distance to a distance code. dist is the distance - 1 and
  16667. + * must not have side effects. _dist_code[256] and _dist_code[257] are never
  16668. + * used.
  16669. + */
  16670. +
  16671. +#ifndef DEBUG
  16672. +/* Inline versions of _tr_tally for speed: */
  16673. +
  16674. +#if defined(GEN_TREES_H) || !defined(STDC)
  16675. + extern uch _length_code[];
  16676. + extern uch _dist_code[];
  16677. +#else
  16678. + extern const uch _length_code[];
  16679. + extern const uch _dist_code[];
  16680. +#endif
  16681. +
  16682. +# define _tr_tally_lit(s, c, flush) \
  16683. + { uch cc = (c); \
  16684. + s->d_buf[s->last_lit] = 0; \
  16685. + s->l_buf[s->last_lit++] = cc; \
  16686. + s->dyn_ltree[cc].Freq++; \
  16687. + flush = (s->last_lit == s->lit_bufsize-1); \
  16688. + }
  16689. +# define _tr_tally_dist(s, distance, length, flush) \
  16690. + { uch len = (length); \
  16691. + ush dist = (distance); \
  16692. + s->d_buf[s->last_lit] = dist; \
  16693. + s->l_buf[s->last_lit++] = len; \
  16694. + dist--; \
  16695. + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
  16696. + s->dyn_dtree[d_code(dist)].Freq++; \
  16697. + flush = (s->last_lit == s->lit_bufsize-1); \
  16698. + }
  16699. +#else
  16700. +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
  16701. +# define _tr_tally_dist(s, distance, length, flush) \
  16702. + flush = _tr_tally(s, distance, length)
  16703. +#endif
  16704. +
  16705. +#endif /* _DEFLATE_H */
  16706. --- /dev/null Tue Mar 11 13:02:56 2003
  16707. +++ linux/net/ipsec/des/COPYRIGHT Mon Feb 9 13:51:03 2004
  16708. @@ -0,0 +1,50 @@
  16709. +Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  16710. +All rights reserved.
  16711. +
  16712. +This package is an DES implementation written by Eric Young (eay@cryptsoft.com).
  16713. +The implementation was written so as to conform with MIT's libdes.
  16714. +
  16715. +This library is free for commercial and non-commercial use as long as
  16716. +the following conditions are aheared to. The following conditions
  16717. +apply to all code found in this distribution.
  16718. +
  16719. +Copyright remains Eric Young's, and as such any Copyright notices in
  16720. +the code are not to be removed.
  16721. +If this package is used in a product, Eric Young should be given attribution
  16722. +as the author of that the SSL library. This can be in the form of a textual
  16723. +message at program startup or in documentation (online or textual) provided
  16724. +with the package.
  16725. +
  16726. +Redistribution and use in source and binary forms, with or without
  16727. +modification, are permitted provided that the following conditions
  16728. +are met:
  16729. +1. Redistributions of source code must retain the copyright
  16730. + notice, this list of conditions and the following disclaimer.
  16731. +2. Redistributions in binary form must reproduce the above copyright
  16732. + notice, this list of conditions and the following disclaimer in the
  16733. + documentation and/or other materials provided with the distribution.
  16734. +3. All advertising materials mentioning features or use of this software
  16735. + must display the following acknowledgement:
  16736. + This product includes software developed by Eric Young (eay@cryptsoft.com)
  16737. +
  16738. +THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  16739. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16740. +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16741. +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16742. +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16743. +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  16744. +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  16745. +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16746. +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16747. +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16748. +SUCH DAMAGE.
  16749. +
  16750. +The license and distribution terms for any publically available version or
  16751. +derivative of this code cannot be changed. i.e. this code cannot simply be
  16752. +copied and put under another distrubution license
  16753. +[including the GNU Public License.]
  16754. +
  16755. +The reason behind this being stated in this direct manner is past
  16756. +experience in code simply being copied and the attribution removed
  16757. +from it and then being distributed as part of other packages. This
  16758. +implementation was a non-trivial and unpaid effort.
  16759. --- /dev/null Tue Mar 11 13:02:56 2003
  16760. +++ linux/net/ipsec/des/INSTALL Mon Feb 9 13:51:03 2004
  16761. @@ -0,0 +1,69 @@
  16762. +Check the CC and CFLAGS lines in the makefile
  16763. +
  16764. +If your C library does not support the times(3) function, change the
  16765. +#define TIMES to
  16766. +#undef TIMES in speed.c
  16767. +If it does, check the HZ value for the times(3) function.
  16768. +If your system does not define CLK_TCK it will be assumed to
  16769. +be 100.0.
  16770. +
  16771. +If possible use gcc v 2.7.?
  16772. +Turn on the maximum optimising (normally '-O3 -fomit-frame-pointer' for gcc)
  16773. +In recent times, some system compilers give better performace.
  16774. +
  16775. +type 'make'
  16776. +
  16777. +run './destest' to check things are ok.
  16778. +run './rpw' to check the tty code for reading passwords works.
  16779. +run './speed' to see how fast those optimisations make the library run :-)
  16780. +run './des_opts' to determin the best compile time options.
  16781. +
  16782. +The output from des_opts should be put in the makefile options and des_enc.c
  16783. +should be rebuilt. For 64 bit computers, do not use the DES_PTR option.
  16784. +For the DEC Alpha, edit des.h and change DES_LONG to 'unsigned int'
  16785. +and then you can use the 'DES_PTR' option.
  16786. +
  16787. +The file options.txt has the options listed for best speed on quite a
  16788. +few systems. Look and the options (UNROLL, PTR, RISC2 etc) and then
  16789. +turn on the relevent option in the Makefile
  16790. +
  16791. +There are some special Makefile targets that make life easier.
  16792. +make cc - standard cc build
  16793. +make gcc - standard gcc build
  16794. +make x86-elf - x86 assembler (elf), linux-elf.
  16795. +make x86-out - x86 assembler (a.out), FreeBSD
  16796. +make x86-solaris- x86 assembler
  16797. +make x86-bsdi - x86 assembler (a.out with primative assembler).
  16798. +
  16799. +If at all possible use the assembler (for Windows NT/95, use
  16800. +asm/win32.obj to link with). The x86 assembler is very very fast.
  16801. +
  16802. +A make install will by default install
  16803. +libdes.a in /usr/local/lib/libdes.a
  16804. +des in /usr/local/bin/des
  16805. +des_crypt.man in /usr/local/man/man3/des_crypt.3
  16806. +des.man in /usr/local/man/man1/des.1
  16807. +des.h in /usr/include/des.h
  16808. +
  16809. +des(1) should be compatible with sunOS's but I have been unable to
  16810. +test it.
  16811. +
  16812. +These routines should compile on MSDOS, most 32bit and 64bit version
  16813. +of Unix (BSD and SYSV) and VMS, without modification.
  16814. +The only problems should be #include files that are in the wrong places.
  16815. +
  16816. +These routines can be compiled under MSDOS.
  16817. +I have successfully encrypted files using des(1) under MSDOS and then
  16818. +decrypted the files on a SparcStation.
  16819. +I have been able to compile and test the routines with
  16820. +Microsoft C v 5.1 and Turbo C v 2.0.
  16821. +The code in this library is in no way optimised for the 16bit
  16822. +operation of MSDOS.
  16823. +
  16824. +When building for glibc, ignore all of the above and just unpack into
  16825. +glibc-1.??/des and then gmake as per normal.
  16826. +
  16827. +As a final note on performace. Certain CPUs like sparcs and Alpha often give
  16828. +a %10 speed difference depending on the link order. It is rather anoying
  16829. +when one program reports 'x' DES encrypts a second and another reports
  16830. +'x*0.9' the speed.
  16831. --- /dev/null Tue Mar 11 13:02:56 2003
  16832. +++ linux/net/ipsec/des/Makefile Mon Feb 9 13:51:03 2004
  16833. @@ -0,0 +1,63 @@
  16834. +# Makefile for KLIPS kernel code as a module for 2.6 kernels
  16835. +#
  16836. +# Makefile for KLIPS kernel code as a module
  16837. +# Copyright (C) 1998, 1999, 2000,2001 Richard Guy Briggs.
  16838. +# Copyright (C) 2002-2004 Michael Richardson <mcr@freeswan.org>
  16839. +#
  16840. +# This program is free software; you can redistribute it and/or modify it
  16841. +# under the terms of the GNU General Public License as published by the
  16842. +# Free Software Foundation; either version 2 of the License, or (at your
  16843. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  16844. +#
  16845. +# This program is distributed in the hope that it will be useful, but
  16846. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16847. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16848. +# for more details.
  16849. +#
  16850. +# RCSID $Id: Makefile.fs2_6,v 1.2.2.1 2005/08/12 16:10:57 ken Exp $
  16851. +#
  16852. +# Note! Dependencies are done automagically by 'make dep', which also
  16853. +# removes any old dependencies. DON'T put your own dependencies here
  16854. +# unless it's something special (ie not a .c file).
  16855. +#
  16856. +
  16857. +obj-$(CONFIG_KLIPS_ENC_3DES) += ipsec_alg_3des.o
  16858. +obj-$(CONFIG_KLIPS_ENC_3DES) += cbc_enc.o
  16859. +obj-$(CONFIG_KLIPS_ENC_3DES) += ecb_enc.o
  16860. +obj-$(CONFIG_KLIPS_ENC_3DES) += set_key.o
  16861. +
  16862. +ifeq ($(strip ${SUBARCH}),)
  16863. +SUBARCH:=${ARCH}
  16864. +endif
  16865. +
  16866. +# the assembly version expects frame pointers, which are
  16867. +# optional in many kernel builds. If you want speed, you should
  16868. +# probably use cryptoapi code instead.
  16869. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  16870. +ifeq (${USEASSEMBLY},i386y)
  16871. +obj-$(CONFIG_KLIPS_ENC_3DES) += dx86unix.o
  16872. +else
  16873. +obj-$(CONFIG_KLIPS_ENC_3DES) += des_enc.o
  16874. +endif
  16875. +
  16876. +#
  16877. +# $Log: Makefile.fs2_6,v $
  16878. +# Revision 1.2.2.1 2005/08/12 16:10:57 ken
  16879. +# do not use assembly code with there are no frame pointers
  16880. +#
  16881. +# Revision 1.3 2005/08/12 14:13:59 mcr
  16882. +# do not use assembly code with there are no frame pointers,
  16883. +# as it does not have the right linkages.
  16884. +#
  16885. +# Revision 1.2 2005/04/29 05:13:07 mcr
  16886. +# 3DES algorithm code.
  16887. +#
  16888. +# Revision 1.1 2004/08/17 03:27:30 mcr
  16889. +# klips 2.6 edits.
  16890. +#
  16891. +#
  16892. +# Local Variables:
  16893. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  16894. +# End Variables:
  16895. +#
  16896. +
  16897. --- /dev/null Tue Mar 11 13:02:56 2003
  16898. +++ linux/net/ipsec/des/README Mon Feb 9 13:51:03 2004
  16899. @@ -0,0 +1,54 @@
  16900. +
  16901. + libdes, Version 4.01 10-Jan-97
  16902. +
  16903. + Copyright (c) 1997, Eric Young
  16904. + All rights reserved.
  16905. +
  16906. + This program is free software; you can redistribute it and/or modify
  16907. + it under the terms specified in COPYRIGHT.
  16908. +
  16909. +--
  16910. +The primary ftp site for this library is
  16911. +ftp://ftp.psy.uq.oz.au/pub/Crypto/DES/libdes-x.xx.tar.gz
  16912. +libdes is now also shipped with SSLeay. Primary ftp site of
  16913. +ftp://ftp.psy.uq.oz.au/pub/Crypto/SSL/SSLeay-x.x.x.tar.gz
  16914. +
  16915. +The best way to build this library is to build it as part of SSLeay.
  16916. +
  16917. +This kit builds a DES encryption library and a DES encryption program.
  16918. +It supports ecb, cbc, ofb, cfb, triple ecb, triple cbc, triple ofb,
  16919. +triple cfb, desx, and MIT's pcbc encryption modes and also has a fast
  16920. +implementation of crypt(3).
  16921. +It contains support routines to read keys from a terminal,
  16922. +generate a random key, generate a key from an arbitrary length string,
  16923. +read/write encrypted data from/to a file descriptor.
  16924. +
  16925. +The implementation was written so as to conform with the manual entry
  16926. +for the des_crypt(3) library routines from MIT's project Athena.
  16927. +
  16928. +destest should be run after compilation to test the des routines.
  16929. +rpw should be run after compilation to test the read password routines.
  16930. +The des program is a replacement for the sun des command. I believe it
  16931. +conforms to the sun version.
  16932. +
  16933. +The Imakefile is setup for use in the kerberos distribution.
  16934. +
  16935. +These routines are best compiled with gcc or any other good
  16936. +optimising compiler.
  16937. +Just turn you optimiser up to the highest settings and run destest
  16938. +after the build to make sure everything works.
  16939. +
  16940. +I believe these routines are close to the fastest and most portable DES
  16941. +routines that use small lookup tables (4.5k) that are publicly available.
  16942. +The fcrypt routine is faster than ufc's fcrypt (when compiling with
  16943. +gcc2 -O2) on the sparc 2 (1410 vs 1270) but is not so good on other machines
  16944. +(on a sun3/260 168 vs 336). It is a function of CPU on chip cache size.
  16945. +[ 10-Jan-97 and a function of an incorrect speed testing program in
  16946. + ufc which gave much better test figures that reality ].
  16947. +
  16948. +It is worth noting that on sparc and Alpha CPUs, performance of the DES
  16949. +library can vary by upto %10 due to the positioning of files after application
  16950. +linkage.
  16951. +
  16952. +Eric Young (eay@cryptsoft.com)
  16953. +
  16954. --- /dev/null Tue Mar 11 13:02:56 2003
  16955. +++ linux/net/ipsec/des/README.freeswan Mon Feb 9 13:51:03 2004
  16956. @@ -0,0 +1,33 @@
  16957. +The only changes the FreeS/WAN project has made to libdes-lite 4.04b are:
  16958. +
  16959. +We #ifdef-ed the declaration of DES_LONG in des.h, so it's more efficient
  16960. +on the Alpha, instead of just noting the issue in a comment.
  16961. +
  16962. +We #ifdef-ed out the des_options() function in ecb_enc.c, because we don't
  16963. +use it, and its call to sprintf() can cause subtle difficulties when KLIPS
  16964. +is built as a module (depending on details of Linux configuration options).
  16965. +
  16966. +We changed some instances of CC=$(CC) in the Makefile to CC='$(CC)' to make
  16967. +it cope better with Linux kernel Makefile stupidities, and took out an
  16968. +explicit CC=gcc (unwise on systems with strange compilers).
  16969. +
  16970. +We deleted some references to <stdio.h> and <stdlib.h>, and a declaration
  16971. +of one function found only in the full libdes (not in libdes-lite), to
  16972. +avoid dragging in bits of stdio/stdlib unnecessarily. (Our thanks to Hans
  16973. +Schultz for spotting this and pointing out the fixes.)
  16974. +
  16975. +We deleted a couple of .obj files in the asm subdirectory, which appear to
  16976. +have been included in the original library by accident.
  16977. +
  16978. +We have added an include of our Makefile.inc file, to permit overriding
  16979. +things like choice of compiler (although the libdes Makefile would
  16980. +probably need some work to make this effective).
  16981. +
  16982. +
  16983. +
  16984. +Note that Eric Young is no longer at the email address listed in these
  16985. +files, and is (alas) no longer working on free crypto software.
  16986. +
  16987. +
  16988. +
  16989. +This file is RCSID $Id: README.freeswan,v 1.12 2004/07/10 08:06:51 mcr Exp $
  16990. --- /dev/null Tue Mar 11 13:02:56 2003
  16991. +++ linux/net/ipsec/des/VERSION Mon Feb 9 13:51:03 2004
  16992. @@ -0,0 +1,406 @@
  16993. +Version 4.04
  16994. + Fixed a few tests in destest. Also added x86 assember for
  16995. + des_ncbc_encrypt() which is the standard cbc mode function.
  16996. + This makes a very very large performace difference.
  16997. + Ariel Glenn ariel@columbia.edu reports that the terminal
  16998. + 'turn echo off' can return (errno == EINVAL) under solaris
  16999. + when redirection is used. So I now catch that as well as ENOTTY.
  17000. +
  17001. +
  17002. +Version 4.03
  17003. + Left a static out of enc_write.c, which caused to buffer to be
  17004. + continiously malloc()ed. Does anyone use these functions? I keep
  17005. + on feeling like removing them since I only had these in there
  17006. + for a version of kerberised login. Anyway, this was pointed out
  17007. + by Theo de Raadt <deraadt@cvs.openbsd.org>
  17008. + The 'n' bit ofb code was wrong, it was not shifting the shift
  17009. + register. It worked correctly for n == 64. Thanks to
  17010. + Gigi Ankeny <Gigi.Ankeny@Eng.Sun.COM> for pointing this one out.
  17011. +
  17012. +Version 4.02
  17013. + I was doing 'if (memcmp(weak_keys[i],key,sizeof(key)) == 0)'
  17014. + when checking for weak keys which is wrong :-(, pointed out by
  17015. + Markus F.X.J. Oberhumer <markus.oberhumer@jk.uni-linz.ac.at>.
  17016. +
  17017. +Version 4.01
  17018. + Even faster inner loop in the DES assembler for x86 and a modification
  17019. + for IP/FP which is faster on x86. Both of these changes are
  17020. + from Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>. His
  17021. + changes make the assembler run %40 faster on a pentium. This is just
  17022. + a case of getting the instruction sequence 'just right'.
  17023. + All credit to 'Svend' :-)
  17024. + Quite a few special x86 'make' targets.
  17025. + A libdes-l (lite) distribution.
  17026. +
  17027. +Version 4.00
  17028. + After a bit of a pause, I'll up the major version number since this
  17029. + is mostly a performace release. I've added x86 assembler and
  17030. + added more options for performance. A %28 speedup for gcc
  17031. + on a pentium and the assembler is a %50 speedup.
  17032. + MIPS CPU's, sparc and Alpha are the main CPU's with speedups.
  17033. + Run des_opts to work out which options should be used.
  17034. + DES_RISC1/DES_RISC2 use alternative inner loops which use
  17035. + more registers but should give speedups on any CPU that does
  17036. + dual issue (pentium). DES_UNROLL unrolls the inner loop,
  17037. + which costs in code size.
  17038. +
  17039. +Version 3.26
  17040. + I've finally removed one of the shifts in D_ENCRYPT. This
  17041. + meant I've changed the des_SPtrans table (spr.h), the set_key()
  17042. + function and some things in des_enc.c. This has definitly
  17043. + made things faster :-). I've known about this one for some
  17044. + time but I've been too lazy to follow it up :-).
  17045. + Noticed that in the D_ENCRYPT() macro, we can just do L^=(..)^(..)^..
  17046. + instead of L^=((..)|(..)|(..).. This should save a register at
  17047. + least.
  17048. + Assember for x86. The file to replace is des_enc.c, which is replaced
  17049. + by one of the assembler files found in asm. Look at des/asm/readme
  17050. + for more info.
  17051. +
  17052. + /* Modification to fcrypt so it can be compiled to support
  17053. + HPUX 10.x's long password format, define -DLONGCRYPT to use this.
  17054. + Thanks to Jens Kupferschmidt <bt1cu@hpboot.rz.uni-leipzig.de>. */
  17055. +
  17056. + SIGWINCH case put in des_read_passwd() so the function does not
  17057. + 'exit' if this function is recieved.
  17058. +
  17059. +Version 3.25 17/07/96
  17060. + Modified read_pwd.c so that stdin can be read if not a tty.
  17061. + Thanks to Jeff Barber <jeffb@issl.atl.hp.com> for the patches.
  17062. + des_init_random_number_generator() shortened due to VMS linker
  17063. + limits.
  17064. + Added RSA's DESX cbc mode. It is a form of cbc encryption, with 2
  17065. + 8 byte quantites xored before and after encryption.
  17066. + des_xcbc_encryption() - the name is funny to preserve the des_
  17067. + prefix on all functions.
  17068. +
  17069. +Version 3.24 20/04/96
  17070. + The DES_PTR macro option checked and used by SSLeay configuration
  17071. +
  17072. +Version 3.23 11/04/96
  17073. + Added DES_LONG. If defined to 'unsigned int' on the DEC Alpha,
  17074. + it gives a %20 speedup :-)
  17075. + Fixed the problem with des.pl under perl5. The patches were
  17076. + sent by Ed Kubaitis (ejk@uiuc.edu).
  17077. + if fcrypt.c, changed values to handle illegal salt values the way
  17078. + normal crypt() implementations do. Some programs apparently use
  17079. + them :-(. The patch was sent by Bjorn Gronvall <bg@sics.se>
  17080. +
  17081. +Version 3.22 29/11/95
  17082. + Bug in des(1), an error with the uuencoding stuff when the
  17083. + 'data' is small, thanks to Geoff Keating <keagchon@mehta.anu.edu.au>
  17084. + for the patch.
  17085. +
  17086. +Version 3.21 22/11/95
  17087. + After some emailing back and forth with
  17088. + Colin Plumb <colin@nyx10.cs.du.edu>, I've tweaked a few things
  17089. + and in a future version I will probably put in some of the
  17090. + optimisation he suggested for use with the DES_USE_PTR option.
  17091. + Extra routines from Mark Murray <mark@grondar.za> for use in
  17092. + freeBSD. They mostly involve random number generation for use
  17093. + with kerberos. They involve evil machine specific system calls
  17094. + etc so I would normally suggest pushing this stuff into the
  17095. + application and/or using RAND_seed()/RAND_bytes() if you are
  17096. + using this DES library as part of SSLeay.
  17097. + Redone the read_pw() function so that it is cleaner and
  17098. + supports termios, thanks to Sameer Parekh <sameer@c2.org>
  17099. + for the initial patches for this.
  17100. + Renamed 3ecb_encrypt() to ecb3_encrypt(). This has been
  17101. + done just to make things more consistent.
  17102. + I have also now added triple DES versions of cfb and ofb.
  17103. +
  17104. +Version 3.20
  17105. + Damn, Damn, Damn, as pointed out by Mike_Spreitzer.PARC@xerox.com,
  17106. + my des_random_seed() function was only copying 4 bytes of the
  17107. + passed seed into the init structure. It is now fixed to copy 8.
  17108. + My own suggestion is to used something like MD5 :-)
  17109. +
  17110. +Version 3.19
  17111. + While looking at my code one day, I though, why do I keep on
  17112. + calling des_encrypt(in,out,ks,enc) when every function that
  17113. + calls it has in and out the same. So I dropped the 'out'
  17114. + parameter, people should not be using this function.
  17115. +
  17116. +Version 3.18 30/08/95
  17117. + Fixed a few bit with the distribution and the filenames.
  17118. + 3.17 had been munged via a move to DOS and back again.
  17119. + NO CODE CHANGES
  17120. +
  17121. +Version 3.17 14/07/95
  17122. + Fixed ede3 cbc which I had broken in 3.16. I have also
  17123. + removed some unneeded variables in 7-8 of the routines.
  17124. +
  17125. +Version 3.16 26/06/95
  17126. + Added des_encrypt2() which does not use IP/FP, used by triple
  17127. + des routines. Tweaked things a bit elsewhere. %13 speedup on
  17128. + sparc and %6 on a R4400 for ede3 cbc mode.
  17129. +
  17130. +Version 3.15 06/06/95
  17131. + Added des_ncbc_encrypt(), it is des_cbc mode except that it is
  17132. + 'normal' and copies the new iv value back over the top of the
  17133. + passed parameter.
  17134. + CHANGED des_ede3_cbc_encrypt() so that it too now overwrites
  17135. + the iv. THIS WILL BREAK EXISTING CODE, but since this function
  17136. + only new, I feel I can change it, not so with des_cbc_encrypt :-(.
  17137. + I need to update the documentation.
  17138. +
  17139. +Version 3.14 31/05/95
  17140. + New release upon the world, as part of my SSL implementation.
  17141. + New copyright and usage stuff. Basically free for all to use
  17142. + as long as you say it came from me :-)
  17143. +
  17144. +Version 3.13 31/05/95
  17145. + A fix in speed.c, if HZ is not defined, I set it to 100.0
  17146. + which is reasonable for most unixes except SunOS 4.x.
  17147. + I now have a #ifdef sun but timing for SunOS 4.x looked very
  17148. + good :-(. At my last job where I used SunOS 4.x, it was
  17149. + defined to be 60.0 (look at the old INSTALL documentation), at
  17150. + the last release had it changed to 100.0 since I now work with
  17151. + Solaris2 and SVR4 boxes.
  17152. + Thanks to Rory Chisholm <rchishol@math.ethz.ch> for pointing this
  17153. + one out.
  17154. +
  17155. +Version 3.12 08/05/95
  17156. + As pointed out by The Crypt Keeper <tck@bend.UCSD.EDU>,
  17157. + my D_ENCRYPT macro in crypt() had an un-necessary variable.
  17158. + It has been removed.
  17159. +
  17160. +Version 3.11 03/05/95
  17161. + Added des_ede3_cbc_encrypt() which is cbc mode des with 3 keys
  17162. + and one iv. It is a standard and I needed it for my SSL code.
  17163. + It makes more sense to use this for triple DES than
  17164. + 3cbc_encrypt(). I have also added (or should I say tested :-)
  17165. + cfb64_encrypt() which is cfb64 but it will encrypt a partial
  17166. + number of bytes - 3 bytes in 3 bytes out. Again this is for
  17167. + my SSL library, as a form of encryption to use with SSL
  17168. + telnet.
  17169. +
  17170. +Version 3.10 22/03/95
  17171. + Fixed a bug in 3cbc_encrypt() :-(. When making repeated calls
  17172. + to cbc3_encrypt, the 2 iv values that were being returned to
  17173. + be used in the next call were reversed :-(.
  17174. + Many thanks to Bill Wade <wade@Stoner.COM> for pointing out
  17175. + this error.
  17176. +
  17177. +Version 3.09 01/02/95
  17178. + Fixed des_random_key to far more random, it was rather feeble
  17179. + with regards to picking the initial seed. The problem was
  17180. + pointed out by Olaf Kirch <okir@monad.swb.de>.
  17181. +
  17182. +Version 3.08 14/12/94
  17183. + Added Makefile.PL so libdes can be built into perl5.
  17184. + Changed des_locl.h so RAND is always defined.
  17185. +
  17186. +Version 3.07 05/12/94
  17187. + Added GNUmake and stuff so the library can be build with
  17188. + glibc.
  17189. +
  17190. +Version 3.06 30/08/94
  17191. + Added rpc_enc.c which contains _des_crypt. This is for use in
  17192. + secure_rpc v 4.0
  17193. + Finally fixed the cfb_enc problems.
  17194. + Fixed a few parameter parsing bugs in des (-3 and -b), thanks
  17195. + to Rob McMillan <R.McMillan@its.gu.edu.au>
  17196. +
  17197. +Version 3.05 21/04/94
  17198. + for unsigned long l; gcc does not produce ((l>>34) == 0)
  17199. + This causes bugs in cfb_enc.
  17200. + Thanks to Hadmut Danisch <danisch@ira.uka.de>
  17201. +
  17202. +Version 3.04 20/04/94
  17203. + Added a version number to des.c and libdes.a
  17204. +
  17205. +Version 3.03 12/01/94
  17206. + Fixed a bug in non zero iv in 3cbc_enc.
  17207. +
  17208. +Version 3.02 29/10/93
  17209. + I now work in a place where there are 6+ architectures and 14+
  17210. + OS versions :-).
  17211. + Fixed TERMIO definition so the most sys V boxes will work :-)
  17212. +
  17213. +Release upon comp.sources.misc
  17214. +Version 3.01 08/10/93
  17215. + Added des_3cbc_encrypt()
  17216. +
  17217. +Version 3.00 07/10/93
  17218. + Fixed up documentation.
  17219. + quad_cksum definitely compatible with MIT's now.
  17220. +
  17221. +Version 2.30 24/08/93
  17222. + Triple DES now defaults to triple cbc but can do triple ecb
  17223. + with the -b flag.
  17224. + Fixed some MSDOS uuen/uudecoding problems, thanks to
  17225. + Added prototypes.
  17226. +
  17227. +Version 2.22 29/06/93
  17228. + Fixed a bug in des_is_weak_key() which stopped it working :-(
  17229. + thanks to engineering@MorningStar.Com.
  17230. +
  17231. +Version 2.21 03/06/93
  17232. + des(1) with no arguments gives quite a bit of help.
  17233. + Added -c (generate ckecksum) flag to des(1).
  17234. + Added -3 (triple DES) flag to des(1).
  17235. + Added cfb and ofb routines to the library.
  17236. +
  17237. +Version 2.20 11/03/93
  17238. + Added -u (uuencode) flag to des(1).
  17239. + I have been playing with byte order in quad_cksum to make it
  17240. + compatible with MIT's version. All I can say is avid this
  17241. + function if possible since MIT's output is endian dependent.
  17242. +
  17243. +Version 2.12 14/10/92
  17244. + Added MSDOS specific macro in ecb_encrypt which gives a %70
  17245. + speed up when the code is compiled with turbo C.
  17246. +
  17247. +Version 2.11 12/10/92
  17248. + Speedup in set_key (recoding of PC-1)
  17249. + I now do it in 47 simple operations, down from 60.
  17250. + Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  17251. + for motivating me to look for a faster system :-)
  17252. + The speedup is probably less that 1% but it is still 13
  17253. + instructions less :-).
  17254. +
  17255. +Version 2.10 06/10/92
  17256. + The code now works on the 64bit ETA10 and CRAY without modifications or
  17257. + #defines. I believe the code should work on any machine that
  17258. + defines long, int or short to be 8 bytes long.
  17259. + Thanks to Shabbir J. Safdar (shabby@mentor.cc.purdue.edu)
  17260. + for helping me fix the code to run on 64bit machines (he had
  17261. + access to an ETA10).
  17262. + Thanks also to John Fletcher <john_fletcher@lccmail.ocf.llnl.gov>
  17263. + for testing the routines on a CRAY.
  17264. + read_password.c has been renamed to read_passwd.c
  17265. + string_to_key.c has been renamed to string2key.c
  17266. +
  17267. +Version 2.00 14/09/92
  17268. + Made mods so that the library should work on 64bit CPU's.
  17269. + Removed all my uchar and ulong defs. To many different
  17270. + versions of unix define them in their header files in too many
  17271. + different combinations :-)
  17272. + IRIX - Sillicon Graphics mods (mostly in read_password.c).
  17273. + Thanks to Andrew Daviel (advax@erich.triumf.ca)
  17274. +
  17275. +Version 1.99 26/08/92
  17276. + Fixed a bug or 2 in enc_read.c
  17277. + Fixed a bug in enc_write.c
  17278. + Fixed a pseudo bug in fcrypt.c (very obscure).
  17279. +
  17280. +Version 1.98 31/07/92
  17281. + Support for the ETA10. This is a strange machine that defines
  17282. + longs and ints as 8 bytes and shorts as 4 bytes.
  17283. + Since I do evil things with long * that assume that they are 4
  17284. + bytes. Look in the Makefile for the option to compile for
  17285. + this machine. quad_cksum appears to have problems but I
  17286. + will don't have the time to fix it right now, and this is not
  17287. + a function that uses DES and so will not effect the main uses
  17288. + of the library.
  17289. +
  17290. +Version 1.97 20/05/92 eay
  17291. + Fixed the Imakefile and made some changes to des.h to fix some
  17292. + problems when building this package with Kerberos v 4.
  17293. +
  17294. +Version 1.96 18/05/92 eay
  17295. + Fixed a small bug in string_to_key() where problems could
  17296. + occur if des_check_key was set to true and the string
  17297. + generated a weak key.
  17298. +
  17299. +Patch2 posted to comp.sources.misc
  17300. +Version 1.95 13/05/92 eay
  17301. + Added an alternative version of the D_ENCRYPT macro in
  17302. + ecb_encrypt and fcrypt. Depending on the compiler, one version or the
  17303. + other will be faster. This was inspired by
  17304. + Dana How <how@isl.stanford.edu>, and her pointers about doing the
  17305. + *(ulong *)((uchar *)ptr+(value&0xfc))
  17306. + vs
  17307. + ptr[value&0x3f]
  17308. + to stop the C compiler doing a <<2 to convert the long array index.
  17309. +
  17310. +Version 1.94 05/05/92 eay
  17311. + Fixed an incompatibility between my string_to_key and the MIT
  17312. + version. When the key is longer than 8 chars, I was wrapping
  17313. + with a different method. To use the old version, define
  17314. + OLD_STR_TO_KEY in the makefile. Thanks to
  17315. + viktor@newsu.shearson.com (Viktor Dukhovni).
  17316. +
  17317. +Version 1.93 28/04/92 eay
  17318. + Fixed the VMS mods so that echo is now turned off in
  17319. + read_password. Thanks again to brennan@coco.cchs.su.oz.AU.
  17320. + MSDOS support added. The routines can be compiled with
  17321. + Turbo C (v2.0) and MSC (v5.1). Make sure MSDOS is defined.
  17322. +
  17323. +Patch1 posted to comp.sources.misc
  17324. +Version 1.92 13/04/92 eay
  17325. + Changed D_ENCRYPT so that the rotation of R occurs outside of
  17326. + the loop. This required rotating all the longs in sp.h (now
  17327. + called spr.h). Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  17328. + speed.c has been changed so it will work without SIGALRM. If
  17329. + times(3) is not present it will try to use ftime() instead.
  17330. +
  17331. +Version 1.91 08/04/92 eay
  17332. + Added -E/-D options to des(1) so it can use string_to_key.
  17333. + Added SVR4 mods suggested by witr@rwwa.COM
  17334. + Added VMS mods suggested by brennan@coco.cchs.su.oz.AU. If
  17335. + anyone knows how to turn of tty echo in VMS please tell me or
  17336. + implement it yourself :-).
  17337. + Changed FILE *IN/*OUT to *DES_IN/*DES_OUT since it appears VMS
  17338. + does not like IN/OUT being used.
  17339. +
  17340. +Libdes posted to comp.sources.misc
  17341. +Version 1.9 24/03/92 eay
  17342. + Now contains a fast small crypt replacement.
  17343. + Added des(1) command.
  17344. + Added des_rw_mode so people can use cbc encryption with
  17345. + enc_read and enc_write.
  17346. +
  17347. +Version 1.8 15/10/91 eay
  17348. + Bug in cbc_cksum.
  17349. + Many thanks to Keith Reynolds (keithr@sco.COM) for pointing this
  17350. + one out.
  17351. +
  17352. +Version 1.7 24/09/91 eay
  17353. + Fixed set_key :-)
  17354. + set_key is 4 times faster and takes less space.
  17355. + There are a few minor changes that could be made.
  17356. +
  17357. +Version 1.6 19/09/1991 eay
  17358. + Finally go IP and FP finished.
  17359. + Now I need to fix set_key.
  17360. + This version is quite a bit faster that 1.51
  17361. +
  17362. +Version 1.52 15/06/1991 eay
  17363. + 20% speedup in ecb_encrypt by changing the E bit selection
  17364. + to use 2 32bit words. This also required modification of the
  17365. + sp table. There is still a way to speedup the IP and IP-1
  17366. + (hints from outer@sq.com) still working on this one :-(.
  17367. +
  17368. +Version 1.51 07/06/1991 eay
  17369. + Faster des_encrypt by loop unrolling
  17370. + Fixed bug in quad_cksum.c (thanks to hughes@logos.ucs.indiana.edu)
  17371. +
  17372. +Version 1.50 28/05/1991 eay
  17373. + Optimised the code a bit more for the sparc. I have improved the
  17374. + speed of the inner des_encrypt by speeding up the initial and
  17375. + final permutations.
  17376. +
  17377. +Version 1.40 23/10/1990 eay
  17378. + Fixed des_random_key, it did not produce a random key :-(
  17379. +
  17380. +Version 1.30 2/10/1990 eay
  17381. + Have made des_quad_cksum the same as MIT's, the full package
  17382. + should be compatible with MIT's
  17383. + Have tested on a DECstation 3100
  17384. + Still need to fix des_set_key (make it faster).
  17385. + Does des_cbc_encrypts at 70.5k/sec on a 3100.
  17386. +
  17387. +Version 1.20 18/09/1990 eay
  17388. + Fixed byte order dependencies.
  17389. + Fixed (I hope) all the word alignment problems.
  17390. + Speedup in des_ecb_encrypt.
  17391. +
  17392. +Version 1.10 11/09/1990 eay
  17393. + Added des_enc_read and des_enc_write.
  17394. + Still need to fix des_quad_cksum.
  17395. + Still need to document des_enc_read and des_enc_write.
  17396. +
  17397. +Version 1.00 27/08/1990 eay
  17398. +
  17399. --- /dev/null Tue Mar 11 13:02:56 2003
  17400. +++ linux/net/ipsec/des/asm/des-586.pl Mon Feb 9 13:51:03 2004
  17401. @@ -0,0 +1,251 @@
  17402. +#!/usr/local/bin/perl
  17403. +#
  17404. +# The inner loop instruction sequence and the IP/FP modifications are from
  17405. +# Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
  17406. +#
  17407. +
  17408. +push(@INC,"perlasm","../../perlasm");
  17409. +require "x86asm.pl";
  17410. +require "cbc.pl";
  17411. +require "desboth.pl";
  17412. +
  17413. +# base code is in microsft
  17414. +# op dest, source
  17415. +# format.
  17416. +#
  17417. +
  17418. +&asm_init($ARGV[0],"des-586.pl");
  17419. +
  17420. +$L="edi";
  17421. +$R="esi";
  17422. +
  17423. +&external_label("des_SPtrans");
  17424. +&des_encrypt("des_encrypt",1);
  17425. +&des_encrypt("des_encrypt2",0);
  17426. +&des_encrypt3("des_encrypt3",1);
  17427. +&des_encrypt3("des_decrypt3",0);
  17428. +&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
  17429. +&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
  17430. +
  17431. +&asm_finish();
  17432. +
  17433. +sub des_encrypt
  17434. + {
  17435. + local($name,$do_ip)=@_;
  17436. +
  17437. + &function_begin_B($name,"EXTRN _des_SPtrans:DWORD");
  17438. +
  17439. + &push("esi");
  17440. + &push("edi");
  17441. +
  17442. + &comment("");
  17443. + &comment("Load the 2 words");
  17444. + $ks="ebp";
  17445. +
  17446. + if ($do_ip)
  17447. + {
  17448. + &mov($R,&wparam(0));
  17449. + &xor( "ecx", "ecx" );
  17450. +
  17451. + &push("ebx");
  17452. + &push("ebp");
  17453. +
  17454. + &mov("eax",&DWP(0,$R,"",0));
  17455. + &mov("ebx",&wparam(2)); # get encrypt flag
  17456. + &mov($L,&DWP(4,$R,"",0));
  17457. + &comment("");
  17458. + &comment("IP");
  17459. + &IP_new("eax",$L,$R,3);
  17460. + }
  17461. + else
  17462. + {
  17463. + &mov("eax",&wparam(0));
  17464. + &xor( "ecx", "ecx" );
  17465. +
  17466. + &push("ebx");
  17467. + &push("ebp");
  17468. +
  17469. + &mov($R,&DWP(0,"eax","",0));
  17470. + &mov("ebx",&wparam(2)); # get encrypt flag
  17471. + &rotl($R,3);
  17472. + &mov($L,&DWP(4,"eax","",0));
  17473. + &rotl($L,3);
  17474. + }
  17475. +
  17476. + &mov( $ks, &wparam(1) );
  17477. + &cmp("ebx","0");
  17478. + &je(&label("start_decrypt"));
  17479. +
  17480. + for ($i=0; $i<16; $i+=2)
  17481. + {
  17482. + &comment("");
  17483. + &comment("Round $i");
  17484. + &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17485. +
  17486. + &comment("");
  17487. + &comment("Round ".sprintf("%d",$i+1));
  17488. + &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17489. + }
  17490. + &jmp(&label("end"));
  17491. +
  17492. + &set_label("start_decrypt");
  17493. +
  17494. + for ($i=15; $i>0; $i-=2)
  17495. + {
  17496. + &comment("");
  17497. + &comment("Round $i");
  17498. + &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17499. + &comment("");
  17500. + &comment("Round ".sprintf("%d",$i-1));
  17501. + &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17502. + }
  17503. +
  17504. + &set_label("end");
  17505. +
  17506. + if ($do_ip)
  17507. + {
  17508. + &comment("");
  17509. + &comment("FP");
  17510. + &mov("edx",&wparam(0));
  17511. + &FP_new($L,$R,"eax",3);
  17512. +
  17513. + &mov(&DWP(0,"edx","",0),"eax");
  17514. + &mov(&DWP(4,"edx","",0),$R);
  17515. + }
  17516. + else
  17517. + {
  17518. + &comment("");
  17519. + &comment("Fixup");
  17520. + &rotr($L,3); # r
  17521. + &mov("eax",&wparam(0));
  17522. + &rotr($R,3); # l
  17523. + &mov(&DWP(0,"eax","",0),$L);
  17524. + &mov(&DWP(4,"eax","",0),$R);
  17525. + }
  17526. +
  17527. + &pop("ebp");
  17528. + &pop("ebx");
  17529. + &pop("edi");
  17530. + &pop("esi");
  17531. + &ret();
  17532. +
  17533. + &function_end_B($name);
  17534. + }
  17535. +
  17536. +sub D_ENCRYPT
  17537. + {
  17538. + local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
  17539. +
  17540. + &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
  17541. + &xor( $tmp1, $tmp1);
  17542. + &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
  17543. + &xor( $u, $R);
  17544. + &xor( $t, $R);
  17545. + &and( $u, "0xfcfcfcfc" );
  17546. + &and( $t, "0xcfcfcfcf" );
  17547. + &movb( &LB($tmp1), &LB($u) );
  17548. + &movb( &LB($tmp2), &HB($u) );
  17549. + &rotr( $t, 4 );
  17550. + &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
  17551. + &movb( &LB($tmp1), &LB($t) );
  17552. + &xor( $L, $ks);
  17553. + &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
  17554. + &xor( $L, $ks); ######
  17555. + &movb( &LB($tmp2), &HB($t) );
  17556. + &shr( $u, 16);
  17557. + &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
  17558. + &xor( $L, $ks); ######
  17559. + &movb( &LB($tmp1), &HB($u) );
  17560. + &shr( $t, 16);
  17561. + &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
  17562. + &xor( $L, $ks);
  17563. + &mov( $ks, &wparam(1) );
  17564. + &movb( &LB($tmp2), &HB($t) );
  17565. + &and( $u, "0xff" );
  17566. + &and( $t, "0xff" );
  17567. + &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
  17568. + &xor( $L, $tmp1);
  17569. + &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
  17570. + &xor( $L, $tmp1);
  17571. + &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
  17572. + &xor( $L, $tmp1);
  17573. + &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
  17574. + &xor( $L, $tmp1);
  17575. + }
  17576. +
  17577. +sub n2a
  17578. + {
  17579. + sprintf("%d",$_[0]);
  17580. + }
  17581. +
  17582. +# now has a side affect of rotating $a by $shift
  17583. +sub R_PERM_OP
  17584. + {
  17585. + local($a,$b,$tt,$shift,$mask,$last)=@_;
  17586. +
  17587. + &rotl( $a, $shift ) if ($shift != 0);
  17588. + &mov( $tt, $a );
  17589. + &xor( $a, $b );
  17590. + &and( $a, $mask );
  17591. + if (!$last eq $b)
  17592. + {
  17593. + &xor( $b, $a );
  17594. + &xor( $tt, $a );
  17595. + }
  17596. + else
  17597. + {
  17598. + &xor( $tt, $a );
  17599. + &xor( $b, $a );
  17600. + }
  17601. + &comment("");
  17602. + }
  17603. +
  17604. +sub IP_new
  17605. + {
  17606. + local($l,$r,$tt,$lr)=@_;
  17607. +
  17608. + &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
  17609. + &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
  17610. + &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
  17611. + &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
  17612. + &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
  17613. +
  17614. + if ($lr != 3)
  17615. + {
  17616. + if (($lr-3) < 0)
  17617. + { &rotr($tt, 3-$lr); }
  17618. + else { &rotl($tt, $lr-3); }
  17619. + }
  17620. + if ($lr != 2)
  17621. + {
  17622. + if (($lr-2) < 0)
  17623. + { &rotr($r, 2-$lr); }
  17624. + else { &rotl($r, $lr-2); }
  17625. + }
  17626. + }
  17627. +
  17628. +sub FP_new
  17629. + {
  17630. + local($l,$r,$tt,$lr)=@_;
  17631. +
  17632. + if ($lr != 2)
  17633. + {
  17634. + if (($lr-2) < 0)
  17635. + { &rotl($r, 2-$lr); }
  17636. + else { &rotr($r, $lr-2); }
  17637. + }
  17638. + if ($lr != 3)
  17639. + {
  17640. + if (($lr-3) < 0)
  17641. + { &rotl($l, 3-$lr); }
  17642. + else { &rotr($l, $lr-3); }
  17643. + }
  17644. +
  17645. + &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
  17646. + &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
  17647. + &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
  17648. + &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
  17649. + &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
  17650. + &rotr($tt , 4);
  17651. + }
  17652. +
  17653. --- /dev/null Tue Mar 11 13:02:56 2003
  17654. +++ linux/net/ipsec/des/asm/des686.pl Mon Feb 9 13:51:03 2004
  17655. @@ -0,0 +1,230 @@
  17656. +#!/usr/local/bin/perl
  17657. +
  17658. +$prog="des686.pl";
  17659. +
  17660. +# base code is in microsft
  17661. +# op dest, source
  17662. +# format.
  17663. +#
  17664. +
  17665. +# WILL NOT WORK ANYMORE WITH desboth.pl
  17666. +require "desboth.pl";
  17667. +
  17668. +if ( ($ARGV[0] eq "elf"))
  17669. + { require "x86unix.pl"; }
  17670. +elsif ( ($ARGV[0] eq "a.out"))
  17671. + { $aout=1; require "x86unix.pl"; }
  17672. +elsif ( ($ARGV[0] eq "sol"))
  17673. + { $sol=1; require "x86unix.pl"; }
  17674. +elsif ( ($ARGV[0] eq "cpp"))
  17675. + { $cpp=1; require "x86unix.pl"; }
  17676. +elsif ( ($ARGV[0] eq "win32"))
  17677. + { require "x86ms.pl"; }
  17678. +else
  17679. + {
  17680. + print STDERR <<"EOF";
  17681. +Pick one target type from
  17682. + elf - linux, FreeBSD etc
  17683. + a.out - old linux
  17684. + sol - x86 solaris
  17685. + cpp - format so x86unix.cpp can be used
  17686. + win32 - Windows 95/Windows NT
  17687. +EOF
  17688. + exit(1);
  17689. + }
  17690. +
  17691. +&comment("Don't even think of reading this code");
  17692. +&comment("It was automatically generated by $prog");
  17693. +&comment("Which is a perl program used to generate the x86 assember for");
  17694. +&comment("any of elf, a.out, Win32, or Solaris");
  17695. +&comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
  17696. +&comment("eric <eay\@cryptsoft.com>");
  17697. +&comment("");
  17698. +
  17699. +&file("dx86xxxx");
  17700. +
  17701. +$L="edi";
  17702. +$R="esi";
  17703. +
  17704. +&des_encrypt("des_encrypt",1);
  17705. +&des_encrypt("des_encrypt2",0);
  17706. +
  17707. +&des_encrypt3("des_encrypt3",1);
  17708. +&des_encrypt3("des_decrypt3",0);
  17709. +
  17710. +&file_end();
  17711. +
  17712. +sub des_encrypt
  17713. + {
  17714. + local($name,$do_ip)=@_;
  17715. +
  17716. + &function_begin($name,"EXTRN _des_SPtrans:DWORD");
  17717. +
  17718. + &comment("");
  17719. + &comment("Load the 2 words");
  17720. + &mov("eax",&wparam(0));
  17721. + &mov($L,&DWP(0,"eax","",0));
  17722. + &mov($R,&DWP(4,"eax","",0));
  17723. +
  17724. + $ksp=&wparam(1);
  17725. +
  17726. + if ($do_ip)
  17727. + {
  17728. + &comment("");
  17729. + &comment("IP");
  17730. + &IP_new($L,$R,"eax");
  17731. + }
  17732. +
  17733. + &comment("");
  17734. + &comment("fixup rotate");
  17735. + &rotl($R,3);
  17736. + &rotl($L,3);
  17737. + &exch($L,$R);
  17738. +
  17739. + &comment("");
  17740. + &comment("load counter, key_schedule and enc flag");
  17741. + &mov("eax",&wparam(2)); # get encrypt flag
  17742. + &mov("ebp",&wparam(1)); # get ks
  17743. + &cmp("eax","0");
  17744. + &je(&label("start_decrypt"));
  17745. +
  17746. + # encrypting part
  17747. +
  17748. + for ($i=0; $i<16; $i+=2)
  17749. + {
  17750. + &comment("");
  17751. + &comment("Round $i");
  17752. + &D_ENCRYPT($L,$R,$i*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17753. +
  17754. + &comment("");
  17755. + &comment("Round ".sprintf("%d",$i+1));
  17756. + &D_ENCRYPT($R,$L,($i+1)*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17757. + }
  17758. + &jmp(&label("end"));
  17759. +
  17760. + &set_label("start_decrypt");
  17761. +
  17762. + for ($i=15; $i>0; $i-=2)
  17763. + {
  17764. + &comment("");
  17765. + &comment("Round $i");
  17766. + &D_ENCRYPT($L,$R,$i*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17767. + &comment("");
  17768. + &comment("Round ".sprintf("%d",$i-1));
  17769. + &D_ENCRYPT($R,$L,($i-1)*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17770. + }
  17771. +
  17772. + &set_label("end");
  17773. +
  17774. + &comment("");
  17775. + &comment("Fixup");
  17776. + &rotr($L,3); # r
  17777. + &rotr($R,3); # l
  17778. +
  17779. + if ($do_ip)
  17780. + {
  17781. + &comment("");
  17782. + &comment("FP");
  17783. + &FP_new($R,$L,"eax");
  17784. + }
  17785. +
  17786. + &mov("eax",&wparam(0));
  17787. + &mov(&DWP(0,"eax","",0),$L);
  17788. + &mov(&DWP(4,"eax","",0),$R);
  17789. +
  17790. + &function_end($name);
  17791. + }
  17792. +
  17793. +
  17794. +# The logic is to load R into 2 registers and operate on both at the same time.
  17795. +# We also load the 2 R's into 2 more registers so we can do the 'move word down a byte'
  17796. +# while also masking the other copy and doing a lookup. We then also accumulate the
  17797. +# L value in 2 registers then combine them at the end.
  17798. +sub D_ENCRYPT
  17799. + {
  17800. + local($L,$R,$S,$ks,$desSP,$u,$t,$tmp1,$tmp2,$tmp3)=@_;
  17801. +
  17802. + &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
  17803. + &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
  17804. + &xor( $u, $R );
  17805. + &xor( $t, $R );
  17806. + &rotr( $t, 4 );
  17807. +
  17808. + # the numbers at the end of the line are origional instruction order
  17809. + &mov( $tmp2, $u ); # 1 2
  17810. + &mov( $tmp1, $t ); # 1 1
  17811. + &and( $tmp2, "0xfc" ); # 1 4
  17812. + &and( $tmp1, "0xfc" ); # 1 3
  17813. + &shr( $t, 8 ); # 1 5
  17814. + &xor( $L, &DWP("0x100+$desSP",$tmp1,"",0)); # 1 7
  17815. + &shr( $u, 8 ); # 1 6
  17816. + &mov( $tmp1, &DWP(" $desSP",$tmp2,"",0)); # 1 8
  17817. +
  17818. + &mov( $tmp2, $u ); # 2 2
  17819. + &xor( $L, $tmp1 ); # 1 9
  17820. + &and( $tmp2, "0xfc" ); # 2 4
  17821. + &mov( $tmp1, $t ); # 2 1
  17822. + &and( $tmp1, "0xfc" ); # 2 3
  17823. + &shr( $t, 8 ); # 2 5
  17824. + &xor( $L, &DWP("0x300+$desSP",$tmp1,"",0)); # 2 7
  17825. + &shr( $u, 8 ); # 2 6
  17826. + &mov( $tmp1, &DWP("0x200+$desSP",$tmp2,"",0)); # 2 8
  17827. + &mov( $tmp2, $u ); # 3 2
  17828. +
  17829. + &xor( $L, $tmp1 ); # 2 9
  17830. + &and( $tmp2, "0xfc" ); # 3 4
  17831. +
  17832. + &mov( $tmp1, $t ); # 3 1
  17833. + &shr( $u, 8 ); # 3 6
  17834. + &and( $tmp1, "0xfc" ); # 3 3
  17835. + &shr( $t, 8 ); # 3 5
  17836. + &xor( $L, &DWP("0x500+$desSP",$tmp1,"",0)); # 3 7
  17837. + &mov( $tmp1, &DWP("0x400+$desSP",$tmp2,"",0)); # 3 8
  17838. +
  17839. + &and( $t, "0xfc" ); # 4 1
  17840. + &xor( $L, $tmp1 ); # 3 9
  17841. +
  17842. + &and( $u, "0xfc" ); # 4 2
  17843. + &xor( $L, &DWP("0x700+$desSP",$t,"",0)); # 4 3
  17844. + &xor( $L, &DWP("0x600+$desSP",$u,"",0)); # 4 4
  17845. + }
  17846. +
  17847. +sub PERM_OP
  17848. + {
  17849. + local($a,$b,$tt,$shift,$mask)=@_;
  17850. +
  17851. + &mov( $tt, $a );
  17852. + &shr( $tt, $shift );
  17853. + &xor( $tt, $b );
  17854. + &and( $tt, $mask );
  17855. + &xor( $b, $tt );
  17856. + &shl( $tt, $shift );
  17857. + &xor( $a, $tt );
  17858. + }
  17859. +
  17860. +sub IP_new
  17861. + {
  17862. + local($l,$r,$tt)=@_;
  17863. +
  17864. + &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
  17865. + &PERM_OP($l,$r,$tt,16,"0x0000ffff");
  17866. + &PERM_OP($r,$l,$tt, 2,"0x33333333");
  17867. + &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
  17868. + &PERM_OP($r,$l,$tt, 1,"0x55555555");
  17869. + }
  17870. +
  17871. +sub FP_new
  17872. + {
  17873. + local($l,$r,$tt)=@_;
  17874. +
  17875. + &PERM_OP($l,$r,$tt, 1,"0x55555555");
  17876. + &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
  17877. + &PERM_OP($l,$r,$tt, 2,"0x33333333");
  17878. + &PERM_OP($r,$l,$tt,16,"0x0000ffff");
  17879. + &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
  17880. + }
  17881. +
  17882. +sub n2a
  17883. + {
  17884. + sprintf("%d",$_[0]);
  17885. + }
  17886. --- /dev/null Tue Mar 11 13:02:56 2003
  17887. +++ linux/net/ipsec/des/asm/desboth.pl Mon Feb 9 13:51:03 2004
  17888. @@ -0,0 +1,79 @@
  17889. +#!/usr/local/bin/perl
  17890. +
  17891. +$L="edi";
  17892. +$R="esi";
  17893. +
  17894. +sub des_encrypt3
  17895. + {
  17896. + local($name,$enc)=@_;
  17897. +
  17898. + &function_begin_B($name,"");
  17899. + &push("ebx");
  17900. + &mov("ebx",&wparam(0));
  17901. +
  17902. + &push("ebp");
  17903. + &push("esi");
  17904. +
  17905. + &push("edi");
  17906. +
  17907. + &comment("");
  17908. + &comment("Load the data words");
  17909. + &mov($L,&DWP(0,"ebx","",0));
  17910. + &mov($R,&DWP(4,"ebx","",0));
  17911. + &stack_push(3);
  17912. +
  17913. + &comment("");
  17914. + &comment("IP");
  17915. + &IP_new($L,$R,"edx",0);
  17916. +
  17917. + # put them back
  17918. +
  17919. + if ($enc)
  17920. + {
  17921. + &mov(&DWP(4,"ebx","",0),$R);
  17922. + &mov("eax",&wparam(1));
  17923. + &mov(&DWP(0,"ebx","",0),"edx");
  17924. + &mov("edi",&wparam(2));
  17925. + &mov("esi",&wparam(3));
  17926. + }
  17927. + else
  17928. + {
  17929. + &mov(&DWP(4,"ebx","",0),$R);
  17930. + &mov("esi",&wparam(1));
  17931. + &mov(&DWP(0,"ebx","",0),"edx");
  17932. + &mov("edi",&wparam(2));
  17933. + &mov("eax",&wparam(3));
  17934. + }
  17935. + &mov(&swtmp(2), (($enc)?"1":"0"));
  17936. + &mov(&swtmp(1), "eax");
  17937. + &mov(&swtmp(0), "ebx");
  17938. + &call("des_encrypt2");
  17939. + &mov(&swtmp(2), (($enc)?"0":"1"));
  17940. + &mov(&swtmp(1), "edi");
  17941. + &mov(&swtmp(0), "ebx");
  17942. + &call("des_encrypt2");
  17943. + &mov(&swtmp(2), (($enc)?"1":"0"));
  17944. + &mov(&swtmp(1), "esi");
  17945. + &mov(&swtmp(0), "ebx");
  17946. + &call("des_encrypt2");
  17947. +
  17948. + &stack_pop(3);
  17949. + &mov($L,&DWP(0,"ebx","",0));
  17950. + &mov($R,&DWP(4,"ebx","",0));
  17951. +
  17952. + &comment("");
  17953. + &comment("FP");
  17954. + &FP_new($L,$R,"eax",0);
  17955. +
  17956. + &mov(&DWP(0,"ebx","",0),"eax");
  17957. + &mov(&DWP(4,"ebx","",0),$R);
  17958. +
  17959. + &pop("edi");
  17960. + &pop("esi");
  17961. + &pop("ebp");
  17962. + &pop("ebx");
  17963. + &ret();
  17964. + &function_end_B($name);
  17965. + }
  17966. +
  17967. +
  17968. --- /dev/null Tue Mar 11 13:02:56 2003
  17969. +++ linux/net/ipsec/des/asm/readme Mon Feb 9 13:51:03 2004
  17970. @@ -0,0 +1,131 @@
  17971. +First up, let me say I don't like writing in assembler. It is not portable,
  17972. +dependant on the particular CPU architecture release and is generally a pig
  17973. +to debug and get right. Having said that, the x86 architecture is probably
  17974. +the most important for speed due to number of boxes and since
  17975. +it appears to be the worst architecture to to get
  17976. +good C compilers for. So due to this, I have lowered myself to do
  17977. +assembler for the inner DES routines in libdes :-).
  17978. +
  17979. +The file to implement in assembler is des_enc.c. Replace the following
  17980. +4 functions
  17981. +des_encrypt(DES_LONG data[2],des_key_schedule ks, int encrypt);
  17982. +des_encrypt2(DES_LONG data[2],des_key_schedule ks, int encrypt);
  17983. +des_encrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  17984. +des_decrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  17985. +
  17986. +They encrypt/decrypt the 64 bits held in 'data' using
  17987. +the 'ks' key schedules. The only difference between the 4 functions is that
  17988. +des_encrypt2() does not perform IP() or FP() on the data (this is an
  17989. +optimization for when doing triple DES and des_encrypt3() and des_decrypt3()
  17990. +perform triple des. The triple DES routines are in here because it does
  17991. +make a big difference to have them located near the des_encrypt2 function
  17992. +at link time..
  17993. +
  17994. +Now as we all know, there are lots of different operating systems running on
  17995. +x86 boxes, and unfortunately they normally try to make sure their assembler
  17996. +formating is not the same as the other peoples.
  17997. +The 4 main formats I know of are
  17998. +Microsoft Windows 95/Windows NT
  17999. +Elf Includes Linux and FreeBSD(?).
  18000. +a.out The older Linux.
  18001. +Solaris Same as Elf but different comments :-(.
  18002. +
  18003. +Now I was not overly keen to write 4 different copies of the same code,
  18004. +so I wrote a few perl routines to output the correct assembler, given
  18005. +a target assembler type. This code is ugly and is just a hack.
  18006. +The libraries are x86unix.pl and x86ms.pl.
  18007. +des586.pl, des686.pl and des-som[23].pl are the programs to actually
  18008. +generate the assembler.
  18009. +
  18010. +So to generate elf assembler
  18011. +perl des-som3.pl elf >dx86-elf.s
  18012. +For Windows 95/NT
  18013. +perl des-som2.pl win32 >win32.asm
  18014. +
  18015. +[ update 4 Jan 1996 ]
  18016. +I have added another way to do things.
  18017. +perl des-som3.pl cpp >dx86-cpp.s
  18018. +generates a file that will be included by dx86unix.cpp when it is compiled.
  18019. +To build for elf, a.out, solaris, bsdi etc,
  18020. +cc -E -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o
  18021. +cc -E -DSOL asm/dx86unix.cpp | as -o asm/dx86-sol.o
  18022. +cc -E -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o
  18023. +cc -E -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o
  18024. +This was done to cut down the number of files in the distribution.
  18025. +
  18026. +Now the ugly part. I acquired my copy of Intels
  18027. +"Optimization's For Intel's 32-Bit Processors" and found a few interesting
  18028. +things. First, the aim of the exersize is to 'extract' one byte at a time
  18029. +from a word and do an array lookup. This involves getting the byte from
  18030. +the 4 locations in the word and moving it to a new word and doing the lookup.
  18031. +The most obvious way to do this is
  18032. +xor eax, eax # clear word
  18033. +movb al, cl # get low byte
  18034. +xor edi DWORD PTR 0x100+des_SP[eax] # xor in word
  18035. +movb al, ch # get next byte
  18036. +xor edi DWORD PTR 0x300+des_SP[eax] # xor in word
  18037. +shr ecx 16
  18038. +which seems ok. For the pentium, this system appears to be the best.
  18039. +One has to do instruction interleaving to keep both functional units
  18040. +operating, but it is basically very efficient.
  18041. +
  18042. +Now the crunch. When a full register is used after a partial write, eg.
  18043. +mov al, cl
  18044. +xor edi, DWORD PTR 0x100+des_SP[eax]
  18045. +386 - 1 cycle stall
  18046. +486 - 1 cycle stall
  18047. +586 - 0 cycle stall
  18048. +686 - at least 7 cycle stall (page 22 of the above mentioned document).
  18049. +
  18050. +So the technique that produces the best results on a pentium, according to
  18051. +the documentation, will produce hideous results on a pentium pro.
  18052. +
  18053. +To get around this, des686.pl will generate code that is not as fast on
  18054. +a pentium, should be very good on a pentium pro.
  18055. +mov eax, ecx # copy word
  18056. +shr ecx, 8 # line up next byte
  18057. +and eax, 0fch # mask byte
  18058. +xor edi DWORD PTR 0x100+des_SP[eax] # xor in array lookup
  18059. +mov eax, ecx # get word
  18060. +shr ecx 8 # line up next byte
  18061. +and eax, 0fch # mask byte
  18062. +xor edi DWORD PTR 0x300+des_SP[eax] # xor in array lookup
  18063. +
  18064. +Due to the execution units in the pentium, this actually works quite well.
  18065. +For a pentium pro it should be very good. This is the type of output
  18066. +Visual C++ generates.
  18067. +
  18068. +There is a third option. instead of using
  18069. +mov al, ch
  18070. +which is bad on the pentium pro, one may be able to use
  18071. +movzx eax, ch
  18072. +which may not incur the partial write penalty. On the pentium,
  18073. +this instruction takes 4 cycles so is not worth using but on the
  18074. +pentium pro it appears it may be worth while. I need access to one to
  18075. +experiment :-).
  18076. +
  18077. +eric (20 Oct 1996)
  18078. +
  18079. +22 Nov 1996 - I have asked people to run the 2 different version on pentium
  18080. +pros and it appears that the intel documentation is wrong. The
  18081. +mov al,bh is still faster on a pentium pro, so just use the des586.pl
  18082. +install des686.pl
  18083. +
  18084. +3 Dec 1996 - I added des_encrypt3/des_decrypt3 because I have moved these
  18085. +functions into des_enc.c because it does make a massive performance
  18086. +difference on some boxes to have the functions code located close to
  18087. +the des_encrypt2() function.
  18088. +
  18089. +9 Jan 1997 - des-som2.pl is now the correct perl script to use for
  18090. +pentiums. It contains an inner loop from
  18091. +Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk> which does raw ecb DES calls at
  18092. +273,000 per second. He had a previous version at 250,000 and the best
  18093. +I was able to get was 203,000. The content has not changed, this is all
  18094. +due to instruction sequencing (and actual instructions choice) which is able
  18095. +to keep both functional units of the pentium going.
  18096. +We may have lost the ugly register usage restrictions when x86 went 32 bit
  18097. +but for the pentium it has been replaced by evil instruction ordering tricks.
  18098. +
  18099. +13 Jan 1997 - des-som3.pl, more optimizations from Svend Olaf.
  18100. +raw DES at 281,000 per second on a pentium 100.
  18101. +
  18102. --- /dev/null Tue Mar 11 13:02:56 2003
  18103. +++ linux/net/ipsec/des/cbc_enc.c Mon Feb 9 13:51:03 2004
  18104. @@ -0,0 +1,135 @@
  18105. +/* crypto/des/cbc_enc.c */
  18106. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  18107. + * All rights reserved.
  18108. + *
  18109. + * This package is an SSL implementation written
  18110. + * by Eric Young (eay@cryptsoft.com).
  18111. + * The implementation was written so as to conform with Netscapes SSL.
  18112. + *
  18113. + * This library is free for commercial and non-commercial use as long as
  18114. + * the following conditions are aheared to. The following conditions
  18115. + * apply to all code found in this distribution, be it the RC4, RSA,
  18116. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18117. + * included with this distribution is covered by the same copyright terms
  18118. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  18119. + *
  18120. + * Copyright remains Eric Young's, and as such any Copyright notices in
  18121. + * the code are not to be removed.
  18122. + * If this package is used in a product, Eric Young should be given attribution
  18123. + * as the author of the parts of the library used.
  18124. + * This can be in the form of a textual message at program startup or
  18125. + * in documentation (online or textual) provided with the package.
  18126. + *
  18127. + * Redistribution and use in source and binary forms, with or without
  18128. + * modification, are permitted provided that the following conditions
  18129. + * are met:
  18130. + * 1. Redistributions of source code must retain the copyright
  18131. + * notice, this list of conditions and the following disclaimer.
  18132. + * 2. Redistributions in binary form must reproduce the above copyright
  18133. + * notice, this list of conditions and the following disclaimer in the
  18134. + * documentation and/or other materials provided with the distribution.
  18135. + * 3. All advertising materials mentioning features or use of this software
  18136. + * must display the following acknowledgement:
  18137. + * "This product includes cryptographic software written by
  18138. + * Eric Young (eay@cryptsoft.com)"
  18139. + * The word 'cryptographic' can be left out if the rouines from the library
  18140. + * being used are not cryptographic related :-).
  18141. + * 4. If you include any Windows specific code (or a derivative thereof) from
  18142. + * the apps directory (application code) you must include an acknowledgement:
  18143. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  18144. + *
  18145. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  18146. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18147. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18148. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18149. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18150. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18151. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18152. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18153. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18154. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18155. + * SUCH DAMAGE.
  18156. + *
  18157. + * The licence and distribution terms for any publically available version or
  18158. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  18159. + * copied and put under another distribution licence
  18160. + * [including the GNU Public Licence.]
  18161. + */
  18162. +
  18163. +#include "des/des_locl.h"
  18164. +
  18165. +void des_cbc_encrypt(input, output, length, schedule, ivec, enc)
  18166. +des_cblock (*input);
  18167. +des_cblock (*output);
  18168. +long length;
  18169. +des_key_schedule schedule;
  18170. +des_cblock (*ivec);
  18171. +int enc;
  18172. + {
  18173. + register DES_LONG tin0,tin1;
  18174. + register DES_LONG tout0,tout1,xor0,xor1;
  18175. + register unsigned char *in,*out;
  18176. + register long l=length;
  18177. + DES_LONG tin[2];
  18178. + unsigned char *iv;
  18179. +
  18180. + in=(unsigned char *)input;
  18181. + out=(unsigned char *)output;
  18182. + iv=(unsigned char *)ivec;
  18183. +
  18184. + if (enc)
  18185. + {
  18186. + c2l(iv,tout0);
  18187. + c2l(iv,tout1);
  18188. + for (l-=8; l>=0; l-=8)
  18189. + {
  18190. + c2l(in,tin0);
  18191. + c2l(in,tin1);
  18192. + tin0^=tout0; tin[0]=tin0;
  18193. + tin1^=tout1; tin[1]=tin1;
  18194. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  18195. + tout0=tin[0]; l2c(tout0,out);
  18196. + tout1=tin[1]; l2c(tout1,out);
  18197. + }
  18198. + if (l != -8)
  18199. + {
  18200. + c2ln(in,tin0,tin1,l+8);
  18201. + tin0^=tout0; tin[0]=tin0;
  18202. + tin1^=tout1; tin[1]=tin1;
  18203. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  18204. + tout0=tin[0]; l2c(tout0,out);
  18205. + tout1=tin[1]; l2c(tout1,out);
  18206. + }
  18207. + }
  18208. + else
  18209. + {
  18210. + c2l(iv,xor0);
  18211. + c2l(iv,xor1);
  18212. + for (l-=8; l>=0; l-=8)
  18213. + {
  18214. + c2l(in,tin0); tin[0]=tin0;
  18215. + c2l(in,tin1); tin[1]=tin1;
  18216. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  18217. + tout0=tin[0]^xor0;
  18218. + tout1=tin[1]^xor1;
  18219. + l2c(tout0,out);
  18220. + l2c(tout1,out);
  18221. + xor0=tin0;
  18222. + xor1=tin1;
  18223. + }
  18224. + if (l != -8)
  18225. + {
  18226. + c2l(in,tin0); tin[0]=tin0;
  18227. + c2l(in,tin1); tin[1]=tin1;
  18228. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  18229. + tout0=tin[0]^xor0;
  18230. + tout1=tin[1]^xor1;
  18231. + l2cn(tout0,tout1,out,l+8);
  18232. + /* xor0=tin0;
  18233. + xor1=tin1; */
  18234. + }
  18235. + }
  18236. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  18237. + tin[0]=tin[1]=0;
  18238. + }
  18239. +
  18240. --- /dev/null Tue Mar 11 13:02:56 2003
  18241. +++ linux/net/ipsec/des/des.doc Mon Feb 9 13:51:03 2004
  18242. @@ -0,0 +1,505 @@
  18243. +The DES library.
  18244. +
  18245. +Please note that this library was originally written to operate with
  18246. +eBones, a version of Kerberos that had had encryption removed when it left
  18247. +the USA and then put back in. As such there are some routines that I will
  18248. +advise not using but they are still in the library for historical reasons.
  18249. +For all calls that have an 'input' and 'output' variables, they can be the
  18250. +same.
  18251. +
  18252. +This library requires the inclusion of 'des.h'.
  18253. +
  18254. +All of the encryption functions take what is called a des_key_schedule as an
  18255. +argument. A des_key_schedule is an expanded form of the des key.
  18256. +A des_key is 8 bytes of odd parity, the type used to hold the key is a
  18257. +des_cblock. A des_cblock is an array of 8 bytes, often in this library
  18258. +description I will refer to input bytes when the function specifies
  18259. +des_cblock's as input or output, this just means that the variable should
  18260. +be a multiple of 8 bytes.
  18261. +
  18262. +The define DES_ENCRYPT is passed to specify encryption, DES_DECRYPT to
  18263. +specify decryption. The functions and global variable are as follows:
  18264. +
  18265. +int des_check_key;
  18266. + DES keys are supposed to be odd parity. If this variable is set to
  18267. + a non-zero value, des_set_key() will check that the key has odd
  18268. + parity and is not one of the known weak DES keys. By default this
  18269. + variable is turned off;
  18270. +
  18271. +void des_set_odd_parity(
  18272. +des_cblock *key );
  18273. + This function takes a DES key (8 bytes) and sets the parity to odd.
  18274. +
  18275. +int des_is_weak_key(
  18276. +des_cblock *key );
  18277. + This function returns a non-zero value if the DES key passed is a
  18278. + weak, DES key. If it is a weak key, don't use it, try a different
  18279. + one. If you are using 'random' keys, the chances of hitting a weak
  18280. + key are 1/2^52 so it is probably not worth checking for them.
  18281. +
  18282. +int des_set_key(
  18283. +des_cblock *key,
  18284. +des_key_schedule schedule);
  18285. + Des_set_key converts an 8 byte DES key into a des_key_schedule.
  18286. + A des_key_schedule is an expanded form of the key which is used to
  18287. + perform actual encryption. It can be regenerated from the DES key
  18288. + so it only needs to be kept when encryption or decryption is about
  18289. + to occur. Don't save or pass around des_key_schedule's since they
  18290. + are CPU architecture dependent, DES keys are not. If des_check_key
  18291. + is non zero, zero is returned if the key has the wrong parity or
  18292. + the key is a weak key, else 1 is returned.
  18293. +
  18294. +int des_key_sched(
  18295. +des_cblock *key,
  18296. +des_key_schedule schedule);
  18297. + An alternative name for des_set_key().
  18298. +
  18299. +int des_rw_mode; /* defaults to DES_PCBC_MODE */
  18300. + This flag holds either DES_CBC_MODE or DES_PCBC_MODE (default).
  18301. + This specifies the function to use in the enc_read() and enc_write()
  18302. + functions.
  18303. +
  18304. +void des_encrypt(
  18305. +unsigned long *data,
  18306. +des_key_schedule ks,
  18307. +int enc);
  18308. + This is the DES encryption function that gets called by just about
  18309. + every other DES routine in the library. You should not use this
  18310. + function except to implement 'modes' of DES. I say this because the
  18311. + functions that call this routine do the conversion from 'char *' to
  18312. + long, and this needs to be done to make sure 'non-aligned' memory
  18313. + access do not occur. The characters are loaded 'little endian',
  18314. + have a look at my source code for more details on how I use this
  18315. + function.
  18316. + Data is a pointer to 2 unsigned long's and ks is the
  18317. + des_key_schedule to use. enc, is non zero specifies encryption,
  18318. + zero if decryption.
  18319. +
  18320. +void des_encrypt2(
  18321. +unsigned long *data,
  18322. +des_key_schedule ks,
  18323. +int enc);
  18324. + This functions is the same as des_encrypt() except that the DES
  18325. + initial permutation (IP) and final permutation (FP) have been left
  18326. + out. As for des_encrypt(), you should not use this function.
  18327. + It is used by the routines in my library that implement triple DES.
  18328. + IP() des_encrypt2() des_encrypt2() des_encrypt2() FP() is the same
  18329. + as des_encrypt() des_encrypt() des_encrypt() except faster :-).
  18330. +
  18331. +void des_ecb_encrypt(
  18332. +des_cblock *input,
  18333. +des_cblock *output,
  18334. +des_key_schedule ks,
  18335. +int enc);
  18336. + This is the basic Electronic Code Book form of DES, the most basic
  18337. + form. Input is encrypted into output using the key represented by
  18338. + ks. If enc is non zero (DES_ENCRYPT), encryption occurs, otherwise
  18339. + decryption occurs. Input is 8 bytes long and output is 8 bytes.
  18340. + (the des_cblock structure is 8 chars).
  18341. +
  18342. +void des_ecb3_encrypt(
  18343. +des_cblock *input,
  18344. +des_cblock *output,
  18345. +des_key_schedule ks1,
  18346. +des_key_schedule ks2,
  18347. +des_key_schedule ks3,
  18348. +int enc);
  18349. + This is the 3 key EDE mode of ECB DES. What this means is that
  18350. + the 8 bytes of input is encrypted with ks1, decrypted with ks2 and
  18351. + then encrypted again with ks3, before being put into output;
  18352. + C=E(ks3,D(ks2,E(ks1,M))). There is a macro, des_ecb2_encrypt()
  18353. + that only takes 2 des_key_schedules that implements,
  18354. + C=E(ks1,D(ks2,E(ks1,M))) in that the final encrypt is done with ks1.
  18355. +
  18356. +void des_cbc_encrypt(
  18357. +des_cblock *input,
  18358. +des_cblock *output,
  18359. +long length,
  18360. +des_key_schedule ks,
  18361. +des_cblock *ivec,
  18362. +int enc);
  18363. + This routine implements DES in Cipher Block Chaining mode.
  18364. + Input, which should be a multiple of 8 bytes is encrypted
  18365. + (or decrypted) to output which will also be a multiple of 8 bytes.
  18366. + The number of bytes is in length (and from what I've said above,
  18367. + should be a multiple of 8). If length is not a multiple of 8, I'm
  18368. + not being held responsible :-). ivec is the initialisation vector.
  18369. + This function does not modify this variable. To correctly implement
  18370. + cbc mode, you need to do one of 2 things; copy the last 8 bytes of
  18371. + cipher text for use as the next ivec in your application,
  18372. + or use des_ncbc_encrypt().
  18373. + Only this routine has this problem with updating the ivec, all
  18374. + other routines that are implementing cbc mode update ivec.
  18375. +
  18376. +void des_ncbc_encrypt(
  18377. +des_cblock *input,
  18378. +des_cblock *output,
  18379. +long length,
  18380. +des_key_schedule sk,
  18381. +des_cblock *ivec,
  18382. +int enc);
  18383. + For historical reasons, des_cbc_encrypt() did not update the
  18384. + ivec with the value requires so that subsequent calls to
  18385. + des_cbc_encrypt() would 'chain'. This was needed so that the same
  18386. + 'length' values would not need to be used when decrypting.
  18387. + des_ncbc_encrypt() does the right thing. It is the same as
  18388. + des_cbc_encrypt accept that ivec is updates with the correct value
  18389. + to pass in subsequent calls to des_ncbc_encrypt(). I advise using
  18390. + des_ncbc_encrypt() instead of des_cbc_encrypt();
  18391. +
  18392. +void des_xcbc_encrypt(
  18393. +des_cblock *input,
  18394. +des_cblock *output,
  18395. +long length,
  18396. +des_key_schedule sk,
  18397. +des_cblock *ivec,
  18398. +des_cblock *inw,
  18399. +des_cblock *outw,
  18400. +int enc);
  18401. + This is RSA's DESX mode of DES. It uses inw and outw to
  18402. + 'whiten' the encryption. inw and outw are secret (unlike the iv)
  18403. + and are as such, part of the key. So the key is sort of 24 bytes.
  18404. + This is much better than cbc des.
  18405. +
  18406. +void des_3cbc_encrypt(
  18407. +des_cblock *input,
  18408. +des_cblock *output,
  18409. +long length,
  18410. +des_key_schedule sk1,
  18411. +des_key_schedule sk2,
  18412. +des_cblock *ivec1,
  18413. +des_cblock *ivec2,
  18414. +int enc);
  18415. + This function is flawed, do not use it. I have left it in the
  18416. + library because it is used in my des(1) program and will function
  18417. + correctly when used by des(1). If I removed the function, people
  18418. + could end up unable to decrypt files.
  18419. + This routine implements outer triple cbc encryption using 2 ks and
  18420. + 2 ivec's. Use des_ede2_cbc_encrypt() instead.
  18421. +
  18422. +void des_ede3_cbc_encrypt(
  18423. +des_cblock *input,
  18424. +des_cblock *output,
  18425. +long length,
  18426. +des_key_schedule ks1,
  18427. +des_key_schedule ks2,
  18428. +des_key_schedule ks3,
  18429. +des_cblock *ivec,
  18430. +int enc);
  18431. + This function implements inner triple CBC DES encryption with 3
  18432. + keys. What this means is that each 'DES' operation
  18433. + inside the cbc mode is really an C=E(ks3,D(ks2,E(ks1,M))).
  18434. + Again, this is cbc mode so an ivec is requires.
  18435. + This mode is used by SSL.
  18436. + There is also a des_ede2_cbc_encrypt() that only uses 2
  18437. + des_key_schedule's, the first being reused for the final
  18438. + encryption. C=E(ks1,D(ks2,E(ks1,M))). This form of triple DES
  18439. + is used by the RSAref library.
  18440. +
  18441. +void des_pcbc_encrypt(
  18442. +des_cblock *input,
  18443. +des_cblock *output,
  18444. +long length,
  18445. +des_key_schedule ks,
  18446. +des_cblock *ivec,
  18447. +int enc);
  18448. + This is Propagating Cipher Block Chaining mode of DES. It is used
  18449. + by Kerberos v4. It's parameters are the same as des_ncbc_encrypt().
  18450. +
  18451. +void des_cfb_encrypt(
  18452. +unsigned char *in,
  18453. +unsigned char *out,
  18454. +int numbits,
  18455. +long length,
  18456. +des_key_schedule ks,
  18457. +des_cblock *ivec,
  18458. +int enc);
  18459. + Cipher Feedback Back mode of DES. This implementation 'feeds back'
  18460. + in numbit blocks. The input (and output) is in multiples of numbits
  18461. + bits. numbits should to be a multiple of 8 bits. Length is the
  18462. + number of bytes input. If numbits is not a multiple of 8 bits,
  18463. + the extra bits in the bytes will be considered padding. So if
  18464. + numbits is 12, for each 2 input bytes, the 4 high bits of the
  18465. + second byte will be ignored. So to encode 72 bits when using
  18466. + a numbits of 12 take 12 bytes. To encode 72 bits when using
  18467. + numbits of 9 will take 16 bytes. To encode 80 bits when using
  18468. + numbits of 16 will take 10 bytes. etc, etc. This padding will
  18469. + apply to both input and output.
  18470. +
  18471. +
  18472. +void des_cfb64_encrypt(
  18473. +unsigned char *in,
  18474. +unsigned char *out,
  18475. +long length,
  18476. +des_key_schedule ks,
  18477. +des_cblock *ivec,
  18478. +int *num,
  18479. +int enc);
  18480. + This is one of the more useful functions in this DES library, it
  18481. + implements CFB mode of DES with 64bit feedback. Why is this
  18482. + useful you ask? Because this routine will allow you to encrypt an
  18483. + arbitrary number of bytes, no 8 byte padding. Each call to this
  18484. + routine will encrypt the input bytes to output and then update ivec
  18485. + and num. num contains 'how far' we are though ivec. If this does
  18486. + not make much sense, read more about cfb mode of DES :-).
  18487. +
  18488. +void des_ede3_cfb64_encrypt(
  18489. +unsigned char *in,
  18490. +unsigned char *out,
  18491. +long length,
  18492. +des_key_schedule ks1,
  18493. +des_key_schedule ks2,
  18494. +des_key_schedule ks3,
  18495. +des_cblock *ivec,
  18496. +int *num,
  18497. +int enc);
  18498. + Same as des_cfb64_encrypt() accept that the DES operation is
  18499. + triple DES. As usual, there is a macro for
  18500. + des_ede2_cfb64_encrypt() which reuses ks1.
  18501. +
  18502. +void des_ofb_encrypt(
  18503. +unsigned char *in,
  18504. +unsigned char *out,
  18505. +int numbits,
  18506. +long length,
  18507. +des_key_schedule ks,
  18508. +des_cblock *ivec);
  18509. + This is a implementation of Output Feed Back mode of DES. It is
  18510. + the same as des_cfb_encrypt() in that numbits is the size of the
  18511. + units dealt with during input and output (in bits).
  18512. +
  18513. +void des_ofb64_encrypt(
  18514. +unsigned char *in,
  18515. +unsigned char *out,
  18516. +long length,
  18517. +des_key_schedule ks,
  18518. +des_cblock *ivec,
  18519. +int *num);
  18520. + The same as des_cfb64_encrypt() except that it is Output Feed Back
  18521. + mode.
  18522. +
  18523. +void des_ede3_ofb64_encrypt(
  18524. +unsigned char *in,
  18525. +unsigned char *out,
  18526. +long length,
  18527. +des_key_schedule ks1,
  18528. +des_key_schedule ks2,
  18529. +des_key_schedule ks3,
  18530. +des_cblock *ivec,
  18531. +int *num);
  18532. + Same as des_ofb64_encrypt() accept that the DES operation is
  18533. + triple DES. As usual, there is a macro for
  18534. + des_ede2_ofb64_encrypt() which reuses ks1.
  18535. +
  18536. +int des_read_pw_string(
  18537. +char *buf,
  18538. +int length,
  18539. +char *prompt,
  18540. +int verify);
  18541. + This routine is used to get a password from the terminal with echo
  18542. + turned off. Buf is where the string will end up and length is the
  18543. + size of buf. Prompt is a string presented to the 'user' and if
  18544. + verify is set, the key is asked for twice and unless the 2 copies
  18545. + match, an error is returned. A return code of -1 indicates a
  18546. + system error, 1 failure due to use interaction, and 0 is success.
  18547. +
  18548. +unsigned long des_cbc_cksum(
  18549. +des_cblock *input,
  18550. +des_cblock *output,
  18551. +long length,
  18552. +des_key_schedule ks,
  18553. +des_cblock *ivec);
  18554. + This function produces an 8 byte checksum from input that it puts in
  18555. + output and returns the last 4 bytes as a long. The checksum is
  18556. + generated via cbc mode of DES in which only the last 8 byes are
  18557. + kept. I would recommend not using this function but instead using
  18558. + the EVP_Digest routines, or at least using MD5 or SHA. This
  18559. + function is used by Kerberos v4 so that is why it stays in the
  18560. + library.
  18561. +
  18562. +char *des_fcrypt(
  18563. +const char *buf,
  18564. +const char *salt
  18565. +char *ret);
  18566. + This is my fast version of the unix crypt(3) function. This version
  18567. + takes only a small amount of space relative to other fast
  18568. + crypt() implementations. This is different to the normal crypt
  18569. + in that the third parameter is the buffer that the return value
  18570. + is written into. It needs to be at least 14 bytes long. This
  18571. + function is thread safe, unlike the normal crypt.
  18572. +
  18573. +char *crypt(
  18574. +const char *buf,
  18575. +const char *salt);
  18576. + This function calls des_fcrypt() with a static array passed as the
  18577. + third parameter. This emulates the normal non-thread safe semantics
  18578. + of crypt(3).
  18579. +
  18580. +void des_string_to_key(
  18581. +char *str,
  18582. +des_cblock *key);
  18583. + This function takes str and converts it into a DES key. I would
  18584. + recommend using MD5 instead and use the first 8 bytes of output.
  18585. + When I wrote the first version of these routines back in 1990, MD5
  18586. + did not exist but I feel these routines are still sound. This
  18587. + routines is compatible with the one in MIT's libdes.
  18588. +
  18589. +void des_string_to_2keys(
  18590. +char *str,
  18591. +des_cblock *key1,
  18592. +des_cblock *key2);
  18593. + This function takes str and converts it into 2 DES keys.
  18594. + I would recommend using MD5 and using the 16 bytes as the 2 keys.
  18595. + I have nothing against these 2 'string_to_key' routines, it's just
  18596. + that if you say that your encryption key is generated by using the
  18597. + 16 bytes of an MD5 hash, every-one knows how you generated your
  18598. + keys.
  18599. +
  18600. +int des_read_password(
  18601. +des_cblock *key,
  18602. +char *prompt,
  18603. +int verify);
  18604. + This routine combines des_read_pw_string() with des_string_to_key().
  18605. +
  18606. +int des_read_2passwords(
  18607. +des_cblock *key1,
  18608. +des_cblock *key2,
  18609. +char *prompt,
  18610. +int verify);
  18611. + This routine combines des_read_pw_string() with des_string_to_2key().
  18612. +
  18613. +void des_random_seed(
  18614. +des_cblock key);
  18615. + This routine sets a starting point for des_random_key().
  18616. +
  18617. +void des_random_key(
  18618. +des_cblock ret);
  18619. + This function return a random key. Make sure to 'seed' the random
  18620. + number generator (with des_random_seed()) before using this function.
  18621. + I personally now use a MD5 based random number system.
  18622. +
  18623. +int des_enc_read(
  18624. +int fd,
  18625. +char *buf,
  18626. +int len,
  18627. +des_key_schedule ks,
  18628. +des_cblock *iv);
  18629. + This function will write to a file descriptor the encrypted data
  18630. + from buf. This data will be preceded by a 4 byte 'byte count' and
  18631. + will be padded out to 8 bytes. The encryption is either CBC of
  18632. + PCBC depending on the value of des_rw_mode. If it is DES_PCBC_MODE,
  18633. + pcbc is used, if DES_CBC_MODE, cbc is used. The default is to use
  18634. + DES_PCBC_MODE.
  18635. +
  18636. +int des_enc_write(
  18637. +int fd,
  18638. +char *buf,
  18639. +int len,
  18640. +des_key_schedule ks,
  18641. +des_cblock *iv);
  18642. + This routines read stuff written by des_enc_read() and decrypts it.
  18643. + I have used these routines quite a lot but I don't believe they are
  18644. + suitable for non-blocking io. If you are after a full
  18645. + authentication/encryption over networks, have a look at SSL instead.
  18646. +
  18647. +unsigned long des_quad_cksum(
  18648. +des_cblock *input,
  18649. +des_cblock *output,
  18650. +long length,
  18651. +int out_count,
  18652. +des_cblock *seed);
  18653. + This is a function from Kerberos v4 that is not anything to do with
  18654. + DES but was needed. It is a cksum that is quicker to generate than
  18655. + des_cbc_cksum(); I personally would use MD5 routines now.
  18656. +=====
  18657. +Modes of DES
  18658. +Quite a bit of the following information has been taken from
  18659. + AS 2805.5.2
  18660. + Australian Standard
  18661. + Electronic funds transfer - Requirements for interfaces,
  18662. + Part 5.2: Modes of operation for an n-bit block cipher algorithm
  18663. + Appendix A
  18664. +
  18665. +There are several different modes in which DES can be used, they are
  18666. +as follows.
  18667. +
  18668. +Electronic Codebook Mode (ECB) (des_ecb_encrypt())
  18669. +- 64 bits are enciphered at a time.
  18670. +- The order of the blocks can be rearranged without detection.
  18671. +- The same plaintext block always produces the same ciphertext block
  18672. + (for the same key) making it vulnerable to a 'dictionary attack'.
  18673. +- An error will only affect one ciphertext block.
  18674. +
  18675. +Cipher Block Chaining Mode (CBC) (des_cbc_encrypt())
  18676. +- a multiple of 64 bits are enciphered at a time.
  18677. +- The CBC mode produces the same ciphertext whenever the same
  18678. + plaintext is encrypted using the same key and starting variable.
  18679. +- The chaining operation makes the ciphertext blocks dependent on the
  18680. + current and all preceding plaintext blocks and therefore blocks can not
  18681. + be rearranged.
  18682. +- The use of different starting variables prevents the same plaintext
  18683. + enciphering to the same ciphertext.
  18684. +- An error will affect the current and the following ciphertext blocks.
  18685. +
  18686. +Cipher Feedback Mode (CFB) (des_cfb_encrypt())
  18687. +- a number of bits (j) <= 64 are enciphered at a time.
  18688. +- The CFB mode produces the same ciphertext whenever the same
  18689. + plaintext is encrypted using the same key and starting variable.
  18690. +- The chaining operation makes the ciphertext variables dependent on the
  18691. + current and all preceding variables and therefore j-bit variables are
  18692. + chained together and can not be rearranged.
  18693. +- The use of different starting variables prevents the same plaintext
  18694. + enciphering to the same ciphertext.
  18695. +- The strength of the CFB mode depends on the size of k (maximal if
  18696. + j == k). In my implementation this is always the case.
  18697. +- Selection of a small value for j will require more cycles through
  18698. + the encipherment algorithm per unit of plaintext and thus cause
  18699. + greater processing overheads.
  18700. +- Only multiples of j bits can be enciphered.
  18701. +- An error will affect the current and the following ciphertext variables.
  18702. +
  18703. +Output Feedback Mode (OFB) (des_ofb_encrypt())
  18704. +- a number of bits (j) <= 64 are enciphered at a time.
  18705. +- The OFB mode produces the same ciphertext whenever the same
  18706. + plaintext enciphered using the same key and starting variable. More
  18707. + over, in the OFB mode the same key stream is produced when the same
  18708. + key and start variable are used. Consequently, for security reasons
  18709. + a specific start variable should be used only once for a given key.
  18710. +- The absence of chaining makes the OFB more vulnerable to specific attacks.
  18711. +- The use of different start variables values prevents the same
  18712. + plaintext enciphering to the same ciphertext, by producing different
  18713. + key streams.
  18714. +- Selection of a small value for j will require more cycles through
  18715. + the encipherment algorithm per unit of plaintext and thus cause
  18716. + greater processing overheads.
  18717. +- Only multiples of j bits can be enciphered.
  18718. +- OFB mode of operation does not extend ciphertext errors in the
  18719. + resultant plaintext output. Every bit error in the ciphertext causes
  18720. + only one bit to be in error in the deciphered plaintext.
  18721. +- OFB mode is not self-synchronising. If the two operation of
  18722. + encipherment and decipherment get out of synchronism, the system needs
  18723. + to be re-initialised.
  18724. +- Each re-initialisation should use a value of the start variable
  18725. + different from the start variable values used before with the same
  18726. + key. The reason for this is that an identical bit stream would be
  18727. + produced each time from the same parameters. This would be
  18728. + susceptible to a ' known plaintext' attack.
  18729. +
  18730. +Triple ECB Mode (des_ecb3_encrypt())
  18731. +- Encrypt with key1, decrypt with key2 and encrypt with key3 again.
  18732. +- As for ECB encryption but increases the key length to 168 bits.
  18733. + There are theoretic attacks that can be used that make the effective
  18734. + key length 112 bits, but this attack also requires 2^56 blocks of
  18735. + memory, not very likely, even for the NSA.
  18736. +- If both keys are the same it is equivalent to encrypting once with
  18737. + just one key.
  18738. +- If the first and last key are the same, the key length is 112 bits.
  18739. + There are attacks that could reduce the key space to 55 bit's but it
  18740. + requires 2^56 blocks of memory.
  18741. +- If all 3 keys are the same, this is effectively the same as normal
  18742. + ecb mode.
  18743. +
  18744. +Triple CBC Mode (des_ede3_cbc_encrypt())
  18745. +- Encrypt with key1, decrypt with key2 and then encrypt with key3.
  18746. +- As for CBC encryption but increases the key length to 168 bits with
  18747. + the same restrictions as for triple ecb mode.
  18748. --- /dev/null Tue Mar 11 13:02:56 2003
  18749. +++ linux/net/ipsec/des/des_enc.c Mon Feb 9 13:51:03 2004
  18750. @@ -0,0 +1,502 @@
  18751. +/* crypto/des/des_enc.c */
  18752. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  18753. + * All rights reserved.
  18754. + *
  18755. + * This package is an SSL implementation written
  18756. + * by Eric Young (eay@cryptsoft.com).
  18757. + * The implementation was written so as to conform with Netscapes SSL.
  18758. + *
  18759. + * This library is free for commercial and non-commercial use as long as
  18760. + * the following conditions are aheared to. The following conditions
  18761. + * apply to all code found in this distribution, be it the RC4, RSA,
  18762. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18763. + * included with this distribution is covered by the same copyright terms
  18764. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  18765. + *
  18766. + * Copyright remains Eric Young's, and as such any Copyright notices in
  18767. + * the code are not to be removed.
  18768. + * If this package is used in a product, Eric Young should be given attribution
  18769. + * as the author of the parts of the library used.
  18770. + * This can be in the form of a textual message at program startup or
  18771. + * in documentation (online or textual) provided with the package.
  18772. + *
  18773. + * Redistribution and use in source and binary forms, with or without
  18774. + * modification, are permitted provided that the following conditions
  18775. + * are met:
  18776. + * 1. Redistributions of source code must retain the copyright
  18777. + * notice, this list of conditions and the following disclaimer.
  18778. + * 2. Redistributions in binary form must reproduce the above copyright
  18779. + * notice, this list of conditions and the following disclaimer in the
  18780. + * documentation and/or other materials provided with the distribution.
  18781. + * 3. All advertising materials mentioning features or use of this software
  18782. + * must display the following acknowledgement:
  18783. + * "This product includes cryptographic software written by
  18784. + * Eric Young (eay@cryptsoft.com)"
  18785. + * The word 'cryptographic' can be left out if the rouines from the library
  18786. + * being used are not cryptographic related :-).
  18787. + * 4. If you include any Windows specific code (or a derivative thereof) from
  18788. + * the apps directory (application code) you must include an acknowledgement:
  18789. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  18790. + *
  18791. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  18792. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18793. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18794. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18795. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18796. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18797. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18798. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18799. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18800. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18801. + * SUCH DAMAGE.
  18802. + *
  18803. + * The licence and distribution terms for any publically available version or
  18804. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  18805. + * copied and put under another distribution licence
  18806. + * [including the GNU Public Licence.]
  18807. + */
  18808. +
  18809. +#include "des/des_locl.h"
  18810. +
  18811. +void des_encrypt(data, ks, enc)
  18812. +DES_LONG *data;
  18813. +des_key_schedule ks;
  18814. +int enc;
  18815. + {
  18816. + register DES_LONG l,r,t,u;
  18817. +#ifdef DES_PTR
  18818. + register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  18819. +#endif
  18820. +#ifndef DES_UNROLL
  18821. + register int i;
  18822. +#endif
  18823. + register DES_LONG *s;
  18824. +
  18825. + r=data[0];
  18826. + l=data[1];
  18827. +
  18828. + IP(r,l);
  18829. + /* Things have been modified so that the initial rotate is
  18830. + * done outside the loop. This required the
  18831. + * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  18832. + * One perl script later and things have a 5% speed up on a sparc2.
  18833. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  18834. + * for pointing this out. */
  18835. + /* clear the top bits on machines with 8byte longs */
  18836. + /* shift left by 2 */
  18837. + r=ROTATE(r,29)&0xffffffffL;
  18838. + l=ROTATE(l,29)&0xffffffffL;
  18839. +
  18840. + s=(DES_LONG *)ks;
  18841. + /* I don't know if it is worth the effort of loop unrolling the
  18842. + * inner loop */
  18843. + if (enc)
  18844. + {
  18845. +#ifdef DES_UNROLL
  18846. + D_ENCRYPT(l,r, 0); /* 1 */
  18847. + D_ENCRYPT(r,l, 2); /* 2 */
  18848. + D_ENCRYPT(l,r, 4); /* 3 */
  18849. + D_ENCRYPT(r,l, 6); /* 4 */
  18850. + D_ENCRYPT(l,r, 8); /* 5 */
  18851. + D_ENCRYPT(r,l,10); /* 6 */
  18852. + D_ENCRYPT(l,r,12); /* 7 */
  18853. + D_ENCRYPT(r,l,14); /* 8 */
  18854. + D_ENCRYPT(l,r,16); /* 9 */
  18855. + D_ENCRYPT(r,l,18); /* 10 */
  18856. + D_ENCRYPT(l,r,20); /* 11 */
  18857. + D_ENCRYPT(r,l,22); /* 12 */
  18858. + D_ENCRYPT(l,r,24); /* 13 */
  18859. + D_ENCRYPT(r,l,26); /* 14 */
  18860. + D_ENCRYPT(l,r,28); /* 15 */
  18861. + D_ENCRYPT(r,l,30); /* 16 */
  18862. +#else
  18863. + for (i=0; i<32; i+=8)
  18864. + {
  18865. + D_ENCRYPT(l,r,i+0); /* 1 */
  18866. + D_ENCRYPT(r,l,i+2); /* 2 */
  18867. + D_ENCRYPT(l,r,i+4); /* 3 */
  18868. + D_ENCRYPT(r,l,i+6); /* 4 */
  18869. + }
  18870. +#endif
  18871. + }
  18872. + else
  18873. + {
  18874. +#ifdef DES_UNROLL
  18875. + D_ENCRYPT(l,r,30); /* 16 */
  18876. + D_ENCRYPT(r,l,28); /* 15 */
  18877. + D_ENCRYPT(l,r,26); /* 14 */
  18878. + D_ENCRYPT(r,l,24); /* 13 */
  18879. + D_ENCRYPT(l,r,22); /* 12 */
  18880. + D_ENCRYPT(r,l,20); /* 11 */
  18881. + D_ENCRYPT(l,r,18); /* 10 */
  18882. + D_ENCRYPT(r,l,16); /* 9 */
  18883. + D_ENCRYPT(l,r,14); /* 8 */
  18884. + D_ENCRYPT(r,l,12); /* 7 */
  18885. + D_ENCRYPT(l,r,10); /* 6 */
  18886. + D_ENCRYPT(r,l, 8); /* 5 */
  18887. + D_ENCRYPT(l,r, 6); /* 4 */
  18888. + D_ENCRYPT(r,l, 4); /* 3 */
  18889. + D_ENCRYPT(l,r, 2); /* 2 */
  18890. + D_ENCRYPT(r,l, 0); /* 1 */
  18891. +#else
  18892. + for (i=30; i>0; i-=8)
  18893. + {
  18894. + D_ENCRYPT(l,r,i-0); /* 16 */
  18895. + D_ENCRYPT(r,l,i-2); /* 15 */
  18896. + D_ENCRYPT(l,r,i-4); /* 14 */
  18897. + D_ENCRYPT(r,l,i-6); /* 13 */
  18898. + }
  18899. +#endif
  18900. + }
  18901. +
  18902. + /* rotate and clear the top bits on machines with 8byte longs */
  18903. + l=ROTATE(l,3)&0xffffffffL;
  18904. + r=ROTATE(r,3)&0xffffffffL;
  18905. +
  18906. + FP(r,l);
  18907. + data[0]=l;
  18908. + data[1]=r;
  18909. + l=r=t=u=0;
  18910. + }
  18911. +
  18912. +void des_encrypt2(data, ks, enc)
  18913. +DES_LONG *data;
  18914. +des_key_schedule ks;
  18915. +int enc;
  18916. + {
  18917. + register DES_LONG l,r,t,u;
  18918. +#ifdef DES_PTR
  18919. + register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  18920. +#endif
  18921. +#ifndef DES_UNROLL
  18922. + register int i;
  18923. +#endif
  18924. + register DES_LONG *s;
  18925. +
  18926. + r=data[0];
  18927. + l=data[1];
  18928. +
  18929. + /* Things have been modified so that the initial rotate is
  18930. + * done outside the loop. This required the
  18931. + * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  18932. + * One perl script later and things have a 5% speed up on a sparc2.
  18933. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  18934. + * for pointing this out. */
  18935. + /* clear the top bits on machines with 8byte longs */
  18936. + r=ROTATE(r,29)&0xffffffffL;
  18937. + l=ROTATE(l,29)&0xffffffffL;
  18938. +
  18939. + s=(DES_LONG *)ks;
  18940. + /* I don't know if it is worth the effort of loop unrolling the
  18941. + * inner loop */
  18942. + if (enc)
  18943. + {
  18944. +#ifdef DES_UNROLL
  18945. + D_ENCRYPT(l,r, 0); /* 1 */
  18946. + D_ENCRYPT(r,l, 2); /* 2 */
  18947. + D_ENCRYPT(l,r, 4); /* 3 */
  18948. + D_ENCRYPT(r,l, 6); /* 4 */
  18949. + D_ENCRYPT(l,r, 8); /* 5 */
  18950. + D_ENCRYPT(r,l,10); /* 6 */
  18951. + D_ENCRYPT(l,r,12); /* 7 */
  18952. + D_ENCRYPT(r,l,14); /* 8 */
  18953. + D_ENCRYPT(l,r,16); /* 9 */
  18954. + D_ENCRYPT(r,l,18); /* 10 */
  18955. + D_ENCRYPT(l,r,20); /* 11 */
  18956. + D_ENCRYPT(r,l,22); /* 12 */
  18957. + D_ENCRYPT(l,r,24); /* 13 */
  18958. + D_ENCRYPT(r,l,26); /* 14 */
  18959. + D_ENCRYPT(l,r,28); /* 15 */
  18960. + D_ENCRYPT(r,l,30); /* 16 */
  18961. +#else
  18962. + for (i=0; i<32; i+=8)
  18963. + {
  18964. + D_ENCRYPT(l,r,i+0); /* 1 */
  18965. + D_ENCRYPT(r,l,i+2); /* 2 */
  18966. + D_ENCRYPT(l,r,i+4); /* 3 */
  18967. + D_ENCRYPT(r,l,i+6); /* 4 */
  18968. + }
  18969. +#endif
  18970. + }
  18971. + else
  18972. + {
  18973. +#ifdef DES_UNROLL
  18974. + D_ENCRYPT(l,r,30); /* 16 */
  18975. + D_ENCRYPT(r,l,28); /* 15 */
  18976. + D_ENCRYPT(l,r,26); /* 14 */
  18977. + D_ENCRYPT(r,l,24); /* 13 */
  18978. + D_ENCRYPT(l,r,22); /* 12 */
  18979. + D_ENCRYPT(r,l,20); /* 11 */
  18980. + D_ENCRYPT(l,r,18); /* 10 */
  18981. + D_ENCRYPT(r,l,16); /* 9 */
  18982. + D_ENCRYPT(l,r,14); /* 8 */
  18983. + D_ENCRYPT(r,l,12); /* 7 */
  18984. + D_ENCRYPT(l,r,10); /* 6 */
  18985. + D_ENCRYPT(r,l, 8); /* 5 */
  18986. + D_ENCRYPT(l,r, 6); /* 4 */
  18987. + D_ENCRYPT(r,l, 4); /* 3 */
  18988. + D_ENCRYPT(l,r, 2); /* 2 */
  18989. + D_ENCRYPT(r,l, 0); /* 1 */
  18990. +#else
  18991. + for (i=30; i>0; i-=8)
  18992. + {
  18993. + D_ENCRYPT(l,r,i-0); /* 16 */
  18994. + D_ENCRYPT(r,l,i-2); /* 15 */
  18995. + D_ENCRYPT(l,r,i-4); /* 14 */
  18996. + D_ENCRYPT(r,l,i-6); /* 13 */
  18997. + }
  18998. +#endif
  18999. + }
  19000. + /* rotate and clear the top bits on machines with 8byte longs */
  19001. + data[0]=ROTATE(l,3)&0xffffffffL;
  19002. + data[1]=ROTATE(r,3)&0xffffffffL;
  19003. + l=r=t=u=0;
  19004. + }
  19005. +
  19006. +void des_encrypt3(data,ks1,ks2,ks3)
  19007. +DES_LONG *data;
  19008. +des_key_schedule ks1;
  19009. +des_key_schedule ks2;
  19010. +des_key_schedule ks3;
  19011. + {
  19012. + register DES_LONG l,r;
  19013. +
  19014. + l=data[0];
  19015. + r=data[1];
  19016. + IP(l,r);
  19017. + data[0]=l;
  19018. + data[1]=r;
  19019. + des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
  19020. + des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
  19021. + des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
  19022. + l=data[0];
  19023. + r=data[1];
  19024. + FP(r,l);
  19025. + data[0]=l;
  19026. + data[1]=r;
  19027. + }
  19028. +
  19029. +void des_decrypt3(data,ks1,ks2,ks3)
  19030. +DES_LONG *data;
  19031. +des_key_schedule ks1;
  19032. +des_key_schedule ks2;
  19033. +des_key_schedule ks3;
  19034. + {
  19035. + register DES_LONG l,r;
  19036. +
  19037. + l=data[0];
  19038. + r=data[1];
  19039. + IP(l,r);
  19040. + data[0]=l;
  19041. + data[1]=r;
  19042. + des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
  19043. + des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
  19044. + des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
  19045. + l=data[0];
  19046. + r=data[1];
  19047. + FP(r,l);
  19048. + data[0]=l;
  19049. + data[1]=r;
  19050. + }
  19051. +
  19052. +#ifndef DES_DEFAULT_OPTIONS
  19053. +
  19054. +void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
  19055. +des_cblock (*input);
  19056. +des_cblock (*output);
  19057. +long length;
  19058. +des_key_schedule schedule;
  19059. +des_cblock (*ivec);
  19060. +int enc;
  19061. + {
  19062. + register DES_LONG tin0,tin1;
  19063. + register DES_LONG tout0,tout1,xor0,xor1;
  19064. + register unsigned char *in,*out;
  19065. + register long l=length;
  19066. + DES_LONG tin[2];
  19067. + unsigned char *iv;
  19068. +
  19069. + in=(unsigned char *)input;
  19070. + out=(unsigned char *)output;
  19071. + iv=(unsigned char *)ivec;
  19072. +
  19073. + if (enc)
  19074. + {
  19075. + c2l(iv,tout0);
  19076. + c2l(iv,tout1);
  19077. + for (l-=8; l>=0; l-=8)
  19078. + {
  19079. + c2l(in,tin0);
  19080. + c2l(in,tin1);
  19081. + tin0^=tout0; tin[0]=tin0;
  19082. + tin1^=tout1; tin[1]=tin1;
  19083. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  19084. + tout0=tin[0]; l2c(tout0,out);
  19085. + tout1=tin[1]; l2c(tout1,out);
  19086. + }
  19087. + if (l != -8)
  19088. + {
  19089. + c2ln(in,tin0,tin1,l+8);
  19090. + tin0^=tout0; tin[0]=tin0;
  19091. + tin1^=tout1; tin[1]=tin1;
  19092. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  19093. + tout0=tin[0]; l2c(tout0,out);
  19094. + tout1=tin[1]; l2c(tout1,out);
  19095. + }
  19096. + iv=(unsigned char *)ivec;
  19097. + l2c(tout0,iv);
  19098. + l2c(tout1,iv);
  19099. + }
  19100. + else
  19101. + {
  19102. + c2l(iv,xor0);
  19103. + c2l(iv,xor1);
  19104. + for (l-=8; l>=0; l-=8)
  19105. + {
  19106. + c2l(in,tin0); tin[0]=tin0;
  19107. + c2l(in,tin1); tin[1]=tin1;
  19108. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  19109. + tout0=tin[0]^xor0;
  19110. + tout1=tin[1]^xor1;
  19111. + l2c(tout0,out);
  19112. + l2c(tout1,out);
  19113. + xor0=tin0;
  19114. + xor1=tin1;
  19115. + }
  19116. + if (l != -8)
  19117. + {
  19118. + c2l(in,tin0); tin[0]=tin0;
  19119. + c2l(in,tin1); tin[1]=tin1;
  19120. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  19121. + tout0=tin[0]^xor0;
  19122. + tout1=tin[1]^xor1;
  19123. + l2cn(tout0,tout1,out,l+8);
  19124. + xor0=tin0;
  19125. + xor1=tin1;
  19126. + }
  19127. +
  19128. + iv=(unsigned char *)ivec;
  19129. + l2c(xor0,iv);
  19130. + l2c(xor1,iv);
  19131. + }
  19132. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  19133. + tin[0]=tin[1]=0;
  19134. + }
  19135. +
  19136. +void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, enc)
  19137. +des_cblock (*input);
  19138. +des_cblock (*output);
  19139. +long length;
  19140. +des_key_schedule ks1;
  19141. +des_key_schedule ks2;
  19142. +des_key_schedule ks3;
  19143. +des_cblock (*ivec);
  19144. +int enc;
  19145. + {
  19146. + register DES_LONG tin0,tin1;
  19147. + register DES_LONG tout0,tout1,xor0,xor1;
  19148. + register unsigned char *in,*out;
  19149. + register long l=length;
  19150. + DES_LONG tin[2];
  19151. + unsigned char *iv;
  19152. +
  19153. + in=(unsigned char *)input;
  19154. + out=(unsigned char *)output;
  19155. + iv=(unsigned char *)ivec;
  19156. +
  19157. + if (enc)
  19158. + {
  19159. + c2l(iv,tout0);
  19160. + c2l(iv,tout1);
  19161. + for (l-=8; l>=0; l-=8)
  19162. + {
  19163. + c2l(in,tin0);
  19164. + c2l(in,tin1);
  19165. + tin0^=tout0;
  19166. + tin1^=tout1;
  19167. +
  19168. + tin[0]=tin0;
  19169. + tin[1]=tin1;
  19170. + des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19171. + tout0=tin[0];
  19172. + tout1=tin[1];
  19173. +
  19174. + l2c(tout0,out);
  19175. + l2c(tout1,out);
  19176. + }
  19177. + if (l != -8)
  19178. + {
  19179. + c2ln(in,tin0,tin1,l+8);
  19180. + tin0^=tout0;
  19181. + tin1^=tout1;
  19182. +
  19183. + tin[0]=tin0;
  19184. + tin[1]=tin1;
  19185. + des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19186. + tout0=tin[0];
  19187. + tout1=tin[1];
  19188. +
  19189. + l2c(tout0,out);
  19190. + l2c(tout1,out);
  19191. + }
  19192. + iv=(unsigned char *)ivec;
  19193. + l2c(tout0,iv);
  19194. + l2c(tout1,iv);
  19195. + }
  19196. + else
  19197. + {
  19198. + register DES_LONG t0,t1;
  19199. +
  19200. + c2l(iv,xor0);
  19201. + c2l(iv,xor1);
  19202. + for (l-=8; l>=0; l-=8)
  19203. + {
  19204. + c2l(in,tin0);
  19205. + c2l(in,tin1);
  19206. +
  19207. + t0=tin0;
  19208. + t1=tin1;
  19209. +
  19210. + tin[0]=tin0;
  19211. + tin[1]=tin1;
  19212. + des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19213. + tout0=tin[0];
  19214. + tout1=tin[1];
  19215. +
  19216. + tout0^=xor0;
  19217. + tout1^=xor1;
  19218. + l2c(tout0,out);
  19219. + l2c(tout1,out);
  19220. + xor0=t0;
  19221. + xor1=t1;
  19222. + }
  19223. + if (l != -8)
  19224. + {
  19225. + c2l(in,tin0);
  19226. + c2l(in,tin1);
  19227. +
  19228. + t0=tin0;
  19229. + t1=tin1;
  19230. +
  19231. + tin[0]=tin0;
  19232. + tin[1]=tin1;
  19233. + des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19234. + tout0=tin[0];
  19235. + tout1=tin[1];
  19236. +
  19237. + tout0^=xor0;
  19238. + tout1^=xor1;
  19239. + l2cn(tout0,tout1,out,l+8);
  19240. + xor0=t0;
  19241. + xor1=t1;
  19242. + }
  19243. +
  19244. + iv=(unsigned char *)ivec;
  19245. + l2c(xor0,iv);
  19246. + l2c(xor1,iv);
  19247. + }
  19248. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  19249. + tin[0]=tin[1]=0;
  19250. + }
  19251. +
  19252. +#endif /* DES_DEFAULT_OPTIONS */
  19253. --- /dev/null Tue Mar 11 13:02:56 2003
  19254. +++ linux/net/ipsec/des/des_opts.c Mon Feb 9 13:51:03 2004
  19255. @@ -0,0 +1,620 @@
  19256. +/* crypto/des/des_opts.c */
  19257. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  19258. + * All rights reserved.
  19259. + *
  19260. + * This package is an SSL implementation written
  19261. + * by Eric Young (eay@cryptsoft.com).
  19262. + * The implementation was written so as to conform with Netscapes SSL.
  19263. + *
  19264. + * This library is free for commercial and non-commercial use as long as
  19265. + * the following conditions are aheared to. The following conditions
  19266. + * apply to all code found in this distribution, be it the RC4, RSA,
  19267. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  19268. + * included with this distribution is covered by the same copyright terms
  19269. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  19270. + *
  19271. + * Copyright remains Eric Young's, and as such any Copyright notices in
  19272. + * the code are not to be removed.
  19273. + * If this package is used in a product, Eric Young should be given attribution
  19274. + * as the author of the parts of the library used.
  19275. + * This can be in the form of a textual message at program startup or
  19276. + * in documentation (online or textual) provided with the package.
  19277. + *
  19278. + * Redistribution and use in source and binary forms, with or without
  19279. + * modification, are permitted provided that the following conditions
  19280. + * are met:
  19281. + * 1. Redistributions of source code must retain the copyright
  19282. + * notice, this list of conditions and the following disclaimer.
  19283. + * 2. Redistributions in binary form must reproduce the above copyright
  19284. + * notice, this list of conditions and the following disclaimer in the
  19285. + * documentation and/or other materials provided with the distribution.
  19286. + * 3. All advertising materials mentioning features or use of this software
  19287. + * must display the following acknowledgement:
  19288. + * "This product includes cryptographic software written by
  19289. + * Eric Young (eay@cryptsoft.com)"
  19290. + * The word 'cryptographic' can be left out if the rouines from the library
  19291. + * being used are not cryptographic related :-).
  19292. + * 4. If you include any Windows specific code (or a derivative thereof) from
  19293. + * the apps directory (application code) you must include an acknowledgement:
  19294. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  19295. + *
  19296. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  19297. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19298. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19299. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19300. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19301. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19302. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19303. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19304. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19305. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  19306. + * SUCH DAMAGE.
  19307. + *
  19308. + * The licence and distribution terms for any publically available version or
  19309. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  19310. + * copied and put under another distribution licence
  19311. + * [including the GNU Public Licence.]
  19312. + */
  19313. +
  19314. +/* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
  19315. + * This is for machines with 64k code segment size restrictions. */
  19316. +
  19317. +#ifndef MSDOS
  19318. +#define TIMES
  19319. +#endif
  19320. +
  19321. +#include <stdio.h>
  19322. +#ifndef MSDOS
  19323. +#include <unistd.h>
  19324. +#else
  19325. +#include <io.h>
  19326. +extern void exit();
  19327. +#endif
  19328. +#include <signal.h>
  19329. +#ifndef VMS
  19330. +#ifndef _IRIX
  19331. +#include <time.h>
  19332. +#endif
  19333. +#ifdef TIMES
  19334. +#include <sys/types.h>
  19335. +#include <sys/times.h>
  19336. +#endif
  19337. +#else /* VMS */
  19338. +#include <types.h>
  19339. +struct tms {
  19340. + time_t tms_utime;
  19341. + time_t tms_stime;
  19342. + time_t tms_uchild; /* I dunno... */
  19343. + time_t tms_uchildsys; /* so these names are a guess :-) */
  19344. + }
  19345. +#endif
  19346. +#ifndef TIMES
  19347. +#include <sys/timeb.h>
  19348. +#endif
  19349. +
  19350. +#ifdef sun
  19351. +#include <limits.h>
  19352. +#include <sys/param.h>
  19353. +#endif
  19354. +
  19355. +#include "des/des_locl.h"
  19356. +#include "des/spr.h"
  19357. +
  19358. +#define DES_DEFAULT_OPTIONS
  19359. +
  19360. +#if !defined(PART1) && !defined(PART2) && !defined(PART3) && !defined(PART4)
  19361. +#define PART1
  19362. +#define PART2
  19363. +#define PART3
  19364. +#define PART4
  19365. +#endif
  19366. +
  19367. +#ifdef PART1
  19368. +
  19369. +#undef DES_UNROLL
  19370. +#undef DES_RISC1
  19371. +#undef DES_RISC2
  19372. +#undef DES_PTR
  19373. +#undef D_ENCRYPT
  19374. +#define des_encrypt des_encrypt_u4_cisc_idx
  19375. +#define des_encrypt2 des_encrypt2_u4_cisc_idx
  19376. +#define des_encrypt3 des_encrypt3_u4_cisc_idx
  19377. +#define des_decrypt3 des_decrypt3_u4_cisc_idx
  19378. +#undef HEADER_DES_LOCL_H
  19379. +#include "des_enc.c"
  19380. +
  19381. +#define DES_UNROLL
  19382. +#undef DES_RISC1
  19383. +#undef DES_RISC2
  19384. +#undef DES_PTR
  19385. +#undef D_ENCRYPT
  19386. +#undef des_encrypt
  19387. +#undef des_encrypt2
  19388. +#undef des_encrypt3
  19389. +#undef des_decrypt3
  19390. +#define des_encrypt des_encrypt_u16_cisc_idx
  19391. +#define des_encrypt2 des_encrypt2_u16_cisc_idx
  19392. +#define des_encrypt3 des_encrypt3_u16_cisc_idx
  19393. +#define des_decrypt3 des_decrypt3_u16_cisc_idx
  19394. +#undef HEADER_DES_LOCL_H
  19395. +#include "des_enc.c"
  19396. +
  19397. +#undef DES_UNROLL
  19398. +#define DES_RISC1
  19399. +#undef DES_RISC2
  19400. +#undef DES_PTR
  19401. +#undef D_ENCRYPT
  19402. +#undef des_encrypt
  19403. +#undef des_encrypt2
  19404. +#undef des_encrypt3
  19405. +#undef des_decrypt3
  19406. +#define des_encrypt des_encrypt_u4_risc1_idx
  19407. +#define des_encrypt2 des_encrypt2_u4_risc1_idx
  19408. +#define des_encrypt3 des_encrypt3_u4_risc1_idx
  19409. +#define des_decrypt3 des_decrypt3_u4_risc1_idx
  19410. +#undef HEADER_DES_LOCL_H
  19411. +#include "des_enc.c"
  19412. +
  19413. +#endif
  19414. +
  19415. +#ifdef PART2
  19416. +
  19417. +#undef DES_UNROLL
  19418. +#undef DES_RISC1
  19419. +#define DES_RISC2
  19420. +#undef DES_PTR
  19421. +#undef D_ENCRYPT
  19422. +#undef des_encrypt
  19423. +#undef des_encrypt2
  19424. +#undef des_encrypt3
  19425. +#undef des_decrypt3
  19426. +#define des_encrypt des_encrypt_u4_risc2_idx
  19427. +#define des_encrypt2 des_encrypt2_u4_risc2_idx
  19428. +#define des_encrypt3 des_encrypt3_u4_risc2_idx
  19429. +#define des_decrypt3 des_decrypt3_u4_risc2_idx
  19430. +#undef HEADER_DES_LOCL_H
  19431. +#include "des_enc.c"
  19432. +
  19433. +#define DES_UNROLL
  19434. +#define DES_RISC1
  19435. +#undef DES_RISC2
  19436. +#undef DES_PTR
  19437. +#undef D_ENCRYPT
  19438. +#undef des_encrypt
  19439. +#undef des_encrypt2
  19440. +#undef des_encrypt3
  19441. +#undef des_decrypt3
  19442. +#define des_encrypt des_encrypt_u16_risc1_idx
  19443. +#define des_encrypt2 des_encrypt2_u16_risc1_idx
  19444. +#define des_encrypt3 des_encrypt3_u16_risc1_idx
  19445. +#define des_decrypt3 des_decrypt3_u16_risc1_idx
  19446. +#undef HEADER_DES_LOCL_H
  19447. +#include "des_enc.c"
  19448. +
  19449. +#define DES_UNROLL
  19450. +#undef DES_RISC1
  19451. +#define DES_RISC2
  19452. +#undef DES_PTR
  19453. +#undef D_ENCRYPT
  19454. +#undef des_encrypt
  19455. +#undef des_encrypt2
  19456. +#undef des_encrypt3
  19457. +#undef des_decrypt3
  19458. +#define des_encrypt des_encrypt_u16_risc2_idx
  19459. +#define des_encrypt2 des_encrypt2_u16_risc2_idx
  19460. +#define des_encrypt3 des_encrypt3_u16_risc2_idx
  19461. +#define des_decrypt3 des_decrypt3_u16_risc2_idx
  19462. +#undef HEADER_DES_LOCL_H
  19463. +#include "des_enc.c"
  19464. +
  19465. +#endif
  19466. +
  19467. +#ifdef PART3
  19468. +
  19469. +#undef DES_UNROLL
  19470. +#undef DES_RISC1
  19471. +#undef DES_RISC2
  19472. +#define DES_PTR
  19473. +#undef D_ENCRYPT
  19474. +#undef des_encrypt
  19475. +#undef des_encrypt2
  19476. +#undef des_encrypt3
  19477. +#undef des_decrypt3
  19478. +#define des_encrypt des_encrypt_u4_cisc_ptr
  19479. +#define des_encrypt2 des_encrypt2_u4_cisc_ptr
  19480. +#define des_encrypt3 des_encrypt3_u4_cisc_ptr
  19481. +#define des_decrypt3 des_decrypt3_u4_cisc_ptr
  19482. +#undef HEADER_DES_LOCL_H
  19483. +#include "des_enc.c"
  19484. +
  19485. +#define DES_UNROLL
  19486. +#undef DES_RISC1
  19487. +#undef DES_RISC2
  19488. +#define DES_PTR
  19489. +#undef D_ENCRYPT
  19490. +#undef des_encrypt
  19491. +#undef des_encrypt2
  19492. +#undef des_encrypt3
  19493. +#undef des_decrypt3
  19494. +#define des_encrypt des_encrypt_u16_cisc_ptr
  19495. +#define des_encrypt2 des_encrypt2_u16_cisc_ptr
  19496. +#define des_encrypt3 des_encrypt3_u16_cisc_ptr
  19497. +#define des_decrypt3 des_decrypt3_u16_cisc_ptr
  19498. +#undef HEADER_DES_LOCL_H
  19499. +#include "des_enc.c"
  19500. +
  19501. +#undef DES_UNROLL
  19502. +#define DES_RISC1
  19503. +#undef DES_RISC2
  19504. +#define DES_PTR
  19505. +#undef D_ENCRYPT
  19506. +#undef des_encrypt
  19507. +#undef des_encrypt2
  19508. +#undef des_encrypt3
  19509. +#undef des_decrypt3
  19510. +#define des_encrypt des_encrypt_u4_risc1_ptr
  19511. +#define des_encrypt2 des_encrypt2_u4_risc1_ptr
  19512. +#define des_encrypt3 des_encrypt3_u4_risc1_ptr
  19513. +#define des_decrypt3 des_decrypt3_u4_risc1_ptr
  19514. +#undef HEADER_DES_LOCL_H
  19515. +#include "des_enc.c"
  19516. +
  19517. +#endif
  19518. +
  19519. +#ifdef PART4
  19520. +
  19521. +#undef DES_UNROLL
  19522. +#undef DES_RISC1
  19523. +#define DES_RISC2
  19524. +#define DES_PTR
  19525. +#undef D_ENCRYPT
  19526. +#undef des_encrypt
  19527. +#undef des_encrypt2
  19528. +#undef des_encrypt3
  19529. +#undef des_decrypt3
  19530. +#define des_encrypt des_encrypt_u4_risc2_ptr
  19531. +#define des_encrypt2 des_encrypt2_u4_risc2_ptr
  19532. +#define des_encrypt3 des_encrypt3_u4_risc2_ptr
  19533. +#define des_decrypt3 des_decrypt3_u4_risc2_ptr
  19534. +#undef HEADER_DES_LOCL_H
  19535. +#include "des_enc.c"
  19536. +
  19537. +#define DES_UNROLL
  19538. +#define DES_RISC1
  19539. +#undef DES_RISC2
  19540. +#define DES_PTR
  19541. +#undef D_ENCRYPT
  19542. +#undef des_encrypt
  19543. +#undef des_encrypt2
  19544. +#undef des_encrypt3
  19545. +#undef des_decrypt3
  19546. +#define des_encrypt des_encrypt_u16_risc1_ptr
  19547. +#define des_encrypt2 des_encrypt2_u16_risc1_ptr
  19548. +#define des_encrypt3 des_encrypt3_u16_risc1_ptr
  19549. +#define des_decrypt3 des_decrypt3_u16_risc1_ptr
  19550. +#undef HEADER_DES_LOCL_H
  19551. +#include "des_enc.c"
  19552. +
  19553. +#define DES_UNROLL
  19554. +#undef DES_RISC1
  19555. +#define DES_RISC2
  19556. +#define DES_PTR
  19557. +#undef D_ENCRYPT
  19558. +#undef des_encrypt
  19559. +#undef des_encrypt2
  19560. +#undef des_encrypt3
  19561. +#undef des_decrypt3
  19562. +#define des_encrypt des_encrypt_u16_risc2_ptr
  19563. +#define des_encrypt2 des_encrypt2_u16_risc2_ptr
  19564. +#define des_encrypt3 des_encrypt3_u16_risc2_ptr
  19565. +#define des_decrypt3 des_decrypt3_u16_risc2_ptr
  19566. +#undef HEADER_DES_LOCL_H
  19567. +#include "des_enc.c"
  19568. +
  19569. +#endif
  19570. +
  19571. +/* The following if from times(3) man page. It may need to be changed */
  19572. +#ifndef HZ
  19573. +# ifndef CLK_TCK
  19574. +# ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
  19575. +# ifndef VMS
  19576. +# define HZ 100.0
  19577. +# else /* VMS */
  19578. +# define HZ 100.0
  19579. +# endif
  19580. +# else /* _BSD_CLK_TCK_ */
  19581. +# define HZ ((double)_BSD_CLK_TCK_)
  19582. +# endif
  19583. +# else /* CLK_TCK */
  19584. +# define HZ ((double)CLK_TCK)
  19585. +# endif
  19586. +#endif
  19587. +
  19588. +#define BUFSIZE ((long)1024)
  19589. +long run=0;
  19590. +
  19591. +#ifndef NOPROTO
  19592. +double Time_F(int s);
  19593. +#else
  19594. +double Time_F();
  19595. +#endif
  19596. +
  19597. +#ifdef SIGALRM
  19598. +#if defined(__STDC__) || defined(sgi)
  19599. +#define SIGRETTYPE void
  19600. +#else
  19601. +#define SIGRETTYPE int
  19602. +#endif
  19603. +
  19604. +#ifndef NOPROTO
  19605. +SIGRETTYPE sig_done(int sig);
  19606. +#else
  19607. +SIGRETTYPE sig_done();
  19608. +#endif
  19609. +
  19610. +SIGRETTYPE sig_done(sig)
  19611. +int sig;
  19612. + {
  19613. + signal(SIGALRM,sig_done);
  19614. + run=0;
  19615. +#ifdef LINT
  19616. + sig=sig;
  19617. +#endif
  19618. + }
  19619. +#endif
  19620. +
  19621. +#define START 0
  19622. +#define STOP 1
  19623. +
  19624. +double Time_F(s)
  19625. +int s;
  19626. + {
  19627. + double ret;
  19628. +#ifdef TIMES
  19629. + static struct tms tstart,tend;
  19630. +
  19631. + if (s == START)
  19632. + {
  19633. + times(&tstart);
  19634. + return(0);
  19635. + }
  19636. + else
  19637. + {
  19638. + times(&tend);
  19639. + ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
  19640. + return((ret == 0.0)?1e-6:ret);
  19641. + }
  19642. +#else /* !times() */
  19643. + static struct timeb tstart,tend;
  19644. + long i;
  19645. +
  19646. + if (s == START)
  19647. + {
  19648. + ftime(&tstart);
  19649. + return(0);
  19650. + }
  19651. + else
  19652. + {
  19653. + ftime(&tend);
  19654. + i=(long)tend.millitm-(long)tstart.millitm;
  19655. + ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
  19656. + return((ret == 0.0)?1e-6:ret);
  19657. + }
  19658. +#endif
  19659. + }
  19660. +
  19661. +#ifdef SIGALRM
  19662. +#define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
  19663. +#else
  19664. +#define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
  19665. +#endif
  19666. +
  19667. +#define time_it(func,name,index) \
  19668. + print_name(name); \
  19669. + Time_F(START); \
  19670. + for (count=0,run=1; COND(cb); count++) \
  19671. + { \
  19672. + unsigned long d[2]; \
  19673. + func(d,&(sch[0]),DES_ENCRYPT); \
  19674. + } \
  19675. + tm[index]=Time_F(STOP); \
  19676. + fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
  19677. + tm[index]=((double)COUNT(cb))/tm[index];
  19678. +
  19679. +#define print_it(name,index) \
  19680. + fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
  19681. + tm[index]*8,1.0e6/tm[index]);
  19682. +
  19683. +int main(argc,argv)
  19684. +int argc;
  19685. +char **argv;
  19686. + {
  19687. + long count;
  19688. + static unsigned char buf[BUFSIZE];
  19689. + static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
  19690. + static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
  19691. + static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
  19692. + des_key_schedule sch,sch2,sch3;
  19693. + double d,tm[16],max=0;
  19694. + int rank[16];
  19695. + char *str[16];
  19696. + int max_idx=0,i,num=0,j;
  19697. +#ifndef SIGALARM
  19698. + long ca,cb,cc,cd,ce;
  19699. +#endif
  19700. +
  19701. + for (i=0; i<12; i++)
  19702. + {
  19703. + tm[i]=0.0;
  19704. + rank[i]=0;
  19705. + }
  19706. +
  19707. +#ifndef TIMES
  19708. + fprintf(stderr,"To get the most acurate results, try to run this\n");
  19709. + fprintf(stderr,"program when this computer is idle.\n");
  19710. +#endif
  19711. +
  19712. + des_set_key((C_Block *)key,sch);
  19713. + des_set_key((C_Block *)key2,sch2);
  19714. + des_set_key((C_Block *)key3,sch3);
  19715. +
  19716. +#ifndef SIGALRM
  19717. + fprintf(stderr,"First we calculate the approximate speed ...\n");
  19718. + des_set_key((C_Block *)key,sch);
  19719. + count=10;
  19720. + do {
  19721. + long i;
  19722. + unsigned long data[2];
  19723. +
  19724. + count*=2;
  19725. + Time_F(START);
  19726. + for (i=count; i; i--)
  19727. + des_encrypt(data,&(sch[0]),DES_ENCRYPT);
  19728. + d=Time_F(STOP);
  19729. + } while (d < 3.0);
  19730. + ca=count;
  19731. + cb=count*3;
  19732. + cc=count*3*8/BUFSIZE+1;
  19733. + cd=count*8/BUFSIZE+1;
  19734. +
  19735. + ce=count/20+1;
  19736. +#define COND(d) (count != (d))
  19737. +#define COUNT(d) (d)
  19738. +#else
  19739. +#define COND(c) (run)
  19740. +#define COUNT(d) (count)
  19741. + signal(SIGALRM,sig_done);
  19742. + alarm(10);
  19743. +#endif
  19744. +
  19745. +#ifdef PART1
  19746. + time_it(des_encrypt_u4_cisc_idx, "des_encrypt_u4_cisc_idx ", 0);
  19747. + time_it(des_encrypt_u16_cisc_idx, "des_encrypt_u16_cisc_idx ", 1);
  19748. + time_it(des_encrypt_u4_risc1_idx, "des_encrypt_u4_risc1_idx ", 2);
  19749. + num+=3;
  19750. +#endif
  19751. +#ifdef PART2
  19752. + time_it(des_encrypt_u16_risc1_idx,"des_encrypt_u16_risc1_idx", 3);
  19753. + time_it(des_encrypt_u4_risc2_idx, "des_encrypt_u4_risc2_idx ", 4);
  19754. + time_it(des_encrypt_u16_risc2_idx,"des_encrypt_u16_risc2_idx", 5);
  19755. + num+=3;
  19756. +#endif
  19757. +#ifdef PART3
  19758. + time_it(des_encrypt_u4_cisc_ptr, "des_encrypt_u4_cisc_ptr ", 6);
  19759. + time_it(des_encrypt_u16_cisc_ptr, "des_encrypt_u16_cisc_ptr ", 7);
  19760. + time_it(des_encrypt_u4_risc1_ptr, "des_encrypt_u4_risc1_ptr ", 8);
  19761. + num+=3;
  19762. +#endif
  19763. +#ifdef PART4
  19764. + time_it(des_encrypt_u16_risc1_ptr,"des_encrypt_u16_risc1_ptr", 9);
  19765. + time_it(des_encrypt_u4_risc2_ptr, "des_encrypt_u4_risc2_ptr ",10);
  19766. + time_it(des_encrypt_u16_risc2_ptr,"des_encrypt_u16_risc2_ptr",11);
  19767. + num+=3;
  19768. +#endif
  19769. +
  19770. +#ifdef PART1
  19771. + str[0]=" 4 c i";
  19772. + print_it("des_encrypt_u4_cisc_idx ",0);
  19773. + max=tm[0];
  19774. + max_idx=0;
  19775. + str[1]="16 c i";
  19776. + print_it("des_encrypt_u16_cisc_idx ",1);
  19777. + if (max < tm[1]) { max=tm[1]; max_idx=1; }
  19778. + str[2]=" 4 r1 i";
  19779. + print_it("des_encrypt_u4_risc1_idx ",2);
  19780. + if (max < tm[2]) { max=tm[2]; max_idx=2; }
  19781. +#endif
  19782. +#ifdef PART2
  19783. + str[3]="16 r1 i";
  19784. + print_it("des_encrypt_u16_risc1_idx",3);
  19785. + if (max < tm[3]) { max=tm[3]; max_idx=3; }
  19786. + str[4]=" 4 r2 i";
  19787. + print_it("des_encrypt_u4_risc2_idx ",4);
  19788. + if (max < tm[4]) { max=tm[4]; max_idx=4; }
  19789. + str[5]="16 r2 i";
  19790. + print_it("des_encrypt_u16_risc2_idx",5);
  19791. + if (max < tm[5]) { max=tm[5]; max_idx=5; }
  19792. +#endif
  19793. +#ifdef PART3
  19794. + str[6]=" 4 c p";
  19795. + print_it("des_encrypt_u4_cisc_ptr ",6);
  19796. + if (max < tm[6]) { max=tm[6]; max_idx=6; }
  19797. + str[7]="16 c p";
  19798. + print_it("des_encrypt_u16_cisc_ptr ",7);
  19799. + if (max < tm[7]) { max=tm[7]; max_idx=7; }
  19800. + str[8]=" 4 r1 p";
  19801. + print_it("des_encrypt_u4_risc1_ptr ",8);
  19802. + if (max < tm[8]) { max=tm[8]; max_idx=8; }
  19803. +#endif
  19804. +#ifdef PART4
  19805. + str[9]="16 r1 p";
  19806. + print_it("des_encrypt_u16_risc1_ptr",9);
  19807. + if (max < tm[9]) { max=tm[9]; max_idx=9; }
  19808. + str[10]=" 4 r2 p";
  19809. + print_it("des_encrypt_u4_risc2_ptr ",10);
  19810. + if (max < tm[10]) { max=tm[10]; max_idx=10; }
  19811. + str[11]="16 r2 p";
  19812. + print_it("des_encrypt_u16_risc2_ptr",11);
  19813. + if (max < tm[11]) { max=tm[11]; max_idx=11; }
  19814. +#endif
  19815. + printf("options des ecb/s\n");
  19816. + printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
  19817. + d=tm[max_idx];
  19818. + tm[max_idx]= -2.0;
  19819. + max= -1.0;
  19820. + for (;;)
  19821. + {
  19822. + for (i=0; i<12; i++)
  19823. + {
  19824. + if (max < tm[i]) { max=tm[i]; j=i; }
  19825. + }
  19826. + if (max < 0.0) break;
  19827. + printf("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
  19828. + tm[j]= -2.0;
  19829. + max= -1.0;
  19830. + }
  19831. +
  19832. + switch (max_idx)
  19833. + {
  19834. + case 0:
  19835. + printf("-DDES_DEFAULT_OPTIONS\n");
  19836. + break;
  19837. + case 1:
  19838. + printf("-DDES_UNROLL\n");
  19839. + break;
  19840. + case 2:
  19841. + printf("-DDES_RISC1\n");
  19842. + break;
  19843. + case 3:
  19844. + printf("-DDES_UNROLL -DDES_RISC1\n");
  19845. + break;
  19846. + case 4:
  19847. + printf("-DDES_RISC2\n");
  19848. + break;
  19849. + case 5:
  19850. + printf("-DDES_UNROLL -DDES_RISC2\n");
  19851. + break;
  19852. + case 6:
  19853. + printf("-DDES_PTR\n");
  19854. + break;
  19855. + case 7:
  19856. + printf("-DDES_UNROLL -DDES_PTR\n");
  19857. + break;
  19858. + case 8:
  19859. + printf("-DDES_RISC1 -DDES_PTR\n");
  19860. + break;
  19861. + case 9:
  19862. + printf("-DDES_UNROLL -DDES_RISC1 -DDES_PTR\n");
  19863. + break;
  19864. + case 10:
  19865. + printf("-DDES_RISC2 -DDES_PTR\n");
  19866. + break;
  19867. + case 11:
  19868. + printf("-DDES_UNROLL -DDES_RISC2 -DDES_PTR\n");
  19869. + break;
  19870. + }
  19871. + exit(0);
  19872. +#if defined(LINT) || defined(MSDOS)
  19873. + return(0);
  19874. +#endif
  19875. + }
  19876. --- /dev/null Tue Mar 11 13:02:56 2003
  19877. +++ linux/net/ipsec/des/dx86unix.S Mon Feb 9 13:51:03 2004
  19878. @@ -0,0 +1,3160 @@
  19879. +/*
  19880. + * This file was originally generated by Michael Richardson <mcr@freeswan.org>
  19881. + * via the perl scripts found in the ASM subdir. It remains copyright of
  19882. + * Eric Young, see the file COPYRIGHT.
  19883. + *
  19884. + * This was last done on October 9, 2002.
  19885. + *
  19886. + * While this file does not need to go through cpp, we pass it through
  19887. + * CPP by naming it dx86unix.S instead of dx86unix.s because there is
  19888. + * a bug in Rules.make for .s builds - specifically it references EXTRA_CFLAGS
  19889. + * which may contain stuff that AS doesn't understand instead of
  19890. + * referencing EXTRA_AFLAGS.
  19891. + */
  19892. +
  19893. + .file "dx86unix.S"
  19894. + .version "01.01"
  19895. +.text
  19896. + .align 16
  19897. +.globl des_encrypt
  19898. + .type des_encrypt , @function
  19899. +des_encrypt:
  19900. + pushl %esi
  19901. + pushl %edi
  19902. +
  19903. +
  19904. + movl 12(%esp), %esi
  19905. + xorl %ecx, %ecx
  19906. + pushl %ebx
  19907. + pushl %ebp
  19908. + movl (%esi), %eax
  19909. + movl 28(%esp), %ebx
  19910. + movl 4(%esi), %edi
  19911. +
  19912. +
  19913. + roll $4, %eax
  19914. + movl %eax, %esi
  19915. + xorl %edi, %eax
  19916. + andl $0xf0f0f0f0, %eax
  19917. + xorl %eax, %esi
  19918. + xorl %eax, %edi
  19919. +
  19920. + roll $20, %edi
  19921. + movl %edi, %eax
  19922. + xorl %esi, %edi
  19923. + andl $0xfff0000f, %edi
  19924. + xorl %edi, %eax
  19925. + xorl %edi, %esi
  19926. +
  19927. + roll $14, %eax
  19928. + movl %eax, %edi
  19929. + xorl %esi, %eax
  19930. + andl $0x33333333, %eax
  19931. + xorl %eax, %edi
  19932. + xorl %eax, %esi
  19933. +
  19934. + roll $22, %esi
  19935. + movl %esi, %eax
  19936. + xorl %edi, %esi
  19937. + andl $0x03fc03fc, %esi
  19938. + xorl %esi, %eax
  19939. + xorl %esi, %edi
  19940. +
  19941. + roll $9, %eax
  19942. + movl %eax, %esi
  19943. + xorl %edi, %eax
  19944. + andl $0xaaaaaaaa, %eax
  19945. + xorl %eax, %esi
  19946. + xorl %eax, %edi
  19947. +
  19948. +.byte 209
  19949. +.byte 199
  19950. + movl 24(%esp), %ebp
  19951. + cmpl $0, %ebx
  19952. + je .L000start_decrypt
  19953. +
  19954. +
  19955. + movl (%ebp), %eax
  19956. + xorl %ebx, %ebx
  19957. + movl 4(%ebp), %edx
  19958. + xorl %esi, %eax
  19959. + xorl %esi, %edx
  19960. + andl $0xfcfcfcfc, %eax
  19961. + andl $0xcfcfcfcf, %edx
  19962. + movb %al, %bl
  19963. + movb %ah, %cl
  19964. + rorl $4, %edx
  19965. + movl des_SPtrans(%ebx),%ebp
  19966. + movb %dl, %bl
  19967. + xorl %ebp, %edi
  19968. + movl 0x200+des_SPtrans(%ecx),%ebp
  19969. + xorl %ebp, %edi
  19970. + movb %dh, %cl
  19971. + shrl $16, %eax
  19972. + movl 0x100+des_SPtrans(%ebx),%ebp
  19973. + xorl %ebp, %edi
  19974. + movb %ah, %bl
  19975. + shrl $16, %edx
  19976. + movl 0x300+des_SPtrans(%ecx),%ebp
  19977. + xorl %ebp, %edi
  19978. + movl 24(%esp), %ebp
  19979. + movb %dh, %cl
  19980. + andl $0xff, %eax
  19981. + andl $0xff, %edx
  19982. + movl 0x600+des_SPtrans(%ebx),%ebx
  19983. + xorl %ebx, %edi
  19984. + movl 0x700+des_SPtrans(%ecx),%ebx
  19985. + xorl %ebx, %edi
  19986. + movl 0x400+des_SPtrans(%eax),%ebx
  19987. + xorl %ebx, %edi
  19988. + movl 0x500+des_SPtrans(%edx),%ebx
  19989. + xorl %ebx, %edi
  19990. +
  19991. +
  19992. + movl 8(%ebp), %eax
  19993. + xorl %ebx, %ebx
  19994. + movl 12(%ebp), %edx
  19995. + xorl %edi, %eax
  19996. + xorl %edi, %edx
  19997. + andl $0xfcfcfcfc, %eax
  19998. + andl $0xcfcfcfcf, %edx
  19999. + movb %al, %bl
  20000. + movb %ah, %cl
  20001. + rorl $4, %edx
  20002. + movl des_SPtrans(%ebx),%ebp
  20003. + movb %dl, %bl
  20004. + xorl %ebp, %esi
  20005. + movl 0x200+des_SPtrans(%ecx),%ebp
  20006. + xorl %ebp, %esi
  20007. + movb %dh, %cl
  20008. + shrl $16, %eax
  20009. + movl 0x100+des_SPtrans(%ebx),%ebp
  20010. + xorl %ebp, %esi
  20011. + movb %ah, %bl
  20012. + shrl $16, %edx
  20013. + movl 0x300+des_SPtrans(%ecx),%ebp
  20014. + xorl %ebp, %esi
  20015. + movl 24(%esp), %ebp
  20016. + movb %dh, %cl
  20017. + andl $0xff, %eax
  20018. + andl $0xff, %edx
  20019. + movl 0x600+des_SPtrans(%ebx),%ebx
  20020. + xorl %ebx, %esi
  20021. + movl 0x700+des_SPtrans(%ecx),%ebx
  20022. + xorl %ebx, %esi
  20023. + movl 0x400+des_SPtrans(%eax),%ebx
  20024. + xorl %ebx, %esi
  20025. + movl 0x500+des_SPtrans(%edx),%ebx
  20026. + xorl %ebx, %esi
  20027. +
  20028. +
  20029. + movl 16(%ebp), %eax
  20030. + xorl %ebx, %ebx
  20031. + movl 20(%ebp), %edx
  20032. + xorl %esi, %eax
  20033. + xorl %esi, %edx
  20034. + andl $0xfcfcfcfc, %eax
  20035. + andl $0xcfcfcfcf, %edx
  20036. + movb %al, %bl
  20037. + movb %ah, %cl
  20038. + rorl $4, %edx
  20039. + movl des_SPtrans(%ebx),%ebp
  20040. + movb %dl, %bl
  20041. + xorl %ebp, %edi
  20042. + movl 0x200+des_SPtrans(%ecx),%ebp
  20043. + xorl %ebp, %edi
  20044. + movb %dh, %cl
  20045. + shrl $16, %eax
  20046. + movl 0x100+des_SPtrans(%ebx),%ebp
  20047. + xorl %ebp, %edi
  20048. + movb %ah, %bl
  20049. + shrl $16, %edx
  20050. + movl 0x300+des_SPtrans(%ecx),%ebp
  20051. + xorl %ebp, %edi
  20052. + movl 24(%esp), %ebp
  20053. + movb %dh, %cl
  20054. + andl $0xff, %eax
  20055. + andl $0xff, %edx
  20056. + movl 0x600+des_SPtrans(%ebx),%ebx
  20057. + xorl %ebx, %edi
  20058. + movl 0x700+des_SPtrans(%ecx),%ebx
  20059. + xorl %ebx, %edi
  20060. + movl 0x400+des_SPtrans(%eax),%ebx
  20061. + xorl %ebx, %edi
  20062. + movl 0x500+des_SPtrans(%edx),%ebx
  20063. + xorl %ebx, %edi
  20064. +
  20065. +
  20066. + movl 24(%ebp), %eax
  20067. + xorl %ebx, %ebx
  20068. + movl 28(%ebp), %edx
  20069. + xorl %edi, %eax
  20070. + xorl %edi, %edx
  20071. + andl $0xfcfcfcfc, %eax
  20072. + andl $0xcfcfcfcf, %edx
  20073. + movb %al, %bl
  20074. + movb %ah, %cl
  20075. + rorl $4, %edx
  20076. + movl des_SPtrans(%ebx),%ebp
  20077. + movb %dl, %bl
  20078. + xorl %ebp, %esi
  20079. + movl 0x200+des_SPtrans(%ecx),%ebp
  20080. + xorl %ebp, %esi
  20081. + movb %dh, %cl
  20082. + shrl $16, %eax
  20083. + movl 0x100+des_SPtrans(%ebx),%ebp
  20084. + xorl %ebp, %esi
  20085. + movb %ah, %bl
  20086. + shrl $16, %edx
  20087. + movl 0x300+des_SPtrans(%ecx),%ebp
  20088. + xorl %ebp, %esi
  20089. + movl 24(%esp), %ebp
  20090. + movb %dh, %cl
  20091. + andl $0xff, %eax
  20092. + andl $0xff, %edx
  20093. + movl 0x600+des_SPtrans(%ebx),%ebx
  20094. + xorl %ebx, %esi
  20095. + movl 0x700+des_SPtrans(%ecx),%ebx
  20096. + xorl %ebx, %esi
  20097. + movl 0x400+des_SPtrans(%eax),%ebx
  20098. + xorl %ebx, %esi
  20099. + movl 0x500+des_SPtrans(%edx),%ebx
  20100. + xorl %ebx, %esi
  20101. +
  20102. +
  20103. + movl 32(%ebp), %eax
  20104. + xorl %ebx, %ebx
  20105. + movl 36(%ebp), %edx
  20106. + xorl %esi, %eax
  20107. + xorl %esi, %edx
  20108. + andl $0xfcfcfcfc, %eax
  20109. + andl $0xcfcfcfcf, %edx
  20110. + movb %al, %bl
  20111. + movb %ah, %cl
  20112. + rorl $4, %edx
  20113. + movl des_SPtrans(%ebx),%ebp
  20114. + movb %dl, %bl
  20115. + xorl %ebp, %edi
  20116. + movl 0x200+des_SPtrans(%ecx),%ebp
  20117. + xorl %ebp, %edi
  20118. + movb %dh, %cl
  20119. + shrl $16, %eax
  20120. + movl 0x100+des_SPtrans(%ebx),%ebp
  20121. + xorl %ebp, %edi
  20122. + movb %ah, %bl
  20123. + shrl $16, %edx
  20124. + movl 0x300+des_SPtrans(%ecx),%ebp
  20125. + xorl %ebp, %edi
  20126. + movl 24(%esp), %ebp
  20127. + movb %dh, %cl
  20128. + andl $0xff, %eax
  20129. + andl $0xff, %edx
  20130. + movl 0x600+des_SPtrans(%ebx),%ebx
  20131. + xorl %ebx, %edi
  20132. + movl 0x700+des_SPtrans(%ecx),%ebx
  20133. + xorl %ebx, %edi
  20134. + movl 0x400+des_SPtrans(%eax),%ebx
  20135. + xorl %ebx, %edi
  20136. + movl 0x500+des_SPtrans(%edx),%ebx
  20137. + xorl %ebx, %edi
  20138. +
  20139. +
  20140. + movl 40(%ebp), %eax
  20141. + xorl %ebx, %ebx
  20142. + movl 44(%ebp), %edx
  20143. + xorl %edi, %eax
  20144. + xorl %edi, %edx
  20145. + andl $0xfcfcfcfc, %eax
  20146. + andl $0xcfcfcfcf, %edx
  20147. + movb %al, %bl
  20148. + movb %ah, %cl
  20149. + rorl $4, %edx
  20150. + movl des_SPtrans(%ebx),%ebp
  20151. + movb %dl, %bl
  20152. + xorl %ebp, %esi
  20153. + movl 0x200+des_SPtrans(%ecx),%ebp
  20154. + xorl %ebp, %esi
  20155. + movb %dh, %cl
  20156. + shrl $16, %eax
  20157. + movl 0x100+des_SPtrans(%ebx),%ebp
  20158. + xorl %ebp, %esi
  20159. + movb %ah, %bl
  20160. + shrl $16, %edx
  20161. + movl 0x300+des_SPtrans(%ecx),%ebp
  20162. + xorl %ebp, %esi
  20163. + movl 24(%esp), %ebp
  20164. + movb %dh, %cl
  20165. + andl $0xff, %eax
  20166. + andl $0xff, %edx
  20167. + movl 0x600+des_SPtrans(%ebx),%ebx
  20168. + xorl %ebx, %esi
  20169. + movl 0x700+des_SPtrans(%ecx),%ebx
  20170. + xorl %ebx, %esi
  20171. + movl 0x400+des_SPtrans(%eax),%ebx
  20172. + xorl %ebx, %esi
  20173. + movl 0x500+des_SPtrans(%edx),%ebx
  20174. + xorl %ebx, %esi
  20175. +
  20176. +
  20177. + movl 48(%ebp), %eax
  20178. + xorl %ebx, %ebx
  20179. + movl 52(%ebp), %edx
  20180. + xorl %esi, %eax
  20181. + xorl %esi, %edx
  20182. + andl $0xfcfcfcfc, %eax
  20183. + andl $0xcfcfcfcf, %edx
  20184. + movb %al, %bl
  20185. + movb %ah, %cl
  20186. + rorl $4, %edx
  20187. + movl des_SPtrans(%ebx),%ebp
  20188. + movb %dl, %bl
  20189. + xorl %ebp, %edi
  20190. + movl 0x200+des_SPtrans(%ecx),%ebp
  20191. + xorl %ebp, %edi
  20192. + movb %dh, %cl
  20193. + shrl $16, %eax
  20194. + movl 0x100+des_SPtrans(%ebx),%ebp
  20195. + xorl %ebp, %edi
  20196. + movb %ah, %bl
  20197. + shrl $16, %edx
  20198. + movl 0x300+des_SPtrans(%ecx),%ebp
  20199. + xorl %ebp, %edi
  20200. + movl 24(%esp), %ebp
  20201. + movb %dh, %cl
  20202. + andl $0xff, %eax
  20203. + andl $0xff, %edx
  20204. + movl 0x600+des_SPtrans(%ebx),%ebx
  20205. + xorl %ebx, %edi
  20206. + movl 0x700+des_SPtrans(%ecx),%ebx
  20207. + xorl %ebx, %edi
  20208. + movl 0x400+des_SPtrans(%eax),%ebx
  20209. + xorl %ebx, %edi
  20210. + movl 0x500+des_SPtrans(%edx),%ebx
  20211. + xorl %ebx, %edi
  20212. +
  20213. +
  20214. + movl 56(%ebp), %eax
  20215. + xorl %ebx, %ebx
  20216. + movl 60(%ebp), %edx
  20217. + xorl %edi, %eax
  20218. + xorl %edi, %edx
  20219. + andl $0xfcfcfcfc, %eax
  20220. + andl $0xcfcfcfcf, %edx
  20221. + movb %al, %bl
  20222. + movb %ah, %cl
  20223. + rorl $4, %edx
  20224. + movl des_SPtrans(%ebx),%ebp
  20225. + movb %dl, %bl
  20226. + xorl %ebp, %esi
  20227. + movl 0x200+des_SPtrans(%ecx),%ebp
  20228. + xorl %ebp, %esi
  20229. + movb %dh, %cl
  20230. + shrl $16, %eax
  20231. + movl 0x100+des_SPtrans(%ebx),%ebp
  20232. + xorl %ebp, %esi
  20233. + movb %ah, %bl
  20234. + shrl $16, %edx
  20235. + movl 0x300+des_SPtrans(%ecx),%ebp
  20236. + xorl %ebp, %esi
  20237. + movl 24(%esp), %ebp
  20238. + movb %dh, %cl
  20239. + andl $0xff, %eax
  20240. + andl $0xff, %edx
  20241. + movl 0x600+des_SPtrans(%ebx),%ebx
  20242. + xorl %ebx, %esi
  20243. + movl 0x700+des_SPtrans(%ecx),%ebx
  20244. + xorl %ebx, %esi
  20245. + movl 0x400+des_SPtrans(%eax),%ebx
  20246. + xorl %ebx, %esi
  20247. + movl 0x500+des_SPtrans(%edx),%ebx
  20248. + xorl %ebx, %esi
  20249. +
  20250. +
  20251. + movl 64(%ebp), %eax
  20252. + xorl %ebx, %ebx
  20253. + movl 68(%ebp), %edx
  20254. + xorl %esi, %eax
  20255. + xorl %esi, %edx
  20256. + andl $0xfcfcfcfc, %eax
  20257. + andl $0xcfcfcfcf, %edx
  20258. + movb %al, %bl
  20259. + movb %ah, %cl
  20260. + rorl $4, %edx
  20261. + movl des_SPtrans(%ebx),%ebp
  20262. + movb %dl, %bl
  20263. + xorl %ebp, %edi
  20264. + movl 0x200+des_SPtrans(%ecx),%ebp
  20265. + xorl %ebp, %edi
  20266. + movb %dh, %cl
  20267. + shrl $16, %eax
  20268. + movl 0x100+des_SPtrans(%ebx),%ebp
  20269. + xorl %ebp, %edi
  20270. + movb %ah, %bl
  20271. + shrl $16, %edx
  20272. + movl 0x300+des_SPtrans(%ecx),%ebp
  20273. + xorl %ebp, %edi
  20274. + movl 24(%esp), %ebp
  20275. + movb %dh, %cl
  20276. + andl $0xff, %eax
  20277. + andl $0xff, %edx
  20278. + movl 0x600+des_SPtrans(%ebx),%ebx
  20279. + xorl %ebx, %edi
  20280. + movl 0x700+des_SPtrans(%ecx),%ebx
  20281. + xorl %ebx, %edi
  20282. + movl 0x400+des_SPtrans(%eax),%ebx
  20283. + xorl %ebx, %edi
  20284. + movl 0x500+des_SPtrans(%edx),%ebx
  20285. + xorl %ebx, %edi
  20286. +
  20287. +
  20288. + movl 72(%ebp), %eax
  20289. + xorl %ebx, %ebx
  20290. + movl 76(%ebp), %edx
  20291. + xorl %edi, %eax
  20292. + xorl %edi, %edx
  20293. + andl $0xfcfcfcfc, %eax
  20294. + andl $0xcfcfcfcf, %edx
  20295. + movb %al, %bl
  20296. + movb %ah, %cl
  20297. + rorl $4, %edx
  20298. + movl des_SPtrans(%ebx),%ebp
  20299. + movb %dl, %bl
  20300. + xorl %ebp, %esi
  20301. + movl 0x200+des_SPtrans(%ecx),%ebp
  20302. + xorl %ebp, %esi
  20303. + movb %dh, %cl
  20304. + shrl $16, %eax
  20305. + movl 0x100+des_SPtrans(%ebx),%ebp
  20306. + xorl %ebp, %esi
  20307. + movb %ah, %bl
  20308. + shrl $16, %edx
  20309. + movl 0x300+des_SPtrans(%ecx),%ebp
  20310. + xorl %ebp, %esi
  20311. + movl 24(%esp), %ebp
  20312. + movb %dh, %cl
  20313. + andl $0xff, %eax
  20314. + andl $0xff, %edx
  20315. + movl 0x600+des_SPtrans(%ebx),%ebx
  20316. + xorl %ebx, %esi
  20317. + movl 0x700+des_SPtrans(%ecx),%ebx
  20318. + xorl %ebx, %esi
  20319. + movl 0x400+des_SPtrans(%eax),%ebx
  20320. + xorl %ebx, %esi
  20321. + movl 0x500+des_SPtrans(%edx),%ebx
  20322. + xorl %ebx, %esi
  20323. +
  20324. +
  20325. + movl 80(%ebp), %eax
  20326. + xorl %ebx, %ebx
  20327. + movl 84(%ebp), %edx
  20328. + xorl %esi, %eax
  20329. + xorl %esi, %edx
  20330. + andl $0xfcfcfcfc, %eax
  20331. + andl $0xcfcfcfcf, %edx
  20332. + movb %al, %bl
  20333. + movb %ah, %cl
  20334. + rorl $4, %edx
  20335. + movl des_SPtrans(%ebx),%ebp
  20336. + movb %dl, %bl
  20337. + xorl %ebp, %edi
  20338. + movl 0x200+des_SPtrans(%ecx),%ebp
  20339. + xorl %ebp, %edi
  20340. + movb %dh, %cl
  20341. + shrl $16, %eax
  20342. + movl 0x100+des_SPtrans(%ebx),%ebp
  20343. + xorl %ebp, %edi
  20344. + movb %ah, %bl
  20345. + shrl $16, %edx
  20346. + movl 0x300+des_SPtrans(%ecx),%ebp
  20347. + xorl %ebp, %edi
  20348. + movl 24(%esp), %ebp
  20349. + movb %dh, %cl
  20350. + andl $0xff, %eax
  20351. + andl $0xff, %edx
  20352. + movl 0x600+des_SPtrans(%ebx),%ebx
  20353. + xorl %ebx, %edi
  20354. + movl 0x700+des_SPtrans(%ecx),%ebx
  20355. + xorl %ebx, %edi
  20356. + movl 0x400+des_SPtrans(%eax),%ebx
  20357. + xorl %ebx, %edi
  20358. + movl 0x500+des_SPtrans(%edx),%ebx
  20359. + xorl %ebx, %edi
  20360. +
  20361. +
  20362. + movl 88(%ebp), %eax
  20363. + xorl %ebx, %ebx
  20364. + movl 92(%ebp), %edx
  20365. + xorl %edi, %eax
  20366. + xorl %edi, %edx
  20367. + andl $0xfcfcfcfc, %eax
  20368. + andl $0xcfcfcfcf, %edx
  20369. + movb %al, %bl
  20370. + movb %ah, %cl
  20371. + rorl $4, %edx
  20372. + movl des_SPtrans(%ebx),%ebp
  20373. + movb %dl, %bl
  20374. + xorl %ebp, %esi
  20375. + movl 0x200+des_SPtrans(%ecx),%ebp
  20376. + xorl %ebp, %esi
  20377. + movb %dh, %cl
  20378. + shrl $16, %eax
  20379. + movl 0x100+des_SPtrans(%ebx),%ebp
  20380. + xorl %ebp, %esi
  20381. + movb %ah, %bl
  20382. + shrl $16, %edx
  20383. + movl 0x300+des_SPtrans(%ecx),%ebp
  20384. + xorl %ebp, %esi
  20385. + movl 24(%esp), %ebp
  20386. + movb %dh, %cl
  20387. + andl $0xff, %eax
  20388. + andl $0xff, %edx
  20389. + movl 0x600+des_SPtrans(%ebx),%ebx
  20390. + xorl %ebx, %esi
  20391. + movl 0x700+des_SPtrans(%ecx),%ebx
  20392. + xorl %ebx, %esi
  20393. + movl 0x400+des_SPtrans(%eax),%ebx
  20394. + xorl %ebx, %esi
  20395. + movl 0x500+des_SPtrans(%edx),%ebx
  20396. + xorl %ebx, %esi
  20397. +
  20398. +
  20399. + movl 96(%ebp), %eax
  20400. + xorl %ebx, %ebx
  20401. + movl 100(%ebp), %edx
  20402. + xorl %esi, %eax
  20403. + xorl %esi, %edx
  20404. + andl $0xfcfcfcfc, %eax
  20405. + andl $0xcfcfcfcf, %edx
  20406. + movb %al, %bl
  20407. + movb %ah, %cl
  20408. + rorl $4, %edx
  20409. + movl des_SPtrans(%ebx),%ebp
  20410. + movb %dl, %bl
  20411. + xorl %ebp, %edi
  20412. + movl 0x200+des_SPtrans(%ecx),%ebp
  20413. + xorl %ebp, %edi
  20414. + movb %dh, %cl
  20415. + shrl $16, %eax
  20416. + movl 0x100+des_SPtrans(%ebx),%ebp
  20417. + xorl %ebp, %edi
  20418. + movb %ah, %bl
  20419. + shrl $16, %edx
  20420. + movl 0x300+des_SPtrans(%ecx),%ebp
  20421. + xorl %ebp, %edi
  20422. + movl 24(%esp), %ebp
  20423. + movb %dh, %cl
  20424. + andl $0xff, %eax
  20425. + andl $0xff, %edx
  20426. + movl 0x600+des_SPtrans(%ebx),%ebx
  20427. + xorl %ebx, %edi
  20428. + movl 0x700+des_SPtrans(%ecx),%ebx
  20429. + xorl %ebx, %edi
  20430. + movl 0x400+des_SPtrans(%eax),%ebx
  20431. + xorl %ebx, %edi
  20432. + movl 0x500+des_SPtrans(%edx),%ebx
  20433. + xorl %ebx, %edi
  20434. +
  20435. +
  20436. + movl 104(%ebp), %eax
  20437. + xorl %ebx, %ebx
  20438. + movl 108(%ebp), %edx
  20439. + xorl %edi, %eax
  20440. + xorl %edi, %edx
  20441. + andl $0xfcfcfcfc, %eax
  20442. + andl $0xcfcfcfcf, %edx
  20443. + movb %al, %bl
  20444. + movb %ah, %cl
  20445. + rorl $4, %edx
  20446. + movl des_SPtrans(%ebx),%ebp
  20447. + movb %dl, %bl
  20448. + xorl %ebp, %esi
  20449. + movl 0x200+des_SPtrans(%ecx),%ebp
  20450. + xorl %ebp, %esi
  20451. + movb %dh, %cl
  20452. + shrl $16, %eax
  20453. + movl 0x100+des_SPtrans(%ebx),%ebp
  20454. + xorl %ebp, %esi
  20455. + movb %ah, %bl
  20456. + shrl $16, %edx
  20457. + movl 0x300+des_SPtrans(%ecx),%ebp
  20458. + xorl %ebp, %esi
  20459. + movl 24(%esp), %ebp
  20460. + movb %dh, %cl
  20461. + andl $0xff, %eax
  20462. + andl $0xff, %edx
  20463. + movl 0x600+des_SPtrans(%ebx),%ebx
  20464. + xorl %ebx, %esi
  20465. + movl 0x700+des_SPtrans(%ecx),%ebx
  20466. + xorl %ebx, %esi
  20467. + movl 0x400+des_SPtrans(%eax),%ebx
  20468. + xorl %ebx, %esi
  20469. + movl 0x500+des_SPtrans(%edx),%ebx
  20470. + xorl %ebx, %esi
  20471. +
  20472. +
  20473. + movl 112(%ebp), %eax
  20474. + xorl %ebx, %ebx
  20475. + movl 116(%ebp), %edx
  20476. + xorl %esi, %eax
  20477. + xorl %esi, %edx
  20478. + andl $0xfcfcfcfc, %eax
  20479. + andl $0xcfcfcfcf, %edx
  20480. + movb %al, %bl
  20481. + movb %ah, %cl
  20482. + rorl $4, %edx
  20483. + movl des_SPtrans(%ebx),%ebp
  20484. + movb %dl, %bl
  20485. + xorl %ebp, %edi
  20486. + movl 0x200+des_SPtrans(%ecx),%ebp
  20487. + xorl %ebp, %edi
  20488. + movb %dh, %cl
  20489. + shrl $16, %eax
  20490. + movl 0x100+des_SPtrans(%ebx),%ebp
  20491. + xorl %ebp, %edi
  20492. + movb %ah, %bl
  20493. + shrl $16, %edx
  20494. + movl 0x300+des_SPtrans(%ecx),%ebp
  20495. + xorl %ebp, %edi
  20496. + movl 24(%esp), %ebp
  20497. + movb %dh, %cl
  20498. + andl $0xff, %eax
  20499. + andl $0xff, %edx
  20500. + movl 0x600+des_SPtrans(%ebx),%ebx
  20501. + xorl %ebx, %edi
  20502. + movl 0x700+des_SPtrans(%ecx),%ebx
  20503. + xorl %ebx, %edi
  20504. + movl 0x400+des_SPtrans(%eax),%ebx
  20505. + xorl %ebx, %edi
  20506. + movl 0x500+des_SPtrans(%edx),%ebx
  20507. + xorl %ebx, %edi
  20508. +
  20509. +
  20510. + movl 120(%ebp), %eax
  20511. + xorl %ebx, %ebx
  20512. + movl 124(%ebp), %edx
  20513. + xorl %edi, %eax
  20514. + xorl %edi, %edx
  20515. + andl $0xfcfcfcfc, %eax
  20516. + andl $0xcfcfcfcf, %edx
  20517. + movb %al, %bl
  20518. + movb %ah, %cl
  20519. + rorl $4, %edx
  20520. + movl des_SPtrans(%ebx),%ebp
  20521. + movb %dl, %bl
  20522. + xorl %ebp, %esi
  20523. + movl 0x200+des_SPtrans(%ecx),%ebp
  20524. + xorl %ebp, %esi
  20525. + movb %dh, %cl
  20526. + shrl $16, %eax
  20527. + movl 0x100+des_SPtrans(%ebx),%ebp
  20528. + xorl %ebp, %esi
  20529. + movb %ah, %bl
  20530. + shrl $16, %edx
  20531. + movl 0x300+des_SPtrans(%ecx),%ebp
  20532. + xorl %ebp, %esi
  20533. + movl 24(%esp), %ebp
  20534. + movb %dh, %cl
  20535. + andl $0xff, %eax
  20536. + andl $0xff, %edx
  20537. + movl 0x600+des_SPtrans(%ebx),%ebx
  20538. + xorl %ebx, %esi
  20539. + movl 0x700+des_SPtrans(%ecx),%ebx
  20540. + xorl %ebx, %esi
  20541. + movl 0x400+des_SPtrans(%eax),%ebx
  20542. + xorl %ebx, %esi
  20543. + movl 0x500+des_SPtrans(%edx),%ebx
  20544. + xorl %ebx, %esi
  20545. + jmp .L001end
  20546. +.L000start_decrypt:
  20547. +
  20548. +
  20549. + movl 120(%ebp), %eax
  20550. + xorl %ebx, %ebx
  20551. + movl 124(%ebp), %edx
  20552. + xorl %esi, %eax
  20553. + xorl %esi, %edx
  20554. + andl $0xfcfcfcfc, %eax
  20555. + andl $0xcfcfcfcf, %edx
  20556. + movb %al, %bl
  20557. + movb %ah, %cl
  20558. + rorl $4, %edx
  20559. + movl des_SPtrans(%ebx),%ebp
  20560. + movb %dl, %bl
  20561. + xorl %ebp, %edi
  20562. + movl 0x200+des_SPtrans(%ecx),%ebp
  20563. + xorl %ebp, %edi
  20564. + movb %dh, %cl
  20565. + shrl $16, %eax
  20566. + movl 0x100+des_SPtrans(%ebx),%ebp
  20567. + xorl %ebp, %edi
  20568. + movb %ah, %bl
  20569. + shrl $16, %edx
  20570. + movl 0x300+des_SPtrans(%ecx),%ebp
  20571. + xorl %ebp, %edi
  20572. + movl 24(%esp), %ebp
  20573. + movb %dh, %cl
  20574. + andl $0xff, %eax
  20575. + andl $0xff, %edx
  20576. + movl 0x600+des_SPtrans(%ebx),%ebx
  20577. + xorl %ebx, %edi
  20578. + movl 0x700+des_SPtrans(%ecx),%ebx
  20579. + xorl %ebx, %edi
  20580. + movl 0x400+des_SPtrans(%eax),%ebx
  20581. + xorl %ebx, %edi
  20582. + movl 0x500+des_SPtrans(%edx),%ebx
  20583. + xorl %ebx, %edi
  20584. +
  20585. +
  20586. + movl 112(%ebp), %eax
  20587. + xorl %ebx, %ebx
  20588. + movl 116(%ebp), %edx
  20589. + xorl %edi, %eax
  20590. + xorl %edi, %edx
  20591. + andl $0xfcfcfcfc, %eax
  20592. + andl $0xcfcfcfcf, %edx
  20593. + movb %al, %bl
  20594. + movb %ah, %cl
  20595. + rorl $4, %edx
  20596. + movl des_SPtrans(%ebx),%ebp
  20597. + movb %dl, %bl
  20598. + xorl %ebp, %esi
  20599. + movl 0x200+des_SPtrans(%ecx),%ebp
  20600. + xorl %ebp, %esi
  20601. + movb %dh, %cl
  20602. + shrl $16, %eax
  20603. + movl 0x100+des_SPtrans(%ebx),%ebp
  20604. + xorl %ebp, %esi
  20605. + movb %ah, %bl
  20606. + shrl $16, %edx
  20607. + movl 0x300+des_SPtrans(%ecx),%ebp
  20608. + xorl %ebp, %esi
  20609. + movl 24(%esp), %ebp
  20610. + movb %dh, %cl
  20611. + andl $0xff, %eax
  20612. + andl $0xff, %edx
  20613. + movl 0x600+des_SPtrans(%ebx),%ebx
  20614. + xorl %ebx, %esi
  20615. + movl 0x700+des_SPtrans(%ecx),%ebx
  20616. + xorl %ebx, %esi
  20617. + movl 0x400+des_SPtrans(%eax),%ebx
  20618. + xorl %ebx, %esi
  20619. + movl 0x500+des_SPtrans(%edx),%ebx
  20620. + xorl %ebx, %esi
  20621. +
  20622. +
  20623. + movl 104(%ebp), %eax
  20624. + xorl %ebx, %ebx
  20625. + movl 108(%ebp), %edx
  20626. + xorl %esi, %eax
  20627. + xorl %esi, %edx
  20628. + andl $0xfcfcfcfc, %eax
  20629. + andl $0xcfcfcfcf, %edx
  20630. + movb %al, %bl
  20631. + movb %ah, %cl
  20632. + rorl $4, %edx
  20633. + movl des_SPtrans(%ebx),%ebp
  20634. + movb %dl, %bl
  20635. + xorl %ebp, %edi
  20636. + movl 0x200+des_SPtrans(%ecx),%ebp
  20637. + xorl %ebp, %edi
  20638. + movb %dh, %cl
  20639. + shrl $16, %eax
  20640. + movl 0x100+des_SPtrans(%ebx),%ebp
  20641. + xorl %ebp, %edi
  20642. + movb %ah, %bl
  20643. + shrl $16, %edx
  20644. + movl 0x300+des_SPtrans(%ecx),%ebp
  20645. + xorl %ebp, %edi
  20646. + movl 24(%esp), %ebp
  20647. + movb %dh, %cl
  20648. + andl $0xff, %eax
  20649. + andl $0xff, %edx
  20650. + movl 0x600+des_SPtrans(%ebx),%ebx
  20651. + xorl %ebx, %edi
  20652. + movl 0x700+des_SPtrans(%ecx),%ebx
  20653. + xorl %ebx, %edi
  20654. + movl 0x400+des_SPtrans(%eax),%ebx
  20655. + xorl %ebx, %edi
  20656. + movl 0x500+des_SPtrans(%edx),%ebx
  20657. + xorl %ebx, %edi
  20658. +
  20659. +
  20660. + movl 96(%ebp), %eax
  20661. + xorl %ebx, %ebx
  20662. + movl 100(%ebp), %edx
  20663. + xorl %edi, %eax
  20664. + xorl %edi, %edx
  20665. + andl $0xfcfcfcfc, %eax
  20666. + andl $0xcfcfcfcf, %edx
  20667. + movb %al, %bl
  20668. + movb %ah, %cl
  20669. + rorl $4, %edx
  20670. + movl des_SPtrans(%ebx),%ebp
  20671. + movb %dl, %bl
  20672. + xorl %ebp, %esi
  20673. + movl 0x200+des_SPtrans(%ecx),%ebp
  20674. + xorl %ebp, %esi
  20675. + movb %dh, %cl
  20676. + shrl $16, %eax
  20677. + movl 0x100+des_SPtrans(%ebx),%ebp
  20678. + xorl %ebp, %esi
  20679. + movb %ah, %bl
  20680. + shrl $16, %edx
  20681. + movl 0x300+des_SPtrans(%ecx),%ebp
  20682. + xorl %ebp, %esi
  20683. + movl 24(%esp), %ebp
  20684. + movb %dh, %cl
  20685. + andl $0xff, %eax
  20686. + andl $0xff, %edx
  20687. + movl 0x600+des_SPtrans(%ebx),%ebx
  20688. + xorl %ebx, %esi
  20689. + movl 0x700+des_SPtrans(%ecx),%ebx
  20690. + xorl %ebx, %esi
  20691. + movl 0x400+des_SPtrans(%eax),%ebx
  20692. + xorl %ebx, %esi
  20693. + movl 0x500+des_SPtrans(%edx),%ebx
  20694. + xorl %ebx, %esi
  20695. +
  20696. +
  20697. + movl 88(%ebp), %eax
  20698. + xorl %ebx, %ebx
  20699. + movl 92(%ebp), %edx
  20700. + xorl %esi, %eax
  20701. + xorl %esi, %edx
  20702. + andl $0xfcfcfcfc, %eax
  20703. + andl $0xcfcfcfcf, %edx
  20704. + movb %al, %bl
  20705. + movb %ah, %cl
  20706. + rorl $4, %edx
  20707. + movl des_SPtrans(%ebx),%ebp
  20708. + movb %dl, %bl
  20709. + xorl %ebp, %edi
  20710. + movl 0x200+des_SPtrans(%ecx),%ebp
  20711. + xorl %ebp, %edi
  20712. + movb %dh, %cl
  20713. + shrl $16, %eax
  20714. + movl 0x100+des_SPtrans(%ebx),%ebp
  20715. + xorl %ebp, %edi
  20716. + movb %ah, %bl
  20717. + shrl $16, %edx
  20718. + movl 0x300+des_SPtrans(%ecx),%ebp
  20719. + xorl %ebp, %edi
  20720. + movl 24(%esp), %ebp
  20721. + movb %dh, %cl
  20722. + andl $0xff, %eax
  20723. + andl $0xff, %edx
  20724. + movl 0x600+des_SPtrans(%ebx),%ebx
  20725. + xorl %ebx, %edi
  20726. + movl 0x700+des_SPtrans(%ecx),%ebx
  20727. + xorl %ebx, %edi
  20728. + movl 0x400+des_SPtrans(%eax),%ebx
  20729. + xorl %ebx, %edi
  20730. + movl 0x500+des_SPtrans(%edx),%ebx
  20731. + xorl %ebx, %edi
  20732. +
  20733. +
  20734. + movl 80(%ebp), %eax
  20735. + xorl %ebx, %ebx
  20736. + movl 84(%ebp), %edx
  20737. + xorl %edi, %eax
  20738. + xorl %edi, %edx
  20739. + andl $0xfcfcfcfc, %eax
  20740. + andl $0xcfcfcfcf, %edx
  20741. + movb %al, %bl
  20742. + movb %ah, %cl
  20743. + rorl $4, %edx
  20744. + movl des_SPtrans(%ebx),%ebp
  20745. + movb %dl, %bl
  20746. + xorl %ebp, %esi
  20747. + movl 0x200+des_SPtrans(%ecx),%ebp
  20748. + xorl %ebp, %esi
  20749. + movb %dh, %cl
  20750. + shrl $16, %eax
  20751. + movl 0x100+des_SPtrans(%ebx),%ebp
  20752. + xorl %ebp, %esi
  20753. + movb %ah, %bl
  20754. + shrl $16, %edx
  20755. + movl 0x300+des_SPtrans(%ecx),%ebp
  20756. + xorl %ebp, %esi
  20757. + movl 24(%esp), %ebp
  20758. + movb %dh, %cl
  20759. + andl $0xff, %eax
  20760. + andl $0xff, %edx
  20761. + movl 0x600+des_SPtrans(%ebx),%ebx
  20762. + xorl %ebx, %esi
  20763. + movl 0x700+des_SPtrans(%ecx),%ebx
  20764. + xorl %ebx, %esi
  20765. + movl 0x400+des_SPtrans(%eax),%ebx
  20766. + xorl %ebx, %esi
  20767. + movl 0x500+des_SPtrans(%edx),%ebx
  20768. + xorl %ebx, %esi
  20769. +
  20770. +
  20771. + movl 72(%ebp), %eax
  20772. + xorl %ebx, %ebx
  20773. + movl 76(%ebp), %edx
  20774. + xorl %esi, %eax
  20775. + xorl %esi, %edx
  20776. + andl $0xfcfcfcfc, %eax
  20777. + andl $0xcfcfcfcf, %edx
  20778. + movb %al, %bl
  20779. + movb %ah, %cl
  20780. + rorl $4, %edx
  20781. + movl des_SPtrans(%ebx),%ebp
  20782. + movb %dl, %bl
  20783. + xorl %ebp, %edi
  20784. + movl 0x200+des_SPtrans(%ecx),%ebp
  20785. + xorl %ebp, %edi
  20786. + movb %dh, %cl
  20787. + shrl $16, %eax
  20788. + movl 0x100+des_SPtrans(%ebx),%ebp
  20789. + xorl %ebp, %edi
  20790. + movb %ah, %bl
  20791. + shrl $16, %edx
  20792. + movl 0x300+des_SPtrans(%ecx),%ebp
  20793. + xorl %ebp, %edi
  20794. + movl 24(%esp), %ebp
  20795. + movb %dh, %cl
  20796. + andl $0xff, %eax
  20797. + andl $0xff, %edx
  20798. + movl 0x600+des_SPtrans(%ebx),%ebx
  20799. + xorl %ebx, %edi
  20800. + movl 0x700+des_SPtrans(%ecx),%ebx
  20801. + xorl %ebx, %edi
  20802. + movl 0x400+des_SPtrans(%eax),%ebx
  20803. + xorl %ebx, %edi
  20804. + movl 0x500+des_SPtrans(%edx),%ebx
  20805. + xorl %ebx, %edi
  20806. +
  20807. +
  20808. + movl 64(%ebp), %eax
  20809. + xorl %ebx, %ebx
  20810. + movl 68(%ebp), %edx
  20811. + xorl %edi, %eax
  20812. + xorl %edi, %edx
  20813. + andl $0xfcfcfcfc, %eax
  20814. + andl $0xcfcfcfcf, %edx
  20815. + movb %al, %bl
  20816. + movb %ah, %cl
  20817. + rorl $4, %edx
  20818. + movl des_SPtrans(%ebx),%ebp
  20819. + movb %dl, %bl
  20820. + xorl %ebp, %esi
  20821. + movl 0x200+des_SPtrans(%ecx),%ebp
  20822. + xorl %ebp, %esi
  20823. + movb %dh, %cl
  20824. + shrl $16, %eax
  20825. + movl 0x100+des_SPtrans(%ebx),%ebp
  20826. + xorl %ebp, %esi
  20827. + movb %ah, %bl
  20828. + shrl $16, %edx
  20829. + movl 0x300+des_SPtrans(%ecx),%ebp
  20830. + xorl %ebp, %esi
  20831. + movl 24(%esp), %ebp
  20832. + movb %dh, %cl
  20833. + andl $0xff, %eax
  20834. + andl $0xff, %edx
  20835. + movl 0x600+des_SPtrans(%ebx),%ebx
  20836. + xorl %ebx, %esi
  20837. + movl 0x700+des_SPtrans(%ecx),%ebx
  20838. + xorl %ebx, %esi
  20839. + movl 0x400+des_SPtrans(%eax),%ebx
  20840. + xorl %ebx, %esi
  20841. + movl 0x500+des_SPtrans(%edx),%ebx
  20842. + xorl %ebx, %esi
  20843. +
  20844. +
  20845. + movl 56(%ebp), %eax
  20846. + xorl %ebx, %ebx
  20847. + movl 60(%ebp), %edx
  20848. + xorl %esi, %eax
  20849. + xorl %esi, %edx
  20850. + andl $0xfcfcfcfc, %eax
  20851. + andl $0xcfcfcfcf, %edx
  20852. + movb %al, %bl
  20853. + movb %ah, %cl
  20854. + rorl $4, %edx
  20855. + movl des_SPtrans(%ebx),%ebp
  20856. + movb %dl, %bl
  20857. + xorl %ebp, %edi
  20858. + movl 0x200+des_SPtrans(%ecx),%ebp
  20859. + xorl %ebp, %edi
  20860. + movb %dh, %cl
  20861. + shrl $16, %eax
  20862. + movl 0x100+des_SPtrans(%ebx),%ebp
  20863. + xorl %ebp, %edi
  20864. + movb %ah, %bl
  20865. + shrl $16, %edx
  20866. + movl 0x300+des_SPtrans(%ecx),%ebp
  20867. + xorl %ebp, %edi
  20868. + movl 24(%esp), %ebp
  20869. + movb %dh, %cl
  20870. + andl $0xff, %eax
  20871. + andl $0xff, %edx
  20872. + movl 0x600+des_SPtrans(%ebx),%ebx
  20873. + xorl %ebx, %edi
  20874. + movl 0x700+des_SPtrans(%ecx),%ebx
  20875. + xorl %ebx, %edi
  20876. + movl 0x400+des_SPtrans(%eax),%ebx
  20877. + xorl %ebx, %edi
  20878. + movl 0x500+des_SPtrans(%edx),%ebx
  20879. + xorl %ebx, %edi
  20880. +
  20881. +
  20882. + movl 48(%ebp), %eax
  20883. + xorl %ebx, %ebx
  20884. + movl 52(%ebp), %edx
  20885. + xorl %edi, %eax
  20886. + xorl %edi, %edx
  20887. + andl $0xfcfcfcfc, %eax
  20888. + andl $0xcfcfcfcf, %edx
  20889. + movb %al, %bl
  20890. + movb %ah, %cl
  20891. + rorl $4, %edx
  20892. + movl des_SPtrans(%ebx),%ebp
  20893. + movb %dl, %bl
  20894. + xorl %ebp, %esi
  20895. + movl 0x200+des_SPtrans(%ecx),%ebp
  20896. + xorl %ebp, %esi
  20897. + movb %dh, %cl
  20898. + shrl $16, %eax
  20899. + movl 0x100+des_SPtrans(%ebx),%ebp
  20900. + xorl %ebp, %esi
  20901. + movb %ah, %bl
  20902. + shrl $16, %edx
  20903. + movl 0x300+des_SPtrans(%ecx),%ebp
  20904. + xorl %ebp, %esi
  20905. + movl 24(%esp), %ebp
  20906. + movb %dh, %cl
  20907. + andl $0xff, %eax
  20908. + andl $0xff, %edx
  20909. + movl 0x600+des_SPtrans(%ebx),%ebx
  20910. + xorl %ebx, %esi
  20911. + movl 0x700+des_SPtrans(%ecx),%ebx
  20912. + xorl %ebx, %esi
  20913. + movl 0x400+des_SPtrans(%eax),%ebx
  20914. + xorl %ebx, %esi
  20915. + movl 0x500+des_SPtrans(%edx),%ebx
  20916. + xorl %ebx, %esi
  20917. +
  20918. +
  20919. + movl 40(%ebp), %eax
  20920. + xorl %ebx, %ebx
  20921. + movl 44(%ebp), %edx
  20922. + xorl %esi, %eax
  20923. + xorl %esi, %edx
  20924. + andl $0xfcfcfcfc, %eax
  20925. + andl $0xcfcfcfcf, %edx
  20926. + movb %al, %bl
  20927. + movb %ah, %cl
  20928. + rorl $4, %edx
  20929. + movl des_SPtrans(%ebx),%ebp
  20930. + movb %dl, %bl
  20931. + xorl %ebp, %edi
  20932. + movl 0x200+des_SPtrans(%ecx),%ebp
  20933. + xorl %ebp, %edi
  20934. + movb %dh, %cl
  20935. + shrl $16, %eax
  20936. + movl 0x100+des_SPtrans(%ebx),%ebp
  20937. + xorl %ebp, %edi
  20938. + movb %ah, %bl
  20939. + shrl $16, %edx
  20940. + movl 0x300+des_SPtrans(%ecx),%ebp
  20941. + xorl %ebp, %edi
  20942. + movl 24(%esp), %ebp
  20943. + movb %dh, %cl
  20944. + andl $0xff, %eax
  20945. + andl $0xff, %edx
  20946. + movl 0x600+des_SPtrans(%ebx),%ebx
  20947. + xorl %ebx, %edi
  20948. + movl 0x700+des_SPtrans(%ecx),%ebx
  20949. + xorl %ebx, %edi
  20950. + movl 0x400+des_SPtrans(%eax),%ebx
  20951. + xorl %ebx, %edi
  20952. + movl 0x500+des_SPtrans(%edx),%ebx
  20953. + xorl %ebx, %edi
  20954. +
  20955. +
  20956. + movl 32(%ebp), %eax
  20957. + xorl %ebx, %ebx
  20958. + movl 36(%ebp), %edx
  20959. + xorl %edi, %eax
  20960. + xorl %edi, %edx
  20961. + andl $0xfcfcfcfc, %eax
  20962. + andl $0xcfcfcfcf, %edx
  20963. + movb %al, %bl
  20964. + movb %ah, %cl
  20965. + rorl $4, %edx
  20966. + movl des_SPtrans(%ebx),%ebp
  20967. + movb %dl, %bl
  20968. + xorl %ebp, %esi
  20969. + movl 0x200+des_SPtrans(%ecx),%ebp
  20970. + xorl %ebp, %esi
  20971. + movb %dh, %cl
  20972. + shrl $16, %eax
  20973. + movl 0x100+des_SPtrans(%ebx),%ebp
  20974. + xorl %ebp, %esi
  20975. + movb %ah, %bl
  20976. + shrl $16, %edx
  20977. + movl 0x300+des_SPtrans(%ecx),%ebp
  20978. + xorl %ebp, %esi
  20979. + movl 24(%esp), %ebp
  20980. + movb %dh, %cl
  20981. + andl $0xff, %eax
  20982. + andl $0xff, %edx
  20983. + movl 0x600+des_SPtrans(%ebx),%ebx
  20984. + xorl %ebx, %esi
  20985. + movl 0x700+des_SPtrans(%ecx),%ebx
  20986. + xorl %ebx, %esi
  20987. + movl 0x400+des_SPtrans(%eax),%ebx
  20988. + xorl %ebx, %esi
  20989. + movl 0x500+des_SPtrans(%edx),%ebx
  20990. + xorl %ebx, %esi
  20991. +
  20992. +
  20993. + movl 24(%ebp), %eax
  20994. + xorl %ebx, %ebx
  20995. + movl 28(%ebp), %edx
  20996. + xorl %esi, %eax
  20997. + xorl %esi, %edx
  20998. + andl $0xfcfcfcfc, %eax
  20999. + andl $0xcfcfcfcf, %edx
  21000. + movb %al, %bl
  21001. + movb %ah, %cl
  21002. + rorl $4, %edx
  21003. + movl des_SPtrans(%ebx),%ebp
  21004. + movb %dl, %bl
  21005. + xorl %ebp, %edi
  21006. + movl 0x200+des_SPtrans(%ecx),%ebp
  21007. + xorl %ebp, %edi
  21008. + movb %dh, %cl
  21009. + shrl $16, %eax
  21010. + movl 0x100+des_SPtrans(%ebx),%ebp
  21011. + xorl %ebp, %edi
  21012. + movb %ah, %bl
  21013. + shrl $16, %edx
  21014. + movl 0x300+des_SPtrans(%ecx),%ebp
  21015. + xorl %ebp, %edi
  21016. + movl 24(%esp), %ebp
  21017. + movb %dh, %cl
  21018. + andl $0xff, %eax
  21019. + andl $0xff, %edx
  21020. + movl 0x600+des_SPtrans(%ebx),%ebx
  21021. + xorl %ebx, %edi
  21022. + movl 0x700+des_SPtrans(%ecx),%ebx
  21023. + xorl %ebx, %edi
  21024. + movl 0x400+des_SPtrans(%eax),%ebx
  21025. + xorl %ebx, %edi
  21026. + movl 0x500+des_SPtrans(%edx),%ebx
  21027. + xorl %ebx, %edi
  21028. +
  21029. +
  21030. + movl 16(%ebp), %eax
  21031. + xorl %ebx, %ebx
  21032. + movl 20(%ebp), %edx
  21033. + xorl %edi, %eax
  21034. + xorl %edi, %edx
  21035. + andl $0xfcfcfcfc, %eax
  21036. + andl $0xcfcfcfcf, %edx
  21037. + movb %al, %bl
  21038. + movb %ah, %cl
  21039. + rorl $4, %edx
  21040. + movl des_SPtrans(%ebx),%ebp
  21041. + movb %dl, %bl
  21042. + xorl %ebp, %esi
  21043. + movl 0x200+des_SPtrans(%ecx),%ebp
  21044. + xorl %ebp, %esi
  21045. + movb %dh, %cl
  21046. + shrl $16, %eax
  21047. + movl 0x100+des_SPtrans(%ebx),%ebp
  21048. + xorl %ebp, %esi
  21049. + movb %ah, %bl
  21050. + shrl $16, %edx
  21051. + movl 0x300+des_SPtrans(%ecx),%ebp
  21052. + xorl %ebp, %esi
  21053. + movl 24(%esp), %ebp
  21054. + movb %dh, %cl
  21055. + andl $0xff, %eax
  21056. + andl $0xff, %edx
  21057. + movl 0x600+des_SPtrans(%ebx),%ebx
  21058. + xorl %ebx, %esi
  21059. + movl 0x700+des_SPtrans(%ecx),%ebx
  21060. + xorl %ebx, %esi
  21061. + movl 0x400+des_SPtrans(%eax),%ebx
  21062. + xorl %ebx, %esi
  21063. + movl 0x500+des_SPtrans(%edx),%ebx
  21064. + xorl %ebx, %esi
  21065. +
  21066. +
  21067. + movl 8(%ebp), %eax
  21068. + xorl %ebx, %ebx
  21069. + movl 12(%ebp), %edx
  21070. + xorl %esi, %eax
  21071. + xorl %esi, %edx
  21072. + andl $0xfcfcfcfc, %eax
  21073. + andl $0xcfcfcfcf, %edx
  21074. + movb %al, %bl
  21075. + movb %ah, %cl
  21076. + rorl $4, %edx
  21077. + movl des_SPtrans(%ebx),%ebp
  21078. + movb %dl, %bl
  21079. + xorl %ebp, %edi
  21080. + movl 0x200+des_SPtrans(%ecx),%ebp
  21081. + xorl %ebp, %edi
  21082. + movb %dh, %cl
  21083. + shrl $16, %eax
  21084. + movl 0x100+des_SPtrans(%ebx),%ebp
  21085. + xorl %ebp, %edi
  21086. + movb %ah, %bl
  21087. + shrl $16, %edx
  21088. + movl 0x300+des_SPtrans(%ecx),%ebp
  21089. + xorl %ebp, %edi
  21090. + movl 24(%esp), %ebp
  21091. + movb %dh, %cl
  21092. + andl $0xff, %eax
  21093. + andl $0xff, %edx
  21094. + movl 0x600+des_SPtrans(%ebx),%ebx
  21095. + xorl %ebx, %edi
  21096. + movl 0x700+des_SPtrans(%ecx),%ebx
  21097. + xorl %ebx, %edi
  21098. + movl 0x400+des_SPtrans(%eax),%ebx
  21099. + xorl %ebx, %edi
  21100. + movl 0x500+des_SPtrans(%edx),%ebx
  21101. + xorl %ebx, %edi
  21102. +
  21103. +
  21104. + movl (%ebp), %eax
  21105. + xorl %ebx, %ebx
  21106. + movl 4(%ebp), %edx
  21107. + xorl %edi, %eax
  21108. + xorl %edi, %edx
  21109. + andl $0xfcfcfcfc, %eax
  21110. + andl $0xcfcfcfcf, %edx
  21111. + movb %al, %bl
  21112. + movb %ah, %cl
  21113. + rorl $4, %edx
  21114. + movl des_SPtrans(%ebx),%ebp
  21115. + movb %dl, %bl
  21116. + xorl %ebp, %esi
  21117. + movl 0x200+des_SPtrans(%ecx),%ebp
  21118. + xorl %ebp, %esi
  21119. + movb %dh, %cl
  21120. + shrl $16, %eax
  21121. + movl 0x100+des_SPtrans(%ebx),%ebp
  21122. + xorl %ebp, %esi
  21123. + movb %ah, %bl
  21124. + shrl $16, %edx
  21125. + movl 0x300+des_SPtrans(%ecx),%ebp
  21126. + xorl %ebp, %esi
  21127. + movl 24(%esp), %ebp
  21128. + movb %dh, %cl
  21129. + andl $0xff, %eax
  21130. + andl $0xff, %edx
  21131. + movl 0x600+des_SPtrans(%ebx),%ebx
  21132. + xorl %ebx, %esi
  21133. + movl 0x700+des_SPtrans(%ecx),%ebx
  21134. + xorl %ebx, %esi
  21135. + movl 0x400+des_SPtrans(%eax),%ebx
  21136. + xorl %ebx, %esi
  21137. + movl 0x500+des_SPtrans(%edx),%ebx
  21138. + xorl %ebx, %esi
  21139. +.L001end:
  21140. +
  21141. +
  21142. + movl 20(%esp), %edx
  21143. +.byte 209
  21144. +.byte 206
  21145. + movl %edi, %eax
  21146. + xorl %esi, %edi
  21147. + andl $0xaaaaaaaa, %edi
  21148. + xorl %edi, %eax
  21149. + xorl %edi, %esi
  21150. +
  21151. + roll $23, %eax
  21152. + movl %eax, %edi
  21153. + xorl %esi, %eax
  21154. + andl $0x03fc03fc, %eax
  21155. + xorl %eax, %edi
  21156. + xorl %eax, %esi
  21157. +
  21158. + roll $10, %edi
  21159. + movl %edi, %eax
  21160. + xorl %esi, %edi
  21161. + andl $0x33333333, %edi
  21162. + xorl %edi, %eax
  21163. + xorl %edi, %esi
  21164. +
  21165. + roll $18, %esi
  21166. + movl %esi, %edi
  21167. + xorl %eax, %esi
  21168. + andl $0xfff0000f, %esi
  21169. + xorl %esi, %edi
  21170. + xorl %esi, %eax
  21171. +
  21172. + roll $12, %edi
  21173. + movl %edi, %esi
  21174. + xorl %eax, %edi
  21175. + andl $0xf0f0f0f0, %edi
  21176. + xorl %edi, %esi
  21177. + xorl %edi, %eax
  21178. +
  21179. + rorl $4, %eax
  21180. + movl %eax, (%edx)
  21181. + movl %esi, 4(%edx)
  21182. + popl %ebp
  21183. + popl %ebx
  21184. + popl %edi
  21185. + popl %esi
  21186. + ret
  21187. +.des_encrypt_end:
  21188. + .size des_encrypt , .des_encrypt_end-des_encrypt
  21189. +.ident "desasm.pl"
  21190. +.text
  21191. + .align 16
  21192. +.globl des_encrypt2
  21193. + .type des_encrypt2 , @function
  21194. +des_encrypt2:
  21195. + pushl %esi
  21196. + pushl %edi
  21197. +
  21198. +
  21199. + movl 12(%esp), %eax
  21200. + xorl %ecx, %ecx
  21201. + pushl %ebx
  21202. + pushl %ebp
  21203. + movl (%eax), %esi
  21204. + movl 28(%esp), %ebx
  21205. + roll $3, %esi
  21206. + movl 4(%eax), %edi
  21207. + roll $3, %edi
  21208. + movl 24(%esp), %ebp
  21209. + cmpl $0, %ebx
  21210. + je .L002start_decrypt
  21211. +
  21212. +
  21213. + movl (%ebp), %eax
  21214. + xorl %ebx, %ebx
  21215. + movl 4(%ebp), %edx
  21216. + xorl %esi, %eax
  21217. + xorl %esi, %edx
  21218. + andl $0xfcfcfcfc, %eax
  21219. + andl $0xcfcfcfcf, %edx
  21220. + movb %al, %bl
  21221. + movb %ah, %cl
  21222. + rorl $4, %edx
  21223. + movl des_SPtrans(%ebx),%ebp
  21224. + movb %dl, %bl
  21225. + xorl %ebp, %edi
  21226. + movl 0x200+des_SPtrans(%ecx),%ebp
  21227. + xorl %ebp, %edi
  21228. + movb %dh, %cl
  21229. + shrl $16, %eax
  21230. + movl 0x100+des_SPtrans(%ebx),%ebp
  21231. + xorl %ebp, %edi
  21232. + movb %ah, %bl
  21233. + shrl $16, %edx
  21234. + movl 0x300+des_SPtrans(%ecx),%ebp
  21235. + xorl %ebp, %edi
  21236. + movl 24(%esp), %ebp
  21237. + movb %dh, %cl
  21238. + andl $0xff, %eax
  21239. + andl $0xff, %edx
  21240. + movl 0x600+des_SPtrans(%ebx),%ebx
  21241. + xorl %ebx, %edi
  21242. + movl 0x700+des_SPtrans(%ecx),%ebx
  21243. + xorl %ebx, %edi
  21244. + movl 0x400+des_SPtrans(%eax),%ebx
  21245. + xorl %ebx, %edi
  21246. + movl 0x500+des_SPtrans(%edx),%ebx
  21247. + xorl %ebx, %edi
  21248. +
  21249. +
  21250. + movl 8(%ebp), %eax
  21251. + xorl %ebx, %ebx
  21252. + movl 12(%ebp), %edx
  21253. + xorl %edi, %eax
  21254. + xorl %edi, %edx
  21255. + andl $0xfcfcfcfc, %eax
  21256. + andl $0xcfcfcfcf, %edx
  21257. + movb %al, %bl
  21258. + movb %ah, %cl
  21259. + rorl $4, %edx
  21260. + movl des_SPtrans(%ebx),%ebp
  21261. + movb %dl, %bl
  21262. + xorl %ebp, %esi
  21263. + movl 0x200+des_SPtrans(%ecx),%ebp
  21264. + xorl %ebp, %esi
  21265. + movb %dh, %cl
  21266. + shrl $16, %eax
  21267. + movl 0x100+des_SPtrans(%ebx),%ebp
  21268. + xorl %ebp, %esi
  21269. + movb %ah, %bl
  21270. + shrl $16, %edx
  21271. + movl 0x300+des_SPtrans(%ecx),%ebp
  21272. + xorl %ebp, %esi
  21273. + movl 24(%esp), %ebp
  21274. + movb %dh, %cl
  21275. + andl $0xff, %eax
  21276. + andl $0xff, %edx
  21277. + movl 0x600+des_SPtrans(%ebx),%ebx
  21278. + xorl %ebx, %esi
  21279. + movl 0x700+des_SPtrans(%ecx),%ebx
  21280. + xorl %ebx, %esi
  21281. + movl 0x400+des_SPtrans(%eax),%ebx
  21282. + xorl %ebx, %esi
  21283. + movl 0x500+des_SPtrans(%edx),%ebx
  21284. + xorl %ebx, %esi
  21285. +
  21286. +
  21287. + movl 16(%ebp), %eax
  21288. + xorl %ebx, %ebx
  21289. + movl 20(%ebp), %edx
  21290. + xorl %esi, %eax
  21291. + xorl %esi, %edx
  21292. + andl $0xfcfcfcfc, %eax
  21293. + andl $0xcfcfcfcf, %edx
  21294. + movb %al, %bl
  21295. + movb %ah, %cl
  21296. + rorl $4, %edx
  21297. + movl des_SPtrans(%ebx),%ebp
  21298. + movb %dl, %bl
  21299. + xorl %ebp, %edi
  21300. + movl 0x200+des_SPtrans(%ecx),%ebp
  21301. + xorl %ebp, %edi
  21302. + movb %dh, %cl
  21303. + shrl $16, %eax
  21304. + movl 0x100+des_SPtrans(%ebx),%ebp
  21305. + xorl %ebp, %edi
  21306. + movb %ah, %bl
  21307. + shrl $16, %edx
  21308. + movl 0x300+des_SPtrans(%ecx),%ebp
  21309. + xorl %ebp, %edi
  21310. + movl 24(%esp), %ebp
  21311. + movb %dh, %cl
  21312. + andl $0xff, %eax
  21313. + andl $0xff, %edx
  21314. + movl 0x600+des_SPtrans(%ebx),%ebx
  21315. + xorl %ebx, %edi
  21316. + movl 0x700+des_SPtrans(%ecx),%ebx
  21317. + xorl %ebx, %edi
  21318. + movl 0x400+des_SPtrans(%eax),%ebx
  21319. + xorl %ebx, %edi
  21320. + movl 0x500+des_SPtrans(%edx),%ebx
  21321. + xorl %ebx, %edi
  21322. +
  21323. +
  21324. + movl 24(%ebp), %eax
  21325. + xorl %ebx, %ebx
  21326. + movl 28(%ebp), %edx
  21327. + xorl %edi, %eax
  21328. + xorl %edi, %edx
  21329. + andl $0xfcfcfcfc, %eax
  21330. + andl $0xcfcfcfcf, %edx
  21331. + movb %al, %bl
  21332. + movb %ah, %cl
  21333. + rorl $4, %edx
  21334. + movl des_SPtrans(%ebx),%ebp
  21335. + movb %dl, %bl
  21336. + xorl %ebp, %esi
  21337. + movl 0x200+des_SPtrans(%ecx),%ebp
  21338. + xorl %ebp, %esi
  21339. + movb %dh, %cl
  21340. + shrl $16, %eax
  21341. + movl 0x100+des_SPtrans(%ebx),%ebp
  21342. + xorl %ebp, %esi
  21343. + movb %ah, %bl
  21344. + shrl $16, %edx
  21345. + movl 0x300+des_SPtrans(%ecx),%ebp
  21346. + xorl %ebp, %esi
  21347. + movl 24(%esp), %ebp
  21348. + movb %dh, %cl
  21349. + andl $0xff, %eax
  21350. + andl $0xff, %edx
  21351. + movl 0x600+des_SPtrans(%ebx),%ebx
  21352. + xorl %ebx, %esi
  21353. + movl 0x700+des_SPtrans(%ecx),%ebx
  21354. + xorl %ebx, %esi
  21355. + movl 0x400+des_SPtrans(%eax),%ebx
  21356. + xorl %ebx, %esi
  21357. + movl 0x500+des_SPtrans(%edx),%ebx
  21358. + xorl %ebx, %esi
  21359. +
  21360. +
  21361. + movl 32(%ebp), %eax
  21362. + xorl %ebx, %ebx
  21363. + movl 36(%ebp), %edx
  21364. + xorl %esi, %eax
  21365. + xorl %esi, %edx
  21366. + andl $0xfcfcfcfc, %eax
  21367. + andl $0xcfcfcfcf, %edx
  21368. + movb %al, %bl
  21369. + movb %ah, %cl
  21370. + rorl $4, %edx
  21371. + movl des_SPtrans(%ebx),%ebp
  21372. + movb %dl, %bl
  21373. + xorl %ebp, %edi
  21374. + movl 0x200+des_SPtrans(%ecx),%ebp
  21375. + xorl %ebp, %edi
  21376. + movb %dh, %cl
  21377. + shrl $16, %eax
  21378. + movl 0x100+des_SPtrans(%ebx),%ebp
  21379. + xorl %ebp, %edi
  21380. + movb %ah, %bl
  21381. + shrl $16, %edx
  21382. + movl 0x300+des_SPtrans(%ecx),%ebp
  21383. + xorl %ebp, %edi
  21384. + movl 24(%esp), %ebp
  21385. + movb %dh, %cl
  21386. + andl $0xff, %eax
  21387. + andl $0xff, %edx
  21388. + movl 0x600+des_SPtrans(%ebx),%ebx
  21389. + xorl %ebx, %edi
  21390. + movl 0x700+des_SPtrans(%ecx),%ebx
  21391. + xorl %ebx, %edi
  21392. + movl 0x400+des_SPtrans(%eax),%ebx
  21393. + xorl %ebx, %edi
  21394. + movl 0x500+des_SPtrans(%edx),%ebx
  21395. + xorl %ebx, %edi
  21396. +
  21397. +
  21398. + movl 40(%ebp), %eax
  21399. + xorl %ebx, %ebx
  21400. + movl 44(%ebp), %edx
  21401. + xorl %edi, %eax
  21402. + xorl %edi, %edx
  21403. + andl $0xfcfcfcfc, %eax
  21404. + andl $0xcfcfcfcf, %edx
  21405. + movb %al, %bl
  21406. + movb %ah, %cl
  21407. + rorl $4, %edx
  21408. + movl des_SPtrans(%ebx),%ebp
  21409. + movb %dl, %bl
  21410. + xorl %ebp, %esi
  21411. + movl 0x200+des_SPtrans(%ecx),%ebp
  21412. + xorl %ebp, %esi
  21413. + movb %dh, %cl
  21414. + shrl $16, %eax
  21415. + movl 0x100+des_SPtrans(%ebx),%ebp
  21416. + xorl %ebp, %esi
  21417. + movb %ah, %bl
  21418. + shrl $16, %edx
  21419. + movl 0x300+des_SPtrans(%ecx),%ebp
  21420. + xorl %ebp, %esi
  21421. + movl 24(%esp), %ebp
  21422. + movb %dh, %cl
  21423. + andl $0xff, %eax
  21424. + andl $0xff, %edx
  21425. + movl 0x600+des_SPtrans(%ebx),%ebx
  21426. + xorl %ebx, %esi
  21427. + movl 0x700+des_SPtrans(%ecx),%ebx
  21428. + xorl %ebx, %esi
  21429. + movl 0x400+des_SPtrans(%eax),%ebx
  21430. + xorl %ebx, %esi
  21431. + movl 0x500+des_SPtrans(%edx),%ebx
  21432. + xorl %ebx, %esi
  21433. +
  21434. +
  21435. + movl 48(%ebp), %eax
  21436. + xorl %ebx, %ebx
  21437. + movl 52(%ebp), %edx
  21438. + xorl %esi, %eax
  21439. + xorl %esi, %edx
  21440. + andl $0xfcfcfcfc, %eax
  21441. + andl $0xcfcfcfcf, %edx
  21442. + movb %al, %bl
  21443. + movb %ah, %cl
  21444. + rorl $4, %edx
  21445. + movl des_SPtrans(%ebx),%ebp
  21446. + movb %dl, %bl
  21447. + xorl %ebp, %edi
  21448. + movl 0x200+des_SPtrans(%ecx),%ebp
  21449. + xorl %ebp, %edi
  21450. + movb %dh, %cl
  21451. + shrl $16, %eax
  21452. + movl 0x100+des_SPtrans(%ebx),%ebp
  21453. + xorl %ebp, %edi
  21454. + movb %ah, %bl
  21455. + shrl $16, %edx
  21456. + movl 0x300+des_SPtrans(%ecx),%ebp
  21457. + xorl %ebp, %edi
  21458. + movl 24(%esp), %ebp
  21459. + movb %dh, %cl
  21460. + andl $0xff, %eax
  21461. + andl $0xff, %edx
  21462. + movl 0x600+des_SPtrans(%ebx),%ebx
  21463. + xorl %ebx, %edi
  21464. + movl 0x700+des_SPtrans(%ecx),%ebx
  21465. + xorl %ebx, %edi
  21466. + movl 0x400+des_SPtrans(%eax),%ebx
  21467. + xorl %ebx, %edi
  21468. + movl 0x500+des_SPtrans(%edx),%ebx
  21469. + xorl %ebx, %edi
  21470. +
  21471. +
  21472. + movl 56(%ebp), %eax
  21473. + xorl %ebx, %ebx
  21474. + movl 60(%ebp), %edx
  21475. + xorl %edi, %eax
  21476. + xorl %edi, %edx
  21477. + andl $0xfcfcfcfc, %eax
  21478. + andl $0xcfcfcfcf, %edx
  21479. + movb %al, %bl
  21480. + movb %ah, %cl
  21481. + rorl $4, %edx
  21482. + movl des_SPtrans(%ebx),%ebp
  21483. + movb %dl, %bl
  21484. + xorl %ebp, %esi
  21485. + movl 0x200+des_SPtrans(%ecx),%ebp
  21486. + xorl %ebp, %esi
  21487. + movb %dh, %cl
  21488. + shrl $16, %eax
  21489. + movl 0x100+des_SPtrans(%ebx),%ebp
  21490. + xorl %ebp, %esi
  21491. + movb %ah, %bl
  21492. + shrl $16, %edx
  21493. + movl 0x300+des_SPtrans(%ecx),%ebp
  21494. + xorl %ebp, %esi
  21495. + movl 24(%esp), %ebp
  21496. + movb %dh, %cl
  21497. + andl $0xff, %eax
  21498. + andl $0xff, %edx
  21499. + movl 0x600+des_SPtrans(%ebx),%ebx
  21500. + xorl %ebx, %esi
  21501. + movl 0x700+des_SPtrans(%ecx),%ebx
  21502. + xorl %ebx, %esi
  21503. + movl 0x400+des_SPtrans(%eax),%ebx
  21504. + xorl %ebx, %esi
  21505. + movl 0x500+des_SPtrans(%edx),%ebx
  21506. + xorl %ebx, %esi
  21507. +
  21508. +
  21509. + movl 64(%ebp), %eax
  21510. + xorl %ebx, %ebx
  21511. + movl 68(%ebp), %edx
  21512. + xorl %esi, %eax
  21513. + xorl %esi, %edx
  21514. + andl $0xfcfcfcfc, %eax
  21515. + andl $0xcfcfcfcf, %edx
  21516. + movb %al, %bl
  21517. + movb %ah, %cl
  21518. + rorl $4, %edx
  21519. + movl des_SPtrans(%ebx),%ebp
  21520. + movb %dl, %bl
  21521. + xorl %ebp, %edi
  21522. + movl 0x200+des_SPtrans(%ecx),%ebp
  21523. + xorl %ebp, %edi
  21524. + movb %dh, %cl
  21525. + shrl $16, %eax
  21526. + movl 0x100+des_SPtrans(%ebx),%ebp
  21527. + xorl %ebp, %edi
  21528. + movb %ah, %bl
  21529. + shrl $16, %edx
  21530. + movl 0x300+des_SPtrans(%ecx),%ebp
  21531. + xorl %ebp, %edi
  21532. + movl 24(%esp), %ebp
  21533. + movb %dh, %cl
  21534. + andl $0xff, %eax
  21535. + andl $0xff, %edx
  21536. + movl 0x600+des_SPtrans(%ebx),%ebx
  21537. + xorl %ebx, %edi
  21538. + movl 0x700+des_SPtrans(%ecx),%ebx
  21539. + xorl %ebx, %edi
  21540. + movl 0x400+des_SPtrans(%eax),%ebx
  21541. + xorl %ebx, %edi
  21542. + movl 0x500+des_SPtrans(%edx),%ebx
  21543. + xorl %ebx, %edi
  21544. +
  21545. +
  21546. + movl 72(%ebp), %eax
  21547. + xorl %ebx, %ebx
  21548. + movl 76(%ebp), %edx
  21549. + xorl %edi, %eax
  21550. + xorl %edi, %edx
  21551. + andl $0xfcfcfcfc, %eax
  21552. + andl $0xcfcfcfcf, %edx
  21553. + movb %al, %bl
  21554. + movb %ah, %cl
  21555. + rorl $4, %edx
  21556. + movl des_SPtrans(%ebx),%ebp
  21557. + movb %dl, %bl
  21558. + xorl %ebp, %esi
  21559. + movl 0x200+des_SPtrans(%ecx),%ebp
  21560. + xorl %ebp, %esi
  21561. + movb %dh, %cl
  21562. + shrl $16, %eax
  21563. + movl 0x100+des_SPtrans(%ebx),%ebp
  21564. + xorl %ebp, %esi
  21565. + movb %ah, %bl
  21566. + shrl $16, %edx
  21567. + movl 0x300+des_SPtrans(%ecx),%ebp
  21568. + xorl %ebp, %esi
  21569. + movl 24(%esp), %ebp
  21570. + movb %dh, %cl
  21571. + andl $0xff, %eax
  21572. + andl $0xff, %edx
  21573. + movl 0x600+des_SPtrans(%ebx),%ebx
  21574. + xorl %ebx, %esi
  21575. + movl 0x700+des_SPtrans(%ecx),%ebx
  21576. + xorl %ebx, %esi
  21577. + movl 0x400+des_SPtrans(%eax),%ebx
  21578. + xorl %ebx, %esi
  21579. + movl 0x500+des_SPtrans(%edx),%ebx
  21580. + xorl %ebx, %esi
  21581. +
  21582. +
  21583. + movl 80(%ebp), %eax
  21584. + xorl %ebx, %ebx
  21585. + movl 84(%ebp), %edx
  21586. + xorl %esi, %eax
  21587. + xorl %esi, %edx
  21588. + andl $0xfcfcfcfc, %eax
  21589. + andl $0xcfcfcfcf, %edx
  21590. + movb %al, %bl
  21591. + movb %ah, %cl
  21592. + rorl $4, %edx
  21593. + movl des_SPtrans(%ebx),%ebp
  21594. + movb %dl, %bl
  21595. + xorl %ebp, %edi
  21596. + movl 0x200+des_SPtrans(%ecx),%ebp
  21597. + xorl %ebp, %edi
  21598. + movb %dh, %cl
  21599. + shrl $16, %eax
  21600. + movl 0x100+des_SPtrans(%ebx),%ebp
  21601. + xorl %ebp, %edi
  21602. + movb %ah, %bl
  21603. + shrl $16, %edx
  21604. + movl 0x300+des_SPtrans(%ecx),%ebp
  21605. + xorl %ebp, %edi
  21606. + movl 24(%esp), %ebp
  21607. + movb %dh, %cl
  21608. + andl $0xff, %eax
  21609. + andl $0xff, %edx
  21610. + movl 0x600+des_SPtrans(%ebx),%ebx
  21611. + xorl %ebx, %edi
  21612. + movl 0x700+des_SPtrans(%ecx),%ebx
  21613. + xorl %ebx, %edi
  21614. + movl 0x400+des_SPtrans(%eax),%ebx
  21615. + xorl %ebx, %edi
  21616. + movl 0x500+des_SPtrans(%edx),%ebx
  21617. + xorl %ebx, %edi
  21618. +
  21619. +
  21620. + movl 88(%ebp), %eax
  21621. + xorl %ebx, %ebx
  21622. + movl 92(%ebp), %edx
  21623. + xorl %edi, %eax
  21624. + xorl %edi, %edx
  21625. + andl $0xfcfcfcfc, %eax
  21626. + andl $0xcfcfcfcf, %edx
  21627. + movb %al, %bl
  21628. + movb %ah, %cl
  21629. + rorl $4, %edx
  21630. + movl des_SPtrans(%ebx),%ebp
  21631. + movb %dl, %bl
  21632. + xorl %ebp, %esi
  21633. + movl 0x200+des_SPtrans(%ecx),%ebp
  21634. + xorl %ebp, %esi
  21635. + movb %dh, %cl
  21636. + shrl $16, %eax
  21637. + movl 0x100+des_SPtrans(%ebx),%ebp
  21638. + xorl %ebp, %esi
  21639. + movb %ah, %bl
  21640. + shrl $16, %edx
  21641. + movl 0x300+des_SPtrans(%ecx),%ebp
  21642. + xorl %ebp, %esi
  21643. + movl 24(%esp), %ebp
  21644. + movb %dh, %cl
  21645. + andl $0xff, %eax
  21646. + andl $0xff, %edx
  21647. + movl 0x600+des_SPtrans(%ebx),%ebx
  21648. + xorl %ebx, %esi
  21649. + movl 0x700+des_SPtrans(%ecx),%ebx
  21650. + xorl %ebx, %esi
  21651. + movl 0x400+des_SPtrans(%eax),%ebx
  21652. + xorl %ebx, %esi
  21653. + movl 0x500+des_SPtrans(%edx),%ebx
  21654. + xorl %ebx, %esi
  21655. +
  21656. +
  21657. + movl 96(%ebp), %eax
  21658. + xorl %ebx, %ebx
  21659. + movl 100(%ebp), %edx
  21660. + xorl %esi, %eax
  21661. + xorl %esi, %edx
  21662. + andl $0xfcfcfcfc, %eax
  21663. + andl $0xcfcfcfcf, %edx
  21664. + movb %al, %bl
  21665. + movb %ah, %cl
  21666. + rorl $4, %edx
  21667. + movl des_SPtrans(%ebx),%ebp
  21668. + movb %dl, %bl
  21669. + xorl %ebp, %edi
  21670. + movl 0x200+des_SPtrans(%ecx),%ebp
  21671. + xorl %ebp, %edi
  21672. + movb %dh, %cl
  21673. + shrl $16, %eax
  21674. + movl 0x100+des_SPtrans(%ebx),%ebp
  21675. + xorl %ebp, %edi
  21676. + movb %ah, %bl
  21677. + shrl $16, %edx
  21678. + movl 0x300+des_SPtrans(%ecx),%ebp
  21679. + xorl %ebp, %edi
  21680. + movl 24(%esp), %ebp
  21681. + movb %dh, %cl
  21682. + andl $0xff, %eax
  21683. + andl $0xff, %edx
  21684. + movl 0x600+des_SPtrans(%ebx),%ebx
  21685. + xorl %ebx, %edi
  21686. + movl 0x700+des_SPtrans(%ecx),%ebx
  21687. + xorl %ebx, %edi
  21688. + movl 0x400+des_SPtrans(%eax),%ebx
  21689. + xorl %ebx, %edi
  21690. + movl 0x500+des_SPtrans(%edx),%ebx
  21691. + xorl %ebx, %edi
  21692. +
  21693. +
  21694. + movl 104(%ebp), %eax
  21695. + xorl %ebx, %ebx
  21696. + movl 108(%ebp), %edx
  21697. + xorl %edi, %eax
  21698. + xorl %edi, %edx
  21699. + andl $0xfcfcfcfc, %eax
  21700. + andl $0xcfcfcfcf, %edx
  21701. + movb %al, %bl
  21702. + movb %ah, %cl
  21703. + rorl $4, %edx
  21704. + movl des_SPtrans(%ebx),%ebp
  21705. + movb %dl, %bl
  21706. + xorl %ebp, %esi
  21707. + movl 0x200+des_SPtrans(%ecx),%ebp
  21708. + xorl %ebp, %esi
  21709. + movb %dh, %cl
  21710. + shrl $16, %eax
  21711. + movl 0x100+des_SPtrans(%ebx),%ebp
  21712. + xorl %ebp, %esi
  21713. + movb %ah, %bl
  21714. + shrl $16, %edx
  21715. + movl 0x300+des_SPtrans(%ecx),%ebp
  21716. + xorl %ebp, %esi
  21717. + movl 24(%esp), %ebp
  21718. + movb %dh, %cl
  21719. + andl $0xff, %eax
  21720. + andl $0xff, %edx
  21721. + movl 0x600+des_SPtrans(%ebx),%ebx
  21722. + xorl %ebx, %esi
  21723. + movl 0x700+des_SPtrans(%ecx),%ebx
  21724. + xorl %ebx, %esi
  21725. + movl 0x400+des_SPtrans(%eax),%ebx
  21726. + xorl %ebx, %esi
  21727. + movl 0x500+des_SPtrans(%edx),%ebx
  21728. + xorl %ebx, %esi
  21729. +
  21730. +
  21731. + movl 112(%ebp), %eax
  21732. + xorl %ebx, %ebx
  21733. + movl 116(%ebp), %edx
  21734. + xorl %esi, %eax
  21735. + xorl %esi, %edx
  21736. + andl $0xfcfcfcfc, %eax
  21737. + andl $0xcfcfcfcf, %edx
  21738. + movb %al, %bl
  21739. + movb %ah, %cl
  21740. + rorl $4, %edx
  21741. + movl des_SPtrans(%ebx),%ebp
  21742. + movb %dl, %bl
  21743. + xorl %ebp, %edi
  21744. + movl 0x200+des_SPtrans(%ecx),%ebp
  21745. + xorl %ebp, %edi
  21746. + movb %dh, %cl
  21747. + shrl $16, %eax
  21748. + movl 0x100+des_SPtrans(%ebx),%ebp
  21749. + xorl %ebp, %edi
  21750. + movb %ah, %bl
  21751. + shrl $16, %edx
  21752. + movl 0x300+des_SPtrans(%ecx),%ebp
  21753. + xorl %ebp, %edi
  21754. + movl 24(%esp), %ebp
  21755. + movb %dh, %cl
  21756. + andl $0xff, %eax
  21757. + andl $0xff, %edx
  21758. + movl 0x600+des_SPtrans(%ebx),%ebx
  21759. + xorl %ebx, %edi
  21760. + movl 0x700+des_SPtrans(%ecx),%ebx
  21761. + xorl %ebx, %edi
  21762. + movl 0x400+des_SPtrans(%eax),%ebx
  21763. + xorl %ebx, %edi
  21764. + movl 0x500+des_SPtrans(%edx),%ebx
  21765. + xorl %ebx, %edi
  21766. +
  21767. +
  21768. + movl 120(%ebp), %eax
  21769. + xorl %ebx, %ebx
  21770. + movl 124(%ebp), %edx
  21771. + xorl %edi, %eax
  21772. + xorl %edi, %edx
  21773. + andl $0xfcfcfcfc, %eax
  21774. + andl $0xcfcfcfcf, %edx
  21775. + movb %al, %bl
  21776. + movb %ah, %cl
  21777. + rorl $4, %edx
  21778. + movl des_SPtrans(%ebx),%ebp
  21779. + movb %dl, %bl
  21780. + xorl %ebp, %esi
  21781. + movl 0x200+des_SPtrans(%ecx),%ebp
  21782. + xorl %ebp, %esi
  21783. + movb %dh, %cl
  21784. + shrl $16, %eax
  21785. + movl 0x100+des_SPtrans(%ebx),%ebp
  21786. + xorl %ebp, %esi
  21787. + movb %ah, %bl
  21788. + shrl $16, %edx
  21789. + movl 0x300+des_SPtrans(%ecx),%ebp
  21790. + xorl %ebp, %esi
  21791. + movl 24(%esp), %ebp
  21792. + movb %dh, %cl
  21793. + andl $0xff, %eax
  21794. + andl $0xff, %edx
  21795. + movl 0x600+des_SPtrans(%ebx),%ebx
  21796. + xorl %ebx, %esi
  21797. + movl 0x700+des_SPtrans(%ecx),%ebx
  21798. + xorl %ebx, %esi
  21799. + movl 0x400+des_SPtrans(%eax),%ebx
  21800. + xorl %ebx, %esi
  21801. + movl 0x500+des_SPtrans(%edx),%ebx
  21802. + xorl %ebx, %esi
  21803. + jmp .L003end
  21804. +.L002start_decrypt:
  21805. +
  21806. +
  21807. + movl 120(%ebp), %eax
  21808. + xorl %ebx, %ebx
  21809. + movl 124(%ebp), %edx
  21810. + xorl %esi, %eax
  21811. + xorl %esi, %edx
  21812. + andl $0xfcfcfcfc, %eax
  21813. + andl $0xcfcfcfcf, %edx
  21814. + movb %al, %bl
  21815. + movb %ah, %cl
  21816. + rorl $4, %edx
  21817. + movl des_SPtrans(%ebx),%ebp
  21818. + movb %dl, %bl
  21819. + xorl %ebp, %edi
  21820. + movl 0x200+des_SPtrans(%ecx),%ebp
  21821. + xorl %ebp, %edi
  21822. + movb %dh, %cl
  21823. + shrl $16, %eax
  21824. + movl 0x100+des_SPtrans(%ebx),%ebp
  21825. + xorl %ebp, %edi
  21826. + movb %ah, %bl
  21827. + shrl $16, %edx
  21828. + movl 0x300+des_SPtrans(%ecx),%ebp
  21829. + xorl %ebp, %edi
  21830. + movl 24(%esp), %ebp
  21831. + movb %dh, %cl
  21832. + andl $0xff, %eax
  21833. + andl $0xff, %edx
  21834. + movl 0x600+des_SPtrans(%ebx),%ebx
  21835. + xorl %ebx, %edi
  21836. + movl 0x700+des_SPtrans(%ecx),%ebx
  21837. + xorl %ebx, %edi
  21838. + movl 0x400+des_SPtrans(%eax),%ebx
  21839. + xorl %ebx, %edi
  21840. + movl 0x500+des_SPtrans(%edx),%ebx
  21841. + xorl %ebx, %edi
  21842. +
  21843. +
  21844. + movl 112(%ebp), %eax
  21845. + xorl %ebx, %ebx
  21846. + movl 116(%ebp), %edx
  21847. + xorl %edi, %eax
  21848. + xorl %edi, %edx
  21849. + andl $0xfcfcfcfc, %eax
  21850. + andl $0xcfcfcfcf, %edx
  21851. + movb %al, %bl
  21852. + movb %ah, %cl
  21853. + rorl $4, %edx
  21854. + movl des_SPtrans(%ebx),%ebp
  21855. + movb %dl, %bl
  21856. + xorl %ebp, %esi
  21857. + movl 0x200+des_SPtrans(%ecx),%ebp
  21858. + xorl %ebp, %esi
  21859. + movb %dh, %cl
  21860. + shrl $16, %eax
  21861. + movl 0x100+des_SPtrans(%ebx),%ebp
  21862. + xorl %ebp, %esi
  21863. + movb %ah, %bl
  21864. + shrl $16, %edx
  21865. + movl 0x300+des_SPtrans(%ecx),%ebp
  21866. + xorl %ebp, %esi
  21867. + movl 24(%esp), %ebp
  21868. + movb %dh, %cl
  21869. + andl $0xff, %eax
  21870. + andl $0xff, %edx
  21871. + movl 0x600+des_SPtrans(%ebx),%ebx
  21872. + xorl %ebx, %esi
  21873. + movl 0x700+des_SPtrans(%ecx),%ebx
  21874. + xorl %ebx, %esi
  21875. + movl 0x400+des_SPtrans(%eax),%ebx
  21876. + xorl %ebx, %esi
  21877. + movl 0x500+des_SPtrans(%edx),%ebx
  21878. + xorl %ebx, %esi
  21879. +
  21880. +
  21881. + movl 104(%ebp), %eax
  21882. + xorl %ebx, %ebx
  21883. + movl 108(%ebp), %edx
  21884. + xorl %esi, %eax
  21885. + xorl %esi, %edx
  21886. + andl $0xfcfcfcfc, %eax
  21887. + andl $0xcfcfcfcf, %edx
  21888. + movb %al, %bl
  21889. + movb %ah, %cl
  21890. + rorl $4, %edx
  21891. + movl des_SPtrans(%ebx),%ebp
  21892. + movb %dl, %bl
  21893. + xorl %ebp, %edi
  21894. + movl 0x200+des_SPtrans(%ecx),%ebp
  21895. + xorl %ebp, %edi
  21896. + movb %dh, %cl
  21897. + shrl $16, %eax
  21898. + movl 0x100+des_SPtrans(%ebx),%ebp
  21899. + xorl %ebp, %edi
  21900. + movb %ah, %bl
  21901. + shrl $16, %edx
  21902. + movl 0x300+des_SPtrans(%ecx),%ebp
  21903. + xorl %ebp, %edi
  21904. + movl 24(%esp), %ebp
  21905. + movb %dh, %cl
  21906. + andl $0xff, %eax
  21907. + andl $0xff, %edx
  21908. + movl 0x600+des_SPtrans(%ebx),%ebx
  21909. + xorl %ebx, %edi
  21910. + movl 0x700+des_SPtrans(%ecx),%ebx
  21911. + xorl %ebx, %edi
  21912. + movl 0x400+des_SPtrans(%eax),%ebx
  21913. + xorl %ebx, %edi
  21914. + movl 0x500+des_SPtrans(%edx),%ebx
  21915. + xorl %ebx, %edi
  21916. +
  21917. +
  21918. + movl 96(%ebp), %eax
  21919. + xorl %ebx, %ebx
  21920. + movl 100(%ebp), %edx
  21921. + xorl %edi, %eax
  21922. + xorl %edi, %edx
  21923. + andl $0xfcfcfcfc, %eax
  21924. + andl $0xcfcfcfcf, %edx
  21925. + movb %al, %bl
  21926. + movb %ah, %cl
  21927. + rorl $4, %edx
  21928. + movl des_SPtrans(%ebx),%ebp
  21929. + movb %dl, %bl
  21930. + xorl %ebp, %esi
  21931. + movl 0x200+des_SPtrans(%ecx),%ebp
  21932. + xorl %ebp, %esi
  21933. + movb %dh, %cl
  21934. + shrl $16, %eax
  21935. + movl 0x100+des_SPtrans(%ebx),%ebp
  21936. + xorl %ebp, %esi
  21937. + movb %ah, %bl
  21938. + shrl $16, %edx
  21939. + movl 0x300+des_SPtrans(%ecx),%ebp
  21940. + xorl %ebp, %esi
  21941. + movl 24(%esp), %ebp
  21942. + movb %dh, %cl
  21943. + andl $0xff, %eax
  21944. + andl $0xff, %edx
  21945. + movl 0x600+des_SPtrans(%ebx),%ebx
  21946. + xorl %ebx, %esi
  21947. + movl 0x700+des_SPtrans(%ecx),%ebx
  21948. + xorl %ebx, %esi
  21949. + movl 0x400+des_SPtrans(%eax),%ebx
  21950. + xorl %ebx, %esi
  21951. + movl 0x500+des_SPtrans(%edx),%ebx
  21952. + xorl %ebx, %esi
  21953. +
  21954. +
  21955. + movl 88(%ebp), %eax
  21956. + xorl %ebx, %ebx
  21957. + movl 92(%ebp), %edx
  21958. + xorl %esi, %eax
  21959. + xorl %esi, %edx
  21960. + andl $0xfcfcfcfc, %eax
  21961. + andl $0xcfcfcfcf, %edx
  21962. + movb %al, %bl
  21963. + movb %ah, %cl
  21964. + rorl $4, %edx
  21965. + movl des_SPtrans(%ebx),%ebp
  21966. + movb %dl, %bl
  21967. + xorl %ebp, %edi
  21968. + movl 0x200+des_SPtrans(%ecx),%ebp
  21969. + xorl %ebp, %edi
  21970. + movb %dh, %cl
  21971. + shrl $16, %eax
  21972. + movl 0x100+des_SPtrans(%ebx),%ebp
  21973. + xorl %ebp, %edi
  21974. + movb %ah, %bl
  21975. + shrl $16, %edx
  21976. + movl 0x300+des_SPtrans(%ecx),%ebp
  21977. + xorl %ebp, %edi
  21978. + movl 24(%esp), %ebp
  21979. + movb %dh, %cl
  21980. + andl $0xff, %eax
  21981. + andl $0xff, %edx
  21982. + movl 0x600+des_SPtrans(%ebx),%ebx
  21983. + xorl %ebx, %edi
  21984. + movl 0x700+des_SPtrans(%ecx),%ebx
  21985. + xorl %ebx, %edi
  21986. + movl 0x400+des_SPtrans(%eax),%ebx
  21987. + xorl %ebx, %edi
  21988. + movl 0x500+des_SPtrans(%edx),%ebx
  21989. + xorl %ebx, %edi
  21990. +
  21991. +
  21992. + movl 80(%ebp), %eax
  21993. + xorl %ebx, %ebx
  21994. + movl 84(%ebp), %edx
  21995. + xorl %edi, %eax
  21996. + xorl %edi, %edx
  21997. + andl $0xfcfcfcfc, %eax
  21998. + andl $0xcfcfcfcf, %edx
  21999. + movb %al, %bl
  22000. + movb %ah, %cl
  22001. + rorl $4, %edx
  22002. + movl des_SPtrans(%ebx),%ebp
  22003. + movb %dl, %bl
  22004. + xorl %ebp, %esi
  22005. + movl 0x200+des_SPtrans(%ecx),%ebp
  22006. + xorl %ebp, %esi
  22007. + movb %dh, %cl
  22008. + shrl $16, %eax
  22009. + movl 0x100+des_SPtrans(%ebx),%ebp
  22010. + xorl %ebp, %esi
  22011. + movb %ah, %bl
  22012. + shrl $16, %edx
  22013. + movl 0x300+des_SPtrans(%ecx),%ebp
  22014. + xorl %ebp, %esi
  22015. + movl 24(%esp), %ebp
  22016. + movb %dh, %cl
  22017. + andl $0xff, %eax
  22018. + andl $0xff, %edx
  22019. + movl 0x600+des_SPtrans(%ebx),%ebx
  22020. + xorl %ebx, %esi
  22021. + movl 0x700+des_SPtrans(%ecx),%ebx
  22022. + xorl %ebx, %esi
  22023. + movl 0x400+des_SPtrans(%eax),%ebx
  22024. + xorl %ebx, %esi
  22025. + movl 0x500+des_SPtrans(%edx),%ebx
  22026. + xorl %ebx, %esi
  22027. +
  22028. +
  22029. + movl 72(%ebp), %eax
  22030. + xorl %ebx, %ebx
  22031. + movl 76(%ebp), %edx
  22032. + xorl %esi, %eax
  22033. + xorl %esi, %edx
  22034. + andl $0xfcfcfcfc, %eax
  22035. + andl $0xcfcfcfcf, %edx
  22036. + movb %al, %bl
  22037. + movb %ah, %cl
  22038. + rorl $4, %edx
  22039. + movl des_SPtrans(%ebx),%ebp
  22040. + movb %dl, %bl
  22041. + xorl %ebp, %edi
  22042. + movl 0x200+des_SPtrans(%ecx),%ebp
  22043. + xorl %ebp, %edi
  22044. + movb %dh, %cl
  22045. + shrl $16, %eax
  22046. + movl 0x100+des_SPtrans(%ebx),%ebp
  22047. + xorl %ebp, %edi
  22048. + movb %ah, %bl
  22049. + shrl $16, %edx
  22050. + movl 0x300+des_SPtrans(%ecx),%ebp
  22051. + xorl %ebp, %edi
  22052. + movl 24(%esp), %ebp
  22053. + movb %dh, %cl
  22054. + andl $0xff, %eax
  22055. + andl $0xff, %edx
  22056. + movl 0x600+des_SPtrans(%ebx),%ebx
  22057. + xorl %ebx, %edi
  22058. + movl 0x700+des_SPtrans(%ecx),%ebx
  22059. + xorl %ebx, %edi
  22060. + movl 0x400+des_SPtrans(%eax),%ebx
  22061. + xorl %ebx, %edi
  22062. + movl 0x500+des_SPtrans(%edx),%ebx
  22063. + xorl %ebx, %edi
  22064. +
  22065. +
  22066. + movl 64(%ebp), %eax
  22067. + xorl %ebx, %ebx
  22068. + movl 68(%ebp), %edx
  22069. + xorl %edi, %eax
  22070. + xorl %edi, %edx
  22071. + andl $0xfcfcfcfc, %eax
  22072. + andl $0xcfcfcfcf, %edx
  22073. + movb %al, %bl
  22074. + movb %ah, %cl
  22075. + rorl $4, %edx
  22076. + movl des_SPtrans(%ebx),%ebp
  22077. + movb %dl, %bl
  22078. + xorl %ebp, %esi
  22079. + movl 0x200+des_SPtrans(%ecx),%ebp
  22080. + xorl %ebp, %esi
  22081. + movb %dh, %cl
  22082. + shrl $16, %eax
  22083. + movl 0x100+des_SPtrans(%ebx),%ebp
  22084. + xorl %ebp, %esi
  22085. + movb %ah, %bl
  22086. + shrl $16, %edx
  22087. + movl 0x300+des_SPtrans(%ecx),%ebp
  22088. + xorl %ebp, %esi
  22089. + movl 24(%esp), %ebp
  22090. + movb %dh, %cl
  22091. + andl $0xff, %eax
  22092. + andl $0xff, %edx
  22093. + movl 0x600+des_SPtrans(%ebx),%ebx
  22094. + xorl %ebx, %esi
  22095. + movl 0x700+des_SPtrans(%ecx),%ebx
  22096. + xorl %ebx, %esi
  22097. + movl 0x400+des_SPtrans(%eax),%ebx
  22098. + xorl %ebx, %esi
  22099. + movl 0x500+des_SPtrans(%edx),%ebx
  22100. + xorl %ebx, %esi
  22101. +
  22102. +
  22103. + movl 56(%ebp), %eax
  22104. + xorl %ebx, %ebx
  22105. + movl 60(%ebp), %edx
  22106. + xorl %esi, %eax
  22107. + xorl %esi, %edx
  22108. + andl $0xfcfcfcfc, %eax
  22109. + andl $0xcfcfcfcf, %edx
  22110. + movb %al, %bl
  22111. + movb %ah, %cl
  22112. + rorl $4, %edx
  22113. + movl des_SPtrans(%ebx),%ebp
  22114. + movb %dl, %bl
  22115. + xorl %ebp, %edi
  22116. + movl 0x200+des_SPtrans(%ecx),%ebp
  22117. + xorl %ebp, %edi
  22118. + movb %dh, %cl
  22119. + shrl $16, %eax
  22120. + movl 0x100+des_SPtrans(%ebx),%ebp
  22121. + xorl %ebp, %edi
  22122. + movb %ah, %bl
  22123. + shrl $16, %edx
  22124. + movl 0x300+des_SPtrans(%ecx),%ebp
  22125. + xorl %ebp, %edi
  22126. + movl 24(%esp), %ebp
  22127. + movb %dh, %cl
  22128. + andl $0xff, %eax
  22129. + andl $0xff, %edx
  22130. + movl 0x600+des_SPtrans(%ebx),%ebx
  22131. + xorl %ebx, %edi
  22132. + movl 0x700+des_SPtrans(%ecx),%ebx
  22133. + xorl %ebx, %edi
  22134. + movl 0x400+des_SPtrans(%eax),%ebx
  22135. + xorl %ebx, %edi
  22136. + movl 0x500+des_SPtrans(%edx),%ebx
  22137. + xorl %ebx, %edi
  22138. +
  22139. +
  22140. + movl 48(%ebp), %eax
  22141. + xorl %ebx, %ebx
  22142. + movl 52(%ebp), %edx
  22143. + xorl %edi, %eax
  22144. + xorl %edi, %edx
  22145. + andl $0xfcfcfcfc, %eax
  22146. + andl $0xcfcfcfcf, %edx
  22147. + movb %al, %bl
  22148. + movb %ah, %cl
  22149. + rorl $4, %edx
  22150. + movl des_SPtrans(%ebx),%ebp
  22151. + movb %dl, %bl
  22152. + xorl %ebp, %esi
  22153. + movl 0x200+des_SPtrans(%ecx),%ebp
  22154. + xorl %ebp, %esi
  22155. + movb %dh, %cl
  22156. + shrl $16, %eax
  22157. + movl 0x100+des_SPtrans(%ebx),%ebp
  22158. + xorl %ebp, %esi
  22159. + movb %ah, %bl
  22160. + shrl $16, %edx
  22161. + movl 0x300+des_SPtrans(%ecx),%ebp
  22162. + xorl %ebp, %esi
  22163. + movl 24(%esp), %ebp
  22164. + movb %dh, %cl
  22165. + andl $0xff, %eax
  22166. + andl $0xff, %edx
  22167. + movl 0x600+des_SPtrans(%ebx),%ebx
  22168. + xorl %ebx, %esi
  22169. + movl 0x700+des_SPtrans(%ecx),%ebx
  22170. + xorl %ebx, %esi
  22171. + movl 0x400+des_SPtrans(%eax),%ebx
  22172. + xorl %ebx, %esi
  22173. + movl 0x500+des_SPtrans(%edx),%ebx
  22174. + xorl %ebx, %esi
  22175. +
  22176. +
  22177. + movl 40(%ebp), %eax
  22178. + xorl %ebx, %ebx
  22179. + movl 44(%ebp), %edx
  22180. + xorl %esi, %eax
  22181. + xorl %esi, %edx
  22182. + andl $0xfcfcfcfc, %eax
  22183. + andl $0xcfcfcfcf, %edx
  22184. + movb %al, %bl
  22185. + movb %ah, %cl
  22186. + rorl $4, %edx
  22187. + movl des_SPtrans(%ebx),%ebp
  22188. + movb %dl, %bl
  22189. + xorl %ebp, %edi
  22190. + movl 0x200+des_SPtrans(%ecx),%ebp
  22191. + xorl %ebp, %edi
  22192. + movb %dh, %cl
  22193. + shrl $16, %eax
  22194. + movl 0x100+des_SPtrans(%ebx),%ebp
  22195. + xorl %ebp, %edi
  22196. + movb %ah, %bl
  22197. + shrl $16, %edx
  22198. + movl 0x300+des_SPtrans(%ecx),%ebp
  22199. + xorl %ebp, %edi
  22200. + movl 24(%esp), %ebp
  22201. + movb %dh, %cl
  22202. + andl $0xff, %eax
  22203. + andl $0xff, %edx
  22204. + movl 0x600+des_SPtrans(%ebx),%ebx
  22205. + xorl %ebx, %edi
  22206. + movl 0x700+des_SPtrans(%ecx),%ebx
  22207. + xorl %ebx, %edi
  22208. + movl 0x400+des_SPtrans(%eax),%ebx
  22209. + xorl %ebx, %edi
  22210. + movl 0x500+des_SPtrans(%edx),%ebx
  22211. + xorl %ebx, %edi
  22212. +
  22213. +
  22214. + movl 32(%ebp), %eax
  22215. + xorl %ebx, %ebx
  22216. + movl 36(%ebp), %edx
  22217. + xorl %edi, %eax
  22218. + xorl %edi, %edx
  22219. + andl $0xfcfcfcfc, %eax
  22220. + andl $0xcfcfcfcf, %edx
  22221. + movb %al, %bl
  22222. + movb %ah, %cl
  22223. + rorl $4, %edx
  22224. + movl des_SPtrans(%ebx),%ebp
  22225. + movb %dl, %bl
  22226. + xorl %ebp, %esi
  22227. + movl 0x200+des_SPtrans(%ecx),%ebp
  22228. + xorl %ebp, %esi
  22229. + movb %dh, %cl
  22230. + shrl $16, %eax
  22231. + movl 0x100+des_SPtrans(%ebx),%ebp
  22232. + xorl %ebp, %esi
  22233. + movb %ah, %bl
  22234. + shrl $16, %edx
  22235. + movl 0x300+des_SPtrans(%ecx),%ebp
  22236. + xorl %ebp, %esi
  22237. + movl 24(%esp), %ebp
  22238. + movb %dh, %cl
  22239. + andl $0xff, %eax
  22240. + andl $0xff, %edx
  22241. + movl 0x600+des_SPtrans(%ebx),%ebx
  22242. + xorl %ebx, %esi
  22243. + movl 0x700+des_SPtrans(%ecx),%ebx
  22244. + xorl %ebx, %esi
  22245. + movl 0x400+des_SPtrans(%eax),%ebx
  22246. + xorl %ebx, %esi
  22247. + movl 0x500+des_SPtrans(%edx),%ebx
  22248. + xorl %ebx, %esi
  22249. +
  22250. +
  22251. + movl 24(%ebp), %eax
  22252. + xorl %ebx, %ebx
  22253. + movl 28(%ebp), %edx
  22254. + xorl %esi, %eax
  22255. + xorl %esi, %edx
  22256. + andl $0xfcfcfcfc, %eax
  22257. + andl $0xcfcfcfcf, %edx
  22258. + movb %al, %bl
  22259. + movb %ah, %cl
  22260. + rorl $4, %edx
  22261. + movl des_SPtrans(%ebx),%ebp
  22262. + movb %dl, %bl
  22263. + xorl %ebp, %edi
  22264. + movl 0x200+des_SPtrans(%ecx),%ebp
  22265. + xorl %ebp, %edi
  22266. + movb %dh, %cl
  22267. + shrl $16, %eax
  22268. + movl 0x100+des_SPtrans(%ebx),%ebp
  22269. + xorl %ebp, %edi
  22270. + movb %ah, %bl
  22271. + shrl $16, %edx
  22272. + movl 0x300+des_SPtrans(%ecx),%ebp
  22273. + xorl %ebp, %edi
  22274. + movl 24(%esp), %ebp
  22275. + movb %dh, %cl
  22276. + andl $0xff, %eax
  22277. + andl $0xff, %edx
  22278. + movl 0x600+des_SPtrans(%ebx),%ebx
  22279. + xorl %ebx, %edi
  22280. + movl 0x700+des_SPtrans(%ecx),%ebx
  22281. + xorl %ebx, %edi
  22282. + movl 0x400+des_SPtrans(%eax),%ebx
  22283. + xorl %ebx, %edi
  22284. + movl 0x500+des_SPtrans(%edx),%ebx
  22285. + xorl %ebx, %edi
  22286. +
  22287. +
  22288. + movl 16(%ebp), %eax
  22289. + xorl %ebx, %ebx
  22290. + movl 20(%ebp), %edx
  22291. + xorl %edi, %eax
  22292. + xorl %edi, %edx
  22293. + andl $0xfcfcfcfc, %eax
  22294. + andl $0xcfcfcfcf, %edx
  22295. + movb %al, %bl
  22296. + movb %ah, %cl
  22297. + rorl $4, %edx
  22298. + movl des_SPtrans(%ebx),%ebp
  22299. + movb %dl, %bl
  22300. + xorl %ebp, %esi
  22301. + movl 0x200+des_SPtrans(%ecx),%ebp
  22302. + xorl %ebp, %esi
  22303. + movb %dh, %cl
  22304. + shrl $16, %eax
  22305. + movl 0x100+des_SPtrans(%ebx),%ebp
  22306. + xorl %ebp, %esi
  22307. + movb %ah, %bl
  22308. + shrl $16, %edx
  22309. + movl 0x300+des_SPtrans(%ecx),%ebp
  22310. + xorl %ebp, %esi
  22311. + movl 24(%esp), %ebp
  22312. + movb %dh, %cl
  22313. + andl $0xff, %eax
  22314. + andl $0xff, %edx
  22315. + movl 0x600+des_SPtrans(%ebx),%ebx
  22316. + xorl %ebx, %esi
  22317. + movl 0x700+des_SPtrans(%ecx),%ebx
  22318. + xorl %ebx, %esi
  22319. + movl 0x400+des_SPtrans(%eax),%ebx
  22320. + xorl %ebx, %esi
  22321. + movl 0x500+des_SPtrans(%edx),%ebx
  22322. + xorl %ebx, %esi
  22323. +
  22324. +
  22325. + movl 8(%ebp), %eax
  22326. + xorl %ebx, %ebx
  22327. + movl 12(%ebp), %edx
  22328. + xorl %esi, %eax
  22329. + xorl %esi, %edx
  22330. + andl $0xfcfcfcfc, %eax
  22331. + andl $0xcfcfcfcf, %edx
  22332. + movb %al, %bl
  22333. + movb %ah, %cl
  22334. + rorl $4, %edx
  22335. + movl des_SPtrans(%ebx),%ebp
  22336. + movb %dl, %bl
  22337. + xorl %ebp, %edi
  22338. + movl 0x200+des_SPtrans(%ecx),%ebp
  22339. + xorl %ebp, %edi
  22340. + movb %dh, %cl
  22341. + shrl $16, %eax
  22342. + movl 0x100+des_SPtrans(%ebx),%ebp
  22343. + xorl %ebp, %edi
  22344. + movb %ah, %bl
  22345. + shrl $16, %edx
  22346. + movl 0x300+des_SPtrans(%ecx),%ebp
  22347. + xorl %ebp, %edi
  22348. + movl 24(%esp), %ebp
  22349. + movb %dh, %cl
  22350. + andl $0xff, %eax
  22351. + andl $0xff, %edx
  22352. + movl 0x600+des_SPtrans(%ebx),%ebx
  22353. + xorl %ebx, %edi
  22354. + movl 0x700+des_SPtrans(%ecx),%ebx
  22355. + xorl %ebx, %edi
  22356. + movl 0x400+des_SPtrans(%eax),%ebx
  22357. + xorl %ebx, %edi
  22358. + movl 0x500+des_SPtrans(%edx),%ebx
  22359. + xorl %ebx, %edi
  22360. +
  22361. +
  22362. + movl (%ebp), %eax
  22363. + xorl %ebx, %ebx
  22364. + movl 4(%ebp), %edx
  22365. + xorl %edi, %eax
  22366. + xorl %edi, %edx
  22367. + andl $0xfcfcfcfc, %eax
  22368. + andl $0xcfcfcfcf, %edx
  22369. + movb %al, %bl
  22370. + movb %ah, %cl
  22371. + rorl $4, %edx
  22372. + movl des_SPtrans(%ebx),%ebp
  22373. + movb %dl, %bl
  22374. + xorl %ebp, %esi
  22375. + movl 0x200+des_SPtrans(%ecx),%ebp
  22376. + xorl %ebp, %esi
  22377. + movb %dh, %cl
  22378. + shrl $16, %eax
  22379. + movl 0x100+des_SPtrans(%ebx),%ebp
  22380. + xorl %ebp, %esi
  22381. + movb %ah, %bl
  22382. + shrl $16, %edx
  22383. + movl 0x300+des_SPtrans(%ecx),%ebp
  22384. + xorl %ebp, %esi
  22385. + movl 24(%esp), %ebp
  22386. + movb %dh, %cl
  22387. + andl $0xff, %eax
  22388. + andl $0xff, %edx
  22389. + movl 0x600+des_SPtrans(%ebx),%ebx
  22390. + xorl %ebx, %esi
  22391. + movl 0x700+des_SPtrans(%ecx),%ebx
  22392. + xorl %ebx, %esi
  22393. + movl 0x400+des_SPtrans(%eax),%ebx
  22394. + xorl %ebx, %esi
  22395. + movl 0x500+des_SPtrans(%edx),%ebx
  22396. + xorl %ebx, %esi
  22397. +.L003end:
  22398. +
  22399. +
  22400. + rorl $3, %edi
  22401. + movl 20(%esp), %eax
  22402. + rorl $3, %esi
  22403. + movl %edi, (%eax)
  22404. + movl %esi, 4(%eax)
  22405. + popl %ebp
  22406. + popl %ebx
  22407. + popl %edi
  22408. + popl %esi
  22409. + ret
  22410. +.des_encrypt2_end:
  22411. + .size des_encrypt2 , .des_encrypt2_end-des_encrypt2
  22412. +.ident "desasm.pl"
  22413. +.text
  22414. + .align 16
  22415. +.globl des_encrypt3
  22416. + .type des_encrypt3 , @function
  22417. +des_encrypt3:
  22418. + pushl %ebx
  22419. + movl 8(%esp), %ebx
  22420. + pushl %ebp
  22421. + pushl %esi
  22422. + pushl %edi
  22423. +
  22424. +
  22425. + movl (%ebx), %edi
  22426. + movl 4(%ebx), %esi
  22427. + subl $12, %esp
  22428. +
  22429. +
  22430. + roll $4, %edi
  22431. + movl %edi, %edx
  22432. + xorl %esi, %edi
  22433. + andl $0xf0f0f0f0, %edi
  22434. + xorl %edi, %edx
  22435. + xorl %edi, %esi
  22436. +
  22437. + roll $20, %esi
  22438. + movl %esi, %edi
  22439. + xorl %edx, %esi
  22440. + andl $0xfff0000f, %esi
  22441. + xorl %esi, %edi
  22442. + xorl %esi, %edx
  22443. +
  22444. + roll $14, %edi
  22445. + movl %edi, %esi
  22446. + xorl %edx, %edi
  22447. + andl $0x33333333, %edi
  22448. + xorl %edi, %esi
  22449. + xorl %edi, %edx
  22450. +
  22451. + roll $22, %edx
  22452. + movl %edx, %edi
  22453. + xorl %esi, %edx
  22454. + andl $0x03fc03fc, %edx
  22455. + xorl %edx, %edi
  22456. + xorl %edx, %esi
  22457. +
  22458. + roll $9, %edi
  22459. + movl %edi, %edx
  22460. + xorl %esi, %edi
  22461. + andl $0xaaaaaaaa, %edi
  22462. + xorl %edi, %edx
  22463. + xorl %edi, %esi
  22464. +
  22465. + rorl $3, %edx
  22466. + rorl $2, %esi
  22467. + movl %esi, 4(%ebx)
  22468. + movl 36(%esp), %eax
  22469. + movl %edx, (%ebx)
  22470. + movl 40(%esp), %edi
  22471. + movl 44(%esp), %esi
  22472. + movl $1, 8(%esp)
  22473. + movl %eax, 4(%esp)
  22474. + movl %ebx, (%esp)
  22475. + call des_encrypt2
  22476. + movl $0, 8(%esp)
  22477. + movl %edi, 4(%esp)
  22478. + movl %ebx, (%esp)
  22479. + call des_encrypt2
  22480. + movl $1, 8(%esp)
  22481. + movl %esi, 4(%esp)
  22482. + movl %ebx, (%esp)
  22483. + call des_encrypt2
  22484. + addl $12, %esp
  22485. + movl (%ebx), %edi
  22486. + movl 4(%ebx), %esi
  22487. +
  22488. +
  22489. + roll $2, %esi
  22490. + roll $3, %edi
  22491. + movl %edi, %eax
  22492. + xorl %esi, %edi
  22493. + andl $0xaaaaaaaa, %edi
  22494. + xorl %edi, %eax
  22495. + xorl %edi, %esi
  22496. +
  22497. + roll $23, %eax
  22498. + movl %eax, %edi
  22499. + xorl %esi, %eax
  22500. + andl $0x03fc03fc, %eax
  22501. + xorl %eax, %edi
  22502. + xorl %eax, %esi
  22503. +
  22504. + roll $10, %edi
  22505. + movl %edi, %eax
  22506. + xorl %esi, %edi
  22507. + andl $0x33333333, %edi
  22508. + xorl %edi, %eax
  22509. + xorl %edi, %esi
  22510. +
  22511. + roll $18, %esi
  22512. + movl %esi, %edi
  22513. + xorl %eax, %esi
  22514. + andl $0xfff0000f, %esi
  22515. + xorl %esi, %edi
  22516. + xorl %esi, %eax
  22517. +
  22518. + roll $12, %edi
  22519. + movl %edi, %esi
  22520. + xorl %eax, %edi
  22521. + andl $0xf0f0f0f0, %edi
  22522. + xorl %edi, %esi
  22523. + xorl %edi, %eax
  22524. +
  22525. + rorl $4, %eax
  22526. + movl %eax, (%ebx)
  22527. + movl %esi, 4(%ebx)
  22528. + popl %edi
  22529. + popl %esi
  22530. + popl %ebp
  22531. + popl %ebx
  22532. + ret
  22533. +.des_encrypt3_end:
  22534. + .size des_encrypt3 , .des_encrypt3_end-des_encrypt3
  22535. +.ident "desasm.pl"
  22536. +.text
  22537. + .align 16
  22538. +.globl des_decrypt3
  22539. + .type des_decrypt3 , @function
  22540. +des_decrypt3:
  22541. + pushl %ebx
  22542. + movl 8(%esp), %ebx
  22543. + pushl %ebp
  22544. + pushl %esi
  22545. + pushl %edi
  22546. +
  22547. +
  22548. + movl (%ebx), %edi
  22549. + movl 4(%ebx), %esi
  22550. + subl $12, %esp
  22551. +
  22552. +
  22553. + roll $4, %edi
  22554. + movl %edi, %edx
  22555. + xorl %esi, %edi
  22556. + andl $0xf0f0f0f0, %edi
  22557. + xorl %edi, %edx
  22558. + xorl %edi, %esi
  22559. +
  22560. + roll $20, %esi
  22561. + movl %esi, %edi
  22562. + xorl %edx, %esi
  22563. + andl $0xfff0000f, %esi
  22564. + xorl %esi, %edi
  22565. + xorl %esi, %edx
  22566. +
  22567. + roll $14, %edi
  22568. + movl %edi, %esi
  22569. + xorl %edx, %edi
  22570. + andl $0x33333333, %edi
  22571. + xorl %edi, %esi
  22572. + xorl %edi, %edx
  22573. +
  22574. + roll $22, %edx
  22575. + movl %edx, %edi
  22576. + xorl %esi, %edx
  22577. + andl $0x03fc03fc, %edx
  22578. + xorl %edx, %edi
  22579. + xorl %edx, %esi
  22580. +
  22581. + roll $9, %edi
  22582. + movl %edi, %edx
  22583. + xorl %esi, %edi
  22584. + andl $0xaaaaaaaa, %edi
  22585. + xorl %edi, %edx
  22586. + xorl %edi, %esi
  22587. +
  22588. + rorl $3, %edx
  22589. + rorl $2, %esi
  22590. + movl %esi, 4(%ebx)
  22591. + movl 36(%esp), %esi
  22592. + movl %edx, (%ebx)
  22593. + movl 40(%esp), %edi
  22594. + movl 44(%esp), %eax
  22595. + movl $0, 8(%esp)
  22596. + movl %eax, 4(%esp)
  22597. + movl %ebx, (%esp)
  22598. + call des_encrypt2
  22599. + movl $1, 8(%esp)
  22600. + movl %edi, 4(%esp)
  22601. + movl %ebx, (%esp)
  22602. + call des_encrypt2
  22603. + movl $0, 8(%esp)
  22604. + movl %esi, 4(%esp)
  22605. + movl %ebx, (%esp)
  22606. + call des_encrypt2
  22607. + addl $12, %esp
  22608. + movl (%ebx), %edi
  22609. + movl 4(%ebx), %esi
  22610. +
  22611. +
  22612. + roll $2, %esi
  22613. + roll $3, %edi
  22614. + movl %edi, %eax
  22615. + xorl %esi, %edi
  22616. + andl $0xaaaaaaaa, %edi
  22617. + xorl %edi, %eax
  22618. + xorl %edi, %esi
  22619. +
  22620. + roll $23, %eax
  22621. + movl %eax, %edi
  22622. + xorl %esi, %eax
  22623. + andl $0x03fc03fc, %eax
  22624. + xorl %eax, %edi
  22625. + xorl %eax, %esi
  22626. +
  22627. + roll $10, %edi
  22628. + movl %edi, %eax
  22629. + xorl %esi, %edi
  22630. + andl $0x33333333, %edi
  22631. + xorl %edi, %eax
  22632. + xorl %edi, %esi
  22633. +
  22634. + roll $18, %esi
  22635. + movl %esi, %edi
  22636. + xorl %eax, %esi
  22637. + andl $0xfff0000f, %esi
  22638. + xorl %esi, %edi
  22639. + xorl %esi, %eax
  22640. +
  22641. + roll $12, %edi
  22642. + movl %edi, %esi
  22643. + xorl %eax, %edi
  22644. + andl $0xf0f0f0f0, %edi
  22645. + xorl %edi, %esi
  22646. + xorl %edi, %eax
  22647. +
  22648. + rorl $4, %eax
  22649. + movl %eax, (%ebx)
  22650. + movl %esi, 4(%ebx)
  22651. + popl %edi
  22652. + popl %esi
  22653. + popl %ebp
  22654. + popl %ebx
  22655. + ret
  22656. +.des_decrypt3_end:
  22657. + .size des_decrypt3 , .des_decrypt3_end-des_decrypt3
  22658. +.ident "desasm.pl"
  22659. +.text
  22660. + .align 16
  22661. +.globl des_ncbc_encrypt
  22662. + .type des_ncbc_encrypt , @function
  22663. +des_ncbc_encrypt:
  22664. +
  22665. + pushl %ebp
  22666. + pushl %ebx
  22667. + pushl %esi
  22668. + pushl %edi
  22669. + movl 28(%esp), %ebp
  22670. +
  22671. + movl 36(%esp), %ebx
  22672. + movl (%ebx), %esi
  22673. + movl 4(%ebx), %edi
  22674. + pushl %edi
  22675. + pushl %esi
  22676. + pushl %edi
  22677. + pushl %esi
  22678. + movl %esp, %ebx
  22679. + movl 36(%esp), %esi
  22680. + movl 40(%esp), %edi
  22681. +
  22682. + movl 56(%esp), %ecx
  22683. +
  22684. + pushl %ecx
  22685. +
  22686. + movl 52(%esp), %eax
  22687. + pushl %eax
  22688. + pushl %ebx
  22689. + cmpl $0, %ecx
  22690. + jz .L004decrypt
  22691. + andl $4294967288, %ebp
  22692. + movl 12(%esp), %eax
  22693. + movl 16(%esp), %ebx
  22694. + jz .L005encrypt_finish
  22695. +.L006encrypt_loop:
  22696. + movl (%esi), %ecx
  22697. + movl 4(%esi), %edx
  22698. + xorl %ecx, %eax
  22699. + xorl %edx, %ebx
  22700. + movl %eax, 12(%esp)
  22701. + movl %ebx, 16(%esp)
  22702. + call des_encrypt
  22703. + movl 12(%esp), %eax
  22704. + movl 16(%esp), %ebx
  22705. + movl %eax, (%edi)
  22706. + movl %ebx, 4(%edi)
  22707. + addl $8, %esi
  22708. + addl $8, %edi
  22709. + subl $8, %ebp
  22710. + jnz .L006encrypt_loop
  22711. +.L005encrypt_finish:
  22712. + movl 56(%esp), %ebp
  22713. + andl $7, %ebp
  22714. + jz .L007finish
  22715. + xorl %ecx, %ecx
  22716. + xorl %edx, %edx
  22717. + movl .L008cbc_enc_jmp_table(,%ebp,4),%ebp
  22718. + jmp *%ebp
  22719. +.L009ej7:
  22720. + movb 6(%esi), %dh
  22721. + sall $8, %edx
  22722. +.L010ej6:
  22723. + movb 5(%esi), %dh
  22724. +.L011ej5:
  22725. + movb 4(%esi), %dl
  22726. +.L012ej4:
  22727. + movl (%esi), %ecx
  22728. + jmp .L013ejend
  22729. +.L014ej3:
  22730. + movb 2(%esi), %ch
  22731. + sall $8, %ecx
  22732. +.L015ej2:
  22733. + movb 1(%esi), %ch
  22734. +.L016ej1:
  22735. + movb (%esi), %cl
  22736. +.L013ejend:
  22737. + xorl %ecx, %eax
  22738. + xorl %edx, %ebx
  22739. + movl %eax, 12(%esp)
  22740. + movl %ebx, 16(%esp)
  22741. + call des_encrypt
  22742. + movl 12(%esp), %eax
  22743. + movl 16(%esp), %ebx
  22744. + movl %eax, (%edi)
  22745. + movl %ebx, 4(%edi)
  22746. + jmp .L007finish
  22747. +.align 16
  22748. +.L004decrypt:
  22749. + andl $4294967288, %ebp
  22750. + movl 20(%esp), %eax
  22751. + movl 24(%esp), %ebx
  22752. + jz .L017decrypt_finish
  22753. +.L018decrypt_loop:
  22754. + movl (%esi), %eax
  22755. + movl 4(%esi), %ebx
  22756. + movl %eax, 12(%esp)
  22757. + movl %ebx, 16(%esp)
  22758. + call des_encrypt
  22759. + movl 12(%esp), %eax
  22760. + movl 16(%esp), %ebx
  22761. + movl 20(%esp), %ecx
  22762. + movl 24(%esp), %edx
  22763. + xorl %eax, %ecx
  22764. + xorl %ebx, %edx
  22765. + movl (%esi), %eax
  22766. + movl 4(%esi), %ebx
  22767. + movl %ecx, (%edi)
  22768. + movl %edx, 4(%edi)
  22769. + movl %eax, 20(%esp)
  22770. + movl %ebx, 24(%esp)
  22771. + addl $8, %esi
  22772. + addl $8, %edi
  22773. + subl $8, %ebp
  22774. + jnz .L018decrypt_loop
  22775. +.L017decrypt_finish:
  22776. + movl 56(%esp), %ebp
  22777. + andl $7, %ebp
  22778. + jz .L007finish
  22779. + movl (%esi), %eax
  22780. + movl 4(%esi), %ebx
  22781. + movl %eax, 12(%esp)
  22782. + movl %ebx, 16(%esp)
  22783. + call des_encrypt
  22784. + movl 12(%esp), %eax
  22785. + movl 16(%esp), %ebx
  22786. + movl 20(%esp), %ecx
  22787. + movl 24(%esp), %edx
  22788. + xorl %eax, %ecx
  22789. + xorl %ebx, %edx
  22790. + movl (%esi), %eax
  22791. + movl 4(%esi), %ebx
  22792. +.L019dj7:
  22793. + rorl $16, %edx
  22794. + movb %dl, 6(%edi)
  22795. + shrl $16, %edx
  22796. +.L020dj6:
  22797. + movb %dh, 5(%edi)
  22798. +.L021dj5:
  22799. + movb %dl, 4(%edi)
  22800. +.L022dj4:
  22801. + movl %ecx, (%edi)
  22802. + jmp .L023djend
  22803. +.L024dj3:
  22804. + rorl $16, %ecx
  22805. + movb %cl, 2(%edi)
  22806. + sall $16, %ecx
  22807. +.L025dj2:
  22808. + movb %ch, 1(%esi)
  22809. +.L026dj1:
  22810. + movb %cl, (%esi)
  22811. +.L023djend:
  22812. + jmp .L007finish
  22813. +.align 16
  22814. +.L007finish:
  22815. + movl 64(%esp), %ecx
  22816. + addl $28, %esp
  22817. + movl %eax, (%ecx)
  22818. + movl %ebx, 4(%ecx)
  22819. + popl %edi
  22820. + popl %esi
  22821. + popl %ebx
  22822. + popl %ebp
  22823. + ret
  22824. +.align 16
  22825. +.L008cbc_enc_jmp_table:
  22826. + .long 0
  22827. + .long .L016ej1
  22828. + .long .L015ej2
  22829. + .long .L014ej3
  22830. + .long .L012ej4
  22831. + .long .L011ej5
  22832. + .long .L010ej6
  22833. + .long .L009ej7
  22834. +.align 16
  22835. +.L027cbc_dec_jmp_table:
  22836. + .long 0
  22837. + .long .L026dj1
  22838. + .long .L025dj2
  22839. + .long .L024dj3
  22840. + .long .L022dj4
  22841. + .long .L021dj5
  22842. + .long .L020dj6
  22843. + .long .L019dj7
  22844. +.des_ncbc_encrypt_end:
  22845. + .size des_ncbc_encrypt , .des_ncbc_encrypt_end-des_ncbc_encrypt
  22846. +.ident "desasm.pl"
  22847. +.text
  22848. + .align 16
  22849. +.globl des_ede3_cbc_encrypt
  22850. + .type des_ede3_cbc_encrypt , @function
  22851. +des_ede3_cbc_encrypt:
  22852. +
  22853. + pushl %ebp
  22854. + pushl %ebx
  22855. + pushl %esi
  22856. + pushl %edi
  22857. + movl 28(%esp), %ebp
  22858. +
  22859. + movl 44(%esp), %ebx
  22860. + movl (%ebx), %esi
  22861. + movl 4(%ebx), %edi
  22862. + pushl %edi
  22863. + pushl %esi
  22864. + pushl %edi
  22865. + pushl %esi
  22866. + movl %esp, %ebx
  22867. + movl 36(%esp), %esi
  22868. + movl 40(%esp), %edi
  22869. +
  22870. + movl 64(%esp), %ecx
  22871. +
  22872. + movl 56(%esp), %eax
  22873. + pushl %eax
  22874. +
  22875. + movl 56(%esp), %eax
  22876. + pushl %eax
  22877. +
  22878. + movl 56(%esp), %eax
  22879. + pushl %eax
  22880. + pushl %ebx
  22881. + cmpl $0, %ecx
  22882. + jz .L028decrypt
  22883. + andl $4294967288, %ebp
  22884. + movl 16(%esp), %eax
  22885. + movl 20(%esp), %ebx
  22886. + jz .L029encrypt_finish
  22887. +.L030encrypt_loop:
  22888. + movl (%esi), %ecx
  22889. + movl 4(%esi), %edx
  22890. + xorl %ecx, %eax
  22891. + xorl %edx, %ebx
  22892. + movl %eax, 16(%esp)
  22893. + movl %ebx, 20(%esp)
  22894. + call des_encrypt3
  22895. + movl 16(%esp), %eax
  22896. + movl 20(%esp), %ebx
  22897. + movl %eax, (%edi)
  22898. + movl %ebx, 4(%edi)
  22899. + addl $8, %esi
  22900. + addl $8, %edi
  22901. + subl $8, %ebp
  22902. + jnz .L030encrypt_loop
  22903. +.L029encrypt_finish:
  22904. + movl 60(%esp), %ebp
  22905. + andl $7, %ebp
  22906. + jz .L031finish
  22907. + xorl %ecx, %ecx
  22908. + xorl %edx, %edx
  22909. + movl .L032cbc_enc_jmp_table(,%ebp,4),%ebp
  22910. + jmp *%ebp
  22911. +.L033ej7:
  22912. + movb 6(%esi), %dh
  22913. + sall $8, %edx
  22914. +.L034ej6:
  22915. + movb 5(%esi), %dh
  22916. +.L035ej5:
  22917. + movb 4(%esi), %dl
  22918. +.L036ej4:
  22919. + movl (%esi), %ecx
  22920. + jmp .L037ejend
  22921. +.L038ej3:
  22922. + movb 2(%esi), %ch
  22923. + sall $8, %ecx
  22924. +.L039ej2:
  22925. + movb 1(%esi), %ch
  22926. +.L040ej1:
  22927. + movb (%esi), %cl
  22928. +.L037ejend:
  22929. + xorl %ecx, %eax
  22930. + xorl %edx, %ebx
  22931. + movl %eax, 16(%esp)
  22932. + movl %ebx, 20(%esp)
  22933. + call des_encrypt3
  22934. + movl 16(%esp), %eax
  22935. + movl 20(%esp), %ebx
  22936. + movl %eax, (%edi)
  22937. + movl %ebx, 4(%edi)
  22938. + jmp .L031finish
  22939. +.align 16
  22940. +.L028decrypt:
  22941. + andl $4294967288, %ebp
  22942. + movl 24(%esp), %eax
  22943. + movl 28(%esp), %ebx
  22944. + jz .L041decrypt_finish
  22945. +.L042decrypt_loop:
  22946. + movl (%esi), %eax
  22947. + movl 4(%esi), %ebx
  22948. + movl %eax, 16(%esp)
  22949. + movl %ebx, 20(%esp)
  22950. + call des_decrypt3
  22951. + movl 16(%esp), %eax
  22952. + movl 20(%esp), %ebx
  22953. + movl 24(%esp), %ecx
  22954. + movl 28(%esp), %edx
  22955. + xorl %eax, %ecx
  22956. + xorl %ebx, %edx
  22957. + movl (%esi), %eax
  22958. + movl 4(%esi), %ebx
  22959. + movl %ecx, (%edi)
  22960. + movl %edx, 4(%edi)
  22961. + movl %eax, 24(%esp)
  22962. + movl %ebx, 28(%esp)
  22963. + addl $8, %esi
  22964. + addl $8, %edi
  22965. + subl $8, %ebp
  22966. + jnz .L042decrypt_loop
  22967. +.L041decrypt_finish:
  22968. + movl 60(%esp), %ebp
  22969. + andl $7, %ebp
  22970. + jz .L031finish
  22971. + movl (%esi), %eax
  22972. + movl 4(%esi), %ebx
  22973. + movl %eax, 16(%esp)
  22974. + movl %ebx, 20(%esp)
  22975. + call des_decrypt3
  22976. + movl 16(%esp), %eax
  22977. + movl 20(%esp), %ebx
  22978. + movl 24(%esp), %ecx
  22979. + movl 28(%esp), %edx
  22980. + xorl %eax, %ecx
  22981. + xorl %ebx, %edx
  22982. + movl (%esi), %eax
  22983. + movl 4(%esi), %ebx
  22984. +.L043dj7:
  22985. + rorl $16, %edx
  22986. + movb %dl, 6(%edi)
  22987. + shrl $16, %edx
  22988. +.L044dj6:
  22989. + movb %dh, 5(%edi)
  22990. +.L045dj5:
  22991. + movb %dl, 4(%edi)
  22992. +.L046dj4:
  22993. + movl %ecx, (%edi)
  22994. + jmp .L047djend
  22995. +.L048dj3:
  22996. + rorl $16, %ecx
  22997. + movb %cl, 2(%edi)
  22998. + sall $16, %ecx
  22999. +.L049dj2:
  23000. + movb %ch, 1(%esi)
  23001. +.L050dj1:
  23002. + movb %cl, (%esi)
  23003. +.L047djend:
  23004. + jmp .L031finish
  23005. +.align 16
  23006. +.L031finish:
  23007. + movl 76(%esp), %ecx
  23008. + addl $32, %esp
  23009. + movl %eax, (%ecx)
  23010. + movl %ebx, 4(%ecx)
  23011. + popl %edi
  23012. + popl %esi
  23013. + popl %ebx
  23014. + popl %ebp
  23015. + ret
  23016. +.align 16
  23017. +.L032cbc_enc_jmp_table:
  23018. + .long 0
  23019. + .long .L040ej1
  23020. + .long .L039ej2
  23021. + .long .L038ej3
  23022. + .long .L036ej4
  23023. + .long .L035ej5
  23024. + .long .L034ej6
  23025. + .long .L033ej7
  23026. +.align 16
  23027. +.L051cbc_dec_jmp_table:
  23028. + .long 0
  23029. + .long .L050dj1
  23030. + .long .L049dj2
  23031. + .long .L048dj3
  23032. + .long .L046dj4
  23033. + .long .L045dj5
  23034. + .long .L044dj6
  23035. + .long .L043dj7
  23036. +.des_ede3_cbc_encrypt_end:
  23037. + .size des_ede3_cbc_encrypt , .des_ede3_cbc_encrypt_end-des_ede3_cbc_encrypt
  23038. +.ident "desasm.pl"
  23039. --- /dev/null Tue Mar 11 13:02:56 2003
  23040. +++ linux/net/ipsec/des/ecb_enc.c Mon Feb 9 13:51:03 2004
  23041. @@ -0,0 +1,128 @@
  23042. +/* crypto/des/ecb_enc.c */
  23043. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  23044. + * All rights reserved.
  23045. + *
  23046. + * This package is an SSL implementation written
  23047. + * by Eric Young (eay@cryptsoft.com).
  23048. + * The implementation was written so as to conform with Netscapes SSL.
  23049. + *
  23050. + * This library is free for commercial and non-commercial use as long as
  23051. + * the following conditions are aheared to. The following conditions
  23052. + * apply to all code found in this distribution, be it the RC4, RSA,
  23053. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  23054. + * included with this distribution is covered by the same copyright terms
  23055. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  23056. + *
  23057. + * Copyright remains Eric Young's, and as such any Copyright notices in
  23058. + * the code are not to be removed.
  23059. + * If this package is used in a product, Eric Young should be given attribution
  23060. + * as the author of the parts of the library used.
  23061. + * This can be in the form of a textual message at program startup or
  23062. + * in documentation (online or textual) provided with the package.
  23063. + *
  23064. + * Redistribution and use in source and binary forms, with or without
  23065. + * modification, are permitted provided that the following conditions
  23066. + * are met:
  23067. + * 1. Redistributions of source code must retain the copyright
  23068. + * notice, this list of conditions and the following disclaimer.
  23069. + * 2. Redistributions in binary form must reproduce the above copyright
  23070. + * notice, this list of conditions and the following disclaimer in the
  23071. + * documentation and/or other materials provided with the distribution.
  23072. + * 3. All advertising materials mentioning features or use of this software
  23073. + * must display the following acknowledgement:
  23074. + * "This product includes cryptographic software written by
  23075. + * Eric Young (eay@cryptsoft.com)"
  23076. + * The word 'cryptographic' can be left out if the rouines from the library
  23077. + * being used are not cryptographic related :-).
  23078. + * 4. If you include any Windows specific code (or a derivative thereof) from
  23079. + * the apps directory (application code) you must include an acknowledgement:
  23080. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  23081. + *
  23082. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  23083. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23084. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23085. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23086. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23087. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23088. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23089. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23090. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23091. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23092. + * SUCH DAMAGE.
  23093. + *
  23094. + * The licence and distribution terms for any publically available version or
  23095. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  23096. + * copied and put under another distribution licence
  23097. + * [including the GNU Public Licence.]
  23098. + */
  23099. +
  23100. +#include "des/des_locl.h"
  23101. +#include "des/spr.h"
  23102. +
  23103. +char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay";
  23104. +char *DES_version="DES part of SSLeay 0.8.2b 08-Jan-1998";
  23105. +
  23106. +/* RCSID $Id: ecb_enc.c,v 1.8 2004/08/04 15:57:22 mcr Exp $ */
  23107. +/* This function ifdef'ed out for FreeS/WAN project. */
  23108. +#ifdef notdef
  23109. +char *des_options()
  23110. + {
  23111. + static int init=1;
  23112. + static char buf[32];
  23113. +
  23114. + if (init)
  23115. + {
  23116. + char *ptr,*unroll,*risc,*size;
  23117. +
  23118. + init=0;
  23119. +#ifdef DES_PTR
  23120. + ptr="ptr";
  23121. +#else
  23122. + ptr="idx";
  23123. +#endif
  23124. +#if defined(DES_RISC1) || defined(DES_RISC2)
  23125. +#ifdef DES_RISC1
  23126. + risc="risc1";
  23127. +#endif
  23128. +#ifdef DES_RISC2
  23129. + risc="risc2";
  23130. +#endif
  23131. +#else
  23132. + risc="cisc";
  23133. +#endif
  23134. +#ifdef DES_UNROLL
  23135. + unroll="16";
  23136. +#else
  23137. + unroll="4";
  23138. +#endif
  23139. + if (sizeof(DES_LONG) != sizeof(long))
  23140. + size="int";
  23141. + else
  23142. + size="long";
  23143. + sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
  23144. + }
  23145. + return(buf);
  23146. + }
  23147. +#endif
  23148. +
  23149. +
  23150. +void des_ecb_encrypt(input, output, ks, enc)
  23151. +des_cblock (*input);
  23152. +des_cblock (*output);
  23153. +des_key_schedule ks;
  23154. +int enc;
  23155. + {
  23156. + register DES_LONG l;
  23157. + register unsigned char *in,*out;
  23158. + DES_LONG ll[2];
  23159. +
  23160. + in=(unsigned char *)input;
  23161. + out=(unsigned char *)output;
  23162. + c2l(in,l); ll[0]=l;
  23163. + c2l(in,l); ll[1]=l;
  23164. + des_encrypt(ll,ks,enc);
  23165. + l=ll[0]; l2c(l,out);
  23166. + l=ll[1]; l2c(l,out);
  23167. + l=ll[0]=ll[1]=0;
  23168. + }
  23169. +
  23170. --- /dev/null Tue Mar 11 13:02:56 2003
  23171. +++ linux/net/ipsec/des/ipsec_alg_3des.c Mon Feb 9 13:51:03 2004
  23172. @@ -0,0 +1,181 @@
  23173. +/*
  23174. + * ipsec_alg 3DES cipher stubs
  23175. + *
  23176. + * Copyright (C) 2005 Michael Richardson <mcr@xelerance.com>
  23177. + *
  23178. + * Adapted from ipsec_alg_aes.c by JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  23179. + *
  23180. + * ipsec_alg_aes.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  23181. + *
  23182. + * This program is free software; you can redistribute it and/or modify it
  23183. + * under the terms of the GNU General Public License as published by the
  23184. + * Free Software Foundation; either version 2 of the License, or (at your
  23185. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  23186. + *
  23187. + * This program is distributed in the hope that it will be useful, but
  23188. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23189. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23190. + * for more details.
  23191. + *
  23192. + */
  23193. +#ifndef AUTOCONF_INCLUDED
  23194. +#include <linux/config.h>
  23195. +#endif
  23196. +#include <linux/version.h>
  23197. +
  23198. +/*
  23199. + * special case: ipsec core modular with this static algo inside:
  23200. + * must avoid MODULE magic for this file
  23201. + */
  23202. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_3DES)
  23203. +#undef MODULE
  23204. +#endif
  23205. +
  23206. +#include <linux/module.h>
  23207. +#include <linux/init.h>
  23208. +
  23209. +#include <linux/kernel.h> /* printk() */
  23210. +#include <linux/errno.h> /* error codes */
  23211. +#include <linux/types.h> /* size_t */
  23212. +#include <linux/string.h>
  23213. +
  23214. +/* Low freeswan header coupling */
  23215. +#include "openswan/ipsec_xform.h"
  23216. +#include "openswan/ipsec_alg.h"
  23217. +#include "crypto/des.h"
  23218. +#include "openswan/ipsec_alg_3des.h"
  23219. +
  23220. +#define AES_CONTEXT_T aes_context
  23221. +static int debug_3des=0;
  23222. +static int test_3des=0;
  23223. +static int excl_3des=0;
  23224. +
  23225. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23226. +MODULE_AUTHOR("Michael Richardson <mcr@xelerance.com>");
  23227. +#ifdef module_param
  23228. +module_param(debug_3des,int,0600)
  23229. +module_param(test_des,int,0600)
  23230. +module_param(excl_des,int,0600)
  23231. +#else
  23232. +MODULE_PARM(debug_3des, "i");
  23233. +MODULE_PARM(test_des, "i");
  23234. +MODULE_PARM(excl_des, "i");
  23235. +#endif
  23236. +#endif
  23237. +
  23238. +#define ESP_AES_MAC_KEY_SZ 16 /* 128 bit MAC key */
  23239. +#define ESP_AES_MAC_BLK_LEN 16 /* 128 bit block */
  23240. +
  23241. +static int _3des_set_key(struct ipsec_alg_enc *alg,
  23242. + __u8 * key_e, const __u8 * key,
  23243. + size_t keysize)
  23244. +{
  23245. + int ret = 0;
  23246. + TripleDES_context *ctx = (TripleDES_context*)key_e;
  23247. +
  23248. + if(keysize != 192/8) {
  23249. + return EINVAL;
  23250. + }
  23251. +
  23252. + des_set_key((des_cblock *)(key + DES_KEY_SZ*0), ctx->s1);
  23253. + des_set_key((des_cblock *)(key + DES_KEY_SZ*1), ctx->s2);
  23254. + des_set_key((des_cblock *)(key + DES_KEY_SZ*2), ctx->s3);
  23255. +
  23256. + if (debug_3des > 0)
  23257. + printk(KERN_DEBUG "klips_debug:_3des_set_key:"
  23258. + "ret=%d key_e=%p key=%p keysize=%ld\n",
  23259. + ret, key_e, key, (unsigned long int) keysize);
  23260. + return ret;
  23261. +}
  23262. +
  23263. +static int _3des_cbc_encrypt(struct ipsec_alg_enc *alg,
  23264. + __u8 * key_e,
  23265. + __u8 * in,
  23266. + int ilen, const __u8 * iv,
  23267. + int encrypt)
  23268. +{
  23269. + TripleDES_context *ctx=(TripleDES_context*)key_e;
  23270. + des_cblock miv;
  23271. +
  23272. + memcpy(&miv, iv, sizeof(miv));
  23273. +
  23274. + if (debug_3des > 0)
  23275. + printk(KERN_DEBUG "klips_debug:_aes_cbc_encrypt:"
  23276. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  23277. + key_e, in, ilen, iv, encrypt);
  23278. +
  23279. + des_ede3_cbc_encrypt((des_cblock *)in,
  23280. + (des_cblock *)in,
  23281. + ilen,
  23282. + ctx->s1,
  23283. + ctx->s2,
  23284. + ctx->s3,
  23285. + &miv, encrypt);
  23286. + return 1;
  23287. +}
  23288. +
  23289. +static struct ipsec_alg_enc ipsec_alg_3DES = {
  23290. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  23291. + ixt_refcnt: ATOMIC_INIT(0),
  23292. + ixt_name: "3des",
  23293. + ixt_blocksize: ESP_3DES_CBC_BLK_LEN,
  23294. + ixt_support: {
  23295. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  23296. + ias_id: ESP_3DES,
  23297. + ias_keyminbits: ESP_3DES_KEY_SZ*8,
  23298. + ias_keymaxbits: ESP_3DES_KEY_SZ*8,
  23299. + },
  23300. + },
  23301. +#if defined(MODULE_KLIPS_ENC_3DES_MODULE)
  23302. + ixt_module: THIS_MODULE,
  23303. +#endif
  23304. + ixt_e_keylen: ESP_3DES_KEY_SZ*8,
  23305. + ixt_e_ctx_size: sizeof(TripleDES_context),
  23306. + ixt_e_set_key: _3des_set_key,
  23307. + ixt_e_cbc_encrypt:_3des_cbc_encrypt,
  23308. +};
  23309. +
  23310. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23311. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_3des_init )
  23312. +#else
  23313. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_3des_init )
  23314. +#endif
  23315. +{
  23316. + int ret, test_ret;
  23317. +
  23318. + if (excl_3des) ipsec_alg_3DES.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  23319. + ret=register_ipsec_alg_enc(&ipsec_alg_3DES);
  23320. + printk("ipsec_3des_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  23321. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23322. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23323. + ipsec_alg_3DES.ixt_common.ixt_name,
  23324. + ret);
  23325. + if (ret==0 && test_3des) {
  23326. + test_ret=ipsec_alg_test(
  23327. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23328. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23329. + test_3des);
  23330. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  23331. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23332. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23333. + test_ret);
  23334. + }
  23335. + return ret;
  23336. +}
  23337. +
  23338. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23339. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_3des_fini )
  23340. +#else
  23341. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_3des_fini )
  23342. +#endif
  23343. +{
  23344. + unregister_ipsec_alg_enc(&ipsec_alg_3DES);
  23345. + return;
  23346. +}
  23347. +
  23348. +/* Dual, because 3des code is 4-clause BSD licensed */
  23349. +#ifdef MODULE_LICENSE
  23350. +MODULE_LICENSE("Dual BSD/GPL");
  23351. +#endif
  23352. +
  23353. +
  23354. --- /dev/null Tue Mar 11 13:02:56 2003
  23355. +++ linux/net/ipsec/des/set_key.c Mon Feb 9 13:51:03 2004
  23356. @@ -0,0 +1,246 @@
  23357. +/* crypto/des/set_key.c */
  23358. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  23359. + * All rights reserved.
  23360. + *
  23361. + * This package is an SSL implementation written
  23362. + * by Eric Young (eay@cryptsoft.com).
  23363. + * The implementation was written so as to conform with Netscapes SSL.
  23364. + *
  23365. + * This library is free for commercial and non-commercial use as long as
  23366. + * the following conditions are aheared to. The following conditions
  23367. + * apply to all code found in this distribution, be it the RC4, RSA,
  23368. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  23369. + * included with this distribution is covered by the same copyright terms
  23370. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  23371. + *
  23372. + * Copyright remains Eric Young's, and as such any Copyright notices in
  23373. + * the code are not to be removed.
  23374. + * If this package is used in a product, Eric Young should be given attribution
  23375. + * as the author of the parts of the library used.
  23376. + * This can be in the form of a textual message at program startup or
  23377. + * in documentation (online or textual) provided with the package.
  23378. + *
  23379. + * Redistribution and use in source and binary forms, with or without
  23380. + * modification, are permitted provided that the following conditions
  23381. + * are met:
  23382. + * 1. Redistributions of source code must retain the copyright
  23383. + * notice, this list of conditions and the following disclaimer.
  23384. + * 2. Redistributions in binary form must reproduce the above copyright
  23385. + * notice, this list of conditions and the following disclaimer in the
  23386. + * documentation and/or other materials provided with the distribution.
  23387. + * 3. All advertising materials mentioning features or use of this software
  23388. + * must display the following acknowledgement:
  23389. + * "This product includes cryptographic software written by
  23390. + * Eric Young (eay@cryptsoft.com)"
  23391. + * The word 'cryptographic' can be left out if the rouines from the library
  23392. + * being used are not cryptographic related :-).
  23393. + * 4. If you include any Windows specific code (or a derivative thereof) from
  23394. + * the apps directory (application code) you must include an acknowledgement:
  23395. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  23396. + *
  23397. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  23398. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23399. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23400. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23401. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23402. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23403. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23404. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23405. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23406. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23407. + * SUCH DAMAGE.
  23408. + *
  23409. + * The licence and distribution terms for any publically available version or
  23410. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  23411. + * copied and put under another distribution licence
  23412. + * [including the GNU Public Licence.]
  23413. + */
  23414. +
  23415. +/* set_key.c v 1.4 eay 24/9/91
  23416. + * 1.4 Speed up by 400% :-)
  23417. + * 1.3 added register declarations.
  23418. + * 1.2 unrolled make_key_sched a bit more
  23419. + * 1.1 added norm_expand_bits
  23420. + * 1.0 First working version
  23421. + */
  23422. +#include "des/des_locl.h"
  23423. +#include "des/podd.h"
  23424. +#include "des/sk.h"
  23425. +
  23426. +#ifndef NOPROTO
  23427. +static int check_parity(des_cblock (*key));
  23428. +#else
  23429. +static int check_parity();
  23430. +#endif
  23431. +
  23432. +int des_check_key=0;
  23433. +
  23434. +void des_set_odd_parity(key)
  23435. +des_cblock (*key);
  23436. + {
  23437. + int i;
  23438. +
  23439. + for (i=0; i<DES_KEY_SZ; i++)
  23440. + (*key)[i]=odd_parity[(*key)[i]];
  23441. + }
  23442. +
  23443. +static int check_parity(key)
  23444. +des_cblock (*key);
  23445. + {
  23446. + int i;
  23447. +
  23448. + for (i=0; i<DES_KEY_SZ; i++)
  23449. + {
  23450. + if ((*key)[i] != odd_parity[(*key)[i]])
  23451. + return(0);
  23452. + }
  23453. + return(1);
  23454. + }
  23455. +
  23456. +/* Weak and semi week keys as take from
  23457. + * %A D.W. Davies
  23458. + * %A W.L. Price
  23459. + * %T Security for Computer Networks
  23460. + * %I John Wiley & Sons
  23461. + * %D 1984
  23462. + * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
  23463. + * (and actual cblock values).
  23464. + */
  23465. +#define NUM_WEAK_KEY 16
  23466. +static des_cblock weak_keys[NUM_WEAK_KEY]={
  23467. + /* weak keys */
  23468. + {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  23469. + {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
  23470. + {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
  23471. + {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
  23472. + /* semi-weak keys */
  23473. + {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
  23474. + {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
  23475. + {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
  23476. + {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
  23477. + {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
  23478. + {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
  23479. + {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
  23480. + {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
  23481. + {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
  23482. + {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
  23483. + {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  23484. + {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
  23485. +
  23486. +int des_is_weak_key(key)
  23487. +des_cblock (*key);
  23488. + {
  23489. + int i;
  23490. +
  23491. + for (i=0; i<NUM_WEAK_KEY; i++)
  23492. + /* Added == 0 to comparision, I obviously don't run
  23493. + * this section very often :-(, thanks to
  23494. + * engineering@MorningStar.Com for the fix
  23495. + * eay 93/06/29
  23496. + * Another problem, I was comparing only the first 4
  23497. + * bytes, 97/03/18 */
  23498. + if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
  23499. + return(0);
  23500. + }
  23501. +
  23502. +/* NOW DEFINED IN des_local.h
  23503. + * See ecb_encrypt.c for a pseudo description of these macros.
  23504. + * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  23505. + * (b)^=(t),\
  23506. + * (a)=((a)^((t)<<(n))))
  23507. + */
  23508. +
  23509. +#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
  23510. + (a)=(a)^(t)^(t>>(16-(n))))
  23511. +
  23512. +/* return 0 if key parity is odd (correct),
  23513. + * return -1 if key parity error,
  23514. + * return -2 if illegal weak key.
  23515. + */
  23516. +int des_set_key(key, schedule)
  23517. +des_cblock (*key);
  23518. +des_key_schedule schedule;
  23519. + {
  23520. + static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
  23521. + register DES_LONG c,d,t,s,t2;
  23522. + register unsigned char *in;
  23523. + register DES_LONG *k;
  23524. + register int i;
  23525. +
  23526. + if (des_check_key)
  23527. + {
  23528. + if (!check_parity(key))
  23529. + return(-1);
  23530. +
  23531. + if (des_is_weak_key(key))
  23532. + return(-2);
  23533. + }
  23534. +
  23535. + k=(DES_LONG *)schedule;
  23536. + in=(unsigned char *)key;
  23537. +
  23538. + c2l(in,c);
  23539. + c2l(in,d);
  23540. +
  23541. + /* do PC1 in 60 simple operations */
  23542. +/* PERM_OP(d,c,t,4,0x0f0f0f0fL);
  23543. + HPERM_OP(c,t,-2, 0xcccc0000L);
  23544. + HPERM_OP(c,t,-1, 0xaaaa0000L);
  23545. + HPERM_OP(c,t, 8, 0x00ff0000L);
  23546. + HPERM_OP(c,t,-1, 0xaaaa0000L);
  23547. + HPERM_OP(d,t,-8, 0xff000000L);
  23548. + HPERM_OP(d,t, 8, 0x00ff0000L);
  23549. + HPERM_OP(d,t, 2, 0x33330000L);
  23550. + d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
  23551. + d=(d>>8)|((c&0xf0000000L)>>4);
  23552. + c&=0x0fffffffL; */
  23553. +
  23554. + /* I now do it in 47 simple operations :-)
  23555. + * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  23556. + * for the inspiration. :-) */
  23557. + PERM_OP (d,c,t,4,0x0f0f0f0fL);
  23558. + HPERM_OP(c,t,-2,0xcccc0000L);
  23559. + HPERM_OP(d,t,-2,0xcccc0000L);
  23560. + PERM_OP (d,c,t,1,0x55555555L);
  23561. + PERM_OP (c,d,t,8,0x00ff00ffL);
  23562. + PERM_OP (d,c,t,1,0x55555555L);
  23563. + d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
  23564. + ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
  23565. + c&=0x0fffffffL;
  23566. +
  23567. + for (i=0; i<ITERATIONS; i++)
  23568. + {
  23569. + if (shifts2[i])
  23570. + { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
  23571. + else
  23572. + { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
  23573. + c&=0x0fffffffL;
  23574. + d&=0x0fffffffL;
  23575. + /* could be a few less shifts but I am to lazy at this
  23576. + * point in time to investigate */
  23577. + s= des_skb[0][ (c )&0x3f ]|
  23578. + des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
  23579. + des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
  23580. + des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
  23581. + ((c>>22L)&0x38)];
  23582. + t= des_skb[4][ (d )&0x3f ]|
  23583. + des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
  23584. + des_skb[6][ (d>>15L)&0x3f ]|
  23585. + des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
  23586. +
  23587. + /* table contained 0213 4657 */
  23588. + t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
  23589. + *(k++)=ROTATE(t2,30)&0xffffffffL;
  23590. +
  23591. + t2=((s>>16L)|(t&0xffff0000L));
  23592. + *(k++)=ROTATE(t2,26)&0xffffffffL;
  23593. + }
  23594. + return(0);
  23595. + }
  23596. +
  23597. +int des_key_sched(key, schedule)
  23598. +des_cblock (*key);
  23599. +des_key_schedule schedule;
  23600. + {
  23601. + return(des_set_key(key,schedule));
  23602. + }
  23603. --- /dev/null Tue Mar 11 13:02:56 2003
  23604. +++ linux/net/ipsec/goodmask.c Mon Feb 9 13:51:03 2004
  23605. @@ -0,0 +1,100 @@
  23606. +/*
  23607. + * minor utilities for subnet-mask manipulation
  23608. + * Copyright (C) 1998, 1999 Henry Spencer.
  23609. + *
  23610. + * This library is free software; you can redistribute it and/or modify it
  23611. + * under the terms of the GNU Library General Public License as published by
  23612. + * the Free Software Foundation; either version 2 of the License, or (at your
  23613. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  23614. + *
  23615. + * This library is distributed in the hope that it will be useful, but
  23616. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23617. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  23618. + * License for more details.
  23619. + *
  23620. + * RCSID $Id: goodmask.c,v 1.12 2004/07/10 07:43:47 mcr Exp $
  23621. + */
  23622. +#include "openswan.h"
  23623. +
  23624. +#ifndef ABITS
  23625. +#define ABITS 32 /* bits in an IPv4 address */
  23626. +#endif
  23627. +
  23628. +/*
  23629. + - goodmask - is this a good (^1*0*$) subnet mask?
  23630. + * You are not expected to understand this. See Henry S. Warren Jr,
  23631. + * "Functions realizable with word-parallel logical and two's-complement
  23632. + * addition instructions", CACM 20.6 (June 1977), p.439.
  23633. + */
  23634. +int /* predicate */
  23635. +goodmask(mask)
  23636. +struct in_addr mask;
  23637. +{
  23638. + unsigned long x = ntohl(mask.s_addr);
  23639. + /* clear rightmost contiguous string of 1-bits */
  23640. +# define CRCS1B(x) (((x|(x-1))+1)&x)
  23641. +# define TOPBIT (1UL << 31)
  23642. +
  23643. + /* either zero, or has one string of 1-bits which is left-justified */
  23644. + if (x == 0 || (CRCS1B(x) == 0 && (x&TOPBIT)))
  23645. + return 1;
  23646. + return 0;
  23647. +}
  23648. +
  23649. +/*
  23650. + - masktobits - how many bits in this mask?
  23651. + * The algorithm is essentially a binary search, but highly optimized
  23652. + * for this particular task.
  23653. + */
  23654. +int /* -1 means !goodmask() */
  23655. +masktobits(mask)
  23656. +struct in_addr mask;
  23657. +{
  23658. + unsigned long m = ntohl(mask.s_addr);
  23659. + int masklen;
  23660. +
  23661. + if (!goodmask(mask))
  23662. + return -1;
  23663. +
  23664. + if (m&0x00000001UL)
  23665. + return 32;
  23666. + masklen = 0;
  23667. + if (m&(0x0000ffffUL<<1)) { /* <<1 for 1-origin numbering */
  23668. + masklen |= 0x10;
  23669. + m <<= 16;
  23670. + }
  23671. + if (m&(0x00ff0000UL<<1)) {
  23672. + masklen |= 0x08;
  23673. + m <<= 8;
  23674. + }
  23675. + if (m&(0x0f000000UL<<1)) {
  23676. + masklen |= 0x04;
  23677. + m <<= 4;
  23678. + }
  23679. + if (m&(0x30000000UL<<1)) {
  23680. + masklen |= 0x02;
  23681. + m <<= 2;
  23682. + }
  23683. + if (m&(0x40000000UL<<1))
  23684. + masklen |= 0x01;
  23685. +
  23686. + return masklen;
  23687. +}
  23688. +
  23689. +/*
  23690. + - bitstomask - return a mask with this many high bits on
  23691. + */
  23692. +struct in_addr
  23693. +bitstomask(n)
  23694. +int n;
  23695. +{
  23696. + struct in_addr result;
  23697. +
  23698. + if (n > 0 && n <= ABITS)
  23699. + result.s_addr = htonl(~((1UL << (ABITS - n)) - 1));
  23700. + else if (n == 0)
  23701. + result.s_addr = 0;
  23702. + else
  23703. + result.s_addr = 0; /* best error report we can do */
  23704. + return result;
  23705. +}
  23706. --- /dev/null Tue Mar 11 13:02:56 2003
  23707. +++ linux/net/ipsec/infblock.c Mon Feb 9 13:51:03 2004
  23708. @@ -0,0 +1,403 @@
  23709. +/* infblock.c -- interpret and process block types to last block
  23710. + * Copyright (C) 1995-2002 Mark Adler
  23711. + * For conditions of distribution and use, see copyright notice in zlib.h
  23712. + */
  23713. +
  23714. +#include <zlib/zutil.h>
  23715. +#include "infblock.h"
  23716. +#include "inftrees.h"
  23717. +#include "infcodes.h"
  23718. +#include "infutil.h"
  23719. +
  23720. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  23721. +
  23722. +/* simplify the use of the inflate_huft type with some defines */
  23723. +#define exop word.what.Exop
  23724. +#define bits word.what.Bits
  23725. +
  23726. +/* Table for deflate from PKZIP's appnote.txt. */
  23727. +local const uInt border[] = { /* Order of the bit length code lengths */
  23728. + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  23729. +
  23730. +/*
  23731. + Notes beyond the 1.93a appnote.txt:
  23732. +
  23733. + 1. Distance pointers never point before the beginning of the output
  23734. + stream.
  23735. + 2. Distance pointers can point back across blocks, up to 32k away.
  23736. + 3. There is an implied maximum of 7 bits for the bit length table and
  23737. + 15 bits for the actual data.
  23738. + 4. If only one code exists, then it is encoded using one bit. (Zero
  23739. + would be more efficient, but perhaps a little confusing.) If two
  23740. + codes exist, they are coded using one bit each (0 and 1).
  23741. + 5. There is no way of sending zero distance codes--a dummy must be
  23742. + sent if there are none. (History: a pre 2.0 version of PKZIP would
  23743. + store blocks with no distance codes, but this was discovered to be
  23744. + too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  23745. + zero distance codes, which is sent as one code of zero bits in
  23746. + length.
  23747. + 6. There are up to 286 literal/length codes. Code 256 represents the
  23748. + end-of-block. Note however that the static length tree defines
  23749. + 288 codes just to fill out the Huffman codes. Codes 286 and 287
  23750. + cannot be used though, since there is no length base or extra bits
  23751. + defined for them. Similarily, there are up to 30 distance codes.
  23752. + However, static trees define 32 codes (all 5 bits) to fill out the
  23753. + Huffman codes, but the last two had better not show up in the data.
  23754. + 7. Unzip can check dynamic Huffman blocks for complete code sets.
  23755. + The exception is that a single code would not be complete (see #4).
  23756. + 8. The five bits following the block type is really the number of
  23757. + literal codes sent minus 257.
  23758. + 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  23759. + (1+6+6). Therefore, to output three times the length, you output
  23760. + three codes (1+1+1), whereas to output four times the same length,
  23761. + you only need two codes (1+3). Hmm.
  23762. + 10. In the tree reconstruction algorithm, Code = Code + Increment
  23763. + only if BitLength(i) is not zero. (Pretty obvious.)
  23764. + 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  23765. + 12. Note: length code 284 can represent 227-258, but length code 285
  23766. + really is 258. The last length deserves its own, short code
  23767. + since it gets used a lot in very redundant files. The length
  23768. + 258 is special since 258 - 3 (the min match length) is 255.
  23769. + 13. The literal/length and distance code bit lengths are read as a
  23770. + single stream of lengths. It is possible (and advantageous) for
  23771. + a repeat code (16, 17, or 18) to go across the boundary between
  23772. + the two sets of lengths.
  23773. + */
  23774. +
  23775. +
  23776. +void inflate_blocks_reset(s, z, c)
  23777. +inflate_blocks_statef *s;
  23778. +z_streamp z;
  23779. +uLongf *c;
  23780. +{
  23781. + if (c != Z_NULL)
  23782. + *c = s->check;
  23783. + if (s->mode == BTREE || s->mode == DTREE)
  23784. + ZFREE(z, s->sub.trees.blens);
  23785. + if (s->mode == CODES)
  23786. + inflate_codes_free(s->sub.decode.codes, z);
  23787. + s->mode = TYPE;
  23788. + s->bitk = 0;
  23789. + s->bitb = 0;
  23790. + s->read = s->write = s->window;
  23791. + if (s->checkfn != Z_NULL)
  23792. + z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
  23793. + Tracev((stderr, "inflate: blocks reset\n"));
  23794. +}
  23795. +
  23796. +
  23797. +inflate_blocks_statef *inflate_blocks_new(z, c, w)
  23798. +z_streamp z;
  23799. +check_func c;
  23800. +uInt w;
  23801. +{
  23802. + inflate_blocks_statef *s;
  23803. +
  23804. + if ((s = (inflate_blocks_statef *)ZALLOC
  23805. + (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
  23806. + return s;
  23807. + if ((s->hufts =
  23808. + (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
  23809. + {
  23810. + ZFREE(z, s);
  23811. + return Z_NULL;
  23812. + }
  23813. + if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
  23814. + {
  23815. + ZFREE(z, s->hufts);
  23816. + ZFREE(z, s);
  23817. + return Z_NULL;
  23818. + }
  23819. + s->end = s->window + w;
  23820. + s->checkfn = c;
  23821. + s->mode = TYPE;
  23822. + Tracev((stderr, "inflate: blocks allocated\n"));
  23823. + inflate_blocks_reset(s, z, Z_NULL);
  23824. + return s;
  23825. +}
  23826. +
  23827. +
  23828. +int inflate_blocks(s, z, r)
  23829. +inflate_blocks_statef *s;
  23830. +z_streamp z;
  23831. +int r;
  23832. +{
  23833. + uInt t; /* temporary storage */
  23834. + uLong b; /* bit buffer */
  23835. + uInt k; /* bits in bit buffer */
  23836. + Bytef *p; /* input data pointer */
  23837. + uInt n; /* bytes available there */
  23838. + Bytef *q; /* output window write pointer */
  23839. + uInt m; /* bytes to end of window or read pointer */
  23840. +
  23841. + /* copy input/output information to locals (UPDATE macro restores) */
  23842. + LOAD
  23843. +
  23844. + /* process input based on current state */
  23845. + while (1) switch (s->mode)
  23846. + {
  23847. + case TYPE:
  23848. + NEEDBITS(3)
  23849. + t = (uInt)b & 7;
  23850. + s->last = t & 1;
  23851. + switch (t >> 1)
  23852. + {
  23853. + case 0: /* stored */
  23854. + Tracev((stderr, "inflate: stored block%s\n",
  23855. + s->last ? " (last)" : ""));
  23856. + DUMPBITS(3)
  23857. + t = k & 7; /* go to byte boundary */
  23858. + DUMPBITS(t)
  23859. + s->mode = LENS; /* get length of stored block */
  23860. + break;
  23861. + case 1: /* fixed */
  23862. + Tracev((stderr, "inflate: fixed codes block%s\n",
  23863. + s->last ? " (last)" : ""));
  23864. + {
  23865. + uInt bl, bd;
  23866. + inflate_huft *tl, *td;
  23867. +
  23868. + inflate_trees_fixed(&bl, &bd, &tl, &td, z);
  23869. + s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
  23870. + if (s->sub.decode.codes == Z_NULL)
  23871. + {
  23872. + r = Z_MEM_ERROR;
  23873. + LEAVE
  23874. + }
  23875. + }
  23876. + DUMPBITS(3)
  23877. + s->mode = CODES;
  23878. + break;
  23879. + case 2: /* dynamic */
  23880. + Tracev((stderr, "inflate: dynamic codes block%s\n",
  23881. + s->last ? " (last)" : ""));
  23882. + DUMPBITS(3)
  23883. + s->mode = TABLE;
  23884. + break;
  23885. + case 3: /* illegal */
  23886. + DUMPBITS(3)
  23887. + s->mode = BAD;
  23888. + z->msg = (char*)"invalid block type";
  23889. + r = Z_DATA_ERROR;
  23890. + LEAVE
  23891. + }
  23892. + break;
  23893. + case LENS:
  23894. + NEEDBITS(32)
  23895. + if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
  23896. + {
  23897. + s->mode = BAD;
  23898. + z->msg = (char*)"invalid stored block lengths";
  23899. + r = Z_DATA_ERROR;
  23900. + LEAVE
  23901. + }
  23902. + s->sub.left = (uInt)b & 0xffff;
  23903. + b = k = 0; /* dump bits */
  23904. + Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
  23905. + s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
  23906. + break;
  23907. + case STORED:
  23908. + if (n == 0)
  23909. + LEAVE
  23910. + NEEDOUT
  23911. + t = s->sub.left;
  23912. + if (t > n) t = n;
  23913. + if (t > m) t = m;
  23914. + zmemcpy(q, p, t);
  23915. + p += t; n -= t;
  23916. + q += t; m -= t;
  23917. + if ((s->sub.left -= t) != 0)
  23918. + break;
  23919. + Tracev((stderr, "inflate: stored end, %lu total out\n",
  23920. + z->total_out + (q >= s->read ? q - s->read :
  23921. + (s->end - s->read) + (q - s->window))));
  23922. + s->mode = s->last ? DRY : TYPE;
  23923. + break;
  23924. + case TABLE:
  23925. + NEEDBITS(14)
  23926. + s->sub.trees.table = t = (uInt)b & 0x3fff;
  23927. +#ifndef PKZIP_BUG_WORKAROUND
  23928. + if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  23929. + {
  23930. + s->mode = BAD;
  23931. + z->msg = (char*)"too many length or distance symbols";
  23932. + r = Z_DATA_ERROR;
  23933. + LEAVE
  23934. + }
  23935. +#endif
  23936. + t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  23937. + if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
  23938. + {
  23939. + r = Z_MEM_ERROR;
  23940. + LEAVE
  23941. + }
  23942. + DUMPBITS(14)
  23943. + s->sub.trees.index = 0;
  23944. + Tracev((stderr, "inflate: table sizes ok\n"));
  23945. + s->mode = BTREE;
  23946. + case BTREE:
  23947. + while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  23948. + {
  23949. + NEEDBITS(3)
  23950. + s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  23951. + DUMPBITS(3)
  23952. + }
  23953. + while (s->sub.trees.index < 19)
  23954. + s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
  23955. + s->sub.trees.bb = 7;
  23956. + t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
  23957. + &s->sub.trees.tb, s->hufts, z);
  23958. + if (t != Z_OK)
  23959. + {
  23960. + r = t;
  23961. + if (r == Z_DATA_ERROR)
  23962. + {
  23963. + ZFREE(z, s->sub.trees.blens);
  23964. + s->mode = BAD;
  23965. + }
  23966. + LEAVE
  23967. + }
  23968. + s->sub.trees.index = 0;
  23969. + Tracev((stderr, "inflate: bits tree ok\n"));
  23970. + s->mode = DTREE;
  23971. + case DTREE:
  23972. + while (t = s->sub.trees.table,
  23973. + s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  23974. + {
  23975. + inflate_huft *h;
  23976. + uInt i, j, c;
  23977. +
  23978. + t = s->sub.trees.bb;
  23979. + NEEDBITS(t)
  23980. + h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
  23981. + t = h->bits;
  23982. + c = h->base;
  23983. + if (c < 16)
  23984. + {
  23985. + DUMPBITS(t)
  23986. + s->sub.trees.blens[s->sub.trees.index++] = c;
  23987. + }
  23988. + else /* c == 16..18 */
  23989. + {
  23990. + i = c == 18 ? 7 : c - 14;
  23991. + j = c == 18 ? 11 : 3;
  23992. + NEEDBITS(t + i)
  23993. + DUMPBITS(t)
  23994. + j += (uInt)b & inflate_mask[i];
  23995. + DUMPBITS(i)
  23996. + i = s->sub.trees.index;
  23997. + t = s->sub.trees.table;
  23998. + if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  23999. + (c == 16 && i < 1))
  24000. + {
  24001. + ZFREE(z, s->sub.trees.blens);
  24002. + s->mode = BAD;
  24003. + z->msg = (char*)"invalid bit length repeat";
  24004. + r = Z_DATA_ERROR;
  24005. + LEAVE
  24006. + }
  24007. + c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
  24008. + do {
  24009. + s->sub.trees.blens[i++] = c;
  24010. + } while (--j);
  24011. + s->sub.trees.index = i;
  24012. + }
  24013. + }
  24014. + s->sub.trees.tb = Z_NULL;
  24015. + {
  24016. + uInt bl, bd;
  24017. + inflate_huft *tl, *td;
  24018. + inflate_codes_statef *c;
  24019. +
  24020. + bl = 9; /* must be <= 9 for lookahead assumptions */
  24021. + bd = 6; /* must be <= 9 for lookahead assumptions */
  24022. + t = s->sub.trees.table;
  24023. + t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
  24024. + s->sub.trees.blens, &bl, &bd, &tl, &td,
  24025. + s->hufts, z);
  24026. + if (t != Z_OK)
  24027. + {
  24028. + if (t == (uInt)Z_DATA_ERROR)
  24029. + {
  24030. + ZFREE(z, s->sub.trees.blens);
  24031. + s->mode = BAD;
  24032. + }
  24033. + r = t;
  24034. + LEAVE
  24035. + }
  24036. + Tracev((stderr, "inflate: trees ok\n"));
  24037. + if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
  24038. + {
  24039. + r = Z_MEM_ERROR;
  24040. + LEAVE
  24041. + }
  24042. + s->sub.decode.codes = c;
  24043. + }
  24044. + ZFREE(z, s->sub.trees.blens);
  24045. + s->mode = CODES;
  24046. + case CODES:
  24047. + UPDATE
  24048. + if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
  24049. + return inflate_flush(s, z, r);
  24050. + r = Z_OK;
  24051. + inflate_codes_free(s->sub.decode.codes, z);
  24052. + LOAD
  24053. + Tracev((stderr, "inflate: codes end, %lu total out\n",
  24054. + z->total_out + (q >= s->read ? q - s->read :
  24055. + (s->end - s->read) + (q - s->window))));
  24056. + if (!s->last)
  24057. + {
  24058. + s->mode = TYPE;
  24059. + break;
  24060. + }
  24061. + s->mode = DRY;
  24062. + case DRY:
  24063. + FLUSH
  24064. + if (s->read != s->write)
  24065. + LEAVE
  24066. + s->mode = DONE;
  24067. + case DONE:
  24068. + r = Z_STREAM_END;
  24069. + LEAVE
  24070. + case BAD:
  24071. + r = Z_DATA_ERROR;
  24072. + LEAVE
  24073. + default:
  24074. + r = Z_STREAM_ERROR;
  24075. + LEAVE
  24076. + }
  24077. +}
  24078. +
  24079. +
  24080. +int inflate_blocks_free(s, z)
  24081. +inflate_blocks_statef *s;
  24082. +z_streamp z;
  24083. +{
  24084. + inflate_blocks_reset(s, z, Z_NULL);
  24085. + ZFREE(z, s->window);
  24086. + ZFREE(z, s->hufts);
  24087. + ZFREE(z, s);
  24088. + Tracev((stderr, "inflate: blocks freed\n"));
  24089. + return Z_OK;
  24090. +}
  24091. +
  24092. +
  24093. +void inflate_set_dictionary(s, d, n)
  24094. +inflate_blocks_statef *s;
  24095. +const Bytef *d;
  24096. +uInt n;
  24097. +{
  24098. + zmemcpy(s->window, d, n);
  24099. + s->read = s->write = s->window + n;
  24100. +}
  24101. +
  24102. +
  24103. +/* Returns true if inflate is currently at the end of a block generated
  24104. + * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  24105. + * IN assertion: s != Z_NULL
  24106. + */
  24107. +int inflate_blocks_sync_point(s)
  24108. +inflate_blocks_statef *s;
  24109. +{
  24110. + return s->mode == LENS;
  24111. +}
  24112. --- /dev/null Tue Mar 11 13:02:56 2003
  24113. +++ linux/net/ipsec/infblock.h Mon Feb 9 13:51:03 2004
  24114. @@ -0,0 +1,39 @@
  24115. +/* infblock.h -- header to use infblock.c
  24116. + * Copyright (C) 1995-2002 Mark Adler
  24117. + * For conditions of distribution and use, see copyright notice in zlib.h
  24118. + */
  24119. +
  24120. +/* WARNING: this file should *not* be used by applications. It is
  24121. + part of the implementation of the compression library and is
  24122. + subject to change. Applications should only use zlib.h.
  24123. + */
  24124. +
  24125. +struct inflate_blocks_state;
  24126. +typedef struct inflate_blocks_state FAR inflate_blocks_statef;
  24127. +
  24128. +extern inflate_blocks_statef * inflate_blocks_new OF((
  24129. + z_streamp z,
  24130. + check_func c, /* check function */
  24131. + uInt w)); /* window size */
  24132. +
  24133. +extern int inflate_blocks OF((
  24134. + inflate_blocks_statef *,
  24135. + z_streamp ,
  24136. + int)); /* initial return code */
  24137. +
  24138. +extern void inflate_blocks_reset OF((
  24139. + inflate_blocks_statef *,
  24140. + z_streamp ,
  24141. + uLongf *)); /* check value on output */
  24142. +
  24143. +extern int inflate_blocks_free OF((
  24144. + inflate_blocks_statef *,
  24145. + z_streamp));
  24146. +
  24147. +extern void inflate_set_dictionary OF((
  24148. + inflate_blocks_statef *s,
  24149. + const Bytef *d, /* dictionary */
  24150. + uInt n)); /* dictionary length */
  24151. +
  24152. +extern int inflate_blocks_sync_point OF((
  24153. + inflate_blocks_statef *s));
  24154. --- /dev/null Tue Mar 11 13:02:56 2003
  24155. +++ linux/net/ipsec/infcodes.c Mon Feb 9 13:51:03 2004
  24156. @@ -0,0 +1,251 @@
  24157. +/* infcodes.c -- process literals and length/distance pairs
  24158. + * Copyright (C) 1995-2002 Mark Adler
  24159. + * For conditions of distribution and use, see copyright notice in zlib.h
  24160. + */
  24161. +
  24162. +#include <zlib/zutil.h>
  24163. +#include "inftrees.h"
  24164. +#include "infblock.h"
  24165. +#include "infcodes.h"
  24166. +#include "infutil.h"
  24167. +#include "inffast.h"
  24168. +
  24169. +/* simplify the use of the inflate_huft type with some defines */
  24170. +#define exop word.what.Exop
  24171. +#define bits word.what.Bits
  24172. +
  24173. +typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  24174. + START, /* x: set up for LEN */
  24175. + LEN, /* i: get length/literal/eob next */
  24176. + LENEXT, /* i: getting length extra (have base) */
  24177. + DIST, /* i: get distance next */
  24178. + DISTEXT, /* i: getting distance extra */
  24179. + COPY, /* o: copying bytes in window, waiting for space */
  24180. + LIT, /* o: got literal, waiting for output space */
  24181. + WASH, /* o: got eob, possibly still output waiting */
  24182. + END, /* x: got eob and all data flushed */
  24183. + BADCODE} /* x: got error */
  24184. +inflate_codes_mode;
  24185. +
  24186. +/* inflate codes private state */
  24187. +struct inflate_codes_state {
  24188. +
  24189. + /* mode */
  24190. + inflate_codes_mode mode; /* current inflate_codes mode */
  24191. +
  24192. + /* mode dependent information */
  24193. + uInt len;
  24194. + union {
  24195. + struct {
  24196. + inflate_huft *tree; /* pointer into tree */
  24197. + uInt need; /* bits needed */
  24198. + } code; /* if LEN or DIST, where in tree */
  24199. + uInt lit; /* if LIT, literal */
  24200. + struct {
  24201. + uInt get; /* bits to get for extra */
  24202. + uInt dist; /* distance back to copy from */
  24203. + } copy; /* if EXT or COPY, where and how much */
  24204. + } sub; /* submode */
  24205. +
  24206. + /* mode independent information */
  24207. + Byte lbits; /* ltree bits decoded per branch */
  24208. + Byte dbits; /* dtree bits decoder per branch */
  24209. + inflate_huft *ltree; /* literal/length/eob tree */
  24210. + inflate_huft *dtree; /* distance tree */
  24211. +
  24212. +};
  24213. +
  24214. +
  24215. +inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
  24216. +uInt bl, bd;
  24217. +inflate_huft *tl;
  24218. +inflate_huft *td; /* need separate declaration for Borland C++ */
  24219. +z_streamp z;
  24220. +{
  24221. + inflate_codes_statef *c;
  24222. +
  24223. + if ((c = (inflate_codes_statef *)
  24224. + ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  24225. + {
  24226. + c->mode = START;
  24227. + c->lbits = (Byte)bl;
  24228. + c->dbits = (Byte)bd;
  24229. + c->ltree = tl;
  24230. + c->dtree = td;
  24231. + Tracev((stderr, "inflate: codes new\n"));
  24232. + }
  24233. + return c;
  24234. +}
  24235. +
  24236. +
  24237. +int inflate_codes(s, z, r)
  24238. +inflate_blocks_statef *s;
  24239. +z_streamp z;
  24240. +int r;
  24241. +{
  24242. + uInt j; /* temporary storage */
  24243. + inflate_huft *t; /* temporary pointer */
  24244. + uInt e; /* extra bits or operation */
  24245. + uLong b; /* bit buffer */
  24246. + uInt k; /* bits in bit buffer */
  24247. + Bytef *p; /* input data pointer */
  24248. + uInt n; /* bytes available there */
  24249. + Bytef *q; /* output window write pointer */
  24250. + uInt m; /* bytes to end of window or read pointer */
  24251. + Bytef *f; /* pointer to copy strings from */
  24252. + inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
  24253. +
  24254. + /* copy input/output information to locals (UPDATE macro restores) */
  24255. + LOAD
  24256. +
  24257. + /* process input and output based on current state */
  24258. + while (1) switch (c->mode)
  24259. + { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  24260. + case START: /* x: set up for LEN */
  24261. +#ifndef SLOW
  24262. + if (m >= 258 && n >= 10)
  24263. + {
  24264. + UPDATE
  24265. + r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  24266. + LOAD
  24267. + if (r != Z_OK)
  24268. + {
  24269. + c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  24270. + break;
  24271. + }
  24272. + }
  24273. +#endif /* !SLOW */
  24274. + c->sub.code.need = c->lbits;
  24275. + c->sub.code.tree = c->ltree;
  24276. + c->mode = LEN;
  24277. + case LEN: /* i: get length/literal/eob next */
  24278. + j = c->sub.code.need;
  24279. + NEEDBITS(j)
  24280. + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  24281. + DUMPBITS(t->bits)
  24282. + e = (uInt)(t->exop);
  24283. + if (e == 0) /* literal */
  24284. + {
  24285. + c->sub.lit = t->base;
  24286. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24287. + "inflate: literal '%c'\n" :
  24288. + "inflate: literal 0x%02x\n", t->base));
  24289. + c->mode = LIT;
  24290. + break;
  24291. + }
  24292. + if (e & 16) /* length */
  24293. + {
  24294. + c->sub.copy.get = e & 15;
  24295. + c->len = t->base;
  24296. + c->mode = LENEXT;
  24297. + break;
  24298. + }
  24299. + if ((e & 64) == 0) /* next table */
  24300. + {
  24301. + c->sub.code.need = e;
  24302. + c->sub.code.tree = t + t->base;
  24303. + break;
  24304. + }
  24305. + if (e & 32) /* end of block */
  24306. + {
  24307. + Tracevv((stderr, "inflate: end of block\n"));
  24308. + c->mode = WASH;
  24309. + break;
  24310. + }
  24311. + c->mode = BADCODE; /* invalid code */
  24312. + z->msg = (char*)"invalid literal/length code";
  24313. + r = Z_DATA_ERROR;
  24314. + LEAVE
  24315. + case LENEXT: /* i: getting length extra (have base) */
  24316. + j = c->sub.copy.get;
  24317. + NEEDBITS(j)
  24318. + c->len += (uInt)b & inflate_mask[j];
  24319. + DUMPBITS(j)
  24320. + c->sub.code.need = c->dbits;
  24321. + c->sub.code.tree = c->dtree;
  24322. + Tracevv((stderr, "inflate: length %u\n", c->len));
  24323. + c->mode = DIST;
  24324. + case DIST: /* i: get distance next */
  24325. + j = c->sub.code.need;
  24326. + NEEDBITS(j)
  24327. + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  24328. + DUMPBITS(t->bits)
  24329. + e = (uInt)(t->exop);
  24330. + if (e & 16) /* distance */
  24331. + {
  24332. + c->sub.copy.get = e & 15;
  24333. + c->sub.copy.dist = t->base;
  24334. + c->mode = DISTEXT;
  24335. + break;
  24336. + }
  24337. + if ((e & 64) == 0) /* next table */
  24338. + {
  24339. + c->sub.code.need = e;
  24340. + c->sub.code.tree = t + t->base;
  24341. + break;
  24342. + }
  24343. + c->mode = BADCODE; /* invalid code */
  24344. + z->msg = (char*)"invalid distance code";
  24345. + r = Z_DATA_ERROR;
  24346. + LEAVE
  24347. + case DISTEXT: /* i: getting distance extra */
  24348. + j = c->sub.copy.get;
  24349. + NEEDBITS(j)
  24350. + c->sub.copy.dist += (uInt)b & inflate_mask[j];
  24351. + DUMPBITS(j)
  24352. + Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
  24353. + c->mode = COPY;
  24354. + case COPY: /* o: copying bytes in window, waiting for space */
  24355. + f = q - c->sub.copy.dist;
  24356. + while (f < s->window) /* modulo window size-"while" instead */
  24357. + f += s->end - s->window; /* of "if" handles invalid distances */
  24358. + while (c->len)
  24359. + {
  24360. + NEEDOUT
  24361. + OUTBYTE(*f++)
  24362. + if (f == s->end)
  24363. + f = s->window;
  24364. + c->len--;
  24365. + }
  24366. + c->mode = START;
  24367. + break;
  24368. + case LIT: /* o: got literal, waiting for output space */
  24369. + NEEDOUT
  24370. + OUTBYTE(c->sub.lit)
  24371. + c->mode = START;
  24372. + break;
  24373. + case WASH: /* o: got eob, possibly more output */
  24374. + if (k > 7) /* return unused byte, if any */
  24375. + {
  24376. + Assert(k < 16, "inflate_codes grabbed too many bytes")
  24377. + k -= 8;
  24378. + n++;
  24379. + p--; /* can always return one */
  24380. + }
  24381. + FLUSH
  24382. + if (s->read != s->write)
  24383. + LEAVE
  24384. + c->mode = END;
  24385. + case END:
  24386. + r = Z_STREAM_END;
  24387. + LEAVE
  24388. + case BADCODE: /* x: got error */
  24389. + r = Z_DATA_ERROR;
  24390. + LEAVE
  24391. + default:
  24392. + r = Z_STREAM_ERROR;
  24393. + LEAVE
  24394. + }
  24395. +#ifdef NEED_DUMMY_RETURN
  24396. + return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  24397. +#endif
  24398. +}
  24399. +
  24400. +
  24401. +void inflate_codes_free(c, z)
  24402. +inflate_codes_statef *c;
  24403. +z_streamp z;
  24404. +{
  24405. + ZFREE(z, c);
  24406. + Tracev((stderr, "inflate: codes free\n"));
  24407. +}
  24408. --- /dev/null Tue Mar 11 13:02:56 2003
  24409. +++ linux/net/ipsec/infcodes.h Mon Feb 9 13:51:03 2004
  24410. @@ -0,0 +1,31 @@
  24411. +/* infcodes.h -- header to use infcodes.c
  24412. + * Copyright (C) 1995-2002 Mark Adler
  24413. + * For conditions of distribution and use, see copyright notice in zlib.h
  24414. + */
  24415. +
  24416. +/* WARNING: this file should *not* be used by applications. It is
  24417. + part of the implementation of the compression library and is
  24418. + subject to change. Applications should only use zlib.h.
  24419. + */
  24420. +
  24421. +#ifndef _INFCODES_H
  24422. +#define _INFCODES_H
  24423. +
  24424. +struct inflate_codes_state;
  24425. +typedef struct inflate_codes_state FAR inflate_codes_statef;
  24426. +
  24427. +extern inflate_codes_statef *inflate_codes_new OF((
  24428. + uInt, uInt,
  24429. + inflate_huft *, inflate_huft *,
  24430. + z_streamp ));
  24431. +
  24432. +extern int inflate_codes OF((
  24433. + inflate_blocks_statef *,
  24434. + z_streamp ,
  24435. + int));
  24436. +
  24437. +extern void inflate_codes_free OF((
  24438. + inflate_codes_statef *,
  24439. + z_streamp ));
  24440. +
  24441. +#endif /* _INFCODES_H */
  24442. --- /dev/null Tue Mar 11 13:02:56 2003
  24443. +++ linux/net/ipsec/inffast.c Mon Feb 9 13:51:03 2004
  24444. @@ -0,0 +1,183 @@
  24445. +/* inffast.c -- process literals and length/distance pairs fast
  24446. + * Copyright (C) 1995-2002 Mark Adler
  24447. + * For conditions of distribution and use, see copyright notice in zlib.h
  24448. + */
  24449. +
  24450. +#include <zlib/zutil.h>
  24451. +#include "inftrees.h"
  24452. +#include "infblock.h"
  24453. +#include "infcodes.h"
  24454. +#include "infutil.h"
  24455. +#include "inffast.h"
  24456. +
  24457. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  24458. +
  24459. +/* simplify the use of the inflate_huft type with some defines */
  24460. +#define exop word.what.Exop
  24461. +#define bits word.what.Bits
  24462. +
  24463. +/* macros for bit input with no checking and for returning unused bytes */
  24464. +#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  24465. +#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
  24466. +
  24467. +/* Called with number of bytes left to write in window at least 258
  24468. + (the maximum string length) and number of input bytes available
  24469. + at least ten. The ten bytes are six bytes for the longest length/
  24470. + distance pair plus four bytes for overloading the bit buffer. */
  24471. +
  24472. +int inflate_fast(bl, bd, tl, td, s, z)
  24473. +uInt bl, bd;
  24474. +inflate_huft *tl;
  24475. +inflate_huft *td; /* need separate declaration for Borland C++ */
  24476. +inflate_blocks_statef *s;
  24477. +z_streamp z;
  24478. +{
  24479. + inflate_huft *t; /* temporary pointer */
  24480. + uInt e; /* extra bits or operation */
  24481. + uLong b; /* bit buffer */
  24482. + uInt k; /* bits in bit buffer */
  24483. + Bytef *p; /* input data pointer */
  24484. + uInt n; /* bytes available there */
  24485. + Bytef *q; /* output window write pointer */
  24486. + uInt m; /* bytes to end of window or read pointer */
  24487. + uInt ml; /* mask for literal/length tree */
  24488. + uInt md; /* mask for distance tree */
  24489. + uInt c; /* bytes to copy */
  24490. + uInt d; /* distance back to copy from */
  24491. + Bytef *r; /* copy source pointer */
  24492. +
  24493. + /* load input, output, bit values */
  24494. + LOAD
  24495. +
  24496. + /* initialize masks */
  24497. + ml = inflate_mask[bl];
  24498. + md = inflate_mask[bd];
  24499. +
  24500. + /* do until not enough input or output space for fast loop */
  24501. + do { /* assume called with m >= 258 && n >= 10 */
  24502. + /* get literal/length code */
  24503. + GRABBITS(20) /* max bits for literal/length code */
  24504. + if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
  24505. + {
  24506. + DUMPBITS(t->bits)
  24507. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24508. + "inflate: * literal '%c'\n" :
  24509. + "inflate: * literal 0x%02x\n", t->base));
  24510. + *q++ = (Byte)t->base;
  24511. + m--;
  24512. + continue;
  24513. + }
  24514. + do {
  24515. + DUMPBITS(t->bits)
  24516. + if (e & 16)
  24517. + {
  24518. + /* get extra bits for length */
  24519. + e &= 15;
  24520. + c = t->base + ((uInt)b & inflate_mask[e]);
  24521. + DUMPBITS(e)
  24522. + Tracevv((stderr, "inflate: * length %u\n", c));
  24523. +
  24524. + /* decode distance base of block to copy */
  24525. + GRABBITS(15); /* max bits for distance code */
  24526. + e = (t = td + ((uInt)b & md))->exop;
  24527. + do {
  24528. + DUMPBITS(t->bits)
  24529. + if (e & 16)
  24530. + {
  24531. + /* get extra bits to add to distance base */
  24532. + e &= 15;
  24533. + GRABBITS(e) /* get extra bits (up to 13) */
  24534. + d = t->base + ((uInt)b & inflate_mask[e]);
  24535. + DUMPBITS(e)
  24536. + Tracevv((stderr, "inflate: * distance %u\n", d));
  24537. +
  24538. + /* do the copy */
  24539. + m -= c;
  24540. + r = q - d;
  24541. + if (r < s->window) /* wrap if needed */
  24542. + {
  24543. + do {
  24544. + r += s->end - s->window; /* force pointer in window */
  24545. + } while (r < s->window); /* covers invalid distances */
  24546. + e = s->end - r;
  24547. + if (c > e)
  24548. + {
  24549. + c -= e; /* wrapped copy */
  24550. + do {
  24551. + *q++ = *r++;
  24552. + } while (--e);
  24553. + r = s->window;
  24554. + do {
  24555. + *q++ = *r++;
  24556. + } while (--c);
  24557. + }
  24558. + else /* normal copy */
  24559. + {
  24560. + *q++ = *r++; c--;
  24561. + *q++ = *r++; c--;
  24562. + do {
  24563. + *q++ = *r++;
  24564. + } while (--c);
  24565. + }
  24566. + }
  24567. + else /* normal copy */
  24568. + {
  24569. + *q++ = *r++; c--;
  24570. + *q++ = *r++; c--;
  24571. + do {
  24572. + *q++ = *r++;
  24573. + } while (--c);
  24574. + }
  24575. + break;
  24576. + }
  24577. + else if ((e & 64) == 0)
  24578. + {
  24579. + t += t->base;
  24580. + e = (t += ((uInt)b & inflate_mask[e]))->exop;
  24581. + }
  24582. + else
  24583. + {
  24584. + z->msg = (char*)"invalid distance code";
  24585. + UNGRAB
  24586. + UPDATE
  24587. + return Z_DATA_ERROR;
  24588. + }
  24589. + } while (1);
  24590. + break;
  24591. + }
  24592. + if ((e & 64) == 0)
  24593. + {
  24594. + t += t->base;
  24595. + if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
  24596. + {
  24597. + DUMPBITS(t->bits)
  24598. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24599. + "inflate: * literal '%c'\n" :
  24600. + "inflate: * literal 0x%02x\n", t->base));
  24601. + *q++ = (Byte)t->base;
  24602. + m--;
  24603. + break;
  24604. + }
  24605. + }
  24606. + else if (e & 32)
  24607. + {
  24608. + Tracevv((stderr, "inflate: * end of block\n"));
  24609. + UNGRAB
  24610. + UPDATE
  24611. + return Z_STREAM_END;
  24612. + }
  24613. + else
  24614. + {
  24615. + z->msg = (char*)"invalid literal/length code";
  24616. + UNGRAB
  24617. + UPDATE
  24618. + return Z_DATA_ERROR;
  24619. + }
  24620. + } while (1);
  24621. + } while (m >= 258 && n >= 10);
  24622. +
  24623. + /* not enough input or output--restore pointers and return */
  24624. + UNGRAB
  24625. + UPDATE
  24626. + return Z_OK;
  24627. +}
  24628. --- /dev/null Tue Mar 11 13:02:56 2003
  24629. +++ linux/net/ipsec/inffast.h Mon Feb 9 13:51:03 2004
  24630. @@ -0,0 +1,22 @@
  24631. +/* inffast.h -- header to use inffast.c
  24632. + * Copyright (C) 1995-2002 Mark Adler
  24633. + * For conditions of distribution and use, see copyright notice in zlib.h
  24634. + */
  24635. +
  24636. +/* WARNING: this file should *not* be used by applications. It is
  24637. + part of the implementation of the compression library and is
  24638. + subject to change. Applications should only use zlib.h.
  24639. + */
  24640. +
  24641. +#ifndef _INFFAST_H
  24642. +#define _INFFAST_H
  24643. +
  24644. +extern int inflate_fast OF((
  24645. + uInt,
  24646. + uInt,
  24647. + inflate_huft *,
  24648. + inflate_huft *,
  24649. + inflate_blocks_statef *,
  24650. + z_streamp ));
  24651. +
  24652. +#endif /* _INFFAST_H */
  24653. --- /dev/null Tue Mar 11 13:02:56 2003
  24654. +++ linux/net/ipsec/inffixed.h Mon Feb 9 13:51:03 2004
  24655. @@ -0,0 +1,151 @@
  24656. +/* inffixed.h -- table for decoding fixed codes
  24657. + * Generated automatically by the maketree.c program
  24658. + */
  24659. +
  24660. +/* WARNING: this file should *not* be used by applications. It is
  24661. + part of the implementation of the compression library and is
  24662. + subject to change. Applications should only use zlib.h.
  24663. + */
  24664. +
  24665. +local uInt fixed_bl = 9;
  24666. +local uInt fixed_bd = 5;
  24667. +local inflate_huft fixed_tl[] = {
  24668. + {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  24669. + {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
  24670. + {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
  24671. + {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
  24672. + {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
  24673. + {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
  24674. + {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
  24675. + {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
  24676. + {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  24677. + {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
  24678. + {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
  24679. + {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
  24680. + {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
  24681. + {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
  24682. + {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
  24683. + {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
  24684. + {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  24685. + {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
  24686. + {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
  24687. + {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
  24688. + {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
  24689. + {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
  24690. + {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
  24691. + {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
  24692. + {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  24693. + {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
  24694. + {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
  24695. + {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
  24696. + {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
  24697. + {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
  24698. + {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
  24699. + {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
  24700. + {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  24701. + {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
  24702. + {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
  24703. + {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
  24704. + {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
  24705. + {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
  24706. + {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
  24707. + {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
  24708. + {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  24709. + {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
  24710. + {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
  24711. + {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
  24712. + {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
  24713. + {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
  24714. + {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
  24715. + {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
  24716. + {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  24717. + {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
  24718. + {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
  24719. + {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
  24720. + {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
  24721. + {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
  24722. + {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
  24723. + {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
  24724. + {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  24725. + {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
  24726. + {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
  24727. + {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
  24728. + {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
  24729. + {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
  24730. + {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
  24731. + {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
  24732. + {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  24733. + {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
  24734. + {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
  24735. + {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
  24736. + {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
  24737. + {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
  24738. + {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
  24739. + {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
  24740. + {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  24741. + {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
  24742. + {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
  24743. + {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
  24744. + {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
  24745. + {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
  24746. + {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
  24747. + {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
  24748. + {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  24749. + {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
  24750. + {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
  24751. + {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
  24752. + {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
  24753. + {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
  24754. + {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
  24755. + {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
  24756. + {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  24757. + {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
  24758. + {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
  24759. + {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
  24760. + {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
  24761. + {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
  24762. + {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
  24763. + {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
  24764. + {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  24765. + {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
  24766. + {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
  24767. + {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
  24768. + {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
  24769. + {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
  24770. + {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
  24771. + {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
  24772. + {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  24773. + {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
  24774. + {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
  24775. + {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
  24776. + {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
  24777. + {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
  24778. + {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
  24779. + {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
  24780. + {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  24781. + {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
  24782. + {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
  24783. + {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
  24784. + {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
  24785. + {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
  24786. + {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
  24787. + {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
  24788. + {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  24789. + {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
  24790. + {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
  24791. + {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
  24792. + {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
  24793. + {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
  24794. + {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
  24795. + {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
  24796. + };
  24797. +local inflate_huft fixed_td[] = {
  24798. + {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
  24799. + {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
  24800. + {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
  24801. + {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
  24802. + {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
  24803. + {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
  24804. + {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
  24805. + {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
  24806. + };
  24807. --- /dev/null Tue Mar 11 13:02:56 2003
  24808. +++ linux/net/ipsec/inflate.c Mon Feb 9 13:51:03 2004
  24809. @@ -0,0 +1,368 @@
  24810. +/* inflate.c -- zlib interface to inflate modules
  24811. + * Copyright (C) 1995-2002 Mark Adler
  24812. + * For conditions of distribution and use, see copyright notice in zlib.h
  24813. + */
  24814. +
  24815. +#include <zlib/zutil.h>
  24816. +#include "infblock.h"
  24817. +
  24818. +struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
  24819. +
  24820. +typedef enum {
  24821. + METHOD, /* waiting for method byte */
  24822. + FLAG, /* waiting for flag byte */
  24823. + DICT4, /* four dictionary check bytes to go */
  24824. + DICT3, /* three dictionary check bytes to go */
  24825. + DICT2, /* two dictionary check bytes to go */
  24826. + DICT1, /* one dictionary check byte to go */
  24827. + DICT0, /* waiting for inflateSetDictionary */
  24828. + BLOCKS, /* decompressing blocks */
  24829. + CHECK4, /* four check bytes to go */
  24830. + CHECK3, /* three check bytes to go */
  24831. + CHECK2, /* two check bytes to go */
  24832. + CHECK1, /* one check byte to go */
  24833. + DONE, /* finished check, done */
  24834. + BAD} /* got an error--stay here */
  24835. +inflate_mode;
  24836. +
  24837. +/* inflate private state */
  24838. +struct internal_state {
  24839. +
  24840. + /* mode */
  24841. + inflate_mode mode; /* current inflate mode */
  24842. +
  24843. + /* mode dependent information */
  24844. + union {
  24845. + uInt method; /* if FLAGS, method byte */
  24846. + struct {
  24847. + uLong was; /* computed check value */
  24848. + uLong need; /* stream check value */
  24849. + } check; /* if CHECK, check values to compare */
  24850. + uInt marker; /* if BAD, inflateSync's marker bytes count */
  24851. + } sub; /* submode */
  24852. +
  24853. + /* mode independent information */
  24854. + int nowrap; /* flag for no wrapper */
  24855. + uInt wbits; /* log2(window size) (8..15, defaults to 15) */
  24856. + inflate_blocks_statef
  24857. + *blocks; /* current inflate_blocks state */
  24858. +
  24859. +};
  24860. +
  24861. +
  24862. +int ZEXPORT inflateReset(z)
  24863. +z_streamp z;
  24864. +{
  24865. + if (z == Z_NULL || z->state == Z_NULL)
  24866. + return Z_STREAM_ERROR;
  24867. + z->total_in = z->total_out = 0;
  24868. + z->msg = Z_NULL;
  24869. + z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  24870. + inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  24871. + Tracev((stderr, "inflate: reset\n"));
  24872. + return Z_OK;
  24873. +}
  24874. +
  24875. +
  24876. +int ZEXPORT inflateEnd(z)
  24877. +z_streamp z;
  24878. +{
  24879. + if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  24880. + return Z_STREAM_ERROR;
  24881. + if (z->state->blocks != Z_NULL)
  24882. + inflate_blocks_free(z->state->blocks, z);
  24883. + ZFREE(z, z->state);
  24884. + z->state = Z_NULL;
  24885. + Tracev((stderr, "inflate: end\n"));
  24886. + return Z_OK;
  24887. +}
  24888. +
  24889. +
  24890. +int ZEXPORT inflateInit2_(z, w, version, stream_size)
  24891. +z_streamp z;
  24892. +int w;
  24893. +const char *version;
  24894. +int stream_size;
  24895. +{
  24896. + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  24897. + stream_size != sizeof(z_stream))
  24898. + return Z_VERSION_ERROR;
  24899. +
  24900. + /* initialize state */
  24901. + if (z == Z_NULL)
  24902. + return Z_STREAM_ERROR;
  24903. + z->msg = Z_NULL;
  24904. + if (z->zalloc == Z_NULL)
  24905. + {
  24906. + return Z_STREAM_ERROR;
  24907. +/* z->zalloc = zcalloc;
  24908. + z->opaque = (voidpf)0;
  24909. +*/
  24910. + }
  24911. + if (z->zfree == Z_NULL) return Z_STREAM_ERROR; /* z->zfree = zcfree; */
  24912. + if ((z->state = (struct internal_state FAR *)
  24913. + ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  24914. + return Z_MEM_ERROR;
  24915. + z->state->blocks = Z_NULL;
  24916. +
  24917. + /* handle undocumented nowrap option (no zlib header or check) */
  24918. + z->state->nowrap = 0;
  24919. + if (w < 0)
  24920. + {
  24921. + w = - w;
  24922. + z->state->nowrap = 1;
  24923. + }
  24924. +
  24925. + /* set window size */
  24926. + if (w < 8 || w > 15)
  24927. + {
  24928. + inflateEnd(z);
  24929. + return Z_STREAM_ERROR;
  24930. + }
  24931. + z->state->wbits = (uInt)w;
  24932. +
  24933. + /* create inflate_blocks state */
  24934. + if ((z->state->blocks =
  24935. + inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  24936. + == Z_NULL)
  24937. + {
  24938. + inflateEnd(z);
  24939. + return Z_MEM_ERROR;
  24940. + }
  24941. + Tracev((stderr, "inflate: allocated\n"));
  24942. +
  24943. + /* reset state */
  24944. + inflateReset(z);
  24945. + return Z_OK;
  24946. +}
  24947. +
  24948. +
  24949. +int ZEXPORT inflateInit_(z, version, stream_size)
  24950. +z_streamp z;
  24951. +const char *version;
  24952. +int stream_size;
  24953. +{
  24954. + return inflateInit2_(z, DEF_WBITS, version, stream_size);
  24955. +}
  24956. +
  24957. +
  24958. +#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  24959. +#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  24960. +
  24961. +int ZEXPORT inflate(z, f)
  24962. +z_streamp z;
  24963. +int f;
  24964. +{
  24965. + int r;
  24966. + uInt b;
  24967. +
  24968. + if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  24969. + return Z_STREAM_ERROR;
  24970. + f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  24971. + r = Z_BUF_ERROR;
  24972. + while (1) switch (z->state->mode)
  24973. + {
  24974. + case METHOD:
  24975. + NEEDBYTE
  24976. + if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  24977. + {
  24978. + z->state->mode = BAD;
  24979. + z->msg = (char*)"unknown compression method";
  24980. + z->state->sub.marker = 5; /* can't try inflateSync */
  24981. + break;
  24982. + }
  24983. + if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  24984. + {
  24985. + z->state->mode = BAD;
  24986. + z->msg = (char*)"invalid window size";
  24987. + z->state->sub.marker = 5; /* can't try inflateSync */
  24988. + break;
  24989. + }
  24990. + z->state->mode = FLAG;
  24991. + case FLAG:
  24992. + NEEDBYTE
  24993. + b = NEXTBYTE;
  24994. + if (((z->state->sub.method << 8) + b) % 31)
  24995. + {
  24996. + z->state->mode = BAD;
  24997. + z->msg = (char*)"incorrect header check";
  24998. + z->state->sub.marker = 5; /* can't try inflateSync */
  24999. + break;
  25000. + }
  25001. + Tracev((stderr, "inflate: zlib header ok\n"));
  25002. + if (!(b & PRESET_DICT))
  25003. + {
  25004. + z->state->mode = BLOCKS;
  25005. + break;
  25006. + }
  25007. + z->state->mode = DICT4;
  25008. + case DICT4:
  25009. + NEEDBYTE
  25010. + z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  25011. + z->state->mode = DICT3;
  25012. + case DICT3:
  25013. + NEEDBYTE
  25014. + z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  25015. + z->state->mode = DICT2;
  25016. + case DICT2:
  25017. + NEEDBYTE
  25018. + z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  25019. + z->state->mode = DICT1;
  25020. + case DICT1:
  25021. + NEEDBYTE
  25022. + z->state->sub.check.need += (uLong)NEXTBYTE;
  25023. + z->adler = z->state->sub.check.need;
  25024. + z->state->mode = DICT0;
  25025. + return Z_NEED_DICT;
  25026. + case DICT0:
  25027. + z->state->mode = BAD;
  25028. + z->msg = (char*)"need dictionary";
  25029. + z->state->sub.marker = 0; /* can try inflateSync */
  25030. + return Z_STREAM_ERROR;
  25031. + case BLOCKS:
  25032. + r = inflate_blocks(z->state->blocks, z, r);
  25033. + if (r == Z_DATA_ERROR)
  25034. + {
  25035. + z->state->mode = BAD;
  25036. + z->state->sub.marker = 0; /* can try inflateSync */
  25037. + break;
  25038. + }
  25039. + if (r == Z_OK)
  25040. + r = f;
  25041. + if (r != Z_STREAM_END)
  25042. + return r;
  25043. + r = f;
  25044. + inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  25045. + if (z->state->nowrap)
  25046. + {
  25047. + z->state->mode = DONE;
  25048. + break;
  25049. + }
  25050. + z->state->mode = CHECK4;
  25051. + case CHECK4:
  25052. + NEEDBYTE
  25053. + z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  25054. + z->state->mode = CHECK3;
  25055. + case CHECK3:
  25056. + NEEDBYTE
  25057. + z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  25058. + z->state->mode = CHECK2;
  25059. + case CHECK2:
  25060. + NEEDBYTE
  25061. + z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  25062. + z->state->mode = CHECK1;
  25063. + case CHECK1:
  25064. + NEEDBYTE
  25065. + z->state->sub.check.need += (uLong)NEXTBYTE;
  25066. +
  25067. + if (z->state->sub.check.was != z->state->sub.check.need)
  25068. + {
  25069. + z->state->mode = BAD;
  25070. + z->msg = (char*)"incorrect data check";
  25071. + z->state->sub.marker = 5; /* can't try inflateSync */
  25072. + break;
  25073. + }
  25074. + Tracev((stderr, "inflate: zlib check ok\n"));
  25075. + z->state->mode = DONE;
  25076. + case DONE:
  25077. + return Z_STREAM_END;
  25078. + case BAD:
  25079. + return Z_DATA_ERROR;
  25080. + default:
  25081. + return Z_STREAM_ERROR;
  25082. + }
  25083. +#ifdef NEED_DUMMY_RETURN
  25084. + return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  25085. +#endif
  25086. +}
  25087. +
  25088. +
  25089. +int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
  25090. +z_streamp z;
  25091. +const Bytef *dictionary;
  25092. +uInt dictLength;
  25093. +{
  25094. + uInt length = dictLength;
  25095. +
  25096. + if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
  25097. + return Z_STREAM_ERROR;
  25098. +
  25099. + if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
  25100. + z->adler = 1L;
  25101. +
  25102. + if (length >= ((uInt)1<<z->state->wbits))
  25103. + {
  25104. + length = (1<<z->state->wbits)-1;
  25105. + dictionary += dictLength - length;
  25106. + }
  25107. + inflate_set_dictionary(z->state->blocks, dictionary, length);
  25108. + z->state->mode = BLOCKS;
  25109. + return Z_OK;
  25110. +}
  25111. +
  25112. +
  25113. +int ZEXPORT inflateSync(z)
  25114. +z_streamp z;
  25115. +{
  25116. + uInt n; /* number of bytes to look at */
  25117. + Bytef *p; /* pointer to bytes */
  25118. + uInt m; /* number of marker bytes found in a row */
  25119. + uLong r, w; /* temporaries to save total_in and total_out */
  25120. +
  25121. + /* set up */
  25122. + if (z == Z_NULL || z->state == Z_NULL)
  25123. + return Z_STREAM_ERROR;
  25124. + if (z->state->mode != BAD)
  25125. + {
  25126. + z->state->mode = BAD;
  25127. + z->state->sub.marker = 0;
  25128. + }
  25129. + if ((n = z->avail_in) == 0)
  25130. + return Z_BUF_ERROR;
  25131. + p = z->next_in;
  25132. + m = z->state->sub.marker;
  25133. +
  25134. + /* search */
  25135. + while (n && m < 4)
  25136. + {
  25137. + static const Byte mark[4] = {0, 0, 0xff, 0xff};
  25138. + if (*p == mark[m])
  25139. + m++;
  25140. + else if (*p)
  25141. + m = 0;
  25142. + else
  25143. + m = 4 - m;
  25144. + p++, n--;
  25145. + }
  25146. +
  25147. + /* restore */
  25148. + z->total_in += p - z->next_in;
  25149. + z->next_in = p;
  25150. + z->avail_in = n;
  25151. + z->state->sub.marker = m;
  25152. +
  25153. + /* return no joy or set up to restart on a new block */
  25154. + if (m != 4)
  25155. + return Z_DATA_ERROR;
  25156. + r = z->total_in; w = z->total_out;
  25157. + inflateReset(z);
  25158. + z->total_in = r; z->total_out = w;
  25159. + z->state->mode = BLOCKS;
  25160. + return Z_OK;
  25161. +}
  25162. +
  25163. +
  25164. +/* Returns true if inflate is currently at the end of a block generated
  25165. + * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  25166. + * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  25167. + * but removes the length bytes of the resulting empty stored block. When
  25168. + * decompressing, PPP checks that at the end of input packet, inflate is
  25169. + * waiting for these length bytes.
  25170. + */
  25171. +int ZEXPORT inflateSyncPoint(z)
  25172. +z_streamp z;
  25173. +{
  25174. + if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
  25175. + return Z_STREAM_ERROR;
  25176. + return inflate_blocks_sync_point(z->state->blocks);
  25177. +}
  25178. --- /dev/null Tue Mar 11 13:02:56 2003
  25179. +++ linux/net/ipsec/inftrees.c Mon Feb 9 13:51:03 2004
  25180. @@ -0,0 +1,454 @@
  25181. +/* inftrees.c -- generate Huffman trees for efficient decoding
  25182. + * Copyright (C) 1995-2002 Mark Adler
  25183. + * For conditions of distribution and use, see copyright notice in zlib.h
  25184. + */
  25185. +
  25186. +#include <zlib/zutil.h>
  25187. +#include "inftrees.h"
  25188. +
  25189. +#if !defined(BUILDFIXED) && !defined(STDC)
  25190. +# define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
  25191. +#endif
  25192. +
  25193. +local const char inflate_copyright[] =
  25194. + " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
  25195. +/*
  25196. + If you use the zlib library in a product, an acknowledgment is welcome
  25197. + in the documentation of your product. If for some reason you cannot
  25198. + include such an acknowledgment, I would appreciate that you keep this
  25199. + copyright string in the executable of your product.
  25200. + */
  25201. +struct internal_state {int dummy;}; /* for buggy compilers */
  25202. +
  25203. +/* simplify the use of the inflate_huft type with some defines */
  25204. +#define exop word.what.Exop
  25205. +#define bits word.what.Bits
  25206. +
  25207. +
  25208. +local int huft_build OF((
  25209. + uIntf *, /* code lengths in bits */
  25210. + uInt, /* number of codes */
  25211. + uInt, /* number of "simple" codes */
  25212. + const uIntf *, /* list of base values for non-simple codes */
  25213. + const uIntf *, /* list of extra bits for non-simple codes */
  25214. + inflate_huft * FAR*,/* result: starting table */
  25215. + uIntf *, /* maximum lookup bits (returns actual) */
  25216. + inflate_huft *, /* space for trees */
  25217. + uInt *, /* hufts used in space */
  25218. + uIntf * )); /* space for values */
  25219. +
  25220. +/* Tables for deflate from PKZIP's appnote.txt. */
  25221. +local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
  25222. + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  25223. + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  25224. + /* see note #13 above about 258 */
  25225. +local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
  25226. + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  25227. + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
  25228. +local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
  25229. + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  25230. + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  25231. + 8193, 12289, 16385, 24577};
  25232. +local const uInt cpdext[30] = { /* Extra bits for distance codes */
  25233. + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  25234. + 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  25235. + 12, 12, 13, 13};
  25236. +
  25237. +/*
  25238. + Huffman code decoding is performed using a multi-level table lookup.
  25239. + The fastest way to decode is to simply build a lookup table whose
  25240. + size is determined by the longest code. However, the time it takes
  25241. + to build this table can also be a factor if the data being decoded
  25242. + is not very long. The most common codes are necessarily the
  25243. + shortest codes, so those codes dominate the decoding time, and hence
  25244. + the speed. The idea is you can have a shorter table that decodes the
  25245. + shorter, more probable codes, and then point to subsidiary tables for
  25246. + the longer codes. The time it costs to decode the longer codes is
  25247. + then traded against the time it takes to make longer tables.
  25248. +
  25249. + This results of this trade are in the variables lbits and dbits
  25250. + below. lbits is the number of bits the first level table for literal/
  25251. + length codes can decode in one step, and dbits is the same thing for
  25252. + the distance codes. Subsequent tables are also less than or equal to
  25253. + those sizes. These values may be adjusted either when all of the
  25254. + codes are shorter than that, in which case the longest code length in
  25255. + bits is used, or when the shortest code is *longer* than the requested
  25256. + table size, in which case the length of the shortest code in bits is
  25257. + used.
  25258. +
  25259. + There are two different values for the two tables, since they code a
  25260. + different number of possibilities each. The literal/length table
  25261. + codes 286 possible values, or in a flat code, a little over eight
  25262. + bits. The distance table codes 30 possible values, or a little less
  25263. + than five bits, flat. The optimum values for speed end up being
  25264. + about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  25265. + The optimum values may differ though from machine to machine, and
  25266. + possibly even between compilers. Your mileage may vary.
  25267. + */
  25268. +
  25269. +
  25270. +/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
  25271. +#define BMAX 15 /* maximum bit length of any code */
  25272. +
  25273. +local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
  25274. +uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
  25275. +uInt n; /* number of codes (assumed <= 288) */
  25276. +uInt s; /* number of simple-valued codes (0..s-1) */
  25277. +const uIntf *d; /* list of base values for non-simple codes */
  25278. +const uIntf *e; /* list of extra bits for non-simple codes */
  25279. +inflate_huft * FAR *t; /* result: starting table */
  25280. +uIntf *m; /* maximum lookup bits, returns actual */
  25281. +inflate_huft *hp; /* space for trees */
  25282. +uInt *hn; /* hufts used in space */
  25283. +uIntf *v; /* working area: values in order of bit length */
  25284. +/* Given a list of code lengths and a maximum table size, make a set of
  25285. + tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
  25286. + if the given code set is incomplete (the tables are still built in this
  25287. + case), or Z_DATA_ERROR if the input is invalid. */
  25288. +{
  25289. +
  25290. + uInt a; /* counter for codes of length k */
  25291. + uInt c[BMAX+1]; /* bit length count table */
  25292. + uInt f; /* i repeats in table every f entries */
  25293. + int g; /* maximum code length */
  25294. + int h; /* table level */
  25295. + register uInt i; /* counter, current code */
  25296. + register uInt j; /* counter */
  25297. + register int k; /* number of bits in current code */
  25298. + int l; /* bits per table (returned in m) */
  25299. + uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */
  25300. + register uIntf *p; /* pointer into c[], b[], or v[] */
  25301. + inflate_huft *q; /* points to current table */
  25302. + struct inflate_huft_s r; /* table entry for structure assignment */
  25303. + inflate_huft *u[BMAX]; /* table stack */
  25304. + register int w; /* bits before this table == (l * h) */
  25305. + uInt x[BMAX+1]; /* bit offsets, then code stack */
  25306. + uIntf *xp; /* pointer into x */
  25307. + int y; /* number of dummy codes added */
  25308. + uInt z; /* number of entries in current table */
  25309. +
  25310. +
  25311. + /* Generate counts for each bit length */
  25312. + p = c;
  25313. +#define C0 *p++ = 0;
  25314. +#define C2 C0 C0 C0 C0
  25315. +#define C4 C2 C2 C2 C2
  25316. + C4 /* clear c[]--assume BMAX+1 is 16 */
  25317. + p = b; i = n;
  25318. + do {
  25319. + c[*p++]++; /* assume all entries <= BMAX */
  25320. + } while (--i);
  25321. + if (c[0] == n) /* null input--all zero length codes */
  25322. + {
  25323. + *t = (inflate_huft *)Z_NULL;
  25324. + *m = 0;
  25325. + return Z_OK;
  25326. + }
  25327. +
  25328. +
  25329. + /* Find minimum and maximum length, bound *m by those */
  25330. + l = *m;
  25331. + for (j = 1; j <= BMAX; j++)
  25332. + if (c[j])
  25333. + break;
  25334. + k = j; /* minimum code length */
  25335. + if ((uInt)l < j)
  25336. + l = j;
  25337. + for (i = BMAX; i; i--)
  25338. + if (c[i])
  25339. + break;
  25340. + g = i; /* maximum code length */
  25341. + if ((uInt)l > i)
  25342. + l = i;
  25343. + *m = l;
  25344. +
  25345. +
  25346. + /* Adjust last length count to fill out codes, if needed */
  25347. + for (y = 1 << j; j < i; j++, y <<= 1)
  25348. + if ((y -= c[j]) < 0)
  25349. + return Z_DATA_ERROR;
  25350. + if ((y -= c[i]) < 0)
  25351. + return Z_DATA_ERROR;
  25352. + c[i] += y;
  25353. +
  25354. +
  25355. + /* Generate starting offsets into the value table for each length */
  25356. + x[1] = j = 0;
  25357. + p = c + 1; xp = x + 2;
  25358. + while (--i) { /* note that i == g from above */
  25359. + *xp++ = (j += *p++);
  25360. + }
  25361. +
  25362. +
  25363. + /* Make a table of values in order of bit lengths */
  25364. + p = b; i = 0;
  25365. + do {
  25366. + if ((j = *p++) != 0)
  25367. + v[x[j]++] = i;
  25368. + } while (++i < n);
  25369. + n = x[g]; /* set n to length of v */
  25370. +
  25371. +
  25372. + /* Generate the Huffman codes and for each, make the table entries */
  25373. + x[0] = i = 0; /* first Huffman code is zero */
  25374. + p = v; /* grab values in bit order */
  25375. + h = -1; /* no tables yet--level -1 */
  25376. + w = -l; /* bits decoded == (l * h) */
  25377. + u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
  25378. + q = (inflate_huft *)Z_NULL; /* ditto */
  25379. + z = 0; /* ditto */
  25380. +
  25381. + /* go through the bit lengths (k already is bits in shortest code) */
  25382. + for (; k <= g; k++)
  25383. + {
  25384. + a = c[k];
  25385. + while (a--)
  25386. + {
  25387. + /* here i is the Huffman code of length k bits for value *p */
  25388. + /* make tables up to required level */
  25389. + while (k > w + l)
  25390. + {
  25391. + h++;
  25392. + w += l; /* previous table always l bits */
  25393. +
  25394. + /* compute minimum size table less than or equal to l bits */
  25395. + z = g - w;
  25396. + z = z > (uInt)l ? l : z; /* table size upper limit */
  25397. + if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  25398. + { /* too few codes for k-w bit table */
  25399. + f -= a + 1; /* deduct codes from patterns left */
  25400. + xp = c + k;
  25401. + if (j < z)
  25402. + while (++j < z) /* try smaller tables up to z bits */
  25403. + {
  25404. + if ((f <<= 1) <= *++xp)
  25405. + break; /* enough codes to use up j bits */
  25406. + f -= *xp; /* else deduct codes from patterns */
  25407. + }
  25408. + }
  25409. + z = 1 << j; /* table entries for j-bit table */
  25410. +
  25411. + /* allocate new table */
  25412. + if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
  25413. + return Z_DATA_ERROR; /* overflow of MANY */
  25414. + u[h] = q = hp + *hn;
  25415. + *hn += z;
  25416. +
  25417. + /* connect to last table, if there is one */
  25418. + if (h)
  25419. + {
  25420. + x[h] = i; /* save pattern for backing up */
  25421. + r.bits = (Byte)l; /* bits to dump before this table */
  25422. + r.exop = (Byte)j; /* bits in this table */
  25423. + j = i >> (w - l);
  25424. + r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
  25425. + u[h-1][j] = r; /* connect to last table */
  25426. + }
  25427. + else
  25428. + *t = q; /* first table is returned result */
  25429. + }
  25430. +
  25431. + /* set up table entry in r */
  25432. + r.bits = (Byte)(k - w);
  25433. + if (p >= v + n)
  25434. + r.exop = 128 + 64; /* out of values--invalid code */
  25435. + else if (*p < s)
  25436. + {
  25437. + r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
  25438. + r.base = *p++; /* simple code is just the value */
  25439. + }
  25440. + else
  25441. + {
  25442. + r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
  25443. + r.base = d[*p++ - s];
  25444. + }
  25445. +
  25446. + /* fill code-like entries with r */
  25447. + f = 1 << (k - w);
  25448. + for (j = i >> w; j < z; j += f)
  25449. + q[j] = r;
  25450. +
  25451. + /* backwards increment the k-bit code i */
  25452. + for (j = 1 << (k - 1); i & j; j >>= 1)
  25453. + i ^= j;
  25454. + i ^= j;
  25455. +
  25456. + /* backup over finished tables */
  25457. + mask = (1 << w) - 1; /* needed on HP, cc -O bug */
  25458. + while ((i & mask) != x[h])
  25459. + {
  25460. + h--; /* don't need to update q */
  25461. + w -= l;
  25462. + mask = (1 << w) - 1;
  25463. + }
  25464. + }
  25465. + }
  25466. +
  25467. +
  25468. + /* Return Z_BUF_ERROR if we were given an incomplete table */
  25469. + return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  25470. +}
  25471. +
  25472. +
  25473. +int inflate_trees_bits(c, bb, tb, hp, z)
  25474. +uIntf *c; /* 19 code lengths */
  25475. +uIntf *bb; /* bits tree desired/actual depth */
  25476. +inflate_huft * FAR *tb; /* bits tree result */
  25477. +inflate_huft *hp; /* space for trees */
  25478. +z_streamp z; /* for messages */
  25479. +{
  25480. + int r;
  25481. + uInt hn = 0; /* hufts used in space */
  25482. + uIntf *v; /* work area for huft_build */
  25483. +
  25484. + if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
  25485. + return Z_MEM_ERROR;
  25486. + r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
  25487. + tb, bb, hp, &hn, v);
  25488. + if (r == Z_DATA_ERROR)
  25489. + z->msg = (char*)"oversubscribed dynamic bit lengths tree";
  25490. + else if (r == Z_BUF_ERROR || *bb == 0)
  25491. + {
  25492. + z->msg = (char*)"incomplete dynamic bit lengths tree";
  25493. + r = Z_DATA_ERROR;
  25494. + }
  25495. + ZFREE(z, v);
  25496. + return r;
  25497. +}
  25498. +
  25499. +
  25500. +int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
  25501. +uInt nl; /* number of literal/length codes */
  25502. +uInt nd; /* number of distance codes */
  25503. +uIntf *c; /* that many (total) code lengths */
  25504. +uIntf *bl; /* literal desired/actual bit depth */
  25505. +uIntf *bd; /* distance desired/actual bit depth */
  25506. +inflate_huft * FAR *tl; /* literal/length tree result */
  25507. +inflate_huft * FAR *td; /* distance tree result */
  25508. +inflate_huft *hp; /* space for trees */
  25509. +z_streamp z; /* for messages */
  25510. +{
  25511. + int r;
  25512. + uInt hn = 0; /* hufts used in space */
  25513. + uIntf *v; /* work area for huft_build */
  25514. +
  25515. + /* allocate work area */
  25516. + if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25517. + return Z_MEM_ERROR;
  25518. +
  25519. + /* build literal/length tree */
  25520. + r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  25521. + if (r != Z_OK || *bl == 0)
  25522. + {
  25523. + if (r == Z_DATA_ERROR)
  25524. + z->msg = (char*)"oversubscribed literal/length tree";
  25525. + else if (r != Z_MEM_ERROR)
  25526. + {
  25527. + z->msg = (char*)"incomplete literal/length tree";
  25528. + r = Z_DATA_ERROR;
  25529. + }
  25530. + ZFREE(z, v);
  25531. + return r;
  25532. + }
  25533. +
  25534. + /* build distance tree */
  25535. + r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  25536. + if (r != Z_OK || (*bd == 0 && nl > 257))
  25537. + {
  25538. + if (r == Z_DATA_ERROR)
  25539. + z->msg = (char*)"oversubscribed distance tree";
  25540. + else if (r == Z_BUF_ERROR) {
  25541. +#ifdef PKZIP_BUG_WORKAROUND
  25542. + r = Z_OK;
  25543. + }
  25544. +#else
  25545. + z->msg = (char*)"incomplete distance tree";
  25546. + r = Z_DATA_ERROR;
  25547. + }
  25548. + else if (r != Z_MEM_ERROR)
  25549. + {
  25550. + z->msg = (char*)"empty distance tree with lengths";
  25551. + r = Z_DATA_ERROR;
  25552. + }
  25553. + ZFREE(z, v);
  25554. + return r;
  25555. +#endif
  25556. + }
  25557. +
  25558. + /* done */
  25559. + ZFREE(z, v);
  25560. + return Z_OK;
  25561. +}
  25562. +
  25563. +
  25564. +/* build fixed tables only once--keep them here */
  25565. +#ifdef BUILDFIXED
  25566. +local int fixed_built = 0;
  25567. +#define FIXEDH 544 /* number of hufts used by fixed tables */
  25568. +local inflate_huft fixed_mem[FIXEDH];
  25569. +local uInt fixed_bl;
  25570. +local uInt fixed_bd;
  25571. +local inflate_huft *fixed_tl;
  25572. +local inflate_huft *fixed_td;
  25573. +#else
  25574. +#include "inffixed.h"
  25575. +#endif
  25576. +
  25577. +
  25578. +int inflate_trees_fixed(bl, bd, tl, td, z)
  25579. +uIntf *bl; /* literal desired/actual bit depth */
  25580. +uIntf *bd; /* distance desired/actual bit depth */
  25581. +inflate_huft * FAR *tl; /* literal/length tree result */
  25582. +inflate_huft * FAR *td; /* distance tree result */
  25583. +z_streamp z; /* for memory allocation */
  25584. +{
  25585. +#ifdef BUILDFIXED
  25586. + /* build fixed tables if not already */
  25587. + if (!fixed_built)
  25588. + {
  25589. + int k; /* temporary variable */
  25590. + uInt f = 0; /* number of hufts used in fixed_mem */
  25591. + uIntf *c; /* length list for huft_build */
  25592. + uIntf *v; /* work area for huft_build */
  25593. +
  25594. + /* allocate memory */
  25595. + if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25596. + return Z_MEM_ERROR;
  25597. + if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25598. + {
  25599. + ZFREE(z, c);
  25600. + return Z_MEM_ERROR;
  25601. + }
  25602. +
  25603. + /* literal table */
  25604. + for (k = 0; k < 144; k++)
  25605. + c[k] = 8;
  25606. + for (; k < 256; k++)
  25607. + c[k] = 9;
  25608. + for (; k < 280; k++)
  25609. + c[k] = 7;
  25610. + for (; k < 288; k++)
  25611. + c[k] = 8;
  25612. + fixed_bl = 9;
  25613. + huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
  25614. + fixed_mem, &f, v);
  25615. +
  25616. + /* distance table */
  25617. + for (k = 0; k < 30; k++)
  25618. + c[k] = 5;
  25619. + fixed_bd = 5;
  25620. + huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
  25621. + fixed_mem, &f, v);
  25622. +
  25623. + /* done */
  25624. + ZFREE(z, v);
  25625. + ZFREE(z, c);
  25626. + fixed_built = 1;
  25627. + }
  25628. +#endif
  25629. + *bl = fixed_bl;
  25630. + *bd = fixed_bd;
  25631. + *tl = fixed_tl;
  25632. + *td = fixed_td;
  25633. + return Z_OK;
  25634. +}
  25635. --- /dev/null Tue Mar 11 13:02:56 2003
  25636. +++ linux/net/ipsec/inftrees.h Mon Feb 9 13:51:03 2004
  25637. @@ -0,0 +1,63 @@
  25638. +/* inftrees.h -- header to use inftrees.c
  25639. + * Copyright (C) 1995-2002 Mark Adler
  25640. + * For conditions of distribution and use, see copyright notice in zlib.h
  25641. + */
  25642. +
  25643. +/* WARNING: this file should *not* be used by applications. It is
  25644. + part of the implementation of the compression library and is
  25645. + subject to change. Applications should only use zlib.h.
  25646. + */
  25647. +
  25648. +/* Huffman code lookup table entry--this entry is four bytes for machines
  25649. + that have 16-bit pointers (e.g. PC's in the small or medium model). */
  25650. +
  25651. +#ifndef _INFTREES_H
  25652. +#define _INFTREES_H
  25653. +
  25654. +typedef struct inflate_huft_s FAR inflate_huft;
  25655. +
  25656. +struct inflate_huft_s {
  25657. + union {
  25658. + struct {
  25659. + Byte Exop; /* number of extra bits or operation */
  25660. + Byte Bits; /* number of bits in this code or subcode */
  25661. + } what;
  25662. + uInt pad; /* pad structure to a power of 2 (4 bytes for */
  25663. + } word; /* 16-bit, 8 bytes for 32-bit int's) */
  25664. + uInt base; /* literal, length base, distance base,
  25665. + or table offset */
  25666. +};
  25667. +
  25668. +/* Maximum size of dynamic tree. The maximum found in a long but non-
  25669. + exhaustive search was 1004 huft structures (850 for length/literals
  25670. + and 154 for distances, the latter actually the result of an
  25671. + exhaustive search). The actual maximum is not known, but the
  25672. + value below is more than safe. */
  25673. +#define MANY 1440
  25674. +
  25675. +extern int inflate_trees_bits OF((
  25676. + uIntf *, /* 19 code lengths */
  25677. + uIntf *, /* bits tree desired/actual depth */
  25678. + inflate_huft * FAR *, /* bits tree result */
  25679. + inflate_huft *, /* space for trees */
  25680. + z_streamp)); /* for messages */
  25681. +
  25682. +extern int inflate_trees_dynamic OF((
  25683. + uInt, /* number of literal/length codes */
  25684. + uInt, /* number of distance codes */
  25685. + uIntf *, /* that many (total) code lengths */
  25686. + uIntf *, /* literal desired/actual bit depth */
  25687. + uIntf *, /* distance desired/actual bit depth */
  25688. + inflate_huft * FAR *, /* literal/length tree result */
  25689. + inflate_huft * FAR *, /* distance tree result */
  25690. + inflate_huft *, /* space for trees */
  25691. + z_streamp)); /* for messages */
  25692. +
  25693. +extern int inflate_trees_fixed OF((
  25694. + uIntf *, /* literal desired/actual bit depth */
  25695. + uIntf *, /* distance desired/actual bit depth */
  25696. + inflate_huft * FAR *, /* literal/length tree result */
  25697. + inflate_huft * FAR *, /* distance tree result */
  25698. + z_streamp)); /* for memory allocation */
  25699. +
  25700. +#endif /* _INFTREES_H */
  25701. --- /dev/null Tue Mar 11 13:02:56 2003
  25702. +++ linux/net/ipsec/infutil.c Mon Feb 9 13:51:03 2004
  25703. @@ -0,0 +1,87 @@
  25704. +/* inflate_util.c -- data and routines common to blocks and codes
  25705. + * Copyright (C) 1995-2002 Mark Adler
  25706. + * For conditions of distribution and use, see copyright notice in zlib.h
  25707. + */
  25708. +
  25709. +#include <zlib/zutil.h>
  25710. +#include "infblock.h"
  25711. +#include "inftrees.h"
  25712. +#include "infcodes.h"
  25713. +#include "infutil.h"
  25714. +
  25715. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  25716. +
  25717. +/* And'ing with mask[n] masks the lower n bits */
  25718. +uInt inflate_mask[17] = {
  25719. + 0x0000,
  25720. + 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  25721. + 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  25722. +};
  25723. +
  25724. +
  25725. +/* copy as much as possible from the sliding window to the output area */
  25726. +int inflate_flush(s, z, r)
  25727. +inflate_blocks_statef *s;
  25728. +z_streamp z;
  25729. +int r;
  25730. +{
  25731. + uInt n;
  25732. + Bytef *p;
  25733. + Bytef *q;
  25734. +
  25735. + /* local copies of source and destination pointers */
  25736. + p = z->next_out;
  25737. + q = s->read;
  25738. +
  25739. + /* compute number of bytes to copy as far as end of window */
  25740. + n = (uInt)((q <= s->write ? s->write : s->end) - q);
  25741. + if (n > z->avail_out) n = z->avail_out;
  25742. + if (n && r == Z_BUF_ERROR) r = Z_OK;
  25743. +
  25744. + /* update counters */
  25745. + z->avail_out -= n;
  25746. + z->total_out += n;
  25747. +
  25748. + /* update check information */
  25749. + if (s->checkfn != Z_NULL)
  25750. + z->adler = s->check = (*s->checkfn)(s->check, q, n);
  25751. +
  25752. + /* copy as far as end of window */
  25753. + zmemcpy(p, q, n);
  25754. + p += n;
  25755. + q += n;
  25756. +
  25757. + /* see if more to copy at beginning of window */
  25758. + if (q == s->end)
  25759. + {
  25760. + /* wrap pointers */
  25761. + q = s->window;
  25762. + if (s->write == s->end)
  25763. + s->write = s->window;
  25764. +
  25765. + /* compute bytes to copy */
  25766. + n = (uInt)(s->write - q);
  25767. + if (n > z->avail_out) n = z->avail_out;
  25768. + if (n && r == Z_BUF_ERROR) r = Z_OK;
  25769. +
  25770. + /* update counters */
  25771. + z->avail_out -= n;
  25772. + z->total_out += n;
  25773. +
  25774. + /* update check information */
  25775. + if (s->checkfn != Z_NULL)
  25776. + z->adler = s->check = (*s->checkfn)(s->check, q, n);
  25777. +
  25778. + /* copy */
  25779. + zmemcpy(p, q, n);
  25780. + p += n;
  25781. + q += n;
  25782. + }
  25783. +
  25784. + /* update pointers */
  25785. + z->next_out = p;
  25786. + s->read = q;
  25787. +
  25788. + /* done */
  25789. + return r;
  25790. +}
  25791. --- /dev/null Tue Mar 11 13:02:56 2003
  25792. +++ linux/net/ipsec/infutil.h Mon Feb 9 13:51:03 2004
  25793. @@ -0,0 +1,98 @@
  25794. +/* infutil.h -- types and macros common to blocks and codes
  25795. + * Copyright (C) 1995-2002 Mark Adler
  25796. + * For conditions of distribution and use, see copyright notice in zlib.h
  25797. + */
  25798. +
  25799. +/* WARNING: this file should *not* be used by applications. It is
  25800. + part of the implementation of the compression library and is
  25801. + subject to change. Applications should only use zlib.h.
  25802. + */
  25803. +
  25804. +#ifndef _INFUTIL_H
  25805. +#define _INFUTIL_H
  25806. +
  25807. +typedef enum {
  25808. + TYPE, /* get type bits (3, including end bit) */
  25809. + LENS, /* get lengths for stored */
  25810. + STORED, /* processing stored block */
  25811. + TABLE, /* get table lengths */
  25812. + BTREE, /* get bit lengths tree for a dynamic block */
  25813. + DTREE, /* get length, distance trees for a dynamic block */
  25814. + CODES, /* processing fixed or dynamic block */
  25815. + DRY, /* output remaining window bytes */
  25816. + DONE, /* finished last block, done */
  25817. + BAD} /* got a data error--stuck here */
  25818. +inflate_block_mode;
  25819. +
  25820. +/* inflate blocks semi-private state */
  25821. +struct inflate_blocks_state {
  25822. +
  25823. + /* mode */
  25824. + inflate_block_mode mode; /* current inflate_block mode */
  25825. +
  25826. + /* mode dependent information */
  25827. + union {
  25828. + uInt left; /* if STORED, bytes left to copy */
  25829. + struct {
  25830. + uInt table; /* table lengths (14 bits) */
  25831. + uInt index; /* index into blens (or border) */
  25832. + uIntf *blens; /* bit lengths of codes */
  25833. + uInt bb; /* bit length tree depth */
  25834. + inflate_huft *tb; /* bit length decoding tree */
  25835. + } trees; /* if DTREE, decoding info for trees */
  25836. + struct {
  25837. + inflate_codes_statef
  25838. + *codes;
  25839. + } decode; /* if CODES, current state */
  25840. + } sub; /* submode */
  25841. + uInt last; /* true if this block is the last block */
  25842. +
  25843. + /* mode independent information */
  25844. + uInt bitk; /* bits in bit buffer */
  25845. + uLong bitb; /* bit buffer */
  25846. + inflate_huft *hufts; /* single malloc for tree space */
  25847. + Bytef *window; /* sliding window */
  25848. + Bytef *end; /* one byte after sliding window */
  25849. + Bytef *read; /* window read pointer */
  25850. + Bytef *write; /* window write pointer */
  25851. + check_func checkfn; /* check function */
  25852. + uLong check; /* check on output */
  25853. +
  25854. +};
  25855. +
  25856. +
  25857. +/* defines for inflate input/output */
  25858. +/* update pointers and return */
  25859. +#define UPDBITS {s->bitb=b;s->bitk=k;}
  25860. +#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
  25861. +#define UPDOUT {s->write=q;}
  25862. +#define UPDATE {UPDBITS UPDIN UPDOUT}
  25863. +#define LEAVE {UPDATE return inflate_flush(s,z,r);}
  25864. +/* get bytes and bits */
  25865. +#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  25866. +#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
  25867. +#define NEXTBYTE (n--,*p++)
  25868. +#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  25869. +#define DUMPBITS(j) {b>>=(j);k-=(j);}
  25870. +/* output bytes */
  25871. +#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  25872. +#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
  25873. +#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  25874. +#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
  25875. +#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
  25876. +#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  25877. +/* load local pointers */
  25878. +#define LOAD {LOADIN LOADOUT}
  25879. +
  25880. +/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
  25881. +extern uInt inflate_mask[17];
  25882. +
  25883. +/* copy as much as possible from the sliding window to the output area */
  25884. +extern int inflate_flush OF((
  25885. + inflate_blocks_statef *,
  25886. + z_streamp ,
  25887. + int));
  25888. +
  25889. +struct internal_state {int dummy;}; /* for buggy compilers */
  25890. +
  25891. +#endif /* _INFUTIL_H */
  25892. --- /dev/null Tue Mar 11 13:02:56 2003
  25893. +++ linux/net/ipsec/initaddr.c Mon Feb 9 13:51:03 2004
  25894. @@ -0,0 +1,50 @@
  25895. +/*
  25896. + * initialize address structure
  25897. + * Copyright (C) 2000 Henry Spencer.
  25898. + *
  25899. + * This library is free software; you can redistribute it and/or modify it
  25900. + * under the terms of the GNU Library General Public License as published by
  25901. + * the Free Software Foundation; either version 2 of the License, or (at your
  25902. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  25903. + *
  25904. + * This library is distributed in the hope that it will be useful, but
  25905. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  25906. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  25907. + * License for more details.
  25908. + *
  25909. + * RCSID $Id: initaddr.c,v 1.6 2004/07/10 07:43:47 mcr Exp $
  25910. + */
  25911. +#include "openswan.h"
  25912. +
  25913. +/*
  25914. + - initaddr - initialize ip_address from bytes
  25915. + */
  25916. +err_t /* NULL for success, else string literal */
  25917. +initaddr(src, srclen, af, dst)
  25918. +const unsigned char *src;
  25919. +size_t srclen;
  25920. +int af; /* address family */
  25921. +ip_address *dst;
  25922. +{
  25923. + switch (af) {
  25924. + case AF_INET:
  25925. + if (srclen != 4)
  25926. + return "IPv4 address must be exactly 4 bytes";
  25927. + dst->u.v4.sin_family = af;
  25928. + dst->u.v4.sin_port = 0; /* unused */
  25929. + memcpy((char *)&dst->u.v4.sin_addr.s_addr, src, srclen);
  25930. + break;
  25931. + case AF_INET6:
  25932. + if (srclen != 16)
  25933. + return "IPv6 address must be exactly 16 bytes";
  25934. + dst->u.v6.sin6_family = af;
  25935. + dst->u.v6.sin6_flowinfo = 0; /* unused */
  25936. + dst->u.v6.sin6_port = 0; /* unused */
  25937. + memcpy((char *)&dst->u.v6.sin6_addr, src, srclen);
  25938. + break;
  25939. + default:
  25940. + return "unknown address family in initaddr";
  25941. + break;
  25942. + }
  25943. + return NULL;
  25944. +}
  25945. --- /dev/null Tue Mar 11 13:02:56 2003
  25946. +++ linux/net/ipsec/ipcomp.c Mon Feb 9 13:51:03 2004
  25947. @@ -0,0 +1,701 @@
  25948. +/*
  25949. + * IPCOMP zlib interface code.
  25950. + * Copyright (C) 2000 Svenning Soerensen <svenning@post5.tele.dk>
  25951. + * Copyright (C) 2000, 2001 Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  25952. + *
  25953. + * This program is free software; you can redistribute it and/or modify it
  25954. + * under the terms of the GNU General Public License as published by the
  25955. + * Free Software Foundation; either version 2 of the License, or (at your
  25956. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  25957. + *
  25958. + * This program is distributed in the hope that it will be useful, but
  25959. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  25960. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  25961. + * for more details.
  25962. + */
  25963. +
  25964. +char ipcomp_c_version[] = "RCSID $Id: ipcomp.c,v 1.41.2.5 2006/10/06 21:39:26 paul Exp $";
  25965. +
  25966. +/* SSS */
  25967. +
  25968. +#ifndef AUTOCONF_INCLUDED
  25969. +#include <linux/config.h>
  25970. +#endif
  25971. +#include <linux/version.h>
  25972. +
  25973. +#define __NO_VERSION__
  25974. +#include <linux/module.h>
  25975. +#include <linux/kernel.h> /* printk() */
  25976. +
  25977. +#include "openswan/ipsec_param.h"
  25978. +
  25979. +#ifdef MALLOC_SLAB
  25980. +# include <linux/slab.h> /* kmalloc() */
  25981. +#else /* MALLOC_SLAB */
  25982. +# include <linux/malloc.h> /* kmalloc() */
  25983. +#endif /* MALLOC_SLAB */
  25984. +#include <linux/errno.h> /* error codes */
  25985. +#include <linux/types.h>
  25986. +#include <linux/netdevice.h>
  25987. +#include <linux/ip.h>
  25988. +#include <linux/skbuff.h>
  25989. +
  25990. +#include <linux/netdevice.h> /* struct device, and other headers */
  25991. +#include <linux/etherdevice.h> /* eth_type_trans */
  25992. +#include <linux/ip.h> /* struct iphdr */
  25993. +#include <linux/skbuff.h>
  25994. +#include <asm/uaccess.h>
  25995. +#include <asm/checksum.h>
  25996. +
  25997. +#include <openswan.h>
  25998. +
  25999. +#include <net/ip.h>
  26000. +
  26001. +#include "openswan/radij.h"
  26002. +#include "openswan/ipsec_encap.h"
  26003. +#include "openswan/ipsec_sa.h"
  26004. +
  26005. +#include "openswan/ipsec_xform.h"
  26006. +#include "openswan/ipsec_tunnel.h"
  26007. +#include "openswan/ipsec_rcv.h" /* sysctl_ipsec_inbound_policy_check */
  26008. +#include "openswan/ipsec_proto.h"
  26009. +#include "openswan/ipcomp.h"
  26010. +#include "zlib/zlib.h"
  26011. +#include "zlib/zutil.h"
  26012. +
  26013. +#include <pfkeyv2.h> /* SADB_X_CALG_DEFLATE */
  26014. +
  26015. +#ifdef CONFIG_KLIPS_DEBUG
  26016. +int sysctl_ipsec_debug_ipcomp = 0;
  26017. +#endif /* CONFIG_KLIPS_DEBUG */
  26018. +
  26019. +static
  26020. +struct sk_buff *skb_copy_ipcomp(struct sk_buff *skb, int data_growth, int gfp_mask);
  26021. +
  26022. +static
  26023. +voidpf my_zcalloc(voidpf opaque, uInt items, uInt size)
  26024. +{
  26025. + return (voidpf) kmalloc(items*size, GFP_ATOMIC);
  26026. +}
  26027. +
  26028. +static
  26029. +void my_zfree(voidpf opaque, voidpf address)
  26030. +{
  26031. + kfree(address);
  26032. +}
  26033. +
  26034. +/*
  26035. + * We use this function because sometimes we want to pass a negative offset
  26036. + * into skb_put(), this does not work on 64bit platforms because long to
  26037. + * unsigned int casting.
  26038. + */
  26039. +static inline unsigned char *
  26040. +safe_skb_put(struct sk_buff *skb, int extend)
  26041. +{
  26042. + unsigned char *ptr;
  26043. +
  26044. + if (extend>0) {
  26045. + // increase the size of the packet
  26046. + ptr = skb_put(skb, extend);
  26047. + } else {
  26048. + // shrink the size of the packet
  26049. + ptr = skb->tail;
  26050. + skb_trim (skb, skb->len + extend);
  26051. + }
  26052. +
  26053. + return ptr;
  26054. +}
  26055. +
  26056. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags)
  26057. +{
  26058. + struct iphdr *iph;
  26059. + unsigned int iphlen, pyldsz, cpyldsz;
  26060. + unsigned char *buffer;
  26061. + z_stream zs;
  26062. + int zresult;
  26063. +
  26064. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26065. + "klips_debug:skb_compress: .\n");
  26066. +
  26067. + if(skb == NULL) {
  26068. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26069. + "klips_debug:skb_compress: "
  26070. + "passed in NULL skb, returning ERROR.\n");
  26071. + if(flags != NULL) {
  26072. + *flags |= IPCOMP_PARMERROR;
  26073. + }
  26074. + return skb;
  26075. + }
  26076. +
  26077. + if(ips == NULL) {
  26078. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26079. + "klips_debug:skb_compress: "
  26080. + "passed in NULL ipsec_sa needed for cpi, returning ERROR.\n");
  26081. + if(flags) {
  26082. + *flags |= IPCOMP_PARMERROR;
  26083. + }
  26084. + return skb;
  26085. + }
  26086. +
  26087. + if (flags == NULL) {
  26088. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26089. + "klips_debug:skb_compress: "
  26090. + "passed in NULL flags, returning ERROR.\n");
  26091. + ipsec_kfree_skb(skb);
  26092. + return NULL;
  26093. + }
  26094. +
  26095. +#ifdef NET_21
  26096. + iph = skb->nh.iph;
  26097. +#else /* NET_21 */
  26098. + iph = skb->ip_hdr;
  26099. +#endif /* NET_21 */
  26100. +
  26101. + switch (iph->protocol) {
  26102. + case IPPROTO_COMP:
  26103. + case IPPROTO_AH:
  26104. + case IPPROTO_ESP:
  26105. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26106. + "klips_debug:skb_compress: "
  26107. + "skipping compression of packet with ip protocol %d.\n",
  26108. + iph->protocol);
  26109. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26110. + return skb;
  26111. + }
  26112. +
  26113. + /* Don't compress packets already fragmented */
  26114. + if (iph->frag_off & __constant_htons(IP_MF | IP_OFFSET)) {
  26115. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26116. + "klips_debug:skb_compress: "
  26117. + "skipping compression of fragmented packet.\n");
  26118. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26119. + return skb;
  26120. + }
  26121. +
  26122. + iphlen = iph->ihl << 2;
  26123. + pyldsz = ntohs(iph->tot_len) - iphlen;
  26124. +
  26125. + /* Don't compress less than 90 bytes (rfc 2394) */
  26126. + if (pyldsz < 90) {
  26127. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26128. + "klips_debug:skb_compress: "
  26129. + "skipping compression of tiny packet, len=%d.\n",
  26130. + pyldsz);
  26131. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26132. + return skb;
  26133. + }
  26134. +
  26135. + /* Adaptive decision */
  26136. + if (ips->ips_comp_adapt_skip) {
  26137. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26138. + "klips_debug:skb_compress: "
  26139. + "skipping compression: ips_comp_adapt_skip=%d.\n",
  26140. + ips->ips_comp_adapt_skip);
  26141. + ips->ips_comp_adapt_skip--;
  26142. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26143. + return skb;
  26144. + }
  26145. +
  26146. + zs.zalloc = my_zcalloc;
  26147. + zs.zfree = my_zfree;
  26148. + zs.opaque = 0;
  26149. +
  26150. + /* We want to use deflateInit2 because we don't want the adler
  26151. + header. */
  26152. + zresult = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -11,
  26153. + DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  26154. + if (zresult != Z_OK) {
  26155. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26156. + "klips_error:skb_compress: "
  26157. + "deflateInit2() returned error %d (%s), "
  26158. + "skipping compression.\n",
  26159. + zresult,
  26160. + zs.msg ? zs.msg : zError(zresult));
  26161. + *flags |= IPCOMP_COMPRESSIONERROR;
  26162. + return skb;
  26163. + }
  26164. +
  26165. +
  26166. + /* Max output size. Result should be max this size.
  26167. + * Implementation specific tweak:
  26168. + * If it's not at least 32 bytes and 6.25% smaller than
  26169. + * the original packet, it's probably not worth wasting
  26170. + * the receiver's CPU cycles decompressing it.
  26171. + * Your mileage may vary.
  26172. + */
  26173. + cpyldsz = pyldsz - sizeof(struct ipcomphdr) - (pyldsz <= 512 ? 32 : pyldsz >> 4);
  26174. +
  26175. + buffer = kmalloc(cpyldsz, GFP_ATOMIC);
  26176. + if (!buffer) {
  26177. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26178. + "klips_error:skb_compress: "
  26179. + "unable to kmalloc(%d, GFP_ATOMIC), "
  26180. + "skipping compression.\n",
  26181. + cpyldsz);
  26182. + *flags |= IPCOMP_COMPRESSIONERROR;
  26183. + deflateEnd(&zs);
  26184. + return skb;
  26185. + }
  26186. +
  26187. +#ifdef CONFIG_KLIPS_DEBUG
  26188. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26189. + __u8 *c;
  26190. +
  26191. + c = (__u8*)iph + iphlen;
  26192. + ipsec_dmp_block("compress before", c, pyldsz);
  26193. + }
  26194. +#endif /* CONFIG_KLIPS_DEBUG */
  26195. +
  26196. + zs.next_in = (char *) iph + iphlen; /* start of payload */
  26197. + zs.avail_in = pyldsz;
  26198. + zs.next_out = buffer; /* start of compressed payload */
  26199. + zs.avail_out = cpyldsz;
  26200. +
  26201. + /* Finish compression in one step */
  26202. + zresult = deflate(&zs, Z_FINISH);
  26203. +
  26204. + /* Free all dynamically allocated buffers */
  26205. + deflateEnd(&zs);
  26206. + if (zresult != Z_STREAM_END) {
  26207. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26208. + kfree(buffer);
  26209. +
  26210. + /* Adjust adaptive counters */
  26211. + if (++(ips->ips_comp_adapt_tries) == IPCOMP_ADAPT_INITIAL_TRIES) {
  26212. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26213. + "klips_debug:skb_compress: "
  26214. + "first %d packets didn't compress, "
  26215. + "skipping next %d\n",
  26216. + IPCOMP_ADAPT_INITIAL_TRIES,
  26217. + IPCOMP_ADAPT_INITIAL_SKIP);
  26218. + ips->ips_comp_adapt_skip = IPCOMP_ADAPT_INITIAL_SKIP;
  26219. + }
  26220. + else if (ips->ips_comp_adapt_tries == IPCOMP_ADAPT_INITIAL_TRIES + IPCOMP_ADAPT_SUBSEQ_TRIES) {
  26221. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26222. + "klips_debug:skb_compress: "
  26223. + "next %d packets didn't compress, "
  26224. + "skipping next %d\n",
  26225. + IPCOMP_ADAPT_SUBSEQ_TRIES,
  26226. + IPCOMP_ADAPT_SUBSEQ_SKIP);
  26227. + ips->ips_comp_adapt_skip = IPCOMP_ADAPT_SUBSEQ_SKIP;
  26228. + ips->ips_comp_adapt_tries = IPCOMP_ADAPT_INITIAL_TRIES;
  26229. + }
  26230. +
  26231. + return skb;
  26232. + }
  26233. +
  26234. + /* resulting compressed size */
  26235. + cpyldsz -= zs.avail_out;
  26236. +
  26237. + /* Insert IPCOMP header */
  26238. + ((struct ipcomphdr*) ((char*) iph + iphlen))->ipcomp_nh = iph->protocol;
  26239. + ((struct ipcomphdr*) ((char*) iph + iphlen))->ipcomp_flags = 0;
  26240. + /* use the bottom 16 bits of the spi for the cpi. The top 16 bits are
  26241. + for internal reference only. */
  26242. + ((struct ipcomphdr*) (((char*)iph) + iphlen))->ipcomp_cpi = htons((__u16)(ntohl(ips->ips_said.spi) & 0x0000ffff));
  26243. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26244. + "klips_debug:skb_compress: "
  26245. + "spi=%08x, spi&0xffff=%04x, cpi=%04x, payload size: raw=%d, comp=%d.\n",
  26246. + ntohl(ips->ips_said.spi),
  26247. + ntohl(ips->ips_said.spi) & 0x0000ffff,
  26248. + ntohs(((struct ipcomphdr*)(((char*)iph)+iphlen))->ipcomp_cpi),
  26249. + pyldsz,
  26250. + cpyldsz);
  26251. +
  26252. + /* Update IP header */
  26253. + iph->protocol = IPPROTO_COMP;
  26254. + iph->tot_len = htons(iphlen + sizeof(struct ipcomphdr) + cpyldsz);
  26255. +#if 1 /* XXX checksum is done by ipsec_tunnel ? */
  26256. + iph->check = 0;
  26257. + iph->check = ip_fast_csum((char *) iph, iph->ihl);
  26258. +#endif
  26259. +
  26260. + /* Copy compressed payload */
  26261. + memcpy((char *) iph + iphlen + sizeof(struct ipcomphdr),
  26262. + buffer,
  26263. + cpyldsz);
  26264. + kfree(buffer);
  26265. +
  26266. + /* Update skb length/tail by "unputting" the shrinkage */
  26267. + safe_skb_put (skb, cpyldsz + sizeof(struct ipcomphdr) - pyldsz);
  26268. +
  26269. +#ifdef CONFIG_KLIPS_DEBUG
  26270. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26271. + __u8 *c;
  26272. +
  26273. + c = (__u8*)iph + iphlen + sizeof(struct ipcomphdr);
  26274. + ipsec_dmp_block("compress result", c, cpyldsz);
  26275. + }
  26276. +#endif /* CONFIG_KLIPS_DEBUG */
  26277. +
  26278. + ips->ips_comp_adapt_skip = 0;
  26279. + ips->ips_comp_adapt_tries = 0;
  26280. +
  26281. + return skb;
  26282. +}
  26283. +
  26284. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags)
  26285. +{
  26286. + struct sk_buff *nskb = NULL;
  26287. +
  26288. + /* original ip header */
  26289. + struct iphdr *oiph, *iph;
  26290. + unsigned int iphlen, pyldsz, cpyldsz;
  26291. + z_stream zs;
  26292. + int zresult;
  26293. +
  26294. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26295. + "klips_debug:skb_decompress: .\n");
  26296. +
  26297. + if(!skb) {
  26298. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26299. + "klips_error:skb_decompress: "
  26300. + "passed in NULL skb, returning ERROR.\n");
  26301. + if (flags) *flags |= IPCOMP_PARMERROR;
  26302. + return skb;
  26303. + }
  26304. +
  26305. + if(!ips && sysctl_ipsec_inbound_policy_check) {
  26306. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26307. + "klips_error:skb_decompress: "
  26308. + "passed in NULL ipsec_sa needed for comp alg, returning ERROR.\n");
  26309. + if (flags) *flags |= IPCOMP_PARMERROR;
  26310. + return skb;
  26311. + }
  26312. +
  26313. + if (!flags) {
  26314. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26315. + "klips_error:skb_decompress: "
  26316. + "passed in NULL flags, returning ERROR.\n");
  26317. + ipsec_kfree_skb(skb);
  26318. + return NULL;
  26319. + }
  26320. +
  26321. +#ifdef NET_21
  26322. + oiph = skb->nh.iph;
  26323. +#else /* NET_21 */
  26324. + oiph = skb->ip_hdr;
  26325. +#endif /* NET_21 */
  26326. +
  26327. + iphlen = oiph->ihl << 2;
  26328. +
  26329. + if (oiph->protocol != IPPROTO_COMP) {
  26330. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26331. + "klips_error:skb_decompress: "
  26332. + "called with non-IPCOMP packet (protocol=%d),"
  26333. + "skipping decompression.\n",
  26334. + oiph->protocol);
  26335. + *flags |= IPCOMP_PARMERROR;
  26336. + return skb;
  26337. + }
  26338. +
  26339. + if ( (((struct ipcomphdr*)((char*) oiph + iphlen))->ipcomp_flags != 0)
  26340. + || ((((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_cpi
  26341. + != htons(SADB_X_CALG_DEFLATE))
  26342. + && sysctl_ipsec_inbound_policy_check
  26343. + && (!ips || (ips && (ips->ips_encalg != SADB_X_CALG_DEFLATE)))) ) {
  26344. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26345. + "klips_error:skb_decompress: "
  26346. + "called with incompatible IPCOMP packet (flags=%d, "
  26347. + "cpi=%d), ips-compalg=%d, skipping decompression.\n",
  26348. + ntohs(((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_flags),
  26349. + ntohs(((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_cpi),
  26350. + ips ? ips->ips_encalg : 0);
  26351. + *flags |= IPCOMP_PARMERROR;
  26352. +
  26353. + return skb;
  26354. + }
  26355. +
  26356. + if (ntohs(oiph->frag_off) & ~0x4000) {
  26357. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26358. + "klips_error:skb_decompress: "
  26359. + "called with fragmented IPCOMP packet, "
  26360. + "skipping decompression.\n");
  26361. + *flags |= IPCOMP_PARMERROR;
  26362. + return skb;
  26363. + }
  26364. +
  26365. + /* original compressed payload size */
  26366. + cpyldsz = ntohs(oiph->tot_len) - iphlen - sizeof(struct ipcomphdr);
  26367. +
  26368. + zs.zalloc = my_zcalloc;
  26369. + zs.zfree = my_zfree;
  26370. + zs.opaque = 0;
  26371. +
  26372. + zs.next_in = (char *) oiph + iphlen + sizeof(struct ipcomphdr);
  26373. + zs.avail_in = cpyldsz;
  26374. +
  26375. + /* Maybe we should be a bit conservative about memory
  26376. + requirements and use inflateInit2 */
  26377. + /* Beware, that this might make us unable to decompress packets
  26378. + from other implementations - HINT: check PGPnet source code */
  26379. + /* We want to use inflateInit2 because we don't want the adler
  26380. + header. */
  26381. + zresult = inflateInit2(&zs, -15);
  26382. + if (zresult != Z_OK) {
  26383. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26384. + "klips_error:skb_decompress: "
  26385. + "inflateInit2() returned error %d (%s), "
  26386. + "skipping decompression.\n",
  26387. + zresult,
  26388. + zs.msg ? zs.msg : zError(zresult));
  26389. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26390. +
  26391. + return skb;
  26392. + }
  26393. +
  26394. + /* We have no way of knowing the exact length of the resulting
  26395. + decompressed output before we have actually done the decompression.
  26396. + For now, we guess that the packet will not be bigger than the
  26397. + attached ipsec device's mtu or 16260, whichever is biggest.
  26398. + This may be wrong, since the sender's mtu may be bigger yet.
  26399. + XXX This must be dealt with later XXX
  26400. + */
  26401. +
  26402. + /* max payload size */
  26403. + pyldsz = skb->dev ? (skb->dev->mtu < 16260 ? 16260 : skb->dev->mtu)
  26404. + : (65520 - iphlen);
  26405. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26406. + "klips_debug:skb_decompress: "
  26407. + "max payload size: %d\n", pyldsz);
  26408. +
  26409. + while (pyldsz > (cpyldsz + sizeof(struct ipcomphdr)) &&
  26410. + (nskb = skb_copy_ipcomp(skb,
  26411. + pyldsz - cpyldsz - sizeof(struct ipcomphdr),
  26412. + GFP_ATOMIC)) == NULL) {
  26413. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26414. + "klips_error:skb_decompress: "
  26415. + "unable to skb_copy_ipcomp(skb, %d, GFP_ATOMIC), "
  26416. + "trying with less payload size.\n",
  26417. + (int)(pyldsz - cpyldsz - sizeof(struct ipcomphdr)));
  26418. + pyldsz >>=1;
  26419. + }
  26420. +
  26421. + if (!nskb) {
  26422. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26423. + "klips_error:skb_decompress: "
  26424. + "unable to allocate memory, dropping packet.\n");
  26425. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26426. + inflateEnd(&zs);
  26427. +
  26428. + return skb;
  26429. + }
  26430. +
  26431. +#ifdef CONFIG_KLIPS_DEBUG
  26432. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26433. + __u8 *c;
  26434. +
  26435. + c = (__u8*)oiph + iphlen + sizeof(struct ipcomphdr);
  26436. + ipsec_dmp_block("decompress before", c, cpyldsz);
  26437. + }
  26438. +#endif /* CONFIG_KLIPS_DEBUG */
  26439. +
  26440. +#ifdef NET_21
  26441. + iph = nskb->nh.iph;
  26442. +#else /* NET_21 */
  26443. + iph = nskb->ip_hdr;
  26444. +#endif /* NET_21 */
  26445. + zs.next_out = (char *)iph + iphlen;
  26446. + zs.avail_out = pyldsz;
  26447. +
  26448. + zresult = inflate(&zs, Z_SYNC_FLUSH);
  26449. +
  26450. + /* work around a bug in zlib, which sometimes wants to taste an extra
  26451. + * byte when being used in the (undocumented) raw deflate mode.
  26452. + */
  26453. + if (zresult == Z_OK && !zs.avail_in && zs.avail_out) {
  26454. + __u8 zerostuff = 0;
  26455. +
  26456. + zs.next_in = &zerostuff;
  26457. + zs.avail_in = 1;
  26458. + zresult = inflate(&zs, Z_FINISH);
  26459. + }
  26460. +
  26461. + inflateEnd(&zs);
  26462. + if (zresult != Z_STREAM_END) {
  26463. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26464. + "klips_error:skb_decompress: "
  26465. + "inflate() returned error %d (%s), "
  26466. + "skipping decompression.\n",
  26467. + zresult,
  26468. + zs.msg ? zs.msg : zError(zresult));
  26469. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26470. + ipsec_kfree_skb(nskb);
  26471. +
  26472. + return skb;
  26473. + }
  26474. +
  26475. + /* Update IP header */
  26476. + /* resulting decompressed size */
  26477. + pyldsz -= zs.avail_out;
  26478. + iph->tot_len = htons(iphlen + pyldsz);
  26479. + iph->protocol = ((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_nh;
  26480. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26481. + "klips_debug:skb_decompress: "
  26482. + "spi=%08x, spi&0xffff=%04x, cpi=%04x, payload size: comp=%d, raw=%d, nh=%d.\n",
  26483. + ips ? ntohl(ips->ips_said.spi) : 0,
  26484. + ips ? ntohl(ips->ips_said.spi) & 0x0000ffff : 0,
  26485. + ntohs(((struct ipcomphdr*)(((char*)oiph)+iphlen))->ipcomp_cpi),
  26486. + cpyldsz,
  26487. + pyldsz,
  26488. + iph->protocol);
  26489. +
  26490. +#if 1 /* XXX checksum is done by ipsec_rcv ? */
  26491. + iph->check = 0;
  26492. + iph->check = ip_fast_csum((char*) iph, iph->ihl);
  26493. +#endif
  26494. +
  26495. + /* Update skb length/tail by "unputting" the unused data area */
  26496. + safe_skb_put(nskb, -zs.avail_out);
  26497. +
  26498. + ipsec_kfree_skb(skb);
  26499. +
  26500. + if (iph->protocol == IPPROTO_COMP)
  26501. + {
  26502. +#ifdef CONFIG_KLIPS_DEBUG
  26503. + if(sysctl_ipsec_debug_ipcomp)
  26504. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26505. + "klips_debug:skb_decompress: "
  26506. + "Eh? inner packet is also compressed, dropping.\n");
  26507. +#endif /* CONFIG_KLIPS_DEBUG */
  26508. +
  26509. + ipsec_kfree_skb(nskb);
  26510. + return NULL;
  26511. + }
  26512. +
  26513. +#ifdef CONFIG_KLIPS_DEBUG
  26514. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26515. + __u8 *c;
  26516. +
  26517. + c = (__u8*)iph + iphlen;
  26518. + ipsec_dmp_block("decompress result", c, pyldsz);
  26519. + }
  26520. +#endif /* CONFIG_KLIPS_DEBUG */
  26521. +
  26522. + return nskb;
  26523. +}
  26524. +
  26525. +
  26526. +/* this is derived from skb_copy() in linux 2.2.14 */
  26527. +/* May be incompatible with other kernel versions!! */
  26528. +static
  26529. +struct sk_buff *skb_copy_ipcomp(struct sk_buff *skb, int data_growth, int gfp_mask)
  26530. +{
  26531. + struct sk_buff *n;
  26532. + struct iphdr *iph;
  26533. + unsigned long offset;
  26534. + unsigned int iphlen;
  26535. +
  26536. + if(!skb) {
  26537. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26538. + "klips_debug:skb_copy_ipcomp: "
  26539. + "passed in NULL skb, returning NULL.\n");
  26540. + return NULL;
  26541. + }
  26542. +
  26543. + /*
  26544. + * Allocate the copy buffer
  26545. + */
  26546. +
  26547. +#ifdef NET_21
  26548. + iph = skb->nh.iph;
  26549. +#else /* NET_21 */
  26550. + iph = skb->ip_hdr;
  26551. +#endif /* NET_21 */
  26552. + if (!iph) return NULL;
  26553. + iphlen = iph->ihl << 2;
  26554. +
  26555. + n=alloc_skb(skb->end - skb->head + data_growth, gfp_mask);
  26556. + if(n==NULL)
  26557. + return NULL;
  26558. +
  26559. + /*
  26560. + * Shift between the two data areas in bytes
  26561. + */
  26562. +
  26563. + offset=n->head-skb->head;
  26564. +
  26565. + /* Set the data pointer */
  26566. + skb_reserve(n,skb->data-skb->head);
  26567. + /* Set the tail pointer and length */
  26568. + safe_skb_put(n,skb->len+data_growth);
  26569. + /* Copy the bytes up to and including the ip header */
  26570. + memcpy(n->head,
  26571. + skb->head,
  26572. + ((char *)iph - (char *)skb->head) + iphlen);
  26573. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  26574. + n->list=NULL;
  26575. +#endif
  26576. + n->next=NULL;
  26577. + n->prev=NULL;
  26578. + n->sk=NULL;
  26579. + n->dev=skb->dev;
  26580. + if (skb->h.raw)
  26581. + n->h.raw=skb->h.raw+offset;
  26582. + else
  26583. + n->h.raw=NULL;
  26584. + n->protocol=skb->protocol;
  26585. +#ifdef NET_21
  26586. + n->csum = 0;
  26587. + n->priority=skb->priority;
  26588. + n->dst=dst_clone(skb->dst);
  26589. + n->nh.raw=skb->nh.raw+offset;
  26590. +#ifndef NETDEV_23
  26591. + n->is_clone=0;
  26592. +#endif /* NETDEV_23 */
  26593. + atomic_set(&n->users, 1);
  26594. + n->destructor = NULL;
  26595. +#ifdef HAVE_SOCK_SECURITY
  26596. + n->security=skb->security;
  26597. +#endif
  26598. + memcpy(n->cb, skb->cb, sizeof(skb->cb));
  26599. +#ifdef CONFIG_IP_FIREWALL
  26600. + n->fwmark = skb->fwmark;
  26601. +#endif
  26602. +#else /* NET_21 */
  26603. + n->link3=NULL;
  26604. + n->when=skb->when;
  26605. + n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset);
  26606. + n->saddr=skb->saddr;
  26607. + n->daddr=skb->daddr;
  26608. + n->raddr=skb->raddr;
  26609. + n->seq=skb->seq;
  26610. + n->end_seq=skb->end_seq;
  26611. + n->ack_seq=skb->ack_seq;
  26612. + n->acked=skb->acked;
  26613. + n->free=1;
  26614. + n->arp=skb->arp;
  26615. + n->tries=0;
  26616. + n->lock=0;
  26617. + n->users=0;
  26618. + memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
  26619. +#endif /* NET_21 */
  26620. + if (skb->mac.raw)
  26621. + n->mac.raw=skb->mac.raw+offset;
  26622. + else
  26623. + n->mac.raw=NULL;
  26624. +#ifndef NETDEV_23
  26625. + n->used=skb->used;
  26626. +#endif /* !NETDEV_23 */
  26627. + n->pkt_type=skb->pkt_type;
  26628. +#ifndef NETDEV_23
  26629. + n->pkt_bridged=skb->pkt_bridged;
  26630. +#endif /* NETDEV_23 */
  26631. + n->ip_summed=0;
  26632. +#ifdef HAVE_TSTAMP
  26633. + n->tstamp = skb->tstamp;
  26634. +#else
  26635. + n->stamp=skb->stamp;
  26636. +#endif
  26637. +#ifndef NETDEV_23 /* this seems to have been removed in 2.4 */
  26638. +#if defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE)
  26639. + n->shapelatency=skb->shapelatency; /* Latency on frame */
  26640. + n->shapeclock=skb->shapeclock; /* Time it should go out */
  26641. + n->shapelen=skb->shapelen; /* Frame length in clocks */
  26642. + n->shapestamp=skb->shapestamp; /* Stamp for shaper */
  26643. + n->shapepend=skb->shapepend; /* Pending */
  26644. +#endif /* defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE) */
  26645. +#endif /* NETDEV_23 */
  26646. +
  26647. + return n;
  26648. +}
  26649. --- /dev/null Tue Mar 11 13:02:56 2003
  26650. +++ linux/net/ipsec/ipsec_ah.c Mon Feb 9 13:51:03 2004
  26651. @@ -0,0 +1,407 @@
  26652. +/*
  26653. + * processing code for AH
  26654. + * Copyright (C) 2003-2004 Michael Richardson <mcr@xelerance.com>
  26655. + *
  26656. + * This program is free software; you can redistribute it and/or modify it
  26657. + * under the terms of the GNU General Public License as published by the
  26658. + * Free Software Foundation; either version 2 of the License, or (at your
  26659. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  26660. + *
  26661. + * This program is distributed in the hope that it will be useful, but
  26662. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  26663. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  26664. + * for more details.
  26665. + */
  26666. +
  26667. +char ipsec_ah_c_version[] = "RCSID $Id: ipsec_ah.c,v 1.12.2.2 2006/10/06 21:39:26 paul Exp $";
  26668. +#ifndef AUTOCONF_INCLUDED
  26669. +#include <linux/config.h>
  26670. +#endif
  26671. +#include <linux/version.h>
  26672. +
  26673. +#define __NO_VERSION__
  26674. +#include <linux/module.h>
  26675. +#include <linux/kernel.h> /* printk() */
  26676. +
  26677. +#include "openswan/ipsec_param.h"
  26678. +
  26679. +#ifdef MALLOC_SLAB
  26680. +# include <linux/slab.h> /* kmalloc() */
  26681. +#else /* MALLOC_SLAB */
  26682. +# include <linux/malloc.h> /* kmalloc() */
  26683. +#endif /* MALLOC_SLAB */
  26684. +#include <linux/errno.h> /* error codes */
  26685. +#include <linux/types.h> /* size_t */
  26686. +#include <linux/interrupt.h> /* mark_bh */
  26687. +
  26688. +#include <linux/netdevice.h> /* struct device, and other headers */
  26689. +#include <linux/etherdevice.h> /* eth_type_trans */
  26690. +#include <linux/ip.h> /* struct iphdr */
  26691. +#include <linux/skbuff.h>
  26692. +#include <openswan.h>
  26693. +#ifdef SPINLOCK
  26694. +# ifdef SPINLOCK_23
  26695. +# include <linux/spinlock.h> /* *lock* */
  26696. +# else /* SPINLOCK_23 */
  26697. +# include <asm/spinlock.h> /* *lock* */
  26698. +# endif /* SPINLOCK_23 */
  26699. +#endif /* SPINLOCK */
  26700. +
  26701. +#include <net/ip.h>
  26702. +#include <net/protocol.h>
  26703. +
  26704. +#include "openswan/radij.h"
  26705. +#include "openswan/ipsec_encap.h"
  26706. +#include "openswan/ipsec_sa.h"
  26707. +
  26708. +#include "openswan/ipsec_radij.h"
  26709. +#include "openswan/ipsec_xform.h"
  26710. +#include "openswan/ipsec_tunnel.h"
  26711. +#include "openswan/ipsec_rcv.h"
  26712. +#include "openswan/ipsec_xmit.h"
  26713. +
  26714. +#include "openswan/ipsec_auth.h"
  26715. +#include "openswan/ipsec_ah.h"
  26716. +#include "openswan/ipsec_proto.h"
  26717. +
  26718. +__u32 zeroes[AH_AMAX];
  26719. +
  26720. +enum ipsec_rcv_value
  26721. +ipsec_rcv_ah_checks(struct ipsec_rcv_state *irs,
  26722. + struct sk_buff *skb)
  26723. +{
  26724. + int ahminlen;
  26725. +
  26726. + ahminlen = irs->hard_header_len + sizeof(struct iphdr);
  26727. +
  26728. + /* take care not to deref this pointer until we check the minlen though */
  26729. + irs->protostuff.ahstuff.ahp = (struct ahhdr *)skb->h.raw;
  26730. +
  26731. + if((skb->len < ahminlen+sizeof(struct ahhdr)) ||
  26732. + (skb->len < ahminlen+(irs->protostuff.ahstuff.ahp->ah_hl << 2))) {
  26733. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  26734. + "klips_debug:ipsec_rcv: "
  26735. + "runt ah packet of skb->len=%d received from %s, dropped.\n",
  26736. + skb->len,
  26737. + irs->ipsaddr_txt);
  26738. + if(irs->stats) {
  26739. + irs->stats->rx_errors++;
  26740. + }
  26741. + return IPSEC_RCV_BADLEN;
  26742. + }
  26743. +
  26744. + irs->said.spi = irs->protostuff.ahstuff.ahp->ah_spi;
  26745. +
  26746. + /* XXX we only support the one 12-byte authenticator for now */
  26747. + if(irs->protostuff.ahstuff.ahp->ah_hl != ((AHHMAC_HASHLEN+AHHMAC_RPLLEN) >> 2)) {
  26748. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  26749. + "klips_debug:ipsec_rcv: "
  26750. + "bad authenticator length %ld, expected %lu from %s.\n",
  26751. + (long)(irs->protostuff.ahstuff.ahp->ah_hl << 2),
  26752. + (unsigned long) sizeof(struct ahhdr),
  26753. + irs->ipsaddr_txt);
  26754. + if(irs->stats) {
  26755. + irs->stats->rx_errors++;
  26756. + }
  26757. + return IPSEC_RCV_BADLEN;
  26758. + }
  26759. +
  26760. + return IPSEC_RCV_OK;
  26761. +}
  26762. +
  26763. +
  26764. +enum ipsec_rcv_value
  26765. +ipsec_rcv_ah_setup_auth(struct ipsec_rcv_state *irs,
  26766. + struct sk_buff *skb,
  26767. + __u32 *replay,
  26768. + unsigned char **authenticator)
  26769. +{
  26770. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26771. +
  26772. + *replay = ntohl(ahp->ah_rpl);
  26773. + *authenticator = ahp->ah_data;
  26774. +
  26775. + return IPSEC_RCV_OK;
  26776. +}
  26777. +
  26778. +enum ipsec_rcv_value
  26779. +ipsec_rcv_ah_authcalc(struct ipsec_rcv_state *irs,
  26780. + struct sk_buff *skb)
  26781. +{
  26782. + struct auth_alg *aa;
  26783. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26784. + union {
  26785. + MD5_CTX md5;
  26786. + SHA1_CTX sha1;
  26787. + } tctx;
  26788. + struct iphdr ipo;
  26789. + int ahhlen;
  26790. +
  26791. + aa = irs->authfuncs;
  26792. +
  26793. + /* copy the initialized keying material */
  26794. + memcpy(&tctx, irs->ictx, irs->ictx_len);
  26795. +
  26796. + ipo = *irs->ipp;
  26797. + ipo.tos = 0; /* mutable RFC 2402 3.3.3.1.1.1 */
  26798. + ipo.frag_off = 0;
  26799. + ipo.ttl = 0;
  26800. + ipo.check = 0;
  26801. +
  26802. +
  26803. + /* do the sanitized header */
  26804. + (*aa->update)((void*)&tctx, (caddr_t)&ipo, sizeof(struct iphdr));
  26805. +
  26806. + /* XXX we didn't do the options here! */
  26807. +
  26808. + /* now do the AH header itself */
  26809. + ahhlen = AH_BASIC_LEN + (ahp->ah_hl << 2);
  26810. + (*aa->update)((void*)&tctx, (caddr_t)ahp, ahhlen - AHHMAC_HASHLEN);
  26811. +
  26812. + /* now, do some zeroes */
  26813. + (*aa->update)((void*)&tctx, (caddr_t)zeroes, AHHMAC_HASHLEN);
  26814. +
  26815. + /* finally, do the packet contents themselves */
  26816. + (*aa->update)((void*)&tctx,
  26817. + (caddr_t)skb->h.raw + ahhlen,
  26818. + skb->len - ahhlen);
  26819. +
  26820. + (*aa->final)(irs->hash, (void *)&tctx);
  26821. +
  26822. + memcpy(&tctx, irs->octx, irs->octx_len);
  26823. +
  26824. + (*aa->update)((void *)&tctx, irs->hash, aa->hashlen);
  26825. + (*aa->final)(irs->hash, (void *)&tctx);
  26826. +
  26827. + return IPSEC_RCV_OK;
  26828. +}
  26829. +
  26830. +enum ipsec_rcv_value
  26831. +ipsec_rcv_ah_decap(struct ipsec_rcv_state *irs)
  26832. +{
  26833. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26834. + struct sk_buff *skb;
  26835. + int ahhlen;
  26836. +
  26837. + skb=irs->skb;
  26838. +
  26839. + ahhlen = AH_BASIC_LEN + (ahp->ah_hl << 2);
  26840. +
  26841. + irs->ipp->tot_len = htons(ntohs(irs->ipp->tot_len) - ahhlen);
  26842. + irs->next_header = ahp->ah_nh;
  26843. +
  26844. + /*
  26845. + * move the IP header forward by the size of the AH header, which
  26846. + * will remove the the AH header from the packet.
  26847. + */
  26848. + memmove((void *)(skb->nh.raw + ahhlen),
  26849. + (void *)(skb->nh.raw), irs->iphlen);
  26850. +
  26851. + ipsec_rcv_dmp("ah postmove", skb->data, skb->len);
  26852. +
  26853. + /* skb_pull below, will move up by ahhlen */
  26854. +
  26855. + /* XXX not clear how this can happen, as the message indicates */
  26856. + if(skb->len < ahhlen) {
  26857. + printk(KERN_WARNING
  26858. + "klips_error:ipsec_rcv: "
  26859. + "tried to skb_pull ahhlen=%d, %d available. This should never happen, please report.\n",
  26860. + ahhlen,
  26861. + (int)(skb->len));
  26862. + return IPSEC_RCV_DECAPFAIL;
  26863. + }
  26864. + skb_pull(skb, ahhlen);
  26865. +
  26866. + skb->nh.raw = skb->nh.raw + ahhlen;
  26867. + irs->ipp = skb->nh.iph;
  26868. +
  26869. + ipsec_rcv_dmp("ah postpull", (void *)skb->nh.iph, skb->len);
  26870. +
  26871. + return IPSEC_RCV_OK;
  26872. +}
  26873. +
  26874. +enum ipsec_xmit_value
  26875. +ipsec_xmit_ah_setup(struct ipsec_xmit_state *ixs)
  26876. +{
  26877. + struct iphdr ipo;
  26878. + struct ahhdr *ahp;
  26879. + __u8 hash[AH_AMAX];
  26880. + union {
  26881. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  26882. + MD5_CTX md5;
  26883. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  26884. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  26885. + SHA1_CTX sha1;
  26886. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  26887. + } tctx;
  26888. + unsigned char *dat = (unsigned char *)ixs->iph;
  26889. +
  26890. + ahp = (struct ahhdr *)(dat + ixs->iphlen);
  26891. + ahp->ah_spi = ixs->ipsp->ips_said.spi;
  26892. + ahp->ah_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  26893. + ahp->ah_rv = 0;
  26894. + ahp->ah_nh = ixs->iph->protocol;
  26895. + ahp->ah_hl = (sizeof(struct ahhdr) >> 2) - sizeof(__u64)/sizeof(__u32);
  26896. + ixs->iph->protocol = IPPROTO_AH;
  26897. + ipsec_xmit_dmp("ahp", (char*)ahp, sizeof(*ahp));
  26898. +
  26899. + ipo = *ixs->iph;
  26900. + ipo.tos = 0;
  26901. + ipo.frag_off = 0;
  26902. + ipo.ttl = 0;
  26903. + ipo.check = 0;
  26904. + ipsec_xmit_dmp("ipo", (char*)&ipo, sizeof(ipo));
  26905. +
  26906. + switch(ixs->ipsp->ips_authalg) {
  26907. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  26908. + case AH_MD5:
  26909. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  26910. + ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  26911. + osMD5Update(&tctx.md5, (unsigned char *)&ipo, sizeof (struct iphdr));
  26912. + ipsec_xmit_dmp("ictx+ipo", (char*)&tctx.md5, sizeof(tctx.md5));
  26913. + osMD5Update(&tctx.md5, (unsigned char *)ahp,
  26914. + sizeof(struct ahhdr) - sizeof(ahp->ah_data));
  26915. + ipsec_xmit_dmp("ictx+ahp", (char*)&tctx.md5, sizeof(tctx.md5));
  26916. + osMD5Update(&tctx.md5, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  26917. + ipsec_xmit_dmp("ictx+zeroes", (char*)&tctx.md5, sizeof(tctx.md5));
  26918. + osMD5Update(&tctx.md5, dat + ixs->iphlen + sizeof(struct ahhdr),
  26919. + ixs->skb->len - ixs->iphlen - sizeof(struct ahhdr));
  26920. + ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  26921. + osMD5Final(hash, &tctx.md5);
  26922. + ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
  26923. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  26924. + ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  26925. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  26926. + ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  26927. + osMD5Final(hash, &tctx.md5);
  26928. + ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
  26929. +
  26930. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  26931. +
  26932. + /* paranoid */
  26933. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  26934. + memset((caddr_t)hash, 0, sizeof(*hash));
  26935. + break;
  26936. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  26937. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  26938. + case AH_SHA:
  26939. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  26940. + SHA1Update(&tctx.sha1, (unsigned char *)&ipo, sizeof (struct iphdr));
  26941. + SHA1Update(&tctx.sha1, (unsigned char *)ahp, sizeof(struct ahhdr) - sizeof(ahp->ah_data));
  26942. + SHA1Update(&tctx.sha1, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  26943. + SHA1Update(&tctx.sha1, dat + ixs->iphlen + sizeof(struct ahhdr),
  26944. + ixs->skb->len - ixs->iphlen - sizeof(struct ahhdr));
  26945. + SHA1Final(hash, &tctx.sha1);
  26946. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  26947. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  26948. + SHA1Final(hash, &tctx.sha1);
  26949. +
  26950. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  26951. +
  26952. + /* paranoid */
  26953. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  26954. + memset((caddr_t)hash, 0, sizeof(*hash));
  26955. + break;
  26956. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  26957. + default:
  26958. + ixs->stats->tx_errors++;
  26959. + return IPSEC_XMIT_AH_BADALG;
  26960. + }
  26961. +#ifdef NET_21
  26962. + ixs->skb->h.raw = (unsigned char*)ahp;
  26963. +#endif /* NET_21 */
  26964. +
  26965. + return IPSEC_XMIT_OK;
  26966. +}
  26967. +
  26968. +struct xform_functions ah_xform_funcs[]={
  26969. + { rcv_checks: ipsec_rcv_ah_checks,
  26970. + rcv_setup_auth: ipsec_rcv_ah_setup_auth,
  26971. + rcv_calc_auth: ipsec_rcv_ah_authcalc,
  26972. + rcv_decrypt: ipsec_rcv_ah_decap,
  26973. +
  26974. + xmit_setup: ipsec_xmit_ah_setup,
  26975. + xmit_headroom: sizeof(struct ahhdr),
  26976. + xmit_needtailroom: 0,
  26977. + },
  26978. +};
  26979. +
  26980. +
  26981. +#ifdef NET_26
  26982. +struct inet_protocol ah_protocol = {
  26983. + .handler = ipsec_rcv,
  26984. + .no_policy = 1,
  26985. +};
  26986. +#else
  26987. +struct inet_protocol ah_protocol =
  26988. +{
  26989. + ipsec_rcv, /* AH handler */
  26990. + NULL, /* TUNNEL error control */
  26991. +#ifdef NETDEV_25
  26992. + 1, /* no policy */
  26993. +#else
  26994. + 0, /* next */
  26995. + IPPROTO_AH, /* protocol ID */
  26996. + 0, /* copy */
  26997. + NULL, /* data */
  26998. + "AH" /* name */
  26999. +#endif
  27000. +};
  27001. +#endif /* NET_26 */
  27002. +
  27003. +/*
  27004. + * $Log: ipsec_ah.c,v $
  27005. + * Revision 1.12.2.2 2006/10/06 21:39:26 paul
  27006. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  27007. + * set. This is defined through autoconf.h which is included through the
  27008. + * linux kernel build macros.
  27009. + *
  27010. + * Revision 1.12.2.1 2006/02/15 05:35:14 paul
  27011. + * Patch by David McCullough <davidm@snapgear.com>
  27012. + * If you setup a tunnel without ESP it doesn't work. It used to work in
  27013. + * an older openswan version but stopped when klips was modified to deal
  27014. + * with the pulled IP header on the received SKB's.
  27015. + *
  27016. + * The code in ipsec_ah.c still thinks the IP header is there and runs the
  27017. + * hash on the incorrect data.
  27018. + *
  27019. + * Revision 1.12 2005/04/29 05:10:22 mcr
  27020. + * removed from extraenous includes to make unit testing easier.
  27021. + *
  27022. + * Revision 1.11 2005/04/15 19:50:55 mcr
  27023. + * adjustments to use proper skb fields for data.
  27024. + *
  27025. + * Revision 1.10 2004/09/14 00:22:57 mcr
  27026. + * adjustment of MD5* functions.
  27027. + *
  27028. + * Revision 1.9 2004/09/13 02:22:47 mcr
  27029. + * #define inet_protocol if necessary.
  27030. + *
  27031. + * Revision 1.8 2004/09/06 18:35:48 mcr
  27032. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  27033. + * so adjust for that.
  27034. + *
  27035. + * Revision 1.7 2004/08/22 05:00:48 mcr
  27036. + * if we choose to compile the file, we want the contents,
  27037. + * so don't pull any punches.
  27038. + *
  27039. + * Revision 1.6 2004/08/17 03:27:23 mcr
  27040. + * klips 2.6 edits.
  27041. + *
  27042. + * Revision 1.5 2004/08/14 03:28:24 mcr
  27043. + * fixed log comment to remove warning about embedded comment.
  27044. + *
  27045. + * Revision 1.4 2004/08/04 15:57:07 mcr
  27046. + * moved des .h files to include/des/ *
  27047. + * included 2.6 protocol specific things
  27048. + * started at NAT-T support, but it will require a kernel patch.
  27049. + *
  27050. + * Revision 1.3 2004/07/10 19:11:18 mcr
  27051. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  27052. + *
  27053. + * Revision 1.2 2004/04/06 02:49:25 mcr
  27054. + * pullup of algo code from alg-branch.
  27055. + *
  27056. + *
  27057. + *
  27058. + */
  27059. --- /dev/null Tue Mar 11 13:02:56 2003
  27060. +++ linux/net/ipsec/ipsec_alg.c Mon Feb 9 13:51:03 2004
  27061. @@ -0,0 +1,1057 @@
  27062. +/*
  27063. + * Modular extensions service and registration functions
  27064. + *
  27065. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  27066. + *
  27067. + * Version: 0.8.1
  27068. + *
  27069. + * ipsec_alg.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  27070. + *
  27071. + * This program is free software; you can redistribute it and/or modify it
  27072. + * under the terms of the GNU General Public License as published by the
  27073. + * Free Software Foundation; either version 2 of the License, or (at your
  27074. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  27075. + *
  27076. + * This program is distributed in the hope that it will be useful, but
  27077. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  27078. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  27079. + * for more details.
  27080. + *
  27081. + */
  27082. +#define __NO_VERSION__
  27083. +
  27084. +#if defined (MODULE)
  27085. +#include <linux/module.h>
  27086. +#endif
  27087. +
  27088. +#include <linux/kernel.h> /* printk() */
  27089. +
  27090. +#include <linux/netdevice.h> /* struct device, and other headers */
  27091. +#include <linux/etherdevice.h> /* eth_type_trans */
  27092. +#include <linux/ip.h> /* struct iphdr */
  27093. +#include <linux/skbuff.h>
  27094. +#include <linux/socket.h>
  27095. +#include <linux/in.h>
  27096. +#include <linux/types.h>
  27097. +#include <linux/string.h> /* memcmp() */
  27098. +#include <linux/random.h> /* get_random_bytes() */
  27099. +#include <linux/errno.h> /* error codes */
  27100. +#ifdef SPINLOCK
  27101. +# ifdef SPINLOCK_23
  27102. +# include <linux/spinlock.h> /* *lock* */
  27103. +# else /* SPINLOCK_23 */
  27104. +# include <asm/spinlock.h> /* *lock* */
  27105. +# endif /* SPINLOCK_23 */
  27106. +#endif /* SPINLOCK */
  27107. +
  27108. +#include "openswan/ipsec_param.h"
  27109. +#include <openswan.h>
  27110. +#include "openswan/ipsec_sa.h"
  27111. +#include "openswan/radij.h"
  27112. +#include "openswan/ipsec_encap.h"
  27113. +#include "openswan/ipsec_radij.h"
  27114. +#include "openswan/ipsec_xform.h"
  27115. +#include "openswan/ipsec_tunnel.h"
  27116. +#include "openswan/ipsec_rcv.h"
  27117. +#if defined(CONFIG_KLIPS_ESP) || defined(CONFIG_KLIPS_AH)
  27118. +# include "openswan/ipsec_ah.h"
  27119. +#endif /* defined(CONFIG_KLIPS_ESP) || defined(CONFIG_KLIPS_AH) */
  27120. +#ifdef CONFIG_KLIPS_ESP
  27121. +# include "openswan/ipsec_esp.h"
  27122. +#endif /* !CONFIG_KLIPS_ESP */
  27123. +#ifdef CONFIG_KLIPS_IPCOMP
  27124. +# include "openswan/ipcomp.h"
  27125. +#endif /* CONFIG_KLIPS_COMP */
  27126. +
  27127. +#include <pfkeyv2.h>
  27128. +#include <pfkey.h>
  27129. +
  27130. +#include "openswan/ipsec_alg.h"
  27131. +#include "openswan/ipsec_proto.h"
  27132. +
  27133. +#if SADB_EALG_MAX < 255
  27134. +#warning Compiling with limited ESP support ( SADB_EALG_MAX < 256 )
  27135. +#endif
  27136. +
  27137. +static rwlock_t ipsec_alg_lock = RW_LOCK_UNLOCKED;
  27138. +#define IPSEC_ALG_HASHSZ 16 /* must be power of 2, even 2^0=1 */
  27139. +static struct list_head ipsec_alg_hash_table[IPSEC_ALG_HASHSZ];
  27140. +
  27141. +/* Old gcc's will fail here */
  27142. +#define barf_out(fmt, args...) do { struct ipsec_alg *ixtc = (struct ipsec_alg *)ixt; printk(KERN_ERR "%s: (%s) " fmt, __FUNCTION__, ixtc->ixt_name , ## args) \
  27143. + ; goto out; } while(0)
  27144. +
  27145. +#ifdef NET_26
  27146. +/*
  27147. + * Must be already protected by lock
  27148. + */
  27149. +static void __ipsec_alg_usage_inc(struct ipsec_alg *ixt)
  27150. +{
  27151. +#ifdef MODULE
  27152. + if (ixt->ixt_module)
  27153. + try_module_get(ixt->ixt_module);
  27154. +#endif
  27155. + atomic_inc(&ixt->ixt_refcnt);
  27156. +}
  27157. +static void __ipsec_alg_usage_dec(struct ipsec_alg *ixt) {
  27158. + atomic_dec(&ixt->ixt_refcnt);
  27159. +#ifdef MODULE
  27160. + if (ixt->ixt_module)
  27161. + module_put(ixt->ixt_module);
  27162. +#endif
  27163. +}
  27164. +
  27165. +#else
  27166. +
  27167. +/*
  27168. + * Must be already protected by lock
  27169. + */
  27170. +static void __ipsec_alg_usage_inc(struct ipsec_alg *ixt) {
  27171. +#ifdef MODULE
  27172. + if (ixt->ixt_module) {
  27173. + __MOD_INC_USE_COUNT(ixt->ixt_module);
  27174. + }
  27175. +#endif
  27176. + atomic_inc(&ixt->ixt_refcnt);
  27177. +}
  27178. +static void __ipsec_alg_usage_dec(struct ipsec_alg *ixt) {
  27179. + atomic_dec(&ixt->ixt_refcnt);
  27180. +#ifdef MODULE
  27181. + if (ixt->ixt_module)
  27182. + __MOD_DEC_USE_COUNT(ixt->ixt_module);
  27183. +#endif
  27184. +}
  27185. +#endif
  27186. +
  27187. +/*
  27188. + * simple hash function, optimized for 0-hash (1 list) special
  27189. + * case
  27190. + */
  27191. +#if IPSEC_ALG_HASHSZ > 1
  27192. +static inline unsigned ipsec_alg_hashfn(int alg_type, int alg_id) {
  27193. + return ((alg_type^alg_id)&(IPSEC_ALG_HASHSZ-1));
  27194. +}
  27195. +#else
  27196. +#define ipsec_alg_hashfn(x,y) (0)
  27197. +#endif
  27198. +
  27199. +/*****************************************************************
  27200. + *
  27201. + * INTERNAL table handling: insert, delete, find
  27202. + *
  27203. + *****************************************************************/
  27204. +
  27205. +/*
  27206. + * hash table initialization, called from ipsec_alg_init()
  27207. + */
  27208. +static void ipsec_alg_hash_init(void) {
  27209. + struct list_head *head = ipsec_alg_hash_table;
  27210. + int i = IPSEC_ALG_HASHSZ;
  27211. + do {
  27212. + INIT_LIST_HEAD(head);
  27213. + head++;
  27214. + i--;
  27215. + } while (i);
  27216. +}
  27217. +/*
  27218. + * hash list lookup by {alg_type, alg_id} and table head,
  27219. + * must be already protected by lock
  27220. + */
  27221. +static struct ipsec_alg *__ipsec_alg_find(unsigned alg_type, unsigned alg_id, struct list_head * head) {
  27222. + struct list_head *p;
  27223. + struct ipsec_alg *ixt=NULL;
  27224. + for (p=head->next; p!=head; p=p->next) {
  27225. + ixt = list_entry(p, struct ipsec_alg, ixt_list);
  27226. + if (ixt->ixt_alg_type == alg_type && ixt->ixt_alg_id==alg_id) {
  27227. + goto out;
  27228. + }
  27229. + }
  27230. + ixt=NULL;
  27231. +out:
  27232. + return ixt;
  27233. +}
  27234. +/*
  27235. + * inserts (in front) a new entry in hash table,
  27236. + * called from ipsec_alg_register() when new algorithm is registered.
  27237. + */
  27238. +static int ipsec_alg_insert(struct ipsec_alg *ixt) {
  27239. + int ret=-EINVAL;
  27240. + unsigned hashval=ipsec_alg_hashfn(ixt->ixt_alg_type, ixt->ixt_alg_id);
  27241. + struct list_head *head= ipsec_alg_hash_table + hashval;
  27242. + struct ipsec_alg *ixt_cur;
  27243. +
  27244. + /* new element must be virgin ... */
  27245. + if (ixt->ixt_list.next != &ixt->ixt_list ||
  27246. + ixt->ixt_list.prev != &ixt->ixt_list) {
  27247. + printk(KERN_ERR "ipsec_alg_insert: ixt object \"%s\" "
  27248. + "list head not initialized\n",
  27249. + ixt->ixt_name);
  27250. + return ret;
  27251. + }
  27252. + write_lock_bh(&ipsec_alg_lock);
  27253. +
  27254. + ixt_cur = __ipsec_alg_find(ixt->ixt_alg_type, ixt->ixt_alg_id, head);
  27255. +
  27256. + /* if previous (current) ipsec_alg found check excl flag of _anyone_ */
  27257. + if (ixt_cur
  27258. + && ((ixt->ixt_state|ixt_cur->ixt_state) & IPSEC_ALG_ST_EXCL)) {
  27259. + barf_out("ipsec_alg for alg_type=%d, alg_id=%d already exist. "
  27260. + "Not loaded (ret=%d).\n",
  27261. + ixt->ixt_alg_type,
  27262. + ixt->ixt_alg_id, ret=-EEXIST);
  27263. + }
  27264. + list_add(&ixt->ixt_list, head);
  27265. + ixt->ixt_state |= IPSEC_ALG_ST_REGISTERED;
  27266. + ret=0;
  27267. +out:
  27268. + write_unlock_bh(&ipsec_alg_lock);
  27269. + return ret;
  27270. +}
  27271. +
  27272. +/*
  27273. + * deletes an existing entry in hash table,
  27274. + * called from ipsec_alg_unregister() when algorithm is unregistered.
  27275. + */
  27276. +static int ipsec_alg_delete(struct ipsec_alg *ixt) {
  27277. + write_lock_bh(&ipsec_alg_lock);
  27278. + list_del(&ixt->ixt_list);
  27279. + write_unlock_bh(&ipsec_alg_lock);
  27280. + return 0;
  27281. +}
  27282. +
  27283. +/*
  27284. + * here @user context (read-only when @kernel bh context)
  27285. + * -> no bh disabling
  27286. + *
  27287. + * called from ipsec_sa_init() -> ipsec_alg_sa_init()
  27288. + */
  27289. +static struct ipsec_alg *ipsec_alg_get(int alg_type, int alg_id)
  27290. +{
  27291. + unsigned hashval=ipsec_alg_hashfn(alg_type, alg_id);
  27292. + struct list_head *head= ipsec_alg_hash_table + hashval;
  27293. + struct ipsec_alg *ixt;
  27294. +
  27295. + read_lock(&ipsec_alg_lock);
  27296. + ixt=__ipsec_alg_find(alg_type, alg_id, head);
  27297. + if (ixt) __ipsec_alg_usage_inc(ixt);
  27298. + read_unlock(&ipsec_alg_lock);
  27299. +
  27300. + return ixt;
  27301. +}
  27302. +
  27303. +static void ipsec_alg_put(struct ipsec_alg *ixt) {
  27304. + __ipsec_alg_usage_dec((struct ipsec_alg *)ixt);
  27305. +}
  27306. +
  27307. +/*****************************************************************
  27308. + *
  27309. + * INTERFACE for ENC services: key creation, encrypt function
  27310. + *
  27311. + *****************************************************************/
  27312. +
  27313. +/*
  27314. + * main encrypt service entry point
  27315. + * called from ipsec_rcv() with encrypt=IPSEC_ALG_DECRYPT and
  27316. + * ipsec_tunnel_start_xmit with encrypt=IPSEC_ALG_ENCRYPT
  27317. + */
  27318. +int ipsec_alg_esp_encrypt(struct ipsec_sa *sa_p, __u8 * idat,
  27319. + int ilen, const __u8 * iv, int encrypt)
  27320. +{
  27321. + int ret;
  27322. + struct ipsec_alg_enc *ixt_e=sa_p->ips_alg_enc;
  27323. +#ifdef CONFIG_KLIPS_DEBUG
  27324. + int debug_flag = (encrypt==IPSEC_ALG_ENCRYPT ?
  27325. + debug_tunnel : debug_rcv);
  27326. +#endif
  27327. +
  27328. + KLIPS_PRINT(debug_flag,
  27329. + "klips_debug:ipsec_alg_esp_encrypt: "
  27330. + "entering with encalg=%d, ixt_e=%p\n",
  27331. + sa_p->ips_encalg, ixt_e);
  27332. + if (ixt_e == NULL) {
  27333. +#ifdef CONFIG_KLIPS_DEBUG
  27334. + KLIPS_ERROR(debug_flag,
  27335. + "klips_debug:ipsec_alg_esp_encrypt: "
  27336. + "NULL ipsec_alg_enc object\n");
  27337. +#endif
  27338. + return -1;
  27339. + }
  27340. + KLIPS_PRINT(debug_flag,
  27341. + "klips_debug:ipsec_alg_esp_encrypt: "
  27342. + "calling cbc_encrypt encalg=%d "
  27343. + "ips_key_e=%p idat=%p ilen=%d iv=%p, encrypt=%d\n",
  27344. + sa_p->ips_encalg,
  27345. + sa_p->ips_key_e, idat, ilen, iv, encrypt);
  27346. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, sa_p->ips_key_e, idat,
  27347. + ilen, iv, encrypt);
  27348. + KLIPS_PRINT(debug_flag,
  27349. + "klips_debug:ipsec_alg_esp_encrypt: "
  27350. + "returned ret=%d\n",
  27351. + ret);
  27352. + return ret;
  27353. +}
  27354. +
  27355. +/*
  27356. + * encryption key context creation function
  27357. + * called from pfkey_v2_parser.c:pfkey_ips_init()
  27358. + */
  27359. +int ipsec_alg_enc_key_create(struct ipsec_sa *sa_p) {
  27360. + int ret=-EINVAL;
  27361. + int keyminbits, keymaxbits;
  27362. + caddr_t ekp;
  27363. + struct ipsec_alg_enc *ixt_e=sa_p->ips_alg_enc;
  27364. +
  27365. + KLIPS_PRINT(debug_pfkey,
  27366. + "klips_debug:ipsec_alg_enc_key_create: "
  27367. + "entering with encalg=%d ixt_e=%p\n",
  27368. + sa_p->ips_encalg, ixt_e);
  27369. + if (!ixt_e) {
  27370. + KLIPS_PRINT(debug_pfkey,
  27371. + "klips_debug:ipsec_alg_enc_key_create: "
  27372. + "NULL ipsec_alg_enc object\n");
  27373. + return -EPROTO;
  27374. + }
  27375. +
  27376. + /*
  27377. + * grRRR... DES 7bits jurassic stuff ... f*ckk --jjo
  27378. + */
  27379. + switch(ixt_e->ixt_common.ixt_support.ias_id) {
  27380. + case ESP_3DES:
  27381. + keyminbits=keymaxbits=192;break;
  27382. + case ESP_DES:
  27383. + keyminbits=keymaxbits=64;break;
  27384. + default:
  27385. + keyminbits=ixt_e->ixt_common.ixt_support.ias_keyminbits;
  27386. + keymaxbits=ixt_e->ixt_common.ixt_support.ias_keymaxbits;
  27387. + }
  27388. + if(sa_p->ips_key_bits_e<keyminbits ||
  27389. + sa_p->ips_key_bits_e>keymaxbits) {
  27390. + KLIPS_PRINT(debug_pfkey,
  27391. + "klips_debug:ipsec_alg_enc_key_create: "
  27392. + "incorrect encryption key size for id=%d: %d bits -- "
  27393. + "must be between %d,%d bits\n" /*octets (bytes)\n"*/,
  27394. + ixt_e->ixt_common.ixt_support.ias_id,
  27395. + sa_p->ips_key_bits_e, keyminbits, keymaxbits);
  27396. + ret=-EINVAL;
  27397. + goto ixt_out;
  27398. + }
  27399. + /* save encryption key pointer */
  27400. + ekp = sa_p->ips_key_e;
  27401. +
  27402. +
  27403. + if (ixt_e->ixt_e_new_key) {
  27404. + sa_p->ips_key_e = ixt_e->ixt_e_new_key(ixt_e,
  27405. + ekp, sa_p->ips_key_bits_e/8);
  27406. + ret = (sa_p->ips_key_e)? 0 : -EINVAL;
  27407. + } else {
  27408. + if((sa_p->ips_key_e = (caddr_t)
  27409. + kmalloc((sa_p->ips_key_e_size = ixt_e->ixt_e_ctx_size),
  27410. + GFP_ATOMIC)) == NULL) {
  27411. + ret=-ENOMEM;
  27412. + goto ixt_out;
  27413. + }
  27414. + /* zero-out key_e */
  27415. + memset(sa_p->ips_key_e, 0, sa_p->ips_key_e_size);
  27416. +
  27417. + /* I cast here to allow more decoupling in alg module */
  27418. + KLIPS_PRINT(debug_pfkey,
  27419. + "klips_debug:ipsec_alg_enc_key_create: about to call:"
  27420. + "set_key(key_e=%p, ekp=%p, key_size=%d)\n",
  27421. + (caddr_t)sa_p->ips_key_e, ekp, sa_p->ips_key_bits_e/8);
  27422. + ret = ixt_e->ixt_e_set_key(ixt_e, (caddr_t)sa_p->ips_key_e, ekp, sa_p->ips_key_bits_e/8);
  27423. + }
  27424. + /* paranoid */
  27425. + memset(ekp, 0, sa_p->ips_key_bits_e/8);
  27426. + kfree(ekp);
  27427. +ixt_out:
  27428. + return ret;
  27429. +}
  27430. +
  27431. +/***************************************************************
  27432. + *
  27433. + * INTERFACE for AUTH services: key creation, hash functions
  27434. + *
  27435. + ***************************************************************/
  27436. +
  27437. +/*
  27438. + * auth key context creation function
  27439. + * called from pfkey_v2_parser.c:pfkey_ips_init()
  27440. + */
  27441. +int ipsec_alg_auth_key_create(struct ipsec_sa *sa_p) {
  27442. + int ret=-EINVAL;
  27443. + struct ipsec_alg_auth *ixt_a=sa_p->ips_alg_auth;
  27444. + int keyminbits, keymaxbits;
  27445. + unsigned char *akp;
  27446. + unsigned int aks;
  27447. + KLIPS_PRINT(debug_pfkey,
  27448. + "klips_debug:ipsec_alg_auth_key_create: "
  27449. + "entering with authalg=%d ixt_a=%p\n",
  27450. + sa_p->ips_authalg, ixt_a);
  27451. + if (!ixt_a) {
  27452. + KLIPS_PRINT(debug_pfkey,
  27453. + "klips_debug:ipsec_alg_auth_key_create: "
  27454. + "NULL ipsec_alg_auth object\n");
  27455. + return -EPROTO;
  27456. + }
  27457. + keyminbits=ixt_a->ixt_common.ixt_support.ias_keyminbits;
  27458. + keymaxbits=ixt_a->ixt_common.ixt_support.ias_keymaxbits;
  27459. + if(sa_p->ips_key_bits_a<keyminbits || sa_p->ips_key_bits_a>keymaxbits) {
  27460. + KLIPS_PRINT(debug_pfkey,
  27461. + "klips_debug:ipsec_alg_auth_key_create: incorrect auth"
  27462. + "key size: %d bits -- must be between %d,%d bits\n"/*octets (bytes)\n"*/,
  27463. + sa_p->ips_key_bits_a, keyminbits, keymaxbits);
  27464. + ret=-EINVAL;
  27465. + goto ixt_out;
  27466. + }
  27467. + /* save auth key pointer */
  27468. + sa_p->ips_auth_bits = ixt_a->ixt_a_keylen * 8; /* XXX XXX */
  27469. + akp = sa_p->ips_key_a;
  27470. + aks = sa_p->ips_key_a_size;
  27471. +
  27472. + /* will hold: 2 ctx and a blocksize buffer: kb */
  27473. + sa_p->ips_key_a_size = ixt_a->ixt_a_ctx_size;
  27474. + if((sa_p->ips_key_a =
  27475. + (caddr_t) kmalloc(sa_p->ips_key_a_size, GFP_ATOMIC)) == NULL) {
  27476. + ret=-ENOMEM;
  27477. + goto ixt_out;
  27478. + }
  27479. + ixt_a->ixt_a_hmac_set_key(ixt_a, sa_p->ips_key_a, akp, sa_p->ips_key_bits_a/8); /* XXX XXX */
  27480. + ret=0;
  27481. + memset(akp, 0, aks);
  27482. + kfree(akp);
  27483. +
  27484. +ixt_out:
  27485. + return ret;
  27486. +}
  27487. +
  27488. +
  27489. +int ipsec_alg_sa_esp_hash(const struct ipsec_sa *sa_p, const __u8 *espp,
  27490. + int len, __u8 *hash, int hashlen)
  27491. +{
  27492. + struct ipsec_alg_auth *ixt_a=sa_p->ips_alg_auth;
  27493. + if (!ixt_a) {
  27494. + KLIPS_PRINT(debug_pfkey,
  27495. + "klips_debug:ipsec_sa_esp_hash: "
  27496. + "NULL ipsec_alg_auth object\n");
  27497. + return -EPROTO;
  27498. + }
  27499. + KLIPS_PRINT(debug_tunnel|debug_rcv,
  27500. + "klips_debug:ipsec_sa_esp_hash: "
  27501. + "hashing %p (%d bytes) to %p (%d bytes)\n",
  27502. + espp, len,
  27503. + hash, hashlen);
  27504. + ixt_a->ixt_a_hmac_hash(ixt_a,
  27505. + sa_p->ips_key_a,
  27506. + espp, len,
  27507. + hash, hashlen);
  27508. + return 0;
  27509. +}
  27510. +
  27511. +/***************************************************************
  27512. + *
  27513. + * INTERFACE for module loading,testing, and unloading
  27514. + *
  27515. + ***************************************************************/
  27516. +
  27517. +/* validation for registering (enc) module */
  27518. +static int check_enc(struct ipsec_alg_enc *ixt)
  27519. +{
  27520. + int ret=-EINVAL;
  27521. + if (ixt->ixt_common.ixt_blocksize==0) /* || ixt->ixt_common.ixt_blocksize%2) need for ESP_NULL */
  27522. + barf_out(KERN_ERR "invalid blocksize=%d\n", ixt->ixt_common.ixt_blocksize);
  27523. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0
  27524. + && ixt->ixt_common.ixt_support.ias_keymaxbits==0
  27525. + && ixt->ixt_e_keylen==0)
  27526. + goto zero_key_ok;
  27527. +
  27528. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0)
  27529. + barf_out(KERN_ERR "invalid keyminbits=%d\n", ixt->ixt_common.ixt_support.ias_keyminbits);
  27530. +
  27531. + if (ixt->ixt_common.ixt_support.ias_keymaxbits==0)
  27532. + barf_out(KERN_ERR "invalid keymaxbits=%d\n", ixt->ixt_common.ixt_support.ias_keymaxbits);
  27533. +
  27534. + if (ixt->ixt_e_keylen==0)
  27535. + barf_out(KERN_ERR "invalid keysize=%d\n", ixt->ixt_e_keylen);
  27536. +
  27537. +zero_key_ok:
  27538. + if (ixt->ixt_e_ctx_size==0 && ixt->ixt_e_new_key == NULL)
  27539. + barf_out(KERN_ERR "invalid key_e_size=%d and ixt_e_new_key=NULL\n", ixt->ixt_e_ctx_size);
  27540. + if (ixt->ixt_e_cbc_encrypt==NULL)
  27541. + barf_out(KERN_ERR "e_cbc_encrypt() must be not NULL\n");
  27542. + ret=0;
  27543. +out:
  27544. + return ret;
  27545. +}
  27546. +
  27547. +/* validation for registering (auth) module */
  27548. +static int check_auth(struct ipsec_alg_auth *ixt)
  27549. +{
  27550. + int ret=-EINVAL;
  27551. + if (ixt->ixt_common.ixt_support.ias_id==0 || ixt->ixt_common.ixt_support.ias_id > SADB_AALG_MAX)
  27552. + barf_out("invalid alg_id=%d > %d (SADB_AALG_MAX)\n",
  27553. + ixt->ixt_common.ixt_support.ias_id, SADB_AALG_MAX);
  27554. +
  27555. + if (ixt->ixt_common.ixt_blocksize==0
  27556. + || ixt->ixt_common.ixt_blocksize%2)
  27557. + barf_out(KERN_ERR "invalid blocksize=%d\n",
  27558. + ixt->ixt_common.ixt_blocksize);
  27559. +
  27560. + if (ixt->ixt_common.ixt_blocksize>AH_BLKLEN_MAX)
  27561. + barf_out(KERN_ERR "sorry blocksize=%d > %d. "
  27562. + "Please increase AH_BLKLEN_MAX and recompile\n",
  27563. + ixt->ixt_common.ixt_blocksize,
  27564. + AH_BLKLEN_MAX);
  27565. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0 && ixt->ixt_common.ixt_support.ias_keymaxbits==0 && ixt->ixt_a_keylen==0)
  27566. + goto zero_key_ok;
  27567. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0)
  27568. + barf_out(KERN_ERR "invalid keyminbits=%d\n", ixt->ixt_common.ixt_support.ias_keyminbits);
  27569. + if (ixt->ixt_common.ixt_support.ias_keymaxbits==0)
  27570. + barf_out(KERN_ERR "invalid keymaxbits=%d\n", ixt->ixt_common.ixt_support.ias_keymaxbits);
  27571. + if (ixt->ixt_common.ixt_support.ias_keymaxbits!=ixt->ixt_common.ixt_support.ias_keyminbits)
  27572. + barf_out(KERN_ERR "keymaxbits must equal keyminbits (not sure).\n");
  27573. + if (ixt->ixt_a_keylen==0)
  27574. + barf_out(KERN_ERR "invalid keysize=%d\n", ixt->ixt_a_keylen);
  27575. +zero_key_ok:
  27576. + if (ixt->ixt_a_ctx_size==0)
  27577. + barf_out(KERN_ERR "invalid a_ctx_size=%d\n", ixt->ixt_a_ctx_size);
  27578. + if (ixt->ixt_a_hmac_set_key==NULL)
  27579. + barf_out(KERN_ERR "a_hmac_set_key() must be not NULL\n");
  27580. + if (ixt->ixt_a_hmac_hash==NULL)
  27581. + barf_out(KERN_ERR "a_hmac_hash() must be not NULL\n");
  27582. + ret=0;
  27583. +out:
  27584. + return ret;
  27585. +}
  27586. +
  27587. +/*
  27588. + * Generic (enc, auth) registration entry point
  27589. + */
  27590. +int register_ipsec_alg(struct ipsec_alg *ixt)
  27591. +{
  27592. + int ret=-EINVAL;
  27593. + /* Validation */
  27594. + if (ixt==NULL)
  27595. + barf_out("NULL ipsec_alg object passed\n");
  27596. + if ((ixt->ixt_version&0xffffff00) != (IPSEC_ALG_VERSION&0xffffff00))
  27597. + barf_out("incorrect version: %d.%d.%d-%d, "
  27598. + "must be %d.%d.%d[-%d]\n",
  27599. + IPSEC_ALG_VERSION_QUAD(ixt->ixt_version),
  27600. + IPSEC_ALG_VERSION_QUAD(IPSEC_ALG_VERSION));
  27601. +
  27602. + switch(ixt->ixt_alg_type) {
  27603. + case IPSEC_ALG_TYPE_AUTH:
  27604. + if ((ret=check_auth((struct ipsec_alg_auth *)ixt)<0))
  27605. + goto out;
  27606. + break;
  27607. + case IPSEC_ALG_TYPE_ENCRYPT:
  27608. + if ((ret=check_enc((struct ipsec_alg_enc *)ixt)<0))
  27609. + goto out;
  27610. + /*
  27611. + * Adapted two lines below:
  27612. + * ivlen == 0 is possible (NULL enc has blocksize==1)
  27613. + *
  27614. + * fixed NULL support by David De Reu <DeReu@tComLabs.com>
  27615. + */
  27616. + if (ixt->ixt_support.ias_ivlen == 0
  27617. + && ixt->ixt_blocksize > 1) {
  27618. + ixt->ixt_support.ias_ivlen = ixt->ixt_blocksize*8;
  27619. + }
  27620. + break;
  27621. + default:
  27622. + barf_out("alg_type=%d not supported\n", ixt->ixt_alg_type);
  27623. + }
  27624. + INIT_LIST_HEAD(&ixt->ixt_list);
  27625. + ret = ipsec_alg_insert(ixt);
  27626. + if (ret<0)
  27627. + barf_out(KERN_WARNING "ipsec_alg for alg_id=%d failed."
  27628. + "Not loaded (ret=%d).\n",
  27629. + ixt->ixt_support.ias_id, ret);
  27630. +
  27631. +
  27632. + ret = pfkey_list_insert_supported((struct ipsec_alg_supported *)&ixt->ixt_support
  27633. + , &(pfkey_supported_list[SADB_SATYPE_ESP]));
  27634. +
  27635. + if (ret==0) {
  27636. + ixt->ixt_state |= IPSEC_ALG_ST_SUPP;
  27637. + /* send register event to userspace */
  27638. + pfkey_register_reply(SADB_SATYPE_ESP, NULL);
  27639. + } else
  27640. + printk(KERN_ERR "pfkey_list_insert_supported returned %d. "
  27641. + "Loading anyway.\n", ret);
  27642. + ret=0;
  27643. +out:
  27644. + return ret;
  27645. +}
  27646. +
  27647. +/*
  27648. + * unregister ipsec_alg object from own tables, if
  27649. + * success => calls pfkey_list_remove_supported()
  27650. + */
  27651. +int unregister_ipsec_alg(struct ipsec_alg *ixt) {
  27652. + int ret= -EINVAL;
  27653. + switch(ixt->ixt_alg_type) {
  27654. + case IPSEC_ALG_TYPE_AUTH:
  27655. + case IPSEC_ALG_TYPE_ENCRYPT:
  27656. + break;
  27657. + default:
  27658. + /* this is not a typo :) */
  27659. + barf_out("frog found in list (\"%s\"): ixt_p=NULL\n",
  27660. + ixt->ixt_name);
  27661. + }
  27662. +
  27663. + ret=ipsec_alg_delete(ixt);
  27664. + if (ixt->ixt_state&IPSEC_ALG_ST_SUPP) {
  27665. + ixt->ixt_state &= ~IPSEC_ALG_ST_SUPP;
  27666. + pfkey_list_remove_supported((struct ipsec_alg_supported *)&ixt->ixt_support
  27667. + , &(pfkey_supported_list[SADB_SATYPE_ESP]));
  27668. +
  27669. + /* send register event to userspace */
  27670. + pfkey_register_reply(SADB_SATYPE_ESP, NULL);
  27671. + }
  27672. +
  27673. +out:
  27674. + return ret;
  27675. +}
  27676. +
  27677. +/*
  27678. + * Must be called from user context
  27679. + * used at module load type for testing algo implementation
  27680. + */
  27681. +static int ipsec_alg_test_encrypt(int enc_alg, int test) {
  27682. + int ret;
  27683. + caddr_t buf = NULL;
  27684. + int iv_size, keysize, key_e_size;
  27685. + struct ipsec_alg_enc *ixt_e;
  27686. + void *tmp_key_e = NULL;
  27687. + #define BUFSZ 1024
  27688. + #define MARGIN 0
  27689. + #define test_enc (buf+MARGIN)
  27690. + #define test_dec (test_enc+BUFSZ+MARGIN)
  27691. + #define test_tmp (test_dec+BUFSZ+MARGIN)
  27692. + #define test_key_e (test_tmp+BUFSZ+MARGIN)
  27693. + #define test_iv (test_key_e+key_e_size+MARGIN)
  27694. + #define test_key (test_iv+iv_size+MARGIN)
  27695. + #define test_size (BUFSZ*3+key_e_size+iv_size+keysize+MARGIN*7)
  27696. + ixt_e=(struct ipsec_alg_enc *)ipsec_alg_get(IPSEC_ALG_TYPE_ENCRYPT, enc_alg);
  27697. + if (ixt_e==NULL) {
  27698. + KLIPS_PRINT(1,
  27699. + "klips_debug: ipsec_alg_test_encrypt: "
  27700. + "encalg=%d object not found\n",
  27701. + enc_alg);
  27702. + ret=-EINVAL;
  27703. + goto out;
  27704. + }
  27705. + iv_size=ixt_e->ixt_common.ixt_support.ias_ivlen / 8;
  27706. + key_e_size=ixt_e->ixt_e_ctx_size;
  27707. + keysize=ixt_e->ixt_e_keylen;
  27708. + KLIPS_PRINT(1,
  27709. + "klips_debug: ipsec_alg_test_encrypt: "
  27710. + "enc_alg=%d blocksize=%d key_e_size=%d keysize=%d\n",
  27711. + enc_alg, iv_size, key_e_size, keysize);
  27712. + if ((buf=kmalloc (test_size, GFP_KERNEL)) == NULL) {
  27713. + ret= -ENOMEM;
  27714. + goto out;
  27715. + }
  27716. + get_random_bytes(test_key, keysize);
  27717. + get_random_bytes(test_iv, iv_size);
  27718. + if (ixt_e->ixt_e_new_key) {
  27719. + tmp_key_e = ixt_e->ixt_e_new_key(ixt_e, test_key, keysize);
  27720. + ret = tmp_key_e ? 0 : -EINVAL;
  27721. + } else {
  27722. + tmp_key_e = test_key_e;
  27723. + ret = ixt_e->ixt_e_set_key(ixt_e, test_key_e, test_key, keysize);
  27724. + }
  27725. + if (ret < 0)
  27726. + goto out;
  27727. + get_random_bytes(test_enc, BUFSZ);
  27728. + memcpy(test_tmp, test_enc, BUFSZ);
  27729. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, tmp_key_e, test_enc, BUFSZ, test_iv, 1);
  27730. + printk(KERN_INFO
  27731. + "klips_info: ipsec_alg_test_encrypt: "
  27732. + "cbc_encrypt=1 ret=%d\n",
  27733. + ret);
  27734. + ret=memcmp(test_enc, test_tmp, BUFSZ);
  27735. + printk(KERN_INFO
  27736. + "klips_info: ipsec_alg_test_encrypt: "
  27737. + "memcmp(enc, tmp) ret=%d: %s\n", ret,
  27738. + ret!=0? "OK. (encr->DIFFers)" : "FAIL! (encr->SAME)" );
  27739. + memcpy(test_dec, test_enc, BUFSZ);
  27740. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, tmp_key_e, test_dec, BUFSZ, test_iv, 0);
  27741. + printk(KERN_INFO
  27742. + "klips_info: ipsec_alg_test_encrypt: "
  27743. + "cbc_encrypt=0 ret=%d\n", ret);
  27744. + ret=memcmp(test_dec, test_tmp, BUFSZ);
  27745. + printk(KERN_INFO
  27746. + "klips_info: ipsec_alg_test_encrypt: "
  27747. + "memcmp(dec,tmp) ret=%d: %s\n", ret,
  27748. + ret==0? "OK. (encr->decr->SAME)" : "FAIL! (encr->decr->DIFFers)" );
  27749. + {
  27750. + /* Shamelessly taken from drivers/md sources O:) */
  27751. + unsigned long now;
  27752. + int i, count, max=0;
  27753. + int encrypt, speed;
  27754. + for (encrypt=0; encrypt <2;encrypt ++) {
  27755. + for (i = 0; i < 5; i++) {
  27756. + now = jiffies;
  27757. + count = 0;
  27758. + while (jiffies == now) {
  27759. + mb();
  27760. + ixt_e->ixt_e_cbc_encrypt(ixt_e,
  27761. + tmp_key_e, test_tmp,
  27762. + BUFSZ, test_iv, encrypt);
  27763. + mb();
  27764. + count++;
  27765. + mb();
  27766. + }
  27767. + if (count > max)
  27768. + max = count;
  27769. + }
  27770. + speed = max * (HZ * BUFSZ / 1024);
  27771. + printk(KERN_INFO
  27772. + "klips_info: ipsec_alg_test_encrypt: "
  27773. + "%s %s speed=%d KB/s\n",
  27774. + ixt_e->ixt_common.ixt_name,
  27775. + encrypt? "encrypt": "decrypt", speed);
  27776. + }
  27777. + }
  27778. +out:
  27779. + if (tmp_key_e && ixt_e->ixt_e_destroy_key) ixt_e->ixt_e_destroy_key(ixt_e, tmp_key_e);
  27780. + if (buf) kfree(buf);
  27781. + if (ixt_e) ipsec_alg_put((struct ipsec_alg *)ixt_e);
  27782. + return ret;
  27783. + #undef test_enc
  27784. + #undef test_dec
  27785. + #undef test_tmp
  27786. + #undef test_key_e
  27787. + #undef test_iv
  27788. + #undef test_key
  27789. + #undef test_size
  27790. +}
  27791. +
  27792. +/*
  27793. + * Must be called from user context
  27794. + * used at module load type for testing algo implementation
  27795. + */
  27796. +static int ipsec_alg_test_auth(int auth_alg, int test) {
  27797. + int ret;
  27798. + caddr_t buf = NULL;
  27799. + int blocksize, keysize, key_a_size;
  27800. + struct ipsec_alg_auth *ixt_a;
  27801. + #define BUFSZ 1024
  27802. + #define MARGIN 0
  27803. + #define test_auth (buf+MARGIN)
  27804. + #define test_key_a (test_auth+BUFSZ+MARGIN)
  27805. + #define test_key (test_key_a+key_a_size+MARGIN)
  27806. + #define test_hash (test_key+keysize+MARGIN)
  27807. + #define test_size (BUFSZ+key_a_size+keysize+AHHMAC_HASHLEN+MARGIN*4)
  27808. + ixt_a=(struct ipsec_alg_auth *)ipsec_alg_get(IPSEC_ALG_TYPE_AUTH, auth_alg);
  27809. + if (ixt_a==NULL) {
  27810. + KLIPS_PRINT(1,
  27811. + "klips_debug: ipsec_alg_test_auth: "
  27812. + "encalg=%d object not found\n",
  27813. + auth_alg);
  27814. + ret=-EINVAL;
  27815. + goto out;
  27816. + }
  27817. + blocksize=ixt_a->ixt_common.ixt_blocksize;
  27818. + key_a_size=ixt_a->ixt_a_ctx_size;
  27819. + keysize=ixt_a->ixt_a_keylen;
  27820. + KLIPS_PRINT(1,
  27821. + "klips_debug: ipsec_alg_test_auth: "
  27822. + "auth_alg=%d blocksize=%d key_a_size=%d keysize=%d\n",
  27823. + auth_alg, blocksize, key_a_size, keysize);
  27824. + if ((buf=kmalloc (test_size, GFP_KERNEL)) == NULL) {
  27825. + ret= -ENOMEM;
  27826. + goto out;
  27827. + }
  27828. + get_random_bytes(test_key, keysize);
  27829. + ret = ixt_a->ixt_a_hmac_set_key(ixt_a, test_key_a, test_key, keysize);
  27830. + if (ret < 0 )
  27831. + goto out;
  27832. + get_random_bytes(test_auth, BUFSZ);
  27833. + ret=ixt_a->ixt_a_hmac_hash(ixt_a, test_key_a, test_auth, BUFSZ, test_hash, AHHMAC_HASHLEN);
  27834. + printk(KERN_INFO
  27835. + "klips_info: ipsec_alg_test_auth: "
  27836. + "ret=%d\n", ret);
  27837. + {
  27838. + /* Shamelessly taken from drivers/md sources O:) */
  27839. + unsigned long now;
  27840. + int i, count, max=0;
  27841. + int speed;
  27842. + for (i = 0; i < 5; i++) {
  27843. + now = jiffies;
  27844. + count = 0;
  27845. + while (jiffies == now) {
  27846. + mb();
  27847. + ixt_a->ixt_a_hmac_hash(ixt_a, test_key_a, test_auth, BUFSZ, test_hash, AHHMAC_HASHLEN);
  27848. + mb();
  27849. + count++;
  27850. + mb();
  27851. + }
  27852. + if (count > max)
  27853. + max = count;
  27854. + }
  27855. + speed = max * (HZ * BUFSZ / 1024);
  27856. + printk(KERN_INFO
  27857. + "klips_info: ipsec_alg_test_auth: "
  27858. + "%s hash speed=%d KB/s\n",
  27859. + ixt_a->ixt_common.ixt_name,
  27860. + speed);
  27861. + }
  27862. +out:
  27863. + if (buf) kfree(buf);
  27864. + if (ixt_a) ipsec_alg_put((struct ipsec_alg *)ixt_a);
  27865. + return ret;
  27866. + #undef test_auth
  27867. + #undef test_key_a
  27868. + #undef test_key
  27869. + #undef test_hash
  27870. + #undef test_size
  27871. +}
  27872. +
  27873. +int ipsec_alg_test(unsigned alg_type, unsigned alg_id, int test) {
  27874. + switch(alg_type) {
  27875. + case IPSEC_ALG_TYPE_ENCRYPT:
  27876. + return ipsec_alg_test_encrypt(alg_id, test);
  27877. + break;
  27878. + case IPSEC_ALG_TYPE_AUTH:
  27879. + return ipsec_alg_test_auth(alg_id, test);
  27880. + break;
  27881. + }
  27882. + printk(KERN_ERR "klips_info: ipsec_alg_test() called incorrectly: "
  27883. + "alg_type=%d alg_id=%d\n",
  27884. + alg_type, alg_id);
  27885. + return -EINVAL;
  27886. +}
  27887. +
  27888. +int ipsec_alg_init(void) {
  27889. + KLIPS_PRINT(1, "klips_info:ipsec_alg_init: "
  27890. + "KLIPS alg v=%d.%d.%d-%d (EALG_MAX=%d, AALG_MAX=%d)\n",
  27891. + IPSEC_ALG_VERSION_QUAD(IPSEC_ALG_VERSION),
  27892. + SADB_EALG_MAX, SADB_AALG_MAX);
  27893. + /* Initialize tables */
  27894. + write_lock_bh(&ipsec_alg_lock);
  27895. + ipsec_alg_hash_init();
  27896. + write_unlock_bh(&ipsec_alg_lock);
  27897. +
  27898. + /* Initialize static algos */
  27899. + KLIPS_PRINT(1, "klips_info:ipsec_alg_init: "
  27900. + "calling ipsec_alg_static_init()\n");
  27901. +
  27902. + /* If we are suppose to use our AES, and don't have
  27903. + * CryptoAPI enabled...
  27904. + */
  27905. +#if defined(CONFIG_KLIPS_ENC_AES) && CONFIG_KLIPS_ENC_AES && !defined(CONFIG_KLIPS_ENC_AES_MODULE)
  27906. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27907. +#warning "Using built-in AES rather than CryptoAPI AES"
  27908. +#endif
  27909. + {
  27910. + extern int ipsec_aes_init(void);
  27911. + ipsec_aes_init();
  27912. + }
  27913. +#endif
  27914. +
  27915. +#if defined(CONFIG_KLIPS_ENC_3DES) && CONFIG_KLIPS_ENC_3DES && !defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  27916. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27917. +#warning "Using built-in 3des rather than CryptoAPI 3des"
  27918. +#endif
  27919. + {
  27920. + extern int ipsec_3des_init(void);
  27921. + ipsec_3des_init();
  27922. + }
  27923. +#endif
  27924. +#if defined(CONFIG_KLIPS_ENC_NULL) && CONFIG_KLIPS_ENC_NULL && !defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  27925. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27926. +#warning "Using built-in null cipher rather than CryptoAPI null cipher"
  27927. +#endif
  27928. +#warning "Building with null cipher (ESP_NULL), blame on you :-)"
  27929. + {
  27930. + extern int ipsec_null_init(void);
  27931. + ipsec_null_init();
  27932. + }
  27933. +#endif
  27934. +
  27935. +
  27936. + /* If we are doing CryptoAPI, then init */
  27937. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI && !defined(CONFIG_KLIPS_ENC_CRYPTOAPI_MODULE)
  27938. + {
  27939. + extern int ipsec_cryptoapi_init(void);
  27940. + ipsec_cryptoapi_init();
  27941. + }
  27942. +#endif
  27943. +
  27944. +
  27945. + return 0;
  27946. +}
  27947. +
  27948. +/**********************************************
  27949. + *
  27950. + * INTERFACE for ipsec_sa init and wipe
  27951. + *
  27952. + **********************************************/
  27953. +
  27954. +/*
  27955. + * Called from pluto -> pfkey_v2_parser.c:pfkey_ipsec_sa_init()
  27956. + */
  27957. +int ipsec_alg_sa_init(struct ipsec_sa *sa_p) {
  27958. + struct ipsec_alg_enc *ixt_e;
  27959. + struct ipsec_alg_auth *ixt_a;
  27960. +
  27961. + /* Only ESP for now ... */
  27962. + if (sa_p->ips_said.proto != IPPROTO_ESP)
  27963. + return -EPROTONOSUPPORT;
  27964. +
  27965. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_init() :"
  27966. + "entering for encalg=%d, authalg=%d\n",
  27967. + sa_p->ips_encalg, sa_p->ips_authalg);
  27968. +
  27969. + if ((ixt_e=(struct ipsec_alg_enc *)
  27970. + ipsec_alg_get(IPSEC_ALG_TYPE_ENCRYPT, sa_p->ips_encalg))) {
  27971. + KLIPS_PRINT(debug_pfkey,
  27972. + "klips_debug: ipsec_alg_sa_init() :"
  27973. + "found ipsec_alg (ixt_e=%p) for encalg=%d\n",
  27974. + ixt_e, sa_p->ips_encalg);
  27975. + sa_p->ips_alg_enc=ixt_e;
  27976. + }
  27977. +
  27978. + if ((ixt_a=(struct ipsec_alg_auth *)
  27979. + ipsec_alg_get(IPSEC_ALG_TYPE_AUTH, sa_p->ips_authalg))) {
  27980. + KLIPS_PRINT(debug_pfkey,
  27981. + "klips_debug: ipsec_alg_sa_init() :"
  27982. + "found ipsec_alg (ixt_a=%p) for auth=%d\n",
  27983. + ixt_a, sa_p->ips_authalg);
  27984. + sa_p->ips_alg_auth=ixt_a;
  27985. + }
  27986. + return 0;
  27987. +}
  27988. +
  27989. +/*
  27990. + * Called from pluto -> ipsec_sa.c:ipsec_sa_delchain()
  27991. + */
  27992. +int ipsec_alg_sa_wipe(struct ipsec_sa *sa_p) {
  27993. + struct ipsec_alg *ixt;
  27994. + if ((ixt=(struct ipsec_alg *)sa_p->ips_alg_enc)) {
  27995. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_wipe() :"
  27996. + "unlinking for encalg=%d\n",
  27997. + ixt->ixt_support.ias_id);
  27998. + ipsec_alg_put(ixt);
  27999. + }
  28000. + if ((ixt=(struct ipsec_alg *)sa_p->ips_alg_auth)) {
  28001. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_wipe() :"
  28002. + "unlinking for authalg=%d\n",
  28003. + ixt->ixt_support.ias_id);
  28004. + ipsec_alg_put(ixt);
  28005. + }
  28006. + return 0;
  28007. +}
  28008. +
  28009. +IPSEC_PROCFS_DEBUG_NO_STATIC
  28010. +int
  28011. +ipsec_xform_get_info(char *buffer,
  28012. + char **start,
  28013. + off_t offset,
  28014. + int length IPSEC_PROC_LAST_ARG)
  28015. +{
  28016. + int len = 0;
  28017. + off_t begin = 0;
  28018. + int i;
  28019. + struct list_head *head;
  28020. + struct ipsec_alg *ixt;
  28021. +
  28022. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  28023. + "klips_debug:ipsec_tncfg_get_info: "
  28024. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  28025. + buffer,
  28026. + *start,
  28027. + (int)offset,
  28028. + length);
  28029. +
  28030. + for(i = 0, head = ipsec_alg_hash_table;
  28031. + i<IPSEC_ALG_HASHSZ;
  28032. + i++, head++)
  28033. + {
  28034. + struct list_head *p;
  28035. + for (p=head->next; p!=head; p=p->next)
  28036. + {
  28037. + ixt = list_entry(p, struct ipsec_alg, ixt_list);
  28038. + len += ipsec_snprintf(buffer+len, length-len,
  28039. + "VERSION=%d TYPE=%d ID=%d NAME=%s REFCNT=%d ",
  28040. + ixt->ixt_version, ixt->ixt_alg_type, ixt->ixt_support.ias_id,
  28041. + ixt->ixt_name, ixt->ixt_refcnt);
  28042. +
  28043. + len += ipsec_snprintf(buffer+len, length-len,
  28044. + "STATE=%08x BLOCKSIZE=%d IVLEN=%d KEYMINBITS=%d KEYMAXBITS=%d ",
  28045. + ixt->ixt_state, ixt->ixt_blocksize,
  28046. + ixt->ixt_support.ias_ivlen, ixt->ixt_support.ias_keyminbits, ixt->ixt_support.ias_keymaxbits);
  28047. +
  28048. + len += ipsec_snprintf(buffer+len, length-len,
  28049. + "IVLEN=%d KEYMINBITS=%d KEYMAXBITS=%d ",
  28050. + ixt->ixt_support.ias_ivlen, ixt->ixt_support.ias_keyminbits, ixt->ixt_support.ias_keymaxbits);
  28051. +
  28052. + switch(ixt->ixt_alg_type)
  28053. + {
  28054. + case IPSEC_ALG_TYPE_AUTH:
  28055. + {
  28056. + struct ipsec_alg_auth *auth = (struct ipsec_alg_auth *)ixt;
  28057. +
  28058. + len += ipsec_snprintf(buffer+len, length-len,
  28059. + "KEYLEN=%d CTXSIZE=%d AUTHLEN=%d ",
  28060. + auth->ixt_a_keylen, auth->ixt_a_ctx_size,
  28061. + auth->ixt_a_authlen);
  28062. + break;
  28063. + }
  28064. + case IPSEC_ALG_TYPE_ENCRYPT:
  28065. + {
  28066. + struct ipsec_alg_enc *enc = (struct ipsec_alg_enc *)ixt;
  28067. + len += ipsec_snprintf(buffer+len, length-len,
  28068. + "KEYLEN=%d CTXSIZE=%d ",
  28069. + enc->ixt_e_keylen, enc->ixt_e_ctx_size);
  28070. +
  28071. + break;
  28072. + }
  28073. + }
  28074. +
  28075. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  28076. + }
  28077. + }
  28078. +
  28079. + *start = buffer + (offset - begin); /* Start of wanted data */
  28080. + len -= (offset - begin); /* Start slop */
  28081. + if (len > length)
  28082. + len = length;
  28083. + return len;
  28084. +}
  28085. +
  28086. +
  28087. +/*
  28088. + * As the author of this module, I ONLY ALLOW using it from
  28089. + * GPL (or same LICENSE TERMS as kernel source) modules.
  28090. + *
  28091. + * In respect to hardware crypto engines this means:
  28092. + * * Closed-source device drivers ARE NOT ALLOWED to use
  28093. + * this interface.
  28094. + * * Closed-source VHDL/Verilog firmware running on
  28095. + * the crypto hardware device IS ALLOWED to use this interface
  28096. + * via a GPL (or same LICENSE TERMS as kernel source) device driver.
  28097. + * --Juan Jose Ciarlante 20/03/2002 (thanks RGB for the correct wording)
  28098. + */
  28099. +
  28100. +/*
  28101. + * These symbols can only be used from GPL modules
  28102. + * for now, I'm disabling this because it creates false
  28103. + * symbol problems for old modutils.
  28104. + */
  28105. +
  28106. +#ifdef CONFIG_MODULES
  28107. +#ifndef NET_26
  28108. +#if 0
  28109. +#ifndef EXPORT_SYMBOL_GPL
  28110. +#undef EXPORT_SYMBOL_GPL
  28111. +#define EXPORT_SYMBOL_GPL EXPORT_SYMBOL
  28112. +#endif
  28113. +#endif
  28114. +EXPORT_SYMBOL(register_ipsec_alg);
  28115. +EXPORT_SYMBOL(unregister_ipsec_alg);
  28116. +EXPORT_SYMBOL(ipsec_alg_test);
  28117. +#endif
  28118. +#endif
  28119. --- /dev/null Tue Mar 11 13:02:56 2003
  28120. +++ linux/net/ipsec/ipsec_alg_cryptoapi.c Mon Feb 9 13:51:03 2004
  28121. @@ -0,0 +1,455 @@
  28122. +/*
  28123. + * ipsec_alg to linux cryptoapi GLUE
  28124. + *
  28125. + * Authors: CODE.ar TEAM
  28126. + * Harpo MAxx <harpo@linuxmendoza.org.ar>
  28127. + * JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  28128. + * Luciano Ruete <docemeses@softhome.net>
  28129. + *
  28130. + * ipsec_alg_cryptoapi.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  28131. + *
  28132. + * This program is free software; you can redistribute it and/or modify it
  28133. + * under the terms of the GNU General Public License as published by the
  28134. + * Free Software Foundation; either version 2 of the License, or (at your
  28135. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  28136. + *
  28137. + * This program is distributed in the hope that it will be useful, but
  28138. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  28139. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  28140. + * for more details.
  28141. + *
  28142. + * Example usage:
  28143. + * modinfo -p ipsec_cryptoapi (quite useful info, including supported algos)
  28144. + * modprobe ipsec_cryptoapi
  28145. + * modprobe ipsec_cryptoapi test=1
  28146. + * modprobe ipsec_cryptoapi excl=1 (exclusive cipher/algo)
  28147. + * modprobe ipsec_cryptoapi noauto=1 aes=1 twofish=1 (only these ciphers)
  28148. + * modprobe ipsec_cryptoapi aes=128,128 (force these keylens)
  28149. + * modprobe ipsec_cryptoapi des_ede3=0 (everything but 3DES)
  28150. + */
  28151. +#ifndef AUTOCONF_INCLUDED
  28152. +#include <linux/config.h>
  28153. +#endif
  28154. +#include <linux/version.h>
  28155. +
  28156. +/*
  28157. + * special case: ipsec core modular with this static algo inside:
  28158. + * must avoid MODULE magic for this file
  28159. + */
  28160. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_CRYPTOAPI)
  28161. +#undef MODULE
  28162. +#endif
  28163. +
  28164. +#include <linux/module.h>
  28165. +#include <linux/init.h>
  28166. +
  28167. +#include <linux/kernel.h> /* printk() */
  28168. +#include <linux/errno.h> /* error codes */
  28169. +#include <linux/types.h> /* size_t */
  28170. +#include <linux/string.h>
  28171. +
  28172. +/* Check if __exit is defined, if not null it */
  28173. +#ifndef __exit
  28174. +#define __exit
  28175. +#endif
  28176. +
  28177. +/* warn the innocent */
  28178. +#if !defined (CONFIG_CRYPTO) && !defined (CONFIG_CRYPTO_MODULE)
  28179. +#warning "No linux CryptoAPI found, install 2.4.22+ or 2.6.x"
  28180. +#define NO_CRYPTOAPI_SUPPORT
  28181. +#endif
  28182. +
  28183. +#include "openswan.h"
  28184. +#include "openswan/ipsec_alg.h"
  28185. +#include "openswan/ipsec_policy.h"
  28186. +
  28187. +#include <linux/crypto.h>
  28188. +#ifdef CRYPTO_API_VERSION_CODE
  28189. +#warning "Old CryptoAPI is not supported. Only linux-2.4.22+ or linux-2.6.x are supported"
  28190. +#define NO_CRYPTOAPI_SUPPORT
  28191. +#endif
  28192. +
  28193. +#ifdef NO_CRYPTOAPI_SUPPORT
  28194. +#warning "Building an unusable module :P"
  28195. +/* Catch old CryptoAPI by not allowing module to load */
  28196. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_cryptoapi_init )
  28197. +{
  28198. + printk(KERN_WARNING "ipsec_cryptoapi.o was not built on stock Linux CryptoAPI (2.4.22+ or 2.6.x), not loading.\n");
  28199. + return -EINVAL;
  28200. +}
  28201. +#else
  28202. +#include <asm/scatterlist.h>
  28203. +#include <asm/pgtable.h>
  28204. +#include <linux/mm.h>
  28205. +
  28206. +#define CIPHERNAME_AES "aes"
  28207. +#define CIPHERNAME_1DES "des"
  28208. +#define CIPHERNAME_3DES "des3_ede"
  28209. +#define CIPHERNAME_BLOWFISH "blowfish"
  28210. +#define CIPHERNAME_CAST "cast5"
  28211. +#define CIPHERNAME_SERPENT "serpent"
  28212. +#define CIPHERNAME_TWOFISH "twofish"
  28213. +
  28214. +#define ESP_SERPENT 252 /* from ipsec drafts */
  28215. +#define ESP_TWOFISH 253 /* from ipsec drafts */
  28216. +
  28217. +#define DIGESTNAME_MD5 "md5"
  28218. +#define DIGESTNAME_SHA1 "sha1"
  28219. +
  28220. +MODULE_AUTHOR("Juanjo Ciarlante, Harpo MAxx, Luciano Ruete");
  28221. +static int debug_crypto=0;
  28222. +static int test_crypto=0;
  28223. +static int excl_crypto=0;
  28224. +
  28225. +static int noauto = 0;
  28226. +
  28227. +#ifdef module_param
  28228. +module_param(debug_crypto,int,0600)
  28229. +module_param(test_crypto,int,0600)
  28230. +module_param(excl_crypto,int,0600)
  28231. +
  28232. +module_param(noauto,int,0600)
  28233. +#else
  28234. +MODULE_PARM(debug_crypto, "i");
  28235. +MODULE_PARM(test_crypto, "i");
  28236. +MODULE_PARM(excl_crypto, "i");
  28237. +
  28238. +MODULE_PARM(noauto,"i");
  28239. +#endif
  28240. +MODULE_PARM_DESC(noauto, "Dont try all known algos, just setup enabled ones");
  28241. +
  28242. +#ifdef CONFIG_KLIPS_ENC_1DES
  28243. +static int des_ede1[] = {-1, -1};
  28244. +#endif
  28245. +static int des_ede3[] = {-1, -1};
  28246. +static int aes[] = {-1, -1};
  28247. +static int blowfish[] = {-1, -1};
  28248. +static int cast[] = {-1, -1};
  28249. +static int serpent[] = {-1, -1};
  28250. +static int twofish[] = {-1, -1};
  28251. +
  28252. +#ifdef CONFIG_KLIPS_ENC_1DES
  28253. +#ifdef module_param
  28254. +module_param_array(des_ede1,int,NULL,0)
  28255. +#else
  28256. +MODULE_PARM(des_ede1,"1-2i");
  28257. +#endif
  28258. +#endif
  28259. +#ifdef module_param
  28260. +module_param_array(des_ede3,int,NULL,0)
  28261. +module_param_array(aes,int,NULL,0)
  28262. +module_param_array(blowfish,int,NULL,0)
  28263. +module_param_array(cast,int,NULL,0)
  28264. +module_param_array(serpent,int,NULL,0)
  28265. +module_param_array(twofish,int,NULL,0)
  28266. +#else
  28267. +MODULE_PARM(des_ede3,"1-2i");
  28268. +MODULE_PARM(aes,"1-2i");
  28269. +MODULE_PARM(blowfish,"1-2i");
  28270. +MODULE_PARM(cast,"1-2i");
  28271. +MODULE_PARM(serpent,"1-2i");
  28272. +MODULE_PARM(twofish,"1-2i");
  28273. +#endif
  28274. +MODULE_PARM_DESC(des_ede1, "0: disable | 1: force_enable | min,max: dontuse");
  28275. +MODULE_PARM_DESC(des_ede3, "0: disable | 1: force_enable | min,max: dontuse");
  28276. +MODULE_PARM_DESC(aes, "0: disable | 1: force_enable | min,max: keybitlens");
  28277. +MODULE_PARM_DESC(blowfish, "0: disable | 1: force_enable | min,max: keybitlens");
  28278. +MODULE_PARM_DESC(cast, "0: disable | 1: force_enable | min,max: keybitlens");
  28279. +MODULE_PARM_DESC(serpent, "0: disable | 1: force_enable | min,max: keybitlens");
  28280. +MODULE_PARM_DESC(twofish, "0: disable | 1: force_enable | min,max: keybitlens");
  28281. +
  28282. +struct ipsec_alg_capi_cipher {
  28283. + const char *ciphername; /* cryptoapi's ciphername */
  28284. + unsigned blocksize;
  28285. + unsigned short minbits;
  28286. + unsigned short maxbits;
  28287. + int *parm; /* lkm param for this cipher */
  28288. + struct ipsec_alg_enc alg; /* note it's not a pointer */
  28289. +};
  28290. +
  28291. +static struct ipsec_alg_capi_cipher alg_capi_carray[] = {
  28292. + { CIPHERNAME_AES, 16, 128, 256, aes, { ixt_common:{ ixt_support:{ ias_id: ESP_AES}}}},
  28293. + { CIPHERNAME_TWOFISH, 16, 128, 256, twofish, { ixt_common:{ ixt_support:{ ias_id: ESP_TWOFISH,}}}},
  28294. + { CIPHERNAME_SERPENT, 16, 128, 256, serpent, { ixt_common:{ ixt_support:{ ias_id: ESP_SERPENT,}}}},
  28295. + { CIPHERNAME_CAST, 8, 128, 128, cast , { ixt_common:{ ixt_support:{ ias_id: ESP_CAST,}}}},
  28296. + { CIPHERNAME_BLOWFISH, 8, 96, 448, blowfish, { ixt_common:{ ixt_support:{ ias_id: ESP_BLOWFISH,}}}},
  28297. + { CIPHERNAME_3DES, 8, 192, 192, des_ede3, { ixt_common:{ ixt_support:{ ias_id: ESP_3DES,}}}},
  28298. +#ifdef CONFIG_KLIPS_ENC_1DES
  28299. + { CIPHERNAME_1DES, 8, 64, 64, des_ede1, { ixt_common:{ ixt_support:{ ias_id: ESP_DES,}}}},
  28300. +#endif
  28301. + { NULL, 0, 0, 0, NULL, {} }
  28302. +};
  28303. +
  28304. +#ifdef NOT_YET
  28305. +struct ipsec_alg_capi_digest {
  28306. + const char *digestname; /* cryptoapi's digestname */
  28307. + struct digest_implementation *di;
  28308. + struct ipsec_alg_auth alg; /* note it's not a pointer */
  28309. +};
  28310. +static struct ipsec_alg_capi_cipher alg_capi_darray[] = {
  28311. + { DIGESTNAME_MD5, NULL, { ixt_alg_id: AH_MD5, }},
  28312. + { DIGESTNAME_SHA1, NULL, { ixt_alg_id: AH_SHA, }},
  28313. + { NULL, NULL, {} }
  28314. +};
  28315. +#endif
  28316. +/*
  28317. + * "generic" linux cryptoapi setup_cipher() function
  28318. + */
  28319. +int setup_cipher(const char *ciphername)
  28320. +{
  28321. + return crypto_alg_available(ciphername, 0);
  28322. +}
  28323. +
  28324. +/*
  28325. + * setups ipsec_alg_capi_cipher "hyper" struct components, calling
  28326. + * register_ipsec_alg for cointaned ipsec_alg object
  28327. + */
  28328. +static void _capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e);
  28329. +static __u8 * _capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen);
  28330. +static int _capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt);
  28331. +
  28332. +static int
  28333. +setup_ipsec_alg_capi_cipher(struct ipsec_alg_capi_cipher *cptr)
  28334. +{
  28335. + int ret;
  28336. + cptr->alg.ixt_common.ixt_version = IPSEC_ALG_VERSION;
  28337. + cptr->alg.ixt_common.ixt_module = THIS_MODULE;
  28338. + atomic_set (& cptr->alg.ixt_common.ixt_refcnt, 0);
  28339. + strncpy (cptr->alg.ixt_common.ixt_name , cptr->ciphername, sizeof (cptr->alg.ixt_common.ixt_name));
  28340. +
  28341. + cptr->alg.ixt_common.ixt_blocksize=cptr->blocksize;
  28342. + cptr->alg.ixt_common.ixt_support.ias_keyminbits=cptr->minbits;
  28343. + cptr->alg.ixt_common.ixt_support.ias_keymaxbits=cptr->maxbits;
  28344. + cptr->alg.ixt_common.ixt_state = 0;
  28345. + if (excl_crypto) cptr->alg.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  28346. + cptr->alg.ixt_e_keylen=cptr->alg.ixt_common.ixt_support.ias_keymaxbits/8;
  28347. + cptr->alg.ixt_e_ctx_size = 0;
  28348. + cptr->alg.ixt_common.ixt_support.ias_exttype = IPSEC_ALG_TYPE_ENCRYPT;
  28349. + cptr->alg.ixt_e_new_key = _capi_new_key;
  28350. + cptr->alg.ixt_e_destroy_key = _capi_destroy_key;
  28351. + cptr->alg.ixt_e_cbc_encrypt = _capi_cbc_encrypt;
  28352. + cptr->alg.ixt_common.ixt_data = cptr;
  28353. +
  28354. + ret=register_ipsec_alg_enc(&cptr->alg);
  28355. + printk(KERN_INFO "KLIPS cryptoapi interface: "
  28356. + "alg_type=%d alg_id=%d name=%s "
  28357. + "keyminbits=%d keymaxbits=%d, %s(%d)\n",
  28358. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28359. + cptr->alg.ixt_common.ixt_support.ias_id,
  28360. + cptr->alg.ixt_common.ixt_name,
  28361. + cptr->alg.ixt_common.ixt_support.ias_keyminbits,
  28362. + cptr->alg.ixt_common.ixt_support.ias_keymaxbits,
  28363. + ret ? "not found" : "found", ret);
  28364. + return ret;
  28365. +}
  28366. +/*
  28367. + * called in ipsec_sa_wipe() time, will destroy key contexts
  28368. + * and do 1 unbind()
  28369. + */
  28370. +static void
  28371. +_capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e)
  28372. +{
  28373. + struct crypto_tfm *tfm=(struct crypto_tfm*)key_e;
  28374. +
  28375. + if (debug_crypto > 0)
  28376. + printk(KERN_DEBUG "klips_debug: _capi_destroy_key:"
  28377. + "name=%s key_e=%p \n",
  28378. + alg->ixt_common.ixt_name, key_e);
  28379. + if (!key_e) {
  28380. + printk(KERN_ERR "klips_debug: _capi_destroy_key:"
  28381. + "name=%s NULL key_e!\n",
  28382. + alg->ixt_common.ixt_name);
  28383. + return;
  28384. + }
  28385. + crypto_free_tfm(tfm);
  28386. +}
  28387. +
  28388. +/*
  28389. + * create new key context, need alg->ixt_data to know which
  28390. + * (of many) cipher inside this module is the target
  28391. + */
  28392. +static __u8 *
  28393. +_capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen)
  28394. +{
  28395. + struct ipsec_alg_capi_cipher *cptr;
  28396. + struct crypto_tfm *tfm=NULL;
  28397. +
  28398. + cptr = alg->ixt_common.ixt_data;
  28399. + if (!cptr) {
  28400. + printk(KERN_ERR "_capi_new_key(): "
  28401. + "NULL ixt_data (?!) for \"%s\" algo\n"
  28402. + , alg->ixt_common.ixt_name);
  28403. + goto err;
  28404. + }
  28405. + if (debug_crypto > 0)
  28406. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  28407. + "name=%s cptr=%p key=%p keysize=%d\n",
  28408. + alg->ixt_common.ixt_name, cptr, key, keylen);
  28409. +
  28410. + /*
  28411. + * alloc tfm
  28412. + */
  28413. + tfm = crypto_alloc_tfm(cptr->ciphername, CRYPTO_TFM_MODE_CBC);
  28414. + if (!tfm) {
  28415. + printk(KERN_ERR "_capi_new_key(): "
  28416. + "NULL tfm for \"%s\" cryptoapi (\"%s\") algo\n"
  28417. + , alg->ixt_common.ixt_name, cptr->ciphername);
  28418. + goto err;
  28419. + }
  28420. + if (crypto_cipher_setkey(tfm, key, keylen) < 0) {
  28421. + printk(KERN_ERR "_capi_new_key(): "
  28422. + "failed new_key() for \"%s\" cryptoapi algo (keylen=%d)\n"
  28423. + , alg->ixt_common.ixt_name, keylen);
  28424. + crypto_free_tfm(tfm);
  28425. + tfm=NULL;
  28426. + }
  28427. +err:
  28428. + if (debug_crypto > 0)
  28429. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  28430. + "name=%s key=%p keylen=%d tfm=%p\n",
  28431. + alg->ixt_common.ixt_name, key, keylen, tfm);
  28432. + return (__u8 *) tfm;
  28433. +}
  28434. +/*
  28435. + * core encryption function: will use cx->ci to call actual cipher's
  28436. + * cbc function
  28437. + */
  28438. +static int
  28439. +_capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt) {
  28440. + int error =0;
  28441. + struct crypto_tfm *tfm=(struct crypto_tfm *)key_e;
  28442. + struct scatterlist sg = {
  28443. + .page = virt_to_page(in),
  28444. + .offset = (unsigned long)(in) % PAGE_SIZE,
  28445. + .length=ilen,
  28446. + };
  28447. + if (debug_crypto > 1)
  28448. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  28449. + "key_e=%p "
  28450. + "in=%p out=%p ilen=%d iv=%p encrypt=%d\n"
  28451. + , key_e
  28452. + , in, in, ilen, iv, encrypt);
  28453. + crypto_cipher_set_iv(tfm, iv, crypto_tfm_alg_ivsize(tfm));
  28454. + if (encrypt)
  28455. + error = crypto_cipher_encrypt (tfm, &sg, &sg, ilen);
  28456. + else
  28457. + error = crypto_cipher_decrypt (tfm, &sg, &sg, ilen);
  28458. + if (debug_crypto > 1)
  28459. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  28460. + "error=%d\n"
  28461. + , error);
  28462. + return (error<0)? error : ilen;
  28463. +}
  28464. +/*
  28465. + * main initialization loop: for each cipher in list, do
  28466. + * 1) setup cryptoapi cipher else continue
  28467. + * 2) register ipsec_alg object
  28468. + */
  28469. +static int
  28470. +setup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28471. +{
  28472. + struct ipsec_alg_capi_cipher *cptr;
  28473. + /* foreach cipher in list ... */
  28474. + for (cptr=clist;cptr->ciphername;cptr++) {
  28475. + /*
  28476. + * see if cipher has been disabled (0) or
  28477. + * if noauto set and not enabled (1)
  28478. + */
  28479. + if (cptr->parm[0] == 0 || (noauto && cptr->parm[0] < 0)) {
  28480. + if (debug_crypto>0)
  28481. + printk(KERN_INFO "setup_cipher_list(): "
  28482. + "ciphername=%s skipped at user request: "
  28483. + "noauto=%d parm[0]=%d parm[1]=%d\n"
  28484. + , cptr->ciphername
  28485. + , noauto
  28486. + , cptr->parm[0]
  28487. + , cptr->parm[1]);
  28488. + continue;
  28489. + }
  28490. + /*
  28491. + * use a local ci to avoid touching cptr->ci,
  28492. + * if register ipsec_alg success then bind cipher
  28493. + */
  28494. + if(cptr->alg.ixt_common.ixt_support.ias_name == NULL) {
  28495. + cptr->alg.ixt_common.ixt_support.ias_name = cptr->ciphername;
  28496. + }
  28497. +
  28498. + if( setup_cipher(cptr->ciphername) ) {
  28499. + if (debug_crypto > 0)
  28500. + printk(KERN_DEBUG "klips_debug:"
  28501. + "setup_cipher_list():"
  28502. + "ciphername=%s found\n"
  28503. + , cptr->ciphername);
  28504. +
  28505. + if (setup_ipsec_alg_capi_cipher(cptr) != 0) {
  28506. + printk(KERN_ERR "klips_debug:"
  28507. + "setup_cipher_list():"
  28508. + "ciphername=%s failed ipsec_alg_register\n"
  28509. + , cptr->ciphername);
  28510. + }
  28511. + } else {
  28512. + printk(KERN_INFO "KLIPS: lookup for ciphername=%s: not found \n",
  28513. + cptr->ciphername);
  28514. + }
  28515. + }
  28516. + return 0;
  28517. +}
  28518. +/*
  28519. + * deregister ipsec_alg objects and unbind ciphers
  28520. + */
  28521. +static int
  28522. +unsetup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28523. +{
  28524. + struct ipsec_alg_capi_cipher *cptr;
  28525. + /* foreach cipher in list ... */
  28526. + for (cptr=clist;cptr->ciphername;cptr++) {
  28527. + if (cptr->alg.ixt_common.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  28528. + unregister_ipsec_alg_enc(&cptr->alg);
  28529. + }
  28530. + }
  28531. + return 0;
  28532. +}
  28533. +/*
  28534. + * test loop for registered algos
  28535. + */
  28536. +static int
  28537. +test_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28538. +{
  28539. + int test_ret;
  28540. + struct ipsec_alg_capi_cipher *cptr;
  28541. + /* foreach cipher in list ... */
  28542. + for (cptr=clist;cptr->ciphername;cptr++) {
  28543. + if (cptr->alg.ixt_common.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  28544. + test_ret=ipsec_alg_test(
  28545. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28546. + cptr->alg.ixt_common.ixt_support.ias_id,
  28547. + test_crypto);
  28548. + printk("test_cipher_list(alg_type=%d alg_id=%d): test_ret=%d\n",
  28549. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28550. + cptr->alg.ixt_common.ixt_support.ias_id,
  28551. + test_ret);
  28552. + }
  28553. + }
  28554. + return 0;
  28555. +}
  28556. +
  28557. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_cryptoapi_init )
  28558. +{
  28559. + int ret, test_ret;
  28560. + if ((ret=setup_cipher_list(alg_capi_carray)) < 0)
  28561. + return -EPROTONOSUPPORT;
  28562. + if (ret==0 && test_crypto) {
  28563. + test_ret=test_cipher_list(alg_capi_carray);
  28564. + }
  28565. + return ret;
  28566. +}
  28567. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_cryptoapi_fini )
  28568. +{
  28569. + unsetup_cipher_list(alg_capi_carray);
  28570. + return;
  28571. +}
  28572. +#ifdef MODULE_LICENSE
  28573. +MODULE_LICENSE("GPL");
  28574. +#endif
  28575. +
  28576. +#endif /* NO_CRYPTOAPI_SUPPORT */
  28577. --- /dev/null Tue Mar 11 13:02:56 2003
  28578. +++ linux/net/ipsec/ipsec_esp.c Mon Feb 9 13:51:03 2004
  28579. @@ -0,0 +1,607 @@
  28580. +/*
  28581. + * processing code for ESP
  28582. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  28583. + *
  28584. + * This program is free software; you can redistribute it and/or modify it
  28585. + * under the terms of the GNU General Public License as published by the
  28586. + * Free Software Foundation; either version 2 of the License, or (at your
  28587. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  28588. + *
  28589. + * This program is distributed in the hope that it will be useful, but
  28590. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  28591. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  28592. + * for more details.
  28593. + */
  28594. +
  28595. +char ipsec_esp_c_version[] = "RCSID $Id: ipsec_esp.c,v 1.13.2.6 2006/10/06 21:39:26 paul Exp $";
  28596. +#ifndef AUTOCONF_INCLUDED
  28597. +#include <linux/config.h>
  28598. +#endif
  28599. +#include <linux/version.h>
  28600. +
  28601. +#define __NO_VERSION__
  28602. +#include <linux/module.h>
  28603. +#include <linux/kernel.h> /* printk() */
  28604. +
  28605. +#include "openswan/ipsec_param.h"
  28606. +
  28607. +#ifdef MALLOC_SLAB
  28608. +# include <linux/slab.h> /* kmalloc() */
  28609. +#else /* MALLOC_SLAB */
  28610. +# include <linux/malloc.h> /* kmalloc() */
  28611. +#endif /* MALLOC_SLAB */
  28612. +#include <linux/errno.h> /* error codes */
  28613. +#include <linux/types.h> /* size_t */
  28614. +#include <linux/interrupt.h> /* mark_bh */
  28615. +
  28616. +#include <linux/netdevice.h> /* struct device, and other headers */
  28617. +#include <linux/etherdevice.h> /* eth_type_trans */
  28618. +#include <linux/ip.h> /* struct iphdr */
  28619. +#include <linux/skbuff.h>
  28620. +#include <openswan.h>
  28621. +#ifdef SPINLOCK
  28622. +# ifdef SPINLOCK_23
  28623. +# include <linux/spinlock.h> /* *lock* */
  28624. +# else /* SPINLOCK_23 */
  28625. +# include <asm/spinlock.h> /* *lock* */
  28626. +# endif /* SPINLOCK_23 */
  28627. +#endif /* SPINLOCK */
  28628. +
  28629. +#include <net/ip.h>
  28630. +#include <net/protocol.h>
  28631. +
  28632. +#include "openswan/radij.h"
  28633. +#include "openswan/ipsec_encap.h"
  28634. +#include "openswan/ipsec_sa.h"
  28635. +
  28636. +#include "openswan/ipsec_radij.h"
  28637. +#include "openswan/ipsec_xform.h"
  28638. +#include "openswan/ipsec_tunnel.h"
  28639. +#include "openswan/ipsec_rcv.h"
  28640. +#include "openswan/ipsec_xmit.h"
  28641. +
  28642. +#include "openswan/ipsec_auth.h"
  28643. +
  28644. +#ifdef CONFIG_KLIPS_ESP
  28645. +#include "openswan/ipsec_esp.h"
  28646. +#endif /* CONFIG_KLIPS_ESP */
  28647. +
  28648. +#include "openswan/ipsec_proto.h"
  28649. +#include "openswan/ipsec_alg.h"
  28650. +
  28651. +#ifdef CONFIG_KLIPS_DEBUG
  28652. +#define ESP_DMP(_x,_y,_z) if(debug_rcv && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  28653. +#else
  28654. +#define ESP_DMP(_x,_y,_z)
  28655. +#endif
  28656. +
  28657. +#ifdef CONFIG_KLIPS_ESP
  28658. +enum ipsec_rcv_value
  28659. +ipsec_rcv_esp_checks(struct ipsec_rcv_state *irs,
  28660. + struct sk_buff *skb)
  28661. +{
  28662. + __u8 proto;
  28663. + int len; /* packet length */
  28664. +
  28665. + len = skb->len;
  28666. + proto = irs->ipp->protocol;
  28667. +
  28668. + /* XXX this will need to be 8 for IPv6 */
  28669. + if ((proto == IPPROTO_ESP) && ((len - irs->iphlen) % 4)) {
  28670. + printk("klips_error:ipsec_rcv: "
  28671. + "got packet with content length = %d from %s -- should be on 4 octet boundary, packet dropped\n",
  28672. + len - irs->iphlen,
  28673. + irs->ipsaddr_txt);
  28674. + if(irs->stats) {
  28675. + irs->stats->rx_errors++;
  28676. + }
  28677. + return IPSEC_RCV_BADLEN;
  28678. + }
  28679. +
  28680. + if(skb->len < (irs->hard_header_len + sizeof(struct iphdr) + sizeof(struct esphdr))) {
  28681. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  28682. + "klips_debug:ipsec_rcv: "
  28683. + "runt esp packet of skb->len=%d received from %s, dropped.\n",
  28684. + skb->len,
  28685. + irs->ipsaddr_txt);
  28686. + if(irs->stats) {
  28687. + irs->stats->rx_errors++;
  28688. + }
  28689. + return IPSEC_RCV_BADLEN;
  28690. + }
  28691. +
  28692. + irs->protostuff.espstuff.espp = (struct esphdr *)skb->h.raw;
  28693. + irs->said.spi = irs->protostuff.espstuff.espp->esp_spi;
  28694. +
  28695. + return IPSEC_RCV_OK;
  28696. +}
  28697. +
  28698. +enum ipsec_rcv_value
  28699. +ipsec_rcv_esp_decrypt_setup(struct ipsec_rcv_state *irs,
  28700. + struct sk_buff *skb,
  28701. + __u32 *replay,
  28702. + unsigned char **authenticator)
  28703. +{
  28704. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28705. + //unsigned char *idat = (unsigned char *)espp;
  28706. +
  28707. + KLIPS_PRINT(debug_rcv,
  28708. + "klips_debug:ipsec_rcv: "
  28709. + "packet from %s received with seq=%d (iv)=0x%08x%08x iplen=%d esplen=%d sa=%s\n",
  28710. + irs->ipsaddr_txt,
  28711. + (__u32)ntohl(espp->esp_rpl),
  28712. + (__u32)ntohl(*((__u32 *)(espp->esp_iv) )),
  28713. + (__u32)ntohl(*((__u32 *)(espp->esp_iv) + 1)),
  28714. + irs->len,
  28715. + irs->ilen,
  28716. + irs->sa_len ? irs->sa : " (error)");
  28717. +
  28718. + *replay = ntohl(espp->esp_rpl);
  28719. + *authenticator = &(skb->h.raw[irs->ilen]);
  28720. +
  28721. + return IPSEC_RCV_OK;
  28722. +}
  28723. +
  28724. +enum ipsec_rcv_value
  28725. +ipsec_rcv_esp_authcalc(struct ipsec_rcv_state *irs,
  28726. + struct sk_buff *skb)
  28727. +{
  28728. + struct auth_alg *aa;
  28729. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28730. + union {
  28731. + MD5_CTX md5;
  28732. + SHA1_CTX sha1;
  28733. + } tctx;
  28734. +
  28735. + if (irs->ipsp->ips_alg_auth) {
  28736. + KLIPS_PRINT(debug_rcv,
  28737. + "klips_debug:ipsec_rcv: "
  28738. + "ipsec_alg hashing proto=%d... ",
  28739. + irs->said.proto);
  28740. + if(irs->said.proto == IPPROTO_ESP) {
  28741. + ipsec_alg_sa_esp_hash(irs->ipsp,
  28742. + (caddr_t)espp, irs->ilen,
  28743. + irs->hash, AHHMAC_HASHLEN);
  28744. + return IPSEC_RCV_OK;
  28745. + }
  28746. + return IPSEC_RCV_BADPROTO;
  28747. + }
  28748. + aa = irs->authfuncs;
  28749. +
  28750. + /* copy the initialized keying material */
  28751. + memcpy(&tctx, irs->ictx, irs->ictx_len);
  28752. +
  28753. +#ifdef HASH_DEBUG
  28754. + ESP_DMP("ictx", irs->ictx, irs->ictx_len);
  28755. +
  28756. + ESP_DMP("mac_esp", (caddr_t)espp, irs->ilen);
  28757. +#endif
  28758. + (*aa->update)((void *)&tctx, (caddr_t)espp, irs->ilen);
  28759. +
  28760. + (*aa->final)(irs->hash, (void *)&tctx);
  28761. +
  28762. +#ifdef HASH_DEBUG
  28763. + ESP_DMP("hash1", irs->hash, aa->hashlen);
  28764. +#endif
  28765. +
  28766. + memcpy(&tctx, irs->octx, irs->octx_len);
  28767. +
  28768. +#ifdef HASH_DEBUG
  28769. + ESP_DMP("octx", irs->octx, irs->octx_len);
  28770. +#endif
  28771. +
  28772. + (*aa->update)((void *)&tctx, irs->hash, aa->hashlen);
  28773. + (*aa->final)(irs->hash, (void *)&tctx);
  28774. +
  28775. + return IPSEC_RCV_OK;
  28776. +}
  28777. +
  28778. +
  28779. +enum ipsec_rcv_value
  28780. +ipsec_rcv_esp_decrypt(struct ipsec_rcv_state *irs)
  28781. +{
  28782. + struct ipsec_sa *ipsp = irs->ipsp;
  28783. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28784. + int i;
  28785. + int pad = 0, padlen;
  28786. + int badpad = 0;
  28787. + int esphlen = 0;
  28788. + __u8 *idat; /* pointer to content to be decrypted/authenticated */
  28789. + int encaplen = 0;
  28790. + struct sk_buff *skb;
  28791. + struct ipsec_alg_enc *ixt_e=NULL;
  28792. +
  28793. + skb=irs->skb;
  28794. +
  28795. + idat = skb->h.raw;
  28796. +
  28797. + /* encaplen is the distance between the end of the IP
  28798. + * header and the beginning of the ESP header.
  28799. + * on ESP headers it is zero, but on UDP-encap ESP
  28800. + * it includes the space for the UDP header.
  28801. + *
  28802. + * Note: UDP-encap code has already moved the
  28803. + * skb->data forward to accomodate this.
  28804. + */
  28805. + encaplen = idat - (skb->nh.raw + irs->iphlen);
  28806. +
  28807. + ixt_e=ipsp->ips_alg_enc;
  28808. + esphlen = ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  28809. + KLIPS_PRINT(debug_rcv,
  28810. + "klips_debug:ipsec_rcv: "
  28811. + "encalg=%d esphlen=%d\n",
  28812. + ipsp->ips_encalg, esphlen);
  28813. +
  28814. + idat += esphlen;
  28815. + irs->ilen -= esphlen;
  28816. +
  28817. + if (ipsec_alg_esp_encrypt(ipsp,
  28818. + idat, irs->ilen, espp->esp_iv,
  28819. + IPSEC_ALG_DECRYPT) <= 0) {
  28820. +#ifdef CONFIG_KLIPS_DEBUG
  28821. + KLIPS_ERROR(debug_rcv, "klips_error:ipsec_rcv: "
  28822. + "got packet with esplen = %d "
  28823. + "from %s -- should be on "
  28824. + "ENC(%d) octet boundary, "
  28825. + "packet dropped\n",
  28826. + irs->ilen,
  28827. + irs->ipsaddr_txt,
  28828. + ipsp->ips_encalg);
  28829. +#endif
  28830. + if(irs->stats) {
  28831. + irs->stats->rx_errors++;
  28832. + }
  28833. + return IPSEC_RCV_BAD_DECRYPT;
  28834. + }
  28835. +
  28836. + ESP_DMP("postdecrypt", idat, irs->ilen);
  28837. +
  28838. + irs->next_header = idat[irs->ilen - 1];
  28839. + padlen = idat[irs->ilen - 2];
  28840. + pad = padlen + 2 + irs->authlen;
  28841. +
  28842. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28843. + "klips_debug:ipsec_rcv: "
  28844. + "padlen=%d, contents: 0x<offset>: 0x<value> 0x<value> ...\n",
  28845. + padlen);
  28846. +
  28847. + for (i = 1; i <= padlen; i++) {
  28848. + if((i % 16) == 1) {
  28849. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28850. + "klips_debug: %02x:",
  28851. + i - 1);
  28852. + }
  28853. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28854. + " %02x",
  28855. + idat[irs->ilen - 2 - padlen + i - 1]);
  28856. + if(i != idat[irs->ilen - 2 - padlen + i - 1]) {
  28857. + badpad = 1;
  28858. + }
  28859. + if((i % 16) == 0) {
  28860. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28861. + "\n");
  28862. + }
  28863. + }
  28864. + if((i % 16) != 1) {
  28865. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28866. + "\n");
  28867. + }
  28868. + if(badpad) {
  28869. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28870. + "klips_debug:ipsec_rcv: "
  28871. + "warning, decrypted packet from %s has bad padding\n",
  28872. + irs->ipsaddr_txt);
  28873. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28874. + "klips_debug:ipsec_rcv: "
  28875. + "...may be bad decryption -- not dropped\n");
  28876. + ipsp->ips_errs.ips_encpad_errs += 1;
  28877. + }
  28878. +
  28879. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28880. + "klips_debug:ipsec_rcv: "
  28881. + "packet decrypted from %s: next_header = %d, padding = %d\n",
  28882. + irs->ipsaddr_txt,
  28883. + irs->next_header,
  28884. + pad - 2 - irs->authlen);
  28885. +
  28886. + irs->ipp->tot_len = htons(ntohs(irs->ipp->tot_len) - (esphlen + pad));
  28887. +
  28888. + /*
  28889. + * move the IP header forward by the size of the ESP header, which
  28890. + * will remove the the ESP header from the packet.
  28891. + *
  28892. + * XXX this is really unnecessary, since odds we are in tunnel
  28893. + * mode, and we will be *removing* this IP header.
  28894. + *
  28895. + */
  28896. + memmove((void *)(idat - irs->iphlen),
  28897. + (void *)(skb->nh.raw), irs->iphlen);
  28898. +
  28899. + ESP_DMP("esp postmove", (idat - irs->iphlen),
  28900. + irs->iphlen + irs->ilen);
  28901. +
  28902. + /* skb_pull below, will move up by esphlen */
  28903. +
  28904. + /* XXX not clear how this can happen, as the message indicates */
  28905. + if(skb->len < esphlen) {
  28906. + printk(KERN_WARNING
  28907. + "klips_error:ipsec_rcv: "
  28908. + "tried to skb_pull esphlen=%d, %d available. This should never happen, please report.\n",
  28909. + esphlen, (int)(skb->len));
  28910. + return IPSEC_RCV_ESP_DECAPFAIL;
  28911. + }
  28912. + skb_pull(skb, esphlen);
  28913. + skb->nh.raw = idat - irs->iphlen;
  28914. + irs->ipp = skb->nh.iph;
  28915. +
  28916. + ESP_DMP("esp postpull", skb->data, skb->len);
  28917. +
  28918. + /* now, trip off the padding from the end */
  28919. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  28920. + "klips_debug:ipsec_rcv: "
  28921. + "trimming to %d.\n",
  28922. + irs->len - esphlen - pad);
  28923. + if(pad + esphlen <= irs->len) {
  28924. + skb_trim(skb, irs->len - esphlen - pad);
  28925. + } else {
  28926. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  28927. + "klips_debug:ipsec_rcv: "
  28928. + "bogus packet, size is zero or negative, dropping.\n");
  28929. + return IPSEC_RCV_DECAPFAIL;
  28930. + }
  28931. +
  28932. + return IPSEC_RCV_OK;
  28933. +}
  28934. +
  28935. +/*
  28936. + *
  28937. + */
  28938. +enum ipsec_xmit_value
  28939. +ipsec_xmit_esp_setup(struct ipsec_xmit_state *ixs)
  28940. +{
  28941. +#ifdef CONFIG_KLIPS_ENC_3DES
  28942. + __u32 iv[2];
  28943. +#endif
  28944. + struct esphdr *espp;
  28945. + int ilen = 0;
  28946. + int padlen = 0, i;
  28947. + unsigned char *dat;
  28948. + unsigned char *idat, *pad;
  28949. + __u8 hash[AH_AMAX];
  28950. + union {
  28951. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  28952. + MD5_CTX md5;
  28953. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  28954. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  28955. + SHA1_CTX sha1;
  28956. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  28957. + } tctx;
  28958. +
  28959. + dat = (unsigned char *)ixs->iph;
  28960. +
  28961. + espp = (struct esphdr *)(dat + ixs->iphlen);
  28962. + espp->esp_spi = ixs->ipsp->ips_said.spi;
  28963. + espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  28964. +
  28965. + switch(ixs->ipsp->ips_encalg) {
  28966. +#if defined(CONFIG_KLIPS_ENC_3DES)
  28967. +#ifdef CONFIG_KLIPS_ENC_3DES
  28968. + case ESP_3DES:
  28969. +#endif /* CONFIG_KLIPS_ENC_3DES */
  28970. + iv[0] = *((__u32*)&(espp->esp_iv) ) =
  28971. + ((__u32*)(ixs->ipsp->ips_iv))[0];
  28972. + iv[1] = *((__u32*)&(espp->esp_iv) + 1) =
  28973. + ((__u32*)(ixs->ipsp->ips_iv))[1];
  28974. + break;
  28975. +#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
  28976. + default:
  28977. + ixs->stats->tx_errors++;
  28978. + return IPSEC_XMIT_ESP_BADALG;
  28979. + }
  28980. +
  28981. + idat = dat + ixs->iphlen + sizeof(struct esphdr);
  28982. + ilen = ixs->skb->len - (ixs->iphlen + sizeof(struct esphdr) + ixs->authlen);
  28983. +
  28984. + /* Self-describing padding */
  28985. + pad = &dat[ixs->skb->len - ixs->tailroom];
  28986. + padlen = ixs->tailroom - 2 - ixs->authlen;
  28987. + for (i = 0; i < padlen; i++) {
  28988. + pad[i] = i + 1;
  28989. + }
  28990. + dat[ixs->skb->len - ixs->authlen - 2] = padlen;
  28991. +
  28992. + dat[ixs->skb->len - ixs->authlen - 1] = ixs->iph->protocol;
  28993. + ixs->iph->protocol = IPPROTO_ESP;
  28994. +
  28995. + switch(ixs->ipsp->ips_encalg) {
  28996. +#ifdef CONFIG_KLIPS_ENC_3DES
  28997. + case ESP_3DES:
  28998. + des_ede3_cbc_encrypt((des_cblock *)idat,
  28999. + (des_cblock *)idat,
  29000. + ilen,
  29001. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[0].ks,
  29002. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[1].ks,
  29003. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[2].ks,
  29004. + (des_cblock *)iv, 1);
  29005. + break;
  29006. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29007. + default:
  29008. + ixs->stats->tx_errors++;
  29009. + return IPSEC_XMIT_ESP_BADALG;
  29010. + }
  29011. +
  29012. + switch(ixs->ipsp->ips_encalg) {
  29013. +#if defined(CONFIG_KLIPS_ENC_3DES)
  29014. +#ifdef CONFIG_KLIPS_ENC_3DES
  29015. + case ESP_3DES:
  29016. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29017. + /* XXX update IV with the last 8 octets of the encryption */
  29018. +#if KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK
  29019. + ((__u32*)(ixs->ipsp->ips_iv))[0] =
  29020. + ((__u32 *)(idat))[(ilen >> 2) - 2];
  29021. + ((__u32*)(ixs->ipsp->ips_iv))[1] =
  29022. + ((__u32 *)(idat))[(ilen >> 2) - 1];
  29023. +#else /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
  29024. + prng_bytes(&ipsec_prng, (char *)ixs->ipsp->ips_iv, EMT_ESPDES_IV_SZ);
  29025. +#endif /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
  29026. + break;
  29027. +#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
  29028. + default:
  29029. + ixs->stats->tx_errors++;
  29030. + return IPSEC_XMIT_ESP_BADALG;
  29031. + }
  29032. +
  29033. + switch(ixs->ipsp->ips_authalg) {
  29034. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  29035. + case AH_MD5:
  29036. + ipsec_xmit_dmp("espp", (char*)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29037. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  29038. + ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  29039. + osMD5Update(&tctx.md5, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29040. + ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  29041. + osMD5Final(hash, &tctx.md5);
  29042. + ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
  29043. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  29044. + ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  29045. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  29046. + ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  29047. + osMD5Final(hash, &tctx.md5);
  29048. + ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
  29049. + memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
  29050. +
  29051. + /* paranoid */
  29052. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  29053. + memset((caddr_t)hash, 0, sizeof(*hash));
  29054. + break;
  29055. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  29056. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  29057. + case AH_SHA:
  29058. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  29059. + SHA1Update(&tctx.sha1, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29060. + SHA1Final(hash, &tctx.sha1);
  29061. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  29062. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  29063. + SHA1Final(hash, &tctx.sha1);
  29064. + memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
  29065. +
  29066. + /* paranoid */
  29067. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  29068. + memset((caddr_t)hash, 0, sizeof(*hash));
  29069. + break;
  29070. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  29071. + case AH_NONE:
  29072. + break;
  29073. + default:
  29074. + ixs->stats->tx_errors++;
  29075. + return IPSEC_XMIT_AH_BADALG;
  29076. + }
  29077. +
  29078. + ixs->skb->h.raw = (unsigned char*)espp;
  29079. +
  29080. + return IPSEC_XMIT_OK;
  29081. +}
  29082. +
  29083. +
  29084. +struct xform_functions esp_xform_funcs[]={
  29085. + { rcv_checks: ipsec_rcv_esp_checks,
  29086. + rcv_setup_auth: ipsec_rcv_esp_decrypt_setup,
  29087. + rcv_calc_auth: ipsec_rcv_esp_authcalc,
  29088. + rcv_decrypt: ipsec_rcv_esp_decrypt,
  29089. +
  29090. + xmit_setup: ipsec_xmit_esp_setup,
  29091. + xmit_headroom: sizeof(struct esphdr),
  29092. + xmit_needtailroom: 1,
  29093. + },
  29094. +};
  29095. +
  29096. +#ifdef NET_26
  29097. +struct inet_protocol esp_protocol = {
  29098. + .handler = ipsec_rcv,
  29099. + .no_policy = 1,
  29100. +};
  29101. +#else
  29102. +struct inet_protocol esp_protocol =
  29103. +{
  29104. + ipsec_rcv, /* ESP handler */
  29105. + NULL, /* TUNNEL error control */
  29106. +#ifdef NETDEV_25
  29107. + 1, /* no policy */
  29108. +#else
  29109. + 0, /* next */
  29110. + IPPROTO_ESP, /* protocol ID */
  29111. + 0, /* copy */
  29112. + NULL, /* data */
  29113. + "ESP" /* name */
  29114. +#endif
  29115. +};
  29116. +#endif /* NET_26 */
  29117. +
  29118. +#endif /* !CONFIG_KLIPS_ESP */
  29119. +
  29120. +
  29121. +/*
  29122. + * $Log: ipsec_esp.c,v $
  29123. + * Revision 1.13.2.6 2006/10/06 21:39:26 paul
  29124. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  29125. + * set. This is defined through autoconf.h which is included through the
  29126. + * linux kernel build macros.
  29127. + *
  29128. + * Revision 1.13.2.5 2006/08/24 03:02:01 paul
  29129. + * Compile fixes for when CONFIG_KLIPS_DEBUG is not set. (bug #642)
  29130. + *
  29131. + * Revision 1.13.2.4 2006/05/06 03:07:38 ken
  29132. + * Pull in proper padsize->tailroom fix from #public
  29133. + * Need to do correct math on padlen since padsize is not equal to tailroom
  29134. + *
  29135. + * Revision 1.13.2.3 2006/05/05 03:58:04 ken
  29136. + * ixs->padsize becomes ixs->tailroom
  29137. + *
  29138. + * Revision 1.13.2.2 2006/05/01 14:36:03 mcr
  29139. + * use KLIPS_ERROR for fatal things.
  29140. + *
  29141. + * Revision 1.13.2.1 2006/04/20 16:33:06 mcr
  29142. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  29143. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  29144. + *
  29145. + * Revision 1.13 2005/05/21 03:19:57 mcr
  29146. + * hash ctx is not really that interesting most of the time.
  29147. + *
  29148. + * Revision 1.12 2005/05/11 01:28:49 mcr
  29149. + * removed "poor-man"s OOP in favour of proper C structures.
  29150. + *
  29151. + * Revision 1.11 2005/04/29 05:10:22 mcr
  29152. + * removed from extraenous includes to make unit testing easier.
  29153. + *
  29154. + * Revision 1.10 2005/04/17 04:36:14 mcr
  29155. + * code now deals with ESP and UDP-ESP code.
  29156. + *
  29157. + * Revision 1.9 2005/04/15 19:52:30 mcr
  29158. + * adjustments to use proper skb fields for data.
  29159. + *
  29160. + * Revision 1.8 2004/09/14 00:22:57 mcr
  29161. + * adjustment of MD5* functions.
  29162. + *
  29163. + * Revision 1.7 2004/09/13 02:23:01 mcr
  29164. + * #define inet_protocol if necessary.
  29165. + *
  29166. + * Revision 1.6 2004/09/06 18:35:49 mcr
  29167. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  29168. + * so adjust for that.
  29169. + *
  29170. + * Revision 1.5 2004/08/17 03:27:23 mcr
  29171. + * klips 2.6 edits.
  29172. + *
  29173. + * Revision 1.4 2004/08/04 15:57:07 mcr
  29174. + * moved des .h files to include/des/ *
  29175. + * included 2.6 protocol specific things
  29176. + * started at NAT-T support, but it will require a kernel patch.
  29177. + *
  29178. + * Revision 1.3 2004/07/10 19:11:18 mcr
  29179. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  29180. + *
  29181. + * Revision 1.2 2004/04/06 02:49:25 mcr
  29182. + * pullup of algo code from alg-branch.
  29183. + *
  29184. + *
  29185. + *
  29186. + */
  29187. --- /dev/null Tue Mar 11 13:02:56 2003
  29188. +++ linux/net/ipsec/ipsec_init.c Mon Feb 9 13:51:03 2004
  29189. @@ -0,0 +1,614 @@
  29190. +/*
  29191. + * @(#) Initialization code.
  29192. + * Copyright (C) 1996, 1997 John Ioannidis.
  29193. + * Copyright (C) 1998 - 2002 Richard Guy Briggs <rgb@freeswan.org>
  29194. + * 2001 - 2004 Michael Richardson <mcr@xelerance.com>
  29195. + *
  29196. + * This program is free software; you can redistribute it and/or modify it
  29197. + * under the terms of the GNU General Public License as published by the
  29198. + * Free Software Foundation; either version 2 of the License, or (at your
  29199. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  29200. + *
  29201. + * This program is distributed in the hope that it will be useful, but
  29202. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29203. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  29204. + * for more details.
  29205. + *
  29206. + * /proc system code was split out into ipsec_proc.c after rev. 1.70.
  29207. + *
  29208. + */
  29209. +
  29210. +char ipsec_init_c_version[] = "RCSID $Id: ipsec_init.c,v 1.104.2.4 2006/10/06 21:39:26 paul Exp $";
  29211. +
  29212. +#ifndef AUTOCONF_INCLUDED
  29213. +#include <linux/config.h>
  29214. +#endif
  29215. +#include <linux/version.h>
  29216. +#include <linux/module.h>
  29217. +#include <linux/kernel.h> /* printk() */
  29218. +
  29219. +#include "openswan/ipsec_param.h"
  29220. +
  29221. +#ifdef MALLOC_SLAB
  29222. +# include <linux/slab.h> /* kmalloc() */
  29223. +#else /* MALLOC_SLAB */
  29224. +# include <linux/malloc.h> /* kmalloc() */
  29225. +#endif /* MALLOC_SLAB */
  29226. +#include <linux/errno.h> /* error codes */
  29227. +#include <linux/types.h> /* size_t */
  29228. +#include <linux/interrupt.h> /* mark_bh */
  29229. +
  29230. +#include <linux/netdevice.h> /* struct device, and other headers */
  29231. +#include <linux/etherdevice.h> /* eth_type_trans */
  29232. +#include <linux/ip.h> /* struct iphdr */
  29233. +#include <linux/in.h> /* struct sockaddr_in */
  29234. +#include <linux/skbuff.h>
  29235. +#include <linux/random.h> /* get_random_bytes() */
  29236. +#include <net/protocol.h>
  29237. +
  29238. +#include <openswan.h>
  29239. +
  29240. +#ifdef SPINLOCK
  29241. +# ifdef SPINLOCK_23
  29242. +# include <linux/spinlock.h> /* *lock* */
  29243. +# else /* 23_SPINLOCK */
  29244. +# include <asm/spinlock.h> /* *lock* */
  29245. +# endif /* 23_SPINLOCK */
  29246. +#endif /* SPINLOCK */
  29247. +
  29248. +#include <net/ip.h>
  29249. +
  29250. +#ifdef CONFIG_PROC_FS
  29251. +# include <linux/proc_fs.h>
  29252. +#endif /* CONFIG_PROC_FS */
  29253. +
  29254. +#ifdef NETLINK_SOCK
  29255. +# include <linux/netlink.h>
  29256. +#else
  29257. +# include <net/netlink.h>
  29258. +#endif
  29259. +
  29260. +#include "openswan/radij.h"
  29261. +
  29262. +#include "openswan/ipsec_life.h"
  29263. +#include "openswan/ipsec_stats.h"
  29264. +#include "openswan/ipsec_sa.h"
  29265. +
  29266. +#include "openswan/ipsec_encap.h"
  29267. +#include "openswan/ipsec_radij.h"
  29268. +#include "openswan/ipsec_xform.h"
  29269. +#include "openswan/ipsec_tunnel.h"
  29270. +
  29271. +#include "openswan/ipsec_rcv.h"
  29272. +#include "openswan/ipsec_ah.h"
  29273. +#include "openswan/ipsec_esp.h"
  29274. +
  29275. +#ifdef CONFIG_KLIPS_IPCOMP
  29276. +# include "openswan/ipcomp.h"
  29277. +#endif /* CONFIG_KLIPS_IPCOMP */
  29278. +
  29279. +#include "openswan/ipsec_proto.h"
  29280. +#include "openswan/ipsec_alg.h"
  29281. +
  29282. +#include <pfkeyv2.h>
  29283. +#include <pfkey.h>
  29284. +
  29285. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29286. +#include <net/xfrmudp.h>
  29287. +#endif
  29288. +
  29289. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(HAVE_XFRM4_UDP_REGISTER)
  29290. +#warning "You are trying to build KLIPS2.6 with NAT-T support, but you did not"
  29291. +#error "properly apply the NAT-T patch to your 2.6 kernel source tree."
  29292. +#endif
  29293. +
  29294. +#if !defined(CONFIG_KLIPS_ESP) && !defined(CONFIG_KLIPS_AH)
  29295. +#error "kernel configuration must include ESP or AH"
  29296. +#endif
  29297. +
  29298. +/*
  29299. + * seems to be present in 2.4.10 (Linus), but also in some RH and other
  29300. + * distro kernels of a lower number.
  29301. + */
  29302. +#ifdef MODULE_LICENSE
  29303. +MODULE_LICENSE("GPL");
  29304. +#endif
  29305. +
  29306. +#ifdef CONFIG_KLIPS_DEBUG
  29307. +int debug_eroute = 0;
  29308. +int debug_spi = 0;
  29309. +int debug_netlink = 0;
  29310. +#endif /* CONFIG_KLIPS_DEBUG */
  29311. +
  29312. +struct prng ipsec_prng;
  29313. +
  29314. +
  29315. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29316. +xfrm4_rcv_encap_t klips_old_encap = NULL;
  29317. +#endif
  29318. +
  29319. +extern int ipsec_device_event(struct notifier_block *dnot, unsigned long event, void *ptr);
  29320. +/*
  29321. + * the following structure is required so that we receive
  29322. + * event notifications when network devices are enabled and
  29323. + * disabled (ifconfig up and down).
  29324. + */
  29325. +static struct notifier_block ipsec_dev_notifier={
  29326. + ipsec_device_event,
  29327. + NULL,
  29328. + 0
  29329. +};
  29330. +
  29331. +#ifdef CONFIG_SYSCTL
  29332. +extern int ipsec_sysctl_register(void);
  29333. +extern void ipsec_sysctl_unregister(void);
  29334. +#endif
  29335. +
  29336. +#if defined(NET_26) || defined(IPSKB_XFRM_TUNNEL_SIZE)
  29337. +static inline int
  29338. +openswan_inet_add_protocol(struct inet_protocol *prot, unsigned protocol)
  29339. +{
  29340. + return inet_add_protocol(prot, protocol);
  29341. +}
  29342. +
  29343. +static inline int
  29344. +openswan_inet_del_protocol(struct inet_protocol *prot, unsigned protocol)
  29345. +{
  29346. + return inet_del_protocol(prot, protocol);
  29347. +}
  29348. +
  29349. +#else
  29350. +static inline int
  29351. +openswan_inet_add_protocol(struct inet_protocol *prot, unsigned protocol)
  29352. +{
  29353. + inet_add_protocol(prot);
  29354. + return 0;
  29355. +}
  29356. +
  29357. +static inline int
  29358. +openswan_inet_del_protocol(struct inet_protocol *prot, unsigned protocol)
  29359. +{
  29360. + inet_del_protocol(prot);
  29361. + return 0;
  29362. +}
  29363. +
  29364. +#endif
  29365. +
  29366. +/* void */
  29367. +int
  29368. +ipsec_klips_init(void)
  29369. +{
  29370. + int error = 0;
  29371. + unsigned char seed[256];
  29372. +#ifdef CONFIG_KLIPS_ENC_3DES
  29373. + extern int des_check_key;
  29374. +
  29375. + /* turn off checking of keys */
  29376. + des_check_key=0;
  29377. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29378. +
  29379. + KLIPS_PRINT(1, "klips_info:ipsec_init: "
  29380. + "KLIPS startup, Openswan KLIPS IPsec stack version: %s\n",
  29381. + ipsec_version_code());
  29382. +
  29383. + error |= ipsec_proc_init();
  29384. +
  29385. +#ifdef SPINLOCK
  29386. + ipsec_sadb.sadb_lock = SPIN_LOCK_UNLOCKED;
  29387. +#else /* SPINLOCK */
  29388. + ipsec_sadb.sadb_lock = 0;
  29389. +#endif /* SPINLOCK */
  29390. +
  29391. +#ifndef SPINLOCK
  29392. + tdb_lock.lock = 0;
  29393. + eroute_lock.lock = 0;
  29394. +#endif /* !SPINLOCK */
  29395. +
  29396. + error |= ipsec_sadb_init();
  29397. + error |= ipsec_radijinit();
  29398. +
  29399. + error |= pfkey_init();
  29400. +
  29401. + error |= register_netdevice_notifier(&ipsec_dev_notifier);
  29402. +
  29403. +#ifdef CONFIG_KLIPS_ESP
  29404. + openswan_inet_add_protocol(&esp_protocol, IPPROTO_ESP);
  29405. +#endif /* CONFIG_KLIPS_ESP */
  29406. +
  29407. +#ifdef CONFIG_KLIPS_AH
  29408. + openswan_inet_add_protocol(&ah_protocol, IPPROTO_AH);
  29409. +#endif /* CONFIG_KLIPS_AH */
  29410. +
  29411. +/* we never actually link IPCOMP to the stack */
  29412. +#ifdef IPCOMP_USED_ALONE
  29413. +#ifdef CONFIG_KLIPS_IPCOMP
  29414. + openswan_inet_add_protocol(&comp_protocol, IPPROTO_COMP);
  29415. +#endif /* CONFIG_KLIPS_IPCOMP */
  29416. +#endif
  29417. +
  29418. + error |= ipsec_tunnel_init_devices();
  29419. +
  29420. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29421. + /* register our ESP-UDP handler */
  29422. + if(udp4_register_esp_rcvencap(klips26_rcv_encap
  29423. + , &klips_old_encap)!=0) {
  29424. + printk(KERN_ERR "KLIPS: can not register klips_rcv_encap function\n");
  29425. + }
  29426. +#endif
  29427. +
  29428. +
  29429. +#ifdef CONFIG_SYSCTL
  29430. + error |= ipsec_sysctl_register();
  29431. +#endif
  29432. +
  29433. + ipsec_alg_init();
  29434. +
  29435. + get_random_bytes((void *)seed, sizeof(seed));
  29436. + prng_init(&ipsec_prng, seed, sizeof(seed));
  29437. +
  29438. + return error;
  29439. +}
  29440. +
  29441. +
  29442. +/* void */
  29443. +int
  29444. +ipsec_cleanup(void)
  29445. +{
  29446. + int error = 0;
  29447. +
  29448. +#ifdef CONFIG_SYSCTL
  29449. + ipsec_sysctl_unregister();
  29450. +#endif
  29451. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29452. + if(udp4_unregister_esp_rcvencap(klips_old_encap) < 0) {
  29453. + printk(KERN_ERR "KLIPS: can not unregister klips_rcv_encap function\n");
  29454. + }
  29455. +#endif
  29456. +
  29457. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29458. + "klips_debug:ipsec_cleanup: "
  29459. + "calling ipsec_tunnel_cleanup_devices.\n");
  29460. + error |= ipsec_tunnel_cleanup_devices();
  29461. +
  29462. + KLIPS_PRINT(debug_netlink, "called ipsec_tunnel_cleanup_devices");
  29463. +
  29464. +/* we never actually link IPCOMP to the stack */
  29465. +#ifdef IPCOMP_USED_ALONE
  29466. +#ifdef CONFIG_KLIPS_IPCOMP
  29467. + if (openswan_inet_del_protocol(&comp_protocol, IPPROTO_COMP) < 0)
  29468. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29469. + "comp close: can't remove protocol\n");
  29470. +#endif /* CONFIG_KLIPS_IPCOMP */
  29471. +#endif /* IPCOMP_USED_ALONE */
  29472. +
  29473. +#ifdef CONFIG_KLIPS_AH
  29474. + if (openswan_inet_del_protocol(&ah_protocol, IPPROTO_AH) < 0)
  29475. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29476. + "ah close: can't remove protocol\n");
  29477. +#endif /* CONFIG_KLIPS_AH */
  29478. +
  29479. +#ifdef CONFIG_KLIPS_ESP
  29480. + if (openswan_inet_del_protocol(&esp_protocol, IPPROTO_ESP) < 0)
  29481. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29482. + "esp close: can't remove protocol\n");
  29483. +#endif /* CONFIG_KLIPS_ESP */
  29484. +
  29485. + error |= unregister_netdevice_notifier(&ipsec_dev_notifier);
  29486. +
  29487. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29488. + "klips_debug:ipsec_cleanup: "
  29489. + "calling ipsec_sadb_cleanup.\n");
  29490. + error |= ipsec_sadb_cleanup(0);
  29491. + error |= ipsec_sadb_free();
  29492. +
  29493. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29494. + "klips_debug:ipsec_cleanup: "
  29495. + "calling ipsec_radijcleanup.\n");
  29496. + error |= ipsec_radijcleanup();
  29497. +
  29498. + KLIPS_PRINT(debug_pfkey, /* debug_tunnel & DB_TN_INIT, */
  29499. + "klips_debug:ipsec_cleanup: "
  29500. + "calling pfkey_cleanup.\n");
  29501. + error |= pfkey_cleanup();
  29502. +
  29503. + ipsec_proc_cleanup();
  29504. +
  29505. + prng_final(&ipsec_prng);
  29506. +
  29507. + return error;
  29508. +}
  29509. +
  29510. +#ifdef MODULE
  29511. +int
  29512. +init_module(void)
  29513. +{
  29514. + int error = 0;
  29515. +
  29516. + error |= ipsec_klips_init();
  29517. +
  29518. + return error;
  29519. +}
  29520. +
  29521. +void
  29522. +cleanup_module(void)
  29523. +{
  29524. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29525. + "klips_debug:cleanup_module: "
  29526. + "calling ipsec_cleanup.\n");
  29527. +
  29528. + ipsec_cleanup();
  29529. +
  29530. + KLIPS_PRINT(1, "klips_info:cleanup_module: "
  29531. + "ipsec module unloaded.\n");
  29532. +}
  29533. +#endif /* MODULE */
  29534. +
  29535. +/*
  29536. + * $Log: ipsec_init.c,v $
  29537. + * Revision 1.104.2.4 2006/10/06 21:39:26 paul
  29538. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  29539. + * set. This is defined through autoconf.h which is included through the
  29540. + * linux kernel build macros.
  29541. + *
  29542. + * Revision 1.104.2.3 2006/07/31 15:25:20 paul
  29543. + * Check for NETKEY backport in Debian using IPSKB_XFRM_TUNNEL_SIZE to
  29544. + * determine wether inet_add_protocol needs the protocol argument.
  29545. + *
  29546. + * Revision 1.104.2.2 2006/04/20 16:33:06 mcr
  29547. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  29548. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  29549. + *
  29550. + * Revision 1.104.2.1 2005/08/12 01:18:20 ken
  29551. + * Warn people who don't have NAT-T patch applied, but try and compile NAT-T code
  29552. + *
  29553. + * Revision 1.105 2005/08/12 00:56:33 mcr
  29554. + * add warning for people who didn't apply nat-t patch.
  29555. + *
  29556. + * Revision 1.104 2005/07/08 15:51:41 mcr
  29557. + * removed duplicate NAT-T code.
  29558. + * if CONFIG_IPSEC_NAT_TRAVERSAL isn't defined, then there is no issue.
  29559. + *
  29560. + * Revision 1.103 2005/07/08 03:02:05 paul
  29561. + * Fixed garbled define that accidentally got commited to the real tree.
  29562. + *
  29563. + * Revision 1.102 2005/07/08 02:56:37 paul
  29564. + * gcc4 fixes that were not commited because vault was down
  29565. + *
  29566. + * Revision 1.101 2005/04/29 05:10:22 mcr
  29567. + * removed from extraenous includes to make unit testing easier.
  29568. + *
  29569. + * Revision 1.100 2005/04/10 22:56:09 mcr
  29570. + * change to udp.c registration API.
  29571. + *
  29572. + * Revision 1.99 2005/04/08 18:26:13 mcr
  29573. + * register with udp.c, the klips26 encap receive function
  29574. + *
  29575. + * Revision 1.98 2004/09/13 02:23:18 mcr
  29576. + * #define inet_protocol if necessary.
  29577. + *
  29578. + * Revision 1.97 2004/09/06 18:35:49 mcr
  29579. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  29580. + * so adjust for that.
  29581. + *
  29582. + * Revision 1.96 2004/08/17 03:27:23 mcr
  29583. + * klips 2.6 edits.
  29584. + *
  29585. + * Revision 1.95 2004/08/03 18:19:08 mcr
  29586. + * in 2.6, use "net_device" instead of #define device->net_device.
  29587. + * this probably breaks 2.0 compiles.
  29588. + *
  29589. + * Revision 1.94 2004/07/10 19:11:18 mcr
  29590. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  29591. + *
  29592. + * Revision 1.93 2004/04/06 02:49:26 mcr
  29593. + * pullup of algo code from alg-branch.
  29594. + *
  29595. + * Revision 1.92 2004/03/30 15:30:39 ken
  29596. + * Proper Capitalization
  29597. + *
  29598. + * Revision 1.91 2004/03/22 01:51:51 ken
  29599. + * We are open
  29600. + *
  29601. + * Revision 1.90.4.2 2004/04/05 04:30:46 mcr
  29602. + * patches for alg-branch to compile/work with 2.x openswan
  29603. + *
  29604. + * Revision 1.90.4.1 2003/12/22 15:25:52 jjo
  29605. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  29606. + *
  29607. + * Revision 1.90 2003/10/31 02:27:55 mcr
  29608. + * pulled up port-selector patches and sa_id elimination.
  29609. + *
  29610. + * Revision 1.89.4.1 2003/10/29 01:30:41 mcr
  29611. + * elimited "struct sa_id".
  29612. + *
  29613. + * Revision 1.89 2003/07/31 22:47:16 mcr
  29614. + * preliminary (untested by FS-team) 2.5 patches.
  29615. + *
  29616. + * Revision 1.88 2003/06/22 20:05:36 mcr
  29617. + * clarified why IPCOMP was not being registered, and put a new
  29618. + * #ifdef in rather than #if 0.
  29619. + *
  29620. + * Revision 1.87 2002/09/20 15:40:51 rgb
  29621. + * Added a lock to the global ipsec_sadb struct for future use.
  29622. + * Split ipsec_sadb_cleanup from new funciton ipsec_sadb_free to avoid problem
  29623. + * of freeing newly created structures when clearing the reftable upon startup
  29624. + * to start from a known state.
  29625. + *
  29626. + * Revision 1.86 2002/08/15 18:39:15 rgb
  29627. + * Move ipsec_prng outside debug code.
  29628. + *
  29629. + * Revision 1.85 2002/05/14 02:35:29 rgb
  29630. + * Change reference to tdb to ipsa.
  29631. + *
  29632. + * Revision 1.84 2002/04/24 07:55:32 mcr
  29633. + * #include patches and Makefiles for post-reorg compilation.
  29634. + *
  29635. + * Revision 1.83 2002/04/24 07:36:28 mcr
  29636. + * Moved from ./klips/net/ipsec/ipsec_init.c,v
  29637. + *
  29638. + * Revision 1.82 2002/04/20 00:12:25 rgb
  29639. + * Added esp IV CBC attack fix, disabled.
  29640. + *
  29641. + * Revision 1.81 2002/04/09 16:13:32 mcr
  29642. + * switch license to straight GPL.
  29643. + *
  29644. + * Revision 1.80 2002/03/24 07:34:08 rgb
  29645. + * Sanity check for at least one of AH or ESP configured.
  29646. + *
  29647. + * Revision 1.79 2002/02/05 22:55:15 mcr
  29648. + * added MODULE_LICENSE declaration.
  29649. + * This macro does not appear in all kernel versions (see comment).
  29650. + *
  29651. + * Revision 1.78 2002/01/29 17:17:55 mcr
  29652. + * moved include of ipsec_param.h to after include of linux/kernel.h
  29653. + * otherwise, it seems that some option that is set in ipsec_param.h
  29654. + * screws up something subtle in the include path to kernel.h, and
  29655. + * it complains on the snprintf() prototype.
  29656. + *
  29657. + * Revision 1.77 2002/01/29 04:00:51 mcr
  29658. + * more excise of kversions.h header.
  29659. + *
  29660. + * Revision 1.76 2002/01/29 02:13:17 mcr
  29661. + * introduction of ipsec_kversion.h means that include of
  29662. + * ipsec_param.h must preceed any decisions about what files to
  29663. + * include to deal with differences in kernel source.
  29664. + *
  29665. + * Revision 1.75 2001/11/26 09:23:48 rgb
  29666. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  29667. + *
  29668. + * Revision 1.74 2001/11/22 05:44:11 henry
  29669. + * new version stuff
  29670. + *
  29671. + * Revision 1.71.2.2 2001/10/22 20:51:00 mcr
  29672. + * explicitely set des_check_key.
  29673. + *
  29674. + * Revision 1.71.2.1 2001/09/25 02:19:39 mcr
  29675. + * /proc manipulation code moved to new ipsec_proc.c
  29676. + *
  29677. + * Revision 1.73 2001/11/06 19:47:17 rgb
  29678. + * Changed lifetime_packets to uint32 from uint64.
  29679. + *
  29680. + * Revision 1.72 2001/10/18 04:45:19 rgb
  29681. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  29682. + * lib/freeswan.h version macros moved to lib/kversions.h.
  29683. + * Other compiler directive cleanups.
  29684. + *
  29685. + * Revision 1.71 2001/09/20 15:32:45 rgb
  29686. + * Minor pfkey lifetime fixes.
  29687. + *
  29688. + * Revision 1.70 2001/07/06 19:51:21 rgb
  29689. + * Added inbound policy checking code for IPIP SAs.
  29690. + *
  29691. + * Revision 1.69 2001/06/14 19:33:26 rgb
  29692. + * Silence startup message for console, but allow it to be logged.
  29693. + * Update copyright date.
  29694. + *
  29695. + * Revision 1.68 2001/05/29 05:14:36 rgb
  29696. + * Added PMTU to /proc/net/ipsec_tncfg output. See 'man 5 ipsec_tncfg'.
  29697. + *
  29698. + * Revision 1.67 2001/05/04 16:34:52 rgb
  29699. + * Rremove erroneous checking of return codes for proc_net_* in 2.4.
  29700. + *
  29701. + * Revision 1.66 2001/05/03 19:40:34 rgb
  29702. + * Check error return codes in startup and shutdown.
  29703. + *
  29704. + * Revision 1.65 2001/02/28 05:03:27 rgb
  29705. + * Clean up and rationalise startup messages.
  29706. + *
  29707. + * Revision 1.64 2001/02/27 22:24:53 rgb
  29708. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  29709. + * Check for satoa() return codes.
  29710. + *
  29711. + * Revision 1.63 2000/11/29 20:14:06 rgb
  29712. + * Add src= to the output of /proc/net/ipsec_spi and delete dst from IPIP.
  29713. + *
  29714. + * Revision 1.62 2000/11/06 04:31:24 rgb
  29715. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  29716. + * Fixed longlong for pre-2.4 kernels (Svenning).
  29717. + * Add Svenning's adaptive content compression.
  29718. + * Disabled registration of ipcomp handler.
  29719. + *
  29720. + * Revision 1.61 2000/10/11 13:37:54 rgb
  29721. + * #ifdef out debug print that causes proc/net/ipsec_version to oops.
  29722. + *
  29723. + * Revision 1.60 2000/09/20 03:59:01 rgb
  29724. + * Change static info functions to DEBUG_NO_STATIC to reveal function names
  29725. + * in oopsen.
  29726. + *
  29727. + * Revision 1.59 2000/09/16 01:06:26 rgb
  29728. + * Added cast of var to silence compiler warning about long fed to int
  29729. + * format.
  29730. + *
  29731. + * Revision 1.58 2000/09/15 11:37:01 rgb
  29732. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  29733. + * IPCOMP zlib deflate code.
  29734. + *
  29735. + * Revision 1.57 2000/09/12 03:21:50 rgb
  29736. + * Moved radij_c_version printing to ipsec_version_get_info().
  29737. + * Reformatted ipsec_version_get_info().
  29738. + * Added sysctl_{,un}register() calls.
  29739. + *
  29740. + * Revision 1.56 2000/09/08 19:16:50 rgb
  29741. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  29742. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  29743. + *
  29744. + * Revision 1.55 2000/08/30 05:19:03 rgb
  29745. + * Cleaned up no longer used spi_next, netlink register/unregister, other
  29746. + * minor cleanup.
  29747. + * Removed cruft replaced by TDB_XFORM_NAME.
  29748. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  29749. + * Moved debug version strings to printk when /proc/net/ipsec_version is
  29750. + * called.
  29751. + *
  29752. + * Revision 1.54 2000/08/20 18:31:05 rgb
  29753. + * Changed cosmetic alignment in spi_info.
  29754. + * Changed addtime and usetime to use actual value which is relative
  29755. + * anyways, as intended. (Momchil)
  29756. + *
  29757. + * Revision 1.53 2000/08/18 17:37:03 rgb
  29758. + * Added an (int) cast to shut up the compiler...
  29759. + *
  29760. + * Revision 1.52 2000/08/01 14:51:50 rgb
  29761. + * Removed _all_ remaining traces of DES.
  29762. + *
  29763. + * Revision 1.51 2000/07/25 20:41:22 rgb
  29764. + * Removed duplicate parameter in spi_getinfo.
  29765. + *
  29766. + * Revision 1.50 2000/07/17 03:21:45 rgb
  29767. + * Removed /proc/net/ipsec_spinew.
  29768. + *
  29769. + * Revision 1.49 2000/06/28 05:46:51 rgb
  29770. + * Renamed ivlen to iv_bits for consistency.
  29771. + * Changed output of add and use times to be relative to now.
  29772. + *
  29773. + * Revision 1.48 2000/05/11 18:26:10 rgb
  29774. + * Commented out calls to netlink_attach/detach to avoid activating netlink
  29775. + * in the kenrel config.
  29776. + *
  29777. + * Revision 1.47 2000/05/10 22:35:26 rgb
  29778. + * Comment out most of the startup version information.
  29779. + *
  29780. + * Revision 1.46 2000/03/22 16:15:36 rgb
  29781. + * Fixed renaming of dev_get (MB).
  29782. + *
  29783. + * Revision 1.45 2000/03/16 06:40:48 rgb
  29784. + * Hardcode PF_KEYv2 support.
  29785. + *
  29786. + * Revision 1.44 2000/01/22 23:19:20 rgb
  29787. + * Simplified code to use existing macro TDB_XFORM_NAME().
  29788. + *
  29789. + * Revision 1.43 2000/01/21 06:14:04 rgb
  29790. + * Print individual stats only if non-zero.
  29791. + * Removed 'bits' from each keylength for brevity.
  29792. + * Shortened lifetimes legend for brevity.
  29793. + * Changed wording from 'last_used' to the clearer 'idle'.
  29794. + *
  29795. + * Revision 1.42 1999/12/31 14:57:19 rgb
  29796. + * MB fix for new dummy-less proc_get_info in 2.3.35.
  29797. + *
  29798. + *
  29799. + * Local variables:
  29800. + * c-file-style: "linux"
  29801. + * End:
  29802. + *
  29803. + */
  29804. --- /dev/null Tue Mar 11 13:02:56 2003
  29805. +++ linux/net/ipsec/ipsec_ipcomp.c Mon Feb 9 13:51:03 2004
  29806. @@ -0,0 +1,256 @@
  29807. +/*
  29808. + * processing code for IPCOMP
  29809. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  29810. + *
  29811. + * This program is free software; you can redistribute it and/or modify it
  29812. + * under the terms of the GNU General Public License as published by the
  29813. + * Free Software Foundation; either version 2 of the License, or (at your
  29814. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  29815. + *
  29816. + * This program is distributed in the hope that it will be useful, but
  29817. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29818. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  29819. + * for more details.
  29820. + */
  29821. +
  29822. +char ipsec_ipcomp_c_version[] = "RCSID $Id: ipsec_ipcomp.c,v 1.5.2.2 2006/10/06 21:39:26 paul Exp $";
  29823. +#ifndef AUTOCONF_INCLUDED
  29824. +#include <linux/config.h>
  29825. +#endif
  29826. +#include <linux/version.h>
  29827. +
  29828. +#define __NO_VERSION__
  29829. +#include <linux/module.h>
  29830. +#include <linux/kernel.h> /* printk() */
  29831. +
  29832. +#include "openswan/ipsec_param.h"
  29833. +
  29834. +#ifdef MALLOC_SLAB
  29835. +# include <linux/slab.h> /* kmalloc() */
  29836. +#else /* MALLOC_SLAB */
  29837. +# include <linux/malloc.h> /* kmalloc() */
  29838. +#endif /* MALLOC_SLAB */
  29839. +#include <linux/errno.h> /* error codes */
  29840. +#include <linux/types.h> /* size_t */
  29841. +#include <linux/interrupt.h> /* mark_bh */
  29842. +
  29843. +#include <linux/netdevice.h> /* struct device, and other headers */
  29844. +#include <linux/etherdevice.h> /* eth_type_trans */
  29845. +#include <linux/ip.h> /* struct iphdr */
  29846. +#include <linux/skbuff.h>
  29847. +#include <openswan.h>
  29848. +#ifdef SPINLOCK
  29849. +# ifdef SPINLOCK_23
  29850. +# include <linux/spinlock.h> /* *lock* */
  29851. +# else /* SPINLOCK_23 */
  29852. +# include <asm/spinlock.h> /* *lock* */
  29853. +# endif /* SPINLOCK_23 */
  29854. +#endif /* SPINLOCK */
  29855. +
  29856. +#include <net/ip.h>
  29857. +
  29858. +#include "openswan/radij.h"
  29859. +#include "openswan/ipsec_encap.h"
  29860. +#include "openswan/ipsec_sa.h"
  29861. +
  29862. +#include "openswan/ipsec_radij.h"
  29863. +#include "openswan/ipsec_xform.h"
  29864. +#include "openswan/ipsec_tunnel.h"
  29865. +#include "openswan/ipsec_rcv.h"
  29866. +#include "openswan/ipsec_xmit.h"
  29867. +
  29868. +#include "openswan/ipsec_auth.h"
  29869. +
  29870. +#ifdef CONFIG_KLIPS_IPCOMP
  29871. +#include "openswan/ipsec_ipcomp.h"
  29872. +#endif /* CONFIG_KLIPS_IPCOMP */
  29873. +
  29874. +#include "openswan/ipsec_proto.h"
  29875. +
  29876. +#ifdef CONFIG_KLIPS_DEBUG
  29877. +int debug_ipcomp = 0;
  29878. +#endif /* CONFIG_KLIPS_DEBUG */
  29879. +
  29880. +
  29881. +#ifdef CONFIG_KLIPS_IPCOMP
  29882. +enum ipsec_rcv_value
  29883. +ipsec_rcv_ipcomp_checks(struct ipsec_rcv_state *irs,
  29884. + struct sk_buff *skb)
  29885. +{
  29886. + int ipcompminlen;
  29887. +
  29888. + ipcompminlen = sizeof(struct iphdr);
  29889. +
  29890. + if(skb->len < (ipcompminlen + sizeof(struct ipcomphdr))) {
  29891. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  29892. + "klips_debug:ipsec_rcv: "
  29893. + "runt comp packet of skb->len=%d received from %s, dropped.\n",
  29894. + skb->len,
  29895. + irs->ipsaddr_txt);
  29896. + if(irs->stats) {
  29897. + irs->stats->rx_errors++;
  29898. + }
  29899. + return IPSEC_RCV_BADLEN;
  29900. + }
  29901. +
  29902. + irs->protostuff.ipcompstuff.compp = (struct ipcomphdr *)skb->h.raw;
  29903. + irs->said.spi = htonl((__u32)ntohs(irs->protostuff.ipcompstuff.compp->ipcomp_cpi));
  29904. + return IPSEC_RCV_OK;
  29905. +}
  29906. +
  29907. +enum ipsec_rcv_value
  29908. +ipsec_rcv_ipcomp_decomp(struct ipsec_rcv_state *irs)
  29909. +{
  29910. + unsigned int flags = 0;
  29911. + struct ipsec_sa *ipsp = irs->ipsp;
  29912. + struct sk_buff *skb;
  29913. +
  29914. + skb=irs->skb;
  29915. +
  29916. + ipsec_xmit_dmp("ipcomp", skb->h.raw, skb->len);
  29917. +
  29918. + if(ipsp == NULL) {
  29919. + return IPSEC_RCV_SAIDNOTFOUND;
  29920. + }
  29921. +
  29922. + if(sysctl_ipsec_inbound_policy_check &&
  29923. + ((((ntohl(ipsp->ips_said.spi) & 0x0000ffff) != ntohl(irs->said.spi)) &&
  29924. + (ipsp->ips_encalg != ntohl(irs->said.spi)) /* this is a workaround for peer non-compliance with rfc2393 */
  29925. + ))) {
  29926. + char sa2[SATOT_BUF];
  29927. + size_t sa_len2 = 0;
  29928. +
  29929. + sa_len2 = satot(&ipsp->ips_said, 0, sa2, sizeof(sa2));
  29930. +
  29931. + KLIPS_PRINT(debug_rcv,
  29932. + "klips_debug:ipsec_rcv: "
  29933. + "Incoming packet with SA(IPCA):%s does not match policy SA(IPCA):%s cpi=%04x cpi->spi=%08x spi=%08x, spi->cpi=%04x for SA grouping, dropped.\n",
  29934. + irs->sa_len ? irs->sa : " (error)",
  29935. + ipsp != NULL ? (sa_len2 ? sa2 : " (error)") : "NULL",
  29936. + ntohs(irs->protostuff.ipcompstuff.compp->ipcomp_cpi),
  29937. + (__u32)ntohl(irs->said.spi),
  29938. + ipsp != NULL ? (__u32)ntohl((ipsp->ips_said.spi)) : 0,
  29939. + ipsp != NULL ? (__u16)(ntohl(ipsp->ips_said.spi) & 0x0000ffff) : 0);
  29940. + if(irs->stats) {
  29941. + irs->stats->rx_dropped++;
  29942. + }
  29943. + return IPSEC_RCV_SAIDNOTFOUND;
  29944. + }
  29945. +
  29946. + ipsp->ips_comp_ratio_cbytes += ntohs(irs->ipp->tot_len);
  29947. + irs->next_header = irs->protostuff.ipcompstuff.compp->ipcomp_nh;
  29948. +
  29949. + skb = skb_decompress(skb, ipsp, &flags);
  29950. + if (!skb || flags) {
  29951. + spin_unlock(&tdb_lock);
  29952. + KLIPS_PRINT(debug_rcv,
  29953. + "klips_debug:ipsec_rcv: "
  29954. + "skb_decompress() returned error flags=%x, dropped.\n",
  29955. + flags);
  29956. + if (irs->stats) {
  29957. + if (flags)
  29958. + irs->stats->rx_errors++;
  29959. + else
  29960. + irs->stats->rx_dropped++;
  29961. + }
  29962. + return IPSEC_RCV_IPCOMPFAILED;
  29963. + }
  29964. +
  29965. + /* make sure we update the pointer */
  29966. + irs->skb = skb;
  29967. +
  29968. +#ifdef NET_21
  29969. + irs->ipp = skb->nh.iph;
  29970. +#else /* NET_21 */
  29971. + irs->ipp = skb->ip_hdr;
  29972. +#endif /* NET_21 */
  29973. +
  29974. + ipsp->ips_comp_ratio_dbytes += ntohs(irs->ipp->tot_len);
  29975. +
  29976. + KLIPS_PRINT(debug_rcv,
  29977. + "klips_debug:ipsec_rcv: "
  29978. + "packet decompressed SA(IPCA):%s cpi->spi=%08x spi=%08x, spi->cpi=%04x, nh=%d.\n",
  29979. + irs->sa_len ? irs->sa : " (error)",
  29980. + (__u32)ntohl(irs->said.spi),
  29981. + ipsp != NULL ? (__u32)ntohl((ipsp->ips_said.spi)) : 0,
  29982. + ipsp != NULL ? (__u16)(ntohl(ipsp->ips_said.spi) & 0x0000ffff) : 0,
  29983. + irs->next_header);
  29984. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, irs->ipp);
  29985. +
  29986. + return IPSEC_RCV_OK;
  29987. +}
  29988. +
  29989. +enum ipsec_xmit_value
  29990. +ipsec_xmit_ipcomp_setup(struct ipsec_xmit_state *ixs)
  29991. +{
  29992. + unsigned int flags = 0;
  29993. +#ifdef CONFIG_KLIPS_DEBUG
  29994. + unsigned int old_tot_len = ntohs(ixs->iph->tot_len);
  29995. +#endif /* CONFIG_KLIPS_DEBUG */
  29996. +
  29997. + ixs->ipsp->ips_comp_ratio_dbytes += ntohs(ixs->iph->tot_len);
  29998. +
  29999. + ixs->skb = skb_compress(ixs->skb, ixs->ipsp, &flags);
  30000. +
  30001. +#ifdef NET_21
  30002. + ixs->iph = ixs->skb->nh.iph;
  30003. +#else /* NET_21 */
  30004. + ixs->iph = ixs->skb->ip_hdr;
  30005. +#endif /* NET_21 */
  30006. +
  30007. + ixs->ipsp->ips_comp_ratio_cbytes += ntohs(ixs->iph->tot_len);
  30008. +
  30009. +#ifdef CONFIG_KLIPS_DEBUG
  30010. + if (debug_tunnel & DB_TN_CROUT)
  30011. + {
  30012. + if (old_tot_len > ntohs(ixs->iph->tot_len))
  30013. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  30014. + "klips_debug:ipsec_xmit_encap_once: "
  30015. + "packet shrunk from %d to %d bytes after compression, cpi=%04x (should be from spi=%08x, spi&0xffff=%04x.\n",
  30016. + old_tot_len, ntohs(ixs->iph->tot_len),
  30017. + ntohs(((struct ipcomphdr*)(((char*)ixs->iph) + ((ixs->iph->ihl) << 2)))->ipcomp_cpi),
  30018. + ntohl(ixs->ipsp->ips_said.spi),
  30019. + (__u16)(ntohl(ixs->ipsp->ips_said.spi) & 0x0000ffff));
  30020. + else
  30021. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  30022. + "klips_debug:ipsec_xmit_encap_once: "
  30023. + "packet did not compress (flags = %d).\n",
  30024. + flags);
  30025. + }
  30026. +#endif /* CONFIG_KLIPS_DEBUG */
  30027. +
  30028. + return IPSEC_XMIT_OK;
  30029. +}
  30030. +
  30031. +struct xform_functions ipcomp_xform_funcs[]={
  30032. + {rcv_checks: ipsec_rcv_ipcomp_checks,
  30033. + rcv_decrypt: ipsec_rcv_ipcomp_decomp,
  30034. + xmit_setup: ipsec_xmit_ipcomp_setup,
  30035. + xmit_headroom: 0,
  30036. + xmit_needtailroom: 0,
  30037. + },
  30038. +};
  30039. +
  30040. +#if 0
  30041. +/* We probably don't want to install a pure IPCOMP protocol handler, but
  30042. + only want to handle IPCOMP if it is encapsulated inside an ESP payload
  30043. + (which is already handled) */
  30044. +#ifdef CONFIG_KLIPS_IPCOMP
  30045. +struct inet_protocol comp_protocol =
  30046. +{
  30047. + ipsec_rcv, /* COMP handler */
  30048. + NULL, /* COMP error control */
  30049. +#ifdef NETDEV_25
  30050. + 1, /* no policy */
  30051. +#else
  30052. + 0, /* next */
  30053. + IPPROTO_COMP, /* protocol ID */
  30054. + 0, /* copy */
  30055. + NULL, /* data */
  30056. + "COMP" /* name */
  30057. +#endif
  30058. +};
  30059. +#endif /* CONFIG_KLIPS_IPCOMP */
  30060. +#endif
  30061. +
  30062. +#endif /* CONFIG_KLIPS_IPCOMP */
  30063. --- /dev/null Tue Mar 11 13:02:56 2003
  30064. +++ linux/net/ipsec/ipsec_ipip.c Mon Feb 9 13:51:03 2004
  30065. @@ -0,0 +1,122 @@
  30066. +/*
  30067. + * processing code for IPIP
  30068. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  30069. + *
  30070. + * This program is free software; you can redistribute it and/or modify it
  30071. + * under the terms of the GNU General Public License as published by the
  30072. + * Free Software Foundation; either version 2 of the License, or (at your
  30073. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30074. + *
  30075. + * This program is distributed in the hope that it will be useful, but
  30076. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30077. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30078. + * for more details.
  30079. + */
  30080. +
  30081. +char ipsec_ipip_c_version[] = "RCSID $Id: ipsec_ipip.c,v 1.3.2.3 2006/10/06 21:39:26 paul Exp $";
  30082. +#ifndef AUTOCONF_INCLUDED
  30083. +#include <linux/config.h>
  30084. +#endif
  30085. +#include <linux/version.h>
  30086. +
  30087. +#define __NO_VERSION__
  30088. +#include <linux/module.h>
  30089. +#include <linux/kernel.h> /* printk() */
  30090. +
  30091. +#include "openswan/ipsec_param.h"
  30092. +
  30093. +#ifdef MALLOC_SLAB
  30094. +# include <linux/slab.h> /* kmalloc() */
  30095. +#else /* MALLOC_SLAB */
  30096. +# include <linux/malloc.h> /* kmalloc() */
  30097. +#endif /* MALLOC_SLAB */
  30098. +#include <linux/errno.h> /* error codes */
  30099. +#include <linux/types.h> /* size_t */
  30100. +#include <linux/interrupt.h> /* mark_bh */
  30101. +
  30102. +#include <linux/netdevice.h> /* struct device, and other headers */
  30103. +#include <linux/etherdevice.h> /* eth_type_trans */
  30104. +#include <linux/ip.h> /* struct iphdr */
  30105. +#include <linux/skbuff.h>
  30106. +#include <openswan.h>
  30107. +#ifdef SPINLOCK
  30108. +# ifdef SPINLOCK_23
  30109. +# include <linux/spinlock.h> /* *lock* */
  30110. +# else /* SPINLOCK_23 */
  30111. +# include <asm/spinlock.h> /* *lock* */
  30112. +# endif /* SPINLOCK_23 */
  30113. +#endif /* SPINLOCK */
  30114. +
  30115. +#include <net/ip.h>
  30116. +
  30117. +#include "openswan/radij.h"
  30118. +#include "openswan/ipsec_encap.h"
  30119. +#include "openswan/ipsec_sa.h"
  30120. +
  30121. +#include "openswan/ipsec_radij.h"
  30122. +#include "openswan/ipsec_xform.h"
  30123. +#include "openswan/ipsec_tunnel.h"
  30124. +#include "openswan/ipsec_rcv.h"
  30125. +#include "openswan/ipsec_xmit.h"
  30126. +
  30127. +#include "openswan/ipsec_auth.h"
  30128. +#include "openswan/ipsec_ipip.h"
  30129. +#include "openswan/ipsec_param.h"
  30130. +
  30131. +#include "openswan/ipsec_proto.h"
  30132. +
  30133. +enum ipsec_xmit_value
  30134. +ipsec_xmit_ipip_setup(struct ipsec_xmit_state *ixs)
  30135. +{
  30136. + ixs->iph->version = 4;
  30137. +
  30138. + switch(sysctl_ipsec_tos) {
  30139. + case 0:
  30140. +#ifdef NET_21
  30141. + ixs->iph->tos = ixs->skb->nh.iph->tos;
  30142. +#else /* NET_21 */
  30143. + ixs->iph->tos = ixs->skb->ip_hdr->tos;
  30144. +#endif /* NET_21 */
  30145. + break;
  30146. + case 1:
  30147. + ixs->iph->tos = 0;
  30148. + break;
  30149. + default:
  30150. + break;
  30151. + }
  30152. + ixs->iph->ttl = SYSCTL_IPSEC_DEFAULT_TTL;
  30153. + ixs->iph->frag_off = 0;
  30154. + ixs->iph->saddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_s))->sin_addr.s_addr;
  30155. + ixs->iph->daddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_d))->sin_addr.s_addr;
  30156. + ixs->iph->protocol = IPPROTO_IPIP;
  30157. + ixs->iph->ihl = sizeof(struct iphdr) >> 2;
  30158. +
  30159. + KLIPS_IP_SELECT_IDENT(ixs->iph, ixs->skb);
  30160. +
  30161. + ixs->newdst = (__u32)ixs->iph->daddr;
  30162. + ixs->newsrc = (__u32)ixs->iph->saddr;
  30163. +
  30164. +#ifdef NET_21
  30165. + ixs->skb->h.ipiph = ixs->skb->nh.iph;
  30166. +#endif /* NET_21 */
  30167. + return IPSEC_XMIT_OK;
  30168. +}
  30169. +
  30170. +struct xform_functions ipip_xform_funcs[]={
  30171. + { rcv_checks: NULL,
  30172. + rcv_setup_auth: NULL,
  30173. + rcv_calc_auth: NULL,
  30174. + rcv_decrypt: NULL,
  30175. +
  30176. + xmit_setup: ipsec_xmit_ipip_setup,
  30177. + xmit_headroom: sizeof(struct iphdr),
  30178. + xmit_needtailroom: 0,
  30179. + },
  30180. +};
  30181. +
  30182. +
  30183. +
  30184. +
  30185. +
  30186. +
  30187. +
  30188. --- /dev/null Tue Mar 11 13:02:56 2003
  30189. +++ linux/net/ipsec/ipsec_kern24.c Mon Feb 9 13:51:03 2004
  30190. @@ -0,0 +1,74 @@
  30191. +/*
  30192. + * Copyright 2005 (C) Michael Richardson <mcr@xelerance.com>
  30193. + *
  30194. + * This is a file of functions which are present in 2.6 kernels,
  30195. + * but are not available by default in the 2.4 series.
  30196. + *
  30197. + * As such this code is usually from the Linux kernel, and is covered by
  30198. + * GPL.
  30199. + *
  30200. + * This program is free software; you can redistribute it and/or modify it
  30201. + * under the terms of the GNU General Public License as published by the
  30202. + * Free Software Foundation; either version 2 of the License, or (at your
  30203. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30204. + *
  30205. + * This program is distributed in the hope that it will be useful, but
  30206. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30207. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30208. + * for more details.
  30209. + *
  30210. + * $Id: ipsec_kern24.c,v 1.2 2005/05/20 03:19:18 mcr Exp $
  30211. + *
  30212. + */
  30213. +
  30214. +#include <linux/kernel.h>
  30215. +#include <linux/mm.h>
  30216. +#include <linux/spinlock.h>
  30217. +
  30218. +/*
  30219. + * printk rate limiting, lifted from the networking subsystem.
  30220. + *
  30221. + * This enforces a rate limit: not more than one kernel message
  30222. + * every printk_ratelimit_jiffies to make a denial-of-service
  30223. + * attack impossible.
  30224. + */
  30225. +static spinlock_t ratelimit_lock = SPIN_LOCK_UNLOCKED;
  30226. +
  30227. +int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  30228. +{
  30229. + static unsigned long toks = 10*5*HZ;
  30230. + static unsigned long last_msg;
  30231. + static int missed;
  30232. + unsigned long flags;
  30233. + unsigned long now = jiffies;
  30234. +
  30235. + spin_lock_irqsave(&ratelimit_lock, flags);
  30236. + toks += now - last_msg;
  30237. + last_msg = now;
  30238. + if (toks > (ratelimit_burst * ratelimit_jiffies))
  30239. + toks = ratelimit_burst * ratelimit_jiffies;
  30240. + if (toks >= ratelimit_jiffies) {
  30241. + int lost = missed;
  30242. + missed = 0;
  30243. + toks -= ratelimit_jiffies;
  30244. + spin_unlock_irqrestore(&ratelimit_lock, flags);
  30245. + if (lost)
  30246. + printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  30247. + return 1;
  30248. + }
  30249. + missed++;
  30250. + spin_unlock_irqrestore(&ratelimit_lock, flags);
  30251. + return 0;
  30252. +}
  30253. +
  30254. +/* minimum time in jiffies between messages */
  30255. +int printk_ratelimit_jiffies = 5*HZ;
  30256. +
  30257. +/* number of messages we send before ratelimiting */
  30258. +int printk_ratelimit_burst = 10;
  30259. +
  30260. +int printk_ratelimit(void)
  30261. +{
  30262. + return __printk_ratelimit(printk_ratelimit_jiffies,
  30263. + printk_ratelimit_burst);
  30264. +}
  30265. --- /dev/null Tue Mar 11 13:02:56 2003
  30266. +++ linux/net/ipsec/ipsec_life.c Mon Feb 9 13:51:03 2004
  30267. @@ -0,0 +1,273 @@
  30268. +/*
  30269. + * @(#) lifetime structure utilities
  30270. + *
  30271. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  30272. + * and Michael Richardson <mcr@freeswan.org>
  30273. + *
  30274. + * This program is free software; you can redistribute it and/or modify it
  30275. + * under the terms of the GNU General Public License as published by the
  30276. + * Free Software Foundation; either version 2 of the License, or (at your
  30277. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30278. + *
  30279. + * This program is distributed in the hope that it will be useful, but
  30280. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30281. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30282. + * for more details.
  30283. + *
  30284. + * RCSID $Id: ipsec_life.c,v 1.13.10.1 2006/10/06 21:39:26 paul Exp $
  30285. + *
  30286. + */
  30287. +
  30288. +/*
  30289. + * This provides series of utility functions for dealing with lifetime
  30290. + * structures.
  30291. + *
  30292. + * ipsec_check_lifetime - returns -1 hard lifetime exceeded
  30293. + * 0 soft lifetime exceeded
  30294. + * 1 everything is okay
  30295. + * based upon whether or not the count exceeds hard/soft
  30296. + *
  30297. + */
  30298. +
  30299. +#define __NO_VERSION__
  30300. +#include <linux/module.h>
  30301. +#ifndef AUTOCONF_INCLUDED
  30302. +#include <linux/config.h>
  30303. +#endif /* for CONFIG_IP_FORWARD */
  30304. +#include <linux/version.h>
  30305. +#include <linux/kernel.h> /* printk() */
  30306. +
  30307. +#include "openswan/ipsec_param.h"
  30308. +
  30309. +#include <linux/netdevice.h> /* struct device, struct net_device_stats and other headers */
  30310. +#include <linux/etherdevice.h> /* eth_type_trans */
  30311. +#include <linux/skbuff.h>
  30312. +#include <openswan.h>
  30313. +
  30314. +#include "openswan/radij.h"
  30315. +#include "openswan/ipsec_life.h"
  30316. +#include "openswan/ipsec_xform.h"
  30317. +#include "openswan/ipsec_eroute.h"
  30318. +#include "openswan/ipsec_encap.h"
  30319. +#include "openswan/ipsec_radij.h"
  30320. +
  30321. +#include "openswan/ipsec_sa.h"
  30322. +#include "openswan/ipsec_tunnel.h"
  30323. +#include "openswan/ipsec_ipe4.h"
  30324. +#include "openswan/ipsec_ah.h"
  30325. +#include "openswan/ipsec_esp.h"
  30326. +
  30327. +#ifdef CONFIG_KLIPS_IPCOMP
  30328. +#include "openswan/ipcomp.h"
  30329. +#endif /* CONFIG_KLIPS_IPCOMP */
  30330. +
  30331. +#include <pfkeyv2.h>
  30332. +#include <pfkey.h>
  30333. +
  30334. +#include "openswan/ipsec_proto.h"
  30335. +
  30336. +
  30337. +enum ipsec_life_alive
  30338. +ipsec_lifetime_check(struct ipsec_lifetime64 *il64,
  30339. + const char *lifename,
  30340. + const char *saname,
  30341. + enum ipsec_life_type ilt,
  30342. + enum ipsec_direction idir,
  30343. + struct ipsec_sa *ips)
  30344. +{
  30345. + __u64 count;
  30346. + const char *dir;
  30347. +
  30348. + if(saname == NULL) {
  30349. + saname = "unknown-SA";
  30350. + }
  30351. +
  30352. + if(idir == ipsec_incoming) {
  30353. + dir = "incoming";
  30354. + } else {
  30355. + dir = "outgoing";
  30356. + }
  30357. +
  30358. +
  30359. + if(ilt == ipsec_life_timebased) {
  30360. + count = jiffies/HZ - il64->ipl_count;
  30361. + } else {
  30362. + count = il64->ipl_count;
  30363. + }
  30364. +
  30365. + if(il64->ipl_hard &&
  30366. + (count > il64->ipl_hard)) {
  30367. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  30368. + "klips_debug:ipsec_lifetime_check: "
  30369. + "hard %s lifetime of SA:<%s%s%s> %s has been reached, SA expired, "
  30370. + "%s packet dropped.\n",
  30371. + lifename,
  30372. + IPS_XFORM_NAME(ips),
  30373. + saname,
  30374. + dir);
  30375. +
  30376. + pfkey_expire(ips, 1);
  30377. + return ipsec_life_harddied;
  30378. + }
  30379. +
  30380. + if(il64->ipl_soft &&
  30381. + (count > il64->ipl_soft)) {
  30382. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  30383. + "klips_debug:ipsec_lifetime_check: "
  30384. + "soft %s lifetime of SA:<%s%s%s> %s has been reached, SA expiring, "
  30385. + "soft expire message sent up, %s packet still processed.\n",
  30386. + lifename,
  30387. + IPS_XFORM_NAME(ips),
  30388. + saname,
  30389. + dir);
  30390. +
  30391. + if(ips->ips_state != SADB_SASTATE_DYING) {
  30392. + pfkey_expire(ips, 0);
  30393. + }
  30394. + ips->ips_state = SADB_SASTATE_DYING;
  30395. +
  30396. + return ipsec_life_softdied;
  30397. + }
  30398. + return ipsec_life_okay;
  30399. +}
  30400. +
  30401. +
  30402. +/*
  30403. + * This function takes a buffer (with length), a lifetime name and type,
  30404. + * and formats a string to represent the current values of the lifetime.
  30405. + *
  30406. + * It returns the number of bytes that the format took (or would take,
  30407. + * if the buffer were large enough: snprintf semantics).
  30408. + * This is used in /proc routines and in debug output.
  30409. + */
  30410. +int
  30411. +ipsec_lifetime_format(char *buffer,
  30412. + int buflen,
  30413. + char *lifename,
  30414. + enum ipsec_life_type timebaselife,
  30415. + struct ipsec_lifetime64 *lifetime)
  30416. +{
  30417. + int len = 0;
  30418. + __u64 count;
  30419. +
  30420. + if(timebaselife == ipsec_life_timebased) {
  30421. + count = jiffies/HZ - lifetime->ipl_count;
  30422. + } else {
  30423. + count = lifetime->ipl_count;
  30424. + }
  30425. +
  30426. + if(lifetime->ipl_count > 1 ||
  30427. + lifetime->ipl_soft ||
  30428. + lifetime->ipl_hard) {
  30429. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
  30430. + len = ipsec_snprintf(buffer, buflen,
  30431. + "%s(%Lu,%Lu,%Lu)",
  30432. + lifename,
  30433. + count,
  30434. + lifetime->ipl_soft,
  30435. + lifetime->ipl_hard);
  30436. +#else /* XXX high 32 bits are not displayed */
  30437. + len = ipsec_snprintf(buffer, buflen,
  30438. + "%s(%lu,%lu,%lu)",
  30439. + lifename,
  30440. + (unsigned long)count,
  30441. + (unsigned long)lifetime->ipl_soft,
  30442. + (unsigned long)lifetime->ipl_hard);
  30443. +#endif
  30444. + }
  30445. +
  30446. + return len;
  30447. +}
  30448. +
  30449. +void
  30450. +ipsec_lifetime_update_hard(struct ipsec_lifetime64 *lifetime,
  30451. + __u64 newvalue)
  30452. +{
  30453. + if(newvalue &&
  30454. + (!lifetime->ipl_hard ||
  30455. + (newvalue < lifetime->ipl_hard))) {
  30456. + lifetime->ipl_hard = newvalue;
  30457. +
  30458. + if(!lifetime->ipl_soft &&
  30459. + (lifetime->ipl_hard < lifetime->ipl_soft)) {
  30460. + lifetime->ipl_soft = lifetime->ipl_hard;
  30461. + }
  30462. + }
  30463. +}
  30464. +
  30465. +void
  30466. +ipsec_lifetime_update_soft(struct ipsec_lifetime64 *lifetime,
  30467. + __u64 newvalue)
  30468. +{
  30469. + if(newvalue &&
  30470. + (!lifetime->ipl_soft ||
  30471. + (newvalue < lifetime->ipl_soft))) {
  30472. + lifetime->ipl_soft = newvalue;
  30473. +
  30474. + if(lifetime->ipl_hard &&
  30475. + (lifetime->ipl_hard < lifetime->ipl_soft)) {
  30476. + lifetime->ipl_soft = lifetime->ipl_hard;
  30477. + }
  30478. + }
  30479. +}
  30480. +
  30481. +
  30482. +/*
  30483. + * $Log: ipsec_life.c,v $
  30484. + * Revision 1.13.10.1 2006/10/06 21:39:26 paul
  30485. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  30486. + * set. This is defined through autoconf.h which is included through the
  30487. + * linux kernel build macros.
  30488. + *
  30489. + * Revision 1.13 2004/07/10 19:11:18 mcr
  30490. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  30491. + *
  30492. + * Revision 1.12 2004/04/23 20:44:35 ken
  30493. + * Update comments
  30494. + *
  30495. + * Revision 1.11 2004/04/06 02:49:26 mcr
  30496. + * pullup of algo code from alg-branch.
  30497. + *
  30498. + * Revision 1.10 2004/03/30 11:03:10 paul
  30499. + * two more occurances of snprintf, found by Sam from a users oops msg.
  30500. + *
  30501. + * Revision 1.9 2003/10/31 02:27:55 mcr
  30502. + * pulled up port-selector patches and sa_id elimination.
  30503. + *
  30504. + * Revision 1.8.4.1 2003/10/29 01:30:41 mcr
  30505. + * elimited "struct sa_id".
  30506. + *
  30507. + * Revision 1.8 2003/02/06 02:00:10 rgb
  30508. + * Fixed incorrect debugging text label
  30509. + *
  30510. + * Revision 1.7 2002/05/23 07:16:26 rgb
  30511. + * Fixed absolute/relative reference to lifetime count printout.
  30512. + *
  30513. + * Revision 1.6 2002/04/24 07:55:32 mcr
  30514. + * #include patches and Makefiles for post-reorg compilation.
  30515. + *
  30516. + * Revision 1.5 2002/04/24 07:36:28 mcr
  30517. + * Moved from ./klips/net/ipsec/ipsec_life.c,v
  30518. + *
  30519. + * Revision 1.4 2002/01/29 17:17:55 mcr
  30520. + * moved include of ipsec_param.h to after include of linux/kernel.h
  30521. + * otherwise, it seems that some option that is set in ipsec_param.h
  30522. + * screws up something subtle in the include path to kernel.h, and
  30523. + * it complains on the snprintf() prototype.
  30524. + *
  30525. + * Revision 1.3 2002/01/29 02:13:17 mcr
  30526. + * introduction of ipsec_kversion.h means that include of
  30527. + * ipsec_param.h must preceed any decisions about what files to
  30528. + * include to deal with differences in kernel source.
  30529. + *
  30530. + * Revision 1.2 2001/11/26 09:16:14 rgb
  30531. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  30532. + *
  30533. + * Revision 1.1.2.1 2001/09/25 02:25:57 mcr
  30534. + * lifetime structure created and common functions created.
  30535. + *
  30536. + * Local variables:
  30537. + * c-file-style: "linux"
  30538. + * End:
  30539. + *
  30540. + */
  30541. --- /dev/null Tue Mar 11 13:02:56 2003
  30542. +++ linux/net/ipsec/ipsec_mast.c Mon Feb 9 13:51:03 2004
  30543. @@ -0,0 +1,1099 @@
  30544. +/*
  30545. + * IPSEC MAST code.
  30546. + * Copyright (C) 1996, 1997 John Ioannidis.
  30547. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Richard Guy Briggs.
  30548. + *
  30549. + * This program is free software; you can redistribute it and/or modify it
  30550. + * under the terms of the GNU General Public License as published by the
  30551. + * Free Software Foundation; either version 2 of the License, or (at your
  30552. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30553. + *
  30554. + * This program is distributed in the hope that it will be useful, but
  30555. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30556. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30557. + * for more details.
  30558. + */
  30559. +
  30560. +char ipsec_mast_c_version[] = "RCSID $Id: ipsec_mast.c,v 1.7.2.1 2006/10/06 21:39:26 paul Exp $";
  30561. +
  30562. +#define __NO_VERSION__
  30563. +#include <linux/module.h>
  30564. +#ifndef AUTOCONF_INCLUDED
  30565. +#include <linux/config.h>
  30566. +#endif /* for CONFIG_IP_FORWARD */
  30567. +#include <linux/version.h>
  30568. +#include <linux/kernel.h> /* printk() */
  30569. +
  30570. +#include "freeswan/ipsec_param.h"
  30571. +
  30572. +#ifdef MALLOC_SLAB
  30573. +# include <linux/slab.h> /* kmalloc() */
  30574. +#else /* MALLOC_SLAB */
  30575. +# include <linux/malloc.h> /* kmalloc() */
  30576. +#endif /* MALLOC_SLAB */
  30577. +#include <linux/errno.h> /* error codes */
  30578. +#include <linux/types.h> /* size_t */
  30579. +#include <linux/interrupt.h> /* mark_bh */
  30580. +
  30581. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  30582. +#include <linux/etherdevice.h> /* eth_type_trans */
  30583. +#include <linux/ip.h> /* struct iphdr */
  30584. +#include <linux/tcp.h> /* struct tcphdr */
  30585. +#include <linux/udp.h> /* struct udphdr */
  30586. +#include <linux/skbuff.h>
  30587. +#include <freeswan.h>
  30588. +#include <linux/in6.h>
  30589. +#include <net/dst.h>
  30590. +#undef dev_kfree_skb
  30591. +#define dev_kfree_skb(a,b) kfree_skb(a)
  30592. +#define PHYSDEV_TYPE
  30593. +#include <net/icmp.h> /* icmp_send() */
  30594. +#include <net/ip.h>
  30595. +#include <linux/netfilter_ipv4.h>
  30596. +
  30597. +#include <linux/if_arp.h>
  30598. +
  30599. +#include "freeswan/radij.h"
  30600. +#include "freeswan/ipsec_life.h"
  30601. +#include "freeswan/ipsec_xform.h"
  30602. +#include "freeswan/ipsec_eroute.h"
  30603. +#include "freeswan/ipsec_encap.h"
  30604. +#include "freeswan/ipsec_radij.h"
  30605. +#include "freeswan/ipsec_sa.h"
  30606. +#include "freeswan/ipsec_tunnel.h"
  30607. +#include "freeswan/ipsec_mast.h"
  30608. +#include "freeswan/ipsec_ipe4.h"
  30609. +#include "freeswan/ipsec_ah.h"
  30610. +#include "freeswan/ipsec_esp.h"
  30611. +
  30612. +#include <pfkeyv2.h>
  30613. +#include <pfkey.h>
  30614. +
  30615. +#include "freeswan/ipsec_proto.h"
  30616. +
  30617. +int ipsec_maxdevice_count = -1;
  30618. +
  30619. +DEBUG_NO_STATIC int
  30620. +ipsec_mast_open(struct net_device *dev)
  30621. +{
  30622. + struct ipsecpriv *prv = dev->priv;
  30623. +
  30624. + /*
  30625. + * Can't open until attached.
  30626. + */
  30627. +
  30628. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  30629. + "klips_debug:ipsec_mast_open: "
  30630. + "dev = %s, prv->dev = %s\n",
  30631. + dev->name, prv->dev?prv->dev->name:"NONE");
  30632. +
  30633. + if (prv->dev == NULL)
  30634. + return -ENODEV;
  30635. +
  30636. + KLIPS_INC_USE;
  30637. + return 0;
  30638. +}
  30639. +
  30640. +DEBUG_NO_STATIC int
  30641. +ipsec_mast_close(struct net_device *dev)
  30642. +{
  30643. + KLIPS_DEC_USE;
  30644. + return 0;
  30645. +}
  30646. +
  30647. +static inline int ipsec_mast_xmit2(struct sk_buff *skb)
  30648. +{
  30649. + return ip_send(skb);
  30650. +}
  30651. +
  30652. +enum ipsec_xmit_value
  30653. +ipsec_mast_send(struct ipsec_xmit_state*ixs)
  30654. +{
  30655. + /* new route/dst cache code from James Morris */
  30656. + ixs->skb->dev = ixs->physdev;
  30657. + /*skb_orphan(ixs->skb);*/
  30658. + if((ixs->error = ip_route_output(&ixs->route,
  30659. + ixs->skb->nh.iph->daddr,
  30660. + ixs->pass ? 0 : ixs->skb->nh.iph->saddr,
  30661. + RT_TOS(ixs->skb->nh.iph->tos),
  30662. + ixs->physdev->iflink /* rgb: should this be 0? */))) {
  30663. + ixs->stats->tx_errors++;
  30664. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30665. + "klips_debug:ipsec_xmit_send: "
  30666. + "ip_route_output failed with error code %d, rt->u.dst.dev=%s, dropped\n",
  30667. + ixs->error,
  30668. + ixs->route->u.dst.dev->name);
  30669. + return IPSEC_XMIT_ROUTEERR;
  30670. + }
  30671. + if(ixs->dev == ixs->route->u.dst.dev) {
  30672. + ip_rt_put(ixs->route);
  30673. + /* This is recursion, drop it. */
  30674. + ixs->stats->tx_errors++;
  30675. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30676. + "klips_debug:ipsec_xmit_send: "
  30677. + "suspect recursion, dev=rt->u.dst.dev=%s, dropped\n",
  30678. + ixs->dev->name);
  30679. + return IPSEC_XMIT_RECURSDETECT;
  30680. + }
  30681. + dst_release(ixs->skb->dst);
  30682. + ixs->skb->dst = &ixs->route->u.dst;
  30683. + ixs->stats->tx_bytes += ixs->skb->len;
  30684. + if(ixs->skb->len < ixs->skb->nh.raw - ixs->skb->data) {
  30685. + ixs->stats->tx_errors++;
  30686. + printk(KERN_WARNING
  30687. + "klips_error:ipsec_xmit_send: "
  30688. + "tried to __skb_pull nh-data=%ld, %d available. This should never happen, please report.\n",
  30689. + (unsigned long)(ixs->skb->nh.raw - ixs->skb->data),
  30690. + ixs->skb->len);
  30691. + return IPSEC_XMIT_PUSHPULLERR;
  30692. + }
  30693. + __skb_pull(ixs->skb, ixs->skb->nh.raw - ixs->skb->data);
  30694. +#ifdef SKB_RESET_NFCT
  30695. + nf_conntrack_put(ixs->skb->nfct);
  30696. + ixs->skb->nfct = NULL;
  30697. +#ifdef CONFIG_NETFILTER_DEBUG
  30698. + ixs->skb->nf_debug = 0;
  30699. +#endif /* CONFIG_NETFILTER_DEBUG */
  30700. +#endif /* SKB_RESET_NFCT */
  30701. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30702. + "klips_debug:ipsec_xmit_send: "
  30703. + "...done, calling ip_send() on device:%s\n",
  30704. + ixs->skb->dev ? ixs->skb->dev->name : "NULL");
  30705. + KLIPS_IP_PRINT(debug_mast & DB_MAST_XMIT, ixs->skb->nh.iph);
  30706. + {
  30707. + int err;
  30708. +
  30709. + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, ixs->skb, NULL, ixs->route->u.dst.dev,
  30710. + ipsec_mast_xmit2);
  30711. + if(err != NET_XMIT_SUCCESS && err != NET_XMIT_CN) {
  30712. + if(net_ratelimit())
  30713. + printk(KERN_ERR
  30714. + "klips_error:ipsec_xmit_send: "
  30715. + "ip_send() failed, err=%d\n",
  30716. + -err);
  30717. + ixs->stats->tx_errors++;
  30718. + ixs->stats->tx_aborted_errors++;
  30719. + ixs->skb = NULL;
  30720. + return IPSEC_XMIT_IPSENDFAILURE;
  30721. + }
  30722. + }
  30723. + ixs->stats->tx_packets++;
  30724. +
  30725. + ixs->skb = NULL;
  30726. +
  30727. + return IPSEC_XMIT_OK;
  30728. +}
  30729. +
  30730. +void
  30731. +ipsec_mast_cleanup(struct ipsec_xmit_state*ixs)
  30732. +{
  30733. +#if defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE)
  30734. + netif_wake_queue(ixs->dev);
  30735. +#else /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  30736. + ixs->dev->tbusy = 0;
  30737. +#endif /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  30738. + if(ixs->saved_header) {
  30739. + kfree(ixs->saved_header);
  30740. + }
  30741. + if(ixs->skb) {
  30742. + dev_kfree_skb(ixs->skb, FREE_WRITE);
  30743. + }
  30744. + if(ixs->oskb) {
  30745. + dev_kfree_skb(ixs->oskb, FREE_WRITE);
  30746. + }
  30747. + if (ixs->ips.ips_ident_s.data) {
  30748. + kfree(ixs->ips.ips_ident_s.data);
  30749. + }
  30750. + if (ixs->ips.ips_ident_d.data) {
  30751. + kfree(ixs->ips.ips_ident_d.data);
  30752. + }
  30753. +}
  30754. +
  30755. +#if 0
  30756. +/*
  30757. + * This function assumes it is being called from dev_queue_xmit()
  30758. + * and that skb is filled properly by that function.
  30759. + */
  30760. +int
  30761. +ipsec_mast_start_xmit(struct sk_buff *skb, struct net_device *dev, IPsecSAref_t SAref)
  30762. +{
  30763. + struct ipsec_xmit_state ixs_mem;
  30764. + struct ipsec_xmit_state *ixs = &ixs_mem;
  30765. + enum ipsec_xmit_value stat = IPSEC_XMIT_OK;
  30766. +
  30767. + /* dev could be a mast device, but should be optional, I think... */
  30768. + /* SAref is also optional, but one of the two must be present. */
  30769. + /* I wonder if it could accept no device or saref and guess? */
  30770. +
  30771. +/* ipsec_xmit_sanity_check_dev(ixs); */
  30772. +
  30773. + ipsec_xmit_sanity_check_skb(ixs);
  30774. +
  30775. + ipsec_xmit_adjust_hard_header(ixs);
  30776. +
  30777. + stat = ipsec_xmit_encap_bundle(ixs);
  30778. + if(stat != IPSEC_XMIT_OK) {
  30779. + /* SA processing failed */
  30780. + }
  30781. +
  30782. + ipsec_xmit_hard_header_restore();
  30783. +}
  30784. +#endif
  30785. +
  30786. +DEBUG_NO_STATIC struct net_device_stats *
  30787. +ipsec_mast_get_stats(struct net_device *dev)
  30788. +{
  30789. + return &(((struct ipsecpriv *)(dev->priv))->mystats);
  30790. +}
  30791. +
  30792. +/*
  30793. + * Revectored calls.
  30794. + * For each of these calls, a field exists in our private structure.
  30795. + */
  30796. +
  30797. +DEBUG_NO_STATIC int
  30798. +ipsec_mast_hard_header(struct sk_buff *skb, struct net_device *dev,
  30799. + unsigned short type, void *daddr, void *saddr, unsigned len)
  30800. +{
  30801. + struct ipsecpriv *prv = dev->priv;
  30802. + struct net_device *tmp;
  30803. + int ret;
  30804. + struct net_device_stats *stats; /* This device's statistics */
  30805. +
  30806. + if(skb == NULL) {
  30807. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30808. + "klips_debug:ipsec_mast_hard_header: "
  30809. + "no skb...\n");
  30810. + return -ENODATA;
  30811. + }
  30812. +
  30813. + if(dev == NULL) {
  30814. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30815. + "klips_debug:ipsec_mast_hard_header: "
  30816. + "no device...\n");
  30817. + return -ENODEV;
  30818. + }
  30819. +
  30820. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30821. + "klips_debug:ipsec_mast_hard_header: "
  30822. + "skb->dev=%s dev=%s.\n",
  30823. + skb->dev ? skb->dev->name : "NULL",
  30824. + dev->name);
  30825. +
  30826. + if(prv == NULL) {
  30827. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30828. + "klips_debug:ipsec_mast_hard_header: "
  30829. + "no private space associated with dev=%s\n",
  30830. + dev->name ? dev->name : "NULL");
  30831. + return -ENODEV;
  30832. + }
  30833. +
  30834. + stats = (struct net_device_stats *) &(prv->mystats);
  30835. +
  30836. + if(prv->dev == NULL) {
  30837. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30838. + "klips_debug:ipsec_mast_hard_header: "
  30839. + "no physical device associated with dev=%s\n",
  30840. + dev->name ? dev->name : "NULL");
  30841. + stats->tx_dropped++;
  30842. + return -ENODEV;
  30843. + }
  30844. +
  30845. + /* check if we have to send a IPv6 packet. It might be a Router
  30846. + Solicitation, where the building of the packet happens in
  30847. + reverse order:
  30848. + 1. ll hdr,
  30849. + 2. IPv6 hdr,
  30850. + 3. ICMPv6 hdr
  30851. + -> skb->nh.raw is still uninitialized when this function is
  30852. + called!! If this is no IPv6 packet, we can print debugging
  30853. + messages, otherwise we skip all debugging messages and just
  30854. + build the ll header */
  30855. + if(type != ETH_P_IPV6) {
  30856. + /* execute this only, if we don't have to build the
  30857. + header for a IPv6 packet */
  30858. + if(!prv->hard_header) {
  30859. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30860. + "klips_debug:ipsec_mast_hard_header: "
  30861. + "physical device has been detached, packet dropped 0p%p->0p%p len=%d type=%d dev=%s->NULL ",
  30862. + saddr,
  30863. + daddr,
  30864. + len,
  30865. + type,
  30866. + dev->name);
  30867. + KLIPS_PRINTMORE(debug_mast & DB_MAST_REVEC,
  30868. + "ip=%08x->%08x\n",
  30869. + (__u32)ntohl(skb->nh.iph->saddr),
  30870. + (__u32)ntohl(skb->nh.iph->daddr) );
  30871. + stats->tx_dropped++;
  30872. + return -ENODEV;
  30873. + }
  30874. +
  30875. +#define da ((struct net_device *)(prv->dev))->dev_addr
  30876. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30877. + "klips_debug:ipsec_mast_hard_header: "
  30878. + "Revectored 0p%p->0p%p len=%d type=%d dev=%s->%s dev_addr=%02x:%02x:%02x:%02x:%02x:%02x ",
  30879. + saddr,
  30880. + daddr,
  30881. + len,
  30882. + type,
  30883. + dev->name,
  30884. + prv->dev->name,
  30885. + da[0], da[1], da[2], da[3], da[4], da[5]);
  30886. + KLIPS_PRINTMORE(debug_mast & DB_MAST_REVEC,
  30887. + "ip=%08x->%08x\n",
  30888. + (__u32)ntohl(skb->nh.iph->saddr),
  30889. + (__u32)ntohl(skb->nh.iph->daddr) );
  30890. + } else {
  30891. + KLIPS_PRINT(debug_mast,
  30892. + "klips_debug:ipsec_mast_hard_header: "
  30893. + "is IPv6 packet, skip debugging messages, only revector and build linklocal header.\n");
  30894. + }
  30895. + tmp = skb->dev;
  30896. + skb->dev = prv->dev;
  30897. + ret = prv->hard_header(skb, prv->dev, type, (void *)daddr, (void *)saddr, len);
  30898. + skb->dev = tmp;
  30899. + return ret;
  30900. +}
  30901. +
  30902. +DEBUG_NO_STATIC int
  30903. +ipsec_mast_rebuild_header(struct sk_buff *skb)
  30904. +{
  30905. + struct ipsecpriv *prv = skb->dev->priv;
  30906. + struct net_device *tmp;
  30907. + int ret;
  30908. + struct net_device_stats *stats; /* This device's statistics */
  30909. +
  30910. + if(skb->dev == NULL) {
  30911. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30912. + "klips_debug:ipsec_mast_rebuild_header: "
  30913. + "no device...");
  30914. + return -ENODEV;
  30915. + }
  30916. +
  30917. + if(prv == NULL) {
  30918. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30919. + "klips_debug:ipsec_mast_rebuild_header: "
  30920. + "no private space associated with dev=%s",
  30921. + skb->dev->name ? skb->dev->name : "NULL");
  30922. + return -ENODEV;
  30923. + }
  30924. +
  30925. + stats = (struct net_device_stats *) &(prv->mystats);
  30926. +
  30927. + if(prv->dev == NULL) {
  30928. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30929. + "klips_debug:ipsec_mast_rebuild_header: "
  30930. + "no physical device associated with dev=%s",
  30931. + skb->dev->name ? skb->dev->name : "NULL");
  30932. + stats->tx_dropped++;
  30933. + return -ENODEV;
  30934. + }
  30935. +
  30936. + if(!prv->rebuild_header) {
  30937. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30938. + "klips_debug:ipsec_mast_rebuild_header: "
  30939. + "physical device has been detached, packet dropped skb->dev=%s->NULL ",
  30940. + skb->dev->name);
  30941. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30942. + "ip=%08x->%08x\n",
  30943. + (__u32)ntohl(skb->nh.iph->saddr),
  30944. + (__u32)ntohl(skb->nh.iph->daddr) );
  30945. + stats->tx_dropped++;
  30946. + return -ENODEV;
  30947. + }
  30948. +
  30949. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30950. + "klips_debug:ipsec_mast: "
  30951. + "Revectored rebuild_header dev=%s->%s ",
  30952. + skb->dev->name, prv->dev->name);
  30953. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30954. + "ip=%08x->%08x\n",
  30955. + (__u32)ntohl(skb->nh.iph->saddr),
  30956. + (__u32)ntohl(skb->nh.iph->daddr) );
  30957. + tmp = skb->dev;
  30958. + skb->dev = prv->dev;
  30959. +
  30960. + ret = prv->rebuild_header(skb);
  30961. + skb->dev = tmp;
  30962. + return ret;
  30963. +}
  30964. +
  30965. +DEBUG_NO_STATIC int
  30966. +ipsec_mast_set_mac_address(struct net_device *dev, void *addr)
  30967. +{
  30968. + struct ipsecpriv *prv = dev->priv;
  30969. +
  30970. + struct net_device_stats *stats; /* This device's statistics */
  30971. +
  30972. + if(dev == NULL) {
  30973. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30974. + "klips_debug:ipsec_mast_set_mac_address: "
  30975. + "no device...");
  30976. + return -ENODEV;
  30977. + }
  30978. +
  30979. + if(prv == NULL) {
  30980. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30981. + "klips_debug:ipsec_mast_set_mac_address: "
  30982. + "no private space associated with dev=%s",
  30983. + dev->name ? dev->name : "NULL");
  30984. + return -ENODEV;
  30985. + }
  30986. +
  30987. + stats = (struct net_device_stats *) &(prv->mystats);
  30988. +
  30989. + if(prv->dev == NULL) {
  30990. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30991. + "klips_debug:ipsec_mast_set_mac_address: "
  30992. + "no physical device associated with dev=%s",
  30993. + dev->name ? dev->name : "NULL");
  30994. + stats->tx_dropped++;
  30995. + return -ENODEV;
  30996. + }
  30997. +
  30998. + if(!prv->set_mac_address) {
  30999. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31000. + "klips_debug:ipsec_mast_set_mac_address: "
  31001. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  31002. + dev->name);
  31003. + return -ENODEV;
  31004. + }
  31005. +
  31006. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31007. + "klips_debug:ipsec_mast_set_mac_address: "
  31008. + "Revectored dev=%s->%s addr=0p%p\n",
  31009. + dev->name, prv->dev->name, addr);
  31010. + return prv->set_mac_address(prv->dev, addr);
  31011. +
  31012. +}
  31013. +
  31014. +DEBUG_NO_STATIC void
  31015. +ipsec_mast_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
  31016. +{
  31017. + struct ipsecpriv *prv = dev->priv;
  31018. +
  31019. + struct net_device_stats *stats; /* This device's statistics */
  31020. +
  31021. + if(dev == NULL) {
  31022. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31023. + "klips_debug:ipsec_mast_cache_update: "
  31024. + "no device...");
  31025. + return;
  31026. + }
  31027. +
  31028. + if(prv == NULL) {
  31029. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31030. + "klips_debug:ipsec_mast_cache_update: "
  31031. + "no private space associated with dev=%s",
  31032. + dev->name ? dev->name : "NULL");
  31033. + return;
  31034. + }
  31035. +
  31036. + stats = (struct net_device_stats *) &(prv->mystats);
  31037. +
  31038. + if(prv->dev == NULL) {
  31039. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31040. + "klips_debug:ipsec_mast_cache_update: "
  31041. + "no physical device associated with dev=%s",
  31042. + dev->name ? dev->name : "NULL");
  31043. + stats->tx_dropped++;
  31044. + return;
  31045. + }
  31046. +
  31047. + if(!prv->header_cache_update) {
  31048. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31049. + "klips_debug:ipsec_mast_cache_update: "
  31050. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  31051. + dev->name);
  31052. + return;
  31053. + }
  31054. +
  31055. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31056. + "klips_debug:ipsec_mast: "
  31057. + "Revectored cache_update\n");
  31058. + prv->header_cache_update(hh, prv->dev, haddr);
  31059. + return;
  31060. +}
  31061. +
  31062. +DEBUG_NO_STATIC int
  31063. +ipsec_mast_neigh_setup(struct neighbour *n)
  31064. +{
  31065. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31066. + "klips_debug:ipsec_mast_neigh_setup:\n");
  31067. +
  31068. + if (n->nud_state == NUD_NONE) {
  31069. + n->ops = &arp_broken_ops;
  31070. + n->output = n->ops->output;
  31071. + }
  31072. + return 0;
  31073. +}
  31074. +
  31075. +DEBUG_NO_STATIC int
  31076. +ipsec_mast_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  31077. +{
  31078. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31079. + "klips_debug:ipsec_mast_neigh_setup_dev: "
  31080. + "setting up %s\n",
  31081. + dev ? dev->name : "NULL");
  31082. +
  31083. + if (p->tbl->family == AF_INET) {
  31084. + p->neigh_setup = ipsec_mast_neigh_setup;
  31085. + p->ucast_probes = 0;
  31086. + p->mcast_probes = 0;
  31087. + }
  31088. + return 0;
  31089. +}
  31090. +
  31091. +/*
  31092. + * We call the attach routine to attach another device.
  31093. + */
  31094. +
  31095. +DEBUG_NO_STATIC int
  31096. +ipsec_mast_attach(struct net_device *dev, struct net_device *physdev)
  31097. +{
  31098. + int i;
  31099. + struct ipsecpriv *prv = dev->priv;
  31100. +
  31101. + if(dev == NULL) {
  31102. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31103. + "klips_debug:ipsec_mast_attach: "
  31104. + "no device...");
  31105. + return -ENODEV;
  31106. + }
  31107. +
  31108. + if(prv == NULL) {
  31109. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31110. + "klips_debug:ipsec_mast_attach: "
  31111. + "no private space associated with dev=%s",
  31112. + dev->name ? dev->name : "NULL");
  31113. + return -ENODATA;
  31114. + }
  31115. +
  31116. + prv->dev = physdev;
  31117. + prv->hard_start_xmit = physdev->hard_start_xmit;
  31118. + prv->get_stats = physdev->get_stats;
  31119. +
  31120. + if (physdev->hard_header) {
  31121. + prv->hard_header = physdev->hard_header;
  31122. + dev->hard_header = ipsec_mast_hard_header;
  31123. + } else
  31124. + dev->hard_header = NULL;
  31125. +
  31126. + if (physdev->rebuild_header) {
  31127. + prv->rebuild_header = physdev->rebuild_header;
  31128. + dev->rebuild_header = ipsec_mast_rebuild_header;
  31129. + } else
  31130. + dev->rebuild_header = NULL;
  31131. +
  31132. + if (physdev->set_mac_address) {
  31133. + prv->set_mac_address = physdev->set_mac_address;
  31134. + dev->set_mac_address = ipsec_mast_set_mac_address;
  31135. + } else
  31136. + dev->set_mac_address = NULL;
  31137. +
  31138. + if (physdev->header_cache_update) {
  31139. + prv->header_cache_update = physdev->header_cache_update;
  31140. + dev->header_cache_update = ipsec_mast_cache_update;
  31141. + } else
  31142. + dev->header_cache_update = NULL;
  31143. +
  31144. + dev->hard_header_len = physdev->hard_header_len;
  31145. +
  31146. +/* prv->neigh_setup = physdev->neigh_setup; */
  31147. + dev->neigh_setup = ipsec_mast_neigh_setup_dev;
  31148. + dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */
  31149. + prv->mtu = physdev->mtu;
  31150. +
  31151. +#ifdef PHYSDEV_TYPE
  31152. + dev->type = physdev->type; /* ARPHRD_MAST; */
  31153. +#endif /* PHYSDEV_TYPE */
  31154. +
  31155. + dev->addr_len = physdev->addr_len;
  31156. + for (i=0; i<dev->addr_len; i++) {
  31157. + dev->dev_addr[i] = physdev->dev_addr[i];
  31158. + }
  31159. +#ifdef CONFIG_KLIPS_DEBUG
  31160. + if(debug_mast & DB_MAST_INIT) {
  31161. + printk(KERN_INFO "klips_debug:ipsec_mast_attach: "
  31162. + "physical device %s being attached has HW address: %2x",
  31163. + physdev->name, physdev->dev_addr[0]);
  31164. + for (i=1; i < physdev->addr_len; i++) {
  31165. + printk(":%02x", physdev->dev_addr[i]);
  31166. + }
  31167. + printk("\n");
  31168. + }
  31169. +#endif /* CONFIG_KLIPS_DEBUG */
  31170. +
  31171. + return 0;
  31172. +}
  31173. +
  31174. +/*
  31175. + * We call the detach routine to detach the ipsec mast from another device.
  31176. + */
  31177. +
  31178. +DEBUG_NO_STATIC int
  31179. +ipsec_mast_detach(struct net_device *dev)
  31180. +{
  31181. + int i;
  31182. + struct ipsecpriv *prv = dev->priv;
  31183. +
  31184. + if(dev == NULL) {
  31185. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31186. + "klips_debug:ipsec_mast_detach: "
  31187. + "no device...");
  31188. + return -ENODEV;
  31189. + }
  31190. +
  31191. + if(prv == NULL) {
  31192. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31193. + "klips_debug:ipsec_mast_detach: "
  31194. + "no private space associated with dev=%s",
  31195. + dev->name ? dev->name : "NULL");
  31196. + return -ENODATA;
  31197. + }
  31198. +
  31199. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31200. + "klips_debug:ipsec_mast_detach: "
  31201. + "physical device %s being detached from virtual device %s\n",
  31202. + prv->dev ? prv->dev->name : "NULL",
  31203. + dev->name);
  31204. +
  31205. + prv->dev = NULL;
  31206. + prv->hard_start_xmit = NULL;
  31207. + prv->get_stats = NULL;
  31208. +
  31209. + prv->hard_header = NULL;
  31210. +#ifdef DETACH_AND_DOWN
  31211. + dev->hard_header = NULL;
  31212. +#endif /* DETACH_AND_DOWN */
  31213. +
  31214. + prv->rebuild_header = NULL;
  31215. +#ifdef DETACH_AND_DOWN
  31216. + dev->rebuild_header = NULL;
  31217. +#endif /* DETACH_AND_DOWN */
  31218. +
  31219. + prv->set_mac_address = NULL;
  31220. +#ifdef DETACH_AND_DOWN
  31221. + dev->set_mac_address = NULL;
  31222. +#endif /* DETACH_AND_DOWN */
  31223. +
  31224. + prv->header_cache_update = NULL;
  31225. +#ifdef DETACH_AND_DOWN
  31226. + dev->header_cache_update = NULL;
  31227. +#endif /* DETACH_AND_DOWN */
  31228. +
  31229. +#ifdef DETACH_AND_DOWN
  31230. + dev->neigh_setup = NULL;
  31231. +#endif /* DETACH_AND_DOWN */
  31232. +
  31233. + dev->hard_header_len = 0;
  31234. +#ifdef DETACH_AND_DOWN
  31235. + dev->mtu = 0;
  31236. +#endif /* DETACH_AND_DOWN */
  31237. + prv->mtu = 0;
  31238. + for (i=0; i<MAX_ADDR_LEN; i++) {
  31239. + dev->dev_addr[i] = 0;
  31240. + }
  31241. + dev->addr_len = 0;
  31242. +#ifdef PHYSDEV_TYPE
  31243. + dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */
  31244. +#endif /* PHYSDEV_TYPE */
  31245. +
  31246. + return 0;
  31247. +}
  31248. +
  31249. +/*
  31250. + * We call the clear routine to detach all ipsec masts from other devices.
  31251. + */
  31252. +DEBUG_NO_STATIC int
  31253. +ipsec_mast_clear(void)
  31254. +{
  31255. + int i;
  31256. + struct net_device *ipsecdev = NULL, *prvdev;
  31257. + struct ipsecpriv *prv;
  31258. + char name[9];
  31259. + int ret;
  31260. +
  31261. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31262. + "klips_debug:ipsec_mast_clear: .\n");
  31263. +
  31264. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  31265. + sprintf(name, IPSEC_DEV_FORMAT, i);
  31266. + if((ipsecdev = ipsec_dev_get(name)) != NULL) {
  31267. + if((prv = (struct ipsecpriv *)(ipsecdev->priv))) {
  31268. + prvdev = (struct net_device *)(prv->dev);
  31269. + if(prvdev) {
  31270. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31271. + "klips_debug:ipsec_mast_clear: "
  31272. + "physical device for device %s is %s\n",
  31273. + name, prvdev->name);
  31274. + if((ret = ipsec_mast_detach(ipsecdev))) {
  31275. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31276. + "klips_debug:ipsec_mast_clear: "
  31277. + "error %d detatching device %s from device %s.\n",
  31278. + ret, name, prvdev->name);
  31279. + return ret;
  31280. + }
  31281. + }
  31282. + }
  31283. + }
  31284. + }
  31285. + return 0;
  31286. +}
  31287. +
  31288. +DEBUG_NO_STATIC int
  31289. +ipsec_mast_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  31290. +{
  31291. + struct ipsecmastconf *cf = (struct ipsecmastconf *)&ifr->ifr_data;
  31292. + struct ipsecpriv *prv = dev->priv;
  31293. + struct net_device *them; /* physical device */
  31294. +#ifdef CONFIG_IP_ALIAS
  31295. + char *colon;
  31296. + char realphysname[IFNAMSIZ];
  31297. +#endif /* CONFIG_IP_ALIAS */
  31298. +
  31299. + if(dev == NULL) {
  31300. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31301. + "klips_debug:ipsec_mast_ioctl: "
  31302. + "device not supplied.\n");
  31303. + return -ENODEV;
  31304. + }
  31305. +
  31306. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31307. + "klips_debug:ipsec_mast_ioctl: "
  31308. + "tncfg service call #%d for dev=%s\n",
  31309. + cmd,
  31310. + dev->name ? dev->name : "NULL");
  31311. + switch (cmd) {
  31312. + /* attach a virtual ipsec? device to a physical device */
  31313. + case IPSEC_SET_DEV:
  31314. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31315. + "klips_debug:ipsec_mast_ioctl: "
  31316. + "calling ipsec_mast_attatch...\n");
  31317. +#ifdef CONFIG_IP_ALIAS
  31318. + /* If this is an IP alias interface, get its real physical name */
  31319. + strncpy(realphysname, cf->cf_name, IFNAMSIZ);
  31320. + realphysname[IFNAMSIZ-1] = 0;
  31321. + colon = strchr(realphysname, ':');
  31322. + if (colon) *colon = 0;
  31323. + them = ipsec_dev_get(realphysname);
  31324. +#else /* CONFIG_IP_ALIAS */
  31325. + them = ipsec_dev_get(cf->cf_name);
  31326. +#endif /* CONFIG_IP_ALIAS */
  31327. +
  31328. + if (them == NULL) {
  31329. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31330. + "klips_debug:ipsec_mast_ioctl: "
  31331. + "physical device %s requested is null\n",
  31332. + cf->cf_name);
  31333. + return -ENXIO;
  31334. + }
  31335. +
  31336. +#if 0
  31337. + if (them->flags & IFF_UP) {
  31338. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31339. + "klips_debug:ipsec_mast_ioctl: "
  31340. + "physical device %s requested is not up.\n",
  31341. + cf->cf_name);
  31342. + return -ENXIO;
  31343. + }
  31344. +#endif
  31345. +
  31346. + if (prv && prv->dev) {
  31347. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31348. + "klips_debug:ipsec_mast_ioctl: "
  31349. + "virtual device is already connected to %s.\n",
  31350. + prv->dev->name ? prv->dev->name : "NULL");
  31351. + return -EBUSY;
  31352. + }
  31353. + return ipsec_mast_attach(dev, them);
  31354. +
  31355. + case IPSEC_DEL_DEV:
  31356. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31357. + "klips_debug:ipsec_mast_ioctl: "
  31358. + "calling ipsec_mast_detatch.\n");
  31359. + if (! prv->dev) {
  31360. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31361. + "klips_debug:ipsec_mast_ioctl: "
  31362. + "physical device not connected.\n");
  31363. + return -ENODEV;
  31364. + }
  31365. + return ipsec_mast_detach(dev);
  31366. +
  31367. + case IPSEC_CLR_DEV:
  31368. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31369. + "klips_debug:ipsec_mast_ioctl: "
  31370. + "calling ipsec_mast_clear.\n");
  31371. + return ipsec_mast_clear();
  31372. +
  31373. + default:
  31374. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31375. + "klips_debug:ipsec_mast_ioctl: "
  31376. + "unknown command %d.\n",
  31377. + cmd);
  31378. + return -EOPNOTSUPP;
  31379. + }
  31380. +}
  31381. +
  31382. +int
  31383. +ipsec_mast_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  31384. +{
  31385. + struct net_device *dev = ptr;
  31386. + struct net_device *ipsec_dev;
  31387. + struct ipsecpriv *priv;
  31388. + char name[9];
  31389. + int i;
  31390. +
  31391. + if (dev == NULL) {
  31392. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31393. + "klips_debug:ipsec_mast_device_event: "
  31394. + "dev=NULL for event type %ld.\n",
  31395. + event);
  31396. + return(NOTIFY_DONE);
  31397. + }
  31398. +
  31399. + /* check for loopback devices */
  31400. + if (dev && (dev->flags & IFF_LOOPBACK)) {
  31401. + return(NOTIFY_DONE);
  31402. + }
  31403. +
  31404. + switch (event) {
  31405. + case NETDEV_DOWN:
  31406. + /* look very carefully at the scope of these compiler
  31407. + directives before changing anything... -- RGB */
  31408. +
  31409. + case NETDEV_UNREGISTER:
  31410. + switch (event) {
  31411. + case NETDEV_DOWN:
  31412. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31413. + "klips_debug:ipsec_mast_device_event: "
  31414. + "NETDEV_DOWN dev=%s flags=%x\n",
  31415. + dev->name,
  31416. + dev->flags);
  31417. + if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) {
  31418. + printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n",
  31419. + dev->name);
  31420. + }
  31421. + break;
  31422. + case NETDEV_UNREGISTER:
  31423. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31424. + "klips_debug:ipsec_mast_device_event: "
  31425. + "NETDEV_UNREGISTER dev=%s flags=%x\n",
  31426. + dev->name,
  31427. + dev->flags);
  31428. + break;
  31429. + }
  31430. +
  31431. + /* find the attached physical device and detach it. */
  31432. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  31433. + sprintf(name, IPSEC_DEV_FORMAT, i);
  31434. + ipsec_dev = ipsec_dev_get(name);
  31435. + if(ipsec_dev) {
  31436. + priv = (struct ipsecpriv *)(ipsec_dev->priv);
  31437. + if(priv) {
  31438. + ;
  31439. + if(((struct net_device *)(priv->dev)) == dev) {
  31440. + /* dev_close(ipsec_dev); */
  31441. + /* return */ ipsec_mast_detach(ipsec_dev);
  31442. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31443. + "klips_debug:ipsec_mast_device_event: "
  31444. + "device '%s' has been detached.\n",
  31445. + ipsec_dev->name);
  31446. + break;
  31447. + }
  31448. + } else {
  31449. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31450. + "klips_debug:ipsec_mast_device_event: "
  31451. + "device '%s' has no private data space!\n",
  31452. + ipsec_dev->name);
  31453. + }
  31454. + }
  31455. + }
  31456. + break;
  31457. + case NETDEV_UP:
  31458. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31459. + "klips_debug:ipsec_mast_device_event: "
  31460. + "NETDEV_UP dev=%s\n",
  31461. + dev->name);
  31462. + break;
  31463. + case NETDEV_REBOOT:
  31464. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31465. + "klips_debug:ipsec_mast_device_event: "
  31466. + "NETDEV_REBOOT dev=%s\n",
  31467. + dev->name);
  31468. + break;
  31469. + case NETDEV_CHANGE:
  31470. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31471. + "klips_debug:ipsec_mast_device_event: "
  31472. + "NETDEV_CHANGE dev=%s flags=%x\n",
  31473. + dev->name,
  31474. + dev->flags);
  31475. + break;
  31476. + case NETDEV_REGISTER:
  31477. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31478. + "klips_debug:ipsec_mast_device_event: "
  31479. + "NETDEV_REGISTER dev=%s\n",
  31480. + dev->name);
  31481. + break;
  31482. + case NETDEV_CHANGEMTU:
  31483. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31484. + "klips_debug:ipsec_mast_device_event: "
  31485. + "NETDEV_CHANGEMTU dev=%s to mtu=%d\n",
  31486. + dev->name,
  31487. + dev->mtu);
  31488. + break;
  31489. + case NETDEV_CHANGEADDR:
  31490. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31491. + "klips_debug:ipsec_mast_device_event: "
  31492. + "NETDEV_CHANGEADDR dev=%s\n",
  31493. + dev->name);
  31494. + break;
  31495. + case NETDEV_GOING_DOWN:
  31496. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31497. + "klips_debug:ipsec_mast_device_event: "
  31498. + "NETDEV_GOING_DOWN dev=%s\n",
  31499. + dev->name);
  31500. + break;
  31501. + case NETDEV_CHANGENAME:
  31502. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31503. + "klips_debug:ipsec_mast_device_event: "
  31504. + "NETDEV_CHANGENAME dev=%s\n",
  31505. + dev->name);
  31506. + break;
  31507. + default:
  31508. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31509. + "klips_debug:ipsec_mast_device_event: "
  31510. + "event type %ld unrecognised for dev=%s\n",
  31511. + event,
  31512. + dev->name);
  31513. + break;
  31514. + }
  31515. + return NOTIFY_DONE;
  31516. +}
  31517. +
  31518. +/*
  31519. + * Called when an ipsec mast device is initialized.
  31520. + * The ipsec mast device structure is passed to us.
  31521. + */
  31522. +
  31523. +int
  31524. +ipsec_mast_init(struct net_device *dev)
  31525. +{
  31526. + int i;
  31527. +
  31528. + KLIPS_PRINT(debug_mast,
  31529. + "klips_debug:ipsec_mast_init: "
  31530. + "allocating %lu bytes initialising device: %s\n",
  31531. + (unsigned long) sizeof(struct ipsecpriv),
  31532. + dev->name ? dev->name : "NULL");
  31533. +
  31534. + /* Add our mast functions to the device */
  31535. + dev->open = ipsec_mast_open;
  31536. + dev->stop = ipsec_mast_close;
  31537. + dev->hard_start_xmit = ipsec_mast_start_xmit;
  31538. + dev->get_stats = ipsec_mast_get_stats;
  31539. +
  31540. + dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL);
  31541. + if (dev->priv == NULL)
  31542. + return -ENOMEM;
  31543. + memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv));
  31544. +
  31545. + for(i = 0; i < sizeof(zeroes); i++) {
  31546. + ((__u8*)(zeroes))[i] = 0;
  31547. + }
  31548. +
  31549. + dev->set_multicast_list = NULL;
  31550. + dev->do_ioctl = ipsec_mast_ioctl;
  31551. + dev->hard_header = NULL;
  31552. + dev->rebuild_header = NULL;
  31553. + dev->set_mac_address = NULL;
  31554. + dev->header_cache_update= NULL;
  31555. + dev->neigh_setup = ipsec_mast_neigh_setup_dev;
  31556. + dev->hard_header_len = 0;
  31557. + dev->mtu = 0;
  31558. + dev->addr_len = 0;
  31559. + dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */ /* ARPHRD_ETHER; */
  31560. + dev->tx_queue_len = 10; /* Small queue */
  31561. + memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */
  31562. +
  31563. + /* New-style flags. */
  31564. + dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */;
  31565. + dev_init_buffers(dev);
  31566. +
  31567. + /* We're done. Have I forgotten anything? */
  31568. + return 0;
  31569. +}
  31570. +
  31571. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31572. +/* Module specific interface (but it links with the rest of IPSEC) */
  31573. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31574. +
  31575. +int
  31576. +ipsec_mast_probe(struct net_device *dev)
  31577. +{
  31578. + ipsec_mast_init(dev);
  31579. + return 0;
  31580. +}
  31581. +
  31582. +int
  31583. +ipsec_mast_init_devices(void)
  31584. +{
  31585. + return 0;
  31586. +}
  31587. +
  31588. +/* void */
  31589. +int
  31590. +ipsec_mast_cleanup_devices(void)
  31591. +{
  31592. + int error = 0;
  31593. + int i;
  31594. + char name[10];
  31595. + struct net_device *dev_mast;
  31596. +
  31597. + for(i = 0; i < ipsec_mastdevice_count; i++) {
  31598. + sprintf(name, MAST_DEV_FORMAT, i);
  31599. + if((dev_mast = ipsec_dev_get(name)) == NULL) {
  31600. + break;
  31601. + }
  31602. + unregister_netdev(dev_mast);
  31603. + kfree(dev_mast->priv);
  31604. + dev_mast->priv=NULL;
  31605. + }
  31606. + return error;
  31607. +}
  31608. +
  31609. +/*
  31610. + * $Log: ipsec_mast.c,v $
  31611. + * Revision 1.7.2.1 2006/10/06 21:39:26 paul
  31612. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  31613. + * set. This is defined through autoconf.h which is included through the
  31614. + * linux kernel build macros.
  31615. + *
  31616. + * Revision 1.7 2005/04/29 05:10:22 mcr
  31617. + * removed from extraenous includes to make unit testing easier.
  31618. + *
  31619. + * Revision 1.6 2004/12/03 21:25:57 mcr
  31620. + * compile time fixes for running on 2.6.
  31621. + * still experimental.
  31622. + *
  31623. + * Revision 1.5 2004/08/03 18:19:08 mcr
  31624. + * in 2.6, use "net_device" instead of #define device->net_device.
  31625. + * this probably breaks 2.0 compiles.
  31626. + *
  31627. + * Revision 1.4 2004/07/10 19:11:18 mcr
  31628. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  31629. + *
  31630. + * Revision 1.3 2003/10/31 02:27:55 mcr
  31631. + * pulled up port-selector patches and sa_id elimination.
  31632. + *
  31633. + * Revision 1.2.4.1 2003/10/29 01:30:41 mcr
  31634. + * elimited "struct sa_id".
  31635. + *
  31636. + * Revision 1.2 2003/06/22 20:06:17 mcr
  31637. + * refactored mast code still had lots of ipsecX junk in it.
  31638. + *
  31639. + * Revision 1.1 2003/02/12 19:31:12 rgb
  31640. + * Refactored from ipsec_tunnel.c
  31641. + *
  31642. + */
  31643. --- /dev/null Tue Mar 11 13:02:56 2003
  31644. +++ linux/net/ipsec/ipsec_md5c.c Mon Feb 9 13:51:03 2004
  31645. @@ -0,0 +1,453 @@
  31646. +/*
  31647. + * RCSID $Id: ipsec_md5c.c,v 1.10 2005/04/15 01:25:57 mcr Exp $
  31648. + */
  31649. +
  31650. +/*
  31651. + * The rest of the code is derived from MD5C.C by RSADSI. Minor cosmetic
  31652. + * changes to accomodate it in the kernel by ji.
  31653. + */
  31654. +
  31655. +#include <asm/byteorder.h>
  31656. +#include <linux/string.h>
  31657. +
  31658. +#include "openswan/ipsec_md5h.h"
  31659. +
  31660. +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  31661. + */
  31662. +
  31663. +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  31664. +rights reserved.
  31665. +
  31666. +License to copy and use this software is granted provided that it
  31667. +is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  31668. +Algorithm" in all material mentioning or referencing this software
  31669. +or this function.
  31670. +
  31671. +License is also granted to make and use derivative works provided
  31672. +that such works are identified as "derived from the RSA Data
  31673. +Security, Inc. MD5 Message-Digest Algorithm" in all material
  31674. +mentioning or referencing the derived work.
  31675. +
  31676. +RSA Data Security, Inc. makes no representations concerning either
  31677. +the merchantability of this software or the suitability of this
  31678. +software for any particular purpose. It is provided "as is"
  31679. +without express or implied warranty of any kind.
  31680. +
  31681. +These notices must be retained in any copies of any part of this
  31682. +documentation and/or software.
  31683. + */
  31684. +
  31685. +/*
  31686. + * Additions by JI
  31687. + *
  31688. + * HAVEMEMCOPY is defined if mem* routines are available
  31689. + *
  31690. + * HAVEHTON is defined if htons() and htonl() can be used
  31691. + * for big/little endian conversions
  31692. + *
  31693. + */
  31694. +
  31695. +#define HAVEMEMCOPY
  31696. +#ifdef __LITTLE_ENDIAN
  31697. +#define LITTLENDIAN
  31698. +#endif
  31699. +#ifdef __BIG_ENDIAN
  31700. +#define BIGENDIAN
  31701. +#endif
  31702. +
  31703. +/* Constants for MD5Transform routine.
  31704. + */
  31705. +
  31706. +#define S11 7
  31707. +#define S12 12
  31708. +#define S13 17
  31709. +#define S14 22
  31710. +#define S21 5
  31711. +#define S22 9
  31712. +#define S23 14
  31713. +#define S24 20
  31714. +#define S31 4
  31715. +#define S32 11
  31716. +#define S33 16
  31717. +#define S34 23
  31718. +#define S41 6
  31719. +#define S42 10
  31720. +#define S43 15
  31721. +#define S44 21
  31722. +
  31723. +static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  31724. +
  31725. +#ifdef LITTLEENDIAN
  31726. +#define Encode MD5_memcpy
  31727. +#define Decode MD5_memcpy
  31728. +#else
  31729. +static void Encode PROTO_LIST
  31730. + ((unsigned char *, UINT4 *, unsigned int));
  31731. +static void Decode PROTO_LIST
  31732. + ((UINT4 *, unsigned char *, unsigned int));
  31733. +#endif
  31734. +
  31735. +#ifdef HAVEMEMCOPY
  31736. +/* no need to include <memory.h> here; <linux/string.h> defines these */
  31737. +#define MD5_memcpy memcpy
  31738. +#define MD5_memset memset
  31739. +#else
  31740. +#ifdef HAVEBCOPY
  31741. +#define MD5_memcpy(_a,_b,_c) bcopy((_b),(_a),(_c))
  31742. +#define MD5_memset(_a,_b,_c) bzero((_a),(_c))
  31743. +#else
  31744. +static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  31745. +static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  31746. +#endif
  31747. +#endif
  31748. +static unsigned char PADDING[64] = {
  31749. + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31750. + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31751. + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  31752. +};
  31753. +
  31754. +/* F, G, H and I are basic MD5 functions.
  31755. + */
  31756. +#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  31757. +#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  31758. +#define H(x, y, z) ((x) ^ (y) ^ (z))
  31759. +#define I(x, y, z) ((y) ^ ((x) | (~z)))
  31760. +
  31761. +/* ROTATE_LEFT rotates x left n bits.
  31762. + */
  31763. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  31764. +
  31765. +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  31766. +Rotation is separate from addition to prevent recomputation.
  31767. + */
  31768. +#define FF(a, b, c, d, x, s, ac) { \
  31769. + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31770. + (a) = ROTATE_LEFT ((a), (s)); \
  31771. + (a) += (b); \
  31772. + }
  31773. +#define GG(a, b, c, d, x, s, ac) { \
  31774. + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31775. + (a) = ROTATE_LEFT ((a), (s)); \
  31776. + (a) += (b); \
  31777. + }
  31778. +#define HH(a, b, c, d, x, s, ac) { \
  31779. + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31780. + (a) = ROTATE_LEFT ((a), (s)); \
  31781. + (a) += (b); \
  31782. + }
  31783. +#define II(a, b, c, d, x, s, ac) { \
  31784. + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31785. + (a) = ROTATE_LEFT ((a), (s)); \
  31786. + (a) += (b); \
  31787. + }
  31788. +
  31789. +/*
  31790. + * MD5 initialization. Begins an MD5 operation, writing a new context.
  31791. + */
  31792. +void osMD5Init(void *vcontext)
  31793. +{
  31794. + MD5_CTX *context = vcontext;
  31795. +
  31796. + context->count[0] = context->count[1] = 0;
  31797. + /* Load magic initialization constants.*/
  31798. + context->state[0] = 0x67452301;
  31799. + context->state[1] = 0xefcdab89;
  31800. + context->state[2] = 0x98badcfe;
  31801. + context->state[3] = 0x10325476;
  31802. +}
  31803. +
  31804. +/* MD5 block update operation. Continues an MD5 message-digest
  31805. + operation, processing another message block, and updating the
  31806. + context.
  31807. + */
  31808. +void osMD5Update (vcontext, input, inputLen)
  31809. + void *vcontext;
  31810. + unsigned char *input; /* input block */
  31811. + __u32 inputLen; /* length of input block */
  31812. +{
  31813. + MD5_CTX *context = vcontext;
  31814. + __u32 i;
  31815. + unsigned int index, partLen;
  31816. +
  31817. + /* Compute number of bytes mod 64 */
  31818. + index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  31819. +
  31820. + /* Update number of bits */
  31821. + if ((context->count[0] += ((UINT4)inputLen << 3))
  31822. + < ((UINT4)inputLen << 3))
  31823. + context->count[1]++;
  31824. + context->count[1] += ((UINT4)inputLen >> 29);
  31825. +
  31826. + partLen = 64 - index;
  31827. +
  31828. + /* Transform as many times as possible.
  31829. +*/
  31830. + if (inputLen >= partLen) {
  31831. + MD5_memcpy
  31832. + ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  31833. + MD5Transform (context->state, context->buffer);
  31834. +
  31835. + for (i = partLen; i + 63 < inputLen; i += 64)
  31836. + MD5Transform (context->state, &input[i]);
  31837. +
  31838. + index = 0;
  31839. + }
  31840. + else
  31841. + i = 0;
  31842. +
  31843. + /* Buffer remaining input */
  31844. + MD5_memcpy
  31845. + ((POINTER)&context->buffer[index], (POINTER)&input[i],
  31846. + inputLen-i);
  31847. +}
  31848. +
  31849. +/* MD5 finalization. Ends an MD5 message-digest operation, writing the
  31850. + the message digest and zeroizing the context.
  31851. + */
  31852. +void osMD5Final (digest, vcontext)
  31853. +unsigned char digest[16]; /* message digest */
  31854. +void *vcontext; /* context */
  31855. +{
  31856. + MD5_CTX *context = vcontext;
  31857. + unsigned char bits[8];
  31858. + unsigned int index, padLen;
  31859. +
  31860. + /* Save number of bits */
  31861. + Encode (bits, context->count, 8);
  31862. +
  31863. + /* Pad out to 56 mod 64.
  31864. +*/
  31865. + index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  31866. + padLen = (index < 56) ? (56 - index) : (120 - index);
  31867. + osMD5Update (context, PADDING, padLen);
  31868. +
  31869. + /* Append length (before padding) */
  31870. + osMD5Update (context, bits, 8);
  31871. +
  31872. + if (digest != NULL) /* Bill Simpson's padding */
  31873. + {
  31874. + /* store state in digest */
  31875. + Encode (digest, context->state, 16);
  31876. +
  31877. + /* Zeroize sensitive information.
  31878. + */
  31879. + MD5_memset ((POINTER)context, 0, sizeof (*context));
  31880. + }
  31881. +}
  31882. +
  31883. +/* MD5 basic transformation. Transforms state based on block.
  31884. + */
  31885. +static void MD5Transform (state, block)
  31886. +UINT4 state[4];
  31887. +unsigned char block[64];
  31888. +{
  31889. + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  31890. +
  31891. + Decode (x, block, 64);
  31892. +
  31893. + /* Round 1 */
  31894. + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  31895. + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  31896. + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  31897. + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  31898. + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  31899. + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  31900. + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  31901. + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  31902. + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  31903. + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  31904. + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  31905. + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  31906. + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  31907. + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  31908. + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  31909. + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  31910. +
  31911. + /* Round 2 */
  31912. + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  31913. + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  31914. + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  31915. + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  31916. + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  31917. + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  31918. + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  31919. + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  31920. + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  31921. + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  31922. + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  31923. + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  31924. + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  31925. + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  31926. + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  31927. + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  31928. +
  31929. + /* Round 3 */
  31930. + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  31931. + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  31932. + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  31933. + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  31934. + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  31935. + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  31936. + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  31937. + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  31938. + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  31939. + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  31940. + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  31941. + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  31942. + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  31943. + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  31944. + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  31945. + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  31946. +
  31947. + /* Round 4 */
  31948. + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  31949. + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  31950. + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  31951. + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  31952. + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  31953. + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  31954. + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  31955. + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  31956. + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  31957. + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  31958. + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  31959. + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  31960. + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  31961. + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  31962. + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  31963. + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  31964. +
  31965. + state[0] += a;
  31966. + state[1] += b;
  31967. + state[2] += c;
  31968. + state[3] += d;
  31969. +
  31970. + /* Zeroize sensitive information.
  31971. +*/
  31972. + MD5_memset ((POINTER)x, 0, sizeof (x));
  31973. +}
  31974. +
  31975. +#ifndef LITTLEENDIAN
  31976. +
  31977. +/* Encodes input (UINT4) into output (unsigned char). Assumes len is
  31978. + a multiple of 4.
  31979. + */
  31980. +static void Encode (output, input, len)
  31981. +unsigned char *output;
  31982. +UINT4 *input;
  31983. +unsigned int len;
  31984. +{
  31985. + unsigned int i, j;
  31986. +
  31987. + for (i = 0, j = 0; j < len; i++, j += 4) {
  31988. + output[j] = (unsigned char)(input[i] & 0xff);
  31989. + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  31990. + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  31991. + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  31992. + }
  31993. +}
  31994. +
  31995. +/* Decodes input (unsigned char) into output (UINT4). Assumes len is
  31996. + a multiple of 4.
  31997. + */
  31998. +static void Decode (output, input, len)
  31999. +UINT4 *output;
  32000. +unsigned char *input;
  32001. +unsigned int len;
  32002. +{
  32003. + unsigned int i, j;
  32004. +
  32005. + for (i = 0, j = 0; j < len; i++, j += 4)
  32006. + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  32007. + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  32008. +}
  32009. +
  32010. +#endif
  32011. +
  32012. +#ifndef HAVEMEMCOPY
  32013. +#ifndef HAVEBCOPY
  32014. +/* Note: Replace "for loop" with standard memcpy if possible.
  32015. + */
  32016. +
  32017. +static void MD5_memcpy (output, input, len)
  32018. +POINTER output;
  32019. +POINTER input;
  32020. +unsigned int len;
  32021. +{
  32022. + unsigned int i;
  32023. +
  32024. + for (i = 0; i < len; i++)
  32025. +
  32026. + output[i] = input[i];
  32027. +}
  32028. +
  32029. +/* Note: Replace "for loop" with standard memset if possible.
  32030. + */
  32031. +
  32032. +static void MD5_memset (output, value, len)
  32033. +POINTER output;
  32034. +int value;
  32035. +unsigned int len;
  32036. +{
  32037. + unsigned int i;
  32038. +
  32039. + for (i = 0; i < len; i++)
  32040. + ((char *)output)[i] = (char)value;
  32041. +}
  32042. +#endif
  32043. +#endif
  32044. +
  32045. +/*
  32046. + * $Log: ipsec_md5c.c,v $
  32047. + * Revision 1.10 2005/04/15 01:25:57 mcr
  32048. + * minor fix to comments.
  32049. + *
  32050. + * Revision 1.9 2004/09/08 17:21:36 ken
  32051. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  32052. + *
  32053. + * Revision 1.8 2004/04/06 02:49:26 mcr
  32054. + * pullup of algo code from alg-branch.
  32055. + *
  32056. + * Revision 1.7 2002/09/10 01:45:14 mcr
  32057. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  32058. + * the function prototypes would match, and could be placed
  32059. + * into a pointer to a function.
  32060. + *
  32061. + * Revision 1.6 2002/04/24 07:55:32 mcr
  32062. + * #include patches and Makefiles for post-reorg compilation.
  32063. + *
  32064. + * Revision 1.5 2002/04/24 07:36:28 mcr
  32065. + * Moved from ./klips/net/ipsec/ipsec_md5c.c,v
  32066. + *
  32067. + * Revision 1.4 1999/12/13 13:59:12 rgb
  32068. + * Quick fix to argument size to Update bugs.
  32069. + *
  32070. + * Revision 1.3 1999/05/21 18:09:28 henry
  32071. + * unnecessary <memory.h> include causes trouble in 2.2
  32072. + *
  32073. + * Revision 1.2 1999/04/06 04:54:26 rgb
  32074. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  32075. + * patch shell fixes.
  32076. + *
  32077. + * Revision 1.1 1998/06/18 21:27:48 henry
  32078. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  32079. + * kernel-build scripts happier in the presence of symlinks
  32080. + *
  32081. + * Revision 1.2 1998/04/23 20:54:02 rgb
  32082. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  32083. + * verified.
  32084. + *
  32085. + * Revision 1.1 1998/04/09 03:06:08 henry
  32086. + * sources moved up from linux/net/ipsec
  32087. + *
  32088. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  32089. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  32090. + *
  32091. + * Revision 0.3 1996/11/20 14:48:53 ji
  32092. + * Release update only.
  32093. + *
  32094. + * Revision 0.2 1996/11/02 00:18:33 ji
  32095. + * First limited release.
  32096. + *
  32097. + *
  32098. + */
  32099. --- /dev/null Tue Mar 11 13:02:56 2003
  32100. +++ linux/net/ipsec/ipsec_proc.c Mon Feb 9 13:51:03 2004
  32101. @@ -0,0 +1,1186 @@
  32102. +/*
  32103. + * @(#) /proc file system interface code.
  32104. + *
  32105. + * Copyright (C) 1996, 1997 John Ioannidis.
  32106. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  32107. + * 2001 Michael Richardson <mcr@freeswan.org>
  32108. + *
  32109. + * This program is free software; you can redistribute it and/or modify it
  32110. + * under the terms of the GNU General Public License as published by the
  32111. + * Free Software Foundation; either version 2 of the License, or (at your
  32112. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  32113. + *
  32114. + * This program is distributed in the hope that it will be useful, but
  32115. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32116. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  32117. + * for more details.
  32118. + *
  32119. + * Split out from ipsec_init.c version 1.70.
  32120. + */
  32121. +
  32122. +char ipsec_proc_c_version[] = "RCSID $Id: ipsec_proc.c,v 1.39.2.4 2006/11/15 22:21:39 paul Exp $";
  32123. +
  32124. +
  32125. +#ifndef AUTOCONF_INCLUDED
  32126. +#include <linux/config.h>
  32127. +#endif
  32128. +#include <linux/version.h>
  32129. +#define __NO_VERSION__
  32130. +#include <linux/module.h>
  32131. +#include <linux/kernel.h> /* printk() */
  32132. +
  32133. +#include "openswan/ipsec_kversion.h"
  32134. +#include "openswan/ipsec_param.h"
  32135. +
  32136. +#ifdef MALLOC_SLAB
  32137. +# include <linux/slab.h> /* kmalloc() */
  32138. +#else /* MALLOC_SLAB */
  32139. +# include <linux/malloc.h> /* kmalloc() */
  32140. +#endif /* MALLOC_SLAB */
  32141. +#include <linux/errno.h> /* error codes */
  32142. +#include <linux/types.h> /* size_t */
  32143. +#include <linux/interrupt.h> /* mark_bh */
  32144. +
  32145. +#include <linux/netdevice.h> /* struct device, and other headers */
  32146. +#include <linux/etherdevice.h> /* eth_type_trans */
  32147. +#include <linux/ip.h> /* struct iphdr */
  32148. +#include <linux/in.h> /* struct sockaddr_in */
  32149. +#include <linux/skbuff.h>
  32150. +#include <asm/uaccess.h> /* copy_from_user */
  32151. +#include <openswan.h>
  32152. +#ifdef SPINLOCK
  32153. +#ifdef SPINLOCK_23
  32154. +#include <linux/spinlock.h> /* *lock* */
  32155. +#else /* SPINLOCK_23 */
  32156. +#include <asm/spinlock.h> /* *lock* */
  32157. +#endif /* SPINLOCK_23 */
  32158. +#endif /* SPINLOCK */
  32159. +
  32160. +#include <net/ip.h>
  32161. +#ifdef CONFIG_PROC_FS
  32162. +#include <linux/proc_fs.h>
  32163. +#endif /* CONFIG_PROC_FS */
  32164. +#ifdef NETLINK_SOCK
  32165. +#include <linux/netlink.h>
  32166. +#else
  32167. +#include <net/netlink.h>
  32168. +#endif
  32169. +
  32170. +#include "openswan/radij.h"
  32171. +
  32172. +#include "openswan/ipsec_life.h"
  32173. +#include "openswan/ipsec_stats.h"
  32174. +#include "openswan/ipsec_sa.h"
  32175. +
  32176. +#include "openswan/ipsec_encap.h"
  32177. +#include "openswan/ipsec_radij.h"
  32178. +#include "openswan/ipsec_xform.h"
  32179. +#include "openswan/ipsec_tunnel.h"
  32180. +#include "openswan/ipsec_xmit.h"
  32181. +
  32182. +#include "openswan/ipsec_rcv.h"
  32183. +#include "openswan/ipsec_ah.h"
  32184. +#include "openswan/ipsec_esp.h"
  32185. +#include "openswan/ipsec_kern24.h"
  32186. +
  32187. +#ifdef CONFIG_KLIPS_IPCOMP
  32188. +#include "openswan/ipcomp.h"
  32189. +#endif /* CONFIG_KLIPS_IPCOMP */
  32190. +
  32191. +#include "openswan/ipsec_proto.h"
  32192. +
  32193. +#include <pfkeyv2.h>
  32194. +#include <pfkey.h>
  32195. +
  32196. +#ifdef CONFIG_PROC_FS
  32197. +
  32198. +#ifdef IPSEC_PROC_SUBDIRS
  32199. +static struct proc_dir_entry *proc_net_ipsec_dir = NULL;
  32200. +static struct proc_dir_entry *proc_eroute_dir = NULL;
  32201. +static struct proc_dir_entry *proc_spi_dir = NULL;
  32202. +static struct proc_dir_entry *proc_spigrp_dir = NULL;
  32203. +static struct proc_dir_entry *proc_birth_dir = NULL;
  32204. +static struct proc_dir_entry *proc_stats_dir = NULL;
  32205. +#endif
  32206. +
  32207. +struct ipsec_birth_reply ipsec_ipv4_birth_packet;
  32208. +struct ipsec_birth_reply ipsec_ipv6_birth_packet;
  32209. +
  32210. +#ifdef CONFIG_KLIPS_DEBUG
  32211. +int debug_esp = 0;
  32212. +int debug_ah = 0;
  32213. +#endif /* CONFIG_KLIPS_DEBUG */
  32214. +
  32215. +#define DECREMENT_UNSIGNED(X, amount) ((amount < (X)) ? (X)-amount : 0)
  32216. +
  32217. +extern int ipsec_xform_get_info(char *buffer, char **start,
  32218. + off_t offset, int length IPSEC_PROC_LAST_ARG);
  32219. +
  32220. +
  32221. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32222. +int
  32223. +ipsec_eroute_get_info(char *buffer,
  32224. + char **start,
  32225. + off_t offset,
  32226. + int length IPSEC_PROC_LAST_ARG)
  32227. +{
  32228. + struct wsbuf w = {buffer, length, offset, 0, 0};
  32229. +
  32230. +#ifdef CONFIG_KLIPS_DEBUG
  32231. + if (debug_radij & DB_RJ_DUMPTREES)
  32232. + rj_dumptrees(); /* XXXXXXXXX */
  32233. +#endif /* CONFIG_KLIPS_DEBUG */
  32234. +
  32235. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32236. + "klips_debug:ipsec_eroute_get_info: "
  32237. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32238. + buffer,
  32239. + *start,
  32240. + (int)offset,
  32241. + length);
  32242. +
  32243. + spin_lock_bh(&eroute_lock);
  32244. +
  32245. + rj_walktree(rnh, ipsec_rj_walker_procprint, &w);
  32246. +/* rj_walktree(mask_rjhead, ipsec_rj_walker_procprint, &w); */
  32247. +
  32248. + spin_unlock_bh(&eroute_lock);
  32249. +
  32250. + *start = buffer + (offset - w.begin); /* Start of wanted data */
  32251. + return w.len - (offset - w.begin);
  32252. +}
  32253. +
  32254. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32255. +int
  32256. +ipsec_spi_get_info(char *buffer,
  32257. + char **start,
  32258. + off_t offset,
  32259. + int length IPSEC_PROC_LAST_ARG)
  32260. +{
  32261. + const int max_content = length > 0? length-1 : 0;
  32262. + int len = 0;
  32263. + off_t begin = 0;
  32264. + int i;
  32265. + struct ipsec_sa *sa_p;
  32266. + char sa[SATOT_BUF];
  32267. + char buf_s[SUBNETTOA_BUF];
  32268. + char buf_d[SUBNETTOA_BUF];
  32269. + size_t sa_len;
  32270. +
  32271. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32272. + "klips_debug:ipsec_spi_get_info: "
  32273. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32274. + buffer,
  32275. + *start,
  32276. + (int)offset,
  32277. + length);
  32278. +
  32279. + spin_lock_bh(&tdb_lock);
  32280. +
  32281. + for (i = 0; i < SADB_HASHMOD; i++) {
  32282. + for (sa_p = ipsec_sadb_hash[i];
  32283. + sa_p;
  32284. + sa_p = sa_p->ips_hnext) {
  32285. + atomic_inc(&sa_p->ips_refcount);
  32286. + sa_len = satot(&sa_p->ips_said, 'x', sa, sizeof(sa));
  32287. + len += ipsec_snprintf(buffer+len, length-len, "%s ",
  32288. + sa_len ? sa : " (error)");
  32289. +
  32290. + len += ipsec_snprintf(buffer+len, length-len, "%s%s%s",
  32291. + IPS_XFORM_NAME(sa_p));
  32292. +
  32293. + len += ipsec_snprintf(buffer+len, length-len, ": dir=%s",
  32294. + (sa_p->ips_flags & EMT_INBOUND) ?
  32295. + "in " : "out");
  32296. +
  32297. + if(sa_p->ips_addr_s) {
  32298. + addrtoa(((struct sockaddr_in*)(sa_p->ips_addr_s))->sin_addr,
  32299. + 0, buf_s, sizeof(buf_s));
  32300. + len += ipsec_snprintf(buffer+len, length-len, " src=%s",
  32301. + buf_s);
  32302. + }
  32303. +
  32304. + if((sa_p->ips_said.proto == IPPROTO_IPIP)
  32305. + && (sa_p->ips_flags & SADB_X_SAFLAGS_INFLOW)) {
  32306. + subnettoa(sa_p->ips_flow_s.u.v4.sin_addr,
  32307. + sa_p->ips_mask_s.u.v4.sin_addr,
  32308. + 0,
  32309. + buf_s,
  32310. + sizeof(buf_s));
  32311. +
  32312. + subnettoa(sa_p->ips_flow_d.u.v4.sin_addr,
  32313. + sa_p->ips_mask_d.u.v4.sin_addr,
  32314. + 0,
  32315. + buf_d,
  32316. + sizeof(buf_d));
  32317. +
  32318. + len += ipsec_snprintf(buffer+len, length-len, " policy=%s->%s",
  32319. + buf_s, buf_d);
  32320. + }
  32321. +
  32322. + if(sa_p->ips_iv_bits) {
  32323. + int j;
  32324. + len += ipsec_snprintf(buffer+len, length-len, " iv_bits=%dbits iv=0x",
  32325. + sa_p->ips_iv_bits);
  32326. +
  32327. + for(j = 0; j < sa_p->ips_iv_bits / 8; j++) {
  32328. + len += ipsec_snprintf(buffer+len, length-len, "%02x",
  32329. + (__u32)((__u8*)(sa_p->ips_iv))[j]);
  32330. + }
  32331. + }
  32332. +
  32333. + if(sa_p->ips_encalg || sa_p->ips_authalg) {
  32334. + if(sa_p->ips_replaywin) {
  32335. + len += ipsec_snprintf(buffer+len, length-len, " ooowin=%d",
  32336. + sa_p->ips_replaywin);
  32337. + }
  32338. + if(sa_p->ips_errs.ips_replaywin_errs) {
  32339. + len += ipsec_snprintf(buffer+len, length-len, " ooo_errs=%d",
  32340. + sa_p->ips_errs.ips_replaywin_errs);
  32341. + }
  32342. + if(sa_p->ips_replaywin_lastseq) {
  32343. + len += ipsec_snprintf(buffer+len, length-len, " seq=%d",
  32344. + sa_p->ips_replaywin_lastseq);
  32345. + }
  32346. + if(sa_p->ips_replaywin_bitmap) {
  32347. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32348. + len += ipsec_snprintf(buffer+len, length-len, " bit=0x%Lx",
  32349. + sa_p->ips_replaywin_bitmap);
  32350. +#else
  32351. + len += ipsec_snprintf(buffer+len, length-len, " bit=0x%x%08x",
  32352. + (__u32)(sa_p->ips_replaywin_bitmap >> 32),
  32353. + (__u32)sa_p->ips_replaywin_bitmap);
  32354. +#endif
  32355. + }
  32356. + if(sa_p->ips_replaywin_maxdiff) {
  32357. + len += ipsec_snprintf(buffer+len, length-len, " max_seq_diff=%d",
  32358. + sa_p->ips_replaywin_maxdiff);
  32359. + }
  32360. + }
  32361. + if(sa_p->ips_flags & ~EMT_INBOUND) {
  32362. + len += ipsec_snprintf(buffer+len, length-len, " flags=0x%x",
  32363. + sa_p->ips_flags & ~EMT_INBOUND);
  32364. + len += ipsec_snprintf(buffer+len, length-len, "<");
  32365. + /* flag printing goes here */
  32366. + len += ipsec_snprintf(buffer+len, length-len, ">");
  32367. + }
  32368. + if(sa_p->ips_auth_bits) {
  32369. + len += ipsec_snprintf(buffer+len, length-len, " alen=%d",
  32370. + sa_p->ips_auth_bits);
  32371. + }
  32372. + if(sa_p->ips_key_bits_a) {
  32373. + len += ipsec_snprintf(buffer+len, length-len, " aklen=%d",
  32374. + sa_p->ips_key_bits_a);
  32375. + }
  32376. + if(sa_p->ips_errs.ips_auth_errs) {
  32377. + len += ipsec_snprintf(buffer+len, length-len, " auth_errs=%d",
  32378. + sa_p->ips_errs.ips_auth_errs);
  32379. + }
  32380. + if(sa_p->ips_key_bits_e) {
  32381. + len += ipsec_snprintf(buffer+len, length-len, " eklen=%d",
  32382. + sa_p->ips_key_bits_e);
  32383. + }
  32384. + if(sa_p->ips_errs.ips_encsize_errs) {
  32385. + len += ipsec_snprintf(buffer+len, length-len, " encr_size_errs=%d",
  32386. + sa_p->ips_errs.ips_encsize_errs);
  32387. + }
  32388. + if(sa_p->ips_errs.ips_encpad_errs) {
  32389. + len += ipsec_snprintf(buffer+len, length-len, " encr_pad_errs=%d",
  32390. + sa_p->ips_errs.ips_encpad_errs);
  32391. + }
  32392. +
  32393. + len += ipsec_snprintf(buffer+len, length-len, " life(c,s,h)=");
  32394. +
  32395. + len += ipsec_lifetime_format(buffer + len,
  32396. + length - len,
  32397. + "alloc",
  32398. + ipsec_life_countbased,
  32399. + &sa_p->ips_life.ipl_allocations);
  32400. +
  32401. + len += ipsec_lifetime_format(buffer + len,
  32402. + length - len,
  32403. + "bytes",
  32404. + ipsec_life_countbased,
  32405. + &sa_p->ips_life.ipl_bytes);
  32406. +
  32407. + len += ipsec_lifetime_format(buffer + len,
  32408. + length - len,
  32409. + "addtime",
  32410. + ipsec_life_timebased,
  32411. + &sa_p->ips_life.ipl_addtime);
  32412. +
  32413. + len += ipsec_lifetime_format(buffer + len,
  32414. + length - len,
  32415. + "usetime",
  32416. + ipsec_life_timebased,
  32417. + &sa_p->ips_life.ipl_usetime);
  32418. +
  32419. + len += ipsec_lifetime_format(buffer + len,
  32420. + length - len,
  32421. + "packets",
  32422. + ipsec_life_countbased,
  32423. + &sa_p->ips_life.ipl_packets);
  32424. +
  32425. + if(sa_p->ips_life.ipl_usetime.ipl_last) { /* XXX-MCR should be last? */
  32426. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32427. + len += ipsec_snprintf(buffer+len, length-len, " idle=%Ld",
  32428. + jiffies / HZ - sa_p->ips_life.ipl_usetime.ipl_last);
  32429. +#else
  32430. + len += ipsec_snprintf(buffer+len, length-len, " idle=%lu",
  32431. + jiffies / HZ - (unsigned long)sa_p->ips_life.ipl_usetime.ipl_last);
  32432. +#endif
  32433. + }
  32434. +
  32435. +#ifdef CONFIG_KLIPS_IPCOMP
  32436. + if(sa_p->ips_said.proto == IPPROTO_COMP &&
  32437. + (sa_p->ips_comp_ratio_dbytes ||
  32438. + sa_p->ips_comp_ratio_cbytes)) {
  32439. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32440. + len += ipsec_snprintf(buffer+len, length-len, " ratio=%Ld:%Ld",
  32441. + sa_p->ips_comp_ratio_dbytes,
  32442. + sa_p->ips_comp_ratio_cbytes);
  32443. +#else
  32444. + len += ipsec_snprintf(buffer+len, length-len, " ratio=%lu:%lu",
  32445. + (unsigned long)sa_p->ips_comp_ratio_dbytes,
  32446. + (unsigned long)sa_p->ips_comp_ratio_cbytes);
  32447. +#endif
  32448. + }
  32449. +#endif /* CONFIG_KLIPS_IPCOMP */
  32450. +
  32451. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32452. + {
  32453. + char *natttype_name;
  32454. +
  32455. + switch(sa_p->ips_natt_type)
  32456. + {
  32457. + case 0:
  32458. + natttype_name="none";
  32459. + break;
  32460. + case ESPINUDP_WITH_NON_IKE:
  32461. + natttype_name="nonike";
  32462. + break;
  32463. + case ESPINUDP_WITH_NON_ESP:
  32464. + natttype_name="nonesp";
  32465. + break;
  32466. + default:
  32467. + natttype_name = "unknown";
  32468. + break;
  32469. + }
  32470. +
  32471. + len += ipsec_snprintf(buffer + len, length-len, " natencap=%s",
  32472. + natttype_name);
  32473. +
  32474. + len += ipsec_snprintf(buffer + len, length-len, " natsport=%d",
  32475. + sa_p->ips_natt_sport);
  32476. +
  32477. + len += ipsec_snprintf(buffer + len,length-len, " natdport=%d",
  32478. + sa_p->ips_natt_dport);
  32479. + }
  32480. +#else
  32481. + len += ipsec_snprintf(buffer + len, length-len, " natencap=na");
  32482. +#endif /* CONFIG_IPSEC_NAT_TRAVERSAL */
  32483. +
  32484. + len += ipsec_snprintf(buffer + len,length-len, " refcount=%d",
  32485. + atomic_read(&sa_p->ips_refcount));
  32486. +
  32487. + len += ipsec_snprintf(buffer+len, length-len, " ref=%d",
  32488. + sa_p->ips_ref);
  32489. +#ifdef CONFIG_KLIPS_DEBUG
  32490. + if(debug_xform) {
  32491. + len += ipsec_snprintf(buffer+len, length-len, " reftable=%lu refentry=%lu",
  32492. + (unsigned long)IPsecSAref2table(sa_p->ips_ref),
  32493. + (unsigned long)IPsecSAref2entry(sa_p->ips_ref));
  32494. + }
  32495. +#endif /* CONFIG_KLIPS_DEBUG */
  32496. +
  32497. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32498. +
  32499. + atomic_dec(&sa_p->ips_refcount);
  32500. +
  32501. + if (len >= max_content) {
  32502. + /* we've done all that can fit -- stop loops */
  32503. + len = max_content; /* truncate crap */
  32504. + goto done_spi_i;
  32505. + } else {
  32506. + const off_t pos = begin + len; /* file position of end of what we've generated */
  32507. +
  32508. + if (pos <= offset) {
  32509. + /* all is before first interesting character:
  32510. + * discard, but note where we are.
  32511. + */
  32512. + len = 0;
  32513. + begin = pos;
  32514. + }
  32515. + }
  32516. + }
  32517. + }
  32518. +
  32519. +done_spi_i:
  32520. + spin_unlock_bh(&tdb_lock);
  32521. +
  32522. + *start = buffer + (offset - begin); /* Start of wanted data */
  32523. + return len - (offset - begin);
  32524. +}
  32525. +
  32526. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32527. +int
  32528. +ipsec_spigrp_get_info(char *buffer,
  32529. + char **start,
  32530. + off_t offset,
  32531. + int length IPSEC_PROC_LAST_ARG)
  32532. +{
  32533. + /* Limit of useful snprintf output */
  32534. + const int max_content = length > 0? length-1 : 0;
  32535. +
  32536. + int len = 0;
  32537. + off_t begin = 0;
  32538. + int i;
  32539. + struct ipsec_sa *sa_p, *sa_p2;
  32540. + char sa[SATOT_BUF];
  32541. + size_t sa_len;
  32542. +
  32543. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32544. + "klips_debug:ipsec_spigrp_get_info: "
  32545. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32546. + buffer,
  32547. + *start,
  32548. + (int)offset,
  32549. + length);
  32550. +
  32551. + spin_lock_bh(&tdb_lock);
  32552. +
  32553. + for (i = 0; i < SADB_HASHMOD; i++) {
  32554. + for (sa_p = ipsec_sadb_hash[i];
  32555. + sa_p != NULL;
  32556. + sa_p = sa_p->ips_hnext)
  32557. + {
  32558. + atomic_inc(&sa_p->ips_refcount);
  32559. + if(sa_p->ips_inext == NULL) {
  32560. + sa_p2 = sa_p;
  32561. + while(sa_p2 != NULL) {
  32562. + atomic_inc(&sa_p2->ips_refcount);
  32563. + sa_len = satot(&sa_p2->ips_said,
  32564. + 'x', sa, sizeof(sa));
  32565. +
  32566. + len += ipsec_snprintf(buffer+len, length-len, "%s ",
  32567. + sa_len ? sa : " (error)");
  32568. + atomic_dec(&sa_p2->ips_refcount);
  32569. + sa_p2 = sa_p2->ips_onext;
  32570. + }
  32571. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32572. + }
  32573. +
  32574. + atomic_dec(&sa_p->ips_refcount);
  32575. +
  32576. + if (len >= max_content) {
  32577. + /* we've done all that can fit -- stop loops */
  32578. + len = max_content; /* truncate crap */
  32579. + goto done_spigrp_i;
  32580. + } else {
  32581. + const off_t pos = begin + len;
  32582. +
  32583. + if (pos <= offset) {
  32584. + /* all is before first interesting character:
  32585. + * discard, but note where we are.
  32586. + */
  32587. + len = 0;
  32588. + begin = pos;
  32589. + }
  32590. + }
  32591. + }
  32592. + }
  32593. +
  32594. +done_spigrp_i:
  32595. + spin_unlock_bh(&tdb_lock);
  32596. +
  32597. + *start = buffer + (offset - begin); /* Start of wanted data */
  32598. + return len - (offset - begin);
  32599. +}
  32600. +
  32601. +
  32602. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32603. +int
  32604. +ipsec_tncfg_get_info(char *buffer,
  32605. + char **start,
  32606. + off_t offset,
  32607. + int length IPSEC_PROC_LAST_ARG)
  32608. +{
  32609. + /* limit of useful snprintf output */
  32610. + const int max_content = length > 0? length-1 : 0;
  32611. + int len = 0;
  32612. + off_t begin = 0;
  32613. + int i;
  32614. + char name[9];
  32615. + struct net_device *dev, *privdev;
  32616. + struct ipsecpriv *priv;
  32617. +
  32618. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32619. + "klips_debug:ipsec_tncfg_get_info: "
  32620. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32621. + buffer,
  32622. + *start,
  32623. + (int)offset,
  32624. + length);
  32625. +
  32626. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  32627. + ipsec_snprintf(name, (ssize_t) sizeof(name), IPSEC_DEV_FORMAT, i);
  32628. + dev = __ipsec_dev_get(name);
  32629. + if(dev) {
  32630. + priv = (struct ipsecpriv *)(dev->priv);
  32631. + len += ipsec_snprintf(buffer+len, length-len, "%s",
  32632. + dev->name);
  32633. + if(priv) {
  32634. + privdev = (struct net_device *)(priv->dev);
  32635. + len += ipsec_snprintf(buffer+len, length-len, " -> %s",
  32636. + privdev ? privdev->name : "NULL");
  32637. + len += ipsec_snprintf(buffer+len, length-len, " mtu=%d(%d) -> %d",
  32638. + dev->mtu,
  32639. + priv->mtu,
  32640. + privdev ? privdev->mtu : 0);
  32641. + } else {
  32642. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32643. + "klips_debug:ipsec_tncfg_get_info: device '%s' has no private data space!\n",
  32644. + dev->name);
  32645. + }
  32646. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32647. +
  32648. + if (len >= max_content) {
  32649. + /* we've done all that can fit -- stop loop */
  32650. + len = max_content; /* truncate crap */
  32651. + break;
  32652. + } else {
  32653. + const off_t pos = begin + len;
  32654. + if (pos <= offset) {
  32655. + len = 0;
  32656. + begin = pos;
  32657. + }
  32658. + }
  32659. + }
  32660. + }
  32661. + *start = buffer + (offset - begin); /* Start of wanted data */
  32662. + len -= (offset - begin); /* Start slop */
  32663. + if (len > length)
  32664. + len = length;
  32665. + return len;
  32666. +}
  32667. +
  32668. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32669. +int
  32670. +ipsec_version_get_info(char *buffer,
  32671. + char **start,
  32672. + off_t offset,
  32673. + int length IPSEC_PROC_LAST_ARG)
  32674. +{
  32675. + int len = 0;
  32676. + off_t begin = 0;
  32677. +
  32678. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32679. + "klips_debug:ipsec_version_get_info: "
  32680. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32681. + buffer,
  32682. + *start,
  32683. + (int)offset,
  32684. + length);
  32685. +
  32686. + len += ipsec_snprintf(buffer + len,length-len, "Openswan version: %s\n",
  32687. + ipsec_version_code());
  32688. +#if 0
  32689. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32690. + "klips_debug:ipsec_version_get_info: "
  32691. + "ipsec_init version: %s\n",
  32692. + ipsec_init_c_version);
  32693. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32694. + "klips_debug:ipsec_version_get_info: "
  32695. + "ipsec_tunnel version: %s\n",
  32696. + ipsec_tunnel_c_version);
  32697. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32698. + "klips_debug:ipsec_version_get_info: "
  32699. + "ipsec_netlink version: %s\n",
  32700. + ipsec_netlink_c_version);
  32701. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32702. + "klips_debug:ipsec_version_get_info: "
  32703. + "radij_c_version: %s\n",
  32704. + radij_c_version);
  32705. +#endif
  32706. +
  32707. +
  32708. + *start = buffer + (offset - begin); /* Start of wanted data */
  32709. + len -= (offset - begin); /* Start slop */
  32710. + if (len > length)
  32711. + len = length;
  32712. + return len;
  32713. +}
  32714. +
  32715. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32716. +unsigned int natt_available = 1;
  32717. +#else
  32718. +unsigned int natt_available = 0;
  32719. +#endif
  32720. +module_param(natt_available, int, 0444);
  32721. +
  32722. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32723. +int
  32724. +ipsec_natt_get_info(char *buffer,
  32725. + char **start,
  32726. + off_t offset,
  32727. + int length IPSEC_PROC_LAST_ARG)
  32728. +{
  32729. + int len = 0;
  32730. + off_t begin = 0;
  32731. +
  32732. + len += ipsec_snprintf(buffer + len,
  32733. + length-len, "%d\n",
  32734. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32735. + 1
  32736. +#else
  32737. + 0
  32738. +#endif
  32739. + );
  32740. +
  32741. + *start = buffer + (offset - begin); /* Start of wanted data */
  32742. + len -= (offset - begin); /* Start slop */
  32743. + if (len > length)
  32744. + len = length;
  32745. + return len;
  32746. +}
  32747. +
  32748. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32749. +int
  32750. +ipsec_birth_info(char *page,
  32751. + char **start,
  32752. + off_t offset,
  32753. + int count,
  32754. + int *eof,
  32755. + void *data)
  32756. +{
  32757. + struct ipsec_birth_reply *ibr = (struct ipsec_birth_reply *)data;
  32758. + int len;
  32759. +
  32760. + if(offset >= ibr->packet_template_len) {
  32761. + if(eof) {
  32762. + *eof=1;
  32763. + }
  32764. + return 0;
  32765. + }
  32766. +
  32767. + len = ibr->packet_template_len;
  32768. + len -= offset;
  32769. + if (len > count)
  32770. + len = count;
  32771. +
  32772. + memcpy(page + offset, ibr->packet_template+offset, len);
  32773. +
  32774. + return len;
  32775. +}
  32776. +
  32777. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32778. +int
  32779. +ipsec_birth_set(struct file *file, const char *buffer,
  32780. + unsigned long count, void *data)
  32781. +{
  32782. + struct ipsec_birth_reply *ibr = (struct ipsec_birth_reply *)data;
  32783. + int len;
  32784. +
  32785. + KLIPS_INC_USE;
  32786. + if(count > IPSEC_BIRTH_TEMPLATE_MAXLEN) {
  32787. + len = IPSEC_BIRTH_TEMPLATE_MAXLEN;
  32788. + } else {
  32789. + len = count;
  32790. + }
  32791. +
  32792. + if(copy_from_user(ibr->packet_template, buffer, len)) {
  32793. + KLIPS_DEC_USE;
  32794. + return -EFAULT;
  32795. + }
  32796. + ibr->packet_template_len = len;
  32797. +
  32798. + KLIPS_DEC_USE;
  32799. +
  32800. + return len;
  32801. +}
  32802. +
  32803. +
  32804. +#ifdef CONFIG_KLIPS_DEBUG
  32805. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32806. +int
  32807. +ipsec_klipsdebug_get_info(char *buffer,
  32808. + char **start,
  32809. + off_t offset,
  32810. + int length IPSEC_PROC_LAST_ARG)
  32811. +{
  32812. + int len = 0;
  32813. + off_t begin = 0;
  32814. +
  32815. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32816. + "klips_debug:ipsec_klipsdebug_get_info: "
  32817. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32818. + buffer,
  32819. + *start,
  32820. + (int)offset,
  32821. + length);
  32822. +
  32823. + len += ipsec_snprintf(buffer+len, length-len, "debug_tunnel=%08x.\n", debug_tunnel);
  32824. + len += ipsec_snprintf(buffer+len, length-len, "debug_xform=%08x.\n", debug_xform);
  32825. + len += ipsec_snprintf(buffer+len, length-len, "debug_eroute=%08x.\n", debug_eroute);
  32826. + len += ipsec_snprintf(buffer+len, length-len, "debug_spi=%08x.\n", debug_spi);
  32827. + len += ipsec_snprintf(buffer+len, length-len, "debug_radij=%08x.\n", debug_radij);
  32828. + len += ipsec_snprintf(buffer+len, length-len, "debug_esp=%08x.\n", debug_esp);
  32829. + len += ipsec_snprintf(buffer+len, length-len, "debug_ah=%08x.\n", debug_ah);
  32830. + len += ipsec_snprintf(buffer+len, length-len, "debug_rcv=%08x.\n", debug_rcv);
  32831. + len += ipsec_snprintf(buffer+len, length-len, "debug_pfkey=%08x.\n", debug_pfkey);
  32832. +
  32833. + *start = buffer + (offset - begin); /* Start of wanted data */
  32834. + len -= (offset - begin); /* Start slop */
  32835. + if (len > length)
  32836. + len = length;
  32837. + return len;
  32838. +}
  32839. +#endif /* CONFIG_KLIPS_DEBUG */
  32840. +
  32841. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32842. +int
  32843. +ipsec_stats_get_int_info(char *buffer,
  32844. + char **start,
  32845. + off_t offset,
  32846. + int length,
  32847. + int *eof,
  32848. + void *data)
  32849. +{
  32850. +
  32851. + const int max_content = length > 0? length-1 : 0;
  32852. + int len = 0;
  32853. + int *thing;
  32854. +
  32855. + thing = (int *)data;
  32856. +
  32857. + len = ipsec_snprintf(buffer+len, length-len, "%08x\n", *thing);
  32858. +
  32859. + if (len >= max_content)
  32860. + len = max_content; /* truncate crap */
  32861. +
  32862. + *start = buffer + offset; /* Start of wanted data */
  32863. + return len > offset? len - offset : 0;
  32864. +
  32865. +}
  32866. +
  32867. +#ifndef PROC_FS_2325
  32868. +struct proc_dir_entry ipsec_eroute =
  32869. +{
  32870. + 0,
  32871. + 12, "ipsec_eroute",
  32872. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32873. + &proc_net_inode_operations,
  32874. + ipsec_eroute_get_info,
  32875. + NULL, NULL, NULL, NULL, NULL
  32876. +};
  32877. +
  32878. +struct proc_dir_entry ipsec_spi =
  32879. +{
  32880. + 0,
  32881. + 9, "ipsec_spi",
  32882. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32883. + &proc_net_inode_operations,
  32884. + ipsec_spi_get_info,
  32885. + NULL, NULL, NULL, NULL, NULL
  32886. +};
  32887. +
  32888. +struct proc_dir_entry ipsec_spigrp =
  32889. +{
  32890. + 0,
  32891. + 12, "ipsec_spigrp",
  32892. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32893. + &proc_net_inode_operations,
  32894. + ipsec_spigrp_get_info,
  32895. + NULL, NULL, NULL, NULL, NULL
  32896. +};
  32897. +
  32898. +struct proc_dir_entry ipsec_tncfg =
  32899. +{
  32900. + 0,
  32901. + 11, "ipsec_tncfg",
  32902. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32903. + &proc_net_inode_operations,
  32904. + ipsec_tncfg_get_info,
  32905. + NULL, NULL, NULL, NULL, NULL
  32906. +};
  32907. +
  32908. +struct proc_dir_entry ipsec_version =
  32909. +{
  32910. + 0,
  32911. + 13, "ipsec_version",
  32912. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32913. + &proc_net_inode_operations,
  32914. + ipsec_version_get_info,
  32915. + NULL, NULL, NULL, NULL, NULL
  32916. +};
  32917. +
  32918. +#ifdef CONFIG_KLIPS_DEBUG
  32919. +struct proc_dir_entry ipsec_klipsdebug =
  32920. +{
  32921. + 0,
  32922. + 16, "ipsec_klipsdebug",
  32923. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32924. + &proc_net_inode_operations,
  32925. + ipsec_klipsdebug_get_info,
  32926. + NULL, NULL, NULL, NULL, NULL
  32927. +};
  32928. +#endif /* CONFIG_KLIPS_DEBUG */
  32929. +#endif /* !PROC_FS_2325 */
  32930. +#endif /* CONFIG_PROC_FS */
  32931. +
  32932. +#if defined(PROC_FS_2325)
  32933. +struct ipsec_proc_list {
  32934. + char *name;
  32935. + struct proc_dir_entry **parent;
  32936. + struct proc_dir_entry **dir;
  32937. + read_proc_t *readthing;
  32938. + write_proc_t *writething;
  32939. + void *data;
  32940. +};
  32941. +static struct ipsec_proc_list proc_items[]={
  32942. +#ifdef CONFIG_KLIPS_DEBUG
  32943. + {"klipsdebug", &proc_net_ipsec_dir, NULL, ipsec_klipsdebug_get_info, NULL, NULL},
  32944. +#endif
  32945. + {"eroute", &proc_net_ipsec_dir, &proc_eroute_dir, NULL, NULL, NULL},
  32946. + {"all", &proc_eroute_dir, NULL, ipsec_eroute_get_info, NULL, NULL},
  32947. + {"spi", &proc_net_ipsec_dir, &proc_spi_dir, NULL, NULL, NULL},
  32948. + {"all", &proc_spi_dir, NULL, ipsec_spi_get_info, NULL, NULL},
  32949. + {"spigrp", &proc_net_ipsec_dir, &proc_spigrp_dir, NULL, NULL, NULL},
  32950. + {"all", &proc_spigrp_dir, NULL, ipsec_spigrp_get_info, NULL, NULL},
  32951. + {"birth", &proc_net_ipsec_dir, &proc_birth_dir, NULL, NULL, NULL},
  32952. + {"ipv4", &proc_birth_dir, NULL, ipsec_birth_info, ipsec_birth_set, (void *)&ipsec_ipv4_birth_packet},
  32953. + {"ipv6", &proc_birth_dir, NULL, ipsec_birth_info, ipsec_birth_set, (void *)&ipsec_ipv6_birth_packet},
  32954. + {"tncfg", &proc_net_ipsec_dir, NULL, ipsec_tncfg_get_info, NULL, NULL},
  32955. + {"xforms", &proc_net_ipsec_dir, NULL, ipsec_xform_get_info, NULL, NULL},
  32956. + {"stats", &proc_net_ipsec_dir, &proc_stats_dir, NULL, NULL, NULL},
  32957. + {"trap_count", &proc_stats_dir, NULL, ipsec_stats_get_int_info, NULL, &ipsec_xmit_trap_count},
  32958. + {"trap_sendcount", &proc_stats_dir, NULL, ipsec_stats_get_int_info, NULL, &ipsec_xmit_trap_sendcount},
  32959. + {"version", &proc_net_ipsec_dir, NULL, ipsec_version_get_info, NULL, NULL},
  32960. + {NULL, NULL, NULL, NULL, NULL, NULL}
  32961. +};
  32962. +#endif
  32963. +
  32964. +int
  32965. +ipsec_proc_init()
  32966. +{
  32967. + int error = 0;
  32968. +#ifdef IPSEC_PROC_SUBDIRS
  32969. + struct proc_dir_entry *item;
  32970. +#endif
  32971. +
  32972. + /*
  32973. + * just complain because pluto won't run without /proc!
  32974. + */
  32975. +#ifndef CONFIG_PROC_FS
  32976. +#error You must have PROC_FS built in to use KLIPS
  32977. +#endif
  32978. +
  32979. + /* for 2.0 kernels */
  32980. +#if !defined(PROC_FS_2325) && !defined(PROC_FS_21)
  32981. + error |= proc_register_dynamic(&proc_net, &ipsec_eroute);
  32982. + error |= proc_register_dynamic(&proc_net, &ipsec_spi);
  32983. + error |= proc_register_dynamic(&proc_net, &ipsec_spigrp);
  32984. + error |= proc_register_dynamic(&proc_net, &ipsec_tncfg);
  32985. + error |= proc_register_dynamic(&proc_net, &ipsec_version);
  32986. +#ifdef CONFIG_KLIPS_DEBUG
  32987. + error |= proc_register_dynamic(&proc_net, &ipsec_klipsdebug);
  32988. +#endif /* CONFIG_KLIPS_DEBUG */
  32989. +#endif
  32990. +
  32991. + /* for 2.2 kernels */
  32992. +#if !defined(PROC_FS_2325) && defined(PROC_FS_21)
  32993. + error |= proc_register(proc_net, &ipsec_eroute);
  32994. + error |= proc_register(proc_net, &ipsec_spi);
  32995. + error |= proc_register(proc_net, &ipsec_spigrp);
  32996. + error |= proc_register(proc_net, &ipsec_tncfg);
  32997. + error |= proc_register(proc_net, &ipsec_version);
  32998. +#ifdef CONFIG_KLIPS_DEBUG
  32999. + error |= proc_register(proc_net, &ipsec_klipsdebug);
  33000. +#endif /* CONFIG_KLIPS_DEBUG */
  33001. +#endif
  33002. +
  33003. + /* for 2.4 kernels */
  33004. +#if defined(PROC_FS_2325)
  33005. + /* create /proc/net/ipsec */
  33006. +
  33007. + /* zero these out before we initialize /proc/net/ipsec/birth/stuff */
  33008. + memset(&ipsec_ipv4_birth_packet, 0, sizeof(struct ipsec_birth_reply));
  33009. + memset(&ipsec_ipv6_birth_packet, 0, sizeof(struct ipsec_birth_reply));
  33010. +
  33011. + proc_net_ipsec_dir = proc_mkdir("ipsec", proc_net);
  33012. + if(proc_net_ipsec_dir == NULL) {
  33013. + /* no point in continuing */
  33014. + return 1;
  33015. + }
  33016. +
  33017. + {
  33018. + struct ipsec_proc_list *it;
  33019. +
  33020. + it=proc_items;
  33021. + while(it->name!=NULL) {
  33022. + if(it->dir) {
  33023. + /* make a dir instead */
  33024. + item = proc_mkdir(it->name, *it->parent);
  33025. + *it->dir = item;
  33026. + } else {
  33027. + item = create_proc_entry(it->name, 0400, *it->parent);
  33028. + }
  33029. + if(item) {
  33030. + item->read_proc = it->readthing;
  33031. + item->write_proc = it->writething;
  33032. + item->data = it->data;
  33033. +#ifdef MODULE
  33034. + item->owner = THIS_MODULE;
  33035. +#endif
  33036. + } else {
  33037. + error |= 1;
  33038. + }
  33039. + it++;
  33040. + }
  33041. + }
  33042. +
  33043. + /* now create some symlinks to provide compatibility */
  33044. + proc_symlink("ipsec_eroute", proc_net, "ipsec/eroute/all");
  33045. + proc_symlink("ipsec_spi", proc_net, "ipsec/spi/all");
  33046. + proc_symlink("ipsec_spigrp", proc_net, "ipsec/spigrp/all");
  33047. + proc_symlink("ipsec_tncfg", proc_net, "ipsec/tncfg");
  33048. + proc_symlink("ipsec_version",proc_net, "ipsec/version");
  33049. + proc_symlink("ipsec_klipsdebug",proc_net,"ipsec/klipsdebug");
  33050. +
  33051. +#endif /* !PROC_FS_2325 */
  33052. +
  33053. + return error;
  33054. +}
  33055. +
  33056. +void
  33057. +ipsec_proc_cleanup()
  33058. +{
  33059. +
  33060. + /* for 2.0 and 2.2 kernels */
  33061. +#if !defined(PROC_FS_2325)
  33062. +
  33063. +#ifdef CONFIG_KLIPS_DEBUG
  33064. + if (proc_net_unregister(ipsec_klipsdebug.low_ino) != 0)
  33065. + printk("klips_debug:ipsec_cleanup: "
  33066. + "cannot unregister /proc/net/ipsec_klipsdebug\n");
  33067. +#endif /* CONFIG_KLIPS_DEBUG */
  33068. +
  33069. + if (proc_net_unregister(ipsec_version.low_ino) != 0)
  33070. + printk("klips_debug:ipsec_cleanup: "
  33071. + "cannot unregister /proc/net/ipsec_version\n");
  33072. + if (proc_net_unregister(ipsec_eroute.low_ino) != 0)
  33073. + printk("klips_debug:ipsec_cleanup: "
  33074. + "cannot unregister /proc/net/ipsec_eroute\n");
  33075. + if (proc_net_unregister(ipsec_spi.low_ino) != 0)
  33076. + printk("klips_debug:ipsec_cleanup: "
  33077. + "cannot unregister /proc/net/ipsec_spi\n");
  33078. + if (proc_net_unregister(ipsec_spigrp.low_ino) != 0)
  33079. + printk("klips_debug:ipsec_cleanup: "
  33080. + "cannot unregister /proc/net/ipsec_spigrp\n");
  33081. + if (proc_net_unregister(ipsec_tncfg.low_ino) != 0)
  33082. + printk("klips_debug:ipsec_cleanup: "
  33083. + "cannot unregister /proc/net/ipsec_tncfg\n");
  33084. +#endif
  33085. +
  33086. + /* for 2.4 kernels */
  33087. +#if defined(PROC_FS_2325)
  33088. + {
  33089. + struct ipsec_proc_list *it;
  33090. +
  33091. + /* find end of list */
  33092. + it=proc_items;
  33093. + while(it->name!=NULL) {
  33094. + it++;
  33095. + }
  33096. + it--;
  33097. +
  33098. + do {
  33099. + remove_proc_entry(it->name, *it->parent);
  33100. + it--;
  33101. + } while(it >= proc_items);
  33102. + }
  33103. +
  33104. +
  33105. +#ifdef CONFIG_KLIPS_DEBUG
  33106. + remove_proc_entry("ipsec_klipsdebug", proc_net);
  33107. +#endif /* CONFIG_KLIPS_DEBUG */
  33108. + remove_proc_entry("ipsec_eroute", proc_net);
  33109. + remove_proc_entry("ipsec_spi", proc_net);
  33110. + remove_proc_entry("ipsec_spigrp", proc_net);
  33111. + remove_proc_entry("ipsec_tncfg", proc_net);
  33112. + remove_proc_entry("ipsec_version", proc_net);
  33113. + remove_proc_entry("ipsec", proc_net);
  33114. +#endif /* 2.4 kernel */
  33115. +}
  33116. +
  33117. +/*
  33118. + * $Log: ipsec_proc.c,v $
  33119. + * Revision 1.39.2.4 2006/11/15 22:21:39 paul
  33120. + * backport of creating a /sys/ file to test for nat-t capability in kernel.
  33121. + *
  33122. + * Revision 1.39.2.3 2006/10/06 21:39:26 paul
  33123. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  33124. + * set. This is defined through autoconf.h which is included through the
  33125. + * linux kernel build macros.
  33126. + *
  33127. + * Revision 1.39.2.2 2006/02/13 18:48:12 paul
  33128. + * Fix by Ankit Desai <ankit@elitecore.com> for module unloading.
  33129. + *
  33130. + * Revision 1.39.2.1 2005/09/07 00:45:59 paul
  33131. + * pull up of mcr's nat-t klips detection patch from head
  33132. + *
  33133. + * Revision 1.39 2005/05/20 03:19:18 mcr
  33134. + * modifications for use on 2.4.30 kernel, with backported
  33135. + * printk_ratelimit(). all warnings removed.
  33136. + *
  33137. + * Revision 1.38 2005/04/29 05:10:22 mcr
  33138. + * removed from extraenous includes to make unit testing easier.
  33139. + *
  33140. + * Revision 1.37 2005/04/13 22:49:49 mcr
  33141. + * moved KLIPS specific snprintf() wrapper to seperate file.
  33142. + *
  33143. + * Revision 1.36 2005/04/06 17:44:36 mcr
  33144. + * when NAT-T is compiled out, show encap as "NA"
  33145. + *
  33146. + * Revision 1.35 2005/01/26 00:50:35 mcr
  33147. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  33148. + * and make sure that NAT_TRAVERSAL is set as well to match
  33149. + * userspace compiles of code.
  33150. + *
  33151. + * Revision 1.34 2004/12/03 21:25:57 mcr
  33152. + * compile time fixes for running on 2.6.
  33153. + * still experimental.
  33154. + *
  33155. + * Revision 1.33 2004/08/17 03:27:23 mcr
  33156. + * klips 2.6 edits.
  33157. + *
  33158. + * Revision 1.32 2004/08/03 18:19:08 mcr
  33159. + * in 2.6, use "net_device" instead of #define device->net_device.
  33160. + * this probably breaks 2.0 compiles.
  33161. + *
  33162. + * Revision 1.31 2004/07/10 19:11:18 mcr
  33163. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  33164. + *
  33165. + * Revision 1.30 2004/04/25 21:23:11 ken
  33166. + * Pull in dhr's changes from FreeS/WAN 2.06
  33167. + *
  33168. + * Revision 1.29 2004/04/06 02:49:26 mcr
  33169. + * pullup of algo code from alg-branch.
  33170. + *
  33171. + * Revision 1.28 2004/03/28 20:29:58 paul
  33172. + * <hugh_> ssize_t, not ssized_t
  33173. + *
  33174. + * Revision 1.27 2004/03/28 20:27:20 paul
  33175. + * Included tested and confirmed fixes mcr made and dhr verified for
  33176. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  33177. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  33178. + * dhr. (thanks dhr!)
  33179. + *
  33180. + * Revision 1.26 2004/02/09 22:07:06 mcr
  33181. + * added information about nat-traversal setting to spi-output.
  33182. + *
  33183. + * Revision 1.25.4.1 2004/04/05 04:30:46 mcr
  33184. + * patches for alg-branch to compile/work with 2.x openswan
  33185. + *
  33186. + * Revision 1.25 2003/10/31 02:27:55 mcr
  33187. + * pulled up port-selector patches and sa_id elimination.
  33188. + *
  33189. + * Revision 1.24.4.1 2003/10/29 01:30:41 mcr
  33190. + * elimited "struct sa_id".
  33191. + *
  33192. + * Revision 1.24 2003/06/20 01:42:21 mcr
  33193. + * added counters to measure how many ACQUIREs we send to pluto,
  33194. + * and how many are successfully sent.
  33195. + *
  33196. + * Revision 1.23 2003/04/03 17:38:09 rgb
  33197. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  33198. + *
  33199. + * Revision 1.22 2002/09/20 15:40:57 rgb
  33200. + * Renamed saref macros for consistency and brevity.
  33201. + *
  33202. + * Revision 1.21 2002/09/20 05:01:35 rgb
  33203. + * Print ref and reftable, refentry seperately.
  33204. + *
  33205. + * Revision 1.20 2002/09/19 02:35:39 mcr
  33206. + * do not define structures needed by /proc/net/ipsec/ if we
  33207. + * aren't going create that directory.
  33208. + *
  33209. + * Revision 1.19 2002/09/10 01:43:25 mcr
  33210. + * fixed problem in /-* comment.
  33211. + *
  33212. + * Revision 1.18 2002/09/03 16:22:11 mcr
  33213. + * fixed initialization of birth/stuff values - some simple
  33214. + * screw ups in the code.
  33215. + * removed debugging that was left in by mistake.
  33216. + *
  33217. + * Revision 1.17 2002/09/02 17:54:53 mcr
  33218. + * changed how the table driven /proc entries are created so that
  33219. + * making subdirs is now explicit rather than implicit.
  33220. + *
  33221. + * Revision 1.16 2002/08/30 01:23:37 mcr
  33222. + * reorganized /proc creating code to clear up ifdefs,
  33223. + * make the 2.4 code table driven, and put things into
  33224. + * /proc/net/ipsec subdir. Symlinks are left for compatibility.
  33225. + *
  33226. + * Revision 1.15 2002/08/13 19:01:25 mcr
  33227. + * patches from kenb to permit compilation of FreeSWAN on ia64.
  33228. + * des library patched to use proper DES_LONG type for ia64.
  33229. + *
  33230. + * Revision 1.14 2002/07/26 08:48:31 rgb
  33231. + * Added SA ref table code.
  33232. + *
  33233. + * Revision 1.13 2002/07/24 18:44:54 rgb
  33234. + * Type fiddling to tame ia64 compiler.
  33235. + *
  33236. + * Revision 1.12 2002/05/27 18:56:07 rgb
  33237. + * Convert to dynamic ipsec device allocation.
  33238. + *
  33239. + * Revision 1.11 2002/05/23 07:14:50 rgb
  33240. + * Added refcount code.
  33241. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  33242. + * Convert "usecount" to "refcount" to remove ambiguity.
  33243. + *
  33244. + * Revision 1.10 2002/04/24 07:55:32 mcr
  33245. + * #include patches and Makefiles for post-reorg compilation.
  33246. + *
  33247. + * Revision 1.9 2002/04/24 07:36:28 mcr
  33248. + * Moved from ./klips/net/ipsec/ipsec_proc.c,v
  33249. + *
  33250. + * Revision 1.8 2002/01/29 17:17:55 mcr
  33251. + * moved include of ipsec_param.h to after include of linux/kernel.h
  33252. + * otherwise, it seems that some option that is set in ipsec_param.h
  33253. + * screws up something subtle in the include path to kernel.h, and
  33254. + * it complains on the snprintf() prototype.
  33255. + *
  33256. + * Revision 1.7 2002/01/29 04:00:52 mcr
  33257. + * more excise of kversions.h header.
  33258. + *
  33259. + * Revision 1.6 2002/01/29 02:13:17 mcr
  33260. + * introduction of ipsec_kversion.h means that include of
  33261. + * ipsec_param.h must preceed any decisions about what files to
  33262. + * include to deal with differences in kernel source.
  33263. + *
  33264. + * Revision 1.5 2002/01/12 02:54:30 mcr
  33265. + * beginnings of /proc/net/ipsec dir.
  33266. + *
  33267. + * Revision 1.4 2001/12/11 02:21:05 rgb
  33268. + * Don't include module version here, fixing 2.2 compile bug.
  33269. + *
  33270. + * Revision 1.3 2001/12/05 07:19:44 rgb
  33271. + * Fixed extraneous #include "version.c" bug causing modular KLIPS failure.
  33272. + *
  33273. + * Revision 1.2 2001/11/26 09:16:14 rgb
  33274. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  33275. + *
  33276. + * Revision 1.74 2001/11/22 05:44:11 henry
  33277. + * new version stuff
  33278. + *
  33279. + * Revision 1.1.2.1 2001/09/25 02:19:40 mcr
  33280. + * /proc manipulation code moved to new ipsec_proc.c
  33281. + *
  33282. + *
  33283. + * Local variables:
  33284. + * c-file-style: "linux"
  33285. + * End:
  33286. + *
  33287. + */
  33288. --- /dev/null Tue Mar 11 13:02:56 2003
  33289. +++ linux/net/ipsec/ipsec_radij.c Mon Feb 9 13:51:03 2004
  33290. @@ -0,0 +1,889 @@
  33291. +/*
  33292. + * Interface between the IPSEC code and the radix (radij) tree code
  33293. + * Copyright (C) 1996, 1997 John Ioannidis.
  33294. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  33295. + *
  33296. + * This program is free software; you can redistribute it and/or modify it
  33297. + * under the terms of the GNU General Public License as published by the
  33298. + * Free Software Foundation; either version 2 of the License, or (at your
  33299. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  33300. + *
  33301. + * This program is distributed in the hope that it will be useful, but
  33302. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  33303. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  33304. + * for more details.
  33305. + *
  33306. + * RCSID $Id: ipsec_radij.c,v 1.73.2.1 2006/10/06 21:39:26 paul Exp $
  33307. + */
  33308. +
  33309. +#ifndef AUTOCONF_INCLUDED
  33310. +#include <linux/config.h>
  33311. +#endif
  33312. +#include <linux/version.h>
  33313. +#include <linux/kernel.h> /* printk() */
  33314. +
  33315. +#include "openswan/ipsec_param.h"
  33316. +
  33317. +#ifdef MALLOC_SLAB
  33318. +# include <linux/slab.h> /* kmalloc() */
  33319. +#else /* MALLOC_SLAB */
  33320. +# include <linux/malloc.h> /* kmalloc() */
  33321. +#endif /* MALLOC_SLAB */
  33322. +#include <linux/errno.h> /* error codes */
  33323. +#include <linux/types.h> /* size_t */
  33324. +#include <linux/interrupt.h> /* mark_bh */
  33325. +
  33326. +#include <linux/netdevice.h> /* struct device, struct net_device_stats and other headers */
  33327. +#include <linux/etherdevice.h> /* eth_type_trans */
  33328. +#include <linux/ip.h> /* struct iphdr */
  33329. +#include <linux/skbuff.h>
  33330. +#include <openswan.h>
  33331. +#ifdef SPINLOCK
  33332. +# ifdef SPINLOCK_23
  33333. +# include <linux/spinlock.h> /* *lock* */
  33334. +# else /* 23_SPINLOCK */
  33335. +# include <asm/spinlock.h> /* *lock* */
  33336. +# endif /* 23_SPINLOCK */
  33337. +#endif /* SPINLOCK */
  33338. +
  33339. +#include <net/ip.h>
  33340. +
  33341. +#include "openswan/ipsec_eroute.h"
  33342. +#include "openswan/ipsec_sa.h"
  33343. +
  33344. +#include "openswan/radij.h"
  33345. +#include "openswan/ipsec_encap.h"
  33346. +#include "openswan/radij.h"
  33347. +#include "openswan/ipsec_encap.h"
  33348. +#include "openswan/ipsec_radij.h"
  33349. +#include "openswan/ipsec_tunnel.h" /* struct ipsecpriv */
  33350. +#include "openswan/ipsec_xform.h"
  33351. +
  33352. +#include <pfkeyv2.h>
  33353. +#include <pfkey.h>
  33354. +
  33355. +#include "openswan/ipsec_proto.h"
  33356. +
  33357. +#ifdef CONFIG_KLIPS_DEBUG
  33358. +int debug_radij = 0;
  33359. +#endif /* CONFIG_KLIPS_DEBUG */
  33360. +
  33361. +struct radij_node_head *rnh = NULL;
  33362. +#ifdef SPINLOCK
  33363. +spinlock_t eroute_lock = SPIN_LOCK_UNLOCKED;
  33364. +#else /* SPINLOCK */
  33365. +spinlock_t eroute_lock;
  33366. +#endif /* SPINLOCK */
  33367. +
  33368. +int
  33369. +ipsec_radijinit(void)
  33370. +{
  33371. + maj_keylen = sizeof (struct sockaddr_encap);
  33372. +
  33373. + rj_init();
  33374. +
  33375. + if (rj_inithead((void **)&rnh, /*16*/offsetof(struct sockaddr_encap, sen_type) * sizeof(__u8)) == 0) /* 16 is bit offset of sen_type */
  33376. + return -1;
  33377. + return 0;
  33378. +}
  33379. +
  33380. +int
  33381. +ipsec_radijcleanup(void)
  33382. +{
  33383. + int error;
  33384. +
  33385. + spin_lock_bh(&eroute_lock);
  33386. +
  33387. + error = radijcleanup();
  33388. +
  33389. + spin_unlock_bh(&eroute_lock);
  33390. +
  33391. + return error;
  33392. +}
  33393. +
  33394. +int
  33395. +ipsec_cleareroutes(void)
  33396. +{
  33397. + int error;
  33398. +
  33399. + spin_lock_bh(&eroute_lock);
  33400. +
  33401. + error = radijcleartree();
  33402. +
  33403. + spin_unlock_bh(&eroute_lock);
  33404. +
  33405. + return error;
  33406. +}
  33407. +
  33408. +int
  33409. +ipsec_breakroute(struct sockaddr_encap *eaddr,
  33410. + struct sockaddr_encap *emask,
  33411. + struct sk_buff **first,
  33412. + struct sk_buff **last)
  33413. +{
  33414. + struct eroute *ro;
  33415. + struct radij_node *rn;
  33416. + int error;
  33417. +#ifdef CONFIG_KLIPS_DEBUG
  33418. +
  33419. + if (debug_eroute) {
  33420. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33421. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33422. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33423. + KLIPS_PRINT(debug_eroute,
  33424. + "klips_debug:ipsec_breakroute: "
  33425. + "attempting to delete eroute for %s:%d->%s:%d %d\n",
  33426. + buf1, ntohs(eaddr->sen_sport),
  33427. + buf2, ntohs(eaddr->sen_dport), eaddr->sen_proto);
  33428. + }
  33429. +#endif /* CONFIG_KLIPS_DEBUG */
  33430. +
  33431. + spin_lock_bh(&eroute_lock);
  33432. +
  33433. + if ((error = rj_delete(eaddr, emask, rnh, &rn)) != 0) {
  33434. + spin_unlock_bh(&eroute_lock);
  33435. + KLIPS_PRINT(debug_eroute,
  33436. + "klips_debug:ipsec_breakroute: "
  33437. + "node not found, eroute delete failed.\n");
  33438. + return error;
  33439. + }
  33440. +
  33441. + spin_unlock_bh(&eroute_lock);
  33442. +
  33443. + ro = (struct eroute *)rn;
  33444. +
  33445. + KLIPS_PRINT(debug_eroute,
  33446. + "klips_debug:ipsec_breakroute: "
  33447. + "deleted eroute=0p%p, ident=0p%p->0p%p, first=0p%p, last=0p%p\n",
  33448. + ro,
  33449. + ro->er_ident_s.data,
  33450. + ro->er_ident_d.data,
  33451. + ro->er_first,
  33452. + ro->er_last);
  33453. +
  33454. + if (ro->er_ident_s.data != NULL) {
  33455. + kfree(ro->er_ident_s.data);
  33456. + }
  33457. + if (ro->er_ident_d.data != NULL) {
  33458. + kfree(ro->er_ident_d.data);
  33459. + }
  33460. + if (ro->er_first != NULL) {
  33461. +#if 0
  33462. + struct net_device_stats *stats = (struct net_device_stats *) &(((struct ipsecpriv *)(ro->er_first->dev->priv))->mystats);
  33463. + stats->tx_dropped--;
  33464. +#endif
  33465. + *first = ro->er_first;
  33466. + }
  33467. + if (ro->er_last != NULL) {
  33468. +#if 0
  33469. + struct net_device_stats *stats = (struct net_device_stats *) &(((struct ipsecpriv *)(ro->er_last->dev->priv))->mystats);
  33470. + stats->tx_dropped--;
  33471. +#endif
  33472. + *last = ro->er_last;
  33473. + }
  33474. +
  33475. + if (rn->rj_flags & (RJF_ACTIVE | RJF_ROOT))
  33476. + panic ("ipsec_breakroute RMT_DELEROUTE root or active node\n");
  33477. + memset((caddr_t)rn, 0, sizeof (struct eroute));
  33478. + kfree(rn);
  33479. +
  33480. + return 0;
  33481. +}
  33482. +
  33483. +int
  33484. +ipsec_makeroute(struct sockaddr_encap *eaddr,
  33485. + struct sockaddr_encap *emask,
  33486. + ip_said said,
  33487. + uint32_t pid,
  33488. + struct sk_buff *skb,
  33489. + struct ident *ident_s,
  33490. + struct ident *ident_d)
  33491. +{
  33492. + struct eroute *retrt;
  33493. + int error;
  33494. + char sa[SATOT_BUF];
  33495. + size_t sa_len;
  33496. +
  33497. +#ifdef CONFIG_KLIPS_DEBUG
  33498. +
  33499. + if (debug_eroute) {
  33500. +
  33501. + {
  33502. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33503. +
  33504. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33505. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33506. + sa_len = satot(&said, 0, sa, sizeof(sa));
  33507. + KLIPS_PRINT(debug_eroute,
  33508. + "klips_debug:ipsec_makeroute: "
  33509. + "attempting to allocate %lu bytes to insert eroute for %s->%s, SA: %s, PID:%d, skb=0p%p, ident:%s->%s\n",
  33510. + (unsigned long) sizeof(struct eroute),
  33511. + buf1,
  33512. + buf2,
  33513. + sa_len ? sa : " (error)",
  33514. + pid,
  33515. + skb,
  33516. + (ident_s ? (ident_s->data ? ident_s->data : "NULL") : "NULL"),
  33517. + (ident_d ? (ident_d->data ? ident_d->data : "NULL") : "NULL"));
  33518. + }
  33519. + {
  33520. + char buf1[sizeof(struct sockaddr_encap)*2 + 1],
  33521. + buf2[sizeof(struct sockaddr_encap)*2 + 1];
  33522. + int i;
  33523. + unsigned char *b1 = buf1,
  33524. + *b2 = buf2,
  33525. + *ea = (unsigned char *)eaddr,
  33526. + *em = (unsigned char *)emask;
  33527. +
  33528. +
  33529. + for (i=0; i<sizeof(struct sockaddr_encap); i++) {
  33530. + sprintf(b1, "%02x", ea[i]);
  33531. + sprintf(b2, "%02x", em[i]);
  33532. + b1+=2;
  33533. + b2+=2;
  33534. + }
  33535. + KLIPS_PRINT(debug_eroute, "klips_debug:ipsec_makeroute: %s / %s \n", buf1, buf2);
  33536. + }
  33537. +
  33538. + }
  33539. +#endif /* CONFIG_KLIPS_DEBUG */
  33540. +
  33541. + retrt = (struct eroute *)kmalloc(sizeof (struct eroute), GFP_ATOMIC);
  33542. + if (retrt == NULL) {
  33543. + printk("klips_error:ipsec_makeroute: "
  33544. + "not able to allocate kernel memory");
  33545. + return -ENOMEM;
  33546. + }
  33547. + memset((caddr_t)retrt, 0, sizeof (struct eroute));
  33548. +
  33549. + retrt->er_eaddr = *eaddr;
  33550. + retrt->er_emask = *emask;
  33551. + retrt->er_said = said;
  33552. + retrt->er_pid = pid;
  33553. + retrt->er_count = 0;
  33554. + retrt->er_lasttime = jiffies/HZ;
  33555. +
  33556. + {
  33557. + /* this is because gcc 3. doesn't like cast's as lvalues */
  33558. + struct rjtentry *rje = (struct rjtentry *)&(retrt->er_rjt);
  33559. + caddr_t er = (caddr_t)&(retrt->er_eaddr);
  33560. +
  33561. + rje->rd_nodes->rj_key= er;
  33562. + }
  33563. +
  33564. + if (ident_s && ident_s->type != SADB_IDENTTYPE_RESERVED) {
  33565. + int data_len = ident_s->len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  33566. +
  33567. + retrt->er_ident_s.type = ident_s->type;
  33568. + retrt->er_ident_s.id = ident_s->id;
  33569. + retrt->er_ident_s.len = ident_s->len;
  33570. + if(data_len) {
  33571. + KLIPS_PRINT(debug_eroute,
  33572. + "klips_debug:ipsec_makeroute: "
  33573. + "attempting to allocate %u bytes for ident_s.\n",
  33574. + data_len);
  33575. + if(!(retrt->er_ident_s.data = kmalloc(data_len, GFP_KERNEL))) {
  33576. + kfree(retrt);
  33577. + printk("klips_error:ipsec_makeroute: not able to allocate kernel memory (%d)\n", data_len);
  33578. + return ENOMEM;
  33579. + }
  33580. + memcpy(retrt->er_ident_s.data, ident_s->data, data_len);
  33581. + } else {
  33582. + retrt->er_ident_s.data = NULL;
  33583. + }
  33584. + }
  33585. +
  33586. + if (ident_d && ident_d->type != SADB_IDENTTYPE_RESERVED) {
  33587. + int data_len = ident_d->len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  33588. +
  33589. + retrt->er_ident_d.type = ident_d->type;
  33590. + retrt->er_ident_d.id = ident_d->id;
  33591. + retrt->er_ident_d.len = ident_d->len;
  33592. + if(data_len) {
  33593. + KLIPS_PRINT(debug_eroute,
  33594. + "klips_debug:ipsec_makeroute: "
  33595. + "attempting to allocate %u bytes for ident_d.\n",
  33596. + data_len);
  33597. + if(!(retrt->er_ident_d.data = kmalloc(data_len, GFP_KERNEL))) {
  33598. + if (retrt->er_ident_s.data)
  33599. + kfree(retrt->er_ident_s.data);
  33600. + kfree(retrt);
  33601. + printk("klips_error:ipsec_makeroute: not able to allocate kernel memory (%d)\n", data_len);
  33602. + return ENOMEM;
  33603. + }
  33604. + memcpy(retrt->er_ident_d.data, ident_d->data, data_len);
  33605. + } else {
  33606. + retrt->er_ident_d.data = NULL;
  33607. + }
  33608. + }
  33609. + retrt->er_first = skb;
  33610. + retrt->er_last = NULL;
  33611. +
  33612. + KLIPS_PRINT(debug_eroute,
  33613. + "klips_debug:ipsec_makeroute: "
  33614. + "calling rj_addroute now\n");
  33615. +
  33616. + spin_lock_bh(&eroute_lock);
  33617. +
  33618. + error = rj_addroute(&(retrt->er_eaddr), &(retrt->er_emask),
  33619. + rnh, retrt->er_rjt.rd_nodes);
  33620. +
  33621. + spin_unlock_bh(&eroute_lock);
  33622. +
  33623. + if(error) {
  33624. + sa_len = satot(&said, 0, sa, sizeof(sa));
  33625. + KLIPS_PRINT(debug_eroute,
  33626. + "klips_debug:ipsec_makeroute: "
  33627. + "rj_addroute not able to insert eroute for SA:%s (error:%d)\n",
  33628. + sa_len ? sa : " (error)", error);
  33629. + if (retrt->er_ident_s.data)
  33630. + kfree(retrt->er_ident_s.data);
  33631. + if (retrt->er_ident_d.data)
  33632. + kfree(retrt->er_ident_d.data);
  33633. +
  33634. + kfree(retrt);
  33635. +
  33636. + return error;
  33637. + }
  33638. +
  33639. +#ifdef CONFIG_KLIPS_DEBUG
  33640. + if (debug_eroute) {
  33641. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33642. +/*
  33643. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33644. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33645. +*/
  33646. + subnettoa(rd_key((&(retrt->er_rjt)))->sen_ip_src, rd_mask((&(retrt->er_rjt)))->sen_ip_src, 0, buf1, sizeof(buf1));
  33647. + subnettoa(rd_key((&(retrt->er_rjt)))->sen_ip_dst, rd_mask((&(retrt->er_rjt)))->sen_ip_dst, 0, buf2, sizeof(buf2));
  33648. + sa_len = satot(&retrt->er_said, 0, sa, sizeof(sa));
  33649. +
  33650. + KLIPS_PRINT(debug_eroute,
  33651. + "klips_debug:ipsec_makeroute: "
  33652. + "pid=%05d "
  33653. + "count=%10d "
  33654. + "lasttime=%6d "
  33655. + "%-18s -> %-18s => %s\n",
  33656. + retrt->er_pid,
  33657. + retrt->er_count,
  33658. + (int)(jiffies/HZ - retrt->er_lasttime),
  33659. + buf1,
  33660. + buf2,
  33661. + sa_len ? sa : " (error)");
  33662. + }
  33663. +#endif /* CONFIG_KLIPS_DEBUG */
  33664. + KLIPS_PRINT(debug_eroute,
  33665. + "klips_debug:ipsec_makeroute: "
  33666. + "succeeded.\n");
  33667. + return 0;
  33668. +}
  33669. +
  33670. +struct eroute *
  33671. +ipsec_findroute(struct sockaddr_encap *eaddr)
  33672. +{
  33673. + struct radij_node *rn;
  33674. +#ifdef CONFIG_KLIPS_DEBUG
  33675. + char buf1[ADDRTOA_BUF], buf2[ADDRTOA_BUF];
  33676. +
  33677. + if (debug_radij & DB_RJ_FINDROUTE) {
  33678. + addrtoa(eaddr->sen_ip_src, 0, buf1, sizeof(buf1));
  33679. + addrtoa(eaddr->sen_ip_dst, 0, buf2, sizeof(buf2));
  33680. + KLIPS_PRINT(debug_eroute,
  33681. + "klips_debug:ipsec_findroute: "
  33682. + "%s:%d->%s:%d %d\n",
  33683. + buf1, ntohs(eaddr->sen_sport),
  33684. + buf2, ntohs(eaddr->sen_dport),
  33685. + eaddr->sen_proto);
  33686. + }
  33687. +#endif /* CONFIG_KLIPS_DEBUG */
  33688. + rn = rj_match((caddr_t)eaddr, rnh);
  33689. + if(rn) {
  33690. + KLIPS_PRINT(debug_eroute && sysctl_ipsec_debug_verbose,
  33691. + "klips_debug:ipsec_findroute: "
  33692. + "found, points to proto=%d, spi=%x, dst=%x.\n",
  33693. + ((struct eroute*)rn)->er_said.proto,
  33694. + ntohl(((struct eroute*)rn)->er_said.spi),
  33695. + ntohl(((struct eroute*)rn)->er_said.dst.u.v4.sin_addr.s_addr));
  33696. + }
  33697. + return (struct eroute *)rn;
  33698. +}
  33699. +
  33700. +#ifdef CONFIG_PROC_FS
  33701. +/** ipsec_rj_walker_procprint: print one line of eroute table output.
  33702. + *
  33703. + * Theoretical BUG: if w->length is less than the length
  33704. + * of some line we should produce, that line will never
  33705. + * be finished. In effect, the "file" will stop part way
  33706. + * through that line.
  33707. + */
  33708. +int
  33709. +ipsec_rj_walker_procprint(struct radij_node *rn, void *w0)
  33710. +{
  33711. + struct eroute *ro = (struct eroute *)rn;
  33712. + struct rjtentry *rd = (struct rjtentry *)rn;
  33713. + struct wsbuf *w = (struct wsbuf *)w0;
  33714. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33715. + char buf3[16];
  33716. + char sa[SATOT_BUF];
  33717. + size_t sa_len, buf_len;
  33718. + struct sockaddr_encap *key, *mask;
  33719. +
  33720. + KLIPS_PRINT(debug_radij,
  33721. + "klips_debug:ipsec_rj_walker_procprint: "
  33722. + "rn=0p%p, w0=0p%p\n",
  33723. + rn,
  33724. + w0);
  33725. + if (rn->rj_b >= 0) {
  33726. + return 0;
  33727. + }
  33728. +
  33729. + key = rd_key(rd);
  33730. + mask = rd_mask(rd);
  33731. +
  33732. + if (key == NULL || mask == NULL) {
  33733. + return 0;
  33734. + }
  33735. +
  33736. + buf_len = subnettoa(key->sen_ip_src, mask->sen_ip_src, 0, buf1, sizeof(buf1));
  33737. + if(key->sen_sport != 0) {
  33738. + sprintf(buf1+buf_len-1, ":%d", ntohs(key->sen_sport));
  33739. + }
  33740. +
  33741. + buf_len = subnettoa(key->sen_ip_dst, mask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33742. + if(key->sen_dport != 0) {
  33743. + sprintf(buf2+buf_len-1, ":%d", ntohs(key->sen_dport));
  33744. + }
  33745. +
  33746. + buf3[0]='\0';
  33747. + if(key->sen_proto != 0) {
  33748. + sprintf(buf3, ":%d", key->sen_proto);
  33749. + }
  33750. +
  33751. + sa_len = satot(&ro->er_said, 'x', sa, sizeof(sa));
  33752. + w->len += ipsec_snprintf(w->buffer + w->len,
  33753. + w->length - w->len,
  33754. + "%-10d "
  33755. + "%-18s -> %-18s => %s%s\n",
  33756. + ro->er_count,
  33757. + buf1,
  33758. + buf2,
  33759. + sa_len ? sa : " (error)",
  33760. + buf3);
  33761. +
  33762. + {
  33763. + /* snprintf can only fill the last character with NUL
  33764. + * so the maximum useful character is w->length-1.
  33765. + * However, if w->length == 0, we cannot go back.
  33766. + * (w->length surely cannot be negative.)
  33767. + */
  33768. + int max_content = w->length > 0? w->length-1 : 0;
  33769. +
  33770. + if (w->len >= max_content) {
  33771. + /* we've done all that can fit -- stop treewalking */
  33772. + w->len = max_content; /* truncate crap */
  33773. + return -ENOBUFS;
  33774. + } else {
  33775. + const off_t pos = w->begin + w->len; /* file position of end of what we've generated */
  33776. +
  33777. + if (pos <= w->offset) {
  33778. + /* all is before first interesting character:
  33779. + * discard, but note where we are.
  33780. + */
  33781. + w->len = 0;
  33782. + w->begin = pos;
  33783. + }
  33784. + return 0;
  33785. + }
  33786. + }
  33787. +}
  33788. +#endif /* CONFIG_PROC_FS */
  33789. +
  33790. +int
  33791. +ipsec_rj_walker_delete(struct radij_node *rn, void *w0)
  33792. +{
  33793. + struct eroute *ro;
  33794. + struct rjtentry *rd = (struct rjtentry *)rn;
  33795. + struct radij_node *rn2;
  33796. + int error;
  33797. + struct sockaddr_encap *key, *mask;
  33798. +
  33799. + key = rd_key(rd);
  33800. + mask = rd_mask(rd);
  33801. +
  33802. + if(!key || !mask) {
  33803. + return -ENODATA;
  33804. + }
  33805. +#ifdef CONFIG_KLIPS_DEBUG
  33806. + if(debug_radij) {
  33807. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33808. + subnettoa(key->sen_ip_src, mask->sen_ip_src, 0, buf1, sizeof(buf1));
  33809. + subnettoa(key->sen_ip_dst, mask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33810. + KLIPS_PRINT(debug_radij,
  33811. + "klips_debug:ipsec_rj_walker_delete: "
  33812. + "deleting: %s -> %s\n",
  33813. + buf1,
  33814. + buf2);
  33815. + }
  33816. +#endif /* CONFIG_KLIPS_DEBUG */
  33817. +
  33818. + if((error = rj_delete(key, mask, rnh, &rn2))) {
  33819. + KLIPS_PRINT(debug_radij,
  33820. + "klips_debug:ipsec_rj_walker_delete: "
  33821. + "rj_delete failed with error=%d.\n", error);
  33822. + return error;
  33823. + }
  33824. +
  33825. + if(rn2 != rn) {
  33826. + printk("klips_debug:ipsec_rj_walker_delete: "
  33827. + "tried to delete a different node?!? This should never happen!\n");
  33828. + }
  33829. +
  33830. + ro = (struct eroute *)rn;
  33831. +
  33832. + if (ro->er_ident_s.data)
  33833. + kfree(ro->er_ident_s.data);
  33834. + if (ro->er_ident_d.data)
  33835. + kfree(ro->er_ident_d.data);
  33836. +
  33837. + memset((caddr_t)rn, 0, sizeof (struct eroute));
  33838. + kfree(rn);
  33839. +
  33840. + return 0;
  33841. +}
  33842. +
  33843. +/*
  33844. + * $Log: ipsec_radij.c,v $
  33845. + * Revision 1.73.2.1 2006/10/06 21:39:26 paul
  33846. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  33847. + * set. This is defined through autoconf.h which is included through the
  33848. + * linux kernel build macros.
  33849. + *
  33850. + * Revision 1.73 2005/04/29 05:10:22 mcr
  33851. + * removed from extraenous includes to make unit testing easier.
  33852. + *
  33853. + * Revision 1.72 2004/12/03 21:25:57 mcr
  33854. + * compile time fixes for running on 2.6.
  33855. + * still experimental.
  33856. + *
  33857. + * Revision 1.71 2004/07/10 19:11:18 mcr
  33858. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  33859. + *
  33860. + * Revision 1.70 2004/04/25 21:10:52 ken
  33861. + * Pull in dhr's changes from FreeS/WAN 2.06
  33862. + *
  33863. + * Revision 1.69 2004/04/06 02:49:26 mcr
  33864. + * pullup of algo code from alg-branch.
  33865. + *
  33866. + * Revision 1.68 2004/03/28 20:27:20 paul
  33867. + * Included tested and confirmed fixes mcr made and dhr verified for
  33868. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  33869. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  33870. + * dhr. (thanks dhr!)
  33871. + *
  33872. + * Revision 1.67.4.1 2004/04/05 04:30:46 mcr
  33873. + * patches for alg-branch to compile/work with 2.x openswan
  33874. + *
  33875. + * Revision 1.67 2003/10/31 02:27:55 mcr
  33876. + * pulled up port-selector patches and sa_id elimination.
  33877. + *
  33878. + * Revision 1.66.24.2 2003/10/29 01:30:41 mcr
  33879. + * elimited "struct sa_id".
  33880. + *
  33881. + * Revision 1.66.24.1 2003/09/21 13:59:56 mcr
  33882. + * pre-liminary X.509 patch - does not yet pass tests.
  33883. + *
  33884. + * Revision 1.66 2002/10/12 23:11:53 dhr
  33885. + *
  33886. + * [KenB + DHR] more 64-bit cleanup
  33887. + *
  33888. + * Revision 1.65 2002/09/20 05:01:40 rgb
  33889. + * Added memory allocation debugging.
  33890. + *
  33891. + * Revision 1.64 2002/05/31 01:46:05 mcr
  33892. + * added && sysctl_ipsec_debug_verbose verbose to ipsec_findroute
  33893. + * as requested in PR#14.
  33894. + *
  33895. + * Revision 1.63 2002/05/23 07:14:11 rgb
  33896. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  33897. + *
  33898. + * Revision 1.62 2002/04/24 07:55:32 mcr
  33899. + * #include patches and Makefiles for post-reorg compilation.
  33900. + *
  33901. + * Revision 1.61 2002/04/24 07:36:29 mcr
  33902. + * Moved from ./klips/net/ipsec/ipsec_radij.c,v
  33903. + *
  33904. + * Revision 1.60 2002/02/19 23:59:45 rgb
  33905. + * Removed redundant compiler directives.
  33906. + *
  33907. + * Revision 1.59 2002/02/06 04:13:47 mcr
  33908. + * missing #ifdef CONFIG_IPSEC_DEBUG.
  33909. + *
  33910. + * Revision 1.58 2002/01/29 17:17:56 mcr
  33911. + * moved include of ipsec_param.h to after include of linux/kernel.h
  33912. + * otherwise, it seems that some option that is set in ipsec_param.h
  33913. + * screws up something subtle in the include path to kernel.h, and
  33914. + * it complains on the snprintf() prototype.
  33915. + *
  33916. + * Revision 1.57 2002/01/29 04:00:52 mcr
  33917. + * more excise of kversions.h header.
  33918. + *
  33919. + * Revision 1.56 2002/01/29 02:13:17 mcr
  33920. + * introduction of ipsec_kversion.h means that include of
  33921. + * ipsec_param.h must preceed any decisions about what files to
  33922. + * include to deal with differences in kernel source.
  33923. + *
  33924. + * Revision 1.55 2001/11/26 09:23:48 rgb
  33925. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  33926. + *
  33927. + * Revision 1.53.2.1 2001/09/25 02:26:32 mcr
  33928. + * headers adjusted for new usage.
  33929. + *
  33930. + * Revision 1.54 2001/10/18 04:45:20 rgb
  33931. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  33932. + * lib/freeswan.h version macros moved to lib/kversions.h.
  33933. + * Other compiler directive cleanups.
  33934. + *
  33935. + * Revision 1.53 2001/09/19 17:19:40 rgb
  33936. + * Debug output bugfix for NetCelo's PF_KEY ident patch.
  33937. + *
  33938. + * Revision 1.52 2001/09/19 16:33:37 rgb
  33939. + * Temporarily disable ident fields to /proc/net/ipsec_eroute.
  33940. + *
  33941. + * Revision 1.51 2001/09/15 16:24:04 rgb
  33942. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  33943. + *
  33944. + * Revision 1.50 2001/09/14 16:58:36 rgb
  33945. + * Added support for storing the first and last packets through a HOLD.
  33946. + *
  33947. + * Revision 1.49 2001/09/08 21:13:32 rgb
  33948. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  33949. + *
  33950. + * Revision 1.48 2001/06/15 04:12:56 rgb
  33951. + * Fixed kernel memory allocation error return code polarity bug.
  33952. + *
  33953. + * Revision 1.47 2001/06/14 19:35:09 rgb
  33954. + * Update copyright date.
  33955. + *
  33956. + * Revision 1.46 2001/06/08 08:47:18 rgb
  33957. + * Fixed for debug disabled.
  33958. + *
  33959. + * Revision 1.45 2001/05/27 06:12:11 rgb
  33960. + * Added structures for pid, packet count and last access time to eroute.
  33961. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  33962. + *
  33963. + * Revision 1.44 2001/05/03 19:41:01 rgb
  33964. + * Initialise error return variable.
  33965. + * Use more appropriate return value for ipsec_rj_walker_delete().
  33966. + *
  33967. + * Revision 1.43 2001/02/27 22:24:54 rgb
  33968. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  33969. + * Check for satoa() return codes.
  33970. + *
  33971. + * Revision 1.42 2001/02/27 06:21:57 rgb
  33972. + * Added findroute success instrumentation.
  33973. + *
  33974. + * Revision 1.41 2000/11/06 04:32:08 rgb
  33975. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  33976. + *
  33977. + * Revision 1.40 2000/09/08 19:12:56 rgb
  33978. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  33979. + *
  33980. + * Revision 1.39 2000/08/30 05:25:20 rgb
  33981. + * Correct debug text in ipsec_breakroute() from incorrect
  33982. + * "ipsec_callback".
  33983. + *
  33984. + * Revision 1.38 2000/07/28 14:58:31 rgb
  33985. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  33986. + *
  33987. + * Revision 1.37 2000/03/16 14:02:50 rgb
  33988. + * Fixed debug scope to enable compilation with debug off.
  33989. + *
  33990. + * Revision 1.36 2000/01/21 06:14:46 rgb
  33991. + * Added debugging text to ipsec_rj_walker_delete().
  33992. + * Set return code to negative for consistency.
  33993. + *
  33994. + * Revision 1.35 1999/11/23 23:05:24 rgb
  33995. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  33996. + *
  33997. + * Revision 1.34 1999/11/18 04:13:56 rgb
  33998. + * Replaced all kernel version macros to shorter, readable form.
  33999. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  34000. + *
  34001. + * Revision 1.33 1999/11/17 15:53:39 rgb
  34002. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  34003. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  34004. + * klips/net/ipsec/Makefile.
  34005. + *
  34006. + * Revision 1.32 1999/10/26 13:58:33 rgb
  34007. + * Put spinlock flags variable declaration outside the debug compiler
  34008. + * directive to enable compilation with debug shut off.
  34009. + *
  34010. + * Revision 1.31 1999/10/15 22:13:29 rgb
  34011. + * Clean out cruft.
  34012. + * Align /proc/net/ipsec_eroute output for easier readability.
  34013. + * Fix double linefeed in radij debug output.
  34014. + * Fix double locking bug that locks up 2.0.36 but not 2.0.38.
  34015. + *
  34016. + * Revision 1.30 1999/10/08 18:37:33 rgb
  34017. + * Fix end-of-line spacing to sate whining PHMs.
  34018. + *
  34019. + * Revision 1.29 1999/10/03 18:52:45 rgb
  34020. + * Spinlock support for 2.0.xx.
  34021. + * Dumb return code spin_unlock fix.
  34022. + *
  34023. + * Revision 1.28 1999/10/01 16:22:24 rgb
  34024. + * Switch from assignment init. to functional init. of spinlocks.
  34025. + *
  34026. + * Revision 1.27 1999/10/01 15:44:53 rgb
  34027. + * Move spinlock header include to 2.1> scope.
  34028. + *
  34029. + * Revision 1.26 1999/10/01 00:01:23 rgb
  34030. + * Added eroute structure locking.
  34031. + *
  34032. + * Revision 1.25 1999/06/10 16:07:30 rgb
  34033. + * Silence delete eroute on no debug.
  34034. + *
  34035. + * Revision 1.24 1999/05/09 03:25:36 rgb
  34036. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  34037. + *
  34038. + * Revision 1.23 1999/05/05 22:02:31 rgb
  34039. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  34040. + *
  34041. + * Revision 1.22 1999/04/29 15:17:23 rgb
  34042. + * Add return values to init and cleanup functions.
  34043. + * Add sanity checking for null pointer arguments.
  34044. + *
  34045. + * Revision 1.21 1999/04/11 00:28:58 henry
  34046. + * GPL boilerplate
  34047. + *
  34048. + * Revision 1.20 1999/04/06 04:54:26 rgb
  34049. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  34050. + * patch shell fixes.
  34051. + *
  34052. + * Revision 1.19 1999/02/17 16:50:35 rgb
  34053. + * Clean out unused cruft.
  34054. + * Consolidate for space and speed efficiency.
  34055. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  34056. + *
  34057. + * Revision 1.18 1999/01/22 06:22:06 rgb
  34058. + * Cruft clean-out.
  34059. + * 64-bit clean-up.
  34060. + *
  34061. + * Revision 1.17 1998/12/02 03:09:39 rgb
  34062. + * Clean up debug printing conditionals to compile with debugging off.
  34063. + *
  34064. + * Revision 1.16 1998/12/01 13:49:39 rgb
  34065. + * Wrap version info printing in debug switches.
  34066. + *
  34067. + * Revision 1.15 1998/11/30 13:22:54 rgb
  34068. + * Rationalised all the klips kernel file headers. They are much shorter
  34069. + * now and won't conflict under RH5.2.
  34070. + *
  34071. + * Revision 1.14 1998/10/31 06:48:17 rgb
  34072. + * Fixed up comments in #endif directives.
  34073. + *
  34074. + * Revision 1.13 1998/10/27 13:48:09 rgb
  34075. + * Cleaned up /proc/net/ipsec_* filesystem for easy parsing by scripts.
  34076. + * Fixed less(1) truncated output bug.
  34077. + * Code clean-up.
  34078. + *
  34079. + * Revision 1.12 1998/10/25 02:41:36 rgb
  34080. + * Change return type on ipsec_breakroute and ipsec_makeroute and add an
  34081. + * argument to be able to transmit more infomation about errors.
  34082. + * Fix cut-and-paste debug statement identifier.
  34083. + *
  34084. + * Revision 1.11 1998/10/22 06:45:39 rgb
  34085. + * Cleaned up cruft.
  34086. + * Convert to use satoa for printk.
  34087. + *
  34088. + * Revision 1.10 1998/10/19 14:44:28 rgb
  34089. + * Added inclusion of freeswan.h.
  34090. + * sa_id structure implemented and used: now includes protocol.
  34091. + *
  34092. + * Revision 1.9 1998/10/09 04:30:52 rgb
  34093. + * Added 'klips_debug' prefix to all klips printk debug statements.
  34094. + * Deleted old commented out cruft.
  34095. + *
  34096. + * Revision 1.8 1998/08/06 17:24:23 rgb
  34097. + * Fix addrtoa return code bug from stale manpage advice preventing packets
  34098. + * from being erouted.
  34099. + *
  34100. + * Revision 1.7 1998/08/06 07:44:59 rgb
  34101. + * Fixed /proc/net/ipsec_eroute subnettoa and addrtoa return value bug that
  34102. + * ended up in nothing being printed.
  34103. + *
  34104. + * Revision 1.6 1998/08/05 22:16:41 rgb
  34105. + * Cleanup to prevent cosmetic errors (ie. debug output) from being fatal.
  34106. + *
  34107. + * Revision 1.5 1998/07/29 20:38:44 rgb
  34108. + * Debug and fix subnettoa and addrtoa output.
  34109. + *
  34110. + * Revision 1.4 1998/07/28 00:02:39 rgb
  34111. + * Converting to exclusive use of addrtoa.
  34112. + * Fix eroute delete.
  34113. + *
  34114. + * Revision 1.3 1998/07/14 18:21:26 rgb
  34115. + * Add function to clear the eroute table.
  34116. + *
  34117. + * Revision 1.2 1998/06/23 02:59:14 rgb
  34118. + * Added debugging output to eroute add/delete routines.
  34119. + *
  34120. + * Revision 1.9 1998/06/18 21:29:06 henry
  34121. + * move sources from klips/src to klips/net/ipsec, to keep stupid kernel
  34122. + * build scripts happier in presence of symbolic links
  34123. + *
  34124. + * Revision 1.8 1998/06/05 02:32:26 rgb
  34125. + * Fix spi ntoh kernel debug output.
  34126. + *
  34127. + * Revision 1.7 1998/05/25 20:30:37 rgb
  34128. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  34129. + *
  34130. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  34131. + * add ipsec_rj_walker_delete.
  34132. + *
  34133. + * Revision 1.6 1998/05/21 13:08:57 rgb
  34134. + * Rewrote procinfo subroutines to avoid *bad things* when more that 3k of
  34135. + * information is available for printout.
  34136. + *
  34137. + * Revision 1.5 1998/05/18 21:35:55 rgb
  34138. + * Clean up output for numerical consistency and readability. Zero freed
  34139. + * eroute memory.
  34140. + *
  34141. + * Revision 1.4 1998/04/21 21:28:58 rgb
  34142. + * Rearrange debug switches to change on the fly debug output from user
  34143. + * space. Only kernel changes checked in at this time. radij.c was also
  34144. + * changed to temporarily remove buggy debugging code in rj_delete causing
  34145. + * an OOPS and hence, netlink device open errors.
  34146. + *
  34147. + * Revision 1.3 1998/04/14 17:30:39 rgb
  34148. + * Fix up compiling errors for radij tree memory reclamation.
  34149. + *
  34150. + * Revision 1.2 1998/04/12 22:03:23 rgb
  34151. + * Updated ESP-3DES-HMAC-MD5-96,
  34152. + * ESP-DES-HMAC-MD5-96,
  34153. + * AH-HMAC-MD5-96,
  34154. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  34155. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  34156. + *
  34157. + * Fixed eroute references in /proc/net/ipsec*.
  34158. + *
  34159. + * Started to patch module unloading memory leaks in ipsec_netlink and
  34160. + * radij tree unloading.
  34161. + *
  34162. + * Revision 1.1 1998/04/09 03:06:10 henry
  34163. + * sources moved up from linux/net/ipsec
  34164. + *
  34165. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  34166. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  34167. + *
  34168. + * Revision 0.4 1997/01/15 01:28:15 ji
  34169. + * No changes.
  34170. + *
  34171. + * Revision 0.3 1996/11/20 14:39:04 ji
  34172. + * Minor cleanups.
  34173. + * Rationalized debugging code.
  34174. + *
  34175. + * Revision 0.2 1996/11/02 00:18:33 ji
  34176. + * First limited release.
  34177. + *
  34178. + *
  34179. + */
  34180. --- /dev/null Tue Mar 11 13:02:56 2003
  34181. +++ linux/net/ipsec/ipsec_rcv.c Mon Feb 9 13:51:03 2004
  34182. @@ -0,0 +1,2317 @@
  34183. +/*
  34184. + * receive code
  34185. + * Copyright (C) 1996, 1997 John Ioannidis.
  34186. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  34187. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  34188. + *
  34189. + * This program is free software; you can redistribute it and/or modify it
  34190. + * under the terms of the GNU General Public License as published by the
  34191. + * Free Software Foundation; either version 2 of the License, or (at your
  34192. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  34193. + *
  34194. + * This program is distributed in the hope that it will be useful, but
  34195. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  34196. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  34197. + * for more details.
  34198. + */
  34199. +
  34200. +char ipsec_rcv_c_version[] = "RCSID $Id: ipsec_rcv.c,v 1.171.2.11 2007/04/28 20:46:40 paul Exp $";
  34201. +
  34202. +#ifndef AUTOCONF_INCLUDED
  34203. +#include <linux/config.h>
  34204. +#endif
  34205. +#include <linux/version.h>
  34206. +
  34207. +#define __NO_VERSION__
  34208. +#include <linux/module.h>
  34209. +#include <linux/kernel.h> /* printk() */
  34210. +
  34211. +#include "openswan/ipsec_param.h"
  34212. +
  34213. +#ifdef MALLOC_SLAB
  34214. +# include <linux/slab.h> /* kmalloc() */
  34215. +#else /* MALLOC_SLAB */
  34216. +# include <linux/malloc.h> /* kmalloc() */
  34217. +#endif /* MALLOC_SLAB */
  34218. +#include <linux/errno.h> /* error codes */
  34219. +#include <linux/types.h> /* size_t */
  34220. +#include <linux/interrupt.h> /* mark_bh */
  34221. +
  34222. +#include <linux/netdevice.h> /* struct device, and other headers */
  34223. +#include <linux/etherdevice.h> /* eth_type_trans */
  34224. +#include <linux/ip.h> /* struct iphdr */
  34225. +
  34226. +#include <net/tcp.h>
  34227. +#include <net/udp.h>
  34228. +#include <linux/skbuff.h>
  34229. +#include <openswan.h>
  34230. +#ifdef SPINLOCK
  34231. +# ifdef SPINLOCK_23
  34232. +# include <linux/spinlock.h> /* *lock* */
  34233. +# else /* SPINLOCK_23 */
  34234. +# include <asm/spinlock.h> /* *lock* */
  34235. +# endif /* SPINLOCK_23 */
  34236. +#endif /* SPINLOCK */
  34237. +
  34238. +#include <net/ip.h>
  34239. +
  34240. +#include "openswan/ipsec_kern24.h"
  34241. +#include "openswan/radij.h"
  34242. +#include "openswan/ipsec_encap.h"
  34243. +#include "openswan/ipsec_sa.h"
  34244. +
  34245. +#include "openswan/ipsec_radij.h"
  34246. +#include "openswan/ipsec_xform.h"
  34247. +#include "openswan/ipsec_tunnel.h"
  34248. +#include "openswan/ipsec_rcv.h"
  34249. +
  34250. +#include "openswan/ipsec_auth.h"
  34251. +
  34252. +#include "openswan/ipsec_esp.h"
  34253. +
  34254. +#ifdef CONFIG_KLIPS_AH
  34255. +#include "openswan/ipsec_ah.h"
  34256. +#endif /* CONFIG_KLIPS_AH */
  34257. +
  34258. +#ifdef CONFIG_KLIPS_IPCOMP
  34259. +#include "openswan/ipsec_ipcomp.h"
  34260. +#endif /* CONFIG_KLIPS_COMP */
  34261. +
  34262. +#include <pfkeyv2.h>
  34263. +#include <pfkey.h>
  34264. +
  34265. +#include "openswan/ipsec_proto.h"
  34266. +#include "openswan/ipsec_alg.h"
  34267. +#include "openswan/ipsec_kern24.h"
  34268. +
  34269. +#ifdef CONFIG_KLIPS_DEBUG
  34270. +int debug_rcv = 0;
  34271. +#endif /* CONFIG_KLIPS_DEBUG */
  34272. +
  34273. +int sysctl_ipsec_inbound_policy_check = 1;
  34274. +
  34275. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34276. +#include <linux/udp.h>
  34277. +#endif
  34278. +
  34279. +/* This is a private use protocol, and AT&T should be ashamed. They should have
  34280. + * used protocol # 59, which is "no next header" instead of 0xFE.
  34281. + */
  34282. +#ifndef IPPROTO_ATT_HEARTBEAT
  34283. +#define IPPROTO_ATT_HEARTBEAT 0xFE
  34284. +#endif
  34285. +
  34286. +/*
  34287. + * Check-replay-window routine, adapted from the original
  34288. + * by J. Hughes, from draft-ietf-ipsec-esp-des-md5-03.txt
  34289. + *
  34290. + * This is a routine that implements a 64 packet window. This is intend-
  34291. + * ed on being an implementation sample.
  34292. + */
  34293. +
  34294. +DEBUG_NO_STATIC int
  34295. +ipsec_checkreplaywindow(struct ipsec_sa*ipsp, __u32 seq)
  34296. +{
  34297. + __u32 diff;
  34298. +
  34299. + if (ipsp->ips_replaywin == 0) /* replay shut off */
  34300. + return 1;
  34301. + if (seq == 0)
  34302. + return 0; /* first == 0 or wrapped */
  34303. +
  34304. + /* new larger sequence number */
  34305. + if (seq > ipsp->ips_replaywin_lastseq) {
  34306. + return 1; /* larger is good */
  34307. + }
  34308. + diff = ipsp->ips_replaywin_lastseq - seq;
  34309. +
  34310. + /* too old or wrapped */ /* if wrapped, kill off SA? */
  34311. + if (diff >= ipsp->ips_replaywin) {
  34312. + return 0;
  34313. + }
  34314. + /* this packet already seen */
  34315. + if (ipsp->ips_replaywin_bitmap & (1 << diff))
  34316. + return 0;
  34317. + return 1; /* out of order but good */
  34318. +}
  34319. +
  34320. +DEBUG_NO_STATIC int
  34321. +ipsec_updatereplaywindow(struct ipsec_sa*ipsp, __u32 seq)
  34322. +{
  34323. + __u32 diff;
  34324. +
  34325. + if (ipsp->ips_replaywin == 0) /* replay shut off */
  34326. + return 1;
  34327. + if (seq == 0)
  34328. + return 0; /* first == 0 or wrapped */
  34329. +
  34330. + /* new larger sequence number */
  34331. + if (seq > ipsp->ips_replaywin_lastseq) {
  34332. + diff = seq - ipsp->ips_replaywin_lastseq;
  34333. +
  34334. + /* In win, set bit for this pkt */
  34335. + if (diff < ipsp->ips_replaywin)
  34336. + ipsp->ips_replaywin_bitmap =
  34337. + (ipsp->ips_replaywin_bitmap << diff) | 1;
  34338. + else
  34339. + /* This packet has way larger seq num */
  34340. + ipsp->ips_replaywin_bitmap = 1;
  34341. +
  34342. + if(seq - ipsp->ips_replaywin_lastseq - 1 > ipsp->ips_replaywin_maxdiff) {
  34343. + ipsp->ips_replaywin_maxdiff = seq - ipsp->ips_replaywin_lastseq - 1;
  34344. + }
  34345. + ipsp->ips_replaywin_lastseq = seq;
  34346. + return 1; /* larger is good */
  34347. + }
  34348. + diff = ipsp->ips_replaywin_lastseq - seq;
  34349. +
  34350. + /* too old or wrapped */ /* if wrapped, kill off SA? */
  34351. + if (diff >= ipsp->ips_replaywin) {
  34352. +/*
  34353. + if(seq < 0.25*max && ipsp->ips_replaywin_lastseq > 0.75*max) {
  34354. + ipsec_sa_delchain(ipsp);
  34355. + }
  34356. +*/
  34357. + return 0;
  34358. + }
  34359. + /* this packet already seen */
  34360. + if (ipsp->ips_replaywin_bitmap & (1 << diff))
  34361. + return 0;
  34362. + ipsp->ips_replaywin_bitmap |= (1 << diff); /* mark as seen */
  34363. + return 1; /* out of order but good */
  34364. +}
  34365. +
  34366. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  34367. +struct auth_alg ipsec_rcv_md5[]={
  34368. + {osMD5Init, osMD5Update, osMD5Final, AHMD596_ALEN}
  34369. +};
  34370. +
  34371. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34372. +
  34373. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  34374. +struct auth_alg ipsec_rcv_sha1[]={
  34375. + {SHA1Init, SHA1Update, SHA1Final, AHSHA196_ALEN}
  34376. +};
  34377. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34378. +
  34379. +/*
  34380. + * decapsulate a single layer of the system
  34381. + *
  34382. + * the following things should be setup to enter this function.
  34383. + *
  34384. + * irs->stats == stats structure (or NULL)
  34385. + * irs->ipp = IP header.
  34386. + * irs->len = total length of packet
  34387. + * skb->nh.iph = ipp;
  34388. + * skb->h.raw = start of payload
  34389. + * irs->ipsp = NULL.
  34390. + * irs->iphlen = N/A = is recalculated.
  34391. + * irs->ilen = 0;
  34392. + * irs->authlen = 0;
  34393. + * irs->authfuncs = NULL;
  34394. + * irs->skb = the skb;
  34395. + *
  34396. + * proto_funcs should be from ipsec_esp.c, ipsec_ah.c or ipsec_ipcomp.c.
  34397. + *
  34398. + */
  34399. +enum ipsec_rcv_value
  34400. +ipsec_rcv_decap_once(struct ipsec_rcv_state *irs
  34401. + , struct xform_functions *proto_funcs)
  34402. +{
  34403. + int iphlen;
  34404. + __u8 proto;
  34405. + struct in_addr ipsaddr;
  34406. + struct in_addr ipdaddr;
  34407. + int replay = 0; /* replay value in AH or ESP packet */
  34408. + struct ipsec_sa* ipsnext = NULL; /* next SA towards inside of packet */
  34409. + struct ipsec_sa *newipsp;
  34410. + struct iphdr *ipp;
  34411. + struct sk_buff *skb;
  34412. + struct ipsec_alg_auth *ixt_a=NULL;
  34413. +
  34414. + skb = irs->skb;
  34415. + irs->len = skb->len;
  34416. + ipp = irs->ipp;
  34417. + proto = ipp->protocol;
  34418. + ipsaddr.s_addr = ipp->saddr;
  34419. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  34420. + ipdaddr.s_addr = ipp->daddr;
  34421. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  34422. +
  34423. + iphlen = ipp->ihl << 2;
  34424. + irs->iphlen=iphlen;
  34425. + ipp->check = 0; /* we know the sum is good */
  34426. +
  34427. + KLIPS_PRINT(debug_rcv,
  34428. + "klips_debug:ipsec_rcv_decap_once: "
  34429. + "decap (%d) from %s -> %s\n",
  34430. + proto, irs->ipsaddr_txt, irs->ipdaddr_txt);
  34431. +
  34432. + /*
  34433. + * Find tunnel control block and (indirectly) call the
  34434. + * appropriate tranform routine. The resulting sk_buf
  34435. + * is a valid IP packet ready to go through input processing.
  34436. + */
  34437. +
  34438. + irs->said.dst.u.v4.sin_addr.s_addr = ipp->daddr;
  34439. + irs->said.dst.u.v4.sin_family = AF_INET;
  34440. +
  34441. + /* note: rcv_checks set up the said.spi value, if appropriate */
  34442. + if(proto_funcs->rcv_checks) {
  34443. + enum ipsec_rcv_value retval =
  34444. + (*proto_funcs->rcv_checks)(irs, skb);
  34445. +
  34446. + if(retval < 0) {
  34447. + return retval;
  34448. + }
  34449. + }
  34450. +
  34451. + irs->said.proto = proto;
  34452. + irs->sa_len = satot(&irs->said, 0, irs->sa, sizeof(irs->sa));
  34453. + if(irs->sa_len == 0) {
  34454. + strcpy(irs->sa, "(error)");
  34455. + }
  34456. +
  34457. + newipsp = ipsec_sa_getbyid(&irs->said);
  34458. + if (newipsp == NULL) {
  34459. + KLIPS_PRINT(debug_rcv,
  34460. + "klips_debug:ipsec_rcv: "
  34461. + "no ipsec_sa for SA:%s: incoming packet with no SA dropped\n",
  34462. + irs->sa_len ? irs->sa : " (error)");
  34463. + if(irs->stats) {
  34464. + irs->stats->rx_dropped++;
  34465. + }
  34466. + return IPSEC_RCV_SAIDNOTFOUND;
  34467. + }
  34468. +
  34469. + /* MCR - XXX this is bizarre. ipsec_sa_getbyid returned it, having
  34470. + * incremented the refcount, why in the world would we decrement it
  34471. + * here? */
  34472. + /* ipsec_sa_put(irs->ipsp);*/ /* incomplete */
  34473. +
  34474. + /* If it is in larval state, drop the packet, we cannot process yet. */
  34475. + if(newipsp->ips_state == SADB_SASTATE_LARVAL) {
  34476. + KLIPS_PRINT(debug_rcv,
  34477. + "klips_debug:ipsec_rcv: "
  34478. + "ipsec_sa in larval state, cannot be used yet, dropping packet.\n");
  34479. + if(irs->stats) {
  34480. + irs->stats->rx_dropped++;
  34481. + }
  34482. + ipsec_sa_put(newipsp);
  34483. + return IPSEC_RCV_SAIDNOTLIVE;
  34484. + }
  34485. +
  34486. + if(newipsp->ips_state == SADB_SASTATE_DEAD) {
  34487. + KLIPS_PRINT(debug_rcv,
  34488. + "klips_debug:ipsec_rcv: "
  34489. + "ipsec_sa in dead state, cannot be used any more, dropping packet.\n");
  34490. + if(irs->stats) {
  34491. + irs->stats->rx_dropped++;
  34492. + }
  34493. + ipsec_sa_put(newipsp);
  34494. + return IPSEC_RCV_SAIDNOTLIVE;
  34495. + }
  34496. +
  34497. + if(sysctl_ipsec_inbound_policy_check) {
  34498. + if(irs->ipp->saddr != ((struct sockaddr_in*)(newipsp->ips_addr_s))->sin_addr.s_addr) {
  34499. + KLIPS_PRINT(debug_rcv,
  34500. + "klips_debug:ipsec_rcv: "
  34501. + "SA:%s, src=%s of pkt does not agree with expected SA source address policy.\n",
  34502. + irs->sa_len ? irs->sa : " (error)",
  34503. + irs->ipsaddr_txt);
  34504. + if(irs->stats) {
  34505. + irs->stats->rx_dropped++;
  34506. + }
  34507. + ipsec_sa_put(newipsp);
  34508. + return IPSEC_RCV_FAILEDINBOUND;
  34509. + }
  34510. +
  34511. + KLIPS_PRINT(debug_rcv,
  34512. + "klips_debug:ipsec_rcv: "
  34513. + "SA:%s, src=%s of pkt agrees with expected SA source address policy.\n",
  34514. + irs->sa_len ? irs->sa : " (error)",
  34515. + irs->ipsaddr_txt);
  34516. +
  34517. + /*
  34518. + * at this point, we have looked up a new SA, and we want to make sure that if this
  34519. + * isn't the first SA in the list, that the previous SA actually points at this one.
  34520. + */
  34521. + if(irs->ipsp) {
  34522. + if(irs->ipsp->ips_inext != newipsp) {
  34523. + KLIPS_PRINT(debug_rcv,
  34524. + "klips_debug:ipsec_rcv: "
  34525. + "unexpected SA:%s: does not agree with ips->inext policy, dropped\n",
  34526. + irs->sa_len ? irs->sa : " (error)");
  34527. + if(irs->stats) {
  34528. + irs->stats->rx_dropped++;
  34529. + }
  34530. + ipsec_sa_put(newipsp);
  34531. + return IPSEC_RCV_FAILEDINBOUND;
  34532. + }
  34533. + KLIPS_PRINT(debug_rcv,
  34534. + "klips_debug:ipsec_rcv: "
  34535. + "SA:%s grouping from previous SA is OK.\n",
  34536. + irs->sa_len ? irs->sa : " (error)");
  34537. + } else {
  34538. + KLIPS_PRINT(debug_rcv,
  34539. + "klips_debug:ipsec_rcv: "
  34540. + "SA:%s First SA in group.\n",
  34541. + irs->sa_len ? irs->sa : " (error)");
  34542. + }
  34543. +
  34544. +
  34545. +
  34546. +
  34547. +
  34548. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34549. + if (proto == IPPROTO_ESP) {
  34550. + KLIPS_PRINT(debug_rcv,
  34551. + "klips_debug:ipsec_rcv: "
  34552. + "natt_type=%u tdbp->ips_natt_type=%u : %s\n",
  34553. + irs->natt_type, newipsp->ips_natt_type,
  34554. + (irs->natt_type==newipsp->ips_natt_type)?"ok":"bad");
  34555. + if (irs->natt_type != newipsp->ips_natt_type) {
  34556. + KLIPS_PRINT(debug_rcv,
  34557. + "klips_debug:ipsec_rcv: "
  34558. + "SA:%s does not agree with expected NAT-T policy.\n",
  34559. + irs->sa_len ? irs->sa : " (error)");
  34560. + if(irs->stats) {
  34561. + irs->stats->rx_dropped++;
  34562. + }
  34563. + ipsec_sa_put(newipsp);
  34564. + return IPSEC_RCV_FAILEDINBOUND;
  34565. + }
  34566. + }
  34567. +#endif
  34568. + }
  34569. +
  34570. + /* okay, SA checks out, so free any previous SA, and record a new one*/
  34571. +
  34572. + if(irs->ipsp) {
  34573. + ipsec_sa_put(irs->ipsp);
  34574. + }
  34575. + irs->ipsp=newipsp;
  34576. +
  34577. + /* note that the outer code will free the irs->ipsp
  34578. + if there is an error */
  34579. +
  34580. +
  34581. + /* now check the lifetimes */
  34582. + if(ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_bytes, "bytes",
  34583. + irs->sa, ipsec_life_countbased, ipsec_incoming,
  34584. + irs->ipsp) == ipsec_life_harddied ||
  34585. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_addtime, "addtime",
  34586. + irs->sa, ipsec_life_timebased, ipsec_incoming,
  34587. + irs->ipsp) == ipsec_life_harddied ||
  34588. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_addtime, "usetime",
  34589. + irs->sa, ipsec_life_timebased, ipsec_incoming,
  34590. + irs->ipsp) == ipsec_life_harddied ||
  34591. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_packets, "packets",
  34592. + irs->sa, ipsec_life_countbased, ipsec_incoming,
  34593. + irs->ipsp) == ipsec_life_harddied) {
  34594. + ipsec_sa_delchain(irs->ipsp);
  34595. + if(irs->stats) {
  34596. + irs->stats->rx_dropped++;
  34597. + }
  34598. +
  34599. + KLIPS_PRINT(debug_rcv,
  34600. + "klips_debug:ipsec_rcv_decap_once: "
  34601. + "decap (%d) failed lifetime check\n",
  34602. + proto);
  34603. +
  34604. + return IPSEC_RCV_LIFETIMEFAILED;
  34605. + }
  34606. +
  34607. +#if 0
  34608. + /*
  34609. + * This is removed for some reasons:
  34610. + * 1) it needs to happen *after* authentication.
  34611. + * 2) do we really care, if it authenticates, if it came
  34612. + * from the wrong location?
  34613. + * 3) the NAT_KA messages in IKE will also get to pluto
  34614. + * and it will figure out that stuff has moved.
  34615. + * 4) the 2.6 udp-esp encap function does not pass us
  34616. + * the originating port number, and I can't tell
  34617. + * if skb->sk is guaranteed to be valid here.
  34618. + * 2005-04-16: mcr@xelerance.com
  34619. + */
  34620. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34621. + /*
  34622. + *
  34623. + * XXX we should ONLY update pluto if the SA passes all checks,
  34624. + * which we clearly do not now.
  34625. + */
  34626. + if ((irs->natt_type) &&
  34627. + ( (irs->ipp->saddr != (((struct sockaddr_in*)(newipsp->ips_addr_s))->sin_addr.s_addr)) ||
  34628. + (irs->natt_sport != newipsp->ips_natt_sport)
  34629. + )) {
  34630. + struct sockaddr sipaddr;
  34631. + struct sockaddr_in *psin = (struct sockaddr_in*)(newipsp->ips_addr_s);
  34632. +
  34633. + /** Advertise NAT-T addr change to pluto **/
  34634. + sipaddr.sa_family = AF_INET;
  34635. + ((struct sockaddr_in*)&sipaddr)->sin_addr.s_addr = irs->ipp->saddr;
  34636. + ((struct sockaddr_in*)&sipaddr)->sin_port = htons(irs->natt_sport);
  34637. + pfkey_nat_t_new_mapping(newipsp, &sipaddr, irs->natt_sport);
  34638. +
  34639. + /**
  34640. + * Then allow or block packet depending on
  34641. + * sysctl_ipsec_inbound_policy_check.
  34642. + *
  34643. + * In all cases, pluto will update SA if new mapping is
  34644. + * accepted.
  34645. + */
  34646. + if (sysctl_ipsec_inbound_policy_check) {
  34647. + KLIPS_PRINT(debug_rcv,
  34648. + "klips_debug:ipsec_rcv: "
  34649. + "SA:%s, src=%s:%u of pkt does not agree with expected "
  34650. + "SA source address [%08x:%u] (notifying pluto of change).\n",
  34651. + irs->sa_len ? irs->sa : " (error)",
  34652. + irs->ipsaddr_txt, irs->natt_sport,
  34653. + psin->sin_addr.s_addr,
  34654. + newipsp->ips_natt_sport);
  34655. + if(irs->stats) {
  34656. + irs->stats->rx_dropped++;
  34657. + }
  34658. + ipsec_sa_put(newipsp);
  34659. + return IPSEC_RCV_FAILEDINBOUND;
  34660. + }
  34661. + }
  34662. +#endif
  34663. +#endif
  34664. +
  34665. + irs->authfuncs=NULL;
  34666. +
  34667. + /* authenticate, if required */
  34668. + if ((ixt_a=irs->ipsp->ips_alg_auth)) {
  34669. + irs->authlen = AHHMAC_HASHLEN;
  34670. + irs->authfuncs = NULL;
  34671. + irs->ictx = NULL;
  34672. + irs->octx = NULL;
  34673. + irs->ictx_len = 0;
  34674. + irs->octx_len = 0;
  34675. + KLIPS_PRINT(debug_rcv,
  34676. + "klips_debug:ipsec_rcv: "
  34677. + "authalg=%d authlen=%d\n",
  34678. + irs->ipsp->ips_authalg,
  34679. + irs->authlen);
  34680. + } else
  34681. + switch(irs->ipsp->ips_authalg) {
  34682. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  34683. + case AH_MD5:
  34684. + irs->authlen = AHHMAC_HASHLEN;
  34685. + irs->authfuncs = ipsec_rcv_md5;
  34686. + irs->ictx = (void *)&((struct md5_ctx*)(irs->ipsp->ips_key_a))->ictx;
  34687. + irs->octx = (void *)&((struct md5_ctx*)(irs->ipsp->ips_key_a))->octx;
  34688. + irs->ictx_len = sizeof(((struct md5_ctx*)(irs->ipsp->ips_key_a))->ictx);
  34689. + irs->octx_len = sizeof(((struct md5_ctx*)(irs->ipsp->ips_key_a))->octx);
  34690. + break;
  34691. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34692. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  34693. + case AH_SHA:
  34694. + irs->authlen = AHHMAC_HASHLEN;
  34695. + irs->authfuncs = ipsec_rcv_sha1;
  34696. + irs->ictx = (void *)&((struct sha1_ctx*)(irs->ipsp->ips_key_a))->ictx;
  34697. + irs->octx = (void *)&((struct sha1_ctx*)(irs->ipsp->ips_key_a))->octx;
  34698. + irs->ictx_len = sizeof(((struct sha1_ctx*)(irs->ipsp->ips_key_a))->ictx);
  34699. + irs->octx_len = sizeof(((struct sha1_ctx*)(irs->ipsp->ips_key_a))->octx);
  34700. + break;
  34701. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  34702. + case AH_NONE:
  34703. + irs->authlen = 0;
  34704. + irs->authfuncs = NULL;
  34705. + irs->ictx = NULL;
  34706. + irs->octx = NULL;
  34707. + irs->ictx_len = 0;
  34708. + irs->octx_len = 0;
  34709. + break;
  34710. + default:
  34711. + irs->ipsp->ips_errs.ips_alg_errs += 1;
  34712. + if(irs->stats) {
  34713. + irs->stats->rx_errors++;
  34714. + }
  34715. + return IPSEC_RCV_BADAUTH;
  34716. + }
  34717. +
  34718. + /* ilen counts number of bytes in ESP portion */
  34719. + irs->ilen = ((skb->data + skb->len) - skb->h.raw) - irs->authlen;
  34720. + if(irs->ilen <= 0) {
  34721. + KLIPS_PRINT(debug_rcv,
  34722. + "klips_debug:ipsec_rcv: "
  34723. + "runt %s packet with no data, dropping.\n",
  34724. + (proto == IPPROTO_ESP ? "esp" : "ah"));
  34725. + if(irs->stats) {
  34726. + irs->stats->rx_dropped++;
  34727. + }
  34728. + return IPSEC_RCV_BADLEN;
  34729. + }
  34730. +
  34731. + if(irs->authfuncs || ixt_a) {
  34732. + unsigned char *authenticator = NULL;
  34733. +
  34734. + if(proto_funcs->rcv_setup_auth) {
  34735. + enum ipsec_rcv_value retval
  34736. + = (*proto_funcs->rcv_setup_auth)(irs, skb,
  34737. + &replay,
  34738. + &authenticator);
  34739. + if(retval < 0) {
  34740. + return retval;
  34741. + }
  34742. + }
  34743. +
  34744. + if(!authenticator) {
  34745. + irs->ipsp->ips_errs.ips_auth_errs += 1;
  34746. + if(irs->stats) {
  34747. + irs->stats->rx_dropped++;
  34748. + }
  34749. + return IPSEC_RCV_BADAUTH;
  34750. + }
  34751. +
  34752. + if(!ipsec_checkreplaywindow(irs->ipsp, replay)) {
  34753. + irs->ipsp->ips_errs.ips_replaywin_errs += 1;
  34754. + KLIPS_PRINT(debug_rcv & DB_RX_REPLAY,
  34755. + "klips_debug:ipsec_rcv: "
  34756. + "duplicate frame from %s, packet dropped\n",
  34757. + irs->ipsaddr_txt);
  34758. + if(irs->stats) {
  34759. + irs->stats->rx_dropped++;
  34760. + }
  34761. + return IPSEC_RCV_REPLAYFAILED;
  34762. + }
  34763. +
  34764. + /*
  34765. + * verify authenticator
  34766. + */
  34767. +
  34768. + KLIPS_PRINT(debug_rcv,
  34769. + "klips_debug:ipsec_rcv: "
  34770. + "encalg = %d, authalg = %d.\n",
  34771. + irs->ipsp->ips_encalg,
  34772. + irs->ipsp->ips_authalg);
  34773. +
  34774. + /* calculate authenticator */
  34775. + if(proto_funcs->rcv_calc_auth == NULL) {
  34776. + return IPSEC_RCV_BADAUTH;
  34777. + }
  34778. + (*proto_funcs->rcv_calc_auth)(irs, skb);
  34779. +
  34780. + if (memcmp(irs->hash, authenticator, irs->authlen)) {
  34781. + irs->ipsp->ips_errs.ips_auth_errs += 1;
  34782. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  34783. + "klips_debug:ipsec_rcv: "
  34784. + "auth failed on incoming packet from %s: hash=%08x%08x%08x auth=%08x%08x%08x, dropped\n",
  34785. + irs->ipsaddr_txt,
  34786. + ntohl(*(__u32*)&irs->hash[0]),
  34787. + ntohl(*(__u32*)&irs->hash[4]),
  34788. + ntohl(*(__u32*)&irs->hash[8]),
  34789. + ntohl(*(__u32*)authenticator),
  34790. + ntohl(*((__u32*)authenticator + 1)),
  34791. + ntohl(*((__u32*)authenticator + 2)));
  34792. + if(irs->stats) {
  34793. + irs->stats->rx_dropped++;
  34794. + }
  34795. + return IPSEC_RCV_AUTHFAILED;
  34796. + } else {
  34797. + KLIPS_PRINT(debug_rcv,
  34798. + "klips_debug:ipsec_rcv: "
  34799. + "authentication successful.\n");
  34800. + }
  34801. +
  34802. + /* Crypto hygiene: clear memory used to calculate autheticator.
  34803. + * The length varies with the algorithm.
  34804. + */
  34805. + memset(irs->hash, 0, irs->authlen);
  34806. +
  34807. + /* If the sequence number == 0, expire SA, it had rolled */
  34808. + if(irs->ipsp->ips_replaywin && !replay /* !irs->ipsp->ips_replaywin_lastseq */) {
  34809. + ipsec_sa_delchain(irs->ipsp);
  34810. + KLIPS_PRINT(debug_rcv,
  34811. + "klips_debug:ipsec_rcv: "
  34812. + "replay window counter rolled, expiring SA.\n");
  34813. + if(irs->stats) {
  34814. + irs->stats->rx_dropped++;
  34815. + }
  34816. + return IPSEC_RCV_REPLAYROLLED;
  34817. + }
  34818. +
  34819. + /* now update the replay counter */
  34820. + if (!ipsec_updatereplaywindow(irs->ipsp, replay)) {
  34821. + irs->ipsp->ips_errs.ips_replaywin_errs += 1;
  34822. + KLIPS_PRINT(debug_rcv & DB_RX_REPLAY,
  34823. + "klips_debug:ipsec_rcv: "
  34824. + "duplicate frame from %s, packet dropped\n",
  34825. + irs->ipsaddr_txt);
  34826. + if(irs->stats) {
  34827. + irs->stats->rx_dropped++;
  34828. + }
  34829. + return IPSEC_RCV_REPLAYROLLED;
  34830. + }
  34831. + }
  34832. +
  34833. + if(proto_funcs->rcv_decrypt) {
  34834. + enum ipsec_rcv_value retval =
  34835. + (*proto_funcs->rcv_decrypt)(irs);
  34836. +
  34837. + if(retval != IPSEC_RCV_OK) {
  34838. + return retval;
  34839. + }
  34840. + }
  34841. +
  34842. + /*
  34843. + * Adjust pointers
  34844. + */
  34845. + skb = irs->skb;
  34846. + irs->len = skb->len;
  34847. + ipp = irs->ipp = skb->nh.iph;
  34848. + iphlen = ipp->ihl<<2;
  34849. + skb->h.raw = skb->nh.raw + iphlen;
  34850. +
  34851. + /* zero any options that there might be */
  34852. + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  34853. +
  34854. + ipsaddr.s_addr = ipp->saddr;
  34855. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  34856. + ipdaddr.s_addr = ipp->daddr;
  34857. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  34858. +
  34859. + /*
  34860. + * Discard the original ESP/AH header
  34861. + */
  34862. + ipp->protocol = irs->next_header;
  34863. +
  34864. + ipp->check = 0; /* NOTE: this will be included in checksum */
  34865. + ipp->check = ip_fast_csum((unsigned char *)skb->nh.iph, iphlen >> 2);
  34866. +
  34867. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  34868. + "klips_debug:ipsec_rcv: "
  34869. + "after <%s%s%s>, SA:%s:\n",
  34870. + IPS_XFORM_NAME(irs->ipsp),
  34871. + irs->sa_len ? irs->sa : " (error)");
  34872. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, ipp);
  34873. +
  34874. + skb->protocol = htons(ETH_P_IP);
  34875. + skb->ip_summed = 0;
  34876. +
  34877. + ipsnext = irs->ipsp->ips_inext;
  34878. + if(sysctl_ipsec_inbound_policy_check) {
  34879. + if(ipsnext) {
  34880. + if(
  34881. + ipp->protocol != IPPROTO_AH
  34882. + && ipp->protocol != IPPROTO_ESP
  34883. +#ifdef CONFIG_KLIPS_IPCOMP
  34884. + && ipp->protocol != IPPROTO_COMP
  34885. + && (ipsnext->ips_said.proto != IPPROTO_COMP
  34886. + || ipsnext->ips_inext)
  34887. +#endif /* CONFIG_KLIPS_IPCOMP */
  34888. + && ipp->protocol != IPPROTO_IPIP
  34889. + && ipp->protocol != IPPROTO_ATT_HEARTBEAT /* heartbeats to AT&T SIG/GIG */
  34890. + ) {
  34891. + KLIPS_PRINT(debug_rcv,
  34892. + "klips_debug:ipsec_rcv: "
  34893. + "packet with incomplete policy dropped, last successful SA:%s.\n",
  34894. + irs->sa_len ? irs->sa : " (error)");
  34895. + if(irs->stats) {
  34896. + irs->stats->rx_dropped++;
  34897. + }
  34898. + return IPSEC_RCV_FAILEDINBOUND;
  34899. + }
  34900. + KLIPS_PRINT(debug_rcv,
  34901. + "klips_debug:ipsec_rcv: "
  34902. + "SA:%s, Another IPSEC header to process.\n",
  34903. + irs->sa_len ? irs->sa : " (error)");
  34904. + } else {
  34905. + KLIPS_PRINT(debug_rcv,
  34906. + "klips_debug:ipsec_rcv: "
  34907. + "No ips_inext from this SA:%s.\n",
  34908. + irs->sa_len ? irs->sa : " (error)");
  34909. + }
  34910. + }
  34911. +
  34912. +#ifdef CONFIG_KLIPS_IPCOMP
  34913. + /* update ipcomp ratio counters, even if no ipcomp packet is present */
  34914. + if (ipsnext
  34915. + && ipsnext->ips_said.proto == IPPROTO_COMP
  34916. + && ipp->protocol != IPPROTO_COMP) {
  34917. + ipsnext->ips_comp_ratio_cbytes += ntohs(ipp->tot_len);
  34918. + ipsnext->ips_comp_ratio_dbytes += ntohs(ipp->tot_len);
  34919. + }
  34920. +#endif /* CONFIG_KLIPS_IPCOMP */
  34921. +
  34922. + irs->ipsp->ips_life.ipl_bytes.ipl_count += irs->len;
  34923. + irs->ipsp->ips_life.ipl_bytes.ipl_last = irs->len;
  34924. +
  34925. + if(!irs->ipsp->ips_life.ipl_usetime.ipl_count) {
  34926. + irs->ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  34927. + }
  34928. + irs->ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  34929. + irs->ipsp->ips_life.ipl_packets.ipl_count += 1;
  34930. +
  34931. +#ifdef CONFIG_NETFILTER
  34932. + if(proto == IPPROTO_ESP || proto == IPPROTO_AH) {
  34933. + skb->nfmark = (skb->nfmark & (~(IPsecSAref2NFmark(IPSEC_SA_REF_MASK))))
  34934. + | IPsecSAref2NFmark(IPsecSA2SAref(irs->ipsp));
  34935. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  34936. + "klips_debug:ipsec_rcv: "
  34937. + "%s SA sets skb->nfmark=0x%x.\n",
  34938. + proto == IPPROTO_ESP ? "ESP" : "AH",
  34939. + (unsigned)skb->nfmark);
  34940. + }
  34941. +#endif /* CONFIG_NETFILTER */
  34942. +
  34943. + return IPSEC_RCV_OK;
  34944. +}
  34945. +
  34946. +
  34947. +/*
  34948. + * core decapsulation loop for all protocols.
  34949. + *
  34950. + * the following things should be setup to enter this function.
  34951. + *
  34952. + * irs->stats == stats structure (or NULL)
  34953. + * irs->ipp = IP header.
  34954. + * irs->ipsp = NULL.
  34955. + * irs->ilen = 0;
  34956. + * irs->authlen = 0;
  34957. + * irs->authfuncs = NULL;
  34958. + * irs->skb = skb;
  34959. + * skb->nh.iph = ipp;
  34960. + * skb->h.raw = start of payload
  34961. + *
  34962. + */
  34963. +int ipsec_rcv_decap(struct ipsec_rcv_state *irs)
  34964. +{
  34965. + struct ipsec_sa *ipsp = NULL;
  34966. + struct ipsec_sa* ipsnext = NULL;
  34967. + struct in_addr ipsaddr;
  34968. + struct in_addr ipdaddr;
  34969. + struct iphdr *ipp;
  34970. + struct sk_buff *skb = NULL;
  34971. +
  34972. + /* begin decapsulating loop here */
  34973. +
  34974. + /*
  34975. + The spinlock is to prevent any other process from
  34976. + accessing or deleting the ipsec_sa hash table or any of the
  34977. + ipsec_sa s while we are using and updating them.
  34978. +
  34979. + This is not optimal, but was relatively straightforward
  34980. + at the time. A better way to do it has been planned for
  34981. + more than a year, to lock the hash table and put reference
  34982. + counts on each ipsec_sa instead. This is not likely to happen
  34983. + in KLIPS1 unless a volunteer contributes it, but will be
  34984. + designed into KLIPS2.
  34985. + */
  34986. + spin_lock(&tdb_lock);
  34987. +
  34988. + do {
  34989. + int decap_stat;
  34990. + struct xform_functions *proto_funcs;
  34991. +
  34992. + switch(irs->ipp->protocol) {
  34993. + case IPPROTO_ESP:
  34994. + proto_funcs = esp_xform_funcs;
  34995. + break;
  34996. +
  34997. +#ifdef CONFIG_KLIPS_AH
  34998. + case IPPROTO_AH:
  34999. + proto_funcs = ah_xform_funcs;
  35000. + break;
  35001. +#endif /* !CONFIG_KLIPS_AH */
  35002. +
  35003. +#ifdef CONFIG_KLIPS_IPCOMP
  35004. + case IPPROTO_COMP:
  35005. + proto_funcs = ipcomp_xform_funcs;
  35006. + break;
  35007. +#endif /* !CONFIG_KLIPS_IPCOMP */
  35008. + default:
  35009. + if(irs->stats) {
  35010. + irs->stats->rx_errors++;
  35011. + }
  35012. + decap_stat = IPSEC_RCV_BADPROTO;
  35013. + goto rcvleave;
  35014. + }
  35015. +
  35016. + decap_stat = ipsec_rcv_decap_once(irs, proto_funcs);
  35017. +
  35018. + if(decap_stat != IPSEC_RCV_OK) {
  35019. + spin_unlock(&tdb_lock);
  35020. + KLIPS_PRINT(debug_rcv,
  35021. + "klips_debug:ipsec_rcv: decap_once failed: %d\n",
  35022. + decap_stat);
  35023. +
  35024. + goto rcvleave;
  35025. + }
  35026. + /* end decapsulation loop here */
  35027. + } while( (irs->ipp->protocol == IPPROTO_ESP )
  35028. + || (irs->ipp->protocol == IPPROTO_AH )
  35029. +#ifdef CONFIG_KLIPS_IPCOMP
  35030. + || (irs->ipp->protocol == IPPROTO_COMP)
  35031. +#endif /* CONFIG_KLIPS_IPCOMP */
  35032. + );
  35033. +
  35034. + /* set up for decap loop */
  35035. + ipp =irs->ipp;
  35036. + ipsp =irs->ipsp;
  35037. + ipsnext = ipsp->ips_inext;
  35038. + skb = irs->skb;
  35039. +
  35040. + /* if there is an IPCOMP, but we don't have an IPPROTO_COMP,
  35041. + * then we can just skip it
  35042. + */
  35043. +#ifdef CONFIG_KLIPS_IPCOMP
  35044. + if(ipsnext && ipsnext->ips_said.proto == IPPROTO_COMP) {
  35045. + ipsp = ipsnext;
  35046. + ipsnext = ipsp->ips_inext;
  35047. + }
  35048. +#endif /* CONFIG_KLIPS_IPCOMP */
  35049. +
  35050. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  35051. + if ((irs->natt_type) && (ipp->protocol != IPPROTO_IPIP)) {
  35052. + /**
  35053. + * NAT-Traversal and Transport Mode:
  35054. + * we need to correct TCP/UDP checksum
  35055. + *
  35056. + * If we've got NAT-OA, we can fix checksum without recalculation.
  35057. + */
  35058. + __u32 natt_oa = ipsp->ips_natt_oa ?
  35059. + ((struct sockaddr_in*)(ipsp->ips_natt_oa))->sin_addr.s_addr : 0;
  35060. + __u16 pkt_len = skb->tail - (unsigned char *)ipp;
  35061. + __u16 data_len = pkt_len - (ipp->ihl << 2);
  35062. +
  35063. + switch (ipp->protocol) {
  35064. + case IPPROTO_TCP:
  35065. + if (data_len >= sizeof(struct tcphdr)) {
  35066. + struct tcphdr *tcp = skb->h.th;
  35067. + if (natt_oa) {
  35068. + __u32 buff[2] = { ~natt_oa, ipp->saddr };
  35069. + KLIPS_PRINT(debug_rcv,
  35070. + "klips_debug:ipsec_rcv: "
  35071. + "NAT-T & TRANSPORT: "
  35072. + "fix TCP checksum using NAT-OA\n");
  35073. + tcp->check = csum_fold(
  35074. + csum_partial((unsigned char *)buff, sizeof(buff),
  35075. + tcp->check^0xffff));
  35076. + }
  35077. + else {
  35078. + KLIPS_PRINT(debug_rcv,
  35079. + "klips_debug:ipsec_rcv: "
  35080. + "NAT-T & TRANSPORT: recalc TCP checksum\n");
  35081. + if (pkt_len > (ntohs(ipp->tot_len)))
  35082. + data_len -= (pkt_len - ntohs(ipp->tot_len));
  35083. + tcp->check = 0;
  35084. + tcp->check = csum_tcpudp_magic(ipp->saddr, ipp->daddr,
  35085. + data_len, IPPROTO_TCP,
  35086. + csum_partial((unsigned char *)tcp, data_len, 0));
  35087. + }
  35088. + }
  35089. + else {
  35090. + KLIPS_PRINT(debug_rcv,
  35091. + "klips_debug:ipsec_rcv: "
  35092. + "NAT-T & TRANSPORT: can't fix TCP checksum\n");
  35093. + }
  35094. + break;
  35095. + case IPPROTO_UDP:
  35096. + if (data_len >= sizeof(struct udphdr)) {
  35097. + struct udphdr *udp = skb->h.uh;
  35098. + if (udp->check == 0) {
  35099. + KLIPS_PRINT(debug_rcv,
  35100. + "klips_debug:ipsec_rcv: "
  35101. + "NAT-T & TRANSPORT: UDP checksum already 0\n");
  35102. + }
  35103. + else if (natt_oa) {
  35104. + __u32 buff[2] = { ~natt_oa, ipp->saddr };
  35105. + KLIPS_PRINT(debug_rcv,
  35106. + "klips_debug:ipsec_rcv: "
  35107. + "NAT-T & TRANSPORT: "
  35108. + "fix UDP checksum using NAT-OA\n");
  35109. +#ifdef DISABLE_UDP_CHECKSUM
  35110. + udp->check=0
  35111. + KLIPS_PRINT(debug_rcv,
  35112. + "klips_debug:ipsec_rcv: "
  35113. + "NAT-T & TRANSPORT: "
  35114. + "UDP checksum using NAT-OA disabled at compile time\n");
  35115. +#else
  35116. + udp->check = csum_fold(
  35117. + csum_partial((unsigned char *)buff, sizeof(buff),
  35118. + udp->check^0xffff));
  35119. +#endif
  35120. + }
  35121. + else {
  35122. + KLIPS_PRINT(debug_rcv,
  35123. + "klips_debug:ipsec_rcv: "
  35124. + "NAT-T & TRANSPORT: zero UDP checksum\n");
  35125. + udp->check = 0;
  35126. + }
  35127. + }
  35128. + else {
  35129. + KLIPS_PRINT(debug_rcv,
  35130. + "klips_debug:ipsec_rcv: "
  35131. + "NAT-T & TRANSPORT: can't fix UDP checksum\n");
  35132. + }
  35133. + break;
  35134. + default:
  35135. + KLIPS_PRINT(debug_rcv,
  35136. + "klips_debug:ipsec_rcv: "
  35137. + "NAT-T & TRANSPORT: non TCP/UDP packet -- do nothing\n");
  35138. + break;
  35139. + }
  35140. + }
  35141. +#endif
  35142. +
  35143. + /*
  35144. + * XXX this needs to be locked from when it was first looked
  35145. + * up in the decapsulation loop. Perhaps it is better to put
  35146. + * the IPIP decap inside the loop.
  35147. + */
  35148. + if(ipsnext) {
  35149. + ipsp = ipsnext;
  35150. + irs->sa_len = satot(&irs->said, 0, irs->sa, sizeof(irs->sa));
  35151. + if((ipp->protocol != IPPROTO_IPIP) &&
  35152. + (ipp->protocol != IPPROTO_ATT_HEARTBEAT)) { /* AT&T heartbeats to SIG/GIG */
  35153. + spin_unlock(&tdb_lock);
  35154. + KLIPS_PRINT(debug_rcv,
  35155. + "klips_debug:ipsec_rcv: "
  35156. + "SA:%s, Hey! How did this get through? Dropped.\n",
  35157. + irs->sa_len ? irs->sa : " (error)");
  35158. + if(irs->stats) {
  35159. + irs->stats->rx_dropped++;
  35160. + }
  35161. + goto rcvleave;
  35162. + }
  35163. + if(sysctl_ipsec_inbound_policy_check) {
  35164. + struct sockaddr_in *psin = (struct sockaddr_in*)(ipsp->ips_addr_s);
  35165. + if((ipsnext = ipsp->ips_inext)) {
  35166. + char sa2[SATOT_BUF];
  35167. + size_t sa_len2;
  35168. + sa_len2 = satot(&ipsnext->ips_said, 0, sa2, sizeof(sa2));
  35169. + spin_unlock(&tdb_lock);
  35170. + KLIPS_PRINT(debug_rcv,
  35171. + "klips_debug:ipsec_rcv: "
  35172. + "unexpected SA:%s after IPIP SA:%s\n",
  35173. + sa_len2 ? sa2 : " (error)",
  35174. + irs->sa_len ? irs->sa : " (error)");
  35175. + if(irs->stats) {
  35176. + irs->stats->rx_dropped++;
  35177. + }
  35178. + goto rcvleave;
  35179. + }
  35180. + if(ipp->saddr != psin->sin_addr.s_addr) {
  35181. + spin_unlock(&tdb_lock);
  35182. + KLIPS_PRINT(debug_rcv,
  35183. + "klips_debug:ipsec_rcv: "
  35184. + "SA:%s, src=%s(%08x) does match expected 0x%08x.\n",
  35185. + irs->sa_len ? irs->sa : " (error)",
  35186. + irs->ipsaddr_txt,
  35187. + ipp->saddr, psin->sin_addr.s_addr);
  35188. + if(irs->stats) {
  35189. + irs->stats->rx_dropped++;
  35190. + }
  35191. + goto rcvleave;
  35192. + }
  35193. + }
  35194. +
  35195. + if(ipp->protocol == IPPROTO_IPIP) /* added to support AT&T heartbeats to SIG/GIG */
  35196. + {
  35197. + /*
  35198. + * XXX this needs to be locked from when it was first looked
  35199. + * up in the decapsulation loop. Perhaps it is better to put
  35200. + * the IPIP decap inside the loop.
  35201. + */
  35202. + ipsp->ips_life.ipl_bytes.ipl_count += skb->len;
  35203. + ipsp->ips_life.ipl_bytes.ipl_last = skb->len;
  35204. +
  35205. + if(!ipsp->ips_life.ipl_usetime.ipl_count) {
  35206. + ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  35207. + }
  35208. + ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  35209. + ipsp->ips_life.ipl_packets.ipl_count += 1;
  35210. +
  35211. + if(skb->len < irs->iphlen) {
  35212. + spin_unlock(&tdb_lock);
  35213. + printk(KERN_WARNING "klips_debug:ipsec_rcv: "
  35214. + "tried to skb_pull iphlen=%d, %d available. This should never happen, please report.\n",
  35215. + irs->iphlen,
  35216. + (int)(skb->len));
  35217. +
  35218. + goto rcvleave;
  35219. + }
  35220. +
  35221. + /*
  35222. + * we need to pull up by size of IP header,
  35223. + * options, but also by any UDP/ESP encap there might
  35224. + * have been, and this deals with all cases.
  35225. + */
  35226. + skb_pull(skb, (skb->h.raw - skb->nh.raw));
  35227. +
  35228. + /* new L3 header is where L4 payload was */
  35229. + skb->nh.raw = skb->h.raw;
  35230. +
  35231. + /* now setup new L4 payload location */
  35232. + ipp = (struct iphdr *)skb->nh.raw;
  35233. + skb->h.raw = skb->nh.raw + (ipp->ihl << 2);
  35234. +
  35235. +
  35236. + /* remove any saved options that we might have,
  35237. + * since we have a new IP header.
  35238. + */
  35239. + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  35240. +
  35241. +#if 0
  35242. + KLIPS_PRINT(debug_rcv, "csum: %d\n", ip_fast_csum((u8 *)ipp, ipp->ihl));
  35243. +#endif
  35244. +
  35245. + /* re-do any strings for debugging */
  35246. + ipsaddr.s_addr = ipp->saddr;
  35247. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  35248. + ipdaddr.s_addr = ipp->daddr;
  35249. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  35250. +
  35251. + skb->protocol = htons(ETH_P_IP);
  35252. + skb->ip_summed = 0;
  35253. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35254. + "klips_debug:ipsec_rcv: "
  35255. + "IPIP tunnel stripped.\n");
  35256. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, ipp);
  35257. + }
  35258. +
  35259. + if(sysctl_ipsec_inbound_policy_check
  35260. + /*
  35261. + Note: "xor" (^) logically replaces "not equal"
  35262. + (!=) and "bitwise or" (|) logically replaces
  35263. + "boolean or" (||). This is done to speed up
  35264. + execution by doing only bitwise operations and
  35265. + no branch operations
  35266. + */
  35267. + && (((ipp->saddr & ipsp->ips_mask_s.u.v4.sin_addr.s_addr)
  35268. + ^ ipsp->ips_flow_s.u.v4.sin_addr.s_addr)
  35269. + | ((ipp->daddr & ipsp->ips_mask_d.u.v4.sin_addr.s_addr)
  35270. + ^ ipsp->ips_flow_d.u.v4.sin_addr.s_addr)) )
  35271. + {
  35272. + char sflow_txt[SUBNETTOA_BUF], dflow_txt[SUBNETTOA_BUF];
  35273. +
  35274. + subnettoa(ipsp->ips_flow_s.u.v4.sin_addr,
  35275. + ipsp->ips_mask_s.u.v4.sin_addr,
  35276. + 0, sflow_txt, sizeof(sflow_txt));
  35277. + subnettoa(ipsp->ips_flow_d.u.v4.sin_addr,
  35278. + ipsp->ips_mask_d.u.v4.sin_addr,
  35279. + 0, dflow_txt, sizeof(dflow_txt));
  35280. + spin_unlock(&tdb_lock);
  35281. + KLIPS_PRINT(debug_rcv,
  35282. + "klips_debug:ipsec_rcv: "
  35283. + "SA:%s, inner tunnel policy [%s -> %s] does not agree with pkt contents [%s -> %s].\n",
  35284. + irs->sa_len ? irs->sa : " (error)",
  35285. + sflow_txt,
  35286. + dflow_txt,
  35287. + irs->ipsaddr_txt,
  35288. + irs->ipdaddr_txt);
  35289. + if(irs->stats) {
  35290. + irs->stats->rx_dropped++;
  35291. + }
  35292. + goto rcvleave;
  35293. + }
  35294. +#ifdef CONFIG_NETFILTER
  35295. + skb->nfmark = (skb->nfmark & (~(IPsecSAref2NFmark(IPSEC_SA_REF_TABLE_MASK))))
  35296. + | IPsecSAref2NFmark(IPsecSA2SAref(ipsp));
  35297. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35298. + "klips_debug:ipsec_rcv: "
  35299. + "IPIP SA sets skb->nfmark=0x%x.\n",
  35300. + (unsigned)skb->nfmark);
  35301. +#endif /* CONFIG_NETFILTER */
  35302. + }
  35303. +
  35304. + spin_unlock(&tdb_lock);
  35305. +
  35306. + if(irs->stats) {
  35307. + irs->stats->rx_bytes += skb->len;
  35308. + }
  35309. + if(skb->dst) {
  35310. + dst_release(skb->dst);
  35311. + skb->dst = NULL;
  35312. + }
  35313. + skb->pkt_type = PACKET_HOST;
  35314. + if(irs->hard_header_len &&
  35315. + (skb->mac.raw != (skb->nh.raw - irs->hard_header_len)) &&
  35316. + (irs->hard_header_len <= skb_headroom(skb))) {
  35317. + /* copy back original MAC header */
  35318. + memmove(skb->nh.raw - irs->hard_header_len,
  35319. + skb->mac.raw, irs->hard_header_len);
  35320. + skb->mac.raw = skb->nh.raw - irs->hard_header_len;
  35321. + }
  35322. +
  35323. +#ifdef CONFIG_KLIPS_IPCOMP
  35324. + if(ipp->protocol == IPPROTO_COMP) {
  35325. + unsigned int flags = 0;
  35326. +
  35327. + if(sysctl_ipsec_inbound_policy_check) {
  35328. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35329. + "klips_debug:ipsec_rcv: "
  35330. + "inbound policy checking enabled, IPCOMP follows IPIP, dropped.\n");
  35331. + if (irs->stats) {
  35332. + irs->stats->rx_errors++;
  35333. + }
  35334. + goto rcvleave;
  35335. + }
  35336. + /*
  35337. + XXX need a ipsec_sa for updating ratio counters but it is not
  35338. + following policy anyways so it is not a priority
  35339. + */
  35340. + skb = skb_decompress(skb, NULL, &flags);
  35341. + if (!skb || flags) {
  35342. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35343. + "klips_debug:ipsec_rcv: "
  35344. + "skb_decompress() returned error flags: %d, dropped.\n",
  35345. + flags);
  35346. + if (irs->stats) {
  35347. + irs->stats->rx_errors++;
  35348. + }
  35349. + goto rcvleave;
  35350. + }
  35351. + }
  35352. +#endif /* CONFIG_KLIPS_IPCOMP */
  35353. +
  35354. + /*
  35355. + * make sure that data now starts at IP header, since we are going
  35356. + * to pass this back to ip_input (aka netif_rx). Rules for what the
  35357. + * pointers wind up a different for 2.6 vs 2.4, so we just fudge it here.
  35358. + */
  35359. +#ifdef NET_26
  35360. + skb->data = skb_push(skb, skb->h.raw - skb->nh.raw);
  35361. +#else
  35362. + skb->data = skb->nh.raw;
  35363. + {
  35364. + struct iphdr *iph = skb->nh.iph;
  35365. + int len = ntohs(iph->tot_len);
  35366. + skb->len = len;
  35367. + }
  35368. +#endif
  35369. +
  35370. +#ifdef SKB_RESET_NFCT
  35371. + nf_conntrack_put(skb->nfct);
  35372. + skb->nfct = NULL;
  35373. +#if defined(CONFIG_NETFILTER_DEBUG) && defined(HAVE_SKB_NF_DEBUG)
  35374. + skb->nf_debug = 0;
  35375. +#endif /* CONFIG_NETFILTER_DEBUG */
  35376. +#endif /* SKB_RESET_NFCT */
  35377. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35378. + "klips_debug:ipsec_rcv: "
  35379. + "netif_rx() called.\n");
  35380. + netif_rx(skb);
  35381. + skb=NULL;
  35382. +
  35383. + rcvleave:
  35384. + if(skb) {
  35385. + ipsec_kfree_skb(skb);
  35386. + }
  35387. +
  35388. + /* KLIPS_DEC_USE; Artifact from refactor? bug # 454 */
  35389. + return(0);
  35390. +}
  35391. +
  35392. +struct sk_buff *ipsec_rcv_unclone(struct sk_buff *skb,
  35393. + struct ipsec_rcv_state *irs)
  35394. +{
  35395. + /* if skb was cloned (most likely due to a packet sniffer such as
  35396. + tcpdump being momentarily attached to the interface), make
  35397. + a copy of our own to modify */
  35398. + if(skb_cloned(skb)) {
  35399. + /* include any mac header while copying.. */
  35400. + if(skb_headroom(skb) < irs->hard_header_len) {
  35401. + printk(KERN_WARNING "klips_error:ipsec_rcv: "
  35402. + "tried to skb_push hhlen=%d, %d available. This should never happen, please report.\n",
  35403. + irs->hard_header_len,
  35404. + skb_headroom(skb));
  35405. + goto rcvleave;
  35406. + }
  35407. + skb_push(skb, irs->hard_header_len);
  35408. + if
  35409. +#ifdef SKB_COW_NEW
  35410. + (skb_cow(skb, skb_headroom(skb)) != 0)
  35411. +#else /* SKB_COW_NEW */
  35412. + ((skb = skb_cow(skb, skb_headroom(skb))) == NULL)
  35413. +#endif /* SKB_COW_NEW */
  35414. + {
  35415. + goto rcvleave;
  35416. + }
  35417. + if(skb->len < irs->hard_header_len) {
  35418. + printk(KERN_WARNING "klips_error:ipsec_rcv: "
  35419. + "tried to skb_pull hhlen=%d, %d available. This should never happen, please report.\n",
  35420. + irs->hard_header_len,
  35421. + skb->len);
  35422. + goto rcvleave;
  35423. + }
  35424. + skb_pull(skb, irs->hard_header_len);
  35425. + }
  35426. + return skb;
  35427. +
  35428. +rcvleave:
  35429. + ipsec_kfree_skb(skb);
  35430. + return NULL;
  35431. +}
  35432. +
  35433. +
  35434. +#if !defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  35435. +/*
  35436. + * decapsulate a UDP encapsulated ESP packet
  35437. + */
  35438. +struct sk_buff *ipsec_rcv_natt_decap(struct sk_buff *skb
  35439. + , struct ipsec_rcv_state *irs
  35440. + , int *udp_decap_ret_p)
  35441. +{
  35442. + *udp_decap_ret_p = 0;
  35443. + if (skb->sk && skb->nh.iph && skb->nh.iph->protocol==IPPROTO_UDP) {
  35444. + /**
  35445. + * Packet comes from udp_queue_rcv_skb so it is already defrag,
  35446. + * checksum verified, ... (ie safe to use)
  35447. + *
  35448. + * If the packet is not for us, return -1 and udp_queue_rcv_skb
  35449. + * will continue to handle it (do not kfree skb !!).
  35450. + */
  35451. +
  35452. +#ifndef UDP_OPT_IN_SOCK
  35453. + struct udp_opt {
  35454. + __u32 esp_in_udp;
  35455. + };
  35456. + struct udp_opt *tp = (struct udp_opt *)&(skb->sk->tp_pinfo.af_tcp);
  35457. +#else
  35458. + struct udp_opt *tp = &(skb->sk->tp_pinfo.af_udp);
  35459. +#endif
  35460. +
  35461. + struct iphdr *ip = (struct iphdr *)skb->nh.iph;
  35462. + struct udphdr *udp = (struct udphdr *)((__u32 *)ip+ip->ihl);
  35463. + __u8 *udpdata = (__u8 *)udp + sizeof(struct udphdr);
  35464. + __u32 *udpdata32 = (__u32 *)udpdata;
  35465. +
  35466. + irs->natt_sport = ntohs(udp->source);
  35467. + irs->natt_dport = ntohs(udp->dest);
  35468. +
  35469. + KLIPS_PRINT(debug_rcv,
  35470. + "klips_debug:ipsec_rcv: "
  35471. + "suspected ESPinUDP packet (NAT-Traversal) [%d].\n",
  35472. + tp->esp_in_udp);
  35473. + KLIPS_IP_PRINT(debug_rcv, ip);
  35474. +
  35475. + if (udpdata < skb->tail) {
  35476. + unsigned int len = skb->tail - udpdata;
  35477. + if ((len==1) && (udpdata[0]==0xff)) {
  35478. + KLIPS_PRINT(debug_rcv,
  35479. + "klips_debug:ipsec_rcv: "
  35480. + /* not IPv6 compliant message */
  35481. + "NAT-keepalive from %d.%d.%d.%d.\n", NIPQUAD(ip->saddr));
  35482. + *udp_decap_ret_p = 0;
  35483. + return NULL;
  35484. + }
  35485. + else if ( (tp->esp_in_udp == ESPINUDP_WITH_NON_IKE) &&
  35486. + (len > (2*sizeof(__u32) + sizeof(struct esphdr))) &&
  35487. + (udpdata32[0]==0) && (udpdata32[1]==0) ) {
  35488. + /* ESP Packet with Non-IKE header */
  35489. + KLIPS_PRINT(debug_rcv,
  35490. + "klips_debug:ipsec_rcv: "
  35491. + "ESPinUDP pkt with Non-IKE - spi=0x%x\n",
  35492. + ntohl(udpdata32[2]));
  35493. + irs->natt_type = ESPINUDP_WITH_NON_IKE;
  35494. + irs->natt_len = sizeof(struct udphdr)+(2*sizeof(__u32));
  35495. + }
  35496. + else if ( (tp->esp_in_udp == ESPINUDP_WITH_NON_ESP) &&
  35497. + (len > sizeof(struct esphdr)) &&
  35498. + (udpdata32[0]!=0) ) {
  35499. + /* ESP Packet without Non-ESP header */
  35500. + irs->natt_type = ESPINUDP_WITH_NON_ESP;
  35501. + irs->natt_len = sizeof(struct udphdr);
  35502. + KLIPS_PRINT(debug_rcv,
  35503. + "klips_debug:ipsec_rcv: "
  35504. + "ESPinUDP pkt without Non-ESP - spi=0x%x\n",
  35505. + ntohl(udpdata32[0]));
  35506. + }
  35507. + else {
  35508. + KLIPS_PRINT(debug_rcv,
  35509. + "klips_debug:ipsec_rcv: "
  35510. + "IKE packet - not handled here\n");
  35511. + *udp_decap_ret_p = -1;
  35512. + return NULL;
  35513. + }
  35514. + }
  35515. + else {
  35516. + return NULL;
  35517. + }
  35518. + }
  35519. + return skb;
  35520. +}
  35521. +#endif
  35522. +
  35523. +
  35524. +int
  35525. +ipsec_rcv(struct sk_buff *skb
  35526. +#ifndef PROTO_HANDLER_SINGLE_PARM
  35527. + unsigned short xlen
  35528. +#endif /* PROTO_HANDLER_SINGLE_PARM */
  35529. + )
  35530. +{
  35531. +#ifdef CONFIG_KLIPS_DEBUG
  35532. + struct net_device *dev = skb->dev;
  35533. +#endif /* CONFIG_KLIPS_DEBUG */
  35534. + unsigned char protoc;
  35535. + struct net_device_stats *stats = NULL; /* This device's statistics */
  35536. + struct net_device *ipsecdev = NULL, *prvdev;
  35537. + struct ipsecpriv *prv;
  35538. + struct ipsec_rcv_state nirs, *irs = &nirs;
  35539. + struct iphdr *ipp;
  35540. + char name[9];
  35541. + int i;
  35542. +
  35543. + /* Don't unlink in the middle of a turnaround */
  35544. + KLIPS_INC_USE;
  35545. +
  35546. + memset(&nirs, 0, sizeof(struct ipsec_rcv_state));
  35547. +
  35548. + if (skb == NULL) {
  35549. + KLIPS_PRINT(debug_rcv,
  35550. + "klips_debug:ipsec_rcv: "
  35551. + "NULL skb passed in.\n");
  35552. + goto rcvleave;
  35553. + }
  35554. +
  35555. + if (skb->data == NULL) {
  35556. + KLIPS_PRINT(debug_rcv,
  35557. + "klips_debug:ipsec_rcv: "
  35558. + "NULL skb->data passed in, packet is bogus, dropping.\n");
  35559. + goto rcvleave;
  35560. + }
  35561. +
  35562. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(NET_26)
  35563. + {
  35564. + /* NET_26 NAT-T is handled by seperate function */
  35565. + struct sk_buff *nskb;
  35566. + int udp_decap_ret = 0;
  35567. +
  35568. + nskb = ipsec_rcv_natt_decap(skb, irs, &udp_decap_ret);
  35569. + if(nskb == NULL) {
  35570. + /* return with non-zero, because UDP.c code
  35571. + * need to send it upstream.
  35572. + */
  35573. + if(skb && udp_decap_ret == 0) {
  35574. + ipsec_kfree_skb(skb);
  35575. + }
  35576. + KLIPS_DEC_USE;
  35577. + return(udp_decap_ret);
  35578. + }
  35579. + skb = nskb;
  35580. + }
  35581. +#endif /* NAT_T */
  35582. +
  35583. + /* dev->hard_header_len is unreliable and should not be used */
  35584. + irs->hard_header_len = skb->mac.raw ? (skb->nh.raw - skb->mac.raw) : 0;
  35585. + if((irs->hard_header_len < 0) || (irs->hard_header_len > skb_headroom(skb)))
  35586. + irs->hard_header_len = 0;
  35587. +
  35588. + skb = ipsec_rcv_unclone(skb, irs);
  35589. + if(skb == NULL) {
  35590. + goto rcvleave;
  35591. + }
  35592. +
  35593. +#if IP_FRAGMENT_LINEARIZE
  35594. + /* In Linux 2.4.4, we may have to reassemble fragments. They are
  35595. + not assembled automatically to save TCP from having to copy
  35596. + twice.
  35597. + */
  35598. + if (skb_is_nonlinear(skb)) {
  35599. +#ifdef HAVE_NEW_SKB_LINEARIZE
  35600. + if (skb_linearize_cow(skb) != 0)
  35601. +#else
  35602. + if (skb_linearize(skb, GFP_ATOMIC) != 0)
  35603. +#endif
  35604. + {
  35605. + goto rcvleave;
  35606. + }
  35607. + }
  35608. +#endif /* IP_FRAGMENT_LINEARIZE */
  35609. +
  35610. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(NET_26)
  35611. + if (irs->natt_len) {
  35612. + /**
  35613. + * Now, we are sure packet is ESPinUDP, and we have a private
  35614. + * copy that has been linearized, remove natt_len bytes
  35615. + * from packet and modify protocol to ESP.
  35616. + */
  35617. + if (((unsigned char *)skb->data > (unsigned char *)skb->nh.iph)
  35618. + && ((unsigned char *)skb->nh.iph > (unsigned char *)skb->head))
  35619. + {
  35620. + unsigned int _len = (unsigned char *)skb->data -
  35621. + (unsigned char *)skb->nh.iph;
  35622. + KLIPS_PRINT(debug_rcv,
  35623. + "klips_debug:ipsec_rcv: adjusting skb: skb_push(%u)\n",
  35624. + _len);
  35625. + skb_push(skb, _len);
  35626. + }
  35627. + KLIPS_PRINT(debug_rcv,
  35628. + "klips_debug:ipsec_rcv: "
  35629. + "removing %d bytes from ESPinUDP packet\n", irs->natt_len);
  35630. + ipp = skb->nh.iph;
  35631. + irs->iphlen = ipp->ihl << 2;
  35632. + ipp->tot_len = htons(ntohs(ipp->tot_len) - irs->natt_len);
  35633. + if (skb->len < irs->iphlen + irs->natt_len) {
  35634. + printk(KERN_WARNING
  35635. + "klips_error:ipsec_rcv: "
  35636. + "ESPinUDP packet is too small (%d < %d+%d). "
  35637. + "This should never happen, please report.\n",
  35638. + (int)(skb->len), irs->iphlen, irs->natt_len);
  35639. + goto rcvleave;
  35640. + }
  35641. +
  35642. + /* advance payload pointer to point past the UDP header */
  35643. + skb->h.raw = skb->h.raw + irs->natt_len;
  35644. +
  35645. + /* modify protocol */
  35646. + ipp->protocol = IPPROTO_ESP;
  35647. +
  35648. + skb->sk = NULL;
  35649. +
  35650. + KLIPS_IP_PRINT(debug_rcv, skb->nh.iph);
  35651. + }
  35652. +#endif
  35653. +
  35654. + ipp = skb->nh.iph;
  35655. +
  35656. + {
  35657. + struct in_addr ipsaddr;
  35658. + struct in_addr ipdaddr;
  35659. +
  35660. + ipsaddr.s_addr = ipp->saddr;
  35661. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt
  35662. + , sizeof(irs->ipsaddr_txt));
  35663. + ipdaddr.s_addr = ipp->daddr;
  35664. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt
  35665. + , sizeof(irs->ipdaddr_txt));
  35666. + }
  35667. +
  35668. + irs->iphlen = ipp->ihl << 2;
  35669. +
  35670. + KLIPS_PRINT(debug_rcv,
  35671. + "klips_debug:ipsec_rcv: "
  35672. + "<<< Info -- ");
  35673. + KLIPS_PRINTMORE(debug_rcv && skb->dev, "skb->dev=%s ",
  35674. + skb->dev->name ? skb->dev->name : "NULL");
  35675. + KLIPS_PRINTMORE(debug_rcv && dev, "dev=%s ",
  35676. + dev->name ? dev->name : "NULL");
  35677. + KLIPS_PRINTMORE(debug_rcv, "\n");
  35678. +
  35679. + KLIPS_PRINT(debug_rcv && !(skb->dev && dev && (skb->dev == dev)),
  35680. + "klips_debug:ipsec_rcv: "
  35681. + "Informational -- **if this happens, find out why** skb->dev:%s is not equal to dev:%s\n",
  35682. + skb->dev ? (skb->dev->name ? skb->dev->name : "NULL") : "NULL",
  35683. + dev ? (dev->name ? dev->name : "NULL") : "NULL");
  35684. +
  35685. + protoc = ipp->protocol;
  35686. +#ifndef NET_21
  35687. + if((!protocol) || (protocol->protocol != protoc)) {
  35688. + KLIPS_PRINT(debug_rcv & DB_RX_IPSA,
  35689. + "klips_debug:ipsec_rcv: "
  35690. + "protocol arg is NULL or unequal to the packet contents, this is odd, using value in packet.\n");
  35691. + }
  35692. +#endif /* !NET_21 */
  35693. +
  35694. + if( (protoc != IPPROTO_AH) &&
  35695. +#ifdef CONFIG_KLIPS_IPCOMP_disabled_until_we_register_IPCOMP_HANDLER
  35696. + (protoc != IPPROTO_COMP) &&
  35697. +#endif /* CONFIG_KLIPS_IPCOMP */
  35698. + (protoc != IPPROTO_ESP) ) {
  35699. + KLIPS_PRINT(debug_rcv & DB_RX_IPSA,
  35700. + "klips_debug:ipsec_rcv: Why the hell is someone "
  35701. + "passing me a non-ipsec protocol = %d packet? -- dropped.\n",
  35702. + protoc);
  35703. + goto rcvleave;
  35704. + }
  35705. +
  35706. + if(skb->dev) {
  35707. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  35708. + sprintf(name, IPSEC_DEV_FORMAT, i);
  35709. + if(!strcmp(name, skb->dev->name)) {
  35710. + prv = (struct ipsecpriv *)(skb->dev->priv);
  35711. + if(prv) {
  35712. + stats = (struct net_device_stats *) &(prv->mystats);
  35713. + }
  35714. + ipsecdev = skb->dev;
  35715. + KLIPS_PRINT(debug_rcv,
  35716. + "klips_debug:ipsec_rcv: "
  35717. + "Info -- pkt already proc'ed a group of ipsec headers, processing next group of ipsec headers.\n");
  35718. + break;
  35719. + }
  35720. + if((ipsecdev = __ipsec_dev_get(name)) == NULL) {
  35721. + KLIPS_PRINT(debug_rcv,
  35722. + "klips_error:ipsec_rcv: "
  35723. + "device %s does not exist\n",
  35724. + name);
  35725. + }
  35726. + prv = ipsecdev ? (struct ipsecpriv *)(ipsecdev->priv) : NULL;
  35727. + prvdev = prv ? (struct net_device *)(prv->dev) : NULL;
  35728. +
  35729. +#if 0
  35730. + KLIPS_PRINT(debug_rcv && prvdev,
  35731. + "klips_debug:ipsec_rcv: "
  35732. + "physical device for device %s is %s\n",
  35733. + name,
  35734. + prvdev->name);
  35735. +#endif
  35736. + if(prvdev && skb->dev &&
  35737. + !strcmp(prvdev->name, skb->dev->name)) {
  35738. + stats = prv ? ((struct net_device_stats *) &(prv->mystats)) : NULL;
  35739. + skb->dev = ipsecdev;
  35740. + KLIPS_PRINT(debug_rcv && prvdev,
  35741. + "klips_debug:ipsec_rcv: "
  35742. + "assigning packet ownership to virtual device %s from physical device %s.\n",
  35743. + name, prvdev->name);
  35744. + if(stats) {
  35745. + stats->rx_packets++;
  35746. + }
  35747. + break;
  35748. + }
  35749. + }
  35750. + } else {
  35751. + KLIPS_PRINT(debug_rcv,
  35752. + "klips_debug:ipsec_rcv: "
  35753. + "device supplied with skb is NULL\n");
  35754. + }
  35755. +
  35756. + if(stats == NULL) {
  35757. + KLIPS_PRINT((debug_rcv),
  35758. + "klips_error:ipsec_rcv: "
  35759. + "packet received from physical I/F (%s) not connected to ipsec I/F. Cannot record stats. May not have SA for decoding. Is IPSEC traffic expected on this I/F? Check routing.\n",
  35760. + skb->dev ? (skb->dev->name ? skb->dev->name : "NULL") : "NULL");
  35761. + }
  35762. +
  35763. + KLIPS_IP_PRINT(debug_rcv, ipp);
  35764. +
  35765. + /* set up for decap loop */
  35766. + irs->stats= stats;
  35767. + irs->ipp = ipp;
  35768. + irs->ipsp = NULL;
  35769. + irs->ilen = 0;
  35770. + irs->authlen=0;
  35771. + irs->authfuncs=NULL;
  35772. + irs->skb = skb;
  35773. +
  35774. + ipsec_rcv_decap(irs);
  35775. + KLIPS_DEC_USE;
  35776. + return(0);
  35777. +
  35778. + rcvleave:
  35779. + if(skb) {
  35780. + ipsec_kfree_skb(skb);
  35781. + }
  35782. + KLIPS_DEC_USE;
  35783. + return(0);
  35784. +
  35785. +}
  35786. +
  35787. +#ifdef NET_26
  35788. +/*
  35789. + * this entry point is not a protocol entry point, so the entry
  35790. + * is a bit different.
  35791. + *
  35792. + * skb->iph->tot_len has been byte-swapped, and reduced by the size of
  35793. + * the IP header (and options).
  35794. + *
  35795. + * skb->h.raw has been pulled up the ESP header.
  35796. + *
  35797. + * skb->iph->protocol = 50 IPPROTO_ESP;
  35798. + *
  35799. + */
  35800. +int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type)
  35801. +{
  35802. + struct ipsec_rcv_state nirs, *irs = &nirs;
  35803. + struct iphdr *ipp;
  35804. +
  35805. + /* Don't unlink in the middle of a turnaround */
  35806. + KLIPS_INC_USE;
  35807. +
  35808. + memset(irs, 0, sizeof(*irs));
  35809. +
  35810. + /* XXX fudge it so that all nat-t stuff comes from ipsec0 */
  35811. + /* eventually, the SA itself will determine which device
  35812. + * it comes from
  35813. + */
  35814. + {
  35815. + skb->dev = ipsec_get_device(0);
  35816. + }
  35817. +
  35818. + /* set up for decap loop */
  35819. + irs->hard_header_len = skb->dev->hard_header_len;
  35820. +
  35821. + skb = ipsec_rcv_unclone(skb, irs);
  35822. +
  35823. +#if IP_FRAGMENT_LINEARIZE
  35824. + /* In Linux 2.4.4, we may have to reassemble fragments. They are
  35825. + not assembled automatically to save TCP from having to copy
  35826. + twice.
  35827. + */
  35828. + if (skb_is_nonlinear(skb)) {
  35829. +#ifdef HAVE_NEW_SKB_LINEARIZE
  35830. + if (skb_linearize_cow(skb) != 0)
  35831. +#else
  35832. + if (skb_linearize(skb, GFP_ATOMIC) != 0)
  35833. +#endif
  35834. + {
  35835. + goto rcvleave;
  35836. + }
  35837. + }
  35838. +#endif /* IP_FRAGMENT_LINEARIZE */
  35839. +
  35840. + ipp = skb->nh.iph;
  35841. +
  35842. + {
  35843. + struct in_addr ipsaddr;
  35844. + struct in_addr ipdaddr;
  35845. +
  35846. + ipsaddr.s_addr = ipp->saddr;
  35847. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt
  35848. + , sizeof(irs->ipsaddr_txt));
  35849. + ipdaddr.s_addr = ipp->daddr;
  35850. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt
  35851. + , sizeof(irs->ipdaddr_txt));
  35852. + }
  35853. +
  35854. + irs->iphlen = ipp->ihl << 2;
  35855. +
  35856. + KLIPS_IP_PRINT(debug_rcv, ipp);
  35857. +
  35858. + irs->stats= NULL;
  35859. + irs->ipp = ipp;
  35860. + irs->ipsp = NULL;
  35861. + irs->ilen = 0;
  35862. + irs->authlen=0;
  35863. + irs->authfuncs=NULL;
  35864. + irs->skb = skb;
  35865. +
  35866. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  35867. + switch(encap_type) {
  35868. + case UDP_ENCAP_ESPINUDP:
  35869. + irs->natt_type = ESPINUDP_WITH_NON_ESP;
  35870. + break;
  35871. +
  35872. + case UDP_ENCAP_ESPINUDP_NON_IKE:
  35873. + irs->natt_type = ESPINUDP_WITH_NON_IKE;
  35874. + break;
  35875. +
  35876. + default:
  35877. + if(printk_ratelimit()) {
  35878. + printk(KERN_INFO "KLIPS received unknown UDP-ESP encap type %u\n",
  35879. + encap_type);
  35880. + }
  35881. + return -1;
  35882. + }
  35883. +
  35884. +#endif
  35885. + ipsec_rcv_decap(irs);
  35886. + KLIPS_DEC_USE;
  35887. + return 0;
  35888. +
  35889. +rcvleave:
  35890. + if(skb) {
  35891. + ipsec_kfree_skb(skb);
  35892. + }
  35893. + KLIPS_DEC_USE;
  35894. + return 0;
  35895. +}
  35896. +#endif
  35897. +
  35898. +
  35899. +/*
  35900. + * $Log: ipsec_rcv.c,v $
  35901. + * Revision 1.171.2.11 2007/04/28 20:46:40 paul
  35902. + * Added compile time switch for -DDISABLE_UDP_CHECKSUM that seems to be
  35903. + * breaking IPsec+NAT+Transport mode with NAT-OA. Enabled this per default
  35904. + * via Makefile.inc's USERCOMPILE flags.
  35905. + *
  35906. + * Revision 1.171.2.10 2006/10/06 21:39:26 paul
  35907. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  35908. + * set. This is defined through autoconf.h which is included through the
  35909. + * linux kernel build macros.
  35910. + *
  35911. + * Revision 1.171.2.9 2006/07/30 02:09:33 paul
  35912. + * Author: Bart Trojanowski <bart@xelerance.com>
  35913. + * This fixes a NATT+ESP bug in rcv path.
  35914. + *
  35915. + * We only want to test NATT policy on the ESP packet. Doing so on the
  35916. + * bundled SA breaks because the next layer does not know anything about
  35917. + * NATT.
  35918. + *
  35919. + * Fix just puts an if(proto == IPPROTO_ESP) around the NATT policy check.
  35920. + *
  35921. + * Revision 1.171.2.8 2006/07/29 05:03:04 paul
  35922. + * Added check for new version of skb_linearize that only takes 1 argument,
  35923. + * for 2.6.18+ kernels.
  35924. + *
  35925. + * Revision 1.171.2.7 2006/04/20 16:33:07 mcr
  35926. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  35927. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  35928. + *
  35929. + * Revision 1.171.2.6 2005/12/07 06:07:04 paul
  35930. + * comment out KLIPS_DEC_USE in ipsec_rcv_decap. Likely an artifact from
  35931. + * refactoring. http://bugs.xelerance.com/view.php?id=454
  35932. + *
  35933. + * Revision 1.171.2.5 2005/10/21 02:22:29 mcr
  35934. + * pull up of another try at 2.4.x kernel fix
  35935. + *
  35936. + * Revision 1.171.2.4 2005/10/21 01:39:56 mcr
  35937. + * nat-t fix is 2.4/2.6 specific
  35938. + *
  35939. + * Revision 1.178 2005/10/21 02:19:34 mcr
  35940. + * on 2.4 systems, we have to fix up the length as well.
  35941. + *
  35942. + * Revision 1.177 2005/10/21 00:18:31 mcr
  35943. + * nat-t fix is 2.4 specific.
  35944. + *
  35945. + * Revision 1.176 2005/10/20 21:06:11 mcr
  35946. + * possible fix for nat-t problem on 2.4 kernels.
  35947. + *
  35948. + * Revision 1.175 2005/10/13 02:49:24 mcr
  35949. + * tested UDP-encapsulated ESP packets that were not actually ESP,
  35950. + * (but IKE) were being eaten.
  35951. + *
  35952. + * Revision 1.174 2005/10/13 01:25:22 mcr
  35953. + * UDP-encapsulated ESP packets that were not actually ESP,
  35954. + * (but IKE) were being eaten.
  35955. + *
  35956. + * Revision 1.173 2005/08/31 23:26:11 mcr
  35957. + * fixes for 2.6.13
  35958. + *
  35959. + * Revision 1.172 2005/08/05 08:44:54 mcr
  35960. + * ipsec_kern24.h (compat code for 2.4) must be include
  35961. + * explicitely now.
  35962. + *
  35963. + * Revision 1.171 2005/07/08 23:56:06 ken
  35964. + * #ifdef
  35965. + *
  35966. + * Revision 1.170 2005/07/08 23:50:05 ken
  35967. + * Don't attempt to decapsulate if NAT-T isn't available in the code
  35968. + *
  35969. + * Revision 1.169 2005/06/06 00:27:31 mcr
  35970. + * fix for making tcpdump (packet capture) work correctly for
  35971. + * nat-t received packets.
  35972. + *
  35973. + * Revision 1.168 2005/06/04 16:06:06 mcr
  35974. + * better patch for nat-t rcv-device code.
  35975. + *
  35976. + * Revision 1.167 2005/06/03 17:04:46 mcr
  35977. + * nat-t packets are forced to arrive from ipsec0.
  35978. + *
  35979. + * Revision 1.166 2005/04/29 05:10:22 mcr
  35980. + * removed from extraenous includes to make unit testing easier.
  35981. + *
  35982. + * Revision 1.165 2005/04/20 17:11:32 mcr
  35983. + * fixed to compile on 2.4.
  35984. + *
  35985. + * Revision 1.164 2005/04/18 03:09:50 ken
  35986. + * Fix typo
  35987. + *
  35988. + * Revision 1.163 2005/04/17 05:32:58 mcr
  35989. + * remove extraneous debugging
  35990. + * make sure to return success from klips26_encap_rcv().
  35991. + *
  35992. + * Revision 1.162 2005/04/17 04:37:01 mcr
  35993. + * make sure that irs->ipp is still set.
  35994. + *
  35995. + * Revision 1.161 2005/04/17 03:51:52 mcr
  35996. + * removed old comment about removed code.
  35997. + * added translation from udp.c/2.6 to KLIPS NAT-ESP naming.
  35998. + * comment about check for origin address/port for incoming NAT-ESP packets.
  35999. + *
  36000. + * Revision 1.160 2005/04/15 19:55:58 mcr
  36001. + * adjustments to use proper skb fields for data.
  36002. + *
  36003. + * Revision 1.159 2005/04/10 22:58:20 mcr
  36004. + * refactoring of receive functions to make it easier to
  36005. + * call the ESP decap.
  36006. + *
  36007. + * Revision 1.158 2005/04/08 18:27:53 mcr
  36008. + * refactored ipsec_rcv() into ipsec_rcv() and ipsec_rcv_decap().
  36009. + *
  36010. + * Revision 1.157 2004/12/28 23:13:09 mcr
  36011. + * use consistent CONFIG_IPSEC_NAT_TRAVERSAL.
  36012. + *
  36013. + * Revision 1.156 2004/12/03 21:34:51 mcr
  36014. + * mistype of KLIPS_USE_COUNT -> KLIPS_INC_USE;
  36015. + *
  36016. + * Revision 1.155 2004/12/03 21:25:57 mcr
  36017. + * compile time fixes for running on 2.6.
  36018. + * still experimental.
  36019. + *
  36020. + * Revision 1.154 2004/09/08 17:21:36 ken
  36021. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  36022. + *
  36023. + * Revision 1.153 2004/08/22 20:10:00 mcr
  36024. + * removed check for incorrect setting of NET_26.
  36025. + *
  36026. + * Revision 1.152 2004/08/21 15:22:39 mcr
  36027. + * added #defines for ATT heartbeat.
  36028. + *
  36029. + * Revision 1.151 2004/08/21 02:16:32 ken
  36030. + * Patch from Jochen Eisinger for AT&T MTS Heartbeat packet support
  36031. + *
  36032. + * Revision 1.150 2004/08/21 00:44:48 mcr
  36033. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  36034. + *
  36035. + * Revision 1.149 2004/08/20 21:45:45 mcr
  36036. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  36037. + * be 26sec compatible. But, some defines where changed.
  36038. + *
  36039. + * Revision 1.148 2004/08/17 03:27:23 mcr
  36040. + * klips 2.6 edits.
  36041. + *
  36042. + * Revision 1.147 2004/08/05 23:29:27 mcr
  36043. + * fixed nesting of #ifdef vs {} in ipsec_rcv().
  36044. + *
  36045. + * Revision 1.146 2004/08/04 15:57:07 mcr
  36046. + * moved des .h files to include/des/ *
  36047. + * included 2.6 protocol specific things
  36048. + * started at NAT-T support, but it will require a kernel patch.
  36049. + *
  36050. + * Revision 1.145 2004/08/03 18:19:08 mcr
  36051. + * in 2.6, use "net_device" instead of #define device->net_device.
  36052. + * this probably breaks 2.0 compiles.
  36053. + *
  36054. + * Revision 1.144 2004/07/10 19:11:18 mcr
  36055. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  36056. + *
  36057. + * Revision 1.143 2004/05/10 22:27:00 mcr
  36058. + * fix for ESP-3DES-noauth test case.
  36059. + *
  36060. + * Revision 1.142 2004/05/10 22:25:57 mcr
  36061. + * reformat of calls to ipsec_lifetime_check().
  36062. + *
  36063. + * Revision 1.141 2004/04/06 02:49:26 mcr
  36064. + * pullup of algo code from alg-branch.
  36065. + *
  36066. + * Revision 1.140 2004/02/03 03:12:53 mcr
  36067. + * removed erroneously, double patched code.
  36068. + *
  36069. + * Revision 1.139 2004/01/05 23:21:29 mcr
  36070. + * initialize sin_family in ipsec_rcv.c
  36071. + *
  36072. + * Revision 1.138 2003/12/24 19:46:52 mcr
  36073. + * if sock.h patch has not been applied, then define appropriate
  36074. + * structure so we can use it. This is serious inferior, and
  36075. + * depends upon the concept that the structure in question is
  36076. + * smaller than the other members of that union.
  36077. + * getting rid of differing methods is a better solution.
  36078. + *
  36079. + * Revision 1.137 2003/12/22 19:40:57 mcr
  36080. + * NAT-T patches 0.6c.
  36081. + *
  36082. + * Revision 1.136 2003/12/15 18:13:12 mcr
  36083. + * when compiling with NAT traversal, don't assume that the
  36084. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  36085. + * is set.
  36086. + *
  36087. + * Revision 1.135 2003/12/13 19:10:21 mcr
  36088. + * refactored rcv and xmit code - same as FS 2.05.
  36089. + *
  36090. + * Revision 1.134.2.1 2003/12/22 15:25:52 jjo
  36091. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  36092. + *
  36093. + * Revision 1.134 2003/12/10 01:14:27 mcr
  36094. + * NAT-traversal patches to KLIPS.
  36095. + *
  36096. + * Revision 1.133 2003/10/31 02:27:55 mcr
  36097. + * pulled up port-selector patches and sa_id elimination.
  36098. + *
  36099. + * Revision 1.132.2.1 2003/10/29 01:30:41 mcr
  36100. + * elimited "struct sa_id".
  36101. + *
  36102. + * Revision 1.132 2003/09/02 19:51:48 mcr
  36103. + * fixes for PR#252.
  36104. + *
  36105. + * Revision 1.131 2003/07/31 22:47:16 mcr
  36106. + * preliminary (untested by FS-team) 2.5 patches.
  36107. + *
  36108. + * Revision 1.130 2003/04/03 17:38:25 rgb
  36109. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  36110. + * Clarified logic for non-connected devices.
  36111. + *
  36112. + * Revision 1.129 2003/02/06 02:21:34 rgb
  36113. + *
  36114. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  36115. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  36116. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  36117. + *
  36118. + * Revision 1.128 2002/12/13 20:58:03 rgb
  36119. + * Relegated MCR's recent "_dmp" routine to debug_verbose.
  36120. + * Cleaned up printing of source and destination addresses in debug output.
  36121. + *
  36122. + * Revision 1.127 2002/12/04 16:00:16 rgb
  36123. + *
  36124. + * Fixed AH decapsulation pointer update bug and added some comments and
  36125. + * debugging.
  36126. + * This bug was caught by west-ah-0[12].
  36127. + *
  36128. + * Revision 1.126 2002/11/04 05:03:43 mcr
  36129. + * fixes for IPCOMP. There were two problems:
  36130. + * 1) the irs->ipp pointer was not being updated properly after
  36131. + * the ESP descryption. The meant nothing for IPIP, as the
  36132. + * later IP header overwrote the earlier one.
  36133. + * 2) the more serious problem was that skb_decompress will
  36134. + * usually allocate a new SKB, so we have to make sure that
  36135. + * it doesn't get lost.
  36136. + * #2 meant removing the skb argument from the ->decrypt routine
  36137. + * and moving it to the irs->skb, so it could be value/result.
  36138. + *
  36139. + * Revision 1.125 2002/11/01 01:53:35 dhr
  36140. + *
  36141. + * fix typo
  36142. + *
  36143. + * Revision 1.124 2002/10/31 22:49:01 dhr
  36144. + *
  36145. + * - eliminate unused variable "hash"
  36146. + * - reduce scope of variable "authenticator"
  36147. + * - add comment on a couple of tricky bits
  36148. + *
  36149. + * Revision 1.123 2002/10/31 22:39:56 dhr
  36150. + *
  36151. + * use correct type for result of function calls
  36152. + *
  36153. + * Revision 1.122 2002/10/31 22:36:25 dhr
  36154. + *
  36155. + * simplify complex test
  36156. + *
  36157. + * Revision 1.121 2002/10/31 22:34:04 dhr
  36158. + *
  36159. + * ipsprev is never used: ditch it
  36160. + *
  36161. + * Revision 1.120 2002/10/31 22:30:21 dhr
  36162. + *
  36163. + * eliminate redundant assignments
  36164. + *
  36165. + * Revision 1.119 2002/10/31 22:27:43 dhr
  36166. + *
  36167. + * make whitespace canonical
  36168. + *
  36169. + * Revision 1.118 2002/10/30 05:47:17 rgb
  36170. + * Fixed cut-and-paste error mis-identifying comp runt as ah.
  36171. + *
  36172. + * Revision 1.117 2002/10/17 16:37:45 rgb
  36173. + * Remove compp intermediate variable and in-line its contents
  36174. + * where used
  36175. + *
  36176. + * Revision 1.116 2002/10/12 23:11:53 dhr
  36177. + *
  36178. + * [KenB + DHR] more 64-bit cleanup
  36179. + *
  36180. + * Revision 1.115 2002/10/07 19:06:58 rgb
  36181. + * Minor fixups and activation to west-rcv-nfmark-set-01 test to check for SA reference properly set on incoming.
  36182. + *
  36183. + * Revision 1.114 2002/10/07 18:31:31 rgb
  36184. + * Set saref on incoming packets.
  36185. + *
  36186. + * Revision 1.113 2002/09/16 21:28:12 mcr
  36187. + * adjust hash length for HMAC calculation - must look at whether
  36188. + * it is MD5 or SHA1.
  36189. + *
  36190. + * Revision 1.112 2002/09/16 21:19:15 mcr
  36191. + * fixes for west-ah-icmp-01 - length of AH header must be
  36192. + * calculated properly, and next_header field properly copied.
  36193. + *
  36194. + * Revision 1.111 2002/09/10 02:45:56 mcr
  36195. + * re-factored the ipsec_rcv function into several functions,
  36196. + * ipsec_rcv_decap_once, and a set of functions for AH, ESP and IPCOMP.
  36197. + * In addition, the MD5 and SHA1 functions are replaced with pointers.
  36198. + *
  36199. + * Revision 1.110 2002/08/30 06:34:33 rgb
  36200. + * Fix scope of shift in AH header length check.
  36201. + *
  36202. + * Revision 1.109 2002/08/27 16:49:20 rgb
  36203. + * Fixed ESP short packet DOS (and AH and IPCOMP).
  36204. + *
  36205. + * Revision 1.108 2002/07/24 18:44:54 rgb
  36206. + * Type fiddling to tame ia64 compiler.
  36207. + *
  36208. + * Revision 1.107 2002/05/27 18:58:18 rgb
  36209. + * Convert to dynamic ipsec device allocation.
  36210. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  36211. + *
  36212. + * Revision 1.106 2002/05/23 07:15:21 rgb
  36213. + * Pointer clean-up.
  36214. + * Added refcount code.
  36215. + *
  36216. + * Revision 1.105 2002/05/14 02:35:06 rgb
  36217. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  36218. + * ipsec_sa or ipsec_sa.
  36219. + * Change references to _TDB to _IPSA.
  36220. + *
  36221. + * Revision 1.104 2002/04/24 07:55:32 mcr
  36222. + * #include patches and Makefiles for post-reorg compilation.
  36223. + *
  36224. + * Revision 1.103 2002/04/24 07:36:30 mcr
  36225. + * Moved from ./klips/net/ipsec/ipsec_rcv.c,v
  36226. + *
  36227. + * Revision 1.102 2002/01/29 17:17:56 mcr
  36228. + * moved include of ipsec_param.h to after include of linux/kernel.h
  36229. + * otherwise, it seems that some option that is set in ipsec_param.h
  36230. + * screws up something subtle in the include path to kernel.h, and
  36231. + * it complains on the snprintf() prototype.
  36232. + *
  36233. + * Revision 1.101 2002/01/29 04:00:52 mcr
  36234. + * more excise of kversions.h header.
  36235. + *
  36236. + * Revision 1.100 2002/01/29 02:13:17 mcr
  36237. + * introduction of ipsec_kversion.h means that include of
  36238. + * ipsec_param.h must preceed any decisions about what files to
  36239. + * include to deal with differences in kernel source.
  36240. + *
  36241. + * Revision 1.99 2002/01/28 21:40:59 mcr
  36242. + * should use #if to test boolean option rather than #ifdef.
  36243. + *
  36244. + * Revision 1.98 2002/01/20 20:19:36 mcr
  36245. + * renamed option to IP_FRAGMENT_LINEARIZE.
  36246. + *
  36247. + * Revision 1.97 2002/01/12 02:55:36 mcr
  36248. + * fix for post-2.4.4 to linearize skb's when ESP packet
  36249. + * was assembled from fragments.
  36250. + *
  36251. + * Revision 1.96 2001/11/26 09:23:49 rgb
  36252. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  36253. + *
  36254. + * Revision 1.93.2.2 2001/10/22 20:54:07 mcr
  36255. + * include des.h, removed phony prototypes and fixed calling
  36256. + * conventions to match real prototypes.
  36257. + *
  36258. + * Revision 1.93.2.1 2001/09/25 02:22:22 mcr
  36259. + * struct tdb -> struct ipsec_sa.
  36260. + * lifetime checks moved to ipsec_life.c
  36261. + * some sa(tdb) manipulation functions renamed.
  36262. + *
  36263. + * Revision 1.95 2001/11/06 19:49:07 rgb
  36264. + * Added variable descriptions.
  36265. + * Removed unauthenticated sequence==0 check to prevent DoS.
  36266. + *
  36267. + * Revision 1.94 2001/10/18 04:45:20 rgb
  36268. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  36269. + * lib/freeswan.h version macros moved to lib/kversions.h.
  36270. + * Other compiler directive cleanups.
  36271. + *
  36272. + * Revision 1.93 2001/09/07 22:17:24 rgb
  36273. + * Fix for removal of transport layer protocol handler arg in 2.4.4.
  36274. + * Fix to accomodate peer non-conformance to IPCOMP rfc2393.
  36275. + *
  36276. + * Revision 1.92 2001/08/27 19:44:41 rgb
  36277. + * Fix error in comment.
  36278. + *
  36279. + * Revision 1.91 2001/07/20 19:31:48 dhr
  36280. + * [DHR] fix source and destination subnets of policy in diagnostic
  36281. + *
  36282. + * Revision 1.90 2001/07/06 19:51:09 rgb
  36283. + * Added inbound policy checking code for IPIP SAs.
  36284. + * Renamed unused function argument for ease and intuitive naming.
  36285. + *
  36286. + * Revision 1.89 2001/06/22 19:35:23 rgb
  36287. + * Disable ipcomp processing if we are handed a ipcomp packet with no esp
  36288. + * or ah header.
  36289. + * Print protocol if we are handed a non-ipsec packet.
  36290. + *
  36291. + * Revision 1.88 2001/06/20 06:30:47 rgb
  36292. + * Fixed transport mode IPCOMP policy check bug.
  36293. + *
  36294. + * Revision 1.87 2001/06/13 20:58:40 rgb
  36295. + * Added parentheses around assignment used as truth value to silence
  36296. + * compiler.
  36297. + *
  36298. + * Revision 1.86 2001/06/07 22:25:23 rgb
  36299. + * Added a source address policy check for tunnel mode. It still does
  36300. + * not check client addresses and masks.
  36301. + * Only decapsulate IPIP if it is expected.
  36302. + *
  36303. + * Revision 1.85 2001/05/30 08:14:02 rgb
  36304. + * Removed vestiges of esp-null transforms.
  36305. + *
  36306. + * Revision 1.84 2001/05/27 06:12:11 rgb
  36307. + * Added structures for pid, packet count and last access time to eroute.
  36308. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  36309. + *
  36310. + * Revision 1.83 2001/05/04 16:45:47 rgb
  36311. + * Remove unneeded code. ipp is not used after this point.
  36312. + *
  36313. + * Revision 1.82 2001/05/04 16:36:00 rgb
  36314. + * Fix skb_cow() call for 2.4.4. (SS)
  36315. + *
  36316. + * Revision 1.81 2001/05/02 14:46:53 rgb
  36317. + * Fix typo for compiler directive to pull IPH back.
  36318. + *
  36319. + * Revision 1.80 2001/04/30 19:46:34 rgb
  36320. + * Update for 2.4.4. We now receive the skb with skb->data pointing to
  36321. + * h.raw.
  36322. + *
  36323. + * Revision 1.79 2001/04/23 15:01:15 rgb
  36324. + * Added spin_lock() check to prevent double-locking for multiple
  36325. + * transforms and hence kernel lock-ups with SMP kernels.
  36326. + * Minor spin_unlock() adjustments to unlock before non-dependant prints
  36327. + * and IPSEC device stats updates.
  36328. + *
  36329. + * Revision 1.78 2001/04/21 23:04:24 rgb
  36330. + * Check if soft expire has already been sent before sending another to
  36331. + * prevent ACQUIRE flooding.
  36332. + *
  36333. + * Revision 1.77 2001/03/16 07:35:20 rgb
  36334. + * Ditch extra #if 1 around now permanent policy checking code.
  36335. + *
  36336. + * Revision 1.76 2001/02/27 22:24:54 rgb
  36337. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  36338. + * Check for satoa() return codes.
  36339. + *
  36340. + * Revision 1.75 2001/02/19 22:28:30 rgb
  36341. + * Minor change to virtual device discovery code to assert which I/F has
  36342. + * been found.
  36343. + *
  36344. + * Revision 1.74 2000/11/25 03:50:36 rgb
  36345. + * Oops fix by minor re-arrangement of code to avoid accessing a freed tdb.
  36346. + *
  36347. + * Revision 1.73 2000/11/09 20:52:15 rgb
  36348. + * More spinlock shuffling, locking earlier and unlocking later in rcv to
  36349. + * include ipcomp and prevent races, renaming some tdb variables that got
  36350. + * forgotten, moving some unlocks to include tdbs and adding a missing
  36351. + * unlock. Thanks to Svenning for some of these.
  36352. + *
  36353. + * Revision 1.72 2000/11/09 20:11:22 rgb
  36354. + * Minor shuffles to fix non-standard kernel config option selection.
  36355. + *
  36356. + * Revision 1.71 2000/11/06 04:36:18 rgb
  36357. + * Ditched spin_lock_irqsave in favour of spin_lock.
  36358. + * Minor initial protocol check rewrite.
  36359. + * Clean up debug printing.
  36360. + * Clean up tdb handling on ipcomp.
  36361. + * Fixed transport mode null pointer de-reference without ipcomp.
  36362. + * Add Svenning's adaptive content compression.
  36363. + * Disabled registration of ipcomp handler.
  36364. + *
  36365. + * Revision 1.70 2000/10/30 23:41:43 henry
  36366. + * Hans-Joerg Hoexer's null-pointer fix
  36367. + *
  36368. + * Revision 1.69 2000/10/10 18:54:16 rgb
  36369. + * Added a fix for incoming policy check with ipcomp enabled but
  36370. + * uncompressible.
  36371. + *
  36372. + * Revision 1.68 2000/09/22 17:53:12 rgb
  36373. + * Fixed ipcomp tdb pointers update for policy checking.
  36374. + *
  36375. + * Revision 1.67 2000/09/21 03:40:58 rgb
  36376. + * Added more debugging to try and track down the cpi outward copy problem.
  36377. + *
  36378. + * Revision 1.66 2000/09/20 04:00:10 rgb
  36379. + * Changed static functions to DEBUG_NO_STATIC to reveal function names for
  36380. + * debugging oopsen.
  36381. + *
  36382. + * Revision 1.65 2000/09/19 07:07:16 rgb
  36383. + * Added debugging to inbound policy check for ipcomp.
  36384. + * Added missing spin_unlocks (thanks Svenning!).
  36385. + * Fixed misplaced tdbnext pointers causing mismatched ipip policy check.
  36386. + * Protect ipcomp policy check following ipip decap with sysctl switch.
  36387. + *
  36388. + * Revision 1.64 2000/09/18 21:27:29 rgb
  36389. + * 2.0 fixes.
  36390. + *
  36391. + * Revision 1.63 2000/09/18 02:35:50 rgb
  36392. + * Added policy checking to ipcomp and re-enabled policy checking by
  36393. + * default.
  36394. + * Optimised satoa calls.
  36395. + *
  36396. + * Revision 1.62 2000/09/17 21:02:32 rgb
  36397. + * Clean up debugging, removing slow timestamp debug code.
  36398. + *
  36399. + * Revision 1.61 2000/09/16 01:07:55 rgb
  36400. + * Fixed erroneous ref from struct ipcomp to struct ipcomphdr.
  36401. + *
  36402. + * Revision 1.60 2000/09/15 11:37:01 rgb
  36403. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  36404. + * IPCOMP zlib deflate code.
  36405. + *
  36406. + * Revision 1.59 2000/09/15 04:56:20 rgb
  36407. + * Remove redundant satoa() call, reformat comment.
  36408. + *
  36409. + * Revision 1.58 2000/09/13 08:00:52 rgb
  36410. + * Flick on inbound policy checking.
  36411. + *
  36412. + * Revision 1.57 2000/09/12 03:22:19 rgb
  36413. + * Converted inbound_policy_check to sysctl.
  36414. + * Re-enabled policy backcheck.
  36415. + * Moved policy checks to top and within tdb lock.
  36416. + *
  36417. + * Revision 1.56 2000/09/08 19:12:56 rgb
  36418. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  36419. + *
  36420. + * Revision 1.55 2000/08/28 18:15:46 rgb
  36421. + * Added MB's nf-debug reset patch.
  36422. + *
  36423. + * Revision 1.54 2000/08/27 01:41:26 rgb
  36424. + * More minor tweaks to the bad padding debug code.
  36425. + *
  36426. + * Revision 1.53 2000/08/24 16:54:16 rgb
  36427. + * Added KLIPS_PRINTMORE macro to continue lines without KERN_INFO level
  36428. + * info.
  36429. + * Tidied up device reporting at the start of ipsec_rcv.
  36430. + * Tidied up bad padding debugging and processing.
  36431. + *
  36432. + * Revision 1.52 2000/08/20 21:36:03 rgb
  36433. + * Activated pfkey_expire() calls.
  36434. + * Added a hard/soft expiry parameter to pfkey_expire().
  36435. + * Added sanity checking to avoid propagating zero or smaller-length skbs
  36436. + * from a bogus decryption.
  36437. + * Re-arranged the order of soft and hard expiry to conform to RFC2367.
  36438. + * Clean up references to CONFIG_IPSEC_PFKEYv2.
  36439. + *
  36440. + * Revision 1.51 2000/08/18 21:23:30 rgb
  36441. + * Improve bad padding warning so that the printk buffer doesn't get
  36442. + * trampled.
  36443. + *
  36444. + * Revision 1.50 2000/08/01 14:51:51 rgb
  36445. + * Removed _all_ remaining traces of DES.
  36446. + *
  36447. + * Revision 1.49 2000/07/28 13:50:53 rgb
  36448. + * Changed enet_statistics to net_device_stats and added back compatibility
  36449. + * for pre-2.1.19.
  36450. + *
  36451. + * Revision 1.48 2000/05/10 19:14:40 rgb
  36452. + * Only check usetime against soft and hard limits if the tdb has been
  36453. + * used.
  36454. + * Cast output of ntohl so that the broken prototype doesn't make our
  36455. + * compile noisy.
  36456. + *
  36457. + * Revision 1.47 2000/05/09 17:45:43 rgb
  36458. + * Fix replay bitmap corruption bug upon receipt of bogus packet
  36459. + * with correct SPI. This was a DoS.
  36460. + *
  36461. + * Revision 1.46 2000/03/27 02:31:58 rgb
  36462. + * Fixed authentication failure printout bug.
  36463. + *
  36464. + * Revision 1.45 2000/03/22 16:15:37 rgb
  36465. + * Fixed renaming of dev_get (MB).
  36466. + *
  36467. + * Revision 1.44 2000/03/16 08:17:24 rgb
  36468. + * Hardcode PF_KEYv2 support.
  36469. + * Fixed minor bug checking AH header length.
  36470. + *
  36471. + * Revision 1.43 2000/03/14 12:26:59 rgb
  36472. + * Added skb->nfct support for clearing netfilter conntrack bits (MB).
  36473. + *
  36474. + * Revision 1.42 2000/01/26 10:04:04 rgb
  36475. + * Fixed inbound policy checking on transport mode bug.
  36476. + * Fixed noisy 2.0 printk arguments.
  36477. + *
  36478. + * Revision 1.41 2000/01/24 20:58:02 rgb
  36479. + * Improve debugging/reporting support for (disabled) inbound
  36480. + * policy checking.
  36481. + *
  36482. + * Revision 1.40 2000/01/22 23:20:10 rgb
  36483. + * Fixed up inboud policy checking code.
  36484. + * Cleaned out unused crud.
  36485. + *
  36486. + * Revision 1.39 2000/01/21 06:15:29 rgb
  36487. + * Added sanity checks on skb_push(), skb_pull() to prevent panics.
  36488. + * Fixed cut-and-paste debug_tunnel to debug_rcv.
  36489. + * Added inbound policy checking code, disabled.
  36490. + * Simplified output code by updating ipp to post-IPIP decapsulation.
  36491. + *
  36492. + * elided pre-2000 comments. Use "cvs log"
  36493. + *
  36494. + *
  36495. + * Local Variables:
  36496. + * c-set-style: linux
  36497. + * End:
  36498. + *
  36499. + */
  36500. --- /dev/null Tue Mar 11 13:02:56 2003
  36501. +++ linux/net/ipsec/ipsec_sa.c Mon Feb 9 13:51:03 2004
  36502. @@ -0,0 +1,1870 @@
  36503. +/*
  36504. + * Common routines for IPsec SA maintenance routines.
  36505. + *
  36506. + * Copyright (C) 1996, 1997 John Ioannidis.
  36507. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Richard Guy Briggs.
  36508. + *
  36509. + * This program is free software; you can redistribute it and/or modify it
  36510. + * under the terms of the GNU General Public License as published by the
  36511. + * Free Software Foundation; either version 2 of the License, or (at your
  36512. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  36513. + *
  36514. + * This program is distributed in the hope that it will be useful, but
  36515. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  36516. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  36517. + * for more details.
  36518. + *
  36519. + * RCSID $Id: ipsec_sa.c,v 1.30.2.2 2006/10/06 21:39:26 paul Exp $
  36520. + *
  36521. + * This is the file formerly known as "ipsec_xform.h"
  36522. + *
  36523. + */
  36524. +
  36525. +#ifndef AUTOCONF_INCLUDED
  36526. +#include <linux/config.h>
  36527. +#endif
  36528. +#include <linux/version.h>
  36529. +#include <linux/kernel.h> /* printk() */
  36530. +
  36531. +#include "openswan/ipsec_param.h"
  36532. +
  36533. +#ifdef MALLOC_SLAB
  36534. +# include <linux/slab.h> /* kmalloc() */
  36535. +#else /* MALLOC_SLAB */
  36536. +# include <linux/malloc.h> /* kmalloc() */
  36537. +#endif /* MALLOC_SLAB */
  36538. +#include <linux/vmalloc.h> /* vmalloc() */
  36539. +#include <linux/errno.h> /* error codes */
  36540. +#include <linux/types.h> /* size_t */
  36541. +#include <linux/interrupt.h> /* mark_bh */
  36542. +
  36543. +#include <linux/netdevice.h> /* struct device, and other headers */
  36544. +#include <linux/etherdevice.h> /* eth_type_trans */
  36545. +#include <linux/ip.h> /* struct iphdr */
  36546. +#include <linux/skbuff.h>
  36547. +#include <openswan.h>
  36548. +#ifdef SPINLOCK
  36549. +#ifdef SPINLOCK_23
  36550. +#include <linux/spinlock.h> /* *lock* */
  36551. +#else /* SPINLOCK_23 */
  36552. +#include <asm/spinlock.h> /* *lock* */
  36553. +#endif /* SPINLOCK_23 */
  36554. +#endif /* SPINLOCK */
  36555. +
  36556. +#include <net/ip.h>
  36557. +
  36558. +#include "openswan/radij.h"
  36559. +
  36560. +#include "openswan/ipsec_stats.h"
  36561. +#include "openswan/ipsec_life.h"
  36562. +#include "openswan/ipsec_sa.h"
  36563. +#include "openswan/ipsec_xform.h"
  36564. +
  36565. +#include "openswan/ipsec_encap.h"
  36566. +#include "openswan/ipsec_radij.h"
  36567. +#include "openswan/ipsec_xform.h"
  36568. +#include "openswan/ipsec_ipe4.h"
  36569. +#include "openswan/ipsec_ah.h"
  36570. +#include "openswan/ipsec_esp.h"
  36571. +
  36572. +#include <pfkeyv2.h>
  36573. +#include <pfkey.h>
  36574. +
  36575. +#include "openswan/ipsec_proto.h"
  36576. +#include "openswan/ipsec_alg.h"
  36577. +
  36578. +
  36579. +#ifdef CONFIG_KLIPS_DEBUG
  36580. +int debug_xform = 0;
  36581. +#endif /* CONFIG_KLIPS_DEBUG */
  36582. +
  36583. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  36584. +
  36585. +struct ipsec_sa *ipsec_sadb_hash[SADB_HASHMOD];
  36586. +#ifdef SPINLOCK
  36587. +spinlock_t tdb_lock = SPIN_LOCK_UNLOCKED;
  36588. +#else /* SPINLOCK */
  36589. +spinlock_t tdb_lock;
  36590. +#endif /* SPINLOCK */
  36591. +
  36592. +struct ipsec_sadb ipsec_sadb;
  36593. +
  36594. +#if IPSEC_SA_REF_CODE
  36595. +
  36596. +/* the sub table must be narrower (or equal) in bits than the variable type
  36597. + in the main table to count the number of unused entries in it. */
  36598. +typedef struct {
  36599. + int testSizeOf_refSubTable :
  36600. + ((sizeof(IPsecRefTableUnusedCount) * 8) < IPSEC_SA_REF_SUBTABLE_IDX_WIDTH ? -1 : 1);
  36601. +} dummy;
  36602. +
  36603. +
  36604. +/* The field where the saref will be hosted in the skb must be wide enough to
  36605. + accomodate the information it needs to store. */
  36606. +typedef struct {
  36607. + int testSizeOf_refField :
  36608. + (IPSEC_SA_REF_HOST_FIELD_WIDTH < IPSEC_SA_REF_TABLE_IDX_WIDTH ? -1 : 1 );
  36609. +} dummy2;
  36610. +
  36611. +
  36612. +#define IPS_HASH(said) (((said)->spi + (said)->dst.u.v4.sin_addr.s_addr + (said)->proto) % SADB_HASHMOD)
  36613. +
  36614. +
  36615. +void
  36616. +ipsec_SAtest(void)
  36617. +{
  36618. + IPsecSAref_t SAref = 258;
  36619. + struct ipsec_sa ips;
  36620. + ips.ips_ref = 772;
  36621. +
  36622. + printk("klips_debug:ipsec_SAtest: "
  36623. + "IPSEC_SA_REF_SUBTABLE_IDX_WIDTH=%u\n"
  36624. + "IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES=%u\n"
  36625. + "IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES=%u\n"
  36626. + "IPSEC_SA_REF_HOST_FIELD_WIDTH=%lu\n"
  36627. + "IPSEC_SA_REF_TABLE_MASK=%x\n"
  36628. + "IPSEC_SA_REF_ENTRY_MASK=%x\n"
  36629. + "IPsecSAref2table(%d)=%u\n"
  36630. + "IPsecSAref2entry(%d)=%u\n"
  36631. + "IPsecSAref2NFmark(%d)=%u\n"
  36632. + "IPsecSAref2SA(%d)=%p\n"
  36633. + "IPsecSA2SAref(%p)=%d\n"
  36634. + ,
  36635. + IPSEC_SA_REF_SUBTABLE_IDX_WIDTH,
  36636. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES,
  36637. + IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES,
  36638. + (unsigned long) IPSEC_SA_REF_HOST_FIELD_WIDTH,
  36639. + IPSEC_SA_REF_TABLE_MASK,
  36640. + IPSEC_SA_REF_ENTRY_MASK,
  36641. + SAref, IPsecSAref2table(SAref),
  36642. + SAref, IPsecSAref2entry(SAref),
  36643. + SAref, IPsecSAref2NFmark(SAref),
  36644. + SAref, IPsecSAref2SA(SAref),
  36645. + (&ips), IPsecSA2SAref((&ips))
  36646. + );
  36647. + return;
  36648. +}
  36649. +
  36650. +int
  36651. +ipsec_SAref_recycle(void)
  36652. +{
  36653. + int table;
  36654. + int entry;
  36655. + int error = 0;
  36656. +
  36657. + ipsec_sadb.refFreeListHead = -1;
  36658. + ipsec_sadb.refFreeListTail = -1;
  36659. +
  36660. + if(ipsec_sadb.refFreeListCont == IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES * IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES) {
  36661. + KLIPS_PRINT(debug_xform,
  36662. + "klips_debug:ipsec_SAref_recycle: "
  36663. + "end of table reached, continuing at start..\n");
  36664. + ipsec_sadb.refFreeListCont = 0;
  36665. + }
  36666. +
  36667. + KLIPS_PRINT(debug_xform,
  36668. + "klips_debug:ipsec_SAref_recycle: "
  36669. + "recycling, continuing from SAref=%d (0p%p), table=%d, entry=%d.\n",
  36670. + ipsec_sadb.refFreeListCont,
  36671. + (ipsec_sadb.refTable[IPsecSAref2table(ipsec_sadb.refFreeListCont)] != NULL) ? IPsecSAref2SA(ipsec_sadb.refFreeListCont) : NULL,
  36672. + IPsecSAref2table(ipsec_sadb.refFreeListCont),
  36673. + IPsecSAref2entry(ipsec_sadb.refFreeListCont));
  36674. +
  36675. + for(table = IPsecSAref2table(ipsec_sadb.refFreeListCont);
  36676. + table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES;
  36677. + table++) {
  36678. + if(ipsec_sadb.refTable[table] == NULL) {
  36679. + error = ipsec_SArefSubTable_alloc(table);
  36680. + if(error) {
  36681. + return error;
  36682. + }
  36683. + }
  36684. + for(entry = IPsecSAref2entry(ipsec_sadb.refFreeListCont);
  36685. + entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES;
  36686. + entry++) {
  36687. + if(ipsec_sadb.refTable[table]->entry[entry] == NULL) {
  36688. + ipsec_sadb.refFreeList[++ipsec_sadb.refFreeListTail] = IPsecSArefBuild(table, entry);
  36689. + if(ipsec_sadb.refFreeListTail == (IPSEC_SA_REF_FREELIST_NUM_ENTRIES - 1)) {
  36690. + ipsec_sadb.refFreeListHead = 0;
  36691. + ipsec_sadb.refFreeListCont = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListTail] + 1;
  36692. + KLIPS_PRINT(debug_xform,
  36693. + "klips_debug:ipsec_SAref_recycle: "
  36694. + "SArefFreeList refilled.\n");
  36695. + return 0;
  36696. + }
  36697. + }
  36698. + }
  36699. + }
  36700. +
  36701. + if(ipsec_sadb.refFreeListTail == -1) {
  36702. + KLIPS_PRINT(debug_xform,
  36703. + "klips_debug:ipsec_SAref_recycle: "
  36704. + "out of room in the SArefTable.\n");
  36705. +
  36706. + return(-ENOSPC);
  36707. + }
  36708. +
  36709. + ipsec_sadb.refFreeListHead = 0;
  36710. + ipsec_sadb.refFreeListCont = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListTail] + 1;
  36711. + KLIPS_PRINT(debug_xform,
  36712. + "klips_debug:ipsec_SAref_recycle: "
  36713. + "SArefFreeList partly refilled to %d of %d.\n",
  36714. + ipsec_sadb.refFreeListTail,
  36715. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36716. + return 0;
  36717. +}
  36718. +
  36719. +int
  36720. +ipsec_SArefSubTable_alloc(unsigned table)
  36721. +{
  36722. + unsigned entry;
  36723. + struct IPsecSArefSubTable* SArefsub;
  36724. +
  36725. + KLIPS_PRINT(debug_xform,
  36726. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36727. + "allocating %lu bytes for table %u of %u.\n",
  36728. + (unsigned long) (IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES * sizeof(struct ipsec_sa *)),
  36729. + table,
  36730. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES);
  36731. +
  36732. + /* allocate another sub-table */
  36733. + SArefsub = vmalloc(IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES * sizeof(struct ipsec_sa *));
  36734. + if(SArefsub == NULL) {
  36735. + KLIPS_PRINT(debug_xform,
  36736. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36737. + "error allocating memory for table %u of %u!\n",
  36738. + table,
  36739. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES);
  36740. + return -ENOMEM;
  36741. + }
  36742. +
  36743. + /* add this sub-table to the main table */
  36744. + ipsec_sadb.refTable[table] = SArefsub;
  36745. +
  36746. + /* initialise each element to NULL */
  36747. + KLIPS_PRINT(debug_xform,
  36748. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36749. + "initialising %u elements (2 ^ %u) of table %u.\n",
  36750. + IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES,
  36751. + IPSEC_SA_REF_SUBTABLE_IDX_WIDTH,
  36752. + table);
  36753. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  36754. + SArefsub->entry[entry] = NULL;
  36755. + }
  36756. +
  36757. + return 0;
  36758. +}
  36759. +#endif /* IPSEC_SA_REF_CODE */
  36760. +
  36761. +int
  36762. +ipsec_saref_freelist_init(void)
  36763. +{
  36764. + int i;
  36765. +
  36766. + KLIPS_PRINT(debug_xform,
  36767. + "klips_debug:ipsec_saref_freelist_init: "
  36768. + "initialising %u elements of FreeList.\n",
  36769. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36770. +
  36771. + for(i = 0; i < IPSEC_SA_REF_FREELIST_NUM_ENTRIES; i++) {
  36772. + ipsec_sadb.refFreeList[i] = IPSEC_SAREF_NULL;
  36773. + }
  36774. + ipsec_sadb.refFreeListHead = -1;
  36775. + ipsec_sadb.refFreeListCont = 0;
  36776. + ipsec_sadb.refFreeListTail = -1;
  36777. +
  36778. + return 0;
  36779. +}
  36780. +
  36781. +int
  36782. +ipsec_sadb_init(void)
  36783. +{
  36784. + int error = 0;
  36785. + unsigned i;
  36786. +
  36787. + for(i = 0; i < SADB_HASHMOD; i++) {
  36788. + ipsec_sadb_hash[i] = NULL;
  36789. + }
  36790. + /* parts above are for the old style SADB hash table */
  36791. +
  36792. +
  36793. +#if IPSEC_SA_REF_CODE
  36794. + /* initialise SA reference table */
  36795. +
  36796. + /* initialise the main table */
  36797. + KLIPS_PRINT(debug_xform,
  36798. + "klips_debug:ipsec_sadb_init: "
  36799. + "initialising main table of size %u (2 ^ %u).\n",
  36800. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES,
  36801. + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH);
  36802. + {
  36803. + unsigned table;
  36804. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  36805. + ipsec_sadb.refTable[table] = NULL;
  36806. + }
  36807. + }
  36808. +
  36809. + /* allocate the first sub-table */
  36810. + error = ipsec_SArefSubTable_alloc(0);
  36811. + if(error) {
  36812. + return error;
  36813. + }
  36814. +
  36815. + error = ipsec_saref_freelist_init();
  36816. +#endif /* IPSEC_SA_REF_CODE */
  36817. + return error;
  36818. +}
  36819. +
  36820. +#if IPSEC_SA_REF_CODE
  36821. +IPsecSAref_t
  36822. +ipsec_SAref_alloc(int*error) /* pass in error var by pointer */
  36823. +{
  36824. + IPsecSAref_t SAref;
  36825. +
  36826. + KLIPS_PRINT(debug_xform,
  36827. + "klips_debug:ipsec_SAref_alloc: "
  36828. + "SAref requested... head=%d, cont=%d, tail=%d, listsize=%d.\n",
  36829. + ipsec_sadb.refFreeListHead,
  36830. + ipsec_sadb.refFreeListCont,
  36831. + ipsec_sadb.refFreeListTail,
  36832. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36833. +
  36834. + if(ipsec_sadb.refFreeListHead == -1) {
  36835. + KLIPS_PRINT(debug_xform,
  36836. + "klips_debug:ipsec_SAref_alloc: "
  36837. + "FreeList empty, recycling...\n");
  36838. + *error = ipsec_SAref_recycle();
  36839. + if(*error) {
  36840. + return IPSEC_SAREF_NULL;
  36841. + }
  36842. + }
  36843. +
  36844. + SAref = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListHead];
  36845. + if(SAref == IPSEC_SAREF_NULL) {
  36846. + KLIPS_PRINT(debug_xform,
  36847. + "klips_debug:ipsec_SAref_alloc: "
  36848. + "unexpected error, refFreeListHead = %d points to invalid entry.\n",
  36849. + ipsec_sadb.refFreeListHead);
  36850. + *error = -ESPIPE;
  36851. + return IPSEC_SAREF_NULL;
  36852. + }
  36853. +
  36854. + KLIPS_PRINT(debug_xform,
  36855. + "klips_debug:ipsec_SAref_alloc: "
  36856. + "allocating SAref=%d, table=%u, entry=%u of %u.\n",
  36857. + SAref,
  36858. + IPsecSAref2table(SAref),
  36859. + IPsecSAref2entry(SAref),
  36860. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES * IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES);
  36861. +
  36862. + ipsec_sadb.refFreeList[ipsec_sadb.refFreeListHead] = IPSEC_SAREF_NULL;
  36863. + ipsec_sadb.refFreeListHead++;
  36864. + if(ipsec_sadb.refFreeListHead > ipsec_sadb.refFreeListTail) {
  36865. + KLIPS_PRINT(debug_xform,
  36866. + "klips_debug:ipsec_SAref_alloc: "
  36867. + "last FreeList entry allocated, resetting list head to empty.\n");
  36868. + ipsec_sadb.refFreeListHead = -1;
  36869. + }
  36870. +
  36871. + return SAref;
  36872. +}
  36873. +#endif /* IPSEC_SA_REF_CODE */
  36874. +
  36875. +int
  36876. +ipsec_sa_print(struct ipsec_sa *ips)
  36877. +{
  36878. + char sa[SATOT_BUF];
  36879. + size_t sa_len;
  36880. +
  36881. + printk(KERN_INFO "klips_debug: SA:");
  36882. + if(ips == NULL) {
  36883. + printk("NULL\n");
  36884. + return -ENOENT;
  36885. + }
  36886. + printk(" ref=%d", ips->ips_ref);
  36887. + printk(" refcount=%d", atomic_read(&ips->ips_refcount));
  36888. + if(ips->ips_hnext != NULL) {
  36889. + printk(" hnext=0p%p", ips->ips_hnext);
  36890. + }
  36891. + if(ips->ips_inext != NULL) {
  36892. + printk(" inext=0p%p", ips->ips_inext);
  36893. + }
  36894. + if(ips->ips_onext != NULL) {
  36895. + printk(" onext=0p%p", ips->ips_onext);
  36896. + }
  36897. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  36898. + printk(" said=%s", sa_len ? sa : " (error)");
  36899. + if(ips->ips_seq) {
  36900. + printk(" seq=%u", ips->ips_seq);
  36901. + }
  36902. + if(ips->ips_pid) {
  36903. + printk(" pid=%u", ips->ips_pid);
  36904. + }
  36905. + if(ips->ips_authalg) {
  36906. + printk(" authalg=%u", ips->ips_authalg);
  36907. + }
  36908. + if(ips->ips_encalg) {
  36909. + printk(" encalg=%u", ips->ips_encalg);
  36910. + }
  36911. + printk(" XFORM=%s%s%s", IPS_XFORM_NAME(ips));
  36912. + if(ips->ips_replaywin) {
  36913. + printk(" ooowin=%u", ips->ips_replaywin);
  36914. + }
  36915. + if(ips->ips_flags) {
  36916. + printk(" flags=%u", ips->ips_flags);
  36917. + }
  36918. + if(ips->ips_addr_s) {
  36919. + char buf[SUBNETTOA_BUF];
  36920. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_s))->sin_addr,
  36921. + 0, buf, sizeof(buf));
  36922. + printk(" src=%s", buf);
  36923. + }
  36924. + if(ips->ips_addr_d) {
  36925. + char buf[SUBNETTOA_BUF];
  36926. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_s))->sin_addr,
  36927. + 0, buf, sizeof(buf));
  36928. + printk(" dst=%s", buf);
  36929. + }
  36930. + if(ips->ips_addr_p) {
  36931. + char buf[SUBNETTOA_BUF];
  36932. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_p))->sin_addr,
  36933. + 0, buf, sizeof(buf));
  36934. + printk(" proxy=%s", buf);
  36935. + }
  36936. + if(ips->ips_key_bits_a) {
  36937. + printk(" key_bits_a=%u", ips->ips_key_bits_a);
  36938. + }
  36939. + if(ips->ips_key_bits_e) {
  36940. + printk(" key_bits_e=%u", ips->ips_key_bits_e);
  36941. + }
  36942. +
  36943. + printk("\n");
  36944. + return 0;
  36945. +}
  36946. +
  36947. +struct ipsec_sa*
  36948. +ipsec_sa_alloc(int*error) /* pass in error var by pointer */
  36949. +{
  36950. + struct ipsec_sa* ips;
  36951. +
  36952. + if((ips = kmalloc(sizeof(*ips), GFP_ATOMIC) ) == NULL) {
  36953. + KLIPS_PRINT(debug_xform,
  36954. + "klips_debug:ipsec_sa_alloc: "
  36955. + "memory allocation error\n");
  36956. + *error = -ENOMEM;
  36957. + return NULL;
  36958. + }
  36959. + memset((caddr_t)ips, 0, sizeof(*ips));
  36960. +#if IPSEC_SA_REF_CODE
  36961. + ips->ips_ref = ipsec_SAref_alloc(error); /* pass in error return by pointer */
  36962. + KLIPS_PRINT(debug_xform,
  36963. + "klips_debug:ipsec_sa_alloc: "
  36964. + "allocated %lu bytes for ipsec_sa struct=0p%p ref=%d.\n",
  36965. + (unsigned long) sizeof(*ips),
  36966. + ips,
  36967. + ips->ips_ref);
  36968. + if(ips->ips_ref == IPSEC_SAREF_NULL) {
  36969. + kfree(ips);
  36970. + KLIPS_PRINT(debug_xform,
  36971. + "klips_debug:ipsec_sa_alloc: "
  36972. + "SAref allocation error\n");
  36973. + return NULL;
  36974. + }
  36975. +
  36976. + atomic_inc(&ips->ips_refcount);
  36977. + IPsecSAref2SA(ips->ips_ref) = ips;
  36978. +#endif /* IPSEC_SA_REF_CODE */
  36979. +
  36980. + *error = 0;
  36981. + return(ips);
  36982. +}
  36983. +
  36984. +int
  36985. +ipsec_sa_free(struct ipsec_sa* ips)
  36986. +{
  36987. + return ipsec_sa_wipe(ips);
  36988. +}
  36989. +
  36990. +struct ipsec_sa *
  36991. +ipsec_sa_getbyid(ip_said *said)
  36992. +{
  36993. + int hashval;
  36994. + struct ipsec_sa *ips;
  36995. + char sa[SATOT_BUF];
  36996. + size_t sa_len;
  36997. +
  36998. + if(said == NULL) {
  36999. + KLIPS_PRINT(debug_xform,
  37000. + "klips_error:ipsec_sa_getbyid: "
  37001. + "null pointer passed in!\n");
  37002. + return NULL;
  37003. + }
  37004. +
  37005. + sa_len = satot(said, 0, sa, sizeof(sa));
  37006. +
  37007. + hashval = IPS_HASH(said);
  37008. +
  37009. + KLIPS_PRINT(debug_xform,
  37010. + "klips_debug:ipsec_sa_getbyid: "
  37011. + "linked entry in ipsec_sa table for hash=%d of SA:%s requested.\n",
  37012. + hashval,
  37013. + sa_len ? sa : " (error)");
  37014. +
  37015. + if((ips = ipsec_sadb_hash[hashval]) == NULL) {
  37016. + KLIPS_PRINT(debug_xform,
  37017. + "klips_debug:ipsec_sa_getbyid: "
  37018. + "no entries in ipsec_sa table for hash=%d of SA:%s.\n",
  37019. + hashval,
  37020. + sa_len ? sa : " (error)");
  37021. + return NULL;
  37022. + }
  37023. +
  37024. + for (; ips; ips = ips->ips_hnext) {
  37025. + if ((ips->ips_said.spi == said->spi) &&
  37026. + (ips->ips_said.dst.u.v4.sin_addr.s_addr == said->dst.u.v4.sin_addr.s_addr) &&
  37027. + (ips->ips_said.proto == said->proto)) {
  37028. + atomic_inc(&ips->ips_refcount);
  37029. + return ips;
  37030. + }
  37031. + }
  37032. +
  37033. + KLIPS_PRINT(debug_xform,
  37034. + "klips_debug:ipsec_sa_getbyid: "
  37035. + "no entry in linked list for hash=%d of SA:%s.\n",
  37036. + hashval,
  37037. + sa_len ? sa : " (error)");
  37038. + return NULL;
  37039. +}
  37040. +
  37041. +int
  37042. +ipsec_sa_put(struct ipsec_sa *ips)
  37043. +{
  37044. + char sa[SATOT_BUF];
  37045. + size_t sa_len;
  37046. +
  37047. + if(ips == NULL) {
  37048. + KLIPS_PRINT(debug_xform,
  37049. + "klips_error:ipsec_sa_put: "
  37050. + "null pointer passed in!\n");
  37051. + return -1;
  37052. + }
  37053. +
  37054. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37055. +
  37056. + KLIPS_PRINT(debug_xform,
  37057. + "klips_debug:ipsec_sa_put: "
  37058. + "ipsec_sa SA:%s, ref:%d reference count decremented.\n",
  37059. + sa_len ? sa : " (error)",
  37060. + ips->ips_ref);
  37061. +
  37062. + atomic_dec(&ips->ips_refcount);
  37063. +
  37064. + return 0;
  37065. +}
  37066. +
  37067. +/*
  37068. + The ipsec_sa table better *NOT* be locked before it is handed in, or SMP locks will happen
  37069. +*/
  37070. +int
  37071. +ipsec_sa_add(struct ipsec_sa *ips)
  37072. +{
  37073. + int error = 0;
  37074. + unsigned int hashval;
  37075. +
  37076. + if(ips == NULL) {
  37077. + KLIPS_PRINT(debug_xform,
  37078. + "klips_error:ipsec_sa_add: "
  37079. + "null pointer passed in!\n");
  37080. + return -ENODATA;
  37081. + }
  37082. + hashval = IPS_HASH(&ips->ips_said);
  37083. +
  37084. + atomic_inc(&ips->ips_refcount);
  37085. + spin_lock_bh(&tdb_lock);
  37086. +
  37087. + ips->ips_hnext = ipsec_sadb_hash[hashval];
  37088. + ipsec_sadb_hash[hashval] = ips;
  37089. +
  37090. + spin_unlock_bh(&tdb_lock);
  37091. +
  37092. + return error;
  37093. +}
  37094. +
  37095. +/*
  37096. + The ipsec_sa table better be locked before it is handed in, or races might happen
  37097. +*/
  37098. +int
  37099. +ipsec_sa_del(struct ipsec_sa *ips)
  37100. +{
  37101. + unsigned int hashval;
  37102. + struct ipsec_sa *ipstp;
  37103. + char sa[SATOT_BUF];
  37104. + size_t sa_len;
  37105. +
  37106. + if(ips == NULL) {
  37107. + KLIPS_PRINT(debug_xform,
  37108. + "klips_error:ipsec_sa_del: "
  37109. + "null pointer passed in!\n");
  37110. + return -ENODATA;
  37111. + }
  37112. +
  37113. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37114. + if(ips->ips_inext || ips->ips_onext) {
  37115. + KLIPS_PRINT(debug_xform,
  37116. + "klips_error:ipsec_sa_del: "
  37117. + "SA:%s still linked!\n",
  37118. + sa_len ? sa : " (error)");
  37119. + return -EMLINK;
  37120. + }
  37121. +
  37122. + hashval = IPS_HASH(&ips->ips_said);
  37123. +
  37124. + KLIPS_PRINT(debug_xform,
  37125. + "klips_debug:ipsec_sa_del: "
  37126. + "deleting SA:%s, hashval=%d.\n",
  37127. + sa_len ? sa : " (error)",
  37128. + hashval);
  37129. + if(ipsec_sadb_hash[hashval] == NULL) {
  37130. + KLIPS_PRINT(debug_xform,
  37131. + "klips_debug:ipsec_sa_del: "
  37132. + "no entries in ipsec_sa table for hash=%d of SA:%s.\n",
  37133. + hashval,
  37134. + sa_len ? sa : " (error)");
  37135. + return -ENOENT;
  37136. + }
  37137. +
  37138. + if (ips == ipsec_sadb_hash[hashval]) {
  37139. + ipsec_sadb_hash[hashval] = ipsec_sadb_hash[hashval]->ips_hnext;
  37140. + ips->ips_hnext = NULL;
  37141. + atomic_dec(&ips->ips_refcount);
  37142. + KLIPS_PRINT(debug_xform,
  37143. + "klips_debug:ipsec_sa_del: "
  37144. + "successfully deleted first ipsec_sa in chain.\n");
  37145. + return 0;
  37146. + } else {
  37147. + for (ipstp = ipsec_sadb_hash[hashval];
  37148. + ipstp;
  37149. + ipstp = ipstp->ips_hnext) {
  37150. + if (ipstp->ips_hnext == ips) {
  37151. + ipstp->ips_hnext = ips->ips_hnext;
  37152. + ips->ips_hnext = NULL;
  37153. + atomic_dec(&ips->ips_refcount);
  37154. + KLIPS_PRINT(debug_xform,
  37155. + "klips_debug:ipsec_sa_del: "
  37156. + "successfully deleted link in ipsec_sa chain.\n");
  37157. + return 0;
  37158. + }
  37159. + }
  37160. + }
  37161. +
  37162. + KLIPS_PRINT(debug_xform,
  37163. + "klips_debug:ipsec_sa_del: "
  37164. + "no entries in linked list for hash=%d of SA:%s.\n",
  37165. + hashval,
  37166. + sa_len ? sa : " (error)");
  37167. + return -ENOENT;
  37168. +}
  37169. +
  37170. +/*
  37171. + The ipsec_sa table better be locked before it is handed in, or races
  37172. + might happen
  37173. +*/
  37174. +int
  37175. +ipsec_sa_delchain(struct ipsec_sa *ips)
  37176. +{
  37177. + struct ipsec_sa *ipsdel;
  37178. + int error = 0;
  37179. + char sa[SATOT_BUF];
  37180. + size_t sa_len;
  37181. +
  37182. + if(ips == NULL) {
  37183. + KLIPS_PRINT(debug_xform,
  37184. + "klips_error:ipsec_sa_delchain: "
  37185. + "null pointer passed in!\n");
  37186. + return -ENODATA;
  37187. + }
  37188. +
  37189. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37190. + KLIPS_PRINT(debug_xform,
  37191. + "klips_debug:ipsec_sa_delchain: "
  37192. + "passed SA:%s\n",
  37193. + sa_len ? sa : " (error)");
  37194. + while(ips->ips_onext != NULL) {
  37195. + ips = ips->ips_onext;
  37196. + }
  37197. +
  37198. + while(ips) {
  37199. + /* XXX send a pfkey message up to advise of deleted ipsec_sa */
  37200. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37201. + KLIPS_PRINT(debug_xform,
  37202. + "klips_debug:ipsec_sa_delchain: "
  37203. + "unlinking and delting SA:%s",
  37204. + sa_len ? sa : " (error)");
  37205. + ipsdel = ips;
  37206. + ips = ips->ips_inext;
  37207. + if(ips != NULL) {
  37208. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37209. + KLIPS_PRINT(debug_xform,
  37210. + ", inext=%s",
  37211. + sa_len ? sa : " (error)");
  37212. + atomic_dec(&ipsdel->ips_refcount);
  37213. + ipsdel->ips_inext = NULL;
  37214. + atomic_dec(&ips->ips_refcount);
  37215. + ips->ips_onext = NULL;
  37216. + }
  37217. + KLIPS_PRINT(debug_xform,
  37218. + ".\n");
  37219. + if((error = ipsec_sa_del(ipsdel))) {
  37220. + KLIPS_PRINT(debug_xform,
  37221. + "klips_debug:ipsec_sa_delchain: "
  37222. + "ipsec_sa_del returned error %d.\n", -error);
  37223. + return error;
  37224. + }
  37225. + if((error = ipsec_sa_wipe(ipsdel))) {
  37226. + KLIPS_PRINT(debug_xform,
  37227. + "klips_debug:ipsec_sa_delchain: "
  37228. + "ipsec_sa_wipe returned error %d.\n", -error);
  37229. + return error;
  37230. + }
  37231. + }
  37232. + return error;
  37233. +}
  37234. +
  37235. +int
  37236. +ipsec_sadb_cleanup(__u8 proto)
  37237. +{
  37238. + unsigned i;
  37239. + int error = 0;
  37240. + struct ipsec_sa *ips, **ipsprev, *ipsdel;
  37241. + char sa[SATOT_BUF];
  37242. + size_t sa_len;
  37243. +
  37244. + KLIPS_PRINT(debug_xform,
  37245. + "klips_debug:ipsec_sadb_cleanup: "
  37246. + "cleaning up proto=%d.\n",
  37247. + proto);
  37248. +
  37249. + spin_lock_bh(&tdb_lock);
  37250. +
  37251. + for (i = 0; i < SADB_HASHMOD; i++) {
  37252. + ipsprev = &(ipsec_sadb_hash[i]);
  37253. + ips = ipsec_sadb_hash[i];
  37254. + if(ips != NULL) {
  37255. + atomic_inc(&ips->ips_refcount);
  37256. + }
  37257. + for(; ips != NULL;) {
  37258. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37259. + KLIPS_PRINT(debug_xform,
  37260. + "klips_debug:ipsec_sadb_cleanup: "
  37261. + "checking SA:%s, hash=%d, ref=%d",
  37262. + sa_len ? sa : " (error)",
  37263. + i,
  37264. + ips->ips_ref);
  37265. + ipsdel = ips;
  37266. + ips = ipsdel->ips_hnext;
  37267. + if(ips != NULL) {
  37268. + atomic_inc(&ips->ips_refcount);
  37269. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37270. + KLIPS_PRINT(debug_xform,
  37271. + ", hnext=%s",
  37272. + sa_len ? sa : " (error)");
  37273. + }
  37274. + if(*ipsprev != NULL) {
  37275. + sa_len = satot(&(*ipsprev)->ips_said, 0, sa, sizeof(sa));
  37276. + KLIPS_PRINT(debug_xform,
  37277. + ", *ipsprev=%s",
  37278. + sa_len ? sa : " (error)");
  37279. + if((*ipsprev)->ips_hnext) {
  37280. + sa_len = satot(&(*ipsprev)->ips_hnext->ips_said, 0, sa, sizeof(sa));
  37281. + KLIPS_PRINT(debug_xform,
  37282. + ", *ipsprev->ips_hnext=%s",
  37283. + sa_len ? sa : " (error)");
  37284. + }
  37285. + }
  37286. + KLIPS_PRINT(debug_xform,
  37287. + ".\n");
  37288. + if(proto == 0 || (proto == ipsdel->ips_said.proto)) {
  37289. + sa_len = satot(&ipsdel->ips_said, 0, sa, sizeof(sa));
  37290. + KLIPS_PRINT(debug_xform,
  37291. + "klips_debug:ipsec_sadb_cleanup: "
  37292. + "deleting SA chain:%s.\n",
  37293. + sa_len ? sa : " (error)");
  37294. + if((error = ipsec_sa_delchain(ipsdel))) {
  37295. + SENDERR(-error);
  37296. + }
  37297. + ipsprev = &(ipsec_sadb_hash[i]);
  37298. + ips = ipsec_sadb_hash[i];
  37299. +
  37300. + KLIPS_PRINT(debug_xform,
  37301. + "klips_debug:ipsec_sadb_cleanup: "
  37302. + "deleted SA chain:%s",
  37303. + sa_len ? sa : " (error)");
  37304. + if(ips != NULL) {
  37305. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37306. + KLIPS_PRINT(debug_xform,
  37307. + ", ipsec_sadb_hash[%d]=%s",
  37308. + i,
  37309. + sa_len ? sa : " (error)");
  37310. + }
  37311. + if(*ipsprev != NULL) {
  37312. + sa_len = satot(&(*ipsprev)->ips_said, 0, sa, sizeof(sa));
  37313. + KLIPS_PRINT(debug_xform,
  37314. + ", *ipsprev=%s",
  37315. + sa_len ? sa : " (error)");
  37316. + if((*ipsprev)->ips_hnext != NULL) {
  37317. + sa_len = satot(&(*ipsprev)->ips_hnext->ips_said, 0, sa, sizeof(sa));
  37318. + KLIPS_PRINT(debug_xform,
  37319. + ", *ipsprev->ips_hnext=%s",
  37320. + sa_len ? sa : " (error)");
  37321. + }
  37322. + }
  37323. + KLIPS_PRINT(debug_xform,
  37324. + ".\n");
  37325. + } else {
  37326. + ipsprev = &ipsdel;
  37327. + }
  37328. + if(ipsdel != NULL) {
  37329. + ipsec_sa_put(ipsdel);
  37330. + }
  37331. + }
  37332. + }
  37333. + errlab:
  37334. +
  37335. + spin_unlock_bh(&tdb_lock);
  37336. +
  37337. +
  37338. +#if IPSEC_SA_REF_CODE
  37339. + /* clean up SA reference table */
  37340. +
  37341. + /* go through the ref table and clean out all the SAs */
  37342. + KLIPS_PRINT(debug_xform,
  37343. + "klips_debug:ipsec_sadb_cleanup: "
  37344. + "removing SAref entries and tables.");
  37345. + {
  37346. + unsigned table, entry;
  37347. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  37348. + KLIPS_PRINT(debug_xform,
  37349. + "klips_debug:ipsec_sadb_cleanup: "
  37350. + "cleaning SAref table=%u.\n",
  37351. + table);
  37352. + if(ipsec_sadb.refTable[table] == NULL) {
  37353. + printk("\n");
  37354. + KLIPS_PRINT(debug_xform,
  37355. + "klips_debug:ipsec_sadb_cleanup: "
  37356. + "cleaned %u used refTables.\n",
  37357. + table);
  37358. + break;
  37359. + }
  37360. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  37361. + if(ipsec_sadb.refTable[table]->entry[entry] != NULL) {
  37362. + ipsec_sa_delchain(ipsec_sadb.refTable[table]->entry[entry]);
  37363. + ipsec_sadb.refTable[table]->entry[entry] = NULL;
  37364. + }
  37365. + }
  37366. + }
  37367. + }
  37368. +#endif /* IPSEC_SA_REF_CODE */
  37369. +
  37370. + return(error);
  37371. +}
  37372. +
  37373. +int
  37374. +ipsec_sadb_free(void)
  37375. +{
  37376. + int error = 0;
  37377. +
  37378. + KLIPS_PRINT(debug_xform,
  37379. + "klips_debug:ipsec_sadb_free: "
  37380. + "freeing SArefTable memory.\n");
  37381. +
  37382. + /* clean up SA reference table */
  37383. +
  37384. + /* go through the ref table and clean out all the SAs if any are
  37385. + left and free table memory */
  37386. + KLIPS_PRINT(debug_xform,
  37387. + "klips_debug:ipsec_sadb_free: "
  37388. + "removing SAref entries and tables.\n");
  37389. + {
  37390. + unsigned table, entry;
  37391. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  37392. + KLIPS_PRINT(debug_xform,
  37393. + "klips_debug:ipsec_sadb_free: "
  37394. + "removing SAref table=%u.\n",
  37395. + table);
  37396. + if(ipsec_sadb.refTable[table] == NULL) {
  37397. + KLIPS_PRINT(debug_xform,
  37398. + "klips_debug:ipsec_sadb_free: "
  37399. + "removed %u used refTables.\n",
  37400. + table);
  37401. + break;
  37402. + }
  37403. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  37404. + if(ipsec_sadb.refTable[table]->entry[entry] != NULL) {
  37405. + ipsec_sa_delchain(ipsec_sadb.refTable[table]->entry[entry]);
  37406. + ipsec_sadb.refTable[table]->entry[entry] = NULL;
  37407. + }
  37408. + }
  37409. + vfree(ipsec_sadb.refTable[table]);
  37410. + ipsec_sadb.refTable[table] = NULL;
  37411. + }
  37412. + }
  37413. +
  37414. + return(error);
  37415. +}
  37416. +
  37417. +int
  37418. +ipsec_sa_wipe(struct ipsec_sa *ips)
  37419. +{
  37420. + if(ips == NULL) {
  37421. + return -ENODATA;
  37422. + }
  37423. +
  37424. + /* if(atomic_dec_and_test(ips)) {
  37425. + }; */
  37426. +
  37427. +#if IPSEC_SA_REF_CODE
  37428. + /* remove me from the SArefTable */
  37429. + {
  37430. + char sa[SATOT_BUF];
  37431. + size_t sa_len;
  37432. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37433. + KLIPS_PRINT(debug_xform,
  37434. + "klips_debug:ipsec_sa_wipe: "
  37435. + "removing SA=%s(0p%p), SAref=%d, table=%d(0p%p), entry=%d from the refTable.\n",
  37436. + sa_len ? sa : " (error)",
  37437. + ips,
  37438. + ips->ips_ref,
  37439. + IPsecSAref2table(IPsecSA2SAref(ips)),
  37440. + ipsec_sadb.refTable[IPsecSAref2table(IPsecSA2SAref(ips))],
  37441. + IPsecSAref2entry(IPsecSA2SAref(ips)));
  37442. + }
  37443. + if(ips->ips_ref == IPSEC_SAREF_NULL) {
  37444. + KLIPS_PRINT(debug_xform,
  37445. + "klips_debug:ipsec_sa_wipe: "
  37446. + "why does this SA not have a valid SAref?.\n");
  37447. + }
  37448. + ipsec_sadb.refTable[IPsecSAref2table(IPsecSA2SAref(ips))]->entry[IPsecSAref2entry(IPsecSA2SAref(ips))] = NULL;
  37449. + ips->ips_ref = IPSEC_SAREF_NULL;
  37450. + ipsec_sa_put(ips);
  37451. +#endif /* IPSEC_SA_REF_CODE */
  37452. +
  37453. + /* paranoid clean up */
  37454. + if(ips->ips_addr_s != NULL) {
  37455. + memset((caddr_t)(ips->ips_addr_s), 0, ips->ips_addr_s_size);
  37456. + kfree(ips->ips_addr_s);
  37457. + }
  37458. + ips->ips_addr_s = NULL;
  37459. +
  37460. + if(ips->ips_addr_d != NULL) {
  37461. + memset((caddr_t)(ips->ips_addr_d), 0, ips->ips_addr_d_size);
  37462. + kfree(ips->ips_addr_d);
  37463. + }
  37464. + ips->ips_addr_d = NULL;
  37465. +
  37466. + if(ips->ips_addr_p != NULL) {
  37467. + memset((caddr_t)(ips->ips_addr_p), 0, ips->ips_addr_p_size);
  37468. + kfree(ips->ips_addr_p);
  37469. + }
  37470. + ips->ips_addr_p = NULL;
  37471. +
  37472. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  37473. + if(ips->ips_natt_oa) {
  37474. + memset((caddr_t)(ips->ips_natt_oa), 0, ips->ips_natt_oa_size);
  37475. + kfree(ips->ips_natt_oa);
  37476. + }
  37477. + ips->ips_natt_oa = NULL;
  37478. +#endif
  37479. +
  37480. + if(ips->ips_key_a != NULL) {
  37481. + memset((caddr_t)(ips->ips_key_a), 0, ips->ips_key_a_size);
  37482. + kfree(ips->ips_key_a);
  37483. + }
  37484. + ips->ips_key_a = NULL;
  37485. +
  37486. + if(ips->ips_key_e != NULL) {
  37487. + if (ips->ips_alg_enc &&
  37488. + ips->ips_alg_enc->ixt_e_destroy_key)
  37489. + {
  37490. + ips->ips_alg_enc->ixt_e_destroy_key(ips->ips_alg_enc,
  37491. + ips->ips_key_e);
  37492. + } else
  37493. + {
  37494. + memset((caddr_t)(ips->ips_key_e), 0, ips->ips_key_e_size);
  37495. + kfree(ips->ips_key_e);
  37496. + }
  37497. + }
  37498. + ips->ips_key_e = NULL;
  37499. +
  37500. + if(ips->ips_iv != NULL) {
  37501. + memset((caddr_t)(ips->ips_iv), 0, ips->ips_iv_size);
  37502. + kfree(ips->ips_iv);
  37503. + }
  37504. + ips->ips_iv = NULL;
  37505. +
  37506. + if(ips->ips_ident_s.data != NULL) {
  37507. + memset((caddr_t)(ips->ips_ident_s.data),
  37508. + 0,
  37509. + ips->ips_ident_s.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident));
  37510. + kfree(ips->ips_ident_s.data);
  37511. + }
  37512. + ips->ips_ident_s.data = NULL;
  37513. +
  37514. + if(ips->ips_ident_d.data != NULL) {
  37515. + memset((caddr_t)(ips->ips_ident_d.data),
  37516. + 0,
  37517. + ips->ips_ident_d.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident));
  37518. + kfree(ips->ips_ident_d.data);
  37519. + }
  37520. + ips->ips_ident_d.data = NULL;
  37521. +
  37522. + if (ips->ips_alg_enc||ips->ips_alg_auth) {
  37523. + ipsec_alg_sa_wipe(ips);
  37524. + }
  37525. +
  37526. + memset((caddr_t)ips, 0, sizeof(*ips));
  37527. + kfree(ips);
  37528. + ips = NULL;
  37529. +
  37530. + return 0;
  37531. +}
  37532. +
  37533. +extern int sysctl_ipsec_debug_verbose;
  37534. +
  37535. +int ipsec_sa_init(struct ipsec_sa *ipsp)
  37536. +{
  37537. + int i;
  37538. + int error = 0;
  37539. + char sa[SATOT_BUF];
  37540. + size_t sa_len;
  37541. + char ipaddr_txt[ADDRTOA_BUF];
  37542. + char ipaddr2_txt[ADDRTOA_BUF];
  37543. +#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)
  37544. + unsigned char kb[AHMD596_BLKLEN];
  37545. +#endif
  37546. + struct ipsec_alg_enc *ixt_e = NULL;
  37547. + struct ipsec_alg_auth *ixt_a = NULL;
  37548. +
  37549. + if(ipsp == NULL) {
  37550. + KLIPS_PRINT(debug_pfkey,
  37551. + "ipsec_sa_init: "
  37552. + "ipsp is NULL, fatal\n");
  37553. + SENDERR(EINVAL);
  37554. + }
  37555. +
  37556. + sa_len = satot(&ipsp->ips_said, 0, sa, sizeof(sa));
  37557. +
  37558. + KLIPS_PRINT(debug_pfkey,
  37559. + "ipsec_sa_init: "
  37560. + "(pfkey defined) called for SA:%s\n",
  37561. + sa_len ? sa : " (error)");
  37562. +
  37563. + KLIPS_PRINT(debug_pfkey,
  37564. + "ipsec_sa_init: "
  37565. + "calling init routine of %s%s%s\n",
  37566. + IPS_XFORM_NAME(ipsp));
  37567. +
  37568. + switch(ipsp->ips_said.proto) {
  37569. +
  37570. +#ifdef CONFIG_KLIPS_IPIP
  37571. + case IPPROTO_IPIP: {
  37572. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_s))->sin_addr,
  37573. + 0,
  37574. + ipaddr_txt, sizeof(ipaddr_txt));
  37575. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,
  37576. + 0,
  37577. + ipaddr2_txt, sizeof(ipaddr_txt));
  37578. + KLIPS_PRINT(debug_pfkey,
  37579. + "ipsec_sa_init: "
  37580. + "(pfkey defined) IPIP ipsec_sa set for %s->%s.\n",
  37581. + ipaddr_txt,
  37582. + ipaddr2_txt);
  37583. + }
  37584. + break;
  37585. +#endif /* !CONFIG_KLIPS_IPIP */
  37586. +
  37587. +#ifdef CONFIG_KLIPS_AH
  37588. + case IPPROTO_AH:
  37589. + switch(ipsp->ips_authalg) {
  37590. +# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  37591. + case AH_MD5: {
  37592. + unsigned char *akp;
  37593. + unsigned int aks;
  37594. + MD5_CTX *ictx;
  37595. + MD5_CTX *octx;
  37596. +
  37597. + if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {
  37598. + KLIPS_PRINT(debug_pfkey,
  37599. + "ipsec_sa_init: "
  37600. + "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37601. + ipsp->ips_key_bits_a, AHMD596_KLEN * 8);
  37602. + SENDERR(EINVAL);
  37603. + }
  37604. +
  37605. +# if KLIPS_DIVULGE_HMAC_KEY
  37606. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37607. + "ipsec_sa_init: "
  37608. + "hmac md5-96 key is 0x%08x %08x %08x %08x\n",
  37609. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37610. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37611. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37612. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37613. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37614. +
  37615. + ipsp->ips_auth_bits = AHMD596_ALEN * 8;
  37616. +
  37617. + /* save the pointer to the key material */
  37618. + akp = ipsp->ips_key_a;
  37619. + aks = ipsp->ips_key_a_size;
  37620. +
  37621. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37622. + "ipsec_sa_init: "
  37623. + "allocating %lu bytes for md5_ctx.\n",
  37624. + (unsigned long) sizeof(struct md5_ctx));
  37625. + if((ipsp->ips_key_a = (caddr_t)
  37626. + kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {
  37627. + ipsp->ips_key_a = akp;
  37628. + SENDERR(ENOMEM);
  37629. + }
  37630. + ipsp->ips_key_a_size = sizeof(struct md5_ctx);
  37631. +
  37632. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37633. + kb[i] = akp[i] ^ HMAC_IPAD;
  37634. + }
  37635. + for (; i < AHMD596_BLKLEN; i++) {
  37636. + kb[i] = HMAC_IPAD;
  37637. + }
  37638. +
  37639. + ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);
  37640. + osMD5Init(ictx);
  37641. + osMD5Update(ictx, kb, AHMD596_BLKLEN);
  37642. +
  37643. + for (i = 0; i < AHMD596_BLKLEN; i++) {
  37644. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37645. + }
  37646. +
  37647. + octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);
  37648. + osMD5Init(octx);
  37649. + osMD5Update(octx, kb, AHMD596_BLKLEN);
  37650. +
  37651. +# if KLIPS_DIVULGE_HMAC_KEY
  37652. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37653. + "ipsec_sa_init: "
  37654. + "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37655. + ((__u32*)ictx)[0],
  37656. + ((__u32*)ictx)[1],
  37657. + ((__u32*)ictx)[2],
  37658. + ((__u32*)ictx)[3],
  37659. + ((__u32*)octx)[0],
  37660. + ((__u32*)octx)[1],
  37661. + ((__u32*)octx)[2],
  37662. + ((__u32*)octx)[3] );
  37663. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37664. +
  37665. + /* zero key buffer -- paranoid */
  37666. + memset(akp, 0, aks);
  37667. + kfree(akp);
  37668. + }
  37669. + break;
  37670. +# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  37671. +# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  37672. + case AH_SHA: {
  37673. + unsigned char *akp;
  37674. + unsigned int aks;
  37675. + SHA1_CTX *ictx;
  37676. + SHA1_CTX *octx;
  37677. +
  37678. + if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {
  37679. + KLIPS_PRINT(debug_pfkey,
  37680. + "ipsec_sa_init: "
  37681. + "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37682. + ipsp->ips_key_bits_a, AHSHA196_KLEN * 8);
  37683. + SENDERR(EINVAL);
  37684. + }
  37685. +
  37686. +# if KLIPS_DIVULGE_HMAC_KEY
  37687. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37688. + "ipsec_sa_init: "
  37689. + "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",
  37690. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37691. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37692. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37693. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37694. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37695. +
  37696. + ipsp->ips_auth_bits = AHSHA196_ALEN * 8;
  37697. +
  37698. + /* save the pointer to the key material */
  37699. + akp = ipsp->ips_key_a;
  37700. + aks = ipsp->ips_key_a_size;
  37701. +
  37702. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37703. + "ipsec_sa_init: "
  37704. + "allocating %lu bytes for sha1_ctx.\n",
  37705. + (unsigned long) sizeof(struct sha1_ctx));
  37706. + if((ipsp->ips_key_a = (caddr_t)
  37707. + kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {
  37708. + ipsp->ips_key_a = akp;
  37709. + SENDERR(ENOMEM);
  37710. + }
  37711. + ipsp->ips_key_a_size = sizeof(struct sha1_ctx);
  37712. +
  37713. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37714. + kb[i] = akp[i] ^ HMAC_IPAD;
  37715. + }
  37716. + for (; i < AHMD596_BLKLEN; i++) {
  37717. + kb[i] = HMAC_IPAD;
  37718. + }
  37719. +
  37720. + ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);
  37721. + SHA1Init(ictx);
  37722. + SHA1Update(ictx, kb, AHSHA196_BLKLEN);
  37723. +
  37724. + for (i = 0; i < AHSHA196_BLKLEN; i++) {
  37725. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37726. + }
  37727. +
  37728. + octx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->octx);
  37729. + SHA1Init(octx);
  37730. + SHA1Update(octx, kb, AHSHA196_BLKLEN);
  37731. +
  37732. +# if KLIPS_DIVULGE_HMAC_KEY
  37733. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37734. + "ipsec_sa_init: "
  37735. + "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37736. + ((__u32*)ictx)[0],
  37737. + ((__u32*)ictx)[1],
  37738. + ((__u32*)ictx)[2],
  37739. + ((__u32*)ictx)[3],
  37740. + ((__u32*)octx)[0],
  37741. + ((__u32*)octx)[1],
  37742. + ((__u32*)octx)[2],
  37743. + ((__u32*)octx)[3] );
  37744. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37745. + /* zero key buffer -- paranoid */
  37746. + memset(akp, 0, aks);
  37747. + kfree(akp);
  37748. + }
  37749. + break;
  37750. +# endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  37751. + default:
  37752. + KLIPS_PRINT(debug_pfkey,
  37753. + "ipsec_sa_init: "
  37754. + "authalg=%d support not available in the kernel",
  37755. + ipsp->ips_authalg);
  37756. + SENDERR(EINVAL);
  37757. + }
  37758. + break;
  37759. +#endif /* CONFIG_KLIPS_AH */
  37760. +
  37761. +#ifdef CONFIG_KLIPS_ESP
  37762. + case IPPROTO_ESP:
  37763. + {
  37764. +#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)
  37765. + unsigned char *akp;
  37766. + unsigned int aks;
  37767. +#endif
  37768. +
  37769. + ipsec_alg_sa_init(ipsp);
  37770. + ixt_e=ipsp->ips_alg_enc;
  37771. +
  37772. + if (ixt_e == NULL) {
  37773. + if(printk_ratelimit()) {
  37774. + printk(KERN_INFO
  37775. + "ipsec_sa_init: "
  37776. + "encalg=%d support not available in the kernel",
  37777. + ipsp->ips_encalg);
  37778. + }
  37779. + SENDERR(ENOENT);
  37780. + }
  37781. +
  37782. + ipsp->ips_iv_size = ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  37783. +
  37784. + /* Create IV */
  37785. + if (ipsp->ips_iv_size) {
  37786. + if((ipsp->ips_iv = (caddr_t)
  37787. + kmalloc(ipsp->ips_iv_size, GFP_ATOMIC)) == NULL) {
  37788. + SENDERR(ENOMEM);
  37789. + }
  37790. + prng_bytes(&ipsec_prng,
  37791. + (char *)ipsp->ips_iv,
  37792. + ipsp->ips_iv_size);
  37793. + ipsp->ips_iv_bits = ipsp->ips_iv_size * 8;
  37794. + }
  37795. +
  37796. + if ((error=ipsec_alg_enc_key_create(ipsp)) < 0)
  37797. + SENDERR(-error);
  37798. +
  37799. + if ((ixt_a=ipsp->ips_alg_auth)) {
  37800. + if ((error=ipsec_alg_auth_key_create(ipsp)) < 0)
  37801. + SENDERR(-error);
  37802. + } else
  37803. +
  37804. + switch(ipsp->ips_authalg) {
  37805. +# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  37806. + case AH_MD5: {
  37807. + MD5_CTX *ictx;
  37808. + MD5_CTX *octx;
  37809. +
  37810. + if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {
  37811. + KLIPS_PRINT(debug_pfkey,
  37812. + "ipsec_sa_init: "
  37813. + "incorrect authorisation key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37814. + ipsp->ips_key_bits_a,
  37815. + AHMD596_KLEN * 8);
  37816. + SENDERR(EINVAL);
  37817. + }
  37818. +
  37819. +# if KLIPS_DIVULGE_HMAC_KEY
  37820. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37821. + "ipsec_sa_init: "
  37822. + "hmac md5-96 key is 0x%08x %08x %08x %08x\n",
  37823. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+0)),
  37824. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+1)),
  37825. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+2)),
  37826. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+3)));
  37827. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37828. + ipsp->ips_auth_bits = AHMD596_ALEN * 8;
  37829. +
  37830. + /* save the pointer to the key material */
  37831. + akp = ipsp->ips_key_a;
  37832. + aks = ipsp->ips_key_a_size;
  37833. +
  37834. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37835. + "ipsec_sa_init: "
  37836. + "allocating %lu bytes for md5_ctx.\n",
  37837. + (unsigned long) sizeof(struct md5_ctx));
  37838. + if((ipsp->ips_key_a = (caddr_t)
  37839. + kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {
  37840. + ipsp->ips_key_a = akp;
  37841. + SENDERR(ENOMEM);
  37842. + }
  37843. + ipsp->ips_key_a_size = sizeof(struct md5_ctx);
  37844. +
  37845. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37846. + kb[i] = akp[i] ^ HMAC_IPAD;
  37847. + }
  37848. + for (; i < AHMD596_BLKLEN; i++) {
  37849. + kb[i] = HMAC_IPAD;
  37850. + }
  37851. +
  37852. + ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);
  37853. + osMD5Init(ictx);
  37854. + osMD5Update(ictx, kb, AHMD596_BLKLEN);
  37855. +
  37856. + for (i = 0; i < AHMD596_BLKLEN; i++) {
  37857. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37858. + }
  37859. +
  37860. + octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);
  37861. + osMD5Init(octx);
  37862. + osMD5Update(octx, kb, AHMD596_BLKLEN);
  37863. +
  37864. +# if KLIPS_DIVULGE_HMAC_KEY
  37865. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37866. + "ipsec_sa_init: "
  37867. + "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37868. + ((__u32*)ictx)[0],
  37869. + ((__u32*)ictx)[1],
  37870. + ((__u32*)ictx)[2],
  37871. + ((__u32*)ictx)[3],
  37872. + ((__u32*)octx)[0],
  37873. + ((__u32*)octx)[1],
  37874. + ((__u32*)octx)[2],
  37875. + ((__u32*)octx)[3] );
  37876. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37877. + /* paranoid */
  37878. + memset(akp, 0, aks);
  37879. + kfree(akp);
  37880. + break;
  37881. + }
  37882. +# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  37883. +# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  37884. + case AH_SHA: {
  37885. + SHA1_CTX *ictx;
  37886. + SHA1_CTX *octx;
  37887. +
  37888. + if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {
  37889. + KLIPS_PRINT(debug_pfkey,
  37890. + "ipsec_sa_init: "
  37891. + "incorrect authorisation key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37892. + ipsp->ips_key_bits_a,
  37893. + AHSHA196_KLEN * 8);
  37894. + SENDERR(EINVAL);
  37895. + }
  37896. +
  37897. +# if KLIPS_DIVULGE_HMAC_KEY
  37898. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37899. + "ipsec_sa_init: "
  37900. + "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",
  37901. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37902. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37903. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37904. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37905. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37906. + ipsp->ips_auth_bits = AHSHA196_ALEN * 8;
  37907. +
  37908. + /* save the pointer to the key material */
  37909. + akp = ipsp->ips_key_a;
  37910. + aks = ipsp->ips_key_a_size;
  37911. +
  37912. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37913. + "ipsec_sa_init: "
  37914. + "allocating %lu bytes for sha1_ctx.\n",
  37915. + (unsigned long) sizeof(struct sha1_ctx));
  37916. + if((ipsp->ips_key_a = (caddr_t)
  37917. + kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {
  37918. + ipsp->ips_key_a = akp;
  37919. + SENDERR(ENOMEM);
  37920. + }
  37921. + ipsp->ips_key_a_size = sizeof(struct sha1_ctx);
  37922. +
  37923. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37924. + kb[i] = akp[i] ^ HMAC_IPAD;
  37925. + }
  37926. + for (; i < AHMD596_BLKLEN; i++) {
  37927. + kb[i] = HMAC_IPAD;
  37928. + }
  37929. +
  37930. + ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);
  37931. + SHA1Init(ictx);
  37932. + SHA1Update(ictx, kb, AHSHA196_BLKLEN);
  37933. +
  37934. + for (i = 0; i < AHSHA196_BLKLEN; i++) {
  37935. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37936. + }
  37937. +
  37938. + octx = &((struct sha1_ctx*)(ipsp->ips_key_a))->octx;
  37939. + SHA1Init(octx);
  37940. + SHA1Update(octx, kb, AHSHA196_BLKLEN);
  37941. +
  37942. +# if KLIPS_DIVULGE_HMAC_KEY
  37943. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37944. + "ipsec_sa_init: "
  37945. + "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37946. + ((__u32*)ictx)[0],
  37947. + ((__u32*)ictx)[1],
  37948. + ((__u32*)ictx)[2],
  37949. + ((__u32*)ictx)[3],
  37950. + ((__u32*)octx)[0],
  37951. + ((__u32*)octx)[1],
  37952. + ((__u32*)octx)[2],
  37953. + ((__u32*)octx)[3] );
  37954. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37955. + memset(akp, 0, aks);
  37956. + kfree(akp);
  37957. + break;
  37958. + }
  37959. +# endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  37960. + case AH_NONE:
  37961. + break;
  37962. + default:
  37963. + KLIPS_PRINT(debug_pfkey,
  37964. + "ipsec_sa_init: "
  37965. + "authalg=%d support not available in the kernel.\n",
  37966. + ipsp->ips_authalg);
  37967. + SENDERR(EINVAL);
  37968. + }
  37969. + }
  37970. + break;
  37971. +#endif /* !CONFIG_KLIPS_ESP */
  37972. +#ifdef CONFIG_KLIPS_IPCOMP
  37973. + case IPPROTO_COMP:
  37974. + ipsp->ips_comp_adapt_tries = 0;
  37975. + ipsp->ips_comp_adapt_skip = 0;
  37976. + ipsp->ips_comp_ratio_cbytes = 0;
  37977. + ipsp->ips_comp_ratio_dbytes = 0;
  37978. + break;
  37979. +#endif /* CONFIG_KLIPS_IPCOMP */
  37980. + default:
  37981. + printk(KERN_ERR "KLIPS sa initialization: "
  37982. + "proto=%d unknown.\n",
  37983. + ipsp->ips_said.proto);
  37984. + SENDERR(EINVAL);
  37985. + }
  37986. +
  37987. + errlab:
  37988. + return(error);
  37989. +}
  37990. +
  37991. +
  37992. +
  37993. +/*
  37994. + * $Log: ipsec_sa.c,v $
  37995. + * Revision 1.30.2.2 2006/10/06 21:39:26 paul
  37996. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  37997. + * set. This is defined through autoconf.h which is included through the
  37998. + * linux kernel build macros.
  37999. + *
  38000. + * Revision 1.30.2.1 2006/04/20 16:33:07 mcr
  38001. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  38002. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  38003. + *
  38004. + * Revision 1.30 2005/05/24 01:02:35 mcr
  38005. + * some refactoring/simplification of situation where alg
  38006. + * is not found.
  38007. + *
  38008. + * Revision 1.29 2005/05/18 19:13:28 mcr
  38009. + * rename debug messages. make sure that algo not found is not
  38010. + * a debug message.
  38011. + *
  38012. + * Revision 1.28 2005/05/11 01:30:20 mcr
  38013. + * removed "poor-man"s OOP in favour of proper C structures.
  38014. + *
  38015. + * Revision 1.27 2005/04/29 05:10:22 mcr
  38016. + * removed from extraenous includes to make unit testing easier.
  38017. + *
  38018. + * Revision 1.26 2005/04/14 20:56:24 mcr
  38019. + * moved (pfkey_)ipsec_sa_init to ipsec_sa.c.
  38020. + *
  38021. + * Revision 1.25 2004/08/22 20:12:16 mcr
  38022. + * one more KLIPS_NAT->IPSEC_NAT.
  38023. + *
  38024. + * Revision 1.24 2004/07/10 19:11:18 mcr
  38025. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  38026. + *
  38027. + * Revision 1.23 2004/04/06 02:49:26 mcr
  38028. + * pullup of algo code from alg-branch.
  38029. + *
  38030. + * Revision 1.22.2.1 2003/12/22 15:25:52 jjo
  38031. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  38032. + *
  38033. + * Revision 1.22 2003/12/10 01:14:27 mcr
  38034. + * NAT-traversal patches to KLIPS.
  38035. + *
  38036. + * Revision 1.21 2003/10/31 02:27:55 mcr
  38037. + * pulled up port-selector patches and sa_id elimination.
  38038. + *
  38039. + * Revision 1.20.4.1 2003/10/29 01:30:41 mcr
  38040. + * elimited "struct sa_id".
  38041. + *
  38042. + * Revision 1.20 2003/02/06 01:50:34 rgb
  38043. + * Fixed initialisation bug for first sadb hash bucket that would only manifest itself on platforms where NULL != 0.
  38044. + *
  38045. + * Revision 1.19 2003/01/30 02:32:22 rgb
  38046. + *
  38047. + * Rename SAref table macro names for clarity.
  38048. + * Transmit error code through to caller from callee for better diagnosis of problems.
  38049. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  38050. + *
  38051. + * Revision 1.18 2002/10/12 23:11:53 dhr
  38052. + *
  38053. + * [KenB + DHR] more 64-bit cleanup
  38054. + *
  38055. + * Revision 1.17 2002/10/07 18:31:43 rgb
  38056. + * Move field width sanity checks to ipsec_sa.c
  38057. + *
  38058. + * Revision 1.16 2002/09/20 15:41:02 rgb
  38059. + * Re-wrote most of the SAref code to eliminate Entry pointers.
  38060. + * Added SAref code compiler directive switch.
  38061. + * Added a saref test function for testing macros.
  38062. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  38063. + * Split ipsec_sadb_cleanup from new funciton ipsec_sadb_free to avoid problem
  38064. + * of freeing newly created structures when clearing the reftable upon startup
  38065. + * to start from a known state.
  38066. + * Place all ipsec sadb globals into one struct.
  38067. + * Rework saref freelist.
  38068. + * Added memory allocation debugging.
  38069. + *
  38070. + * Revision 1.15 2002/09/20 05:01:44 rgb
  38071. + * Update copyright date.
  38072. + *
  38073. + * Revision 1.14 2002/08/13 19:01:25 mcr
  38074. + * patches from kenb to permit compilation of FreeSWAN on ia64.
  38075. + * des library patched to use proper DES_LONG type for ia64.
  38076. + *
  38077. + * Revision 1.13 2002/07/29 03:06:20 mcr
  38078. + * get rid of variable not used warnings.
  38079. + *
  38080. + * Revision 1.12 2002/07/26 08:48:31 rgb
  38081. + * Added SA ref table code.
  38082. + *
  38083. + * Revision 1.11 2002/06/04 16:48:49 rgb
  38084. + * Tidied up pointer code for processor independance.
  38085. + *
  38086. + * Revision 1.10 2002/05/23 07:16:17 rgb
  38087. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  38088. + * Pointer clean-up.
  38089. + * Added refcount code.
  38090. + * Convert "usecount" to "refcount" to remove ambiguity.
  38091. + *
  38092. + * Revision 1.9 2002/05/14 02:34:49 rgb
  38093. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  38094. + * with "put" usage in the kernel.
  38095. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  38096. + * ipsec_sa or ipsec_sa.
  38097. + * Added some preliminary refcount code.
  38098. + *
  38099. + * Revision 1.8 2002/04/24 07:55:32 mcr
  38100. + * #include patches and Makefiles for post-reorg compilation.
  38101. + *
  38102. + * Revision 1.7 2002/04/24 07:36:30 mcr
  38103. + * Moved from ./klips/net/ipsec/ipsec_sa.c,v
  38104. + *
  38105. + * Revision 1.6 2002/04/20 00:12:25 rgb
  38106. + * Added esp IV CBC attack fix, disabled.
  38107. + *
  38108. + * Revision 1.5 2002/01/29 17:17:56 mcr
  38109. + * moved include of ipsec_param.h to after include of linux/kernel.h
  38110. + * otherwise, it seems that some option that is set in ipsec_param.h
  38111. + * screws up something subtle in the include path to kernel.h, and
  38112. + * it complains on the snprintf() prototype.
  38113. + *
  38114. + * Revision 1.4 2002/01/29 04:00:52 mcr
  38115. + * more excise of kversions.h header.
  38116. + *
  38117. + * Revision 1.3 2002/01/29 02:13:18 mcr
  38118. + * introduction of ipsec_kversion.h means that include of
  38119. + * ipsec_param.h must preceed any decisions about what files to
  38120. + * include to deal with differences in kernel source.
  38121. + *
  38122. + * Revision 1.2 2001/11/26 09:16:15 rgb
  38123. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  38124. + *
  38125. + * Revision 1.1.2.2 2001/10/22 21:05:41 mcr
  38126. + * removed phony prototype for des_set_key.
  38127. + *
  38128. + * Revision 1.1.2.1 2001/09/25 02:24:57 mcr
  38129. + * struct tdb -> struct ipsec_sa.
  38130. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  38131. + * ipsec_xform.c removed. header file still contains useful things.
  38132. + *
  38133. + *
  38134. + *
  38135. + * CLONED from ipsec_xform.c:
  38136. + * Revision 1.53 2001/09/08 21:13:34 rgb
  38137. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  38138. + *
  38139. + * Revision 1.52 2001/06/14 19:35:11 rgb
  38140. + * Update copyright date.
  38141. + *
  38142. + * Revision 1.51 2001/05/30 08:14:03 rgb
  38143. + * Removed vestiges of esp-null transforms.
  38144. + *
  38145. + * Revision 1.50 2001/05/03 19:43:18 rgb
  38146. + * Initialise error return variable.
  38147. + * Update SENDERR macro.
  38148. + * Fix sign of error return code for ipsec_tdbcleanup().
  38149. + * Use more appropriate return code for ipsec_tdbwipe().
  38150. + *
  38151. + * Revision 1.49 2001/04/19 18:56:17 rgb
  38152. + * Fixed tdb table locking comments.
  38153. + *
  38154. + * Revision 1.48 2001/02/27 22:24:55 rgb
  38155. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  38156. + * Check for satoa() return codes.
  38157. + *
  38158. + * Revision 1.47 2000/11/06 04:32:08 rgb
  38159. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  38160. + *
  38161. + * Revision 1.46 2000/09/20 16:21:57 rgb
  38162. + * Cleaned up ident string alloc/free.
  38163. + *
  38164. + * Revision 1.45 2000/09/08 19:16:51 rgb
  38165. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  38166. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  38167. + *
  38168. + * Revision 1.44 2000/08/30 05:29:04 rgb
  38169. + * Compiler-define out no longer used tdb_init() in ipsec_xform.c.
  38170. + *
  38171. + * Revision 1.43 2000/08/18 21:30:41 rgb
  38172. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  38173. + *
  38174. + * Revision 1.42 2000/08/01 14:51:51 rgb
  38175. + * Removed _all_ remaining traces of DES.
  38176. + *
  38177. + * Revision 1.41 2000/07/28 14:58:31 rgb
  38178. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  38179. + *
  38180. + * Revision 1.40 2000/06/28 05:50:11 rgb
  38181. + * Actually set iv_bits.
  38182. + *
  38183. + * Revision 1.39 2000/05/10 23:11:09 rgb
  38184. + * Added netlink debugging output.
  38185. + * Added a cast to quiet down the ntohl bug.
  38186. + *
  38187. + * Revision 1.38 2000/05/10 19:18:42 rgb
  38188. + * Cast output of ntohl so that the broken prototype doesn't make our
  38189. + * compile noisy.
  38190. + *
  38191. + * Revision 1.37 2000/03/16 14:04:59 rgb
  38192. + * Hardwired CONFIG_IPSEC_PFKEYv2 on.
  38193. + *
  38194. + * Revision 1.36 2000/01/26 10:11:28 rgb
  38195. + * Fixed spacing in error text causing run-in words.
  38196. + *
  38197. + * Revision 1.35 2000/01/21 06:17:16 rgb
  38198. + * Tidied up compiler directive indentation for readability.
  38199. + * Added ictx,octx vars for simplification.(kravietz)
  38200. + * Added macros for HMAC padding magic numbers.(kravietz)
  38201. + * Fixed missing key length reporting bug.
  38202. + * Fixed bug in tdbwipe to return immediately on NULL tdbp passed in.
  38203. + *
  38204. + * Revision 1.34 1999/12/08 00:04:19 rgb
  38205. + * Fixed SA direction overwriting bug for netlink users.
  38206. + *
  38207. + * Revision 1.33 1999/12/01 22:16:44 rgb
  38208. + * Minor formatting changes in ESP MD5 initialisation.
  38209. + *
  38210. + * Revision 1.32 1999/11/25 09:06:36 rgb
  38211. + * Fixed error return messages, should be returning negative numbers.
  38212. + * Implemented SENDERR macro for propagating error codes.
  38213. + * Added debug message and separate error code for algorithms not compiled
  38214. + * in.
  38215. + *
  38216. + * Revision 1.31 1999/11/23 23:06:26 rgb
  38217. + * Sort out pfkey and freeswan headers, putting them in a library path.
  38218. + *
  38219. + * Revision 1.30 1999/11/18 04:09:20 rgb
  38220. + * Replaced all kernel version macros to shorter, readable form.
  38221. + *
  38222. + * Revision 1.29 1999/11/17 15:53:40 rgb
  38223. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  38224. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  38225. + * klips/net/ipsec/Makefile.
  38226. + *
  38227. + * Revision 1.28 1999/10/18 20:04:01 rgb
  38228. + * Clean-out unused cruft.
  38229. + *
  38230. + * Revision 1.27 1999/10/03 19:01:03 rgb
  38231. + * Spinlock support for 2.3.xx and 2.0.xx kernels.
  38232. + *
  38233. + * Revision 1.26 1999/10/01 16:22:24 rgb
  38234. + * Switch from assignment init. to functional init. of spinlocks.
  38235. + *
  38236. + * Revision 1.25 1999/10/01 15:44:54 rgb
  38237. + * Move spinlock header include to 2.1> scope.
  38238. + *
  38239. + * Revision 1.24 1999/10/01 00:03:46 rgb
  38240. + * Added tdb structure locking.
  38241. + * Minor formatting changes.
  38242. + * Add function to initialize tdb hash table.
  38243. + *
  38244. + * Revision 1.23 1999/05/25 22:42:12 rgb
  38245. + * Add deltdbchain() debugging.
  38246. + *
  38247. + * Revision 1.22 1999/05/25 21:24:31 rgb
  38248. + * Add debugging statements to deltdbchain().
  38249. + *
  38250. + * Revision 1.21 1999/05/25 03:51:48 rgb
  38251. + * Refix error return code.
  38252. + *
  38253. + * Revision 1.20 1999/05/25 03:34:07 rgb
  38254. + * Fix error return for flush.
  38255. + *
  38256. + * Revision 1.19 1999/05/09 03:25:37 rgb
  38257. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  38258. + *
  38259. + * Revision 1.18 1999/05/05 22:02:32 rgb
  38260. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  38261. + *
  38262. + * Revision 1.17 1999/04/29 15:20:16 rgb
  38263. + * Change gettdb parameter to a pointer to reduce stack loading and
  38264. + * facilitate parameter sanity checking.
  38265. + * Add sanity checking for null pointer arguments.
  38266. + * Add debugging instrumentation.
  38267. + * Add function deltdbchain() which will take care of unlinking,
  38268. + * zeroing and deleting a chain of tdbs.
  38269. + * Add a parameter to tdbcleanup to be able to delete a class of SAs.
  38270. + * tdbwipe now actually zeroes the tdb as well as any of its pointed
  38271. + * structures.
  38272. + *
  38273. + * Revision 1.16 1999/04/16 15:36:29 rgb
  38274. + * Fix cut-and-paste error causing a memory leak in IPIP TDB freeing.
  38275. + *
  38276. + * Revision 1.15 1999/04/11 00:29:01 henry
  38277. + * GPL boilerplate
  38278. + *
  38279. + * Revision 1.14 1999/04/06 04:54:28 rgb
  38280. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  38281. + * patch shell fixes.
  38282. + *
  38283. + * Revision 1.13 1999/02/19 18:23:01 rgb
  38284. + * Nix debug off compile warning.
  38285. + *
  38286. + * Revision 1.12 1999/02/17 16:52:16 rgb
  38287. + * Consolidate satoa()s for space and speed efficiency.
  38288. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  38289. + * Clean out unused cruft.
  38290. + * Ditch NET_IPIP dependancy.
  38291. + * Loop for 3des key setting.
  38292. + *
  38293. + * Revision 1.11 1999/01/26 02:09:05 rgb
  38294. + * Remove ah/esp/IPIP switching on include files.
  38295. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  38296. + * Removed dead code.
  38297. + * Clean up debug code when switched off.
  38298. + * Remove references to INET_GET_PROTOCOL.
  38299. + * Added code exclusion macros to reduce code from unused algorithms.
  38300. + *
  38301. + * Revision 1.10 1999/01/22 06:28:55 rgb
  38302. + * Cruft clean-out.
  38303. + * Put random IV generation in kernel.
  38304. + * Added algorithm switch code.
  38305. + * Enhanced debugging.
  38306. + * 64-bit clean-up.
  38307. + *
  38308. + * Revision 1.9 1998/11/30 13:22:55 rgb
  38309. + * Rationalised all the klips kernel file headers. They are much shorter
  38310. + * now and won't conflict under RH5.2.
  38311. + *
  38312. + * Revision 1.8 1998/11/25 04:59:06 rgb
  38313. + * Add conditionals for no IPIP tunnel code.
  38314. + * Delete commented out code.
  38315. + *
  38316. + * Revision 1.7 1998/10/31 06:50:41 rgb
  38317. + * Convert xform ASCII names to no spaces.
  38318. + * Fixed up comments in #endif directives.
  38319. + *
  38320. + * Revision 1.6 1998/10/19 14:44:28 rgb
  38321. + * Added inclusion of freeswan.h.
  38322. + * sa_id structure implemented and used: now includes protocol.
  38323. + *
  38324. + * Revision 1.5 1998/10/09 04:32:19 rgb
  38325. + * Added 'klips_debug' prefix to all klips printk debug statements.
  38326. + *
  38327. + * Revision 1.4 1998/08/12 00:11:31 rgb
  38328. + * Added new xform functions to the xform table.
  38329. + * Fixed minor debug output spelling error.
  38330. + *
  38331. + * Revision 1.3 1998/07/09 17:45:31 rgb
  38332. + * Clarify algorithm not available message.
  38333. + *
  38334. + * Revision 1.2 1998/06/23 03:00:51 rgb
  38335. + * Check for presence of IPIP protocol if it is setup one way (we don't
  38336. + * know what has been set up the other way and can only assume it will be
  38337. + * symmetrical with the exception of keys).
  38338. + *
  38339. + * Revision 1.1 1998/06/18 21:27:51 henry
  38340. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  38341. + * kernel-build scripts happier in the presence of symlinks
  38342. + *
  38343. + * Revision 1.3 1998/06/11 05:54:59 rgb
  38344. + * Added transform version string pointer to xformsw initialisations.
  38345. + *
  38346. + * Revision 1.2 1998/04/21 21:28:57 rgb
  38347. + * Rearrange debug switches to change on the fly debug output from user
  38348. + * space. Only kernel changes checked in at this time. radij.c was also
  38349. + * changed to temporarily remove buggy debugging code in rj_delete causing
  38350. + * an OOPS and hence, netlink device open errors.
  38351. + *
  38352. + * Revision 1.1 1998/04/09 03:06:13 henry
  38353. + * sources moved up from linux/net/ipsec
  38354. + *
  38355. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  38356. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  38357. + *
  38358. + * Revision 0.5 1997/06/03 04:24:48 ji
  38359. + * Added ESP-3DES-MD5-96
  38360. + *
  38361. + * Revision 0.4 1997/01/15 01:28:15 ji
  38362. + * Added new transforms.
  38363. + *
  38364. + * Revision 0.3 1996/11/20 14:39:04 ji
  38365. + * Minor cleanups.
  38366. + * Rationalized debugging code.
  38367. + *
  38368. + * Revision 0.2 1996/11/02 00:18:33 ji
  38369. + * First limited release.
  38370. + *
  38371. + *
  38372. + */
  38373. --- /dev/null Tue Mar 11 13:02:56 2003
  38374. +++ linux/net/ipsec/ipsec_sha1.c Mon Feb 9 13:51:03 2004
  38375. @@ -0,0 +1,219 @@
  38376. +/*
  38377. + * RCSID $Id: ipsec_sha1.c,v 1.9 2004/04/06 02:49:26 mcr Exp $
  38378. + */
  38379. +
  38380. +/*
  38381. + * The rest of the code is derived from sha1.c by Steve Reid, which is
  38382. + * public domain.
  38383. + * Minor cosmetic changes to accomodate it in the Linux kernel by ji.
  38384. + */
  38385. +
  38386. +#include <asm/byteorder.h>
  38387. +#include <linux/string.h>
  38388. +
  38389. +#include "openswan/ipsec_sha1.h"
  38390. +
  38391. +#if defined(rol)
  38392. +#undef rol
  38393. +#endif
  38394. +
  38395. +#define SHA1HANDSOFF
  38396. +
  38397. +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  38398. +
  38399. +/* blk0() and blk() perform the initial expand. */
  38400. +/* I got the idea of expanding during the round function from SSLeay */
  38401. +#ifdef __LITTLE_ENDIAN
  38402. +#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  38403. + |(rol(block->l[i],8)&0x00FF00FF))
  38404. +#else
  38405. +#define blk0(i) block->l[i]
  38406. +#endif
  38407. +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  38408. + ^block->l[(i+2)&15]^block->l[i&15],1))
  38409. +
  38410. +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  38411. +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38412. +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38413. +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  38414. +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  38415. +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  38416. +
  38417. +
  38418. +/* Hash a single 512-bit block. This is the core of the algorithm. */
  38419. +
  38420. +void SHA1Transform(__u32 state[5], __u8 buffer[64])
  38421. +{
  38422. +__u32 a, b, c, d, e;
  38423. +typedef union {
  38424. + unsigned char c[64];
  38425. + __u32 l[16];
  38426. +} CHAR64LONG16;
  38427. +CHAR64LONG16* block;
  38428. +#ifdef SHA1HANDSOFF
  38429. +static unsigned char workspace[64];
  38430. + block = (CHAR64LONG16*)workspace;
  38431. + memcpy(block, buffer, 64);
  38432. +#else
  38433. + block = (CHAR64LONG16*)buffer;
  38434. +#endif
  38435. + /* Copy context->state[] to working vars */
  38436. + a = state[0];
  38437. + b = state[1];
  38438. + c = state[2];
  38439. + d = state[3];
  38440. + e = state[4];
  38441. + /* 4 rounds of 20 operations each. Loop unrolled. */
  38442. + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  38443. + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  38444. + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  38445. + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  38446. + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  38447. + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  38448. + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  38449. + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  38450. + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  38451. + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  38452. + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  38453. + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  38454. + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  38455. + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  38456. + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  38457. + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  38458. + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  38459. + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  38460. + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  38461. + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  38462. + /* Add the working vars back into context.state[] */
  38463. + state[0] += a;
  38464. + state[1] += b;
  38465. + state[2] += c;
  38466. + state[3] += d;
  38467. + state[4] += e;
  38468. + /* Wipe variables */
  38469. + a = b = c = d = e = 0;
  38470. +}
  38471. +
  38472. +
  38473. +/* SHA1Init - Initialize new context */
  38474. +
  38475. +void SHA1Init(void *vcontext)
  38476. +{
  38477. + SHA1_CTX* context = vcontext;
  38478. +
  38479. + /* SHA1 initialization constants */
  38480. + context->state[0] = 0x67452301;
  38481. + context->state[1] = 0xEFCDAB89;
  38482. + context->state[2] = 0x98BADCFE;
  38483. + context->state[3] = 0x10325476;
  38484. + context->state[4] = 0xC3D2E1F0;
  38485. + context->count[0] = context->count[1] = 0;
  38486. +}
  38487. +
  38488. +
  38489. +/* Run your data through this. */
  38490. +
  38491. +void SHA1Update(void *vcontext, unsigned char* data, __u32 len)
  38492. +{
  38493. + SHA1_CTX* context = vcontext;
  38494. + __u32 i, j;
  38495. +
  38496. + j = context->count[0];
  38497. + if ((context->count[0] += len << 3) < j)
  38498. + context->count[1]++;
  38499. + context->count[1] += (len>>29);
  38500. + j = (j >> 3) & 63;
  38501. + if ((j + len) > 63) {
  38502. + memcpy(&context->buffer[j], data, (i = 64-j));
  38503. + SHA1Transform(context->state, context->buffer);
  38504. + for ( ; i + 63 < len; i += 64) {
  38505. + SHA1Transform(context->state, &data[i]);
  38506. + }
  38507. + j = 0;
  38508. + }
  38509. + else i = 0;
  38510. + memcpy(&context->buffer[j], &data[i], len - i);
  38511. +}
  38512. +
  38513. +
  38514. +/* Add padding and return the message digest. */
  38515. +
  38516. +void SHA1Final(unsigned char digest[20], void *vcontext)
  38517. +{
  38518. + __u32 i, j;
  38519. + unsigned char finalcount[8];
  38520. + SHA1_CTX* context = vcontext;
  38521. +
  38522. + for (i = 0; i < 8; i++) {
  38523. + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  38524. + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  38525. + }
  38526. + SHA1Update(context, (unsigned char *)"\200", 1);
  38527. + while ((context->count[0] & 504) != 448) {
  38528. + SHA1Update(context, (unsigned char *)"\0", 1);
  38529. + }
  38530. + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
  38531. + for (i = 0; i < 20; i++) {
  38532. + digest[i] = (unsigned char)
  38533. + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
  38534. + }
  38535. + /* Wipe variables */
  38536. + i = j = 0;
  38537. + memset(context->buffer, 0, 64);
  38538. + memset(context->state, 0, 20);
  38539. + memset(context->count, 0, 8);
  38540. + memset(&finalcount, 0, 8);
  38541. +#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
  38542. + SHA1Transform(context->state, context->buffer);
  38543. +#endif
  38544. +}
  38545. +
  38546. +
  38547. +/*
  38548. + * $Log: ipsec_sha1.c,v $
  38549. + * Revision 1.9 2004/04/06 02:49:26 mcr
  38550. + * pullup of algo code from alg-branch.
  38551. + *
  38552. + * Revision 1.8 2002/09/10 01:45:14 mcr
  38553. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  38554. + * the function prototypes would match, and could be placed
  38555. + * into a pointer to a function.
  38556. + *
  38557. + * Revision 1.7 2002/04/24 07:55:32 mcr
  38558. + * #include patches and Makefiles for post-reorg compilation.
  38559. + *
  38560. + * Revision 1.6 2002/04/24 07:36:30 mcr
  38561. + * Moved from ./klips/net/ipsec/ipsec_sha1.c,v
  38562. + *
  38563. + * Revision 1.5 1999/12/13 13:59:13 rgb
  38564. + * Quick fix to argument size to Update bugs.
  38565. + *
  38566. + * Revision 1.4 1999/04/11 00:29:00 henry
  38567. + * GPL boilerplate
  38568. + *
  38569. + * Revision 1.3 1999/04/06 04:54:27 rgb
  38570. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  38571. + * patch shell fixes.
  38572. + *
  38573. + * Revision 1.2 1999/01/22 06:55:50 rgb
  38574. + * 64-bit clean-up.
  38575. + *
  38576. + * Revision 1.1 1998/06/18 21:27:50 henry
  38577. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  38578. + * kernel-build scripts happier in the presence of symlinks
  38579. + *
  38580. + * Revision 1.2 1998/04/23 20:54:04 rgb
  38581. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  38582. + * verified.
  38583. + *
  38584. + * Revision 1.1 1998/04/09 03:06:11 henry
  38585. + * sources moved up from linux/net/ipsec
  38586. + *
  38587. + * Revision 1.1.1.1 1998/04/08 05:35:05 henry
  38588. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  38589. + *
  38590. + * Revision 0.4 1997/01/15 01:28:15 ji
  38591. + * New transform
  38592. + *
  38593. + *
  38594. + */
  38595. --- /dev/null Tue Mar 11 13:02:56 2003
  38596. +++ linux/net/ipsec/ipsec_snprintf.c Mon Feb 9 13:51:03 2004
  38597. @@ -0,0 +1,135 @@
  38598. +/*
  38599. + * @(#) ipsec_snprintf() function
  38600. + *
  38601. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  38602. + * 2001 Michael Richardson <mcr@freeswan.org>
  38603. + * Copyright (C) 2005 Michael Richardson <mcr@xelerance.com>
  38604. + *
  38605. + * This program is free software; you can redistribute it and/or modify it
  38606. + * under the terms of the GNU General Public License as published by the
  38607. + * Free Software Foundation; either version 2 of the License, or (at your
  38608. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  38609. + *
  38610. + * This program is distributed in the hope that it will be useful, but
  38611. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  38612. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  38613. + * for more details.
  38614. + *
  38615. + * Split out from ipsec_proc.c.
  38616. + */
  38617. +
  38618. +#ifndef AUTOCONF_INCLUDED
  38619. +#include <linux/config.h>
  38620. +#endif
  38621. +#include <linux/version.h>
  38622. +#define __NO_VERSION__
  38623. +#include <linux/module.h>
  38624. +#include <linux/kernel.h> /* printk() */
  38625. +
  38626. +#include "openswan/ipsec_kversion.h"
  38627. +#include "openswan/ipsec_param.h"
  38628. +
  38629. +#include <net/ip.h>
  38630. +
  38631. +#include "openswan/radij.h"
  38632. +
  38633. +#include "openswan/ipsec_life.h"
  38634. +#include "openswan/ipsec_stats.h"
  38635. +#include "openswan/ipsec_sa.h"
  38636. +
  38637. +#include "openswan/ipsec_encap.h"
  38638. +#include "openswan/ipsec_radij.h"
  38639. +#include "openswan/ipsec_xform.h"
  38640. +#include "openswan/ipsec_tunnel.h"
  38641. +#include "openswan/ipsec_xmit.h"
  38642. +
  38643. +#include "openswan/ipsec_rcv.h"
  38644. +#include "openswan/ipsec_ah.h"
  38645. +#include "openswan/ipsec_esp.h"
  38646. +#include "openswan/ipsec_kern24.h"
  38647. +
  38648. +#ifdef CONFIG_KLIPS_IPCOMP
  38649. +#include "openswan/ipcomp.h"
  38650. +#endif /* CONFIG_KLIPS_IPCOMP */
  38651. +
  38652. +#include "openswan/ipsec_proto.h"
  38653. +
  38654. +#include <pfkeyv2.h>
  38655. +#include <pfkey.h>
  38656. +
  38657. +/* ipsec_snprintf: like snprintf except
  38658. + * - size is signed and a negative value is treated as if it were 0
  38659. + * - the returned result is never negative --
  38660. + * an error generates a "?" or null output (depending on space).
  38661. + * (Our callers are too lazy to check for an error return.)
  38662. + *
  38663. + * @param buf String buffer
  38664. + * @param size Size of the string
  38665. + * @param fmt printf string
  38666. + * @param ... Variables to be displayed in fmt
  38667. + * @return int Return code
  38668. + */
  38669. +int ipsec_snprintf(char *buf, ssize_t size, const char *fmt, ...)
  38670. +{
  38671. + va_list args;
  38672. + int i;
  38673. + size_t possize = size < 0? 0 : size;
  38674. + va_start(args, fmt);
  38675. + i = vsnprintf(buf,possize,fmt,args);
  38676. + va_end(args);
  38677. + if (i < 0) {
  38678. + /* create empty output in place of error */
  38679. + i = 0;
  38680. + if (size > 0) {
  38681. + *buf = '\0';
  38682. + }
  38683. + }
  38684. + return i;
  38685. +}
  38686. +
  38687. +
  38688. +void ipsec_dmp_block(char *s, caddr_t bb, int len)
  38689. +{
  38690. + int i;
  38691. + unsigned char *b = bb;
  38692. +
  38693. + printk(KERN_INFO "klips_dmp: "
  38694. + "at %s, len=%d:\n", s, len);
  38695. +
  38696. + for(i = 0; i < len; i++ /*, c++*/) {
  38697. + if(!(i % 16)) {
  38698. + printk(KERN_INFO
  38699. + "klips_debug: @%03x:",
  38700. + i);
  38701. + }
  38702. + printk(" %02x", b[i]);
  38703. + if(!((i + 1) % 16)) {
  38704. + printk("\n");
  38705. + }
  38706. + }
  38707. + if(i % 16) {
  38708. + printk("\n");
  38709. + }
  38710. +}
  38711. +
  38712. +/*
  38713. + *
  38714. + * $Log: ipsec_snprintf.c,v $
  38715. + * Revision 1.3.2.1 2006/10/06 21:39:26 paul
  38716. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  38717. + * set. This is defined through autoconf.h which is included through the
  38718. + * linux kernel build macros.
  38719. + *
  38720. + * Revision 1.3 2005/04/29 05:10:22 mcr
  38721. + * removed from extraenous includes to make unit testing easier.
  38722. + *
  38723. + * Revision 1.2 2005/04/15 00:32:01 mcr
  38724. + * added ipsec_dmp_block routine.
  38725. + *
  38726. + *
  38727. + * Local Variables:
  38728. + * c-file-style: "linux"
  38729. + * End:
  38730. + *
  38731. + */
  38732. +
  38733. --- /dev/null Tue Mar 11 13:02:56 2003
  38734. +++ linux/net/ipsec/ipsec_tunnel.c Mon Feb 9 13:51:03 2004
  38735. @@ -0,0 +1,2878 @@
  38736. +/*
  38737. + * IPSEC Tunneling code. Heavily based on drivers/net/new_tunnel.c
  38738. + * Copyright (C) 1996, 1997 John Ioannidis.
  38739. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  38740. + *
  38741. + * This program is free software; you can redistribute it and/or modify it
  38742. + * under the terms of the GNU General Public License as published by the
  38743. + * Free Software Foundation; either version 2 of the License, or (at your
  38744. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  38745. + *
  38746. + * This program is distributed in the hope that it will be useful, but
  38747. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  38748. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  38749. + * for more details.
  38750. + */
  38751. +
  38752. +char ipsec_tunnel_c_version[] = "RCSID $Id: ipsec_tunnel.c,v 1.232.2.5 2006/10/06 21:39:26 paul Exp $";
  38753. +
  38754. +#define __NO_VERSION__
  38755. +#include <linux/module.h>
  38756. +#ifndef AUTOCONF_INCLUDED
  38757. +#include <linux/config.h>
  38758. +#endif /* for CONFIG_IP_FORWARD */
  38759. +#include <linux/version.h>
  38760. +#include <linux/kernel.h> /* printk() */
  38761. +
  38762. +#include "openswan/ipsec_param.h"
  38763. +
  38764. +#ifdef MALLOC_SLAB
  38765. +# include <linux/slab.h> /* kmalloc() */
  38766. +#else /* MALLOC_SLAB */
  38767. +# include <linux/malloc.h> /* kmalloc() */
  38768. +#endif /* MALLOC_SLAB */
  38769. +#include <linux/errno.h> /* error codes */
  38770. +#include <linux/types.h> /* size_t */
  38771. +#include <linux/interrupt.h> /* mark_bh */
  38772. +
  38773. +#include <net/tcp.h>
  38774. +#include <net/udp.h>
  38775. +#include <linux/skbuff.h>
  38776. +
  38777. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  38778. +#include <linux/etherdevice.h> /* eth_type_trans */
  38779. +#include <linux/ip.h> /* struct iphdr */
  38780. +#include <linux/skbuff.h>
  38781. +
  38782. +#include <openswan.h>
  38783. +
  38784. +#ifdef NET_21
  38785. +# include <linux/in6.h>
  38786. +# define ip_chk_addr inet_addr_type
  38787. +# define IS_MYADDR RTN_LOCAL
  38788. +# include <net/dst.h>
  38789. +# undef dev_kfree_skb
  38790. +# define dev_kfree_skb(a,b) kfree_skb(a)
  38791. +# define PHYSDEV_TYPE
  38792. +#endif /* NET_21 */
  38793. +
  38794. +#include <net/icmp.h> /* icmp_send() */
  38795. +#include <net/ip.h>
  38796. +#ifdef NETDEV_23
  38797. +# include <linux/netfilter_ipv4.h>
  38798. +#endif /* NETDEV_23 */
  38799. +
  38800. +#include <linux/if_arp.h>
  38801. +#include <net/arp.h>
  38802. +
  38803. +#include "openswan/ipsec_kversion.h"
  38804. +#include "openswan/radij.h"
  38805. +#include "openswan/ipsec_life.h"
  38806. +#include "openswan/ipsec_xform.h"
  38807. +#include "openswan/ipsec_eroute.h"
  38808. +#include "openswan/ipsec_encap.h"
  38809. +#include "openswan/ipsec_radij.h"
  38810. +#include "openswan/ipsec_sa.h"
  38811. +#include "openswan/ipsec_tunnel.h"
  38812. +#include "openswan/ipsec_xmit.h"
  38813. +#include "openswan/ipsec_ipe4.h"
  38814. +#include "openswan/ipsec_ah.h"
  38815. +#include "openswan/ipsec_esp.h"
  38816. +#include "openswan/ipsec_kern24.h"
  38817. +
  38818. +#include <pfkeyv2.h>
  38819. +#include <pfkey.h>
  38820. +
  38821. +#include "openswan/ipsec_proto.h"
  38822. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  38823. +#include <linux/udp.h>
  38824. +#endif
  38825. +
  38826. +static __u32 zeroes[64];
  38827. +
  38828. +#ifdef CONFIG_KLIPS_DEBUG
  38829. +int debug_tunnel = 0;
  38830. +#endif /* CONFIG_KLIPS_DEBUG */
  38831. +
  38832. +DEBUG_NO_STATIC int
  38833. +ipsec_tunnel_open(struct net_device *dev)
  38834. +{
  38835. + struct ipsecpriv *prv = dev->priv;
  38836. +
  38837. + /*
  38838. + * Can't open until attached.
  38839. + */
  38840. +
  38841. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  38842. + "klips_debug:ipsec_tunnel_open: "
  38843. + "dev = %s, prv->dev = %s\n",
  38844. + dev->name, prv->dev?prv->dev->name:"NONE");
  38845. +
  38846. + if (prv->dev == NULL)
  38847. + return -ENODEV;
  38848. +
  38849. + KLIPS_INC_USE;
  38850. + return 0;
  38851. +}
  38852. +
  38853. +DEBUG_NO_STATIC int
  38854. +ipsec_tunnel_close(struct net_device *dev)
  38855. +{
  38856. + KLIPS_DEC_USE;
  38857. + return 0;
  38858. +}
  38859. +
  38860. +#ifdef NETDEV_23
  38861. +static inline int ipsec_tunnel_xmit2(struct sk_buff *skb)
  38862. +{
  38863. +#ifdef NETDEV_25 /* 2.6 kernels */
  38864. + return dst_output(skb);
  38865. +#else
  38866. + return ip_send(skb);
  38867. +#endif
  38868. +}
  38869. +#endif /* NETDEV_23 */
  38870. +
  38871. +enum ipsec_xmit_value
  38872. +ipsec_tunnel_strip_hard_header(struct ipsec_xmit_state *ixs)
  38873. +{
  38874. + /* ixs->physdev->hard_header_len is unreliable and should not be used */
  38875. + ixs->hard_header_len = (unsigned char *)(ixs->iph) - ixs->skb->data;
  38876. +
  38877. + if(ixs->hard_header_len < 0) {
  38878. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38879. + "klips_error:ipsec_xmit_strip_hard_header: "
  38880. + "Negative hard_header_len (%d)?!\n", ixs->hard_header_len);
  38881. + ixs->stats->tx_dropped++;
  38882. + return IPSEC_XMIT_BADHHLEN;
  38883. + }
  38884. +
  38885. + /* while ixs->physdev->hard_header_len is unreliable and
  38886. + * should not be trusted, it accurate and required for ATM, GRE and
  38887. + * some other interfaces to work. Thanks to Willy Tarreau
  38888. + * <willy@w.ods.org>.
  38889. + */
  38890. + if(ixs->hard_header_len == 0) { /* no hard header present */
  38891. + ixs->hard_header_stripped = 1;
  38892. + ixs->hard_header_len = ixs->physdev->hard_header_len;
  38893. + }
  38894. +
  38895. +#ifdef CONFIG_KLIPS_DEBUG
  38896. + if (debug_tunnel & DB_TN_XMIT) {
  38897. + int i;
  38898. + char c;
  38899. +
  38900. + printk(KERN_INFO "klips_debug:ipsec_xmit_strip_hard_header: "
  38901. + ">>> skb->len=%ld hard_header_len:%d",
  38902. + (unsigned long int)ixs->skb->len, ixs->hard_header_len);
  38903. + c = ' ';
  38904. + for (i=0; i < ixs->hard_header_len; i++) {
  38905. + printk("%c%02x", c, ixs->skb->data[i]);
  38906. + c = ':';
  38907. + }
  38908. + printk(" \n");
  38909. + }
  38910. +#endif /* CONFIG_KLIPS_DEBUG */
  38911. +
  38912. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->iph);
  38913. +
  38914. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  38915. + "klips_debug:ipsec_xmit_strip_hard_header: "
  38916. + "Original head,tailroom: %d,%d\n",
  38917. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  38918. +
  38919. + return IPSEC_XMIT_OK;
  38920. +}
  38921. +
  38922. +enum ipsec_xmit_value
  38923. +ipsec_tunnel_SAlookup(struct ipsec_xmit_state *ixs)
  38924. +{
  38925. + unsigned int bypass;
  38926. +
  38927. + bypass = FALSE;
  38928. +
  38929. + /*
  38930. + * First things first -- look us up in the erouting tables.
  38931. + */
  38932. + ixs->matcher.sen_len = sizeof (struct sockaddr_encap);
  38933. + ixs->matcher.sen_family = AF_ENCAP;
  38934. + ixs->matcher.sen_type = SENT_IP4;
  38935. + ixs->matcher.sen_ip_src.s_addr = ixs->iph->saddr;
  38936. + ixs->matcher.sen_ip_dst.s_addr = ixs->iph->daddr;
  38937. + ixs->matcher.sen_proto = ixs->iph->protocol;
  38938. + ipsec_extract_ports(ixs->iph, &ixs->matcher);
  38939. +
  38940. + /*
  38941. + * The spinlock is to prevent any other process from accessing or deleting
  38942. + * the eroute while we are using and updating it.
  38943. + */
  38944. + spin_lock(&eroute_lock);
  38945. +
  38946. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  38947. +
  38948. + if(ixs->iph->protocol == IPPROTO_UDP) {
  38949. + struct udphdr *t = NULL;
  38950. +
  38951. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38952. + "klips_debug:udp port check: "
  38953. + "fragoff: %d len: %d>%ld \n",
  38954. + ntohs(ixs->iph->frag_off) & IP_OFFSET,
  38955. + (ixs->skb->len - ixs->hard_header_len),
  38956. + (unsigned long int) ((ixs->iph->ihl << 2) + sizeof(struct udphdr)));
  38957. +
  38958. + if((ntohs(ixs->iph->frag_off) & IP_OFFSET) == 0 &&
  38959. + ((ixs->skb->len - ixs->hard_header_len) >=
  38960. + ((ixs->iph->ihl << 2) + sizeof(struct udphdr))))
  38961. + {
  38962. + t =((struct udphdr*)((caddr_t)ixs->iph+(ixs->iph->ihl<<2)));
  38963. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38964. + "klips_debug:udp port in packet: "
  38965. + "port %d -> %d\n",
  38966. + ntohs(t->source), ntohs(t->dest));
  38967. + }
  38968. +
  38969. + ixs->sport=0; ixs->dport=0;
  38970. +
  38971. + if(ixs->skb->sk) {
  38972. +#ifdef NET_26
  38973. + struct udp_sock *us;
  38974. +
  38975. + us = (struct udp_sock *)ixs->skb->sk;
  38976. +
  38977. + ixs->sport = ntohs(us->inet.sport);
  38978. + ixs->dport = ntohs(us->inet.dport);
  38979. +#else
  38980. + ixs->sport = ntohs(ixs->skb->sk->sport);
  38981. + ixs->dport = ntohs(ixs->skb->sk->dport);
  38982. +#endif
  38983. +
  38984. + }
  38985. +
  38986. + if(t != NULL) {
  38987. + if(ixs->sport == 0) {
  38988. + ixs->sport = ntohs(t->source);
  38989. + }
  38990. + if(ixs->dport == 0) {
  38991. + ixs->dport = ntohs(t->dest);
  38992. + }
  38993. + }
  38994. + }
  38995. +
  38996. + /*
  38997. + * practically identical to above, but let's be careful about
  38998. + * tcp vs udp headers
  38999. + */
  39000. + if(ixs->iph->protocol == IPPROTO_TCP) {
  39001. + struct tcphdr *t = NULL;
  39002. +
  39003. + if((ntohs(ixs->iph->frag_off) & IP_OFFSET) == 0 &&
  39004. + ((ixs->skb->len - ixs->hard_header_len) >=
  39005. + ((ixs->iph->ihl << 2) + sizeof(struct tcphdr)))) {
  39006. + t =((struct tcphdr*)((caddr_t)ixs->iph+(ixs->iph->ihl<<2)));
  39007. + }
  39008. +
  39009. + ixs->sport=0; ixs->dport=0;
  39010. +
  39011. + if(ixs->skb->sk) {
  39012. +#ifdef NET_26
  39013. +#ifdef HAVE_INET_SK_SPORT
  39014. + ixs->sport = ntohs(inet_sk(ixs->skb->sk)->sport);
  39015. + ixs->dport = ntohs(inet_sk(ixs->skb->sk)->dport);
  39016. +#else
  39017. + struct tcp_tw_bucket *tw;
  39018. +
  39019. + tw = (struct tcp_tw_bucket *)ixs->skb->sk;
  39020. +
  39021. + ixs->sport = ntohs(tw->tw_sport);
  39022. + ixs->dport = ntohs(tw->tw_dport);
  39023. +#endif
  39024. +#else
  39025. + ixs->sport = ntohs(ixs->skb->sk->sport);
  39026. + ixs->dport = ntohs(ixs->skb->sk->dport);
  39027. +#endif
  39028. + }
  39029. +
  39030. + if(t != NULL) {
  39031. + if(ixs->sport == 0) {
  39032. + ixs->sport = ntohs(t->source);
  39033. + }
  39034. + if(ixs->dport == 0) {
  39035. + ixs->dport = ntohs(t->dest);
  39036. + }
  39037. + }
  39038. + }
  39039. +
  39040. + /* default to a %drop eroute */
  39041. + ixs->outgoing_said.proto = IPPROTO_INT;
  39042. + ixs->outgoing_said.spi = htonl(SPI_DROP);
  39043. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr = INADDR_ANY;
  39044. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39045. + "klips_debug:ipsec_xmit_SAlookup: "
  39046. + "checking for local udp/500 IKE packet "
  39047. + "saddr=%x, er=0p%p, daddr=%x, er_dst=%x, proto=%d sport=%d dport=%d\n",
  39048. + ntohl((unsigned int)ixs->iph->saddr),
  39049. + ixs->eroute,
  39050. + ntohl((unsigned int)ixs->iph->daddr),
  39051. + ixs->eroute ? ntohl((unsigned int)ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr) : 0,
  39052. + ixs->iph->protocol,
  39053. + ixs->sport,
  39054. + ixs->dport);
  39055. +
  39056. + /*
  39057. + * cheat for now...are we udp/500? If so, let it through
  39058. + * without interference since it is most likely an IKE packet.
  39059. + */
  39060. +
  39061. + if (ip_chk_addr((unsigned long)ixs->iph->saddr) == IS_MYADDR
  39062. + && (ixs->eroute==NULL
  39063. + || ixs->iph->daddr == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr
  39064. + || INADDR_ANY == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr)
  39065. + && (ixs->iph->protocol == IPPROTO_UDP &&
  39066. + (ixs->sport == 500 || ixs->sport == 4500))) {
  39067. + /* Whatever the eroute, this is an IKE message
  39068. + * from us (i.e. not being forwarded).
  39069. + * Furthermore, if there is a tunnel eroute,
  39070. + * the destination is the peer for this eroute.
  39071. + * So %pass the packet: modify the default %drop.
  39072. + */
  39073. +
  39074. + ixs->outgoing_said.spi = htonl(SPI_PASS);
  39075. + if(!(ixs->skb->sk) && ((ntohs(ixs->iph->frag_off) & IP_MF) != 0)) {
  39076. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39077. + "klips_debug:ipsec_xmit_SAlookup: "
  39078. + "local UDP/500 (probably IKE) passthrough: base fragment, rest of fragments will probably get filtered.\n");
  39079. + }
  39080. + bypass = TRUE;
  39081. + }
  39082. +
  39083. +#ifdef KLIPS_EXCEPT_DNS53
  39084. + /*
  39085. + *
  39086. + * if we are udp/53 or tcp/53, also let it through a %trap or %hold,
  39087. + * since it is DNS, but *also* follow the %trap.
  39088. + *
  39089. + * we do not do this for tunnels, only %trap's and %hold's.
  39090. + *
  39091. + */
  39092. +
  39093. + if (ip_chk_addr((unsigned long)ixs->iph->saddr) == IS_MYADDR
  39094. + && (ixs->eroute==NULL
  39095. + || ixs->iph->daddr == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr
  39096. + || INADDR_ANY == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr)
  39097. + && ((ixs->iph->protocol == IPPROTO_UDP
  39098. + || ixs->iph->protocol == IPPROTO_TCP)
  39099. + && ixs->dport == 53)) {
  39100. +
  39101. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39102. + "klips_debug:ipsec_xmit_SAlookup: "
  39103. + "possible DNS packet\n");
  39104. +
  39105. + if(ixs->eroute)
  39106. + {
  39107. + if(ixs->eroute->er_said.spi == htonl(SPI_TRAP)
  39108. + || ixs->eroute->er_said.spi == htonl(SPI_HOLD))
  39109. + {
  39110. + ixs->outgoing_said.spi = htonl(SPI_PASSTRAP);
  39111. + bypass = TRUE;
  39112. + }
  39113. + }
  39114. + else
  39115. + {
  39116. + ixs->outgoing_said.spi = htonl(SPI_PASSTRAP);
  39117. + bypass = TRUE;
  39118. + }
  39119. +
  39120. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39121. + "klips_debug:ipsec_xmit_SAlookup: "
  39122. + "bypass = %d\n", bypass);
  39123. +
  39124. + if(bypass
  39125. + && !(ixs->skb->sk)
  39126. + && ((ntohs(ixs->iph->frag_off) & IP_MF) != 0))
  39127. + {
  39128. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39129. + "klips_debug:ipsec_xmit_SAlookup: "
  39130. + "local port 53 (probably DNS) passthrough:"
  39131. + "base fragment, rest of fragments will "
  39132. + "probably get filtered.\n");
  39133. + }
  39134. + }
  39135. +#endif
  39136. +
  39137. + if (bypass==FALSE && ixs->eroute) {
  39138. + ixs->eroute->er_count++;
  39139. + ixs->eroute->er_lasttime = jiffies/HZ;
  39140. + if(ixs->eroute->er_said.proto==IPPROTO_INT
  39141. + && ixs->eroute->er_said.spi==htonl(SPI_HOLD))
  39142. + {
  39143. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39144. + "klips_debug:ipsec_xmit_SAlookup: "
  39145. + "shunt SA of HOLD: skb stored in HOLD.\n");
  39146. + if(ixs->eroute->er_last != NULL) {
  39147. + kfree_skb(ixs->eroute->er_last);
  39148. + }
  39149. + ixs->eroute->er_last = ixs->skb;
  39150. + ixs->skb = NULL;
  39151. + ixs->stats->tx_dropped++;
  39152. + spin_unlock(&eroute_lock);
  39153. + return IPSEC_XMIT_STOLEN;
  39154. + }
  39155. + ixs->outgoing_said = ixs->eroute->er_said;
  39156. + ixs->eroute_pid = ixs->eroute->er_pid;
  39157. +
  39158. + /* Copy of the ident for the TRAP/TRAPSUBNET eroutes */
  39159. + if(ixs->outgoing_said.proto==IPPROTO_INT
  39160. + && (ixs->outgoing_said.spi==htonl(SPI_TRAP)
  39161. + || (ixs->outgoing_said.spi==htonl(SPI_TRAPSUBNET)))) {
  39162. + int len;
  39163. +
  39164. + ixs->ips.ips_ident_s.type = ixs->eroute->er_ident_s.type;
  39165. + ixs->ips.ips_ident_s.id = ixs->eroute->er_ident_s.id;
  39166. + ixs->ips.ips_ident_s.len = ixs->eroute->er_ident_s.len;
  39167. + if (ixs->ips.ips_ident_s.len)
  39168. + {
  39169. + len = ixs->ips.ips_ident_s.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  39170. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39171. + "klips_debug:ipsec_xmit_SAlookup: "
  39172. + "allocating %d bytes for ident_s shunt SA of HOLD: skb stored in HOLD.\n",
  39173. + len);
  39174. + if ((ixs->ips.ips_ident_s.data = kmalloc(len, GFP_ATOMIC)) == NULL) {
  39175. + printk(KERN_WARNING "klips_debug:ipsec_xmit_SAlookup: "
  39176. + "Failed, tried to allocate %d bytes for source ident.\n",
  39177. + len);
  39178. + ixs->stats->tx_dropped++;
  39179. + spin_unlock(&eroute_lock);
  39180. + return IPSEC_XMIT_ERRMEMALLOC;
  39181. + }
  39182. + memcpy(ixs->ips.ips_ident_s.data, ixs->eroute->er_ident_s.data, len);
  39183. + }
  39184. + ixs->ips.ips_ident_d.type = ixs->eroute->er_ident_d.type;
  39185. + ixs->ips.ips_ident_d.id = ixs->eroute->er_ident_d.id;
  39186. + ixs->ips.ips_ident_d.len = ixs->eroute->er_ident_d.len;
  39187. + if (ixs->ips.ips_ident_d.len)
  39188. + {
  39189. + len = ixs->ips.ips_ident_d.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  39190. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39191. + "klips_debug:ipsec_xmit_SAlookup: "
  39192. + "allocating %d bytes for ident_d shunt SA of HOLD: skb stored in HOLD.\n",
  39193. + len);
  39194. + if ((ixs->ips.ips_ident_d.data = kmalloc(len, GFP_ATOMIC)) == NULL) {
  39195. + printk(KERN_WARNING "klips_debug:ipsec_xmit_SAlookup: "
  39196. + "Failed, tried to allocate %d bytes for dest ident.\n",
  39197. + len);
  39198. + ixs->stats->tx_dropped++;
  39199. + spin_unlock(&eroute_lock);
  39200. + return IPSEC_XMIT_ERRMEMALLOC;
  39201. + }
  39202. + memcpy(ixs->ips.ips_ident_d.data, ixs->eroute->er_ident_d.data, len);
  39203. + }
  39204. + }
  39205. + }
  39206. +
  39207. + spin_unlock(&eroute_lock);
  39208. + return IPSEC_XMIT_OK;
  39209. +}
  39210. +
  39211. +
  39212. +enum ipsec_xmit_value
  39213. +ipsec_tunnel_restore_hard_header(struct ipsec_xmit_state*ixs)
  39214. +{
  39215. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  39216. + "klips_debug:ipsec_xmit_restore_hard_header: "
  39217. + "After recursive xforms -- head,tailroom: %d,%d\n",
  39218. + skb_headroom(ixs->skb),
  39219. + skb_tailroom(ixs->skb));
  39220. +
  39221. + if(ixs->saved_header) {
  39222. + if(skb_headroom(ixs->skb) < ixs->hard_header_len) {
  39223. + printk(KERN_WARNING
  39224. + "klips_error:ipsec_xmit_restore_hard_header: "
  39225. + "tried to skb_push hhlen=%d, %d available. This should never happen, please report.\n",
  39226. + ixs->hard_header_len,
  39227. + skb_headroom(ixs->skb));
  39228. + ixs->stats->tx_errors++;
  39229. + return IPSEC_XMIT_PUSHPULLERR;
  39230. +
  39231. + }
  39232. + skb_push(ixs->skb, ixs->hard_header_len);
  39233. + {
  39234. + int i;
  39235. + for (i = 0; i < ixs->hard_header_len; i++) {
  39236. + ixs->skb->data[i] = ixs->saved_header[i];
  39237. + }
  39238. + }
  39239. + }
  39240. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  39241. + if (ixs->natt_type && ixs->natt_head) {
  39242. + struct iphdr *ipp = ixs->skb->nh.iph;
  39243. + struct udphdr *udp;
  39244. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39245. + "klips_debug:ipsec_tunnel_start_xmit: "
  39246. + "encapsuling packet into UDP (NAT-Traversal) (%d %d)\n",
  39247. + ixs->natt_type, ixs->natt_head);
  39248. +
  39249. + ixs->iphlen = ipp->ihl << 2;
  39250. + ipp->tot_len =
  39251. + htons(ntohs(ipp->tot_len) + ixs->natt_head);
  39252. + if(skb_tailroom(ixs->skb) < ixs->natt_head) {
  39253. + printk(KERN_WARNING "klips_error:ipsec_tunnel_start_xmit: "
  39254. + "tried to skb_put %d, %d available. "
  39255. + "This should never happen, please report.\n",
  39256. + ixs->natt_head,
  39257. + skb_tailroom(ixs->skb));
  39258. + ixs->stats->tx_errors++;
  39259. + return IPSEC_XMIT_ESPUDP;
  39260. + }
  39261. + skb_put(ixs->skb, ixs->natt_head);
  39262. +
  39263. + udp = (struct udphdr *)((char *)ipp + ixs->iphlen);
  39264. +
  39265. + /* move ESP hdr after UDP hdr */
  39266. + memmove((void *)((char *)udp + ixs->natt_head),
  39267. + (void *)(udp),
  39268. + ntohs(ipp->tot_len) - ixs->iphlen - ixs->natt_head);
  39269. +
  39270. + /* clear UDP & Non-IKE Markers (if any) */
  39271. + memset(udp, 0, ixs->natt_head);
  39272. +
  39273. + /* fill UDP with usefull informations ;-) */
  39274. + udp->source = htons(ixs->natt_sport);
  39275. + udp->dest = htons(ixs->natt_dport);
  39276. + udp->len = htons(ntohs(ipp->tot_len) - ixs->iphlen);
  39277. +
  39278. + /* set protocol */
  39279. + ipp->protocol = IPPROTO_UDP;
  39280. +
  39281. + /* fix IP checksum */
  39282. + ipp->check = 0;
  39283. + ipp->check = ip_fast_csum((unsigned char *)ipp, ipp->ihl);
  39284. + }
  39285. +#endif
  39286. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  39287. + "klips_debug:ipsec_xmit_restore_hard_header: "
  39288. + "With hard_header, final head,tailroom: %d,%d\n",
  39289. + skb_headroom(ixs->skb),
  39290. + skb_tailroom(ixs->skb));
  39291. +
  39292. + return IPSEC_XMIT_OK;
  39293. +}
  39294. +
  39295. +enum ipsec_xmit_value
  39296. +ipsec_tunnel_send(struct ipsec_xmit_state*ixs)
  39297. +{
  39298. +#ifdef NETDEV_25
  39299. + struct flowi fl;
  39300. +#endif
  39301. +
  39302. +#ifdef NET_21 /* 2.2 and 2.4 kernels */
  39303. + /* new route/dst cache code from James Morris */
  39304. + ixs->skb->dev = ixs->physdev;
  39305. +#ifdef NETDEV_25
  39306. + memset (&fl, 0x0, sizeof (struct flowi));
  39307. + fl.oif = ixs->physdev->iflink;
  39308. + fl.nl_u.ip4_u.daddr = ixs->skb->nh.iph->daddr;
  39309. + fl.nl_u.ip4_u.saddr = ixs->pass ? 0 : ixs->skb->nh.iph->saddr;
  39310. + fl.nl_u.ip4_u.tos = RT_TOS(ixs->skb->nh.iph->tos);
  39311. + fl.proto = ixs->skb->nh.iph->protocol;
  39312. + if ((ixs->error = ip_route_output_key(&ixs->route, &fl))) {
  39313. +#else
  39314. + /*skb_orphan(ixs->skb);*/
  39315. + if((ixs->error = ip_route_output(&ixs->route,
  39316. + ixs->skb->nh.iph->daddr,
  39317. + ixs->pass ? 0 : ixs->skb->nh.iph->saddr,
  39318. + RT_TOS(ixs->skb->nh.iph->tos),
  39319. + /* mcr->rgb: should this be 0 instead? */
  39320. + ixs->physdev->iflink))) {
  39321. +#endif
  39322. + ixs->stats->tx_errors++;
  39323. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39324. + "klips_debug:ipsec_xmit_send: "
  39325. + "ip_route_output failed with error code %d, rt->u.dst.dev=%s, dropped\n",
  39326. + ixs->error,
  39327. + ixs->route->u.dst.dev->name);
  39328. + return IPSEC_XMIT_ROUTEERR;
  39329. + }
  39330. + if(ixs->dev == ixs->route->u.dst.dev) {
  39331. + ip_rt_put(ixs->route);
  39332. + /* This is recursion, drop it. */
  39333. + ixs->stats->tx_errors++;
  39334. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39335. + "klips_debug:ipsec_xmit_send: "
  39336. + "suspect recursion, dev=rt->u.dst.dev=%s, dropped\n",
  39337. + ixs->dev->name);
  39338. + return IPSEC_XMIT_RECURSDETECT;
  39339. + }
  39340. + dst_release(ixs->skb->dst);
  39341. + ixs->skb->dst = &ixs->route->u.dst;
  39342. + ixs->stats->tx_bytes += ixs->skb->len;
  39343. + if(ixs->skb->len < ixs->skb->nh.raw - ixs->skb->data) {
  39344. + ixs->stats->tx_errors++;
  39345. + printk(KERN_WARNING
  39346. + "klips_error:ipsec_xmit_send: "
  39347. + "tried to __skb_pull nh-data=%ld, %d available. This should never happen, please report.\n",
  39348. + (unsigned long)(ixs->skb->nh.raw - ixs->skb->data),
  39349. + ixs->skb->len);
  39350. + return IPSEC_XMIT_PUSHPULLERR;
  39351. + }
  39352. + __skb_pull(ixs->skb, ixs->skb->nh.raw - ixs->skb->data);
  39353. +#ifdef SKB_RESET_NFCT
  39354. + if(!ixs->pass) {
  39355. + nf_conntrack_put(ixs->skb->nfct);
  39356. + ixs->skb->nfct = NULL;
  39357. + }
  39358. +#if defined(CONFIG_NETFILTER_DEBUG) && defined(HAVE_SKB_NF_DEBUG)
  39359. + ixs->skb->nf_debug = 0;
  39360. +#endif /* CONFIG_NETFILTER_DEBUG */
  39361. +#endif /* SKB_RESET_NFCT */
  39362. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39363. + "klips_debug:ipsec_xmit_send: "
  39364. + "...done, calling ip_send() on device:%s\n",
  39365. + ixs->skb->dev ? ixs->skb->dev->name : "NULL");
  39366. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->skb->nh.iph);
  39367. +#ifdef NETDEV_23 /* 2.4 kernels */
  39368. + {
  39369. + int err;
  39370. +
  39371. + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, ixs->skb, NULL, ixs->route->u.dst.dev,
  39372. + ipsec_tunnel_xmit2);
  39373. + if(err != NET_XMIT_SUCCESS && err != NET_XMIT_CN) {
  39374. + if(net_ratelimit())
  39375. + printk(KERN_ERR
  39376. + "klips_error:ipsec_xmit_send: "
  39377. + "ip_send() failed, err=%d\n",
  39378. + -err);
  39379. + ixs->stats->tx_errors++;
  39380. + ixs->stats->tx_aborted_errors++;
  39381. + ixs->skb = NULL;
  39382. + return IPSEC_XMIT_IPSENDFAILURE;
  39383. + }
  39384. + }
  39385. +#else /* NETDEV_23 */ /* 2.2 kernels */
  39386. + ip_send(ixs->skb);
  39387. +#endif /* NETDEV_23 */
  39388. +#else /* NET_21 */ /* 2.0 kernels */
  39389. + ixs->skb->arp = 1;
  39390. + /* ISDN/ASYNC PPP from Matjaz Godec. */
  39391. + /* skb->protocol = htons(ETH_P_IP); */
  39392. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39393. + "klips_debug:ipsec_xmit_send: "
  39394. + "...done, calling dev_queue_xmit() or ip_fragment().\n");
  39395. + IP_SEND(ixs->skb, ixs->physdev);
  39396. +#endif /* NET_21 */
  39397. + ixs->stats->tx_packets++;
  39398. +
  39399. + ixs->skb = NULL;
  39400. +
  39401. + return IPSEC_XMIT_OK;
  39402. +}
  39403. +
  39404. +void
  39405. +ipsec_tunnel_cleanup(struct ipsec_xmit_state*ixs)
  39406. +{
  39407. +#if defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE)
  39408. + netif_wake_queue(ixs->dev);
  39409. +#else /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  39410. + ixs->dev->tbusy = 0;
  39411. +#endif /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  39412. + if(ixs->saved_header) {
  39413. + kfree(ixs->saved_header);
  39414. + }
  39415. + if(ixs->skb) {
  39416. + dev_kfree_skb(ixs->skb, FREE_WRITE);
  39417. + }
  39418. + if(ixs->oskb) {
  39419. + dev_kfree_skb(ixs->oskb, FREE_WRITE);
  39420. + }
  39421. + if (ixs->ips.ips_ident_s.data) {
  39422. + kfree(ixs->ips.ips_ident_s.data);
  39423. + }
  39424. + if (ixs->ips.ips_ident_d.data) {
  39425. + kfree(ixs->ips.ips_ident_d.data);
  39426. + }
  39427. +}
  39428. +
  39429. +/*
  39430. + * This function assumes it is being called from dev_queue_xmit()
  39431. + * and that skb is filled properly by that function.
  39432. + */
  39433. +int
  39434. +ipsec_tunnel_start_xmit(struct sk_buff *skb, struct net_device *dev)
  39435. +{
  39436. + struct ipsec_xmit_state ixs_mem;
  39437. + struct ipsec_xmit_state *ixs = &ixs_mem;
  39438. + enum ipsec_xmit_value stat;
  39439. +
  39440. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  39441. + ixs->natt_type = 0, ixs->natt_head = 0;
  39442. + ixs->natt_sport = 0, ixs->natt_dport = 0;
  39443. +#endif
  39444. +
  39445. + memset((caddr_t)ixs, 0, sizeof(*ixs));
  39446. + ixs->oskb = NULL;
  39447. + ixs->saved_header = NULL; /* saved copy of the hard header */
  39448. + ixs->route = NULL;
  39449. + memset((caddr_t)&(ixs->ips), 0, sizeof(ixs->ips));
  39450. + ixs->dev = dev;
  39451. + ixs->skb = skb;
  39452. +
  39453. + stat = ipsec_xmit_sanity_check_dev(ixs);
  39454. + if(stat != IPSEC_XMIT_OK) {
  39455. + goto cleanup;
  39456. + }
  39457. +
  39458. + stat = ipsec_xmit_sanity_check_skb(ixs);
  39459. + if(stat != IPSEC_XMIT_OK) {
  39460. + goto cleanup;
  39461. + }
  39462. +
  39463. + stat = ipsec_tunnel_strip_hard_header(ixs);
  39464. + if(stat != IPSEC_XMIT_OK) {
  39465. + goto cleanup;
  39466. + }
  39467. +
  39468. + stat = ipsec_tunnel_SAlookup(ixs);
  39469. + if(stat != IPSEC_XMIT_OK) {
  39470. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39471. + "klips_debug:ipsec_tunnel_start_xmit: SAlookup failed: %d\n",
  39472. + stat);
  39473. + goto cleanup;
  39474. + }
  39475. +
  39476. + ixs->innersrc = ixs->iph->saddr;
  39477. + /* start encapsulation loop here XXX */
  39478. + do {
  39479. + stat = ipsec_xmit_encap_bundle(ixs);
  39480. + if(stat != IPSEC_XMIT_OK) {
  39481. + if(stat == IPSEC_XMIT_PASS) {
  39482. + goto bypass;
  39483. + }
  39484. +
  39485. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39486. + "klips_debug:ipsec_tunnel_start_xmit: encap_bundle failed: %d\n",
  39487. + stat);
  39488. + goto cleanup;
  39489. + }
  39490. +
  39491. + ixs->matcher.sen_ip_src.s_addr = ixs->iph->saddr;
  39492. + ixs->matcher.sen_ip_dst.s_addr = ixs->iph->daddr;
  39493. + ixs->matcher.sen_proto = ixs->iph->protocol;
  39494. + ipsec_extract_ports(ixs->iph, &ixs->matcher);
  39495. +
  39496. + spin_lock(&eroute_lock);
  39497. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  39498. + if(ixs->eroute) {
  39499. + ixs->outgoing_said = ixs->eroute->er_said;
  39500. + ixs->eroute_pid = ixs->eroute->er_pid;
  39501. + ixs->eroute->er_count++;
  39502. + ixs->eroute->er_lasttime = jiffies/HZ;
  39503. + }
  39504. + spin_unlock(&eroute_lock);
  39505. +
  39506. + KLIPS_PRINT((debug_tunnel & DB_TN_XMIT) &&
  39507. + /* ((ixs->orgdst != ixs->newdst) || (ixs->orgsrc != ixs->newsrc)) */
  39508. + (ixs->orgedst != ixs->outgoing_said.dst.u.v4.sin_addr.s_addr) &&
  39509. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr &&
  39510. + ixs->eroute,
  39511. + "klips_debug:ipsec_tunnel_start_xmit: "
  39512. + "We are recursing here.\n");
  39513. +
  39514. + } while(/*((ixs->orgdst != ixs->newdst) || (ixs->orgsrc != ixs->newsrc))*/
  39515. + (ixs->orgedst != ixs->outgoing_said.dst.u.v4.sin_addr.s_addr) &&
  39516. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr &&
  39517. + ixs->eroute);
  39518. +
  39519. + stat = ipsec_tunnel_restore_hard_header(ixs);
  39520. + if(stat != IPSEC_XMIT_OK) {
  39521. + goto cleanup;
  39522. + }
  39523. +
  39524. + bypass:
  39525. + stat = ipsec_tunnel_send(ixs);
  39526. +
  39527. + cleanup:
  39528. + ipsec_tunnel_cleanup(ixs);
  39529. +
  39530. + return 0;
  39531. +}
  39532. +
  39533. +DEBUG_NO_STATIC struct net_device_stats *
  39534. +ipsec_tunnel_get_stats(struct net_device *dev)
  39535. +{
  39536. + return &(((struct ipsecpriv *)(dev->priv))->mystats);
  39537. +}
  39538. +
  39539. +/*
  39540. + * Revectored calls.
  39541. + * For each of these calls, a field exists in our private structure.
  39542. + */
  39543. +
  39544. +DEBUG_NO_STATIC int
  39545. +ipsec_tunnel_hard_header(struct sk_buff *skb, struct net_device *dev,
  39546. + unsigned short type, void *daddr, void *saddr, unsigned len)
  39547. +{
  39548. + struct ipsecpriv *prv = dev->priv;
  39549. + struct net_device *tmp;
  39550. + int ret;
  39551. + struct net_device_stats *stats; /* This device's statistics */
  39552. +
  39553. + if(skb == NULL) {
  39554. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39555. + "klips_debug:ipsec_tunnel_hard_header: "
  39556. + "no skb...\n");
  39557. + return -ENODATA;
  39558. + }
  39559. +
  39560. + if(dev == NULL) {
  39561. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39562. + "klips_debug:ipsec_tunnel_hard_header: "
  39563. + "no device...\n");
  39564. + return -ENODEV;
  39565. + }
  39566. +
  39567. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39568. + "klips_debug:ipsec_tunnel_hard_header: "
  39569. + "skb->dev=%s dev=%s.\n",
  39570. + skb->dev ? skb->dev->name : "NULL",
  39571. + dev->name);
  39572. +
  39573. + if(prv == NULL) {
  39574. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39575. + "klips_debug:ipsec_tunnel_hard_header: "
  39576. + "no private space associated with dev=%s\n",
  39577. + dev->name ? dev->name : "NULL");
  39578. + return -ENODEV;
  39579. + }
  39580. +
  39581. + stats = (struct net_device_stats *) &(prv->mystats);
  39582. +
  39583. + if(prv->dev == NULL) {
  39584. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39585. + "klips_debug:ipsec_tunnel_hard_header: "
  39586. + "no physical device associated with dev=%s\n",
  39587. + dev->name ? dev->name : "NULL");
  39588. + stats->tx_dropped++;
  39589. + return -ENODEV;
  39590. + }
  39591. +
  39592. + /* check if we have to send a IPv6 packet. It might be a Router
  39593. + Solicitation, where the building of the packet happens in
  39594. + reverse order:
  39595. + 1. ll hdr,
  39596. + 2. IPv6 hdr,
  39597. + 3. ICMPv6 hdr
  39598. + -> skb->nh.raw is still uninitialized when this function is
  39599. + called!! If this is no IPv6 packet, we can print debugging
  39600. + messages, otherwise we skip all debugging messages and just
  39601. + build the ll header */
  39602. + if(type != ETH_P_IPV6) {
  39603. + /* execute this only, if we don't have to build the
  39604. + header for a IPv6 packet */
  39605. + if(!prv->hard_header) {
  39606. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39607. + "klips_debug:ipsec_tunnel_hard_header: "
  39608. + "physical device has been detached, packet dropped 0p%p->0p%p len=%d type=%d dev=%s->NULL ",
  39609. + saddr,
  39610. + daddr,
  39611. + len,
  39612. + type,
  39613. + dev->name);
  39614. +#ifdef NET_21
  39615. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39616. + "ip=%08x->%08x\n",
  39617. + (__u32)ntohl(skb->nh.iph->saddr),
  39618. + (__u32)ntohl(skb->nh.iph->daddr) );
  39619. +#else /* NET_21 */
  39620. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39621. + "ip=%08x->%08x\n",
  39622. + (__u32)ntohl(skb->ip_hdr->saddr),
  39623. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39624. +#endif /* NET_21 */
  39625. + stats->tx_dropped++;
  39626. + return -ENODEV;
  39627. + }
  39628. +
  39629. +#define da ((struct net_device *)(prv->dev))->dev_addr
  39630. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39631. + "klips_debug:ipsec_tunnel_hard_header: "
  39632. + "Revectored 0p%p->0p%p len=%d type=%d dev=%s->%s dev_addr=%02x:%02x:%02x:%02x:%02x:%02x ",
  39633. + saddr,
  39634. + daddr,
  39635. + len,
  39636. + type,
  39637. + dev->name,
  39638. + prv->dev->name,
  39639. + da[0], da[1], da[2], da[3], da[4], da[5]);
  39640. +#ifdef NET_21
  39641. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39642. + "ip=%08x->%08x\n",
  39643. + (__u32)ntohl(skb->nh.iph->saddr),
  39644. + (__u32)ntohl(skb->nh.iph->daddr) );
  39645. +#else /* NET_21 */
  39646. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39647. + "ip=%08x->%08x\n",
  39648. + (__u32)ntohl(skb->ip_hdr->saddr),
  39649. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39650. +#endif /* NET_21 */
  39651. + } else {
  39652. + KLIPS_PRINT(debug_tunnel,
  39653. + "klips_debug:ipsec_tunnel_hard_header: "
  39654. + "is IPv6 packet, skip debugging messages, only revector and build linklocal header.\n");
  39655. + }
  39656. + tmp = skb->dev;
  39657. + skb->dev = prv->dev;
  39658. + ret = prv->hard_header(skb, prv->dev, type, (void *)daddr, (void *)saddr, len);
  39659. + skb->dev = tmp;
  39660. + return ret;
  39661. +}
  39662. +
  39663. +DEBUG_NO_STATIC int
  39664. +#ifdef NET_21
  39665. +ipsec_tunnel_rebuild_header(struct sk_buff *skb)
  39666. +#else /* NET_21 */
  39667. +ipsec_tunnel_rebuild_header(void *buff, struct net_device *dev,
  39668. + unsigned long raddr, struct sk_buff *skb)
  39669. +#endif /* NET_21 */
  39670. +{
  39671. + struct ipsecpriv *prv = skb->dev->priv;
  39672. + struct net_device *tmp;
  39673. + int ret;
  39674. + struct net_device_stats *stats; /* This device's statistics */
  39675. +
  39676. + if(skb->dev == NULL) {
  39677. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39678. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39679. + "no device...");
  39680. + return -ENODEV;
  39681. + }
  39682. +
  39683. + if(prv == NULL) {
  39684. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39685. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39686. + "no private space associated with dev=%s",
  39687. + skb->dev->name ? skb->dev->name : "NULL");
  39688. + return -ENODEV;
  39689. + }
  39690. +
  39691. + stats = (struct net_device_stats *) &(prv->mystats);
  39692. +
  39693. + if(prv->dev == NULL) {
  39694. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39695. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39696. + "no physical device associated with dev=%s",
  39697. + skb->dev->name ? skb->dev->name : "NULL");
  39698. + stats->tx_dropped++;
  39699. + return -ENODEV;
  39700. + }
  39701. +
  39702. + if(!prv->rebuild_header) {
  39703. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39704. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39705. + "physical device has been detached, packet dropped skb->dev=%s->NULL ",
  39706. + skb->dev->name);
  39707. +#ifdef NET_21
  39708. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39709. + "ip=%08x->%08x\n",
  39710. + (__u32)ntohl(skb->nh.iph->saddr),
  39711. + (__u32)ntohl(skb->nh.iph->daddr) );
  39712. +#else /* NET_21 */
  39713. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39714. + "ip=%08x->%08x\n",
  39715. + (__u32)ntohl(skb->ip_hdr->saddr),
  39716. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39717. +#endif /* NET_21 */
  39718. + stats->tx_dropped++;
  39719. + return -ENODEV;
  39720. + }
  39721. +
  39722. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39723. + "klips_debug:ipsec_tunnel: "
  39724. + "Revectored rebuild_header dev=%s->%s ",
  39725. + skb->dev->name, prv->dev->name);
  39726. +#ifdef NET_21
  39727. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39728. + "ip=%08x->%08x\n",
  39729. + (__u32)ntohl(skb->nh.iph->saddr),
  39730. + (__u32)ntohl(skb->nh.iph->daddr) );
  39731. +#else /* NET_21 */
  39732. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39733. + "ip=%08x->%08x\n",
  39734. + (__u32)ntohl(skb->ip_hdr->saddr),
  39735. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39736. +#endif /* NET_21 */
  39737. + tmp = skb->dev;
  39738. + skb->dev = prv->dev;
  39739. +
  39740. +#ifdef NET_21
  39741. + ret = prv->rebuild_header(skb);
  39742. +#else /* NET_21 */
  39743. + ret = prv->rebuild_header(buff, prv->dev, raddr, skb);
  39744. +#endif /* NET_21 */
  39745. + skb->dev = tmp;
  39746. + return ret;
  39747. +}
  39748. +
  39749. +DEBUG_NO_STATIC int
  39750. +ipsec_tunnel_set_mac_address(struct net_device *dev, void *addr)
  39751. +{
  39752. + struct ipsecpriv *prv = dev->priv;
  39753. +
  39754. + struct net_device_stats *stats; /* This device's statistics */
  39755. +
  39756. + if(dev == NULL) {
  39757. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39758. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39759. + "no device...");
  39760. + return -ENODEV;
  39761. + }
  39762. +
  39763. + if(prv == NULL) {
  39764. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39765. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39766. + "no private space associated with dev=%s",
  39767. + dev->name ? dev->name : "NULL");
  39768. + return -ENODEV;
  39769. + }
  39770. +
  39771. + stats = (struct net_device_stats *) &(prv->mystats);
  39772. +
  39773. + if(prv->dev == NULL) {
  39774. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39775. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39776. + "no physical device associated with dev=%s",
  39777. + dev->name ? dev->name : "NULL");
  39778. + stats->tx_dropped++;
  39779. + return -ENODEV;
  39780. + }
  39781. +
  39782. + if(!prv->set_mac_address) {
  39783. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39784. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39785. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39786. + dev->name);
  39787. + return -ENODEV;
  39788. + }
  39789. +
  39790. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39791. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39792. + "Revectored dev=%s->%s addr=0p%p\n",
  39793. + dev->name, prv->dev->name, addr);
  39794. + return prv->set_mac_address(prv->dev, addr);
  39795. +
  39796. +}
  39797. +
  39798. +#ifndef NET_21
  39799. +DEBUG_NO_STATIC void
  39800. +ipsec_tunnel_cache_bind(struct hh_cache **hhp, struct net_device *dev,
  39801. + unsigned short htype, __u32 daddr)
  39802. +{
  39803. + struct ipsecpriv *prv = dev->priv;
  39804. +
  39805. + struct net_device_stats *stats; /* This device's statistics */
  39806. +
  39807. + if(dev == NULL) {
  39808. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39809. + "klips_debug:ipsec_tunnel_cache_bind: "
  39810. + "no device...");
  39811. + return;
  39812. + }
  39813. +
  39814. + if(prv == NULL) {
  39815. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39816. + "klips_debug:ipsec_tunnel_cache_bind: "
  39817. + "no private space associated with dev=%s",
  39818. + dev->name ? dev->name : "NULL");
  39819. + return;
  39820. + }
  39821. +
  39822. + stats = (struct net_device_stats *) &(prv->mystats);
  39823. +
  39824. + if(prv->dev == NULL) {
  39825. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39826. + "klips_debug:ipsec_tunnel_cache_bind: "
  39827. + "no physical device associated with dev=%s",
  39828. + dev->name ? dev->name : "NULL");
  39829. + stats->tx_dropped++;
  39830. + return;
  39831. + }
  39832. +
  39833. + if(!prv->header_cache_bind) {
  39834. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39835. + "klips_debug:ipsec_tunnel_cache_bind: "
  39836. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39837. + dev->name);
  39838. + stats->tx_dropped++;
  39839. + return;
  39840. + }
  39841. +
  39842. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39843. + "klips_debug:ipsec_tunnel_cache_bind: "
  39844. + "Revectored \n");
  39845. + prv->header_cache_bind(hhp, prv->dev, htype, daddr);
  39846. + return;
  39847. +}
  39848. +#endif /* !NET_21 */
  39849. +
  39850. +
  39851. +DEBUG_NO_STATIC void
  39852. +ipsec_tunnel_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
  39853. +{
  39854. + struct ipsecpriv *prv = dev->priv;
  39855. +
  39856. + struct net_device_stats *stats; /* This device's statistics */
  39857. +
  39858. + if(dev == NULL) {
  39859. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39860. + "klips_debug:ipsec_tunnel_cache_update: "
  39861. + "no device...");
  39862. + return;
  39863. + }
  39864. +
  39865. + if(prv == NULL) {
  39866. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39867. + "klips_debug:ipsec_tunnel_cache_update: "
  39868. + "no private space associated with dev=%s",
  39869. + dev->name ? dev->name : "NULL");
  39870. + return;
  39871. + }
  39872. +
  39873. + stats = (struct net_device_stats *) &(prv->mystats);
  39874. +
  39875. + if(prv->dev == NULL) {
  39876. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39877. + "klips_debug:ipsec_tunnel_cache_update: "
  39878. + "no physical device associated with dev=%s",
  39879. + dev->name ? dev->name : "NULL");
  39880. + stats->tx_dropped++;
  39881. + return;
  39882. + }
  39883. +
  39884. + if(!prv->header_cache_update) {
  39885. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39886. + "klips_debug:ipsec_tunnel_cache_update: "
  39887. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39888. + dev->name);
  39889. + return;
  39890. + }
  39891. +
  39892. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39893. + "klips_debug:ipsec_tunnel: "
  39894. + "Revectored cache_update\n");
  39895. + prv->header_cache_update(hh, prv->dev, haddr);
  39896. + return;
  39897. +}
  39898. +
  39899. +#ifdef NET_21
  39900. +DEBUG_NO_STATIC int
  39901. +ipsec_tunnel_neigh_setup(struct neighbour *n)
  39902. +{
  39903. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39904. + "klips_debug:ipsec_tunnel_neigh_setup:\n");
  39905. +
  39906. + if (n->nud_state == NUD_NONE) {
  39907. + n->ops = &arp_broken_ops;
  39908. + n->output = n->ops->output;
  39909. + }
  39910. + return 0;
  39911. +}
  39912. +
  39913. +DEBUG_NO_STATIC int
  39914. +ipsec_tunnel_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  39915. +{
  39916. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39917. + "klips_debug:ipsec_tunnel_neigh_setup_dev: "
  39918. + "setting up %s\n",
  39919. + dev ? dev->name : "NULL");
  39920. +
  39921. + if (p->tbl->family == AF_INET) {
  39922. + p->neigh_setup = ipsec_tunnel_neigh_setup;
  39923. + p->ucast_probes = 0;
  39924. + p->mcast_probes = 0;
  39925. + }
  39926. + return 0;
  39927. +}
  39928. +#endif /* NET_21 */
  39929. +
  39930. +/*
  39931. + * We call the attach routine to attach another device.
  39932. + */
  39933. +
  39934. +DEBUG_NO_STATIC int
  39935. +ipsec_tunnel_attach(struct net_device *dev, struct net_device *physdev)
  39936. +{
  39937. + int i;
  39938. + struct ipsecpriv *prv = dev->priv;
  39939. +
  39940. + if(dev == NULL) {
  39941. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39942. + "klips_debug:ipsec_tunnel_attach: "
  39943. + "no device...");
  39944. + return -ENODEV;
  39945. + }
  39946. +
  39947. + if(prv == NULL) {
  39948. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39949. + "klips_debug:ipsec_tunnel_attach: "
  39950. + "no private space associated with dev=%s",
  39951. + dev->name ? dev->name : "NULL");
  39952. + return -ENODATA;
  39953. + }
  39954. +
  39955. + prv->dev = physdev;
  39956. + prv->hard_start_xmit = physdev->hard_start_xmit;
  39957. + prv->get_stats = physdev->get_stats;
  39958. +
  39959. + if (physdev->hard_header) {
  39960. + prv->hard_header = physdev->hard_header;
  39961. + dev->hard_header = ipsec_tunnel_hard_header;
  39962. + } else
  39963. + dev->hard_header = NULL;
  39964. +
  39965. + if (physdev->rebuild_header) {
  39966. + prv->rebuild_header = physdev->rebuild_header;
  39967. + dev->rebuild_header = ipsec_tunnel_rebuild_header;
  39968. + } else
  39969. + dev->rebuild_header = NULL;
  39970. +
  39971. + if (physdev->set_mac_address) {
  39972. + prv->set_mac_address = physdev->set_mac_address;
  39973. + dev->set_mac_address = ipsec_tunnel_set_mac_address;
  39974. + } else
  39975. + dev->set_mac_address = NULL;
  39976. +
  39977. +#ifndef NET_21
  39978. + if (physdev->header_cache_bind) {
  39979. + prv->header_cache_bind = physdev->header_cache_bind;
  39980. + dev->header_cache_bind = ipsec_tunnel_cache_bind;
  39981. + } else
  39982. + dev->header_cache_bind = NULL;
  39983. +#endif /* !NET_21 */
  39984. +
  39985. + if (physdev->header_cache_update) {
  39986. + prv->header_cache_update = physdev->header_cache_update;
  39987. + dev->header_cache_update = ipsec_tunnel_cache_update;
  39988. + } else
  39989. + dev->header_cache_update = NULL;
  39990. +
  39991. + dev->hard_header_len = physdev->hard_header_len;
  39992. +
  39993. +#ifdef NET_21
  39994. +/* prv->neigh_setup = physdev->neigh_setup; */
  39995. + dev->neigh_setup = ipsec_tunnel_neigh_setup_dev;
  39996. +#endif /* NET_21 */
  39997. + dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */
  39998. + prv->mtu = physdev->mtu;
  39999. +
  40000. +#ifdef PHYSDEV_TYPE
  40001. + dev->type = physdev->type; /* ARPHRD_TUNNEL; */
  40002. +#endif /* PHYSDEV_TYPE */
  40003. +
  40004. + dev->addr_len = physdev->addr_len;
  40005. + for (i=0; i<dev->addr_len; i++) {
  40006. + dev->dev_addr[i] = physdev->dev_addr[i];
  40007. + }
  40008. +#ifdef CONFIG_KLIPS_DEBUG
  40009. + if(debug_tunnel & DB_TN_INIT) {
  40010. + printk(KERN_INFO "klips_debug:ipsec_tunnel_attach: "
  40011. + "physical device %s being attached has HW address: %2x",
  40012. + physdev->name, physdev->dev_addr[0]);
  40013. + for (i=1; i < physdev->addr_len; i++) {
  40014. + printk(":%02x", physdev->dev_addr[i]);
  40015. + }
  40016. + printk("\n");
  40017. + }
  40018. +#endif /* CONFIG_KLIPS_DEBUG */
  40019. +
  40020. + return 0;
  40021. +}
  40022. +
  40023. +/*
  40024. + * We call the detach routine to detach the ipsec tunnel from another device.
  40025. + */
  40026. +
  40027. +DEBUG_NO_STATIC int
  40028. +ipsec_tunnel_detach(struct net_device *dev)
  40029. +{
  40030. + int i;
  40031. + struct ipsecpriv *prv = dev->priv;
  40032. +
  40033. + if(dev == NULL) {
  40034. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  40035. + "klips_debug:ipsec_tunnel_detach: "
  40036. + "no device...");
  40037. + return -ENODEV;
  40038. + }
  40039. +
  40040. + if(prv == NULL) {
  40041. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  40042. + "klips_debug:ipsec_tunnel_detach: "
  40043. + "no private space associated with dev=%s",
  40044. + dev->name ? dev->name : "NULL");
  40045. + return -ENODATA;
  40046. + }
  40047. +
  40048. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40049. + "klips_debug:ipsec_tunnel_detach: "
  40050. + "physical device %s being detached from virtual device %s\n",
  40051. + prv->dev ? prv->dev->name : "NULL",
  40052. + dev->name);
  40053. +
  40054. + ipsec_dev_put(prv->dev);
  40055. + prv->dev = NULL;
  40056. + prv->hard_start_xmit = NULL;
  40057. + prv->get_stats = NULL;
  40058. +
  40059. + prv->hard_header = NULL;
  40060. +#ifdef DETACH_AND_DOWN
  40061. + dev->hard_header = NULL;
  40062. +#endif /* DETACH_AND_DOWN */
  40063. +
  40064. + prv->rebuild_header = NULL;
  40065. +#ifdef DETACH_AND_DOWN
  40066. + dev->rebuild_header = NULL;
  40067. +#endif /* DETACH_AND_DOWN */
  40068. +
  40069. + prv->set_mac_address = NULL;
  40070. +#ifdef DETACH_AND_DOWN
  40071. + dev->set_mac_address = NULL;
  40072. +#endif /* DETACH_AND_DOWN */
  40073. +
  40074. +#ifndef NET_21
  40075. + prv->header_cache_bind = NULL;
  40076. +#ifdef DETACH_AND_DOWN
  40077. + dev->header_cache_bind = NULL;
  40078. +#endif /* DETACH_AND_DOWN */
  40079. +#endif /* !NET_21 */
  40080. +
  40081. + prv->header_cache_update = NULL;
  40082. +#ifdef DETACH_AND_DOWN
  40083. + dev->header_cache_update = NULL;
  40084. +#endif /* DETACH_AND_DOWN */
  40085. +
  40086. +#ifdef NET_21
  40087. +/* prv->neigh_setup = NULL; */
  40088. +#ifdef DETACH_AND_DOWN
  40089. + dev->neigh_setup = NULL;
  40090. +#endif /* DETACH_AND_DOWN */
  40091. +#endif /* NET_21 */
  40092. + dev->hard_header_len = 0;
  40093. +#ifdef DETACH_AND_DOWN
  40094. + dev->mtu = 0;
  40095. +#endif /* DETACH_AND_DOWN */
  40096. + prv->mtu = 0;
  40097. + for (i=0; i<MAX_ADDR_LEN; i++) {
  40098. + dev->dev_addr[i] = 0;
  40099. + }
  40100. + dev->addr_len = 0;
  40101. +#ifdef PHYSDEV_TYPE
  40102. + dev->type = ARPHRD_VOID; /* ARPHRD_TUNNEL; */
  40103. +#endif /* PHYSDEV_TYPE */
  40104. +
  40105. + return 0;
  40106. +}
  40107. +
  40108. +/*
  40109. + * We call the clear routine to detach all ipsec tunnels from other devices.
  40110. + */
  40111. +DEBUG_NO_STATIC int
  40112. +ipsec_tunnel_clear(void)
  40113. +{
  40114. + int i;
  40115. + struct net_device *ipsecdev = NULL, *prvdev;
  40116. + struct ipsecpriv *prv;
  40117. + int ret;
  40118. +
  40119. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40120. + "klips_debug:ipsec_tunnel_clear: .\n");
  40121. +
  40122. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40123. + ipsecdev = ipsecdevices[i];
  40124. + if(ipsecdev != NULL) {
  40125. + if((prv = (struct ipsecpriv *)(ipsecdev->priv))) {
  40126. + prvdev = (struct net_device *)(prv->dev);
  40127. + if(prvdev) {
  40128. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40129. + "klips_debug:ipsec_tunnel_clear: "
  40130. + "physical device for device %s is %s\n",
  40131. + ipsecdev->name, prvdev->name);
  40132. + if((ret = ipsec_tunnel_detach(ipsecdev))) {
  40133. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40134. + "klips_debug:ipsec_tunnel_clear: "
  40135. + "error %d detatching device %s from device %s.\n",
  40136. + ret, ipsecdev->name, prvdev->name);
  40137. + return ret;
  40138. + }
  40139. + }
  40140. + }
  40141. + }
  40142. + }
  40143. + return 0;
  40144. +}
  40145. +
  40146. +DEBUG_NO_STATIC int
  40147. +ipsec_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  40148. +{
  40149. + struct ipsectunnelconf *cf = (struct ipsectunnelconf *)&ifr->ifr_data;
  40150. + struct ipsecpriv *prv = dev->priv;
  40151. + struct net_device *them; /* physical device */
  40152. +#ifdef CONFIG_IP_ALIAS
  40153. + char *colon;
  40154. + char realphysname[IFNAMSIZ];
  40155. +#endif /* CONFIG_IP_ALIAS */
  40156. +
  40157. + if(dev == NULL) {
  40158. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40159. + "klips_debug:ipsec_tunnel_ioctl: "
  40160. + "device not supplied.\n");
  40161. + return -ENODEV;
  40162. + }
  40163. +
  40164. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40165. + "klips_debug:ipsec_tunnel_ioctl: "
  40166. + "tncfg service call #%d for dev=%s\n",
  40167. + cmd,
  40168. + dev->name ? dev->name : "NULL");
  40169. + switch (cmd) {
  40170. + /* attach a virtual ipsec? device to a physical device */
  40171. + case IPSEC_SET_DEV:
  40172. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40173. + "klips_debug:ipsec_tunnel_ioctl: "
  40174. + "calling ipsec_tunnel_attatch...\n");
  40175. +#ifdef CONFIG_IP_ALIAS
  40176. + /* If this is an IP alias interface, get its real physical name */
  40177. + strncpy(realphysname, cf->cf_name, IFNAMSIZ);
  40178. + realphysname[IFNAMSIZ-1] = 0;
  40179. + colon = strchr(realphysname, ':');
  40180. + if (colon) *colon = 0;
  40181. + them = ipsec_dev_get(realphysname);
  40182. +#else /* CONFIG_IP_ALIAS */
  40183. + them = ipsec_dev_get(cf->cf_name);
  40184. +#endif /* CONFIG_IP_ALIAS */
  40185. +
  40186. + if (them == NULL) {
  40187. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40188. + "klips_debug:ipsec_tunnel_ioctl: "
  40189. + "physical device %s requested is null\n",
  40190. + cf->cf_name);
  40191. + return -ENXIO;
  40192. + }
  40193. +
  40194. +#if 0
  40195. + if (them->flags & IFF_UP) {
  40196. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40197. + "klips_debug:ipsec_tunnel_ioctl: "
  40198. + "physical device %s requested is not up.\n",
  40199. + cf->cf_name);
  40200. + ipsec_dev_put(them);
  40201. + return -ENXIO;
  40202. + }
  40203. +#endif
  40204. +
  40205. + if (prv && prv->dev) {
  40206. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40207. + "klips_debug:ipsec_tunnel_ioctl: "
  40208. + "virtual device is already connected to %s.\n",
  40209. + prv->dev->name ? prv->dev->name : "NULL");
  40210. + ipsec_dev_put(them);
  40211. + return -EBUSY;
  40212. + }
  40213. + return ipsec_tunnel_attach(dev, them);
  40214. +
  40215. + case IPSEC_DEL_DEV:
  40216. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40217. + "klips_debug:ipsec_tunnel_ioctl: "
  40218. + "calling ipsec_tunnel_detatch.\n");
  40219. + if (! prv->dev) {
  40220. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40221. + "klips_debug:ipsec_tunnel_ioctl: "
  40222. + "physical device not connected.\n");
  40223. + return -ENODEV;
  40224. + }
  40225. + return ipsec_tunnel_detach(dev);
  40226. +
  40227. + case IPSEC_CLR_DEV:
  40228. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40229. + "klips_debug:ipsec_tunnel_ioctl: "
  40230. + "calling ipsec_tunnel_clear.\n");
  40231. + return ipsec_tunnel_clear();
  40232. +
  40233. + default:
  40234. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40235. + "klips_debug:ipsec_tunnel_ioctl: "
  40236. + "unknown command %d.\n",
  40237. + cmd);
  40238. + return -EOPNOTSUPP;
  40239. + }
  40240. +}
  40241. +
  40242. +struct net_device *ipsec_get_device(int inst)
  40243. +{
  40244. + struct net_device *ipsec_dev;
  40245. +
  40246. + ipsec_dev = NULL;
  40247. +
  40248. + if(inst < IPSEC_NUM_IF) {
  40249. + ipsec_dev = ipsecdevices[inst];
  40250. + }
  40251. +
  40252. + return ipsec_dev;
  40253. +}
  40254. +
  40255. +int
  40256. +ipsec_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  40257. +{
  40258. + struct net_device *dev = ptr;
  40259. + struct net_device *ipsec_dev;
  40260. + struct ipsecpriv *priv;
  40261. + int i;
  40262. +
  40263. + if (dev == NULL) {
  40264. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40265. + "klips_debug:ipsec_device_event: "
  40266. + "dev=NULL for event type %ld.\n",
  40267. + event);
  40268. + return(NOTIFY_DONE);
  40269. + }
  40270. +
  40271. + /* check for loopback devices */
  40272. + if (dev && (dev->flags & IFF_LOOPBACK)) {
  40273. + return(NOTIFY_DONE);
  40274. + }
  40275. +
  40276. + switch (event) {
  40277. + case NETDEV_DOWN:
  40278. + /* look very carefully at the scope of these compiler
  40279. + directives before changing anything... -- RGB */
  40280. +#ifdef NET_21
  40281. + case NETDEV_UNREGISTER:
  40282. + switch (event) {
  40283. + case NETDEV_DOWN:
  40284. +#endif /* NET_21 */
  40285. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40286. + "klips_debug:ipsec_device_event: "
  40287. + "NETDEV_DOWN dev=%s flags=%x\n",
  40288. + dev->name,
  40289. + dev->flags);
  40290. + if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) {
  40291. + printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n",
  40292. + dev->name);
  40293. + }
  40294. +#ifdef NET_21
  40295. + break;
  40296. + case NETDEV_UNREGISTER:
  40297. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40298. + "klips_debug:ipsec_device_event: "
  40299. + "NETDEV_UNREGISTER dev=%s flags=%x\n",
  40300. + dev->name,
  40301. + dev->flags);
  40302. + break;
  40303. + }
  40304. +#endif /* NET_21 */
  40305. +
  40306. + /* find the attached physical device and detach it. */
  40307. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40308. + ipsec_dev = ipsecdevices[i];
  40309. +
  40310. + if(ipsec_dev) {
  40311. + priv = (struct ipsecpriv *)(ipsec_dev->priv);
  40312. + if(priv) {
  40313. + ;
  40314. + if(((struct net_device *)(priv->dev)) == dev) {
  40315. + /* dev_close(ipsec_dev); */
  40316. + /* return */ ipsec_tunnel_detach(ipsec_dev);
  40317. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40318. + "klips_debug:ipsec_device_event: "
  40319. + "device '%s' has been detached.\n",
  40320. + ipsec_dev->name);
  40321. + break;
  40322. + }
  40323. + } else {
  40324. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40325. + "klips_debug:ipsec_device_event: "
  40326. + "device '%s' has no private data space!\n",
  40327. + ipsec_dev->name);
  40328. + }
  40329. + }
  40330. + }
  40331. + break;
  40332. + case NETDEV_UP:
  40333. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40334. + "klips_debug:ipsec_device_event: "
  40335. + "NETDEV_UP dev=%s\n",
  40336. + dev->name);
  40337. + break;
  40338. +#ifdef NET_21
  40339. + case NETDEV_REBOOT:
  40340. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40341. + "klips_debug:ipsec_device_event: "
  40342. + "NETDEV_REBOOT dev=%s\n",
  40343. + dev->name);
  40344. + break;
  40345. + case NETDEV_CHANGE:
  40346. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40347. + "klips_debug:ipsec_device_event: "
  40348. + "NETDEV_CHANGE dev=%s flags=%x\n",
  40349. + dev->name,
  40350. + dev->flags);
  40351. + break;
  40352. + case NETDEV_REGISTER:
  40353. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40354. + "klips_debug:ipsec_device_event: "
  40355. + "NETDEV_REGISTER dev=%s\n",
  40356. + dev->name);
  40357. + break;
  40358. + case NETDEV_CHANGEMTU:
  40359. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40360. + "klips_debug:ipsec_device_event: "
  40361. + "NETDEV_CHANGEMTU dev=%s to mtu=%d\n",
  40362. + dev->name,
  40363. + dev->mtu);
  40364. + break;
  40365. + case NETDEV_CHANGEADDR:
  40366. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40367. + "klips_debug:ipsec_device_event: "
  40368. + "NETDEV_CHANGEADDR dev=%s\n",
  40369. + dev->name);
  40370. + break;
  40371. + case NETDEV_GOING_DOWN:
  40372. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40373. + "klips_debug:ipsec_device_event: "
  40374. + "NETDEV_GOING_DOWN dev=%s\n",
  40375. + dev->name);
  40376. + break;
  40377. + case NETDEV_CHANGENAME:
  40378. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40379. + "klips_debug:ipsec_device_event: "
  40380. + "NETDEV_CHANGENAME dev=%s\n",
  40381. + dev->name);
  40382. + break;
  40383. +#endif /* NET_21 */
  40384. + default:
  40385. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40386. + "klips_debug:ipsec_device_event: "
  40387. + "event type %ld unrecognised for dev=%s\n",
  40388. + event,
  40389. + dev->name);
  40390. + break;
  40391. + }
  40392. + return NOTIFY_DONE;
  40393. +}
  40394. +
  40395. +/*
  40396. + * Called when an ipsec tunnel device is initialized.
  40397. + * The ipsec tunnel device structure is passed to us.
  40398. + */
  40399. +
  40400. +int
  40401. +ipsec_tunnel_init(struct net_device *dev)
  40402. +{
  40403. + int i;
  40404. +
  40405. + KLIPS_PRINT(debug_tunnel,
  40406. + "klips_debug:ipsec_tunnel_init: "
  40407. + "allocating %lu bytes initialising device: %s\n",
  40408. + (unsigned long) sizeof(struct ipsecpriv),
  40409. + dev->name ? dev->name : "NULL");
  40410. +
  40411. + /* Add our tunnel functions to the device */
  40412. + dev->open = ipsec_tunnel_open;
  40413. + dev->stop = ipsec_tunnel_close;
  40414. + dev->hard_start_xmit = ipsec_tunnel_start_xmit;
  40415. + dev->get_stats = ipsec_tunnel_get_stats;
  40416. +
  40417. + dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL);
  40418. + if (dev->priv == NULL)
  40419. + return -ENOMEM;
  40420. + memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv));
  40421. +
  40422. + for(i = 0; i < sizeof(zeroes); i++) {
  40423. + ((__u8*)(zeroes))[i] = 0;
  40424. + }
  40425. +
  40426. +#ifndef NET_21
  40427. + /* Initialize the tunnel device structure */
  40428. + for (i = 0; i < DEV_NUMBUFFS; i++)
  40429. + skb_queue_head_init(&dev->buffs[i]);
  40430. +#endif /* !NET_21 */
  40431. +
  40432. + dev->set_multicast_list = NULL;
  40433. + dev->do_ioctl = ipsec_tunnel_ioctl;
  40434. + dev->hard_header = NULL;
  40435. + dev->rebuild_header = NULL;
  40436. + dev->set_mac_address = NULL;
  40437. +#ifndef NET_21
  40438. + dev->header_cache_bind = NULL;
  40439. +#endif /* !NET_21 */
  40440. + dev->header_cache_update= NULL;
  40441. +
  40442. +#ifdef NET_21
  40443. +/* prv->neigh_setup = NULL; */
  40444. + dev->neigh_setup = ipsec_tunnel_neigh_setup_dev;
  40445. +#endif /* NET_21 */
  40446. + dev->hard_header_len = 0;
  40447. + dev->mtu = 0;
  40448. + dev->addr_len = 0;
  40449. + dev->type = ARPHRD_VOID; /* ARPHRD_TUNNEL; */ /* ARPHRD_ETHER; */
  40450. + dev->tx_queue_len = 10; /* Small queue */
  40451. + memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */
  40452. +
  40453. + /* New-style flags. */
  40454. + dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */;
  40455. +
  40456. +#if 0
  40457. +#ifdef NET_21
  40458. + dev_init_buffers(dev);
  40459. +#else /* NET_21 */
  40460. + dev->family = AF_INET;
  40461. + dev->pa_addr = 0;
  40462. + dev->pa_brdaddr = 0;
  40463. + dev->pa_mask = 0;
  40464. + dev->pa_alen = 4;
  40465. +#endif /* NET_21 */
  40466. +#endif
  40467. +
  40468. + /* We're done. Have I forgotten anything? */
  40469. + return 0;
  40470. +}
  40471. +
  40472. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  40473. +/* Module specific interface (but it links with the rest of IPSEC) */
  40474. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  40475. +
  40476. +int
  40477. +ipsec_tunnel_probe(struct net_device *dev)
  40478. +{
  40479. + ipsec_tunnel_init(dev);
  40480. + return 0;
  40481. +}
  40482. +
  40483. +struct net_device *ipsecdevices[IPSEC_NUM_IF];
  40484. +
  40485. +int
  40486. +ipsec_tunnel_init_devices(void)
  40487. +{
  40488. + int i;
  40489. + char name[IFNAMSIZ];
  40490. + struct net_device *dev_ipsec;
  40491. +
  40492. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40493. + "klips_debug:ipsec_tunnel_init_devices: "
  40494. + "creating and registering IPSEC_NUM_IF=%u devices, allocating %lu per device, IFNAMSIZ=%u.\n",
  40495. + IPSEC_NUM_IF,
  40496. + (unsigned long) (sizeof(struct net_device) + IFNAMSIZ),
  40497. + IFNAMSIZ);
  40498. +
  40499. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40500. + sprintf(name, IPSEC_DEV_FORMAT, i);
  40501. + dev_ipsec = (struct net_device*)kmalloc(sizeof(struct net_device), GFP_KERNEL);
  40502. + if (dev_ipsec == NULL) {
  40503. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40504. + "klips_debug:ipsec_tunnel_init_devices: "
  40505. + "failed to allocate memory for device %s, quitting device init.\n",
  40506. + name);
  40507. + return -ENOMEM;
  40508. + }
  40509. + memset((caddr_t)dev_ipsec, 0, sizeof(struct net_device));
  40510. +#ifdef NETDEV_23
  40511. + strncpy(dev_ipsec->name, name, sizeof(dev_ipsec->name));
  40512. +#else /* NETDEV_23 */
  40513. + dev_ipsec->name = (char*)kmalloc(IFNAMSIZ, GFP_KERNEL);
  40514. + if (dev_ipsec->name == NULL) {
  40515. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40516. + "klips_debug:ipsec_tunnel_init_devices: "
  40517. + "failed to allocate memory for device %s name, quitting device init.\n",
  40518. + name);
  40519. + return -ENOMEM;
  40520. + }
  40521. + memset((caddr_t)dev_ipsec->name, 0, IFNAMSIZ);
  40522. + strncpy(dev_ipsec->name, name, IFNAMSIZ);
  40523. +#endif /* NETDEV_23 */
  40524. + dev_ipsec->next = NULL;
  40525. + dev_ipsec->init = &ipsec_tunnel_probe;
  40526. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40527. + "klips_debug:ipsec_tunnel_init_devices: "
  40528. + "registering device %s\n",
  40529. + dev_ipsec->name);
  40530. +
  40531. + /* reference and hold the device reference */
  40532. + dev_hold(dev_ipsec);
  40533. + ipsecdevices[i]=dev_ipsec;
  40534. +
  40535. + if (register_netdev(dev_ipsec) != 0) {
  40536. + KLIPS_PRINT(1 || debug_tunnel & DB_TN_INIT,
  40537. + "klips_debug:ipsec_tunnel_init_devices: "
  40538. + "registering device %s failed, quitting device init.\n",
  40539. + dev_ipsec->name);
  40540. + return -EIO;
  40541. + } else {
  40542. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40543. + "klips_debug:ipsec_tunnel_init_devices: "
  40544. + "registering device %s succeeded, continuing...\n",
  40545. + dev_ipsec->name);
  40546. + }
  40547. + }
  40548. + return 0;
  40549. +}
  40550. +
  40551. +/* void */
  40552. +int
  40553. +ipsec_tunnel_cleanup_devices(void)
  40554. +{
  40555. + int error = 0;
  40556. + int i;
  40557. + struct net_device *dev_ipsec;
  40558. +
  40559. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40560. + dev_ipsec = ipsecdevices[i];
  40561. + if(dev_ipsec == NULL) {
  40562. + continue;
  40563. + }
  40564. +
  40565. + /* release reference */
  40566. + ipsecdevices[i]=NULL;
  40567. + ipsec_dev_put(dev_ipsec);
  40568. +
  40569. + KLIPS_PRINT(debug_tunnel, "Unregistering %s (refcnt=%d)\n",
  40570. + dev_ipsec->name,
  40571. + atomic_read(&dev_ipsec->refcnt));
  40572. + unregister_netdev(dev_ipsec);
  40573. + KLIPS_PRINT(debug_tunnel, "Unregisted %s\n", dev_ipsec->name);
  40574. +#ifndef NETDEV_23
  40575. + kfree(dev_ipsec->name);
  40576. + dev_ipsec->name=NULL;
  40577. +#endif /* !NETDEV_23 */
  40578. + kfree(dev_ipsec->priv);
  40579. + dev_ipsec->priv=NULL;
  40580. + }
  40581. + return error;
  40582. +}
  40583. +
  40584. +/*
  40585. + * $Log: ipsec_tunnel.c,v $
  40586. + * Revision 1.232.2.5 2006/10/06 21:39:26 paul
  40587. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  40588. + * set. This is defined through autoconf.h which is included through the
  40589. + * linux kernel build macros.
  40590. + *
  40591. + * Revision 1.232.2.4 2006/03/28 20:58:19 ken
  40592. + * Fix for KLIPS on 2.6.16 - need to include <net/arp.h> now
  40593. + *
  40594. + * Revision 1.232.2.3 2006/02/15 05:14:12 paul
  40595. + * 568: uninitialized struct in ipsec_tunnel.c coud break routing under 2.6 kernels
  40596. + * ipsec_tunnel_send() calls the entry point function of routing subsystem
  40597. + * (ip_route_output_key()) using a not fully initialized struct of type
  40598. + * struct flowi.
  40599. + * This will cause a failure in routing packets through an ipsec interface
  40600. + * when patches for multipath routing from http://www.ssi.bg/~ja/
  40601. + * are applied.
  40602. + *
  40603. + * Revision 1.232.2.2 2005/11/22 04:11:52 ken
  40604. + * Backport fixes for 2.6.14 kernels from HEAD
  40605. + *
  40606. + * Revision 1.232.2.1 2005/09/21 22:57:43 paul
  40607. + * pulled up compile fix for 2.6.13
  40608. + *
  40609. + * Revision 1.232 2005/06/04 16:06:06 mcr
  40610. + * better patch for nat-t rcv-device code.
  40611. + *
  40612. + * Revision 1.231 2005/05/21 03:28:51 mcr
  40613. + * make sure that port-500 hole is used for port-4500 as well.
  40614. + *
  40615. + * Revision 1.230 2005/05/11 01:42:04 mcr
  40616. + * removal of debugging showed useless/wrong variables used.
  40617. + *
  40618. + * Revision 1.229 2005/04/29 05:10:22 mcr
  40619. + * removed from extraenous includes to make unit testing easier.
  40620. + *
  40621. + * Revision 1.228 2005/01/26 00:50:35 mcr
  40622. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  40623. + * and make sure that NAT_TRAVERSAL is set as well to match
  40624. + * userspace compiles of code.
  40625. + *
  40626. + * Revision 1.227 2004/12/10 21:16:08 ken
  40627. + * 64bit fixes from Opteron port of KLIPS 2.6
  40628. + *
  40629. + * Revision 1.226 2004/12/04 07:11:23 mcr
  40630. + * fix for snmp SIOCPRIVATE use of snmpd.
  40631. + * http://bugs.xelerance.com/view.php?id=144
  40632. + *
  40633. + * Revision 1.225 2004/12/03 21:25:57 mcr
  40634. + * compile time fixes for running on 2.6.
  40635. + * still experimental.
  40636. + *
  40637. + * Revision 1.224 2004/08/14 03:28:24 mcr
  40638. + * fixed log comment to remove warning about embedded comment.
  40639. + *
  40640. + * Revision 1.223 2004/08/04 15:57:07 mcr
  40641. + * moved des .h files to include/des/ *
  40642. + * included 2.6 protocol specific things
  40643. + * started at NAT-T support, but it will require a kernel patch.
  40644. + *
  40645. + * Revision 1.222 2004/08/03 18:19:08 mcr
  40646. + * in 2.6, use "net_device" instead of #define device->net_device.
  40647. + * this probably breaks 2.0 compiles.
  40648. + *
  40649. + * Revision 1.221 2004/07/10 19:11:18 mcr
  40650. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  40651. + *
  40652. + * Revision 1.220 2004/04/06 02:49:26 mcr
  40653. + * pullup of algo code from alg-branch.
  40654. + *
  40655. + * Revision 1.219 2004/02/03 03:13:17 mcr
  40656. + * minor edits for readability, and error reporting.
  40657. + *
  40658. + * Revision 1.218 2004/01/27 20:29:20 mcr
  40659. + * fix for unregister_netdev() problem for underlying eth0.
  40660. + *
  40661. + * Revision 1.217 2003/12/10 01:14:27 mcr
  40662. + * NAT-traversal patches to KLIPS.
  40663. + *
  40664. + * Revision 1.216 2003/12/04 23:01:17 mcr
  40665. + * removed ipsec_netlink.h
  40666. + *
  40667. + * Revision 1.215 2003/12/04 16:35:16 ken
  40668. + * Fix for ATM devices where physdev->hard_header_len *is* correct
  40669. + *
  40670. + * Revision 1.214 2003/11/25 23:52:37 mcr
  40671. + * fix typo in patch - ixs-> needed.
  40672. + *
  40673. + * Revision 1.213 2003/11/24 18:25:49 mcr
  40674. + * patch from willy@w.ods.org to fix problems with ATM interfaces.
  40675. + *
  40676. + * Revision 1.212 2003/10/31 02:27:55 mcr
  40677. + * pulled up port-selector patches and sa_id elimination.
  40678. + *
  40679. + * Revision 1.211.2.2 2003/10/29 01:30:41 mcr
  40680. + * elimited "struct sa_id".
  40681. + *
  40682. + * Revision 1.211.2.1 2003/09/21 13:59:56 mcr
  40683. + * pre-liminary X.509 patch - does not yet pass tests.
  40684. + *
  40685. + * Revision 1.211 2003/09/10 16:46:30 mcr
  40686. + * patches for 2.4 backport/2.6 existence.
  40687. + *
  40688. + * Revision 1.210 2003/07/31 22:47:16 mcr
  40689. + * preliminary (untested by FS-team) 2.5 patches.
  40690. + *
  40691. + * Revision 1.209 2003/06/22 21:28:43 mcr
  40692. + * inability to unload module was caused by calls to dev_get
  40693. + * (ipsec_dev_get), to gather a device from a name. There is
  40694. + * simply no reason to look the devices up - they should be kept
  40695. + * in a nice array, ready for use.
  40696. + *
  40697. + * Revision 1.208 2003/06/22 21:25:07 mcr
  40698. + * all staticly counted ipsecXXX device support removed.
  40699. + *
  40700. + * Revision 1.207 2003/04/02 20:15:37 mcr
  40701. + * fix for PR#204 - do not clear connection tracking info if we
  40702. + * the packet is being sent in the clear.
  40703. + *
  40704. + * Revision 1.206 2003/02/12 19:32:51 rgb
  40705. + * Refactored file to:
  40706. + * ipsec_xmit.c
  40707. + * ipsec_xmit.h
  40708. + * ipsec_mast.c
  40709. + *
  40710. + * Revision 1.205 2003/02/06 17:47:00 rgb
  40711. + *
  40712. + * Remove unused ipsec_tunnel_lock() and ipsec_tunnel_unlock() code.
  40713. + * Refactor ipsec_tunnel_start_xmit() further into:
  40714. + * ipsec_xmit_sanity_check_dev()
  40715. + * ipsec_xmit_sanity_check_skb()
  40716. + * ipsec_xmit_strip_hard_header()
  40717. + * ipsec_xmit_restore_hard_header()
  40718. + * ipsec_xmit_send()
  40719. + * ipsec_xmit_cleanup()
  40720. + * and start a skeletal ipsec_mast_start_xmit() .
  40721. + *
  40722. + * Revision 1.204 2003/02/06 06:43:46 rgb
  40723. + *
  40724. + * Refactor ipsec_tunnel_start_xmit, bringing out:
  40725. + * ipsec_xmit_SAlookup
  40726. + * ipsec_xmit_encap_once
  40727. + * ipsec_xmit_encap_bundle
  40728. + *
  40729. + * Revision 1.203 2003/02/06 02:21:34 rgb
  40730. + *
  40731. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  40732. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  40733. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  40734. + *
  40735. + * Revision 1.202 2003/01/03 07:38:01 rgb
  40736. + *
  40737. + * Start to refactor ipsec_tunnel_start_xmit() by putting local variables
  40738. + * into struct ipsec_xmit_state and renaming a few variables to give more
  40739. + * unique or searchable names.
  40740. + *
  40741. + * Revision 1.201 2003/01/03 00:31:28 rgb
  40742. + *
  40743. + * Clean up memset usage, including fixing 2 places where keys were not
  40744. + * properly wiped.
  40745. + *
  40746. + * Revision 1.200 2002/12/06 02:24:02 mcr
  40747. + * patches for compiling against SUSE 8.1 kernels. Requires
  40748. + * an additional -DSUSE_LINUX_2_4_19_IS_STUPID.
  40749. + *
  40750. + * Revision 1.199 2002/10/12 23:11:53 dhr
  40751. + *
  40752. + * [KenB + DHR] more 64-bit cleanup
  40753. + *
  40754. + * Revision 1.198 2002/10/05 05:02:58 dhr
  40755. + *
  40756. + * C labels go on statements
  40757. + *
  40758. + * Revision 1.197 2002/09/20 05:01:50 rgb
  40759. + * Added compiler directive to switch on IP options and fix IP options bug.
  40760. + * Make ip->ihl treatment consistent using shifts rather than multiplications.
  40761. + * Check for large enough packet before accessing udp header for IKE bypass.
  40762. + * Added memory allocation debugging.
  40763. + * Fixed potential memory allocation failure-induced oops.
  40764. + *
  40765. + * Revision 1.196 2002/07/24 18:44:54 rgb
  40766. + * Type fiddling to tame ia64 compiler.
  40767. + *
  40768. + * Revision 1.195 2002/07/23 03:36:07 rgb
  40769. + * Fixed 2.2 device initialisation hang.
  40770. + *
  40771. + * Revision 1.194 2002/05/27 21:40:34 rgb
  40772. + * Set unused ipsec devices to ARPHRD_VOID to avoid confusing iproute2.
  40773. + * Cleaned up intermediate step to dynamic device allocation.
  40774. + *
  40775. + * Revision 1.193 2002/05/27 19:31:36 rgb
  40776. + * Convert to dynamic ipsec device allocation.
  40777. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  40778. + *
  40779. + * Revision 1.192 2002/05/23 07:14:28 rgb
  40780. + * Added refcount code.
  40781. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  40782. + *
  40783. + * Revision 1.191 2002/05/14 02:34:37 rgb
  40784. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  40785. + * ipsec_sa or ipsec_sa.
  40786. + *
  40787. + * Revision 1.190 2002/04/24 07:55:32 mcr
  40788. + * #include patches and Makefiles for post-reorg compilation.
  40789. + *
  40790. + * Revision 1.189 2002/04/24 07:36:32 mcr
  40791. + * Moved from ./klips/net/ipsec/ipsec_tunnel.c,v
  40792. + *
  40793. + * Revision 1.188 2002/04/20 00:12:25 rgb
  40794. + * Added esp IV CBC attack fix, disabled.
  40795. + *
  40796. + * Revision 1.187 2002/03/23 19:55:17 rgb
  40797. + * Fix for 2.2 local IKE fragmentation blackhole. Still won't work if
  40798. + * iptraf or another pcap app is running.
  40799. + *
  40800. + * Revision 1.186 2002/03/19 03:26:22 rgb
  40801. + * Applied DHR's tunnel patch to streamline IKE/specialSA processing.
  40802. + *
  40803. + * Revision 1.185 2002/02/20 04:13:05 rgb
  40804. + * Send back ICMP_PKT_FILTERED upon %reject.
  40805. + *
  40806. + * Revision 1.184 2002/01/29 17:17:56 mcr
  40807. + * moved include of ipsec_param.h to after include of linux/kernel.h
  40808. + * otherwise, it seems that some option that is set in ipsec_param.h
  40809. + * screws up something subtle in the include path to kernel.h, and
  40810. + * it complains on the snprintf() prototype.
  40811. + *
  40812. + * Revision 1.183 2002/01/29 04:00:53 mcr
  40813. + * more excise of kversions.h header.
  40814. + *
  40815. + * Revision 1.182 2002/01/29 02:13:18 mcr
  40816. + * introduction of ipsec_kversion.h means that include of
  40817. + * ipsec_param.h must preceed any decisions about what files to
  40818. + * include to deal with differences in kernel source.
  40819. + *
  40820. + * Revision 1.181 2002/01/07 20:00:33 rgb
  40821. + * Added IKE destination port debugging.
  40822. + *
  40823. + * Revision 1.180 2001/12/21 21:49:54 rgb
  40824. + * Fixed bug as a result of moving IKE bypass above %trap/%hold code.
  40825. + *
  40826. + * Revision 1.179 2001/12/19 21:08:14 rgb
  40827. + * Added transport protocol ports to ipsec_print_ip().
  40828. + * Update eroute info for non-SA targets.
  40829. + * Added obey DF code disabled.
  40830. + * Fixed formatting bugs in ipsec_tunnel_hard_header().
  40831. + *
  40832. + * Revision 1.178 2001/12/05 09:36:10 rgb
  40833. + * Moved the UDP/500 IKE check just above the %hold/%trap checks to avoid
  40834. + * IKE packets being stolen by the %hold (and returned to the sending KMd
  40835. + * in an ACQUIRE, ironically ;-).
  40836. + *
  40837. + * Revision 1.177 2001/11/26 09:23:50 rgb
  40838. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  40839. + *
  40840. + * Revision 1.170.2.1 2001/09/25 02:28:27 mcr
  40841. + * struct tdb -> struct ipsec_sa.
  40842. + * lifetime checks moved to common routines.
  40843. + * cleaned up includes.
  40844. + *
  40845. + * Revision 1.170.2.2 2001/10/22 21:08:01 mcr
  40846. + * include des.h, removed phony prototypes and fixed calling
  40847. + * conventions to match real prototypes.
  40848. + *
  40849. + * Revision 1.176 2001/11/09 18:32:31 rgb
  40850. + * Added Hans Schultz' fragmented UDP/500 IKE socket port selector.
  40851. + *
  40852. + * Revision 1.175 2001/11/06 20:47:00 rgb
  40853. + * Added Eric Espie's TRAPSUBNET fix, minus spin-lock-bh dabbling.
  40854. + *
  40855. + * Revision 1.174 2001/11/06 19:50:43 rgb
  40856. + * Moved IP_SEND, ICMP_SEND, DEV_QUEUE_XMIT macros to ipsec_tunnel.h for
  40857. + * use also by pfkey_v2_parser.c
  40858. + *
  40859. + * Revision 1.173 2001/10/29 21:53:44 henry
  40860. + * tone down the device-down message slightly, until we can make it smarter
  40861. + *
  40862. + * Revision 1.172 2001/10/26 04:59:37 rgb
  40863. + * Added a critical level syslog message if an ipsec device goes down.
  40864. + *
  40865. + * Revision 1.171 2001/10/18 04:45:21 rgb
  40866. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  40867. + * lib/freeswan.h version macros moved to lib/kversions.h.
  40868. + * Other compiler directive cleanups.
  40869. + *
  40870. + * Revision 1.170 2001/09/25 00:09:50 rgb
  40871. + * Added NetCelo's TRAPSUBNET code to convert a new type TRAPSUBNET into a
  40872. + * HOLD.
  40873. + *
  40874. + * Revision 1.169 2001/09/15 16:24:05 rgb
  40875. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  40876. + *
  40877. + * Revision 1.168 2001/09/14 16:58:37 rgb
  40878. + * Added support for storing the first and last packets through a HOLD.
  40879. + *
  40880. + * Revision 1.167 2001/09/08 21:13:33 rgb
  40881. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  40882. + *
  40883. + * Revision 1.166 2001/08/27 19:47:59 rgb
  40884. + * Clear tdb before usage.
  40885. + * Added comment: clear IF before calling routing?
  40886. + *
  40887. + * Revision 1.165 2001/07/03 01:23:53 rgb
  40888. + * Send back ICMP iff DF set, !ICMP, offset==0, sysctl_icmp, iph->tot_len >
  40889. + * emtu, and don't drop.
  40890. + *
  40891. + * Revision 1.164 2001/06/14 19:35:10 rgb
  40892. + * Update copyright date.
  40893. + *
  40894. + * Revision 1.163 2001/06/06 20:28:51 rgb
  40895. + * Added sanity checks for NULL skbs and devices.
  40896. + * Added more debugging output to various functions.
  40897. + * Removed redundant dev->priv argument to ipsec_tunnel_{at,de}tach().
  40898. + * Renamed ipsec_tunnel_attach() virtual and physical device arguments.
  40899. + * Corrected neigh_setup() device function assignment.
  40900. + * Keep valid pointers to ipsec_tunnel_*() on detach.
  40901. + * Set dev->type to the originally-initiallised value.
  40902. + *
  40903. + * Revision 1.162 2001/06/01 07:28:04 rgb
  40904. + * Added sanity checks for detached devices. Don't down virtual devices
  40905. + * to prevent packets going out in the clear if the detached device comes
  40906. + * back up.
  40907. + *
  40908. + * Revision 1.161 2001/05/30 08:14:52 rgb
  40909. + * Removed vestiges of esp-null transforms.
  40910. + * NetDev Notifier instrumentation to track down disappearing devices.
  40911. + *
  40912. + * Revision 1.160 2001/05/29 05:15:12 rgb
  40913. + * Added SS' PMTU patch which notifies sender if packet doesn't fit
  40914. + * physical MTU (if it wasn't ICMP) and then drops it.
  40915. + *
  40916. + * Revision 1.159 2001/05/27 06:12:12 rgb
  40917. + * Added structures for pid, packet count and last access time to eroute.
  40918. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  40919. + *
  40920. + * Revision 1.158 2001/05/24 05:39:33 rgb
  40921. + * Applied source zeroing to 2.2 ip_route_output() call as well to enable
  40922. + * PASS eroutes for opportunism.
  40923. + *
  40924. + * Revision 1.157 2001/05/23 22:35:28 rgb
  40925. + * 2.4 source override simplification.
  40926. + *
  40927. + * Revision 1.156 2001/05/23 21:41:31 rgb
  40928. + * Added error return code printing on ip_route_output().
  40929. + *
  40930. + * Revision 1.155 2001/05/23 05:09:13 rgb
  40931. + * Fixed incorrect ip_route_output() failure message.
  40932. + *
  40933. + * Revision 1.154 2001/05/21 14:53:31 rgb
  40934. + * Added debug statement for case when ip_route_output() fails, causing
  40935. + * packet to be dropped, but log looked ok.
  40936. + *
  40937. + * Revision 1.153 2001/05/19 02:37:54 rgb
  40938. + * Fixed missing comment termination.
  40939. + *
  40940. + * Revision 1.152 2001/05/19 02:35:50 rgb
  40941. + * Debug code optimisation for non-debug speed.
  40942. + * Kernel version compiler define comments.
  40943. + * 2.2 and 2.4 kernel ip_send device and ip debug output added.
  40944. + *
  40945. + * Revision 1.151 2001/05/18 16:17:35 rgb
  40946. + * Changed reference from "magic" to "shunt" SAs.
  40947. + *
  40948. + * Revision 1.150 2001/05/18 16:12:19 rgb
  40949. + * Changed UDP/500 bypass test from 3 nested ifs to one anded if.
  40950. + *
  40951. + * Revision 1.149 2001/05/16 04:39:33 rgb
  40952. + * Add default == eroute.dest to IKE bypass conditions for magic eroutes.
  40953. + *
  40954. + * Revision 1.148 2001/05/05 03:31:41 rgb
  40955. + * IP frag debugging updates and enhancements.
  40956. + *
  40957. + * Revision 1.147 2001/05/03 19:41:40 rgb
  40958. + * Added SS' skb_cow fix for 2.4.4.
  40959. + *
  40960. + * Revision 1.146 2001/04/30 19:28:16 rgb
  40961. + * Update for 2.4.4. ip_select_ident() now has 3 args.
  40962. + *
  40963. + * Revision 1.145 2001/04/23 14:56:10 rgb
  40964. + * Added spin_lock() check to prevent double-locking for multiple
  40965. + * transforms and hence kernel lock-ups with SMP kernels.
  40966. + *
  40967. + * Revision 1.144 2001/04/21 23:04:45 rgb
  40968. + * Define out skb->used for 2.4 kernels.
  40969. + * Check if soft expire has already been sent before sending another to
  40970. + * prevent ACQUIRE flooding.
  40971. + *
  40972. + * Revision 1.143 2001/03/16 07:37:21 rgb
  40973. + * Added comments to all #endifs.
  40974. + *
  40975. + * Revision 1.142 2001/02/28 05:03:27 rgb
  40976. + * Clean up and rationalise startup messages.
  40977. + *
  40978. + * Revision 1.141 2001/02/27 22:24:54 rgb
  40979. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  40980. + * Check for satoa() return codes.
  40981. + *
  40982. + * Revision 1.140 2001/02/27 06:40:12 rgb
  40983. + * Fixed TRAP->HOLD eroute byte order.
  40984. + *
  40985. + * Revision 1.139 2001/02/26 20:38:59 rgb
  40986. + * Added compiler defines for 2.4.x-specific code.
  40987. + *
  40988. + * Revision 1.138 2001/02/26 19:57:27 rgb
  40989. + * Implement magic SAs %drop, %reject, %trap, %hold, %pass as part
  40990. + * of the new SPD and to support opportunistic.
  40991. + * Drop sysctl_ipsec_{no_eroute_pass,opportunistic}, replaced by magic SAs.
  40992. + *
  40993. + * Revision 1.137 2001/02/19 22:29:49 rgb
  40994. + * Fixes for presence of active ipv6 segments which share ipsec physical
  40995. + * device (gg).
  40996. + *
  40997. + * Revision 1.136 2001/01/29 22:30:38 rgb
  40998. + * Fixed minor acquire debug printing bug.
  40999. + *
  41000. + * Revision 1.135 2001/01/29 22:19:45 rgb
  41001. + * Zero source address for 2.4 bypass route lookup.
  41002. + *
  41003. + * Revision 1.134 2001/01/23 20:19:49 rgb
  41004. + * 2.4 fix to remove removed is_clone member.
  41005. + *
  41006. + * Revision 1.133 2000/12/09 22:08:35 rgb
  41007. + * Fix NET_23 bug, should be NETDEV_23.
  41008. + *
  41009. + * Revision 1.132 2000/12/01 06:54:50 rgb
  41010. + * Fix for new 2.4 IP TTL default variable name.
  41011. + *
  41012. + * Revision 1.131 2000/11/09 20:52:15 rgb
  41013. + * More spinlock shuffling, locking earlier and unlocking later in rcv to
  41014. + * include ipcomp and prevent races, renaming some tdb variables that got
  41015. + * forgotten, moving some unlocks to include tdbs and adding a missing
  41016. + * unlock. Thanks to Svenning for some of these.
  41017. + *
  41018. + * Revision 1.130 2000/11/09 20:11:22 rgb
  41019. + * Minor shuffles to fix non-standard kernel config option selection.
  41020. + *
  41021. + * Revision 1.129 2000/11/06 04:32:49 rgb
  41022. + * Clean up debug printing.
  41023. + * Copy skb->protocol for all kernel versions.
  41024. + * Ditched spin_lock_irqsave in favour of spin_lock.
  41025. + * Disabled TTL decrement, done in ip_forward.
  41026. + * Added debug printing before pfkey_acquire().
  41027. + * Fixed printk-deltdbchain-spin_lock races (Svenning).
  41028. + * Use defaultTTL for 2.1+ kernels.
  41029. + * Add Svenning's adaptive content compression.
  41030. + * Fix up debug display arguments.
  41031. + *
  41032. + * Revision 1.128 2000/09/28 00:58:57 rgb
  41033. + * Moved the IKE passthrough check after the eroute lookup so we can pass
  41034. + * IKE through intermediate tunnels.
  41035. + *
  41036. + * Revision 1.127 2000/09/22 17:52:11 rgb
  41037. + * Fixed misleading ipcomp debug output.
  41038. + *
  41039. + * Revision 1.126 2000/09/22 04:22:56 rgb
  41040. + * Fixed dumb spi->cpi conversion error.
  41041. + *
  41042. + * Revision 1.125 2000/09/21 04:34:48 rgb
  41043. + * A few debug-specific things should be hidden under
  41044. + * CONFIG_IPSEC_DEBUG.(MB)
  41045. + * Improved ip_send() error handling.(MB)
  41046. + *
  41047. + * Revision 1.124 2000/09/21 03:40:58 rgb
  41048. + * Added more debugging to try and track down the cpi outward copy problem.
  41049. + *
  41050. + * Revision 1.123 2000/09/19 07:08:49 rgb
  41051. + * Added debugging to outgoing compression report.
  41052. + *
  41053. + * Revision 1.122 2000/09/18 19:21:26 henry
  41054. + * RGB-supplied fix for RH5.2 problem
  41055. + *
  41056. + * Revision 1.121 2000/09/17 21:05:09 rgb
  41057. + * Added tdb to skb_compress call to write in cpi.
  41058. + *
  41059. + * Revision 1.120 2000/09/17 16:57:16 rgb
  41060. + * Added Svenning's patch to remove restriction of ipcomp to innermost
  41061. + * transform.
  41062. + *
  41063. + * Revision 1.119 2000/09/15 11:37:01 rgb
  41064. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  41065. + * IPCOMP zlib deflate code.
  41066. + *
  41067. + * Revision 1.118 2000/09/15 04:57:16 rgb
  41068. + * Moved debug output after sanity check.
  41069. + * Added tos copy sysctl.
  41070. + *
  41071. + * Revision 1.117 2000/09/12 03:22:51 rgb
  41072. + * Converted ipsec_icmp, no_eroute_pass, opportunistic and #if0 debugs to
  41073. + * sysctl.
  41074. + *
  41075. + * Revision 1.116 2000/09/08 19:18:19 rgb
  41076. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  41077. + * Added outgoing opportunistic hook, ifdef'ed out.
  41078. + *
  41079. + * Revision 1.115 2000/08/30 05:27:29 rgb
  41080. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  41081. + * Kill remainder of tdb_xform, tdb_xdata, xformsw.
  41082. + *
  41083. + * Revision 1.114 2000/08/28 18:15:46 rgb
  41084. + * Added MB's nf-debug reset patch.
  41085. + *
  41086. + * Revision 1.113 2000/08/27 02:26:40 rgb
  41087. + * Send all no-eroute-bypass, pluto-bypass and passthrough packets through
  41088. + * fragmentation machinery for 2.0, 2.2 and 2.4 kernels.
  41089. + *
  41090. + * Revision 1.112 2000/08/20 21:37:33 rgb
  41091. + * Activated pfkey_expire() calls.
  41092. + * Added a hard/soft expiry parameter to pfkey_expire(). (Momchil)
  41093. + * Re-arranged the order of soft and hard expiry to conform to RFC2367.
  41094. + * Clean up references to CONFIG_IPSEC_PFKEYv2.
  41095. + *
  41096. + * Revision 1.111 2000/08/01 14:51:51 rgb
  41097. + * Removed _all_ remaining traces of DES.
  41098. + *
  41099. + * Revision 1.110 2000/07/28 14:58:31 rgb
  41100. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  41101. + *
  41102. + * Revision 1.109 2000/07/28 13:50:54 rgb
  41103. + * Changed enet_statistics to net_device_stats and added back compatibility
  41104. + * for pre-2.1.19.
  41105. + *
  41106. + * Revision 1.108 2000/05/16 03:03:11 rgb
  41107. + * Updates for 2.3.99pre8 from MB.
  41108. + *
  41109. + * Revision 1.107 2000/05/10 23:08:21 rgb
  41110. + * Print a debug warning about bogus packets received by the outgoing
  41111. + * processing machinery only when klipsdebug is not set to none.
  41112. + * Comment out the device initialisation informational messages.
  41113. + *
  41114. + * Revision 1.106 2000/05/10 19:17:14 rgb
  41115. + * Define an IP_SEND macro, intending to have all packet passthroughs
  41116. + * use fragmentation. This didn't quite work, but is a step in the
  41117. + * right direction.
  41118. + * Added buffer allocation debugging statements.
  41119. + * Added configure option to shut off no eroute passthrough.
  41120. + * Only check usetime against soft and hard limits if the tdb has been
  41121. + * used.
  41122. + * Cast output of ntohl so that the broken prototype doesn't make our
  41123. + * compile noisy.
  41124. + *
  41125. + * Revision 1.105 2000/03/22 16:15:37 rgb
  41126. + * Fixed renaming of dev_get (MB).
  41127. + *
  41128. + * Revision 1.104 2000/03/16 14:04:15 rgb
  41129. + * Indented headers for readability.
  41130. + * Fixed debug scope to enable compilation with debug off.
  41131. + * Added macros for ip_chk_addr and IS_MYADDR for identifying self.
  41132. + *
  41133. + * Revision 1.103 2000/03/16 07:11:07 rgb
  41134. + * Hardcode PF_KEYv2 support.
  41135. + * Fixed bug which allowed UDP/500 packet from another machine
  41136. + * through in the clear.
  41137. + * Added disabled skb->protocol fix for ISDN/ASYNC PPP from Matjaz Godec.
  41138. + *
  41139. + * Revision 1.102 2000/03/14 12:26:59 rgb
  41140. + * Added skb->nfct support for clearing netfilter conntrack bits (MB).
  41141. + *
  41142. + * Revision 1.101 2000/02/14 21:05:22 rgb
  41143. + * Added MB's netif_queue fix for kernels 2.3.43+.
  41144. + *
  41145. + * Revision 1.100 2000/01/26 10:04:57 rgb
  41146. + * Fixed noisy 2.0 printk arguments.
  41147. + *
  41148. + * Revision 1.99 2000/01/21 06:16:25 rgb
  41149. + * Added sanity checks on skb_push(), skb_pull() to prevent panics.
  41150. + * Switched to AF_ENCAP macro.
  41151. + * Shortened debug output per packet and re-arranging debug_tunnel
  41152. + * bitmap flags, while retaining necessary information to avoid
  41153. + * trampling the kernel print ring buffer.
  41154. + * Reformatted recursion switch code.
  41155. + * Changed all references to tdb_proto to tdb_said.proto for clarity.
  41156. + *
  41157. + * Revision 1.98 2000/01/13 08:09:31 rgb
  41158. + * Shuffled debug_tunnel switches to focus output.
  41159. + * Fixed outgoing recursion bug, limiting to recursing only if the remote
  41160. + * SG changes and if it is valid, ie. not passthrough.
  41161. + * Clarified a number of debug messages.
  41162. + *
  41163. + * Revision 1.97 2000/01/10 16:37:16 rgb
  41164. + * MB support for new ip_select_ident() upon disappearance of
  41165. + * ip_id_count in 2.3.36+.
  41166. + *
  41167. + * Revision 1.96 1999/12/31 14:59:08 rgb
  41168. + * MB fix to use new skb_copy_expand in kernel 2.3.35.
  41169. + *
  41170. + * Revision 1.95 1999/12/29 21:15:44 rgb
  41171. + * Fix tncfg to aliased device bug.
  41172. + *
  41173. + * Revision 1.94 1999/12/22 04:26:06 rgb
  41174. + * Converted all 'static' functions to 'DEBUG_NO_STATIC' to enable
  41175. + * debugging by providing external labels to all functions with debugging
  41176. + * turned on.
  41177. + *
  41178. + * Revision 1.93 1999/12/13 13:30:14 rgb
  41179. + * Changed MTU reports and HW address reporting back to debug only.
  41180. + *
  41181. + * Revision 1.92 1999/12/07 18:57:56 rgb
  41182. + * Fix PFKEY symbol compile error (SADB_*) without pfkey enabled.
  41183. + *
  41184. + * Revision 1.91 1999/12/01 22:15:36 rgb
  41185. + * Add checks for LARVAL and DEAD SAs.
  41186. + * Change state of SA from MATURE to DYING when a soft lifetime is
  41187. + * reached and print debug warning.
  41188. + *
  41189. + * Revision 1.90 1999/11/23 23:04:04 rgb
  41190. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  41191. + * Sort out pfkey and freeswan headers, putting them in a library path.
  41192. + *
  41193. + * Revision 1.89 1999/11/18 18:50:59 rgb
  41194. + * Changed all device registrations for static linking to
  41195. + * dynamic to reduce the number and size of patches.
  41196. + *
  41197. + * Revision 1.88 1999/11/18 04:09:19 rgb
  41198. + * Replaced all kernel version macros to shorter, readable form.
  41199. + *
  41200. + * Revision 1.87 1999/11/17 15:53:40 rgb
  41201. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  41202. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  41203. + * klips/net/ipsec/Makefile.
  41204. + *
  41205. + * Revision 1.86 1999/10/16 18:25:37 rgb
  41206. + * Moved SA lifetime expiry checks before packet processing.
  41207. + * Expire SA on replay counter rollover.
  41208. + *
  41209. + * Revision 1.85 1999/10/16 04:24:31 rgb
  41210. + * Add stats for time since last packet.
  41211. + *
  41212. + * Revision 1.84 1999/10/16 00:30:47 rgb
  41213. + * Added SA lifetime counting.
  41214. + *
  41215. + * Revision 1.83 1999/10/15 22:15:57 rgb
  41216. + * Clean out cruft.
  41217. + * Add debugging.
  41218. + *
  41219. + * Revision 1.82 1999/10/08 18:26:19 rgb
  41220. + * Fix 2.0.3x outgoing fragmented packet memory leak.
  41221. + *
  41222. + * Revision 1.81 1999/10/05 02:38:54 rgb
  41223. + * Lower the default mtu of virtual devices to 16260.
  41224. + *
  41225. + * Revision 1.80 1999/10/03 18:56:41 rgb
  41226. + * Spinlock support for 2.3.xx.
  41227. + * Don't forget to undo spinlocks on error!
  41228. + * Check for valid eroute before copying the structure.
  41229. + *
  41230. + * Revision 1.79 1999/10/01 15:44:53 rgb
  41231. + * Move spinlock header include to 2.1> scope.
  41232. + *
  41233. + * Revision 1.78 1999/10/01 00:02:43 rgb
  41234. + * Added tdb structure locking.
  41235. + * Added eroute structure locking.
  41236. + *
  41237. + * Revision 1.77 1999/09/30 02:52:29 rgb
  41238. + * Add Marc Boucher's Copy-On-Write code (same as ipsec_rcv.c).
  41239. + *
  41240. + * Revision 1.76 1999/09/25 19:31:27 rgb
  41241. + * Refine MSS hack to affect SYN, but not SYN+ACK packets.
  41242. + *
  41243. + * Revision 1.75 1999/09/24 22:52:38 rgb
  41244. + * Fix two things broken in 2.0.38 by trying to fix network notifiers.
  41245. + *
  41246. + * Revision 1.74 1999/09/24 00:30:37 rgb
  41247. + * Add test for changed source as well as destination to check for
  41248. + * recursion.
  41249. + *
  41250. + * Revision 1.73 1999/09/23 20:52:24 rgb
  41251. + * Add James Morris' MSS hack patch, disabled.
  41252. + *
  41253. + * Revision 1.72 1999/09/23 20:22:40 rgb
  41254. + * Enable, tidy and fix network notifier code.
  41255. + *
  41256. + * Revision 1.71 1999/09/23 18:09:05 rgb
  41257. + * Clean up 2.2.x fragmenting traces.
  41258. + * Disable dev->type switching, forcing ARPHRD_TUNNEL.
  41259. + *
  41260. + * Revision 1.70 1999/09/22 14:14:24 rgb
  41261. + * Add sanity checks for revectored calls to prevent calling a downed I/F.
  41262. + *
  41263. + * Revision 1.69 1999/09/21 15:00:57 rgb
  41264. + * Add Marc Boucher's packet size check.
  41265. + * Flesh out network device notifier code.
  41266. + *
  41267. + * Revision 1.68 1999/09/18 11:39:57 rgb
  41268. + * Start to add (disabled) netdevice notifier code.
  41269. + *
  41270. + * Revision 1.67 1999/09/17 23:44:40 rgb
  41271. + * Add a comment warning potential code hackers to stay away from mac.raw.
  41272. + *
  41273. + * Revision 1.66 1999/09/17 18:04:02 rgb
  41274. + * Add fix for unpredictable hard_header_len for ISDN folks (thanks MB).
  41275. + * Ditch TTL decrement in 2.2 (MB).
  41276. + *
  41277. + * Revision 1.65 1999/09/15 23:15:35 henry
  41278. + * Marc Boucher's PPP fixes
  41279. + *
  41280. + * Revision 1.64 1999/09/07 13:40:53 rgb
  41281. + * Ditch unreliable references to skb->mac.raw.
  41282. + *
  41283. + * Revision 1.63 1999/08/28 11:33:09 rgb
  41284. + * Check for null skb->mac pointer.
  41285. + *
  41286. + * Revision 1.62 1999/08/28 02:02:30 rgb
  41287. + * Add Marc Boucher's fix for properly dealing with skb->sk.
  41288. + *
  41289. + * Revision 1.61 1999/08/27 05:23:05 rgb
  41290. + * Clean up skb->data/raw/nh/h manipulation.
  41291. + * Add Marc Boucher's mods to aid tcpdump.
  41292. + * Add sanity checks to skb->raw/nh/h pointer copies in skb_copy_expand.
  41293. + * Re-order hard_header stripping -- might be able to remove it...
  41294. + *
  41295. + * Revision 1.60 1999/08/26 20:01:02 rgb
  41296. + * Tidy up compiler directives and macros.
  41297. + * Re-enable ICMP for tunnels where inner_dst != outer_dst.
  41298. + * Remove unnecessary skb->dev = physdev assignment affecting 2.2.x.
  41299. + *
  41300. + * Revision 1.59 1999/08/25 15:44:41 rgb
  41301. + * Clean up from 2.2.x instrumenting for compilation under 2.0.36.
  41302. + *
  41303. + * Revision 1.58 1999/08/25 15:00:54 rgb
  41304. + * Add dst cache code for 2.2.xx.
  41305. + * Add sanity check for skb packet header pointers.
  41306. + * Add/modify debugging instrumentation to *_start_xmit, *_hard_header and
  41307. + * *_rebuild_header.
  41308. + * Add neigh_* cache code.
  41309. + * Change dev->type back to ARPHRD_TUNNEL.
  41310. + *
  41311. + * Revision 1.57 1999/08/17 21:50:23 rgb
  41312. + * Fixed minor debug output bugs.
  41313. + * Regrouped error recovery exit code.
  41314. + * Added compiler directives to remove unwanted code and symbols.
  41315. + * Shut off ICMP messages: to be refined to only send ICMP to remote systems.
  41316. + * Add debugging code for output function addresses.
  41317. + * Fix minor bug in (possibly unused) header_cache_bind function.
  41318. + * Add device neighbour caching code.
  41319. + * Change dev->type from ARPHRD_TUNNEL to physdev->type.
  41320. + *
  41321. + * Revision 1.56 1999/08/03 17:22:56 rgb
  41322. + * Debug output clarification using KERN_* macros. Other inactive changes
  41323. + * added.
  41324. + *
  41325. + * Revision 1.55 1999/08/03 16:58:46 rgb
  41326. + * Fix skb_copy_expand size bug. Was getting incorrect size.
  41327. + *
  41328. + * Revision 1.54 1999/07/14 19:32:38 rgb
  41329. + * Fix oversize packet crash and ssh stalling in 2.2.x kernels.
  41330. + *
  41331. + * Revision 1.53 1999/06/10 15:44:02 rgb
  41332. + * Minor reformatting and clean-up.
  41333. + *
  41334. + * Revision 1.52 1999/05/09 03:25:36 rgb
  41335. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  41336. + *
  41337. + * Revision 1.51 1999/05/08 21:24:59 rgb
  41338. + * Add casting to silence the 2.2.x compile.
  41339. + *
  41340. + * Revision 1.50 1999/05/05 22:02:32 rgb
  41341. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  41342. + *
  41343. + * Revision 1.49 1999/04/29 15:18:52 rgb
  41344. + * Change gettdb parameter to a pointer to reduce stack loading and
  41345. + * facilitate parameter sanity checking.
  41346. + * Fix undetected bug that might have tried to access a null pointer.
  41347. + * Eliminate unnessessary usage of tdb_xform member to further switch
  41348. + * away from the transform switch to the algorithm switch.
  41349. + * Add return values to init and cleanup functions.
  41350. + *
  41351. + * Revision 1.48 1999/04/16 15:38:00 rgb
  41352. + * Minor rearrangement of freeing code to avoid memory leaks with impossible or
  41353. + * rare situations.
  41354. + *
  41355. + * Revision 1.47 1999/04/15 15:37:25 rgb
  41356. + * Forward check changes from POST1_00 branch.
  41357. + *
  41358. + * Revision 1.32.2.4 1999/04/13 21:00:18 rgb
  41359. + * Ditch 'things I wish I had known before...'.
  41360. + *
  41361. + * Revision 1.32.2.3 1999/04/13 20:34:38 rgb
  41362. + * Free skb after fragmentation.
  41363. + * Use stats more effectively.
  41364. + * Add I/F to mtu notch-down reporting.
  41365. + *
  41366. + * Revision 1.32.2.2 1999/04/02 04:26:14 rgb
  41367. + * Backcheck from HEAD, pre1.0.
  41368. + *
  41369. + * Revision 1.46 1999/04/11 00:29:00 henry
  41370. + * GPL boilerplate
  41371. + *
  41372. + * Revision 1.45 1999/04/07 15:42:01 rgb
  41373. + * Fix mtu/ping bug AGAIN!
  41374. + *
  41375. + * Revision 1.44 1999/04/06 04:54:27 rgb
  41376. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  41377. + * patch shell fixes.
  41378. + *
  41379. + * Revision 1.43 1999/04/04 03:57:07 rgb
  41380. + * ip_fragment() doesn't free the supplied skb. Freed.
  41381. + *
  41382. + * Revision 1.42 1999/04/01 23:27:15 rgb
  41383. + * Preload size of virtual mtu.
  41384. + *
  41385. + * Revision 1.41 1999/04/01 09:31:23 rgb
  41386. + * Invert meaning of ICMP PMTUD config option and clarify.
  41387. + * Code clean-up.
  41388. + *
  41389. + * Revision 1.40 1999/04/01 04:37:17 rgb
  41390. + * SSH stalling bug fix.
  41391. + *
  41392. + * Revision 1.39 1999/03/31 23:44:28 rgb
  41393. + * Don't send ICMP on DF and frag_off.
  41394. + *
  41395. + * Revision 1.38 1999/03/31 15:20:10 rgb
  41396. + * Quiet down debugging.
  41397. + *
  41398. + * Revision 1.37 1999/03/31 08:30:31 rgb
  41399. + * Add switch to shut off ICMP PMTUD packets.
  41400. + *
  41401. + * Revision 1.36 1999/03/31 05:44:47 rgb
  41402. + * Keep PMTU reduction private.
  41403. + *
  41404. + * Revision 1.35 1999/03/27 15:13:02 rgb
  41405. + * PMTU/fragmentation bug fix.
  41406. + *
  41407. + * Revision 1.34 1999/03/17 21:19:26 rgb
  41408. + * Fix kmalloc nonatomic bug.
  41409. + *
  41410. + * Revision 1.33 1999/03/17 15:38:42 rgb
  41411. + * Code clean-up.
  41412. + * ESP_NULL IV bug fix.
  41413. + *
  41414. + * Revision 1.32 1999/03/01 20:44:25 rgb
  41415. + * Code clean-up.
  41416. + * Memory leak bug fix.
  41417. + *
  41418. + * Revision 1.31 1999/02/27 00:02:09 rgb
  41419. + * Tune to report the MTU reduction once, rather than after every recursion
  41420. + * through the encapsulating code, preventing tcp stream stalling.
  41421. + *
  41422. + * Revision 1.30 1999/02/24 20:21:01 rgb
  41423. + * Reformat debug printk's.
  41424. + * Fix recursive encapsulation, dynamic MTU bugs and add debugging code.
  41425. + * Clean-up.
  41426. + *
  41427. + * Revision 1.29 1999/02/22 17:08:14 rgb
  41428. + * Fix recursive encapsulation code.
  41429. + *
  41430. + * Revision 1.28 1999/02/19 18:27:02 rgb
  41431. + * Improve DF, fragmentation and PMTU behaviour and add dynamic MTU discovery.
  41432. + *
  41433. + * Revision 1.27 1999/02/17 16:51:37 rgb
  41434. + * Clean out unused cruft.
  41435. + * Temporarily tone down volume of debug output.
  41436. + * Temporarily shut off fragment rejection.
  41437. + * Disabled temporary failed recursive encapsulation loop.
  41438. + *
  41439. + * Revision 1.26 1999/02/12 21:21:26 rgb
  41440. + * Move KLIPS_PRINT to ipsec_netlink.h for accessibility.
  41441. + *
  41442. + * Revision 1.25 1999/02/11 19:38:27 rgb
  41443. + * More clean-up.
  41444. + * Add sanity checking for skb_copy_expand() to prevent kernel panics on
  41445. + * skb_put() values out of range.
  41446. + * Fix head/tailroom calculation causing skb_put() out-of-range values.
  41447. + * Fix return values to prevent 'nonatomic alloc_skb' warnings.
  41448. + * Allocate new skb iff needed.
  41449. + * Added more debug statements.
  41450. + * Make headroom depend on structure, not hard-coded values.
  41451. + *
  41452. + * Revision 1.24 1999/02/10 23:20:33 rgb
  41453. + * Shut up annoying 'statement has no effect' compiler warnings with
  41454. + * debugging compiled out.
  41455. + *
  41456. + * Revision 1.23 1999/02/10 22:36:30 rgb
  41457. + * Clean-up obsolete, unused and messy code.
  41458. + * Converted most IPSEC_DEBUG statements to KLIPS_PRINT macros.
  41459. + * Rename ipsec_tunnel_do_xmit to ipsec_tunnel_start_xmit and eliminated
  41460. + * original ipsec_tunnel_start_xmit.
  41461. + * Send all packet with different inner and outer destinations directly to
  41462. + * the attached physical device, rather than back through ip_forward,
  41463. + * preventing disappearing routes problems.
  41464. + * Do sanity checking before investing too much CPU in allocating new
  41465. + * structures.
  41466. + * Fail on IP header options: We cannot process them yet.
  41467. + * Add some helpful comments.
  41468. + * Use virtual device for parameters instead of physical device.
  41469. + *
  41470. + * Revision 1.22 1999/02/10 03:03:02 rgb
  41471. + * Duh. Fixed the TTL bug: forgot to update the checksum.
  41472. + *
  41473. + * Revision 1.21 1999/02/09 23:17:53 rgb
  41474. + * Add structure members to ipsec_print_ip debug function.
  41475. + * Temporarily fix TTL bug preventing tunnel mode from functioning.
  41476. + *
  41477. + * Revision 1.20 1999/02/09 00:14:25 rgb
  41478. + * Add KLIPSPRINT macro. (Not used yet, though.)
  41479. + * Delete old ip_tunnel code (BADCODE).
  41480. + * Decrement TTL in outgoing packet.
  41481. + * Set TTL on new IPIP_TUNNEL to default, not existing packet TTL.
  41482. + * Delete ethernet only feature and fix hard-coded hard_header_len.
  41483. + *
  41484. + * Revision 1.19 1999/01/29 17:56:22 rgb
  41485. + * 64-bit re-fix submitted by Peter Onion.
  41486. + *
  41487. + * Revision 1.18 1999/01/28 22:43:24 rgb
  41488. + * Fixed bug in ipsec_print_ip that caused an OOPS, found by P.Onion.
  41489. + *
  41490. + * Revision 1.17 1999/01/26 02:08:16 rgb
  41491. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  41492. + * Removed dead code.
  41493. + *
  41494. + * Revision 1.16 1999/01/22 06:25:26 rgb
  41495. + * Cruft clean-out.
  41496. + * Added algorithm switch code.
  41497. + * 64-bit clean-up.
  41498. + * Passthrough on IPIP protocol, spi 0x0 fix.
  41499. + * Enhanced debugging.
  41500. + *
  41501. + * Revision 1.15 1998/12/01 13:22:04 rgb
  41502. + * Added support for debug printing of version info.
  41503. + *
  41504. + * Revision 1.14 1998/11/30 13:22:55 rgb
  41505. + * Rationalised all the klips kernel file headers. They are much shorter
  41506. + * now and won't conflict under RH5.2.
  41507. + *
  41508. + * Revision 1.13 1998/11/17 21:13:52 rgb
  41509. + * Put IKE port bypass debug output in user-switched debug statements.
  41510. + *
  41511. + * Revision 1.12 1998/11/13 13:20:25 rgb
  41512. + * Fixed ntohs bug in udp/500 hole for IKE.
  41513. + *
  41514. + * Revision 1.11 1998/11/10 08:01:19 rgb
  41515. + * Kill tcp/500 hole, keep udp/500 hole.
  41516. + *
  41517. + * Revision 1.10 1998/11/09 21:29:26 rgb
  41518. + * If no eroute is found, discard packet and incr. tx_error.
  41519. + *
  41520. + * Revision 1.9 1998/10/31 06:50:00 rgb
  41521. + * Add tcp/udp/500 bypass.
  41522. + * Fixed up comments in #endif directives.
  41523. + *
  41524. + * Revision 1.8 1998/10/27 00:34:31 rgb
  41525. + * Reformat debug output of IP headers.
  41526. + * Newlines added before calls to ipsec_print_ip.
  41527. + *
  41528. + * Revision 1.7 1998/10/19 14:44:28 rgb
  41529. + * Added inclusion of freeswan.h.
  41530. + * sa_id structure implemented and used: now includes protocol.
  41531. + *
  41532. + * Revision 1.6 1998/10/09 04:31:35 rgb
  41533. + * Added 'klips_debug' prefix to all klips printk debug statements.
  41534. + *
  41535. + * Revision 1.5 1998/08/28 03:09:51 rgb
  41536. + * Prevent kernel log spam with default route through ipsec.
  41537. + *
  41538. + * Revision 1.4 1998/08/05 22:23:09 rgb
  41539. + * Change setdev return code to ENXIO for a non-existant physical device.
  41540. + *
  41541. + * Revision 1.3 1998/07/29 20:41:11 rgb
  41542. + * Add ipsec_tunnel_clear to clear all tunnel attachments.
  41543. + *
  41544. + * Revision 1.2 1998/06/25 20:00:33 rgb
  41545. + * Clean up #endif comments.
  41546. + * Rename dev_ipsec to dev_ipsec0 for consistency.
  41547. + * Document ipsec device fields.
  41548. + * Make ipsec_tunnel_probe visible from rest of kernel for static linking.
  41549. + * Get debugging report for *every* ipsec device initialisation.
  41550. + * Comment out redundant code.
  41551. + *
  41552. + * Revision 1.1 1998/06/18 21:27:50 henry
  41553. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  41554. + * kernel-build scripts happier in the presence of symlinks
  41555. + *
  41556. + * Revision 1.8 1998/06/14 23:49:40 rgb
  41557. + * Clarify version reporting on module loading.
  41558. + *
  41559. + * Revision 1.7 1998/05/27 23:19:20 rgb
  41560. + * Added version reporting.
  41561. + *
  41562. + * Revision 1.6 1998/05/18 21:56:23 rgb
  41563. + * Clean up for numerical consistency of output and cleaning up debug code.
  41564. + *
  41565. + * Revision 1.5 1998/05/12 02:44:23 rgb
  41566. + * Clarifying 'no e-route to host' message.
  41567. + *
  41568. + * Revision 1.4 1998/04/30 15:34:35 rgb
  41569. + * Enclosed most remaining debugging statements in #ifdef's to make it quieter.
  41570. + *
  41571. + * Revision 1.3 1998/04/21 21:28:54 rgb
  41572. + * Rearrange debug switches to change on the fly debug output from user
  41573. + * space. Only kernel changes checked in at this time. radij.c was also
  41574. + * changed to temporarily remove buggy debugging code in rj_delete causing
  41575. + * an OOPS and hence, netlink device open errors.
  41576. + *
  41577. + * Revision 1.2 1998/04/12 22:03:24 rgb
  41578. + * Updated ESP-3DES-HMAC-MD5-96,
  41579. + * ESP-DES-HMAC-MD5-96,
  41580. + * AH-HMAC-MD5-96,
  41581. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  41582. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  41583. + *
  41584. + * Fixed eroute references in /proc/net/ipsec*.
  41585. + *
  41586. + * Started to patch module unloading memory leaks in ipsec_netlink and
  41587. + * radij tree unloading.
  41588. + *
  41589. + * Revision 1.1 1998/04/09 03:06:12 henry
  41590. + * sources moved up from linux/net/ipsec
  41591. + *
  41592. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  41593. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  41594. + *
  41595. + * Revision 0.5 1997/06/03 04:24:48 ji
  41596. + * Added transport mode.
  41597. + * Changed the way routing is done.
  41598. + * Lots of bug fixes.
  41599. + *
  41600. + * Revision 0.4 1997/01/15 01:28:15 ji
  41601. + * No changes.
  41602. + *
  41603. + * Revision 0.3 1996/11/20 14:39:04 ji
  41604. + * Minor cleanups.
  41605. + * Rationalized debugging code.
  41606. + *
  41607. + * Revision 0.2 1996/11/02 00:18:33 ji
  41608. + * First limited release.
  41609. + *
  41610. + * Local Variables:
  41611. + * c-style: linux
  41612. + * End:
  41613. + */
  41614. --- /dev/null Tue Mar 11 13:02:56 2003
  41615. +++ linux/net/ipsec/ipsec_xform.c Mon Feb 9 13:51:03 2004
  41616. @@ -0,0 +1,360 @@
  41617. +/*
  41618. + * Common routines for IPSEC transformations.
  41619. + * Copyright (C) 1996, 1997 John Ioannidis.
  41620. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  41621. + *
  41622. + * This program is free software; you can redistribute it and/or modify it
  41623. + * under the terms of the GNU General Public License as published by the
  41624. + * Free Software Foundation; either version 2 of the License, or (at your
  41625. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  41626. + *
  41627. + * This program is distributed in the hope that it will be useful, but
  41628. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  41629. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  41630. + * for more details.
  41631. + *
  41632. + * RCSID $Id: ipsec_xform.c,v 1.65.2.1 2006/10/06 21:39:26 paul Exp $
  41633. + */
  41634. +
  41635. +#ifndef AUTOCONF_INCLUDED
  41636. +#include <linux/config.h>
  41637. +#endif
  41638. +#include <linux/version.h>
  41639. +#include <linux/kernel.h> /* printk() */
  41640. +
  41641. +#include "freeswan/ipsec_param.h"
  41642. +
  41643. +#ifdef MALLOC_SLAB
  41644. +# include <linux/slab.h> /* kmalloc() */
  41645. +#else /* MALLOC_SLAB */
  41646. +# include <linux/malloc.h> /* kmalloc() */
  41647. +#endif /* MALLOC_SLAB */
  41648. +#include <linux/errno.h> /* error codes */
  41649. +#include <linux/types.h> /* size_t */
  41650. +#include <linux/interrupt.h> /* mark_bh */
  41651. +
  41652. +#include <linux/netdevice.h> /* struct device, and other headers */
  41653. +#include <linux/etherdevice.h> /* eth_type_trans */
  41654. +#include <linux/ip.h> /* struct iphdr */
  41655. +#include <linux/skbuff.h>
  41656. +#include <linux/random.h> /* get_random_bytes() */
  41657. +#include <freeswan.h>
  41658. +#ifdef SPINLOCK
  41659. +# ifdef SPINLOCK_23
  41660. +# include <linux/spinlock.h> /* *lock* */
  41661. +# else /* SPINLOCK_23 */
  41662. +# include <asm/spinlock.h> /* *lock* */
  41663. +# endif /* SPINLOCK_23 */
  41664. +#endif /* SPINLOCK */
  41665. +
  41666. +#include <net/ip.h>
  41667. +
  41668. +#include "freeswan/radij.h"
  41669. +#include "freeswan/ipsec_encap.h"
  41670. +#include "freeswan/ipsec_radij.h"
  41671. +#include "freeswan/ipsec_xform.h"
  41672. +#include "freeswan/ipsec_ipe4.h"
  41673. +#include "freeswan/ipsec_ah.h"
  41674. +#include "freeswan/ipsec_esp.h"
  41675. +
  41676. +#include <pfkeyv2.h>
  41677. +#include <pfkey.h>
  41678. +
  41679. +#ifdef CONFIG_KLIPS_DEBUG
  41680. +int debug_xform = 0;
  41681. +#endif /* CONFIG_KLIPS_DEBUG */
  41682. +
  41683. +#ifdef SPINLOCK
  41684. +spinlock_t tdb_lock = SPIN_LOCK_UNLOCKED;
  41685. +#else /* SPINLOCK */
  41686. +spinlock_t tdb_lock;
  41687. +#endif /* SPINLOCK */
  41688. +
  41689. +/*
  41690. + * $Log: ipsec_xform.c,v $
  41691. + * Revision 1.65.2.1 2006/10/06 21:39:26 paul
  41692. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  41693. + * set. This is defined through autoconf.h which is included through the
  41694. + * linux kernel build macros.
  41695. + *
  41696. + * Revision 1.65 2005/04/29 05:10:22 mcr
  41697. + * removed from extraenous includes to make unit testing easier.
  41698. + *
  41699. + * Revision 1.64 2004/07/10 19:11:18 mcr
  41700. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  41701. + *
  41702. + * Revision 1.63 2003/10/31 02:27:55 mcr
  41703. + * pulled up port-selector patches and sa_id elimination.
  41704. + *
  41705. + * Revision 1.62.30.1 2003/10/29 01:30:41 mcr
  41706. + * elimited "struct sa_id".
  41707. + *
  41708. + * Revision 1.62 2002/05/14 02:34:21 rgb
  41709. + * Delete stale code.
  41710. + *
  41711. + * Revision 1.61 2002/04/24 07:55:32 mcr
  41712. + * #include patches and Makefiles for post-reorg compilation.
  41713. + *
  41714. + * Revision 1.60 2002/04/24 07:36:33 mcr
  41715. + * Moved from ./klips/net/ipsec/ipsec_xform.c,v
  41716. + *
  41717. + * Revision 1.59 2002/03/29 15:01:36 rgb
  41718. + * Delete decommissioned code.
  41719. + *
  41720. + * Revision 1.58 2002/01/29 17:17:57 mcr
  41721. + * moved include of ipsec_param.h to after include of linux/kernel.h
  41722. + * otherwise, it seems that some option that is set in ipsec_param.h
  41723. + * screws up something subtle in the include path to kernel.h, and
  41724. + * it complains on the snprintf() prototype.
  41725. + *
  41726. + * Revision 1.57 2002/01/29 04:00:53 mcr
  41727. + * more excise of kversions.h header.
  41728. + *
  41729. + * Revision 1.56 2001/11/27 05:17:22 mcr
  41730. + * turn off the worst of the per-packet debugging.
  41731. + *
  41732. + * Revision 1.55 2001/11/26 09:23:50 rgb
  41733. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  41734. + *
  41735. + * Revision 1.54 2001/10/18 04:45:21 rgb
  41736. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  41737. + * lib/freeswan.h version macros moved to lib/kversions.h.
  41738. + * Other compiler directive cleanups.
  41739. + *
  41740. + * Revision 1.53 2001/09/08 21:13:34 rgb
  41741. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  41742. + *
  41743. + * Revision 1.52 2001/06/14 19:35:11 rgb
  41744. + * Update copyright date.
  41745. + *
  41746. + * Revision 1.51 2001/05/30 08:14:03 rgb
  41747. + * Removed vestiges of esp-null transforms.
  41748. + *
  41749. + * Revision 1.50 2001/05/03 19:43:18 rgb
  41750. + * Initialise error return variable.
  41751. + * Update SENDERR macro.
  41752. + * Fix sign of error return code for ipsec_tdbcleanup().
  41753. + * Use more appropriate return code for ipsec_tdbwipe().
  41754. + *
  41755. + * Revision 1.49 2001/04/19 18:56:17 rgb
  41756. + * Fixed tdb table locking comments.
  41757. + *
  41758. + * Revision 1.48 2001/02/27 22:24:55 rgb
  41759. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  41760. + * Check for satoa() return codes.
  41761. + *
  41762. + * Revision 1.47 2000/11/06 04:32:08 rgb
  41763. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  41764. + *
  41765. + * Revision 1.46 2000/09/20 16:21:57 rgb
  41766. + * Cleaned up ident string alloc/free.
  41767. + *
  41768. + * Revision 1.45 2000/09/08 19:16:51 rgb
  41769. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  41770. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  41771. + *
  41772. + * Revision 1.44 2000/08/30 05:29:04 rgb
  41773. + * Compiler-define out no longer used tdb_init() in ipsec_xform.c.
  41774. + *
  41775. + * Revision 1.43 2000/08/18 21:30:41 rgb
  41776. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  41777. + *
  41778. + * Revision 1.42 2000/08/01 14:51:51 rgb
  41779. + * Removed _all_ remaining traces of DES.
  41780. + *
  41781. + * Revision 1.41 2000/07/28 14:58:31 rgb
  41782. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  41783. + *
  41784. + * Revision 1.40 2000/06/28 05:50:11 rgb
  41785. + * Actually set iv_bits.
  41786. + *
  41787. + * Revision 1.39 2000/05/10 23:11:09 rgb
  41788. + * Added netlink debugging output.
  41789. + * Added a cast to quiet down the ntohl bug.
  41790. + *
  41791. + * Revision 1.38 2000/05/10 19:18:42 rgb
  41792. + * Cast output of ntohl so that the broken prototype doesn't make our
  41793. + * compile noisy.
  41794. + *
  41795. + * Revision 1.37 2000/03/16 14:04:59 rgb
  41796. + * Hardwired CONFIG_IPSEC_PFKEYv2 on.
  41797. + *
  41798. + * Revision 1.36 2000/01/26 10:11:28 rgb
  41799. + * Fixed spacing in error text causing run-in words.
  41800. + *
  41801. + * Revision 1.35 2000/01/21 06:17:16 rgb
  41802. + * Tidied up compiler directive indentation for readability.
  41803. + * Added ictx,octx vars for simplification.(kravietz)
  41804. + * Added macros for HMAC padding magic numbers.(kravietz)
  41805. + * Fixed missing key length reporting bug.
  41806. + * Fixed bug in tdbwipe to return immediately on NULL tdbp passed in.
  41807. + *
  41808. + * Revision 1.34 1999/12/08 00:04:19 rgb
  41809. + * Fixed SA direction overwriting bug for netlink users.
  41810. + *
  41811. + * Revision 1.33 1999/12/01 22:16:44 rgb
  41812. + * Minor formatting changes in ESP MD5 initialisation.
  41813. + *
  41814. + * Revision 1.32 1999/11/25 09:06:36 rgb
  41815. + * Fixed error return messages, should be returning negative numbers.
  41816. + * Implemented SENDERR macro for propagating error codes.
  41817. + * Added debug message and separate error code for algorithms not compiled
  41818. + * in.
  41819. + *
  41820. + * Revision 1.31 1999/11/23 23:06:26 rgb
  41821. + * Sort out pfkey and freeswan headers, putting them in a library path.
  41822. + *
  41823. + * Revision 1.30 1999/11/18 04:09:20 rgb
  41824. + * Replaced all kernel version macros to shorter, readable form.
  41825. + *
  41826. + * Revision 1.29 1999/11/17 15:53:40 rgb
  41827. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  41828. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  41829. + * klips/net/ipsec/Makefile.
  41830. + *
  41831. + * Revision 1.28 1999/10/18 20:04:01 rgb
  41832. + * Clean-out unused cruft.
  41833. + *
  41834. + * Revision 1.27 1999/10/03 19:01:03 rgb
  41835. + * Spinlock support for 2.3.xx and 2.0.xx kernels.
  41836. + *
  41837. + * Revision 1.26 1999/10/01 16:22:24 rgb
  41838. + * Switch from assignment init. to functional init. of spinlocks.
  41839. + *
  41840. + * Revision 1.25 1999/10/01 15:44:54 rgb
  41841. + * Move spinlock header include to 2.1> scope.
  41842. + *
  41843. + * Revision 1.24 1999/10/01 00:03:46 rgb
  41844. + * Added tdb structure locking.
  41845. + * Minor formatting changes.
  41846. + * Add function to initialize tdb hash table.
  41847. + *
  41848. + * Revision 1.23 1999/05/25 22:42:12 rgb
  41849. + * Add deltdbchain() debugging.
  41850. + *
  41851. + * Revision 1.22 1999/05/25 21:24:31 rgb
  41852. + * Add debugging statements to deltdbchain().
  41853. + *
  41854. + * Revision 1.21 1999/05/25 03:51:48 rgb
  41855. + * Refix error return code.
  41856. + *
  41857. + * Revision 1.20 1999/05/25 03:34:07 rgb
  41858. + * Fix error return for flush.
  41859. + *
  41860. + * Revision 1.19 1999/05/09 03:25:37 rgb
  41861. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  41862. + *
  41863. + * Revision 1.18 1999/05/05 22:02:32 rgb
  41864. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  41865. + *
  41866. + * Revision 1.17 1999/04/29 15:20:16 rgb
  41867. + * Change gettdb parameter to a pointer to reduce stack loading and
  41868. + * facilitate parameter sanity checking.
  41869. + * Add sanity checking for null pointer arguments.
  41870. + * Add debugging instrumentation.
  41871. + * Add function deltdbchain() which will take care of unlinking,
  41872. + * zeroing and deleting a chain of tdbs.
  41873. + * Add a parameter to tdbcleanup to be able to delete a class of SAs.
  41874. + * tdbwipe now actually zeroes the tdb as well as any of its pointed
  41875. + * structures.
  41876. + *
  41877. + * Revision 1.16 1999/04/16 15:36:29 rgb
  41878. + * Fix cut-and-paste error causing a memory leak in IPIP TDB freeing.
  41879. + *
  41880. + * Revision 1.15 1999/04/11 00:29:01 henry
  41881. + * GPL boilerplate
  41882. + *
  41883. + * Revision 1.14 1999/04/06 04:54:28 rgb
  41884. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  41885. + * patch shell fixes.
  41886. + *
  41887. + * Revision 1.13 1999/02/19 18:23:01 rgb
  41888. + * Nix debug off compile warning.
  41889. + *
  41890. + * Revision 1.12 1999/02/17 16:52:16 rgb
  41891. + * Consolidate satoa()s for space and speed efficiency.
  41892. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  41893. + * Clean out unused cruft.
  41894. + * Ditch NET_IPIP dependancy.
  41895. + * Loop for 3des key setting.
  41896. + *
  41897. + * Revision 1.11 1999/01/26 02:09:05 rgb
  41898. + * Remove ah/esp/IPIP switching on include files.
  41899. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  41900. + * Removed dead code.
  41901. + * Clean up debug code when switched off.
  41902. + * Remove references to INET_GET_PROTOCOL.
  41903. + * Added code exclusion macros to reduce code from unused algorithms.
  41904. + *
  41905. + * Revision 1.10 1999/01/22 06:28:55 rgb
  41906. + * Cruft clean-out.
  41907. + * Put random IV generation in kernel.
  41908. + * Added algorithm switch code.
  41909. + * Enhanced debugging.
  41910. + * 64-bit clean-up.
  41911. + *
  41912. + * Revision 1.9 1998/11/30 13:22:55 rgb
  41913. + * Rationalised all the klips kernel file headers. They are much shorter
  41914. + * now and won't conflict under RH5.2.
  41915. + *
  41916. + * Revision 1.8 1998/11/25 04:59:06 rgb
  41917. + * Add conditionals for no IPIP tunnel code.
  41918. + * Delete commented out code.
  41919. + *
  41920. + * Revision 1.7 1998/10/31 06:50:41 rgb
  41921. + * Convert xform ASCII names to no spaces.
  41922. + * Fixed up comments in #endif directives.
  41923. + *
  41924. + * Revision 1.6 1998/10/19 14:44:28 rgb
  41925. + * Added inclusion of freeswan.h.
  41926. + * sa_id structure implemented and used: now includes protocol.
  41927. + *
  41928. + * Revision 1.5 1998/10/09 04:32:19 rgb
  41929. + * Added 'klips_debug' prefix to all klips printk debug statements.
  41930. + *
  41931. + * Revision 1.4 1998/08/12 00:11:31 rgb
  41932. + * Added new xform functions to the xform table.
  41933. + * Fixed minor debug output spelling error.
  41934. + *
  41935. + * Revision 1.3 1998/07/09 17:45:31 rgb
  41936. + * Clarify algorithm not available message.
  41937. + *
  41938. + * Revision 1.2 1998/06/23 03:00:51 rgb
  41939. + * Check for presence of IPIP protocol if it is setup one way (we don't
  41940. + * know what has been set up the other way and can only assume it will be
  41941. + * symmetrical with the exception of keys).
  41942. + *
  41943. + * Revision 1.1 1998/06/18 21:27:51 henry
  41944. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  41945. + * kernel-build scripts happier in the presence of symlinks
  41946. + *
  41947. + * Revision 1.3 1998/06/11 05:54:59 rgb
  41948. + * Added transform version string pointer to xformsw initialisations.
  41949. + *
  41950. + * Revision 1.2 1998/04/21 21:28:57 rgb
  41951. + * Rearrange debug switches to change on the fly debug output from user
  41952. + * space. Only kernel changes checked in at this time. radij.c was also
  41953. + * changed to temporarily remove buggy debugging code in rj_delete causing
  41954. + * an OOPS and hence, netlink device open errors.
  41955. + *
  41956. + * Revision 1.1 1998/04/09 03:06:13 henry
  41957. + * sources moved up from linux/net/ipsec
  41958. + *
  41959. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  41960. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  41961. + *
  41962. + * Revision 0.5 1997/06/03 04:24:48 ji
  41963. + * Added ESP-3DES-MD5-96
  41964. + *
  41965. + * Revision 0.4 1997/01/15 01:28:15 ji
  41966. + * Added new transforms.
  41967. + *
  41968. + * Revision 0.3 1996/11/20 14:39:04 ji
  41969. + * Minor cleanups.
  41970. + * Rationalized debugging code.
  41971. + *
  41972. + * Revision 0.2 1996/11/02 00:18:33 ji
  41973. + * First limited release.
  41974. + *
  41975. + *
  41976. + */
  41977. --- /dev/null Tue Mar 11 13:02:56 2003
  41978. +++ linux/net/ipsec/ipsec_xmit.c Mon Feb 9 13:51:03 2004
  41979. @@ -0,0 +1,1850 @@
  41980. +/*
  41981. + * IPSEC Transmit code.
  41982. + * Copyright (C) 1996, 1997 John Ioannidis.
  41983. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  41984. + * Copyright (C) 2004-2005 Michael Richardson <mcr@xelerance.com>
  41985. + *
  41986. + * This program is free software; you can redistribute it and/or modify it
  41987. + * under the terms of the GNU General Public License as published by the
  41988. + * Free Software Foundation; either version 2 of the License, or (at your
  41989. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  41990. + *
  41991. + * This program is distributed in the hope that it will be useful, but
  41992. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  41993. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  41994. + * for more details.
  41995. + */
  41996. +
  41997. +char ipsec_xmit_c_version[] = "RCSID $Id: ipsec_xmit.c,v 1.20.2.8 2006/10/06 21:39:26 paul Exp $";
  41998. +
  41999. +#define __NO_VERSION__
  42000. +#include <linux/module.h>
  42001. +#ifndef AUTOCONF_INCLUDED
  42002. +#include <linux/config.h>
  42003. +#endif /* for CONFIG_IP_FORWARD */
  42004. +#include <linux/version.h>
  42005. +#include <linux/kernel.h> /* printk() */
  42006. +
  42007. +#include "openswan/ipsec_param.h"
  42008. +
  42009. +#ifdef MALLOC_SLAB
  42010. +# include <linux/slab.h> /* kmalloc() */
  42011. +#else /* MALLOC_SLAB */
  42012. +# include <linux/malloc.h> /* kmalloc() */
  42013. +#endif /* MALLOC_SLAB */
  42014. +#include <linux/errno.h> /* error codes */
  42015. +#include <linux/types.h> /* size_t */
  42016. +#include <linux/interrupt.h> /* mark_bh */
  42017. +
  42018. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  42019. +#include <linux/etherdevice.h> /* eth_type_trans */
  42020. +#include <linux/ip.h> /* struct iphdr */
  42021. +#include <linux/tcp.h> /* struct tcphdr */
  42022. +#include <linux/udp.h> /* struct udphdr */
  42023. +#include <linux/skbuff.h>
  42024. +#include <asm/uaccess.h>
  42025. +#include <asm/checksum.h>
  42026. +#include <openswan.h>
  42027. +#ifdef NET_21
  42028. +# define MSS_HACK_ /* experimental */
  42029. +# include <linux/in6.h>
  42030. +# include <net/dst.h>
  42031. +# define proto_priv cb
  42032. +#endif /* NET_21 */
  42033. +
  42034. +#include <net/icmp.h> /* icmp_send() */
  42035. +#include <net/ip.h>
  42036. +#ifdef NETDEV_23
  42037. +# include <linux/netfilter_ipv4.h>
  42038. +#endif /* NETDEV_23 */
  42039. +
  42040. +#include <linux/if_arp.h>
  42041. +#ifdef MSS_HACK
  42042. +# include <net/tcp.h> /* TCP options */
  42043. +#endif /* MSS_HACK */
  42044. +
  42045. +#include "openswan/radij.h"
  42046. +#include "openswan/ipsec_life.h"
  42047. +#include "openswan/ipsec_xform.h"
  42048. +#include "openswan/ipsec_eroute.h"
  42049. +#include "openswan/ipsec_encap.h"
  42050. +#include "openswan/ipsec_radij.h"
  42051. +#include "openswan/ipsec_xmit.h"
  42052. +#include "openswan/ipsec_sa.h"
  42053. +#include "openswan/ipsec_tunnel.h"
  42054. +#include "openswan/ipsec_ipe4.h"
  42055. +#include "openswan/ipsec_ah.h"
  42056. +#include "openswan/ipsec_esp.h"
  42057. +
  42058. +#ifdef CONFIG_KLIPS_IPCOMP
  42059. +#include "openswan/ipcomp.h"
  42060. +#endif /* CONFIG_KLIPS_IPCOMP */
  42061. +
  42062. +#include <pfkeyv2.h>
  42063. +#include <pfkey.h>
  42064. +
  42065. +#include "openswan/ipsec_proto.h"
  42066. +#include "openswan/ipsec_alg.h"
  42067. +
  42068. +
  42069. +/*
  42070. + * Stupid kernel API differences in APIs. Not only do some
  42071. + * kernels not have ip_select_ident, but some have differing APIs,
  42072. + * and SuSE has one with one parameter, but no way of checking to
  42073. + * see what is really what.
  42074. + */
  42075. +
  42076. +#ifdef SUSE_LINUX_2_4_19_IS_STUPID
  42077. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph)
  42078. +#else
  42079. +
  42080. +/* simplest case, nothing */
  42081. +#if !defined(IP_SELECT_IDENT)
  42082. +#define KLIPS_IP_SELECT_IDENT(iph, skb) do { iph->id = htons(ip_id_count++); } while(0)
  42083. +#endif
  42084. +
  42085. +/* kernels > 2.3.37-ish */
  42086. +#if defined(IP_SELECT_IDENT) && !defined(IP_SELECT_IDENT_NEW)
  42087. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst)
  42088. +#endif
  42089. +
  42090. +/* kernels > 2.4.2 */
  42091. +#if defined(IP_SELECT_IDENT) && defined(IP_SELECT_IDENT_NEW)
  42092. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst, NULL)
  42093. +#endif
  42094. +
  42095. +#endif /* SUSE_LINUX_2_4_19_IS_STUPID */
  42096. +
  42097. +
  42098. +
  42099. +#if defined(CONFIG_KLIPS_AH)
  42100. +static __u32 zeroes[64];
  42101. +#endif
  42102. +
  42103. +#ifdef CONFIG_KLIPS_DEBUG
  42104. +int sysctl_ipsec_debug_verbose = 0;
  42105. +#endif /* CONFIG_KLIPS_DEBUG */
  42106. +
  42107. +int ipsec_xmit_trap_count = 0;
  42108. +int ipsec_xmit_trap_sendcount = 0;
  42109. +
  42110. +int sysctl_ipsec_icmp = 0;
  42111. +int sysctl_ipsec_tos = 0;
  42112. +
  42113. +#ifdef CONFIG_KLIPS_DEBUG
  42114. +#define dmp(_x,_y,_z) if(debug_tunnel) ipsec_dmp_block(_x,_y,_z)
  42115. +#else /* CONFIG_KLIPS_DEBUG */
  42116. +#define dmp(_x, _y, _z)
  42117. +#endif /* CONFIG_KLIPS_DEBUG */
  42118. +
  42119. +
  42120. +#if !defined(SKB_COPY_EXPAND) || defined(KLIPS_UNIT_TESTS)
  42121. +/*
  42122. + * This is mostly skbuff.c:skb_copy().
  42123. + */
  42124. +struct sk_buff *
  42125. +skb_copy_expand(const struct sk_buff *skb, int headroom,
  42126. + int tailroom, int priority)
  42127. +{
  42128. + struct sk_buff *n;
  42129. + unsigned long offset;
  42130. +
  42131. + /*
  42132. + * Do sanity checking
  42133. + */
  42134. + if((headroom < 0) || (tailroom < 0) || ((headroom+tailroom) < 0)) {
  42135. + printk(KERN_WARNING
  42136. + "klips_error:skb_copy_expand: "
  42137. + "Illegal negative head,tailroom %d,%d\n",
  42138. + headroom,
  42139. + tailroom);
  42140. + return NULL;
  42141. + }
  42142. + /*
  42143. + * Allocate the copy buffer
  42144. + */
  42145. +
  42146. +#ifndef NET_21
  42147. + IS_SKB(skb);
  42148. +#endif /* !NET_21 */
  42149. +
  42150. +
  42151. + n=alloc_skb(skb->end - skb->head + headroom + tailroom, priority);
  42152. +
  42153. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42154. + "klips_debug:skb_copy_expand: "
  42155. + "allocating %d bytes, head=0p%p data=0p%p tail=0p%p end=0p%p end-head=%d tail-data=%d\n",
  42156. + skb->end - skb->head + headroom + tailroom,
  42157. + skb->head,
  42158. + skb->data,
  42159. + skb->tail,
  42160. + skb->end,
  42161. + skb->end - skb->head,
  42162. + skb->tail - skb->data);
  42163. +
  42164. + if(n==NULL)
  42165. + return NULL;
  42166. +
  42167. + /*
  42168. + * Shift between the two data areas in bytes
  42169. + */
  42170. +
  42171. + /* Set the data pointer */
  42172. + skb_reserve(n,skb->data-skb->head+headroom);
  42173. + /* Set the tail pointer and length */
  42174. + if(skb_tailroom(n) < skb->len) {
  42175. + printk(KERN_WARNING "klips_error:skb_copy_expand: "
  42176. + "tried to skb_put %ld, %d available. This should never happen, please report.\n",
  42177. + (unsigned long int)skb->len,
  42178. + skb_tailroom(n));
  42179. + ipsec_kfree_skb(n);
  42180. + return NULL;
  42181. + }
  42182. + skb_put(n,skb->len);
  42183. +
  42184. + offset=n->head + headroom - skb->head;
  42185. +
  42186. + /* Copy the bytes */
  42187. + memcpy(n->head + headroom, skb->head,skb->end-skb->head);
  42188. +#ifdef NET_21
  42189. + n->csum=skb->csum;
  42190. + n->priority=skb->priority;
  42191. + n->dst=dst_clone(skb->dst);
  42192. + if(skb->nh.raw)
  42193. + n->nh.raw=skb->nh.raw+offset;
  42194. +#ifndef NETDEV_23
  42195. + n->is_clone=0;
  42196. +#endif /* NETDEV_23 */
  42197. + atomic_set(&n->users, 1);
  42198. + n->destructor = NULL;
  42199. +#ifdef HAVE_SOCK_SECURITY
  42200. + n->security=skb->security;
  42201. +#endif
  42202. +#else /* NET_21 */
  42203. + n->link3=NULL;
  42204. + n->when=skb->when;
  42205. + if(skb->ip_hdr)
  42206. + n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset);
  42207. + n->saddr=skb->saddr;
  42208. + n->daddr=skb->daddr;
  42209. + n->raddr=skb->raddr;
  42210. + n->seq=skb->seq;
  42211. + n->end_seq=skb->end_seq;
  42212. + n->ack_seq=skb->ack_seq;
  42213. + n->acked=skb->acked;
  42214. + n->free=1;
  42215. + n->arp=skb->arp;
  42216. + n->tries=0;
  42217. + n->lock=0;
  42218. + n->users=0;
  42219. +#endif /* NET_21 */
  42220. + n->protocol=skb->protocol;
  42221. + n->list=NULL;
  42222. + n->sk=NULL;
  42223. + n->dev=skb->dev;
  42224. + if(skb->h.raw)
  42225. + n->h.raw=skb->h.raw+offset;
  42226. + if(skb->mac.raw)
  42227. + n->mac.raw=skb->mac.raw+offset;
  42228. + memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
  42229. +#ifndef NETDEV_23
  42230. + n->used=skb->used;
  42231. +#endif /* !NETDEV_23 */
  42232. + n->pkt_type=skb->pkt_type;
  42233. + n->stamp=skb->stamp;
  42234. +
  42235. +#ifndef NET_21
  42236. + IS_SKB(n);
  42237. +#endif /* !NET_21 */
  42238. + return n;
  42239. +}
  42240. +#endif /* !SKB_COPY_EXPAND */
  42241. +
  42242. +#ifdef CONFIG_KLIPS_DEBUG
  42243. +void
  42244. +ipsec_print_ip(struct iphdr *ip)
  42245. +{
  42246. + char buf[ADDRTOA_BUF];
  42247. +
  42248. + printk(KERN_INFO "klips_debug: IP:");
  42249. + printk(" ihl:%d", ip->ihl << 2);
  42250. + printk(" ver:%d", ip->version);
  42251. + printk(" tos:%d", ip->tos);
  42252. + printk(" tlen:%d", ntohs(ip->tot_len));
  42253. + printk(" id:%d", ntohs(ip->id));
  42254. + printk(" %s%s%sfrag_off:%d",
  42255. + ip->frag_off & __constant_htons(IP_CE) ? "CE " : "",
  42256. + ip->frag_off & __constant_htons(IP_DF) ? "DF " : "",
  42257. + ip->frag_off & __constant_htons(IP_MF) ? "MF " : "",
  42258. + (ntohs(ip->frag_off) & IP_OFFSET) << 3);
  42259. + printk(" ttl:%d", ip->ttl);
  42260. + printk(" proto:%d", ip->protocol);
  42261. + if(ip->protocol == IPPROTO_UDP)
  42262. + printk(" (UDP)");
  42263. + if(ip->protocol == IPPROTO_TCP)
  42264. + printk(" (TCP)");
  42265. + if(ip->protocol == IPPROTO_ICMP)
  42266. + printk(" (ICMP)");
  42267. + if(ip->protocol == IPPROTO_ESP)
  42268. + printk(" (ESP)");
  42269. + if(ip->protocol == IPPROTO_AH)
  42270. + printk(" (AH)");
  42271. + if(ip->protocol == IPPROTO_COMP)
  42272. + printk(" (COMP)");
  42273. + printk(" chk:%d", ntohs(ip->check));
  42274. + addrtoa(*((struct in_addr*)(&ip->saddr)), 0, buf, sizeof(buf));
  42275. + printk(" saddr:%s", buf);
  42276. + if(ip->protocol == IPPROTO_UDP)
  42277. + printk(":%d",
  42278. + ntohs(((struct udphdr*)((caddr_t)ip + (ip->ihl << 2)))->source));
  42279. + if(ip->protocol == IPPROTO_TCP)
  42280. + printk(":%d",
  42281. + ntohs(((struct tcphdr*)((caddr_t)ip + (ip->ihl << 2)))->source));
  42282. + addrtoa(*((struct in_addr*)(&ip->daddr)), 0, buf, sizeof(buf));
  42283. + printk(" daddr:%s", buf);
  42284. + if(ip->protocol == IPPROTO_UDP)
  42285. + printk(":%d",
  42286. + ntohs(((struct udphdr*)((caddr_t)ip + (ip->ihl << 2)))->dest));
  42287. + if(ip->protocol == IPPROTO_TCP)
  42288. + printk(":%d",
  42289. + ntohs(((struct tcphdr*)((caddr_t)ip + (ip->ihl << 2)))->dest));
  42290. + if(ip->protocol == IPPROTO_ICMP)
  42291. + printk(" type:code=%d:%d",
  42292. + ((struct icmphdr*)((caddr_t)ip + (ip->ihl << 2)))->type,
  42293. + ((struct icmphdr*)((caddr_t)ip + (ip->ihl << 2)))->code);
  42294. + printk("\n");
  42295. +
  42296. + if(sysctl_ipsec_debug_verbose) {
  42297. + __u8 *c;
  42298. + int len = ntohs(ip->tot_len) - ip->ihl*4;
  42299. +
  42300. + c = ((__u8*)ip) + ip->ihl*4;
  42301. + ipsec_dmp_block("ip_print", c, len);
  42302. + }
  42303. +}
  42304. +#endif /* CONFIG_KLIPS_DEBUG */
  42305. +
  42306. +#ifdef MSS_HACK
  42307. +/*
  42308. + * Issues:
  42309. + * 1) Fragments arriving in the tunnel should probably be rejected.
  42310. + * 2) How does this affect syncookies, mss_cache, dst cache ?
  42311. + * 3) Path MTU discovery handling needs to be reviewed. For example,
  42312. + * if we receive an ICMP 'packet too big' message from an intermediate
  42313. + * router specifying it's next hop MTU, our stack may process this and
  42314. + * adjust the MSS without taking our AH/ESP overheads into account.
  42315. + */
  42316. +
  42317. +
  42318. +/*
  42319. + * Recaclulate checksum using differences between changed datum,
  42320. + * borrowed from netfilter.
  42321. + */
  42322. +DEBUG_NO_STATIC u_int16_t
  42323. +ipsec_fast_csum(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck)
  42324. +{
  42325. + u_int32_t diffs[] = { oldvalinv, newval };
  42326. + return csum_fold(csum_partial((char *)diffs, sizeof(diffs),
  42327. + oldcheck^0xFFFF));
  42328. +}
  42329. +
  42330. +/*
  42331. + * Determine effective MSS.
  42332. + *
  42333. + * Note that we assume that there is always an MSS option for our own
  42334. + * SYN segments, which is mentioned in tcp_syn_build_options(), kernel 2.2.x.
  42335. + * This could change, and we should probably parse TCP options instead.
  42336. + *
  42337. + */
  42338. +DEBUG_NO_STATIC u_int8_t
  42339. +ipsec_adjust_mss(struct sk_buff *skb, struct tcphdr *tcph, u_int16_t mtu)
  42340. +{
  42341. + u_int16_t oldmss, newmss;
  42342. + u_int32_t *mssp;
  42343. + struct sock *sk = skb->sk;
  42344. +
  42345. + newmss = tcp_sync_mss(sk, mtu);
  42346. + printk(KERN_INFO "klips: setting mss to %u\n", newmss);
  42347. + mssp = (u_int32_t *)tcph + sizeof(struct tcphdr) / sizeof(u_int32_t);
  42348. + oldmss = ntohl(*mssp) & 0x0000FFFF;
  42349. + *mssp = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | newmss);
  42350. + tcph->check = ipsec_fast_csum(htons(~oldmss),
  42351. + htons(newmss), tcph->check);
  42352. + return 1;
  42353. +}
  42354. +#endif /* MSS_HACK */
  42355. +
  42356. +/*
  42357. + * Sanity checks
  42358. + */
  42359. +enum ipsec_xmit_value
  42360. +ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs)
  42361. +{
  42362. +
  42363. + if (ixs->dev == NULL) {
  42364. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42365. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42366. + "No device associated with skb!\n" );
  42367. + return IPSEC_XMIT_NODEV;
  42368. + }
  42369. +
  42370. + ixs->prv = ixs->dev->priv;
  42371. + if (ixs->prv == NULL) {
  42372. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42373. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42374. + "Device has no private structure!\n" );
  42375. + return IPSEC_XMIT_NOPRIVDEV;
  42376. + }
  42377. +
  42378. + ixs->physdev = ixs->prv->dev;
  42379. + if (ixs->physdev == NULL) {
  42380. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42381. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42382. + "Device is not attached to physical device!\n" );
  42383. + return IPSEC_XMIT_NOPHYSDEV;
  42384. + }
  42385. +
  42386. + ixs->physmtu = ixs->physdev->mtu;
  42387. + ixs->cur_mtu = ixs->physdev->mtu;
  42388. + ixs->stats = (struct net_device_stats *) &(ixs->prv->mystats);
  42389. +
  42390. + return IPSEC_XMIT_OK;
  42391. +}
  42392. +
  42393. +enum ipsec_xmit_value
  42394. +ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs)
  42395. +{
  42396. + /*
  42397. + * Return if there is nothing to do. (Does this ever happen?) XXX
  42398. + */
  42399. + if (ixs->skb == NULL) {
  42400. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42401. + "klips_error:ipsec_xmit_sanity_check_skb: "
  42402. + "Nothing to do!\n" );
  42403. + return IPSEC_XMIT_NOSKB;
  42404. + }
  42405. +
  42406. + /* if skb was cloned (most likely due to a packet sniffer such as
  42407. + tcpdump being momentarily attached to the interface), make
  42408. + a copy of our own to modify */
  42409. + if(skb_cloned(ixs->skb)) {
  42410. + if
  42411. +#ifdef SKB_COW_NEW
  42412. + (skb_cow(ixs->skb, skb_headroom(ixs->skb)) != 0)
  42413. +#else /* SKB_COW_NEW */
  42414. + ((ixs->skb = skb_cow(ixs->skb, skb_headroom(ixs->skb))) == NULL)
  42415. +#endif /* SKB_COW_NEW */
  42416. + {
  42417. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42418. + "klips_error:ipsec_xmit_sanity_check_skb: "
  42419. + "skb_cow failed to allocate buffer, dropping.\n" );
  42420. + ixs->stats->tx_dropped++;
  42421. + return IPSEC_XMIT_ERRSKBALLOC;
  42422. + }
  42423. + }
  42424. +
  42425. + ixs->iph = ixs->skb->nh.iph;
  42426. +
  42427. + /* sanity check for IP version as we can't handle IPv6 right now */
  42428. + if (ixs->iph->version != 4) {
  42429. + KLIPS_PRINT(debug_tunnel,
  42430. + "klips_debug:ipsec_xmit_sanity_check_skb: "
  42431. + "found IP Version %d but cannot process other IP versions than v4.\n",
  42432. + ixs->iph->version); /* XXX */
  42433. + ixs->stats->tx_dropped++;
  42434. + return IPSEC_XMIT_NOIPV6;
  42435. + }
  42436. +
  42437. +#if IPSEC_DISALLOW_IPOPTIONS
  42438. + if ((ixs->iph->ihl << 2) != sizeof (struct iphdr)) {
  42439. + KLIPS_PRINT(debug_tunnel,
  42440. + "klips_debug:ipsec_xmit_sanity_check_skb: "
  42441. + "cannot process IP header options yet. May be mal-formed packet.\n"); /* XXX */
  42442. + ixs->stats->tx_dropped++;
  42443. + return IPSEC_XMIT_NOIPOPTIONS;
  42444. + }
  42445. +#endif /* IPSEC_DISALLOW_IPOPTIONS */
  42446. +
  42447. +#ifndef NET_21
  42448. + if (ixs->iph->ttl <= 0) {
  42449. + /* Tell the sender its packet died... */
  42450. + ICMP_SEND(ixs->skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0, ixs->physdev);
  42451. +
  42452. + KLIPS_PRINT(debug_tunnel, "klips_debug:ipsec_xmit_sanity_check_skb: "
  42453. + "TTL=0, too many hops!\n");
  42454. + ixs->stats->tx_dropped++;
  42455. + return IPSEC_XMIT_TTLEXPIRED;
  42456. + }
  42457. +#endif /* !NET_21 */
  42458. +
  42459. + return IPSEC_XMIT_OK;
  42460. +}
  42461. +
  42462. +enum ipsec_xmit_value
  42463. +ipsec_xmit_encap_once(struct ipsec_xmit_state *ixs)
  42464. +{
  42465. +#ifdef CONFIG_KLIPS_ESP
  42466. + struct esphdr *espp;
  42467. + unsigned char *idat, *pad;
  42468. + int authlen = 0, padlen = 0, i;
  42469. +#endif /* !CONFIG_KLIPS_ESP */
  42470. +#ifdef CONFIG_KLIPS_AH
  42471. + struct iphdr ipo;
  42472. + struct ahhdr *ahp;
  42473. +#endif /* CONFIG_KLIPS_AH */
  42474. +#if defined(CONFIG_KLIPS_AUTH_HMAC_MD5) || defined(CONFIG_KLIPS_AUTH_HMAC_SHA1)
  42475. + union {
  42476. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42477. + MD5_CTX md5;
  42478. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42479. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42480. + SHA1_CTX sha1;
  42481. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42482. + } tctx;
  42483. + __u8 hash[AH_AMAX];
  42484. +#endif /* defined(CONFIG_KLIPS_AUTH_HMAC_MD5) || defined(CONFIG_KLIPS_AUTH_HMACn_SHA1) */
  42485. + int headroom = 0, tailroom = 0, ilen = 0, len = 0;
  42486. + unsigned char *dat;
  42487. + int blocksize = 8; /* XXX: should be inside ixs --jjo */
  42488. + struct ipsec_alg_enc *ixt_e = NULL;
  42489. + struct ipsec_alg_auth *ixt_a = NULL;
  42490. +
  42491. + ixs->iphlen = ixs->iph->ihl << 2;
  42492. + ixs->pyldsz = ntohs(ixs->iph->tot_len) - ixs->iphlen;
  42493. + ixs->sa_len = satot(&ixs->ipsp->ips_said, 0, ixs->sa_txt, SATOT_BUF);
  42494. + KLIPS_PRINT(debug_tunnel & DB_TN_OXFS,
  42495. + "klips_debug:ipsec_xmit_encap_once: "
  42496. + "calling output for <%s%s%s>, SA:%s\n",
  42497. + IPS_XFORM_NAME(ixs->ipsp),
  42498. + ixs->sa_len ? ixs->sa_txt : " (error)");
  42499. +
  42500. + switch(ixs->ipsp->ips_said.proto) {
  42501. +#ifdef CONFIG_KLIPS_AH
  42502. + case IPPROTO_AH:
  42503. + headroom += sizeof(struct ahhdr);
  42504. + break;
  42505. +#endif /* CONFIG_KLIPS_AH */
  42506. +
  42507. +#ifdef CONFIG_KLIPS_ESP
  42508. + case IPPROTO_ESP:
  42509. + ixt_e=ixs->ipsp->ips_alg_enc;
  42510. + if (ixt_e) {
  42511. + blocksize = ixt_e->ixt_common.ixt_blocksize;
  42512. + headroom += ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  42513. + } else {
  42514. + ixs->stats->tx_errors++;
  42515. + return IPSEC_XMIT_ESP_BADALG;
  42516. + }
  42517. +
  42518. + ixt_a=ixs->ipsp->ips_alg_auth;
  42519. + if (ixt_a) {
  42520. + tailroom += AHHMAC_HASHLEN;
  42521. + } else
  42522. + switch(ixs->ipsp->ips_authalg) {
  42523. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42524. + case AH_MD5:
  42525. + authlen = AHHMAC_HASHLEN;
  42526. + break;
  42527. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42528. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42529. + case AH_SHA:
  42530. + authlen = AHHMAC_HASHLEN;
  42531. + break;
  42532. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42533. + case AH_NONE:
  42534. + break;
  42535. + default:
  42536. + ixs->stats->tx_errors++;
  42537. + return IPSEC_XMIT_ESP_BADALG;
  42538. + }
  42539. + tailroom += blocksize != 1 ?
  42540. + ((blocksize - ((ixs->pyldsz + 2) % blocksize)) % blocksize) + 2 :
  42541. + ((4 - ((ixs->pyldsz + 2) % 4)) % 4) + 2;
  42542. + tailroom += authlen;
  42543. + break;
  42544. +#endif /* CONFIG_KLIPS_ESP */
  42545. +
  42546. +#ifdef CONFIG_KLIPS_IPIP
  42547. + case IPPROTO_IPIP:
  42548. + headroom += sizeof(struct iphdr);
  42549. + ixs->iphlen = sizeof(struct iphdr);
  42550. + break;
  42551. +#endif /* !CONFIG_KLIPS_IPIP */
  42552. +
  42553. +#ifdef CONFIG_KLIPS_IPCOMP
  42554. + case IPPROTO_COMP:
  42555. + break;
  42556. +#endif /* CONFIG_KLIPS_IPCOMP */
  42557. +
  42558. + default:
  42559. + ixs->stats->tx_errors++;
  42560. + return IPSEC_XMIT_BADPROTO;
  42561. + }
  42562. +
  42563. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42564. + "klips_debug:ipsec_xmit_encap_once: "
  42565. + "pushing %d bytes, putting %d, proto %d.\n",
  42566. + headroom, tailroom, ixs->ipsp->ips_said.proto);
  42567. + if(skb_headroom(ixs->skb) < headroom) {
  42568. + printk(KERN_WARNING
  42569. + "klips_error:ipsec_xmit_encap_once: "
  42570. + "tried to skb_push headroom=%d, %d available. This should never happen, please report.\n",
  42571. + headroom, skb_headroom(ixs->skb));
  42572. + ixs->stats->tx_errors++;
  42573. + return IPSEC_XMIT_ESP_PUSHPULLERR;
  42574. + }
  42575. +
  42576. + dat = skb_push(ixs->skb, headroom);
  42577. + ilen = ixs->skb->len - tailroom;
  42578. + if(skb_tailroom(ixs->skb) < tailroom) {
  42579. + printk(KERN_WARNING
  42580. + "klips_error:ipsec_xmit_encap_once: "
  42581. + "tried to skb_put %d, %d available. This should never happen, please report.\n",
  42582. + tailroom, skb_tailroom(ixs->skb));
  42583. + ixs->stats->tx_errors++;
  42584. + return IPSEC_XMIT_ESP_PUSHPULLERR;
  42585. + }
  42586. + skb_put(ixs->skb, tailroom);
  42587. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42588. + "klips_debug:ipsec_xmit_encap_once: "
  42589. + "head,tailroom: %d,%d before xform.\n",
  42590. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  42591. + len = ixs->skb->len;
  42592. + if(len > 0xfff0) {
  42593. + printk(KERN_WARNING "klips_error:ipsec_xmit_encap_once: "
  42594. + "tot_len (%d) > 65520. This should never happen, please report.\n",
  42595. + len);
  42596. + ixs->stats->tx_errors++;
  42597. + return IPSEC_XMIT_BADLEN;
  42598. + }
  42599. + memmove((void *)dat, (void *)(dat + headroom), ixs->iphlen);
  42600. + ixs->iph = (struct iphdr *)dat;
  42601. + ixs->iph->tot_len = htons(ixs->skb->len);
  42602. +
  42603. + switch(ixs->ipsp->ips_said.proto) {
  42604. +#ifdef CONFIG_KLIPS_ESP
  42605. + case IPPROTO_ESP:
  42606. + espp = (struct esphdr *)(dat + ixs->iphlen);
  42607. + espp->esp_spi = ixs->ipsp->ips_said.spi;
  42608. + espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  42609. +
  42610. + if (!ixt_e) {
  42611. + ixs->stats->tx_errors++;
  42612. + return IPSEC_XMIT_ESP_BADALG;
  42613. + }
  42614. +
  42615. + idat = dat + ixs->iphlen + headroom;
  42616. + ilen = len - (ixs->iphlen + headroom + authlen);
  42617. +
  42618. + /* Self-describing padding */
  42619. + pad = &dat[len - tailroom];
  42620. + padlen = tailroom - 2 - authlen;
  42621. + for (i = 0; i < padlen; i++) {
  42622. + pad[i] = i + 1;
  42623. + }
  42624. + dat[len - authlen - 2] = padlen;
  42625. +
  42626. + dat[len - authlen - 1] = ixs->iph->protocol;
  42627. + ixs->iph->protocol = IPPROTO_ESP;
  42628. +#ifdef CONFIG_KLIPS_DEBUG
  42629. + if(debug_tunnel & DB_TN_ENCAP) {
  42630. + dmp("pre-encrypt", dat, len);
  42631. + }
  42632. +#endif
  42633. +
  42634. + /*
  42635. + * Do all operations here:
  42636. + * copy IV->ESP, encrypt, update ips IV
  42637. + *
  42638. + */
  42639. + {
  42640. + int ret;
  42641. + memcpy(espp->esp_iv,
  42642. + ixs->ipsp->ips_iv,
  42643. + ixs->ipsp->ips_iv_size);
  42644. + ret=ipsec_alg_esp_encrypt(ixs->ipsp,
  42645. + idat, ilen, espp->esp_iv,
  42646. + IPSEC_ALG_ENCRYPT);
  42647. +
  42648. + prng_bytes(&ipsec_prng,
  42649. + (char *)ixs->ipsp->ips_iv,
  42650. + ixs->ipsp->ips_iv_size);
  42651. + }
  42652. +
  42653. + if (ixt_a) {
  42654. + ipsec_alg_sa_esp_hash(ixs->ipsp,
  42655. + (caddr_t)espp, len - ixs->iphlen - authlen,
  42656. + &(dat[len - authlen]), authlen);
  42657. +
  42658. + } else
  42659. + switch(ixs->ipsp->ips_authalg) {
  42660. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42661. + case AH_MD5:
  42662. + dmp("espp", (char*)espp, len - ixs->iphlen - authlen);
  42663. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42664. + dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  42665. + osMD5Update(&tctx.md5, (caddr_t)espp, len - ixs->iphlen - authlen);
  42666. + dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  42667. + osMD5Final(hash, &tctx.md5);
  42668. + dmp("ictx hash", (char*)&hash, sizeof(hash));
  42669. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42670. + dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  42671. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  42672. + dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  42673. + osMD5Final(hash, &tctx.md5);
  42674. + dmp("octx hash", (char*)&hash, sizeof(hash));
  42675. + memcpy(&(dat[len - authlen]), hash, authlen);
  42676. +
  42677. + /* paranoid */
  42678. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  42679. + memset((caddr_t)hash, 0, sizeof(*hash));
  42680. + break;
  42681. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42682. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42683. + case AH_SHA:
  42684. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42685. + SHA1Update(&tctx.sha1, (caddr_t)espp, len - ixs->iphlen - authlen);
  42686. + SHA1Final(hash, &tctx.sha1);
  42687. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42688. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  42689. + SHA1Final(hash, &tctx.sha1);
  42690. + memcpy(&(dat[len - authlen]), hash, authlen);
  42691. +
  42692. + /* paranoid */
  42693. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  42694. + memset((caddr_t)hash, 0, sizeof(*hash));
  42695. + break;
  42696. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42697. + case AH_NONE:
  42698. + break;
  42699. + default:
  42700. + ixs->stats->tx_errors++;
  42701. + return IPSEC_XMIT_AH_BADALG;
  42702. + }
  42703. +#ifdef NET_21
  42704. + ixs->skb->h.raw = (unsigned char*)espp;
  42705. +#endif /* NET_21 */
  42706. + break;
  42707. +#endif /* !CONFIG_KLIPS_ESP */
  42708. +#ifdef CONFIG_KLIPS_AH
  42709. + case IPPROTO_AH:
  42710. + ahp = (struct ahhdr *)(dat + ixs->iphlen);
  42711. + ahp->ah_spi = ixs->ipsp->ips_said.spi;
  42712. + ahp->ah_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  42713. + ahp->ah_rv = 0;
  42714. + ahp->ah_nh = ixs->iph->protocol;
  42715. + ahp->ah_hl = (headroom >> 2) - sizeof(__u64)/sizeof(__u32);
  42716. + ixs->iph->protocol = IPPROTO_AH;
  42717. + dmp("ahp", (char*)ahp, sizeof(*ahp));
  42718. +
  42719. + ipo = *ixs->iph;
  42720. + ipo.tos = 0;
  42721. + ipo.frag_off = 0;
  42722. + ipo.ttl = 0;
  42723. + ipo.check = 0;
  42724. + dmp("ipo", (char*)&ipo, sizeof(ipo));
  42725. +
  42726. + switch(ixs->ipsp->ips_authalg) {
  42727. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42728. + case AH_MD5:
  42729. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42730. + dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  42731. + osMD5Update(&tctx.md5, (unsigned char *)&ipo, sizeof (struct iphdr));
  42732. + dmp("ictx+ipo", (char*)&tctx.md5, sizeof(tctx.md5));
  42733. + osMD5Update(&tctx.md5, (unsigned char *)ahp, headroom - sizeof(ahp->ah_data));
  42734. + dmp("ictx+ahp", (char*)&tctx.md5, sizeof(tctx.md5));
  42735. + osMD5Update(&tctx.md5, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  42736. + dmp("ictx+zeroes", (char*)&tctx.md5, sizeof(tctx.md5));
  42737. + osMD5Update(&tctx.md5, dat + ixs->iphlen + headroom, len - ixs->iphlen - headroom);
  42738. + dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  42739. + osMD5Final(hash, &tctx.md5);
  42740. + dmp("ictx hash", (char*)&hash, sizeof(hash));
  42741. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42742. + dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  42743. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  42744. + dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  42745. + osMD5Final(hash, &tctx.md5);
  42746. + dmp("octx hash", (char*)&hash, sizeof(hash));
  42747. +
  42748. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  42749. +
  42750. + /* paranoid */
  42751. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  42752. + memset((caddr_t)hash, 0, sizeof(*hash));
  42753. + break;
  42754. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42755. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42756. + case AH_SHA:
  42757. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42758. + SHA1Update(&tctx.sha1, (unsigned char *)&ipo, sizeof (struct iphdr));
  42759. + SHA1Update(&tctx.sha1, (unsigned char *)ahp, headroom - sizeof(ahp->ah_data));
  42760. + SHA1Update(&tctx.sha1, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  42761. + SHA1Update(&tctx.sha1, dat + ixs->iphlen + headroom, len - ixs->iphlen - headroom);
  42762. + SHA1Final(hash, &tctx.sha1);
  42763. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42764. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  42765. + SHA1Final(hash, &tctx.sha1);
  42766. +
  42767. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  42768. +
  42769. + /* paranoid */
  42770. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  42771. + memset((caddr_t)hash, 0, sizeof(*hash));
  42772. + break;
  42773. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42774. + default:
  42775. + ixs->stats->tx_errors++;
  42776. + return IPSEC_XMIT_AH_BADALG;
  42777. + }
  42778. +#ifdef NET_21
  42779. + ixs->skb->h.raw = (unsigned char*)ahp;
  42780. +#endif /* NET_21 */
  42781. + break;
  42782. +#endif /* CONFIG_KLIPS_AH */
  42783. +#ifdef CONFIG_KLIPS_IPIP
  42784. + case IPPROTO_IPIP:
  42785. + ixs->iph->version = 4;
  42786. + switch(sysctl_ipsec_tos) {
  42787. + case 0:
  42788. +#ifdef NET_21
  42789. + ixs->iph->tos = ixs->skb->nh.iph->tos;
  42790. +#else /* NET_21 */
  42791. + ixs->iph->tos = ixs->skb->ip_hdr->tos;
  42792. +#endif /* NET_21 */
  42793. + break;
  42794. + case 1:
  42795. + ixs->iph->tos = 0;
  42796. + break;
  42797. + default:
  42798. + break;
  42799. + }
  42800. + ixs->iph->ttl = SYSCTL_IPSEC_DEFAULT_TTL;
  42801. + ixs->iph->frag_off = 0;
  42802. + ixs->iph->saddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_s))->sin_addr.s_addr;
  42803. + ixs->iph->daddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_d))->sin_addr.s_addr;
  42804. + ixs->iph->protocol = IPPROTO_IPIP;
  42805. + ixs->iph->ihl = sizeof(struct iphdr) >> 2;
  42806. +
  42807. + KLIPS_IP_SELECT_IDENT(ixs->iph, ixs->skb);
  42808. +
  42809. + ixs->newdst = (__u32)ixs->iph->daddr;
  42810. + ixs->newsrc = (__u32)ixs->iph->saddr;
  42811. +
  42812. +#ifdef NET_21
  42813. + ixs->skb->h.ipiph = ixs->skb->nh.iph;
  42814. +#endif /* NET_21 */
  42815. + break;
  42816. +#endif /* !CONFIG_KLIPS_IPIP */
  42817. +#ifdef CONFIG_KLIPS_IPCOMP
  42818. + case IPPROTO_COMP:
  42819. + {
  42820. + unsigned int flags = 0;
  42821. +#ifdef CONFIG_KLIPS_DEBUG
  42822. + unsigned int old_tot_len = ntohs(ixs->iph->tot_len);
  42823. +#endif /* CONFIG_KLIPS_DEBUG */
  42824. + ixs->ipsp->ips_comp_ratio_dbytes += ntohs(ixs->iph->tot_len);
  42825. +
  42826. + ixs->skb = skb_compress(ixs->skb, ixs->ipsp, &flags);
  42827. +
  42828. +#ifdef NET_21
  42829. + ixs->iph = ixs->skb->nh.iph;
  42830. +#else /* NET_21 */
  42831. + ixs->iph = ixs->skb->ip_hdr;
  42832. +#endif /* NET_21 */
  42833. +
  42834. + ixs->ipsp->ips_comp_ratio_cbytes += ntohs(ixs->iph->tot_len);
  42835. +
  42836. +#ifdef CONFIG_KLIPS_DEBUG
  42837. + if (debug_tunnel & DB_TN_CROUT)
  42838. + {
  42839. + if (old_tot_len > ntohs(ixs->iph->tot_len))
  42840. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42841. + "klips_debug:ipsec_xmit_encap_once: "
  42842. + "packet shrunk from %d to %d bytes after compression, cpi=%04x (should be from spi=%08x, spi&0xffff=%04x.\n",
  42843. + old_tot_len, ntohs(ixs->iph->tot_len),
  42844. + ntohs(((struct ipcomphdr*)(((char*)ixs->iph) + ((ixs->iph->ihl) << 2)))->ipcomp_cpi),
  42845. + ntohl(ixs->ipsp->ips_said.spi),
  42846. + (__u16)(ntohl(ixs->ipsp->ips_said.spi) & 0x0000ffff));
  42847. + else
  42848. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42849. + "klips_debug:ipsec_xmit_encap_once: "
  42850. + "packet did not compress (flags = %d).\n",
  42851. + flags);
  42852. + }
  42853. +#endif /* CONFIG_KLIPS_DEBUG */
  42854. + }
  42855. + break;
  42856. +#endif /* CONFIG_KLIPS_IPCOMP */
  42857. + default:
  42858. + ixs->stats->tx_errors++;
  42859. + return IPSEC_XMIT_BADPROTO;
  42860. + }
  42861. +
  42862. +#ifdef NET_21
  42863. + ixs->skb->nh.raw = ixs->skb->data;
  42864. +#else /* NET_21 */
  42865. + ixs->skb->ip_hdr = ixs->skb->h.iph = (struct iphdr *) ixs->skb->data;
  42866. +#endif /* NET_21 */
  42867. + ixs->iph->check = 0;
  42868. + ixs->iph->check = ip_fast_csum((unsigned char *)ixs->iph, ixs->iph->ihl);
  42869. +
  42870. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42871. + "klips_debug:ipsec_xmit_encap_once: "
  42872. + "after <%s%s%s>, SA:%s:\n",
  42873. + IPS_XFORM_NAME(ixs->ipsp),
  42874. + ixs->sa_len ? ixs->sa_txt : " (error)");
  42875. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->iph);
  42876. +
  42877. + ixs->ipsp->ips_life.ipl_bytes.ipl_count += len;
  42878. + ixs->ipsp->ips_life.ipl_bytes.ipl_last = len;
  42879. +
  42880. + if(!ixs->ipsp->ips_life.ipl_usetime.ipl_count) {
  42881. + ixs->ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  42882. + }
  42883. + ixs->ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  42884. + ixs->ipsp->ips_life.ipl_packets.ipl_count++;
  42885. +
  42886. + ixs->ipsp = ixs->ipsp->ips_onext;
  42887. +
  42888. + return IPSEC_XMIT_OK;
  42889. +}
  42890. +
  42891. +/*
  42892. + * If the IP packet (iph) is a carrying TCP/UDP, then set the encaps
  42893. + * source and destination ports to those from the TCP/UDP header.
  42894. + */
  42895. +void ipsec_extract_ports(struct iphdr * iph, struct sockaddr_encap * er)
  42896. +{
  42897. + struct udphdr *udp;
  42898. +
  42899. + switch (iph->protocol) {
  42900. + case IPPROTO_UDP:
  42901. + case IPPROTO_TCP:
  42902. + /*
  42903. + * The ports are at the same offsets in a TCP and UDP
  42904. + * header so hack it ...
  42905. + */
  42906. + udp = (struct udphdr*)(((char*)iph)+(iph->ihl<<2));
  42907. + er->sen_sport = udp->source;
  42908. + er->sen_dport = udp->dest;
  42909. + break;
  42910. + default:
  42911. + er->sen_sport = 0;
  42912. + er->sen_dport = 0;
  42913. + break;
  42914. + }
  42915. +}
  42916. +
  42917. +/*
  42918. + * A TRAP eroute is installed and we want to replace it with a HOLD
  42919. + * eroute.
  42920. + */
  42921. +static int create_hold_eroute(struct eroute *origtrap,
  42922. + struct sk_buff * skb, struct iphdr * iph,
  42923. + uint32_t eroute_pid)
  42924. +{
  42925. + struct eroute hold_eroute;
  42926. + ip_said hold_said;
  42927. + struct sk_buff *first, *last;
  42928. + int error;
  42929. +
  42930. + first = last = NULL;
  42931. + memset((caddr_t)&hold_eroute, 0, sizeof(hold_eroute));
  42932. + memset((caddr_t)&hold_said, 0, sizeof(hold_said));
  42933. +
  42934. + hold_said.proto = IPPROTO_INT;
  42935. + hold_said.spi = htonl(SPI_HOLD);
  42936. + hold_said.dst.u.v4.sin_addr.s_addr = INADDR_ANY;
  42937. +
  42938. + hold_eroute.er_eaddr.sen_len = sizeof(struct sockaddr_encap);
  42939. + hold_eroute.er_emask.sen_len = sizeof(struct sockaddr_encap);
  42940. + hold_eroute.er_eaddr.sen_family = AF_ENCAP;
  42941. + hold_eroute.er_emask.sen_family = AF_ENCAP;
  42942. + hold_eroute.er_eaddr.sen_type = SENT_IP4;
  42943. + hold_eroute.er_emask.sen_type = 255;
  42944. +
  42945. + hold_eroute.er_eaddr.sen_ip_src.s_addr = iph->saddr;
  42946. + hold_eroute.er_eaddr.sen_ip_dst.s_addr = iph->daddr;
  42947. + hold_eroute.er_emask.sen_ip_src.s_addr = INADDR_BROADCAST;
  42948. + hold_eroute.er_emask.sen_ip_dst.s_addr = INADDR_BROADCAST;
  42949. + hold_eroute.er_emask.sen_sport = 0;
  42950. + hold_eroute.er_emask.sen_dport = 0;
  42951. + hold_eroute.er_pid = eroute_pid;
  42952. + hold_eroute.er_count = 0;
  42953. + hold_eroute.er_lasttime = jiffies/HZ;
  42954. +
  42955. + /*
  42956. + * if it wasn't captured by a wildcard, then don't record it as
  42957. + * a wildcard.
  42958. + */
  42959. + if(origtrap->er_eaddr.sen_proto != 0) {
  42960. + hold_eroute.er_eaddr.sen_proto = iph->protocol;
  42961. +
  42962. + if((iph->protocol == IPPROTO_TCP ||
  42963. + iph->protocol == IPPROTO_UDP) &&
  42964. + (origtrap->er_eaddr.sen_sport != 0 ||
  42965. + origtrap->er_eaddr.sen_dport != 0)) {
  42966. +
  42967. + if(origtrap->er_eaddr.sen_sport != 0)
  42968. + hold_eroute.er_emask.sen_sport = ~0;
  42969. +
  42970. + if(origtrap->er_eaddr.sen_dport != 0)
  42971. + hold_eroute.er_emask.sen_dport = ~0;
  42972. +
  42973. + ipsec_extract_ports(iph, &hold_eroute.er_eaddr);
  42974. + }
  42975. + }
  42976. +
  42977. +#ifdef CONFIG_KLIPS_DEBUG
  42978. + if (debug_pfkey) {
  42979. + char buf1[64], buf2[64];
  42980. + subnettoa(hold_eroute.er_eaddr.sen_ip_src,
  42981. + hold_eroute.er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  42982. + subnettoa(hold_eroute.er_eaddr.sen_ip_dst,
  42983. + hold_eroute.er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  42984. + KLIPS_PRINT(debug_pfkey,
  42985. + "klips_debug:ipsec_tunnel_start_xmit: "
  42986. + "calling breakeroute and makeroute for %s:%d->%s:%d %d HOLD eroute.\n",
  42987. + buf1, ntohs(hold_eroute.er_eaddr.sen_sport),
  42988. + buf2, ntohs(hold_eroute.er_eaddr.sen_dport),
  42989. + hold_eroute.er_eaddr.sen_proto);
  42990. + }
  42991. +#endif /* CONFIG_KLIPS_DEBUG */
  42992. +
  42993. + if (ipsec_breakroute(&(hold_eroute.er_eaddr), &(hold_eroute.er_emask),
  42994. + &first, &last)) {
  42995. + KLIPS_PRINT(debug_pfkey,
  42996. + "klips_debug:ipsec_tunnel_start_xmit: "
  42997. + "HOLD breakeroute found nothing.\n");
  42998. + } else {
  42999. + KLIPS_PRINT(debug_pfkey,
  43000. + "klips_debug:ipsec_tunnel_start_xmit: "
  43001. + "HOLD breakroute deleted %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u %u\n",
  43002. + NIPQUAD(hold_eroute.er_eaddr.sen_ip_src),
  43003. + ntohs(hold_eroute.er_eaddr.sen_sport),
  43004. + NIPQUAD(hold_eroute.er_eaddr.sen_ip_dst),
  43005. + ntohs(hold_eroute.er_eaddr.sen_dport),
  43006. + hold_eroute.er_eaddr.sen_proto);
  43007. + }
  43008. + if (first != NULL)
  43009. + kfree_skb(first);
  43010. + if (last != NULL)
  43011. + kfree_skb(last);
  43012. +
  43013. + error = ipsec_makeroute(&(hold_eroute.er_eaddr),
  43014. + &(hold_eroute.er_emask),
  43015. + hold_said, eroute_pid, skb, NULL, NULL);
  43016. + if (error) {
  43017. + KLIPS_PRINT(debug_pfkey,
  43018. + "klips_debug:ipsec_tunnel_start_xmit: "
  43019. + "HOLD makeroute returned %d, failed.\n", error);
  43020. + } else {
  43021. + KLIPS_PRINT(debug_pfkey,
  43022. + "klips_debug:ipsec_tunnel_start_xmit: "
  43023. + "HOLD makeroute call successful.\n");
  43024. + }
  43025. + return (error == 0);
  43026. +}
  43027. +
  43028. +/*
  43029. + * upon entry to this function, ixs->skb should be setup
  43030. + * as follows:
  43031. + *
  43032. + * data = beginning of IP packet <- differs from ipsec_rcv().
  43033. + * nh.raw = beginning of IP packet.
  43034. + * h.raw = data after the IP packet.
  43035. + *
  43036. + */
  43037. +enum ipsec_xmit_value
  43038. +ipsec_xmit_encap_bundle(struct ipsec_xmit_state *ixs)
  43039. +{
  43040. + struct ipsec_alg_enc *ixt_e = NULL;
  43041. + struct ipsec_alg_auth *ixt_a = NULL;
  43042. + int blocksize = 8;
  43043. + enum ipsec_xmit_value bundle_stat = IPSEC_XMIT_OK;
  43044. +
  43045. + ixs->newdst = ixs->orgdst = ixs->iph->daddr;
  43046. + ixs->newsrc = ixs->orgsrc = ixs->iph->saddr;
  43047. + ixs->orgedst = ixs->outgoing_said.dst.u.v4.sin_addr.s_addr;
  43048. + ixs->iphlen = ixs->iph->ihl << 2;
  43049. + ixs->pyldsz = ntohs(ixs->iph->tot_len) - ixs->iphlen;
  43050. + ixs->max_headroom = ixs->max_tailroom = 0;
  43051. +
  43052. + if (ixs->outgoing_said.proto == IPPROTO_INT) {
  43053. + switch (ntohl(ixs->outgoing_said.spi)) {
  43054. + case SPI_DROP:
  43055. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43056. + "klips_debug:ipsec_xmit_encap_bundle: "
  43057. + "shunt SA of DROP or no eroute: dropping.\n");
  43058. + ixs->stats->tx_dropped++;
  43059. + break;
  43060. +
  43061. + case SPI_REJECT:
  43062. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43063. + "klips_debug:ipsec_xmit_encap_bundle: "
  43064. + "shunt SA of REJECT: notifying and dropping.\n");
  43065. + ICMP_SEND(ixs->skb,
  43066. + ICMP_DEST_UNREACH,
  43067. + ICMP_PKT_FILTERED,
  43068. + 0,
  43069. + ixs->physdev);
  43070. + ixs->stats->tx_dropped++;
  43071. + break;
  43072. +
  43073. + case SPI_PASS:
  43074. +#ifdef NET_21
  43075. + ixs->pass = 1;
  43076. +#endif /* NET_21 */
  43077. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43078. + "klips_debug:ipsec_xmit_encap_bundle: "
  43079. + "PASS: calling dev_queue_xmit\n");
  43080. + return IPSEC_XMIT_PASS;
  43081. + goto cleanup;
  43082. +
  43083. + case SPI_HOLD:
  43084. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43085. + "klips_debug:ipsec_xmit_encap_bundle: "
  43086. + "shunt SA of HOLD: this does not make sense here, dropping.\n");
  43087. + ixs->stats->tx_dropped++;
  43088. + break;
  43089. +
  43090. + case SPI_TRAP:
  43091. + case SPI_TRAPSUBNET:
  43092. + {
  43093. + struct sockaddr_in src, dst;
  43094. +#ifdef CONFIG_KLIPS_DEBUG
  43095. + char bufsrc[ADDRTOA_BUF], bufdst[ADDRTOA_BUF];
  43096. +#endif /* CONFIG_KLIPS_DEBUG */
  43097. +
  43098. + /* Signal all listening KMds with a PF_KEY ACQUIRE */
  43099. +
  43100. + memset(&src, 0, sizeof(src));
  43101. + memset(&dst, 0, sizeof(dst));
  43102. + src.sin_family = AF_INET;
  43103. + dst.sin_family = AF_INET;
  43104. + src.sin_addr.s_addr = ixs->iph->saddr;
  43105. + dst.sin_addr.s_addr = ixs->iph->daddr;
  43106. +
  43107. + ixs->ips.ips_transport_protocol = 0;
  43108. + src.sin_port = 0;
  43109. + dst.sin_port = 0;
  43110. +
  43111. + if(ixs->eroute->er_eaddr.sen_proto != 0) {
  43112. + ixs->ips.ips_transport_protocol = ixs->iph->protocol;
  43113. +
  43114. + if(ixs->eroute->er_eaddr.sen_sport != 0) {
  43115. + src.sin_port =
  43116. + (ixs->iph->protocol == IPPROTO_UDP
  43117. + ? ((struct udphdr*) (((caddr_t)ixs->iph) + (ixs->iph->ihl << 2)))->source
  43118. + : (ixs->iph->protocol == IPPROTO_TCP
  43119. + ? ((struct tcphdr*)((caddr_t)ixs->iph + (ixs->iph->ihl << 2)))->source
  43120. + : 0));
  43121. + }
  43122. + if(ixs->eroute->er_eaddr.sen_dport != 0) {
  43123. + dst.sin_port =
  43124. + (ixs->iph->protocol == IPPROTO_UDP
  43125. + ? ((struct udphdr*) (((caddr_t)ixs->iph) + (ixs->iph->ihl << 2)))->dest
  43126. + : (ixs->iph->protocol == IPPROTO_TCP
  43127. + ? ((struct tcphdr*)((caddr_t)ixs->iph + (ixs->iph->ihl << 2)))->dest
  43128. + : 0));
  43129. + }
  43130. + }
  43131. +
  43132. + ixs->ips.ips_addr_s = (struct sockaddr*)(&src);
  43133. + ixs->ips.ips_addr_d = (struct sockaddr*)(&dst);
  43134. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43135. + "klips_debug:ipsec_xmit_encap_bundle: "
  43136. + "SADB_ACQUIRE sent with src=%s:%d, dst=%s:%d, proto=%d.\n",
  43137. + addrtoa(((struct sockaddr_in*)(ixs->ips.ips_addr_s))->sin_addr, 0, bufsrc, sizeof(bufsrc)) <= ADDRTOA_BUF ? bufsrc : "BAD_ADDR",
  43138. + ntohs(((struct sockaddr_in*)(ixs->ips.ips_addr_s))->sin_port),
  43139. + addrtoa(((struct sockaddr_in*)(ixs->ips.ips_addr_d))->sin_addr, 0, bufdst, sizeof(bufdst)) <= ADDRTOA_BUF ? bufdst : "BAD_ADDR",
  43140. + ntohs(((struct sockaddr_in*)(ixs->ips.ips_addr_d))->sin_port),
  43141. + ixs->ips.ips_said.proto);
  43142. +
  43143. + /* increment count of total traps needed */
  43144. + ipsec_xmit_trap_count++;
  43145. +
  43146. + if (pfkey_acquire(&ixs->ips) == 0) {
  43147. +
  43148. + /* note that we succeeded */
  43149. + ipsec_xmit_trap_sendcount++;
  43150. +
  43151. + if (ixs->outgoing_said.spi==htonl(SPI_TRAPSUBNET)) {
  43152. + /*
  43153. + * The spinlock is to prevent any other
  43154. + * process from accessing or deleting
  43155. + * the eroute while we are using and
  43156. + * updating it.
  43157. + */
  43158. + spin_lock(&eroute_lock);
  43159. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  43160. + if(ixs->eroute) {
  43161. + ixs->eroute->er_said.spi = htonl(SPI_HOLD);
  43162. + ixs->eroute->er_first = ixs->skb;
  43163. + ixs->skb = NULL;
  43164. + }
  43165. + spin_unlock(&eroute_lock);
  43166. + } else if (create_hold_eroute(ixs->eroute,
  43167. + ixs->skb,
  43168. + ixs->iph,
  43169. + ixs->eroute_pid)) {
  43170. + ixs->skb = NULL;
  43171. + }
  43172. + /* whether or not the above succeeded, we continue */
  43173. +
  43174. + }
  43175. + ixs->stats->tx_dropped++;
  43176. + }
  43177. + default:
  43178. + /* XXX what do we do with an unknown shunt spi? */
  43179. + break;
  43180. + } /* switch (ntohl(ixs->outgoing_said.spi)) */
  43181. + return IPSEC_XMIT_STOLEN;
  43182. + } /* if (ixs->outgoing_said.proto == IPPROTO_INT) */
  43183. +
  43184. + /*
  43185. + The spinlock is to prevent any other process from
  43186. + accessing or deleting the ipsec_sa hash table or any of the
  43187. + ipsec_sa s while we are using and updating them.
  43188. +
  43189. + This is not optimal, but was relatively straightforward
  43190. + at the time. A better way to do it has been planned for
  43191. + more than a year, to lock the hash table and put reference
  43192. + counts on each ipsec_sa instead. This is not likely to happen
  43193. + in KLIPS1 unless a volunteer contributes it, but will be
  43194. + designed into KLIPS2.
  43195. + */
  43196. + spin_lock(&tdb_lock);
  43197. +
  43198. + ixs->ipsp = ipsec_sa_getbyid(&ixs->outgoing_said);
  43199. + ixs->sa_len = satot(&ixs->outgoing_said, 0, ixs->sa_txt, sizeof(ixs->sa_txt));
  43200. +
  43201. + if (ixs->ipsp == NULL) {
  43202. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43203. + "klips_debug:ipsec_xmit_encap_bundle: "
  43204. + "no ipsec_sa for SA%s: outgoing packet with no SA, dropped.\n",
  43205. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43206. + if(ixs->stats) {
  43207. + ixs->stats->tx_dropped++;
  43208. + }
  43209. + bundle_stat = IPSEC_XMIT_SAIDNOTFOUND;
  43210. + goto cleanup;
  43211. + }
  43212. +
  43213. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43214. + "klips_debug:ipsec_xmit_encap_bundle: "
  43215. + "found ipsec_sa -- SA:<%s%s%s> %s\n",
  43216. + IPS_XFORM_NAME(ixs->ipsp),
  43217. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43218. +
  43219. + /*
  43220. + * How much headroom do we need to be able to apply
  43221. + * all the grouped transforms?
  43222. + */
  43223. + ixs->ipsq = ixs->ipsp; /* save the head of the ipsec_sa chain */
  43224. + while (ixs->ipsp) {
  43225. + ixs->sa_len = satot(&ixs->ipsp->ips_said, 0, ixs->sa_txt, sizeof(ixs->sa_txt));
  43226. + if(ixs->sa_len == 0) {
  43227. + strcpy(ixs->sa_txt, "(error)");
  43228. + }
  43229. +
  43230. + /* If it is in larval state, drop the packet, we cannot process yet. */
  43231. + if(ixs->ipsp->ips_state == SADB_SASTATE_LARVAL) {
  43232. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43233. + "klips_debug:ipsec_xmit_encap_bundle: "
  43234. + "ipsec_sa in larval state for SA:<%s%s%s> %s, cannot be used yet, dropping packet.\n",
  43235. + IPS_XFORM_NAME(ixs->ipsp),
  43236. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43237. + if(ixs->stats) {
  43238. + ixs->stats->tx_errors++;
  43239. + }
  43240. + bundle_stat = IPSEC_XMIT_SAIDNOTLIVE;
  43241. + goto cleanup;
  43242. + }
  43243. +
  43244. + if(ixs->ipsp->ips_state == SADB_SASTATE_DEAD) {
  43245. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43246. + "klips_debug:ipsec_xmit_encap_bundle: "
  43247. + "ipsec_sa in dead state for SA:<%s%s%s> %s, can no longer be used, dropping packet.\n",
  43248. + IPS_XFORM_NAME(ixs->ipsp),
  43249. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43250. + ixs->stats->tx_errors++;
  43251. + bundle_stat = IPSEC_XMIT_SAIDNOTLIVE;
  43252. + goto cleanup;
  43253. + }
  43254. +
  43255. + /* If the replay window counter == -1, expire SA, it will roll */
  43256. + if(ixs->ipsp->ips_replaywin && ixs->ipsp->ips_replaywin_lastseq == -1) {
  43257. + pfkey_expire(ixs->ipsp, 1);
  43258. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43259. + "klips_debug:ipsec_xmit_encap_bundle: "
  43260. + "replay window counter rolled for SA:<%s%s%s> %s, packet dropped, expiring SA.\n",
  43261. + IPS_XFORM_NAME(ixs->ipsp),
  43262. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43263. + ipsec_sa_delchain(ixs->ipsp);
  43264. + ixs->stats->tx_errors++;
  43265. + bundle_stat = IPSEC_XMIT_REPLAYROLLED;
  43266. + goto cleanup;
  43267. + }
  43268. +
  43269. + /*
  43270. + * if this is the first time we are using this SA, mark start time,
  43271. + * and offset hard/soft counters by "now" for later checking.
  43272. + */
  43273. +#if 0
  43274. + if(ixs->ipsp->ips_life.ipl_usetime.count == 0) {
  43275. + ixs->ipsp->ips_life.ipl_usetime.count = jiffies;
  43276. + ixs->ipsp->ips_life.ipl_usetime.hard += jiffies;
  43277. + ixs->ipsp->ips_life.ipl_usetime.soft += jiffies;
  43278. + }
  43279. +#endif
  43280. +
  43281. +
  43282. + if(ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_bytes, "bytes", ixs->sa_txt,
  43283. + ipsec_life_countbased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43284. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_addtime, "addtime",ixs->sa_txt,
  43285. + ipsec_life_timebased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43286. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_usetime, "usetime",ixs->sa_txt,
  43287. + ipsec_life_timebased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43288. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_packets, "packets",ixs->sa_txt,
  43289. + ipsec_life_countbased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied) {
  43290. +
  43291. + ipsec_sa_delchain(ixs->ipsp);
  43292. + ixs->stats->tx_errors++;
  43293. + bundle_stat = IPSEC_XMIT_LIFETIMEFAILED;
  43294. + goto cleanup;
  43295. + }
  43296. +
  43297. +
  43298. + ixs->headroom = ixs->tailroom = 0;
  43299. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43300. + "klips_debug:ipsec_xmit_encap_bundle: "
  43301. + "calling room for <%s%s%s>, SA:%s\n",
  43302. + IPS_XFORM_NAME(ixs->ipsp),
  43303. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43304. + switch(ixs->ipsp->ips_said.proto) {
  43305. +#ifdef CONFIG_KLIPS_AH
  43306. + case IPPROTO_AH:
  43307. + ixs->headroom += sizeof(struct ahhdr);
  43308. + break;
  43309. +#endif /* CONFIG_KLIPS_AH */
  43310. +#ifdef CONFIG_KLIPS_ESP
  43311. + case IPPROTO_ESP:
  43312. + ixt_e=ixs->ipsp->ips_alg_enc;
  43313. + if (ixt_e) {
  43314. + blocksize = ixt_e->ixt_common.ixt_blocksize;
  43315. + ixs->headroom += ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  43316. + }
  43317. + else {
  43318. + ixs->stats->tx_errors++;
  43319. + bundle_stat = IPSEC_XMIT_ESP_BADALG;
  43320. + goto cleanup;
  43321. + }
  43322. +
  43323. + if ((ixt_a=ixs->ipsp->ips_alg_auth)) {
  43324. + ixs->tailroom += AHHMAC_HASHLEN;
  43325. + } else
  43326. + switch(ixs->ipsp->ips_authalg) {
  43327. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  43328. + case AH_MD5:
  43329. + ixs->tailroom += AHHMAC_HASHLEN;
  43330. + break;
  43331. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  43332. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  43333. + case AH_SHA:
  43334. + ixs->tailroom += AHHMAC_HASHLEN;
  43335. + break;
  43336. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  43337. + case AH_NONE:
  43338. + break;
  43339. + default:
  43340. + ixs->stats->tx_errors++;
  43341. + bundle_stat = IPSEC_XMIT_AH_BADALG;
  43342. + goto cleanup;
  43343. + }
  43344. + ixs->tailroom += blocksize != 1 ?
  43345. + ((blocksize - ((ixs->pyldsz + 2) % blocksize)) % blocksize) + 2 :
  43346. + ((4 - ((ixs->pyldsz + 2) % 4)) % 4) + 2;
  43347. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  43348. + if ((ixs->ipsp->ips_natt_type) && (!ixs->natt_type)) {
  43349. + ixs->natt_type = ixs->ipsp->ips_natt_type;
  43350. + ixs->natt_sport = ixs->ipsp->ips_natt_sport;
  43351. + ixs->natt_dport = ixs->ipsp->ips_natt_dport;
  43352. + switch (ixs->natt_type) {
  43353. + case ESPINUDP_WITH_NON_IKE:
  43354. + ixs->natt_head = sizeof(struct udphdr)+(2*sizeof(__u32));
  43355. + break;
  43356. +
  43357. + case ESPINUDP_WITH_NON_ESP:
  43358. + ixs->natt_head = sizeof(struct udphdr);
  43359. + break;
  43360. +
  43361. + default:
  43362. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT
  43363. + , "klips_xmit: invalid nat-t type %d"
  43364. + , ixs->natt_type);
  43365. + bundle_stat = IPSEC_XMIT_ESPUDP_BADTYPE;
  43366. + goto cleanup;
  43367. +
  43368. + break;
  43369. + }
  43370. + ixs->tailroom += ixs->natt_head;
  43371. + }
  43372. +#endif
  43373. + break;
  43374. +#endif /* !CONFIG_KLIPS_ESP */
  43375. +#ifdef CONFIG_KLIPS_IPIP
  43376. + case IPPROTO_IPIP:
  43377. + ixs->headroom += sizeof(struct iphdr);
  43378. + break;
  43379. +#endif /* !CONFIG_KLIPS_IPIP */
  43380. + case IPPROTO_COMP:
  43381. +#ifdef CONFIG_KLIPS_IPCOMP
  43382. + /*
  43383. + We can't predict how much the packet will
  43384. + shrink without doing the actual compression.
  43385. + We could do it here, if we were the first
  43386. + encapsulation in the chain. That might save
  43387. + us a skb_copy_expand, since we might fit
  43388. + into the existing skb then. However, this
  43389. + would be a bit unclean (and this hack has
  43390. + bit us once), so we better not do it. After
  43391. + all, the skb_copy_expand is cheap in
  43392. + comparison to the actual compression.
  43393. + At least we know the packet will not grow.
  43394. + */
  43395. + break;
  43396. +#endif /* CONFIG_KLIPS_IPCOMP */
  43397. + default:
  43398. + ixs->stats->tx_errors++;
  43399. + bundle_stat = IPSEC_XMIT_BADPROTO;
  43400. + goto cleanup;
  43401. + }
  43402. + ixs->ipsp = ixs->ipsp->ips_onext;
  43403. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43404. + "klips_debug:ipsec_xmit_encap_bundle: "
  43405. + "Required head,tailroom: %d,%d\n",
  43406. + ixs->headroom, ixs->tailroom);
  43407. + ixs->max_headroom += ixs->headroom;
  43408. + ixs->max_tailroom += ixs->tailroom;
  43409. + ixs->pyldsz += (ixs->headroom + ixs->tailroom);
  43410. + }
  43411. + ixs->ipsp = ixs->ipsq; /* restore the head of the ipsec_sa chain */
  43412. +
  43413. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43414. + "klips_debug:ipsec_xmit_encap_bundle: "
  43415. + "existing head,tailroom: %d,%d before applying xforms with head,tailroom: %d,%d .\n",
  43416. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb),
  43417. + ixs->max_headroom, ixs->max_tailroom);
  43418. +
  43419. + ixs->tot_headroom += ixs->max_headroom;
  43420. + ixs->tot_tailroom += ixs->max_tailroom;
  43421. +
  43422. + ixs->mtudiff = ixs->cur_mtu + ixs->tot_headroom + ixs->tot_tailroom - ixs->physmtu;
  43423. +
  43424. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43425. + "klips_debug:ipsec_xmit_encap_bundle: "
  43426. + "mtu:%d physmtu:%d tothr:%d tottr:%d mtudiff:%d ippkttotlen:%d\n",
  43427. + ixs->cur_mtu, ixs->physmtu,
  43428. + ixs->tot_headroom, ixs->tot_tailroom, ixs->mtudiff, ntohs(ixs->iph->tot_len));
  43429. + if(ixs->mtudiff > 0) {
  43430. + int newmtu = ixs->physmtu - (ixs->tot_headroom + ((ixs->tot_tailroom + 2) & ~7) + 5);
  43431. +
  43432. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43433. + "klips_info:ipsec_xmit_encap_bundle: "
  43434. + "dev %s mtu of %d decreased by %d to %d\n",
  43435. + ixs->dev ? ixs->dev->name : "ifX",
  43436. + ixs->cur_mtu,
  43437. + ixs->cur_mtu - newmtu,
  43438. + newmtu);
  43439. + ixs->cur_mtu = newmtu;
  43440. +
  43441. + /* this would seem to adjust the MTU of the route as well */
  43442. +#if 0
  43443. + ixs->skb->dst->pmtu = ixs->prv->mtu; /* RGB */
  43444. +#endif /* 0 */
  43445. + }
  43446. +
  43447. + /*
  43448. + If the sender is doing PMTU discovery, and the
  43449. + packet doesn't fit within ixs->prv->mtu, notify him
  43450. + (unless it was an ICMP packet, or it was not the
  43451. + zero-offset packet) and send it anyways.
  43452. +
  43453. + Note: buggy firewall configuration may prevent the
  43454. + ICMP packet from getting back.
  43455. + */
  43456. + if(sysctl_ipsec_icmp
  43457. + && ixs->cur_mtu < ntohs(ixs->iph->tot_len)
  43458. + && (ixs->iph->frag_off & __constant_htons(IP_DF)) ) {
  43459. + int notify = ixs->iph->protocol != IPPROTO_ICMP
  43460. + && (ixs->iph->frag_off & __constant_htons(IP_OFFSET)) == 0;
  43461. +
  43462. +#ifdef IPSEC_obey_DF
  43463. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43464. + "klips_debug:ipsec_xmit_encap_bundle: "
  43465. + "fragmentation needed and DF set; %sdropping packet\n",
  43466. + notify ? "sending ICMP and " : "");
  43467. + if (notify)
  43468. + ICMP_SEND(ixs->skb,
  43469. + ICMP_DEST_UNREACH,
  43470. + ICMP_FRAG_NEEDED,
  43471. + ixs->cur_mtu,
  43472. + ixs->physdev);
  43473. + ixs->stats->tx_errors++;
  43474. + bundle_stat = IPSEC_XMIT_CANNOTFRAG;
  43475. + goto cleanup;
  43476. +#else /* IPSEC_obey_DF */
  43477. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43478. + "klips_debug:ipsec_xmit_encap_bundle: "
  43479. + "fragmentation needed and DF set; %spassing packet\n",
  43480. + notify ? "sending ICMP and " : "");
  43481. + if (notify)
  43482. + ICMP_SEND(ixs->skb,
  43483. + ICMP_DEST_UNREACH,
  43484. + ICMP_FRAG_NEEDED,
  43485. + ixs->cur_mtu,
  43486. + ixs->physdev);
  43487. +#endif /* IPSEC_obey_DF */
  43488. + }
  43489. +
  43490. +#ifdef MSS_HACK
  43491. + /*
  43492. + * If this is a transport mode TCP packet with
  43493. + * SYN set, determine an effective MSS based on
  43494. + * AH/ESP overheads determined above.
  43495. + */
  43496. + if (ixs->iph->protocol == IPPROTO_TCP
  43497. + && ixs->outgoing_said.proto != IPPROTO_IPIP) {
  43498. + struct tcphdr *tcph = ixs->skb->h.th;
  43499. + if (tcph->syn && !tcph->ack) {
  43500. + if(!ipsec_adjust_mss(ixs->skb, tcph, ixs->cur_mtu)) {
  43501. + printk(KERN_WARNING
  43502. + "klips_warning:ipsec_xmit_encap_bundle: "
  43503. + "ipsec_adjust_mss() failed\n");
  43504. + ixs->stats->tx_errors++;
  43505. + bundle_stat = IPSEC_XMIT_MSSERR;
  43506. + goto cleanup;
  43507. + }
  43508. + }
  43509. + }
  43510. +#endif /* MSS_HACK */
  43511. +
  43512. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  43513. + if ((ixs->natt_type) && (ixs->outgoing_said.proto != IPPROTO_IPIP)) {
  43514. + /**
  43515. + * NAT-Traversal and Transport Mode:
  43516. + * we need to correct TCP/UDP checksum
  43517. + *
  43518. + * If we've got NAT-OA, we can fix checksum without recalculation.
  43519. + * If we don't we can zero udp checksum.
  43520. + */
  43521. + __u32 natt_oa = ixs->ipsp->ips_natt_oa ?
  43522. + ((struct sockaddr_in*)(ixs->ipsp->ips_natt_oa))->sin_addr.s_addr : 0;
  43523. + __u16 pkt_len = ixs->skb->tail - (unsigned char *)ixs->iph;
  43524. + __u16 data_len = pkt_len - (ixs->iph->ihl << 2);
  43525. + switch (ixs->iph->protocol) {
  43526. + case IPPROTO_TCP:
  43527. + if (data_len >= sizeof(struct tcphdr)) {
  43528. + struct tcphdr *tcp = (struct tcphdr *)((__u32 *)ixs->iph+ixs->iph->ihl);
  43529. + if (natt_oa) {
  43530. + __u32 buff[2] = { ~ixs->iph->daddr, natt_oa };
  43531. + KLIPS_PRINT(debug_tunnel,
  43532. + "klips_debug:ipsec_tunnel_start_xmit: "
  43533. + "NAT-T & TRANSPORT: "
  43534. + "fix TCP checksum using NAT-OA\n");
  43535. + tcp->check = csum_fold(
  43536. + csum_partial((unsigned char *)buff, sizeof(buff),
  43537. + tcp->check^0xffff));
  43538. + }
  43539. + else {
  43540. + KLIPS_PRINT(debug_tunnel,
  43541. + "klips_debug:ipsec_tunnel_start_xmit: "
  43542. + "NAT-T & TRANSPORT: do not recalc TCP checksum\n");
  43543. + }
  43544. + }
  43545. + else {
  43546. + KLIPS_PRINT(debug_tunnel,
  43547. + "klips_debug:ipsec_tunnel_start_xmit: "
  43548. + "NAT-T & TRANSPORT: can't fix TCP checksum\n");
  43549. + }
  43550. + break;
  43551. + case IPPROTO_UDP:
  43552. + if (data_len >= sizeof(struct udphdr)) {
  43553. + struct udphdr *udp = (struct udphdr *)((__u32 *)ixs->iph+ixs->iph->ihl);
  43554. + if (udp->check == 0) {
  43555. + KLIPS_PRINT(debug_tunnel,
  43556. + "klips_debug:ipsec_tunnel_start_xmit: "
  43557. + "NAT-T & TRANSPORT: UDP checksum already 0\n");
  43558. + }
  43559. + else if (natt_oa) {
  43560. + __u32 buff[2] = { ~ixs->iph->daddr, natt_oa };
  43561. + KLIPS_PRINT(debug_tunnel,
  43562. + "klips_debug:ipsec_tunnel_start_xmit: "
  43563. + "NAT-T & TRANSPORT: "
  43564. + "fix UDP checksum using NAT-OA\n");
  43565. + udp->check = csum_fold(
  43566. + csum_partial((unsigned char *)buff, sizeof(buff),
  43567. + udp->check^0xffff));
  43568. + }
  43569. + else {
  43570. + KLIPS_PRINT(debug_tunnel,
  43571. + "klips_debug:ipsec_tunnel_start_xmit: "
  43572. + "NAT-T & TRANSPORT: zero UDP checksum\n");
  43573. + udp->check = 0;
  43574. + }
  43575. + }
  43576. + else {
  43577. + KLIPS_PRINT(debug_tunnel,
  43578. + "klips_debug:ipsec_tunnel_start_xmit: "
  43579. + "NAT-T & TRANSPORT: can't fix UDP checksum\n");
  43580. + }
  43581. + break;
  43582. + default:
  43583. + KLIPS_PRINT(debug_tunnel,
  43584. + "klips_debug:ipsec_tunnel_start_xmit: "
  43585. + "NAT-T & TRANSPORT: non TCP/UDP packet -- do nothing\n");
  43586. + break;
  43587. + }
  43588. + }
  43589. +#endif /* CONFIG_IPSEC_NAT_TRAVERSAL */
  43590. +
  43591. + if(!ixs->hard_header_stripped && ixs->hard_header_len>0) {
  43592. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43593. + "klips_debug:ipsec_xmit_encap_bundle: "
  43594. + "allocating %d bytes for hardheader.\n",
  43595. + ixs->hard_header_len);
  43596. + if((ixs->saved_header = kmalloc(ixs->hard_header_len, GFP_ATOMIC)) == NULL) {
  43597. + printk(KERN_WARNING "klips_debug:ipsec_xmit_encap_bundle: "
  43598. + "Failed, tried to allocate %d bytes for temp hard_header.\n",
  43599. + ixs->hard_header_len);
  43600. + ixs->stats->tx_errors++;
  43601. + bundle_stat = IPSEC_XMIT_ERRMEMALLOC;
  43602. + goto cleanup;
  43603. + }
  43604. + {
  43605. + int i;
  43606. + for (i = 0; i < ixs->hard_header_len; i++) {
  43607. + ixs->saved_header[i] = ixs->skb->data[i];
  43608. + }
  43609. + }
  43610. + if(ixs->skb->len < ixs->hard_header_len) {
  43611. + printk(KERN_WARNING "klips_error:ipsec_xmit_encap_bundle: "
  43612. + "tried to skb_pull hhlen=%d, %d available. This should never happen, please report.\n",
  43613. + ixs->hard_header_len, (int)(ixs->skb->len));
  43614. + ixs->stats->tx_errors++;
  43615. + bundle_stat = IPSEC_XMIT_ESP_PUSHPULLERR;
  43616. + goto cleanup;
  43617. + }
  43618. + skb_pull(ixs->skb, ixs->hard_header_len);
  43619. + ixs->hard_header_stripped = 1;
  43620. +
  43621. +/* ixs->iph = (struct iphdr *) (ixs->skb->data); */
  43622. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43623. + "klips_debug:ipsec_xmit_encap_bundle: "
  43624. + "head,tailroom: %d,%d after hard_header stripped.\n",
  43625. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  43626. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_CROUT, ixs->iph);
  43627. + } else {
  43628. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43629. + "klips_debug:ipsec_xmit_encap_bundle: "
  43630. + "hard header already stripped.\n");
  43631. + }
  43632. +
  43633. + ixs->ll_headroom = (ixs->hard_header_len + 15) & ~15;
  43634. +
  43635. + if ((skb_headroom(ixs->skb) >= ixs->max_headroom + 2 * ixs->ll_headroom) &&
  43636. + (skb_tailroom(ixs->skb) >= ixs->max_tailroom)
  43637. +#ifndef NET_21
  43638. + && ixs->skb->free
  43639. +#endif /* !NET_21 */
  43640. + ) {
  43641. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43642. + "klips_debug:ipsec_xmit_encap_bundle: "
  43643. + "data fits in existing skb\n");
  43644. + } else {
  43645. + struct sk_buff* tskb;
  43646. +
  43647. + if(!ixs->oskb) {
  43648. + ixs->oskb = ixs->skb;
  43649. + }
  43650. +
  43651. + tskb = skb_copy_expand(ixs->skb,
  43652. + /* The need for 2 * link layer length here remains unexplained...RGB */
  43653. + ixs->max_headroom + 2 * ixs->ll_headroom,
  43654. + ixs->max_tailroom,
  43655. + GFP_ATOMIC);
  43656. +
  43657. + if(tskb && ixs->skb->sk) {
  43658. + skb_set_owner_w(tskb, ixs->skb->sk);
  43659. + }
  43660. +
  43661. + if(ixs->skb != ixs->oskb) {
  43662. + ipsec_kfree_skb(ixs->skb);
  43663. + }
  43664. + ixs->skb = tskb;
  43665. + if (!ixs->skb) {
  43666. + printk(KERN_WARNING
  43667. + "klips_debug:ipsec_xmit_encap_bundle: "
  43668. + "Failed, tried to allocate %d head and %d tailroom\n",
  43669. + ixs->max_headroom, ixs->max_tailroom);
  43670. + ixs->stats->tx_errors++;
  43671. + bundle_stat = IPSEC_XMIT_ERRSKBALLOC;
  43672. + goto cleanup;
  43673. + }
  43674. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43675. + "klips_debug:ipsec_xmit_encap_bundle: "
  43676. + "head,tailroom: %d,%d after allocation\n",
  43677. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  43678. + }
  43679. +#ifdef CONFIG_KLIPS_DEBUG
  43680. + if(debug_tunnel & DB_TN_ENCAP) {
  43681. + ipsec_print_ip(ixs->iph);
  43682. + }
  43683. +#endif
  43684. +
  43685. + /*
  43686. + * Apply grouped transforms to packet
  43687. + */
  43688. + while (ixs->ipsp) {
  43689. + enum ipsec_xmit_value encap_stat = IPSEC_XMIT_OK;
  43690. +
  43691. + encap_stat = ipsec_xmit_encap_once(ixs);
  43692. +#ifdef CONFIG_KLIPS_DEBUG
  43693. + if(debug_tunnel & DB_TN_ENCAP) {
  43694. + ipsec_print_ip(ixs->iph);
  43695. + }
  43696. +#endif
  43697. +
  43698. + if(encap_stat != IPSEC_XMIT_OK) {
  43699. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43700. + "klips_debug:ipsec_xmit_encap_bundle: encap_once failed: %d\n",
  43701. + encap_stat);
  43702. +
  43703. + bundle_stat = IPSEC_XMIT_ENCAPFAIL;
  43704. + goto cleanup;
  43705. + }
  43706. + }
  43707. +
  43708. + /* we are done with this SA */
  43709. + ipsec_sa_put(ixs->ipsp);
  43710. +
  43711. + /* end encapsulation loop here XXX */
  43712. + cleanup:
  43713. + spin_unlock(&tdb_lock);
  43714. + return bundle_stat;
  43715. +}
  43716. +
  43717. +/*
  43718. + * $Log: ipsec_xmit.c,v $
  43719. + * Revision 1.20.2.8 2006/10/06 21:39:26 paul
  43720. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  43721. + * set. This is defined through autoconf.h which is included through the
  43722. + * linux kernel build macros.
  43723. + *
  43724. + * Revision 1.20.2.7 2006/08/24 03:02:01 paul
  43725. + * Compile fixes for when CONFIG_KLIPS_DEBUG is not set. (bug #642)
  43726. + *
  43727. + * Revision 1.20.2.6 2006/07/07 22:09:49 paul
  43728. + * From: Bart Trojanowski <bart@xelerance.com>
  43729. + * Removing a left over '#else' that split another '#if/#endif' block in two.
  43730. + *
  43731. + * Revision 1.20.2.5 2006/07/07 15:43:17 paul
  43732. + * From: Bart Trojanowski <bart@xelerance.com>
  43733. + * improved protocol detection in ipsec_print_ip() -- a debug aid.
  43734. + *
  43735. + * Revision 1.20.2.4 2006/04/20 16:33:07 mcr
  43736. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  43737. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  43738. + *
  43739. + * Revision 1.20.2.3 2005/11/29 21:52:57 ken
  43740. + * Fix for #518 MTU issues
  43741. + *
  43742. + * Revision 1.20.2.2 2005/11/27 21:41:03 paul
  43743. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"in for klips as module.
  43744. + *
  43745. + * Revision 1.20.2.1 2005/08/27 23:40:00 paul
  43746. + * recommited HAVE_SOCK_SECURITY fixes for linux 2.6.13
  43747. + *
  43748. + * Revision 1.20 2005/07/12 15:39:27 paul
  43749. + * include asm/uaccess.h for VERIFY_WRITE
  43750. + *
  43751. + * Revision 1.19 2005/05/24 01:02:35 mcr
  43752. + * some refactoring/simplification of situation where alg
  43753. + * is not found.
  43754. + *
  43755. + * Revision 1.18 2005/05/23 23:52:33 mcr
  43756. + * adjust comments, add additional debugging.
  43757. + *
  43758. + * Revision 1.17 2005/05/23 22:57:23 mcr
  43759. + * removed explicit 3DES support.
  43760. + *
  43761. + * Revision 1.16 2005/05/21 03:29:15 mcr
  43762. + * fixed warning about unused zeroes if AH is off.
  43763. + *
  43764. + * Revision 1.15 2005/05/20 16:47:59 mcr
  43765. + * include asm/checksum.h to get ip_fast_csum macro.
  43766. + *
  43767. + * Revision 1.14 2005/05/11 01:43:03 mcr
  43768. + * removed "poor-man"s OOP in favour of proper C structures.
  43769. + *
  43770. + * Revision 1.13 2005/04/29 05:10:22 mcr
  43771. + * removed from extraenous includes to make unit testing easier.
  43772. + *
  43773. + * Revision 1.12 2005/04/15 01:28:34 mcr
  43774. + * use ipsec_dmp_block.
  43775. + *
  43776. + * Revision 1.11 2005/01/26 00:50:35 mcr
  43777. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  43778. + * and make sure that NAT_TRAVERSAL is set as well to match
  43779. + * userspace compiles of code.
  43780. + *
  43781. + * Revision 1.10 2004/09/13 17:55:21 ken
  43782. + * MD5* -> osMD5*
  43783. + *
  43784. + * Revision 1.9 2004/07/10 19:11:18 mcr
  43785. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  43786. + *
  43787. + * Revision 1.8 2004/04/06 02:49:26 mcr
  43788. + * pullup of algo code from alg-branch.
  43789. + *
  43790. + * Revision 1.7 2004/02/03 03:13:41 mcr
  43791. + * mark invalid encapsulation states.
  43792. + *
  43793. + * Revision 1.6.2.1 2003/12/22 15:25:52 jjo
  43794. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  43795. + *
  43796. + * Revision 1.6 2003/12/10 01:14:27 mcr
  43797. + * NAT-traversal patches to KLIPS.
  43798. + *
  43799. + * Revision 1.5 2003/10/31 02:27:55 mcr
  43800. + * pulled up port-selector patches and sa_id elimination.
  43801. + *
  43802. + * Revision 1.4.4.2 2003/10/29 01:37:39 mcr
  43803. + * when creating %hold from %trap, only make the %hold as
  43804. + * specific as the %trap was - so if the protocol and ports
  43805. + * were wildcards, then the %hold will be too.
  43806. + *
  43807. + * Revision 1.4.4.1 2003/09/21 13:59:56 mcr
  43808. + * pre-liminary X.509 patch - does not yet pass tests.
  43809. + *
  43810. + * Revision 1.4 2003/06/20 02:28:10 mcr
  43811. + * misstype of variable name, not detected by module build.
  43812. + *
  43813. + * Revision 1.3 2003/06/20 01:42:21 mcr
  43814. + * added counters to measure how many ACQUIREs we send to pluto,
  43815. + * and how many are successfully sent.
  43816. + *
  43817. + * Revision 1.2 2003/04/03 17:38:35 rgb
  43818. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  43819. + * Normalised coding style.
  43820. + * Simplified logic and reduced duplication of code.
  43821. + *
  43822. + * Revision 1.1 2003/02/12 19:31:23 rgb
  43823. + * Refactored from ipsec_tunnel.c
  43824. + *
  43825. + * Local Variables:
  43826. + * c-file-style: "linux"
  43827. + * End:
  43828. + *
  43829. + */
  43830. --- /dev/null Tue Mar 11 13:02:56 2003
  43831. +++ linux/net/ipsec/match586.S Mon Feb 9 13:51:03 2004
  43832. @@ -0,0 +1,357 @@
  43833. +/* match.s -- Pentium-optimized version of longest_match()
  43834. + * Written for zlib 1.1.2
  43835. + * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
  43836. + *
  43837. + * This is free software; you can redistribute it and/or modify it
  43838. + * under the terms of the GNU General Public License.
  43839. + */
  43840. +
  43841. +#ifndef NO_UNDERLINE
  43842. +#define match_init _ipcomp_match_init
  43843. +#define longest_match _ipcomp_longest_match
  43844. +#else
  43845. +#define match_init ipcomp_match_init
  43846. +#define longest_match ipcomp_longest_match
  43847. +#endif
  43848. +
  43849. +#define MAX_MATCH (258)
  43850. +#define MIN_MATCH (3)
  43851. +#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  43852. +#define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  43853. +
  43854. +/* stack frame offsets */
  43855. +
  43856. +#define wmask 0 /* local copy of s->wmask */
  43857. +#define window 4 /* local copy of s->window */
  43858. +#define windowbestlen 8 /* s->window + bestlen */
  43859. +#define chainlenscanend 12 /* high word: current chain len */
  43860. + /* low word: last bytes sought */
  43861. +#define scanstart 16 /* first two bytes of string */
  43862. +#define scanalign 20 /* dword-misalignment of string */
  43863. +#define nicematch 24 /* a good enough match size */
  43864. +#define bestlen 28 /* size of best match so far */
  43865. +#define scan 32 /* ptr to string wanting match */
  43866. +
  43867. +#define LocalVarsSize (36)
  43868. +/* saved ebx 36 */
  43869. +/* saved edi 40 */
  43870. +/* saved esi 44 */
  43871. +/* saved ebp 48 */
  43872. +/* return address 52 */
  43873. +#define deflatestate 56 /* the function arguments */
  43874. +#define curmatch 60
  43875. +
  43876. +/* Offsets for fields in the deflate_state structure. These numbers
  43877. + * are calculated from the definition of deflate_state, with the
  43878. + * assumption that the compiler will dword-align the fields. (Thus,
  43879. + * changing the definition of deflate_state could easily cause this
  43880. + * program to crash horribly, without so much as a warning at
  43881. + * compile time. Sigh.)
  43882. + */
  43883. +#define dsWSize 36
  43884. +#define dsWMask 44
  43885. +#define dsWindow 48
  43886. +#define dsPrev 56
  43887. +#define dsMatchLen 88
  43888. +#define dsPrevMatch 92
  43889. +#define dsStrStart 100
  43890. +#define dsMatchStart 104
  43891. +#define dsLookahead 108
  43892. +#define dsPrevLen 112
  43893. +#define dsMaxChainLen 116
  43894. +#define dsGoodMatch 132
  43895. +#define dsNiceMatch 136
  43896. +
  43897. +
  43898. +.file "match.S"
  43899. +
  43900. +.globl match_init, longest_match
  43901. +
  43902. +.text
  43903. +
  43904. +/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  43905. +
  43906. +longest_match:
  43907. +
  43908. +/* Save registers that the compiler may be using, and adjust %esp to */
  43909. +/* make room for our stack frame. */
  43910. +
  43911. + pushl %ebp
  43912. + pushl %edi
  43913. + pushl %esi
  43914. + pushl %ebx
  43915. + subl $LocalVarsSize, %esp
  43916. +
  43917. +/* Retrieve the function arguments. %ecx will hold cur_match */
  43918. +/* throughout the entire function. %edx will hold the pointer to the */
  43919. +/* deflate_state structure during the function's setup (before */
  43920. +/* entering the main loop). */
  43921. +
  43922. + movl deflatestate(%esp), %edx
  43923. + movl curmatch(%esp), %ecx
  43924. +
  43925. +/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  43926. +
  43927. + movl dsNiceMatch(%edx), %eax
  43928. + movl dsLookahead(%edx), %ebx
  43929. + cmpl %eax, %ebx
  43930. + jl LookaheadLess
  43931. + movl %eax, %ebx
  43932. +LookaheadLess: movl %ebx, nicematch(%esp)
  43933. +
  43934. +/* register Bytef *scan = s->window + s->strstart; */
  43935. +
  43936. + movl dsWindow(%edx), %esi
  43937. + movl %esi, window(%esp)
  43938. + movl dsStrStart(%edx), %ebp
  43939. + lea (%esi,%ebp), %edi
  43940. + movl %edi, scan(%esp)
  43941. +
  43942. +/* Determine how many bytes the scan ptr is off from being */
  43943. +/* dword-aligned. */
  43944. +
  43945. + movl %edi, %eax
  43946. + negl %eax
  43947. + andl $3, %eax
  43948. + movl %eax, scanalign(%esp)
  43949. +
  43950. +/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  43951. +/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  43952. +
  43953. + movl dsWSize(%edx), %eax
  43954. + subl $MIN_LOOKAHEAD, %eax
  43955. + subl %eax, %ebp
  43956. + jg LimitPositive
  43957. + xorl %ebp, %ebp
  43958. +LimitPositive:
  43959. +
  43960. +/* unsigned chain_length = s->max_chain_length; */
  43961. +/* if (s->prev_length >= s->good_match) { */
  43962. +/* chain_length >>= 2; */
  43963. +/* } */
  43964. +
  43965. + movl dsPrevLen(%edx), %eax
  43966. + movl dsGoodMatch(%edx), %ebx
  43967. + cmpl %ebx, %eax
  43968. + movl dsMaxChainLen(%edx), %ebx
  43969. + jl LastMatchGood
  43970. + shrl $2, %ebx
  43971. +LastMatchGood:
  43972. +
  43973. +/* chainlen is decremented once beforehand so that the function can */
  43974. +/* use the sign flag instead of the zero flag for the exit test. */
  43975. +/* It is then shifted into the high word, to make room for the scanend */
  43976. +/* scanend value, which it will always accompany. */
  43977. +
  43978. + decl %ebx
  43979. + shll $16, %ebx
  43980. +
  43981. +/* int best_len = s->prev_length; */
  43982. +
  43983. + movl dsPrevLen(%edx), %eax
  43984. + movl %eax, bestlen(%esp)
  43985. +
  43986. +/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  43987. +
  43988. + addl %eax, %esi
  43989. + movl %esi, windowbestlen(%esp)
  43990. +
  43991. +/* register ush scan_start = *(ushf*)scan; */
  43992. +/* register ush scan_end = *(ushf*)(scan+best_len-1); */
  43993. +
  43994. + movw (%edi), %bx
  43995. + movw %bx, scanstart(%esp)
  43996. + movw -1(%edi,%eax), %bx
  43997. + movl %ebx, chainlenscanend(%esp)
  43998. +
  43999. +/* Posf *prev = s->prev; */
  44000. +/* uInt wmask = s->w_mask; */
  44001. +
  44002. + movl dsPrev(%edx), %edi
  44003. + movl dsWMask(%edx), %edx
  44004. + mov %edx, wmask(%esp)
  44005. +
  44006. +/* Jump into the main loop. */
  44007. +
  44008. + jmp LoopEntry
  44009. +
  44010. +.balign 16
  44011. +
  44012. +/* do {
  44013. + * match = s->window + cur_match;
  44014. + * if (*(ushf*)(match+best_len-1) != scan_end ||
  44015. + * *(ushf*)match != scan_start) continue;
  44016. + * [...]
  44017. + * } while ((cur_match = prev[cur_match & wmask]) > limit
  44018. + * && --chain_length != 0);
  44019. + *
  44020. + * Here is the inner loop of the function. The function will spend the
  44021. + * majority of its time in this loop, and majority of that time will
  44022. + * be spent in the first ten instructions.
  44023. + *
  44024. + * Within this loop:
  44025. + * %ebx = chainlenscanend - i.e., ((chainlen << 16) | scanend)
  44026. + * %ecx = curmatch
  44027. + * %edx = curmatch & wmask
  44028. + * %esi = windowbestlen - i.e., (window + bestlen)
  44029. + * %edi = prev
  44030. + * %ebp = limit
  44031. + *
  44032. + * Two optimization notes on the choice of instructions:
  44033. + *
  44034. + * The first instruction uses a 16-bit address, which costs an extra,
  44035. + * unpairable cycle. This is cheaper than doing a 32-bit access and
  44036. + * zeroing the high word, due to the 3-cycle misalignment penalty which
  44037. + * would occur half the time. This also turns out to be cheaper than
  44038. + * doing two separate 8-bit accesses, as the memory is so rarely in the
  44039. + * L1 cache.
  44040. + *
  44041. + * The window buffer, however, apparently spends a lot of time in the
  44042. + * cache, and so it is faster to retrieve the word at the end of the
  44043. + * match string with two 8-bit loads. The instructions that test the
  44044. + * word at the beginning of the match string, however, are executed
  44045. + * much less frequently, and there it was cheaper to use 16-bit
  44046. + * instructions, which avoided the necessity of saving off and
  44047. + * subsequently reloading one of the other registers.
  44048. + */
  44049. +LookupLoop:
  44050. + /* 1 U & V */
  44051. + movw (%edi,%edx,2), %cx /* 2 U pipe */
  44052. + movl wmask(%esp), %edx /* 2 V pipe */
  44053. + cmpl %ebp, %ecx /* 3 U pipe */
  44054. + jbe LeaveNow /* 3 V pipe */
  44055. + subl $0x00010000, %ebx /* 4 U pipe */
  44056. + js LeaveNow /* 4 V pipe */
  44057. +LoopEntry: movb -1(%esi,%ecx), %al /* 5 U pipe */
  44058. + andl %ecx, %edx /* 5 V pipe */
  44059. + cmpb %bl, %al /* 6 U pipe */
  44060. + jnz LookupLoop /* 6 V pipe */
  44061. + movb (%esi,%ecx), %ah
  44062. + cmpb %bh, %ah
  44063. + jnz LookupLoop
  44064. + movl window(%esp), %eax
  44065. + movw (%eax,%ecx), %ax
  44066. + cmpw scanstart(%esp), %ax
  44067. + jnz LookupLoop
  44068. +
  44069. +/* Store the current value of chainlen. */
  44070. +
  44071. + movl %ebx, chainlenscanend(%esp)
  44072. +
  44073. +/* Point %edi to the string under scrutiny, and %esi to the string we */
  44074. +/* are hoping to match it up with. In actuality, %esi and %edi are */
  44075. +/* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  44076. +/* initialized to -(MAX_MATCH_8 - scanalign). */
  44077. +
  44078. + movl window(%esp), %esi
  44079. + movl scan(%esp), %edi
  44080. + addl %ecx, %esi
  44081. + movl scanalign(%esp), %eax
  44082. + movl $(-MAX_MATCH_8), %edx
  44083. + lea MAX_MATCH_8(%edi,%eax), %edi
  44084. + lea MAX_MATCH_8(%esi,%eax), %esi
  44085. +
  44086. +/* Test the strings for equality, 8 bytes at a time. At the end,
  44087. + * adjust %edx so that it is offset to the exact byte that mismatched.
  44088. + *
  44089. + * We already know at this point that the first three bytes of the
  44090. + * strings match each other, and they can be safely passed over before
  44091. + * starting the compare loop. So what this code does is skip over 0-3
  44092. + * bytes, as much as necessary in order to dword-align the %edi
  44093. + * pointer. (%esi will still be misaligned three times out of four.)
  44094. + *
  44095. + * It should be confessed that this loop usually does not represent
  44096. + * much of the total running time. Replacing it with a more
  44097. + * straightforward "rep cmpsb" would not drastically degrade
  44098. + * performance.
  44099. + */
  44100. +LoopCmps:
  44101. + movl (%esi,%edx), %eax
  44102. + movl (%edi,%edx), %ebx
  44103. + xorl %ebx, %eax
  44104. + jnz LeaveLoopCmps
  44105. + movl 4(%esi,%edx), %eax
  44106. + movl 4(%edi,%edx), %ebx
  44107. + xorl %ebx, %eax
  44108. + jnz LeaveLoopCmps4
  44109. + addl $8, %edx
  44110. + jnz LoopCmps
  44111. + jmp LenMaximum
  44112. +LeaveLoopCmps4: addl $4, %edx
  44113. +LeaveLoopCmps: testl $0x0000FFFF, %eax
  44114. + jnz LenLower
  44115. + addl $2, %edx
  44116. + shrl $16, %eax
  44117. +LenLower: subb $1, %al
  44118. + adcl $0, %edx
  44119. +
  44120. +/* Calculate the length of the match. If it is longer than MAX_MATCH, */
  44121. +/* then automatically accept it as the best possible match and leave. */
  44122. +
  44123. + lea (%edi,%edx), %eax
  44124. + movl scan(%esp), %edi
  44125. + subl %edi, %eax
  44126. + cmpl $MAX_MATCH, %eax
  44127. + jge LenMaximum
  44128. +
  44129. +/* If the length of the match is not longer than the best match we */
  44130. +/* have so far, then forget it and return to the lookup loop. */
  44131. +
  44132. + movl deflatestate(%esp), %edx
  44133. + movl bestlen(%esp), %ebx
  44134. + cmpl %ebx, %eax
  44135. + jg LongerMatch
  44136. + movl chainlenscanend(%esp), %ebx
  44137. + movl windowbestlen(%esp), %esi
  44138. + movl dsPrev(%edx), %edi
  44139. + movl wmask(%esp), %edx
  44140. + andl %ecx, %edx
  44141. + jmp LookupLoop
  44142. +
  44143. +/* s->match_start = cur_match; */
  44144. +/* best_len = len; */
  44145. +/* if (len >= nice_match) break; */
  44146. +/* scan_end = *(ushf*)(scan+best_len-1); */
  44147. +
  44148. +LongerMatch: movl nicematch(%esp), %ebx
  44149. + movl %eax, bestlen(%esp)
  44150. + movl %ecx, dsMatchStart(%edx)
  44151. + cmpl %ebx, %eax
  44152. + jge LeaveNow
  44153. + movl window(%esp), %esi
  44154. + addl %eax, %esi
  44155. + movl %esi, windowbestlen(%esp)
  44156. + movl chainlenscanend(%esp), %ebx
  44157. + movw -1(%edi,%eax), %bx
  44158. + movl dsPrev(%edx), %edi
  44159. + movl %ebx, chainlenscanend(%esp)
  44160. + movl wmask(%esp), %edx
  44161. + andl %ecx, %edx
  44162. + jmp LookupLoop
  44163. +
  44164. +/* Accept the current string, with the maximum possible length. */
  44165. +
  44166. +LenMaximum: movl deflatestate(%esp), %edx
  44167. + movl $MAX_MATCH, bestlen(%esp)
  44168. + movl %ecx, dsMatchStart(%edx)
  44169. +
  44170. +/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  44171. +/* return s->lookahead; */
  44172. +
  44173. +LeaveNow:
  44174. + movl deflatestate(%esp), %edx
  44175. + movl bestlen(%esp), %ebx
  44176. + movl dsLookahead(%edx), %eax
  44177. + cmpl %eax, %ebx
  44178. + jg LookaheadRet
  44179. + movl %ebx, %eax
  44180. +LookaheadRet:
  44181. +
  44182. +/* Restore the stack and return from whence we came. */
  44183. +
  44184. + addl $LocalVarsSize, %esp
  44185. + popl %ebx
  44186. + popl %esi
  44187. + popl %edi
  44188. + popl %ebp
  44189. +match_init: ret
  44190. --- /dev/null Tue Mar 11 13:02:56 2003
  44191. +++ linux/net/ipsec/match686.S Mon Feb 9 13:51:03 2004
  44192. @@ -0,0 +1,330 @@
  44193. +/* match.s -- Pentium-Pro-optimized version of longest_match()
  44194. + * Written for zlib 1.1.2
  44195. + * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
  44196. + *
  44197. + * This is free software; you can redistribute it and/or modify it
  44198. + * under the terms of the GNU General Public License.
  44199. + */
  44200. +
  44201. +#ifndef NO_UNDERLINE
  44202. +#define match_init _ipcomp_match_init
  44203. +#define longest_match _ipcomp_longest_match
  44204. +#else
  44205. +#define match_init ipcomp_match_init
  44206. +#define longest_match ipcomp_longest_match
  44207. +#endif
  44208. +
  44209. +#define MAX_MATCH (258)
  44210. +#define MIN_MATCH (3)
  44211. +#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  44212. +#define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  44213. +
  44214. +/* stack frame offsets */
  44215. +
  44216. +#define chainlenwmask 0 /* high word: current chain len */
  44217. + /* low word: s->wmask */
  44218. +#define window 4 /* local copy of s->window */
  44219. +#define windowbestlen 8 /* s->window + bestlen */
  44220. +#define scanstart 16 /* first two bytes of string */
  44221. +#define scanend 12 /* last two bytes of string */
  44222. +#define scanalign 20 /* dword-misalignment of string */
  44223. +#define nicematch 24 /* a good enough match size */
  44224. +#define bestlen 28 /* size of best match so far */
  44225. +#define scan 32 /* ptr to string wanting match */
  44226. +
  44227. +#define LocalVarsSize (36)
  44228. +/* saved ebx 36 */
  44229. +/* saved edi 40 */
  44230. +/* saved esi 44 */
  44231. +/* saved ebp 48 */
  44232. +/* return address 52 */
  44233. +#define deflatestate 56 /* the function arguments */
  44234. +#define curmatch 60
  44235. +
  44236. +/* Offsets for fields in the deflate_state structure. These numbers
  44237. + * are calculated from the definition of deflate_state, with the
  44238. + * assumption that the compiler will dword-align the fields. (Thus,
  44239. + * changing the definition of deflate_state could easily cause this
  44240. + * program to crash horribly, without so much as a warning at
  44241. + * compile time. Sigh.)
  44242. + */
  44243. +#define dsWSize 36
  44244. +#define dsWMask 44
  44245. +#define dsWindow 48
  44246. +#define dsPrev 56
  44247. +#define dsMatchLen 88
  44248. +#define dsPrevMatch 92
  44249. +#define dsStrStart 100
  44250. +#define dsMatchStart 104
  44251. +#define dsLookahead 108
  44252. +#define dsPrevLen 112
  44253. +#define dsMaxChainLen 116
  44254. +#define dsGoodMatch 132
  44255. +#define dsNiceMatch 136
  44256. +
  44257. +
  44258. +.file "match.S"
  44259. +
  44260. +.globl match_init, longest_match
  44261. +
  44262. +.text
  44263. +
  44264. +/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  44265. +
  44266. +longest_match:
  44267. +
  44268. +/* Save registers that the compiler may be using, and adjust %esp to */
  44269. +/* make room for our stack frame. */
  44270. +
  44271. + pushl %ebp
  44272. + pushl %edi
  44273. + pushl %esi
  44274. + pushl %ebx
  44275. + subl $LocalVarsSize, %esp
  44276. +
  44277. +/* Retrieve the function arguments. %ecx will hold cur_match */
  44278. +/* throughout the entire function. %edx will hold the pointer to the */
  44279. +/* deflate_state structure during the function's setup (before */
  44280. +/* entering the main loop). */
  44281. +
  44282. + movl deflatestate(%esp), %edx
  44283. + movl curmatch(%esp), %ecx
  44284. +
  44285. +/* uInt wmask = s->w_mask; */
  44286. +/* unsigned chain_length = s->max_chain_length; */
  44287. +/* if (s->prev_length >= s->good_match) { */
  44288. +/* chain_length >>= 2; */
  44289. +/* } */
  44290. +
  44291. + movl dsPrevLen(%edx), %eax
  44292. + movl dsGoodMatch(%edx), %ebx
  44293. + cmpl %ebx, %eax
  44294. + movl dsWMask(%edx), %eax
  44295. + movl dsMaxChainLen(%edx), %ebx
  44296. + jl LastMatchGood
  44297. + shrl $2, %ebx
  44298. +LastMatchGood:
  44299. +
  44300. +/* chainlen is decremented once beforehand so that the function can */
  44301. +/* use the sign flag instead of the zero flag for the exit test. */
  44302. +/* It is then shifted into the high word, to make room for the wmask */
  44303. +/* value, which it will always accompany. */
  44304. +
  44305. + decl %ebx
  44306. + shll $16, %ebx
  44307. + orl %eax, %ebx
  44308. + movl %ebx, chainlenwmask(%esp)
  44309. +
  44310. +/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  44311. +
  44312. + movl dsNiceMatch(%edx), %eax
  44313. + movl dsLookahead(%edx), %ebx
  44314. + cmpl %eax, %ebx
  44315. + jl LookaheadLess
  44316. + movl %eax, %ebx
  44317. +LookaheadLess: movl %ebx, nicematch(%esp)
  44318. +
  44319. +/* register Bytef *scan = s->window + s->strstart; */
  44320. +
  44321. + movl dsWindow(%edx), %esi
  44322. + movl %esi, window(%esp)
  44323. + movl dsStrStart(%edx), %ebp
  44324. + lea (%esi,%ebp), %edi
  44325. + movl %edi, scan(%esp)
  44326. +
  44327. +/* Determine how many bytes the scan ptr is off from being */
  44328. +/* dword-aligned. */
  44329. +
  44330. + movl %edi, %eax
  44331. + negl %eax
  44332. + andl $3, %eax
  44333. + movl %eax, scanalign(%esp)
  44334. +
  44335. +/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  44336. +/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  44337. +
  44338. + movl dsWSize(%edx), %eax
  44339. + subl $MIN_LOOKAHEAD, %eax
  44340. + subl %eax, %ebp
  44341. + jg LimitPositive
  44342. + xorl %ebp, %ebp
  44343. +LimitPositive:
  44344. +
  44345. +/* int best_len = s->prev_length; */
  44346. +
  44347. + movl dsPrevLen(%edx), %eax
  44348. + movl %eax, bestlen(%esp)
  44349. +
  44350. +/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  44351. +
  44352. + addl %eax, %esi
  44353. + movl %esi, windowbestlen(%esp)
  44354. +
  44355. +/* register ush scan_start = *(ushf*)scan; */
  44356. +/* register ush scan_end = *(ushf*)(scan+best_len-1); */
  44357. +/* Posf *prev = s->prev; */
  44358. +
  44359. + movzwl (%edi), %ebx
  44360. + movl %ebx, scanstart(%esp)
  44361. + movzwl -1(%edi,%eax), %ebx
  44362. + movl %ebx, scanend(%esp)
  44363. + movl dsPrev(%edx), %edi
  44364. +
  44365. +/* Jump into the main loop. */
  44366. +
  44367. + movl chainlenwmask(%esp), %edx
  44368. + jmp LoopEntry
  44369. +
  44370. +.balign 16
  44371. +
  44372. +/* do {
  44373. + * match = s->window + cur_match;
  44374. + * if (*(ushf*)(match+best_len-1) != scan_end ||
  44375. + * *(ushf*)match != scan_start) continue;
  44376. + * [...]
  44377. + * } while ((cur_match = prev[cur_match & wmask]) > limit
  44378. + * && --chain_length != 0);
  44379. + *
  44380. + * Here is the inner loop of the function. The function will spend the
  44381. + * majority of its time in this loop, and majority of that time will
  44382. + * be spent in the first ten instructions.
  44383. + *
  44384. + * Within this loop:
  44385. + * %ebx = scanend
  44386. + * %ecx = curmatch
  44387. + * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
  44388. + * %esi = windowbestlen - i.e., (window + bestlen)
  44389. + * %edi = prev
  44390. + * %ebp = limit
  44391. + */
  44392. +LookupLoop:
  44393. + andl %edx, %ecx
  44394. + movzwl (%edi,%ecx,2), %ecx
  44395. + cmpl %ebp, %ecx
  44396. + jbe LeaveNow
  44397. + subl $0x00010000, %edx
  44398. + js LeaveNow
  44399. +LoopEntry: movzwl -1(%esi,%ecx), %eax
  44400. + cmpl %ebx, %eax
  44401. + jnz LookupLoop
  44402. + movl window(%esp), %eax
  44403. + movzwl (%eax,%ecx), %eax
  44404. + cmpl scanstart(%esp), %eax
  44405. + jnz LookupLoop
  44406. +
  44407. +/* Store the current value of chainlen. */
  44408. +
  44409. + movl %edx, chainlenwmask(%esp)
  44410. +
  44411. +/* Point %edi to the string under scrutiny, and %esi to the string we */
  44412. +/* are hoping to match it up with. In actuality, %esi and %edi are */
  44413. +/* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  44414. +/* initialized to -(MAX_MATCH_8 - scanalign). */
  44415. +
  44416. + movl window(%esp), %esi
  44417. + movl scan(%esp), %edi
  44418. + addl %ecx, %esi
  44419. + movl scanalign(%esp), %eax
  44420. + movl $(-MAX_MATCH_8), %edx
  44421. + lea MAX_MATCH_8(%edi,%eax), %edi
  44422. + lea MAX_MATCH_8(%esi,%eax), %esi
  44423. +
  44424. +/* Test the strings for equality, 8 bytes at a time. At the end,
  44425. + * adjust %edx so that it is offset to the exact byte that mismatched.
  44426. + *
  44427. + * We already know at this point that the first three bytes of the
  44428. + * strings match each other, and they can be safely passed over before
  44429. + * starting the compare loop. So what this code does is skip over 0-3
  44430. + * bytes, as much as necessary in order to dword-align the %edi
  44431. + * pointer. (%esi will still be misaligned three times out of four.)
  44432. + *
  44433. + * It should be confessed that this loop usually does not represent
  44434. + * much of the total running time. Replacing it with a more
  44435. + * straightforward "rep cmpsb" would not drastically degrade
  44436. + * performance.
  44437. + */
  44438. +LoopCmps:
  44439. + movl (%esi,%edx), %eax
  44440. + xorl (%edi,%edx), %eax
  44441. + jnz LeaveLoopCmps
  44442. + movl 4(%esi,%edx), %eax
  44443. + xorl 4(%edi,%edx), %eax
  44444. + jnz LeaveLoopCmps4
  44445. + addl $8, %edx
  44446. + jnz LoopCmps
  44447. + jmp LenMaximum
  44448. +LeaveLoopCmps4: addl $4, %edx
  44449. +LeaveLoopCmps: testl $0x0000FFFF, %eax
  44450. + jnz LenLower
  44451. + addl $2, %edx
  44452. + shrl $16, %eax
  44453. +LenLower: subb $1, %al
  44454. + adcl $0, %edx
  44455. +
  44456. +/* Calculate the length of the match. If it is longer than MAX_MATCH, */
  44457. +/* then automatically accept it as the best possible match and leave. */
  44458. +
  44459. + lea (%edi,%edx), %eax
  44460. + movl scan(%esp), %edi
  44461. + subl %edi, %eax
  44462. + cmpl $MAX_MATCH, %eax
  44463. + jge LenMaximum
  44464. +
  44465. +/* If the length of the match is not longer than the best match we */
  44466. +/* have so far, then forget it and return to the lookup loop. */
  44467. +
  44468. + movl deflatestate(%esp), %edx
  44469. + movl bestlen(%esp), %ebx
  44470. + cmpl %ebx, %eax
  44471. + jg LongerMatch
  44472. + movl windowbestlen(%esp), %esi
  44473. + movl dsPrev(%edx), %edi
  44474. + movl scanend(%esp), %ebx
  44475. + movl chainlenwmask(%esp), %edx
  44476. + jmp LookupLoop
  44477. +
  44478. +/* s->match_start = cur_match; */
  44479. +/* best_len = len; */
  44480. +/* if (len >= nice_match) break; */
  44481. +/* scan_end = *(ushf*)(scan+best_len-1); */
  44482. +
  44483. +LongerMatch: movl nicematch(%esp), %ebx
  44484. + movl %eax, bestlen(%esp)
  44485. + movl %ecx, dsMatchStart(%edx)
  44486. + cmpl %ebx, %eax
  44487. + jge LeaveNow
  44488. + movl window(%esp), %esi
  44489. + addl %eax, %esi
  44490. + movl %esi, windowbestlen(%esp)
  44491. + movzwl -1(%edi,%eax), %ebx
  44492. + movl dsPrev(%edx), %edi
  44493. + movl %ebx, scanend(%esp)
  44494. + movl chainlenwmask(%esp), %edx
  44495. + jmp LookupLoop
  44496. +
  44497. +/* Accept the current string, with the maximum possible length. */
  44498. +
  44499. +LenMaximum: movl deflatestate(%esp), %edx
  44500. + movl $MAX_MATCH, bestlen(%esp)
  44501. + movl %ecx, dsMatchStart(%edx)
  44502. +
  44503. +/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  44504. +/* return s->lookahead; */
  44505. +
  44506. +LeaveNow:
  44507. + movl deflatestate(%esp), %edx
  44508. + movl bestlen(%esp), %ebx
  44509. + movl dsLookahead(%edx), %eax
  44510. + cmpl %eax, %ebx
  44511. + jg LookaheadRet
  44512. + movl %ebx, %eax
  44513. +LookaheadRet:
  44514. +
  44515. +/* Restore the stack and return from whence we came. */
  44516. +
  44517. + addl $LocalVarsSize, %esp
  44518. + popl %ebx
  44519. + popl %esi
  44520. + popl %edi
  44521. + popl %ebp
  44522. +match_init: ret
  44523. --- /dev/null Tue Mar 11 13:02:56 2003
  44524. +++ linux/net/ipsec/null/ipsec_alg_null.c Mon Feb 9 13:51:03 2004
  44525. @@ -0,0 +1,145 @@
  44526. +/*
  44527. + * ipsec_alg NULL cipher stubs
  44528. + *
  44529. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  44530. + *
  44531. + * $Id: ipsec_alg_null.c,v 1.1.2.1 2006/10/11 18:14:33 paul Exp $
  44532. + *
  44533. + * This program is free software; you can redistribute it and/or modify it
  44534. + * under the terms of the GNU General Public License as published by the
  44535. + * Free Software Foundation; either version 2 of the License, or (at your
  44536. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  44537. + *
  44538. + * This program is distributed in the hope that it will be useful, but
  44539. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  44540. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  44541. + * for more details.
  44542. + *
  44543. + */
  44544. +#include <linux/config.h>
  44545. +#include <linux/version.h>
  44546. +
  44547. +/*
  44548. + * special case: ipsec core modular with this static algo inside:
  44549. + * must avoid MODULE magic for this file
  44550. + */
  44551. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_NULL)
  44552. +#undef MODULE
  44553. +#endif
  44554. +
  44555. +#include <linux/module.h>
  44556. +#include <linux/init.h>
  44557. +
  44558. +#include <linux/kernel.h> /* printk() */
  44559. +#include <linux/errno.h> /* error codes */
  44560. +#include <linux/types.h> /* size_t */
  44561. +#include <linux/string.h>
  44562. +
  44563. +/* Check if __exit is defined, if not null it */
  44564. +#ifndef __exit
  44565. +#define __exit
  44566. +#endif
  44567. +
  44568. +/* Low freeswan header coupling */
  44569. +#include "openswan/ipsec_alg.h"
  44570. +
  44571. +#define ESP_NULL 11 /* from ipsec drafts */
  44572. +#define ESP_NULL_BLK_LEN 1
  44573. +
  44574. +MODULE_AUTHOR("JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>");
  44575. +static int debug_null=0;
  44576. +static int test_null=0;
  44577. +#ifdef module_param
  44578. +module_param(debug_null, int, 0600);
  44579. +module_param(test_null, int, 0600);
  44580. +#else
  44581. +MODULE_PARM(debug_null, "i");
  44582. +MODULE_PARM(test_null, "i");
  44583. +#endif
  44584. +
  44585. +typedef int null_context;
  44586. +
  44587. +struct null_eks{
  44588. + null_context null_ctx;
  44589. +};
  44590. +static int _null_set_key(struct ipsec_alg_enc *alg,
  44591. + __u8 * key_e, const __u8 * key,
  44592. + size_t keysize) {
  44593. + null_context *ctx=&((struct null_eks*)key_e)->null_ctx;
  44594. + if (debug_null > 0)
  44595. + printk(KERN_DEBUG "klips_debug:_null_set_key:"
  44596. + "key_e=%p key=%p keysize=%d\n",
  44597. + key_e, key, keysize);
  44598. + *ctx = 1;
  44599. + return 0;
  44600. +}
  44601. +static int _null_cbc_encrypt(struct ipsec_alg_enc *alg,
  44602. + __u8 * key_e, __u8 * in, int ilen, const __u8 * iv,
  44603. + int encrypt) {
  44604. + null_context *ctx=&((struct null_eks*)key_e)->null_ctx;
  44605. + if (debug_null > 0)
  44606. + printk(KERN_DEBUG "klips_debug:_null_cbc_encrypt:"
  44607. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  44608. + key_e, in, ilen, iv, encrypt);
  44609. + (*ctx)++;
  44610. + return ilen;
  44611. +}
  44612. +static struct ipsec_alg_enc ipsec_alg_NULL = {
  44613. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  44614. + ixt_refcnt: ATOMIC_INIT(0),
  44615. + ixt_name: "null",
  44616. + ixt_blocksize: ESP_NULL_BLK_LEN,
  44617. + ixt_support: {
  44618. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  44619. + ias_id: ESP_NULL,
  44620. + ias_ivlen: 0,
  44621. + ias_keyminbits: 0,
  44622. + ias_keymaxbits: 0,
  44623. + },
  44624. + },
  44625. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44626. + ixt_module: THIS_MODULE,
  44627. +#endif
  44628. + ixt_e_keylen: 0,
  44629. + ixt_e_ctx_size: sizeof(null_context),
  44630. + ixt_e_set_key: _null_set_key,
  44631. + ixt_e_cbc_encrypt:_null_cbc_encrypt,
  44632. +};
  44633. +
  44634. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44635. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_null_init )
  44636. +#else
  44637. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_null_init )
  44638. +#endif
  44639. +{
  44640. + int ret, test_ret;
  44641. + ret=register_ipsec_alg_enc(&ipsec_alg_NULL);
  44642. + printk("ipsec_null_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  44643. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44644. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44645. + ipsec_alg_NULL.ixt_common.ixt_name,
  44646. + ret);
  44647. + if (ret==0 && test_null) {
  44648. + test_ret=ipsec_alg_test(
  44649. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44650. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44651. + test_null);
  44652. + printk("ipsec_null_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  44653. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44654. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44655. + test_ret);
  44656. + }
  44657. + return ret;
  44658. +}
  44659. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44660. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_null_fini )
  44661. +#else
  44662. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_null_fini )
  44663. +#endif
  44664. +{
  44665. + unregister_ipsec_alg_enc(&ipsec_alg_NULL);
  44666. + return;
  44667. +}
  44668. +#ifdef MODULE_LICENSE
  44669. +MODULE_LICENSE("GPL");
  44670. +#endif
  44671. --- /dev/null Tue Mar 11 13:02:56 2003
  44672. +++ linux/net/ipsec/pfkey_v2.c Mon Feb 9 13:51:03 2004
  44673. @@ -0,0 +1,2022 @@
  44674. +/*
  44675. + * @(#) RFC2367 PF_KEYv2 Key management API domain socket I/F
  44676. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  44677. + *
  44678. + * This program is free software; you can redistribute it and/or modify it
  44679. + * under the terms of the GNU General Public License as published by the
  44680. + * Free Software Foundation; either version 2 of the License, or (at your
  44681. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  44682. + *
  44683. + * This program is distributed in the hope that it will be useful, but
  44684. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  44685. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  44686. + * for more details.
  44687. + *
  44688. + * RCSID $Id: pfkey_v2.c,v 1.97.2.12 2006/11/24 05:43:29 paul Exp $
  44689. + */
  44690. +
  44691. +/*
  44692. + * Template from /usr/src/linux-2.0.36/net/unix/af_unix.c.
  44693. + * Hints from /usr/src/linux-2.0.36/net/ipv4/udp.c.
  44694. + */
  44695. +
  44696. +#define __NO_VERSION__
  44697. +#include <linux/module.h>
  44698. +#include <linux/version.h>
  44699. +#ifndef AUTOCONF_INCLUDED
  44700. +#include <linux/config.h>
  44701. +#endif
  44702. +#include <linux/kernel.h>
  44703. +
  44704. +#include "openswan/ipsec_param.h"
  44705. +
  44706. +#include <linux/major.h>
  44707. +#include <linux/signal.h>
  44708. +#include <linux/sched.h>
  44709. +#include <linux/errno.h>
  44710. +#include <linux/string.h>
  44711. +#include <linux/stat.h>
  44712. +#include <linux/socket.h>
  44713. +#include <linux/un.h>
  44714. +#include <linux/fcntl.h>
  44715. +#include <linux/termios.h>
  44716. +#include <linux/socket.h>
  44717. +#include <linux/sockios.h>
  44718. +#include <linux/net.h> /* struct socket */
  44719. +#include <linux/in.h>
  44720. +#include <linux/fs.h>
  44721. +#ifdef MALLOC_SLAB
  44722. +# include <linux/slab.h> /* kmalloc() */
  44723. +#else /* MALLOC_SLAB */
  44724. +# include <linux/malloc.h> /* kmalloc() */
  44725. +#endif /* MALLOC_SLAB */
  44726. +#include <asm/segment.h>
  44727. +#include <linux/skbuff.h>
  44728. +#include <linux/netdevice.h>
  44729. +#include <net/sock.h> /* struct sock */
  44730. +#include <net/protocol.h>
  44731. +/* #include <net/tcp.h> */
  44732. +#include <net/af_unix.h>
  44733. +#ifdef CONFIG_PROC_FS
  44734. +# include <linux/proc_fs.h>
  44735. +#endif /* CONFIG_PROC_FS */
  44736. +
  44737. +#include <linux/types.h>
  44738. +
  44739. +#include <openswan.h>
  44740. +
  44741. +#include "openswan/radij.h"
  44742. +#include "openswan/ipsec_encap.h"
  44743. +#include "openswan/ipsec_sa.h"
  44744. +
  44745. +#include <pfkeyv2.h>
  44746. +#include <pfkey.h>
  44747. +
  44748. +#include "openswan/ipsec_proto.h"
  44749. +#include "openswan/ipsec_kern24.h"
  44750. +
  44751. +#ifdef CONFIG_KLIPS_DEBUG
  44752. +int debug_pfkey = 0;
  44753. +extern int sysctl_ipsec_debug_verbose;
  44754. +#endif /* CONFIG_KLIPS_DEBUG */
  44755. +
  44756. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  44757. +
  44758. +#ifndef SOCKOPS_WRAPPED
  44759. +#define SOCKOPS_WRAPPED(name) name
  44760. +#endif /* SOCKOPS_WRAPPED */
  44761. +
  44762. +#ifdef NET_26
  44763. +static rwlock_t pfkey_sock_lock = RW_LOCK_UNLOCKED;
  44764. +HLIST_HEAD(pfkey_sock_list);
  44765. +static DECLARE_WAIT_QUEUE_HEAD(pfkey_sock_wait);
  44766. +static atomic_t pfkey_sock_users = ATOMIC_INIT(0);
  44767. +#else
  44768. +struct sock *pfkey_sock_list = NULL;
  44769. +#endif
  44770. +
  44771. +struct supported_list *pfkey_supported_list[SADB_SATYPE_MAX+1];
  44772. +
  44773. +struct socket_list *pfkey_open_sockets = NULL;
  44774. +struct socket_list *pfkey_registered_sockets[SADB_SATYPE_MAX+1];
  44775. +
  44776. +int pfkey_msg_interp(struct sock *, struct sadb_msg *, struct sadb_msg **);
  44777. +
  44778. +DEBUG_NO_STATIC int pfkey_create(struct socket *sock, int protocol);
  44779. +DEBUG_NO_STATIC int pfkey_shutdown(struct socket *sock, int mode);
  44780. +DEBUG_NO_STATIC int pfkey_release(struct socket *sock);
  44781. +
  44782. +#ifdef NET_26
  44783. +DEBUG_NO_STATIC int pfkey_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len);
  44784. +DEBUG_NO_STATIC int pfkey_recvmsg(struct kiocb *kiocb, struct socket *sock, struct msghdr *msg
  44785. + , size_t size, int flags);
  44786. +#else
  44787. +DEBUG_NO_STATIC int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm);
  44788. +DEBUG_NO_STATIC int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm);
  44789. +#endif
  44790. +
  44791. +struct net_proto_family pfkey_family_ops = {
  44792. +#ifdef NETDEV_23
  44793. + .family = PF_KEY,
  44794. + .create = pfkey_create,
  44795. +#ifdef NET_26
  44796. + .owner = THIS_MODULE,
  44797. +#endif
  44798. +#else
  44799. + PF_KEY,
  44800. + pfkey_create
  44801. +#endif
  44802. +};
  44803. +
  44804. +struct proto_ops SOCKOPS_WRAPPED(pfkey_ops) = {
  44805. +#ifdef NETDEV_23
  44806. + family: PF_KEY,
  44807. +#ifdef NET_26
  44808. + owner: THIS_MODULE,
  44809. +#endif
  44810. + release: pfkey_release,
  44811. + bind: sock_no_bind,
  44812. + connect: sock_no_connect,
  44813. + socketpair: sock_no_socketpair,
  44814. + accept: sock_no_accept,
  44815. + getname: sock_no_getname,
  44816. + poll: datagram_poll,
  44817. + ioctl: sock_no_ioctl,
  44818. + listen: sock_no_listen,
  44819. + shutdown: pfkey_shutdown,
  44820. + setsockopt: sock_no_setsockopt,
  44821. + getsockopt: sock_no_getsockopt,
  44822. + sendmsg: pfkey_sendmsg,
  44823. + recvmsg: pfkey_recvmsg,
  44824. + mmap: sock_no_mmap,
  44825. +#else /* NETDEV_23 */
  44826. + PF_KEY,
  44827. + sock_no_dup,
  44828. + pfkey_release,
  44829. + sock_no_bind,
  44830. + sock_no_connect,
  44831. + sock_no_socketpair,
  44832. + sock_no_accept,
  44833. + sock_no_getname,
  44834. + datagram_poll,
  44835. + sock_no_ioctl,
  44836. + sock_no_listen,
  44837. + pfkey_shutdown,
  44838. + sock_no_setsockopt,
  44839. + sock_no_getsockopt,
  44840. + sock_no_fcntl,
  44841. + pfkey_sendmsg,
  44842. + pfkey_recvmsg
  44843. +#endif /* NETDEV_23 */
  44844. +};
  44845. +
  44846. +#ifdef NETDEV_23
  44847. +#include <linux/smp_lock.h>
  44848. +SOCKOPS_WRAP(pfkey, PF_KEY);
  44849. +#endif /* NETDEV_23 */
  44850. +
  44851. +#ifdef NET_26
  44852. +static void pfkey_sock_list_grab(void)
  44853. +{
  44854. + write_lock_bh(&pfkey_sock_lock);
  44855. +
  44856. + if (atomic_read(&pfkey_sock_users)) {
  44857. + DECLARE_WAITQUEUE(wait, current);
  44858. +
  44859. + add_wait_queue_exclusive(&pfkey_sock_wait, &wait);
  44860. + for(;;) {
  44861. + set_current_state(TASK_UNINTERRUPTIBLE);
  44862. + if (atomic_read(&pfkey_sock_users) == 0)
  44863. + break;
  44864. + write_unlock_bh(&pfkey_sock_lock);
  44865. + schedule();
  44866. + write_lock_bh(&pfkey_sock_lock);
  44867. + }
  44868. +
  44869. + __set_current_state(TASK_RUNNING);
  44870. + remove_wait_queue(&pfkey_sock_wait, &wait);
  44871. + }
  44872. +}
  44873. +
  44874. +static __inline__ void pfkey_sock_list_ungrab(void)
  44875. +{
  44876. + write_unlock_bh(&pfkey_sock_lock);
  44877. + wake_up(&pfkey_sock_wait);
  44878. +}
  44879. +
  44880. +static __inline__ void pfkey_lock_sock_list(void)
  44881. +{
  44882. + /* read_lock() synchronizes us to pfkey_table_grab */
  44883. +
  44884. + read_lock(&pfkey_sock_lock);
  44885. + atomic_inc(&pfkey_sock_users);
  44886. + read_unlock(&pfkey_sock_lock);
  44887. +}
  44888. +
  44889. +static __inline__ void pfkey_unlock_sock_list(void)
  44890. +{
  44891. + if (atomic_dec_and_test(&pfkey_sock_users))
  44892. + wake_up(&pfkey_sock_wait);
  44893. +}
  44894. +#endif
  44895. +
  44896. +int
  44897. +pfkey_list_remove_socket(struct socket *socketp, struct socket_list **sockets)
  44898. +{
  44899. + struct socket_list *socket_listp,*prev;
  44900. +
  44901. + if(!socketp) {
  44902. + KLIPS_PRINT(debug_pfkey,
  44903. + "klips_debug:pfkey_list_remove_socket: "
  44904. + "NULL socketp handed in, failed.\n");
  44905. + return -EINVAL;
  44906. + }
  44907. +
  44908. + if(!sockets) {
  44909. + KLIPS_PRINT(debug_pfkey,
  44910. + "klips_debug:pfkey_list_remove_socket: "
  44911. + "NULL sockets list handed in, failed.\n");
  44912. + return -EINVAL;
  44913. + }
  44914. +
  44915. + socket_listp = *sockets;
  44916. + prev = NULL;
  44917. +
  44918. + KLIPS_PRINT(debug_pfkey,
  44919. + "klips_debug:pfkey_list_remove_socket: "
  44920. + "removing sock=0p%p\n",
  44921. + socketp);
  44922. +
  44923. + while(socket_listp != NULL) {
  44924. + if(socket_listp->socketp == socketp) {
  44925. + if(prev != NULL) {
  44926. + prev->next = socket_listp->next;
  44927. + } else {
  44928. + *sockets = socket_listp->next;
  44929. + }
  44930. +
  44931. + kfree((void*)socket_listp);
  44932. +
  44933. + break;
  44934. + }
  44935. + prev = socket_listp;
  44936. + socket_listp = socket_listp->next;
  44937. + }
  44938. +
  44939. + return 0;
  44940. +}
  44941. +
  44942. +int
  44943. +pfkey_list_insert_socket(struct socket *socketp, struct socket_list **sockets)
  44944. +{
  44945. + struct socket_list *socket_listp;
  44946. +
  44947. + if(!socketp) {
  44948. + KLIPS_PRINT(debug_pfkey,
  44949. + "klips_debug:pfkey_list_insert_socket: "
  44950. + "NULL socketp handed in, failed.\n");
  44951. + return -EINVAL;
  44952. + }
  44953. +
  44954. + if(!sockets) {
  44955. + KLIPS_PRINT(debug_pfkey,
  44956. + "klips_debug:pfkey_list_insert_socket: "
  44957. + "NULL sockets list handed in, failed.\n");
  44958. + return -EINVAL;
  44959. + }
  44960. +
  44961. + KLIPS_PRINT(debug_pfkey,
  44962. + "klips_debug:pfkey_list_insert_socket: "
  44963. + "allocating %lu bytes for socketp=0p%p\n",
  44964. + (unsigned long) sizeof(struct socket_list),
  44965. + socketp);
  44966. +
  44967. + if((socket_listp = (struct socket_list *)kmalloc(sizeof(struct socket_list), GFP_KERNEL)) == NULL) {
  44968. + KLIPS_PRINT(debug_pfkey,
  44969. + "klips_debug:pfkey_list_insert_socket: "
  44970. + "memory allocation error.\n");
  44971. + return -ENOMEM;
  44972. + }
  44973. +
  44974. + socket_listp->socketp = socketp;
  44975. + socket_listp->next = *sockets;
  44976. + *sockets = socket_listp;
  44977. +
  44978. + return 0;
  44979. +}
  44980. +
  44981. +int
  44982. +pfkey_list_remove_supported(struct ipsec_alg_supported *supported, struct supported_list **supported_list)
  44983. +{
  44984. + struct supported_list *supported_listp = *supported_list, *prev = NULL;
  44985. +
  44986. + if(!supported) {
  44987. + KLIPS_PRINT(debug_pfkey,
  44988. + "klips_debug:pfkey_list_remove_supported: "
  44989. + "NULL supported handed in, failed.\n");
  44990. + return -EINVAL;
  44991. + }
  44992. +
  44993. + if(!supported_list) {
  44994. + KLIPS_PRINT(debug_pfkey,
  44995. + "klips_debug:pfkey_list_remove_supported: "
  44996. + "NULL supported_list handed in, failed.\n");
  44997. + return -EINVAL;
  44998. + }
  44999. +
  45000. + KLIPS_PRINT(debug_pfkey,
  45001. + "klips_debug:pfkey_list_remove_supported: "
  45002. + "removing supported=0p%p\n",
  45003. + supported);
  45004. +
  45005. + while(supported_listp != NULL) {
  45006. + if(supported_listp->supportedp == supported) {
  45007. + if(prev != NULL) {
  45008. + prev->next = supported_listp->next;
  45009. + } else {
  45010. + *supported_list = supported_listp->next;
  45011. + }
  45012. +
  45013. + kfree((void*)supported_listp);
  45014. +
  45015. + break;
  45016. + }
  45017. + prev = supported_listp;
  45018. + supported_listp = supported_listp->next;
  45019. + }
  45020. +
  45021. + return 0;
  45022. +}
  45023. +
  45024. +int
  45025. +pfkey_list_insert_supported(struct ipsec_alg_supported *supported
  45026. + , struct supported_list **supported_list)
  45027. +{
  45028. + struct supported_list *supported_listp;
  45029. +
  45030. + if(!supported) {
  45031. + KLIPS_PRINT(debug_pfkey,
  45032. + "klips_debug:pfkey_list_insert_supported: "
  45033. + "NULL supported handed in, failed.\n");
  45034. + return -EINVAL;
  45035. + }
  45036. +
  45037. + if(!supported_list) {
  45038. + KLIPS_PRINT(debug_pfkey,
  45039. + "klips_debug:pfkey_list_insert_supported: "
  45040. + "NULL supported_list handed in, failed.\n");
  45041. + return -EINVAL;
  45042. + }
  45043. +
  45044. + KLIPS_PRINT(debug_pfkey,
  45045. + "klips_debug:pfkey_list_insert_supported: "
  45046. + "allocating %lu bytes for incoming, supported=0p%p, supported_list=0p%p\n",
  45047. + (unsigned long) sizeof(struct supported_list),
  45048. + supported,
  45049. + supported_list);
  45050. +
  45051. + supported_listp = (struct supported_list *)kmalloc(sizeof(struct supported_list), GFP_KERNEL);
  45052. +
  45053. + if(supported_listp == NULL)
  45054. + {
  45055. + KLIPS_PRINT(debug_pfkey,
  45056. + "klips_debug:pfkey_list_insert_supported: "
  45057. + "memory allocation error.\n");
  45058. + return -ENOMEM;
  45059. + }
  45060. +
  45061. + supported_listp->supportedp = supported;
  45062. + supported_listp->next = *supported_list;
  45063. + *supported_list = supported_listp;
  45064. + KLIPS_PRINT(debug_pfkey,
  45065. + "klips_debug:pfkey_list_insert_supported: "
  45066. + "outgoing, supported=0p%p, supported_list=0p%p\n",
  45067. + supported,
  45068. + supported_list);
  45069. +
  45070. + return 0;
  45071. +}
  45072. +
  45073. +#ifdef NET_26
  45074. +DEBUG_NO_STATIC void
  45075. +pfkey_insert_socket(struct sock *sk)
  45076. +{
  45077. + KLIPS_PRINT(debug_pfkey,
  45078. + "klips_debug:pfkey_insert_socket: "
  45079. + "sk=0p%p\n",
  45080. + sk);
  45081. + pfkey_sock_list_grab();
  45082. + sk_add_node(sk, &pfkey_sock_list);
  45083. + pfkey_sock_list_ungrab();
  45084. +}
  45085. +
  45086. +DEBUG_NO_STATIC void
  45087. +pfkey_remove_socket(struct sock *sk)
  45088. +{
  45089. + KLIPS_PRINT(debug_pfkey,
  45090. + "klips_debug:pfkey_remove_socket: 0p%p\n", sk);
  45091. + pfkey_sock_list_grab();
  45092. + sk_del_node_init(sk);
  45093. + pfkey_sock_list_ungrab();
  45094. + return;
  45095. +}
  45096. +#else
  45097. +
  45098. +DEBUG_NO_STATIC void
  45099. +pfkey_insert_socket(struct sock *sk)
  45100. +{
  45101. + KLIPS_PRINT(debug_pfkey,
  45102. + "klips_debug:pfkey_insert_socket: "
  45103. + "sk=0p%p\n",
  45104. + sk);
  45105. + cli();
  45106. + sk->next=pfkey_sock_list;
  45107. + pfkey_sock_list=sk;
  45108. + sti();
  45109. +}
  45110. +DEBUG_NO_STATIC void
  45111. +pfkey_remove_socket(struct sock *sk)
  45112. +{
  45113. + struct sock **s;
  45114. +
  45115. + s = NULL;
  45116. + KLIPS_PRINT(debug_pfkey,
  45117. + "klips_debug:pfkey_remove_socket: .\n");
  45118. +
  45119. + cli();
  45120. + s=&pfkey_sock_list;
  45121. +
  45122. + while(*s!=NULL) {
  45123. + if(*s==sk) {
  45124. + *s=sk->next;
  45125. + sk->next=NULL;
  45126. + sti();
  45127. + KLIPS_PRINT(debug_pfkey,
  45128. + "klips_debug:pfkey_remove_socket: "
  45129. + "succeeded.\n");
  45130. + return;
  45131. + }
  45132. + s=&((*s)->next);
  45133. + }
  45134. + sti();
  45135. +
  45136. + KLIPS_PRINT(debug_pfkey,
  45137. + "klips_debug:pfkey_remove_socket: "
  45138. + "not found.\n");
  45139. + return;
  45140. +}
  45141. +#endif
  45142. +
  45143. +DEBUG_NO_STATIC void
  45144. +pfkey_destroy_socket(struct sock *sk)
  45145. +{
  45146. + struct sk_buff *skb;
  45147. +
  45148. + KLIPS_PRINT(debug_pfkey,
  45149. + "klips_debug:pfkey_destroy_socket: 0p%p\n",sk);
  45150. + pfkey_remove_socket(sk);
  45151. +
  45152. + KLIPS_PRINT(debug_pfkey,
  45153. + "klips_debug:pfkey_destroy_socket: "
  45154. + "pfkey_remove_socket called, sk=0p%p\n",sk);
  45155. +
  45156. + KLIPS_PRINT(debug_pfkey,
  45157. + "klips_debug:pfkey_destroy_socket: "
  45158. + "sk(0p%p)->(&0p%p)receive_queue.{next=0p%p,prev=0p%p}.\n",
  45159. + sk,
  45160. + &(sk->sk_receive_queue),
  45161. + sk->sk_receive_queue.next,
  45162. + sk->sk_receive_queue.prev);
  45163. +
  45164. + while(sk && ((skb=skb_dequeue(&(sk->sk_receive_queue)))!=NULL)) {
  45165. +#ifdef CONFIG_KLIPS_DEBUG
  45166. + if(debug_pfkey && sysctl_ipsec_debug_verbose) {
  45167. + KLIPS_PRINT(debug_pfkey,
  45168. + "klips_debug:pfkey_destroy_socket: "
  45169. + "skb=0p%p dequeued.\n", skb);
  45170. + printk(KERN_INFO "klips_debug:pfkey_destroy_socket: "
  45171. + "pfkey_skb contents:");
  45172. + printk(" next:0p%p", skb->next);
  45173. + printk(" prev:0p%p", skb->prev);
  45174. + printk(" sk:0p%p", skb->sk);
  45175. + printk(" dev:0p%p", skb->dev);
  45176. + if(skb->dev) {
  45177. + if(skb->dev->name) {
  45178. + printk(" dev->name:%s", skb->dev->name);
  45179. + } else {
  45180. + printk(" dev->name:NULL?");
  45181. + }
  45182. + } else {
  45183. + printk(" dev:NULL");
  45184. + }
  45185. + printk(" h:0p%p", skb->h.raw);
  45186. + printk(" nh:0p%p", skb->nh.raw);
  45187. + printk(" mac:0p%p", skb->mac.raw);
  45188. + printk(" dst:0p%p", skb->dst);
  45189. + if(sysctl_ipsec_debug_verbose) {
  45190. + int i;
  45191. +
  45192. + printk(" cb");
  45193. + for(i=0; i<48; i++) {
  45194. + printk(":%2x", skb->cb[i]);
  45195. + }
  45196. + }
  45197. + printk(" len:%d", skb->len);
  45198. + printk(" csum:%d", skb->csum);
  45199. +#ifndef NETDEV_23
  45200. + printk(" used:%d", skb->used);
  45201. + printk(" is_clone:%d", skb->is_clone);
  45202. +#endif /* NETDEV_23 */
  45203. + printk(" cloned:%d", skb->cloned);
  45204. + printk(" pkt_type:%d", skb->pkt_type);
  45205. + printk(" ip_summed:%d", skb->ip_summed);
  45206. + printk(" priority:%d", skb->priority);
  45207. + printk(" protocol:%d", skb->protocol);
  45208. +#ifdef HAVE_SOCK_SECURITY
  45209. + printk(" security:%d", skb->security);
  45210. +#endif
  45211. + printk(" truesize:%d", skb->truesize);
  45212. + printk(" head:0p%p", skb->head);
  45213. + printk(" data:0p%p", skb->data);
  45214. + printk(" tail:0p%p", skb->tail);
  45215. + printk(" end:0p%p", skb->end);
  45216. + if(sysctl_ipsec_debug_verbose) {
  45217. + unsigned char* i;
  45218. + printk(" data");
  45219. + for(i = skb->head; i < skb->end; i++) {
  45220. + printk(":%2x", (unsigned char)(*(i)));
  45221. + }
  45222. + }
  45223. + printk(" destructor:0p%p", skb->destructor);
  45224. + printk("\n");
  45225. + }
  45226. +#endif /* CONFIG_KLIPS_DEBUG */
  45227. + KLIPS_PRINT(debug_pfkey,
  45228. + "klips_debug:pfkey_destroy_socket: "
  45229. + "skb=0p%p freed.\n",
  45230. + skb);
  45231. + ipsec_kfree_skb(skb);
  45232. + }
  45233. +
  45234. +#ifdef NET_26
  45235. + sock_set_flag(sk, SOCK_DEAD);
  45236. +#else
  45237. + sk->dead = 1;
  45238. +#endif
  45239. + sk_free(sk);
  45240. +
  45241. + KLIPS_PRINT(debug_pfkey,
  45242. + "klips_debug:pfkey_destroy_socket: destroyed.\n");
  45243. +}
  45244. +
  45245. +int
  45246. +pfkey_upmsg(struct socket *sock, struct sadb_msg *pfkey_msg)
  45247. +{
  45248. + int error = 0;
  45249. + struct sk_buff * skb = NULL;
  45250. + struct sock *sk;
  45251. +
  45252. + if(sock == NULL) {
  45253. + KLIPS_PRINT(debug_pfkey,
  45254. + "klips_debug:pfkey_upmsg: "
  45255. + "NULL socket passed in.\n");
  45256. + return -EINVAL;
  45257. + }
  45258. +
  45259. + if(pfkey_msg == NULL) {
  45260. + KLIPS_PRINT(debug_pfkey,
  45261. + "klips_debug:pfkey_upmsg: "
  45262. + "NULL pfkey_msg passed in.\n");
  45263. + return -EINVAL;
  45264. + }
  45265. +
  45266. + sk = sock->sk;
  45267. +
  45268. + if(sk == NULL) {
  45269. + KLIPS_PRINT(debug_pfkey,
  45270. + "klips_debug:pfkey_upmsg: "
  45271. + "NULL sock passed in.\n");
  45272. + return -EINVAL;
  45273. + }
  45274. +
  45275. + KLIPS_PRINT(debug_pfkey,
  45276. + "klips_debug:pfkey_upmsg: "
  45277. + "allocating %d bytes...\n",
  45278. + (int)(pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN));
  45279. + if(!(skb = alloc_skb(pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN, GFP_ATOMIC) )) {
  45280. + KLIPS_PRINT(debug_pfkey,
  45281. + "klips_debug:pfkey_upmsg: "
  45282. + "no buffers left to send up a message.\n");
  45283. + return -ENOBUFS;
  45284. + }
  45285. + KLIPS_PRINT(debug_pfkey,
  45286. + "klips_debug:pfkey_upmsg: "
  45287. + "...allocated at 0p%p.\n",
  45288. + skb);
  45289. +
  45290. + skb->dev = NULL;
  45291. +
  45292. + if(skb_tailroom(skb) < pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {
  45293. + printk(KERN_WARNING "klips_error:pfkey_upmsg: "
  45294. + "tried to skb_put %ld, %d available. This should never happen, please report.\n",
  45295. + (unsigned long int)pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN,
  45296. + skb_tailroom(skb));
  45297. + ipsec_kfree_skb(skb);
  45298. + return -ENOBUFS;
  45299. + }
  45300. + skb->h.raw = skb_put(skb, pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  45301. + memcpy(skb->h.raw, pfkey_msg, pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  45302. +
  45303. + if((error = sock_queue_rcv_skb(sk, skb)) < 0) {
  45304. + skb->sk=NULL;
  45305. + KLIPS_PRINT(debug_pfkey,
  45306. + "klips_debug:pfkey_upmsg: "
  45307. + "error=%d calling sock_queue_rcv_skb with skb=0p%p.\n",
  45308. + error,
  45309. + skb);
  45310. + ipsec_kfree_skb(skb);
  45311. + return error;
  45312. + }
  45313. + return error;
  45314. +}
  45315. +
  45316. +#ifdef NET_26_12_SKALLOC
  45317. +static struct proto key_proto = {
  45318. + .name = "KEY",
  45319. + .owner = THIS_MODULE,
  45320. + .obj_size = sizeof(struct sock),
  45321. +
  45322. +};
  45323. +#endif
  45324. +
  45325. +DEBUG_NO_STATIC int
  45326. +pfkey_create(struct socket *sock, int protocol)
  45327. +{
  45328. + struct sock *sk;
  45329. +
  45330. + if(sock == NULL) {
  45331. + KLIPS_PRINT(debug_pfkey,
  45332. + "klips_debug:pfkey_create: "
  45333. + "socket NULL.\n");
  45334. + return -EINVAL;
  45335. + }
  45336. +
  45337. + KLIPS_PRINT(debug_pfkey,
  45338. + "klips_debug:pfkey_create: "
  45339. + "sock=0p%p type:%d state:%d flags:%ld protocol:%d\n",
  45340. + sock,
  45341. + sock->type,
  45342. + (unsigned int)(sock->state),
  45343. + sock->flags, protocol);
  45344. +
  45345. + if(sock->type != SOCK_RAW) {
  45346. + KLIPS_PRINT(debug_pfkey,
  45347. + "klips_debug:pfkey_create: "
  45348. + "only SOCK_RAW supported.\n");
  45349. + return -ESOCKTNOSUPPORT;
  45350. + }
  45351. +
  45352. + if(protocol != PF_KEY_V2) {
  45353. + KLIPS_PRINT(debug_pfkey,
  45354. + "klips_debug:pfkey_create: "
  45355. + "protocol not PF_KEY_V2.\n");
  45356. + return -EPROTONOSUPPORT;
  45357. + }
  45358. +
  45359. + if((current->uid != 0)) {
  45360. + KLIPS_PRINT(debug_pfkey,
  45361. + "klips_debug:pfkey_create: "
  45362. + "must be root to open pfkey sockets.\n");
  45363. + return -EACCES;
  45364. + }
  45365. +
  45366. + sock->state = SS_UNCONNECTED;
  45367. +
  45368. + KLIPS_INC_USE;
  45369. +
  45370. +#ifdef NET_26
  45371. +#ifdef NET_26_12_SKALLOC
  45372. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
  45373. +#else
  45374. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, 1, NULL);
  45375. +#endif
  45376. +#else
  45377. + /* 2.4 interface */
  45378. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, 1);
  45379. +#endif
  45380. +
  45381. + if(sk == NULL)
  45382. + {
  45383. + KLIPS_PRINT(debug_pfkey,
  45384. + "klips_debug:pfkey_create: "
  45385. + "Out of memory trying to allocate.\n");
  45386. + KLIPS_DEC_USE;
  45387. + return -ENOMEM;
  45388. + }
  45389. +
  45390. + sock_init_data(sock, sk);
  45391. +
  45392. + sk->sk_destruct = NULL;
  45393. + sk->sk_reuse = 1;
  45394. + sock->ops = &pfkey_ops;
  45395. +
  45396. + sk->sk_family = PF_KEY;
  45397. +/* sk->num = protocol; */
  45398. + sk->sk_protocol = protocol;
  45399. + key_pid(sk) = current->pid;
  45400. + KLIPS_PRINT(debug_pfkey,
  45401. + "klips_debug:pfkey_create: "
  45402. + "sock->fasync_list=0p%p sk->sleep=0p%p.\n",
  45403. + sock->fasync_list,
  45404. + sk->sk_sleep);
  45405. +
  45406. + pfkey_insert_socket(sk);
  45407. + pfkey_list_insert_socket(sock, &pfkey_open_sockets);
  45408. +
  45409. + KLIPS_PRINT(debug_pfkey,
  45410. + "klips_debug:pfkey_create: "
  45411. + "Socket sock=0p%p sk=0p%p initialised.\n", sock, sk);
  45412. + return 0;
  45413. +}
  45414. +
  45415. +DEBUG_NO_STATIC int
  45416. +#ifdef NETDEV_23
  45417. +pfkey_release(struct socket *sock)
  45418. +#else /* NETDEV_23 */
  45419. +pfkey_release(struct socket *sock, struct socket *peersock)
  45420. +#endif /* NETDEV_23 */
  45421. +{
  45422. + struct sock *sk;
  45423. + int i;
  45424. +
  45425. + if(sock==NULL) {
  45426. + KLIPS_PRINT(debug_pfkey,
  45427. + "klips_debug:pfkey_release: "
  45428. + "No socket attached.\n");
  45429. + return 0; /* -EINVAL; */
  45430. + }
  45431. +
  45432. + sk=sock->sk;
  45433. +
  45434. + /* May not have data attached */
  45435. + if(sk==NULL) {
  45436. + KLIPS_PRINT(debug_pfkey,
  45437. + "klips_debug:pfkey_release: "
  45438. + "No sk attached to sock=0p%p.\n", sock);
  45439. + return 0; /* -EINVAL; */
  45440. + }
  45441. +
  45442. + KLIPS_PRINT(debug_pfkey,
  45443. + "klips_debug:pfkey_release: "
  45444. + "sock=0p%p sk=0p%p\n", sock, sk);
  45445. +
  45446. + if(sock_flag(sk, SOCK_DEAD))
  45447. + if(sk->sk_state_change) {
  45448. + sk->sk_state_change(sk);
  45449. + }
  45450. +
  45451. + sock->sk = NULL;
  45452. +
  45453. + /* Try to flush out this socket. Throw out buffers at least */
  45454. + pfkey_destroy_socket(sk);
  45455. + pfkey_list_remove_socket(sock, &pfkey_open_sockets);
  45456. + for(i = SADB_SATYPE_UNSPEC; i <= SADB_SATYPE_MAX; i++) {
  45457. + pfkey_list_remove_socket(sock, &(pfkey_registered_sockets[i]));
  45458. + }
  45459. +
  45460. + KLIPS_DEC_USE;
  45461. + KLIPS_PRINT(debug_pfkey,
  45462. + "klips_debug:pfkey_release: "
  45463. + "succeeded.\n");
  45464. +
  45465. + return 0;
  45466. +}
  45467. +
  45468. +DEBUG_NO_STATIC int
  45469. +pfkey_shutdown(struct socket *sock, int mode)
  45470. +{
  45471. + struct sock *sk;
  45472. +
  45473. + if(sock == NULL) {
  45474. + KLIPS_PRINT(debug_pfkey,
  45475. + "klips_debug:pfkey_shutdown: "
  45476. + "NULL socket passed in.\n");
  45477. + return -EINVAL;
  45478. + }
  45479. +
  45480. + sk=sock->sk;
  45481. +
  45482. + if(sk == NULL) {
  45483. + KLIPS_PRINT(debug_pfkey,
  45484. + "klips_debug:pfkey_shutdown: "
  45485. + "No sock attached to socket.\n");
  45486. + return -EINVAL;
  45487. + }
  45488. +
  45489. + KLIPS_PRINT(debug_pfkey,
  45490. + "klips_debug:pfkey_shutdown: "
  45491. + "mode=%x.\n", mode);
  45492. + mode++;
  45493. +
  45494. + if(mode&SEND_SHUTDOWN) {
  45495. + sk->sk_shutdown|=SEND_SHUTDOWN;
  45496. + sk->sk_state_change(sk);
  45497. + }
  45498. +
  45499. + if(mode&RCV_SHUTDOWN) {
  45500. + sk->sk_shutdown|=RCV_SHUTDOWN;
  45501. + sk->sk_state_change(sk);
  45502. + }
  45503. + return 0;
  45504. +}
  45505. +
  45506. +/*
  45507. + * Send PF_KEY data down.
  45508. + */
  45509. +
  45510. +DEBUG_NO_STATIC int
  45511. +#ifdef NET_26
  45512. +pfkey_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
  45513. +#else
  45514. +pfkey_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
  45515. +#endif
  45516. +{
  45517. + struct sock *sk;
  45518. + int error = 0;
  45519. + struct sadb_msg *pfkey_msg = NULL, *pfkey_reply = NULL;
  45520. +
  45521. + if(sock == NULL) {
  45522. + KLIPS_PRINT(debug_pfkey,
  45523. + "klips_debug:pfkey_sendmsg: "
  45524. + "Null socket passed in.\n");
  45525. + SENDERR(EINVAL);
  45526. + }
  45527. +
  45528. + sk = sock->sk;
  45529. +
  45530. + if(sk == NULL) {
  45531. + KLIPS_PRINT(debug_pfkey,
  45532. + "klips_debug:pfkey_sendmsg: "
  45533. + "Null sock passed in.\n");
  45534. + SENDERR(EINVAL);
  45535. + }
  45536. +
  45537. + if(msg == NULL) {
  45538. + KLIPS_PRINT(debug_pfkey,
  45539. + "klips_debug:pfkey_sendmsg: "
  45540. + "Null msghdr passed in.\n");
  45541. + SENDERR(EINVAL);
  45542. + }
  45543. +
  45544. + KLIPS_PRINT(debug_pfkey,
  45545. + "klips_debug:pfkey_sendmsg: .\n");
  45546. + if(sk->sk_err) {
  45547. + error = sock_error(sk);
  45548. + KLIPS_PRINT(debug_pfkey,
  45549. + "klips_debug:pfkey_sendmsg: "
  45550. + "sk->err is non-zero, returns %d.\n",
  45551. + error);
  45552. + SENDERR(-error);
  45553. + }
  45554. +
  45555. + if((current->uid != 0)) {
  45556. + KLIPS_PRINT(debug_pfkey,
  45557. + "klips_debug:pfkey_sendmsg: "
  45558. + "must be root to send messages to pfkey sockets.\n");
  45559. + SENDERR(EACCES);
  45560. + }
  45561. +
  45562. + if(msg->msg_control)
  45563. + {
  45564. + KLIPS_PRINT(debug_pfkey,
  45565. + "klips_debug:pfkey_sendmsg: "
  45566. + "can't set flags or set msg_control.\n");
  45567. + SENDERR(EINVAL);
  45568. + }
  45569. +
  45570. + if(sk->sk_shutdown & SEND_SHUTDOWN) {
  45571. + KLIPS_PRINT(debug_pfkey,
  45572. + "klips_debug:pfkey_sendmsg: "
  45573. + "shutdown.\n");
  45574. + send_sig(SIGPIPE, current, 0);
  45575. + SENDERR(EPIPE);
  45576. + }
  45577. +
  45578. + if(len < sizeof(struct sadb_msg)) {
  45579. + KLIPS_PRINT(debug_pfkey,
  45580. + "klips_debug:pfkey_sendmsg: "
  45581. + "bogus msg len of %d, too small.\n", (int)len);
  45582. + SENDERR(EMSGSIZE);
  45583. + }
  45584. +
  45585. + KLIPS_PRINT(debug_pfkey,
  45586. + "klips_debug:pfkey_sendmsg: "
  45587. + "allocating %d bytes for downward message.\n",
  45588. + (int)len);
  45589. + if((pfkey_msg = (struct sadb_msg*)kmalloc(len, GFP_KERNEL)) == NULL) {
  45590. + KLIPS_PRINT(debug_pfkey,
  45591. + "klips_debug:pfkey_sendmsg: "
  45592. + "memory allocation error.\n");
  45593. + SENDERR(ENOBUFS);
  45594. + }
  45595. +
  45596. + memcpy_fromiovec((void *)pfkey_msg, msg->msg_iov, len);
  45597. +
  45598. + if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {
  45599. + KLIPS_PRINT(1 || debug_pfkey,
  45600. + "klips_debug:pfkey_sendmsg: "
  45601. + "not PF_KEY_V2 msg, found %d, should be %d.\n",
  45602. + pfkey_msg->sadb_msg_version,
  45603. + PF_KEY_V2);
  45604. + kfree((void*)pfkey_msg);
  45605. + return -EINVAL;
  45606. + }
  45607. +
  45608. + if(len != pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {
  45609. + KLIPS_PRINT(debug_pfkey,
  45610. + "klips_debug:pfkey_sendmsg: "
  45611. + "bogus msg len of %d, not %d byte aligned.\n",
  45612. + (int)len, (int)IPSEC_PFKEYv2_ALIGN);
  45613. + SENDERR(EMSGSIZE);
  45614. + }
  45615. +
  45616. +#if 0
  45617. + /* This check is questionable, since a downward message could be
  45618. + the result of an ACQUIRE either from kernel (PID==0) or
  45619. + userspace (some other PID). */
  45620. + /* check PID */
  45621. + if(pfkey_msg->sadb_msg_pid != current->pid) {
  45622. + KLIPS_PRINT(debug_pfkey,
  45623. + "klips_debug:pfkey_sendmsg: "
  45624. + "pid (%d) does not equal sending process pid (%d).\n",
  45625. + pfkey_msg->sadb_msg_pid, current->pid);
  45626. + SENDERR(EINVAL);
  45627. + }
  45628. +#endif
  45629. +
  45630. + if(pfkey_msg->sadb_msg_reserved) {
  45631. + KLIPS_PRINT(debug_pfkey,
  45632. + "klips_debug:pfkey_sendmsg: "
  45633. + "reserved field must be zero, set to %d.\n",
  45634. + pfkey_msg->sadb_msg_reserved);
  45635. + SENDERR(EINVAL);
  45636. + }
  45637. +
  45638. + if((pfkey_msg->sadb_msg_type > SADB_MAX) || (!pfkey_msg->sadb_msg_type)){
  45639. + KLIPS_PRINT(debug_pfkey,
  45640. + "klips_debug:pfkey_sendmsg: "
  45641. + "msg type too large or small:%d.\n",
  45642. + pfkey_msg->sadb_msg_type);
  45643. + SENDERR(EINVAL);
  45644. + }
  45645. +
  45646. + KLIPS_PRINT(debug_pfkey,
  45647. + "klips_debug:pfkey_sendmsg: "
  45648. + "msg sent for parsing.\n");
  45649. +
  45650. + if((error = pfkey_msg_interp(sk, pfkey_msg, &pfkey_reply))) {
  45651. + struct socket_list *pfkey_socketsp;
  45652. +
  45653. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45654. + "pfkey_msg_parse returns %d.\n",
  45655. + error);
  45656. +
  45657. + if((pfkey_reply = (struct sadb_msg*)kmalloc(sizeof(struct sadb_msg), GFP_KERNEL)) == NULL) {
  45658. + KLIPS_PRINT(debug_pfkey,
  45659. + "klips_debug:pfkey_sendmsg: "
  45660. + "memory allocation error.\n");
  45661. + SENDERR(ENOBUFS);
  45662. + }
  45663. + memcpy((void*)pfkey_reply, (void*)pfkey_msg, sizeof(struct sadb_msg));
  45664. + pfkey_reply->sadb_msg_errno = -error;
  45665. + pfkey_reply->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  45666. +
  45667. + for(pfkey_socketsp = pfkey_open_sockets;
  45668. + pfkey_socketsp;
  45669. + pfkey_socketsp = pfkey_socketsp->next) {
  45670. + int error_upmsg = 0;
  45671. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45672. + "sending up error=%d message=0p%p to socket=0p%p.\n",
  45673. + error,
  45674. + pfkey_reply,
  45675. + pfkey_socketsp->socketp);
  45676. + if((error_upmsg = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  45677. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45678. + "sending up error message to socket=0p%p failed with error=%d.\n",
  45679. + pfkey_socketsp->socketp,
  45680. + error_upmsg);
  45681. + /* pfkey_msg_free(&pfkey_reply); */
  45682. + /* SENDERR(-error); */
  45683. + }
  45684. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45685. + "sending up error message to socket=0p%p succeeded.\n",
  45686. + pfkey_socketsp->socketp);
  45687. + }
  45688. +
  45689. + pfkey_msg_free(&pfkey_reply);
  45690. +
  45691. + SENDERR(-error);
  45692. + }
  45693. +
  45694. + errlab:
  45695. + if (pfkey_msg) {
  45696. + kfree((void*)pfkey_msg);
  45697. + }
  45698. +
  45699. + if(error) {
  45700. + return error;
  45701. + } else {
  45702. + return len;
  45703. + }
  45704. +}
  45705. +
  45706. +/*
  45707. + * Receive PF_KEY data up.
  45708. + */
  45709. +
  45710. +DEBUG_NO_STATIC int
  45711. +#ifdef NET_26
  45712. +pfkey_recvmsg(struct kiocb *kiocb
  45713. + , struct socket *sock
  45714. + , struct msghdr *msg
  45715. + , size_t size
  45716. + , int flags)
  45717. +#else
  45718. +pfkey_recvmsg(struct socket *sock
  45719. + , struct msghdr *msg
  45720. + , int size, int flags
  45721. + , struct scm_cookie *scm)
  45722. +#endif
  45723. +{
  45724. + struct sock *sk;
  45725. + int noblock = flags & MSG_DONTWAIT;
  45726. + struct sk_buff *skb;
  45727. + int error;
  45728. +
  45729. + if(sock == NULL) {
  45730. + KLIPS_PRINT(debug_pfkey,
  45731. + "klips_debug:pfkey_recvmsg: "
  45732. + "Null socket passed in.\n");
  45733. + return -EINVAL;
  45734. + }
  45735. +
  45736. + sk = sock->sk;
  45737. +
  45738. + if(sk == NULL) {
  45739. + KLIPS_PRINT(debug_pfkey,
  45740. + "klips_debug:pfkey_recvmsg: "
  45741. + "Null sock passed in for sock=0p%p.\n", sock);
  45742. + return -EINVAL;
  45743. + }
  45744. +
  45745. + if(msg == NULL) {
  45746. + KLIPS_PRINT(debug_pfkey,
  45747. + "klips_debug:pfkey_recvmsg: "
  45748. + "Null msghdr passed in for sock=0p%p, sk=0p%p.\n",
  45749. + sock, sk);
  45750. + return -EINVAL;
  45751. + }
  45752. +
  45753. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  45754. + "klips_debug:pfkey_recvmsg: sock=0p%p sk=0p%p msg=0p%p size=%d.\n",
  45755. + sock, sk, msg, (int)size);
  45756. + if(flags & ~MSG_PEEK) {
  45757. + KLIPS_PRINT(debug_pfkey,
  45758. + "klips_debug:pfkey_sendmsg: "
  45759. + "flags (%d) other than MSG_PEEK not supported.\n",
  45760. + flags);
  45761. + return -EOPNOTSUPP;
  45762. + }
  45763. +
  45764. + msg->msg_namelen = 0; /* sizeof(*ska); */
  45765. +
  45766. + if(sk->sk_err) {
  45767. + KLIPS_PRINT(debug_pfkey,
  45768. + "klips_debug:pfkey_sendmsg: "
  45769. + "sk->sk_err=%d.\n", sk->sk_err);
  45770. + return sock_error(sk);
  45771. + }
  45772. +
  45773. + if((skb = skb_recv_datagram(sk, flags, noblock, &error) ) == NULL) {
  45774. + return error;
  45775. + }
  45776. +
  45777. + if(size > skb->len) {
  45778. + size = skb->len;
  45779. + }
  45780. + else if(size <skb->len) {
  45781. + msg->msg_flags |= MSG_TRUNC;
  45782. + }
  45783. +
  45784. + skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
  45785. +#ifdef HAVE_TSTAMP
  45786. + sk->sk_stamp.tv_sec = skb->tstamp.off_sec;
  45787. + sk->sk_stamp.tv_usec = skb->tstamp.off_usec;
  45788. +#else
  45789. + sk->sk_stamp=skb->stamp;
  45790. +#endif
  45791. +
  45792. + skb_free_datagram(sk, skb);
  45793. + return size;
  45794. +}
  45795. +
  45796. +#ifdef CONFIG_PROC_FS
  45797. +#ifndef PROC_FS_2325
  45798. +DEBUG_NO_STATIC
  45799. +#endif /* PROC_FS_2325 */
  45800. +int
  45801. +pfkey_get_info(char *buffer, char **start, off_t offset, int length
  45802. +#ifndef PROC_NO_DUMMY
  45803. +, int dummy
  45804. +#endif /* !PROC_NO_DUMMY */
  45805. +)
  45806. +{
  45807. + const int max_content = length > 0? length-1 : 0; /* limit of useful snprintf output */
  45808. +#ifdef NET_26
  45809. + struct hlist_node *node;
  45810. +#endif
  45811. + off_t begin=0;
  45812. + int len=0;
  45813. + struct sock *sk;
  45814. +
  45815. +#ifdef CONFIG_KLIPS_DEBUG
  45816. + if(!sysctl_ipsec_debug_verbose) {
  45817. +#endif /* CONFIG_KLIPS_DEBUG */
  45818. + len += ipsec_snprintf(buffer, length,
  45819. + " sock pid socket next prev e n p sndbf Flags Type St\n");
  45820. +#ifdef CONFIG_KLIPS_DEBUG
  45821. + } else {
  45822. + len += ipsec_snprintf(buffer, length,
  45823. + " sock pid d sleep socket next prev e r z n p sndbf stamp Flags Type St\n");
  45824. + }
  45825. +#endif /* CONFIG_KLIPS_DEBUG */
  45826. +
  45827. + sk_for_each(sk, node, &pfkey_sock_list) {
  45828. +
  45829. +#ifdef CONFIG_KLIPS_DEBUG
  45830. + if(!sysctl_ipsec_debug_verbose) {
  45831. +#endif /* CONFIG_KLIPS_DEBUG */
  45832. + len += ipsec_snprintf(buffer+len, length-len,
  45833. + "%8p %5d %8p %d %d %5d %08lX %8X %2X\n",
  45834. + sk,
  45835. + key_pid(sk),
  45836. + sk->sk_socket,
  45837. + sk->sk_err,
  45838. + sk->sk_protocol,
  45839. + sk->sk_sndbuf,
  45840. + sk->sk_socket->flags,
  45841. + sk->sk_socket->type,
  45842. + sk->sk_socket->state);
  45843. +#ifdef CONFIG_KLIPS_DEBUG
  45844. + } else {
  45845. + len += ipsec_snprintf(buffer+len, length-len,
  45846. + "%8p %5d %d %8p %8p %d %d %d %d %5d %d.%06d %08lX %8X %2X\n",
  45847. + sk,
  45848. + key_pid(sk),
  45849. + sock_flag(sk, SOCK_DEAD),
  45850. + sk->sk_sleep,
  45851. + sk->sk_socket,
  45852. + sk->sk_err,
  45853. + sk->sk_reuse,
  45854. +#ifdef HAVE_SOCK_ZAPPED
  45855. + sock_flag(sk, SOCK_ZAPPED),
  45856. +#else
  45857. + sk->sk_zapped,
  45858. +#endif
  45859. + sk->sk_protocol,
  45860. + sk->sk_sndbuf,
  45861. + (unsigned int)sk->sk_stamp.tv_sec,
  45862. + (unsigned int)sk->sk_stamp.tv_usec,
  45863. + sk->sk_socket->flags,
  45864. + sk->sk_socket->type,
  45865. + sk->sk_socket->state);
  45866. + }
  45867. +#endif /* CONFIG_KLIPS_DEBUG */
  45868. +
  45869. + if (len >= max_content) {
  45870. + /* we've done all that can fit -- stop loop */
  45871. + len = max_content; /* truncate crap */
  45872. + break;
  45873. + } else {
  45874. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45875. +
  45876. + if (pos <= offset) {
  45877. + /* all is before first interesting character:
  45878. + * discard, but note where we are.
  45879. + */
  45880. + len = 0;
  45881. + begin = pos;
  45882. + }
  45883. + }
  45884. + }
  45885. +
  45886. + *start = buffer + (offset - begin); /* Start of wanted data */
  45887. + return len - (offset - begin);
  45888. +}
  45889. +
  45890. +#ifndef PROC_FS_2325
  45891. +DEBUG_NO_STATIC
  45892. +#endif /* PROC_FS_2325 */
  45893. +int
  45894. +pfkey_supported_get_info(char *buffer, char **start, off_t offset, int length
  45895. +#ifndef PROC_NO_DUMMY
  45896. +, int dummy
  45897. +#endif /* !PROC_NO_DUMMY */
  45898. +)
  45899. +{
  45900. + /* limit of useful snprintf output */
  45901. + const int max_content = length > 0? length-1 : 0;
  45902. + off_t begin=0;
  45903. + int len=0;
  45904. + int satype;
  45905. + struct supported_list *ps;
  45906. +
  45907. + len += ipsec_snprintf(buffer, length,
  45908. + "satype exttype alg_id ivlen minbits maxbits name\n");
  45909. +
  45910. + for(satype = SADB_SATYPE_UNSPEC; satype <= SADB_SATYPE_MAX; satype++) {
  45911. + ps = pfkey_supported_list[satype];
  45912. + while(ps) {
  45913. + struct ipsec_alg_supported *alg = ps->supportedp;
  45914. + unsigned char *n = alg->ias_name;
  45915. + if(n == NULL) n = "unknown";
  45916. +
  45917. + len += ipsec_snprintf(buffer+len, length-len,
  45918. + " %2d %2d %2d %3d %3d %3d %20s\n",
  45919. + satype,
  45920. + alg->ias_exttype,
  45921. + alg->ias_id,
  45922. + alg->ias_ivlen,
  45923. + alg->ias_keyminbits,
  45924. + alg->ias_keymaxbits,
  45925. + n);
  45926. +
  45927. + if (len >= max_content) {
  45928. + /* we've done all that can fit -- stop loop */
  45929. + len = max_content; /* truncate crap */
  45930. + break;
  45931. + } else {
  45932. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45933. +
  45934. + if (pos <= offset) {
  45935. + /* all is before first interesting character:
  45936. + * discard, but note where we are.
  45937. + */
  45938. + len = 0;
  45939. + begin = pos;
  45940. + }
  45941. + }
  45942. +
  45943. + ps = ps->next;
  45944. + }
  45945. + }
  45946. + *start = buffer + (offset - begin); /* Start of wanted data */
  45947. + return len - (offset - begin);
  45948. +}
  45949. +
  45950. +#ifndef PROC_FS_2325
  45951. +DEBUG_NO_STATIC
  45952. +#endif /* PROC_FS_2325 */
  45953. +int
  45954. +pfkey_registered_get_info(char *buffer, char **start, off_t offset, int length
  45955. +#ifndef PROC_NO_DUMMY
  45956. +, int dummy
  45957. +#endif /* !PROC_NO_DUMMY */
  45958. +)
  45959. +{
  45960. + const int max_content = length > 0? length-1 : 0; /* limit of useful snprintf output */
  45961. + off_t begin=0;
  45962. + int len=0;
  45963. + int satype;
  45964. + struct socket_list *pfkey_sockets;
  45965. +
  45966. + len += ipsec_snprintf(buffer, length,
  45967. + "satype socket pid sk\n");
  45968. +
  45969. + for(satype = SADB_SATYPE_UNSPEC; satype <= SADB_SATYPE_MAX; satype++) {
  45970. + pfkey_sockets = pfkey_registered_sockets[satype];
  45971. + while(pfkey_sockets) {
  45972. + len += ipsec_snprintf(buffer+len, length-len,
  45973. + " %2d %8p %5d %8p\n",
  45974. + satype,
  45975. + pfkey_sockets->socketp,
  45976. + key_pid(pfkey_sockets->socketp->sk),
  45977. + pfkey_sockets->socketp->sk);
  45978. +
  45979. + if (len >= max_content) {
  45980. + /* we've done all that can fit -- stop loop (could stop two) */
  45981. + len = max_content; /* truncate crap */
  45982. + break;
  45983. + } else {
  45984. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45985. +
  45986. + if (pos <= offset) {
  45987. + /* all is before first interesting character:
  45988. + * discard, but note where we are.
  45989. + */
  45990. + len = 0;
  45991. + begin = pos;
  45992. + }
  45993. + }
  45994. +
  45995. + pfkey_sockets = pfkey_sockets->next;
  45996. + }
  45997. + }
  45998. + *start = buffer + (offset - begin); /* Start of wanted data */
  45999. + return len - (offset - begin);
  46000. +}
  46001. +
  46002. +#ifndef PROC_FS_2325
  46003. +struct proc_dir_entry proc_net_pfkey =
  46004. +{
  46005. + 0,
  46006. + 6, "pf_key",
  46007. + S_IFREG | S_IRUGO, 1, 0, 0,
  46008. + 0, &proc_net_inode_operations,
  46009. + pfkey_get_info
  46010. +};
  46011. +struct proc_dir_entry proc_net_pfkey_supported =
  46012. +{
  46013. + 0,
  46014. + 16, "pf_key_supported",
  46015. + S_IFREG | S_IRUGO, 1, 0, 0,
  46016. + 0, &proc_net_inode_operations,
  46017. + pfkey_supported_get_info
  46018. +};
  46019. +struct proc_dir_entry proc_net_pfkey_registered =
  46020. +{
  46021. + 0,
  46022. + 17, "pf_key_registered",
  46023. + S_IFREG | S_IRUGO, 1, 0, 0,
  46024. + 0, &proc_net_inode_operations,
  46025. + pfkey_registered_get_info
  46026. +};
  46027. +#endif /* !PROC_FS_2325 */
  46028. +#endif /* CONFIG_PROC_FS */
  46029. +
  46030. +DEBUG_NO_STATIC int
  46031. +supported_add_all(int satype, struct ipsec_alg_supported supported[], int size)
  46032. +{
  46033. + int i;
  46034. + int error = 0;
  46035. +
  46036. + KLIPS_PRINT(debug_pfkey,
  46037. + "klips_debug:init_pfkey: "
  46038. + "sizeof(supported_init_<satype=%d>)[%d]/sizeof(struct ipsec_alg_supported)[%d]=%d.\n",
  46039. + satype,
  46040. + size,
  46041. + (int)sizeof(struct ipsec_alg_supported),
  46042. + (int)(size/sizeof(struct ipsec_alg_supported)));
  46043. +
  46044. + for(i = 0; i < size / sizeof(struct ipsec_alg_supported); i++) {
  46045. +
  46046. + unsigned char *n = supported[i].ias_name;
  46047. + if(n == NULL) n="unknown";
  46048. +
  46049. + KLIPS_PRINT(debug_pfkey,
  46050. + "klips_debug:init_pfkey: "
  46051. + "i=%d inserting satype=%d exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d name=%s.\n",
  46052. + i,
  46053. + satype,
  46054. + supported[i].ias_exttype,
  46055. + supported[i].ias_id,
  46056. + supported[i].ias_ivlen,
  46057. + supported[i].ias_keyminbits,
  46058. + supported[i].ias_keymaxbits,
  46059. + n);
  46060. +
  46061. + error |= pfkey_list_insert_supported(&(supported[i]),
  46062. + &(pfkey_supported_list[satype]));
  46063. + }
  46064. + return error;
  46065. +}
  46066. +
  46067. +DEBUG_NO_STATIC int
  46068. +supported_remove_all(int satype)
  46069. +{
  46070. + int error = 0;
  46071. + struct ipsec_alg_supported*supportedp;
  46072. +
  46073. + while(pfkey_supported_list[satype]) {
  46074. + unsigned char *n;
  46075. + supportedp = pfkey_supported_list[satype]->supportedp;
  46076. +
  46077. + n = supportedp->ias_name;
  46078. + if(n == NULL) n="unknown";
  46079. +
  46080. + KLIPS_PRINT(debug_pfkey,
  46081. + "klips_debug:init_pfkey: "
  46082. + "removing satype=%d exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d name=%s.\n",
  46083. + satype,
  46084. + supportedp->ias_exttype,
  46085. + supportedp->ias_id,
  46086. + supportedp->ias_ivlen,
  46087. + supportedp->ias_keyminbits,
  46088. + supportedp->ias_keymaxbits, n);
  46089. +
  46090. + error |= pfkey_list_remove_supported(supportedp,
  46091. + &(pfkey_supported_list[satype]));
  46092. + }
  46093. + return error;
  46094. +}
  46095. +
  46096. +int
  46097. +pfkey_init(void)
  46098. +{
  46099. + int error = 0;
  46100. + int i;
  46101. +
  46102. + static struct ipsec_alg_supported supported_init_ah[] = {
  46103. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  46104. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_MD5HMAC, 0, 128, 128},
  46105. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  46106. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  46107. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_SHA1HMAC, 0, 160, 160}
  46108. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  46109. + };
  46110. + static struct ipsec_alg_supported supported_init_esp[] = {
  46111. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  46112. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_MD5HMAC, 0, 128, 128},
  46113. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  46114. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  46115. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_SHA1HMAC, 0, 160, 160},
  46116. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  46117. +#ifdef CONFIG_KLIPS_ENC_3DES
  46118. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_EALG_3DESCBC, 64, 168, 168},
  46119. +#endif /* CONFIG_KLIPS_ENC_3DES */
  46120. + };
  46121. + static struct ipsec_alg_supported supported_init_ipip[] = {
  46122. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv4_in_IPv4, 0, 32, 32}
  46123. +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46124. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv6_in_IPv4, 0, 128, 32}
  46125. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv4_in_IPv6, 0, 32, 128}
  46126. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv6_in_IPv6, 0, 128, 128}
  46127. +#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  46128. + };
  46129. +#ifdef CONFIG_KLIPS_IPCOMP
  46130. + static struct ipsec_alg_supported supported_init_ipcomp[] = {
  46131. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_CALG_DEFLATE, 0, 1, 1}
  46132. + };
  46133. +#endif /* CONFIG_KLIPS_IPCOMP */
  46134. +
  46135. +#if 0
  46136. + printk(KERN_INFO
  46137. + "klips_info:pfkey_init: "
  46138. + "FreeS/WAN: initialising PF_KEYv2 domain sockets.\n");
  46139. +#endif
  46140. +
  46141. + for(i = SADB_SATYPE_UNSPEC; i <= SADB_SATYPE_MAX; i++) {
  46142. + pfkey_registered_sockets[i] = NULL;
  46143. + pfkey_supported_list[i] = NULL;
  46144. + }
  46145. +
  46146. + error |= supported_add_all(SADB_SATYPE_AH, supported_init_ah, sizeof(supported_init_ah));
  46147. + error |= supported_add_all(SADB_SATYPE_ESP, supported_init_esp, sizeof(supported_init_esp));
  46148. +#ifdef CONFIG_KLIPS_IPCOMP
  46149. + error |= supported_add_all(SADB_X_SATYPE_COMP, supported_init_ipcomp, sizeof(supported_init_ipcomp));
  46150. +#endif /* CONFIG_KLIPS_IPCOMP */
  46151. + error |= supported_add_all(SADB_X_SATYPE_IPIP, supported_init_ipip, sizeof(supported_init_ipip));
  46152. +
  46153. + error |= sock_register(&pfkey_family_ops);
  46154. +
  46155. +#ifdef CONFIG_PROC_FS
  46156. +# ifndef PROC_FS_2325
  46157. +# ifdef PROC_FS_21
  46158. + error |= proc_register(proc_net, &proc_net_pfkey);
  46159. + error |= proc_register(proc_net, &proc_net_pfkey_supported);
  46160. + error |= proc_register(proc_net, &proc_net_pfkey_registered);
  46161. +# else /* PROC_FS_21 */
  46162. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey);
  46163. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey_supported);
  46164. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey_registered);
  46165. +# endif /* PROC_FS_21 */
  46166. +# else /* !PROC_FS_2325 */
  46167. + proc_net_create ("pf_key", 0, pfkey_get_info);
  46168. + proc_net_create ("pf_key_supported", 0, pfkey_supported_get_info);
  46169. + proc_net_create ("pf_key_registered", 0, pfkey_registered_get_info);
  46170. +# endif /* !PROC_FS_2325 */
  46171. +#endif /* CONFIG_PROC_FS */
  46172. +
  46173. + return error;
  46174. +}
  46175. +
  46176. +int
  46177. +pfkey_cleanup(void)
  46178. +{
  46179. + int error = 0;
  46180. +
  46181. + printk(KERN_INFO "klips_info:pfkey_cleanup: "
  46182. + "shutting down PF_KEY domain sockets.\n");
  46183. + sock_unregister(PF_KEY);
  46184. +
  46185. + error |= supported_remove_all(SADB_SATYPE_AH);
  46186. + error |= supported_remove_all(SADB_SATYPE_ESP);
  46187. +#ifdef CONFIG_KLIPS_IPCOMP
  46188. + error |= supported_remove_all(SADB_X_SATYPE_COMP);
  46189. +#endif /* CONFIG_KLIPS_IPCOMP */
  46190. + error |= supported_remove_all(SADB_X_SATYPE_IPIP);
  46191. +
  46192. +#ifdef CONFIG_PROC_FS
  46193. +# ifndef PROC_FS_2325
  46194. + if (proc_net_unregister(proc_net_pfkey.low_ino) != 0)
  46195. + printk("klips_debug:pfkey_cleanup: "
  46196. + "cannot unregister /proc/net/pf_key\n");
  46197. + if (proc_net_unregister(proc_net_pfkey_supported.low_ino) != 0)
  46198. + printk("klips_debug:pfkey_cleanup: "
  46199. + "cannot unregister /proc/net/pf_key_supported\n");
  46200. + if (proc_net_unregister(proc_net_pfkey_registered.low_ino) != 0)
  46201. + printk("klips_debug:pfkey_cleanup: "
  46202. + "cannot unregister /proc/net/pf_key_registered\n");
  46203. +# else /* !PROC_FS_2325 */
  46204. + proc_net_remove ("pf_key");
  46205. + proc_net_remove ("pf_key_supported");
  46206. + proc_net_remove ("pf_key_registered");
  46207. +# endif /* !PROC_FS_2325 */
  46208. +#endif /* CONFIG_PROC_FS */
  46209. +
  46210. + /* other module unloading cleanup happens here */
  46211. + return error;
  46212. +}
  46213. +
  46214. +#ifdef MODULE
  46215. +#if 0
  46216. +int
  46217. +init_module(void)
  46218. +{
  46219. + pfkey_init();
  46220. + return 0;
  46221. +}
  46222. +
  46223. +void
  46224. +cleanup_module(void)
  46225. +{
  46226. + pfkey_cleanup();
  46227. +}
  46228. +#endif /* 0 */
  46229. +#else /* MODULE */
  46230. +struct net_protocol;
  46231. +void pfkey_proto_init(struct net_protocol *pro)
  46232. +{
  46233. + pfkey_init();
  46234. +}
  46235. +#endif /* MODULE */
  46236. +
  46237. +/*
  46238. + * $Log: pfkey_v2.c,v $
  46239. + * Revision 1.97.2.12 2006/11/24 05:43:29 paul
  46240. + * kernels after 2.6.18 do not return a code from unregister_socket()
  46241. + * backport from git 41e54a2684dc809d7952e816860ea646a3194a72
  46242. + *
  46243. + * Revision 1.97.2.11 2006/11/15 16:05:57 paul
  46244. + * fix for compiling on 2.4. kernels by Matthias Haas.
  46245. + *
  46246. + * Revision 1.97.2.10 2006/10/10 20:43:28 paul
  46247. + * Add family/create/owner for pfkey_family_ops. This fixes bug #671
  46248. + *
  46249. + * Revision 1.97.2.9 2006/10/06 21:39:26 paul
  46250. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  46251. + * set. This is defined through autoconf.h which is included through the
  46252. + * linux kernel build macros.
  46253. + *
  46254. + * Revision 1.97.2.8 2006/07/10 15:56:11 paul
  46255. + * Fix for bug #642 by Bart.
  46256. + *
  46257. + * Revision 1.97.2.7 2006/04/04 11:34:19 ken
  46258. + * Backport SMP fixes + #ifdef cleanup from #public
  46259. + *
  46260. + * Revision 1.97.2.6 2006/02/15 05:00:20 paul
  46261. + * Fix for crasher on 2.6.12+ with klips (mostly seen on redhat kernels)
  46262. + *
  46263. + * Revision 1.97.2.5 2005/11/22 04:11:52 ken
  46264. + * Backport fixes for 2.6.14 kernels from HEAD
  46265. + *
  46266. + * Revision 1.97.2.4 2005/09/14 16:40:45 mcr
  46267. + * pull up of compilation on 2.4
  46268. + *
  46269. + * Revision 1.97.2.3 2005/09/06 02:10:03 mcr
  46270. + * pulled up possible SMP-related compilation fix
  46271. + *
  46272. + * Revision 1.97.2.2 2005/08/28 01:21:12 paul
  46273. + * Undid Ken's gcc4 fix in version 1.94 since it breaks linking KLIPS on
  46274. + * SMP kernels.
  46275. + *
  46276. + * Revision 1.97.2.1 2005/08/27 23:40:00 paul
  46277. + * recommited HAVE_SOCK_SECURITY fixes for linux 2.6.13
  46278. + *
  46279. + * Revision 1.102 2005/09/14 16:37:23 mcr
  46280. + * fix to compile on 2.4.
  46281. + *
  46282. + * Revision 1.101 2005/09/06 01:42:25 mcr
  46283. + * removed additional SOCKOPS_WRAPPED code
  46284. + *
  46285. + * Revision 1.100 2005/08/30 18:10:15 mcr
  46286. + * remove SOCKOPS_WRAPPED() code, add proper locking to the
  46287. + * pfkey code. (cross fingers)
  46288. + *
  46289. + * Revision 1.99 2005/08/28 01:53:37 paul
  46290. + * Undid Ken's gcc4 fix in version 1.94 since it breaks linking KLIPS on SMP kernels.
  46291. + *
  46292. + * Revision 1.98 2005/08/27 23:07:21 paul
  46293. + * Somewhere between 2.6.12 and 2.6.13rc7 the unused security memnber in sk_buff
  46294. + * has been removed. This patch should fix compilation for both cases.
  46295. + *
  46296. + * Revision 1.97 2005/07/20 00:33:36 mcr
  46297. + * fixed typo in #ifdef for SKALLOC.
  46298. + *
  46299. + * Revision 1.96 2005/07/19 20:02:15 mcr
  46300. + * sk_alloc() interface change.
  46301. + *
  46302. + * Revision 1.95 2005/07/09 00:40:06 ken
  46303. + * Fix for GCC4 - it doesn't like the potential for duplicate declaration
  46304. + *
  46305. + * Revision 1.94 2005/07/09 00:14:04 ken
  46306. + * Casts for 64bit cleanliness
  46307. + *
  46308. + * Revision 1.93 2005/07/08 16:20:05 mcr
  46309. + * fix for 2.6.12 disapperance of sk_zapped field -> sock_flags.
  46310. + *
  46311. + * Revision 1.92 2005/05/21 03:29:39 mcr
  46312. + * fixed missing prototype definition.
  46313. + *
  46314. + * Revision 1.91 2005/05/11 01:43:45 mcr
  46315. + * removed "poor-man"s OOP in favour of proper C structures.
  46316. + *
  46317. + * Revision 1.90 2005/05/02 18:42:47 mcr
  46318. + * fix for cut&paste error with pfkey_v2.c "supported_name"
  46319. + *
  46320. + * Revision 1.89 2005/05/01 03:12:31 mcr
  46321. + * print name if it is available.
  46322. + *
  46323. + * Revision 1.88 2005/04/29 05:10:22 mcr
  46324. + * removed from extraenous includes to make unit testing easier.
  46325. + *
  46326. + * Revision 1.87 2005/04/15 19:57:10 mcr
  46327. + * make sure that address has 0p so that it will
  46328. + * sanitized.
  46329. + *
  46330. + * Revision 1.86 2005/04/08 18:28:36 mcr
  46331. + * some minor #ifdef simplification in pursuit of a possible bug.
  46332. + *
  46333. + * Revision 1.85 2004/12/03 21:25:57 mcr
  46334. + * compile time fixes for running on 2.6.
  46335. + * still experimental.
  46336. + *
  46337. + * Revision 1.84 2004/08/17 03:27:23 mcr
  46338. + * klips 2.6 edits.
  46339. + *
  46340. + * Revision 1.83 2004/08/04 15:57:07 mcr
  46341. + * moved des .h files to include/des/ *
  46342. + * included 2.6 protocol specific things
  46343. + * started at NAT-T support, but it will require a kernel patch.
  46344. + *
  46345. + * Revision 1.82 2004/07/10 19:11:18 mcr
  46346. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  46347. + *
  46348. + * Revision 1.81 2004/04/25 21:23:11 ken
  46349. + * Pull in dhr's changes from FreeS/WAN 2.06
  46350. + *
  46351. + * Revision 1.80 2004/04/06 02:49:26 mcr
  46352. + * pullup of algo code from alg-branch.
  46353. + *
  46354. + * Revision 1.79.4.1 2003/12/22 15:25:52 jjo
  46355. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  46356. + *
  46357. + * Revision 1.79 2003/10/31 02:27:55 mcr
  46358. + * pulled up port-selector patches and sa_id elimination.
  46359. + *
  46360. + * Revision 1.78.4.1 2003/10/29 01:30:41 mcr
  46361. + * elimited "struct sa_id".
  46362. + *
  46363. + * Revision 1.78 2003/04/03 17:38:09 rgb
  46364. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  46365. + *
  46366. + * Revision 1.77 2002/10/17 16:49:36 mcr
  46367. + * sock->ops should reference the unwrapped options so that
  46368. + * we get hacked in locking on SMP systems.
  46369. + *
  46370. + * Revision 1.76 2002/10/12 23:11:53 dhr
  46371. + *
  46372. + * [KenB + DHR] more 64-bit cleanup
  46373. + *
  46374. + * Revision 1.75 2002/09/20 05:01:57 rgb
  46375. + * Added memory allocation debugging.
  46376. + *
  46377. + * Revision 1.74 2002/09/19 02:42:50 mcr
  46378. + * do not define the pfkey_ops function for now.
  46379. + *
  46380. + * Revision 1.73 2002/09/17 17:29:23 mcr
  46381. + * #if 0 out some dead code - pfkey_ops is never used as written.
  46382. + *
  46383. + * Revision 1.72 2002/07/24 18:44:54 rgb
  46384. + * Type fiddling to tame ia64 compiler.
  46385. + *
  46386. + * Revision 1.71 2002/05/23 07:14:11 rgb
  46387. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  46388. + *
  46389. + * Revision 1.70 2002/04/24 07:55:32 mcr
  46390. + * #include patches and Makefiles for post-reorg compilation.
  46391. + *
  46392. + * Revision 1.69 2002/04/24 07:36:33 mcr
  46393. + * Moved from ./klips/net/ipsec/pfkey_v2.c,v
  46394. + *
  46395. + * Revision 1.68 2002/03/08 01:15:17 mcr
  46396. + * put some internal structure only debug messages behind
  46397. + * && sysctl_ipsec_debug_verbose.
  46398. + *
  46399. + * Revision 1.67 2002/01/29 17:17:57 mcr
  46400. + * moved include of ipsec_param.h to after include of linux/kernel.h
  46401. + * otherwise, it seems that some option that is set in ipsec_param.h
  46402. + * screws up something subtle in the include path to kernel.h, and
  46403. + * it complains on the snprintf() prototype.
  46404. + *
  46405. + * Revision 1.66 2002/01/29 04:00:54 mcr
  46406. + * more excise of kversions.h header.
  46407. + *
  46408. + * Revision 1.65 2002/01/29 02:13:18 mcr
  46409. + * introduction of ipsec_kversion.h means that include of
  46410. + * ipsec_param.h must preceed any decisions about what files to
  46411. + * include to deal with differences in kernel source.
  46412. + *
  46413. + * Revision 1.64 2001/11/26 09:23:51 rgb
  46414. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  46415. + *
  46416. + * Revision 1.61.2.1 2001/09/25 02:28:44 mcr
  46417. + * cleaned up includes.
  46418. + *
  46419. + * Revision 1.63 2001/11/12 19:38:00 rgb
  46420. + * Continue trying other sockets even if one fails and return only original
  46421. + * error.
  46422. + *
  46423. + * Revision 1.62 2001/10/18 04:45:22 rgb
  46424. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  46425. + * lib/freeswan.h version macros moved to lib/kversions.h.
  46426. + * Other compiler directive cleanups.
  46427. + *
  46428. + * Revision 1.61 2001/09/20 15:32:59 rgb
  46429. + * Min/max cleanup.
  46430. + *
  46431. + * Revision 1.60 2001/06/14 19:35:12 rgb
  46432. + * Update copyright date.
  46433. + *
  46434. + * Revision 1.59 2001/06/13 15:35:48 rgb
  46435. + * Fixed #endif comments.
  46436. + *
  46437. + * Revision 1.58 2001/05/04 16:37:24 rgb
  46438. + * Remove erroneous checking of return codes for proc_net_* in 2.4.
  46439. + *
  46440. + * Revision 1.57 2001/05/03 19:43:36 rgb
  46441. + * Initialise error return variable.
  46442. + * Check error return codes in startup and shutdown.
  46443. + * Standardise on SENDERR() macro.
  46444. + *
  46445. + * Revision 1.56 2001/04/21 23:05:07 rgb
  46446. + * Define out skb->used for 2.4 kernels.
  46447. + *
  46448. + * Revision 1.55 2001/02/28 05:03:28 rgb
  46449. + * Clean up and rationalise startup messages.
  46450. + *
  46451. + * Revision 1.54 2001/02/27 22:24:55 rgb
  46452. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  46453. + * Check for satoa() return codes.
  46454. + *
  46455. + * Revision 1.53 2001/02/27 06:48:18 rgb
  46456. + * Fixed pfkey socket unregister log message to reflect type and function.
  46457. + *
  46458. + * Revision 1.52 2001/02/26 22:34:38 rgb
  46459. + * Fix error return code that was getting overwritten by the error return
  46460. + * code of an upmsg.
  46461. + *
  46462. + * Revision 1.51 2001/01/30 23:42:47 rgb
  46463. + * Allow pfkey msgs from pid other than user context required for ACQUIRE
  46464. + * and subsequent ADD or UDATE.
  46465. + *
  46466. + * Revision 1.50 2001/01/23 20:22:59 rgb
  46467. + * 2.4 fix to remove removed is_clone member.
  46468. + *
  46469. + * Revision 1.49 2000/11/06 04:33:47 rgb
  46470. + * Changed non-exported functions to DEBUG_NO_STATIC.
  46471. + *
  46472. + * Revision 1.48 2000/09/29 19:47:41 rgb
  46473. + * Update copyright.
  46474. + *
  46475. + * Revision 1.47 2000/09/22 04:23:04 rgb
  46476. + * Added more debugging to pfkey_upmsg() call from pfkey_sendmsg() error.
  46477. + *
  46478. + * Revision 1.46 2000/09/21 04:20:44 rgb
  46479. + * Fixed array size off-by-one error. (Thanks Svenning!)
  46480. + *
  46481. + * Revision 1.45 2000/09/20 04:01:26 rgb
  46482. + * Changed static functions to DEBUG_NO_STATIC for revealing function names
  46483. + * in oopsen.
  46484. + *
  46485. + * Revision 1.44 2000/09/19 00:33:17 rgb
  46486. + * 2.0 fixes.
  46487. + *
  46488. + * Revision 1.43 2000/09/16 01:28:13 rgb
  46489. + * Fixed use of 0 in p format warning.
  46490. + *
  46491. + * Revision 1.42 2000/09/16 01:09:41 rgb
  46492. + * Fixed debug format warning for pointers that was expecting ints.
  46493. + *
  46494. + * Revision 1.41 2000/09/13 15:54:00 rgb
  46495. + * Rewrote pfkey_get_info(), added pfkey_{supported,registered}_get_info().
  46496. + * Moved supported algos add and remove to functions.
  46497. + *
  46498. + * Revision 1.40 2000/09/12 18:49:28 rgb
  46499. + * Added IPIP tunnel and IPCOMP register support.
  46500. + *
  46501. + * Revision 1.39 2000/09/12 03:23:49 rgb
  46502. + * Converted #if0 debugs to sysctl.
  46503. + * Removed debug_pfkey initialisations that prevented no_debug loading or
  46504. + * linking.
  46505. + *
  46506. + * Revision 1.38 2000/09/09 06:38:02 rgb
  46507. + * Return positive errno in pfkey_reply error message.
  46508. + *
  46509. + * Revision 1.37 2000/09/08 19:19:09 rgb
  46510. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  46511. + * Clean-up of long-unused crud...
  46512. + * Create pfkey error message on on failure.
  46513. + * Give pfkey_list_{insert,remove}_{socket,supported}() some error
  46514. + * checking.
  46515. + *
  46516. + * Revision 1.36 2000/09/01 18:49:38 rgb
  46517. + * Reap experimental NET_21_ bits.
  46518. + * Turned registered sockets list into an array of one list per satype.
  46519. + * Remove references to deprecated sklist_{insert,remove}_socket.
  46520. + * Removed leaking socket debugging code.
  46521. + * Removed duplicate pfkey_insert_socket in pfkey_create.
  46522. + * Removed all references to pfkey msg->msg_name, since it is not used for
  46523. + * pfkey.
  46524. + * Added a supported algorithms array lists, one per satype and registered
  46525. + * existing algorithms.
  46526. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  46527. + * list.
  46528. + * Only send pfkey_expire() messages to sockets registered for that satype.
  46529. + *
  46530. + * Revision 1.35 2000/08/24 17:03:00 rgb
  46531. + * Corrected message size error return code for PF_KEYv2.
  46532. + * Removed downward error prohibition.
  46533. + *
  46534. + * Revision 1.34 2000/08/21 16:32:26 rgb
  46535. + * Re-formatted for cosmetic consistency and readability.
  46536. + *
  46537. + * Revision 1.33 2000/08/20 21:38:24 rgb
  46538. + * Added a pfkey_reply parameter to pfkey_msg_interp(). (Momchil)
  46539. + * Extended the upward message initiation of pfkey_sendmsg(). (Momchil)
  46540. + *
  46541. + * Revision 1.32 2000/07/28 14:58:31 rgb
  46542. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  46543. + *
  46544. + * Revision 1.31 2000/05/16 03:04:00 rgb
  46545. + * Updates for 2.3.99pre8 from MB.
  46546. + *
  46547. + * Revision 1.30 2000/05/10 19:22:21 rgb
  46548. + * Use sklist private functions for 2.3.xx compatibility.
  46549. + *
  46550. + * Revision 1.29 2000/03/22 16:17:03 rgb
  46551. + * Fixed SOCKOPS_WRAPPED macro for SMP (MB).
  46552. + *
  46553. + * Revision 1.28 2000/02/21 19:30:45 rgb
  46554. + * Removed references to pkt_bridged for 2.3.47 compatibility.
  46555. + *
  46556. + * Revision 1.27 2000/02/14 21:07:00 rgb
  46557. + * Fixed /proc/net/pf-key legend spacing.
  46558. + *
  46559. + * Revision 1.26 2000/01/22 03:46:59 rgb
  46560. + * Fixed pfkey error return mechanism so that we are able to free the
  46561. + * local copy of the pfkey_msg, plugging a memory leak and silencing
  46562. + * the bad object free complaints.
  46563. + *
  46564. + * Revision 1.25 2000/01/21 06:19:44 rgb
  46565. + * Moved pfkey_list_remove_socket() calls to before MOD_USE_DEC_COUNT.
  46566. + * Added debugging to pfkey_upmsg.
  46567. + *
  46568. + * Revision 1.24 2000/01/10 16:38:23 rgb
  46569. + * MB fixups for 2.3.x.
  46570. + *
  46571. + * Revision 1.23 1999/12/09 23:22:16 rgb
  46572. + * Added more instrumentation for debugging 2.0 socket
  46573. + * selection/reading.
  46574. + * Removed erroneous 2.0 wait==NULL check bug in select.
  46575. + *
  46576. + * Revision 1.22 1999/12/08 20:32:16 rgb
  46577. + * Tidied up 2.0.xx support, after major pfkey work, eliminating
  46578. + * msg->msg_name twiddling in the process, since it is not defined
  46579. + * for PF_KEYv2.
  46580. + *
  46581. + * Revision 1.21 1999/12/01 22:17:19 rgb
  46582. + * Set skb->dev to zero on new skb in case it is a reused skb.
  46583. + * Added check for skb_put overflow and freeing to avoid upmsg on error.
  46584. + * Added check for wrong pfkey version and freeing to avoid upmsg on
  46585. + * error.
  46586. + * Shut off content dumping in pfkey_destroy.
  46587. + * Added debugging message for size of buffer allocated for upmsg.
  46588. + *
  46589. + * Revision 1.20 1999/11/27 12:11:00 rgb
  46590. + * Minor clean-up, enabling quiet operation of pfkey if desired.
  46591. + *
  46592. + * Revision 1.19 1999/11/25 19:04:21 rgb
  46593. + * Update proc_fs code for pfkey to use dynamic registration.
  46594. + *
  46595. + * Revision 1.18 1999/11/25 09:07:17 rgb
  46596. + * Implemented SENDERR macro for propagating error codes.
  46597. + * Fixed error return code bug.
  46598. + *
  46599. + * Revision 1.17 1999/11/23 23:07:20 rgb
  46600. + * Change name of pfkey_msg_parser to pfkey_msg_interp since it no longer
  46601. + * parses. (PJO)
  46602. + * Sort out pfkey and freeswan headers, putting them in a library path.
  46603. + *
  46604. + * Revision 1.16 1999/11/20 22:00:22 rgb
  46605. + * Moved socketlist type declarations and prototypes for shared use.
  46606. + * Renamed reformatted and generically extended for use by other socket
  46607. + * lists pfkey_{del,add}_open_socket to pfkey_list_{remove,insert}_socket.
  46608. + *
  46609. + * Revision 1.15 1999/11/18 04:15:09 rgb
  46610. + * Make pfkey_data_ready temporarily available for 2.2.x testing.
  46611. + * Clean up pfkey_destroy_socket() debugging statements.
  46612. + * Add Peter Onion's code to send messages up to all listening sockets.
  46613. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  46614. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  46615. + * klips/net/ipsec/Makefile.
  46616. + * Replaced all kernel version macros to shorter, readable form.
  46617. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  46618. + *
  46619. + * Revision 1.14 1999/11/17 16:01:00 rgb
  46620. + * Make pfkey_data_ready temporarily available for 2.2.x testing.
  46621. + * Clean up pfkey_destroy_socket() debugging statements.
  46622. + * Add Peter Onion's code to send messages up to all listening sockets.
  46623. + * Changed #include "../../../lib/freeswan.h" to #include <freeswan.h>
  46624. + * which works due to -Ilibfreeswan in the klips/net/ipsec/Makefile.
  46625. + *
  46626. + * Revision 1.13 1999/10/27 19:59:51 rgb
  46627. + * Removed af_unix comments that are no longer relevant.
  46628. + * Added debug prink statements.
  46629. + * Added to the /proc output in pfkey_get_info.
  46630. + * Made most functions non-static to enable oops tracing.
  46631. + * Re-enable skb dequeueing and freeing.
  46632. + * Fix skb_alloc() and skb_put() size bug in pfkey_upmsg().
  46633. + *
  46634. + * Revision 1.12 1999/10/26 17:05:42 rgb
  46635. + * Complete re-ordering based on proto_ops structure order.
  46636. + * Separated out proto_ops structures for 2.0.x and 2.2.x for clarity.
  46637. + * Simplification to use built-in socket ops where possible for 2.2.x.
  46638. + * Add shorter macros for compiler directives to visually clean-up.
  46639. + * Add lots of sk skb dequeueing debugging statements.
  46640. + * Added to the /proc output in pfkey_get_info.
  46641. + *
  46642. + * Revision 1.11 1999/09/30 02:55:10 rgb
  46643. + * Bogus skb detection.
  46644. + * Fix incorrect /proc/net/ipsec-eroute printk message.
  46645. + *
  46646. + * Revision 1.10 1999/09/21 15:22:13 rgb
  46647. + * Temporary fix while I figure out the right way to destroy sockets.
  46648. + *
  46649. + * Revision 1.9 1999/07/08 19:19:44 rgb
  46650. + * Fix pointer format warning.
  46651. + * Fix missing member error under 2.0.xx kernels.
  46652. + *
  46653. + * Revision 1.8 1999/06/13 07:24:04 rgb
  46654. + * Add more debugging.
  46655. + *
  46656. + * Revision 1.7 1999/06/10 05:24:17 rgb
  46657. + * Clarified compiler directives.
  46658. + * Renamed variables to reduce confusion.
  46659. + * Used sklist_*_socket() kernel functions to simplify 2.2.x socket support.
  46660. + * Added lots of sanity checking.
  46661. + *
  46662. + * Revision 1.6 1999/06/03 18:59:50 rgb
  46663. + * More updates to 2.2.x socket support. Almost works, oops at end of call.
  46664. + *
  46665. + * Revision 1.5 1999/05/25 22:44:05 rgb
  46666. + * Start fixing 2.2 sockets.
  46667. + *
  46668. + * Revision 1.4 1999/04/29 15:21:34 rgb
  46669. + * Move log to the end of the file.
  46670. + * Eliminate min/max redefinition in #include <net/tcp.h>.
  46671. + * Correct path for pfkey #includes
  46672. + * Standardise an error return method.
  46673. + * Add debugging instrumentation.
  46674. + * Move message type checking to pfkey_msg_parse().
  46675. + * Add check for errno incorrectly set.
  46676. + * Add check for valid PID.
  46677. + * Add check for reserved illegally set.
  46678. + * Add check for message out of bounds.
  46679. + *
  46680. + * Revision 1.3 1999/04/15 17:58:07 rgb
  46681. + * Add RCSID labels.
  46682. + *
  46683. + * Revision 1.2 1999/04/15 15:37:26 rgb
  46684. + * Forward check changes from POST1_00 branch.
  46685. + *
  46686. + * Revision 1.1.2.2 1999/04/13 20:37:12 rgb
  46687. + * Header Title correction.
  46688. + *
  46689. + * Revision 1.1.2.1 1999/03/26 20:58:55 rgb
  46690. + * Add pfkeyv2 support to KLIPS.
  46691. + *
  46692. + *
  46693. + * RFC 2367
  46694. + * PF_KEY_v2 Key Management API
  46695. + */
  46696. --- /dev/null Tue Mar 11 13:02:56 2003
  46697. +++ linux/net/ipsec/pfkey_v2_build.c Mon Feb 9 13:51:03 2004
  46698. @@ -0,0 +1,1581 @@
  46699. +/*
  46700. + * RFC2367 PF_KEYv2 Key management API message parser
  46701. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  46702. + *
  46703. + * This program is free software; you can redistribute it and/or modify it
  46704. + * under the terms of the GNU General Public License as published by the
  46705. + * Free Software Foundation; either version 2 of the License, or (at your
  46706. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  46707. + *
  46708. + * This program is distributed in the hope that it will be useful, but
  46709. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  46710. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  46711. + * for more details.
  46712. + *
  46713. + * RCSID $Id: pfkey_v2_build.c,v 1.51.8.1 2006/05/01 14:36:39 mcr Exp $
  46714. + */
  46715. +
  46716. +/*
  46717. + * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
  46718. + */
  46719. +
  46720. +char pfkey_v2_build_c_version[] = "$Id: pfkey_v2_build.c,v 1.51.8.1 2006/05/01 14:36:39 mcr Exp $";
  46721. +
  46722. +/*
  46723. + * Some ugly stuff to allow consistent debugging code for use in the
  46724. + * kernel and in user space
  46725. +*/
  46726. +
  46727. +#ifdef __KERNEL__
  46728. +
  46729. +# include <linux/kernel.h> /* for printk */
  46730. +
  46731. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  46732. +# ifdef MALLOC_SLAB
  46733. +# include <linux/slab.h> /* kmalloc() */
  46734. +# else /* MALLOC_SLAB */
  46735. +# include <linux/malloc.h> /* kmalloc() */
  46736. +# endif /* MALLOC_SLAB */
  46737. +# include <linux/errno.h> /* error codes */
  46738. +# include <linux/types.h> /* size_t */
  46739. +# include <linux/interrupt.h> /* mark_bh */
  46740. +
  46741. +# include <linux/netdevice.h> /* struct device, and other headers */
  46742. +# include <linux/etherdevice.h> /* eth_type_trans */
  46743. +# include <linux/ip.h> /* struct iphdr */
  46744. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46745. +# include <linux/ipv6.h> /* struct ipv6hdr */
  46746. +# endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  46747. +
  46748. +# define MALLOC(size) kmalloc(size, GFP_ATOMIC)
  46749. +# define FREE(obj) kfree(obj)
  46750. +# include <openswan.h>
  46751. +#else /* __KERNEL__ */
  46752. +
  46753. +# include <sys/types.h>
  46754. +# include <linux/types.h>
  46755. +# include <linux/errno.h>
  46756. +# include <malloc.h>
  46757. +# include <string.h> /* memset */
  46758. +
  46759. +# include <openswan.h>
  46760. +
  46761. +#endif /* __KERNEL__ */
  46762. +
  46763. +#include <pfkeyv2.h>
  46764. +#include <pfkey.h>
  46765. +
  46766. +#ifdef __KERNEL__
  46767. +#include "openswan/radij.h" /* rd_nodes */
  46768. +#include "openswan/ipsec_encap.h" /* sockaddr_encap */
  46769. +#endif /* __KERNEL__ */
  46770. +
  46771. +
  46772. +#include "openswan/ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
  46773. +#include "openswan/pfkey_debug.h"
  46774. +
  46775. +
  46776. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  46777. +
  46778. +void
  46779. +pfkey_extensions_init(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
  46780. +{
  46781. + int i;
  46782. +
  46783. + for (i = 0; i != SADB_EXT_MAX + 1; i++) {
  46784. + extensions[i] = NULL;
  46785. + }
  46786. +}
  46787. +
  46788. +void
  46789. +pfkey_extensions_free(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
  46790. +{
  46791. + int i;
  46792. +
  46793. + if(!extensions) {
  46794. + return;
  46795. + }
  46796. +
  46797. + if(extensions[0]) {
  46798. + memset(extensions[0], 0, sizeof(struct sadb_msg));
  46799. + FREE(extensions[0]);
  46800. + extensions[0] = NULL;
  46801. + }
  46802. +
  46803. + for (i = 1; i != SADB_EXT_MAX + 1; i++) {
  46804. + if(extensions[i]) {
  46805. + memset(extensions[i], 0, extensions[i]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  46806. + FREE(extensions[i]);
  46807. + extensions[i] = NULL;
  46808. + }
  46809. + }
  46810. +}
  46811. +
  46812. +void
  46813. +pfkey_msg_free(struct sadb_msg **pfkey_msg)
  46814. +{
  46815. + if(*pfkey_msg) {
  46816. + memset(*pfkey_msg, 0, (*pfkey_msg)->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  46817. + FREE(*pfkey_msg);
  46818. + *pfkey_msg = NULL;
  46819. + }
  46820. +}
  46821. +
  46822. +/* Default extension builders taken from the KLIPS code */
  46823. +
  46824. +int
  46825. +pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
  46826. + uint8_t msg_type,
  46827. + uint8_t satype,
  46828. + uint8_t msg_errno,
  46829. + uint32_t seq,
  46830. + uint32_t pid)
  46831. +{
  46832. + int error = 0;
  46833. + struct sadb_msg *pfkey_msg = (struct sadb_msg *)*pfkey_ext;
  46834. +
  46835. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46836. + "pfkey_msg_hdr_build:\n");
  46837. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46838. + "pfkey_msg_hdr_build: "
  46839. + "on_entry &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
  46840. + &pfkey_ext,
  46841. + pfkey_ext,
  46842. + *pfkey_ext);
  46843. + /* sanity checks... */
  46844. + if(pfkey_msg) {
  46845. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46846. + "pfkey_msg_hdr_build: "
  46847. + "why is pfkey_msg already pointing to something?\n");
  46848. + SENDERR(EINVAL);
  46849. + }
  46850. +
  46851. + if(!msg_type) {
  46852. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46853. + "pfkey_msg_hdr_build: "
  46854. + "msg type not set, must be non-zero..\n");
  46855. + SENDERR(EINVAL);
  46856. + }
  46857. +
  46858. + if(msg_type > SADB_MAX) {
  46859. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46860. + "pfkey_msg_hdr_build: "
  46861. + "msg type too large:%d.\n",
  46862. + msg_type);
  46863. + SENDERR(EINVAL);
  46864. + }
  46865. +
  46866. + if(satype > SADB_SATYPE_MAX) {
  46867. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46868. + "pfkey_msg_hdr_build: "
  46869. + "satype %d > max %d\n",
  46870. + satype, SADB_SATYPE_MAX);
  46871. + SENDERR(EINVAL);
  46872. + }
  46873. +
  46874. + pfkey_msg = (struct sadb_msg*)MALLOC(sizeof(struct sadb_msg));
  46875. + *pfkey_ext = (struct sadb_ext*)pfkey_msg;
  46876. +
  46877. + if(pfkey_msg == NULL) {
  46878. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46879. + "pfkey_msg_hdr_build: "
  46880. + "memory allocation failed\n");
  46881. + SENDERR(ENOMEM);
  46882. + }
  46883. + memset(pfkey_msg, 0, sizeof(struct sadb_msg));
  46884. +
  46885. + pfkey_msg->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  46886. +
  46887. + pfkey_msg->sadb_msg_type = msg_type;
  46888. + pfkey_msg->sadb_msg_satype = satype;
  46889. +
  46890. + pfkey_msg->sadb_msg_version = PF_KEY_V2;
  46891. + pfkey_msg->sadb_msg_errno = msg_errno;
  46892. + pfkey_msg->sadb_msg_reserved = 0;
  46893. + pfkey_msg->sadb_msg_seq = seq;
  46894. + pfkey_msg->sadb_msg_pid = pid;
  46895. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46896. + "pfkey_msg_hdr_build: "
  46897. + "on_exit &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
  46898. + &pfkey_ext,
  46899. + pfkey_ext,
  46900. + *pfkey_ext);
  46901. +errlab:
  46902. + return error;
  46903. +}
  46904. +
  46905. +int
  46906. +pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
  46907. + uint16_t exttype,
  46908. + uint32_t spi,
  46909. + uint8_t replay_window,
  46910. + uint8_t sa_state,
  46911. + uint8_t auth,
  46912. + uint8_t encrypt,
  46913. + uint32_t flags,
  46914. + uint32_t/*IPsecSAref_t*/ ref)
  46915. +{
  46916. + int error = 0;
  46917. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)*pfkey_ext;
  46918. +
  46919. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46920. + "pfkey_sa_build: "
  46921. + "spi=%08x replay=%d sa_state=%d auth=%d encrypt=%d flags=%d\n",
  46922. + ntohl(spi), /* in network order */
  46923. + replay_window,
  46924. + sa_state,
  46925. + auth,
  46926. + encrypt,
  46927. + flags);
  46928. + /* sanity checks... */
  46929. + if(pfkey_sa) {
  46930. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46931. + "pfkey_sa_build: "
  46932. + "why is pfkey_sa already pointing to something?\n");
  46933. + SENDERR(EINVAL);
  46934. + }
  46935. +
  46936. + if(exttype != SADB_EXT_SA &&
  46937. + exttype != SADB_X_EXT_SA2) {
  46938. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46939. + "pfkey_sa_build: "
  46940. + "invalid exttype=%d.\n",
  46941. + exttype);
  46942. + SENDERR(EINVAL);
  46943. + }
  46944. +
  46945. + if(replay_window > 64) {
  46946. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46947. + "pfkey_sa_build: "
  46948. + "replay window size: %d -- must be 0 <= size <= 64\n",
  46949. + replay_window);
  46950. + SENDERR(EINVAL);
  46951. + }
  46952. +
  46953. + if(auth > SADB_AALG_MAX) {
  46954. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46955. + "pfkey_sa_build: "
  46956. + "auth=%d > SADB_AALG_MAX=%d.\n",
  46957. + auth,
  46958. + SADB_AALG_MAX);
  46959. + SENDERR(EINVAL);
  46960. + }
  46961. +
  46962. +#if SADB_EALG_MAX < 255
  46963. + if(encrypt > SADB_EALG_MAX) {
  46964. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46965. + "pfkey_sa_build: "
  46966. + "encrypt=%d > SADB_EALG_MAX=%d.\n",
  46967. + encrypt,
  46968. + SADB_EALG_MAX);
  46969. + SENDERR(EINVAL);
  46970. + }
  46971. +#endif
  46972. +
  46973. + if(sa_state > SADB_SASTATE_MAX) {
  46974. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46975. + "pfkey_sa_build: "
  46976. + "sa_state=%d exceeds MAX=%d.\n",
  46977. + sa_state,
  46978. + SADB_SASTATE_MAX);
  46979. + SENDERR(EINVAL);
  46980. + }
  46981. +
  46982. + if(sa_state == SADB_SASTATE_DEAD) {
  46983. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46984. + "pfkey_sa_build: "
  46985. + "sa_state=%d is DEAD=%d is not allowed.\n",
  46986. + sa_state,
  46987. + SADB_SASTATE_DEAD);
  46988. + SENDERR(EINVAL);
  46989. + }
  46990. +
  46991. + if((IPSEC_SAREF_NULL != ref) && (ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
  46992. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46993. + "pfkey_sa_build: "
  46994. + "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
  46995. + ref,
  46996. + IPSEC_SAREF_NULL,
  46997. + IPSEC_SA_REF_TABLE_NUM_ENTRIES);
  46998. + SENDERR(EINVAL);
  46999. + }
  47000. +
  47001. + pfkey_sa = (struct sadb_sa*)MALLOC(sizeof(struct sadb_sa));
  47002. + *pfkey_ext = (struct sadb_ext*)pfkey_sa;
  47003. +
  47004. + if(pfkey_sa == NULL) {
  47005. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47006. + "pfkey_sa_build: "
  47007. + "memory allocation failed\n");
  47008. + SENDERR(ENOMEM);
  47009. + }
  47010. + memset(pfkey_sa, 0, sizeof(struct sadb_sa));
  47011. +
  47012. + pfkey_sa->sadb_sa_len = sizeof(*pfkey_sa) / IPSEC_PFKEYv2_ALIGN;
  47013. + pfkey_sa->sadb_sa_exttype = exttype;
  47014. + pfkey_sa->sadb_sa_spi = spi;
  47015. + pfkey_sa->sadb_sa_replay = replay_window;
  47016. + pfkey_sa->sadb_sa_state = sa_state;
  47017. + pfkey_sa->sadb_sa_auth = auth;
  47018. + pfkey_sa->sadb_sa_encrypt = encrypt;
  47019. + pfkey_sa->sadb_sa_flags = flags;
  47020. + pfkey_sa->sadb_x_sa_ref = ref;
  47021. +
  47022. +errlab:
  47023. + return error;
  47024. +}
  47025. +
  47026. +int
  47027. +pfkey_sa_build(struct sadb_ext ** pfkey_ext,
  47028. + uint16_t exttype,
  47029. + uint32_t spi,
  47030. + uint8_t replay_window,
  47031. + uint8_t sa_state,
  47032. + uint8_t auth,
  47033. + uint8_t encrypt,
  47034. + uint32_t flags)
  47035. +{
  47036. + return pfkey_sa_ref_build(pfkey_ext,
  47037. + exttype,
  47038. + spi,
  47039. + replay_window,
  47040. + sa_state,
  47041. + auth,
  47042. + encrypt,
  47043. + flags,
  47044. + IPSEC_SAREF_NULL);
  47045. +}
  47046. +
  47047. +int
  47048. +pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
  47049. + uint16_t exttype,
  47050. + uint32_t allocations,
  47051. + uint64_t bytes,
  47052. + uint64_t addtime,
  47053. + uint64_t usetime,
  47054. + uint32_t packets)
  47055. +{
  47056. + int error = 0;
  47057. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)*pfkey_ext;
  47058. +
  47059. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47060. + "pfkey_lifetime_build:\n");
  47061. + /* sanity checks... */
  47062. + if(pfkey_lifetime) {
  47063. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47064. + "pfkey_lifetime_build: "
  47065. + "why is pfkey_lifetime already pointing to something?\n");
  47066. + SENDERR(EINVAL);
  47067. + }
  47068. +
  47069. + if(exttype != SADB_EXT_LIFETIME_CURRENT &&
  47070. + exttype != SADB_EXT_LIFETIME_HARD &&
  47071. + exttype != SADB_EXT_LIFETIME_SOFT) {
  47072. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47073. + "pfkey_lifetime_build: "
  47074. + "invalid exttype=%d.\n",
  47075. + exttype);
  47076. + SENDERR(EINVAL);
  47077. + }
  47078. +
  47079. + pfkey_lifetime = (struct sadb_lifetime*)MALLOC(sizeof(struct sadb_lifetime));
  47080. + *pfkey_ext = (struct sadb_ext*) pfkey_lifetime;
  47081. +
  47082. + if(pfkey_lifetime == NULL) {
  47083. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47084. + "pfkey_lifetime_build: "
  47085. + "memory allocation failed\n");
  47086. + SENDERR(ENOMEM);
  47087. + }
  47088. + memset(pfkey_lifetime, 0, sizeof(struct sadb_lifetime));
  47089. +
  47090. + pfkey_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN;
  47091. + pfkey_lifetime->sadb_lifetime_exttype = exttype;
  47092. + pfkey_lifetime->sadb_lifetime_allocations = allocations;
  47093. + pfkey_lifetime->sadb_lifetime_bytes = bytes;
  47094. + pfkey_lifetime->sadb_lifetime_addtime = addtime;
  47095. + pfkey_lifetime->sadb_lifetime_usetime = usetime;
  47096. + pfkey_lifetime->sadb_x_lifetime_packets = packets;
  47097. +
  47098. +errlab:
  47099. + return error;
  47100. +}
  47101. +
  47102. +int
  47103. +pfkey_address_build(struct sadb_ext** pfkey_ext,
  47104. + uint16_t exttype,
  47105. + uint8_t proto,
  47106. + uint8_t prefixlen,
  47107. + struct sockaddr* address)
  47108. +{
  47109. + int error = 0;
  47110. + int saddr_len = 0;
  47111. + char ipaddr_txt[ADDRTOT_BUF + 6/*extra for port number*/];
  47112. + struct sadb_address *pfkey_address = (struct sadb_address *)*pfkey_ext;
  47113. +
  47114. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47115. + "pfkey_address_build: "
  47116. + "exttype=%d proto=%d prefixlen=%d\n",
  47117. + exttype,
  47118. + proto,
  47119. + prefixlen);
  47120. + /* sanity checks... */
  47121. + if(pfkey_address) {
  47122. + ERROR("pfkey_address_build: "
  47123. + "why is pfkey_address already pointing to something?\n");
  47124. + SENDERR(EINVAL);
  47125. + }
  47126. +
  47127. + if (!address) {
  47128. + ERROR("pfkey_address_build: " "address is NULL\n");
  47129. + SENDERR(EINVAL);
  47130. + }
  47131. +
  47132. + switch(exttype) {
  47133. + case SADB_EXT_ADDRESS_SRC:
  47134. + case SADB_EXT_ADDRESS_DST:
  47135. + case SADB_EXT_ADDRESS_PROXY:
  47136. + case SADB_X_EXT_ADDRESS_DST2:
  47137. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  47138. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  47139. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  47140. + case SADB_X_EXT_ADDRESS_DST_MASK:
  47141. +#ifdef NAT_TRAVERSAL
  47142. + case SADB_X_EXT_NAT_T_OA:
  47143. +#endif
  47144. + break;
  47145. + default:
  47146. + ERROR("pfkey_address_build: "
  47147. + "unrecognised ext_type=%d.\n",
  47148. + exttype);
  47149. + SENDERR(EINVAL);
  47150. + }
  47151. +
  47152. + switch(address->sa_family) {
  47153. + case AF_INET:
  47154. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47155. + "pfkey_address_build: "
  47156. + "found address family AF_INET.\n");
  47157. + saddr_len = sizeof(struct sockaddr_in);
  47158. + sprintf(ipaddr_txt, "%d.%d.%d.%d:%d"
  47159. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 0) & 0xFF
  47160. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 8) & 0xFF
  47161. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 16) & 0xFF
  47162. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 24) & 0xFF
  47163. + , ntohs(((struct sockaddr_in*)address)->sin_port));
  47164. + break;
  47165. + case AF_INET6:
  47166. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47167. + "pfkey_address_build: "
  47168. + "found address family AF_INET6.\n");
  47169. + saddr_len = sizeof(struct sockaddr_in6);
  47170. + sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x-%x"
  47171. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[0])
  47172. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[1])
  47173. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[2])
  47174. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[3])
  47175. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[4])
  47176. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[5])
  47177. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[6])
  47178. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[7])
  47179. + , ntohs(((struct sockaddr_in6*)address)->sin6_port));
  47180. + break;
  47181. + default:
  47182. + ERROR("pfkey_address_build: "
  47183. + "address->sa_family=%d not supported.\n",
  47184. + address->sa_family);
  47185. + SENDERR(EPFNOSUPPORT);
  47186. + }
  47187. +
  47188. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47189. + "pfkey_address_build: "
  47190. + "found address=%s.\n",
  47191. + ipaddr_txt);
  47192. + if(prefixlen != 0) {
  47193. + ERROR("pfkey_address_build: "
  47194. + "address prefixes not supported yet.\n");
  47195. + SENDERR(EAFNOSUPPORT); /* not supported yet */
  47196. + }
  47197. +
  47198. + /* allocate some memory for the extension */
  47199. + pfkey_address = (struct sadb_address*)
  47200. + MALLOC(ALIGN_N(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN));
  47201. + *pfkey_ext = (struct sadb_ext*)pfkey_address;
  47202. +
  47203. + if(pfkey_address == NULL ) {
  47204. + ERROR("pfkey_lifetime_build: "
  47205. + "memory allocation failed\n");
  47206. + SENDERR(ENOMEM);
  47207. + }
  47208. + memset(pfkey_address,
  47209. + 0,
  47210. + ALIGN_N(sizeof(struct sadb_address) + saddr_len,
  47211. + IPSEC_PFKEYv2_ALIGN));
  47212. +
  47213. + pfkey_address->sadb_address_len = DIVUP(sizeof(struct sadb_address) + saddr_len,
  47214. + IPSEC_PFKEYv2_ALIGN);
  47215. +
  47216. + pfkey_address->sadb_address_exttype = exttype;
  47217. + pfkey_address->sadb_address_proto = proto;
  47218. + pfkey_address->sadb_address_prefixlen = prefixlen;
  47219. + pfkey_address->sadb_address_reserved = 0;
  47220. +
  47221. + memcpy((char*)pfkey_address + sizeof(struct sadb_address),
  47222. + address,
  47223. + saddr_len);
  47224. +
  47225. +#if 0
  47226. + for(i = 0; i < sizeof(struct sockaddr_in) - offsetof(struct sockaddr_in, sin_zero); i++) {
  47227. + pfkey_address_s_ska.sin_zero[i] = 0;
  47228. + }
  47229. +#endif
  47230. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47231. + "pfkey_address_build: "
  47232. + "successful created len: %d.\n", pfkey_address->sadb_address_len);
  47233. +
  47234. + errlab:
  47235. + return error;
  47236. +}
  47237. +
  47238. +int
  47239. +pfkey_key_build(struct sadb_ext** pfkey_ext,
  47240. + uint16_t exttype,
  47241. + uint16_t key_bits,
  47242. + char* key)
  47243. +{
  47244. + int error = 0;
  47245. + struct sadb_key *pfkey_key = (struct sadb_key *)*pfkey_ext;
  47246. +
  47247. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47248. + "pfkey_key_build:\n");
  47249. + /* sanity checks... */
  47250. + if(pfkey_key) {
  47251. + ERROR("pfkey_key_build: "
  47252. + "why is pfkey_key already pointing to something?\n");
  47253. + SENDERR(EINVAL);
  47254. + }
  47255. +
  47256. + if(!key_bits) {
  47257. + ERROR("pfkey_key_build: "
  47258. + "key_bits is zero, it must be non-zero.\n");
  47259. + SENDERR(EINVAL);
  47260. + }
  47261. +
  47262. + if( !((exttype == SADB_EXT_KEY_AUTH) || (exttype == SADB_EXT_KEY_ENCRYPT))) {
  47263. + ERROR("pfkey_key_build: "
  47264. + "unsupported extension type=%d.\n",
  47265. + exttype);
  47266. + SENDERR(EINVAL);
  47267. + }
  47268. +
  47269. + pfkey_key = (struct sadb_key*)
  47270. + MALLOC(sizeof(struct sadb_key) +
  47271. + DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
  47272. +
  47273. + *pfkey_ext = (struct sadb_ext*)pfkey_key;
  47274. +
  47275. + if(pfkey_key == NULL) {
  47276. + ERROR("pfkey_key_build: "
  47277. + "memory allocation failed\n");
  47278. + SENDERR(ENOMEM);
  47279. + }
  47280. + memset(pfkey_key,
  47281. + 0,
  47282. + sizeof(struct sadb_key) +
  47283. + DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
  47284. +
  47285. + pfkey_key->sadb_key_len = DIVUP(sizeof(struct sadb_key) * IPSEC_PFKEYv2_ALIGN + key_bits,
  47286. + 64);
  47287. + pfkey_key->sadb_key_exttype = exttype;
  47288. + pfkey_key->sadb_key_bits = key_bits;
  47289. + pfkey_key->sadb_key_reserved = 0;
  47290. + memcpy((char*)pfkey_key + sizeof(struct sadb_key),
  47291. + key,
  47292. + DIVUP(key_bits, 8));
  47293. +
  47294. +errlab:
  47295. + return error;
  47296. +}
  47297. +
  47298. +int
  47299. +pfkey_ident_build(struct sadb_ext** pfkey_ext,
  47300. + uint16_t exttype,
  47301. + uint16_t ident_type,
  47302. + uint64_t ident_id,
  47303. + uint8_t ident_len,
  47304. + char* ident_string)
  47305. +{
  47306. + int error = 0;
  47307. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)*pfkey_ext;
  47308. + int data_len = ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  47309. +
  47310. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47311. + "pfkey_ident_build:\n");
  47312. + /* sanity checks... */
  47313. + if(pfkey_ident) {
  47314. + ERROR("pfkey_ident_build: "
  47315. + "why is pfkey_ident already pointing to something?\n");
  47316. + SENDERR(EINVAL);
  47317. + }
  47318. +
  47319. + if( ! ((exttype == SADB_EXT_IDENTITY_SRC) ||
  47320. + (exttype == SADB_EXT_IDENTITY_DST))) {
  47321. + ERROR("pfkey_ident_build: "
  47322. + "unsupported extension type=%d.\n",
  47323. + exttype);
  47324. + SENDERR(EINVAL);
  47325. + }
  47326. +
  47327. + if((ident_type == SADB_IDENTTYPE_RESERVED)) {
  47328. + ERROR("pfkey_ident_build: "
  47329. + "ident_type must be non-zero.\n");
  47330. + SENDERR(EINVAL);
  47331. + }
  47332. +
  47333. + if(ident_type > SADB_IDENTTYPE_MAX) {
  47334. + ERROR("pfkey_ident_build: "
  47335. + "identtype=%d out of range.\n",
  47336. + ident_type);
  47337. + SENDERR(EINVAL);
  47338. + }
  47339. +
  47340. + if(((ident_type == SADB_IDENTTYPE_PREFIX) ||
  47341. + (ident_type == SADB_IDENTTYPE_FQDN)) &&
  47342. + !ident_string) {
  47343. + ERROR("pfkey_ident_build: "
  47344. + "string required to allocate size of extension.\n");
  47345. + SENDERR(EINVAL);
  47346. + }
  47347. +
  47348. +#if 0
  47349. + if((ident_type == SADB_IDENTTYPE_USERFQDN) ) {
  47350. + }
  47351. +#endif
  47352. +
  47353. + pfkey_ident = (struct sadb_ident*)
  47354. + MALLOC(ident_len * IPSEC_PFKEYv2_ALIGN);
  47355. +
  47356. + *pfkey_ext = (struct sadb_ext*)pfkey_ident;
  47357. +
  47358. + if(pfkey_ident == NULL) {
  47359. + ERROR("pfkey_ident_build: "
  47360. + "memory allocation failed\n");
  47361. + SENDERR(ENOMEM);
  47362. + }
  47363. + memset(pfkey_ident, 0, ident_len * IPSEC_PFKEYv2_ALIGN);
  47364. +
  47365. + pfkey_ident->sadb_ident_len = ident_len;
  47366. + pfkey_ident->sadb_ident_exttype = exttype;
  47367. + pfkey_ident->sadb_ident_type = ident_type;
  47368. + pfkey_ident->sadb_ident_reserved = 0;
  47369. + pfkey_ident->sadb_ident_id = ident_id;
  47370. + memcpy((char*)pfkey_ident + sizeof(struct sadb_ident),
  47371. + ident_string,
  47372. + data_len);
  47373. +
  47374. +errlab:
  47375. + return error;
  47376. +}
  47377. +
  47378. +int
  47379. +pfkey_sens_build(struct sadb_ext** pfkey_ext,
  47380. + uint32_t dpd,
  47381. + uint8_t sens_level,
  47382. + uint8_t sens_len,
  47383. + uint64_t* sens_bitmap,
  47384. + uint8_t integ_level,
  47385. + uint8_t integ_len,
  47386. + uint64_t* integ_bitmap)
  47387. +{
  47388. + int error = 0;
  47389. + struct sadb_sens *pfkey_sens = (struct sadb_sens *)*pfkey_ext;
  47390. + int i;
  47391. + uint64_t* bitmap;
  47392. +
  47393. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47394. + "pfkey_sens_build:\n");
  47395. + /* sanity checks... */
  47396. + if(pfkey_sens) {
  47397. + ERROR("pfkey_sens_build: "
  47398. + "why is pfkey_sens already pointing to something?\n");
  47399. + SENDERR(EINVAL);
  47400. + }
  47401. +
  47402. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47403. + "pfkey_sens_build: "
  47404. + "Sorry, I can't build exttype=%d yet.\n",
  47405. + (*pfkey_ext)->sadb_ext_type);
  47406. + SENDERR(EINVAL); /* don't process these yet */
  47407. +
  47408. + pfkey_sens = (struct sadb_sens*)
  47409. + MALLOC(sizeof(struct sadb_sens) +
  47410. + (sens_len + integ_len) * sizeof(uint64_t));
  47411. +
  47412. + *pfkey_ext = (struct sadb_ext*)pfkey_sens;
  47413. +
  47414. + if(pfkey_sens == NULL) {
  47415. + ERROR("pfkey_sens_build: "
  47416. + "memory allocation failed\n");
  47417. + SENDERR(ENOMEM);
  47418. + }
  47419. + memset(pfkey_sens,
  47420. + 0,
  47421. + sizeof(struct sadb_sens) +
  47422. + (sens_len + integ_len) * sizeof(uint64_t));
  47423. +
  47424. + pfkey_sens->sadb_sens_len = (sizeof(struct sadb_sens) +
  47425. + (sens_len + integ_len) * sizeof(uint64_t)) / IPSEC_PFKEYv2_ALIGN;
  47426. + pfkey_sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
  47427. + pfkey_sens->sadb_sens_dpd = dpd;
  47428. + pfkey_sens->sadb_sens_sens_level = sens_level;
  47429. + pfkey_sens->sadb_sens_sens_len = sens_len;
  47430. + pfkey_sens->sadb_sens_integ_level = integ_level;
  47431. + pfkey_sens->sadb_sens_integ_len = integ_len;
  47432. + pfkey_sens->sadb_sens_reserved = 0;
  47433. +
  47434. + bitmap = (uint64_t*)((char*)pfkey_ext + sizeof(struct sadb_sens));
  47435. + for(i = 0; i < sens_len; i++) {
  47436. + *bitmap = sens_bitmap[i];
  47437. + bitmap++;
  47438. + }
  47439. + for(i = 0; i < integ_len; i++) {
  47440. + *bitmap = integ_bitmap[i];
  47441. + bitmap++;
  47442. + }
  47443. +
  47444. +errlab:
  47445. + return error;
  47446. +}
  47447. +
  47448. +int
  47449. +pfkey_prop_build(struct sadb_ext** pfkey_ext,
  47450. + uint8_t replay,
  47451. + unsigned int comb_num,
  47452. + struct sadb_comb* comb)
  47453. +{
  47454. + int error = 0;
  47455. + int i;
  47456. + struct sadb_prop *pfkey_prop = (struct sadb_prop *)*pfkey_ext;
  47457. + struct sadb_comb *combp;
  47458. +
  47459. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47460. + "pfkey_prop_build:\n");
  47461. + /* sanity checks... */
  47462. + if(pfkey_prop) {
  47463. + ERROR("pfkey_prop_build: "
  47464. + "why is pfkey_prop already pointing to something?\n");
  47465. + SENDERR(EINVAL);
  47466. + }
  47467. +
  47468. + pfkey_prop = (struct sadb_prop*)
  47469. + MALLOC(sizeof(struct sadb_prop) +
  47470. + comb_num * sizeof(struct sadb_comb));
  47471. +
  47472. + *pfkey_ext = (struct sadb_ext*)pfkey_prop;
  47473. +
  47474. + if(pfkey_prop == NULL) {
  47475. + ERROR("pfkey_prop_build: "
  47476. + "memory allocation failed\n");
  47477. + SENDERR(ENOMEM);
  47478. + }
  47479. + memset(pfkey_prop,
  47480. + 0,
  47481. + sizeof(struct sadb_prop) +
  47482. + comb_num * sizeof(struct sadb_comb));
  47483. +
  47484. + pfkey_prop->sadb_prop_len = (sizeof(struct sadb_prop) +
  47485. + comb_num * sizeof(struct sadb_comb)) / IPSEC_PFKEYv2_ALIGN;
  47486. +
  47487. + pfkey_prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
  47488. + pfkey_prop->sadb_prop_replay = replay;
  47489. +
  47490. + for(i=0; i<3; i++) {
  47491. + pfkey_prop->sadb_prop_reserved[i] = 0;
  47492. + }
  47493. +
  47494. + combp = (struct sadb_comb*)((char*)*pfkey_ext + sizeof(struct sadb_prop));
  47495. + for(i = 0; i < comb_num; i++) {
  47496. + memcpy (combp, &(comb[i]), sizeof(struct sadb_comb));
  47497. + combp++;
  47498. + }
  47499. +
  47500. +#if 0
  47501. + uint8_t sadb_comb_auth;
  47502. + uint8_t sadb_comb_encrypt;
  47503. + uint16_t sadb_comb_flags;
  47504. + uint16_t sadb_comb_auth_minbits;
  47505. + uint16_t sadb_comb_auth_maxbits;
  47506. + uint16_t sadb_comb_encrypt_minbits;
  47507. + uint16_t sadb_comb_encrypt_maxbits;
  47508. + uint32_t sadb_comb_reserved;
  47509. + uint32_t sadb_comb_soft_allocations;
  47510. + uint32_t sadb_comb_hard_allocations;
  47511. + uint64_t sadb_comb_soft_bytes;
  47512. + uint64_t sadb_comb_hard_bytes;
  47513. + uint64_t sadb_comb_soft_addtime;
  47514. + uint64_t sadb_comb_hard_addtime;
  47515. + uint64_t sadb_comb_soft_usetime;
  47516. + uint64_t sadb_comb_hard_usetime;
  47517. + uint32_t sadb_comb_soft_packets;
  47518. + uint32_t sadb_comb_hard_packets;
  47519. +#endif
  47520. +errlab:
  47521. + return error;
  47522. +}
  47523. +
  47524. +int
  47525. +pfkey_supported_build(struct sadb_ext** pfkey_ext,
  47526. + uint16_t exttype,
  47527. + unsigned int alg_num,
  47528. + struct sadb_alg* alg)
  47529. +{
  47530. + int error = 0;
  47531. + unsigned int i;
  47532. + struct sadb_supported *pfkey_supported = (struct sadb_supported *)*pfkey_ext;
  47533. + struct sadb_alg *pfkey_alg;
  47534. +
  47535. + /* sanity checks... */
  47536. + if(pfkey_supported) {
  47537. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47538. + "pfkey_supported_build: "
  47539. + "why is pfkey_supported already pointing to something?\n");
  47540. + SENDERR(EINVAL);
  47541. + }
  47542. +
  47543. + if( !((exttype == SADB_EXT_SUPPORTED_AUTH) || (exttype == SADB_EXT_SUPPORTED_ENCRYPT))) {
  47544. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47545. + "pfkey_supported_build: "
  47546. + "unsupported extension type=%d.\n",
  47547. + exttype);
  47548. + SENDERR(EINVAL);
  47549. + }
  47550. +
  47551. + pfkey_supported = (struct sadb_supported*)
  47552. + MALLOC(sizeof(struct sadb_supported) +
  47553. + alg_num *
  47554. + sizeof(struct sadb_alg));
  47555. +
  47556. + *pfkey_ext = (struct sadb_ext*)pfkey_supported;
  47557. +
  47558. + if(pfkey_supported == NULL) {
  47559. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47560. + "pfkey_supported_build: "
  47561. + "memory allocation failed\n");
  47562. + SENDERR(ENOMEM);
  47563. + }
  47564. + memset(pfkey_supported,
  47565. + 0,
  47566. + sizeof(struct sadb_supported) +
  47567. + alg_num *
  47568. + sizeof(struct sadb_alg));
  47569. +
  47570. + pfkey_supported->sadb_supported_len = (sizeof(struct sadb_supported) +
  47571. + alg_num *
  47572. + sizeof(struct sadb_alg)) /
  47573. + IPSEC_PFKEYv2_ALIGN;
  47574. + pfkey_supported->sadb_supported_exttype = exttype;
  47575. + pfkey_supported->sadb_supported_reserved = 0;
  47576. +
  47577. + pfkey_alg = (struct sadb_alg*)((char*)pfkey_supported + sizeof(struct sadb_supported));
  47578. + for(i = 0; i < alg_num; i++) {
  47579. + memcpy (pfkey_alg, &(alg[i]), sizeof(struct sadb_alg));
  47580. + pfkey_alg->sadb_alg_reserved = 0;
  47581. + pfkey_alg++;
  47582. + }
  47583. +
  47584. +#if 0
  47585. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47586. + "pfkey_supported_build: "
  47587. + "Sorry, I can't build exttype=%d yet.\n",
  47588. + (*pfkey_ext)->sadb_ext_type);
  47589. + SENDERR(EINVAL); /* don't process these yet */
  47590. +
  47591. + uint8_t sadb_alg_id;
  47592. + uint8_t sadb_alg_ivlen;
  47593. + uint16_t sadb_alg_minbits;
  47594. + uint16_t sadb_alg_maxbits;
  47595. + uint16_t sadb_alg_reserved;
  47596. +#endif
  47597. +errlab:
  47598. + return error;
  47599. +}
  47600. +
  47601. +int
  47602. +pfkey_spirange_build(struct sadb_ext** pfkey_ext,
  47603. + uint16_t exttype,
  47604. + uint32_t min, /* in network order */
  47605. + uint32_t max) /* in network order */
  47606. +{
  47607. + int error = 0;
  47608. + struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)*pfkey_ext;
  47609. +
  47610. + /* sanity checks... */
  47611. + if(pfkey_spirange) {
  47612. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47613. + "pfkey_spirange_build: "
  47614. + "why is pfkey_spirange already pointing to something?\n");
  47615. + SENDERR(EINVAL);
  47616. + }
  47617. +
  47618. + if(ntohl(max) < ntohl(min)) {
  47619. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47620. + "pfkey_spirange_build: "
  47621. + "minspi=%08x must be < maxspi=%08x.\n",
  47622. + ntohl(min),
  47623. + ntohl(max));
  47624. + SENDERR(EINVAL);
  47625. + }
  47626. +
  47627. + if(ntohl(min) <= 255) {
  47628. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47629. + "pfkey_spirange_build: "
  47630. + "minspi=%08x must be > 255.\n",
  47631. + ntohl(min));
  47632. + SENDERR(EEXIST);
  47633. + }
  47634. +
  47635. + pfkey_spirange = (struct sadb_spirange*)
  47636. + MALLOC(sizeof(struct sadb_spirange));
  47637. +
  47638. + *pfkey_ext = (struct sadb_ext*)pfkey_spirange;
  47639. +
  47640. + if(pfkey_spirange == NULL) {
  47641. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47642. + "pfkey_spirange_build: "
  47643. + "memory allocation failed\n");
  47644. + SENDERR(ENOMEM);
  47645. + }
  47646. + memset(pfkey_spirange,
  47647. + 0,
  47648. + sizeof(struct sadb_spirange));
  47649. +
  47650. + pfkey_spirange->sadb_spirange_len = sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN;
  47651. +
  47652. + pfkey_spirange->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
  47653. + pfkey_spirange->sadb_spirange_min = min;
  47654. + pfkey_spirange->sadb_spirange_max = max;
  47655. + pfkey_spirange->sadb_spirange_reserved = 0;
  47656. + errlab:
  47657. + return error;
  47658. +}
  47659. +
  47660. +int
  47661. +pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext)
  47662. +{
  47663. + int error = 0;
  47664. + struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)*pfkey_ext;
  47665. +
  47666. + /* sanity checks... */
  47667. + if(pfkey_x_kmprivate) {
  47668. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47669. + "pfkey_x_kmprivate_build: "
  47670. + "why is pfkey_x_kmprivate already pointing to something?\n");
  47671. + SENDERR(EINVAL);
  47672. + }
  47673. +
  47674. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
  47675. +
  47676. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47677. + "pfkey_x_kmprivate_build: "
  47678. + "Sorry, I can't build exttype=%d yet.\n",
  47679. + (*pfkey_ext)->sadb_ext_type);
  47680. + SENDERR(EINVAL); /* don't process these yet */
  47681. +
  47682. + pfkey_x_kmprivate = (struct sadb_x_kmprivate*)
  47683. + MALLOC(sizeof(struct sadb_x_kmprivate));
  47684. +
  47685. + *pfkey_ext = (struct sadb_ext*)pfkey_x_kmprivate;
  47686. +
  47687. + if(pfkey_x_kmprivate == NULL) {
  47688. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47689. + "pfkey_x_kmprivate_build: "
  47690. + "memory allocation failed\n");
  47691. + SENDERR(ENOMEM);
  47692. + }
  47693. + memset(pfkey_x_kmprivate,
  47694. + 0,
  47695. + sizeof(struct sadb_x_kmprivate));
  47696. +
  47697. + pfkey_x_kmprivate->sadb_x_kmprivate_len =
  47698. + sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN;
  47699. +
  47700. + pfkey_x_kmprivate->sadb_x_kmprivate_exttype = SADB_X_EXT_KMPRIVATE;
  47701. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
  47702. +errlab:
  47703. + return error;
  47704. +}
  47705. +
  47706. +int
  47707. +pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
  47708. + uint8_t satype)
  47709. +{
  47710. + int error = 0;
  47711. + int i;
  47712. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)*pfkey_ext;
  47713. +
  47714. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47715. + "pfkey_x_satype_build:\n");
  47716. + /* sanity checks... */
  47717. + if(pfkey_x_satype) {
  47718. + ERROR("pfkey_x_satype_build: "
  47719. + "why is pfkey_x_satype already pointing to something?\n");
  47720. + SENDERR(EINVAL);
  47721. + }
  47722. +
  47723. + if(!satype) {
  47724. + ERROR("pfkey_x_satype_build: "
  47725. + "SA type not set, must be non-zero.\n");
  47726. + SENDERR(EINVAL);
  47727. + }
  47728. +
  47729. + if(satype > SADB_SATYPE_MAX) {
  47730. + ERROR("pfkey_x_satype_build: "
  47731. + "satype %d > max %d\n",
  47732. + satype, SADB_SATYPE_MAX);
  47733. + SENDERR(EINVAL);
  47734. + }
  47735. +
  47736. + pfkey_x_satype = (struct sadb_x_satype*)
  47737. + MALLOC(sizeof(struct sadb_x_satype));
  47738. +
  47739. + *pfkey_ext = (struct sadb_ext*)pfkey_x_satype;
  47740. + if(pfkey_x_satype == NULL) {
  47741. + ERROR("pfkey_x_satype_build: "
  47742. + "memory allocation failed\n");
  47743. + SENDERR(ENOMEM);
  47744. + }
  47745. + memset(pfkey_x_satype,
  47746. + 0,
  47747. + sizeof(struct sadb_x_satype));
  47748. +
  47749. + pfkey_x_satype->sadb_x_satype_len = sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN;
  47750. +
  47751. + pfkey_x_satype->sadb_x_satype_exttype = SADB_X_EXT_SATYPE2;
  47752. + pfkey_x_satype->sadb_x_satype_satype = satype;
  47753. + for(i=0; i<3; i++) {
  47754. + pfkey_x_satype->sadb_x_satype_reserved[i] = 0;
  47755. + }
  47756. +
  47757. +errlab:
  47758. + return error;
  47759. +}
  47760. +
  47761. +int
  47762. +pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
  47763. + uint32_t tunnel,
  47764. + uint32_t netlink,
  47765. + uint32_t xform,
  47766. + uint32_t eroute,
  47767. + uint32_t spi,
  47768. + uint32_t radij,
  47769. + uint32_t esp,
  47770. + uint32_t ah,
  47771. + uint32_t rcv,
  47772. + uint32_t pfkey,
  47773. + uint32_t ipcomp,
  47774. + uint32_t verbose)
  47775. +{
  47776. + int error = 0;
  47777. + int i;
  47778. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)*pfkey_ext;
  47779. +
  47780. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47781. + "pfkey_x_debug_build:\n");
  47782. + /* sanity checks... */
  47783. + if(pfkey_x_debug) {
  47784. + ERROR("pfkey_x_debug_build: "
  47785. + "why is pfkey_x_debug already pointing to something?\n");
  47786. + SENDERR(EINVAL);
  47787. + }
  47788. +
  47789. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47790. + "pfkey_x_debug_build: "
  47791. + "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",
  47792. + tunnel, netlink, xform, eroute, spi, radij, esp, ah, rcv, pfkey, ipcomp, verbose);
  47793. +
  47794. + pfkey_x_debug = (struct sadb_x_debug*)
  47795. + MALLOC(sizeof(struct sadb_x_debug));
  47796. +
  47797. + *pfkey_ext = (struct sadb_ext*)pfkey_x_debug;
  47798. +
  47799. + if(pfkey_x_debug == NULL) {
  47800. + ERROR("pfkey_x_debug_build: "
  47801. + "memory allocation failed\n");
  47802. + SENDERR(ENOMEM);
  47803. + }
  47804. +#if 0
  47805. + memset(pfkey_x_debug,
  47806. + 0,
  47807. + sizeof(struct sadb_x_debug));
  47808. +#endif
  47809. +
  47810. + pfkey_x_debug->sadb_x_debug_len = sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN;
  47811. + pfkey_x_debug->sadb_x_debug_exttype = SADB_X_EXT_DEBUG;
  47812. +
  47813. + pfkey_x_debug->sadb_x_debug_tunnel = tunnel;
  47814. + pfkey_x_debug->sadb_x_debug_netlink = netlink;
  47815. + pfkey_x_debug->sadb_x_debug_xform = xform;
  47816. + pfkey_x_debug->sadb_x_debug_eroute = eroute;
  47817. + pfkey_x_debug->sadb_x_debug_spi = spi;
  47818. + pfkey_x_debug->sadb_x_debug_radij = radij;
  47819. + pfkey_x_debug->sadb_x_debug_esp = esp;
  47820. + pfkey_x_debug->sadb_x_debug_ah = ah;
  47821. + pfkey_x_debug->sadb_x_debug_rcv = rcv;
  47822. + pfkey_x_debug->sadb_x_debug_pfkey = pfkey;
  47823. + pfkey_x_debug->sadb_x_debug_ipcomp = ipcomp;
  47824. + pfkey_x_debug->sadb_x_debug_verbose = verbose;
  47825. +
  47826. + for(i=0; i<4; i++) {
  47827. + pfkey_x_debug->sadb_x_debug_reserved[i] = 0;
  47828. + }
  47829. +
  47830. +errlab:
  47831. + return error;
  47832. +}
  47833. +
  47834. +int
  47835. +pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
  47836. + uint8_t type)
  47837. +{
  47838. + int error = 0;
  47839. + int i;
  47840. + struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)*pfkey_ext;
  47841. +
  47842. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47843. + "pfkey_x_nat_t_type_build:\n");
  47844. + /* sanity checks... */
  47845. + if(pfkey_x_nat_t_type) {
  47846. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47847. + "pfkey_x_nat_t_type_build: "
  47848. + "why is pfkey_x_nat_t_type already pointing to something?\n");
  47849. + SENDERR(EINVAL);
  47850. + }
  47851. +
  47852. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47853. + "pfkey_x_nat_t_type_build: "
  47854. + "type=%d\n", type);
  47855. +
  47856. + pfkey_x_nat_t_type = (struct sadb_x_nat_t_type*)
  47857. + MALLOC(sizeof(struct sadb_x_nat_t_type));
  47858. +
  47859. + *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_type;
  47860. +
  47861. + if(pfkey_x_nat_t_type == NULL) {
  47862. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47863. + "pfkey_x_nat_t_type_build: "
  47864. + "memory allocation failed\n");
  47865. + SENDERR(ENOMEM);
  47866. + }
  47867. +
  47868. + pfkey_x_nat_t_type->sadb_x_nat_t_type_len = sizeof(struct sadb_x_nat_t_type) / IPSEC_PFKEYv2_ALIGN;
  47869. + pfkey_x_nat_t_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
  47870. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type = type;
  47871. + for(i=0; i<3; i++) {
  47872. + pfkey_x_nat_t_type->sadb_x_nat_t_type_reserved[i] = 0;
  47873. + }
  47874. +
  47875. +errlab:
  47876. + return error;
  47877. +}
  47878. +int
  47879. +pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
  47880. + uint16_t exttype,
  47881. + uint16_t port)
  47882. +{
  47883. + int error = 0;
  47884. + struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)*pfkey_ext;
  47885. +
  47886. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47887. + "pfkey_x_nat_t_port_build:\n");
  47888. + /* sanity checks... */
  47889. + if(pfkey_x_nat_t_port) {
  47890. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47891. + "pfkey_x_nat_t_port_build: "
  47892. + "why is pfkey_x_nat_t_port already pointing to something?\n");
  47893. + SENDERR(EINVAL);
  47894. + }
  47895. +
  47896. + switch(exttype) {
  47897. + case SADB_X_EXT_NAT_T_SPORT:
  47898. + case SADB_X_EXT_NAT_T_DPORT:
  47899. + break;
  47900. + default:
  47901. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47902. + "pfkey_nat_t_port_build: "
  47903. + "unrecognised ext_type=%d.\n",
  47904. + exttype);
  47905. + SENDERR(EINVAL);
  47906. + }
  47907. +
  47908. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47909. + "pfkey_x_nat_t_port_build: "
  47910. + "ext=%d, port=%d\n", exttype, port);
  47911. +
  47912. + pfkey_x_nat_t_port = (struct sadb_x_nat_t_port*)
  47913. + MALLOC(sizeof(struct sadb_x_nat_t_port));
  47914. +
  47915. + *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_port;
  47916. +
  47917. + if(pfkey_x_nat_t_port == NULL) {
  47918. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47919. + "pfkey_x_nat_t_port_build: "
  47920. + "memory allocation failed\n");
  47921. + SENDERR(ENOMEM);
  47922. + }
  47923. +
  47924. + pfkey_x_nat_t_port->sadb_x_nat_t_port_len = sizeof(struct sadb_x_nat_t_port) / IPSEC_PFKEYv2_ALIGN;
  47925. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype = exttype;
  47926. + pfkey_x_nat_t_port->sadb_x_nat_t_port_port = port;
  47927. + pfkey_x_nat_t_port->sadb_x_nat_t_port_reserved = 0;
  47928. +
  47929. +errlab:
  47930. + return error;
  47931. +}
  47932. +
  47933. +int pfkey_x_protocol_build(struct sadb_ext **pfkey_ext,
  47934. + uint8_t protocol)
  47935. +{
  47936. + int error = 0;
  47937. + struct sadb_protocol * p = (struct sadb_protocol *)*pfkey_ext;
  47938. + DEBUGGING(PF_KEY_DEBUG_BUILD,"pfkey_x_protocol_build: protocol=%u\n", protocol);
  47939. + /* sanity checks... */
  47940. + if (p != 0) {
  47941. + ERROR("pfkey_x_protocol_build: bogus protocol pointer\n");
  47942. + SENDERR(EINVAL);
  47943. + }
  47944. + if ((p = (struct sadb_protocol*)MALLOC(sizeof(*p))) == 0) {
  47945. + ERROR("pfkey_build: memory allocation failed\n");
  47946. + SENDERR(ENOMEM);
  47947. + }
  47948. + *pfkey_ext = (struct sadb_ext *)p;
  47949. + p->sadb_protocol_len = sizeof(*p) / sizeof(uint64_t);
  47950. + p->sadb_protocol_exttype = SADB_X_EXT_PROTOCOL;
  47951. + p->sadb_protocol_proto = protocol;
  47952. + p->sadb_protocol_flags = 0;
  47953. + p->sadb_protocol_reserved2 = 0;
  47954. + errlab:
  47955. + return error;
  47956. +}
  47957. +
  47958. +int
  47959. +pfkey_msg_build(struct sadb_msg **pfkey_msg, struct sadb_ext *extensions[], int dir)
  47960. +{
  47961. + int error = 0;
  47962. + unsigned ext;
  47963. + unsigned total_size;
  47964. + struct sadb_ext *pfkey_ext;
  47965. + int extensions_seen = 0;
  47966. +#ifndef __KERNEL__
  47967. + struct sadb_ext *extensions_check[SADB_EXT_MAX + 1];
  47968. +#endif
  47969. +
  47970. + if(!extensions[0]) {
  47971. + ERROR("pfkey_msg_build: "
  47972. + "extensions[0] must be specified (struct sadb_msg).\n");
  47973. + SENDERR(EINVAL);
  47974. + }
  47975. +
  47976. + /* figure out the total size for all the requested extensions */
  47977. + total_size = IPSEC_PFKEYv2_WORDS(sizeof(struct sadb_msg));
  47978. + for(ext = 1; ext <= SADB_EXT_MAX; ext++) {
  47979. + if(extensions[ext]) {
  47980. + total_size += (extensions[ext])->sadb_ext_len;
  47981. + }
  47982. + }
  47983. +
  47984. + /* allocate that much space */
  47985. + *pfkey_msg = (struct sadb_msg*)MALLOC(total_size * IPSEC_PFKEYv2_ALIGN);
  47986. + if(*pfkey_msg == NULL) {
  47987. + ERROR("pfkey_msg_build: "
  47988. + "memory allocation failed\n");
  47989. + SENDERR(ENOMEM);
  47990. + }
  47991. +
  47992. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47993. + "pfkey_msg_build: "
  47994. + "pfkey_msg=0p%p allocated %lu bytes, &(extensions[0])=0p%p\n",
  47995. + *pfkey_msg,
  47996. + (unsigned long)(total_size * IPSEC_PFKEYv2_ALIGN),
  47997. + &(extensions[0]));
  47998. +
  47999. + memcpy(*pfkey_msg,
  48000. + extensions[0],
  48001. + sizeof(struct sadb_msg));
  48002. + (*pfkey_msg)->sadb_msg_len = total_size;
  48003. + (*pfkey_msg)->sadb_msg_reserved = 0;
  48004. + extensions_seen = 1 ;
  48005. +
  48006. + /*
  48007. + * point pfkey_ext to immediately after the space for the header,
  48008. + * i.e. at the first extension location.
  48009. + */
  48010. + pfkey_ext = (struct sadb_ext*)(((char*)(*pfkey_msg)) + sizeof(struct sadb_msg));
  48011. +
  48012. + for(ext = 1; ext <= SADB_EXT_MAX; ext++) {
  48013. + /* copy from extension[ext] to buffer */
  48014. + if(extensions[ext]) {
  48015. + /* Is this type of extension permitted for this type of message? */
  48016. + if(!(extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type] &
  48017. + 1<<ext)) {
  48018. + ERROR("pfkey_msg_build: "
  48019. + "ext type %d not permitted, exts_perm=%08x, 1<<type=%08x\n",
  48020. + ext,
  48021. + extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
  48022. + 1<<ext);
  48023. + SENDERR(EINVAL);
  48024. + }
  48025. +
  48026. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48027. + "pfkey_msg_build: "
  48028. + "copying %lu bytes from extensions[%u] (type=%d)\n",
  48029. + (unsigned long)(extensions[ext]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN),
  48030. + ext,
  48031. + extensions[ext]->sadb_ext_type);
  48032. +
  48033. + memcpy(pfkey_ext,
  48034. + extensions[ext],
  48035. + (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  48036. + {
  48037. + char *pfkey_ext_c = (char *)pfkey_ext;
  48038. +
  48039. + pfkey_ext_c += (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN;
  48040. + pfkey_ext = (struct sadb_ext *)pfkey_ext_c;
  48041. + }
  48042. +
  48043. + /* Mark that we have seen this extension and remember the header location */
  48044. + extensions_seen |= ( 1 << ext );
  48045. + }
  48046. + }
  48047. +
  48048. + /* check required extensions */
  48049. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48050. + "pfkey_msg_build: "
  48051. + "extensions permitted=%08x, seen=%08x, required=%08x.\n",
  48052. + extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
  48053. + extensions_seen,
  48054. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]);
  48055. +
  48056. + if((extensions_seen &
  48057. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) !=
  48058. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) {
  48059. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48060. + "pfkey_msg_build: "
  48061. + "required extensions missing:%08x.\n",
  48062. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type] -
  48063. + (extensions_seen &
  48064. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) );
  48065. + SENDERR(EINVAL);
  48066. + }
  48067. +
  48068. +#ifndef __KERNEL__
  48069. +/*
  48070. + * this is silly, there is no need to reparse the message that we just built.
  48071. + *
  48072. + */
  48073. + if((error = pfkey_msg_parse(*pfkey_msg, NULL, extensions_check, dir))) {
  48074. + ERROR(
  48075. + "pfkey_msg_build: "
  48076. + "Trouble parsing newly built pfkey message, error=%d.\n",
  48077. + error);
  48078. + SENDERR(-error);
  48079. + }
  48080. +#endif
  48081. +
  48082. +errlab:
  48083. +
  48084. + return error;
  48085. +}
  48086. +
  48087. +/*
  48088. + * $Log: pfkey_v2_build.c,v $
  48089. + * Revision 1.51.8.1 2006/05/01 14:36:39 mcr
  48090. + * get rid of dead code.
  48091. + *
  48092. + * Revision 1.51 2004/10/03 01:26:36 mcr
  48093. + * fixes for gcc 3.4 compilation.
  48094. + *
  48095. + * Revision 1.50 2004/07/10 07:48:35 mcr
  48096. + * Moved from linux/lib/libfreeswan/pfkey_v2_build.c,v
  48097. + *
  48098. + * Revision 1.49 2004/04/12 02:59:06 mcr
  48099. + * erroneously moved pfkey_v2_build.c
  48100. + *
  48101. + * Revision 1.48 2004/04/09 18:00:40 mcr
  48102. + * Moved from linux/lib/libfreeswan/pfkey_v2_build.c,v
  48103. + *
  48104. + * Revision 1.47 2004/03/08 01:59:08 ken
  48105. + * freeswan.h -> openswan.h
  48106. + *
  48107. + * Revision 1.46 2003/12/10 01:20:19 mcr
  48108. + * NAT-traversal patches to KLIPS.
  48109. + *
  48110. + * Revision 1.45 2003/12/04 23:01:12 mcr
  48111. + * removed ipsec_netlink.h
  48112. + *
  48113. + * Revision 1.44 2003/10/31 02:27:12 mcr
  48114. + * pulled up port-selector patches and sa_id elimination.
  48115. + *
  48116. + * Revision 1.43.4.2 2003/10/29 01:11:32 mcr
  48117. + * added debugging for pfkey library.
  48118. + *
  48119. + * Revision 1.43.4.1 2003/09/21 13:59:44 mcr
  48120. + * pre-liminary X.509 patch - does not yet pass tests.
  48121. + *
  48122. + * Revision 1.43 2003/05/07 17:29:17 mcr
  48123. + * new function pfkey_debug_func added for us in debugging from
  48124. + * pfkey library.
  48125. + *
  48126. + * Revision 1.42 2003/01/30 02:32:09 rgb
  48127. + *
  48128. + * Rename SAref table macro names for clarity.
  48129. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  48130. + *
  48131. + * Revision 1.41 2002/12/13 18:16:02 mcr
  48132. + * restored sa_ref code
  48133. + *
  48134. + * Revision 1.40 2002/12/13 18:06:52 mcr
  48135. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  48136. + *
  48137. + * Revision 1.39 2002/12/13 17:43:28 mcr
  48138. + * commented out access to sadb_x_sa_ref for 2.xx branch
  48139. + *
  48140. + * Revision 1.38 2002/10/09 03:12:05 dhr
  48141. + *
  48142. + * [kenb+dhr] 64-bit fixes
  48143. + *
  48144. + * Revision 1.37 2002/09/20 15:40:39 rgb
  48145. + * Added new function pfkey_sa_ref_build() to accomodate saref parameter.
  48146. + *
  48147. + * Revision 1.36 2002/09/20 05:01:22 rgb
  48148. + * Generalise for platform independance: fix (ia64) using unsigned for sizes.
  48149. + *
  48150. + * Revision 1.35 2002/07/24 18:44:54 rgb
  48151. + * Type fiddling to tame ia64 compiler.
  48152. + *
  48153. + * Revision 1.34 2002/05/23 07:14:11 rgb
  48154. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  48155. + *
  48156. + * Revision 1.33 2002/04/24 07:55:32 mcr
  48157. + * #include patches and Makefiles for post-reorg compilation.
  48158. + *
  48159. + * Revision 1.32 2002/04/24 07:36:40 mcr
  48160. + * Moved from ./lib/pfkey_v2_build.c,v
  48161. + *
  48162. + * Revision 1.31 2002/01/29 22:25:35 rgb
  48163. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  48164. + *
  48165. + * Revision 1.30 2002/01/29 01:59:09 mcr
  48166. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  48167. + * updating of IPv6 structures to match latest in6.h version.
  48168. + * removed dead code from openswan.h that also duplicated kversions.h
  48169. + * code.
  48170. + *
  48171. + * Revision 1.29 2001/12/19 21:06:09 rgb
  48172. + * Added port numbers to pfkey_address_build() debugging.
  48173. + *
  48174. + * Revision 1.28 2001/11/06 19:47:47 rgb
  48175. + * Added packet parameter to lifetime and comb structures.
  48176. + *
  48177. + * Revision 1.27 2001/10/18 04:45:24 rgb
  48178. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  48179. + * lib/openswan.h version macros moved to lib/kversions.h.
  48180. + * Other compiler directive cleanups.
  48181. + *
  48182. + * Revision 1.26 2001/09/08 21:13:34 rgb
  48183. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  48184. + *
  48185. + * Revision 1.25 2001/06/14 19:35:16 rgb
  48186. + * Update copyright date.
  48187. + *
  48188. + * Revision 1.24 2001/03/20 03:49:45 rgb
  48189. + * Ditch superfluous debug_pfkey declaration.
  48190. + * Move misplaced openswan.h inclusion for kernel case.
  48191. + *
  48192. + * Revision 1.23 2001/03/16 07:41:50 rgb
  48193. + * Put openswan.h include before pluto includes.
  48194. + *
  48195. + * Revision 1.22 2001/02/27 22:24:56 rgb
  48196. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  48197. + * Check for satoa() return codes.
  48198. + *
  48199. + * Revision 1.21 2000/11/17 18:10:30 rgb
  48200. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  48201. + * network byte order since this is the way PF_KEYv2 stored spis.
  48202. + *
  48203. + * Revision 1.20 2000/10/12 00:02:39 rgb
  48204. + * Removed 'format, ##' nonsense from debug macros for RH7.0.
  48205. + *
  48206. + * Revision 1.19 2000/10/10 20:10:20 rgb
  48207. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  48208. + *
  48209. + * Revision 1.18 2000/09/12 18:59:54 rgb
  48210. + * Added Gerhard's IPv6 support to pfkey parts of libopenswan.
  48211. + *
  48212. + * Revision 1.17 2000/09/12 03:27:00 rgb
  48213. + * Moved DEBUGGING definition to compile kernel with debug off.
  48214. + *
  48215. + * Revision 1.16 2000/09/08 19:22:12 rgb
  48216. + * Fixed pfkey_prop_build() parameter to be only single indirection.
  48217. + * Fixed struct alg copy.
  48218. + *
  48219. + * Revision 1.15 2000/08/20 21:40:01 rgb
  48220. + * Added an address parameter sanity check to pfkey_address_build().
  48221. + *
  48222. + * Revision 1.14 2000/08/15 17:29:23 rgb
  48223. + * Fixes from SZI to untested pfkey_prop_build().
  48224. + *
  48225. + * Revision 1.13 2000/06/02 22:54:14 rgb
  48226. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  48227. + *
  48228. + * Revision 1.12 2000/05/10 19:24:01 rgb
  48229. + * Fleshed out sensitivity, proposal and supported extensions.
  48230. + *
  48231. + * Revision 1.11 2000/03/16 14:07:23 rgb
  48232. + * Renamed ALIGN macro to avoid fighting with others in kernel.
  48233. + *
  48234. + * Revision 1.10 2000/01/24 21:14:35 rgb
  48235. + * Added disabled pluto pfkey lib debug flag.
  48236. + *
  48237. + * Revision 1.9 2000/01/21 06:27:32 rgb
  48238. + * Added address cases for eroute flows.
  48239. + * Removed unused code.
  48240. + * Dropped unused argument to pfkey_x_satype_build().
  48241. + * Indented compiler directives for readability.
  48242. + * Added klipsdebug switching capability.
  48243. + * Fixed SADB_EXT_MAX bug not permitting last extension access.
  48244. + *
  48245. + * Revision 1.8 1999/12/29 21:17:41 rgb
  48246. + * Changed pfkey_msg_build() I/F to include a struct sadb_msg**
  48247. + * parameter for cleaner manipulation of extensions[] and to guard
  48248. + * against potential memory leaks.
  48249. + * Changed the I/F to pfkey_msg_free() for the same reason.
  48250. + *
  48251. + * Revision 1.7 1999/12/09 23:12:20 rgb
  48252. + * Removed unused cruft.
  48253. + * Added argument to pfkey_sa_build() to do eroutes.
  48254. + * Fixed exttype check in as yet unused pfkey_lifetime_build().
  48255. + *
  48256. + * Revision 1.6 1999/12/07 19:54:29 rgb
  48257. + * Removed static pluto debug flag.
  48258. + * Added functions for pfkey message and extensions initialisation
  48259. + * and cleanup.
  48260. + *
  48261. + * Revision 1.5 1999/12/01 22:20:06 rgb
  48262. + * Changed pfkey_sa_build to accept an SPI in network byte order.
  48263. + * Added <string.h> to quiet userspace compiler.
  48264. + * Moved pfkey_lib_debug variable into the library.
  48265. + * Removed SATYPE check from pfkey_msg_hdr_build so FLUSH will work.
  48266. + * Added extension assembly debugging.
  48267. + * Isolated assignment with brackets to be sure of scope.
  48268. + *
  48269. + * Revision 1.4 1999/11/27 11:57:35 rgb
  48270. + * Added ipv6 headers.
  48271. + * Remove over-zealous algorithm sanity checkers from pfkey_sa_build.
  48272. + * Debugging error messages added.
  48273. + * Fixed missing auth and encrypt assignment bug.
  48274. + * Add argument to pfkey_msg_parse() for direction.
  48275. + * Move parse-after-build check inside pfkey_msg_build().
  48276. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  48277. + * Add CVS log entry to bottom of file.
  48278. + *
  48279. + */
  48280. --- /dev/null Tue Mar 11 13:02:56 2003
  48281. +++ linux/net/ipsec/pfkey_v2_debug.c Mon Feb 9 13:51:03 2004
  48282. @@ -0,0 +1,181 @@
  48283. +/*
  48284. + * @(#) pfkey version 2 debugging messages
  48285. + *
  48286. + * Copyright (C) 2001 Richard Guy Briggs <rgb@openswan.org>
  48287. + * and Michael Richardson <mcr@openswan.org>
  48288. + *
  48289. + * This program is free software; you can redistribute it and/or modify it
  48290. + * under the terms of the GNU General Public License as published by the
  48291. + * Free Software Foundation; either version 2 of the License, or (at your
  48292. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  48293. + *
  48294. + * This program is distributed in the hope that it will be useful, but
  48295. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  48296. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  48297. + * for more details.
  48298. + *
  48299. + * RCSID $Id: pfkey_v2_debug.c,v 1.11 2005/04/06 17:45:16 mcr Exp $
  48300. + *
  48301. + */
  48302. +
  48303. +#ifdef __KERNEL__
  48304. +
  48305. +# include <linux/kernel.h> /* for printk */
  48306. +
  48307. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  48308. +# ifdef MALLOC_SLAB
  48309. +# include <linux/slab.h> /* kmalloc() */
  48310. +# else /* MALLOC_SLAB */
  48311. +# include <linux/malloc.h> /* kmalloc() */
  48312. +# endif /* MALLOC_SLAB */
  48313. +# include <linux/errno.h> /* error codes */
  48314. +# include <linux/types.h> /* size_t */
  48315. +# include <linux/interrupt.h> /* mark_bh */
  48316. +
  48317. +# include <linux/netdevice.h> /* struct device, and other headers */
  48318. +# include <linux/etherdevice.h> /* eth_type_trans */
  48319. +extern int debug_pfkey;
  48320. +
  48321. +#else /* __KERNEL__ */
  48322. +
  48323. +# include <sys/types.h>
  48324. +# include <linux/types.h>
  48325. +# include <linux/errno.h>
  48326. +
  48327. +#endif /* __KERNEL__ */
  48328. +
  48329. +#include "openswan.h"
  48330. +#include "pfkeyv2.h"
  48331. +#include "pfkey.h"
  48332. +
  48333. +/*
  48334. + * This file provides ASCII translations of PF_KEY magic numbers.
  48335. + *
  48336. + */
  48337. +
  48338. +static char *pfkey_sadb_ext_strings[]={
  48339. + "reserved", /* SADB_EXT_RESERVED 0 */
  48340. + "security-association", /* SADB_EXT_SA 1 */
  48341. + "lifetime-current", /* SADB_EXT_LIFETIME_CURRENT 2 */
  48342. + "lifetime-hard", /* SADB_EXT_LIFETIME_HARD 3 */
  48343. + "lifetime-soft", /* SADB_EXT_LIFETIME_SOFT 4 */
  48344. + "source-address", /* SADB_EXT_ADDRESS_SRC 5 */
  48345. + "destination-address", /* SADB_EXT_ADDRESS_DST 6 */
  48346. + "proxy-address", /* SADB_EXT_ADDRESS_PROXY 7 */
  48347. + "authentication-key", /* SADB_EXT_KEY_AUTH 8 */
  48348. + "cipher-key", /* SADB_EXT_KEY_ENCRYPT 9 */
  48349. + "source-identity", /* SADB_EXT_IDENTITY_SRC 10 */
  48350. + "destination-identity", /* SADB_EXT_IDENTITY_DST 11 */
  48351. + "sensitivity-label", /* SADB_EXT_SENSITIVITY 12 */
  48352. + "proposal", /* SADB_EXT_PROPOSAL 13 */
  48353. + "supported-auth", /* SADB_EXT_SUPPORTED_AUTH 14 */
  48354. + "supported-cipher", /* SADB_EXT_SUPPORTED_ENCRYPT 15 */
  48355. + "spi-range", /* SADB_EXT_SPIRANGE 16 */
  48356. + "X-kmpprivate", /* SADB_X_EXT_KMPRIVATE 17 */
  48357. + "X-satype2", /* SADB_X_EXT_SATYPE2 18 */
  48358. + "X-security-association", /* SADB_X_EXT_SA2 19 */
  48359. + "X-destination-address2", /* SADB_X_EXT_ADDRESS_DST2 20 */
  48360. + "X-source-flow-address", /* SADB_X_EXT_ADDRESS_SRC_FLOW 21 */
  48361. + "X-dest-flow-address", /* SADB_X_EXT_ADDRESS_DST_FLOW 22 */
  48362. + "X-source-mask", /* SADB_X_EXT_ADDRESS_SRC_MASK 23 */
  48363. + "X-dest-mask", /* SADB_X_EXT_ADDRESS_DST_MASK 24 */
  48364. + "X-set-debug", /* SADB_X_EXT_DEBUG 25 */
  48365. + /* NAT_TRAVERSAL */
  48366. + "X-NAT-T-type", /* SADB_X_EXT_NAT_T_TYPE 26 */
  48367. + "X-NAT-T-sport", /* SADB_X_EXT_NAT_T_SPORT 27 */
  48368. + "X-NAT-T-dport", /* SADB_X_EXT_NAT_T_DPORT 28 */
  48369. + "X-NAT-T-OA", /* SADB_X_EXT_NAT_T_OA 29 */
  48370. +};
  48371. +
  48372. +const char *
  48373. +pfkey_v2_sadb_ext_string(int ext)
  48374. +{
  48375. + if(ext <= SADB_EXT_MAX) {
  48376. + return pfkey_sadb_ext_strings[ext];
  48377. + } else {
  48378. + return "unknown-ext";
  48379. + }
  48380. +}
  48381. +
  48382. +
  48383. +static char *pfkey_sadb_type_strings[]={
  48384. + "reserved", /* SADB_RESERVED */
  48385. + "getspi", /* SADB_GETSPI */
  48386. + "update", /* SADB_UPDATE */
  48387. + "add", /* SADB_ADD */
  48388. + "delete", /* SADB_DELETE */
  48389. + "get", /* SADB_GET */
  48390. + "acquire", /* SADB_ACQUIRE */
  48391. + "register", /* SADB_REGISTER */
  48392. + "expire", /* SADB_EXPIRE */
  48393. + "flush", /* SADB_FLUSH */
  48394. + "dump", /* SADB_DUMP */
  48395. + "x-promisc", /* SADB_X_PROMISC */
  48396. + "x-pchange", /* SADB_X_PCHANGE */
  48397. + "x-groupsa", /* SADB_X_GRPSA */
  48398. + "x-addflow(eroute)", /* SADB_X_ADDFLOW */
  48399. + "x-delflow(eroute)", /* SADB_X_DELFLOW */
  48400. + "x-debug", /* SADB_X_DEBUG */
  48401. +};
  48402. +
  48403. +const char *
  48404. +pfkey_v2_sadb_type_string(int sadb_type)
  48405. +{
  48406. + if(sadb_type <= SADB_MAX) {
  48407. + return pfkey_sadb_type_strings[sadb_type];
  48408. + } else {
  48409. + return "unknown-sadb-type";
  48410. + }
  48411. +}
  48412. +
  48413. +
  48414. +
  48415. +
  48416. +/*
  48417. + * $Log: pfkey_v2_debug.c,v $
  48418. + * Revision 1.11 2005/04/06 17:45:16 mcr
  48419. + * always include NAT-T names.
  48420. + *
  48421. + * Revision 1.10 2004/07/10 07:48:35 mcr
  48422. + * Moved from linux/lib/libfreeswan/pfkey_v2_debug.c,v
  48423. + *
  48424. + * Revision 1.9 2004/03/08 01:59:08 ken
  48425. + * freeswan.h -> openswan.h
  48426. + *
  48427. + * Revision 1.8 2003/12/10 01:20:19 mcr
  48428. + * NAT-traversal patches to KLIPS.
  48429. + *
  48430. + * Revision 1.7 2002/09/20 05:01:26 rgb
  48431. + * Fixed limit inclusion error in both type and ext string conversion.
  48432. + *
  48433. + * Revision 1.6 2002/04/24 07:55:32 mcr
  48434. + * #include patches and Makefiles for post-reorg compilation.
  48435. + *
  48436. + * Revision 1.5 2002/04/24 07:36:40 mcr
  48437. + * Moved from ./lib/pfkey_v2_debug.c,v
  48438. + *
  48439. + * Revision 1.4 2002/01/29 22:25:36 rgb
  48440. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  48441. + *
  48442. + * Revision 1.3 2002/01/29 01:59:09 mcr
  48443. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  48444. + * updating of IPv6 structures to match latest in6.h version.
  48445. + * removed dead code from openswan.h that also duplicated kversions.h
  48446. + * code.
  48447. + *
  48448. + * Revision 1.2 2002/01/20 20:34:50 mcr
  48449. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  48450. + *
  48451. + * Revision 1.1 2001/11/27 05:30:06 mcr
  48452. + * initial set of debug strings for pfkey debugging.
  48453. + * this will eventually only be included for debug builds.
  48454. + *
  48455. + * Revision 1.1 2001/09/21 04:12:03 mcr
  48456. + * first compilable version.
  48457. + *
  48458. + *
  48459. + * Local variables:
  48460. + * c-file-style: "linux"
  48461. + * End:
  48462. + *
  48463. + */
  48464. --- /dev/null Tue Mar 11 13:02:56 2003
  48465. +++ linux/net/ipsec/pfkey_v2_ext_bits.c Mon Feb 9 13:51:03 2004
  48466. @@ -0,0 +1,814 @@
  48467. +/*
  48468. + * RFC2367 PF_KEYv2 Key management API message parser
  48469. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  48470. + *
  48471. + * This program is free software; you can redistribute it and/or modify it
  48472. + * under the terms of the GNU General Public License as published by the
  48473. + * Free Software Foundation; either version 2 of the License, or (at your
  48474. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  48475. + *
  48476. + * This program is distributed in the hope that it will be useful, but
  48477. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  48478. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  48479. + * for more details.
  48480. + *
  48481. + * RCSID $Id: pfkey_v2_ext_bits.c,v 1.22 2005/05/11 01:45:31 mcr Exp $
  48482. + */
  48483. +
  48484. +/*
  48485. + * Template from klips/net/ipsec/ipsec/ipsec_parse.c.
  48486. + */
  48487. +
  48488. +char pfkey_v2_ext_bits_c_version[] = "$Id: pfkey_v2_ext_bits.c,v 1.22 2005/05/11 01:45:31 mcr Exp $";
  48489. +
  48490. +/*
  48491. + * Some ugly stuff to allow consistent debugging code for use in the
  48492. + * kernel and in user space
  48493. +*/
  48494. +
  48495. +#ifdef __KERNEL__
  48496. +
  48497. +# include <linux/kernel.h> /* for printk */
  48498. +
  48499. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  48500. +# ifdef MALLOC_SLAB
  48501. +# include <linux/slab.h> /* kmalloc() */
  48502. +# else /* MALLOC_SLAB */
  48503. +# include <linux/malloc.h> /* kmalloc() */
  48504. +# endif /* MALLOC_SLAB */
  48505. +# include <linux/errno.h> /* error codes */
  48506. +# include <linux/types.h> /* size_t */
  48507. +# include <linux/interrupt.h> /* mark_bh */
  48508. +
  48509. +# include <linux/netdevice.h> /* struct device, and other headers */
  48510. +# include <linux/etherdevice.h> /* eth_type_trans */
  48511. +# include <linux/ip.h> /* struct iphdr */
  48512. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  48513. +# include <linux/ipv6.h>
  48514. +# endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  48515. +
  48516. +#else /* __KERNEL__ */
  48517. +
  48518. +# include <sys/types.h>
  48519. +# include <linux/types.h>
  48520. +# include <linux/errno.h>
  48521. +#endif
  48522. +
  48523. +#include <openswan.h>
  48524. +#include <pfkeyv2.h>
  48525. +#include <pfkey.h>
  48526. +
  48527. +unsigned int extensions_bitmaps[2/*in/out*/][2/*perm/req*/][SADB_EXTENSIONS_MAX] = {
  48528. +
  48529. +/* INBOUND EXTENSIONS */
  48530. +{
  48531. +
  48532. +/* PERMITTED IN */
  48533. +{
  48534. +/* SADB_RESERVED */
  48535. +0
  48536. +,
  48537. +/* SADB_GETSPI */
  48538. +1<<SADB_EXT_RESERVED
  48539. +| 1<<SADB_EXT_ADDRESS_SRC
  48540. +| 1<<SADB_EXT_ADDRESS_DST
  48541. +| 1<<SADB_EXT_ADDRESS_PROXY
  48542. +| 1<<SADB_EXT_SPIRANGE
  48543. +,
  48544. +/* SADB_UPDATE */
  48545. +1<<SADB_EXT_RESERVED
  48546. +| 1<<SADB_EXT_SA
  48547. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48548. +| 1<<SADB_EXT_LIFETIME_HARD
  48549. +| 1<<SADB_EXT_LIFETIME_SOFT
  48550. +| 1<<SADB_EXT_ADDRESS_SRC
  48551. +| 1<<SADB_EXT_ADDRESS_DST
  48552. +| 1<<SADB_EXT_ADDRESS_PROXY
  48553. +| 1<<SADB_EXT_KEY_AUTH
  48554. +| 1<<SADB_EXT_KEY_ENCRYPT
  48555. +| 1<<SADB_EXT_IDENTITY_SRC
  48556. +| 1<<SADB_EXT_IDENTITY_DST
  48557. +| 1<<SADB_EXT_SENSITIVITY
  48558. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48559. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48560. +,
  48561. +/* SADB_ADD */
  48562. +1<<SADB_EXT_RESERVED
  48563. +| 1<<SADB_EXT_SA
  48564. +| 1<<SADB_EXT_LIFETIME_HARD
  48565. +| 1<<SADB_EXT_LIFETIME_SOFT
  48566. +| 1<<SADB_EXT_ADDRESS_SRC
  48567. +| 1<<SADB_EXT_ADDRESS_DST
  48568. +| 1<<SADB_EXT_ADDRESS_PROXY
  48569. +| 1<<SADB_EXT_KEY_AUTH
  48570. +| 1<<SADB_EXT_KEY_ENCRYPT
  48571. +| 1<<SADB_EXT_IDENTITY_SRC
  48572. +| 1<<SADB_EXT_IDENTITY_DST
  48573. +| 1<<SADB_EXT_SENSITIVITY
  48574. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48575. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48576. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48577. +| 1<<SADB_X_EXT_NAT_T_OA
  48578. +,
  48579. +/* SADB_DELETE */
  48580. +1<<SADB_EXT_RESERVED
  48581. +| 1<<SADB_EXT_SA
  48582. +| 1<<SADB_EXT_ADDRESS_SRC
  48583. +| 1<<SADB_EXT_ADDRESS_DST
  48584. +,
  48585. +/* SADB_GET */
  48586. +1<<SADB_EXT_RESERVED
  48587. +| 1<<SADB_EXT_SA
  48588. +| 1<<SADB_EXT_ADDRESS_SRC
  48589. +| 1<<SADB_EXT_ADDRESS_DST
  48590. +,
  48591. +/* SADB_ACQUIRE */
  48592. +1<<SADB_EXT_RESERVED
  48593. +| 1<<SADB_EXT_ADDRESS_SRC
  48594. +| 1<<SADB_EXT_ADDRESS_DST
  48595. +| 1<<SADB_EXT_ADDRESS_PROXY
  48596. +| 1<<SADB_EXT_IDENTITY_SRC
  48597. +| 1<<SADB_EXT_IDENTITY_DST
  48598. +| 1<<SADB_EXT_SENSITIVITY
  48599. +| 1<<SADB_EXT_PROPOSAL
  48600. +,
  48601. +/* SADB_REGISTER */
  48602. +1<<SADB_EXT_RESERVED
  48603. +,
  48604. +/* SADB_EXPIRE */
  48605. +0
  48606. +,
  48607. +/* SADB_FLUSH */
  48608. +1<<SADB_EXT_RESERVED
  48609. +,
  48610. +/* SADB_DUMP */
  48611. +1<<SADB_EXT_RESERVED
  48612. +,
  48613. +/* SADB_X_PROMISC */
  48614. +1<<SADB_EXT_RESERVED
  48615. +| 1<<SADB_EXT_SA
  48616. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48617. +| 1<<SADB_EXT_LIFETIME_HARD
  48618. +| 1<<SADB_EXT_LIFETIME_SOFT
  48619. +| 1<<SADB_EXT_ADDRESS_SRC
  48620. +| 1<<SADB_EXT_ADDRESS_DST
  48621. +| 1<<SADB_EXT_ADDRESS_PROXY
  48622. +| 1<<SADB_EXT_KEY_AUTH
  48623. +| 1<<SADB_EXT_KEY_ENCRYPT
  48624. +| 1<<SADB_EXT_IDENTITY_SRC
  48625. +| 1<<SADB_EXT_IDENTITY_DST
  48626. +| 1<<SADB_EXT_SENSITIVITY
  48627. +| 1<<SADB_EXT_PROPOSAL
  48628. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48629. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48630. +| 1<<SADB_EXT_SPIRANGE
  48631. +| 1<<SADB_X_EXT_KMPRIVATE
  48632. +| 1<<SADB_X_EXT_SATYPE2
  48633. +| 1<<SADB_X_EXT_SA2
  48634. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48635. +,
  48636. +/* SADB_X_PCHANGE */
  48637. +1<<SADB_EXT_RESERVED
  48638. +| 1<<SADB_EXT_SA
  48639. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48640. +| 1<<SADB_EXT_LIFETIME_HARD
  48641. +| 1<<SADB_EXT_LIFETIME_SOFT
  48642. +| 1<<SADB_EXT_ADDRESS_SRC
  48643. +| 1<<SADB_EXT_ADDRESS_DST
  48644. +| 1<<SADB_EXT_ADDRESS_PROXY
  48645. +| 1<<SADB_EXT_KEY_AUTH
  48646. +| 1<<SADB_EXT_KEY_ENCRYPT
  48647. +| 1<<SADB_EXT_IDENTITY_SRC
  48648. +| 1<<SADB_EXT_IDENTITY_DST
  48649. +| 1<<SADB_EXT_SENSITIVITY
  48650. +| 1<<SADB_EXT_PROPOSAL
  48651. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48652. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48653. +| 1<<SADB_EXT_SPIRANGE
  48654. +| 1<<SADB_X_EXT_KMPRIVATE
  48655. +| 1<<SADB_X_EXT_SATYPE2
  48656. +| 1<<SADB_X_EXT_SA2
  48657. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48658. +,
  48659. +/* SADB_X_GRPSA */
  48660. +1<<SADB_EXT_RESERVED
  48661. +| 1<<SADB_EXT_SA
  48662. +| 1<<SADB_EXT_ADDRESS_DST
  48663. +| 1<<SADB_X_EXT_SATYPE2
  48664. +| 1<<SADB_X_EXT_SA2
  48665. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48666. +,
  48667. +/* SADB_X_ADDFLOW */
  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_X_EXT_ADDRESS_SRC_FLOW
  48673. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48674. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48675. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48676. +| 1<<SADB_EXT_IDENTITY_SRC
  48677. +| 1<<SADB_EXT_IDENTITY_DST
  48678. +| 1<<SADB_X_EXT_PROTOCOL
  48679. +,
  48680. +/* SADB_X_DELFLOW */
  48681. +1<<SADB_EXT_RESERVED
  48682. +| 1<<SADB_EXT_SA
  48683. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48684. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48685. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48686. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48687. +| 1<<SADB_EXT_IDENTITY_SRC
  48688. +| 1<<SADB_EXT_IDENTITY_DST
  48689. +| 1<<SADB_X_EXT_PROTOCOL
  48690. +,
  48691. +/* SADB_X_DEBUG */
  48692. +1<<SADB_EXT_RESERVED
  48693. +| 1<<SADB_X_EXT_DEBUG
  48694. +,
  48695. +/* SADB_X_NAT_T_NEW_MAPPING */
  48696. +1<<SADB_EXT_RESERVED
  48697. +| 1<<SADB_EXT_SA
  48698. +| 1<<SADB_EXT_ADDRESS_SRC
  48699. +| 1<<SADB_EXT_ADDRESS_DST
  48700. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48701. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48702. +},
  48703. +
  48704. +/* REQUIRED IN */
  48705. +{
  48706. +/* SADB_RESERVED */
  48707. +0
  48708. +,
  48709. +/* SADB_GETSPI */
  48710. +1<<SADB_EXT_RESERVED
  48711. +| 1<<SADB_EXT_ADDRESS_SRC
  48712. +| 1<<SADB_EXT_ADDRESS_DST
  48713. +| 1<<SADB_EXT_SPIRANGE
  48714. +,
  48715. +/* SADB_UPDATE */
  48716. +1<<SADB_EXT_RESERVED
  48717. +| 1<<SADB_EXT_SA
  48718. +| 1<<SADB_EXT_ADDRESS_SRC
  48719. +| 1<<SADB_EXT_ADDRESS_DST
  48720. +/*| 1<<SADB_EXT_KEY_AUTH*/
  48721. +/*| 1<<SADB_EXT_KEY_ENCRYPT*/
  48722. +,
  48723. +/* SADB_ADD */
  48724. +1<<SADB_EXT_RESERVED
  48725. +| 1<<SADB_EXT_SA
  48726. +| 1<<SADB_EXT_ADDRESS_SRC
  48727. +| 1<<SADB_EXT_ADDRESS_DST
  48728. +/*| 1<<SADB_EXT_KEY_AUTH*/
  48729. +/*| 1<<SADB_EXT_KEY_ENCRYPT*/
  48730. +,
  48731. +/* SADB_DELETE */
  48732. +1<<SADB_EXT_RESERVED
  48733. +| 1<<SADB_EXT_SA
  48734. +| 1<<SADB_EXT_ADDRESS_SRC
  48735. +| 1<<SADB_EXT_ADDRESS_DST
  48736. +,
  48737. +/* SADB_GET */
  48738. +1<<SADB_EXT_RESERVED
  48739. +| 1<<SADB_EXT_SA
  48740. +| 1<<SADB_EXT_ADDRESS_SRC
  48741. +| 1<<SADB_EXT_ADDRESS_DST
  48742. +,
  48743. +/* SADB_ACQUIRE */
  48744. +1<<SADB_EXT_RESERVED
  48745. +| 1<<SADB_EXT_ADDRESS_SRC
  48746. +| 1<<SADB_EXT_ADDRESS_DST
  48747. +| 1<<SADB_EXT_PROPOSAL
  48748. +,
  48749. +/* SADB_REGISTER */
  48750. +1<<SADB_EXT_RESERVED
  48751. +,
  48752. +/* SADB_EXPIRE */
  48753. +0
  48754. +,
  48755. +/* SADB_FLUSH */
  48756. +1<<SADB_EXT_RESERVED
  48757. +,
  48758. +/* SADB_DUMP */
  48759. +1<<SADB_EXT_RESERVED
  48760. +,
  48761. +/* SADB_X_PROMISC */
  48762. +1<<SADB_EXT_RESERVED
  48763. +| 1<<SADB_EXT_SA
  48764. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48765. +| 1<<SADB_EXT_LIFETIME_HARD
  48766. +| 1<<SADB_EXT_LIFETIME_SOFT
  48767. +| 1<<SADB_EXT_ADDRESS_SRC
  48768. +| 1<<SADB_EXT_ADDRESS_DST
  48769. +| 1<<SADB_EXT_ADDRESS_PROXY
  48770. +| 1<<SADB_EXT_KEY_AUTH
  48771. +| 1<<SADB_EXT_KEY_ENCRYPT
  48772. +| 1<<SADB_EXT_IDENTITY_SRC
  48773. +| 1<<SADB_EXT_IDENTITY_DST
  48774. +| 1<<SADB_EXT_SENSITIVITY
  48775. +| 1<<SADB_EXT_PROPOSAL
  48776. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48777. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48778. +| 1<<SADB_EXT_SPIRANGE
  48779. +| 1<<SADB_X_EXT_KMPRIVATE
  48780. +| 1<<SADB_X_EXT_SATYPE2
  48781. +| 1<<SADB_X_EXT_SA2
  48782. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48783. +,
  48784. +/* SADB_X_PCHANGE */
  48785. +1<<SADB_EXT_RESERVED
  48786. +| 1<<SADB_EXT_SA
  48787. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48788. +| 1<<SADB_EXT_LIFETIME_HARD
  48789. +| 1<<SADB_EXT_LIFETIME_SOFT
  48790. +| 1<<SADB_EXT_ADDRESS_SRC
  48791. +| 1<<SADB_EXT_ADDRESS_DST
  48792. +| 1<<SADB_EXT_ADDRESS_PROXY
  48793. +| 1<<SADB_EXT_KEY_AUTH
  48794. +| 1<<SADB_EXT_KEY_ENCRYPT
  48795. +| 1<<SADB_EXT_IDENTITY_SRC
  48796. +| 1<<SADB_EXT_IDENTITY_DST
  48797. +| 1<<SADB_EXT_SENSITIVITY
  48798. +| 1<<SADB_EXT_PROPOSAL
  48799. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48800. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48801. +| 1<<SADB_EXT_SPIRANGE
  48802. +| 1<<SADB_X_EXT_KMPRIVATE
  48803. +| 1<<SADB_X_EXT_SATYPE2
  48804. +| 1<<SADB_X_EXT_SA2
  48805. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48806. +,
  48807. +/* SADB_X_GRPSA */
  48808. +1<<SADB_EXT_RESERVED
  48809. +| 1<<SADB_EXT_SA
  48810. +| 1<<SADB_EXT_ADDRESS_DST
  48811. +/*| 1<<SADB_X_EXT_SATYPE2*/
  48812. +/*| 1<<SADB_X_EXT_SA2*/
  48813. +/*| 1<<SADB_X_EXT_ADDRESS_DST2*/
  48814. +,
  48815. +/* SADB_X_ADDFLOW */
  48816. +1<<SADB_EXT_RESERVED
  48817. +| 1<<SADB_EXT_SA
  48818. +| 1<<SADB_EXT_ADDRESS_DST
  48819. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48820. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48821. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48822. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48823. +,
  48824. +/* SADB_X_DELFLOW */
  48825. +1<<SADB_EXT_RESERVED
  48826. +/*| 1<<SADB_EXT_SA*/
  48827. +#if 0 /* SADB_X_CLREROUTE doesn't need all these... */
  48828. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48829. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48830. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48831. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48832. +#endif
  48833. +,
  48834. +/* SADB_X_DEBUG */
  48835. +1<<SADB_EXT_RESERVED
  48836. +| 1<<SADB_X_EXT_DEBUG
  48837. +,
  48838. +/* SADB_X_NAT_T_NEW_MAPPING */
  48839. +1<<SADB_EXT_RESERVED
  48840. +| 1<<SADB_EXT_SA
  48841. +| 1<<SADB_EXT_ADDRESS_SRC
  48842. +| 1<<SADB_EXT_ADDRESS_DST
  48843. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48844. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48845. +}
  48846. +
  48847. +},
  48848. +
  48849. +/* OUTBOUND EXTENSIONS */
  48850. +{
  48851. +
  48852. +/* PERMITTED OUT */
  48853. +{
  48854. +/* SADB_RESERVED */
  48855. +0
  48856. +,
  48857. +/* SADB_GETSPI */
  48858. +1<<SADB_EXT_RESERVED
  48859. +| 1<<SADB_EXT_SA
  48860. +| 1<<SADB_EXT_ADDRESS_SRC
  48861. +| 1<<SADB_EXT_ADDRESS_DST
  48862. +,
  48863. +/* SADB_UPDATE */
  48864. +1<<SADB_EXT_RESERVED
  48865. +| 1<<SADB_EXT_SA
  48866. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48867. +| 1<<SADB_EXT_LIFETIME_HARD
  48868. +| 1<<SADB_EXT_LIFETIME_SOFT
  48869. +| 1<<SADB_EXT_ADDRESS_SRC
  48870. +| 1<<SADB_EXT_ADDRESS_DST
  48871. +| 1<<SADB_EXT_ADDRESS_PROXY
  48872. +| 1<<SADB_EXT_IDENTITY_SRC
  48873. +| 1<<SADB_EXT_IDENTITY_DST
  48874. +| 1<<SADB_EXT_SENSITIVITY
  48875. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48876. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48877. +,
  48878. +/* SADB_ADD */
  48879. +1<<SADB_EXT_RESERVED
  48880. +| 1<<SADB_EXT_SA
  48881. +| 1<<SADB_EXT_LIFETIME_HARD
  48882. +| 1<<SADB_EXT_LIFETIME_SOFT
  48883. +| 1<<SADB_EXT_ADDRESS_SRC
  48884. +| 1<<SADB_EXT_ADDRESS_DST
  48885. +| 1<<SADB_EXT_IDENTITY_SRC
  48886. +| 1<<SADB_EXT_IDENTITY_DST
  48887. +| 1<<SADB_EXT_SENSITIVITY
  48888. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48889. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48890. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48891. +| 1<<SADB_X_EXT_NAT_T_OA
  48892. +,
  48893. +/* SADB_DELETE */
  48894. +1<<SADB_EXT_RESERVED
  48895. +| 1<<SADB_EXT_SA
  48896. +| 1<<SADB_EXT_ADDRESS_SRC
  48897. +| 1<<SADB_EXT_ADDRESS_DST
  48898. +,
  48899. +/* SADB_GET */
  48900. +1<<SADB_EXT_RESERVED
  48901. +| 1<<SADB_EXT_SA
  48902. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48903. +| 1<<SADB_EXT_LIFETIME_HARD
  48904. +| 1<<SADB_EXT_LIFETIME_SOFT
  48905. +| 1<<SADB_EXT_ADDRESS_SRC
  48906. +| 1<<SADB_EXT_ADDRESS_DST
  48907. +| 1<<SADB_EXT_ADDRESS_PROXY
  48908. +| 1<<SADB_EXT_KEY_AUTH
  48909. +| 1<<SADB_EXT_KEY_ENCRYPT
  48910. +| 1<<SADB_EXT_IDENTITY_SRC
  48911. +| 1<<SADB_EXT_IDENTITY_DST
  48912. +| 1<<SADB_EXT_SENSITIVITY
  48913. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48914. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48915. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48916. +| 1<<SADB_X_EXT_NAT_T_OA
  48917. +,
  48918. +/* SADB_ACQUIRE */
  48919. +1<<SADB_EXT_RESERVED
  48920. +| 1<<SADB_EXT_ADDRESS_SRC
  48921. +| 1<<SADB_EXT_ADDRESS_DST
  48922. +| 1<<SADB_EXT_ADDRESS_PROXY
  48923. +| 1<<SADB_EXT_IDENTITY_SRC
  48924. +| 1<<SADB_EXT_IDENTITY_DST
  48925. +| 1<<SADB_EXT_SENSITIVITY
  48926. +| 1<<SADB_EXT_PROPOSAL
  48927. +,
  48928. +/* SADB_REGISTER */
  48929. +1<<SADB_EXT_RESERVED
  48930. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48931. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48932. +,
  48933. +/* SADB_EXPIRE */
  48934. +1<<SADB_EXT_RESERVED
  48935. +| 1<<SADB_EXT_SA
  48936. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48937. +| 1<<SADB_EXT_LIFETIME_HARD
  48938. +| 1<<SADB_EXT_LIFETIME_SOFT
  48939. +| 1<<SADB_EXT_ADDRESS_SRC
  48940. +| 1<<SADB_EXT_ADDRESS_DST
  48941. +,
  48942. +/* SADB_FLUSH */
  48943. +1<<SADB_EXT_RESERVED
  48944. +,
  48945. +/* SADB_DUMP */
  48946. +1<<SADB_EXT_RESERVED
  48947. +| 1<<SADB_EXT_SA
  48948. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48949. +| 1<<SADB_EXT_LIFETIME_HARD
  48950. +| 1<<SADB_EXT_LIFETIME_SOFT
  48951. +| 1<<SADB_EXT_ADDRESS_SRC
  48952. +| 1<<SADB_EXT_ADDRESS_DST
  48953. +| 1<<SADB_EXT_ADDRESS_PROXY
  48954. +| 1<<SADB_EXT_KEY_AUTH
  48955. +| 1<<SADB_EXT_KEY_ENCRYPT
  48956. +| 1<<SADB_EXT_IDENTITY_SRC
  48957. +| 1<<SADB_EXT_IDENTITY_DST
  48958. +| 1<<SADB_EXT_SENSITIVITY
  48959. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48960. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48961. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48962. +| 1<<SADB_X_EXT_NAT_T_OA
  48963. +,
  48964. +/* SADB_X_PROMISC */
  48965. +1<<SADB_EXT_RESERVED
  48966. +| 1<<SADB_EXT_SA
  48967. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48968. +| 1<<SADB_EXT_LIFETIME_HARD
  48969. +| 1<<SADB_EXT_LIFETIME_SOFT
  48970. +| 1<<SADB_EXT_ADDRESS_SRC
  48971. +| 1<<SADB_EXT_ADDRESS_DST
  48972. +| 1<<SADB_EXT_ADDRESS_PROXY
  48973. +| 1<<SADB_EXT_KEY_AUTH
  48974. +| 1<<SADB_EXT_KEY_ENCRYPT
  48975. +| 1<<SADB_EXT_IDENTITY_SRC
  48976. +| 1<<SADB_EXT_IDENTITY_DST
  48977. +| 1<<SADB_EXT_SENSITIVITY
  48978. +| 1<<SADB_EXT_PROPOSAL
  48979. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48980. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48981. +| 1<<SADB_EXT_SPIRANGE
  48982. +| 1<<SADB_X_EXT_KMPRIVATE
  48983. +| 1<<SADB_X_EXT_SATYPE2
  48984. +| 1<<SADB_X_EXT_SA2
  48985. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48986. +,
  48987. +/* SADB_X_PCHANGE */
  48988. +1<<SADB_EXT_RESERVED
  48989. +| 1<<SADB_EXT_SA
  48990. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48991. +| 1<<SADB_EXT_LIFETIME_HARD
  48992. +| 1<<SADB_EXT_LIFETIME_SOFT
  48993. +| 1<<SADB_EXT_ADDRESS_SRC
  48994. +| 1<<SADB_EXT_ADDRESS_DST
  48995. +| 1<<SADB_EXT_ADDRESS_PROXY
  48996. +| 1<<SADB_EXT_KEY_AUTH
  48997. +| 1<<SADB_EXT_KEY_ENCRYPT
  48998. +| 1<<SADB_EXT_IDENTITY_SRC
  48999. +| 1<<SADB_EXT_IDENTITY_DST
  49000. +| 1<<SADB_EXT_SENSITIVITY
  49001. +| 1<<SADB_EXT_PROPOSAL
  49002. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49003. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49004. +| 1<<SADB_EXT_SPIRANGE
  49005. +| 1<<SADB_X_EXT_KMPRIVATE
  49006. +| 1<<SADB_X_EXT_SATYPE2
  49007. +| 1<<SADB_X_EXT_SA2
  49008. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49009. +,
  49010. +/* SADB_X_GRPSA */
  49011. +1<<SADB_EXT_RESERVED
  49012. +| 1<<SADB_EXT_SA
  49013. +| 1<<SADB_EXT_ADDRESS_DST
  49014. +| 1<<SADB_X_EXT_SATYPE2
  49015. +| 1<<SADB_X_EXT_SA2
  49016. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49017. +,
  49018. +/* SADB_X_ADDFLOW */
  49019. +1<<SADB_EXT_RESERVED
  49020. +| 1<<SADB_EXT_SA
  49021. +| 1<<SADB_EXT_ADDRESS_SRC
  49022. +| 1<<SADB_EXT_ADDRESS_DST
  49023. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49024. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49025. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49026. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49027. +| 1<<SADB_X_EXT_PROTOCOL
  49028. +,
  49029. +/* SADB_X_DELFLOW */
  49030. +1<<SADB_EXT_RESERVED
  49031. +| 1<<SADB_EXT_SA
  49032. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49033. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49034. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49035. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49036. +| 1<<SADB_X_EXT_PROTOCOL
  49037. +,
  49038. +/* SADB_X_DEBUG */
  49039. +1<<SADB_EXT_RESERVED
  49040. +| 1<<SADB_X_EXT_DEBUG
  49041. +,
  49042. +/* SADB_X_NAT_T_NEW_MAPPING */
  49043. +1<<SADB_EXT_RESERVED
  49044. +| 1<<SADB_EXT_SA
  49045. +| 1<<SADB_EXT_ADDRESS_SRC
  49046. +| 1<<SADB_EXT_ADDRESS_DST
  49047. +| 1<<SADB_X_EXT_NAT_T_SPORT
  49048. +| 1<<SADB_X_EXT_NAT_T_DPORT
  49049. +},
  49050. +
  49051. +/* REQUIRED OUT */
  49052. +{
  49053. +/* SADB_RESERVED */
  49054. +0
  49055. +,
  49056. +/* SADB_GETSPI */
  49057. +1<<SADB_EXT_RESERVED
  49058. +| 1<<SADB_EXT_SA
  49059. +| 1<<SADB_EXT_ADDRESS_SRC
  49060. +| 1<<SADB_EXT_ADDRESS_DST
  49061. +,
  49062. +/* SADB_UPDATE */
  49063. +1<<SADB_EXT_RESERVED
  49064. +| 1<<SADB_EXT_SA
  49065. +| 1<<SADB_EXT_ADDRESS_SRC
  49066. +| 1<<SADB_EXT_ADDRESS_DST
  49067. +,
  49068. +/* SADB_ADD */
  49069. +1<<SADB_EXT_RESERVED
  49070. +| 1<<SADB_EXT_SA
  49071. +| 1<<SADB_EXT_ADDRESS_SRC
  49072. +| 1<<SADB_EXT_ADDRESS_DST
  49073. +,
  49074. +/* SADB_DELETE */
  49075. +1<<SADB_EXT_RESERVED
  49076. +| 1<<SADB_EXT_SA
  49077. +| 1<<SADB_EXT_ADDRESS_SRC
  49078. +| 1<<SADB_EXT_ADDRESS_DST
  49079. +,
  49080. +/* SADB_GET */
  49081. +1<<SADB_EXT_RESERVED
  49082. +| 1<<SADB_EXT_SA
  49083. +| 1<<SADB_EXT_ADDRESS_SRC
  49084. +| 1<<SADB_EXT_ADDRESS_DST
  49085. +/* | 1<<SADB_EXT_KEY_AUTH */
  49086. +/* | 1<<SADB_EXT_KEY_ENCRYPT */
  49087. +,
  49088. +/* SADB_ACQUIRE */
  49089. +1<<SADB_EXT_RESERVED
  49090. +| 1<<SADB_EXT_ADDRESS_SRC
  49091. +| 1<<SADB_EXT_ADDRESS_DST
  49092. +| 1<<SADB_EXT_PROPOSAL
  49093. +,
  49094. +/* SADB_REGISTER */
  49095. +1<<SADB_EXT_RESERVED
  49096. +/* | 1<<SADB_EXT_SUPPORTED_AUTH
  49097. + | 1<<SADB_EXT_SUPPORTED_ENCRYPT */
  49098. +,
  49099. +/* SADB_EXPIRE */
  49100. +1<<SADB_EXT_RESERVED
  49101. +| 1<<SADB_EXT_SA
  49102. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49103. +/* | 1<<SADB_EXT_LIFETIME_HARD
  49104. + | 1<<SADB_EXT_LIFETIME_SOFT */
  49105. +| 1<<SADB_EXT_ADDRESS_SRC
  49106. +| 1<<SADB_EXT_ADDRESS_DST
  49107. +,
  49108. +/* SADB_FLUSH */
  49109. +1<<SADB_EXT_RESERVED
  49110. +,
  49111. +/* SADB_DUMP */
  49112. +1<<SADB_EXT_RESERVED
  49113. +| 1<<SADB_EXT_SA
  49114. +| 1<<SADB_EXT_ADDRESS_SRC
  49115. +| 1<<SADB_EXT_ADDRESS_DST
  49116. +| 1<<SADB_EXT_KEY_AUTH
  49117. +| 1<<SADB_EXT_KEY_ENCRYPT
  49118. +,
  49119. +/* SADB_X_PROMISC */
  49120. +1<<SADB_EXT_RESERVED
  49121. +| 1<<SADB_EXT_SA
  49122. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49123. +| 1<<SADB_EXT_LIFETIME_HARD
  49124. +| 1<<SADB_EXT_LIFETIME_SOFT
  49125. +| 1<<SADB_EXT_ADDRESS_SRC
  49126. +| 1<<SADB_EXT_ADDRESS_DST
  49127. +| 1<<SADB_EXT_ADDRESS_PROXY
  49128. +| 1<<SADB_EXT_KEY_AUTH
  49129. +| 1<<SADB_EXT_KEY_ENCRYPT
  49130. +| 1<<SADB_EXT_IDENTITY_SRC
  49131. +| 1<<SADB_EXT_IDENTITY_DST
  49132. +| 1<<SADB_EXT_SENSITIVITY
  49133. +| 1<<SADB_EXT_PROPOSAL
  49134. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49135. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49136. +| 1<<SADB_EXT_SPIRANGE
  49137. +| 1<<SADB_X_EXT_KMPRIVATE
  49138. +| 1<<SADB_X_EXT_SATYPE2
  49139. +| 1<<SADB_X_EXT_SA2
  49140. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49141. +,
  49142. +/* SADB_X_PCHANGE */
  49143. +1<<SADB_EXT_RESERVED
  49144. +| 1<<SADB_EXT_SA
  49145. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49146. +| 1<<SADB_EXT_LIFETIME_HARD
  49147. +| 1<<SADB_EXT_LIFETIME_SOFT
  49148. +| 1<<SADB_EXT_ADDRESS_SRC
  49149. +| 1<<SADB_EXT_ADDRESS_DST
  49150. +| 1<<SADB_EXT_ADDRESS_PROXY
  49151. +| 1<<SADB_EXT_KEY_AUTH
  49152. +| 1<<SADB_EXT_KEY_ENCRYPT
  49153. +| 1<<SADB_EXT_IDENTITY_SRC
  49154. +| 1<<SADB_EXT_IDENTITY_DST
  49155. +| 1<<SADB_EXT_SENSITIVITY
  49156. +| 1<<SADB_EXT_PROPOSAL
  49157. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49158. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49159. +| 1<<SADB_EXT_SPIRANGE
  49160. +| 1<<SADB_X_EXT_KMPRIVATE
  49161. +| 1<<SADB_X_EXT_SATYPE2
  49162. +| 1<<SADB_X_EXT_SA2
  49163. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49164. +,
  49165. +/* SADB_X_GRPSA */
  49166. +1<<SADB_EXT_RESERVED
  49167. +| 1<<SADB_EXT_SA
  49168. +| 1<<SADB_EXT_ADDRESS_DST
  49169. +,
  49170. +/* SADB_X_ADDFLOW */
  49171. +1<<SADB_EXT_RESERVED
  49172. +| 1<<SADB_EXT_SA
  49173. +| 1<<SADB_EXT_ADDRESS_DST
  49174. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49175. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49176. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49177. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49178. +,
  49179. +/* SADB_X_DELFLOW */
  49180. +1<<SADB_EXT_RESERVED
  49181. +/*| 1<<SADB_EXT_SA*/
  49182. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49183. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49184. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49185. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49186. +,
  49187. +/* SADB_X_DEBUG */
  49188. +1<<SADB_EXT_RESERVED
  49189. +| 1<<SADB_X_EXT_DEBUG
  49190. +,
  49191. +/* SADB_X_NAT_T_NEW_MAPPING */
  49192. +1<<SADB_EXT_RESERVED
  49193. +| 1<<SADB_EXT_SA
  49194. +| 1<<SADB_EXT_ADDRESS_SRC
  49195. +| 1<<SADB_EXT_ADDRESS_DST
  49196. +| 1<<SADB_X_EXT_NAT_T_SPORT
  49197. +| 1<<SADB_X_EXT_NAT_T_DPORT
  49198. +}
  49199. +}
  49200. +};
  49201. +
  49202. +/*
  49203. + * $Log: pfkey_v2_ext_bits.c,v $
  49204. + * Revision 1.22 2005/05/11 01:45:31 mcr
  49205. + * make pfkey.h standalone.
  49206. + *
  49207. + * Revision 1.21 2004/07/10 07:48:36 mcr
  49208. + * Moved from linux/lib/libfreeswan/pfkey_v2_ext_bits.c,v
  49209. + *
  49210. + * Revision 1.20 2004/03/08 01:59:08 ken
  49211. + * freeswan.h -> openswan.h
  49212. + *
  49213. + * Revision 1.19 2003/12/22 21:38:13 mcr
  49214. + * removed extraenous #endif.
  49215. + *
  49216. + * Revision 1.18 2003/12/22 19:34:41 mcr
  49217. + * added 0.6c NAT-T patch.
  49218. + *
  49219. + * Revision 1.17 2003/12/10 01:20:19 mcr
  49220. + * NAT-traversal patches to KLIPS.
  49221. + *
  49222. + * Revision 1.16 2003/10/31 02:27:12 mcr
  49223. + * pulled up port-selector patches and sa_id elimination.
  49224. + *
  49225. + * Revision 1.15.30.1 2003/09/21 13:59:44 mcr
  49226. + * pre-liminary X.509 patch - does not yet pass tests.
  49227. + *
  49228. + * Revision 1.15 2002/04/24 07:55:32 mcr
  49229. + * #include patches and Makefiles for post-reorg compilation.
  49230. + *
  49231. + * Revision 1.14 2002/04/24 07:36:40 mcr
  49232. + * Moved from ./lib/pfkey_v2_ext_bits.c,v
  49233. + *
  49234. + * Revision 1.13 2002/01/29 22:25:36 rgb
  49235. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  49236. + *
  49237. + * Revision 1.12 2002/01/29 01:59:10 mcr
  49238. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  49239. + * updating of IPv6 structures to match latest in6.h version.
  49240. + * removed dead code from openswan.h that also duplicated kversions.h
  49241. + * code.
  49242. + *
  49243. + * Revision 1.11 2001/10/18 04:45:24 rgb
  49244. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  49245. + * lib/openswan.h version macros moved to lib/kversions.h.
  49246. + * Other compiler directive cleanups.
  49247. + *
  49248. + * Revision 1.10 2001/09/08 21:13:35 rgb
  49249. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  49250. + *
  49251. + * Revision 1.9 2001/06/14 19:35:16 rgb
  49252. + * Update copyright date.
  49253. + *
  49254. + * Revision 1.8 2001/03/26 23:07:36 rgb
  49255. + * Remove requirement for auth and enc key from UPDATE.
  49256. + *
  49257. + * Revision 1.7 2000/09/12 22:35:37 rgb
  49258. + * Restructured to remove unused extensions from CLEARFLOW messages.
  49259. + *
  49260. + * Revision 1.6 2000/09/09 06:39:01 rgb
  49261. + * Added comments for clarity.
  49262. + *
  49263. + * Revision 1.5 2000/06/02 22:54:14 rgb
  49264. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  49265. + *
  49266. + * Revision 1.4 2000/01/21 06:27:56 rgb
  49267. + * Added address cases for eroute flows.
  49268. + * Added comments for each message type.
  49269. + * Added klipsdebug switching capability.
  49270. + * Fixed GRPSA bitfields.
  49271. + *
  49272. + * Revision 1.3 1999/12/01 22:20:27 rgb
  49273. + * Remove requirement for a proxy address in an incoming getspi message.
  49274. + *
  49275. + * Revision 1.2 1999/11/27 11:57:06 rgb
  49276. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  49277. + * Add CVS log entry to bottom of file.
  49278. + * Cleaned out unused bits.
  49279. + *
  49280. + */
  49281. --- /dev/null Tue Mar 11 13:02:56 2003
  49282. +++ linux/net/ipsec/pfkey_v2_ext_process.c Mon Feb 9 13:51:03 2004
  49283. @@ -0,0 +1,951 @@
  49284. +/*
  49285. + * @(#) RFC2367 PF_KEYv2 Key management API message parser
  49286. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  49287. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  49288. + *
  49289. + * This program is free software; you can redistribute it and/or modify it
  49290. + * under the terms of the GNU General Public License as published by the
  49291. + * Free Software Foundation; either version 2 of the License, or (at your
  49292. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  49293. + *
  49294. + * This program is distributed in the hope that it will be useful, but
  49295. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  49296. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  49297. + * for more details.
  49298. + *
  49299. + * RCSID $Id: pfkey_v2_ext_process.c,v 1.20.2.2 2006/10/06 21:39:26 paul Exp $
  49300. + */
  49301. +
  49302. +/*
  49303. + * Template from klips/net/ipsec/ipsec/ipsec_netlink.c.
  49304. + */
  49305. +
  49306. +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 $";
  49307. +
  49308. +#ifndef AUTOCONF_INCLUDED
  49309. +#include <linux/config.h>
  49310. +#endif
  49311. +#include <linux/version.h>
  49312. +#include <linux/kernel.h> /* printk() */
  49313. +
  49314. +#include "openswan/ipsec_param.h"
  49315. +
  49316. +#ifdef MALLOC_SLAB
  49317. +# include <linux/slab.h> /* kmalloc() */
  49318. +#else /* MALLOC_SLAB */
  49319. +# include <linux/malloc.h> /* kmalloc() */
  49320. +#endif /* MALLOC_SLAB */
  49321. +#include <linux/errno.h> /* error codes */
  49322. +#include <linux/types.h> /* size_t */
  49323. +#include <linux/interrupt.h> /* mark_bh */
  49324. +
  49325. +#include <linux/netdevice.h> /* struct device, and other headers */
  49326. +#include <linux/etherdevice.h> /* eth_type_trans */
  49327. +#include <linux/ip.h> /* struct iphdr */
  49328. +#include <linux/skbuff.h>
  49329. +
  49330. +#include <openswan.h>
  49331. +
  49332. +#include <crypto/des.h>
  49333. +
  49334. +#ifdef SPINLOCK
  49335. +# ifdef SPINLOCK_23
  49336. +# include <linux/spinlock.h> /* *lock* */
  49337. +# else /* SPINLOCK_23 */
  49338. +# include <asm/spinlock.h> /* *lock* */
  49339. +# endif /* SPINLOCK_23 */
  49340. +#endif /* SPINLOCK */
  49341. +#ifdef NET_21
  49342. +# include <linux/in6.h>
  49343. +# define ip_chk_addr inet_addr_type
  49344. +# define IS_MYADDR RTN_LOCAL
  49345. +#endif
  49346. +
  49347. +#include <net/ip.h>
  49348. +#ifdef NETLINK_SOCK
  49349. +# include <linux/netlink.h>
  49350. +#else
  49351. +# include <net/netlink.h>
  49352. +#endif
  49353. +
  49354. +#include <linux/random.h> /* get_random_bytes() */
  49355. +
  49356. +#include "openswan/radij.h"
  49357. +#include "openswan/ipsec_encap.h"
  49358. +#include "openswan/ipsec_sa.h"
  49359. +
  49360. +#include "openswan/ipsec_radij.h"
  49361. +#include "openswan/ipsec_xform.h"
  49362. +#include "openswan/ipsec_ah.h"
  49363. +#include "openswan/ipsec_esp.h"
  49364. +#include "openswan/ipsec_tunnel.h"
  49365. +#include "openswan/ipsec_rcv.h"
  49366. +#include "openswan/ipcomp.h"
  49367. +
  49368. +#include <pfkeyv2.h>
  49369. +#include <pfkey.h>
  49370. +
  49371. +#include "openswan/ipsec_proto.h"
  49372. +#include "openswan/ipsec_alg.h"
  49373. +
  49374. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  49375. +
  49376. +int
  49377. +pfkey_sa_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49378. +{
  49379. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)pfkey_ext;
  49380. + int error = 0;
  49381. + struct ipsec_sa* ipsp;
  49382. +
  49383. + KLIPS_PRINT(debug_pfkey,
  49384. + "klips_debug:pfkey_sa_process: .\n");
  49385. +
  49386. + if(!extr || !extr->ips) {
  49387. + KLIPS_PRINT(debug_pfkey,
  49388. + "klips_debug:pfkey_sa_process: "
  49389. + "extr or extr->ips is NULL, fatal\n");
  49390. + SENDERR(EINVAL);
  49391. + }
  49392. +
  49393. + switch(pfkey_ext->sadb_ext_type) {
  49394. + case SADB_EXT_SA:
  49395. + ipsp = extr->ips;
  49396. + break;
  49397. + case SADB_X_EXT_SA2:
  49398. + if(extr->ips2 == NULL) {
  49399. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49400. + }
  49401. + if(extr->ips2 == NULL) {
  49402. + SENDERR(-error);
  49403. + }
  49404. + ipsp = extr->ips2;
  49405. + break;
  49406. + default:
  49407. + KLIPS_PRINT(debug_pfkey,
  49408. + "klips_debug:pfkey_sa_process: "
  49409. + "invalid exttype=%d.\n",
  49410. + pfkey_ext->sadb_ext_type);
  49411. + SENDERR(EINVAL);
  49412. + }
  49413. +
  49414. + ipsp->ips_said.spi = pfkey_sa->sadb_sa_spi;
  49415. + ipsp->ips_replaywin = pfkey_sa->sadb_sa_replay;
  49416. + ipsp->ips_state = pfkey_sa->sadb_sa_state;
  49417. + ipsp->ips_flags = pfkey_sa->sadb_sa_flags;
  49418. + ipsp->ips_replaywin_lastseq = ipsp->ips_replaywin_bitmap = 0;
  49419. + ipsp->ips_ref_rel = pfkey_sa->sadb_x_sa_ref;
  49420. +
  49421. + switch(ipsp->ips_said.proto) {
  49422. + case IPPROTO_AH:
  49423. + ipsp->ips_authalg = pfkey_sa->sadb_sa_auth;
  49424. + ipsp->ips_encalg = SADB_EALG_NONE;
  49425. + break;
  49426. + case IPPROTO_ESP:
  49427. + ipsp->ips_authalg = pfkey_sa->sadb_sa_auth;
  49428. + ipsp->ips_encalg = pfkey_sa->sadb_sa_encrypt;
  49429. + ipsec_alg_sa_init(ipsp);
  49430. + break;
  49431. + case IPPROTO_IPIP:
  49432. + ipsp->ips_authalg = AH_NONE;
  49433. + ipsp->ips_encalg = ESP_NONE;
  49434. + break;
  49435. +#ifdef CONFIG_KLIPS_IPCOMP
  49436. + case IPPROTO_COMP:
  49437. + ipsp->ips_authalg = AH_NONE;
  49438. + ipsp->ips_encalg = pfkey_sa->sadb_sa_encrypt;
  49439. + break;
  49440. +#endif /* CONFIG_KLIPS_IPCOMP */
  49441. + case IPPROTO_INT:
  49442. + ipsp->ips_authalg = AH_NONE;
  49443. + ipsp->ips_encalg = ESP_NONE;
  49444. + break;
  49445. + case 0:
  49446. + break;
  49447. + default:
  49448. + KLIPS_PRINT(debug_pfkey,
  49449. + "klips_debug:pfkey_sa_process: "
  49450. + "unknown proto=%d.\n",
  49451. + ipsp->ips_said.proto);
  49452. + SENDERR(EINVAL);
  49453. + }
  49454. +
  49455. +errlab:
  49456. + return error;
  49457. +}
  49458. +
  49459. +int
  49460. +pfkey_lifetime_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49461. +{
  49462. + int error = 0;
  49463. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)pfkey_ext;
  49464. +
  49465. + KLIPS_PRINT(debug_pfkey,
  49466. + "klips_debug:pfkey_lifetime_process: .\n");
  49467. +
  49468. + if(!extr || !extr->ips) {
  49469. + KLIPS_PRINT(debug_pfkey,
  49470. + "klips_debug:pfkey_lifetime_process: "
  49471. + "extr or extr->ips is NULL, fatal\n");
  49472. + SENDERR(EINVAL);
  49473. + }
  49474. +
  49475. + switch(pfkey_lifetime->sadb_lifetime_exttype) {
  49476. + case SADB_EXT_LIFETIME_CURRENT:
  49477. + KLIPS_PRINT(debug_pfkey,
  49478. + "klips_debug:pfkey_lifetime_process: "
  49479. + "lifetime_current not supported yet.\n");
  49480. + SENDERR(EINVAL);
  49481. + break;
  49482. + case SADB_EXT_LIFETIME_HARD:
  49483. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_allocations,
  49484. + pfkey_lifetime->sadb_lifetime_allocations);
  49485. +
  49486. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_bytes,
  49487. + pfkey_lifetime->sadb_lifetime_bytes);
  49488. +
  49489. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_addtime,
  49490. + pfkey_lifetime->sadb_lifetime_addtime);
  49491. +
  49492. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_usetime,
  49493. + pfkey_lifetime->sadb_lifetime_usetime);
  49494. +
  49495. + break;
  49496. +
  49497. + case SADB_EXT_LIFETIME_SOFT:
  49498. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_allocations,
  49499. + pfkey_lifetime->sadb_lifetime_allocations);
  49500. +
  49501. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_bytes,
  49502. + pfkey_lifetime->sadb_lifetime_bytes);
  49503. +
  49504. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_addtime,
  49505. + pfkey_lifetime->sadb_lifetime_addtime);
  49506. +
  49507. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_usetime,
  49508. + pfkey_lifetime->sadb_lifetime_usetime);
  49509. +
  49510. + break;
  49511. + default:
  49512. + KLIPS_PRINT(debug_pfkey,
  49513. + "klips_debug:pfkey_lifetime_process: "
  49514. + "invalid exttype=%d.\n",
  49515. + pfkey_ext->sadb_ext_type);
  49516. + SENDERR(EINVAL);
  49517. + }
  49518. +
  49519. +errlab:
  49520. + return error;
  49521. +}
  49522. +
  49523. +int
  49524. +pfkey_address_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49525. +{
  49526. + int error = 0;
  49527. + int saddr_len = 0;
  49528. + char ipaddr_txt[ADDRTOA_BUF];
  49529. + unsigned char **sap;
  49530. + unsigned short * portp = 0;
  49531. + struct sadb_address *pfkey_address = (struct sadb_address *)pfkey_ext;
  49532. + struct sockaddr* s = (struct sockaddr*)((char*)pfkey_address + sizeof(*pfkey_address));
  49533. + struct ipsec_sa* ipsp;
  49534. +
  49535. + KLIPS_PRINT(debug_pfkey,
  49536. + "klips_debug:pfkey_address_process:\n");
  49537. +
  49538. + if(!extr || !extr->ips) {
  49539. + KLIPS_PRINT(debug_pfkey,
  49540. + "klips_debug:pfkey_address_process: "
  49541. + "extr or extr->ips is NULL, fatal\n");
  49542. + SENDERR(EINVAL);
  49543. + }
  49544. +
  49545. + switch(s->sa_family) {
  49546. + case AF_INET:
  49547. + saddr_len = sizeof(struct sockaddr_in);
  49548. + addrtoa(((struct sockaddr_in*)s)->sin_addr, 0, ipaddr_txt, sizeof(ipaddr_txt));
  49549. + KLIPS_PRINT(debug_pfkey,
  49550. + "klips_debug:pfkey_address_process: "
  49551. + "found address family=%d, AF_INET, %s.\n",
  49552. + s->sa_family,
  49553. + ipaddr_txt);
  49554. + break;
  49555. +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  49556. + case AF_INET6:
  49557. + saddr_len = sizeof(struct sockaddr_in6);
  49558. + break;
  49559. +#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  49560. + default:
  49561. + KLIPS_PRINT(debug_pfkey,
  49562. + "klips_debug:pfkey_address_process: "
  49563. + "s->sa_family=%d not supported.\n",
  49564. + s->sa_family);
  49565. + SENDERR(EPFNOSUPPORT);
  49566. + }
  49567. +
  49568. + switch(pfkey_address->sadb_address_exttype) {
  49569. + case SADB_EXT_ADDRESS_SRC:
  49570. + KLIPS_PRINT(debug_pfkey,
  49571. + "klips_debug:pfkey_address_process: "
  49572. + "found src address.\n");
  49573. + sap = (unsigned char **)&(extr->ips->ips_addr_s);
  49574. + extr->ips->ips_addr_s_size = saddr_len;
  49575. + break;
  49576. + case SADB_EXT_ADDRESS_DST:
  49577. + KLIPS_PRINT(debug_pfkey,
  49578. + "klips_debug:pfkey_address_process: "
  49579. + "found dst address.\n");
  49580. + sap = (unsigned char **)&(extr->ips->ips_addr_d);
  49581. + extr->ips->ips_addr_d_size = saddr_len;
  49582. + break;
  49583. + case SADB_EXT_ADDRESS_PROXY:
  49584. + KLIPS_PRINT(debug_pfkey,
  49585. + "klips_debug:pfkey_address_process: "
  49586. + "found proxy address.\n");
  49587. + sap = (unsigned char **)&(extr->ips->ips_addr_p);
  49588. + extr->ips->ips_addr_p_size = saddr_len;
  49589. + break;
  49590. + case SADB_X_EXT_ADDRESS_DST2:
  49591. + KLIPS_PRINT(debug_pfkey,
  49592. + "klips_debug:pfkey_address_process: "
  49593. + "found 2nd dst address.\n");
  49594. + if(extr->ips2 == NULL) {
  49595. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49596. + }
  49597. + if(extr->ips2 == NULL) {
  49598. + SENDERR(-error);
  49599. + }
  49600. + sap = (unsigned char **)&(extr->ips2->ips_addr_d);
  49601. + extr->ips2->ips_addr_d_size = saddr_len;
  49602. + break;
  49603. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  49604. + KLIPS_PRINT(debug_pfkey,
  49605. + "klips_debug:pfkey_address_process: "
  49606. + "found src flow address.\n");
  49607. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49608. + SENDERR(ENOMEM);
  49609. + }
  49610. + sap = (unsigned char **)&(extr->eroute->er_eaddr.sen_ip_src);
  49611. + portp = &(extr->eroute->er_eaddr.sen_sport);
  49612. + break;
  49613. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  49614. + KLIPS_PRINT(debug_pfkey,
  49615. + "klips_debug:pfkey_address_process: "
  49616. + "found dst flow address.\n");
  49617. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49618. + SENDERR(ENOMEM);
  49619. + }
  49620. + sap = (unsigned char **)&(extr->eroute->er_eaddr.sen_ip_dst);
  49621. + portp = &(extr->eroute->er_eaddr.sen_dport);
  49622. + break;
  49623. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  49624. + KLIPS_PRINT(debug_pfkey,
  49625. + "klips_debug:pfkey_address_process: "
  49626. + "found src mask address.\n");
  49627. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49628. + SENDERR(ENOMEM);
  49629. + }
  49630. + sap = (unsigned char **)&(extr->eroute->er_emask.sen_ip_src);
  49631. + portp = &(extr->eroute->er_emask.sen_sport);
  49632. + break;
  49633. + case SADB_X_EXT_ADDRESS_DST_MASK:
  49634. + KLIPS_PRINT(debug_pfkey,
  49635. + "klips_debug:pfkey_address_process: "
  49636. + "found dst mask address.\n");
  49637. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49638. + SENDERR(ENOMEM);
  49639. + }
  49640. + sap = (unsigned char **)&(extr->eroute->er_emask.sen_ip_dst);
  49641. + portp = &(extr->eroute->er_emask.sen_dport);
  49642. + break;
  49643. +#ifdef NAT_TRAVERSAL
  49644. + case SADB_X_EXT_NAT_T_OA:
  49645. + KLIPS_PRINT(debug_pfkey,
  49646. + "klips_debug:pfkey_address_process: "
  49647. + "found NAT-OA address.\n");
  49648. + sap = (unsigned char **)&(extr->ips->ips_natt_oa);
  49649. + extr->ips->ips_natt_oa_size = saddr_len;
  49650. + break;
  49651. +#endif
  49652. + default:
  49653. + KLIPS_PRINT(debug_pfkey,
  49654. + "klips_debug:pfkey_address_process: "
  49655. + "unrecognised ext_type=%d.\n",
  49656. + pfkey_address->sadb_address_exttype);
  49657. + SENDERR(EINVAL);
  49658. + }
  49659. +
  49660. + switch(pfkey_address->sadb_address_exttype) {
  49661. + case SADB_EXT_ADDRESS_SRC:
  49662. + case SADB_EXT_ADDRESS_DST:
  49663. + case SADB_EXT_ADDRESS_PROXY:
  49664. + case SADB_X_EXT_ADDRESS_DST2:
  49665. +#ifdef NAT_TRAVERSAL
  49666. + case SADB_X_EXT_NAT_T_OA:
  49667. +#endif
  49668. + KLIPS_PRINT(debug_pfkey,
  49669. + "klips_debug:pfkey_address_process: "
  49670. + "allocating %d bytes for saddr.\n",
  49671. + saddr_len);
  49672. + if(!(*sap = kmalloc(saddr_len, GFP_KERNEL))) {
  49673. + SENDERR(ENOMEM);
  49674. + }
  49675. + memcpy(*sap, s, saddr_len);
  49676. + break;
  49677. + default:
  49678. + if(s->sa_family != AF_INET) {
  49679. + KLIPS_PRINT(debug_pfkey,
  49680. + "klips_debug:pfkey_address_process: "
  49681. + "s->sa_family=%d not supported.\n",
  49682. + s->sa_family);
  49683. + SENDERR(EPFNOSUPPORT);
  49684. + }
  49685. + {
  49686. + unsigned long *ulsap = (unsigned long *)sap;
  49687. + *ulsap = ((struct sockaddr_in*)s)->sin_addr.s_addr;
  49688. + }
  49689. +
  49690. + if (portp != 0)
  49691. + *portp = ((struct sockaddr_in*)s)->sin_port;
  49692. +#ifdef CONFIG_KLIPS_DEBUG
  49693. + if(extr->eroute) {
  49694. + char buf1[64], buf2[64];
  49695. + if (debug_pfkey) {
  49696. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  49697. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  49698. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  49699. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  49700. + KLIPS_PRINT(debug_pfkey,
  49701. + "klips_debug:pfkey_address_parse: "
  49702. + "extr->eroute set to %s:%d->%s:%d\n",
  49703. + buf1,
  49704. + ntohs(extr->eroute->er_eaddr.sen_sport),
  49705. + buf2,
  49706. + ntohs(extr->eroute->er_eaddr.sen_dport));
  49707. + }
  49708. + }
  49709. +#endif /* CONFIG_KLIPS_DEBUG */
  49710. + }
  49711. +
  49712. + ipsp = extr->ips;
  49713. + switch(pfkey_address->sadb_address_exttype) {
  49714. + case SADB_X_EXT_ADDRESS_DST2:
  49715. + ipsp = extr->ips2;
  49716. + case SADB_EXT_ADDRESS_DST:
  49717. + if(s->sa_family == AF_INET) {
  49718. + ipsp->ips_said.dst.u.v4.sin_addr.s_addr = ((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr.s_addr;
  49719. + ipsp->ips_said.dst.u.v4.sin_family = AF_INET;
  49720. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,
  49721. + 0,
  49722. + ipaddr_txt,
  49723. + sizeof(ipaddr_txt));
  49724. + KLIPS_PRINT(debug_pfkey,
  49725. + "klips_debug:pfkey_address_process: "
  49726. + "ips_said.dst set to %s.\n",
  49727. + ipaddr_txt);
  49728. + } else {
  49729. + KLIPS_PRINT(debug_pfkey,
  49730. + "klips_debug:pfkey_address_process: "
  49731. + "uh, ips_said.dst doesn't do address family=%d yet, said will be invalid.\n",
  49732. + s->sa_family);
  49733. + }
  49734. + default:
  49735. + break;
  49736. + }
  49737. +
  49738. + /* XXX check if port!=0 */
  49739. +
  49740. + KLIPS_PRINT(debug_pfkey,
  49741. + "klips_debug:pfkey_address_process: successful.\n");
  49742. + errlab:
  49743. + return error;
  49744. +}
  49745. +
  49746. +int
  49747. +pfkey_key_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49748. +{
  49749. + int error = 0;
  49750. + struct sadb_key *pfkey_key = (struct sadb_key *)pfkey_ext;
  49751. +
  49752. + KLIPS_PRINT(debug_pfkey,
  49753. + "klips_debug:pfkey_key_process: .\n");
  49754. +
  49755. + if(!extr || !extr->ips) {
  49756. + KLIPS_PRINT(debug_pfkey,
  49757. + "klips_debug:pfkey_key_process: "
  49758. + "extr or extr->ips is NULL, fatal\n");
  49759. + SENDERR(EINVAL);
  49760. + }
  49761. +
  49762. + switch(pfkey_key->sadb_key_exttype) {
  49763. + case SADB_EXT_KEY_AUTH:
  49764. + KLIPS_PRINT(debug_pfkey,
  49765. + "klips_debug:pfkey_key_process: "
  49766. + "allocating %d bytes for authkey.\n",
  49767. + DIVUP(pfkey_key->sadb_key_bits, 8));
  49768. + if(!(extr->ips->ips_key_a = kmalloc(DIVUP(pfkey_key->sadb_key_bits, 8), GFP_KERNEL))) {
  49769. + KLIPS_PRINT(debug_pfkey,
  49770. + "klips_debug:pfkey_key_process: "
  49771. + "memory allocation error.\n");
  49772. + SENDERR(ENOMEM);
  49773. + }
  49774. + extr->ips->ips_key_bits_a = pfkey_key->sadb_key_bits;
  49775. + extr->ips->ips_key_a_size = DIVUP(pfkey_key->sadb_key_bits, 8);
  49776. + memcpy(extr->ips->ips_key_a,
  49777. + (char*)pfkey_key + sizeof(struct sadb_key),
  49778. + extr->ips->ips_key_a_size);
  49779. + break;
  49780. + case SADB_EXT_KEY_ENCRYPT: /* Key(s) */
  49781. + KLIPS_PRINT(debug_pfkey,
  49782. + "klips_debug:pfkey_key_process: "
  49783. + "allocating %d bytes for enckey.\n",
  49784. + DIVUP(pfkey_key->sadb_key_bits, 8));
  49785. + if(!(extr->ips->ips_key_e = kmalloc(DIVUP(pfkey_key->sadb_key_bits, 8), GFP_KERNEL))) {
  49786. + KLIPS_PRINT(debug_pfkey,
  49787. + "klips_debug:pfkey_key_process: "
  49788. + "memory allocation error.\n");
  49789. + SENDERR(ENOMEM);
  49790. + }
  49791. + extr->ips->ips_key_bits_e = pfkey_key->sadb_key_bits;
  49792. + extr->ips->ips_key_e_size = DIVUP(pfkey_key->sadb_key_bits, 8);
  49793. + memcpy(extr->ips->ips_key_e,
  49794. + (char*)pfkey_key + sizeof(struct sadb_key),
  49795. + extr->ips->ips_key_e_size);
  49796. + break;
  49797. + default:
  49798. + SENDERR(EINVAL);
  49799. + }
  49800. +
  49801. + KLIPS_PRINT(debug_pfkey,
  49802. + "klips_debug:pfkey_key_process: "
  49803. + "success.\n");
  49804. +errlab:
  49805. + return error;
  49806. +}
  49807. +
  49808. +int
  49809. +pfkey_ident_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49810. +{
  49811. + int error = 0;
  49812. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)pfkey_ext;
  49813. + int data_len;
  49814. +
  49815. + KLIPS_PRINT(debug_pfkey,
  49816. + "klips_debug:pfkey_ident_process: .\n");
  49817. +
  49818. + if(!extr || !extr->ips) {
  49819. + KLIPS_PRINT(debug_pfkey,
  49820. + "klips_debug:pfkey_ident_process: "
  49821. + "extr or extr->ips is NULL, fatal\n");
  49822. + SENDERR(EINVAL);
  49823. + }
  49824. +
  49825. + switch(pfkey_ident->sadb_ident_exttype) {
  49826. + case SADB_EXT_IDENTITY_SRC:
  49827. + data_len = pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  49828. +
  49829. + extr->ips->ips_ident_s.type = pfkey_ident->sadb_ident_type;
  49830. + extr->ips->ips_ident_s.id = pfkey_ident->sadb_ident_id;
  49831. + extr->ips->ips_ident_s.len = pfkey_ident->sadb_ident_len;
  49832. + if(data_len) {
  49833. + KLIPS_PRINT(debug_pfkey,
  49834. + "klips_debug:pfkey_ident_process: "
  49835. + "allocating %d bytes for ident_s.\n",
  49836. + data_len);
  49837. + if(!(extr->ips->ips_ident_s.data
  49838. + = kmalloc(data_len, GFP_KERNEL))) {
  49839. + SENDERR(ENOMEM);
  49840. + }
  49841. + memcpy(extr->ips->ips_ident_s.data,
  49842. + (char*)pfkey_ident + sizeof(struct sadb_ident),
  49843. + data_len);
  49844. + } else {
  49845. + extr->ips->ips_ident_s.data = NULL;
  49846. + }
  49847. + break;
  49848. + case SADB_EXT_IDENTITY_DST: /* Identity(ies) */
  49849. + data_len = pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  49850. +
  49851. + extr->ips->ips_ident_d.type = pfkey_ident->sadb_ident_type;
  49852. + extr->ips->ips_ident_d.id = pfkey_ident->sadb_ident_id;
  49853. + extr->ips->ips_ident_d.len = pfkey_ident->sadb_ident_len;
  49854. + if(data_len) {
  49855. + KLIPS_PRINT(debug_pfkey,
  49856. + "klips_debug:pfkey_ident_process: "
  49857. + "allocating %d bytes for ident_d.\n",
  49858. + data_len);
  49859. + if(!(extr->ips->ips_ident_d.data
  49860. + = kmalloc(data_len, GFP_KERNEL))) {
  49861. + SENDERR(ENOMEM);
  49862. + }
  49863. + memcpy(extr->ips->ips_ident_d.data,
  49864. + (char*)pfkey_ident + sizeof(struct sadb_ident),
  49865. + data_len);
  49866. + } else {
  49867. + extr->ips->ips_ident_d.data = NULL;
  49868. + }
  49869. + break;
  49870. + default:
  49871. + SENDERR(EINVAL);
  49872. + }
  49873. +errlab:
  49874. + return error;
  49875. +}
  49876. +
  49877. +int
  49878. +pfkey_sens_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49879. +{
  49880. + int error = 0;
  49881. +
  49882. + KLIPS_PRINT(debug_pfkey,
  49883. + "klips_debug:pfkey_sens_process: "
  49884. + "Sorry, I can't process exttype=%d yet.\n",
  49885. + pfkey_ext->sadb_ext_type);
  49886. + SENDERR(EINVAL); /* don't process these yet */
  49887. + errlab:
  49888. + return error;
  49889. +}
  49890. +
  49891. +int
  49892. +pfkey_prop_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49893. +{
  49894. + int error = 0;
  49895. +
  49896. + KLIPS_PRINT(debug_pfkey,
  49897. + "klips_debug:pfkey_prop_process: "
  49898. + "Sorry, I can't process exttype=%d yet.\n",
  49899. + pfkey_ext->sadb_ext_type);
  49900. + SENDERR(EINVAL); /* don't process these yet */
  49901. +
  49902. + errlab:
  49903. + return error;
  49904. +}
  49905. +
  49906. +int
  49907. +pfkey_supported_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49908. +{
  49909. + int error = 0;
  49910. +
  49911. + KLIPS_PRINT(debug_pfkey,
  49912. + "klips_debug:pfkey_supported_process: "
  49913. + "Sorry, I can't process exttype=%d yet.\n",
  49914. + pfkey_ext->sadb_ext_type);
  49915. + SENDERR(EINVAL); /* don't process these yet */
  49916. +
  49917. +errlab:
  49918. + return error;
  49919. +}
  49920. +
  49921. +int
  49922. +pfkey_spirange_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49923. +{
  49924. + int error = 0;
  49925. +
  49926. + KLIPS_PRINT(debug_pfkey,
  49927. + "klips_debug:pfkey_spirange_process: .\n");
  49928. +/* errlab: */
  49929. + return error;
  49930. +}
  49931. +
  49932. +int
  49933. +pfkey_x_kmprivate_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49934. +{
  49935. + int error = 0;
  49936. +
  49937. + KLIPS_PRINT(debug_pfkey,
  49938. + "klips_debug:pfkey_x_kmprivate_process: "
  49939. + "Sorry, I can't process exttype=%d yet.\n",
  49940. + pfkey_ext->sadb_ext_type);
  49941. + SENDERR(EINVAL); /* don't process these yet */
  49942. +
  49943. +errlab:
  49944. + return error;
  49945. +}
  49946. +
  49947. +int
  49948. +pfkey_x_satype_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49949. +{
  49950. + int error = 0;
  49951. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)pfkey_ext;
  49952. +
  49953. + KLIPS_PRINT(debug_pfkey,
  49954. + "klips_debug:pfkey_x_satype_process: .\n");
  49955. +
  49956. + if(!extr || !extr->ips) {
  49957. + KLIPS_PRINT(debug_pfkey,
  49958. + "klips_debug:pfkey_x_satype_process: "
  49959. + "extr or extr->ips is NULL, fatal\n");
  49960. + SENDERR(EINVAL);
  49961. + }
  49962. +
  49963. + if(extr->ips2 == NULL) {
  49964. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49965. + }
  49966. + if(extr->ips2 == NULL) {
  49967. + SENDERR(-error);
  49968. + }
  49969. + if(!(extr->ips2->ips_said.proto = satype2proto(pfkey_x_satype->sadb_x_satype_satype))) {
  49970. + KLIPS_PRINT(debug_pfkey,
  49971. + "klips_debug:pfkey_x_satype_process: "
  49972. + "proto lookup from satype=%d failed.\n",
  49973. + pfkey_x_satype->sadb_x_satype_satype);
  49974. + SENDERR(EINVAL);
  49975. + }
  49976. + KLIPS_PRINT(debug_pfkey,
  49977. + "klips_debug:pfkey_x_satype_process: "
  49978. + "protocol==%d decoded from satype==%d(%s).\n",
  49979. + extr->ips2->ips_said.proto,
  49980. + pfkey_x_satype->sadb_x_satype_satype,
  49981. + satype2name(pfkey_x_satype->sadb_x_satype_satype));
  49982. +
  49983. +errlab:
  49984. + return error;
  49985. +}
  49986. +
  49987. +
  49988. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  49989. +int
  49990. +pfkey_x_nat_t_type_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49991. +{
  49992. + int error = 0;
  49993. + struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)pfkey_ext;
  49994. +
  49995. + if(!pfkey_x_nat_t_type) {
  49996. + printk("klips_debug:pfkey_x_nat_t_type_process: "
  49997. + "null pointer passed in\n");
  49998. + SENDERR(EINVAL);
  49999. + }
  50000. +
  50001. + KLIPS_PRINT(debug_pfkey,
  50002. + "klips_debug:pfkey_x_nat_t_type_process: %d.\n",
  50003. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type);
  50004. +
  50005. + if(!extr || !extr->ips) {
  50006. + KLIPS_PRINT(debug_pfkey,
  50007. + "klips_debug:pfkey_nat_t_type_process: "
  50008. + "extr or extr->ips is NULL, fatal\n");
  50009. + SENDERR(EINVAL);
  50010. + }
  50011. +
  50012. + switch(pfkey_x_nat_t_type->sadb_x_nat_t_type_type) {
  50013. + case ESPINUDP_WITH_NON_IKE: /* with Non-IKE (older version) */
  50014. + case ESPINUDP_WITH_NON_ESP: /* with Non-ESP */
  50015. +
  50016. + extr->ips->ips_natt_type = pfkey_x_nat_t_type->sadb_x_nat_t_type_type;
  50017. + break;
  50018. + default:
  50019. + KLIPS_PRINT(debug_pfkey,
  50020. + "klips_debug:pfkey_x_nat_t_type_process: "
  50021. + "unknown type %d.\n",
  50022. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type);
  50023. + SENDERR(EINVAL);
  50024. + break;
  50025. + }
  50026. +
  50027. +errlab:
  50028. + return error;
  50029. +}
  50030. +
  50031. +int
  50032. +pfkey_x_nat_t_port_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  50033. +{
  50034. + int error = 0;
  50035. + struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)pfkey_ext;
  50036. +
  50037. + if(!pfkey_x_nat_t_port) {
  50038. + printk("klips_debug:pfkey_x_nat_t_port_process: "
  50039. + "null pointer passed in\n");
  50040. + SENDERR(EINVAL);
  50041. + }
  50042. +
  50043. + KLIPS_PRINT(debug_pfkey,
  50044. + "klips_debug:pfkey_x_nat_t_port_process: %d/%d.\n",
  50045. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype,
  50046. + pfkey_x_nat_t_port->sadb_x_nat_t_port_port);
  50047. +
  50048. + if(!extr || !extr->ips) {
  50049. + KLIPS_PRINT(debug_pfkey,
  50050. + "klips_debug:pfkey_nat_t_type_process: "
  50051. + "extr or extr->ips is NULL, fatal\n");
  50052. + SENDERR(EINVAL);
  50053. + }
  50054. +
  50055. + switch(pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype) {
  50056. + case SADB_X_EXT_NAT_T_SPORT:
  50057. + extr->ips->ips_natt_sport = pfkey_x_nat_t_port->sadb_x_nat_t_port_port;
  50058. + break;
  50059. + case SADB_X_EXT_NAT_T_DPORT:
  50060. + extr->ips->ips_natt_dport = pfkey_x_nat_t_port->sadb_x_nat_t_port_port;
  50061. + break;
  50062. + default:
  50063. + KLIPS_PRINT(debug_pfkey,
  50064. + "klips_debug:pfkey_x_nat_t_port_process: "
  50065. + "unknown exttype %d.\n",
  50066. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype);
  50067. + SENDERR(EINVAL);
  50068. + break;
  50069. + }
  50070. +
  50071. +errlab:
  50072. + return error;
  50073. +}
  50074. +#endif
  50075. +
  50076. +int
  50077. +pfkey_x_debug_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  50078. +{
  50079. + int error = 0;
  50080. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)pfkey_ext;
  50081. +
  50082. + if(!pfkey_x_debug) {
  50083. + printk("klips_debug:pfkey_x_debug_process: "
  50084. + "null pointer passed in\n");
  50085. + SENDERR(EINVAL);
  50086. + }
  50087. +
  50088. + KLIPS_PRINT(debug_pfkey,
  50089. + "klips_debug:pfkey_x_debug_process: .\n");
  50090. +
  50091. +#ifdef CONFIG_KLIPS_DEBUG
  50092. + if(pfkey_x_debug->sadb_x_debug_netlink >>
  50093. + (sizeof(pfkey_x_debug->sadb_x_debug_netlink) * 8 - 1)) {
  50094. + pfkey_x_debug->sadb_x_debug_netlink &=
  50095. + ~(1 << (sizeof(pfkey_x_debug->sadb_x_debug_netlink) * 8 -1));
  50096. + debug_tunnel |= pfkey_x_debug->sadb_x_debug_tunnel;
  50097. + debug_netlink |= pfkey_x_debug->sadb_x_debug_netlink;
  50098. + debug_xform |= pfkey_x_debug->sadb_x_debug_xform;
  50099. + debug_eroute |= pfkey_x_debug->sadb_x_debug_eroute;
  50100. + debug_spi |= pfkey_x_debug->sadb_x_debug_spi;
  50101. + debug_radij |= pfkey_x_debug->sadb_x_debug_radij;
  50102. + debug_esp |= pfkey_x_debug->sadb_x_debug_esp;
  50103. + debug_ah |= pfkey_x_debug->sadb_x_debug_ah;
  50104. + debug_rcv |= pfkey_x_debug->sadb_x_debug_rcv;
  50105. + debug_pfkey |= pfkey_x_debug->sadb_x_debug_pfkey;
  50106. +#ifdef CONFIG_KLIPS_IPCOMP
  50107. + sysctl_ipsec_debug_ipcomp |= pfkey_x_debug->sadb_x_debug_ipcomp;
  50108. +#endif /* CONFIG_KLIPS_IPCOMP */
  50109. + sysctl_ipsec_debug_verbose |= pfkey_x_debug->sadb_x_debug_verbose;
  50110. + KLIPS_PRINT(debug_pfkey,
  50111. + "klips_debug:pfkey_x_debug_process: "
  50112. + "set\n");
  50113. + } else {
  50114. + KLIPS_PRINT(debug_pfkey,
  50115. + "klips_debug:pfkey_x_debug_process: "
  50116. + "unset\n");
  50117. + debug_tunnel &= pfkey_x_debug->sadb_x_debug_tunnel;
  50118. + debug_netlink &= pfkey_x_debug->sadb_x_debug_netlink;
  50119. + debug_xform &= pfkey_x_debug->sadb_x_debug_xform;
  50120. + debug_eroute &= pfkey_x_debug->sadb_x_debug_eroute;
  50121. + debug_spi &= pfkey_x_debug->sadb_x_debug_spi;
  50122. + debug_radij &= pfkey_x_debug->sadb_x_debug_radij;
  50123. + debug_esp &= pfkey_x_debug->sadb_x_debug_esp;
  50124. + debug_ah &= pfkey_x_debug->sadb_x_debug_ah;
  50125. + debug_rcv &= pfkey_x_debug->sadb_x_debug_rcv;
  50126. + debug_pfkey &= pfkey_x_debug->sadb_x_debug_pfkey;
  50127. +#ifdef CONFIG_KLIPS_IPCOMP
  50128. + sysctl_ipsec_debug_ipcomp &= pfkey_x_debug->sadb_x_debug_ipcomp;
  50129. +#endif /* CONFIG_KLIPS_IPCOMP */
  50130. + sysctl_ipsec_debug_verbose &= pfkey_x_debug->sadb_x_debug_verbose;
  50131. + }
  50132. +#else /* CONFIG_KLIPS_DEBUG */
  50133. + printk("klips_debug:pfkey_x_debug_process: "
  50134. + "debugging not enabled\n");
  50135. + SENDERR(EINVAL);
  50136. +#endif /* CONFIG_KLIPS_DEBUG */
  50137. +
  50138. +errlab:
  50139. + return error;
  50140. +}
  50141. +
  50142. +/*
  50143. + * $Log: pfkey_v2_ext_process.c,v $
  50144. + * Revision 1.20.2.2 2006/10/06 21:39:26 paul
  50145. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  50146. + * set. This is defined through autoconf.h which is included through the
  50147. + * linux kernel build macros.
  50148. + *
  50149. + * Revision 1.20.2.1 2006/04/20 16:33:07 mcr
  50150. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  50151. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  50152. + *
  50153. + * Revision 1.20 2005/04/29 05:10:22 mcr
  50154. + * removed from extraenous includes to make unit testing easier.
  50155. + *
  50156. + * Revision 1.19 2004/12/04 07:14:18 mcr
  50157. + * resolution to gcc3-ism was wrong. fixed to assign correct
  50158. + * variable.
  50159. + *
  50160. + * Revision 1.18 2004/12/03 21:25:57 mcr
  50161. + * compile time fixes for running on 2.6.
  50162. + * still experimental.
  50163. + *
  50164. + * Revision 1.17 2004/08/21 00:45:04 mcr
  50165. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  50166. + *
  50167. + * Revision 1.16 2004/07/10 19:11:18 mcr
  50168. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  50169. + *
  50170. + * Revision 1.15 2004/04/06 02:49:26 mcr
  50171. + * pullup of algo code from alg-branch.
  50172. + *
  50173. + * Revision 1.14 2004/02/03 03:13:59 mcr
  50174. + * no longer #ifdef out NON_ESP mode. That was a mistake.
  50175. + *
  50176. + * Revision 1.13 2003/12/15 18:13:12 mcr
  50177. + * when compiling with NAT traversal, don't assume that the
  50178. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  50179. + * is set.
  50180. + *
  50181. + * Revision 1.12.2.1 2003/12/22 15:25:52 jjo
  50182. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  50183. + *
  50184. + * Revision 1.12 2003/12/10 01:14:27 mcr
  50185. + * NAT-traversal patches to KLIPS.
  50186. + *
  50187. + * Revision 1.11 2003/10/31 02:27:55 mcr
  50188. + * pulled up port-selector patches and sa_id elimination.
  50189. + *
  50190. + * Revision 1.10.4.2 2003/10/29 01:30:41 mcr
  50191. + * elimited "struct sa_id".
  50192. + *
  50193. + * Revision 1.10.4.1 2003/09/21 13:59:56 mcr
  50194. + * pre-liminary X.509 patch - does not yet pass tests.
  50195. + *
  50196. + * Revision 1.10 2003/02/06 01:51:41 rgb
  50197. + * Removed no longer relevant comment
  50198. + *
  50199. + * Revision 1.9 2003/01/30 02:32:44 rgb
  50200. + *
  50201. + * Transmit error code through to caller from callee for better diagnosis of problems.
  50202. + *
  50203. + * Revision 1.8 2002/12/13 22:42:22 mcr
  50204. + * restored sa_ref code
  50205. + *
  50206. + * Revision 1.7 2002/12/13 22:40:48 mcr
  50207. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  50208. + *
  50209. + * Revision 1.6 2002/10/05 05:02:58 dhr
  50210. + *
  50211. + * C labels go on statements
  50212. + *
  50213. + * Revision 1.5 2002/09/20 15:41:08 rgb
  50214. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  50215. + * Added sadb_x_sa_ref to struct sadb_sa.
  50216. + *
  50217. + * Revision 1.4 2002/09/20 05:02:02 rgb
  50218. + * Added memory allocation debugging.
  50219. + *
  50220. + * Revision 1.3 2002/07/24 18:44:54 rgb
  50221. + * Type fiddling to tame ia64 compiler.
  50222. + *
  50223. + * Revision 1.2 2002/05/27 18:55:03 rgb
  50224. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  50225. + *
  50226. + * Revision 1.1 2002/05/14 02:33:51 rgb
  50227. + * Moved all the extension processing functions to pfkey_v2_ext_process.c.
  50228. + *
  50229. + *
  50230. + * Local variables:
  50231. + * c-file-style: "linux"
  50232. + * End:
  50233. + *
  50234. + */
  50235. --- /dev/null Tue Mar 11 13:02:56 2003
  50236. +++ linux/net/ipsec/pfkey_v2_parse.c Mon Feb 9 13:51:03 2004
  50237. @@ -0,0 +1,1846 @@
  50238. +/*
  50239. + * RFC2367 PF_KEYv2 Key management API message parser
  50240. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  50241. + *
  50242. + * This program is free software; you can redistribute it and/or modify it
  50243. + * under the terms of the GNU General Public License as published by the
  50244. + * Free Software Foundation; either version 2 of the License, or (at your
  50245. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  50246. + *
  50247. + * This program is distributed in the hope that it will be useful, but
  50248. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  50249. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  50250. + * for more details.
  50251. + *
  50252. + * RCSID $Id: pfkey_v2_parse.c,v 1.65 2005/04/06 17:46:05 mcr Exp $
  50253. + */
  50254. +
  50255. +/*
  50256. + * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
  50257. + */
  50258. +
  50259. +char pfkey_v2_parse_c_version[] = "$Id: pfkey_v2_parse.c,v 1.65 2005/04/06 17:46:05 mcr Exp $";
  50260. +
  50261. +/*
  50262. + * Some ugly stuff to allow consistent debugging code for use in the
  50263. + * kernel and in user space
  50264. +*/
  50265. +
  50266. +#ifdef __KERNEL__
  50267. +
  50268. +# include <linux/kernel.h> /* for printk */
  50269. +
  50270. +#include "openswan/ipsec_kversion.h" /* for malloc switch */
  50271. +
  50272. +# ifdef MALLOC_SLAB
  50273. +# include <linux/slab.h> /* kmalloc() */
  50274. +# else /* MALLOC_SLAB */
  50275. +# include <linux/malloc.h> /* kmalloc() */
  50276. +# endif /* MALLOC_SLAB */
  50277. +# include <linux/errno.h> /* error codes */
  50278. +# include <linux/types.h> /* size_t */
  50279. +# include <linux/interrupt.h> /* mark_bh */
  50280. +
  50281. +# include <linux/netdevice.h> /* struct device, and other headers */
  50282. +# include <linux/etherdevice.h> /* eth_type_trans */
  50283. +# include <linux/ip.h> /* struct iphdr */
  50284. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  50285. +# include <linux/ipv6.h> /* struct ipv6hdr */
  50286. +# endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  50287. +extern int debug_pfkey;
  50288. +
  50289. +# include <openswan.h>
  50290. +
  50291. +#include "openswan/ipsec_encap.h"
  50292. +
  50293. +#else /* __KERNEL__ */
  50294. +
  50295. +# include <sys/types.h>
  50296. +# include <linux/types.h>
  50297. +# include <linux/errno.h>
  50298. +
  50299. +# include <openswan.h>
  50300. +# include "constants.h"
  50301. +# include "programs/pluto/defs.h" /* for PRINTF_LIKE */
  50302. +
  50303. +#endif /* __KERNEL__ */
  50304. +
  50305. +
  50306. +#include <pfkeyv2.h>
  50307. +#include <pfkey.h>
  50308. +
  50309. +#include "openswan/ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
  50310. +
  50311. +/*
  50312. + * how to handle debugging for pfkey.
  50313. + */
  50314. +#include <openswan/pfkey_debug.h>
  50315. +
  50316. +unsigned int pfkey_lib_debug = PF_KEY_DEBUG_PARSE_NONE;
  50317. +void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
  50318. +void (*pfkey_error_func)(const char *message, ...) PRINTF_LIKE(1);
  50319. +
  50320. +
  50321. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  50322. +
  50323. +struct satype_tbl {
  50324. + uint8_t proto;
  50325. + uint8_t satype;
  50326. + char* name;
  50327. +} static satype_tbl[] = {
  50328. +#ifdef __KERNEL__
  50329. + { IPPROTO_ESP, SADB_SATYPE_ESP, "ESP" },
  50330. + { IPPROTO_AH, SADB_SATYPE_AH, "AH" },
  50331. + { IPPROTO_IPIP, SADB_X_SATYPE_IPIP, "IPIP" },
  50332. +#ifdef CONFIG_KLIPS_IPCOMP
  50333. + { IPPROTO_COMP, SADB_X_SATYPE_COMP, "COMP" },
  50334. +#endif /* CONFIG_KLIPS_IPCOMP */
  50335. + { IPPROTO_INT, SADB_X_SATYPE_INT, "INT" },
  50336. +#else /* __KERNEL__ */
  50337. + { SA_ESP, SADB_SATYPE_ESP, "ESP" },
  50338. + { SA_AH, SADB_SATYPE_AH, "AH" },
  50339. + { SA_IPIP, SADB_X_SATYPE_IPIP, "IPIP" },
  50340. + { SA_COMP, SADB_X_SATYPE_COMP, "COMP" },
  50341. + { SA_INT, SADB_X_SATYPE_INT, "INT" },
  50342. +#endif /* __KERNEL__ */
  50343. + { 0, 0, "UNKNOWN" }
  50344. +};
  50345. +
  50346. +uint8_t
  50347. +satype2proto(uint8_t satype)
  50348. +{
  50349. + int i =0;
  50350. +
  50351. + while(satype_tbl[i].satype != satype && satype_tbl[i].satype != 0) {
  50352. + i++;
  50353. + }
  50354. + return satype_tbl[i].proto;
  50355. +}
  50356. +
  50357. +uint8_t
  50358. +proto2satype(uint8_t proto)
  50359. +{
  50360. + int i = 0;
  50361. +
  50362. + while(satype_tbl[i].proto != proto && satype_tbl[i].proto != 0) {
  50363. + i++;
  50364. + }
  50365. + return satype_tbl[i].satype;
  50366. +}
  50367. +
  50368. +char*
  50369. +satype2name(uint8_t satype)
  50370. +{
  50371. + int i = 0;
  50372. +
  50373. + while(satype_tbl[i].satype != satype && satype_tbl[i].satype != 0) {
  50374. + i++;
  50375. + }
  50376. + return satype_tbl[i].name;
  50377. +}
  50378. +
  50379. +char*
  50380. +proto2name(uint8_t proto)
  50381. +{
  50382. + int i = 0;
  50383. +
  50384. + while(satype_tbl[i].proto != proto && satype_tbl[i].proto != 0) {
  50385. + i++;
  50386. + }
  50387. + return satype_tbl[i].name;
  50388. +}
  50389. +
  50390. +/* Default extension parsers taken from the KLIPS code */
  50391. +
  50392. +DEBUG_NO_STATIC int
  50393. +pfkey_sa_parse(struct sadb_ext *pfkey_ext)
  50394. +{
  50395. + int error = 0;
  50396. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)pfkey_ext;
  50397. +#if 0
  50398. + struct sadb_sa sav2;
  50399. +#endif
  50400. +
  50401. + /* sanity checks... */
  50402. + if(!pfkey_sa) {
  50403. + ERROR("pfkey_sa_parse: "
  50404. + "NULL pointer passed in.\n");
  50405. + SENDERR(EINVAL);
  50406. + }
  50407. +
  50408. +#if 0
  50409. + /* check if this structure is short, and if so, fix it up.
  50410. + * XXX this is NOT the way to do things.
  50411. + */
  50412. + if(pfkey_sa->sadb_sa_len == sizeof(struct sadb_sa_v1)/IPSEC_PFKEYv2_ALIGN) {
  50413. +
  50414. + /* yes, so clear out a temporary structure, and copy first */
  50415. + memset(&sav2, 0, sizeof(sav2));
  50416. + memcpy(&sav2, pfkey_sa, sizeof(struct sadb_sa_v1));
  50417. + sav2.sadb_x_sa_ref=-1;
  50418. + sav2.sadb_sa_len = sizeof(struct sadb_sa) / IPSEC_PFKEYv2_ALIGN;
  50419. +
  50420. + pfkey_sa = &sav2;
  50421. + }
  50422. +#endif
  50423. +
  50424. +
  50425. + if(pfkey_sa->sadb_sa_len != sizeof(struct sadb_sa) / IPSEC_PFKEYv2_ALIGN) {
  50426. + ERROR(
  50427. + "pfkey_sa_parse: "
  50428. + "length wrong pfkey_sa->sadb_sa_len=%d sizeof(struct sadb_sa)=%d.\n",
  50429. + pfkey_sa->sadb_sa_len,
  50430. + (int)sizeof(struct sadb_sa));
  50431. + SENDERR(EINVAL);
  50432. + }
  50433. +
  50434. +#if SADB_EALG_MAX < 255
  50435. + if(pfkey_sa->sadb_sa_encrypt > SADB_EALG_MAX) {
  50436. + ERROR(
  50437. + "pfkey_sa_parse: "
  50438. + "pfkey_sa->sadb_sa_encrypt=%d > SADB_EALG_MAX=%d.\n",
  50439. + pfkey_sa->sadb_sa_encrypt,
  50440. + SADB_EALG_MAX);
  50441. + SENDERR(EINVAL);
  50442. + }
  50443. +#endif
  50444. +
  50445. +#if SADB_AALG_MAX < 255
  50446. + if(pfkey_sa->sadb_sa_auth > SADB_AALG_MAX) {
  50447. + ERROR(
  50448. + "pfkey_sa_parse: "
  50449. + "pfkey_sa->sadb_sa_auth=%d > SADB_AALG_MAX=%d.\n",
  50450. + pfkey_sa->sadb_sa_auth,
  50451. + SADB_AALG_MAX);
  50452. + SENDERR(EINVAL);
  50453. + }
  50454. +#endif
  50455. +
  50456. +#if SADB_SASTATE_MAX < 255
  50457. + if(pfkey_sa->sadb_sa_state > SADB_SASTATE_MAX) {
  50458. + ERROR(
  50459. + "pfkey_sa_parse: "
  50460. + "state=%d exceeds MAX=%d.\n",
  50461. + pfkey_sa->sadb_sa_state,
  50462. + SADB_SASTATE_MAX);
  50463. + SENDERR(EINVAL);
  50464. + }
  50465. +#endif
  50466. +
  50467. + if(pfkey_sa->sadb_sa_state == SADB_SASTATE_DEAD) {
  50468. + ERROR(
  50469. + "pfkey_sa_parse: "
  50470. + "state=%d is DEAD=%d.\n",
  50471. + pfkey_sa->sadb_sa_state,
  50472. + SADB_SASTATE_DEAD);
  50473. + SENDERR(EINVAL);
  50474. + }
  50475. +
  50476. + if(pfkey_sa->sadb_sa_replay > 64) {
  50477. + ERROR(
  50478. + "pfkey_sa_parse: "
  50479. + "replay window size: %d -- must be 0 <= size <= 64\n",
  50480. + pfkey_sa->sadb_sa_replay);
  50481. + SENDERR(EINVAL);
  50482. + }
  50483. +
  50484. + if(! ((pfkey_sa->sadb_sa_exttype == SADB_EXT_SA) ||
  50485. + (pfkey_sa->sadb_sa_exttype == SADB_X_EXT_SA2)))
  50486. + {
  50487. + ERROR(
  50488. + "pfkey_sa_parse: "
  50489. + "unknown exttype=%d, expecting SADB_EXT_SA=%d or SADB_X_EXT_SA2=%d.\n",
  50490. + pfkey_sa->sadb_sa_exttype,
  50491. + SADB_EXT_SA,
  50492. + SADB_X_EXT_SA2);
  50493. + SENDERR(EINVAL);
  50494. + }
  50495. +
  50496. + if((IPSEC_SAREF_NULL != pfkey_sa->sadb_x_sa_ref) && (pfkey_sa->sadb_x_sa_ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
  50497. + ERROR(
  50498. + "pfkey_sa_parse: "
  50499. + "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
  50500. + pfkey_sa->sadb_x_sa_ref,
  50501. + IPSEC_SAREF_NULL,
  50502. + IPSEC_SA_REF_TABLE_NUM_ENTRIES);
  50503. + SENDERR(EINVAL);
  50504. + }
  50505. +
  50506. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50507. + "pfkey_sa_parse: "
  50508. + "successfully found len=%d exttype=%d(%s) spi=%08lx replay=%d state=%d auth=%d encrypt=%d flags=%d ref=%d.\n",
  50509. + pfkey_sa->sadb_sa_len,
  50510. + pfkey_sa->sadb_sa_exttype,
  50511. + pfkey_v2_sadb_ext_string(pfkey_sa->sadb_sa_exttype),
  50512. + (long unsigned int)ntohl(pfkey_sa->sadb_sa_spi),
  50513. + pfkey_sa->sadb_sa_replay,
  50514. + pfkey_sa->sadb_sa_state,
  50515. + pfkey_sa->sadb_sa_auth,
  50516. + pfkey_sa->sadb_sa_encrypt,
  50517. + pfkey_sa->sadb_sa_flags,
  50518. + pfkey_sa->sadb_x_sa_ref);
  50519. +
  50520. + errlab:
  50521. + return error;
  50522. +}
  50523. +
  50524. +DEBUG_NO_STATIC int
  50525. +pfkey_lifetime_parse(struct sadb_ext *pfkey_ext)
  50526. +{
  50527. + int error = 0;
  50528. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)pfkey_ext;
  50529. +
  50530. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  50531. + "pfkey_lifetime_parse:enter\n");
  50532. + /* sanity checks... */
  50533. + if(!pfkey_lifetime) {
  50534. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50535. + "pfkey_lifetime_parse: "
  50536. + "NULL pointer passed in.\n");
  50537. + SENDERR(EINVAL);
  50538. + }
  50539. +
  50540. + if(pfkey_lifetime->sadb_lifetime_len !=
  50541. + sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN) {
  50542. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50543. + "pfkey_lifetime_parse: "
  50544. + "length wrong pfkey_lifetime->sadb_lifetime_len=%d sizeof(struct sadb_lifetime)=%d.\n",
  50545. + pfkey_lifetime->sadb_lifetime_len,
  50546. + (int)sizeof(struct sadb_lifetime));
  50547. + SENDERR(EINVAL);
  50548. + }
  50549. +
  50550. + if((pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_HARD) &&
  50551. + (pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_SOFT) &&
  50552. + (pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_CURRENT)) {
  50553. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50554. + "pfkey_lifetime_parse: "
  50555. + "unexpected ext_type=%d.\n",
  50556. + pfkey_lifetime->sadb_lifetime_exttype);
  50557. + SENDERR(EINVAL);
  50558. + }
  50559. +
  50560. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50561. + "pfkey_lifetime_parse: "
  50562. + "life_type=%d(%s) alloc=%u bytes=%u add=%u use=%u pkts=%u.\n",
  50563. + pfkey_lifetime->sadb_lifetime_exttype,
  50564. + pfkey_v2_sadb_ext_string(pfkey_lifetime->sadb_lifetime_exttype),
  50565. + pfkey_lifetime->sadb_lifetime_allocations,
  50566. + (unsigned)pfkey_lifetime->sadb_lifetime_bytes,
  50567. + (unsigned)pfkey_lifetime->sadb_lifetime_addtime,
  50568. + (unsigned)pfkey_lifetime->sadb_lifetime_usetime,
  50569. + pfkey_lifetime->sadb_x_lifetime_packets);
  50570. +errlab:
  50571. + return error;
  50572. +}
  50573. +
  50574. +DEBUG_NO_STATIC int
  50575. +pfkey_address_parse(struct sadb_ext *pfkey_ext)
  50576. +{
  50577. + int error = 0;
  50578. + int saddr_len = 0;
  50579. + struct sadb_address *pfkey_address = (struct sadb_address *)pfkey_ext;
  50580. + struct sockaddr* s = (struct sockaddr*)((char*)pfkey_address + sizeof(*pfkey_address));
  50581. + char ipaddr_txt[ADDRTOT_BUF];
  50582. +
  50583. + /* sanity checks... */
  50584. + if(!pfkey_address) {
  50585. + ERROR(
  50586. + "pfkey_address_parse: "
  50587. + "NULL pointer passed in.\n");
  50588. + SENDERR(EINVAL);
  50589. + }
  50590. +
  50591. + if(pfkey_address->sadb_address_len <
  50592. + (sizeof(struct sadb_address) + sizeof(struct sockaddr))/
  50593. + IPSEC_PFKEYv2_ALIGN) {
  50594. + ERROR("pfkey_address_parse: "
  50595. + "size wrong 1 ext_len=%d, adr_ext_len=%d, saddr_len=%d.\n",
  50596. + pfkey_address->sadb_address_len,
  50597. + (int)sizeof(struct sadb_address),
  50598. + (int)sizeof(struct sockaddr));
  50599. + SENDERR(EINVAL);
  50600. + }
  50601. +
  50602. + if(pfkey_address->sadb_address_reserved) {
  50603. + ERROR("pfkey_address_parse: "
  50604. + "res=%d, must be zero.\n",
  50605. + pfkey_address->sadb_address_reserved);
  50606. + SENDERR(EINVAL);
  50607. + }
  50608. +
  50609. + switch(pfkey_address->sadb_address_exttype) {
  50610. + case SADB_EXT_ADDRESS_SRC:
  50611. + case SADB_EXT_ADDRESS_DST:
  50612. + case SADB_EXT_ADDRESS_PROXY:
  50613. + case SADB_X_EXT_ADDRESS_DST2:
  50614. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  50615. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  50616. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  50617. + case SADB_X_EXT_ADDRESS_DST_MASK:
  50618. +#ifdef NAT_TRAVERSAL
  50619. + case SADB_X_EXT_NAT_T_OA:
  50620. +#endif
  50621. + break;
  50622. + default:
  50623. + ERROR(
  50624. + "pfkey_address_parse: "
  50625. + "unexpected ext_type=%d.\n",
  50626. + pfkey_address->sadb_address_exttype);
  50627. + SENDERR(ENOPKG);
  50628. + }
  50629. +
  50630. + switch(s->sa_family) {
  50631. + case AF_INET:
  50632. + saddr_len = sizeof(struct sockaddr_in);
  50633. + sprintf(ipaddr_txt, "%d.%d.%d.%d"
  50634. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 0) & 0xFF
  50635. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 8) & 0xFF
  50636. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 16) & 0xFF
  50637. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 24) & 0xFF);
  50638. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50639. + "pfkey_address_parse: "
  50640. + "found exttype=%u(%s) family=%d(AF_INET) address=%s proto=%u port=%u.\n",
  50641. + pfkey_address->sadb_address_exttype,
  50642. + pfkey_v2_sadb_ext_string(pfkey_address->sadb_address_exttype),
  50643. + s->sa_family,
  50644. + ipaddr_txt,
  50645. + pfkey_address->sadb_address_proto,
  50646. + ntohs(((struct sockaddr_in*)s)->sin_port));
  50647. + break;
  50648. + case AF_INET6:
  50649. + saddr_len = sizeof(struct sockaddr_in6);
  50650. + sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x"
  50651. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[0])
  50652. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[1])
  50653. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[2])
  50654. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[3])
  50655. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[4])
  50656. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[5])
  50657. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[6])
  50658. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[7]));
  50659. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50660. + "pfkey_address_parse: "
  50661. + "found exttype=%u(%s) family=%d(AF_INET6) address=%s proto=%u port=%u.\n",
  50662. + pfkey_address->sadb_address_exttype,
  50663. + pfkey_v2_sadb_ext_string(pfkey_address->sadb_address_exttype),
  50664. + s->sa_family,
  50665. + ipaddr_txt,
  50666. + pfkey_address->sadb_address_proto,
  50667. + ((struct sockaddr_in6*)s)->sin6_port);
  50668. + break;
  50669. + default:
  50670. + ERROR(
  50671. + "pfkey_address_parse: "
  50672. + "s->sa_family=%d not supported.\n",
  50673. + s->sa_family);
  50674. + SENDERR(EPFNOSUPPORT);
  50675. + }
  50676. +
  50677. + if(pfkey_address->sadb_address_len !=
  50678. + DIVUP(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN)) {
  50679. + ERROR(
  50680. + "pfkey_address_parse: "
  50681. + "size wrong 2 ext_len=%d, adr_ext_len=%d, saddr_len=%d.\n",
  50682. + pfkey_address->sadb_address_len,
  50683. + (int)sizeof(struct sadb_address),
  50684. + saddr_len);
  50685. + SENDERR(EINVAL);
  50686. + }
  50687. +
  50688. + if(pfkey_address->sadb_address_prefixlen != 0) {
  50689. + ERROR(
  50690. + "pfkey_address_parse: "
  50691. + "address prefixes not supported yet.\n");
  50692. + SENDERR(EAFNOSUPPORT); /* not supported yet */
  50693. + }
  50694. +
  50695. + /* XXX check if port!=0 */
  50696. +
  50697. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  50698. + "pfkey_address_parse: successful.\n");
  50699. + errlab:
  50700. + return error;
  50701. +}
  50702. +
  50703. +DEBUG_NO_STATIC int
  50704. +pfkey_key_parse(struct sadb_ext *pfkey_ext)
  50705. +{
  50706. + int error = 0;
  50707. + struct sadb_key *pfkey_key = (struct sadb_key *)pfkey_ext;
  50708. +
  50709. + /* sanity checks... */
  50710. +
  50711. + if(!pfkey_key) {
  50712. + ERROR(
  50713. + "pfkey_key_parse: "
  50714. + "NULL pointer passed in.\n");
  50715. + SENDERR(EINVAL);
  50716. + }
  50717. +
  50718. + if(pfkey_key->sadb_key_len < sizeof(struct sadb_key) / IPSEC_PFKEYv2_ALIGN) {
  50719. + ERROR(
  50720. + "pfkey_key_parse: "
  50721. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50722. + pfkey_key->sadb_key_len,
  50723. + (int)sizeof(struct sadb_key));
  50724. + SENDERR(EINVAL);
  50725. + }
  50726. +
  50727. + if(!pfkey_key->sadb_key_bits) {
  50728. + ERROR(
  50729. + "pfkey_key_parse: "
  50730. + "key length set to zero, must be non-zero.\n");
  50731. + SENDERR(EINVAL);
  50732. + }
  50733. +
  50734. + if(pfkey_key->sadb_key_len !=
  50735. + DIVUP(sizeof(struct sadb_key) * OCTETBITS + pfkey_key->sadb_key_bits,
  50736. + PFKEYBITS)) {
  50737. + ERROR(
  50738. + "pfkey_key_parse: "
  50739. + "key length=%d does not agree with extension length=%d.\n",
  50740. + pfkey_key->sadb_key_bits,
  50741. + pfkey_key->sadb_key_len);
  50742. + SENDERR(EINVAL);
  50743. + }
  50744. +
  50745. + if(pfkey_key->sadb_key_reserved) {
  50746. + ERROR(
  50747. + "pfkey_key_parse: "
  50748. + "res=%d, must be zero.\n",
  50749. + pfkey_key->sadb_key_reserved);
  50750. + SENDERR(EINVAL);
  50751. + }
  50752. +
  50753. + if(! ( (pfkey_key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ||
  50754. + (pfkey_key->sadb_key_exttype == SADB_EXT_KEY_ENCRYPT))) {
  50755. + ERROR(
  50756. + "pfkey_key_parse: "
  50757. + "expecting extension type AUTH or ENCRYPT, got %d.\n",
  50758. + pfkey_key->sadb_key_exttype);
  50759. + SENDERR(EINVAL);
  50760. + }
  50761. +
  50762. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50763. + "pfkey_key_parse: "
  50764. + "success, found len=%d exttype=%d(%s) bits=%d reserved=%d.\n",
  50765. + pfkey_key->sadb_key_len,
  50766. + pfkey_key->sadb_key_exttype,
  50767. + pfkey_v2_sadb_ext_string(pfkey_key->sadb_key_exttype),
  50768. + pfkey_key->sadb_key_bits,
  50769. + pfkey_key->sadb_key_reserved);
  50770. +
  50771. +errlab:
  50772. + return error;
  50773. +}
  50774. +
  50775. +DEBUG_NO_STATIC int
  50776. +pfkey_ident_parse(struct sadb_ext *pfkey_ext)
  50777. +{
  50778. + int error = 0;
  50779. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)pfkey_ext;
  50780. +
  50781. + /* sanity checks... */
  50782. + if(pfkey_ident->sadb_ident_len < sizeof(struct sadb_ident) / IPSEC_PFKEYv2_ALIGN) {
  50783. + ERROR(
  50784. + "pfkey_ident_parse: "
  50785. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50786. + pfkey_ident->sadb_ident_len,
  50787. + (int)sizeof(struct sadb_ident));
  50788. + SENDERR(EINVAL);
  50789. + }
  50790. +
  50791. + if(pfkey_ident->sadb_ident_type > SADB_IDENTTYPE_MAX) {
  50792. + ERROR(
  50793. + "pfkey_ident_parse: "
  50794. + "ident_type=%d out of range, must be less than %d.\n",
  50795. + pfkey_ident->sadb_ident_type,
  50796. + SADB_IDENTTYPE_MAX);
  50797. + SENDERR(EINVAL);
  50798. + }
  50799. +
  50800. + if(pfkey_ident->sadb_ident_reserved) {
  50801. + ERROR(
  50802. + "pfkey_ident_parse: "
  50803. + "res=%d, must be zero.\n",
  50804. + pfkey_ident->sadb_ident_reserved);
  50805. + SENDERR(EINVAL);
  50806. + }
  50807. +
  50808. + /* string terminator/padding must be zero */
  50809. + if(pfkey_ident->sadb_ident_len > sizeof(struct sadb_ident) / IPSEC_PFKEYv2_ALIGN) {
  50810. + if(*((char*)pfkey_ident + pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - 1)) {
  50811. + ERROR(
  50812. + "pfkey_ident_parse: "
  50813. + "string padding must be zero, last is 0x%02x.\n",
  50814. + *((char*)pfkey_ident +
  50815. + pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - 1));
  50816. + SENDERR(EINVAL);
  50817. + }
  50818. + }
  50819. +
  50820. + if( ! ((pfkey_ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ||
  50821. + (pfkey_ident->sadb_ident_exttype == SADB_EXT_IDENTITY_DST))) {
  50822. + ERROR(
  50823. + "pfkey_key_parse: "
  50824. + "expecting extension type IDENTITY_SRC or IDENTITY_DST, got %d.\n",
  50825. + pfkey_ident->sadb_ident_exttype);
  50826. + SENDERR(EINVAL);
  50827. + }
  50828. +
  50829. +errlab:
  50830. + return error;
  50831. +}
  50832. +
  50833. +DEBUG_NO_STATIC int
  50834. +pfkey_sens_parse(struct sadb_ext *pfkey_ext)
  50835. +{
  50836. + int error = 0;
  50837. + struct sadb_sens *pfkey_sens = (struct sadb_sens *)pfkey_ext;
  50838. +
  50839. + /* sanity checks... */
  50840. + if(pfkey_sens->sadb_sens_len < sizeof(struct sadb_sens) / IPSEC_PFKEYv2_ALIGN) {
  50841. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50842. + "pfkey_sens_parse: "
  50843. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50844. + pfkey_sens->sadb_sens_len,
  50845. + (int)sizeof(struct sadb_sens));
  50846. + SENDERR(EINVAL);
  50847. + }
  50848. +
  50849. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50850. + "pfkey_sens_parse: "
  50851. + "Sorry, I can't parse exttype=%d yet.\n",
  50852. + pfkey_ext->sadb_ext_type);
  50853. +#if 0
  50854. + SENDERR(EINVAL); /* don't process these yet */
  50855. +#endif
  50856. +
  50857. +errlab:
  50858. + return error;
  50859. +}
  50860. +
  50861. +DEBUG_NO_STATIC int
  50862. +pfkey_prop_parse(struct sadb_ext *pfkey_ext)
  50863. +{
  50864. + int error = 0;
  50865. + int i, num_comb;
  50866. + struct sadb_prop *pfkey_prop = (struct sadb_prop *)pfkey_ext;
  50867. + struct sadb_comb *pfkey_comb = (struct sadb_comb *)((char*)pfkey_ext + sizeof(struct sadb_prop));
  50868. +
  50869. + /* sanity checks... */
  50870. + if((pfkey_prop->sadb_prop_len < sizeof(struct sadb_prop) / IPSEC_PFKEYv2_ALIGN) ||
  50871. + (((pfkey_prop->sadb_prop_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_prop)) % sizeof(struct sadb_comb))) {
  50872. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50873. + "pfkey_prop_parse: "
  50874. + "size wrong ext_len=%d, prop_ext_len=%d comb_ext_len=%d.\n",
  50875. + pfkey_prop->sadb_prop_len,
  50876. + (int)sizeof(struct sadb_prop),
  50877. + (int)sizeof(struct sadb_comb));
  50878. + SENDERR(EINVAL);
  50879. + }
  50880. +
  50881. + if(pfkey_prop->sadb_prop_replay > 64) {
  50882. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50883. + "pfkey_prop_parse: "
  50884. + "replay window size: %d -- must be 0 <= size <= 64\n",
  50885. + pfkey_prop->sadb_prop_replay);
  50886. + SENDERR(EINVAL);
  50887. + }
  50888. +
  50889. + for(i=0; i<3; i++) {
  50890. + if(pfkey_prop->sadb_prop_reserved[i]) {
  50891. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50892. + "pfkey_prop_parse: "
  50893. + "res[%d]=%d, must be zero.\n",
  50894. + i, pfkey_prop->sadb_prop_reserved[i]);
  50895. + SENDERR(EINVAL);
  50896. + }
  50897. + }
  50898. +
  50899. + num_comb = ((pfkey_prop->sadb_prop_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_prop)) / sizeof(struct sadb_comb);
  50900. +
  50901. + for(i = 0; i < num_comb; i++) {
  50902. + if(pfkey_comb->sadb_comb_auth > SADB_AALG_MAX) {
  50903. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50904. + "pfkey_prop_parse: "
  50905. + "pfkey_comb[%d]->sadb_comb_auth=%d > SADB_AALG_MAX=%d.\n",
  50906. + i,
  50907. + pfkey_comb->sadb_comb_auth,
  50908. + SADB_AALG_MAX);
  50909. + SENDERR(EINVAL);
  50910. + }
  50911. +
  50912. + if(pfkey_comb->sadb_comb_auth) {
  50913. + if(!pfkey_comb->sadb_comb_auth_minbits) {
  50914. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50915. + "pfkey_prop_parse: "
  50916. + "pfkey_comb[%d]->sadb_comb_auth_minbits=0, fatal.\n",
  50917. + i);
  50918. + SENDERR(EINVAL);
  50919. + }
  50920. + if(!pfkey_comb->sadb_comb_auth_maxbits) {
  50921. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50922. + "pfkey_prop_parse: "
  50923. + "pfkey_comb[%d]->sadb_comb_auth_maxbits=0, fatal.\n",
  50924. + i);
  50925. + SENDERR(EINVAL);
  50926. + }
  50927. + if(pfkey_comb->sadb_comb_auth_minbits > pfkey_comb->sadb_comb_auth_maxbits) {
  50928. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50929. + "pfkey_prop_parse: "
  50930. + "pfkey_comb[%d]->sadb_comb_auth_minbits=%d > maxbits=%d, fatal.\n",
  50931. + i,
  50932. + pfkey_comb->sadb_comb_auth_minbits,
  50933. + pfkey_comb->sadb_comb_auth_maxbits);
  50934. + SENDERR(EINVAL);
  50935. + }
  50936. + } else {
  50937. + if(pfkey_comb->sadb_comb_auth_minbits) {
  50938. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50939. + "pfkey_prop_parse: "
  50940. + "pfkey_comb[%d]->sadb_comb_auth_minbits=%d != 0, fatal.\n",
  50941. + i,
  50942. + pfkey_comb->sadb_comb_auth_minbits);
  50943. + SENDERR(EINVAL);
  50944. + }
  50945. + if(pfkey_comb->sadb_comb_auth_maxbits) {
  50946. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50947. + "pfkey_prop_parse: "
  50948. + "pfkey_comb[%d]->sadb_comb_auth_maxbits=%d != 0, fatal.\n",
  50949. + i,
  50950. + pfkey_comb->sadb_comb_auth_maxbits);
  50951. + SENDERR(EINVAL);
  50952. + }
  50953. + }
  50954. +
  50955. +#if SADB_EALG_MAX < 255
  50956. + if(pfkey_comb->sadb_comb_encrypt > SADB_EALG_MAX) {
  50957. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50958. + "pfkey_comb_parse: "
  50959. + "pfkey_comb[%d]->sadb_comb_encrypt=%d > SADB_EALG_MAX=%d.\n",
  50960. + i,
  50961. + pfkey_comb->sadb_comb_encrypt,
  50962. + SADB_EALG_MAX);
  50963. + SENDERR(EINVAL);
  50964. + }
  50965. +#endif
  50966. +
  50967. + if(pfkey_comb->sadb_comb_encrypt) {
  50968. + if(!pfkey_comb->sadb_comb_encrypt_minbits) {
  50969. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50970. + "pfkey_prop_parse: "
  50971. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=0, fatal.\n",
  50972. + i);
  50973. + SENDERR(EINVAL);
  50974. + }
  50975. + if(!pfkey_comb->sadb_comb_encrypt_maxbits) {
  50976. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50977. + "pfkey_prop_parse: "
  50978. + "pfkey_comb[%d]->sadb_comb_encrypt_maxbits=0, fatal.\n",
  50979. + i);
  50980. + SENDERR(EINVAL);
  50981. + }
  50982. + if(pfkey_comb->sadb_comb_encrypt_minbits > pfkey_comb->sadb_comb_encrypt_maxbits) {
  50983. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50984. + "pfkey_prop_parse: "
  50985. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=%d > maxbits=%d, fatal.\n",
  50986. + i,
  50987. + pfkey_comb->sadb_comb_encrypt_minbits,
  50988. + pfkey_comb->sadb_comb_encrypt_maxbits);
  50989. + SENDERR(EINVAL);
  50990. + }
  50991. + } else {
  50992. + if(pfkey_comb->sadb_comb_encrypt_minbits) {
  50993. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50994. + "pfkey_prop_parse: "
  50995. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=%d != 0, fatal.\n",
  50996. + i,
  50997. + pfkey_comb->sadb_comb_encrypt_minbits);
  50998. + SENDERR(EINVAL);
  50999. + }
  51000. + if(pfkey_comb->sadb_comb_encrypt_maxbits) {
  51001. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51002. + "pfkey_prop_parse: "
  51003. + "pfkey_comb[%d]->sadb_comb_encrypt_maxbits=%d != 0, fatal.\n",
  51004. + i,
  51005. + pfkey_comb->sadb_comb_encrypt_maxbits);
  51006. + SENDERR(EINVAL);
  51007. + }
  51008. + }
  51009. +
  51010. + /* XXX do sanity check on flags */
  51011. +
  51012. + if(pfkey_comb->sadb_comb_hard_allocations && pfkey_comb->sadb_comb_soft_allocations > pfkey_comb->sadb_comb_hard_allocations) {
  51013. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51014. + "pfkey_prop_parse: "
  51015. + "pfkey_comb[%d]->sadb_comb_soft_allocations=%d > hard_allocations=%d, fatal.\n",
  51016. + i,
  51017. + pfkey_comb->sadb_comb_soft_allocations,
  51018. + pfkey_comb->sadb_comb_hard_allocations);
  51019. + SENDERR(EINVAL);
  51020. + }
  51021. +
  51022. + if(pfkey_comb->sadb_comb_hard_bytes && pfkey_comb->sadb_comb_soft_bytes > pfkey_comb->sadb_comb_hard_bytes) {
  51023. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51024. + "pfkey_prop_parse: "
  51025. + "pfkey_comb[%d]->sadb_comb_soft_bytes=%Ld > hard_bytes=%Ld, fatal.\n",
  51026. + i,
  51027. + (unsigned long long int)pfkey_comb->sadb_comb_soft_bytes,
  51028. + (unsigned long long int)pfkey_comb->sadb_comb_hard_bytes);
  51029. + SENDERR(EINVAL);
  51030. + }
  51031. +
  51032. + if(pfkey_comb->sadb_comb_hard_addtime && pfkey_comb->sadb_comb_soft_addtime > pfkey_comb->sadb_comb_hard_addtime) {
  51033. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51034. + "pfkey_prop_parse: "
  51035. + "pfkey_comb[%d]->sadb_comb_soft_addtime=%Ld > hard_addtime=%Ld, fatal.\n",
  51036. + i,
  51037. + (unsigned long long int)pfkey_comb->sadb_comb_soft_addtime,
  51038. + (unsigned long long int)pfkey_comb->sadb_comb_hard_addtime);
  51039. + SENDERR(EINVAL);
  51040. + }
  51041. +
  51042. + if(pfkey_comb->sadb_comb_hard_usetime && pfkey_comb->sadb_comb_soft_usetime > pfkey_comb->sadb_comb_hard_usetime) {
  51043. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51044. + "pfkey_prop_parse: "
  51045. + "pfkey_comb[%d]->sadb_comb_soft_usetime=%Ld > hard_usetime=%Ld, fatal.\n",
  51046. + i,
  51047. + (unsigned long long int)pfkey_comb->sadb_comb_soft_usetime,
  51048. + (unsigned long long int)pfkey_comb->sadb_comb_hard_usetime);
  51049. + SENDERR(EINVAL);
  51050. + }
  51051. +
  51052. + if(pfkey_comb->sadb_x_comb_hard_packets && pfkey_comb->sadb_x_comb_soft_packets > pfkey_comb->sadb_x_comb_hard_packets) {
  51053. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51054. + "pfkey_prop_parse: "
  51055. + "pfkey_comb[%d]->sadb_x_comb_soft_packets=%d > hard_packets=%d, fatal.\n",
  51056. + i,
  51057. + pfkey_comb->sadb_x_comb_soft_packets,
  51058. + pfkey_comb->sadb_x_comb_hard_packets);
  51059. + SENDERR(EINVAL);
  51060. + }
  51061. +
  51062. + if(pfkey_comb->sadb_comb_reserved) {
  51063. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51064. + "pfkey_prop_parse: "
  51065. + "comb[%d].res=%d, must be zero.\n",
  51066. + i,
  51067. + pfkey_comb->sadb_comb_reserved);
  51068. + SENDERR(EINVAL);
  51069. + }
  51070. + pfkey_comb++;
  51071. + }
  51072. +
  51073. +errlab:
  51074. + return error;
  51075. +}
  51076. +
  51077. +DEBUG_NO_STATIC int
  51078. +pfkey_supported_parse(struct sadb_ext *pfkey_ext)
  51079. +{
  51080. + int error = 0;
  51081. + unsigned int i, num_alg;
  51082. + struct sadb_supported *pfkey_supported = (struct sadb_supported *)pfkey_ext;
  51083. + struct sadb_alg *pfkey_alg = (struct sadb_alg*)((char*)pfkey_ext + sizeof(struct sadb_supported));
  51084. +
  51085. + /* sanity checks... */
  51086. + if((pfkey_supported->sadb_supported_len <
  51087. + sizeof(struct sadb_supported) / IPSEC_PFKEYv2_ALIGN) ||
  51088. + (((pfkey_supported->sadb_supported_len * IPSEC_PFKEYv2_ALIGN) -
  51089. + sizeof(struct sadb_supported)) % sizeof(struct sadb_alg))) {
  51090. +
  51091. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51092. + "pfkey_supported_parse: "
  51093. + "size wrong ext_len=%d, supported_ext_len=%d alg_ext_len=%d.\n",
  51094. + pfkey_supported->sadb_supported_len,
  51095. + (int)sizeof(struct sadb_supported),
  51096. + (int)sizeof(struct sadb_alg));
  51097. + SENDERR(EINVAL);
  51098. + }
  51099. +
  51100. + if(pfkey_supported->sadb_supported_reserved) {
  51101. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51102. + "pfkey_supported_parse: "
  51103. + "res=%d, must be zero.\n",
  51104. + pfkey_supported->sadb_supported_reserved);
  51105. + SENDERR(EINVAL);
  51106. + }
  51107. +
  51108. + num_alg = ((pfkey_supported->sadb_supported_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_supported)) / sizeof(struct sadb_alg);
  51109. +
  51110. + for(i = 0; i < num_alg; i++) {
  51111. + /* process algo description */
  51112. + if(pfkey_alg->sadb_alg_reserved) {
  51113. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51114. + "pfkey_supported_parse: "
  51115. + "alg[%d], id=%d, ivlen=%d, minbits=%d, maxbits=%d, res=%d, must be zero.\n",
  51116. + i,
  51117. + pfkey_alg->sadb_alg_id,
  51118. + pfkey_alg->sadb_alg_ivlen,
  51119. + pfkey_alg->sadb_alg_minbits,
  51120. + pfkey_alg->sadb_alg_maxbits,
  51121. + pfkey_alg->sadb_alg_reserved);
  51122. + SENDERR(EINVAL);
  51123. + }
  51124. +
  51125. + /* XXX can alg_id auth/enc be determined from info given?
  51126. + Yes, but OpenBSD's method does not iteroperate with rfc2367.
  51127. + rgb, 2000-04-06 */
  51128. +
  51129. + switch(pfkey_supported->sadb_supported_exttype) {
  51130. + case SADB_EXT_SUPPORTED_AUTH:
  51131. + if(pfkey_alg->sadb_alg_id > SADB_AALG_MAX) {
  51132. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51133. + "pfkey_supported_parse: "
  51134. + "alg[%d], alg_id=%d > SADB_AALG_MAX=%d, fatal.\n",
  51135. + i,
  51136. + pfkey_alg->sadb_alg_id,
  51137. + SADB_AALG_MAX);
  51138. + SENDERR(EINVAL);
  51139. + }
  51140. + break;
  51141. + case SADB_EXT_SUPPORTED_ENCRYPT:
  51142. +#if SADB_EALG_MAX < 255
  51143. + if(pfkey_alg->sadb_alg_id > SADB_EALG_MAX) {
  51144. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51145. + "pfkey_supported_parse: "
  51146. + "alg[%d], alg_id=%d > SADB_EALG_MAX=%d, fatal.\n",
  51147. + i,
  51148. + pfkey_alg->sadb_alg_id,
  51149. + SADB_EALG_MAX);
  51150. + SENDERR(EINVAL);
  51151. + }
  51152. +#endif
  51153. + break;
  51154. + default:
  51155. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51156. + "pfkey_supported_parse: "
  51157. + "alg[%d], alg_id=%d > SADB_EALG_MAX=%d, fatal.\n",
  51158. + i,
  51159. + pfkey_alg->sadb_alg_id,
  51160. + SADB_EALG_MAX);
  51161. + SENDERR(EINVAL);
  51162. + }
  51163. + pfkey_alg++;
  51164. + }
  51165. +
  51166. + errlab:
  51167. + return error;
  51168. +}
  51169. +
  51170. +DEBUG_NO_STATIC int
  51171. +pfkey_spirange_parse(struct sadb_ext *pfkey_ext)
  51172. +{
  51173. + int error = 0;
  51174. + struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)pfkey_ext;
  51175. +
  51176. + /* sanity checks... */
  51177. + if(pfkey_spirange->sadb_spirange_len !=
  51178. + sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN) {
  51179. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51180. + "pfkey_spirange_parse: "
  51181. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51182. + pfkey_spirange->sadb_spirange_len,
  51183. + (int)sizeof(struct sadb_spirange));
  51184. + SENDERR(EINVAL);
  51185. + }
  51186. +
  51187. + if(pfkey_spirange->sadb_spirange_reserved) {
  51188. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51189. + "pfkey_spirange_parse: "
  51190. + "reserved=%d must be set to zero.\n",
  51191. + pfkey_spirange->sadb_spirange_reserved);
  51192. + SENDERR(EINVAL);
  51193. + }
  51194. +
  51195. + if(ntohl(pfkey_spirange->sadb_spirange_max) < ntohl(pfkey_spirange->sadb_spirange_min)) {
  51196. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51197. + "pfkey_spirange_parse: "
  51198. + "minspi=%08x must be < maxspi=%08x.\n",
  51199. + ntohl(pfkey_spirange->sadb_spirange_min),
  51200. + ntohl(pfkey_spirange->sadb_spirange_max));
  51201. + SENDERR(EINVAL);
  51202. + }
  51203. +
  51204. + if(ntohl(pfkey_spirange->sadb_spirange_min) <= 255) {
  51205. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51206. + "pfkey_spirange_parse: "
  51207. + "minspi=%08x must be > 255.\n",
  51208. + ntohl(pfkey_spirange->sadb_spirange_min));
  51209. + SENDERR(EEXIST);
  51210. + }
  51211. +
  51212. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51213. + "pfkey_spirange_parse: "
  51214. + "ext_len=%u ext_type=%u(%s) min=%u max=%u res=%u.\n",
  51215. + pfkey_spirange->sadb_spirange_len,
  51216. + pfkey_spirange->sadb_spirange_exttype,
  51217. + pfkey_v2_sadb_ext_string(pfkey_spirange->sadb_spirange_exttype),
  51218. + pfkey_spirange->sadb_spirange_min,
  51219. + pfkey_spirange->sadb_spirange_max,
  51220. + pfkey_spirange->sadb_spirange_reserved);
  51221. + errlab:
  51222. + return error;
  51223. +}
  51224. +
  51225. +DEBUG_NO_STATIC int
  51226. +pfkey_x_kmprivate_parse(struct sadb_ext *pfkey_ext)
  51227. +{
  51228. + int error = 0;
  51229. + struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)pfkey_ext;
  51230. +
  51231. + /* sanity checks... */
  51232. + if(pfkey_x_kmprivate->sadb_x_kmprivate_len <
  51233. + sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN) {
  51234. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51235. + "pfkey_x_kmprivate_parse: "
  51236. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51237. + pfkey_x_kmprivate->sadb_x_kmprivate_len,
  51238. + (int)sizeof(struct sadb_x_kmprivate));
  51239. + SENDERR(EINVAL);
  51240. + }
  51241. +
  51242. + if(pfkey_x_kmprivate->sadb_x_kmprivate_reserved) {
  51243. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51244. + "pfkey_x_kmprivate_parse: "
  51245. + "reserved=%d must be set to zero.\n",
  51246. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved);
  51247. + SENDERR(EINVAL);
  51248. + }
  51249. +
  51250. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51251. + "pfkey_x_kmprivate_parse: "
  51252. + "Sorry, I can't parse exttype=%d yet.\n",
  51253. + pfkey_ext->sadb_ext_type);
  51254. + SENDERR(EINVAL); /* don't process these yet */
  51255. +
  51256. +errlab:
  51257. + return error;
  51258. +}
  51259. +
  51260. +DEBUG_NO_STATIC int
  51261. +pfkey_x_satype_parse(struct sadb_ext *pfkey_ext)
  51262. +{
  51263. + int error = 0;
  51264. + int i;
  51265. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)pfkey_ext;
  51266. +
  51267. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51268. + "pfkey_x_satype_parse: enter\n");
  51269. + /* sanity checks... */
  51270. + if(pfkey_x_satype->sadb_x_satype_len !=
  51271. + sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN) {
  51272. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51273. + "pfkey_x_satype_parse: "
  51274. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51275. + pfkey_x_satype->sadb_x_satype_len,
  51276. + (int)sizeof(struct sadb_x_satype));
  51277. + SENDERR(EINVAL);
  51278. + }
  51279. +
  51280. + if(!pfkey_x_satype->sadb_x_satype_satype) {
  51281. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51282. + "pfkey_x_satype_parse: "
  51283. + "satype is zero, must be non-zero.\n");
  51284. + SENDERR(EINVAL);
  51285. + }
  51286. +
  51287. + if(pfkey_x_satype->sadb_x_satype_satype > SADB_SATYPE_MAX) {
  51288. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51289. + "pfkey_x_satype_parse: "
  51290. + "satype %d > max %d, invalid.\n",
  51291. + pfkey_x_satype->sadb_x_satype_satype, SADB_SATYPE_MAX);
  51292. + SENDERR(EINVAL);
  51293. + }
  51294. +
  51295. + if(!(satype2proto(pfkey_x_satype->sadb_x_satype_satype))) {
  51296. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51297. + "pfkey_x_satype_parse: "
  51298. + "proto lookup from satype=%d failed.\n",
  51299. + pfkey_x_satype->sadb_x_satype_satype);
  51300. + SENDERR(EINVAL);
  51301. + }
  51302. +
  51303. + for(i = 0; i < 3; i++) {
  51304. + if(pfkey_x_satype->sadb_x_satype_reserved[i]) {
  51305. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51306. + "pfkey_x_satype_parse: "
  51307. + "reserved[%d]=%d must be set to zero.\n",
  51308. + i, pfkey_x_satype->sadb_x_satype_reserved[i]);
  51309. + SENDERR(EINVAL);
  51310. + }
  51311. + }
  51312. +
  51313. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51314. + "pfkey_x_satype_parse: "
  51315. + "len=%u ext=%u(%s) satype=%u(%s) res=%u,%u,%u.\n",
  51316. + pfkey_x_satype->sadb_x_satype_len,
  51317. + pfkey_x_satype->sadb_x_satype_exttype,
  51318. + pfkey_v2_sadb_ext_string(pfkey_x_satype->sadb_x_satype_exttype),
  51319. + pfkey_x_satype->sadb_x_satype_satype,
  51320. + satype2name(pfkey_x_satype->sadb_x_satype_satype),
  51321. + pfkey_x_satype->sadb_x_satype_reserved[0],
  51322. + pfkey_x_satype->sadb_x_satype_reserved[1],
  51323. + pfkey_x_satype->sadb_x_satype_reserved[2]);
  51324. +errlab:
  51325. + return error;
  51326. +}
  51327. +
  51328. +DEBUG_NO_STATIC int
  51329. +pfkey_x_ext_debug_parse(struct sadb_ext *pfkey_ext)
  51330. +{
  51331. + int error = 0;
  51332. + int i;
  51333. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)pfkey_ext;
  51334. +
  51335. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51336. + "pfkey_x_debug_parse: enter\n");
  51337. + /* sanity checks... */
  51338. + if(pfkey_x_debug->sadb_x_debug_len !=
  51339. + sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN) {
  51340. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51341. + "pfkey_x_debug_parse: "
  51342. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51343. + pfkey_x_debug->sadb_x_debug_len,
  51344. + (int)sizeof(struct sadb_x_debug));
  51345. + SENDERR(EINVAL);
  51346. + }
  51347. +
  51348. + for(i = 0; i < 4; i++) {
  51349. + if(pfkey_x_debug->sadb_x_debug_reserved[i]) {
  51350. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51351. + "pfkey_x_debug_parse: "
  51352. + "reserved[%d]=%d must be set to zero.\n",
  51353. + i, pfkey_x_debug->sadb_x_debug_reserved[i]);
  51354. + SENDERR(EINVAL);
  51355. + }
  51356. + }
  51357. +
  51358. +errlab:
  51359. + return error;
  51360. +}
  51361. +
  51362. +DEBUG_NO_STATIC int
  51363. +pfkey_x_ext_protocol_parse(struct sadb_ext *pfkey_ext)
  51364. +{
  51365. + int error = 0;
  51366. + struct sadb_protocol *p = (struct sadb_protocol *)pfkey_ext;
  51367. +
  51368. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM, "pfkey_x_protocol_parse:\n");
  51369. + /* sanity checks... */
  51370. +
  51371. + if (p->sadb_protocol_len != sizeof(*p)/IPSEC_PFKEYv2_ALIGN) {
  51372. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51373. + "pfkey_x_protocol_parse: size wrong ext_len=%d, key_ext_len=%d.\n",
  51374. + p->sadb_protocol_len, (int)sizeof(*p));
  51375. + SENDERR(EINVAL);
  51376. + }
  51377. +
  51378. + if (p->sadb_protocol_reserved2 != 0) {
  51379. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51380. + "pfkey_protocol_parse: res=%d, must be zero.\n",
  51381. + p->sadb_protocol_reserved2);
  51382. + SENDERR(EINVAL);
  51383. + }
  51384. +
  51385. + errlab:
  51386. + return error;
  51387. +}
  51388. +
  51389. +#ifdef NAT_TRAVERSAL
  51390. +DEBUG_NO_STATIC int
  51391. +pfkey_x_ext_nat_t_type_parse(struct sadb_ext *pfkey_ext)
  51392. +{
  51393. + return 0;
  51394. +}
  51395. +DEBUG_NO_STATIC int
  51396. +pfkey_x_ext_nat_t_port_parse(struct sadb_ext *pfkey_ext)
  51397. +{
  51398. + return 0;
  51399. +}
  51400. +#endif
  51401. +
  51402. +#define DEFINEPARSER(NAME) static struct pf_key_ext_parsers_def NAME##_def={NAME, #NAME};
  51403. +
  51404. +DEFINEPARSER(pfkey_sa_parse);
  51405. +DEFINEPARSER(pfkey_lifetime_parse);
  51406. +DEFINEPARSER(pfkey_address_parse);
  51407. +DEFINEPARSER(pfkey_key_parse);
  51408. +DEFINEPARSER(pfkey_ident_parse);
  51409. +DEFINEPARSER(pfkey_sens_parse);
  51410. +DEFINEPARSER(pfkey_prop_parse);
  51411. +DEFINEPARSER(pfkey_supported_parse);
  51412. +DEFINEPARSER(pfkey_spirange_parse);
  51413. +DEFINEPARSER(pfkey_x_kmprivate_parse);
  51414. +DEFINEPARSER(pfkey_x_satype_parse);
  51415. +DEFINEPARSER(pfkey_x_ext_debug_parse);
  51416. +DEFINEPARSER(pfkey_x_ext_protocol_parse);
  51417. +#ifdef NAT_TRAVERSAL
  51418. +DEFINEPARSER(pfkey_x_ext_nat_t_type_parse);
  51419. +DEFINEPARSER(pfkey_x_ext_nat_t_port_parse);
  51420. +#endif
  51421. +
  51422. +struct pf_key_ext_parsers_def *ext_default_parsers[]=
  51423. +{
  51424. + NULL, /* pfkey_msg_parse, */
  51425. + &pfkey_sa_parse_def,
  51426. + &pfkey_lifetime_parse_def,
  51427. + &pfkey_lifetime_parse_def,
  51428. + &pfkey_lifetime_parse_def,
  51429. + &pfkey_address_parse_def,
  51430. + &pfkey_address_parse_def,
  51431. + &pfkey_address_parse_def,
  51432. + &pfkey_key_parse_def,
  51433. + &pfkey_key_parse_def,
  51434. + &pfkey_ident_parse_def,
  51435. + &pfkey_ident_parse_def,
  51436. + &pfkey_sens_parse_def,
  51437. + &pfkey_prop_parse_def,
  51438. + &pfkey_supported_parse_def,
  51439. + &pfkey_supported_parse_def,
  51440. + &pfkey_spirange_parse_def,
  51441. + &pfkey_x_kmprivate_parse_def,
  51442. + &pfkey_x_satype_parse_def,
  51443. + &pfkey_sa_parse_def,
  51444. + &pfkey_address_parse_def,
  51445. + &pfkey_address_parse_def,
  51446. + &pfkey_address_parse_def,
  51447. + &pfkey_address_parse_def,
  51448. + &pfkey_address_parse_def,
  51449. + &pfkey_x_ext_debug_parse_def,
  51450. + &pfkey_x_ext_protocol_parse_def
  51451. +#ifdef NAT_TRAVERSAL
  51452. + ,
  51453. + &pfkey_x_ext_nat_t_type_parse_def,
  51454. + &pfkey_x_ext_nat_t_port_parse_def,
  51455. + &pfkey_x_ext_nat_t_port_parse_def,
  51456. + &pfkey_address_parse_def
  51457. +#endif
  51458. +};
  51459. +
  51460. +int
  51461. +pfkey_msg_parse(struct sadb_msg *pfkey_msg,
  51462. + struct pf_key_ext_parsers_def *ext_parsers[],
  51463. + struct sadb_ext *extensions[],
  51464. + int dir)
  51465. +{
  51466. + int error = 0;
  51467. + int remain;
  51468. + struct sadb_ext *pfkey_ext;
  51469. + int extensions_seen = 0;
  51470. +
  51471. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51472. + "pfkey_msg_parse: "
  51473. + "parsing message ver=%d, type=%d(%s), errno=%d, satype=%d(%s), len=%d, res=%d, seq=%d, pid=%d.\n",
  51474. + pfkey_msg->sadb_msg_version,
  51475. + pfkey_msg->sadb_msg_type,
  51476. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type),
  51477. + pfkey_msg->sadb_msg_errno,
  51478. + pfkey_msg->sadb_msg_satype,
  51479. + satype2name(pfkey_msg->sadb_msg_satype),
  51480. + pfkey_msg->sadb_msg_len,
  51481. + pfkey_msg->sadb_msg_reserved,
  51482. + pfkey_msg->sadb_msg_seq,
  51483. + pfkey_msg->sadb_msg_pid);
  51484. +
  51485. + if(ext_parsers == NULL) ext_parsers = ext_default_parsers;
  51486. +
  51487. + pfkey_extensions_init(extensions);
  51488. +
  51489. + remain = pfkey_msg->sadb_msg_len;
  51490. + remain -= sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  51491. +
  51492. + pfkey_ext = (struct sadb_ext*)((char*)pfkey_msg +
  51493. + sizeof(struct sadb_msg));
  51494. +
  51495. + extensions[0] = (struct sadb_ext *) pfkey_msg;
  51496. +
  51497. +
  51498. + if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {
  51499. + ERROR("pfkey_msg_parse: "
  51500. + "not PF_KEY_V2 msg, found %d, should be %d.\n",
  51501. + pfkey_msg->sadb_msg_version,
  51502. + PF_KEY_V2);
  51503. + SENDERR(EINVAL);
  51504. + }
  51505. +
  51506. + if(!pfkey_msg->sadb_msg_type) {
  51507. + ERROR("pfkey_msg_parse: "
  51508. + "msg type not set, must be non-zero..\n");
  51509. + SENDERR(EINVAL);
  51510. + }
  51511. +
  51512. + if(pfkey_msg->sadb_msg_type > SADB_MAX) {
  51513. + ERROR("pfkey_msg_parse: "
  51514. + "msg type=%d > max=%d.\n",
  51515. + pfkey_msg->sadb_msg_type,
  51516. + SADB_MAX);
  51517. + SENDERR(EINVAL);
  51518. + }
  51519. +
  51520. + switch(pfkey_msg->sadb_msg_type) {
  51521. + case SADB_GETSPI:
  51522. + case SADB_UPDATE:
  51523. + case SADB_ADD:
  51524. + case SADB_DELETE:
  51525. + case SADB_GET:
  51526. + case SADB_X_GRPSA:
  51527. + case SADB_X_ADDFLOW:
  51528. + if(!satype2proto(pfkey_msg->sadb_msg_satype)) {
  51529. + ERROR("pfkey_msg_parse: "
  51530. + "satype %d conversion to proto failed for msg_type %d (%s).\n",
  51531. + pfkey_msg->sadb_msg_satype,
  51532. + pfkey_msg->sadb_msg_type,
  51533. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51534. + SENDERR(EINVAL);
  51535. + } else {
  51536. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51537. + "pfkey_msg_parse: "
  51538. + "satype %d(%s) conversion to proto gives %d for msg_type %d(%s).\n",
  51539. + pfkey_msg->sadb_msg_satype,
  51540. + satype2name(pfkey_msg->sadb_msg_satype),
  51541. + satype2proto(pfkey_msg->sadb_msg_satype),
  51542. + pfkey_msg->sadb_msg_type,
  51543. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51544. + }
  51545. + case SADB_ACQUIRE:
  51546. + case SADB_REGISTER:
  51547. + case SADB_EXPIRE:
  51548. + if(!pfkey_msg->sadb_msg_satype) {
  51549. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51550. + "pfkey_msg_parse: "
  51551. + "satype is zero, must be non-zero for msg_type %d(%s).\n",
  51552. + pfkey_msg->sadb_msg_type,
  51553. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51554. + SENDERR(EINVAL);
  51555. + }
  51556. + default:
  51557. + break;
  51558. + }
  51559. +
  51560. + /* errno must not be set in downward messages */
  51561. + /* this is not entirely true... a response to an ACQUIRE could return an error */
  51562. + if((dir == EXT_BITS_IN) && (pfkey_msg->sadb_msg_type != SADB_ACQUIRE) && pfkey_msg->sadb_msg_errno) {
  51563. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51564. + "pfkey_msg_parse: "
  51565. + "errno set to %d.\n",
  51566. + pfkey_msg->sadb_msg_errno);
  51567. + SENDERR(EINVAL);
  51568. + }
  51569. +
  51570. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51571. + "pfkey_msg_parse: "
  51572. + "remain=%d\n",
  51573. + remain
  51574. + );
  51575. +
  51576. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51577. + "pfkey_msg_parse: "
  51578. + "extensions permitted=%08x, required=%08x.\n",
  51579. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51580. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]);
  51581. +
  51582. + extensions_seen = 1;
  51583. +
  51584. + while( (remain * IPSEC_PFKEYv2_ALIGN) >= sizeof(struct sadb_ext) ) {
  51585. + /* Is there enough message left to support another extension header? */
  51586. + if(remain < pfkey_ext->sadb_ext_len) {
  51587. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51588. + "pfkey_msg_parse: "
  51589. + "remain %d less than ext len %d.\n",
  51590. + remain, pfkey_ext->sadb_ext_len);
  51591. + SENDERR(EINVAL);
  51592. + }
  51593. +
  51594. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51595. + "pfkey_msg_parse: "
  51596. + "parsing ext type=%d(%s) remain=%d.\n",
  51597. + pfkey_ext->sadb_ext_type,
  51598. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51599. + remain);
  51600. +
  51601. + /* Is the extension header type valid? */
  51602. + if((pfkey_ext->sadb_ext_type > SADB_EXT_MAX) || (!pfkey_ext->sadb_ext_type)) {
  51603. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51604. + "pfkey_msg_parse: "
  51605. + "ext type %d(%s) invalid, SADB_EXT_MAX=%d.\n",
  51606. + pfkey_ext->sadb_ext_type,
  51607. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51608. + SADB_EXT_MAX);
  51609. + SENDERR(EINVAL);
  51610. + }
  51611. +
  51612. + /* Have we already seen this type of extension? */
  51613. + if((extensions_seen & ( 1 << pfkey_ext->sadb_ext_type )) != 0)
  51614. + {
  51615. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51616. + "pfkey_msg_parse: "
  51617. + "ext type %d(%s) already seen.\n",
  51618. + pfkey_ext->sadb_ext_type,
  51619. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51620. + SENDERR(EINVAL);
  51621. + }
  51622. +
  51623. + /* Do I even know about this type of extension? */
  51624. + if(ext_parsers[pfkey_ext->sadb_ext_type]==NULL) {
  51625. + ERROR("pfkey_msg_parse: "
  51626. + "ext type %d(%s) unknown, ignoring.\n",
  51627. + pfkey_ext->sadb_ext_type,
  51628. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51629. + goto next_ext;
  51630. + }
  51631. +
  51632. + /* Is this type of extension permitted for this type of message? */
  51633. + if(!(extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type] &
  51634. + 1<<pfkey_ext->sadb_ext_type)) {
  51635. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51636. + "pfkey_msg_parse: "
  51637. + "ext type %d(%s) not permitted, exts_perm_in=%08x, 1<<type=%08x\n",
  51638. + pfkey_ext->sadb_ext_type,
  51639. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51640. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51641. + 1<<pfkey_ext->sadb_ext_type);
  51642. + SENDERR(EINVAL);
  51643. + }
  51644. +
  51645. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51646. + "pfkey_msg_parse: "
  51647. + "remain=%d ext_type=%d(%s) ext_len=%d parsing ext 0p%p with parser %s.\n",
  51648. + remain,
  51649. + pfkey_ext->sadb_ext_type,
  51650. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51651. + pfkey_ext->sadb_ext_len,
  51652. + pfkey_ext,
  51653. + ext_parsers[pfkey_ext->sadb_ext_type]->parser_name);
  51654. +
  51655. + /* Parse the extension */
  51656. + if((error =
  51657. + (*ext_parsers[pfkey_ext->sadb_ext_type]->parser)(pfkey_ext))) {
  51658. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51659. + "pfkey_msg_parse: "
  51660. + "extension parsing for type %d(%s) failed with error %d.\n",
  51661. + pfkey_ext->sadb_ext_type,
  51662. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51663. + error);
  51664. + SENDERR(-error);
  51665. + }
  51666. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51667. + "pfkey_msg_parse: "
  51668. + "Extension %d(%s) parsed.\n",
  51669. + pfkey_ext->sadb_ext_type,
  51670. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51671. +
  51672. + /* Mark that we have seen this extension and remember the header location */
  51673. + extensions_seen |= ( 1 << pfkey_ext->sadb_ext_type );
  51674. + extensions[pfkey_ext->sadb_ext_type] = pfkey_ext;
  51675. +
  51676. + next_ext:
  51677. + /* Calculate how much message remains */
  51678. + remain -= pfkey_ext->sadb_ext_len;
  51679. +
  51680. + if(!remain) {
  51681. + break;
  51682. + }
  51683. + /* Find the next extension header */
  51684. + pfkey_ext = (struct sadb_ext*)((char*)pfkey_ext +
  51685. + pfkey_ext->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  51686. + }
  51687. +
  51688. + if(remain) {
  51689. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51690. + "pfkey_msg_parse: "
  51691. + "unexpected remainder of %d.\n",
  51692. + remain);
  51693. + /* why is there still something remaining? */
  51694. + SENDERR(EINVAL);
  51695. + }
  51696. +
  51697. + /* check required extensions */
  51698. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51699. + "pfkey_msg_parse: "
  51700. + "extensions permitted=%08x, seen=%08x, required=%08x.\n",
  51701. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51702. + extensions_seen,
  51703. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]);
  51704. +
  51705. + /* don't check further if it is an error return message since it
  51706. + may not have a body */
  51707. + if(pfkey_msg->sadb_msg_errno) {
  51708. + SENDERR(-error);
  51709. + }
  51710. +
  51711. + if((extensions_seen &
  51712. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]) !=
  51713. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]) {
  51714. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51715. + "pfkey_msg_parse: "
  51716. + "required extensions missing:%08x.\n",
  51717. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type] -
  51718. + (extensions_seen &
  51719. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]));
  51720. + SENDERR(EINVAL);
  51721. + }
  51722. +
  51723. + if((dir == EXT_BITS_IN) && (pfkey_msg->sadb_msg_type == SADB_X_DELFLOW)
  51724. + && ((extensions_seen & SADB_X_EXT_ADDRESS_DELFLOW)
  51725. + != SADB_X_EXT_ADDRESS_DELFLOW)
  51726. + && (((extensions_seen & (1<<SADB_EXT_SA)) != (1<<SADB_EXT_SA))
  51727. + || ((((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_flags
  51728. + & SADB_X_SAFLAGS_CLEARFLOW)
  51729. + != SADB_X_SAFLAGS_CLEARFLOW))) {
  51730. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51731. + "pfkey_msg_parse: "
  51732. + "required SADB_X_DELFLOW extensions missing: either %08x must be present or %08x must be present with SADB_X_SAFLAGS_CLEARFLOW set.\n",
  51733. + SADB_X_EXT_ADDRESS_DELFLOW
  51734. + - (extensions_seen & SADB_X_EXT_ADDRESS_DELFLOW),
  51735. + (1<<SADB_EXT_SA) - (extensions_seen & (1<<SADB_EXT_SA)));
  51736. + SENDERR(EINVAL);
  51737. + }
  51738. +
  51739. + switch(pfkey_msg->sadb_msg_type) {
  51740. + case SADB_ADD:
  51741. + case SADB_UPDATE:
  51742. + /* check maturity */
  51743. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state !=
  51744. + SADB_SASTATE_MATURE) {
  51745. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51746. + "pfkey_msg_parse: "
  51747. + "state=%d for add or update should be MATURE=%d.\n",
  51748. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  51749. + SADB_SASTATE_MATURE);
  51750. + SENDERR(EINVAL);
  51751. + }
  51752. +
  51753. + /* check AH and ESP */
  51754. + switch(((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype) {
  51755. + case SADB_SATYPE_AH:
  51756. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51757. + ((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. + "auth alg is zero, must be non-zero for AH SAs.\n");
  51762. + SENDERR(EINVAL);
  51763. + }
  51764. + if(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt !=
  51765. + SADB_EALG_NONE) {
  51766. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51767. + "pfkey_msg_parse: "
  51768. + "AH handed encalg=%d, must be zero.\n",
  51769. + ((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt);
  51770. + SENDERR(EINVAL);
  51771. + }
  51772. + break;
  51773. + case SADB_SATYPE_ESP:
  51774. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51775. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt !=
  51776. + SADB_EALG_NONE)) {
  51777. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51778. + "pfkey_msg_parse: "
  51779. + "encrypt alg=%d is zero, must be non-zero for ESP=%d SAs.\n",
  51780. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt,
  51781. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  51782. + SENDERR(EINVAL);
  51783. + }
  51784. + if((((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt ==
  51785. + SADB_EALG_NULL) &&
  51786. + (((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth ==
  51787. + SADB_AALG_NONE) ) {
  51788. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51789. + "pfkey_msg_parse: "
  51790. + "ESP handed encNULL+authNONE, illegal combination.\n");
  51791. + SENDERR(EINVAL);
  51792. + }
  51793. + break;
  51794. + case SADB_X_SATYPE_COMP:
  51795. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51796. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt !=
  51797. + SADB_EALG_NONE)) {
  51798. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51799. + "pfkey_msg_parse: "
  51800. + "encrypt alg=%d is zero, must be non-zero for COMP=%d SAs.\n",
  51801. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt,
  51802. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  51803. + SENDERR(EINVAL);
  51804. + }
  51805. + if(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth !=
  51806. + SADB_AALG_NONE) {
  51807. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51808. + "pfkey_msg_parse: "
  51809. + "COMP handed auth=%d, must be zero.\n",
  51810. + ((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth);
  51811. + SENDERR(EINVAL);
  51812. + }
  51813. + break;
  51814. + default:
  51815. + break;
  51816. + }
  51817. + if(ntohl(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_spi) <= 255) {
  51818. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51819. + "pfkey_msg_parse: "
  51820. + "spi=%08x must be > 255.\n",
  51821. + ntohl(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_spi));
  51822. + SENDERR(EINVAL);
  51823. + }
  51824. + default:
  51825. + break;
  51826. + }
  51827. +errlab:
  51828. +
  51829. + return error;
  51830. +}
  51831. +
  51832. +/*
  51833. + * $Log: pfkey_v2_parse.c,v $
  51834. + * Revision 1.65 2005/04/06 17:46:05 mcr
  51835. + * failure to recognize an extension is considered an error.
  51836. + * This could be a problem in the future, but we need some kind
  51837. + * of logging. This should be rate limited, probably.
  51838. + *
  51839. + * Revision 1.64 2005/01/26 00:50:35 mcr
  51840. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  51841. + * and make sure that NAT_TRAVERSAL is set as well to match
  51842. + * userspace compiles of code.
  51843. + *
  51844. + * Revision 1.63 2004/10/28 22:54:10 mcr
  51845. + * results from valgrind, thanks to: Harald Hoyer <harald@redhat.com>
  51846. + *
  51847. + * Revision 1.62 2004/10/03 01:26:36 mcr
  51848. + * fixes for gcc 3.4 compilation.
  51849. + *
  51850. + * Revision 1.61 2004/07/10 19:11:18 mcr
  51851. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  51852. + *
  51853. + * Revision 1.59 2004/04/18 03:03:49 mcr
  51854. + * renamed common include files from pluto directory.
  51855. + *
  51856. + * Revision 1.58 2004/03/08 01:59:08 ken
  51857. + * freeswan.h -> openswan.h
  51858. + *
  51859. + * Revision 1.57 2003/12/10 01:20:19 mcr
  51860. + * NAT-traversal patches to KLIPS.
  51861. + *
  51862. + * Revision 1.56 2003/12/04 23:01:12 mcr
  51863. + * removed ipsec_netlink.h
  51864. + *
  51865. + * Revision 1.55 2003/11/07 01:30:37 ken
  51866. + * Cast sizeof() to int to keep things 64bit clean
  51867. + *
  51868. + * Revision 1.54 2003/10/31 02:27:12 mcr
  51869. + * pulled up port-selector patches and sa_id elimination.
  51870. + *
  51871. + * Revision 1.53.20.2 2003/10/29 01:11:32 mcr
  51872. + * added debugging for pfkey library.
  51873. + *
  51874. + * Revision 1.53.20.1 2003/09/21 13:59:44 mcr
  51875. + * pre-liminary X.509 patch - does not yet pass tests.
  51876. + *
  51877. + * Revision 1.53 2003/01/30 02:32:09 rgb
  51878. + *
  51879. + * Rename SAref table macro names for clarity.
  51880. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  51881. + *
  51882. + * Revision 1.52 2002/12/30 06:53:07 mcr
  51883. + * deal with short SA structures... #if 0 out for now. Probably
  51884. + * not quite the right way.
  51885. + *
  51886. + * Revision 1.51 2002/12/13 18:16:02 mcr
  51887. + * restored sa_ref code
  51888. + *
  51889. + * Revision 1.50 2002/12/13 18:06:52 mcr
  51890. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  51891. + *
  51892. + * Revision 1.49 2002/10/05 05:02:58 dhr
  51893. + *
  51894. + * C labels go on statements
  51895. + *
  51896. + * Revision 1.48 2002/09/20 15:40:45 rgb
  51897. + * Added sadb_x_sa_ref to struct sadb_sa.
  51898. + *
  51899. + * Revision 1.47 2002/09/20 05:01:31 rgb
  51900. + * Fixed usage of pfkey_lib_debug.
  51901. + * Format for function declaration style consistency.
  51902. + * Added text labels to elucidate numeric values presented.
  51903. + * Re-organised debug output to reduce noise in output.
  51904. + *
  51905. + * Revision 1.46 2002/07/24 18:44:54 rgb
  51906. + * Type fiddling to tame ia64 compiler.
  51907. + *
  51908. + * Revision 1.45 2002/05/23 07:14:11 rgb
  51909. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  51910. + *
  51911. + * Revision 1.44 2002/04/24 07:55:32 mcr
  51912. + * #include patches and Makefiles for post-reorg compilation.
  51913. + *
  51914. + * Revision 1.43 2002/04/24 07:36:40 mcr
  51915. + * Moved from ./lib/pfkey_v2_parse.c,v
  51916. + *
  51917. + * Revision 1.42 2002/01/29 22:25:36 rgb
  51918. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  51919. + *
  51920. + * Revision 1.41 2002/01/29 01:59:10 mcr
  51921. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  51922. + * updating of IPv6 structures to match latest in6.h version.
  51923. + * removed dead code from openswan.h that also duplicated kversions.h
  51924. + * code.
  51925. + *
  51926. + * Revision 1.40 2002/01/20 20:34:50 mcr
  51927. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  51928. + *
  51929. + * Revision 1.39 2001/11/27 05:29:22 mcr
  51930. + * pfkey parses are now maintained by a structure
  51931. + * that includes their name for debug purposes.
  51932. + * DEBUGGING() macro changed so that it takes a debug
  51933. + * level so that pf_key() can use this to decode the
  51934. + * structures without innundanting humans.
  51935. + * Also uses pfkey_v2_sadb_ext_string() in messages.
  51936. + *
  51937. + * Revision 1.38 2001/11/06 19:47:47 rgb
  51938. + * Added packet parameter to lifetime and comb structures.
  51939. + *
  51940. + * Revision 1.37 2001/10/18 04:45:24 rgb
  51941. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  51942. + * lib/openswan.h version macros moved to lib/kversions.h.
  51943. + * Other compiler directive cleanups.
  51944. + *
  51945. + * Revision 1.36 2001/06/14 19:35:16 rgb
  51946. + * Update copyright date.
  51947. + *
  51948. + * Revision 1.35 2001/05/03 19:44:51 rgb
  51949. + * Standardise on SENDERR() macro.
  51950. + *
  51951. + * Revision 1.34 2001/03/16 07:41:51 rgb
  51952. + * Put openswan.h include before pluto includes.
  51953. + *
  51954. + * Revision 1.33 2001/02/27 07:13:51 rgb
  51955. + * Added satype2name() function.
  51956. + * Added text to default satype_tbl entry.
  51957. + * Added satype2name() conversions for most satype debug output.
  51958. + *
  51959. + * Revision 1.32 2001/02/26 20:01:09 rgb
  51960. + * Added internal IP protocol 61 for magic SAs.
  51961. + * Ditch unused sadb_satype2proto[], replaced by satype2proto().
  51962. + * Re-formatted debug output (split lines, consistent spacing).
  51963. + * Removed acquire, register and expire requirements for a known satype.
  51964. + * Changed message type checking to a switch structure.
  51965. + * Verify expected NULL auth for IPCOMP.
  51966. + * Enforced spi > 0x100 requirement, now that pass uses a magic SA for
  51967. + * appropriate message types.
  51968. + *
  51969. + * Revision 1.31 2000/12/01 07:09:00 rgb
  51970. + * Added ipcomp sanity check to require encalgo is set.
  51971. + *
  51972. + * Revision 1.30 2000/11/17 18:10:30 rgb
  51973. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  51974. + * network byte order since this is the way PF_KEYv2 stored spis.
  51975. + *
  51976. + * Revision 1.29 2000/10/12 00:02:39 rgb
  51977. + * Removed 'format, ##' nonsense from debug macros for RH7.0.
  51978. + *
  51979. + * Revision 1.28 2000/09/20 16:23:04 rgb
  51980. + * Remove over-paranoid extension check in the presence of sadb_msg_errno.
  51981. + *
  51982. + * Revision 1.27 2000/09/20 04:04:21 rgb
  51983. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  51984. + * oopsen.
  51985. + *
  51986. + * Revision 1.26 2000/09/15 11:37:02 rgb
  51987. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  51988. + * IPCOMP zlib deflate code.
  51989. + *
  51990. + * Revision 1.25 2000/09/12 22:35:37 rgb
  51991. + * Restructured to remove unused extensions from CLEARFLOW messages.
  51992. + *
  51993. + * Revision 1.24 2000/09/12 18:59:54 rgb
  51994. + * Added Gerhard's IPv6 support to pfkey parts of libopenswan.
  51995. + *
  51996. + * Revision 1.23 2000/09/12 03:27:00 rgb
  51997. + * Moved DEBUGGING definition to compile kernel with debug off.
  51998. + *
  51999. + * Revision 1.22 2000/09/09 06:39:27 rgb
  52000. + * Restrict pfkey errno check to downward messages only.
  52001. + *
  52002. + * Revision 1.21 2000/09/08 19:22:34 rgb
  52003. + * Enabled pfkey_sens_parse().
  52004. + * Added check for errno on downward acquire messages only.
  52005. + *
  52006. + * Revision 1.20 2000/09/01 18:48:23 rgb
  52007. + * Fixed reserved check bug and added debug output in
  52008. + * pfkey_supported_parse().
  52009. + * Fixed debug output label bug in pfkey_ident_parse().
  52010. + *
  52011. + * Revision 1.19 2000/08/27 01:55:26 rgb
  52012. + * Define OCTETBITS and PFKEYBITS to avoid using 'magic' numbers in code.
  52013. + *
  52014. + * Revision 1.18 2000/08/24 17:00:36 rgb
  52015. + * Ignore unknown extensions instead of failing.
  52016. + *
  52017. + * Revision 1.17 2000/06/02 22:54:14 rgb
  52018. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  52019. + *
  52020. + * Revision 1.16 2000/05/10 19:25:11 rgb
  52021. + * Fleshed out proposal and supported extensions.
  52022. + *
  52023. + * Revision 1.15 2000/01/24 21:15:31 rgb
  52024. + * Added disabled pluto pfkey lib debug flag.
  52025. + * Added algo debugging reporting.
  52026. + *
  52027. + * Revision 1.14 2000/01/22 23:24:29 rgb
  52028. + * Added new functions proto2satype() and satype2proto() and lookup
  52029. + * table satype_tbl. Also added proto2name() since it was easy.
  52030. + *
  52031. + * Revision 1.13 2000/01/21 09:43:59 rgb
  52032. + * Cast ntohl(spi) as (unsigned long int) to shut up compiler.
  52033. + *
  52034. + * Revision 1.12 2000/01/21 06:28:19 rgb
  52035. + * Added address cases for eroute flows.
  52036. + * Indented compiler directives for readability.
  52037. + * Added klipsdebug switching capability.
  52038. + *
  52039. + * Revision 1.11 1999/12/29 21:14:59 rgb
  52040. + * Fixed debug text cut and paste typo.
  52041. + *
  52042. + * Revision 1.10 1999/12/10 17:45:24 rgb
  52043. + * Added address debugging.
  52044. + *
  52045. + * Revision 1.9 1999/12/09 23:11:42 rgb
  52046. + * Ditched <string.h> include since we no longer use memset().
  52047. + * Use new pfkey_extensions_init() instead of memset().
  52048. + * Added check for SATYPE in pfkey_msg_build().
  52049. + * Tidy up comments and debugging comments.
  52050. + *
  52051. + * Revision 1.8 1999/12/07 19:55:26 rgb
  52052. + * Removed unused first argument from extension parsers.
  52053. + * Removed static pluto debug flag.
  52054. + * Moved message type and state checking to pfkey_msg_parse().
  52055. + * Changed print[fk] type from lx to x to quiet compiler.
  52056. + * Removed redundant remain check.
  52057. + * Changed __u* types to uint* to avoid use of asm/types.h and
  52058. + * sys/types.h in userspace code.
  52059. + *
  52060. + * Revision 1.7 1999/12/01 22:20:51 rgb
  52061. + * Moved pfkey_lib_debug variable into the library.
  52062. + * Added pfkey version check into header parsing.
  52063. + * Added check for SATYPE only for those extensions that require a
  52064. + * non-zero value.
  52065. + *
  52066. + * Revision 1.6 1999/11/27 11:58:05 rgb
  52067. + * Added ipv6 headers.
  52068. + * Moved sadb_satype2proto protocol lookup table from
  52069. + * klips/net/ipsec/pfkey_v2_parser.c.
  52070. + * Enable lifetime_current checking.
  52071. + * Debugging error messages added.
  52072. + * Add argument to pfkey_msg_parse() for direction.
  52073. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  52074. + * Add CVS log entry to bottom of file.
  52075. + * Moved auth and enc alg check to pfkey_msg_parse().
  52076. + * Enable accidentally disabled spirange parsing.
  52077. + * Moved protocol/algorithm checks from klips/net/ipsec/pfkey_v2_parser.c
  52078. + *
  52079. + * Local variables:
  52080. + * c-file-style: "linux"
  52081. + * End:
  52082. + *
  52083. + */
  52084. --- /dev/null Tue Mar 11 13:02:56 2003
  52085. +++ linux/net/ipsec/pfkey_v2_parser.c Mon Feb 9 13:51:03 2004
  52086. @@ -0,0 +1,3520 @@
  52087. +/*
  52088. + * @(#) RFC2367 PF_KEYv2 Key management API message parser
  52089. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  52090. + *
  52091. + * This program is free software; you can redistribute it and/or modify it
  52092. + * under the terms of the GNU General Public License as published by the
  52093. + * Free Software Foundation; either version 2 of the License, or (at your
  52094. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  52095. + *
  52096. + * This program is distributed in the hope that it will be useful, but
  52097. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  52098. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  52099. + * for more details.
  52100. + *
  52101. + * RCSID $Id: pfkey_v2_parser.c,v 1.134.2.2 2006/10/06 21:39:26 paul Exp $
  52102. + */
  52103. +
  52104. +/*
  52105. + * Template from klips/net/ipsec/ipsec/ipsec_netlink.c.
  52106. + */
  52107. +
  52108. +char pfkey_v2_parser_c_version[] = "$Id: pfkey_v2_parser.c,v 1.134.2.2 2006/10/06 21:39:26 paul Exp $";
  52109. +
  52110. +#ifndef AUTOCONF_INCLUDED
  52111. +#include <linux/config.h>
  52112. +#endif
  52113. +#include <linux/version.h>
  52114. +#include <linux/kernel.h> /* printk() */
  52115. +
  52116. +#include "openswan/ipsec_param.h"
  52117. +
  52118. +#ifdef MALLOC_SLAB
  52119. +# include <linux/slab.h> /* kmalloc() */
  52120. +#else /* MALLOC_SLAB */
  52121. +# include <linux/malloc.h> /* kmalloc() */
  52122. +#endif /* MALLOC_SLAB */
  52123. +#include <linux/errno.h> /* error codes */
  52124. +#include <linux/types.h> /* size_t */
  52125. +#include <linux/interrupt.h> /* mark_bh */
  52126. +
  52127. +#include <linux/netdevice.h> /* struct device, and other headers */
  52128. +#include <linux/etherdevice.h> /* eth_type_trans */
  52129. +#include <linux/ip.h> /* struct iphdr */
  52130. +#include <linux/skbuff.h>
  52131. +
  52132. +#include <openswan.h>
  52133. +
  52134. +#include <crypto/des.h>
  52135. +
  52136. +#ifdef SPINLOCK
  52137. +# ifdef SPINLOCK_23
  52138. +# include <linux/spinlock.h> /* *lock* */
  52139. +# else /* SPINLOCK_23 */
  52140. +# include <asm/spinlock.h> /* *lock* */
  52141. +# endif /* SPINLOCK_23 */
  52142. +#endif /* SPINLOCK */
  52143. +
  52144. +#include <linux/in6.h>
  52145. +#include <net/route.h>
  52146. +
  52147. +#include <net/ip.h>
  52148. +#ifdef NETLINK_SOCK
  52149. +# include <linux/netlink.h>
  52150. +#else
  52151. +# include <net/netlink.h>
  52152. +#endif
  52153. +
  52154. +#include <linux/random.h> /* get_random_bytes() */
  52155. +
  52156. +#include "openswan/radij.h"
  52157. +#include "openswan/ipsec_encap.h"
  52158. +#include "openswan/ipsec_sa.h"
  52159. +
  52160. +#include "openswan/ipsec_radij.h"
  52161. +#include "openswan/ipsec_xform.h"
  52162. +#include "openswan/ipsec_ah.h"
  52163. +#include "openswan/ipsec_esp.h"
  52164. +#include "openswan/ipsec_tunnel.h"
  52165. +#include "openswan/ipsec_rcv.h"
  52166. +#include "openswan/ipcomp.h"
  52167. +
  52168. +#include <pfkeyv2.h>
  52169. +#include <pfkey.h>
  52170. +
  52171. +#include "openswan/ipsec_proto.h"
  52172. +#include "openswan/ipsec_alg.h"
  52173. +
  52174. +#include "openswan/ipsec_kern24.h"
  52175. +
  52176. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  52177. +
  52178. +struct sklist_t {
  52179. + struct socket *sk;
  52180. + struct sklist_t* next;
  52181. +} pfkey_sklist_head, *pfkey_sklist, *pfkey_sklist_prev;
  52182. +
  52183. +__u32 pfkey_msg_seq = 0;
  52184. +
  52185. +
  52186. +#if 0
  52187. +#define DUMP_SAID dump_said(&extr->ips->ips_said, __LINE__)
  52188. +#define DUMP_SAID2 dump_said(&extr.ips->ips_said, __LINE__)
  52189. +static void dump_said(ip_said *s, int line)
  52190. +{
  52191. + char msa[SATOT_BUF];
  52192. + size_t msa_len;
  52193. +
  52194. + msa_len = satot(s, 0, msa, sizeof(msa));
  52195. +
  52196. + printk("line: %d msa: %s\n", line, msa);
  52197. +}
  52198. +#endif
  52199. +
  52200. +
  52201. +int
  52202. +pfkey_alloc_eroute(struct eroute** eroute)
  52203. +{
  52204. + int error = 0;
  52205. + if(*eroute) {
  52206. + KLIPS_PRINT(debug_pfkey,
  52207. + "klips_debug:pfkey_alloc_eroute: "
  52208. + "eroute struct already allocated\n");
  52209. + SENDERR(EEXIST);
  52210. + }
  52211. +
  52212. + if((*eroute = kmalloc(sizeof(**eroute), GFP_ATOMIC) ) == NULL) {
  52213. + KLIPS_PRINT(debug_pfkey,
  52214. + "klips_debug:pfkey_alloc_eroute: "
  52215. + "memory allocation error\n");
  52216. + SENDERR(ENOMEM);
  52217. + }
  52218. +
  52219. + KLIPS_PRINT(debug_pfkey,
  52220. + "klips_debug:pfkey_alloc_eroute: "
  52221. + "allocating %lu bytes for an eroute at 0p%p\n",
  52222. + (unsigned long) sizeof(**eroute), *eroute);
  52223. +
  52224. + memset((caddr_t)*eroute, 0, sizeof(**eroute));
  52225. + (*eroute)->er_eaddr.sen_len =
  52226. + (*eroute)->er_emask.sen_len = sizeof(struct sockaddr_encap);
  52227. + (*eroute)->er_eaddr.sen_family =
  52228. + (*eroute)->er_emask.sen_family = AF_ENCAP;
  52229. + (*eroute)->er_eaddr.sen_type = SENT_IP4;
  52230. + (*eroute)->er_emask.sen_type = 255;
  52231. + (*eroute)->er_pid = 0;
  52232. + (*eroute)->er_count = 0;
  52233. + (*eroute)->er_lasttime = jiffies/HZ;
  52234. +
  52235. + errlab:
  52236. + return(error);
  52237. +}
  52238. +
  52239. +DEBUG_NO_STATIC int
  52240. +pfkey_x_protocol_process(struct sadb_ext *pfkey_ext,
  52241. + struct pfkey_extracted_data *extr)
  52242. +{
  52243. + int error = 0;
  52244. + struct sadb_protocol * p = (struct sadb_protocol *)pfkey_ext;
  52245. +
  52246. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_protocol_process: %p\n", extr);
  52247. +
  52248. + if (extr == 0) {
  52249. + KLIPS_PRINT(debug_pfkey,
  52250. + "klips_debug:pfkey_x_protocol_process:"
  52251. + "extr is NULL, fatal\n");
  52252. + SENDERR(EINVAL);
  52253. + }
  52254. + if (extr->eroute == 0) {
  52255. + KLIPS_PRINT(debug_pfkey,
  52256. + "klips_debug:pfkey_x_protocol_process:"
  52257. + "extr->eroute is NULL, fatal\n");
  52258. + SENDERR(EINVAL);
  52259. + }
  52260. +
  52261. + extr->eroute->er_eaddr.sen_proto = p->sadb_protocol_proto;
  52262. + extr->eroute->er_emask.sen_proto = p->sadb_protocol_proto ? ~0:0;
  52263. + KLIPS_PRINT(debug_pfkey,
  52264. + "klips_debug:pfkey_x_protocol_process: protocol = %d.\n",
  52265. + p->sadb_protocol_proto);
  52266. + errlab:
  52267. + return error;
  52268. +}
  52269. +
  52270. +DEBUG_NO_STATIC int
  52271. +pfkey_ipsec_sa_init(struct ipsec_sa *ipsp)
  52272. +{
  52273. +
  52274. + return ipsec_sa_init(ipsp);
  52275. +}
  52276. +
  52277. +int
  52278. +pfkey_safe_build(int error, struct sadb_ext *extensions[SADB_MAX+1])
  52279. +{
  52280. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build: "
  52281. + "error=%d\n",
  52282. + error);
  52283. + if (!error) {
  52284. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build:"
  52285. + "success.\n");
  52286. + return 1;
  52287. + } else {
  52288. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build:"
  52289. + "caught error %d\n",
  52290. + error);
  52291. + pfkey_extensions_free(extensions);
  52292. + return 0;
  52293. + }
  52294. +}
  52295. +
  52296. +
  52297. +DEBUG_NO_STATIC int
  52298. +pfkey_getspi_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52299. +{
  52300. + int error = 0;
  52301. + ipsec_spi_t minspi = htonl(256), maxspi = htonl(-1L);
  52302. + int found_avail = 0;
  52303. + struct ipsec_sa *ipsq;
  52304. + char sa[SATOT_BUF];
  52305. + size_t sa_len;
  52306. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52307. + struct sadb_msg *pfkey_reply = NULL;
  52308. + struct socket_list *pfkey_socketsp;
  52309. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52310. +
  52311. + KLIPS_PRINT(debug_pfkey,
  52312. + "klips_debug:pfkey_getspi_parse: .\n");
  52313. +
  52314. + pfkey_extensions_init(extensions_reply);
  52315. +
  52316. + if(extr == NULL || extr->ips == NULL) {
  52317. + KLIPS_PRINT(debug_pfkey,
  52318. + "klips_debug:pfkey_getspi_parse: "
  52319. + "error, extr or extr->ipsec_sa pointer NULL\n");
  52320. + SENDERR(EINVAL);
  52321. + }
  52322. +
  52323. + if(extensions[SADB_EXT_SPIRANGE]) {
  52324. + minspi = ((struct sadb_spirange *)extensions[SADB_EXT_SPIRANGE])->sadb_spirange_min;
  52325. + maxspi = ((struct sadb_spirange *)extensions[SADB_EXT_SPIRANGE])->sadb_spirange_max;
  52326. + }
  52327. +
  52328. + if(maxspi == minspi) {
  52329. + extr->ips->ips_said.spi = maxspi;
  52330. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52331. + if(ipsq != NULL) {
  52332. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52333. + ipsec_sa_put(ipsq);
  52334. + KLIPS_PRINT(debug_pfkey,
  52335. + "klips_debug:pfkey_getspi_parse: "
  52336. + "EMT_GETSPI found an old ipsec_sa for SA: %s, delete it first.\n",
  52337. + sa_len ? sa : " (error)");
  52338. + SENDERR(EEXIST);
  52339. + } else {
  52340. + found_avail = 1;
  52341. + }
  52342. + } else {
  52343. + int i = 0;
  52344. + __u32 rand_val;
  52345. + __u32 spi_diff;
  52346. + while( ( i < (spi_diff = (ntohl(maxspi) - ntohl(minspi)))) && !found_avail ) {
  52347. + prng_bytes(&ipsec_prng, (char *) &(rand_val),
  52348. + ( (spi_diff < (2^8)) ? 1 :
  52349. + ( (spi_diff < (2^16)) ? 2 :
  52350. + ( (spi_diff < (2^24)) ? 3 :
  52351. + 4 ) ) ) );
  52352. + extr->ips->ips_said.spi = htonl(ntohl(minspi) +
  52353. + (rand_val %
  52354. + (spi_diff + 1)));
  52355. + i++;
  52356. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52357. + if(ipsq == NULL) {
  52358. + found_avail = 1;
  52359. + } else {
  52360. + ipsec_sa_put(ipsq);
  52361. + }
  52362. + }
  52363. + }
  52364. +
  52365. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52366. +
  52367. + if (!found_avail) {
  52368. + KLIPS_PRINT(debug_pfkey,
  52369. + "klips_debug:pfkey_getspi_parse: "
  52370. + "found an old ipsec_sa for SA: %s, delete it first.\n",
  52371. + sa_len ? sa : " (error)");
  52372. + SENDERR(EEXIST);
  52373. + }
  52374. +
  52375. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52376. + extr->ips->ips_flags |= EMT_INBOUND;
  52377. + }
  52378. +
  52379. + KLIPS_PRINT(debug_pfkey,
  52380. + "klips_debug:pfkey_getspi_parse: "
  52381. + "existing ipsec_sa not found (this is good) for SA: %s, %s-bound, allocating.\n",
  52382. + sa_len ? sa : " (error)",
  52383. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52384. +
  52385. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52386. + extr->ips->ips_rcvif = NULL;
  52387. + extr->ips->ips_life.ipl_addtime.ipl_count = jiffies/HZ;
  52388. +
  52389. + extr->ips->ips_state = SADB_SASTATE_LARVAL;
  52390. +
  52391. + if(!extr->ips->ips_life.ipl_allocations.ipl_count) {
  52392. + extr->ips->ips_life.ipl_allocations.ipl_count += 1;
  52393. + }
  52394. +
  52395. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52396. + SADB_GETSPI,
  52397. + satype,
  52398. + 0,
  52399. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52400. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52401. + extensions_reply)
  52402. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52403. + SADB_EXT_SA,
  52404. + extr->ips->ips_said.spi,
  52405. + 0,
  52406. + SADB_SASTATE_LARVAL,
  52407. + 0,
  52408. + 0,
  52409. + 0,
  52410. + extr->ips->ips_ref),
  52411. + extensions_reply)
  52412. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52413. + SADB_EXT_ADDRESS_SRC,
  52414. + 0, /*extr->ips->ips_said.proto,*/
  52415. + 0,
  52416. + extr->ips->ips_addr_s),
  52417. + extensions_reply)
  52418. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52419. + SADB_EXT_ADDRESS_DST,
  52420. + 0, /*extr->ips->ips_said.proto,*/
  52421. + 0,
  52422. + extr->ips->ips_addr_d),
  52423. + extensions_reply) )) {
  52424. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52425. + "failed to build the getspi reply message extensions\n");
  52426. + goto errlab;
  52427. + }
  52428. +
  52429. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52430. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52431. + "failed to build the getspi reply message\n");
  52432. + SENDERR(-error);
  52433. + }
  52434. + for(pfkey_socketsp = pfkey_open_sockets;
  52435. + pfkey_socketsp;
  52436. + pfkey_socketsp = pfkey_socketsp->next) {
  52437. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52438. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52439. + "sending up getspi reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52440. + satype,
  52441. + satype2name(satype),
  52442. + pfkey_socketsp->socketp,
  52443. + error);
  52444. + SENDERR(-error);
  52445. + }
  52446. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52447. + "sending up getspi reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52448. + satype,
  52449. + satype2name(satype),
  52450. + pfkey_socketsp->socketp);
  52451. + }
  52452. +
  52453. + if((error = ipsec_sa_add(extr->ips))) {
  52454. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52455. + "failed to add the larval SA=%s with error=%d.\n",
  52456. + sa_len ? sa : " (error)",
  52457. + error);
  52458. + SENDERR(-error);
  52459. + }
  52460. + extr->ips = NULL;
  52461. +
  52462. + KLIPS_PRINT(debug_pfkey,
  52463. + "klips_debug:pfkey_getspi_parse: "
  52464. + "successful for SA: %s\n",
  52465. + sa_len ? sa : " (error)");
  52466. +
  52467. + errlab:
  52468. + if (pfkey_reply) {
  52469. + pfkey_msg_free(&pfkey_reply);
  52470. + }
  52471. + pfkey_extensions_free(extensions_reply);
  52472. + return error;
  52473. +}
  52474. +
  52475. +DEBUG_NO_STATIC int
  52476. +pfkey_update_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52477. +{
  52478. + int error = 0;
  52479. + struct ipsec_sa* ipsq;
  52480. + char sa[SATOT_BUF];
  52481. + size_t sa_len;
  52482. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52483. + struct sadb_msg *pfkey_reply = NULL;
  52484. + struct socket_list *pfkey_socketsp;
  52485. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52486. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52487. + struct ipsec_sa *nat_t_ips_saved = NULL;
  52488. +#endif
  52489. + KLIPS_PRINT(debug_pfkey,
  52490. + "klips_debug:pfkey_update_parse: .\n");
  52491. +
  52492. + pfkey_extensions_init(extensions_reply);
  52493. +
  52494. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state != SADB_SASTATE_MATURE) {
  52495. + KLIPS_PRINT(debug_pfkey,
  52496. + "klips_debug:pfkey_update_parse: "
  52497. + "error, sa_state=%d must be MATURE=%d\n",
  52498. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  52499. + SADB_SASTATE_MATURE);
  52500. + SENDERR(EINVAL);
  52501. + }
  52502. +
  52503. + if(extr == NULL || extr->ips == NULL) {
  52504. + KLIPS_PRINT(debug_pfkey,
  52505. + "klips_debug:pfkey_update_parse: "
  52506. + "error, extr or extr->ips pointer NULL\n");
  52507. + SENDERR(EINVAL);
  52508. + }
  52509. +
  52510. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52511. +
  52512. + spin_lock_bh(&tdb_lock);
  52513. +
  52514. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52515. + if (ipsq == NULL) {
  52516. + spin_unlock_bh(&tdb_lock);
  52517. + KLIPS_PRINT(debug_pfkey,
  52518. + "klips_debug:pfkey_update_parse: "
  52519. + "reserved ipsec_sa for SA: %s not found. Call SADB_GETSPI first or call SADB_ADD instead.\n",
  52520. + sa_len ? sa : " (error)");
  52521. + SENDERR(ENOENT);
  52522. + }
  52523. +
  52524. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52525. + extr->ips->ips_flags |= EMT_INBOUND;
  52526. + }
  52527. +
  52528. + KLIPS_PRINT(debug_pfkey,
  52529. + "klips_debug:pfkey_update_parse: "
  52530. + "existing ipsec_sa found (this is good) for SA: %s, %s-bound, updating.\n",
  52531. + sa_len ? sa : " (error)",
  52532. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52533. +
  52534. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52535. + if (extr->ips->ips_natt_sport || extr->ips->ips_natt_dport) {
  52536. + KLIPS_PRINT(debug_pfkey,
  52537. + "klips_debug:pfkey_update_parse: only updating NAT-T ports "
  52538. + "(%u:%u -> %u:%u)\n",
  52539. + ipsq->ips_natt_sport, ipsq->ips_natt_dport,
  52540. + extr->ips->ips_natt_sport, extr->ips->ips_natt_dport);
  52541. +
  52542. + if (extr->ips->ips_natt_sport) {
  52543. + ipsq->ips_natt_sport = extr->ips->ips_natt_sport;
  52544. + if (ipsq->ips_addr_s->sa_family == AF_INET) {
  52545. + ((struct sockaddr_in *)(ipsq->ips_addr_s))->sin_port = htons(extr->ips->ips_natt_sport);
  52546. + }
  52547. + }
  52548. +
  52549. + if (extr->ips->ips_natt_dport) {
  52550. + ipsq->ips_natt_dport = extr->ips->ips_natt_dport;
  52551. + if (ipsq->ips_addr_d->sa_family == AF_INET) {
  52552. + ((struct sockaddr_in *)(ipsq->ips_addr_d))->sin_port = htons(extr->ips->ips_natt_dport);
  52553. + }
  52554. + }
  52555. +
  52556. + nat_t_ips_saved = extr->ips;
  52557. + extr->ips = ipsq;
  52558. + }
  52559. + else {
  52560. +#endif
  52561. +
  52562. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52563. + extr->ips->ips_rcvif = NULL;
  52564. + if ((error = pfkey_ipsec_sa_init(extr->ips))) {
  52565. + ipsec_sa_put(ipsq);
  52566. + spin_unlock_bh(&tdb_lock);
  52567. + KLIPS_PRINT(debug_pfkey,
  52568. + "klips_debug:pfkey_update_parse: "
  52569. + "not successful for SA: %s, deleting.\n",
  52570. + sa_len ? sa : " (error)");
  52571. + SENDERR(-error);
  52572. + }
  52573. +
  52574. + extr->ips->ips_life.ipl_addtime.ipl_count = ipsq->ips_life.ipl_addtime.ipl_count;
  52575. + ipsec_sa_put(ipsq);
  52576. + if((error = ipsec_sa_delchain(ipsq))) {
  52577. + spin_unlock_bh(&tdb_lock);
  52578. + KLIPS_PRINT(debug_pfkey,
  52579. + "klips_debug:pfkey_update_parse: "
  52580. + "error=%d, trouble deleting intermediate ipsec_sa for SA=%s.\n",
  52581. + error,
  52582. + sa_len ? sa : " (error)");
  52583. + SENDERR(-error);
  52584. + }
  52585. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52586. + }
  52587. +#endif
  52588. +
  52589. + spin_unlock_bh(&tdb_lock);
  52590. +
  52591. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52592. + SADB_UPDATE,
  52593. + satype,
  52594. + 0,
  52595. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52596. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52597. + extensions_reply)
  52598. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52599. + SADB_EXT_SA,
  52600. + extr->ips->ips_said.spi,
  52601. + extr->ips->ips_replaywin,
  52602. + extr->ips->ips_state,
  52603. + extr->ips->ips_authalg,
  52604. + extr->ips->ips_encalg,
  52605. + extr->ips->ips_flags,
  52606. + extr->ips->ips_ref),
  52607. + extensions_reply)
  52608. + /* The 3 lifetime extentions should only be sent if non-zero. */
  52609. + && (extensions[SADB_EXT_LIFETIME_HARD]
  52610. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  52611. + SADB_EXT_LIFETIME_HARD,
  52612. + extr->ips->ips_life.ipl_allocations.ipl_hard,
  52613. + extr->ips->ips_life.ipl_bytes.ipl_hard,
  52614. + extr->ips->ips_life.ipl_addtime.ipl_hard,
  52615. + extr->ips->ips_life.ipl_usetime.ipl_hard,
  52616. + extr->ips->ips_life.ipl_packets.ipl_hard),
  52617. + extensions_reply) : 1)
  52618. + && (extensions[SADB_EXT_LIFETIME_SOFT]
  52619. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  52620. + SADB_EXT_LIFETIME_SOFT,
  52621. + extr->ips->ips_life.ipl_allocations.ipl_count,
  52622. + extr->ips->ips_life.ipl_bytes.ipl_count,
  52623. + extr->ips->ips_life.ipl_addtime.ipl_count,
  52624. + extr->ips->ips_life.ipl_usetime.ipl_count,
  52625. + extr->ips->ips_life.ipl_packets.ipl_count),
  52626. + extensions_reply) : 1)
  52627. + && (extr->ips->ips_life.ipl_allocations.ipl_count
  52628. + || extr->ips->ips_life.ipl_bytes.ipl_count
  52629. + || extr->ips->ips_life.ipl_addtime.ipl_count
  52630. + || extr->ips->ips_life.ipl_usetime.ipl_count
  52631. + || extr->ips->ips_life.ipl_packets.ipl_count
  52632. +
  52633. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_CURRENT],
  52634. + SADB_EXT_LIFETIME_CURRENT,
  52635. + extr->ips->ips_life.ipl_allocations.ipl_count,
  52636. + extr->ips->ips_life.ipl_bytes.ipl_count,
  52637. + extr->ips->ips_life.ipl_addtime.ipl_count,
  52638. + extr->ips->ips_life.ipl_usetime.ipl_count,
  52639. + extr->ips->ips_life.ipl_packets.ipl_count),
  52640. + extensions_reply) : 1)
  52641. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52642. + SADB_EXT_ADDRESS_SRC,
  52643. + 0, /*extr->ips->ips_said.proto,*/
  52644. + 0,
  52645. + extr->ips->ips_addr_s),
  52646. + extensions_reply)
  52647. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52648. + SADB_EXT_ADDRESS_DST,
  52649. + 0, /*extr->ips->ips_said.proto,*/
  52650. + 0,
  52651. + extr->ips->ips_addr_d),
  52652. + extensions_reply)
  52653. + && (extr->ips->ips_ident_s.data
  52654. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  52655. + SADB_EXT_IDENTITY_SRC,
  52656. + extr->ips->ips_ident_s.type,
  52657. + extr->ips->ips_ident_s.id,
  52658. + extr->ips->ips_ident_s.len,
  52659. + extr->ips->ips_ident_s.data),
  52660. + extensions_reply) : 1)
  52661. + && (extr->ips->ips_ident_d.data
  52662. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  52663. + SADB_EXT_IDENTITY_DST,
  52664. + extr->ips->ips_ident_d.type,
  52665. + extr->ips->ips_ident_d.id,
  52666. + extr->ips->ips_ident_d.len,
  52667. + extr->ips->ips_ident_d.data),
  52668. + extensions_reply) : 1)
  52669. +#if 0
  52670. + /* FIXME: This won't work yet because I have not finished
  52671. + it. */
  52672. + && (extr->ips->ips_sens_
  52673. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  52674. + extr->ips->ips_sens_dpd,
  52675. + extr->ips->ips_sens_sens_level,
  52676. + extr->ips->ips_sens_sens_len,
  52677. + extr->ips->ips_sens_sens_bitmap,
  52678. + extr->ips->ips_sens_integ_level,
  52679. + extr->ips->ips_sens_integ_len,
  52680. + extr->ips->ips_sens_integ_bitmap),
  52681. + extensions_reply) : 1)
  52682. +#endif
  52683. + )) {
  52684. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52685. + "failed to build the update reply message extensions\n");
  52686. + SENDERR(-error);
  52687. + }
  52688. +
  52689. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52690. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52691. + "failed to build the update reply message\n");
  52692. + SENDERR(-error);
  52693. + }
  52694. + for(pfkey_socketsp = pfkey_open_sockets;
  52695. + pfkey_socketsp;
  52696. + pfkey_socketsp = pfkey_socketsp->next) {
  52697. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52698. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52699. + "sending up update reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52700. + satype,
  52701. + satype2name(satype),
  52702. + pfkey_socketsp->socketp,
  52703. + error);
  52704. + SENDERR(-error);
  52705. + }
  52706. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52707. + "sending up update reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52708. + satype,
  52709. + satype2name(satype),
  52710. + pfkey_socketsp->socketp);
  52711. + }
  52712. +
  52713. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52714. + if (nat_t_ips_saved) {
  52715. + /**
  52716. + * As we _really_ update existing SA, we keep tdbq and need to delete
  52717. + * parsed ips (nat_t_ips_saved, was extr->ips).
  52718. + *
  52719. + * goto errlab with extr->ips = nat_t_ips_saved will free it.
  52720. + */
  52721. +
  52722. + extr->ips = nat_t_ips_saved;
  52723. +
  52724. + error = 0;
  52725. + KLIPS_PRINT(debug_pfkey,
  52726. + "klips_debug:pfkey_update_parse (NAT-T ports): "
  52727. + "successful for SA: %s\n",
  52728. + sa_len ? sa : " (error)");
  52729. +
  52730. + goto errlab;
  52731. + }
  52732. +#endif
  52733. +
  52734. + if((error = ipsec_sa_add(extr->ips))) {
  52735. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52736. + "failed to update the mature SA=%s with error=%d.\n",
  52737. + sa_len ? sa : " (error)",
  52738. + error);
  52739. + SENDERR(-error);
  52740. + }
  52741. + extr->ips = NULL;
  52742. +
  52743. + KLIPS_PRINT(debug_pfkey,
  52744. + "klips_debug:pfkey_update_parse: "
  52745. + "successful for SA: %s\n",
  52746. + sa_len ? sa : " (error)");
  52747. +
  52748. + errlab:
  52749. + if (pfkey_reply) {
  52750. + pfkey_msg_free(&pfkey_reply);
  52751. + }
  52752. + pfkey_extensions_free(extensions_reply);
  52753. + return error;
  52754. +}
  52755. +
  52756. +DEBUG_NO_STATIC int
  52757. +pfkey_add_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52758. +{
  52759. + int error = 0;
  52760. + struct ipsec_sa* ipsq;
  52761. + char sa[SATOT_BUF];
  52762. + size_t sa_len;
  52763. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52764. + struct sadb_msg *pfkey_reply = NULL;
  52765. + struct socket_list *pfkey_socketsp;
  52766. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52767. +
  52768. + KLIPS_PRINT(debug_pfkey,
  52769. + "klips_debug:pfkey_add_parse: .\n");
  52770. +
  52771. + pfkey_extensions_init(extensions_reply);
  52772. +
  52773. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state != SADB_SASTATE_MATURE) {
  52774. + KLIPS_PRINT(debug_pfkey,
  52775. + "klips_debug:pfkey_add_parse: "
  52776. + "error, sa_state=%d must be MATURE=%d\n",
  52777. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  52778. + SADB_SASTATE_MATURE);
  52779. + SENDERR(EINVAL);
  52780. + }
  52781. +
  52782. + if(!extr || !extr->ips) {
  52783. + KLIPS_PRINT(debug_pfkey,
  52784. + "klips_debug:pfkey_add_parse: "
  52785. + "extr or extr->ips pointer NULL\n");
  52786. + SENDERR(EINVAL);
  52787. + }
  52788. +
  52789. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52790. +
  52791. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52792. + if(ipsq != NULL) {
  52793. + ipsec_sa_put(ipsq);
  52794. + KLIPS_PRINT(debug_pfkey,
  52795. + "klips_debug:pfkey_add_parse: "
  52796. + "found an old ipsec_sa for SA%s, delete it first.\n",
  52797. + sa_len ? sa : " (error)");
  52798. + SENDERR(EEXIST);
  52799. + }
  52800. +
  52801. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52802. + extr->ips->ips_flags |= EMT_INBOUND;
  52803. + }
  52804. +
  52805. + KLIPS_PRINT(debug_pfkey,
  52806. + "klips_debug:pfkey_add_parse: "
  52807. + "existing ipsec_sa not found (this is good) for SA%s, %s-bound, allocating.\n",
  52808. + sa_len ? sa : " (error)",
  52809. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52810. +
  52811. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52812. + extr->ips->ips_rcvif = NULL;
  52813. +
  52814. + if ((error = pfkey_ipsec_sa_init(extr->ips))) {
  52815. + KLIPS_PRINT(debug_pfkey,
  52816. + "klips_debug:pfkey_add_parse: "
  52817. + "not successful for SA: %s, deleting.\n",
  52818. + sa_len ? sa : " (error)");
  52819. + SENDERR(-error);
  52820. + }
  52821. +
  52822. + extr->ips->ips_life.ipl_addtime.ipl_count = jiffies / HZ;
  52823. + if(!extr->ips->ips_life.ipl_allocations.ipl_count) {
  52824. + extr->ips->ips_life.ipl_allocations.ipl_count += 1;
  52825. + }
  52826. +
  52827. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52828. + SADB_ADD,
  52829. + satype,
  52830. + 0,
  52831. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52832. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52833. + extensions_reply)
  52834. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52835. + SADB_EXT_SA,
  52836. + extr->ips->ips_said.spi,
  52837. + extr->ips->ips_replaywin,
  52838. + extr->ips->ips_state,
  52839. + extr->ips->ips_authalg,
  52840. + extr->ips->ips_encalg,
  52841. + extr->ips->ips_flags,
  52842. + extr->ips->ips_ref),
  52843. + extensions_reply)
  52844. + /* The 3 lifetime extentions should only be sent if non-zero. */
  52845. + && (extensions[SADB_EXT_LIFETIME_HARD]
  52846. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  52847. + SADB_EXT_LIFETIME_HARD,
  52848. + extr->ips->ips_life.ipl_allocations.ipl_hard,
  52849. + extr->ips->ips_life.ipl_bytes.ipl_hard,
  52850. + extr->ips->ips_life.ipl_addtime.ipl_hard,
  52851. + extr->ips->ips_life.ipl_usetime.ipl_hard,
  52852. + extr->ips->ips_life.ipl_packets.ipl_hard),
  52853. + extensions_reply) : 1)
  52854. + && (extensions[SADB_EXT_LIFETIME_SOFT]
  52855. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  52856. + SADB_EXT_LIFETIME_SOFT,
  52857. + extr->ips->ips_life.ipl_allocations.ipl_soft,
  52858. + extr->ips->ips_life.ipl_bytes.ipl_soft,
  52859. + extr->ips->ips_life.ipl_addtime.ipl_soft,
  52860. + extr->ips->ips_life.ipl_usetime.ipl_soft,
  52861. + extr->ips->ips_life.ipl_packets.ipl_soft),
  52862. + extensions_reply) : 1)
  52863. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52864. + SADB_EXT_ADDRESS_SRC,
  52865. + 0, /*extr->ips->ips_said.proto,*/
  52866. + 0,
  52867. + extr->ips->ips_addr_s),
  52868. + extensions_reply)
  52869. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52870. + SADB_EXT_ADDRESS_DST,
  52871. + 0, /*extr->ips->ips_said.proto,*/
  52872. + 0,
  52873. + extr->ips->ips_addr_d),
  52874. + extensions_reply)
  52875. + && (extr->ips->ips_ident_s.data
  52876. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  52877. + SADB_EXT_IDENTITY_SRC,
  52878. + extr->ips->ips_ident_s.type,
  52879. + extr->ips->ips_ident_s.id,
  52880. + extr->ips->ips_ident_s.len,
  52881. + extr->ips->ips_ident_s.data),
  52882. + extensions_reply) : 1)
  52883. + && (extr->ips->ips_ident_d.data
  52884. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  52885. + SADB_EXT_IDENTITY_DST,
  52886. + extr->ips->ips_ident_d.type,
  52887. + extr->ips->ips_ident_d.id,
  52888. + extr->ips->ips_ident_d.len,
  52889. + extr->ips->ips_ident_d.data),
  52890. + extensions_reply) : 1)
  52891. +#if 0
  52892. + /* FIXME: This won't work yet because I have not finished
  52893. + it. */
  52894. + && (extr->ips->ips_sens_
  52895. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  52896. + extr->ips->ips_sens_dpd,
  52897. + extr->ips->ips_sens_sens_level,
  52898. + extr->ips->ips_sens_sens_len,
  52899. + extr->ips->ips_sens_sens_bitmap,
  52900. + extr->ips->ips_sens_integ_level,
  52901. + extr->ips->ips_sens_integ_len,
  52902. + extr->ips->ips_sens_integ_bitmap),
  52903. + extensions_reply) : 1)
  52904. +#endif
  52905. + )) {
  52906. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52907. + "failed to build the add reply message extensions\n");
  52908. + SENDERR(-error);
  52909. + }
  52910. +
  52911. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52912. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52913. + "failed to build the add reply message\n");
  52914. + SENDERR(-error);
  52915. + }
  52916. + for(pfkey_socketsp = pfkey_open_sockets;
  52917. + pfkey_socketsp;
  52918. + pfkey_socketsp = pfkey_socketsp->next) {
  52919. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52920. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52921. + "sending up add reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52922. + satype,
  52923. + satype2name(satype),
  52924. + pfkey_socketsp->socketp,
  52925. + error);
  52926. + SENDERR(-error);
  52927. + }
  52928. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52929. + "sending up add reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52930. + satype,
  52931. + satype2name(satype),
  52932. + pfkey_socketsp->socketp);
  52933. + }
  52934. +
  52935. + if((error = ipsec_sa_add(extr->ips))) {
  52936. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52937. + "failed to add the mature SA=%s with error=%d.\n",
  52938. + sa_len ? sa : " (error)",
  52939. + error);
  52940. + SENDERR(-error);
  52941. + }
  52942. + extr->ips = NULL;
  52943. +
  52944. + KLIPS_PRINT(debug_pfkey,
  52945. + "klips_debug:pfkey_add_parse: "
  52946. + "successful for SA: %s\n",
  52947. + sa_len ? sa : " (error)");
  52948. +
  52949. + errlab:
  52950. + if (pfkey_reply) {
  52951. + pfkey_msg_free(&pfkey_reply);
  52952. + }
  52953. + pfkey_extensions_free(extensions_reply);
  52954. + return error;
  52955. +}
  52956. +
  52957. +DEBUG_NO_STATIC int
  52958. +pfkey_delete_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52959. +{
  52960. + struct ipsec_sa *ipsp;
  52961. + char sa[SATOT_BUF];
  52962. + size_t sa_len;
  52963. + int error = 0;
  52964. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52965. + struct sadb_msg *pfkey_reply = NULL;
  52966. + struct socket_list *pfkey_socketsp;
  52967. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52968. +
  52969. + KLIPS_PRINT(debug_pfkey,
  52970. + "klips_debug:pfkey_delete_parse: .\n");
  52971. +
  52972. + pfkey_extensions_init(extensions_reply);
  52973. +
  52974. + if(!extr || !extr->ips) {
  52975. + KLIPS_PRINT(debug_pfkey,
  52976. + "klips_debug:pfkey_delete_parse: "
  52977. + "extr or extr->ips pointer NULL, fatal\n");
  52978. + SENDERR(EINVAL);
  52979. + }
  52980. +
  52981. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52982. +
  52983. + spin_lock_bh(&tdb_lock);
  52984. +
  52985. + ipsp = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52986. + if (ipsp == NULL) {
  52987. + spin_unlock_bh(&tdb_lock);
  52988. + KLIPS_PRINT(debug_pfkey,
  52989. + "klips_debug:pfkey_delete_parse: "
  52990. + "ipsec_sa not found for SA:%s, could not delete.\n",
  52991. + sa_len ? sa : " (error)");
  52992. + SENDERR(ESRCH);
  52993. + }
  52994. +
  52995. + ipsec_sa_put(ipsp);
  52996. + if((error = ipsec_sa_delchain(ipsp))) {
  52997. + spin_unlock_bh(&tdb_lock);
  52998. + KLIPS_PRINT(debug_pfkey,
  52999. + "klips_debug:pfkey_delete_parse: "
  53000. + "error=%d returned trying to delete ipsec_sa for SA:%s.\n",
  53001. + error,
  53002. + sa_len ? sa : " (error)");
  53003. + SENDERR(-error);
  53004. + }
  53005. + spin_unlock_bh(&tdb_lock);
  53006. +
  53007. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53008. + SADB_DELETE,
  53009. + satype,
  53010. + 0,
  53011. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53012. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53013. + extensions_reply)
  53014. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53015. + SADB_EXT_SA,
  53016. + extr->ips->ips_said.spi,
  53017. + 0,
  53018. + 0,
  53019. + 0,
  53020. + 0,
  53021. + 0,
  53022. + extr->ips->ips_ref),
  53023. + extensions_reply)
  53024. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  53025. + SADB_EXT_ADDRESS_SRC,
  53026. + 0, /*extr->ips->ips_said.proto,*/
  53027. + 0,
  53028. + extr->ips->ips_addr_s),
  53029. + extensions_reply)
  53030. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53031. + SADB_EXT_ADDRESS_DST,
  53032. + 0, /*extr->ips->ips_said.proto,*/
  53033. + 0,
  53034. + extr->ips->ips_addr_d),
  53035. + extensions_reply)
  53036. + )) {
  53037. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53038. + "failed to build the delete reply message extensions\n");
  53039. + SENDERR(-error);
  53040. + }
  53041. +
  53042. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53043. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53044. + "failed to build the delete reply message\n");
  53045. + SENDERR(-error);
  53046. + }
  53047. + for(pfkey_socketsp = pfkey_open_sockets;
  53048. + pfkey_socketsp;
  53049. + pfkey_socketsp = pfkey_socketsp->next) {
  53050. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53051. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53052. + "sending up delete reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53053. + satype,
  53054. + satype2name(satype),
  53055. + pfkey_socketsp->socketp,
  53056. + error);
  53057. + SENDERR(-error);
  53058. + }
  53059. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53060. + "sending up delete reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53061. + satype,
  53062. + satype2name(satype),
  53063. + pfkey_socketsp->socketp);
  53064. + }
  53065. +
  53066. + errlab:
  53067. + if (pfkey_reply) {
  53068. + pfkey_msg_free(&pfkey_reply);
  53069. + }
  53070. + pfkey_extensions_free(extensions_reply);
  53071. + return error;
  53072. +}
  53073. +
  53074. +DEBUG_NO_STATIC int
  53075. +pfkey_get_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53076. +{
  53077. + int error = 0;
  53078. + struct ipsec_sa *ipsp;
  53079. + char sa[SATOT_BUF];
  53080. + size_t sa_len;
  53081. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53082. + struct sadb_msg *pfkey_reply = NULL;
  53083. +
  53084. + KLIPS_PRINT(debug_pfkey,
  53085. + "klips_debug:pfkey_get_parse: .\n");
  53086. +
  53087. + pfkey_extensions_init(extensions_reply);
  53088. +
  53089. + if(!extr || !extr->ips) {
  53090. + KLIPS_PRINT(debug_pfkey,
  53091. + "klips_debug:pfkey_get_parse: "
  53092. + "extr or extr->ips pointer NULL, fatal\n");
  53093. + SENDERR(EINVAL);
  53094. + }
  53095. +
  53096. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  53097. +
  53098. + spin_lock_bh(&tdb_lock);
  53099. +
  53100. + ipsp = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53101. + if (ipsp == NULL) {
  53102. + spin_unlock_bh(&tdb_lock);
  53103. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53104. + "ipsec_sa not found for SA=%s, could not get.\n",
  53105. + sa_len ? sa : " (error)");
  53106. + SENDERR(ESRCH);
  53107. + }
  53108. +
  53109. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53110. + SADB_GET,
  53111. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype,
  53112. + 0,
  53113. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53114. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53115. + extensions_reply)
  53116. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53117. + SADB_EXT_SA,
  53118. + extr->ips->ips_said.spi,
  53119. + extr->ips->ips_replaywin,
  53120. + extr->ips->ips_state,
  53121. + extr->ips->ips_authalg,
  53122. + extr->ips->ips_encalg,
  53123. + extr->ips->ips_flags,
  53124. + extr->ips->ips_ref),
  53125. + extensions_reply)
  53126. + /* The 3 lifetime extentions should only be sent if non-zero. */
  53127. + && (ipsp->ips_life.ipl_allocations.ipl_count
  53128. + || ipsp->ips_life.ipl_bytes.ipl_count
  53129. + || ipsp->ips_life.ipl_addtime.ipl_count
  53130. + || ipsp->ips_life.ipl_usetime.ipl_count
  53131. + || ipsp->ips_life.ipl_packets.ipl_count
  53132. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_CURRENT],
  53133. + SADB_EXT_LIFETIME_CURRENT,
  53134. + ipsp->ips_life.ipl_allocations.ipl_count,
  53135. + ipsp->ips_life.ipl_bytes.ipl_count,
  53136. + ipsp->ips_life.ipl_addtime.ipl_count,
  53137. + ipsp->ips_life.ipl_usetime.ipl_count,
  53138. + ipsp->ips_life.ipl_packets.ipl_count),
  53139. + extensions_reply) : 1)
  53140. + && (ipsp->ips_life.ipl_allocations.ipl_hard
  53141. + || ipsp->ips_life.ipl_bytes.ipl_hard
  53142. + || ipsp->ips_life.ipl_addtime.ipl_hard
  53143. + || ipsp->ips_life.ipl_usetime.ipl_hard
  53144. + || ipsp->ips_life.ipl_packets.ipl_hard
  53145. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  53146. + SADB_EXT_LIFETIME_HARD,
  53147. + ipsp->ips_life.ipl_allocations.ipl_hard,
  53148. + ipsp->ips_life.ipl_bytes.ipl_hard,
  53149. + ipsp->ips_life.ipl_addtime.ipl_hard,
  53150. + ipsp->ips_life.ipl_usetime.ipl_hard,
  53151. + ipsp->ips_life.ipl_packets.ipl_hard),
  53152. + extensions_reply) : 1)
  53153. + && (ipsp->ips_life.ipl_allocations.ipl_soft
  53154. + || ipsp->ips_life.ipl_bytes.ipl_soft
  53155. + || ipsp->ips_life.ipl_addtime.ipl_soft
  53156. + || ipsp->ips_life.ipl_usetime.ipl_soft
  53157. + || ipsp->ips_life.ipl_packets.ipl_soft
  53158. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  53159. + SADB_EXT_LIFETIME_SOFT,
  53160. + ipsp->ips_life.ipl_allocations.ipl_soft,
  53161. + ipsp->ips_life.ipl_bytes.ipl_soft,
  53162. + ipsp->ips_life.ipl_addtime.ipl_soft,
  53163. + ipsp->ips_life.ipl_usetime.ipl_soft,
  53164. + ipsp->ips_life.ipl_packets.ipl_soft),
  53165. + extensions_reply) : 1)
  53166. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  53167. + SADB_EXT_ADDRESS_SRC,
  53168. + 0, /*extr->ips->ips_said.proto,*/
  53169. + 0,
  53170. + extr->ips->ips_addr_s),
  53171. + extensions_reply)
  53172. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53173. + SADB_EXT_ADDRESS_DST,
  53174. + 0, /*extr->ips->ips_said.proto,*/
  53175. + 0,
  53176. + extr->ips->ips_addr_d),
  53177. + extensions_reply)
  53178. + && (extr->ips->ips_addr_p
  53179. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_PROXY],
  53180. + SADB_EXT_ADDRESS_PROXY,
  53181. + 0, /*extr->ips->ips_said.proto,*/
  53182. + 0,
  53183. + extr->ips->ips_addr_p),
  53184. + extensions_reply) : 1)
  53185. +#if 0
  53186. + /* FIXME: This won't work yet because the keys are not
  53187. + stored directly in the ipsec_sa. They are stored as
  53188. + contexts. */
  53189. + && (extr->ips->ips_key_a_size
  53190. + ? pfkey_safe_build(error = pfkey_key_build(&extensions_reply[SADB_EXT_KEY_AUTH],
  53191. + SADB_EXT_KEY_AUTH,
  53192. + extr->ips->ips_key_a_size * 8,
  53193. + extr->ips->ips_key_a),
  53194. + extensions_reply) : 1)
  53195. + /* FIXME: This won't work yet because the keys are not
  53196. + stored directly in the ipsec_sa. They are stored as
  53197. + key schedules. */
  53198. + && (extr->ips->ips_key_e_size
  53199. + ? pfkey_safe_build(error = pfkey_key_build(&extensions_reply[SADB_EXT_KEY_ENCRYPT],
  53200. + SADB_EXT_KEY_ENCRYPT,
  53201. + extr->ips->ips_key_e_size * 8,
  53202. + extr->ips->ips_key_e),
  53203. + extensions_reply) : 1)
  53204. +#endif
  53205. + && (extr->ips->ips_ident_s.data
  53206. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  53207. + SADB_EXT_IDENTITY_SRC,
  53208. + extr->ips->ips_ident_s.type,
  53209. + extr->ips->ips_ident_s.id,
  53210. + extr->ips->ips_ident_s.len,
  53211. + extr->ips->ips_ident_s.data),
  53212. + extensions_reply) : 1)
  53213. + && (extr->ips->ips_ident_d.data
  53214. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  53215. + SADB_EXT_IDENTITY_DST,
  53216. + extr->ips->ips_ident_d.type,
  53217. + extr->ips->ips_ident_d.id,
  53218. + extr->ips->ips_ident_d.len,
  53219. + extr->ips->ips_ident_d.data),
  53220. + extensions_reply) : 1)
  53221. +#if 0
  53222. + /* FIXME: This won't work yet because I have not finished
  53223. + it. */
  53224. + && (extr->ips->ips_sens_
  53225. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  53226. + extr->ips->ips_sens_dpd,
  53227. + extr->ips->ips_sens_sens_level,
  53228. + extr->ips->ips_sens_sens_len,
  53229. + extr->ips->ips_sens_sens_bitmap,
  53230. + extr->ips->ips_sens_integ_level,
  53231. + extr->ips->ips_sens_integ_len,
  53232. + extr->ips->ips_sens_integ_bitmap),
  53233. + extensions_reply) : 1)
  53234. +#endif
  53235. + )) {
  53236. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53237. + "failed to build the get reply message extensions\n");
  53238. + ipsec_sa_put(ipsp);
  53239. + spin_unlock_bh(&tdb_lock);
  53240. + SENDERR(-error);
  53241. + }
  53242. +
  53243. + ipsec_sa_put(ipsp);
  53244. + spin_unlock_bh(&tdb_lock);
  53245. +
  53246. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53247. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53248. + "failed to build the get reply message\n");
  53249. + SENDERR(-error);
  53250. + }
  53251. +
  53252. + if((error = pfkey_upmsg(sk->sk_socket, pfkey_reply))) {
  53253. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53254. + "failed to send the get reply message\n");
  53255. + SENDERR(-error);
  53256. + }
  53257. +
  53258. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53259. + "succeeded in sending get reply message.\n");
  53260. +
  53261. + errlab:
  53262. + if (pfkey_reply) {
  53263. + pfkey_msg_free(&pfkey_reply);
  53264. + }
  53265. + pfkey_extensions_free(extensions_reply);
  53266. + return error;
  53267. +}
  53268. +
  53269. +DEBUG_NO_STATIC int
  53270. +pfkey_acquire_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53271. +{
  53272. + int error = 0;
  53273. + struct socket_list *pfkey_socketsp;
  53274. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53275. +
  53276. + KLIPS_PRINT(debug_pfkey,
  53277. + "klips_debug:pfkey_acquire_parse: .\n");
  53278. +
  53279. + /* XXX I don't know if we want an upper bound, since userspace may
  53280. + want to register itself for an satype > SADB_SATYPE_MAX. */
  53281. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53282. + KLIPS_PRINT(debug_pfkey,
  53283. + "klips_debug:pfkey_acquire_parse: "
  53284. + "SATYPE=%d invalid.\n",
  53285. + satype);
  53286. + SENDERR(EINVAL);
  53287. + }
  53288. +
  53289. + if(!(pfkey_registered_sockets[satype])) {
  53290. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53291. + "no sockets registered for SAtype=%d(%s).\n",
  53292. + satype,
  53293. + satype2name(satype));
  53294. + SENDERR(EPROTONOSUPPORT);
  53295. + }
  53296. +
  53297. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  53298. + pfkey_socketsp;
  53299. + pfkey_socketsp = pfkey_socketsp->next) {
  53300. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53301. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53302. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53303. + "sending up acquire reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53304. + satype,
  53305. + satype2name(satype),
  53306. + pfkey_socketsp->socketp,
  53307. + error);
  53308. + SENDERR(-error);
  53309. + }
  53310. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53311. + "sending up acquire reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53312. + satype,
  53313. + satype2name(satype),
  53314. + pfkey_socketsp->socketp);
  53315. + }
  53316. +
  53317. + errlab:
  53318. + return error;
  53319. +}
  53320. +
  53321. +DEBUG_NO_STATIC int
  53322. +pfkey_register_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53323. +{
  53324. + int error = 0;
  53325. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53326. +
  53327. + KLIPS_PRINT(debug_pfkey,
  53328. + "klips_debug:pfkey_register_parse: .\n");
  53329. +
  53330. + /* XXX I don't know if we want an upper bound, since userspace may
  53331. + want to register itself for an satype > SADB_SATYPE_MAX. */
  53332. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53333. + KLIPS_PRINT(debug_pfkey,
  53334. + "klips_debug:pfkey_register_parse: "
  53335. + "SATYPE=%d invalid.\n",
  53336. + satype);
  53337. + SENDERR(EINVAL);
  53338. + }
  53339. +
  53340. + if(!pfkey_list_insert_socket(sk->sk_socket,
  53341. + &(pfkey_registered_sockets[satype]))) {
  53342. + KLIPS_PRINT(debug_pfkey,
  53343. + "klips_debug:pfkey_register_parse: "
  53344. + "SATYPE=%02d(%s) successfully registered by KMd (pid=%d).\n",
  53345. + satype,
  53346. + satype2name(satype),
  53347. + key_pid(sk));
  53348. + };
  53349. +
  53350. + /* send up register msg with supported SATYPE algos */
  53351. +
  53352. + error=pfkey_register_reply(satype, (struct sadb_msg*)extensions[SADB_EXT_RESERVED]);
  53353. + errlab:
  53354. + return error;
  53355. +}
  53356. +
  53357. +int
  53358. +pfkey_register_reply(int satype, struct sadb_msg *sadb_msg)
  53359. +{
  53360. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53361. + struct sadb_msg *pfkey_reply = NULL;
  53362. + struct socket_list *pfkey_socketsp;
  53363. + struct supported_list *pfkey_supported_listp;
  53364. + unsigned int alg_num_a = 0, alg_num_e = 0;
  53365. + struct sadb_alg *alg_a = NULL, *alg_e = NULL, *alg_ap = NULL, *alg_ep = NULL;
  53366. + int error = 0;
  53367. +
  53368. + pfkey_extensions_init(extensions_reply);
  53369. +
  53370. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53371. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53372. + "SAtype=%d unspecified or unknown.\n",
  53373. + satype);
  53374. + SENDERR(EINVAL);
  53375. + }
  53376. + if(!(pfkey_registered_sockets[satype])) {
  53377. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53378. + "no sockets registered for SAtype=%d(%s).\n",
  53379. + satype,
  53380. + satype2name(satype));
  53381. + SENDERR(EPROTONOSUPPORT);
  53382. + }
  53383. + /* send up register msg with supported SATYPE algos */
  53384. + pfkey_supported_listp = pfkey_supported_list[satype];
  53385. + KLIPS_PRINT(debug_pfkey,
  53386. + "klips_debug:pfkey_register_reply: "
  53387. + "pfkey_supported_list[%d]=0p%p\n",
  53388. + satype,
  53389. + pfkey_supported_list[satype]);
  53390. + while(pfkey_supported_listp) {
  53391. + KLIPS_PRINT(debug_pfkey,
  53392. + "klips_debug:pfkey_register_reply: "
  53393. + "checking supported=0p%p\n",
  53394. + pfkey_supported_listp);
  53395. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_AUTH) {
  53396. + KLIPS_PRINT(debug_pfkey,
  53397. + "klips_debug:pfkey_register_reply: "
  53398. + "adding auth alg.\n");
  53399. + alg_num_a++;
  53400. + }
  53401. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_ENCRYPT) {
  53402. + KLIPS_PRINT(debug_pfkey,
  53403. + "klips_debug:pfkey_register_reply: "
  53404. + "adding encrypt alg.\n");
  53405. + alg_num_e++;
  53406. + }
  53407. + pfkey_supported_listp = pfkey_supported_listp->next;
  53408. + }
  53409. +
  53410. + if(alg_num_a) {
  53411. + KLIPS_PRINT(debug_pfkey,
  53412. + "klips_debug:pfkey_register_reply: "
  53413. + "allocating %lu bytes for auth algs.\n",
  53414. + (unsigned long) (alg_num_a * sizeof(struct sadb_alg)));
  53415. + if((alg_a = kmalloc(alg_num_a * sizeof(struct sadb_alg), GFP_ATOMIC) ) == NULL) {
  53416. + KLIPS_PRINT(debug_pfkey,
  53417. + "klips_debug:pfkey_register_reply: "
  53418. + "auth alg memory allocation error\n");
  53419. + SENDERR(ENOMEM);
  53420. + }
  53421. + alg_ap = alg_a;
  53422. + }
  53423. +
  53424. + if(alg_num_e) {
  53425. + KLIPS_PRINT(debug_pfkey,
  53426. + "klips_debug:pfkey_register_reply: "
  53427. + "allocating %lu bytes for enc algs.\n",
  53428. + (unsigned long) (alg_num_e * sizeof(struct sadb_alg)));
  53429. + if((alg_e = kmalloc(alg_num_e * sizeof(struct sadb_alg), GFP_ATOMIC) ) == NULL) {
  53430. + KLIPS_PRINT(debug_pfkey,
  53431. + "klips_debug:pfkey_register_reply: "
  53432. + "enc alg memory allocation error\n");
  53433. + SENDERR(ENOMEM);
  53434. + }
  53435. + alg_ep = alg_e;
  53436. + }
  53437. +
  53438. + pfkey_supported_listp = pfkey_supported_list[satype];
  53439. + while(pfkey_supported_listp) {
  53440. + if(alg_num_a) {
  53441. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_AUTH) {
  53442. + alg_ap->sadb_alg_id = pfkey_supported_listp->supportedp->ias_id;
  53443. + alg_ap->sadb_alg_ivlen = pfkey_supported_listp->supportedp->ias_ivlen;
  53444. + alg_ap->sadb_alg_minbits = pfkey_supported_listp->supportedp->ias_keyminbits;
  53445. + alg_ap->sadb_alg_maxbits = pfkey_supported_listp->supportedp->ias_keymaxbits;
  53446. + alg_ap->sadb_alg_reserved = 0;
  53447. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  53448. + "klips_debug:pfkey_register_reply: "
  53449. + "adding auth=0p%p\n",
  53450. + alg_ap);
  53451. + alg_ap++;
  53452. + }
  53453. + }
  53454. + if(alg_num_e) {
  53455. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_ENCRYPT) {
  53456. + alg_ep->sadb_alg_id = pfkey_supported_listp->supportedp->ias_id;
  53457. + alg_ep->sadb_alg_ivlen = pfkey_supported_listp->supportedp->ias_ivlen;
  53458. + alg_ep->sadb_alg_minbits = pfkey_supported_listp->supportedp->ias_keyminbits;
  53459. + alg_ep->sadb_alg_maxbits = pfkey_supported_listp->supportedp->ias_keymaxbits;
  53460. + alg_ep->sadb_alg_reserved = 0;
  53461. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  53462. + "klips_debug:pfkey_register_reply: "
  53463. + "adding encrypt=0p%p\n",
  53464. + alg_ep);
  53465. + alg_ep++;
  53466. + }
  53467. + }
  53468. + KLIPS_PRINT(debug_pfkey,
  53469. + "klips_debug:pfkey_register_reply: "
  53470. + "found satype=%d(%s) exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d.\n",
  53471. + satype,
  53472. + satype2name(satype),
  53473. + pfkey_supported_listp->supportedp->ias_exttype,
  53474. + pfkey_supported_listp->supportedp->ias_id,
  53475. + pfkey_supported_listp->supportedp->ias_ivlen,
  53476. + pfkey_supported_listp->supportedp->ias_keyminbits,
  53477. + pfkey_supported_listp->supportedp->ias_keymaxbits);
  53478. + pfkey_supported_listp = pfkey_supported_listp->next;
  53479. + }
  53480. +
  53481. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53482. + SADB_REGISTER,
  53483. + satype,
  53484. + 0,
  53485. + sadb_msg? sadb_msg->sadb_msg_seq : ++pfkey_msg_seq,
  53486. + sadb_msg? sadb_msg->sadb_msg_pid: current->pid),
  53487. + extensions_reply) &&
  53488. + (alg_num_a ? pfkey_safe_build(error = pfkey_supported_build(&extensions_reply[SADB_EXT_SUPPORTED_AUTH],
  53489. + SADB_EXT_SUPPORTED_AUTH,
  53490. + alg_num_a,
  53491. + alg_a),
  53492. + extensions_reply) : 1) &&
  53493. + (alg_num_e ? pfkey_safe_build(error = pfkey_supported_build(&extensions_reply[SADB_EXT_SUPPORTED_ENCRYPT],
  53494. + SADB_EXT_SUPPORTED_ENCRYPT,
  53495. + alg_num_e,
  53496. + alg_e),
  53497. + extensions_reply) : 1))) {
  53498. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53499. + "failed to build the register message extensions_reply\n");
  53500. + SENDERR(-error);
  53501. + }
  53502. +
  53503. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53504. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53505. + "failed to build the register message\n");
  53506. + SENDERR(-error);
  53507. + }
  53508. + /* this should go to all registered sockets for that satype only */
  53509. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  53510. + pfkey_socketsp;
  53511. + pfkey_socketsp = pfkey_socketsp->next) {
  53512. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53513. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53514. + "sending up acquire 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_register_reply: "
  53522. + "sending up register message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53523. + satype,
  53524. + satype2name(satype),
  53525. + pfkey_socketsp->socketp);
  53526. + }
  53527. +
  53528. + errlab:
  53529. + if(alg_a) {
  53530. + kfree(alg_a);
  53531. + }
  53532. + if(alg_e) {
  53533. + kfree(alg_e);
  53534. + }
  53535. +
  53536. + if (pfkey_reply) {
  53537. + pfkey_msg_free(&pfkey_reply);
  53538. + }
  53539. + pfkey_extensions_free(extensions_reply);
  53540. + return error;
  53541. +}
  53542. +
  53543. +DEBUG_NO_STATIC int
  53544. +pfkey_expire_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53545. +{
  53546. + int error = 0;
  53547. + struct socket_list *pfkey_socketsp;
  53548. +#ifdef CONFIG_KLIPS_DEBUG
  53549. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53550. +#endif /* CONFIG_KLIPS_DEBUG */
  53551. +
  53552. + KLIPS_PRINT(debug_pfkey,
  53553. + "klips_debug:pfkey_expire_parse: .\n");
  53554. +
  53555. + if(pfkey_open_sockets) {
  53556. + for(pfkey_socketsp = pfkey_open_sockets;
  53557. + pfkey_socketsp;
  53558. + pfkey_socketsp = pfkey_socketsp->next) {
  53559. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53560. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53561. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire_parse: "
  53562. + "sending up expire reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53563. + satype,
  53564. + satype2name(satype),
  53565. + pfkey_socketsp->socketp,
  53566. + error);
  53567. + SENDERR(-error);
  53568. + }
  53569. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire_parse: "
  53570. + "sending up expire reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53571. + satype,
  53572. + satype2name(satype),
  53573. + pfkey_socketsp->socketp);
  53574. + }
  53575. + }
  53576. +
  53577. + errlab:
  53578. + return error;
  53579. +}
  53580. +
  53581. +DEBUG_NO_STATIC int
  53582. +pfkey_flush_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53583. +{
  53584. + int error = 0;
  53585. + struct socket_list *pfkey_socketsp;
  53586. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53587. + uint8_t proto = 0;
  53588. +
  53589. + KLIPS_PRINT(debug_pfkey,
  53590. + "klips_debug:pfkey_flush_parse: "
  53591. + "flushing type %d SAs\n",
  53592. + satype);
  53593. +
  53594. + if(satype && !(proto = satype2proto(satype))) {
  53595. + KLIPS_PRINT(debug_pfkey,
  53596. + "klips_debug:pfkey_flush_parse: "
  53597. + "satype %d lookup failed.\n",
  53598. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  53599. + SENDERR(EINVAL);
  53600. + }
  53601. +
  53602. + if ((error = ipsec_sadb_cleanup(proto))) {
  53603. + SENDERR(-error);
  53604. + }
  53605. +
  53606. + if(pfkey_open_sockets) {
  53607. + for(pfkey_socketsp = pfkey_open_sockets;
  53608. + pfkey_socketsp;
  53609. + pfkey_socketsp = pfkey_socketsp->next) {
  53610. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53611. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53612. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_flush_parse: "
  53613. + "sending up flush reply message for satype=%d(%s) (proto=%d) to socket=0p%p failed with error=%d.\n",
  53614. + satype,
  53615. + satype2name(satype),
  53616. + proto,
  53617. + pfkey_socketsp->socketp,
  53618. + error);
  53619. + SENDERR(-error);
  53620. + }
  53621. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_flush_parse: "
  53622. + "sending up flush reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53623. + satype,
  53624. + satype2name(satype),
  53625. + pfkey_socketsp->socketp);
  53626. + }
  53627. + }
  53628. +
  53629. + errlab:
  53630. + return error;
  53631. +}
  53632. +
  53633. +DEBUG_NO_STATIC int
  53634. +pfkey_dump_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53635. +{
  53636. + int error = 0;
  53637. +
  53638. + KLIPS_PRINT(debug_pfkey,
  53639. + "klips_debug:pfkey_dump_parse: .\n");
  53640. +
  53641. + SENDERR(ENOSYS);
  53642. + errlab:
  53643. + return error;
  53644. +}
  53645. +
  53646. +DEBUG_NO_STATIC int
  53647. +pfkey_x_promisc_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53648. +{
  53649. + int error = 0;
  53650. +
  53651. + KLIPS_PRINT(debug_pfkey,
  53652. + "klips_debug:pfkey_promisc_parse: .\n");
  53653. +
  53654. + SENDERR(ENOSYS);
  53655. + errlab:
  53656. + return error;
  53657. +}
  53658. +
  53659. +DEBUG_NO_STATIC int
  53660. +pfkey_x_pchange_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53661. +{
  53662. + int error = 0;
  53663. +
  53664. + KLIPS_PRINT(debug_pfkey,
  53665. + "klips_debug:pfkey_x_pchange_parse: .\n");
  53666. +
  53667. + SENDERR(ENOSYS);
  53668. + errlab:
  53669. + return error;
  53670. +}
  53671. +
  53672. +DEBUG_NO_STATIC int
  53673. +pfkey_x_grpsa_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53674. +{
  53675. + struct ipsec_sa *ips1p, *ips2p, *ipsp;
  53676. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53677. + struct sadb_msg *pfkey_reply = NULL;
  53678. + struct socket_list *pfkey_socketsp;
  53679. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53680. + char sa1[SATOT_BUF], sa2[SATOT_BUF];
  53681. + size_t sa_len1, sa_len2 = 0;
  53682. + int error = 0;
  53683. +
  53684. + KLIPS_PRINT(debug_pfkey,
  53685. + "klips_debug:pfkey_x_grpsa_parse: .\n");
  53686. +
  53687. + pfkey_extensions_init(extensions_reply);
  53688. +
  53689. + if(extr == NULL || extr->ips == NULL) {
  53690. + KLIPS_PRINT(debug_pfkey,
  53691. + "klips_debug:pfkey_x_grpsa_parse: "
  53692. + "extr or extr->ips is NULL, fatal.\n");
  53693. + SENDERR(EINVAL);
  53694. + }
  53695. +
  53696. + sa_len1 = satot(&extr->ips->ips_said, 0, sa1, sizeof(sa1));
  53697. + if(extr->ips2 != NULL) {
  53698. + sa_len2 = satot(&extr->ips2->ips_said, 0, sa2, sizeof(sa2));
  53699. + }
  53700. +
  53701. + spin_lock_bh(&tdb_lock);
  53702. +
  53703. + ips1p = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53704. + if(ips1p == NULL) {
  53705. + spin_unlock_bh(&tdb_lock);
  53706. + KLIPS_PRINT(debug_pfkey,
  53707. + "klips_debug:pfkey_x_grpsa_parse: "
  53708. + "reserved ipsec_sa for SA1: %s not found. Call SADB_ADD/UPDATE first.\n",
  53709. + sa_len1 ? sa1 : " (error)");
  53710. + SENDERR(ENOENT);
  53711. + }
  53712. + if(extr->ips2) { /* GRPSA */
  53713. + ips2p = ipsec_sa_getbyid(&(extr->ips2->ips_said));
  53714. + if(ips2p == NULL) {
  53715. + ipsec_sa_put(ips1p);
  53716. + spin_unlock_bh(&tdb_lock);
  53717. + KLIPS_PRINT(debug_pfkey,
  53718. + "klips_debug:pfkey_x_grpsa_parse: "
  53719. + "reserved ipsec_sa for SA2: %s not found. Call SADB_ADD/UPDATE first.\n",
  53720. + sa_len2 ? sa2 : " (error)");
  53721. + SENDERR(ENOENT);
  53722. + }
  53723. +
  53724. + /* Is either one already linked? */
  53725. + if(ips1p->ips_onext) {
  53726. + ipsec_sa_put(ips1p);
  53727. + ipsec_sa_put(ips2p);
  53728. + spin_unlock_bh(&tdb_lock);
  53729. + KLIPS_PRINT(debug_pfkey,
  53730. + "klips_debug:pfkey_x_grpsa_parse: "
  53731. + "ipsec_sa for SA: %s is already linked.\n",
  53732. + sa_len1 ? sa1 : " (error)");
  53733. + SENDERR(EEXIST);
  53734. + }
  53735. + if(ips2p->ips_inext) {
  53736. + ipsec_sa_put(ips1p);
  53737. + ipsec_sa_put(ips2p);
  53738. + spin_unlock_bh(&tdb_lock);
  53739. + KLIPS_PRINT(debug_pfkey,
  53740. + "klips_debug:pfkey_x_grpsa_parse: "
  53741. + "ipsec_sa for SA: %s is already linked.\n",
  53742. + sa_len2 ? sa2 : " (error)");
  53743. + SENDERR(EEXIST);
  53744. + }
  53745. +
  53746. + /* Is extr->ips already linked to extr->ips2? */
  53747. + ipsp = ips2p;
  53748. + while(ipsp) {
  53749. + if(ipsp == ips1p) {
  53750. + ipsec_sa_put(ips1p);
  53751. + ipsec_sa_put(ips2p);
  53752. + spin_unlock_bh(&tdb_lock);
  53753. + KLIPS_PRINT(debug_pfkey,
  53754. + "klips_debug:pfkey_x_grpsa_parse: "
  53755. + "ipsec_sa for SA: %s is already linked to %s.\n",
  53756. + sa_len1 ? sa1 : " (error)",
  53757. + sa_len2 ? sa2 : " (error)");
  53758. + SENDERR(EEXIST);
  53759. + }
  53760. + ipsp = ipsp->ips_onext;
  53761. + }
  53762. +
  53763. + /* link 'em */
  53764. + KLIPS_PRINT(debug_pfkey,
  53765. + "klips_debug:pfkey_x_grpsa_parse: "
  53766. + "linking ipsec_sa SA: %s with %s.\n",
  53767. + sa_len1 ? sa1 : " (error)",
  53768. + sa_len2 ? sa2 : " (error)");
  53769. + ips1p->ips_onext = ips2p;
  53770. + ips2p->ips_inext = ips1p;
  53771. + } else { /* UNGRPSA */
  53772. + ipsec_sa_put(ips1p);
  53773. + KLIPS_PRINT(debug_pfkey,
  53774. + "klips_debug:pfkey_x_grpsa_parse: "
  53775. + "unlinking ipsec_sa SA: %s.\n",
  53776. + sa_len1 ? sa1 : " (error)");
  53777. + while(ips1p->ips_onext) {
  53778. + ips1p = ips1p->ips_onext;
  53779. + }
  53780. + while(ips1p->ips_inext) {
  53781. + ipsp = ips1p;
  53782. + ips1p = ips1p->ips_inext;
  53783. + ipsec_sa_put(ips1p);
  53784. + ipsp->ips_inext = NULL;
  53785. + ipsec_sa_put(ipsp);
  53786. + ips1p->ips_onext = NULL;
  53787. + }
  53788. + }
  53789. +
  53790. + spin_unlock_bh(&tdb_lock);
  53791. +
  53792. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53793. + SADB_X_GRPSA,
  53794. + satype,
  53795. + 0,
  53796. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53797. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53798. + extensions_reply)
  53799. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53800. + SADB_EXT_SA,
  53801. + extr->ips->ips_said.spi,
  53802. + extr->ips->ips_replaywin,
  53803. + extr->ips->ips_state,
  53804. + extr->ips->ips_authalg,
  53805. + extr->ips->ips_encalg,
  53806. + extr->ips->ips_flags,
  53807. + extr->ips->ips_ref),
  53808. + extensions_reply)
  53809. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53810. + SADB_EXT_ADDRESS_DST,
  53811. + 0, /*extr->ips->ips_said.proto,*/
  53812. + 0,
  53813. + extr->ips->ips_addr_d),
  53814. + extensions_reply)
  53815. + && (extr->ips2
  53816. + ? (pfkey_safe_build(error = pfkey_x_satype_build(&extensions_reply[SADB_X_EXT_SATYPE2],
  53817. + ((struct sadb_x_satype*)extensions[SADB_X_EXT_SATYPE2])->sadb_x_satype_satype
  53818. + /* proto2satype(extr->ips2->ips_said.proto) */),
  53819. + extensions_reply)
  53820. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_X_EXT_SA2],
  53821. + SADB_X_EXT_SA2,
  53822. + extr->ips2->ips_said.spi,
  53823. + extr->ips2->ips_replaywin,
  53824. + extr->ips2->ips_state,
  53825. + extr->ips2->ips_authalg,
  53826. + extr->ips2->ips_encalg,
  53827. + extr->ips2->ips_flags,
  53828. + extr->ips2->ips_ref),
  53829. + extensions_reply)
  53830. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST2],
  53831. + SADB_X_EXT_ADDRESS_DST2,
  53832. + 0, /*extr->ips->ips_said.proto,*/
  53833. + 0,
  53834. + extr->ips2->ips_addr_d),
  53835. + extensions_reply) ) : 1 )
  53836. + )) {
  53837. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53838. + "failed to build the x_grpsa reply message extensions\n");
  53839. + SENDERR(-error);
  53840. + }
  53841. +
  53842. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53843. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53844. + "failed to build the x_grpsa reply message\n");
  53845. + SENDERR(-error);
  53846. + }
  53847. +
  53848. + for(pfkey_socketsp = pfkey_open_sockets;
  53849. + pfkey_socketsp;
  53850. + pfkey_socketsp = pfkey_socketsp->next) {
  53851. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53852. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53853. + "sending up x_grpsa reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53854. + satype,
  53855. + satype2name(satype),
  53856. + pfkey_socketsp->socketp,
  53857. + error);
  53858. + SENDERR(-error);
  53859. + }
  53860. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53861. + "sending up x_grpsa reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53862. + satype,
  53863. + satype2name(satype),
  53864. + pfkey_socketsp->socketp);
  53865. + }
  53866. +
  53867. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53868. + "succeeded in sending x_grpsa reply message.\n");
  53869. +
  53870. + errlab:
  53871. + if (pfkey_reply) {
  53872. + pfkey_msg_free(&pfkey_reply);
  53873. + }
  53874. + pfkey_extensions_free(extensions_reply);
  53875. + return error;
  53876. +}
  53877. +
  53878. +DEBUG_NO_STATIC int
  53879. +pfkey_x_addflow_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53880. +{
  53881. + int error = 0;
  53882. +#ifdef CONFIG_KLIPS_DEBUG
  53883. + char buf1[64], buf2[64];
  53884. +#endif /* CONFIG_KLIPS_DEBUG */
  53885. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53886. + struct sadb_msg *pfkey_reply = NULL;
  53887. + struct socket_list *pfkey_socketsp;
  53888. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53889. + ip_address srcflow, dstflow, srcmask, dstmask;
  53890. +
  53891. + KLIPS_PRINT(debug_pfkey,
  53892. + "klips_debug:pfkey_x_addflow_parse: .\n");
  53893. +
  53894. + pfkey_extensions_init(extensions_reply);
  53895. +
  53896. + memset((caddr_t)&srcflow, 0, sizeof(srcflow));
  53897. + memset((caddr_t)&dstflow, 0, sizeof(dstflow));
  53898. + memset((caddr_t)&srcmask, 0, sizeof(srcmask));
  53899. + memset((caddr_t)&dstmask, 0, sizeof(dstmask));
  53900. +
  53901. + if(!extr || !(extr->ips) || !(extr->eroute)) {
  53902. + KLIPS_PRINT(debug_pfkey,
  53903. + "klips_debug:pfkey_x_addflow_parse: "
  53904. + "missing extr, ipsec_sa or eroute data.\n");
  53905. + SENDERR(EINVAL);
  53906. + }
  53907. +
  53908. + srcflow.u.v4.sin_family = AF_INET;
  53909. + dstflow.u.v4.sin_family = AF_INET;
  53910. + srcmask.u.v4.sin_family = AF_INET;
  53911. + dstmask.u.v4.sin_family = AF_INET;
  53912. + srcflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_src;
  53913. + dstflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_dst;
  53914. + srcmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_src;
  53915. + dstmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_dst;
  53916. +
  53917. +#ifdef CONFIG_KLIPS_DEBUG
  53918. + if (debug_pfkey) {
  53919. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  53920. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  53921. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  53922. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  53923. + KLIPS_PRINT(debug_pfkey,
  53924. + "klips_debug:pfkey_x_addflow_parse: "
  53925. + "calling breakeroute and/or makeroute for %s->%s\n",
  53926. + buf1, buf2);
  53927. + }
  53928. +#endif /* CONFIG_KLIPS_DEBUG */
  53929. +
  53930. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_INFLOW) {
  53931. + struct ipsec_sa *ipsp, *ipsq;
  53932. + char sa[SATOT_BUF];
  53933. + size_t sa_len;
  53934. +
  53935. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53936. + if(ipsq == NULL) {
  53937. + KLIPS_PRINT(debug_pfkey,
  53938. + "klips_debug:pfkey_x_addflow_parse: "
  53939. + "ipsec_sa not found, cannot set incoming policy.\n");
  53940. + SENDERR(ENOENT);
  53941. + }
  53942. +
  53943. + ipsp = ipsq;
  53944. + while(ipsp && ipsp->ips_said.proto != IPPROTO_IPIP) {
  53945. + ipsp = ipsp->ips_inext;
  53946. + }
  53947. +
  53948. + if(ipsp == NULL) {
  53949. + ipsec_sa_put(ipsq);
  53950. + KLIPS_PRINT(debug_pfkey,
  53951. + "klips_debug:pfkey_x_addflow_parse: "
  53952. + "SA chain does not have an IPIP SA, cannot set incoming policy.\n");
  53953. + SENDERR(ENOENT);
  53954. + }
  53955. +
  53956. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  53957. +
  53958. + ipsp->ips_flags |= SADB_X_SAFLAGS_INFLOW;
  53959. + ipsp->ips_flow_s = srcflow;
  53960. + ipsp->ips_flow_d = dstflow;
  53961. + ipsp->ips_mask_s = srcmask;
  53962. + ipsp->ips_mask_d = dstmask;
  53963. +
  53964. + ipsec_sa_put(ipsq);
  53965. +
  53966. + KLIPS_PRINT(debug_pfkey,
  53967. + "klips_debug:pfkey_x_addflow_parse: "
  53968. + "inbound eroute, setting incoming policy information in IPIP ipsec_sa for SA: %s.\n",
  53969. + sa_len ? sa : " (error)");
  53970. + } else {
  53971. + struct sk_buff *first = NULL, *last = NULL;
  53972. +
  53973. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_REPLACEFLOW) {
  53974. + KLIPS_PRINT(debug_pfkey,
  53975. + "klips_debug:pfkey_x_addflow_parse: "
  53976. + "REPLACEFLOW flag set, calling breakeroute.\n");
  53977. + if ((error = ipsec_breakroute(&(extr->eroute->er_eaddr),
  53978. + &(extr->eroute->er_emask),
  53979. + &first, &last))) {
  53980. + KLIPS_PRINT(debug_pfkey,
  53981. + "klips_debug:pfkey_x_addflow_parse: "
  53982. + "breakeroute returned %d. first=0p%p, last=0p%p\n",
  53983. + error,
  53984. + first,
  53985. + last);
  53986. + if(first != NULL) {
  53987. + ipsec_kfree_skb(first);
  53988. + }
  53989. + if(last != NULL) {
  53990. + ipsec_kfree_skb(last);
  53991. + }
  53992. + SENDERR(-error);
  53993. + }
  53994. + }
  53995. +
  53996. + KLIPS_PRINT(debug_pfkey,
  53997. + "klips_debug:pfkey_x_addflow_parse: "
  53998. + "calling makeroute.\n");
  53999. +
  54000. + if ((error = ipsec_makeroute(&(extr->eroute->er_eaddr),
  54001. + &(extr->eroute->er_emask),
  54002. + extr->ips->ips_said,
  54003. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid,
  54004. + NULL,
  54005. + &(extr->ips->ips_ident_s),
  54006. + &(extr->ips->ips_ident_d)))) {
  54007. + KLIPS_PRINT(debug_pfkey,
  54008. + "klips_debug:pfkey_x_addflow_parse: "
  54009. + "makeroute returned %d.\n", error);
  54010. + SENDERR(-error);
  54011. + }
  54012. + if(first != NULL) {
  54013. + KLIPS_PRINT(debug_eroute,
  54014. + "klips_debug:pfkey_x_addflow_parse: "
  54015. + "first=0p%p HOLD packet re-injected.\n",
  54016. + first);
  54017. + DEV_QUEUE_XMIT(first, first->dev, SOPRI_NORMAL);
  54018. + }
  54019. + if(last != NULL) {
  54020. + KLIPS_PRINT(debug_eroute,
  54021. + "klips_debug:pfkey_x_addflow_parse: "
  54022. + "last=0p%p HOLD packet re-injected.\n",
  54023. + last);
  54024. + DEV_QUEUE_XMIT(last, last->dev, SOPRI_NORMAL);
  54025. + }
  54026. + }
  54027. +
  54028. + KLIPS_PRINT(debug_pfkey,
  54029. + "klips_debug:pfkey_x_addflow_parse: "
  54030. + "makeroute call successful.\n");
  54031. +
  54032. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  54033. + SADB_X_ADDFLOW,
  54034. + satype,
  54035. + 0,
  54036. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  54037. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  54038. + extensions_reply)
  54039. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  54040. + SADB_EXT_SA,
  54041. + extr->ips->ips_said.spi,
  54042. + extr->ips->ips_replaywin,
  54043. + extr->ips->ips_state,
  54044. + extr->ips->ips_authalg,
  54045. + extr->ips->ips_encalg,
  54046. + extr->ips->ips_flags,
  54047. + extr->ips->ips_ref),
  54048. + extensions_reply)
  54049. + && (extensions[SADB_EXT_ADDRESS_SRC]
  54050. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  54051. + SADB_EXT_ADDRESS_SRC,
  54052. + 0, /*extr->ips->ips_said.proto,*/
  54053. + 0,
  54054. + extr->ips->ips_addr_s),
  54055. + extensions_reply) : 1)
  54056. + && (extensions[SADB_EXT_ADDRESS_DST]
  54057. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  54058. + SADB_EXT_ADDRESS_DST,
  54059. + 0, /*extr->ips->ips_said.proto,*/
  54060. + 0,
  54061. + extr->ips->ips_addr_d),
  54062. + extensions_reply) : 1)
  54063. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_FLOW],
  54064. + SADB_X_EXT_ADDRESS_SRC_FLOW,
  54065. + 0, /*extr->ips->ips_said.proto,*/
  54066. + 0,
  54067. + (struct sockaddr*)&srcflow),
  54068. + extensions_reply)
  54069. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_FLOW],
  54070. + SADB_X_EXT_ADDRESS_DST_FLOW,
  54071. + 0, /*extr->ips->ips_said.proto,*/
  54072. + 0,
  54073. + (struct sockaddr*)&dstflow),
  54074. + extensions_reply)
  54075. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_MASK],
  54076. + SADB_X_EXT_ADDRESS_SRC_MASK,
  54077. + 0, /*extr->ips->ips_said.proto,*/
  54078. + 0,
  54079. + (struct sockaddr*)&srcmask),
  54080. + extensions_reply)
  54081. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_MASK],
  54082. + SADB_X_EXT_ADDRESS_DST_MASK,
  54083. + 0, /*extr->ips->ips_said.proto,*/
  54084. + 0,
  54085. + (struct sockaddr*)&dstmask),
  54086. + extensions_reply)
  54087. + )) {
  54088. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54089. + "failed to build the x_addflow reply message extensions\n");
  54090. + SENDERR(-error);
  54091. + }
  54092. +
  54093. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  54094. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54095. + "failed to build the x_addflow reply message\n");
  54096. + SENDERR(-error);
  54097. + }
  54098. +
  54099. + for(pfkey_socketsp = pfkey_open_sockets;
  54100. + pfkey_socketsp;
  54101. + pfkey_socketsp = pfkey_socketsp->next) {
  54102. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  54103. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54104. + "sending up x_addflow reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54105. + satype,
  54106. + satype2name(satype),
  54107. + pfkey_socketsp->socketp,
  54108. + error);
  54109. + SENDERR(-error);
  54110. + }
  54111. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54112. + "sending up x_addflow reply message for satype=%d(%s) (proto=%d) to socket=0p%p succeeded.\n",
  54113. + satype,
  54114. + satype2name(satype),
  54115. + extr->ips->ips_said.proto,
  54116. + pfkey_socketsp->socketp);
  54117. + }
  54118. +
  54119. + KLIPS_PRINT(debug_pfkey,
  54120. + "klips_debug:pfkey_x_addflow_parse: "
  54121. + "extr->ips cleaned up and freed.\n");
  54122. +
  54123. + errlab:
  54124. + if (pfkey_reply) {
  54125. + pfkey_msg_free(&pfkey_reply);
  54126. + }
  54127. + pfkey_extensions_free(extensions_reply);
  54128. + return error;
  54129. +}
  54130. +
  54131. +DEBUG_NO_STATIC int
  54132. +pfkey_x_delflow_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54133. +{
  54134. + int error = 0;
  54135. +#ifdef CONFIG_KLIPS_DEBUG
  54136. + char buf1[64], buf2[64];
  54137. +#endif /* CONFIG_KLIPS_DEBUG */
  54138. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  54139. + struct sadb_msg *pfkey_reply = NULL;
  54140. + struct socket_list *pfkey_socketsp;
  54141. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  54142. + ip_address srcflow, dstflow, srcmask, dstmask;
  54143. +
  54144. + KLIPS_PRINT(debug_pfkey,
  54145. + "klips_debug:pfkey_x_delflow_parse: .\n");
  54146. +
  54147. + pfkey_extensions_init(extensions_reply);
  54148. +
  54149. + memset((caddr_t)&srcflow, 0, sizeof(srcflow));
  54150. + memset((caddr_t)&dstflow, 0, sizeof(dstflow));
  54151. + memset((caddr_t)&srcmask, 0, sizeof(srcmask));
  54152. + memset((caddr_t)&dstmask, 0, sizeof(dstmask));
  54153. +
  54154. + if(!extr || !(extr->ips)) {
  54155. + KLIPS_PRINT(debug_pfkey,
  54156. + "klips_debug:pfkey_x_delflow_parse: "
  54157. + "extr, or extr->ips is NULL, fatal\n");
  54158. + SENDERR(EINVAL);
  54159. + }
  54160. +
  54161. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_CLEARFLOW) {
  54162. + KLIPS_PRINT(debug_pfkey,
  54163. + "klips_debug:pfkey_x_delflow_parse: "
  54164. + "CLEARFLOW flag set, calling cleareroutes.\n");
  54165. + if ((error = ipsec_cleareroutes()))
  54166. + KLIPS_PRINT(debug_pfkey,
  54167. + "klips_debug:pfkey_x_delflow_parse: "
  54168. + "cleareroutes returned %d.\n", error);
  54169. + SENDERR(-error);
  54170. + } else {
  54171. + struct sk_buff *first = NULL, *last = NULL;
  54172. +
  54173. + if(!(extr->eroute)) {
  54174. + KLIPS_PRINT(debug_pfkey,
  54175. + "klips_debug:pfkey_x_delflow_parse: "
  54176. + "extr->eroute is NULL, fatal.\n");
  54177. + SENDERR(EINVAL);
  54178. + }
  54179. +
  54180. + srcflow.u.v4.sin_family = AF_INET;
  54181. + dstflow.u.v4.sin_family = AF_INET;
  54182. + srcmask.u.v4.sin_family = AF_INET;
  54183. + dstmask.u.v4.sin_family = AF_INET;
  54184. + srcflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_src;
  54185. + dstflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_dst;
  54186. + srcmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_src;
  54187. + dstmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_dst;
  54188. +
  54189. +#ifdef CONFIG_KLIPS_DEBUG
  54190. + if (debug_pfkey) {
  54191. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  54192. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  54193. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  54194. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  54195. + KLIPS_PRINT(debug_pfkey,
  54196. + "klips_debug:pfkey_x_delflow_parse: "
  54197. + "calling breakeroute for %s->%s\n",
  54198. + buf1, buf2);
  54199. + }
  54200. +#endif /* CONFIG_KLIPS_DEBUG */
  54201. + error = ipsec_breakroute(&(extr->eroute->er_eaddr),
  54202. + &(extr->eroute->er_emask),
  54203. + &first, &last);
  54204. + if(error) {
  54205. + KLIPS_PRINT(debug_pfkey,
  54206. + "klips_debug:pfkey_x_delflow_parse: "
  54207. + "breakeroute returned %d. first=0p%p, last=0p%p\n",
  54208. + error,
  54209. + first,
  54210. + last);
  54211. + }
  54212. + if(first != NULL) {
  54213. + ipsec_kfree_skb(first);
  54214. + }
  54215. + if(last != NULL) {
  54216. + ipsec_kfree_skb(last);
  54217. + }
  54218. + if(error) {
  54219. + SENDERR(-error);
  54220. + }
  54221. + }
  54222. +
  54223. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  54224. + SADB_X_DELFLOW,
  54225. + satype,
  54226. + 0,
  54227. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  54228. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  54229. + extensions_reply)
  54230. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  54231. + SADB_EXT_SA,
  54232. + extr->ips->ips_said.spi,
  54233. + extr->ips->ips_replaywin,
  54234. + extr->ips->ips_state,
  54235. + extr->ips->ips_authalg,
  54236. + extr->ips->ips_encalg,
  54237. + extr->ips->ips_flags,
  54238. + extr->ips->ips_ref),
  54239. + extensions_reply)
  54240. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_FLOW],
  54241. + SADB_X_EXT_ADDRESS_SRC_FLOW,
  54242. + 0, /*extr->ips->ips_said.proto,*/
  54243. + 0,
  54244. + (struct sockaddr*)&srcflow),
  54245. + extensions_reply)
  54246. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_FLOW],
  54247. + SADB_X_EXT_ADDRESS_DST_FLOW,
  54248. + 0, /*extr->ips->ips_said.proto,*/
  54249. + 0,
  54250. + (struct sockaddr*)&dstflow),
  54251. + extensions_reply)
  54252. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_MASK],
  54253. + SADB_X_EXT_ADDRESS_SRC_MASK,
  54254. + 0, /*extr->ips->ips_said.proto,*/
  54255. + 0,
  54256. + (struct sockaddr*)&srcmask),
  54257. + extensions_reply)
  54258. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_MASK],
  54259. + SADB_X_EXT_ADDRESS_DST_MASK,
  54260. + 0, /*extr->ips->ips_said.proto,*/
  54261. + 0,
  54262. + (struct sockaddr*)&dstmask),
  54263. + extensions_reply)
  54264. + )) {
  54265. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54266. + "failed to build the x_delflow reply message extensions\n");
  54267. + SENDERR(-error);
  54268. + }
  54269. +
  54270. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  54271. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54272. + "failed to build the x_delflow reply message\n");
  54273. + SENDERR(-error);
  54274. + }
  54275. +
  54276. + for(pfkey_socketsp = pfkey_open_sockets;
  54277. + pfkey_socketsp;
  54278. + pfkey_socketsp = pfkey_socketsp->next) {
  54279. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  54280. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54281. + "sending up x_delflow reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54282. + satype,
  54283. + satype2name(satype),
  54284. + pfkey_socketsp->socketp,
  54285. + error);
  54286. + SENDERR(-error);
  54287. + }
  54288. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54289. + "sending up x_delflow reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  54290. + satype,
  54291. + satype2name(satype),
  54292. + pfkey_socketsp->socketp);
  54293. + }
  54294. +
  54295. + KLIPS_PRINT(debug_pfkey,
  54296. + "klips_debug:pfkey_x_delflow_parse: "
  54297. + "extr->ips cleaned up and freed.\n");
  54298. +
  54299. + errlab:
  54300. + if (pfkey_reply) {
  54301. + pfkey_msg_free(&pfkey_reply);
  54302. + }
  54303. + pfkey_extensions_free(extensions_reply);
  54304. + return error;
  54305. +}
  54306. +
  54307. +DEBUG_NO_STATIC int
  54308. +pfkey_x_msg_debug_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54309. +{
  54310. + int error = 0;
  54311. +
  54312. + KLIPS_PRINT(debug_pfkey,
  54313. + "klips_debug:pfkey_x_msg_debug_parse: .\n");
  54314. +
  54315. +/* errlab:*/
  54316. + return error;
  54317. +}
  54318. +
  54319. +/* pfkey_expire expects the ipsec_sa table to be locked before being called. */
  54320. +int
  54321. +pfkey_expire(struct ipsec_sa *ipsp, int hard)
  54322. +{
  54323. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54324. + struct sadb_msg *pfkey_msg = NULL;
  54325. + struct socket_list *pfkey_socketsp;
  54326. + int error = 0;
  54327. + uint8_t satype;
  54328. +
  54329. + pfkey_extensions_init(extensions);
  54330. +
  54331. + if(!(satype = proto2satype(ipsp->ips_said.proto))) {
  54332. + KLIPS_PRINT(debug_pfkey,
  54333. + "klips_debug:pfkey_expire: "
  54334. + "satype lookup for protocol %d lookup failed.\n",
  54335. + ipsp->ips_said.proto);
  54336. + SENDERR(EINVAL);
  54337. + }
  54338. +
  54339. + if(!pfkey_open_sockets) {
  54340. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54341. + "no sockets listening.\n");
  54342. + SENDERR(EPROTONOSUPPORT);
  54343. + }
  54344. +
  54345. + if (!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions[0],
  54346. + SADB_EXPIRE,
  54347. + satype,
  54348. + 0,
  54349. + ++pfkey_msg_seq,
  54350. + 0),
  54351. + extensions)
  54352. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions[SADB_EXT_SA],
  54353. + SADB_EXT_SA,
  54354. + ipsp->ips_said.spi,
  54355. + ipsp->ips_replaywin,
  54356. + ipsp->ips_state,
  54357. + ipsp->ips_authalg,
  54358. + ipsp->ips_encalg,
  54359. + ipsp->ips_flags,
  54360. + ipsp->ips_ref),
  54361. + extensions)
  54362. + && pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_CURRENT],
  54363. + SADB_EXT_LIFETIME_CURRENT,
  54364. + ipsp->ips_life.ipl_allocations.ipl_count,
  54365. + ipsp->ips_life.ipl_bytes.ipl_count,
  54366. + ipsp->ips_life.ipl_addtime.ipl_count,
  54367. + ipsp->ips_life.ipl_usetime.ipl_count,
  54368. + ipsp->ips_life.ipl_packets.ipl_count),
  54369. + extensions)
  54370. + && (hard ?
  54371. + pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_HARD],
  54372. + SADB_EXT_LIFETIME_HARD,
  54373. + ipsp->ips_life.ipl_allocations.ipl_hard,
  54374. + ipsp->ips_life.ipl_bytes.ipl_hard,
  54375. + ipsp->ips_life.ipl_addtime.ipl_hard,
  54376. + ipsp->ips_life.ipl_usetime.ipl_hard,
  54377. + ipsp->ips_life.ipl_packets.ipl_hard),
  54378. + extensions)
  54379. + : pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_SOFT],
  54380. + SADB_EXT_LIFETIME_SOFT,
  54381. + ipsp->ips_life.ipl_allocations.ipl_soft,
  54382. + ipsp->ips_life.ipl_bytes.ipl_soft,
  54383. + ipsp->ips_life.ipl_addtime.ipl_soft,
  54384. + ipsp->ips_life.ipl_usetime.ipl_soft,
  54385. + ipsp->ips_life.ipl_packets.ipl_soft),
  54386. + extensions))
  54387. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54388. + SADB_EXT_ADDRESS_SRC,
  54389. + 0, /* ipsp->ips_said.proto, */
  54390. + 0,
  54391. + ipsp->ips_addr_s),
  54392. + extensions)
  54393. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54394. + SADB_EXT_ADDRESS_DST,
  54395. + 0, /* ipsp->ips_said.proto, */
  54396. + 0,
  54397. + ipsp->ips_addr_d),
  54398. + extensions))) {
  54399. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54400. + "failed to build the expire message extensions\n");
  54401. + spin_unlock(&tdb_lock);
  54402. + goto errlab;
  54403. + }
  54404. +
  54405. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54406. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54407. + "failed to build the expire message\n");
  54408. + SENDERR(-error);
  54409. + }
  54410. +
  54411. + for(pfkey_socketsp = pfkey_open_sockets;
  54412. + pfkey_socketsp;
  54413. + pfkey_socketsp = pfkey_socketsp->next) {
  54414. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54415. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54416. + "sending up expire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54417. + satype,
  54418. + satype2name(satype),
  54419. + pfkey_socketsp->socketp,
  54420. + error);
  54421. + SENDERR(-error);
  54422. + }
  54423. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54424. + "sending up expire message for satype=%d(%s) (proto=%d) to socket=0p%p succeeded.\n",
  54425. + satype,
  54426. + satype2name(satype),
  54427. + ipsp->ips_said.proto,
  54428. + pfkey_socketsp->socketp);
  54429. + }
  54430. +
  54431. + errlab:
  54432. + if (pfkey_msg) {
  54433. + pfkey_msg_free(&pfkey_msg);
  54434. + }
  54435. + pfkey_extensions_free(extensions);
  54436. + return error;
  54437. +}
  54438. +
  54439. +int
  54440. +pfkey_acquire(struct ipsec_sa *ipsp)
  54441. +{
  54442. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54443. + struct sadb_msg *pfkey_msg = NULL;
  54444. + struct socket_list *pfkey_socketsp;
  54445. + int error = 0;
  54446. + struct sadb_comb comb[] = {
  54447. + /* auth; encrypt; flags; */
  54448. + /* auth_minbits; auth_maxbits; encrypt_minbits; encrypt_maxbits; */
  54449. + /* reserved; soft_allocations; hard_allocations; soft_bytes; hard_bytes; */
  54450. + /* soft_addtime; hard_addtime; soft_usetime; hard_usetime; */
  54451. + /* soft_packets; hard_packets; */
  54452. + { SADB_AALG_MD5HMAC, SADB_EALG_3DESCBC, SADB_SAFLAGS_PFS,
  54453. + 128, 128, 168, 168,
  54454. + 0, 0, 0, 0, 0,
  54455. + 57600, 86400, 57600, 86400,
  54456. + 0, 0 },
  54457. + { SADB_AALG_SHA1HMAC, SADB_EALG_3DESCBC, SADB_SAFLAGS_PFS,
  54458. + 160, 160, 168, 168,
  54459. + 0, 0, 0, 0, 0,
  54460. + 57600, 86400, 57600, 86400,
  54461. + 0, 0 }
  54462. + };
  54463. +
  54464. + /* XXX This should not be hard-coded. It should be taken from the spdb */
  54465. + uint8_t satype = SADB_SATYPE_ESP;
  54466. +
  54467. + pfkey_extensions_init(extensions);
  54468. +
  54469. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  54470. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire: "
  54471. + "SAtype=%d unspecified or unknown.\n",
  54472. + satype);
  54473. + SENDERR(EINVAL);
  54474. + }
  54475. +
  54476. + if(!(pfkey_registered_sockets[satype])) {
  54477. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54478. + "no sockets registered for SAtype=%d(%s).\n",
  54479. + satype,
  54480. + satype2name(satype));
  54481. + SENDERR(EPROTONOSUPPORT);
  54482. + }
  54483. +
  54484. + if (!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions[0],
  54485. + SADB_ACQUIRE,
  54486. + satype,
  54487. + 0,
  54488. + ++pfkey_msg_seq,
  54489. + 0),
  54490. + extensions)
  54491. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54492. + SADB_EXT_ADDRESS_SRC,
  54493. + ipsp->ips_transport_protocol,
  54494. + 0,
  54495. + ipsp->ips_addr_s),
  54496. + extensions)
  54497. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54498. + SADB_EXT_ADDRESS_DST,
  54499. + ipsp->ips_transport_protocol,
  54500. + 0,
  54501. + ipsp->ips_addr_d),
  54502. + extensions)
  54503. +#if 0
  54504. + && (ipsp->ips_addr_p
  54505. + ? pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_PROXY],
  54506. + SADB_EXT_ADDRESS_PROXY,
  54507. + ipsp->ips_transport_protocol,
  54508. + 0,
  54509. + ipsp->ips_addr_p),
  54510. + extensions) : 1)
  54511. +#endif
  54512. + && (ipsp->ips_ident_s.type != SADB_IDENTTYPE_RESERVED
  54513. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions[SADB_EXT_IDENTITY_SRC],
  54514. + SADB_EXT_IDENTITY_SRC,
  54515. + ipsp->ips_ident_s.type,
  54516. + ipsp->ips_ident_s.id,
  54517. + ipsp->ips_ident_s.len,
  54518. + ipsp->ips_ident_s.data),
  54519. + extensions) : 1)
  54520. +
  54521. + && (ipsp->ips_ident_d.type != SADB_IDENTTYPE_RESERVED
  54522. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions[SADB_EXT_IDENTITY_DST],
  54523. + SADB_EXT_IDENTITY_DST,
  54524. + ipsp->ips_ident_d.type,
  54525. + ipsp->ips_ident_d.id,
  54526. + ipsp->ips_ident_d.len,
  54527. + ipsp->ips_ident_d.data),
  54528. + extensions) : 1)
  54529. +#if 0
  54530. + /* FIXME: This won't work yet because I have not finished
  54531. + it. */
  54532. + && (ipsp->ips_sens_
  54533. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions[SADB_EXT_SENSITIVITY],
  54534. + ipsp->ips_sens_dpd,
  54535. + ipsp->ips_sens_sens_level,
  54536. + ipsp->ips_sens_sens_len,
  54537. + ipsp->ips_sens_sens_bitmap,
  54538. + ipsp->ips_sens_integ_level,
  54539. + ipsp->ips_sens_integ_len,
  54540. + ipsp->ips_sens_integ_bitmap),
  54541. + extensions) : 1)
  54542. +#endif
  54543. + && pfkey_safe_build(error = pfkey_prop_build(&extensions[SADB_EXT_PROPOSAL],
  54544. + 64, /* replay */
  54545. + sizeof(comb)/sizeof(struct sadb_comb),
  54546. + &(comb[0])),
  54547. + extensions)
  54548. + )) {
  54549. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54550. + "failed to build the acquire message extensions\n");
  54551. + SENDERR(-error);
  54552. + }
  54553. +
  54554. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54555. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54556. + "failed to build the acquire message\n");
  54557. + SENDERR(-error);
  54558. + }
  54559. +
  54560. +#if KLIPS_PFKEY_ACQUIRE_LOSSAGE > 0
  54561. + if(sysctl_ipsec_regress_pfkey_lossage) {
  54562. + return(0);
  54563. + }
  54564. +#endif
  54565. +
  54566. + /* this should go to all registered sockets for that satype only */
  54567. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  54568. + pfkey_socketsp;
  54569. + pfkey_socketsp = pfkey_socketsp->next) {
  54570. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54571. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54572. + "sending up acquire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54573. + satype,
  54574. + satype2name(satype),
  54575. + pfkey_socketsp->socketp,
  54576. + error);
  54577. + SENDERR(-error);
  54578. + }
  54579. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire: "
  54580. + "sending up acquire message for satype=%d(%s) to socket=0p%p succeeded.\n",
  54581. + satype,
  54582. + satype2name(satype),
  54583. + pfkey_socketsp->socketp);
  54584. + }
  54585. +
  54586. + errlab:
  54587. + if (pfkey_msg) {
  54588. + pfkey_msg_free(&pfkey_msg);
  54589. + }
  54590. + pfkey_extensions_free(extensions);
  54591. + return error;
  54592. +}
  54593. +
  54594. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54595. +int
  54596. +pfkey_nat_t_new_mapping(struct ipsec_sa *ipsp, struct sockaddr *ipaddr,
  54597. + __u16 sport)
  54598. +{
  54599. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54600. + struct sadb_msg *pfkey_msg = NULL;
  54601. + struct socket_list *pfkey_socketsp;
  54602. + int error = 0;
  54603. + uint8_t satype = (ipsp->ips_said.proto==IPPROTO_ESP) ? SADB_SATYPE_ESP : 0;
  54604. +
  54605. + /* Construct SADB_X_NAT_T_NEW_MAPPING message */
  54606. +
  54607. + pfkey_extensions_init(extensions);
  54608. +
  54609. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  54610. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54611. + "SAtype=%d unspecified or unknown.\n",
  54612. + satype);
  54613. + SENDERR(EINVAL);
  54614. + }
  54615. +
  54616. + if(!(pfkey_registered_sockets[satype])) {
  54617. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54618. + "no sockets registered for SAtype=%d(%s).\n",
  54619. + satype,
  54620. + satype2name(satype));
  54621. + SENDERR(EPROTONOSUPPORT);
  54622. + }
  54623. +
  54624. + if (!(pfkey_safe_build
  54625. + (error = pfkey_msg_hdr_build(&extensions[0], SADB_X_NAT_T_NEW_MAPPING,
  54626. + satype, 0, ++pfkey_msg_seq, 0), extensions)
  54627. + /* SA */
  54628. + && pfkey_safe_build
  54629. + (error = pfkey_sa_build(&extensions[SADB_EXT_SA],
  54630. + SADB_EXT_SA, ipsp->ips_said.spi, 0, 0, 0, 0, 0), extensions)
  54631. + /* ADDRESS_SRC = old addr */
  54632. + && pfkey_safe_build
  54633. + (error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54634. + SADB_EXT_ADDRESS_SRC, ipsp->ips_said.proto, 0, ipsp->ips_addr_s),
  54635. + extensions)
  54636. + /* NAT_T_SPORT = old port */
  54637. + && pfkey_safe_build
  54638. + (error = pfkey_x_nat_t_port_build(&extensions[SADB_X_EXT_NAT_T_SPORT],
  54639. + SADB_X_EXT_NAT_T_SPORT, ipsp->ips_natt_sport), extensions)
  54640. + /* ADDRESS_DST = new addr */
  54641. + && pfkey_safe_build
  54642. + (error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54643. + SADB_EXT_ADDRESS_DST, ipsp->ips_said.proto, 0, ipaddr), extensions)
  54644. + /* NAT_T_DPORT = new port */
  54645. + && pfkey_safe_build
  54646. + (error = pfkey_x_nat_t_port_build(&extensions[SADB_X_EXT_NAT_T_DPORT],
  54647. + SADB_X_EXT_NAT_T_DPORT, sport), extensions)
  54648. + )) {
  54649. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54650. + "failed to build the nat_t_new_mapping message extensions\n");
  54651. + SENDERR(-error);
  54652. + }
  54653. +
  54654. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54655. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54656. + "failed to build the nat_t_new_mapping message\n");
  54657. + SENDERR(-error);
  54658. + }
  54659. +
  54660. + /* this should go to all registered sockets for that satype only */
  54661. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  54662. + pfkey_socketsp;
  54663. + pfkey_socketsp = pfkey_socketsp->next) {
  54664. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54665. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54666. + "sending up nat_t_new_mapping message for satype=%d(%s) to socket=%p failed with error=%d.\n",
  54667. + satype,
  54668. + satype2name(satype),
  54669. + pfkey_socketsp->socketp,
  54670. + error);
  54671. + SENDERR(-error);
  54672. + }
  54673. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54674. + "sending up nat_t_new_mapping message for satype=%d(%s) to socket=%p succeeded.\n",
  54675. + satype,
  54676. + satype2name(satype),
  54677. + pfkey_socketsp->socketp);
  54678. + }
  54679. +
  54680. + errlab:
  54681. + if (pfkey_msg) {
  54682. + pfkey_msg_free(&pfkey_msg);
  54683. + }
  54684. + pfkey_extensions_free(extensions);
  54685. + return error;
  54686. +}
  54687. +
  54688. +DEBUG_NO_STATIC int
  54689. +pfkey_x_nat_t_new_mapping_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54690. +{
  54691. + /* SADB_X_NAT_T_NEW_MAPPING not used in kernel */
  54692. + return -EINVAL;
  54693. +}
  54694. +#endif
  54695. +
  54696. +DEBUG_NO_STATIC int (*ext_processors[SADB_EXT_MAX+1])(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr) =
  54697. +{
  54698. + NULL, /* pfkey_msg_process, */
  54699. + pfkey_sa_process,
  54700. + pfkey_lifetime_process,
  54701. + pfkey_lifetime_process,
  54702. + pfkey_lifetime_process,
  54703. + pfkey_address_process,
  54704. + pfkey_address_process,
  54705. + pfkey_address_process,
  54706. + pfkey_key_process,
  54707. + pfkey_key_process,
  54708. + pfkey_ident_process,
  54709. + pfkey_ident_process,
  54710. + pfkey_sens_process,
  54711. + pfkey_prop_process,
  54712. + pfkey_supported_process,
  54713. + pfkey_supported_process,
  54714. + pfkey_spirange_process,
  54715. + pfkey_x_kmprivate_process,
  54716. + pfkey_x_satype_process,
  54717. + pfkey_sa_process,
  54718. + pfkey_address_process,
  54719. + pfkey_address_process,
  54720. + pfkey_address_process,
  54721. + pfkey_address_process,
  54722. + pfkey_address_process,
  54723. + pfkey_x_debug_process,
  54724. + pfkey_x_protocol_process
  54725. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54726. + ,
  54727. + pfkey_x_nat_t_type_process,
  54728. + pfkey_x_nat_t_port_process,
  54729. + pfkey_x_nat_t_port_process,
  54730. + pfkey_address_process
  54731. +#endif
  54732. +};
  54733. +
  54734. +
  54735. +DEBUG_NO_STATIC int (*msg_parsers[SADB_MAX +1])(struct sock *sk, struct sadb_ext *extensions[], struct pfkey_extracted_data* extr)
  54736. + =
  54737. +{
  54738. + NULL, /* RESERVED */
  54739. + pfkey_getspi_parse,
  54740. + pfkey_update_parse,
  54741. + pfkey_add_parse,
  54742. + pfkey_delete_parse,
  54743. + pfkey_get_parse,
  54744. + pfkey_acquire_parse,
  54745. + pfkey_register_parse,
  54746. + pfkey_expire_parse,
  54747. + pfkey_flush_parse,
  54748. + pfkey_dump_parse,
  54749. + pfkey_x_promisc_parse,
  54750. + pfkey_x_pchange_parse,
  54751. + pfkey_x_grpsa_parse,
  54752. + pfkey_x_addflow_parse,
  54753. + pfkey_x_delflow_parse,
  54754. + pfkey_x_msg_debug_parse
  54755. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54756. + , pfkey_x_nat_t_new_mapping_parse
  54757. +#endif
  54758. +};
  54759. +
  54760. +int
  54761. +pfkey_build_reply(struct sadb_msg *pfkey_msg,
  54762. + struct pfkey_extracted_data *extr,
  54763. + struct sadb_msg **pfkey_reply)
  54764. +{
  54765. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54766. + int error = 0;
  54767. + int msg_type = pfkey_msg->sadb_msg_type;
  54768. + int seq = pfkey_msg->sadb_msg_seq;
  54769. +
  54770. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54771. + "building reply with type: %d\n",
  54772. + msg_type);
  54773. + pfkey_extensions_init(extensions);
  54774. + if (!extr || !extr->ips) {
  54775. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54776. + "bad ipsec_sa passed\n");
  54777. + return EINVAL;
  54778. + }
  54779. + error = pfkey_safe_build(pfkey_msg_hdr_build(&extensions[0],
  54780. + msg_type,
  54781. + proto2satype(extr->ips->ips_said.proto),
  54782. + 0,
  54783. + seq,
  54784. + pfkey_msg->sadb_msg_pid),
  54785. + extensions) &&
  54786. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54787. + 1 << SADB_EXT_SA)
  54788. + || pfkey_safe_build(pfkey_sa_ref_build(&extensions[SADB_EXT_SA],
  54789. + SADB_EXT_SA,
  54790. + extr->ips->ips_said.spi,
  54791. + extr->ips->ips_replaywin,
  54792. + extr->ips->ips_state,
  54793. + extr->ips->ips_authalg,
  54794. + extr->ips->ips_encalg,
  54795. + extr->ips->ips_flags,
  54796. + extr->ips->ips_ref),
  54797. + extensions)) &&
  54798. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54799. + 1 << SADB_EXT_LIFETIME_CURRENT)
  54800. + || pfkey_safe_build(pfkey_lifetime_build(&extensions
  54801. + [SADB_EXT_LIFETIME_CURRENT],
  54802. + SADB_EXT_LIFETIME_CURRENT,
  54803. + extr->ips->ips_life.ipl_allocations.ipl_count,
  54804. + extr->ips->ips_life.ipl_bytes.ipl_count,
  54805. + extr->ips->ips_life.ipl_addtime.ipl_count,
  54806. + extr->ips->ips_life.ipl_usetime.ipl_count,
  54807. + extr->ips->ips_life.ipl_packets.ipl_count),
  54808. + extensions)) &&
  54809. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54810. + 1 << SADB_EXT_ADDRESS_SRC)
  54811. + || pfkey_safe_build(pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54812. + SADB_EXT_ADDRESS_SRC,
  54813. + extr->ips->ips_said.proto,
  54814. + 0,
  54815. + extr->ips->ips_addr_s),
  54816. + extensions)) &&
  54817. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54818. + 1 << SADB_EXT_ADDRESS_DST)
  54819. + || pfkey_safe_build(pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54820. + SADB_EXT_ADDRESS_DST,
  54821. + extr->ips->ips_said.proto,
  54822. + 0,
  54823. + extr->ips->ips_addr_d),
  54824. + extensions));
  54825. +
  54826. + if (error == 0) {
  54827. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54828. + "building extensions failed\n");
  54829. + return EINVAL;
  54830. + }
  54831. +
  54832. + KLIPS_PRINT(debug_pfkey,
  54833. + "klips_debug:pfkey_build_reply: "
  54834. + "built extensions, proceed to build the message\n");
  54835. + KLIPS_PRINT(debug_pfkey,
  54836. + "klips_debug:pfkey_build_reply: "
  54837. + "extensions[1]=0p%p\n",
  54838. + extensions[1]);
  54839. + error = pfkey_msg_build(pfkey_reply, extensions, EXT_BITS_OUT);
  54840. + pfkey_extensions_free(extensions);
  54841. +
  54842. + return error;
  54843. +}
  54844. +
  54845. +int
  54846. +pfkey_msg_interp(struct sock *sk, struct sadb_msg *pfkey_msg,
  54847. + struct sadb_msg **pfkey_reply)
  54848. +{
  54849. + int error = 0;
  54850. + int i;
  54851. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54852. + struct pfkey_extracted_data extr = {NULL, NULL, NULL};
  54853. +
  54854. + pfkey_extensions_init(extensions);
  54855. + KLIPS_PRINT(debug_pfkey,
  54856. + "klips_debug:pfkey_msg_interp: "
  54857. + "parsing message ver=%d, type=%d, errno=%d, satype=%d(%s), len=%d, res=%d, seq=%d, pid=%d.\n",
  54858. + pfkey_msg->sadb_msg_version,
  54859. + pfkey_msg->sadb_msg_type,
  54860. + pfkey_msg->sadb_msg_errno,
  54861. + pfkey_msg->sadb_msg_satype,
  54862. + satype2name(pfkey_msg->sadb_msg_satype),
  54863. + pfkey_msg->sadb_msg_len,
  54864. + pfkey_msg->sadb_msg_reserved,
  54865. + pfkey_msg->sadb_msg_seq,
  54866. + pfkey_msg->sadb_msg_pid);
  54867. +
  54868. + extr.ips = ipsec_sa_alloc(&error); /* pass in error var by pointer */
  54869. + if(extr.ips == NULL) {
  54870. + KLIPS_PRINT(debug_pfkey,
  54871. + "klips_debug:pfkey_msg_interp: "
  54872. + "memory allocation error.\n");
  54873. + SENDERR(-error);
  54874. + }
  54875. +
  54876. + KLIPS_PRINT(debug_pfkey,
  54877. + "klips_debug:pfkey_msg_interp: "
  54878. + "allocated extr->ips=0p%p.\n",
  54879. + extr.ips);
  54880. +
  54881. + if(pfkey_msg->sadb_msg_satype > SADB_SATYPE_MAX) {
  54882. + KLIPS_PRINT(debug_pfkey,
  54883. + "klips_debug:pfkey_msg_interp: "
  54884. + "satype %d > max %d\n",
  54885. + pfkey_msg->sadb_msg_satype,
  54886. + SADB_SATYPE_MAX);
  54887. + SENDERR(EINVAL);
  54888. + }
  54889. +
  54890. + switch(pfkey_msg->sadb_msg_type) {
  54891. + case SADB_GETSPI:
  54892. + case SADB_UPDATE:
  54893. + case SADB_ADD:
  54894. + case SADB_DELETE:
  54895. + case SADB_X_GRPSA:
  54896. + case SADB_X_ADDFLOW:
  54897. + if(!(extr.ips->ips_said.proto = satype2proto(pfkey_msg->sadb_msg_satype))) {
  54898. + KLIPS_PRINT(debug_pfkey,
  54899. + "klips_debug:pfkey_msg_interp: "
  54900. + "satype %d lookup failed.\n",
  54901. + pfkey_msg->sadb_msg_satype);
  54902. + SENDERR(EINVAL);
  54903. + } else {
  54904. + KLIPS_PRINT(debug_pfkey,
  54905. + "klips_debug:pfkey_msg_interp: "
  54906. + "satype %d lookups to proto=%d.\n",
  54907. + pfkey_msg->sadb_msg_satype,
  54908. + extr.ips->ips_said.proto);
  54909. + }
  54910. + break;
  54911. + default:
  54912. + break;
  54913. + }
  54914. +
  54915. + /* The NULL below causes the default extension parsers to be used */
  54916. + /* Parse the extensions */
  54917. + if((error = pfkey_msg_parse(pfkey_msg, NULL, extensions, EXT_BITS_IN)))
  54918. + {
  54919. + KLIPS_PRINT(debug_pfkey,
  54920. + "klips_debug:pfkey_msg_interp: "
  54921. + "message parsing failed with error %d.\n",
  54922. + error);
  54923. + SENDERR(-error);
  54924. + }
  54925. +
  54926. + /* Process the extensions */
  54927. + for(i=1; i <= SADB_EXT_MAX;i++) {
  54928. + if(extensions[i] != NULL) {
  54929. + KLIPS_PRINT(debug_pfkey,
  54930. + "klips_debug:pfkey_msg_interp: "
  54931. + "processing ext %d 0p%p with processor 0p%p.\n",
  54932. + i, extensions[i], ext_processors[i]);
  54933. + if((error = ext_processors[i](extensions[i], &extr))) {
  54934. + KLIPS_PRINT(debug_pfkey,
  54935. + "klips_debug:pfkey_msg_interp: "
  54936. + "extension processing for type %d failed with error %d.\n",
  54937. + i,
  54938. + error);
  54939. + SENDERR(-error);
  54940. + }
  54941. +
  54942. + }
  54943. +
  54944. + }
  54945. +
  54946. + /* Parse the message types */
  54947. + KLIPS_PRINT(debug_pfkey,
  54948. + "klips_debug:pfkey_msg_interp: "
  54949. + "parsing message type %d(%s) with msg_parser 0p%p.\n",
  54950. + pfkey_msg->sadb_msg_type,
  54951. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type),
  54952. + msg_parsers[pfkey_msg->sadb_msg_type]);
  54953. + if((error = msg_parsers[pfkey_msg->sadb_msg_type](sk, extensions, &extr))) {
  54954. + KLIPS_PRINT(debug_pfkey,
  54955. + "klips_debug:pfkey_msg_interp: "
  54956. + "message parsing failed with error %d.\n",
  54957. + error);
  54958. + SENDERR(-error);
  54959. + }
  54960. +
  54961. +#if 0
  54962. + error = pfkey_build_reply(pfkey_msg, &extr, pfkey_reply);
  54963. + if (error) {
  54964. + *pfkey_reply = NULL;
  54965. + }
  54966. +#endif
  54967. + errlab:
  54968. + if(extr.ips != NULL) {
  54969. + ipsec_sa_wipe(extr.ips);
  54970. + }
  54971. + if(extr.ips2 != NULL) {
  54972. + ipsec_sa_wipe(extr.ips2);
  54973. + }
  54974. + if (extr.eroute != NULL) {
  54975. + kfree(extr.eroute);
  54976. + }
  54977. + return(error);
  54978. +}
  54979. +
  54980. +/*
  54981. + * $Log: pfkey_v2_parser.c,v $
  54982. + * Revision 1.134.2.2 2006/10/06 21:39:26 paul
  54983. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  54984. + * set. This is defined through autoconf.h which is included through the
  54985. + * linux kernel build macros.
  54986. + *
  54987. + * Revision 1.134.2.1 2006/05/01 14:37:25 mcr
  54988. + * ip_chk_addr -> inet_addr_type for more direct 2.4/2.6 support.
  54989. + *
  54990. + * Revision 1.134 2005/05/11 01:48:20 mcr
  54991. + * removed "poor-man"s OOP in favour of proper C structures.
  54992. + *
  54993. + * Revision 1.133 2005/04/29 05:10:22 mcr
  54994. + * removed from extraenous includes to make unit testing easier.
  54995. + *
  54996. + * Revision 1.132 2005/04/14 20:56:24 mcr
  54997. + * moved (pfkey_)ipsec_sa_init to ipsec_sa.c.
  54998. + *
  54999. + * Revision 1.131 2005/01/26 00:50:35 mcr
  55000. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  55001. + * and make sure that NAT_TRAVERSAL is set as well to match
  55002. + * userspace compiles of code.
  55003. + *
  55004. + * Revision 1.130 2004/09/08 17:21:36 ken
  55005. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  55006. + *
  55007. + * Revision 1.129 2004/09/06 18:36:30 mcr
  55008. + * if a protocol can not be found, then log it. This is not
  55009. + * debugging.
  55010. + *
  55011. + * Revision 1.128 2004/08/21 00:45:19 mcr
  55012. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  55013. + *
  55014. + * Revision 1.127 2004/08/20 21:45:45 mcr
  55015. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  55016. + * be 26sec compatible. But, some defines where changed.
  55017. + *
  55018. + * Revision 1.126 2004/08/17 03:27:23 mcr
  55019. + * klips 2.6 edits.
  55020. + *
  55021. + * Revision 1.125 2004/08/04 15:57:07 mcr
  55022. + * moved des .h files to include/des/ *
  55023. + * included 2.6 protocol specific things
  55024. + * started at NAT-T support, but it will require a kernel patch.
  55025. + *
  55026. + * Revision 1.124 2004/07/10 19:11:18 mcr
  55027. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  55028. + *
  55029. + * Revision 1.123 2004/04/06 02:49:26 mcr
  55030. + * pullup of algo code from alg-branch.
  55031. + *
  55032. + * Revision 1.122.2.2 2004/04/05 04:30:46 mcr
  55033. + * patches for alg-branch to compile/work with 2.x openswan
  55034. + *
  55035. + * Revision 1.122.2.1 2003/12/22 15:25:52 jjo
  55036. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  55037. + *
  55038. + * Revision 1.122 2003/12/10 01:14:27 mcr
  55039. + * NAT-traversal patches to KLIPS.
  55040. + *
  55041. + * Revision 1.121 2003/10/31 02:27:55 mcr
  55042. + * pulled up port-selector patches and sa_id elimination.
  55043. + *
  55044. + * Revision 1.120.4.2 2003/10/29 01:30:41 mcr
  55045. + * elimited "struct sa_id".
  55046. + *
  55047. + * Revision 1.120.4.1 2003/09/21 13:59:56 mcr
  55048. + * pre-liminary X.509 patch - does not yet pass tests.
  55049. + *
  55050. + * Revision 1.120 2003/04/03 17:38:09 rgb
  55051. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  55052. + *
  55053. + * Revision 1.119 2003/02/06 01:52:37 rgb
  55054. + * Removed no longer relevant comment
  55055. + *
  55056. + * Revision 1.118 2003/01/30 02:32:44 rgb
  55057. + *
  55058. + * Transmit error code through to caller from callee for better diagnosis of problems.
  55059. + *
  55060. + * Revision 1.117 2003/01/16 18:48:13 rgb
  55061. + *
  55062. + * Fixed sign bug in error return from an sa allocation call in
  55063. + * pfkey_msg_interp.
  55064. + *
  55065. + * Revision 1.116 2002/10/17 16:38:01 rgb
  55066. + * Change pfkey_alloc_eroute() to never static since its consumers
  55067. + * have been moved outside the file.
  55068. + *
  55069. + * Revision 1.115 2002/10/12 23:11:53 dhr
  55070. + *
  55071. + * [KenB + DHR] more 64-bit cleanup
  55072. + *
  55073. + * Revision 1.114 2002/10/05 05:02:58 dhr
  55074. + *
  55075. + * C labels go on statements
  55076. + *
  55077. + * Revision 1.113 2002/09/30 19:11:22 rgb
  55078. + * Turn on debugging for upgoing acquire messages to test for reliability.
  55079. + *
  55080. + * Revision 1.112 2002/09/20 15:41:16 rgb
  55081. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  55082. + * Added sadb_x_sa_ref to struct sadb_sa.
  55083. + * Added ref parameter to pfkey_sa_build().
  55084. + *
  55085. + * Revision 1.111 2002/09/20 05:02:08 rgb
  55086. + * Added memory allocation debugging.
  55087. + * Convert to switch to divulge hmac keys for debugging.
  55088. + * Added text labels to elucidate numeric values presented.
  55089. + *
  55090. + * Revision 1.110 2002/08/03 18:03:05 mcr
  55091. + * loop that checks for SPI's to have been already linked
  55092. + * fails to actually step to next pointer, but continuously
  55093. + * resets to head of list. Wrong pointer used.
  55094. + * test east-icmp-02 revealed this.
  55095. + *
  55096. + * Revision 1.109 2002/07/26 08:48:31 rgb
  55097. + * Added SA ref table code.
  55098. + *
  55099. + * Revision 1.108 2002/05/27 18:55:03 rgb
  55100. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  55101. + *
  55102. + * Revision 1.107 2002/05/23 07:16:08 rgb
  55103. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  55104. + * Pointer clean-up.
  55105. + * Added refcount code.
  55106. + *
  55107. + * Revision 1.106 2002/05/14 02:34:13 rgb
  55108. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  55109. + * with "put" usage in the kernel.
  55110. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  55111. + * ipsec_sa or ipsec_sa.
  55112. + * Moved all the extension parsing functions to pfkey_v2_ext_process.c.
  55113. + *
  55114. + * Revision 1.105 2002/04/24 07:55:32 mcr
  55115. + * #include patches and Makefiles for post-reorg compilation.
  55116. + *
  55117. + * Revision 1.104 2002/04/24 07:36:34 mcr
  55118. + * Moved from ./klips/net/ipsec/pfkey_v2_parser.c,v
  55119. + *
  55120. + * Revision 1.103 2002/04/20 00:12:25 rgb
  55121. + * Added esp IV CBC attack fix, disabled.
  55122. + *
  55123. + * Revision 1.102 2002/03/08 01:15:17 mcr
  55124. + * put some internal structure only debug messages behind
  55125. + * && sysctl_ipsec_debug_verbose.
  55126. + *
  55127. + * Revision 1.101 2002/01/29 17:17:57 mcr
  55128. + * moved include of ipsec_param.h to after include of linux/kernel.h
  55129. + * otherwise, it seems that some option that is set in ipsec_param.h
  55130. + * screws up something subtle in the include path to kernel.h, and
  55131. + * it complains on the snprintf() prototype.
  55132. + *
  55133. + * Revision 1.100 2002/01/29 04:00:54 mcr
  55134. + * more excise of kversions.h header.
  55135. + *
  55136. + * Revision 1.99 2002/01/29 02:13:19 mcr
  55137. + * introduction of ipsec_kversion.h means that include of
  55138. + * ipsec_param.h must preceed any decisions about what files to
  55139. + * include to deal with differences in kernel source.
  55140. + *
  55141. + * Revision 1.98 2002/01/12 02:57:57 mcr
  55142. + * first regression test causes acquire messages to be lost
  55143. + * 100% of the time. This is to help testing of pluto.
  55144. + *
  55145. + * Revision 1.97 2001/11/26 09:23:52 rgb
  55146. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  55147. + *
  55148. + * Revision 1.93.2.4 2001/10/23 04:20:27 mcr
  55149. + * parity was forced on wrong structure! prototypes help here.
  55150. + *
  55151. + * Revision 1.93.2.3 2001/10/22 21:14:59 mcr
  55152. + * include des.h, removed phony prototypes and fixed calling
  55153. + * conventions to match real prototypes.
  55154. + *
  55155. + * Revision 1.93.2.2 2001/10/15 05:39:03 mcr
  55156. + * %08lx is not the right format for u32. Use %08x. 64-bit safe? ha.
  55157. + *
  55158. + * Revision 1.93.2.1 2001/09/25 02:30:14 mcr
  55159. + * struct tdb -> struct ipsec_sa.
  55160. + * use new lifetime structure. common format routines for debug.
  55161. + *
  55162. + * Revision 1.96 2001/11/06 20:47:54 rgb
  55163. + * Fixed user context call to ipsec_dev_start_xmit() bug. Call
  55164. + * dev_queue_xmit() instead.
  55165. + *
  55166. + * Revision 1.95 2001/11/06 19:47:46 rgb
  55167. + * Added packet parameter to lifetime and comb structures.
  55168. + *
  55169. + * Revision 1.94 2001/10/18 04:45:23 rgb
  55170. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  55171. + * lib/freeswan.h version macros moved to lib/kversions.h.
  55172. + * Other compiler directive cleanups.
  55173. + *
  55174. + * Revision 1.93 2001/09/20 15:32:59 rgb
  55175. + * Min/max cleanup.
  55176. + *
  55177. + * Revision 1.92 2001/09/19 16:35:48 rgb
  55178. + * PF_KEY ident fix for getspi from NetCelo (puttdb duplication).
  55179. + *
  55180. + * Revision 1.91 2001/09/15 16:24:06 rgb
  55181. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  55182. + *
  55183. + * Revision 1.90 2001/09/14 16:58:38 rgb
  55184. + * Added support for storing the first and last packets through a HOLD.
  55185. + *
  55186. + * Revision 1.89 2001/09/08 21:14:07 rgb
  55187. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  55188. + * Better state coherency (error management) between pf_key and IKE daemon.
  55189. + * (NetCelo)
  55190. + *
  55191. + * Revision 1.88 2001/08/27 19:42:44 rgb
  55192. + * Fix memory leak of encrypt and auth structs in pfkey register.
  55193. + *
  55194. + * Revision 1.87 2001/07/06 19:50:46 rgb
  55195. + * Removed unused debugging code.
  55196. + * Added inbound policy checking code for IPIP SAs.
  55197. + *
  55198. + * Revision 1.86 2001/06/20 06:26:04 rgb
  55199. + * Changed missing SA errors from EEXIST to ENOENT and added debug output
  55200. + * for already linked SAs.
  55201. + *
  55202. + * Revision 1.85 2001/06/15 04:57:02 rgb
  55203. + * Remove single error return condition check and check for all errors in
  55204. + * the case of a replace eroute delete operation. This means that
  55205. + * applications must expect to be deleting something before replacing it
  55206. + * and if nothing is found, complain.
  55207. + *
  55208. + * Revision 1.84 2001/06/14 19:35:12 rgb
  55209. + * Update copyright date.
  55210. + *
  55211. + * Revision 1.83 2001/06/12 00:03:19 rgb
  55212. + * Silence debug set/unset under normal conditions.
  55213. + *
  55214. + * Revision 1.82 2001/05/30 08:14:04 rgb
  55215. + * Removed vestiges of esp-null transforms.
  55216. + *
  55217. + * Revision 1.81 2001/05/27 06:12:12 rgb
  55218. + * Added structures for pid, packet count and last access time to eroute.
  55219. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  55220. + *
  55221. + * Revision 1.80 2001/05/03 19:43:59 rgb
  55222. + * Check error return codes for all build function calls.
  55223. + * Standardise on SENDERR() macro.
  55224. + *
  55225. + * Revision 1.79 2001/04/20 21:09:16 rgb
  55226. + * Cleaned up fixed tdbwipes.
  55227. + * Free pfkey_reply and clean up extensions_reply for grpsa, addflow and
  55228. + * delflow (Per Cederqvist) plugging memleaks.
  55229. + *
  55230. + * Revision 1.78 2001/04/19 19:02:39 rgb
  55231. + * Fixed extr.tdb freeing, stealing it for getspi, update and add.
  55232. + * Refined a couple of spinlocks, fixed the one in update.
  55233. + *
  55234. + * Revision 1.77 2001/04/18 20:26:16 rgb
  55235. + * Wipe/free eroute and both tdbs from extr at end of pfkey_msg_interp()
  55236. + * instead of inside each message type parser. This fixes two memleaks.
  55237. + *
  55238. + * Revision 1.76 2001/04/17 23:51:18 rgb
  55239. + * Quiet down pfkey_x_debug_process().
  55240. + *
  55241. + * Revision 1.75 2001/03/29 01:55:05 rgb
  55242. + * Fixed pfkey key init memleak.
  55243. + * Fixed pfkey encryption key debug output.
  55244. + *
  55245. + * Revision 1.74 2001/03/27 05:29:14 rgb
  55246. + * Debug output cleanup/silencing.
  55247. + *
  55248. + * Revision 1.73 2001/02/28 05:03:28 rgb
  55249. + * Clean up and rationalise startup messages.
  55250. + *
  55251. + * Revision 1.72 2001/02/27 22:24:56 rgb
  55252. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  55253. + * Check for satoa() return codes.
  55254. + *
  55255. + * Revision 1.71 2001/02/27 06:59:30 rgb
  55256. + * Added satype2name() conversions most places satype is debug printed.
  55257. + *
  55258. + * Revision 1.70 2001/02/26 22:37:08 rgb
  55259. + * Fixed 'unknown proto' INT bug in new code.
  55260. + * Added satype to protocol debugging instrumentation.
  55261. + *
  55262. + * Revision 1.69 2001/02/26 19:57:51 rgb
  55263. + * Re-formatted debug output (split lines, consistent spacing).
  55264. + * Fixed as yet undetected FLUSH bug which called ipsec_tdbcleanup()
  55265. + * with an satype instead of proto.
  55266. + * Checked for satype consistency and fixed minor bugs.
  55267. + * Fixed undetected ungrpspi bug that tried to upmsg a second tdb.
  55268. + * Check for satype sanity in pfkey_expire().
  55269. + * Added satype sanity check to addflow.
  55270. + *
  55271. + * Revision 1.68 2001/02/12 23:14:40 rgb
  55272. + * Remove double spin lock in pfkey_expire().
  55273. + *
  55274. + * Revision 1.67 2001/01/31 19:23:40 rgb
  55275. + * Fixed double-unlock bug introduced by grpsa upmsg (found by Lars Heete).
  55276. + *
  55277. + * Revision 1.66 2001/01/29 22:20:04 rgb
  55278. + * Fix minor add upmsg lifetime bug.
  55279. + *
  55280. + * Revision 1.65 2001/01/24 06:12:33 rgb
  55281. + * Fixed address extension compile bugs just introduced.
  55282. + *
  55283. + * Revision 1.64 2001/01/24 00:31:15 rgb
  55284. + * Added upmsg for addflow/delflow.
  55285. + *
  55286. + * Revision 1.63 2001/01/23 22:02:55 rgb
  55287. + * Added upmsg to x_grpsa.
  55288. + * Fixed lifetimes extentions to add/update/get upmsg.
  55289. + *
  55290. + * Revision 1.62 2000/11/30 21:47:51 rgb
  55291. + * Fix error return bug after returning from pfkey_tdb_init().
  55292. + *
  55293. + * Revision 1.61 2000/11/17 18:10:29 rgb
  55294. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  55295. + * network byte order since this is the way PF_KEYv2 stored spis.
  55296. + *
  55297. + * Revision 1.60 2000/11/06 04:34:53 rgb
  55298. + * Changed non-exported functions to DEBUG_NO_STATIC.
  55299. + * Add Svenning's adaptive content compression.
  55300. + * Ditched spin_lock_irqsave in favour of spin_lock/_bh.
  55301. + * Fixed double unlock bug (Svenning).
  55302. + * Fixed pfkey_msg uninitialized bug in pfkey_{expire,acquire}().
  55303. + * Fixed incorrect extension type (prop) in pfkey)acquire().
  55304. + *
  55305. + * Revision 1.59 2000/10/11 15:25:12 rgb
  55306. + * Fixed IPCOMP disabled compile bug.
  55307. + *
  55308. + * Revision 1.58 2000/10/11 14:54:03 rgb
  55309. + * Fixed pfkey_acquire() satype to SADB_SATYPE_ESP and removed pfkey
  55310. + * protocol violations of setting pfkey_address_build() protocol parameter
  55311. + * to non-zero except in the case of pfkey_acquire().
  55312. + *
  55313. + * Revision 1.57 2000/10/10 20:10:18 rgb
  55314. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  55315. + *
  55316. + * Revision 1.56 2000/10/06 20:24:36 rgb
  55317. + * Fixes to pfkey_acquire to initialize extensions[] and use correct
  55318. + * ipproto.
  55319. + *
  55320. + * Revision 1.55 2000/10/03 03:20:57 rgb
  55321. + * Added brackets to get a?b:c scope right for pfkey_register reply.
  55322. + *
  55323. + * Revision 1.54 2000/09/29 19:49:30 rgb
  55324. + * As-yet-unused-bits cleanup.
  55325. + *
  55326. + * Revision 1.53 2000/09/28 00:35:45 rgb
  55327. + * Padded SATYPE printout in pfkey_register for vertical alignment.
  55328. + *
  55329. + * Revision 1.52 2000/09/20 16:21:58 rgb
  55330. + * Cleaned up ident string alloc/free.
  55331. + *
  55332. + * Revision 1.51 2000/09/20 04:04:20 rgb
  55333. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  55334. + * oopsen.
  55335. + *
  55336. + * Revision 1.50 2000/09/16 01:10:53 rgb
  55337. + * Fixed unused var warning with debug off.
  55338. + *
  55339. + * Revision 1.49 2000/09/15 11:37:02 rgb
  55340. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  55341. + * IPCOMP zlib deflate code.
  55342. + *
  55343. + * Revision 1.48 2000/09/15 04:57:57 rgb
  55344. + * Cleaned up existing IPCOMP code before svenning addition.
  55345. + * Initialize pfkey_reply and extensions_reply in case of early error in
  55346. + * message parsing functions (thanks Kai!).
  55347. + *
  55348. + * Revision 1.47 2000/09/13 08:02:56 rgb
  55349. + * Added KMd registration notification.
  55350. + *
  55351. + * Revision 1.46 2000/09/12 22:35:36 rgb
  55352. + * Restructured to remove unused extensions from CLEARFLOW messages.
  55353. + *
  55354. + * Revision 1.45 2000/09/12 03:24:23 rgb
  55355. + * Converted #if0 debugs to sysctl.
  55356. + *
  55357. + * Revision 1.44 2000/09/09 06:38:39 rgb
  55358. + * Correct SADB message type for update, add and delete.
  55359. + *
  55360. + * Revision 1.43 2000/09/08 19:19:56 rgb
  55361. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  55362. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  55363. + * Put in sanity checks in most msg type parsers to catch invalid satypes
  55364. + * and empty socket lists.
  55365. + * Moved spin-locks in pfkey_get_parse() to simplify.
  55366. + * Added pfkey_acquire().
  55367. + * Added upwards messages to update, add, delete, acquire_parse,
  55368. + * expire_parse and flush.
  55369. + * Fix pfkey_prop_build() parameter to be only single indirection.
  55370. + * Changed all replies to use pfkey_reply.
  55371. + * Check return code on puttdb() and deltdbchain() in getspi, update,
  55372. + * add, delete.
  55373. + * Fixed up all pfkey replies to open and registered sockets.
  55374. + *
  55375. + * Revision 1.42 2000/09/01 18:50:26 rgb
  55376. + * Added a supported algorithms array lists, one per satype and registered
  55377. + * existing algorithms.
  55378. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  55379. + * list.
  55380. + * Only send pfkey_expire() messages to sockets registered for that satype.
  55381. + * Added reply to pfkey_getspi_parse().
  55382. + * Added reply to pfkey_get_parse().
  55383. + * Fixed debug output label bug in pfkey_lifetime_process().
  55384. + * Cleaned up pfkey_sa_process a little.
  55385. + * Moved pfkey_safe_build() above message type parsers to make it available
  55386. + * for creating replies.
  55387. + * Added comments for future work in pfkey_acquire_parse().
  55388. + * Fleshed out guts of pfkey_register_parse().
  55389. + *
  55390. + * Revision 1.41 2000/08/24 16:58:11 rgb
  55391. + * Fixed key debugging variables.
  55392. + * Fixed error return code for a failed search.
  55393. + * Changed order of pfkey_get operations.
  55394. + *
  55395. + * Revision 1.40 2000/08/21 16:32:27 rgb
  55396. + * Re-formatted for cosmetic consistency and readability.
  55397. + *
  55398. + * Revision 1.39 2000/08/20 21:38:57 rgb
  55399. + * Bugfixes to as-yet-unused pfkey_update_parse() and
  55400. + * pfkey_register_parse(). (Momchil)
  55401. + * Added functions pfkey_safe_build(), pfkey_expire() and
  55402. + * pfkey_build_reply(). (Momchil)
  55403. + * Added a pfkey_reply parameter to pfkey_msg_interp(). (Momchil)
  55404. + *
  55405. + * Revision 1.38 2000/08/18 21:30:41 rgb
  55406. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  55407. + *
  55408. + * Revision 1.37 2000/08/18 18:18:02 rgb
  55409. + * Cosmetic and descriptive changes made to debug test.
  55410. + * getspi and update fixes from Momchil.
  55411. + *
  55412. + * Revision 1.36 2000/08/15 15:41:55 rgb
  55413. + * Fixed the (as yet unused and untested) pfkey_getspi() routine.
  55414. + *
  55415. + * Revision 1.35 2000/08/01 14:51:52 rgb
  55416. + * Removed _all_ remaining traces of DES.
  55417. + *
  55418. + * Revision 1.34 2000/07/28 14:58:32 rgb
  55419. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  55420. + *
  55421. + * Revision 1.33 2000/06/28 05:50:11 rgb
  55422. + * Actually set iv_bits.
  55423. + *
  55424. + * Revision 1.32 2000/05/30 18:36:56 rgb
  55425. + * Fix AH auth hash setup bug. This breaks interop with previous PF_KEY
  55426. + * FreeS/WAN, but fixes interop with other implementations.
  55427. + *
  55428. + * Revision 1.31 2000/03/16 14:05:48 rgb
  55429. + * Fixed brace scope preventing non-debug compile.
  55430. + * Added null parameter check for pfkey_x_debug().
  55431. + *
  55432. + * Revision 1.30 2000/01/22 23:21:13 rgb
  55433. + * Use new function satype2proto().
  55434. + *
  55435. + * Revision 1.29 2000/01/22 08:40:21 rgb
  55436. + * Invert condition to known value to avoid AF_INET6 in 2.0.36.
  55437. + *
  55438. + * Revision 1.28 2000/01/22 07:58:57 rgb
  55439. + * Fixed REPLACEFLOW bug, missing braces around KLIPS_PRINT *and* SENDERR.
  55440. + *
  55441. + * Revision 1.27 2000/01/22 03:48:01 rgb
  55442. + * Added extr pointer component debugging.
  55443. + *
  55444. + * Revision 1.26 2000/01/21 09:41:25 rgb
  55445. + * Changed a (void*) to (char*) cast to do proper pointer math.
  55446. + * Don't call tdbwipe if tdb2 is NULL.
  55447. + *
  55448. + * Revision 1.25 2000/01/21 06:21:01 rgb
  55449. + * Added address cases for eroute flows.
  55450. + * Tidied up compiler directive indentation for readability.
  55451. + * Added ictx,octx vars for simplification.
  55452. + * Added macros for HMAC padding magic numbers.
  55453. + * Converted from double tdb arguments to one structure (extr)
  55454. + * containing pointers to all temporary information structures
  55455. + * and checking for valid arguments to all ext processors and
  55456. + * msg type parsers.
  55457. + * Added spiungrp'ing.
  55458. + * Added klipsdebug switching capability.
  55459. + * Removed sa_process() check for zero protocol.
  55460. + * Added address case for DST2 for grouping.
  55461. + * Added/changed minor debugging instrumentation.
  55462. + * Fixed spigrp for single said, ungrouping case.
  55463. + * Added code to parse addflow and delflow messages.
  55464. + * Removed redundant statements duplicating tdbwipe() functionality
  55465. + * and causing double kfrees.
  55466. + * Permit addflow to have a protocol of 0.
  55467. + *
  55468. + * Revision 1.24 1999/12/09 23:23:00 rgb
  55469. + * Added check to pfkey_sa_process() to do eroutes.
  55470. + * Converted to DIVUP() macro.
  55471. + * Converted if() to switch() in pfkey_register_parse().
  55472. + * Use new pfkey_extensions_init() instead of memset().
  55473. + *
  55474. + * Revision 1.23 1999/12/01 22:18:13 rgb
  55475. + * Preset minspi and maxspi values in case and spirange extension is not
  55476. + * included and check for the presence of an spirange extension before
  55477. + * using it. Initialise tdb_sastate to LARVAL.
  55478. + * Fixed debugging output typo.
  55479. + * Fixed authentication context initialisation bugs (4 places).
  55480. + *
  55481. + * Revision 1.22 1999/11/27 11:53:08 rgb
  55482. + * Moved pfkey_msg_parse prototype to pfkey.h
  55483. + * Moved exts_permitted/required prototype to pfkey.h.
  55484. + * Moved sadb_satype2proto protocol lookup table to lib/pfkey_v2_parse.c.
  55485. + * Deleted SADB_X_EXT_SA2 code from pfkey_sa_process() since it will never
  55486. + * be called.
  55487. + * Moved protocol/algorithm checks to lib/pfkey_v2_parse.c
  55488. + * Debugging error messages added.
  55489. + * Enable lifetime_current checking.
  55490. + * Remove illegal requirement for SA extension to be present in an
  55491. + * originating GETSPI call.
  55492. + * Re-instate requirement for UPDATE or ADD message to be MATURE.
  55493. + * Add argument to pfkey_msg_parse() for direction.
  55494. + * Fixed IPIP dst address bug and purged redundant, leaky code.
  55495. + *
  55496. + * Revision 1.21 1999/11/24 05:24:20 rgb
  55497. + * hanged 'void*extensions' to 'struct sadb_ext*extensions'.
  55498. + * Fixed indention.
  55499. + * Ditched redundant replay check.
  55500. + * Fixed debug message text from 'parse' to 'process'.
  55501. + * Added more debug output.
  55502. + * Forgot to zero extensions array causing bug, fixed.
  55503. + *
  55504. + * Revision 1.20 1999/11/23 23:08:13 rgb
  55505. + * Move all common parsing code to lib/pfkey_v2_parse.c and rename
  55506. + * remaining bits to *_process. (PJO)
  55507. + * Add macros for dealing with alignment and rounding up more opaquely.
  55508. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  55509. + * Sort out pfkey and freeswan headers, putting them in a library path.
  55510. + * Corrected a couple of bugs in as-yet-inactive code.
  55511. + *
  55512. + * Revision 1.19 1999/11/20 22:01:10 rgb
  55513. + * Add more descriptive error messages for non-zero reserved fields.
  55514. + * Add more descriptive error message for spirange parsing.
  55515. + * Start on supported extension parsing.
  55516. + * Start on register and get message parsing.
  55517. + *
  55518. + * Revision 1.18 1999/11/18 04:09:20 rgb
  55519. + * Replaced all kernel version macros to shorter, readable form.
  55520. + *
  55521. + * Revision 1.17 1999/11/17 15:53:41 rgb
  55522. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  55523. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  55524. + * klips/net/ipsec/Makefile.
  55525. + *
  55526. + * Revision 1.16 1999/10/26 16:57:43 rgb
  55527. + * Add shorter macros for compiler directives to visually clean-up.
  55528. + * Give ipv6 code meaningful compiler directive.
  55529. + * Add comments to other #if 0 debug code.
  55530. + * Remove unused *_bh_atomic() calls.
  55531. + * Fix mis-placed spinlock.
  55532. + *
  55533. + * Revision 1.15 1999/10/16 18:27:10 rgb
  55534. + * Clean-up unused cruft.
  55535. + * Fix-up lifetime_allocations_c and lifetime_addtime_c initialisations.
  55536. + *
  55537. + * Revision 1.14 1999/10/08 18:37:34 rgb
  55538. + * Fix end-of-line spacing to sate whining PHMs.
  55539. + *
  55540. + * Revision 1.13 1999/10/03 18:49:12 rgb
  55541. + * Spinlock fixes for 2.0.xx and 2.3.xx.
  55542. + *
  55543. + * Revision 1.12 1999/10/01 15:44:54 rgb
  55544. + * Move spinlock header include to 2.1> scope.
  55545. + *
  55546. + * Revision 1.11 1999/10/01 00:05:45 rgb
  55547. + * Added tdb structure locking.
  55548. + * Use 'jiffies' instead of do_get_timeofday().
  55549. + * Fix lifetime assignments.
  55550. + *
  55551. + * Revision 1.10 1999/09/21 15:24:45 rgb
  55552. + * Rework spirange code to save entropy and prevent endless loops.
  55553. + *
  55554. + * Revision 1.9 1999/09/16 12:10:21 rgb
  55555. + * Minor fixes to random spi selection for correctness and entropy conservation.
  55556. + *
  55557. + * Revision 1.8 1999/05/25 22:54:46 rgb
  55558. + * Fix comparison that should be an assignment in an if.
  55559. + *
  55560. + * Revision 1.7 1999/05/09 03:25:37 rgb
  55561. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  55562. + *
  55563. + * Revision 1.6 1999/05/08 21:32:30 rgb
  55564. + * Fix error return reporting.
  55565. + *
  55566. + * Revision 1.5 1999/05/05 22:02:33 rgb
  55567. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  55568. + *
  55569. + * Revision 1.4 1999/04/29 15:22:40 rgb
  55570. + * Standardise an error return method.
  55571. + * Add debugging instrumentation.
  55572. + * Add check for existence of macros min/max.
  55573. + * Add extensions permitted/required in/out filters.
  55574. + * Add satype-to-protocol table.
  55575. + * Add a second tdb pointer to each parser to accomodate GRPSA.
  55576. + * Move AH & no_algo_set to GETSPI, UPDATE and ADD.
  55577. + * Add OOO window check.
  55578. + * Add support for IPPROTO_IPIP and hooks for IPPROTO_COMP.
  55579. + * Add timestamp to lifetime parse.
  55580. + * Fix address structure length checking bug.
  55581. + * Fix address structure allocation bug (forgot to kmalloc!).
  55582. + * Add checks for extension lengths.
  55583. + * Add checks for extension reserved illegal values.
  55584. + * Add check for spirange legal values.
  55585. + * Add an extension type for parsing a second satype, SA and
  55586. + * DST_ADDRESS.
  55587. + * Make changes to tdb_init() template to get pfkey_tdb_init(),
  55588. + * eliminating any mention of xformsw.
  55589. + * Implement getspi, update and grpsa (not tested).
  55590. + * Add stubs for as yet unimplemented message types.
  55591. + * Add table of message parsers to substitute for msg_parse switch.
  55592. + *
  55593. + * Revision 1.3 1999/04/15 17:58:07 rgb
  55594. + * Add RCSID labels.
  55595. + *
  55596. + * Revision 1.2 1999/04/15 15:37:26 rgb
  55597. + * Forward check changes from POST1_00 branch.
  55598. + *
  55599. + * Revision 1.1.2.1 1999/03/26 20:58:56 rgb
  55600. + * Add pfkeyv2 support to KLIPS.
  55601. + *
  55602. + * Local variables:
  55603. + * c-file-style: "linux"
  55604. + * End:
  55605. + *
  55606. + */
  55607. --- /dev/null Tue Mar 11 13:02:56 2003
  55608. +++ linux/net/ipsec/prng.c Mon Feb 9 13:51:03 2004
  55609. @@ -0,0 +1,201 @@
  55610. +/*
  55611. + * crypto-class pseudorandom number generator
  55612. + * currently uses same algorithm as RC4(TM), from Schneier 2nd ed p397
  55613. + * Copyright (C) 2002 Henry Spencer.
  55614. + *
  55615. + * This library is free software; you can redistribute it and/or modify it
  55616. + * under the terms of the GNU Library General Public License as published by
  55617. + * the Free Software Foundation; either version 2 of the License, or (at your
  55618. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  55619. + *
  55620. + * This library is distributed in the hope that it will be useful, but
  55621. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  55622. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  55623. + * License for more details.
  55624. + *
  55625. + * RCSID $Id: prng.c,v 1.7 2004/07/10 07:48:36 mcr Exp $
  55626. + */
  55627. +#include "openswan.h"
  55628. +
  55629. +/*
  55630. + - prng_init - initialize PRNG from a key
  55631. + */
  55632. +void
  55633. +prng_init(prng, key, keylen)
  55634. +struct prng *prng;
  55635. +const unsigned char *key;
  55636. +size_t keylen;
  55637. +{
  55638. + unsigned char k[256];
  55639. + int i, j;
  55640. + unsigned const char *p;
  55641. + unsigned const char *keyend = key + keylen;
  55642. + unsigned char t;
  55643. +
  55644. + for (i = 0; i <= 255; i++)
  55645. + prng->sbox[i] = i;
  55646. + p = key;
  55647. + for (i = 0; i <= 255; i++) {
  55648. + k[i] = *p++;
  55649. + if (p >= keyend)
  55650. + p = key;
  55651. + }
  55652. + j = 0;
  55653. + for (i = 0; i <= 255; i++) {
  55654. + j = (j + prng->sbox[i] + k[i]) & 0xff;
  55655. + t = prng->sbox[i];
  55656. + prng->sbox[i] = prng->sbox[j];
  55657. + prng->sbox[j] = t;
  55658. + k[i] = 0; /* clear out key memory */
  55659. + }
  55660. + prng->i = 0;
  55661. + prng->j = 0;
  55662. + prng->count = 0;
  55663. +}
  55664. +
  55665. +/*
  55666. + - prng_bytes - get some pseudorandom bytes from PRNG
  55667. + */
  55668. +void
  55669. +prng_bytes(prng, dst, dstlen)
  55670. +struct prng *prng;
  55671. +unsigned char *dst;
  55672. +size_t dstlen;
  55673. +{
  55674. + int i, j, t;
  55675. + unsigned char *p = dst;
  55676. + size_t remain = dstlen;
  55677. +# define MAX 4000000000ul
  55678. +
  55679. + while (remain > 0) {
  55680. + i = (prng->i + 1) & 0xff;
  55681. + prng->i = i;
  55682. + j = (prng->j + prng->sbox[i]) & 0xff;
  55683. + prng->j = j;
  55684. + t = prng->sbox[i];
  55685. + prng->sbox[i] = prng->sbox[j];
  55686. + prng->sbox[j] = t;
  55687. + t = (t + prng->sbox[i]) & 0xff;
  55688. + *p++ = prng->sbox[t];
  55689. + remain--;
  55690. + }
  55691. + if (prng->count < MAX - dstlen)
  55692. + prng->count += dstlen;
  55693. + else
  55694. + prng->count = MAX;
  55695. +}
  55696. +
  55697. +/*
  55698. + - prnt_count - how many bytes have been extracted from PRNG so far?
  55699. + */
  55700. +unsigned long
  55701. +prng_count(prng)
  55702. +struct prng *prng;
  55703. +{
  55704. + return prng->count;
  55705. +}
  55706. +
  55707. +/*
  55708. + - prng_final - clear out PRNG to ensure nothing left in memory
  55709. + */
  55710. +void
  55711. +prng_final(prng)
  55712. +struct prng *prng;
  55713. +{
  55714. + int i;
  55715. +
  55716. + for (i = 0; i <= 255; i++)
  55717. + prng->sbox[i] = 0;
  55718. + prng->i = 0;
  55719. + prng->j = 0;
  55720. + prng->count = 0; /* just for good measure */
  55721. +}
  55722. +
  55723. +
  55724. +
  55725. +#ifdef PRNG_MAIN
  55726. +
  55727. +#include <stdio.h>
  55728. +
  55729. +void regress();
  55730. +
  55731. +int
  55732. +main(argc, argv)
  55733. +int argc;
  55734. +char *argv[];
  55735. +{
  55736. + struct prng pr;
  55737. + unsigned char buf[100];
  55738. + unsigned char *p;
  55739. + size_t n;
  55740. +
  55741. + if (argc < 2) {
  55742. + fprintf(stderr, "Usage: %s {key|-r}\n", argv[0]);
  55743. + exit(2);
  55744. + }
  55745. +
  55746. + if (strcmp(argv[1], "-r") == 0) {
  55747. + regress();
  55748. + fprintf(stderr, "regress() returned?!?\n");
  55749. + exit(1);
  55750. + }
  55751. +
  55752. + prng_init(&pr, argv[1], strlen(argv[1]));
  55753. + prng_bytes(&pr, buf, 32);
  55754. + printf("0x");
  55755. + for (p = buf, n = 32; n > 0; p++, n--)
  55756. + printf("%02x", *p);
  55757. + printf("\n%lu bytes\n", prng_count(&pr));
  55758. + prng_final(&pr);
  55759. + exit(0);
  55760. +}
  55761. +
  55762. +void
  55763. +regress()
  55764. +{
  55765. + struct prng pr;
  55766. + unsigned char buf[100];
  55767. + unsigned char *p;
  55768. + size_t n;
  55769. + /* somewhat non-random sample key */
  55770. + unsigned char key[] = "here we go gathering nuts in May";
  55771. + /* first thirty bytes of output from that key */
  55772. + unsigned char good[] = "\x3f\x02\x8e\x4a\x2a\xea\x23\x18\x92\x7c"
  55773. + "\x09\x52\x83\x61\xaa\x26\xce\xbb\x9d\x71"
  55774. + "\x71\xe5\x10\x22\xaf\x60\x54\x8d\x5b\x28";
  55775. + int nzero, none;
  55776. + int show = 0;
  55777. +
  55778. + prng_init(&pr, key, strlen(key));
  55779. + prng_bytes(&pr, buf, sizeof(buf));
  55780. + for (p = buf, n = sizeof(buf); n > 0; p++, n--) {
  55781. + if (*p == 0)
  55782. + nzero++;
  55783. + if (*p == 255)
  55784. + none++;
  55785. + }
  55786. + if (nzero > 3 || none > 3) {
  55787. + fprintf(stderr, "suspiciously non-random output!\n");
  55788. + show = 1;
  55789. + }
  55790. + if (memcmp(buf, good, strlen(good)) != 0) {
  55791. + fprintf(stderr, "incorrect output!\n");
  55792. + show = 1;
  55793. + }
  55794. + if (show) {
  55795. + fprintf(stderr, "0x");
  55796. + for (p = buf, n = sizeof(buf); n > 0; p++, n--)
  55797. + fprintf(stderr, "%02x", *p);
  55798. + fprintf(stderr, "\n");
  55799. + exit(1);
  55800. + }
  55801. + if (prng_count(&pr) != sizeof(buf)) {
  55802. + fprintf(stderr, "got %u bytes, but count is %lu\n",
  55803. + sizeof(buf), prng_count(&pr));
  55804. + exit(1);
  55805. + }
  55806. + prng_final(&pr);
  55807. + exit(0);
  55808. +}
  55809. +
  55810. +#endif /* PRNG_MAIN */
  55811. --- /dev/null Tue Mar 11 13:02:56 2003
  55812. +++ linux/net/ipsec/radij.c Mon Feb 9 13:51:03 2004
  55813. @@ -0,0 +1,1237 @@
  55814. +char radij_c_version[] = "RCSID $Id: radij.c,v 1.48.2.1 2006/10/06 21:39:27 paul Exp $";
  55815. +
  55816. +/*
  55817. + * This file is defived from ${SRC}/sys/net/radix.c of BSD 4.4lite
  55818. + *
  55819. + * Variable and procedure names have been modified so that they don't
  55820. + * conflict with the original BSD code, as a small number of modifications
  55821. + * have been introduced and we may want to reuse this code in BSD.
  55822. + *
  55823. + * The `j' in `radij' is pronounced as a voiceless guttural (like a Greek
  55824. + * chi or a German ch sound (as `doch', not as in `milch'), or even a
  55825. + * spanish j as in Juan. It is not as far back in the throat like
  55826. + * the corresponding Hebrew sound, nor is it a soft breath like the English h.
  55827. + * It has nothing to do with the Dutch ij sound.
  55828. + *
  55829. + * Here is the appropriate copyright notice:
  55830. + */
  55831. +
  55832. +/*
  55833. + * Copyright (c) 1988, 1989, 1993
  55834. + * The Regents of the University of California. All rights reserved.
  55835. + *
  55836. + * Redistribution and use in source and binary forms, with or without
  55837. + * modification, are permitted provided that the following conditions
  55838. + * are met:
  55839. + * 1. Redistributions of source code must retain the above copyright
  55840. + * notice, this list of conditions and the following disclaimer.
  55841. + * 2. Redistributions in binary form must reproduce the above copyright
  55842. + * notice, this list of conditions and the following disclaimer in the
  55843. + * documentation and/or other materials provided with the distribution.
  55844. + * 3. All advertising materials mentioning features or use of this software
  55845. + * must display the following acknowledgement:
  55846. + * This product includes software developed by the University of
  55847. + * California, Berkeley and its contributors.
  55848. + * 4. Neither the name of the University nor the names of its contributors
  55849. + * may be used to endorse or promote products derived from this software
  55850. + * without specific prior written permission.
  55851. + *
  55852. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  55853. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55854. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55855. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  55856. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55857. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55858. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55859. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55860. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55861. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55862. + * SUCH DAMAGE.
  55863. + *
  55864. + * @(#)radix.c 8.2 (Berkeley) 1/4/94
  55865. + */
  55866. +
  55867. +/*
  55868. + * Routines to build and maintain radix trees for routing lookups.
  55869. + */
  55870. +
  55871. +#ifndef AUTOCONF_INCLUDED
  55872. +#include <linux/config.h>
  55873. +#endif
  55874. +#include <linux/version.h>
  55875. +#include <linux/kernel.h> /* printk() */
  55876. +
  55877. +#include "openswan/ipsec_param.h"
  55878. +
  55879. +#ifdef MALLOC_SLAB
  55880. +# include <linux/slab.h> /* kmalloc() */
  55881. +#else /* MALLOC_SLAB */
  55882. +# include <linux/malloc.h> /* kmalloc() */
  55883. +#endif /* MALLOC_SLAB */
  55884. +#include <linux/errno.h> /* error codes */
  55885. +#include <linux/types.h> /* size_t */
  55886. +#include <linux/interrupt.h> /* mark_bh */
  55887. +
  55888. +#include <linux/netdevice.h> /* struct device, and other headers */
  55889. +#include <linux/etherdevice.h> /* eth_type_trans */
  55890. +#include <linux/ip.h> /* struct iphdr */
  55891. +#include <linux/skbuff.h>
  55892. +#ifdef NET_21
  55893. +# include <linux/in6.h>
  55894. +#endif /* NET_21 */
  55895. +
  55896. +#include <net/ip.h>
  55897. +
  55898. +#include <openswan.h>
  55899. +
  55900. +#include "openswan/radij.h"
  55901. +#include "openswan/ipsec_encap.h"
  55902. +#include "openswan/ipsec_radij.h"
  55903. +
  55904. +int maj_keylen;
  55905. +struct radij_mask *rj_mkfreelist;
  55906. +struct radij_node_head *mask_rjhead;
  55907. +static int gotOddMasks;
  55908. +static char *maskedKey;
  55909. +static char *rj_zeroes, *rj_ones;
  55910. +
  55911. +#define rj_masktop (mask_rjhead->rnh_treetop)
  55912. +#ifdef Bcmp
  55913. +# undef Bcmp
  55914. +#endif /* Bcmp */
  55915. +#define Bcmp(a, b, l) (l == 0 ? 0 : memcmp((caddr_t)(b), (caddr_t)(a), (size_t)l))
  55916. +/*
  55917. + * The data structure for the keys is a radix tree with one way
  55918. + * branching removed. The index rj_b at an internal node n represents a bit
  55919. + * position to be tested. The tree is arranged so that all descendants
  55920. + * of a node n have keys whose bits all agree up to position rj_b - 1.
  55921. + * (We say the index of n is rj_b.)
  55922. + *
  55923. + * There is at least one descendant which has a one bit at position rj_b,
  55924. + * and at least one with a zero there.
  55925. + *
  55926. + * A route is determined by a pair of key and mask. We require that the
  55927. + * bit-wise logical and of the key and mask to be the key.
  55928. + * We define the index of a route to associated with the mask to be
  55929. + * the first bit number in the mask where 0 occurs (with bit number 0
  55930. + * representing the highest order bit).
  55931. + *
  55932. + * We say a mask is normal if every bit is 0, past the index of the mask.
  55933. + * If a node n has a descendant (k, m) with index(m) == index(n) == rj_b,
  55934. + * and m is a normal mask, then the route applies to every descendant of n.
  55935. + * If the index(m) < rj_b, this implies the trailing last few bits of k
  55936. + * before bit b are all 0, (and hence consequently true of every descendant
  55937. + * of n), so the route applies to all descendants of the node as well.
  55938. + *
  55939. + * The present version of the code makes no use of normal routes,
  55940. + * but similar logic shows that a non-normal mask m such that
  55941. + * index(m) <= index(n) could potentially apply to many children of n.
  55942. + * Thus, for each non-host route, we attach its mask to a list at an internal
  55943. + * node as high in the tree as we can go.
  55944. + */
  55945. +
  55946. +struct radij_node *
  55947. +rj_search(v_arg, head)
  55948. + void *v_arg;
  55949. + struct radij_node *head;
  55950. +{
  55951. + register struct radij_node *x;
  55952. + register caddr_t v;
  55953. +
  55954. + for (x = head, v = v_arg; x->rj_b >= 0;) {
  55955. + if (x->rj_bmask & v[x->rj_off])
  55956. + x = x->rj_r;
  55957. + else
  55958. + x = x->rj_l;
  55959. + }
  55960. + return (x);
  55961. +};
  55962. +
  55963. +struct radij_node *
  55964. +rj_search_m(v_arg, head, m_arg)
  55965. + struct radij_node *head;
  55966. + void *v_arg, *m_arg;
  55967. +{
  55968. + register struct radij_node *x;
  55969. + register caddr_t v = v_arg, m = m_arg;
  55970. +
  55971. + for (x = head; x->rj_b >= 0;) {
  55972. + if ((x->rj_bmask & m[x->rj_off]) &&
  55973. + (x->rj_bmask & v[x->rj_off]))
  55974. + x = x->rj_r;
  55975. + else
  55976. + x = x->rj_l;
  55977. + }
  55978. + return x;
  55979. +};
  55980. +
  55981. +int
  55982. +rj_refines(m_arg, n_arg)
  55983. + void *m_arg, *n_arg;
  55984. +{
  55985. + register caddr_t m = m_arg, n = n_arg;
  55986. + register caddr_t lim, lim2 = lim = n + *(u_char *)n;
  55987. + int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
  55988. + int masks_are_equal = 1;
  55989. +
  55990. + if (longer > 0)
  55991. + lim -= longer;
  55992. + while (n < lim) {
  55993. + if (*n & ~(*m))
  55994. + return 0;
  55995. + if (*n++ != *m++)
  55996. + masks_are_equal = 0;
  55997. +
  55998. + }
  55999. + while (n < lim2)
  56000. + if (*n++)
  56001. + return 0;
  56002. + if (masks_are_equal && (longer < 0))
  56003. + for (lim2 = m - longer; m < lim2; )
  56004. + if (*m++)
  56005. + return 1;
  56006. + return (!masks_are_equal);
  56007. +}
  56008. +
  56009. +
  56010. +struct radij_node *
  56011. +rj_match(v_arg, head)
  56012. + void *v_arg;
  56013. + struct radij_node_head *head;
  56014. +{
  56015. + caddr_t v = v_arg;
  56016. + register struct radij_node *t = head->rnh_treetop, *x;
  56017. + register caddr_t cp = v, cp2, cp3;
  56018. + caddr_t cplim, mstart;
  56019. + struct radij_node *saved_t, *top = t;
  56020. + int off = t->rj_off, vlen = *(u_char *)cp, matched_off;
  56021. +
  56022. + /*
  56023. + * Open code rj_search(v, top) to avoid overhead of extra
  56024. + * subroutine call.
  56025. + */
  56026. + for (; t->rj_b >= 0; ) {
  56027. + if (t->rj_bmask & cp[t->rj_off])
  56028. + t = t->rj_r;
  56029. + else
  56030. + t = t->rj_l;
  56031. + }
  56032. + /*
  56033. + * See if we match exactly as a host destination
  56034. + */
  56035. + KLIPS_PRINT(debug_radij,
  56036. + "klips_debug:rj_match: "
  56037. + "* See if we match exactly as a host destination\n");
  56038. +
  56039. + cp += off; cp2 = t->rj_key + off; cplim = v + vlen;
  56040. + for (; cp < cplim; cp++, cp2++)
  56041. + if (*cp != *cp2)
  56042. + goto on1;
  56043. + /*
  56044. + * This extra grot is in case we are explicitly asked
  56045. + * to look up the default. Ugh!
  56046. + */
  56047. + if ((t->rj_flags & RJF_ROOT) && t->rj_dupedkey)
  56048. + t = t->rj_dupedkey;
  56049. + return t;
  56050. +on1:
  56051. + matched_off = cp - v;
  56052. + saved_t = t;
  56053. + KLIPS_PRINT(debug_radij,
  56054. + "klips_debug:rj_match: "
  56055. + "** try to match a leaf, t=0p%p\n", t);
  56056. + do {
  56057. + if (t->rj_mask) {
  56058. + /*
  56059. + * Even if we don't match exactly as a hosts;
  56060. + * we may match if the leaf we wound up at is
  56061. + * a route to a net.
  56062. + */
  56063. + cp3 = matched_off + t->rj_mask;
  56064. + cp2 = matched_off + t->rj_key;
  56065. + for (; cp < cplim; cp++)
  56066. + if ((*cp2++ ^ *cp) & *cp3++)
  56067. + break;
  56068. + if (cp == cplim)
  56069. + return t;
  56070. + cp = matched_off + v;
  56071. + }
  56072. + } while ((t = t->rj_dupedkey));
  56073. + t = saved_t;
  56074. + /* start searching up the tree */
  56075. + KLIPS_PRINT(debug_radij,
  56076. + "klips_debug:rj_match: "
  56077. + "*** start searching up the tree, t=0p%p\n",
  56078. + t);
  56079. + do {
  56080. + register struct radij_mask *m;
  56081. +
  56082. + t = t->rj_p;
  56083. + KLIPS_PRINT(debug_radij,
  56084. + "klips_debug:rj_match: "
  56085. + "**** t=0p%p\n",
  56086. + t);
  56087. + if ((m = t->rj_mklist)) {
  56088. + /*
  56089. + * After doing measurements here, it may
  56090. + * turn out to be faster to open code
  56091. + * rj_search_m here instead of always
  56092. + * copying and masking.
  56093. + */
  56094. + /* off = min(t->rj_off, matched_off); */
  56095. + off = t->rj_off;
  56096. + if (matched_off < off)
  56097. + off = matched_off;
  56098. + mstart = maskedKey + off;
  56099. + do {
  56100. + cp2 = mstart;
  56101. + cp3 = m->rm_mask + off;
  56102. + KLIPS_PRINT(debug_radij,
  56103. + "klips_debug:rj_match: "
  56104. + "***** cp2=0p%p cp3=0p%p\n",
  56105. + cp2, cp3);
  56106. + for (cp = v + off; cp < cplim;)
  56107. + *cp2++ = *cp++ & *cp3++;
  56108. + x = rj_search(maskedKey, t);
  56109. + while (x && x->rj_mask != m->rm_mask)
  56110. + x = x->rj_dupedkey;
  56111. + if (x &&
  56112. + (Bcmp(mstart, x->rj_key + off,
  56113. + vlen - off) == 0))
  56114. + return x;
  56115. + } while ((m = m->rm_mklist));
  56116. + }
  56117. + } while (t != top);
  56118. + KLIPS_PRINT(debug_radij,
  56119. + "klips_debug:rj_match: "
  56120. + "***** not found.\n");
  56121. + return 0;
  56122. +};
  56123. +
  56124. +#ifdef RJ_DEBUG
  56125. +int rj_nodenum;
  56126. +struct radij_node *rj_clist;
  56127. +int rj_saveinfo;
  56128. +DEBUG_NO_STATIC void traverse(struct radij_node *);
  56129. +#ifdef RJ_DEBUG2
  56130. +int rj_debug = 1;
  56131. +#else
  56132. +int rj_debug = 0;
  56133. +#endif /* RJ_DEBUG2 */
  56134. +#endif /* RJ_DEBUG */
  56135. +
  56136. +struct radij_node *
  56137. +rj_newpair(v, b, nodes)
  56138. + void *v;
  56139. + int b;
  56140. + struct radij_node nodes[2];
  56141. +{
  56142. + register struct radij_node *tt = nodes, *t = tt + 1;
  56143. + t->rj_b = b; t->rj_bmask = 0x80 >> (b & 7);
  56144. + t->rj_l = tt; t->rj_off = b >> 3;
  56145. + tt->rj_b = -1; tt->rj_key = (caddr_t)v; tt->rj_p = t;
  56146. + tt->rj_flags = t->rj_flags = RJF_ACTIVE;
  56147. +#ifdef RJ_DEBUG
  56148. + tt->rj_info = rj_nodenum++; t->rj_info = rj_nodenum++;
  56149. + tt->rj_twin = t; tt->rj_ybro = rj_clist; rj_clist = tt;
  56150. +#endif /* RJ_DEBUG */
  56151. + return t;
  56152. +}
  56153. +
  56154. +struct radij_node *
  56155. +rj_insert(v_arg, head, dupentry, nodes)
  56156. + void *v_arg;
  56157. + struct radij_node_head *head;
  56158. + int *dupentry;
  56159. + struct radij_node nodes[2];
  56160. +{
  56161. + caddr_t v = v_arg;
  56162. + struct radij_node *top = head->rnh_treetop;
  56163. + int head_off = top->rj_off, vlen = (int)*((u_char *)v);
  56164. + register struct radij_node *t = rj_search(v_arg, top);
  56165. + register caddr_t cp = v + head_off;
  56166. + register int b;
  56167. + struct radij_node *tt;
  56168. + /*
  56169. + *find first bit at which v and t->rj_key differ
  56170. + */
  56171. + {
  56172. + register caddr_t cp2 = t->rj_key + head_off;
  56173. + register int cmp_res;
  56174. + caddr_t cplim = v + vlen;
  56175. +
  56176. + while (cp < cplim)
  56177. + if (*cp2++ != *cp++)
  56178. + goto on1;
  56179. + *dupentry = 1;
  56180. + return t;
  56181. +on1:
  56182. + *dupentry = 0;
  56183. + cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
  56184. + for (b = (cp - v) << 3; cmp_res; b--)
  56185. + cmp_res >>= 1;
  56186. + }
  56187. + {
  56188. + register struct radij_node *p, *x = top;
  56189. + cp = v;
  56190. + do {
  56191. + p = x;
  56192. + if (cp[x->rj_off] & x->rj_bmask)
  56193. + x = x->rj_r;
  56194. + else x = x->rj_l;
  56195. + } while (b > (unsigned) x->rj_b); /* x->rj_b < b && x->rj_b >= 0 */
  56196. +#ifdef RJ_DEBUG
  56197. + if (rj_debug)
  56198. + printk("klips_debug:rj_insert: Going In:\n"), traverse(p);
  56199. +#endif /* RJ_DEBUG */
  56200. + t = rj_newpair(v_arg, b, nodes); tt = t->rj_l;
  56201. + if ((cp[p->rj_off] & p->rj_bmask) == 0)
  56202. + p->rj_l = t;
  56203. + else
  56204. + p->rj_r = t;
  56205. + x->rj_p = t; t->rj_p = p; /* frees x, p as temp vars below */
  56206. + if ((cp[t->rj_off] & t->rj_bmask) == 0) {
  56207. + t->rj_r = x;
  56208. + } else {
  56209. + t->rj_r = tt; t->rj_l = x;
  56210. + }
  56211. +#ifdef RJ_DEBUG
  56212. + if (rj_debug)
  56213. + printk("klips_debug:rj_insert: Coming out:\n"), traverse(p);
  56214. +#endif /* RJ_DEBUG */
  56215. + }
  56216. + return (tt);
  56217. +}
  56218. +
  56219. +struct radij_node *
  56220. +rj_addmask(n_arg, search, skip)
  56221. + int search, skip;
  56222. + void *n_arg;
  56223. +{
  56224. + caddr_t netmask = (caddr_t)n_arg;
  56225. + register struct radij_node *x;
  56226. + register caddr_t cp, cplim;
  56227. + register int b, mlen, j;
  56228. + int maskduplicated;
  56229. +
  56230. + mlen = *(u_char *)netmask;
  56231. + if (search) {
  56232. + x = rj_search(netmask, rj_masktop);
  56233. + mlen = *(u_char *)netmask;
  56234. + if (Bcmp(netmask, x->rj_key, mlen) == 0)
  56235. + return (x);
  56236. + }
  56237. + R_Malloc(x, struct radij_node *, maj_keylen + 2 * sizeof (*x));
  56238. + if (x == 0)
  56239. + return (0);
  56240. + Bzero(x, maj_keylen + 2 * sizeof (*x));
  56241. + cp = (caddr_t)(x + 2);
  56242. + Bcopy(netmask, cp, mlen);
  56243. + netmask = cp;
  56244. + x = rj_insert(netmask, mask_rjhead, &maskduplicated, x);
  56245. + /*
  56246. + * Calculate index of mask.
  56247. + */
  56248. + cplim = netmask + mlen;
  56249. + for (cp = netmask + skip; cp < cplim; cp++)
  56250. + if (*(u_char *)cp != 0xff)
  56251. + break;
  56252. + b = (cp - netmask) << 3;
  56253. + if (cp != cplim) {
  56254. + if (*cp != 0) {
  56255. + gotOddMasks = 1;
  56256. + for (j = 0x80; j; b++, j >>= 1)
  56257. + if ((j & *cp) == 0)
  56258. + break;
  56259. + }
  56260. + }
  56261. + x->rj_b = -1 - b;
  56262. + return (x);
  56263. +}
  56264. +
  56265. +#if 0
  56266. +struct radij_node *
  56267. +#endif
  56268. +int
  56269. +rj_addroute(v_arg, n_arg, head, treenodes)
  56270. + void *v_arg, *n_arg;
  56271. + struct radij_node_head *head;
  56272. + struct radij_node treenodes[2];
  56273. +{
  56274. + caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
  56275. + register struct radij_node *t, *x=NULL, *tt;
  56276. + struct radij_node *saved_tt, *top = head->rnh_treetop;
  56277. + short b = 0, b_leaf;
  56278. + int mlen, keyduplicated;
  56279. + caddr_t cplim;
  56280. + struct radij_mask *m, **mp;
  56281. +
  56282. + /*
  56283. + * In dealing with non-contiguous masks, there may be
  56284. + * many different routes which have the same mask.
  56285. + * We will find it useful to have a unique pointer to
  56286. + * the mask to speed avoiding duplicate references at
  56287. + * nodes and possibly save time in calculating indices.
  56288. + */
  56289. + if (netmask) {
  56290. + x = rj_search(netmask, rj_masktop);
  56291. + mlen = *(u_char *)netmask;
  56292. + if (Bcmp(netmask, x->rj_key, mlen) != 0) {
  56293. + x = rj_addmask(netmask, 0, top->rj_off);
  56294. + if (x == 0)
  56295. + return -ENOMEM; /* (0) rgb */
  56296. + }
  56297. + netmask = x->rj_key;
  56298. + b = -1 - x->rj_b;
  56299. + }
  56300. + /*
  56301. + * Deal with duplicated keys: attach node to previous instance
  56302. + */
  56303. + saved_tt = tt = rj_insert(v, head, &keyduplicated, treenodes);
  56304. +#ifdef RJ_DEBUG
  56305. + printk("addkey: duplicated: %d\n", keyduplicated);
  56306. +#endif
  56307. + if (keyduplicated) {
  56308. + do {
  56309. + if (tt->rj_mask == netmask)
  56310. + return -EEXIST; /* -ENXIO; (0) rgb */
  56311. + t = tt;
  56312. + if (netmask == 0 ||
  56313. + (tt->rj_mask && rj_refines(netmask, tt->rj_mask)))
  56314. + break;
  56315. + } while ((tt = tt->rj_dupedkey));
  56316. + /*
  56317. + * If the mask is not duplicated, we wouldn't
  56318. + * find it among possible duplicate key entries
  56319. + * anyway, so the above test doesn't hurt.
  56320. + *
  56321. + * We sort the masks for a duplicated key the same way as
  56322. + * in a masklist -- most specific to least specific.
  56323. + * This may require the unfortunate nuisance of relocating
  56324. + * the head of the list.
  56325. + */
  56326. + if (tt && t == saved_tt) {
  56327. + struct radij_node *xx = x;
  56328. + /* link in at head of list */
  56329. + (tt = treenodes)->rj_dupedkey = t;
  56330. + tt->rj_flags = t->rj_flags;
  56331. + tt->rj_p = x = t->rj_p;
  56332. + if (x->rj_l == t) x->rj_l = tt; else x->rj_r = tt;
  56333. + saved_tt = tt; x = xx;
  56334. + } else {
  56335. + (tt = treenodes)->rj_dupedkey = t->rj_dupedkey;
  56336. + t->rj_dupedkey = tt;
  56337. + }
  56338. +#ifdef RJ_DEBUG
  56339. + t=tt+1; tt->rj_info = rj_nodenum++; t->rj_info = rj_nodenum++;
  56340. + tt->rj_twin = t; tt->rj_ybro = rj_clist; rj_clist = tt;
  56341. +#endif /* RJ_DEBUG */
  56342. + t = saved_tt;
  56343. + tt->rj_key = (caddr_t) v;
  56344. + tt->rj_b = -1;
  56345. + tt->rj_flags = t->rj_flags & ~RJF_ROOT;
  56346. + }
  56347. + /*
  56348. + * Put mask in tree.
  56349. + */
  56350. + if (netmask) {
  56351. + tt->rj_mask = netmask;
  56352. + tt->rj_b = x->rj_b;
  56353. + }
  56354. + t = saved_tt->rj_p;
  56355. + b_leaf = -1 - t->rj_b;
  56356. + if (t->rj_r == saved_tt) x = t->rj_l; else x = t->rj_r;
  56357. + /* Promote general routes from below */
  56358. + if (x->rj_b < 0) {
  56359. + if (x->rj_mask && (x->rj_b >= b_leaf) && x->rj_mklist == 0) {
  56360. + MKGet(m);
  56361. + if (m) {
  56362. + Bzero(m, sizeof *m);
  56363. + m->rm_b = x->rj_b;
  56364. + m->rm_mask = x->rj_mask;
  56365. + x->rj_mklist = t->rj_mklist = m;
  56366. + }
  56367. + }
  56368. + } else if (x->rj_mklist) {
  56369. + /*
  56370. + * Skip over masks whose index is > that of new node
  56371. + */
  56372. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist)
  56373. + if (m->rm_b >= b_leaf)
  56374. + break;
  56375. + t->rj_mklist = m; *mp = 0;
  56376. + }
  56377. + /* Add new route to highest possible ancestor's list */
  56378. + if ((netmask == 0) || (b > t->rj_b )) {
  56379. +#ifdef RJ_DEBUG
  56380. + printk("klips:radij.c: netmask = %p or b(%d)>t->rjb(%d)\n", netmask, b, t->rj_b);
  56381. +#endif
  56382. + return 0; /* tt rgb */ /* can't lift at all */
  56383. + }
  56384. + b_leaf = tt->rj_b;
  56385. + do {
  56386. + x = t;
  56387. + t = t->rj_p;
  56388. + } while (b <= t->rj_b && x != top);
  56389. + /*
  56390. + * Search through routes associated with node to
  56391. + * insert new route according to index.
  56392. + * For nodes of equal index, place more specific
  56393. + * masks first.
  56394. + */
  56395. + cplim = netmask + mlen;
  56396. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist) {
  56397. + if (m->rm_b < b_leaf)
  56398. + continue;
  56399. + if (m->rm_b > b_leaf)
  56400. + break;
  56401. + if (m->rm_mask == netmask) {
  56402. + m->rm_refs++;
  56403. + tt->rj_mklist = m;
  56404. +#ifdef RJ_DEBUG
  56405. + printk("klips:radij.c: m->rm_mask %p == netmask\n", netmask);
  56406. +#endif
  56407. + return 0; /* tt rgb */
  56408. + }
  56409. + if (rj_refines(netmask, m->rm_mask))
  56410. + break;
  56411. + }
  56412. + MKGet(m);
  56413. + if (m == 0) {
  56414. + printk("klips_debug:rj_addroute: "
  56415. + "Mask for route not entered\n");
  56416. + return 0; /* (tt) rgb */
  56417. + }
  56418. + Bzero(m, sizeof *m);
  56419. + m->rm_b = b_leaf;
  56420. + m->rm_mask = netmask;
  56421. + m->rm_mklist = *mp;
  56422. + *mp = m;
  56423. + tt->rj_mklist = m;
  56424. +#ifdef RJ_DEBUG
  56425. + printk("klips:radij.c: addroute done\n");
  56426. +#endif
  56427. + return 0; /* tt rgb */
  56428. +}
  56429. +
  56430. +int
  56431. +rj_delete(v_arg, netmask_arg, head, node)
  56432. + void *v_arg, *netmask_arg;
  56433. + struct radij_node_head *head;
  56434. + struct radij_node **node;
  56435. +{
  56436. + register struct radij_node *t, *p, *x, *tt;
  56437. + struct radij_mask *m, *saved_m, **mp;
  56438. + struct radij_node *dupedkey, *saved_tt, *top;
  56439. + caddr_t v, netmask;
  56440. + int b, head_off, vlen;
  56441. +
  56442. + v = v_arg;
  56443. + netmask = netmask_arg;
  56444. + x = head->rnh_treetop;
  56445. + tt = rj_search(v, x);
  56446. + head_off = x->rj_off;
  56447. + vlen = *(u_char *)v;
  56448. + saved_tt = tt;
  56449. + top = x;
  56450. + if (tt == 0 ||
  56451. + Bcmp(v + head_off, tt->rj_key + head_off, vlen - head_off))
  56452. + return -EFAULT; /* (0) rgb */
  56453. + /*
  56454. + * Delete our route from mask lists.
  56455. + */
  56456. + if ((dupedkey = tt->rj_dupedkey)) {
  56457. + if (netmask)
  56458. + netmask = rj_search(netmask, rj_masktop)->rj_key;
  56459. + while (tt->rj_mask != netmask)
  56460. + if ((tt = tt->rj_dupedkey) == 0)
  56461. + return -ENOENT; /* -ENXIO; (0) rgb */
  56462. + }
  56463. + if (tt->rj_mask == 0 || (saved_m = m = tt->rj_mklist) == 0)
  56464. + goto on1;
  56465. + if (m->rm_mask != tt->rj_mask) {
  56466. + printk("klips_debug:rj_delete: "
  56467. + "inconsistent annotation\n");
  56468. + goto on1;
  56469. + }
  56470. + if (--m->rm_refs >= 0)
  56471. + goto on1;
  56472. + b = -1 - tt->rj_b;
  56473. + t = saved_tt->rj_p;
  56474. + if (b > t->rj_b)
  56475. + goto on1; /* Wasn't lifted at all */
  56476. + do {
  56477. + x = t;
  56478. + t = t->rj_p;
  56479. + } while (b <= t->rj_b && x != top);
  56480. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist)
  56481. + if (m == saved_m) {
  56482. + *mp = m->rm_mklist;
  56483. + MKFree(m);
  56484. + break;
  56485. + }
  56486. + if (m == 0)
  56487. + printk("klips_debug:rj_delete: "
  56488. + "couldn't find our annotation\n");
  56489. +on1:
  56490. + /*
  56491. + * Eliminate us from tree
  56492. + */
  56493. + if (tt->rj_flags & RJF_ROOT)
  56494. + return -EFAULT; /* (0) rgb */
  56495. +#ifdef RJ_DEBUG
  56496. + /* Get us out of the creation list */
  56497. + for (t = rj_clist; t && t->rj_ybro != tt; t = t->rj_ybro) {}
  56498. + if (t) t->rj_ybro = tt->rj_ybro;
  56499. +#endif /* RJ_DEBUG */
  56500. + t = tt->rj_p;
  56501. + if (dupedkey) {
  56502. + if (tt == saved_tt) {
  56503. + x = dupedkey; x->rj_p = t;
  56504. + if (t->rj_l == tt) t->rj_l = x; else t->rj_r = x;
  56505. + } else {
  56506. + for (x = p = saved_tt; p && p->rj_dupedkey != tt;)
  56507. + p = p->rj_dupedkey;
  56508. + if (p) p->rj_dupedkey = tt->rj_dupedkey;
  56509. + else printk("klips_debug:rj_delete: "
  56510. + "couldn't find node that we started with\n");
  56511. + }
  56512. + t = tt + 1;
  56513. + if (t->rj_flags & RJF_ACTIVE) {
  56514. +#ifndef RJ_DEBUG
  56515. + *++x = *t; p = t->rj_p;
  56516. +#else
  56517. + b = t->rj_info; *++x = *t; t->rj_info = b; p = t->rj_p;
  56518. +#endif /* RJ_DEBUG */
  56519. + if (p->rj_l == t) p->rj_l = x; else p->rj_r = x;
  56520. + x->rj_l->rj_p = x; x->rj_r->rj_p = x;
  56521. + }
  56522. + goto out;
  56523. + }
  56524. + if (t->rj_l == tt) x = t->rj_r; else x = t->rj_l;
  56525. + p = t->rj_p;
  56526. + if (p->rj_r == t) p->rj_r = x; else p->rj_l = x;
  56527. + x->rj_p = p;
  56528. + /*
  56529. + * Demote routes attached to us.
  56530. + */
  56531. + if (t->rj_mklist) {
  56532. + if (x->rj_b >= 0) {
  56533. + for (mp = &x->rj_mklist; (m = *mp);)
  56534. + mp = &m->rm_mklist;
  56535. + *mp = t->rj_mklist;
  56536. + } else {
  56537. + for (m = t->rj_mklist; m;) {
  56538. + struct radij_mask *mm = m->rm_mklist;
  56539. + if (m == x->rj_mklist && (--(m->rm_refs) < 0)) {
  56540. + x->rj_mklist = 0;
  56541. + MKFree(m);
  56542. + } else
  56543. + printk("klips_debug:rj_delete: "
  56544. + "Orphaned Mask 0p%p at 0p%p\n", m, x);
  56545. + m = mm;
  56546. + }
  56547. + }
  56548. + }
  56549. + /*
  56550. + * We may be holding an active internal node in the tree.
  56551. + */
  56552. + x = tt + 1;
  56553. + if (t != x) {
  56554. +#ifndef RJ_DEBUG
  56555. + *t = *x;
  56556. +#else
  56557. + b = t->rj_info; *t = *x; t->rj_info = b;
  56558. +#endif /* RJ_DEBUG */
  56559. + t->rj_l->rj_p = t; t->rj_r->rj_p = t;
  56560. + p = x->rj_p;
  56561. + if (p->rj_l == x) p->rj_l = t; else p->rj_r = t;
  56562. + }
  56563. +out:
  56564. + tt->rj_flags &= ~RJF_ACTIVE;
  56565. + tt[1].rj_flags &= ~RJF_ACTIVE;
  56566. + *node = tt;
  56567. + return 0; /* (tt) rgb */
  56568. +}
  56569. +
  56570. +int
  56571. +rj_walktree(h, f, w)
  56572. + struct radij_node_head *h;
  56573. + register int (*f)(struct radij_node *,void *);
  56574. + void *w;
  56575. +{
  56576. + int error;
  56577. + struct radij_node *base, *next;
  56578. + register struct radij_node *rn;
  56579. +
  56580. + if(!h || !f /* || !w */) {
  56581. + return -ENODATA;
  56582. + }
  56583. +
  56584. + rn = h->rnh_treetop;
  56585. + /*
  56586. + * This gets complicated because we may delete the node
  56587. + * while applying the function f to it, so we need to calculate
  56588. + * the successor node in advance.
  56589. + */
  56590. + /* First time through node, go left */
  56591. + while (rn->rj_b >= 0)
  56592. + rn = rn->rj_l;
  56593. + for (;;) {
  56594. +#ifdef CONFIG_KLIPS_DEBUG
  56595. + if(debug_radij) {
  56596. + printk("klips_debug:rj_walktree: "
  56597. + "for: rn=0p%p rj_b=%d rj_flags=%x",
  56598. + rn,
  56599. + rn->rj_b,
  56600. + rn->rj_flags);
  56601. + rn->rj_b >= 0 ?
  56602. + printk(" node off=%x\n",
  56603. + rn->rj_off) :
  56604. + printk(" leaf key = %08x->%08x\n",
  56605. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56606. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56607. + ;
  56608. + }
  56609. +#endif /* CONFIG_KLIPS_DEBUG */
  56610. + base = rn;
  56611. + /* If at right child go back up, otherwise, go right */
  56612. + while (rn->rj_p->rj_r == rn && (rn->rj_flags & RJF_ROOT) == 0)
  56613. + rn = rn->rj_p;
  56614. + /* Find the next *leaf* since next node might vanish, too */
  56615. + for (rn = rn->rj_p->rj_r; rn->rj_b >= 0;)
  56616. + rn = rn->rj_l;
  56617. + next = rn;
  56618. +#ifdef CONFIG_KLIPS_DEBUG
  56619. + if(debug_radij) {
  56620. + printk("klips_debug:rj_walktree: "
  56621. + "processing leaves, rn=0p%p rj_b=%d rj_flags=%x",
  56622. + rn,
  56623. + rn->rj_b,
  56624. + rn->rj_flags);
  56625. + rn->rj_b >= 0 ?
  56626. + printk(" node off=%x\n",
  56627. + rn->rj_off) :
  56628. + printk(" leaf key = %08x->%08x\n",
  56629. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56630. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56631. + ;
  56632. + }
  56633. +#endif /* CONFIG_KLIPS_DEBUG */
  56634. + /* Process leaves */
  56635. + while ((rn = base)) {
  56636. + base = rn->rj_dupedkey;
  56637. +#ifdef CONFIG_KLIPS_DEBUG
  56638. + if(debug_radij) {
  56639. + printk("klips_debug:rj_walktree: "
  56640. + "while: base=0p%p rn=0p%p rj_b=%d rj_flags=%x",
  56641. + base,
  56642. + rn,
  56643. + rn->rj_b,
  56644. + rn->rj_flags);
  56645. + rn->rj_b >= 0 ?
  56646. + printk(" node off=%x\n",
  56647. + rn->rj_off) :
  56648. + printk(" leaf key = %08x->%08x\n",
  56649. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56650. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56651. + ;
  56652. + }
  56653. +#endif /* CONFIG_KLIPS_DEBUG */
  56654. + if (!(rn->rj_flags & RJF_ROOT) && (error = (*f)(rn, w)))
  56655. + return (-error);
  56656. + }
  56657. + rn = next;
  56658. + if (rn->rj_flags & RJF_ROOT)
  56659. + return (0);
  56660. + }
  56661. + /* NOTREACHED */
  56662. +}
  56663. +
  56664. +int
  56665. +rj_inithead(head, off)
  56666. + void **head;
  56667. + int off;
  56668. +{
  56669. + register struct radij_node_head *rnh;
  56670. + register struct radij_node *t, *tt, *ttt;
  56671. + if (*head)
  56672. + return (1);
  56673. + R_Malloc(rnh, struct radij_node_head *, sizeof (*rnh));
  56674. + if (rnh == NULL)
  56675. + return (0);
  56676. + Bzero(rnh, sizeof (*rnh));
  56677. + *head = rnh;
  56678. + t = rj_newpair(rj_zeroes, off, rnh->rnh_nodes);
  56679. + ttt = rnh->rnh_nodes + 2;
  56680. + t->rj_r = ttt;
  56681. + t->rj_p = t;
  56682. + tt = t->rj_l;
  56683. + tt->rj_flags = t->rj_flags = RJF_ROOT | RJF_ACTIVE;
  56684. + tt->rj_b = -1 - off;
  56685. + *ttt = *tt;
  56686. + ttt->rj_key = rj_ones;
  56687. + rnh->rnh_addaddr = rj_addroute;
  56688. + rnh->rnh_deladdr = rj_delete;
  56689. + rnh->rnh_matchaddr = rj_match;
  56690. + rnh->rnh_walktree = rj_walktree;
  56691. + rnh->rnh_treetop = t;
  56692. + return (1);
  56693. +}
  56694. +
  56695. +void
  56696. +rj_init()
  56697. +{
  56698. + char *cp, *cplim;
  56699. +
  56700. + if (maj_keylen == 0) {
  56701. + printk("klips_debug:rj_init: "
  56702. + "radij functions require maj_keylen be set\n");
  56703. + return;
  56704. + }
  56705. + R_Malloc(rj_zeroes, char *, 3 * maj_keylen);
  56706. + if (rj_zeroes == NULL)
  56707. + panic("rj_init");
  56708. + Bzero(rj_zeroes, 3 * maj_keylen);
  56709. + rj_ones = cp = rj_zeroes + maj_keylen;
  56710. + maskedKey = cplim = rj_ones + maj_keylen;
  56711. + while (cp < cplim)
  56712. + *cp++ = -1;
  56713. + if (rj_inithead((void **)&mask_rjhead, 0) == 0)
  56714. + panic("rj_init 2");
  56715. +}
  56716. +
  56717. +void
  56718. +rj_preorder(struct radij_node *rn, int l)
  56719. +{
  56720. + int i;
  56721. +
  56722. + if (rn == NULL){
  56723. + printk("klips_debug:rj_preorder: "
  56724. + "NULL pointer\n");
  56725. + return;
  56726. + }
  56727. +
  56728. + if (rn->rj_b >= 0){
  56729. + rj_preorder(rn->rj_l, l+1);
  56730. + rj_preorder(rn->rj_r, l+1);
  56731. + printk("klips_debug:");
  56732. + for (i=0; i<l; i++)
  56733. + printk("*");
  56734. + printk(" off = %d\n",
  56735. + rn->rj_off);
  56736. + } else {
  56737. + printk("klips_debug:");
  56738. + for (i=0; i<l; i++)
  56739. + printk("@");
  56740. + printk(" flags = %x",
  56741. + (u_int)rn->rj_flags);
  56742. + if (rn->rj_flags & RJF_ACTIVE) {
  56743. + printk(" @key=0p%p",
  56744. + rn->rj_key);
  56745. + printk(" key = %08x->%08x",
  56746. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56747. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr));
  56748. + printk(" @mask=0p%p",
  56749. + rn->rj_mask);
  56750. + if (rn->rj_mask)
  56751. + printk(" mask = %08x->%08x",
  56752. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_mask)->sen_ip_src.s_addr),
  56753. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_mask)->sen_ip_dst.s_addr));
  56754. + if (rn->rj_dupedkey)
  56755. + printk(" dupedkey = 0p%p",
  56756. + rn->rj_dupedkey);
  56757. + }
  56758. + printk("\n");
  56759. + }
  56760. +}
  56761. +
  56762. +#ifdef RJ_DEBUG
  56763. +DEBUG_NO_STATIC void traverse(struct radij_node *p)
  56764. +{
  56765. + rj_preorder(p, 0);
  56766. +}
  56767. +#endif /* RJ_DEBUG */
  56768. +
  56769. +void
  56770. +rj_dumptrees(void)
  56771. +{
  56772. + rj_preorder(rnh->rnh_treetop, 0);
  56773. +}
  56774. +
  56775. +void
  56776. +rj_free_mkfreelist(void)
  56777. +{
  56778. + struct radij_mask *mknp, *mknp2;
  56779. +
  56780. + mknp = rj_mkfreelist;
  56781. + while(mknp)
  56782. + {
  56783. + mknp2 = mknp;
  56784. + mknp = mknp->rm_mklist;
  56785. + kfree(mknp2);
  56786. + }
  56787. +}
  56788. +
  56789. +int
  56790. +radijcleartree(void)
  56791. +{
  56792. + return rj_walktree(rnh, ipsec_rj_walker_delete, NULL);
  56793. +}
  56794. +
  56795. +int
  56796. +radijcleanup(void)
  56797. +{
  56798. + int error = 0;
  56799. +
  56800. + error = radijcleartree();
  56801. +
  56802. + rj_free_mkfreelist();
  56803. +
  56804. +/* rj_walktree(mask_rjhead, ipsec_rj_walker_delete, NULL); */
  56805. + if(mask_rjhead) {
  56806. + kfree(mask_rjhead);
  56807. + }
  56808. +
  56809. + if(rj_zeroes) {
  56810. + kfree(rj_zeroes);
  56811. + }
  56812. +
  56813. + if(rnh) {
  56814. + kfree(rnh);
  56815. + }
  56816. +
  56817. + return error;
  56818. +}
  56819. +
  56820. +/*
  56821. + * $Log: radij.c,v $
  56822. + * Revision 1.48.2.1 2006/10/06 21:39:27 paul
  56823. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  56824. + * set. This is defined through autoconf.h which is included through the
  56825. + * linux kernel build macros.
  56826. + *
  56827. + * Revision 1.48 2005/04/29 05:10:22 mcr
  56828. + * removed from extraenous includes to make unit testing easier.
  56829. + *
  56830. + * Revision 1.47 2004/07/10 19:11:18 mcr
  56831. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  56832. + *
  56833. + * Revision 1.46 2004/04/06 02:49:26 mcr
  56834. + * pullup of algo code from alg-branch.
  56835. + *
  56836. + * Revision 1.45 2003/10/31 02:27:55 mcr
  56837. + * pulled up port-selector patches and sa_id elimination.
  56838. + *
  56839. + * Revision 1.44.30.1 2003/10/29 01:30:41 mcr
  56840. + * elimited "struct sa_id".
  56841. + *
  56842. + * Revision 1.44 2002/07/24 18:44:54 rgb
  56843. + * Type fiddling to tame ia64 compiler.
  56844. + *
  56845. + * Revision 1.43 2002/05/23 07:14:11 rgb
  56846. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  56847. + *
  56848. + * Revision 1.42 2002/04/24 07:55:32 mcr
  56849. + * #include patches and Makefiles for post-reorg compilation.
  56850. + *
  56851. + * Revision 1.41 2002/04/24 07:36:35 mcr
  56852. + * Moved from ./klips/net/ipsec/radij.c,v
  56853. + *
  56854. + * Revision 1.40 2002/01/29 17:17:58 mcr
  56855. + * moved include of ipsec_param.h to after include of linux/kernel.h
  56856. + * otherwise, it seems that some option that is set in ipsec_param.h
  56857. + * screws up something subtle in the include path to kernel.h, and
  56858. + * it complains on the snprintf() prototype.
  56859. + *
  56860. + * Revision 1.39 2002/01/29 04:00:55 mcr
  56861. + * more excise of kversions.h header.
  56862. + *
  56863. + * Revision 1.38 2002/01/29 02:13:19 mcr
  56864. + * introduction of ipsec_kversion.h means that include of
  56865. + * ipsec_param.h must preceed any decisions about what files to
  56866. + * include to deal with differences in kernel source.
  56867. + *
  56868. + * Revision 1.37 2001/10/18 04:45:23 rgb
  56869. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  56870. + * lib/freeswan.h version macros moved to lib/kversions.h.
  56871. + * Other compiler directive cleanups.
  56872. + *
  56873. + * Revision 1.36 2001/08/22 13:43:51 henry
  56874. + * eliminate the single use of min() to avoid problems with Linus changing it
  56875. + *
  56876. + * Revision 1.35 2001/06/15 04:57:29 rgb
  56877. + * Clarified error return codes.
  56878. + * Changed mask add already exists to EEXIST.
  56879. + * Changed mask delete did not exist to ENOENT.
  56880. + *
  56881. + * Revision 1.34 2001/05/03 19:44:26 rgb
  56882. + * Fix sign of error return codes for rj_addroute().
  56883. + *
  56884. + * Revision 1.33 2001/02/27 22:24:56 rgb
  56885. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  56886. + * Check for satoa() return codes.
  56887. + *
  56888. + * Revision 1.32 2001/02/27 06:23:15 rgb
  56889. + * Debug line splitting.
  56890. + *
  56891. + * Revision 1.31 2000/11/06 04:35:21 rgb
  56892. + * Clear table *before* releasing other items in radijcleanup.
  56893. + *
  56894. + * Revision 1.30 2000/09/20 04:07:40 rgb
  56895. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  56896. + * oopsen.
  56897. + *
  56898. + * Revision 1.29 2000/09/12 03:25:02 rgb
  56899. + * Moved radij_c_version printing to ipsec_version_get_info().
  56900. + *
  56901. + * Revision 1.28 2000/09/08 19:12:56 rgb
  56902. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  56903. + *
  56904. + * Revision 1.27 2000/07/28 14:58:32 rgb
  56905. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  56906. + *
  56907. + * Revision 1.26 2000/05/10 23:11:37 rgb
  56908. + * Comment out most of the startup version information.
  56909. + *
  56910. + * Revision 1.25 2000/01/21 06:21:47 rgb
  56911. + * Change return codes to negative on error.
  56912. + *
  56913. + * Revision 1.24 1999/11/18 04:09:20 rgb
  56914. + * Replaced all kernel version macros to shorter, readable form.
  56915. + *
  56916. + * Revision 1.23 1999/11/17 15:53:41 rgb
  56917. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  56918. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  56919. + * klips/net/ipsec/Makefile.
  56920. + *
  56921. + * Revision 1.22 1999/10/15 22:17:28 rgb
  56922. + * Modify radijcleanup() to call radijcleartree().
  56923. + *
  56924. + * Revision 1.21 1999/10/08 18:37:34 rgb
  56925. + * Fix end-of-line spacing to sate whining PHMs.
  56926. + *
  56927. + * Revision 1.20 1999/10/01 15:44:54 rgb
  56928. + * Move spinlock header include to 2.1> scope.
  56929. + *
  56930. + * Revision 1.19 1999/10/01 08:35:52 rgb
  56931. + * Add spinlock include to shut up compiler for 2.0.38.
  56932. + *
  56933. + * Revision 1.18 1999/09/23 18:02:52 rgb
  56934. + * De-alarm the search failure message so it doesn't sound so grave.
  56935. + *
  56936. + * Revision 1.17 1999/05/25 21:26:01 rgb
  56937. + * Fix rj_walktree() sanity checking bug.
  56938. + *
  56939. + * Revision 1.16 1999/05/09 03:25:38 rgb
  56940. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  56941. + *
  56942. + * Revision 1.15 1999/05/05 22:02:33 rgb
  56943. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  56944. + *
  56945. + * Revision 1.14 1999/04/29 15:24:15 rgb
  56946. + * Add sanity checking for null pointer arguments.
  56947. + * Standardise an error return method.
  56948. + *
  56949. + * Revision 1.13 1999/04/11 00:29:02 henry
  56950. + * GPL boilerplate
  56951. + *
  56952. + * Revision 1.12 1999/04/06 04:54:28 rgb
  56953. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  56954. + * patch shell fixes.
  56955. + *
  56956. + * Revision 1.11 1999/02/17 16:52:53 rgb
  56957. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  56958. + * Clean out unused cruft.
  56959. + *
  56960. + * Revision 1.10 1999/01/22 06:30:05 rgb
  56961. + * Cruft clean-out.
  56962. + * 64-bit clean-up.
  56963. + *
  56964. + * Revision 1.9 1998/12/01 13:22:04 rgb
  56965. + * Added support for debug printing of version info.
  56966. + *
  56967. + * Revision 1.8 1998/11/30 13:22:55 rgb
  56968. + * Rationalised all the klips kernel file headers. They are much shorter
  56969. + * now and won't conflict under RH5.2.
  56970. + *
  56971. + * Revision 1.7 1998/10/25 02:43:26 rgb
  56972. + * Change return type on rj_addroute and rj_delete and add and argument
  56973. + * to the latter to be able to transmit more infomation about errors.
  56974. + *
  56975. + * Revision 1.6 1998/10/19 14:30:06 rgb
  56976. + * Added inclusion of freeswan.h.
  56977. + *
  56978. + * Revision 1.5 1998/10/09 04:33:27 rgb
  56979. + * Added 'klips_debug' prefix to all klips printk debug statements.
  56980. + * Fixed output formatting slightly.
  56981. + *
  56982. + * Revision 1.4 1998/07/28 00:06:59 rgb
  56983. + * Add debug detail to tree traversing.
  56984. + *
  56985. + * Revision 1.3 1998/07/14 18:07:58 rgb
  56986. + * Add a routine to clear the eroute tree.
  56987. + *
  56988. + * Revision 1.2 1998/06/25 20:03:22 rgb
  56989. + * Cleanup #endif comments. Debug output for rj_init.
  56990. + *
  56991. + * Revision 1.1 1998/06/18 21:30:22 henry
  56992. + * move sources from klips/src to klips/net/ipsec to keep stupid kernel
  56993. + * build scripts happier about symlinks
  56994. + *
  56995. + * Revision 1.8 1998/05/25 20:34:15 rgb
  56996. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  56997. + *
  56998. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  56999. + * add ipsec_rj_walker_delete.
  57000. + *
  57001. + * Recover memory for eroute table on unload of module.
  57002. + *
  57003. + * Revision 1.7 1998/05/21 12:58:58 rgb
  57004. + * Moved 'extern' definitions to ipsec_radij.h to support /proc 3k limit fix.
  57005. + *
  57006. + * Revision 1.6 1998/04/23 20:57:29 rgb
  57007. + * Cleaned up compiler warnings for unused debugging functions.
  57008. + *
  57009. + * Revision 1.5 1998/04/22 16:51:38 rgb
  57010. + * Tidy up radij debug code from recent rash of modifications to debug code.
  57011. + *
  57012. + * Revision 1.4 1998/04/21 21:28:56 rgb
  57013. + * Rearrange debug switches to change on the fly debug output from user
  57014. + * space. Only kernel changes checked in at this time. radij.c was also
  57015. + * changed to temporarily remove buggy debugging code in rj_delete causing
  57016. + * an OOPS and hence, netlink device open errors.
  57017. + *
  57018. + * Revision 1.3 1998/04/14 17:30:37 rgb
  57019. + * Fix up compiling errors for radij tree memory reclamation.
  57020. + *
  57021. + * Revision 1.2 1998/04/12 22:03:25 rgb
  57022. + * Updated ESP-3DES-HMAC-MD5-96,
  57023. + * ESP-DES-HMAC-MD5-96,
  57024. + * AH-HMAC-MD5-96,
  57025. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  57026. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  57027. + *
  57028. + * Fixed eroute references in /proc/net/ipsec*.
  57029. + *
  57030. + * Started to patch module unloading memory leaks in ipsec_netlink and
  57031. + * radij tree unloading.
  57032. + *
  57033. + * Revision 1.1 1998/04/09 03:06:15 henry
  57034. + * sources moved up from linux/net/ipsec
  57035. + *
  57036. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  57037. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  57038. + *
  57039. + * Revision 0.4 1997/01/15 01:28:15 ji
  57040. + * No changes.
  57041. + *
  57042. + * Revision 0.3 1996/11/20 14:39:04 ji
  57043. + * Minor cleanups.
  57044. + * Rationalized debugging code.
  57045. + *
  57046. + * Revision 0.2 1996/11/02 00:18:33 ji
  57047. + * First limited release.
  57048. + *
  57049. + *
  57050. + */
  57051. --- /dev/null Tue Mar 11 13:02:56 2003
  57052. +++ linux/net/ipsec/rangetoa.c Mon Feb 9 13:51:03 2004
  57053. @@ -0,0 +1,60 @@
  57054. +/*
  57055. + * convert binary form of address range to ASCII
  57056. + * Copyright (C) 1998, 1999 Henry Spencer.
  57057. + *
  57058. + * This library is free software; you can redistribute it and/or modify it
  57059. + * under the terms of the GNU Library General Public License as published by
  57060. + * the Free Software Foundation; either version 2 of the License, or (at your
  57061. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57062. + *
  57063. + * This library is distributed in the hope that it will be useful, but
  57064. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57065. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57066. + * License for more details.
  57067. + *
  57068. + * RCSID $Id: rangetoa.c,v 1.9 2004/07/10 07:48:37 mcr Exp $
  57069. + */
  57070. +#include "openswan.h"
  57071. +
  57072. +/*
  57073. + - rangetoa - convert address range to ASCII
  57074. + */
  57075. +size_t /* space needed for full conversion */
  57076. +rangetoa(addrs, format, dst, dstlen)
  57077. +struct in_addr addrs[2];
  57078. +int format; /* character */
  57079. +char *dst; /* need not be valid if dstlen is 0 */
  57080. +size_t dstlen;
  57081. +{
  57082. + size_t len;
  57083. + size_t rest;
  57084. + int n;
  57085. + char *p;
  57086. +
  57087. + switch (format) {
  57088. + case 0:
  57089. + break;
  57090. + default:
  57091. + return 0;
  57092. + break;
  57093. + }
  57094. +
  57095. + len = addrtoa(addrs[0], 0, dst, dstlen);
  57096. + if (len < dstlen)
  57097. + for (p = dst + len - 1, n = 3; len < dstlen && n > 0;
  57098. + p++, len++, n--)
  57099. + *p = '.';
  57100. + else
  57101. + p = NULL;
  57102. + if (len < dstlen)
  57103. + rest = dstlen - len;
  57104. + else {
  57105. + if (dstlen > 0)
  57106. + *(dst + dstlen - 1) = '\0';
  57107. + rest = 0;
  57108. + }
  57109. +
  57110. + len += addrtoa(addrs[1], 0, p, rest);
  57111. +
  57112. + return len;
  57113. +}
  57114. --- /dev/null Tue Mar 11 13:02:56 2003
  57115. +++ linux/net/ipsec/satot.c Mon Feb 9 13:51:03 2004
  57116. @@ -0,0 +1,133 @@
  57117. +/*
  57118. + * convert from binary form of SA ID to text
  57119. + * Copyright (C) 2000, 2001 Henry Spencer.
  57120. + *
  57121. + * This library is free software; you can redistribute it and/or modify it
  57122. + * under the terms of the GNU Library General Public License as published by
  57123. + * the Free Software Foundation; either version 2 of the License, or (at your
  57124. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57125. + *
  57126. + * This library is distributed in the hope that it will be useful, but
  57127. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57128. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57129. + * License for more details.
  57130. + *
  57131. + * RCSID $Id: satot.c,v 1.13 2004/07/10 07:48:37 mcr Exp $
  57132. + */
  57133. +#include "openswan.h"
  57134. +
  57135. +static struct typename {
  57136. + char type;
  57137. + char *name;
  57138. +} typenames[] = {
  57139. + { SA_AH, "ah" },
  57140. + { SA_ESP, "esp" },
  57141. + { SA_IPIP, "tun" },
  57142. + { SA_COMP, "comp" },
  57143. + { SA_INT, "int" },
  57144. + { 0, NULL }
  57145. +};
  57146. +
  57147. +/*
  57148. + - satot - convert SA to text "ah507@1.2.3.4"
  57149. + */
  57150. +size_t /* space needed for full conversion */
  57151. +satot(sa, format, dst, dstlen)
  57152. +const ip_said *sa;
  57153. +int format; /* character */
  57154. +char *dst; /* need not be valid if dstlen is 0 */
  57155. +size_t dstlen;
  57156. +{
  57157. + size_t len = 0; /* 0 means "not recognized yet" */
  57158. + int base;
  57159. + int showversion; /* use delimiter to show IP version? */
  57160. + struct typename *tn;
  57161. + char *p;
  57162. + char *pre;
  57163. + char buf[10+1+ULTOT_BUF+ADDRTOT_BUF];
  57164. + char unk[10];
  57165. +
  57166. + switch (format) {
  57167. + case 0:
  57168. + base = 16;
  57169. + showversion = 1;
  57170. + break;
  57171. + case 'f':
  57172. + base = 17;
  57173. + showversion = 1;
  57174. + break;
  57175. + case 'x':
  57176. + base = 'x';
  57177. + showversion = 0;
  57178. + break;
  57179. + case 'd':
  57180. + base = 10;
  57181. + showversion = 0;
  57182. + break;
  57183. + default:
  57184. + return 0;
  57185. + break;
  57186. + }
  57187. +
  57188. + memset(buf, 0, sizeof(buf));
  57189. +
  57190. + pre = NULL;
  57191. + for (tn = typenames; tn->name != NULL; tn++)
  57192. + if (sa->proto == tn->type) {
  57193. + pre = tn->name;
  57194. + break; /* NOTE BREAK OUT */
  57195. + }
  57196. + if (pre == NULL) { /* unknown protocol */
  57197. + strcpy(unk, "unk");
  57198. + (void) ultot((unsigned char)sa->proto, 10, unk+strlen(unk),
  57199. + sizeof(unk)-strlen(unk));
  57200. + pre = unk;
  57201. + }
  57202. +
  57203. + if (strcmp(pre, PASSTHROUGHTYPE) == 0 &&
  57204. + sa->spi == PASSTHROUGHSPI &&
  57205. + isunspecaddr(&sa->dst)) {
  57206. + strcpy(buf, (addrtypeof(&sa->dst) == AF_INET) ?
  57207. + PASSTHROUGH4NAME :
  57208. + PASSTHROUGH6NAME);
  57209. + len = strlen(buf);
  57210. + }
  57211. +
  57212. + if (sa->proto == SA_INT) {
  57213. + switch (ntohl(sa->spi)) {
  57214. + case SPI_PASS: p = "%pass"; break;
  57215. + case SPI_DROP: p = "%drop"; break;
  57216. + case SPI_REJECT: p = "%reject"; break;
  57217. + case SPI_HOLD: p = "%hold"; break;
  57218. + case SPI_TRAP: p = "%trap"; break;
  57219. + case SPI_TRAPSUBNET: p = "%trapsubnet"; break;
  57220. + default: p = NULL; break;
  57221. + }
  57222. + if (p != NULL) {
  57223. + strcpy(buf, p);
  57224. + len = strlen(buf);
  57225. + }
  57226. + }
  57227. +
  57228. + if (len == 0) { /* general case needed */
  57229. + strcpy(buf, pre);
  57230. + len = strlen(buf);
  57231. + if (showversion) {
  57232. + *(buf+len) = (addrtypeof(&sa->dst) == AF_INET) ? '.' :
  57233. + ':';
  57234. + len++;
  57235. + *(buf+len) = '\0';
  57236. + }
  57237. + len += ultot(ntohl(sa->spi), base, buf+len, sizeof(buf)-len);
  57238. + *(buf+len-1) = '@';
  57239. + len += addrtot(&sa->dst, 0, buf+len, sizeof(buf)-len);
  57240. + *(buf+len) = '\0';
  57241. + }
  57242. +
  57243. + if (dst != NULL) {
  57244. + if (len > dstlen)
  57245. + *(buf+dstlen-1) = '\0';
  57246. + strcpy(dst, buf);
  57247. + }
  57248. + return len;
  57249. +}
  57250. --- /dev/null Tue Mar 11 13:02:56 2003
  57251. +++ linux/net/ipsec/subnetof.c Mon Feb 9 13:51:03 2004
  57252. @@ -0,0 +1,59 @@
  57253. +/*
  57254. + * minor network-address manipulation utilities
  57255. + * Copyright (C) 1998, 1999 Henry Spencer.
  57256. + *
  57257. + * This library is free software; you can redistribute it and/or modify it
  57258. + * under the terms of the GNU Library General Public License as published by
  57259. + * the Free Software Foundation; either version 2 of the License, or (at your
  57260. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57261. + *
  57262. + * This library is distributed in the hope that it will be useful, but
  57263. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57264. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57265. + * License for more details.
  57266. + *
  57267. + * RCSID $Id: subnetof.c,v 1.8 2004/07/10 07:48:37 mcr Exp $
  57268. + */
  57269. +#include "openswan.h"
  57270. +
  57271. +/*
  57272. + - subnetof - given address and mask, return subnet part
  57273. + */
  57274. +struct in_addr
  57275. +subnetof(addr, mask)
  57276. +struct in_addr addr;
  57277. +struct in_addr mask;
  57278. +{
  57279. + struct in_addr result;
  57280. +
  57281. + result.s_addr = addr.s_addr & mask.s_addr;
  57282. + return result;
  57283. +}
  57284. +
  57285. +/*
  57286. + - hostof - given address and mask, return host part
  57287. + */
  57288. +struct in_addr
  57289. +hostof(addr, mask)
  57290. +struct in_addr addr;
  57291. +struct in_addr mask;
  57292. +{
  57293. + struct in_addr result;
  57294. +
  57295. + result.s_addr = addr.s_addr & ~mask.s_addr;
  57296. + return result;
  57297. +}
  57298. +
  57299. +/*
  57300. + - broadcastof - given (network) address and mask, return broadcast address
  57301. + */
  57302. +struct in_addr
  57303. +broadcastof(addr, mask)
  57304. +struct in_addr addr;
  57305. +struct in_addr mask;
  57306. +{
  57307. + struct in_addr result;
  57308. +
  57309. + result.s_addr = addr.s_addr | ~mask.s_addr;
  57310. + return result;
  57311. +}
  57312. --- /dev/null Tue Mar 11 13:02:56 2003
  57313. +++ linux/net/ipsec/subnettoa.c Mon Feb 9 13:51:03 2004
  57314. @@ -0,0 +1,61 @@
  57315. +/*
  57316. + * convert binary form of subnet description to ASCII
  57317. + * Copyright (C) 1998, 1999 Henry Spencer.
  57318. + *
  57319. + * This library is free software; you can redistribute it and/or modify it
  57320. + * under the terms of the GNU Library General Public License as published by
  57321. + * the Free Software Foundation; either version 2 of the License, or (at your
  57322. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57323. + *
  57324. + * This library is distributed in the hope that it will be useful, but
  57325. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57326. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57327. + * License for more details.
  57328. + *
  57329. + * RCSID $Id: subnettoa.c,v 1.11 2004/07/10 07:48:37 mcr Exp $
  57330. + */
  57331. +#include "openswan.h"
  57332. +
  57333. +/*
  57334. + - subnettoa - convert address and mask to ASCII "addr/mask"
  57335. + * Output expresses the mask as a bit count if possible, else dotted decimal.
  57336. + */
  57337. +size_t /* space needed for full conversion */
  57338. +subnettoa(addr, mask, format, dst, dstlen)
  57339. +struct in_addr addr;
  57340. +struct in_addr mask;
  57341. +int format; /* character */
  57342. +char *dst; /* need not be valid if dstlen is 0 */
  57343. +size_t dstlen;
  57344. +{
  57345. + size_t len;
  57346. + size_t rest;
  57347. + int n;
  57348. + char *p;
  57349. +
  57350. + switch (format) {
  57351. + case 0:
  57352. + break;
  57353. + default:
  57354. + return 0;
  57355. + break;
  57356. + }
  57357. +
  57358. + len = addrtoa(addr, 0, dst, dstlen);
  57359. + if (len < dstlen) {
  57360. + dst[len - 1] = '/';
  57361. + p = dst + len;
  57362. + rest = dstlen - len;
  57363. + } else {
  57364. + p = NULL;
  57365. + rest = 0;
  57366. + }
  57367. +
  57368. + n = masktobits(mask);
  57369. + if (n >= 0)
  57370. + len += ultoa((unsigned long)n, 10, p, rest);
  57371. + else
  57372. + len += addrtoa(mask, 0, p, rest);
  57373. +
  57374. + return len;
  57375. +}
  57376. --- /dev/null Tue Mar 11 13:02:56 2003
  57377. +++ linux/net/ipsec/sysctl_net_ipsec.c Mon Feb 9 13:51:03 2004
  57378. @@ -0,0 +1,199 @@
  57379. +/*
  57380. + * sysctl interface to net IPSEC subsystem.
  57381. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  57382. + *
  57383. + * This program is free software; you can redistribute it and/or modify it
  57384. + * under the terms of the GNU General Public License as published by the
  57385. + * Free Software Foundation; either version 2 of the License, or (at your
  57386. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  57387. + *
  57388. + * This program is distributed in the hope that it will be useful, but
  57389. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57390. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  57391. + * for more details.
  57392. + *
  57393. + * RCSID $Id: sysctl_net_ipsec.c,v 1.17 2004/07/10 19:11:18 mcr Exp $
  57394. + */
  57395. +
  57396. +/* -*- linux-c -*-
  57397. + *
  57398. + * Initiated April 3, 1998, Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  57399. + */
  57400. +
  57401. +#include <linux/mm.h>
  57402. +#include <linux/sysctl.h>
  57403. +
  57404. +#include "openswan/ipsec_param.h"
  57405. +
  57406. +#ifdef CONFIG_SYSCTL
  57407. +
  57408. +#define NET_IPSEC 2112 /* Random number */
  57409. +#ifdef CONFIG_KLIPS_DEBUG
  57410. +extern int debug_ah;
  57411. +extern int debug_esp;
  57412. +extern int debug_tunnel;
  57413. +extern int debug_eroute;
  57414. +extern int debug_spi;
  57415. +extern int debug_radij;
  57416. +extern int debug_netlink;
  57417. +extern int debug_xform;
  57418. +extern int debug_rcv;
  57419. +extern int debug_pfkey;
  57420. +extern int sysctl_ipsec_debug_verbose;
  57421. +#ifdef CONFIG_KLIPS_IPCOMP
  57422. +extern int sysctl_ipsec_debug_ipcomp;
  57423. +#endif /* CONFIG_KLIPS_IPCOMP */
  57424. +#endif /* CONFIG_KLIPS_DEBUG */
  57425. +
  57426. +extern int sysctl_ipsec_icmp;
  57427. +extern int sysctl_ipsec_inbound_policy_check;
  57428. +extern int sysctl_ipsec_tos;
  57429. +int sysctl_ipsec_regress_pfkey_lossage;
  57430. +
  57431. +enum {
  57432. +#ifdef CONFIG_KLIPS_DEBUG
  57433. + NET_IPSEC_DEBUG_AH=1,
  57434. + NET_IPSEC_DEBUG_ESP=2,
  57435. + NET_IPSEC_DEBUG_TUNNEL=3,
  57436. + NET_IPSEC_DEBUG_EROUTE=4,
  57437. + NET_IPSEC_DEBUG_SPI=5,
  57438. + NET_IPSEC_DEBUG_RADIJ=6,
  57439. + NET_IPSEC_DEBUG_NETLINK=7,
  57440. + NET_IPSEC_DEBUG_XFORM=8,
  57441. + NET_IPSEC_DEBUG_RCV=9,
  57442. + NET_IPSEC_DEBUG_PFKEY=10,
  57443. + NET_IPSEC_DEBUG_VERBOSE=11,
  57444. + NET_IPSEC_DEBUG_IPCOMP=12,
  57445. +#endif /* CONFIG_KLIPS_DEBUG */
  57446. + NET_IPSEC_ICMP=13,
  57447. + NET_IPSEC_INBOUND_POLICY_CHECK=14,
  57448. + NET_IPSEC_TOS=15,
  57449. + NET_IPSEC_REGRESS_PFKEY_LOSSAGE=16,
  57450. +};
  57451. +
  57452. +static ctl_table ipsec_table[] = {
  57453. +#ifdef CONFIG_KLIPS_DEBUG
  57454. + { NET_IPSEC_DEBUG_AH, "debug_ah", &debug_ah,
  57455. + sizeof(int), 0644, NULL, &proc_dointvec},
  57456. + { NET_IPSEC_DEBUG_ESP, "debug_esp", &debug_esp,
  57457. + sizeof(int), 0644, NULL, &proc_dointvec},
  57458. + { NET_IPSEC_DEBUG_TUNNEL, "debug_tunnel", &debug_tunnel,
  57459. + sizeof(int), 0644, NULL, &proc_dointvec},
  57460. + { NET_IPSEC_DEBUG_EROUTE, "debug_eroute", &debug_eroute,
  57461. + sizeof(int), 0644, NULL, &proc_dointvec},
  57462. + { NET_IPSEC_DEBUG_SPI, "debug_spi", &debug_spi,
  57463. + sizeof(int), 0644, NULL, &proc_dointvec},
  57464. + { NET_IPSEC_DEBUG_RADIJ, "debug_radij", &debug_radij,
  57465. + sizeof(int), 0644, NULL, &proc_dointvec},
  57466. + { NET_IPSEC_DEBUG_NETLINK, "debug_netlink", &debug_netlink,
  57467. + sizeof(int), 0644, NULL, &proc_dointvec},
  57468. + { NET_IPSEC_DEBUG_XFORM, "debug_xform", &debug_xform,
  57469. + sizeof(int), 0644, NULL, &proc_dointvec},
  57470. + { NET_IPSEC_DEBUG_RCV, "debug_rcv", &debug_rcv,
  57471. + sizeof(int), 0644, NULL, &proc_dointvec},
  57472. + { NET_IPSEC_DEBUG_PFKEY, "debug_pfkey", &debug_pfkey,
  57473. + sizeof(int), 0644, NULL, &proc_dointvec},
  57474. + { NET_IPSEC_DEBUG_VERBOSE, "debug_verbose",&sysctl_ipsec_debug_verbose,
  57475. + sizeof(int), 0644, NULL, &proc_dointvec},
  57476. +#ifdef CONFIG_KLIPS_IPCOMP
  57477. + { NET_IPSEC_DEBUG_IPCOMP, "debug_ipcomp", &sysctl_ipsec_debug_ipcomp,
  57478. + sizeof(int), 0644, NULL, &proc_dointvec},
  57479. +#endif /* CONFIG_KLIPS_IPCOMP */
  57480. +
  57481. +#ifdef CONFIG_KLIPS_REGRESS
  57482. + { NET_IPSEC_REGRESS_PFKEY_LOSSAGE, "pfkey_lossage",
  57483. + &sysctl_ipsec_regress_pfkey_lossage,
  57484. + sizeof(int), 0644, NULL, &proc_dointvec},
  57485. +#endif /* CONFIG_KLIPS_REGRESS */
  57486. +
  57487. +#endif /* CONFIG_KLIPS_DEBUG */
  57488. + { NET_IPSEC_ICMP, "icmp", &sysctl_ipsec_icmp,
  57489. + sizeof(int), 0644, NULL, &proc_dointvec},
  57490. + { NET_IPSEC_INBOUND_POLICY_CHECK, "inbound_policy_check", &sysctl_ipsec_inbound_policy_check,
  57491. + sizeof(int), 0644, NULL, &proc_dointvec},
  57492. + { NET_IPSEC_TOS, "tos", &sysctl_ipsec_tos,
  57493. + sizeof(int), 0644, NULL, &proc_dointvec},
  57494. + {0}
  57495. +};
  57496. +
  57497. +static ctl_table ipsec_net_table[] = {
  57498. + { NET_IPSEC, "ipsec", NULL, 0, 0555, ipsec_table },
  57499. + { 0 }
  57500. +};
  57501. +
  57502. +static ctl_table ipsec_root_table[] = {
  57503. + { CTL_NET, "net", NULL, 0, 0555, ipsec_net_table },
  57504. + { 0 }
  57505. +};
  57506. +
  57507. +static struct ctl_table_header *ipsec_table_header;
  57508. +
  57509. +int ipsec_sysctl_register(void)
  57510. +{
  57511. + ipsec_table_header = register_sysctl_table(ipsec_root_table, 0);
  57512. + if (!ipsec_table_header) {
  57513. + return -ENOMEM;
  57514. + }
  57515. + return 0;
  57516. +}
  57517. +
  57518. +void ipsec_sysctl_unregister(void)
  57519. +{
  57520. + unregister_sysctl_table(ipsec_table_header);
  57521. +}
  57522. +
  57523. +#endif /* CONFIG_SYSCTL */
  57524. +
  57525. +/*
  57526. + * $Log: sysctl_net_ipsec.c,v $
  57527. + * Revision 1.17 2004/07/10 19:11:18 mcr
  57528. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  57529. + *
  57530. + * Revision 1.16 2004/04/06 02:49:26 mcr
  57531. + * pullup of algo code from alg-branch.
  57532. + *
  57533. + * Revision 1.15 2002/04/24 07:55:32 mcr
  57534. + * #include patches and Makefiles for post-reorg compilation.
  57535. + *
  57536. + * Revision 1.14 2002/04/24 07:36:35 mcr
  57537. + * Moved from ./klips/net/ipsec/sysctl_net_ipsec.c,v
  57538. + *
  57539. + * Revision 1.13 2002/01/12 02:58:32 mcr
  57540. + * first regression test causes acquire messages to be lost
  57541. + * 100% of the time. This is to help testing of pluto.
  57542. + *
  57543. + * Revision 1.12 2001/06/14 19:35:13 rgb
  57544. + * Update copyright date.
  57545. + *
  57546. + * Revision 1.11 2001/02/26 19:58:13 rgb
  57547. + * Drop sysctl_ipsec_{no_eroute_pass,opportunistic}, replaced by magic SAs.
  57548. + *
  57549. + * Revision 1.10 2000/09/16 01:50:15 rgb
  57550. + * Protect sysctl_ipsec_debug_ipcomp with compiler defines too so that the
  57551. + * linker won't blame rj_delete() for missing symbols. ;-> Damn statics...
  57552. + *
  57553. + * Revision 1.9 2000/09/15 23:17:51 rgb
  57554. + * Moved stuff around to compile with debug off.
  57555. + *
  57556. + * Revision 1.8 2000/09/15 11:37:02 rgb
  57557. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  57558. + * IPCOMP zlib deflate code.
  57559. + *
  57560. + * Revision 1.7 2000/09/15 07:37:15 rgb
  57561. + * Munged silly log comment that was causing a warning.
  57562. + *
  57563. + * Revision 1.6 2000/09/15 04:58:23 rgb
  57564. + * Added tos runtime switch.
  57565. + * Removed 'sysctl_ipsec_' prefix from /proc/sys/net/ipsec/ filenames.
  57566. + *
  57567. + * Revision 1.5 2000/09/12 03:25:28 rgb
  57568. + * Filled in and implemented sysctl.
  57569. + *
  57570. + * Revision 1.4 1999/04/11 00:29:03 henry
  57571. + * GPL boilerplate
  57572. + *
  57573. + * Revision 1.3 1999/04/06 04:54:29 rgb
  57574. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  57575. + * patch shell fixes.
  57576. + *
  57577. + */
  57578. --- /dev/null Tue Mar 11 13:02:56 2003
  57579. +++ linux/net/ipsec/trees.c Mon Feb 9 13:51:03 2004
  57580. @@ -0,0 +1,1214 @@
  57581. +/* trees.c -- output deflated data using Huffman coding
  57582. + * Copyright (C) 1995-2002 Jean-loup Gailly
  57583. + * For conditions of distribution and use, see copyright notice in zlib.h
  57584. + */
  57585. +
  57586. +/*
  57587. + * ALGORITHM
  57588. + *
  57589. + * The "deflation" process uses several Huffman trees. The more
  57590. + * common source values are represented by shorter bit sequences.
  57591. + *
  57592. + * Each code tree is stored in a compressed form which is itself
  57593. + * a Huffman encoding of the lengths of all the code strings (in
  57594. + * ascending order by source values). The actual code strings are
  57595. + * reconstructed from the lengths in the inflate process, as described
  57596. + * in the deflate specification.
  57597. + *
  57598. + * REFERENCES
  57599. + *
  57600. + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  57601. + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  57602. + *
  57603. + * Storer, James A.
  57604. + * Data Compression: Methods and Theory, pp. 49-50.
  57605. + * Computer Science Press, 1988. ISBN 0-7167-8156-5.
  57606. + *
  57607. + * Sedgewick, R.
  57608. + * Algorithms, p290.
  57609. + * Addison-Wesley, 1983. ISBN 0-201-06672-6.
  57610. + */
  57611. +
  57612. +/* @(#) $Id: trees.c,v 1.4 2004/07/10 07:48:39 mcr Exp $ */
  57613. +
  57614. +/* #define GEN_TREES_H */
  57615. +
  57616. +#include "deflate.h"
  57617. +
  57618. +#ifdef DEBUG
  57619. +# include <ctype.h>
  57620. +#endif
  57621. +
  57622. +/* ===========================================================================
  57623. + * Constants
  57624. + */
  57625. +
  57626. +#define MAX_BL_BITS 7
  57627. +/* Bit length codes must not exceed MAX_BL_BITS bits */
  57628. +
  57629. +#define END_BLOCK 256
  57630. +/* end of block literal code */
  57631. +
  57632. +#define REP_3_6 16
  57633. +/* repeat previous bit length 3-6 times (2 bits of repeat count) */
  57634. +
  57635. +#define REPZ_3_10 17
  57636. +/* repeat a zero length 3-10 times (3 bits of repeat count) */
  57637. +
  57638. +#define REPZ_11_138 18
  57639. +/* repeat a zero length 11-138 times (7 bits of repeat count) */
  57640. +
  57641. +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
  57642. + = {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};
  57643. +
  57644. +local const int extra_dbits[D_CODES] /* extra bits for each distance code */
  57645. + = {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};
  57646. +
  57647. +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
  57648. + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  57649. +
  57650. +local const uch bl_order[BL_CODES]
  57651. + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  57652. +/* The lengths of the bit length codes are sent in order of decreasing
  57653. + * probability, to avoid transmitting the lengths for unused bit length codes.
  57654. + */
  57655. +
  57656. +#define Buf_size (8 * 2*sizeof(char))
  57657. +/* Number of bits used within bi_buf. (bi_buf might be implemented on
  57658. + * more than 16 bits on some systems.)
  57659. + */
  57660. +
  57661. +/* ===========================================================================
  57662. + * Local data. These are initialized only once.
  57663. + */
  57664. +
  57665. +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
  57666. +
  57667. +#if defined(GEN_TREES_H) || !defined(STDC)
  57668. +/* non ANSI compilers may not accept trees.h */
  57669. +
  57670. +local ct_data static_ltree[L_CODES+2];
  57671. +/* The static literal tree. Since the bit lengths are imposed, there is no
  57672. + * need for the L_CODES extra codes used during heap construction. However
  57673. + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  57674. + * below).
  57675. + */
  57676. +
  57677. +local ct_data static_dtree[D_CODES];
  57678. +/* The static distance tree. (Actually a trivial tree since all codes use
  57679. + * 5 bits.)
  57680. + */
  57681. +
  57682. +uch _dist_code[DIST_CODE_LEN];
  57683. +/* Distance codes. The first 256 values correspond to the distances
  57684. + * 3 .. 258, the last 256 values correspond to the top 8 bits of
  57685. + * the 15 bit distances.
  57686. + */
  57687. +
  57688. +uch _length_code[MAX_MATCH-MIN_MATCH+1];
  57689. +/* length code for each normalized match length (0 == MIN_MATCH) */
  57690. +
  57691. +local int base_length[LENGTH_CODES];
  57692. +/* First normalized length for each code (0 = MIN_MATCH) */
  57693. +
  57694. +local int base_dist[D_CODES];
  57695. +/* First normalized distance for each code (0 = distance of 1) */
  57696. +
  57697. +#else
  57698. +# include "trees.h"
  57699. +#endif /* GEN_TREES_H */
  57700. +
  57701. +struct static_tree_desc_s {
  57702. + const ct_data *static_tree; /* static tree or NULL */
  57703. + const intf *extra_bits; /* extra bits for each code or NULL */
  57704. + int extra_base; /* base index for extra_bits */
  57705. + int elems; /* max number of elements in the tree */
  57706. + int max_length; /* max bit length for the codes */
  57707. +};
  57708. +
  57709. +local static_tree_desc static_l_desc =
  57710. +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
  57711. +
  57712. +local static_tree_desc static_d_desc =
  57713. +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
  57714. +
  57715. +local static_tree_desc static_bl_desc =
  57716. +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
  57717. +
  57718. +/* ===========================================================================
  57719. + * Local (static) routines in this file.
  57720. + */
  57721. +
  57722. +local void tr_static_init OF((void));
  57723. +local void init_block OF((deflate_state *s));
  57724. +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
  57725. +local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
  57726. +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
  57727. +local void build_tree OF((deflate_state *s, tree_desc *desc));
  57728. +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
  57729. +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
  57730. +local int build_bl_tree OF((deflate_state *s));
  57731. +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
  57732. + int blcodes));
  57733. +local void compress_block OF((deflate_state *s, const ct_data *ltree,
  57734. + const ct_data *dtree));
  57735. +local void set_data_type OF((deflate_state *s));
  57736. +local unsigned bi_reverse OF((unsigned value, int length));
  57737. +local void bi_windup OF((deflate_state *s));
  57738. +local void bi_flush OF((deflate_state *s));
  57739. +local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
  57740. + int header));
  57741. +
  57742. +#ifdef GEN_TREES_H
  57743. +local void gen_trees_header OF((void));
  57744. +#endif
  57745. +
  57746. +#ifndef DEBUG
  57747. +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
  57748. + /* Send a code of the given tree. c and tree must not have side effects */
  57749. +
  57750. +#else /* DEBUG */
  57751. +# define send_code(s, c, tree) \
  57752. + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
  57753. + send_bits(s, tree[c].Code, tree[c].Len); }
  57754. +#endif
  57755. +
  57756. +/* ===========================================================================
  57757. + * Output a short LSB first on the stream.
  57758. + * IN assertion: there is enough room in pendingBuf.
  57759. + */
  57760. +#define put_short(s, w) { \
  57761. + put_byte(s, (uch)((w) & 0xff)); \
  57762. + put_byte(s, (uch)((ush)(w) >> 8)); \
  57763. +}
  57764. +
  57765. +/* ===========================================================================
  57766. + * Send a value on a given number of bits.
  57767. + * IN assertion: length <= 16 and value fits in length bits.
  57768. + */
  57769. +#ifdef DEBUG
  57770. +local void send_bits OF((deflate_state *s, int value, int length));
  57771. +
  57772. +local void send_bits(s, value, length)
  57773. + deflate_state *s;
  57774. + int value; /* value to send */
  57775. + int length; /* number of bits */
  57776. +{
  57777. + Tracevv((stderr," l %2d v %4x ", length, value));
  57778. + Assert(length > 0 && length <= 15, "invalid length");
  57779. + s->bits_sent += (ulg)length;
  57780. +
  57781. + /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  57782. + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  57783. + * unused bits in value.
  57784. + */
  57785. + if (s->bi_valid > (int)Buf_size - length) {
  57786. + s->bi_buf |= (value << s->bi_valid);
  57787. + put_short(s, s->bi_buf);
  57788. + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
  57789. + s->bi_valid += length - Buf_size;
  57790. + } else {
  57791. + s->bi_buf |= value << s->bi_valid;
  57792. + s->bi_valid += length;
  57793. + }
  57794. +}
  57795. +#else /* !DEBUG */
  57796. +
  57797. +#define send_bits(s, value, length) \
  57798. +{ int len = length;\
  57799. + if (s->bi_valid > (int)Buf_size - len) {\
  57800. + int val = value;\
  57801. + s->bi_buf |= (val << s->bi_valid);\
  57802. + put_short(s, s->bi_buf);\
  57803. + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
  57804. + s->bi_valid += len - Buf_size;\
  57805. + } else {\
  57806. + s->bi_buf |= (value) << s->bi_valid;\
  57807. + s->bi_valid += len;\
  57808. + }\
  57809. +}
  57810. +#endif /* DEBUG */
  57811. +
  57812. +
  57813. +#define MAX(a,b) (a >= b ? a : b)
  57814. +/* the arguments must not have side effects */
  57815. +
  57816. +/* ===========================================================================
  57817. + * Initialize the various 'constant' tables.
  57818. + */
  57819. +local void tr_static_init()
  57820. +{
  57821. +#if defined(GEN_TREES_H) || !defined(STDC)
  57822. + static int static_init_done = 0;
  57823. + int n; /* iterates over tree elements */
  57824. + int bits; /* bit counter */
  57825. + int length; /* length value */
  57826. + int code; /* code value */
  57827. + int dist; /* distance index */
  57828. + ush bl_count[MAX_BITS+1];
  57829. + /* number of codes at each bit length for an optimal tree */
  57830. +
  57831. + if (static_init_done) return;
  57832. +
  57833. + /* For some embedded targets, global variables are not initialized: */
  57834. + static_l_desc.static_tree = static_ltree;
  57835. + static_l_desc.extra_bits = extra_lbits;
  57836. + static_d_desc.static_tree = static_dtree;
  57837. + static_d_desc.extra_bits = extra_dbits;
  57838. + static_bl_desc.extra_bits = extra_blbits;
  57839. +
  57840. + /* Initialize the mapping length (0..255) -> length code (0..28) */
  57841. + length = 0;
  57842. + for (code = 0; code < LENGTH_CODES-1; code++) {
  57843. + base_length[code] = length;
  57844. + for (n = 0; n < (1<<extra_lbits[code]); n++) {
  57845. + _length_code[length++] = (uch)code;
  57846. + }
  57847. + }
  57848. + Assert (length == 256, "tr_static_init: length != 256");
  57849. + /* Note that the length 255 (match length 258) can be represented
  57850. + * in two different ways: code 284 + 5 bits or code 285, so we
  57851. + * overwrite length_code[255] to use the best encoding:
  57852. + */
  57853. + _length_code[length-1] = (uch)code;
  57854. +
  57855. + /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  57856. + dist = 0;
  57857. + for (code = 0 ; code < 16; code++) {
  57858. + base_dist[code] = dist;
  57859. + for (n = 0; n < (1<<extra_dbits[code]); n++) {
  57860. + _dist_code[dist++] = (uch)code;
  57861. + }
  57862. + }
  57863. + Assert (dist == 256, "tr_static_init: dist != 256");
  57864. + dist >>= 7; /* from now on, all distances are divided by 128 */
  57865. + for ( ; code < D_CODES; code++) {
  57866. + base_dist[code] = dist << 7;
  57867. + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  57868. + _dist_code[256 + dist++] = (uch)code;
  57869. + }
  57870. + }
  57871. + Assert (dist == 256, "tr_static_init: 256+dist != 512");
  57872. +
  57873. + /* Construct the codes of the static literal tree */
  57874. + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  57875. + n = 0;
  57876. + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
  57877. + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
  57878. + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
  57879. + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
  57880. + /* Codes 286 and 287 do not exist, but we must include them in the
  57881. + * tree construction to get a canonical Huffman tree (longest code
  57882. + * all ones)
  57883. + */
  57884. + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
  57885. +
  57886. + /* The static distance tree is trivial: */
  57887. + for (n = 0; n < D_CODES; n++) {
  57888. + static_dtree[n].Len = 5;
  57889. + static_dtree[n].Code = bi_reverse((unsigned)n, 5);
  57890. + }
  57891. + static_init_done = 1;
  57892. +
  57893. +# ifdef GEN_TREES_H
  57894. + gen_trees_header();
  57895. +# endif
  57896. +#endif /* defined(GEN_TREES_H) || !defined(STDC) */
  57897. +}
  57898. +
  57899. +/* ===========================================================================
  57900. + * Genererate the file trees.h describing the static trees.
  57901. + */
  57902. +#ifdef GEN_TREES_H
  57903. +# ifndef DEBUG
  57904. +# include <stdio.h>
  57905. +# endif
  57906. +
  57907. +# define SEPARATOR(i, last, width) \
  57908. + ((i) == (last)? "\n};\n\n" : \
  57909. + ((i) % (width) == (width)-1 ? ",\n" : ", "))
  57910. +
  57911. +void gen_trees_header()
  57912. +{
  57913. + FILE *header = fopen("trees.h", "w");
  57914. + int i;
  57915. +
  57916. + Assert (header != NULL, "Can't open trees.h");
  57917. + fprintf(header,
  57918. + "/* header created automatically with -DGEN_TREES_H */\n\n");
  57919. +
  57920. + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
  57921. + for (i = 0; i < L_CODES+2; i++) {
  57922. + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
  57923. + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
  57924. + }
  57925. +
  57926. + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
  57927. + for (i = 0; i < D_CODES; i++) {
  57928. + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
  57929. + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
  57930. + }
  57931. +
  57932. + fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
  57933. + for (i = 0; i < DIST_CODE_LEN; i++) {
  57934. + fprintf(header, "%2u%s", _dist_code[i],
  57935. + SEPARATOR(i, DIST_CODE_LEN-1, 20));
  57936. + }
  57937. +
  57938. + fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
  57939. + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
  57940. + fprintf(header, "%2u%s", _length_code[i],
  57941. + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
  57942. + }
  57943. +
  57944. + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
  57945. + for (i = 0; i < LENGTH_CODES; i++) {
  57946. + fprintf(header, "%1u%s", base_length[i],
  57947. + SEPARATOR(i, LENGTH_CODES-1, 20));
  57948. + }
  57949. +
  57950. + fprintf(header, "local const int base_dist[D_CODES] = {\n");
  57951. + for (i = 0; i < D_CODES; i++) {
  57952. + fprintf(header, "%5u%s", base_dist[i],
  57953. + SEPARATOR(i, D_CODES-1, 10));
  57954. + }
  57955. +
  57956. + fclose(header);
  57957. +}
  57958. +#endif /* GEN_TREES_H */
  57959. +
  57960. +/* ===========================================================================
  57961. + * Initialize the tree data structures for a new zlib stream.
  57962. + */
  57963. +void _tr_init(s)
  57964. + deflate_state *s;
  57965. +{
  57966. + tr_static_init();
  57967. +
  57968. + s->l_desc.dyn_tree = s->dyn_ltree;
  57969. + s->l_desc.stat_desc = &static_l_desc;
  57970. +
  57971. + s->d_desc.dyn_tree = s->dyn_dtree;
  57972. + s->d_desc.stat_desc = &static_d_desc;
  57973. +
  57974. + s->bl_desc.dyn_tree = s->bl_tree;
  57975. + s->bl_desc.stat_desc = &static_bl_desc;
  57976. +
  57977. + s->bi_buf = 0;
  57978. + s->bi_valid = 0;
  57979. + s->last_eob_len = 8; /* enough lookahead for inflate */
  57980. +#ifdef DEBUG
  57981. + s->compressed_len = 0L;
  57982. + s->bits_sent = 0L;
  57983. +#endif
  57984. +
  57985. + /* Initialize the first block of the first file: */
  57986. + init_block(s);
  57987. +}
  57988. +
  57989. +/* ===========================================================================
  57990. + * Initialize a new block.
  57991. + */
  57992. +local void init_block(s)
  57993. + deflate_state *s;
  57994. +{
  57995. + int n; /* iterates over tree elements */
  57996. +
  57997. + /* Initialize the trees. */
  57998. + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
  57999. + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
  58000. + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
  58001. +
  58002. + s->dyn_ltree[END_BLOCK].Freq = 1;
  58003. + s->opt_len = s->static_len = 0L;
  58004. + s->last_lit = s->matches = 0;
  58005. +}
  58006. +
  58007. +#define SMALLEST 1
  58008. +/* Index within the heap array of least frequent node in the Huffman tree */
  58009. +
  58010. +
  58011. +/* ===========================================================================
  58012. + * Remove the smallest element from the heap and recreate the heap with
  58013. + * one less element. Updates heap and heap_len.
  58014. + */
  58015. +#define pqremove(s, tree, top) \
  58016. +{\
  58017. + top = s->heap[SMALLEST]; \
  58018. + s->heap[SMALLEST] = s->heap[s->heap_len--]; \
  58019. + pqdownheap(s, tree, SMALLEST); \
  58020. +}
  58021. +
  58022. +/* ===========================================================================
  58023. + * Compares to subtrees, using the tree depth as tie breaker when
  58024. + * the subtrees have equal frequency. This minimizes the worst case length.
  58025. + */
  58026. +#define smaller(tree, n, m, depth) \
  58027. + (tree[n].Freq < tree[m].Freq || \
  58028. + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
  58029. +
  58030. +/* ===========================================================================
  58031. + * Restore the heap property by moving down the tree starting at node k,
  58032. + * exchanging a node with the smallest of its two sons if necessary, stopping
  58033. + * when the heap property is re-established (each father smaller than its
  58034. + * two sons).
  58035. + */
  58036. +local void pqdownheap(s, tree, k)
  58037. + deflate_state *s;
  58038. + ct_data *tree; /* the tree to restore */
  58039. + int k; /* node to move down */
  58040. +{
  58041. + int v = s->heap[k];
  58042. + int j = k << 1; /* left son of k */
  58043. + while (j <= s->heap_len) {
  58044. + /* Set j to the smallest of the two sons: */
  58045. + if (j < s->heap_len &&
  58046. + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
  58047. + j++;
  58048. + }
  58049. + /* Exit if v is smaller than both sons */
  58050. + if (smaller(tree, v, s->heap[j], s->depth)) break;
  58051. +
  58052. + /* Exchange v with the smallest son */
  58053. + s->heap[k] = s->heap[j]; k = j;
  58054. +
  58055. + /* And continue down the tree, setting j to the left son of k */
  58056. + j <<= 1;
  58057. + }
  58058. + s->heap[k] = v;
  58059. +}
  58060. +
  58061. +/* ===========================================================================
  58062. + * Compute the optimal bit lengths for a tree and update the total bit length
  58063. + * for the current block.
  58064. + * IN assertion: the fields freq and dad are set, heap[heap_max] and
  58065. + * above are the tree nodes sorted by increasing frequency.
  58066. + * OUT assertions: the field len is set to the optimal bit length, the
  58067. + * array bl_count contains the frequencies for each bit length.
  58068. + * The length opt_len is updated; static_len is also updated if stree is
  58069. + * not null.
  58070. + */
  58071. +local void gen_bitlen(s, desc)
  58072. + deflate_state *s;
  58073. + tree_desc *desc; /* the tree descriptor */
  58074. +{
  58075. + ct_data *tree = desc->dyn_tree;
  58076. + int max_code = desc->max_code;
  58077. + const ct_data *stree = desc->stat_desc->static_tree;
  58078. + const intf *extra = desc->stat_desc->extra_bits;
  58079. + int base = desc->stat_desc->extra_base;
  58080. + int max_length = desc->stat_desc->max_length;
  58081. + int h; /* heap index */
  58082. + int n, m; /* iterate over the tree elements */
  58083. + int bits; /* bit length */
  58084. + int xbits; /* extra bits */
  58085. + ush f; /* frequency */
  58086. + int overflow = 0; /* number of elements with bit length too large */
  58087. +
  58088. + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
  58089. +
  58090. + /* In a first pass, compute the optimal bit lengths (which may
  58091. + * overflow in the case of the bit length tree).
  58092. + */
  58093. + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
  58094. +
  58095. + for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
  58096. + n = s->heap[h];
  58097. + bits = tree[tree[n].Dad].Len + 1;
  58098. + if (bits > max_length) bits = max_length, overflow++;
  58099. + tree[n].Len = (ush)bits;
  58100. + /* We overwrite tree[n].Dad which is no longer needed */
  58101. +
  58102. + if (n > max_code) continue; /* not a leaf node */
  58103. +
  58104. + s->bl_count[bits]++;
  58105. + xbits = 0;
  58106. + if (n >= base) xbits = extra[n-base];
  58107. + f = tree[n].Freq;
  58108. + s->opt_len += (ulg)f * (bits + xbits);
  58109. + if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
  58110. + }
  58111. + if (overflow == 0) return;
  58112. +
  58113. + Trace((stderr,"\nbit length overflow\n"));
  58114. + /* This happens for example on obj2 and pic of the Calgary corpus */
  58115. +
  58116. + /* Find the first bit length which could increase: */
  58117. + do {
  58118. + bits = max_length-1;
  58119. + while (s->bl_count[bits] == 0) bits--;
  58120. + s->bl_count[bits]--; /* move one leaf down the tree */
  58121. + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
  58122. + s->bl_count[max_length]--;
  58123. + /* The brother of the overflow item also moves one step up,
  58124. + * but this does not affect bl_count[max_length]
  58125. + */
  58126. + overflow -= 2;
  58127. + } while (overflow > 0);
  58128. +
  58129. + /* Now recompute all bit lengths, scanning in increasing frequency.
  58130. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  58131. + * lengths instead of fixing only the wrong ones. This idea is taken
  58132. + * from 'ar' written by Haruhiko Okumura.)
  58133. + */
  58134. + for (bits = max_length; bits != 0; bits--) {
  58135. + n = s->bl_count[bits];
  58136. + while (n != 0) {
  58137. + m = s->heap[--h];
  58138. + if (m > max_code) continue;
  58139. + if (tree[m].Len != (unsigned) bits) {
  58140. + Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  58141. + s->opt_len += ((long)bits - (long)tree[m].Len)
  58142. + *(long)tree[m].Freq;
  58143. + tree[m].Len = (ush)bits;
  58144. + }
  58145. + n--;
  58146. + }
  58147. + }
  58148. +}
  58149. +
  58150. +/* ===========================================================================
  58151. + * Generate the codes for a given tree and bit counts (which need not be
  58152. + * optimal).
  58153. + * IN assertion: the array bl_count contains the bit length statistics for
  58154. + * the given tree and the field len is set for all tree elements.
  58155. + * OUT assertion: the field code is set for all tree elements of non
  58156. + * zero code length.
  58157. + */
  58158. +local void gen_codes (tree, max_code, bl_count)
  58159. + ct_data *tree; /* the tree to decorate */
  58160. + int max_code; /* largest code with non zero frequency */
  58161. + ushf *bl_count; /* number of codes at each bit length */
  58162. +{
  58163. + ush next_code[MAX_BITS+1]; /* next code value for each bit length */
  58164. + ush code = 0; /* running code value */
  58165. + int bits; /* bit index */
  58166. + int n; /* code index */
  58167. +
  58168. + /* The distribution counts are first used to generate the code values
  58169. + * without bit reversal.
  58170. + */
  58171. + for (bits = 1; bits <= MAX_BITS; bits++) {
  58172. + next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  58173. + }
  58174. + /* Check that the bit counts in bl_count are consistent. The last code
  58175. + * must be all ones.
  58176. + */
  58177. + Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  58178. + "inconsistent bit counts");
  58179. + Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  58180. +
  58181. + for (n = 0; n <= max_code; n++) {
  58182. + int len = tree[n].Len;
  58183. + if (len == 0) continue;
  58184. + /* Now reverse the bits */
  58185. + tree[n].Code = bi_reverse(next_code[len]++, len);
  58186. +
  58187. + Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  58188. + n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  58189. + }
  58190. +}
  58191. +
  58192. +/* ===========================================================================
  58193. + * Construct one Huffman tree and assigns the code bit strings and lengths.
  58194. + * Update the total bit length for the current block.
  58195. + * IN assertion: the field freq is set for all tree elements.
  58196. + * OUT assertions: the fields len and code are set to the optimal bit length
  58197. + * and corresponding code. The length opt_len is updated; static_len is
  58198. + * also updated if stree is not null. The field max_code is set.
  58199. + */
  58200. +local void build_tree(s, desc)
  58201. + deflate_state *s;
  58202. + tree_desc *desc; /* the tree descriptor */
  58203. +{
  58204. + ct_data *tree = desc->dyn_tree;
  58205. + const ct_data *stree = desc->stat_desc->static_tree;
  58206. + int elems = desc->stat_desc->elems;
  58207. + int n, m; /* iterate over heap elements */
  58208. + int max_code = -1; /* largest code with non zero frequency */
  58209. + int node; /* new node being created */
  58210. +
  58211. + /* Construct the initial heap, with least frequent element in
  58212. + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  58213. + * heap[0] is not used.
  58214. + */
  58215. + s->heap_len = 0, s->heap_max = HEAP_SIZE;
  58216. +
  58217. + for (n = 0; n < elems; n++) {
  58218. + if (tree[n].Freq != 0) {
  58219. + s->heap[++(s->heap_len)] = max_code = n;
  58220. + s->depth[n] = 0;
  58221. + } else {
  58222. + tree[n].Len = 0;
  58223. + }
  58224. + }
  58225. +
  58226. + /* The pkzip format requires that at least one distance code exists,
  58227. + * and that at least one bit should be sent even if there is only one
  58228. + * possible code. So to avoid special checks later on we force at least
  58229. + * two codes of non zero frequency.
  58230. + */
  58231. + while (s->heap_len < 2) {
  58232. + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
  58233. + tree[node].Freq = 1;
  58234. + s->depth[node] = 0;
  58235. + s->opt_len--; if (stree) s->static_len -= stree[node].Len;
  58236. + /* node is 0 or 1 so it does not have extra bits */
  58237. + }
  58238. + desc->max_code = max_code;
  58239. +
  58240. + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  58241. + * establish sub-heaps of increasing lengths:
  58242. + */
  58243. + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
  58244. +
  58245. + /* Construct the Huffman tree by repeatedly combining the least two
  58246. + * frequent nodes.
  58247. + */
  58248. + node = elems; /* next internal node of the tree */
  58249. + do {
  58250. + pqremove(s, tree, n); /* n = node of least frequency */
  58251. + m = s->heap[SMALLEST]; /* m = node of next least frequency */
  58252. +
  58253. + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
  58254. + s->heap[--(s->heap_max)] = m;
  58255. +
  58256. + /* Create a new node father of n and m */
  58257. + tree[node].Freq = tree[n].Freq + tree[m].Freq;
  58258. + s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
  58259. + tree[n].Dad = tree[m].Dad = (ush)node;
  58260. +#ifdef DUMP_BL_TREE
  58261. + if (tree == s->bl_tree) {
  58262. + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
  58263. + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
  58264. + }
  58265. +#endif
  58266. + /* and insert the new node in the heap */
  58267. + s->heap[SMALLEST] = node++;
  58268. + pqdownheap(s, tree, SMALLEST);
  58269. +
  58270. + } while (s->heap_len >= 2);
  58271. +
  58272. + s->heap[--(s->heap_max)] = s->heap[SMALLEST];
  58273. +
  58274. + /* At this point, the fields freq and dad are set. We can now
  58275. + * generate the bit lengths.
  58276. + */
  58277. + gen_bitlen(s, (tree_desc *)desc);
  58278. +
  58279. + /* The field len is now set, we can generate the bit codes */
  58280. + gen_codes ((ct_data *)tree, max_code, s->bl_count);
  58281. +}
  58282. +
  58283. +/* ===========================================================================
  58284. + * Scan a literal or distance tree to determine the frequencies of the codes
  58285. + * in the bit length tree.
  58286. + */
  58287. +local void scan_tree (s, tree, max_code)
  58288. + deflate_state *s;
  58289. + ct_data *tree; /* the tree to be scanned */
  58290. + int max_code; /* and its largest code of non zero frequency */
  58291. +{
  58292. + int n; /* iterates over all tree elements */
  58293. + int prevlen = -1; /* last emitted length */
  58294. + int curlen; /* length of current code */
  58295. + int nextlen = tree[0].Len; /* length of next code */
  58296. + int count = 0; /* repeat count of the current code */
  58297. + int max_count = 7; /* max repeat count */
  58298. + int min_count = 4; /* min repeat count */
  58299. +
  58300. + if (nextlen == 0) max_count = 138, min_count = 3;
  58301. + tree[max_code+1].Len = (ush)0xffff; /* guard */
  58302. +
  58303. + for (n = 0; n <= max_code; n++) {
  58304. + curlen = nextlen; nextlen = tree[n+1].Len;
  58305. + if (++count < max_count && curlen == nextlen) {
  58306. + continue;
  58307. + } else if (count < min_count) {
  58308. + s->bl_tree[curlen].Freq += count;
  58309. + } else if (curlen != 0) {
  58310. + if (curlen != prevlen) s->bl_tree[curlen].Freq++;
  58311. + s->bl_tree[REP_3_6].Freq++;
  58312. + } else if (count <= 10) {
  58313. + s->bl_tree[REPZ_3_10].Freq++;
  58314. + } else {
  58315. + s->bl_tree[REPZ_11_138].Freq++;
  58316. + }
  58317. + count = 0; prevlen = curlen;
  58318. + if (nextlen == 0) {
  58319. + max_count = 138, min_count = 3;
  58320. + } else if (curlen == nextlen) {
  58321. + max_count = 6, min_count = 3;
  58322. + } else {
  58323. + max_count = 7, min_count = 4;
  58324. + }
  58325. + }
  58326. +}
  58327. +
  58328. +/* ===========================================================================
  58329. + * Send a literal or distance tree in compressed form, using the codes in
  58330. + * bl_tree.
  58331. + */
  58332. +local void send_tree (s, tree, max_code)
  58333. + deflate_state *s;
  58334. + ct_data *tree; /* the tree to be scanned */
  58335. + int max_code; /* and its largest code of non zero frequency */
  58336. +{
  58337. + int n; /* iterates over all tree elements */
  58338. + int prevlen = -1; /* last emitted length */
  58339. + int curlen; /* length of current code */
  58340. + int nextlen = tree[0].Len; /* length of next code */
  58341. + int count = 0; /* repeat count of the current code */
  58342. + int max_count = 7; /* max repeat count */
  58343. + int min_count = 4; /* min repeat count */
  58344. +
  58345. + /* tree[max_code+1].Len = -1; */ /* guard already set */
  58346. + if (nextlen == 0) max_count = 138, min_count = 3;
  58347. +
  58348. + for (n = 0; n <= max_code; n++) {
  58349. + curlen = nextlen; nextlen = tree[n+1].Len;
  58350. + if (++count < max_count && curlen == nextlen) {
  58351. + continue;
  58352. + } else if (count < min_count) {
  58353. + do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
  58354. +
  58355. + } else if (curlen != 0) {
  58356. + if (curlen != prevlen) {
  58357. + send_code(s, curlen, s->bl_tree); count--;
  58358. + }
  58359. + Assert(count >= 3 && count <= 6, " 3_6?");
  58360. + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
  58361. +
  58362. + } else if (count <= 10) {
  58363. + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
  58364. +
  58365. + } else {
  58366. + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
  58367. + }
  58368. + count = 0; prevlen = curlen;
  58369. + if (nextlen == 0) {
  58370. + max_count = 138, min_count = 3;
  58371. + } else if (curlen == nextlen) {
  58372. + max_count = 6, min_count = 3;
  58373. + } else {
  58374. + max_count = 7, min_count = 4;
  58375. + }
  58376. + }
  58377. +}
  58378. +
  58379. +/* ===========================================================================
  58380. + * Construct the Huffman tree for the bit lengths and return the index in
  58381. + * bl_order of the last bit length code to send.
  58382. + */
  58383. +local int build_bl_tree(s)
  58384. + deflate_state *s;
  58385. +{
  58386. + int max_blindex; /* index of last bit length code of non zero freq */
  58387. +
  58388. + /* Determine the bit length frequencies for literal and distance trees */
  58389. + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
  58390. + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
  58391. +
  58392. + /* Build the bit length tree: */
  58393. + build_tree(s, (tree_desc *)(&(s->bl_desc)));
  58394. + /* opt_len now includes the length of the tree representations, except
  58395. + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  58396. + */
  58397. +
  58398. + /* Determine the number of bit length codes to send. The pkzip format
  58399. + * requires that at least 4 bit length codes be sent. (appnote.txt says
  58400. + * 3 but the actual value used is 4.)
  58401. + */
  58402. + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  58403. + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
  58404. + }
  58405. + /* Update opt_len to include the bit length tree and counts */
  58406. + s->opt_len += 3*(max_blindex+1) + 5+5+4;
  58407. + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  58408. + s->opt_len, s->static_len));
  58409. +
  58410. + return max_blindex;
  58411. +}
  58412. +
  58413. +/* ===========================================================================
  58414. + * Send the header for a block using dynamic Huffman trees: the counts, the
  58415. + * lengths of the bit length codes, the literal tree and the distance tree.
  58416. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  58417. + */
  58418. +local void send_all_trees(s, lcodes, dcodes, blcodes)
  58419. + deflate_state *s;
  58420. + int lcodes, dcodes, blcodes; /* number of codes for each tree */
  58421. +{
  58422. + int rank; /* index in bl_order */
  58423. +
  58424. + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  58425. + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  58426. + "too many codes");
  58427. + Tracev((stderr, "\nbl counts: "));
  58428. + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
  58429. + send_bits(s, dcodes-1, 5);
  58430. + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
  58431. + for (rank = 0; rank < blcodes; rank++) {
  58432. + Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  58433. + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
  58434. + }
  58435. + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  58436. +
  58437. + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
  58438. + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  58439. +
  58440. + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
  58441. + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  58442. +}
  58443. +
  58444. +/* ===========================================================================
  58445. + * Send a stored block
  58446. + */
  58447. +void _tr_stored_block(s, buf, stored_len, eof)
  58448. + deflate_state *s;
  58449. + charf *buf; /* input block */
  58450. + ulg stored_len; /* length of input block */
  58451. + int eof; /* true if this is the last block for a file */
  58452. +{
  58453. + send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
  58454. +#ifdef DEBUG
  58455. + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
  58456. + s->compressed_len += (stored_len + 4) << 3;
  58457. +#endif
  58458. + copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
  58459. +}
  58460. +
  58461. +/* ===========================================================================
  58462. + * Send one empty static block to give enough lookahead for inflate.
  58463. + * This takes 10 bits, of which 7 may remain in the bit buffer.
  58464. + * The current inflate code requires 9 bits of lookahead. If the
  58465. + * last two codes for the previous block (real code plus EOB) were coded
  58466. + * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  58467. + * the last real code. In this case we send two empty static blocks instead
  58468. + * of one. (There are no problems if the previous block is stored or fixed.)
  58469. + * To simplify the code, we assume the worst case of last real code encoded
  58470. + * on one bit only.
  58471. + */
  58472. +void _tr_align(s)
  58473. + deflate_state *s;
  58474. +{
  58475. + send_bits(s, STATIC_TREES<<1, 3);
  58476. + send_code(s, END_BLOCK, static_ltree);
  58477. +#ifdef DEBUG
  58478. + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
  58479. +#endif
  58480. + bi_flush(s);
  58481. + /* Of the 10 bits for the empty block, we have already sent
  58482. + * (10 - bi_valid) bits. The lookahead for the last real code (before
  58483. + * the EOB of the previous block) was thus at least one plus the length
  58484. + * of the EOB plus what we have just sent of the empty static block.
  58485. + */
  58486. + if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
  58487. + send_bits(s, STATIC_TREES<<1, 3);
  58488. + send_code(s, END_BLOCK, static_ltree);
  58489. +#ifdef DEBUG
  58490. + s->compressed_len += 10L;
  58491. +#endif
  58492. + bi_flush(s);
  58493. + }
  58494. + s->last_eob_len = 7;
  58495. +}
  58496. +
  58497. +/* ===========================================================================
  58498. + * Determine the best encoding for the current block: dynamic trees, static
  58499. + * trees or store, and output the encoded block to the zip file.
  58500. + */
  58501. +void _tr_flush_block(s, buf, stored_len, eof)
  58502. + deflate_state *s;
  58503. + charf *buf; /* input block, or NULL if too old */
  58504. + ulg stored_len; /* length of input block */
  58505. + int eof; /* true if this is the last block for a file */
  58506. +{
  58507. + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  58508. + int max_blindex = 0; /* index of last bit length code of non zero freq */
  58509. +
  58510. + /* Build the Huffman trees unless a stored block is forced */
  58511. + if (s->level > 0) {
  58512. +
  58513. + /* Check if the file is ascii or binary */
  58514. + if (s->data_type == Z_UNKNOWN) set_data_type(s);
  58515. +
  58516. + /* Construct the literal and distance trees */
  58517. + build_tree(s, (tree_desc *)(&(s->l_desc)));
  58518. + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  58519. + s->static_len));
  58520. +
  58521. + build_tree(s, (tree_desc *)(&(s->d_desc)));
  58522. + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  58523. + s->static_len));
  58524. + /* At this point, opt_len and static_len are the total bit lengths of
  58525. + * the compressed block data, excluding the tree representations.
  58526. + */
  58527. +
  58528. + /* Build the bit length tree for the above two trees, and get the index
  58529. + * in bl_order of the last bit length code to send.
  58530. + */
  58531. + max_blindex = build_bl_tree(s);
  58532. +
  58533. + /* Determine the best encoding. Compute first the block length in bytes*/
  58534. + opt_lenb = (s->opt_len+3+7)>>3;
  58535. + static_lenb = (s->static_len+3+7)>>3;
  58536. +
  58537. + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  58538. + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  58539. + s->last_lit));
  58540. +
  58541. + if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
  58542. +
  58543. + } else {
  58544. + Assert(buf != (char*)0, "lost buf");
  58545. + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  58546. + }
  58547. +
  58548. +#ifdef FORCE_STORED
  58549. + if (buf != (char*)0) { /* force stored block */
  58550. +#else
  58551. + if (stored_len+4 <= opt_lenb && buf != (char*)0) {
  58552. + /* 4: two words for the lengths */
  58553. +#endif
  58554. + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  58555. + * Otherwise we can't have processed more than WSIZE input bytes since
  58556. + * the last block flush, because compression would have been
  58557. + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  58558. + * transform a block into a stored block.
  58559. + */
  58560. + _tr_stored_block(s, buf, stored_len, eof);
  58561. +
  58562. +#ifdef FORCE_STATIC
  58563. + } else if (static_lenb >= 0) { /* force static trees */
  58564. +#else
  58565. + } else if (static_lenb == opt_lenb) {
  58566. +#endif
  58567. + send_bits(s, (STATIC_TREES<<1)+eof, 3);
  58568. + compress_block(s, static_ltree, static_dtree);
  58569. +#ifdef DEBUG
  58570. + s->compressed_len += 3 + s->static_len;
  58571. +#endif
  58572. + } else {
  58573. + send_bits(s, (DYN_TREES<<1)+eof, 3);
  58574. + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
  58575. + max_blindex+1);
  58576. + compress_block(s, s->dyn_ltree, s->dyn_dtree);
  58577. +#ifdef DEBUG
  58578. + s->compressed_len += 3 + s->opt_len;
  58579. +#endif
  58580. + }
  58581. + Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  58582. + /* The above check is made mod 2^32, for files larger than 512 MB
  58583. + * and uLong implemented on 32 bits.
  58584. + */
  58585. + init_block(s);
  58586. +
  58587. + if (eof) {
  58588. + bi_windup(s);
  58589. +#ifdef DEBUG
  58590. + s->compressed_len += 7; /* align on byte boundary */
  58591. +#endif
  58592. + }
  58593. + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  58594. + s->compressed_len-7*eof));
  58595. +}
  58596. +
  58597. +/* ===========================================================================
  58598. + * Save the match info and tally the frequency counts. Return true if
  58599. + * the current block must be flushed.
  58600. + */
  58601. +int _tr_tally (s, dist, lc)
  58602. + deflate_state *s;
  58603. + unsigned dist; /* distance of matched string */
  58604. + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  58605. +{
  58606. + s->d_buf[s->last_lit] = (ush)dist;
  58607. + s->l_buf[s->last_lit++] = (uch)lc;
  58608. + if (dist == 0) {
  58609. + /* lc is the unmatched char */
  58610. + s->dyn_ltree[lc].Freq++;
  58611. + } else {
  58612. + s->matches++;
  58613. + /* Here, lc is the match length - MIN_MATCH */
  58614. + dist--; /* dist = match distance - 1 */
  58615. + Assert((ush)dist < (ush)MAX_DIST(s) &&
  58616. + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  58617. + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  58618. +
  58619. + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
  58620. + s->dyn_dtree[d_code(dist)].Freq++;
  58621. + }
  58622. +
  58623. +#ifdef TRUNCATE_BLOCK
  58624. + /* Try to guess if it is profitable to stop the current block here */
  58625. + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
  58626. + /* Compute an upper bound for the compressed length */
  58627. + ulg out_length = (ulg)s->last_lit*8L;
  58628. + ulg in_length = (ulg)((long)s->strstart - s->block_start);
  58629. + int dcode;
  58630. + for (dcode = 0; dcode < D_CODES; dcode++) {
  58631. + out_length += (ulg)s->dyn_dtree[dcode].Freq *
  58632. + (5L+extra_dbits[dcode]);
  58633. + }
  58634. + out_length >>= 3;
  58635. + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  58636. + s->last_lit, in_length, out_length,
  58637. + 100L - out_length*100L/in_length));
  58638. + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
  58639. + }
  58640. +#endif
  58641. + return (s->last_lit == s->lit_bufsize-1);
  58642. + /* We avoid equality with lit_bufsize because of wraparound at 64K
  58643. + * on 16 bit machines and because stored blocks are restricted to
  58644. + * 64K-1 bytes.
  58645. + */
  58646. +}
  58647. +
  58648. +/* ===========================================================================
  58649. + * Send the block data compressed using the given Huffman trees
  58650. + */
  58651. +local void compress_block(s, ltree, dtree)
  58652. + deflate_state *s;
  58653. + const ct_data *ltree; /* literal tree */
  58654. + const ct_data *dtree; /* distance tree */
  58655. +{
  58656. + unsigned dist; /* distance of matched string */
  58657. + int lc; /* match length or unmatched char (if dist == 0) */
  58658. + unsigned lx = 0; /* running index in l_buf */
  58659. + unsigned code; /* the code to send */
  58660. + int extra; /* number of extra bits to send */
  58661. +
  58662. + if (s->last_lit != 0) do {
  58663. + dist = s->d_buf[lx];
  58664. + lc = s->l_buf[lx++];
  58665. + if (dist == 0) {
  58666. + send_code(s, lc, ltree); /* send a literal byte */
  58667. + Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  58668. + } else {
  58669. + /* Here, lc is the match length - MIN_MATCH */
  58670. + code = _length_code[lc];
  58671. + send_code(s, code+LITERALS+1, ltree); /* send the length code */
  58672. + extra = extra_lbits[code];
  58673. + if (extra != 0) {
  58674. + lc -= base_length[code];
  58675. + send_bits(s, lc, extra); /* send the extra length bits */
  58676. + }
  58677. + dist--; /* dist is now the match distance - 1 */
  58678. + code = d_code(dist);
  58679. + Assert (code < D_CODES, "bad d_code");
  58680. +
  58681. + send_code(s, code, dtree); /* send the distance code */
  58682. + extra = extra_dbits[code];
  58683. + if (extra != 0) {
  58684. + dist -= base_dist[code];
  58685. + send_bits(s, dist, extra); /* send the extra distance bits */
  58686. + }
  58687. + } /* literal or match pair ? */
  58688. +
  58689. + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  58690. + Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
  58691. +
  58692. + } while (lx < s->last_lit);
  58693. +
  58694. + send_code(s, END_BLOCK, ltree);
  58695. + s->last_eob_len = ltree[END_BLOCK].Len;
  58696. +}
  58697. +
  58698. +/* ===========================================================================
  58699. + * Set the data type to ASCII or BINARY, using a crude approximation:
  58700. + * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  58701. + * IN assertion: the fields freq of dyn_ltree are set and the total of all
  58702. + * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  58703. + */
  58704. +local void set_data_type(s)
  58705. + deflate_state *s;
  58706. +{
  58707. + int n = 0;
  58708. + unsigned ascii_freq = 0;
  58709. + unsigned bin_freq = 0;
  58710. + while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
  58711. + while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
  58712. + while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
  58713. + s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
  58714. +}
  58715. +
  58716. +/* ===========================================================================
  58717. + * Reverse the first len bits of a code, using straightforward code (a faster
  58718. + * method would use a table)
  58719. + * IN assertion: 1 <= len <= 15
  58720. + */
  58721. +local unsigned bi_reverse(code, len)
  58722. + unsigned code; /* the value to invert */
  58723. + int len; /* its bit length */
  58724. +{
  58725. + register unsigned res = 0;
  58726. + do {
  58727. + res |= code & 1;
  58728. + code >>= 1, res <<= 1;
  58729. + } while (--len > 0);
  58730. + return res >> 1;
  58731. +}
  58732. +
  58733. +/* ===========================================================================
  58734. + * Flush the bit buffer, keeping at most 7 bits in it.
  58735. + */
  58736. +local void bi_flush(s)
  58737. + deflate_state *s;
  58738. +{
  58739. + if (s->bi_valid == 16) {
  58740. + put_short(s, s->bi_buf);
  58741. + s->bi_buf = 0;
  58742. + s->bi_valid = 0;
  58743. + } else if (s->bi_valid >= 8) {
  58744. + put_byte(s, (Byte)s->bi_buf);
  58745. + s->bi_buf >>= 8;
  58746. + s->bi_valid -= 8;
  58747. + }
  58748. +}
  58749. +
  58750. +/* ===========================================================================
  58751. + * Flush the bit buffer and align the output on a byte boundary
  58752. + */
  58753. +local void bi_windup(s)
  58754. + deflate_state *s;
  58755. +{
  58756. + if (s->bi_valid > 8) {
  58757. + put_short(s, s->bi_buf);
  58758. + } else if (s->bi_valid > 0) {
  58759. + put_byte(s, (Byte)s->bi_buf);
  58760. + }
  58761. + s->bi_buf = 0;
  58762. + s->bi_valid = 0;
  58763. +#ifdef DEBUG
  58764. + s->bits_sent = (s->bits_sent+7) & ~7;
  58765. +#endif
  58766. +}
  58767. +
  58768. +/* ===========================================================================
  58769. + * Copy a stored block, storing first the length and its
  58770. + * one's complement if requested.
  58771. + */
  58772. +local void copy_block(s, buf, len, header)
  58773. + deflate_state *s;
  58774. + charf *buf; /* the input data */
  58775. + unsigned len; /* its length */
  58776. + int header; /* true if block header must be written */
  58777. +{
  58778. + bi_windup(s); /* align on byte boundary */
  58779. + s->last_eob_len = 8; /* enough lookahead for inflate */
  58780. +
  58781. + if (header) {
  58782. + put_short(s, (ush)len);
  58783. + put_short(s, (ush)~len);
  58784. +#ifdef DEBUG
  58785. + s->bits_sent += 2*16;
  58786. +#endif
  58787. + }
  58788. +#ifdef DEBUG
  58789. + s->bits_sent += (ulg)len<<3;
  58790. +#endif
  58791. + while (len--) {
  58792. + put_byte(s, *buf++);
  58793. + }
  58794. +}
  58795. --- /dev/null Tue Mar 11 13:02:56 2003
  58796. +++ linux/net/ipsec/trees.h Mon Feb 9 13:51:03 2004
  58797. @@ -0,0 +1,128 @@
  58798. +/* header created automatically with -DGEN_TREES_H */
  58799. +
  58800. +local const ct_data static_ltree[L_CODES+2] = {
  58801. +{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
  58802. +{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
  58803. +{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
  58804. +{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
  58805. +{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
  58806. +{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
  58807. +{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
  58808. +{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
  58809. +{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
  58810. +{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
  58811. +{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
  58812. +{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
  58813. +{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
  58814. +{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
  58815. +{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
  58816. +{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
  58817. +{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
  58818. +{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
  58819. +{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
  58820. +{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
  58821. +{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
  58822. +{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
  58823. +{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
  58824. +{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
  58825. +{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
  58826. +{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
  58827. +{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
  58828. +{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
  58829. +{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
  58830. +{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
  58831. +{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
  58832. +{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
  58833. +{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
  58834. +{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
  58835. +{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
  58836. +{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
  58837. +{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
  58838. +{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
  58839. +{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
  58840. +{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
  58841. +{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
  58842. +{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
  58843. +{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
  58844. +{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
  58845. +{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
  58846. +{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
  58847. +{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
  58848. +{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
  58849. +{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
  58850. +{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
  58851. +{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
  58852. +{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
  58853. +{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
  58854. +{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
  58855. +{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
  58856. +{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
  58857. +{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
  58858. +{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
  58859. +};
  58860. +
  58861. +local const ct_data static_dtree[D_CODES] = {
  58862. +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
  58863. +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
  58864. +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
  58865. +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
  58866. +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
  58867. +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
  58868. +};
  58869. +
  58870. +const uch _dist_code[DIST_CODE_LEN] = {
  58871. + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
  58872. + 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
  58873. +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  58874. +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  58875. +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
  58876. +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  58877. +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58878. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58879. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58880. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
  58881. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58882. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58883. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
  58884. +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
  58885. +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58886. +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  58887. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  58888. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
  58889. +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  58890. +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58891. +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58892. +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58893. +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58894. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58895. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58896. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
  58897. +};
  58898. +
  58899. +const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
  58900. + 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  58901. +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  58902. +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  58903. +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  58904. +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  58905. +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  58906. +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58907. +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58908. +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  58909. +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  58910. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  58911. +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  58912. +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
  58913. +};
  58914. +
  58915. +local const int base_length[LENGTH_CODES] = {
  58916. +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
  58917. +64, 80, 96, 112, 128, 160, 192, 224, 0
  58918. +};
  58919. +
  58920. +local const int base_dist[D_CODES] = {
  58921. + 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
  58922. + 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
  58923. + 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
  58924. +};
  58925. +
  58926. --- /dev/null Tue Mar 11 13:02:56 2003
  58927. +++ linux/net/ipsec/ultoa.c Mon Feb 9 13:51:03 2004
  58928. @@ -0,0 +1,66 @@
  58929. +/*
  58930. + * convert unsigned long to ASCII
  58931. + * Copyright (C) 1998, 1999 Henry Spencer.
  58932. + *
  58933. + * This library is free software; you can redistribute it and/or modify it
  58934. + * under the terms of the GNU Library General Public License as published by
  58935. + * the Free Software Foundation; either version 2 of the License, or (at your
  58936. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  58937. + *
  58938. + * This library is distributed in the hope that it will be useful, but
  58939. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  58940. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  58941. + * License for more details.
  58942. + *
  58943. + * RCSID $Id: ultoa.c,v 1.10 2004/07/10 07:48:37 mcr Exp $
  58944. + */
  58945. +#include "openswan.h"
  58946. +
  58947. +/*
  58948. + - ultoa - convert unsigned long to decimal ASCII
  58949. + */
  58950. +size_t /* length required for full conversion */
  58951. +ultoa(n, base, dst, dstlen)
  58952. +unsigned long n;
  58953. +int base;
  58954. +char *dst; /* need not be valid if dstlen is 0 */
  58955. +size_t dstlen;
  58956. +{
  58957. + char buf[3*sizeof(unsigned long) + 1];
  58958. + char *bufend = buf + sizeof(buf);
  58959. + size_t len;
  58960. + char *p;
  58961. + static char hex[] = "0123456789abcdef";
  58962. +
  58963. + p = bufend;
  58964. + *--p = '\0';
  58965. + if (base == 10) {
  58966. + do {
  58967. + *--p = n%10 + '0';
  58968. + n /= 10;
  58969. + } while (n != 0);
  58970. + } else if (base == 16) {
  58971. + do {
  58972. + *--p = hex[n&0xf];
  58973. + n >>= 4;
  58974. + } while (n != 0);
  58975. + *--p = 'x';
  58976. + *--p = '0';
  58977. + } else if (base == 8) {
  58978. + do {
  58979. + *--p = (n&07) + '0';
  58980. + n >>= 3;
  58981. + } while (n != 0);
  58982. + *--p = '0';
  58983. + } else
  58984. + *--p = '?';
  58985. +
  58986. + len = bufend - p;
  58987. +
  58988. + if (dstlen > 0) {
  58989. + if (len > dstlen)
  58990. + *(p + dstlen - 1) = '\0';
  58991. + strcpy(dst, p);
  58992. + }
  58993. + return len;
  58994. +}
  58995. --- /dev/null Tue Mar 11 13:02:56 2003
  58996. +++ linux/net/ipsec/ultot.c Mon Feb 9 13:51:03 2004
  58997. @@ -0,0 +1,82 @@
  58998. +/*
  58999. + * convert unsigned long to text
  59000. + * Copyright (C) 2000 Henry Spencer.
  59001. + *
  59002. + * This library is free software; you can redistribute it and/or modify it
  59003. + * under the terms of the GNU Library General Public License as published by
  59004. + * the Free Software Foundation; either version 2 of the License, or (at your
  59005. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  59006. + *
  59007. + * This library is distributed in the hope that it will be useful, but
  59008. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  59009. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  59010. + * License for more details.
  59011. + *
  59012. + * RCSID $Id: ultot.c,v 1.5 2004/07/10 07:48:37 mcr Exp $
  59013. + */
  59014. +#include "openswan.h"
  59015. +
  59016. +/*
  59017. + - ultot - convert unsigned long to text
  59018. + */
  59019. +size_t /* length required for full conversion */
  59020. +ultot(n, base, dst, dstlen)
  59021. +unsigned long n;
  59022. +int base;
  59023. +char *dst; /* need not be valid if dstlen is 0 */
  59024. +size_t dstlen;
  59025. +{
  59026. + char buf[3*sizeof(unsigned long) + 1];
  59027. + char *bufend = buf + sizeof(buf);
  59028. + size_t len;
  59029. + char *p;
  59030. + static char hex[] = "0123456789abcdef";
  59031. +# define HEX32 (32/4)
  59032. +
  59033. + p = bufend;
  59034. + *--p = '\0';
  59035. + switch (base) {
  59036. + case 10:
  59037. + case 'd':
  59038. + do {
  59039. + *--p = n%10 + '0';
  59040. + n /= 10;
  59041. + } while (n != 0);
  59042. + break;
  59043. + case 16:
  59044. + case 17:
  59045. + case 'x':
  59046. + do {
  59047. + *--p = hex[n&0xf];
  59048. + n >>= 4;
  59049. + } while (n != 0);
  59050. + if (base == 17)
  59051. + while (bufend - p < HEX32 + 1)
  59052. + *--p = '0';
  59053. + if (base == 'x') {
  59054. + *--p = 'x';
  59055. + *--p = '0';
  59056. + }
  59057. + break;
  59058. + case 8:
  59059. + case 'o':
  59060. + do {
  59061. + *--p = (n&07) + '0';
  59062. + n >>= 3;
  59063. + } while (n != 0);
  59064. + if (base == 'o')
  59065. + *--p = '0';
  59066. + break;
  59067. + default:
  59068. + return 0;
  59069. + break;
  59070. + }
  59071. +
  59072. + len = bufend - p;
  59073. + if (dstlen > 0) {
  59074. + if (len > dstlen)
  59075. + *(p + dstlen - 1) = '\0';
  59076. + strcpy(dst, p);
  59077. + }
  59078. + return len;
  59079. +}
  59080. --- /dev/null Tue Mar 11 13:02:56 2003
  59081. +++ linux/net/ipsec/version.c Mon Feb 9 13:51:03 2004
  59082. @@ -0,0 +1,44 @@
  59083. +/*
  59084. + * return IPsec version information
  59085. + * Copyright (C) 2001 Henry Spencer.
  59086. + *
  59087. + * This library is free software; you can redistribute it and/or modify it
  59088. + * under the terms of the GNU Library General Public License as published by
  59089. + * the Free Software Foundation; either version 2 of the License, or (at your
  59090. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  59091. + *
  59092. + * This library is distributed in the hope that it will be useful, but
  59093. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  59094. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  59095. + * License for more details.
  59096. + *
  59097. + * RCSID $Id: version.in.c,v 1.2 2004/04/14 05:09:46 ken Exp $
  59098. + */
  59099. +
  59100. +#ifdef __KERNEL__
  59101. +#include <linux/netdevice.h>
  59102. +#endif
  59103. +
  59104. +#include "openswan.h"
  59105. +
  59106. +#define V "2.4.8" /* substituted in by Makefile */
  59107. +static const char openswan_number[] = V;
  59108. +static const char openswan_string[] = "Openswan " V;
  59109. +
  59110. +/*
  59111. + - ipsec_version_code - return IPsec version number/code, as string
  59112. + */
  59113. +const char *
  59114. +ipsec_version_code()
  59115. +{
  59116. + return openswan_number;
  59117. +}
  59118. +
  59119. +/*
  59120. + - ipsec_version_string - return full version string
  59121. + */
  59122. +const char *
  59123. +ipsec_version_string()
  59124. +{
  59125. + return openswan_string;
  59126. +}
  59127. --- /dev/null Tue Mar 11 13:02:56 2003
  59128. +++ linux/net/ipsec/zutil.c Mon Feb 9 13:51:03 2004
  59129. @@ -0,0 +1,227 @@
  59130. +/* zutil.c -- target dependent utility functions for the compression library
  59131. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  59132. + * For conditions of distribution and use, see copyright notice in zlib.h
  59133. + */
  59134. +
  59135. +/* @(#) $Id: zutil.c,v 1.5 2004/07/10 07:48:40 mcr Exp $ */
  59136. +
  59137. +#include <zlib/zutil.h>
  59138. +
  59139. +#define MY_ZCALLOC
  59140. +
  59141. +struct internal_state {int dummy;}; /* for buggy compilers */
  59142. +
  59143. +#ifndef STDC
  59144. +extern void exit OF((int));
  59145. +#endif
  59146. +
  59147. +const char *z_errmsg[10] = {
  59148. +"need dictionary", /* Z_NEED_DICT 2 */
  59149. +"stream end", /* Z_STREAM_END 1 */
  59150. +"", /* Z_OK 0 */
  59151. +"file error", /* Z_ERRNO (-1) */
  59152. +"stream error", /* Z_STREAM_ERROR (-2) */
  59153. +"data error", /* Z_DATA_ERROR (-3) */
  59154. +"insufficient memory", /* Z_MEM_ERROR (-4) */
  59155. +"buffer error", /* Z_BUF_ERROR (-5) */
  59156. +"incompatible version",/* Z_VERSION_ERROR (-6) */
  59157. +""};
  59158. +
  59159. +
  59160. +const char * ZEXPORT zlibVersion()
  59161. +{
  59162. + return ZLIB_VERSION;
  59163. +}
  59164. +
  59165. +#ifdef DEBUG
  59166. +
  59167. +# ifndef verbose
  59168. +# define verbose 0
  59169. +# endif
  59170. +int z_verbose = verbose;
  59171. +
  59172. +void z_error (m)
  59173. + char *m;
  59174. +{
  59175. + fprintf(stderr, "%s\n", m);
  59176. + exit(1);
  59177. +}
  59178. +#endif
  59179. +
  59180. +/* exported to allow conversion of error code to string for compress() and
  59181. + * uncompress()
  59182. + */
  59183. +const char * ZEXPORT zError(err)
  59184. + int err;
  59185. +{
  59186. + return ERR_MSG(err);
  59187. +}
  59188. +
  59189. +
  59190. +#ifndef HAVE_MEMCPY
  59191. +
  59192. +void zmemcpy(dest, source, len)
  59193. + Bytef* dest;
  59194. + const Bytef* source;
  59195. + uInt len;
  59196. +{
  59197. + if (len == 0) return;
  59198. + do {
  59199. + *dest++ = *source++; /* ??? to be unrolled */
  59200. + } while (--len != 0);
  59201. +}
  59202. +
  59203. +int zmemcmp(s1, s2, len)
  59204. + const Bytef* s1;
  59205. + const Bytef* s2;
  59206. + uInt len;
  59207. +{
  59208. + uInt j;
  59209. +
  59210. + for (j = 0; j < len; j++) {
  59211. + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  59212. + }
  59213. + return 0;
  59214. +}
  59215. +
  59216. +void zmemzero(dest, len)
  59217. + Bytef* dest;
  59218. + uInt len;
  59219. +{
  59220. + if (len == 0) return;
  59221. + do {
  59222. + *dest++ = 0; /* ??? to be unrolled */
  59223. + } while (--len != 0);
  59224. +}
  59225. +#endif
  59226. +
  59227. +#ifdef __TURBOC__
  59228. +#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
  59229. +/* Small and medium model in Turbo C are for now limited to near allocation
  59230. + * with reduced MAX_WBITS and MAX_MEM_LEVEL
  59231. + */
  59232. +# define MY_ZCALLOC
  59233. +
  59234. +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  59235. + * and farmalloc(64K) returns a pointer with an offset of 8, so we
  59236. + * must fix the pointer. Warning: the pointer must be put back to its
  59237. + * original form in order to free it, use zcfree().
  59238. + */
  59239. +
  59240. +#define MAX_PTR 10
  59241. +/* 10*64K = 640K */
  59242. +
  59243. +local int next_ptr = 0;
  59244. +
  59245. +typedef struct ptr_table_s {
  59246. + voidpf org_ptr;
  59247. + voidpf new_ptr;
  59248. +} ptr_table;
  59249. +
  59250. +local ptr_table table[MAX_PTR];
  59251. +/* This table is used to remember the original form of pointers
  59252. + * to large buffers (64K). Such pointers are normalized with a zero offset.
  59253. + * Since MSDOS is not a preemptive multitasking OS, this table is not
  59254. + * protected from concurrent access. This hack doesn't work anyway on
  59255. + * a protected system like OS/2. Use Microsoft C instead.
  59256. + */
  59257. +
  59258. +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  59259. +{
  59260. + voidpf buf = opaque; /* just to make some compilers happy */
  59261. + ulg bsize = (ulg)items*size;
  59262. +
  59263. + /* If we allocate less than 65520 bytes, we assume that farmalloc
  59264. + * will return a usable pointer which doesn't have to be normalized.
  59265. + */
  59266. + if (bsize < 65520L) {
  59267. + buf = farmalloc(bsize);
  59268. + if (*(ush*)&buf != 0) return buf;
  59269. + } else {
  59270. + buf = farmalloc(bsize + 16L);
  59271. + }
  59272. + if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  59273. + table[next_ptr].org_ptr = buf;
  59274. +
  59275. + /* Normalize the pointer to seg:0 */
  59276. + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  59277. + *(ush*)&buf = 0;
  59278. + table[next_ptr++].new_ptr = buf;
  59279. + return buf;
  59280. +}
  59281. +
  59282. +void zcfree (voidpf opaque, voidpf ptr)
  59283. +{
  59284. + int n;
  59285. + if (*(ush*)&ptr != 0) { /* object < 64K */
  59286. + farfree(ptr);
  59287. + return;
  59288. + }
  59289. + /* Find the original pointer */
  59290. + for (n = 0; n < next_ptr; n++) {
  59291. + if (ptr != table[n].new_ptr) continue;
  59292. +
  59293. + farfree(table[n].org_ptr);
  59294. + while (++n < next_ptr) {
  59295. + table[n-1] = table[n];
  59296. + }
  59297. + next_ptr--;
  59298. + return;
  59299. + }
  59300. + ptr = opaque; /* just to make some compilers happy */
  59301. + Assert(0, "zcfree: ptr not found");
  59302. +}
  59303. +#endif
  59304. +#endif /* __TURBOC__ */
  59305. +
  59306. +
  59307. +#if defined(M_I86) && !defined(__32BIT__)
  59308. +/* Microsoft C in 16-bit mode */
  59309. +
  59310. +# define MY_ZCALLOC
  59311. +
  59312. +#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  59313. +# define _halloc halloc
  59314. +# define _hfree hfree
  59315. +#endif
  59316. +
  59317. +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  59318. +{
  59319. + if (opaque) opaque = 0; /* to make compiler happy */
  59320. + return _halloc((long)items, size);
  59321. +}
  59322. +
  59323. +void zcfree (voidpf opaque, voidpf ptr)
  59324. +{
  59325. + if (opaque) opaque = 0; /* to make compiler happy */
  59326. + _hfree(ptr);
  59327. +}
  59328. +
  59329. +#endif /* MSC */
  59330. +
  59331. +
  59332. +#ifndef MY_ZCALLOC /* Any system without a special alloc function */
  59333. +
  59334. +#ifndef STDC
  59335. +extern voidp calloc OF((uInt items, uInt size));
  59336. +extern void free OF((voidpf ptr));
  59337. +#endif
  59338. +
  59339. +voidpf zcalloc (opaque, items, size)
  59340. + voidpf opaque;
  59341. + unsigned items;
  59342. + unsigned size;
  59343. +{
  59344. + if (opaque) items += size - size; /* make compiler happy */
  59345. + return (voidpf)calloc(items, size);
  59346. +}
  59347. +
  59348. +void zcfree (opaque, ptr)
  59349. + voidpf opaque;
  59350. + voidpf ptr;
  59351. +{
  59352. + free(ptr);
  59353. + if (opaque) return; /* make compiler happy */
  59354. +}
  59355. +
  59356. +#endif /* MY_ZCALLOC */
  59357. --- swan26/net/ipv4/af_inet.c.orig Wed Jun 16 01:18:58 2004
  59358. +++ swan26/net/ipv4/af_inet.c Fri Aug 13 23:09:27 2004
  59359. @@ -1169,6 +1169,18 @@
  59360. #if defined(CONFIG_IP_MROUTE)
  59361. ip_mr_init();
  59362. #endif
  59363. +
  59364. +#if defined(CONFIG_KLIPS)
  59365. + {
  59366. + extern int ipsec_klips_init(void);
  59367. + /*
  59368. + * Initialise AF_INET ESP and AH protocol support including
  59369. + * e-routing and SA tables
  59370. + */
  59371. + ipsec_klips_init();
  59372. + }
  59373. +#endif /* CONFIG_IPSEC */
  59374. +
  59375. /*
  59376. * Initialise per-cpu ipv4 mibs
  59377. */
  59378. --- /dev/null Fri May 10 13:59:54 2002
  59379. +++ linux/net/ipsec/Makefile.ver Sun Jul 28 22:10:40 2002
  59380. @@ -0,0 +1 @@
  59381. +IPSECVERSION=2.4.8