if_spppsubr.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418
  1. /*
  2. * Synchronous PPP/Cisco/Frame Relay link level subroutines.
  3. * Keepalive protocol implemented in both Cisco and PPP modes.
  4. */
  5. /*-
  6. * Copyright (C) 1994-2000 Cronyx Engineering.
  7. * Author: Serge Vakulenko, <vak@cronyx.ru>
  8. *
  9. * Heavily revamped to conform to RFC 1661.
  10. * Copyright (C) 1997, 2001 Joerg Wunsch.
  11. *
  12. * This software is distributed with NO WARRANTIES, not even the implied
  13. * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * Authors grant any other persons or organisations permission to use
  16. * or modify this software as long as this message is kept with the software,
  17. * all derivative works or modified versions.
  18. *
  19. * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
  20. *
  21. * $FreeBSD$
  22. */
  23. #include <sys/param.h>
  24. #include "opt_inet.h"
  25. #include "opt_inet6.h"
  26. #include <sys/systm.h>
  27. #include <sys/kernel.h>
  28. #include <sys/lock.h>
  29. #include <sys/module.h>
  30. #include <sys/rmlock.h>
  31. #include <sys/sockio.h>
  32. #include <sys/socket.h>
  33. #include <sys/syslog.h>
  34. #include <sys/random.h>
  35. #include <sys/malloc.h>
  36. #include <sys/mbuf.h>
  37. #include <sys/md5.h>
  38. #include <net/if.h>
  39. #include <net/if_var.h>
  40. #include <net/netisr.h>
  41. #include <net/if_types.h>
  42. #include <net/route.h>
  43. #include <net/vnet.h>
  44. #include <netinet/in.h>
  45. #include <netinet/in_var.h>
  46. #include <netinet/in_systm.h>
  47. #include <netinet/ip.h>
  48. #include <net/slcompress.h>
  49. #include <machine/stdarg.h>
  50. #include <netinet/in_var.h>
  51. #ifdef INET
  52. #include <netinet/ip.h>
  53. #include <netinet/tcp.h>
  54. #endif
  55. #ifdef INET6
  56. #include <netinet6/scope6_var.h>
  57. #endif
  58. #include <netinet/if_ether.h>
  59. #include <net/if_sppp.h>
  60. #define IOCTL_CMD_T u_long
  61. #define MAXALIVECNT 3 /* max. alive packets */
  62. /*
  63. * Interface flags that can be set in an ifconfig command.
  64. *
  65. * Setting link0 will make the link passive, i.e. it will be marked
  66. * as being administrative openable, but won't be opened to begin
  67. * with. Incoming calls will be answered, or subsequent calls with
  68. * -link1 will cause the administrative open of the LCP layer.
  69. *
  70. * Setting link1 will cause the link to auto-dial only as packets
  71. * arrive to be sent.
  72. *
  73. * Setting IFF_DEBUG will syslog the option negotiation and state
  74. * transitions at level kern.debug. Note: all logs consistently look
  75. * like
  76. *
  77. * <if-name><unit>: <proto-name> <additional info...>
  78. *
  79. * with <if-name><unit> being something like "bppp0", and <proto-name>
  80. * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
  81. */
  82. #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
  83. #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
  84. #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
  85. #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
  86. #define PPP_UI 0x03 /* Unnumbered Information */
  87. #define PPP_IP 0x0021 /* Internet Protocol */
  88. #define PPP_ISO 0x0023 /* ISO OSI Protocol */
  89. #define PPP_XNS 0x0025 /* Xerox NS Protocol */
  90. #define PPP_IPX 0x002b /* Novell IPX Protocol */
  91. #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
  92. #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
  93. #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
  94. #define PPP_LCP 0xc021 /* Link Control Protocol */
  95. #define PPP_PAP 0xc023 /* Password Authentication Protocol */
  96. #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
  97. #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
  98. #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
  99. #define CONF_REQ 1 /* PPP configure request */
  100. #define CONF_ACK 2 /* PPP configure acknowledge */
  101. #define CONF_NAK 3 /* PPP configure negative ack */
  102. #define CONF_REJ 4 /* PPP configure reject */
  103. #define TERM_REQ 5 /* PPP terminate request */
  104. #define TERM_ACK 6 /* PPP terminate acknowledge */
  105. #define CODE_REJ 7 /* PPP code reject */
  106. #define PROTO_REJ 8 /* PPP protocol reject */
  107. #define ECHO_REQ 9 /* PPP echo request */
  108. #define ECHO_REPLY 10 /* PPP echo reply */
  109. #define DISC_REQ 11 /* PPP discard request */
  110. #define LCP_OPT_MRU 1 /* maximum receive unit */
  111. #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
  112. #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
  113. #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
  114. #define LCP_OPT_MAGIC 5 /* magic number */
  115. #define LCP_OPT_RESERVED 6 /* reserved */
  116. #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
  117. #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
  118. #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
  119. #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
  120. #define IPCP_OPT_ADDRESS 3 /* local IP address */
  121. #define IPV6CP_OPT_IFID 1 /* interface identifier */
  122. #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
  123. #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
  124. #define PAP_REQ 1 /* PAP name/password request */
  125. #define PAP_ACK 2 /* PAP acknowledge */
  126. #define PAP_NAK 3 /* PAP fail */
  127. #define CHAP_CHALLENGE 1 /* CHAP challenge request */
  128. #define CHAP_RESPONSE 2 /* CHAP challenge response */
  129. #define CHAP_SUCCESS 3 /* CHAP response ok */
  130. #define CHAP_FAILURE 4 /* CHAP response failed */
  131. #define CHAP_MD5 5 /* hash algorithm - MD5 */
  132. #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
  133. #define CISCO_UNICAST 0x0f /* Cisco unicast address */
  134. #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
  135. #define CISCO_ADDR_REQ 0 /* Cisco address request */
  136. #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
  137. #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
  138. /* states are named and numbered according to RFC 1661 */
  139. #define STATE_INITIAL 0
  140. #define STATE_STARTING 1
  141. #define STATE_CLOSED 2
  142. #define STATE_STOPPED 3
  143. #define STATE_CLOSING 4
  144. #define STATE_STOPPING 5
  145. #define STATE_REQ_SENT 6
  146. #define STATE_ACK_RCVD 7
  147. #define STATE_ACK_SENT 8
  148. #define STATE_OPENED 9
  149. static MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
  150. struct ppp_header {
  151. u_char address;
  152. u_char control;
  153. u_short protocol;
  154. } __packed;
  155. #define PPP_HEADER_LEN sizeof (struct ppp_header)
  156. struct lcp_header {
  157. u_char type;
  158. u_char ident;
  159. u_short len;
  160. } __packed;
  161. #define LCP_HEADER_LEN sizeof (struct lcp_header)
  162. struct cisco_packet {
  163. u_long type;
  164. u_long par1;
  165. u_long par2;
  166. u_short rel;
  167. u_short time0;
  168. u_short time1;
  169. } __packed;
  170. #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
  171. /*
  172. * We follow the spelling and capitalization of RFC 1661 here, to make
  173. * it easier comparing with the standard. Please refer to this RFC in
  174. * case you can't make sense out of these abbreviation; it will also
  175. * explain the semantics related to the various events and actions.
  176. */
  177. struct cp {
  178. u_short proto; /* PPP control protocol number */
  179. u_char protoidx; /* index into state table in struct sppp */
  180. u_char flags;
  181. #define CP_LCP 0x01 /* this is the LCP */
  182. #define CP_AUTH 0x02 /* this is an authentication protocol */
  183. #define CP_NCP 0x04 /* this is a NCP */
  184. #define CP_QUAL 0x08 /* this is a quality reporting protocol */
  185. const char *name; /* name of this control protocol */
  186. /* event handlers */
  187. void (*Up)(struct sppp *sp);
  188. void (*Down)(struct sppp *sp);
  189. void (*Open)(struct sppp *sp);
  190. void (*Close)(struct sppp *sp);
  191. void (*TO)(void *sp);
  192. int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
  193. void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
  194. void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
  195. /* actions */
  196. void (*tlu)(struct sppp *sp);
  197. void (*tld)(struct sppp *sp);
  198. void (*tls)(struct sppp *sp);
  199. void (*tlf)(struct sppp *sp);
  200. void (*scr)(struct sppp *sp);
  201. };
  202. #define SPP_FMT "%s: "
  203. #define SPP_ARGS(ifp) (ifp)->if_xname
  204. #define SPPP_LOCK(sp) mtx_lock (&(sp)->mtx)
  205. #define SPPP_UNLOCK(sp) mtx_unlock (&(sp)->mtx)
  206. #define SPPP_LOCK_ASSERT(sp) mtx_assert (&(sp)->mtx, MA_OWNED)
  207. #define SPPP_LOCK_OWNED(sp) mtx_owned (&(sp)->mtx)
  208. #ifdef INET
  209. /*
  210. * The following disgusting hack gets around the problem that IP TOS
  211. * can't be set yet. We want to put "interactive" traffic on a high
  212. * priority queue. To decide if traffic is interactive, we check that
  213. * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
  214. *
  215. * XXX is this really still necessary? - joerg -
  216. */
  217. static const u_short interactive_ports[8] = {
  218. 0, 513, 0, 0,
  219. 0, 21, 0, 23,
  220. };
  221. #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
  222. #endif
  223. /* almost every function needs these */
  224. #define STDDCL \
  225. struct ifnet *ifp = SP2IFP(sp); \
  226. int debug = ifp->if_flags & IFF_DEBUG
  227. static int sppp_output(struct ifnet *ifp, struct mbuf *m,
  228. const struct sockaddr *dst, struct route *ro);
  229. static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
  230. static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
  231. static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
  232. struct mbuf *m);
  233. static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
  234. u_char ident, u_short len, void *data);
  235. /* static void sppp_cp_timeout(void *arg); */
  236. static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
  237. int newstate);
  238. static void sppp_auth_send(const struct cp *cp,
  239. struct sppp *sp, unsigned int type, unsigned int id,
  240. ...);
  241. static void sppp_up_event(const struct cp *cp, struct sppp *sp);
  242. static void sppp_down_event(const struct cp *cp, struct sppp *sp);
  243. static void sppp_open_event(const struct cp *cp, struct sppp *sp);
  244. static void sppp_close_event(const struct cp *cp, struct sppp *sp);
  245. static void sppp_to_event(const struct cp *cp, struct sppp *sp);
  246. static void sppp_null(struct sppp *sp);
  247. static void sppp_pp_up(struct sppp *sp);
  248. static void sppp_pp_down(struct sppp *sp);
  249. static void sppp_lcp_init(struct sppp *sp);
  250. static void sppp_lcp_up(struct sppp *sp);
  251. static void sppp_lcp_down(struct sppp *sp);
  252. static void sppp_lcp_open(struct sppp *sp);
  253. static void sppp_lcp_close(struct sppp *sp);
  254. static void sppp_lcp_TO(void *sp);
  255. static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
  256. static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
  257. static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
  258. static void sppp_lcp_tlu(struct sppp *sp);
  259. static void sppp_lcp_tld(struct sppp *sp);
  260. static void sppp_lcp_tls(struct sppp *sp);
  261. static void sppp_lcp_tlf(struct sppp *sp);
  262. static void sppp_lcp_scr(struct sppp *sp);
  263. static void sppp_lcp_check_and_close(struct sppp *sp);
  264. static int sppp_ncp_check(struct sppp *sp);
  265. static void sppp_ipcp_init(struct sppp *sp);
  266. static void sppp_ipcp_up(struct sppp *sp);
  267. static void sppp_ipcp_down(struct sppp *sp);
  268. static void sppp_ipcp_open(struct sppp *sp);
  269. static void sppp_ipcp_close(struct sppp *sp);
  270. static void sppp_ipcp_TO(void *sp);
  271. static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
  272. static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
  273. static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
  274. static void sppp_ipcp_tlu(struct sppp *sp);
  275. static void sppp_ipcp_tld(struct sppp *sp);
  276. static void sppp_ipcp_tls(struct sppp *sp);
  277. static void sppp_ipcp_tlf(struct sppp *sp);
  278. static void sppp_ipcp_scr(struct sppp *sp);
  279. static void sppp_ipv6cp_init(struct sppp *sp);
  280. static void sppp_ipv6cp_up(struct sppp *sp);
  281. static void sppp_ipv6cp_down(struct sppp *sp);
  282. static void sppp_ipv6cp_open(struct sppp *sp);
  283. static void sppp_ipv6cp_close(struct sppp *sp);
  284. static void sppp_ipv6cp_TO(void *sp);
  285. static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
  286. static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
  287. static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
  288. static void sppp_ipv6cp_tlu(struct sppp *sp);
  289. static void sppp_ipv6cp_tld(struct sppp *sp);
  290. static void sppp_ipv6cp_tls(struct sppp *sp);
  291. static void sppp_ipv6cp_tlf(struct sppp *sp);
  292. static void sppp_ipv6cp_scr(struct sppp *sp);
  293. static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
  294. static void sppp_pap_init(struct sppp *sp);
  295. static void sppp_pap_open(struct sppp *sp);
  296. static void sppp_pap_close(struct sppp *sp);
  297. static void sppp_pap_TO(void *sp);
  298. static void sppp_pap_my_TO(void *sp);
  299. static void sppp_pap_tlu(struct sppp *sp);
  300. static void sppp_pap_tld(struct sppp *sp);
  301. static void sppp_pap_scr(struct sppp *sp);
  302. static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
  303. static void sppp_chap_init(struct sppp *sp);
  304. static void sppp_chap_open(struct sppp *sp);
  305. static void sppp_chap_close(struct sppp *sp);
  306. static void sppp_chap_TO(void *sp);
  307. static void sppp_chap_tlu(struct sppp *sp);
  308. static void sppp_chap_tld(struct sppp *sp);
  309. static void sppp_chap_scr(struct sppp *sp);
  310. static const char *sppp_auth_type_name(u_short proto, u_char type);
  311. static const char *sppp_cp_type_name(u_char type);
  312. #ifdef INET
  313. static const char *sppp_dotted_quad(u_long addr);
  314. static const char *sppp_ipcp_opt_name(u_char opt);
  315. #endif
  316. #ifdef INET6
  317. static const char *sppp_ipv6cp_opt_name(u_char opt);
  318. #endif
  319. static const char *sppp_lcp_opt_name(u_char opt);
  320. static const char *sppp_phase_name(enum ppp_phase phase);
  321. static const char *sppp_proto_name(u_short proto);
  322. static const char *sppp_state_name(int state);
  323. static int sppp_params(struct sppp *sp, u_long cmd, void *data);
  324. static int sppp_strnlen(u_char *p, int max);
  325. static void sppp_keepalive(void *dummy);
  326. static void sppp_phase_network(struct sppp *sp);
  327. static void sppp_print_bytes(const u_char *p, u_short len);
  328. static void sppp_print_string(const char *p, u_short len);
  329. static void sppp_qflush(struct ifqueue *ifq);
  330. #ifdef INET
  331. static void sppp_set_ip_addr(struct sppp *sp, u_long src);
  332. #endif
  333. #ifdef INET6
  334. static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
  335. struct in6_addr *dst, struct in6_addr *srcmask);
  336. #ifdef IPV6CP_MYIFID_DYN
  337. static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
  338. static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
  339. #endif
  340. static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
  341. #endif
  342. /* if_start () wrapper */
  343. static void sppp_ifstart (struct ifnet *ifp);
  344. /* our control protocol descriptors */
  345. static const struct cp lcp = {
  346. PPP_LCP, IDX_LCP, CP_LCP, "lcp",
  347. sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
  348. sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
  349. sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
  350. sppp_lcp_scr
  351. };
  352. static const struct cp ipcp = {
  353. PPP_IPCP, IDX_IPCP,
  354. #ifdef INET /* don't run IPCP if there's no IPv4 support */
  355. CP_NCP,
  356. #else
  357. 0,
  358. #endif
  359. "ipcp",
  360. sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
  361. sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
  362. sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
  363. sppp_ipcp_scr
  364. };
  365. static const struct cp ipv6cp = {
  366. PPP_IPV6CP, IDX_IPV6CP,
  367. #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
  368. CP_NCP,
  369. #else
  370. 0,
  371. #endif
  372. "ipv6cp",
  373. sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
  374. sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
  375. sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
  376. sppp_ipv6cp_scr
  377. };
  378. static const struct cp pap = {
  379. PPP_PAP, IDX_PAP, CP_AUTH, "pap",
  380. sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
  381. sppp_pap_TO, 0, 0, 0,
  382. sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
  383. sppp_pap_scr
  384. };
  385. static const struct cp chap = {
  386. PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
  387. sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
  388. sppp_chap_TO, 0, 0, 0,
  389. sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
  390. sppp_chap_scr
  391. };
  392. static const struct cp *cps[IDX_COUNT] = {
  393. &lcp, /* IDX_LCP */
  394. &ipcp, /* IDX_IPCP */
  395. &ipv6cp, /* IDX_IPV6CP */
  396. &pap, /* IDX_PAP */
  397. &chap, /* IDX_CHAP */
  398. };
  399. static void*
  400. sppp_alloc(u_char type, struct ifnet *ifp)
  401. {
  402. struct sppp *sp;
  403. sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
  404. sp->pp_ifp = ifp;
  405. return (sp);
  406. }
  407. static void
  408. sppp_free(void *com, u_char type)
  409. {
  410. free(com, M_SPPP);
  411. }
  412. static int
  413. sppp_modevent(module_t mod, int type, void *unused)
  414. {
  415. switch (type) {
  416. case MOD_LOAD:
  417. /*
  418. * XXX: should probably be IFT_SPPP, but it's fairly
  419. * harmless to allocate struct sppp's for non-sppp
  420. * interfaces.
  421. */
  422. if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
  423. break;
  424. case MOD_UNLOAD:
  425. /* if_deregister_com_alloc(IFT_PPP); */
  426. return EACCES;
  427. default:
  428. return EOPNOTSUPP;
  429. }
  430. return 0;
  431. }
  432. static moduledata_t spppmod = {
  433. "sppp",
  434. sppp_modevent,
  435. 0
  436. };
  437. MODULE_VERSION(sppp, 1);
  438. DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
  439. /*
  440. * Exported functions, comprising our interface to the lower layer.
  441. */
  442. /*
  443. * Process the received packet.
  444. */
  445. void
  446. sppp_input(struct ifnet *ifp, struct mbuf *m)
  447. {
  448. struct ppp_header *h;
  449. int isr = -1;
  450. struct sppp *sp = IFP2SP(ifp);
  451. int debug, do_account = 0;
  452. #ifdef INET
  453. int hlen, vjlen;
  454. u_char *iphdr;
  455. #endif
  456. SPPP_LOCK(sp);
  457. debug = ifp->if_flags & IFF_DEBUG;
  458. if (ifp->if_flags & IFF_UP)
  459. /* Count received bytes, add FCS and one flag */
  460. if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len + 3);
  461. if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
  462. /* Too small packet, drop it. */
  463. if (debug)
  464. log(LOG_DEBUG,
  465. SPP_FMT "input packet is too small, %d bytes\n",
  466. SPP_ARGS(ifp), m->m_pkthdr.len);
  467. drop:
  468. m_freem (m);
  469. SPPP_UNLOCK(sp);
  470. drop2:
  471. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  472. if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
  473. return;
  474. }
  475. if (sp->pp_mode == PP_FR) {
  476. sppp_fr_input (sp, m);
  477. SPPP_UNLOCK(sp);
  478. return;
  479. }
  480. /* Get PPP header. */
  481. h = mtod (m, struct ppp_header*);
  482. m_adj (m, PPP_HEADER_LEN);
  483. switch (h->address) {
  484. case PPP_ALLSTATIONS:
  485. if (h->control != PPP_UI)
  486. goto invalid;
  487. if (sp->pp_mode == IFF_CISCO) {
  488. if (debug)
  489. log(LOG_DEBUG,
  490. SPP_FMT "PPP packet in Cisco mode "
  491. "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
  492. SPP_ARGS(ifp),
  493. h->address, h->control, ntohs(h->protocol));
  494. goto drop;
  495. }
  496. switch (ntohs (h->protocol)) {
  497. default:
  498. if (debug)
  499. log(LOG_DEBUG,
  500. SPP_FMT "rejecting protocol "
  501. "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
  502. SPP_ARGS(ifp),
  503. h->address, h->control, ntohs(h->protocol));
  504. if (sp->state[IDX_LCP] == STATE_OPENED)
  505. sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
  506. ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
  507. &h->protocol);
  508. if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
  509. goto drop;
  510. case PPP_LCP:
  511. sppp_cp_input(&lcp, sp, m);
  512. m_freem (m);
  513. SPPP_UNLOCK(sp);
  514. return;
  515. case PPP_PAP:
  516. if (sp->pp_phase >= PHASE_AUTHENTICATE)
  517. sppp_pap_input(sp, m);
  518. m_freem (m);
  519. SPPP_UNLOCK(sp);
  520. return;
  521. case PPP_CHAP:
  522. if (sp->pp_phase >= PHASE_AUTHENTICATE)
  523. sppp_chap_input(sp, m);
  524. m_freem (m);
  525. SPPP_UNLOCK(sp);
  526. return;
  527. #ifdef INET
  528. case PPP_IPCP:
  529. if (sp->pp_phase == PHASE_NETWORK)
  530. sppp_cp_input(&ipcp, sp, m);
  531. m_freem (m);
  532. SPPP_UNLOCK(sp);
  533. return;
  534. case PPP_IP:
  535. if (sp->state[IDX_IPCP] == STATE_OPENED) {
  536. isr = NETISR_IP;
  537. }
  538. do_account++;
  539. break;
  540. case PPP_VJ_COMP:
  541. if (sp->state[IDX_IPCP] == STATE_OPENED) {
  542. if ((vjlen =
  543. sl_uncompress_tcp_core(mtod(m, u_char *),
  544. m->m_len, m->m_len,
  545. TYPE_COMPRESSED_TCP,
  546. sp->pp_comp,
  547. &iphdr, &hlen)) <= 0) {
  548. if (debug)
  549. log(LOG_INFO,
  550. SPP_FMT "VJ uncompress failed on compressed packet\n",
  551. SPP_ARGS(ifp));
  552. goto drop;
  553. }
  554. /*
  555. * Trim the VJ header off the packet, and prepend
  556. * the uncompressed IP header (which will usually
  557. * end up in two chained mbufs since there's not
  558. * enough leading space in the existing mbuf).
  559. */
  560. m_adj(m, vjlen);
  561. M_PREPEND(m, hlen, M_NOWAIT);
  562. if (m == NULL) {
  563. SPPP_UNLOCK(sp);
  564. goto drop2;
  565. }
  566. bcopy(iphdr, mtod(m, u_char *), hlen);
  567. isr = NETISR_IP;
  568. }
  569. do_account++;
  570. break;
  571. case PPP_VJ_UCOMP:
  572. if (sp->state[IDX_IPCP] == STATE_OPENED) {
  573. if (sl_uncompress_tcp_core(mtod(m, u_char *),
  574. m->m_len, m->m_len,
  575. TYPE_UNCOMPRESSED_TCP,
  576. sp->pp_comp,
  577. &iphdr, &hlen) != 0) {
  578. if (debug)
  579. log(LOG_INFO,
  580. SPP_FMT "VJ uncompress failed on uncompressed packet\n",
  581. SPP_ARGS(ifp));
  582. goto drop;
  583. }
  584. isr = NETISR_IP;
  585. }
  586. do_account++;
  587. break;
  588. #endif
  589. #ifdef INET6
  590. case PPP_IPV6CP:
  591. if (sp->pp_phase == PHASE_NETWORK)
  592. sppp_cp_input(&ipv6cp, sp, m);
  593. m_freem (m);
  594. SPPP_UNLOCK(sp);
  595. return;
  596. case PPP_IPV6:
  597. if (sp->state[IDX_IPV6CP] == STATE_OPENED)
  598. isr = NETISR_IPV6;
  599. do_account++;
  600. break;
  601. #endif
  602. }
  603. break;
  604. case CISCO_MULTICAST:
  605. case CISCO_UNICAST:
  606. /* Don't check the control field here (RFC 1547). */
  607. if (sp->pp_mode != IFF_CISCO) {
  608. if (debug)
  609. log(LOG_DEBUG,
  610. SPP_FMT "Cisco packet in PPP mode "
  611. "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
  612. SPP_ARGS(ifp),
  613. h->address, h->control, ntohs(h->protocol));
  614. goto drop;
  615. }
  616. switch (ntohs (h->protocol)) {
  617. default:
  618. if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
  619. goto invalid;
  620. case CISCO_KEEPALIVE:
  621. sppp_cisco_input (sp, m);
  622. m_freem (m);
  623. SPPP_UNLOCK(sp);
  624. return;
  625. #ifdef INET
  626. case ETHERTYPE_IP:
  627. isr = NETISR_IP;
  628. do_account++;
  629. break;
  630. #endif
  631. #ifdef INET6
  632. case ETHERTYPE_IPV6:
  633. isr = NETISR_IPV6;
  634. do_account++;
  635. break;
  636. #endif
  637. }
  638. break;
  639. default: /* Invalid PPP packet. */
  640. invalid:
  641. if (debug)
  642. log(LOG_DEBUG,
  643. SPP_FMT "invalid input packet "
  644. "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
  645. SPP_ARGS(ifp),
  646. h->address, h->control, ntohs(h->protocol));
  647. goto drop;
  648. }
  649. if (! (ifp->if_flags & IFF_UP) || isr == -1)
  650. goto drop;
  651. SPPP_UNLOCK(sp);
  652. M_SETFIB(m, ifp->if_fib);
  653. /* Check queue. */
  654. if (netisr_queue(isr, m)) { /* (0) on success. */
  655. if (debug)
  656. log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
  657. SPP_ARGS(ifp));
  658. goto drop2;
  659. }
  660. if (do_account)
  661. /*
  662. * Do only account for network packets, not for control
  663. * packets. This is used by some subsystems to detect
  664. * idle lines.
  665. */
  666. sp->pp_last_recv = time_uptime;
  667. }
  668. static void
  669. sppp_ifstart_sched(void *dummy)
  670. {
  671. struct sppp *sp = dummy;
  672. sp->if_start(SP2IFP(sp));
  673. }
  674. /* if_start () wrapper function. We use it to schedule real if_start () for
  675. * execution. We can't call it directly
  676. */
  677. static void
  678. sppp_ifstart(struct ifnet *ifp)
  679. {
  680. struct sppp *sp = IFP2SP(ifp);
  681. if (SPPP_LOCK_OWNED(sp)) {
  682. if (callout_pending(&sp->ifstart_callout))
  683. return;
  684. callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
  685. (void *)sp);
  686. } else {
  687. sp->if_start(ifp);
  688. }
  689. }
  690. /*
  691. * Enqueue transmit packet.
  692. */
  693. static int
  694. sppp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
  695. struct route *ro)
  696. {
  697. struct sppp *sp = IFP2SP(ifp);
  698. struct ppp_header *h;
  699. struct ifqueue *ifq = NULL;
  700. int error, rv = 0;
  701. #ifdef INET
  702. int ipproto = PPP_IP;
  703. #endif
  704. int debug = ifp->if_flags & IFF_DEBUG;
  705. SPPP_LOCK(sp);
  706. if (!(ifp->if_flags & IFF_UP) ||
  707. (!(ifp->if_flags & IFF_AUTO) &&
  708. !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
  709. #ifdef INET6
  710. drop:
  711. #endif
  712. m_freem (m);
  713. SPPP_UNLOCK(sp);
  714. return (ENETDOWN);
  715. }
  716. if ((ifp->if_flags & IFF_AUTO) &&
  717. !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  718. #ifdef INET6
  719. /*
  720. * XXX
  721. *
  722. * Hack to prevent the initialization-time generated
  723. * IPv6 multicast packet to erroneously cause a
  724. * dialout event in case IPv6 has been
  725. * administratively disabled on that interface.
  726. */
  727. if (dst->sa_family == AF_INET6 &&
  728. !(sp->confflags & CONF_ENABLE_IPV6))
  729. goto drop;
  730. #endif
  731. /*
  732. * Interface is not yet running, but auto-dial. Need
  733. * to start LCP for it.
  734. */
  735. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  736. lcp.Open(sp);
  737. }
  738. #ifdef INET
  739. if (dst->sa_family == AF_INET) {
  740. /* XXX Check mbuf length here? */
  741. struct ip *ip = mtod (m, struct ip*);
  742. struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
  743. /*
  744. * When using dynamic local IP address assignment by using
  745. * 0.0.0.0 as a local address, the first TCP session will
  746. * not connect because the local TCP checksum is computed
  747. * using 0.0.0.0 which will later become our real IP address
  748. * so the TCP checksum computed at the remote end will
  749. * become invalid. So we
  750. * - don't let packets with src ip addr 0 thru
  751. * - we flag TCP packets with src ip 0 as an error
  752. */
  753. if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
  754. {
  755. m_freem(m);
  756. SPPP_UNLOCK(sp);
  757. if(ip->ip_p == IPPROTO_TCP)
  758. return(EADDRNOTAVAIL);
  759. else
  760. return(0);
  761. }
  762. /*
  763. * Put low delay, telnet, rlogin and ftp control packets
  764. * in front of the queue or let ALTQ take care.
  765. */
  766. if (ALTQ_IS_ENABLED(&ifp->if_snd))
  767. ;
  768. else if (_IF_QFULL(&sp->pp_fastq))
  769. ;
  770. else if (ip->ip_tos & IPTOS_LOWDELAY)
  771. ifq = &sp->pp_fastq;
  772. else if (m->m_len < sizeof *ip + sizeof *tcp)
  773. ;
  774. else if (ip->ip_p != IPPROTO_TCP)
  775. ;
  776. else if (INTERACTIVE (ntohs (tcp->th_sport)))
  777. ifq = &sp->pp_fastq;
  778. else if (INTERACTIVE (ntohs (tcp->th_dport)))
  779. ifq = &sp->pp_fastq;
  780. /*
  781. * Do IP Header compression
  782. */
  783. if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
  784. (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
  785. switch (sl_compress_tcp(m, ip, sp->pp_comp,
  786. sp->ipcp.compress_cid)) {
  787. case TYPE_COMPRESSED_TCP:
  788. ipproto = PPP_VJ_COMP;
  789. break;
  790. case TYPE_UNCOMPRESSED_TCP:
  791. ipproto = PPP_VJ_UCOMP;
  792. break;
  793. case TYPE_IP:
  794. ipproto = PPP_IP;
  795. break;
  796. default:
  797. m_freem(m);
  798. SPPP_UNLOCK(sp);
  799. return (EINVAL);
  800. }
  801. }
  802. #endif
  803. #ifdef INET6
  804. if (dst->sa_family == AF_INET6) {
  805. /* XXX do something tricky here? */
  806. }
  807. #endif
  808. if (sp->pp_mode == PP_FR) {
  809. /* Add frame relay header. */
  810. m = sppp_fr_header (sp, m, dst->sa_family);
  811. if (! m)
  812. goto nobufs;
  813. goto out;
  814. }
  815. /*
  816. * Prepend general data packet PPP header. For now, IP only.
  817. */
  818. M_PREPEND (m, PPP_HEADER_LEN, M_NOWAIT);
  819. if (! m) {
  820. nobufs: if (debug)
  821. log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
  822. SPP_ARGS(ifp));
  823. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  824. SPPP_UNLOCK(sp);
  825. return (ENOBUFS);
  826. }
  827. /*
  828. * May want to check size of packet
  829. * (albeit due to the implementation it's always enough)
  830. */
  831. h = mtod (m, struct ppp_header*);
  832. if (sp->pp_mode == IFF_CISCO) {
  833. h->address = CISCO_UNICAST; /* unicast address */
  834. h->control = 0;
  835. } else {
  836. h->address = PPP_ALLSTATIONS; /* broadcast address */
  837. h->control = PPP_UI; /* Unnumbered Info */
  838. }
  839. switch (dst->sa_family) {
  840. #ifdef INET
  841. case AF_INET: /* Internet Protocol */
  842. if (sp->pp_mode == IFF_CISCO)
  843. h->protocol = htons (ETHERTYPE_IP);
  844. else {
  845. /*
  846. * Don't choke with an ENETDOWN early. It's
  847. * possible that we just started dialing out,
  848. * so don't drop the packet immediately. If
  849. * we notice that we run out of buffer space
  850. * below, we will however remember that we are
  851. * not ready to carry IP packets, and return
  852. * ENETDOWN, as opposed to ENOBUFS.
  853. */
  854. h->protocol = htons(ipproto);
  855. if (sp->state[IDX_IPCP] != STATE_OPENED)
  856. rv = ENETDOWN;
  857. }
  858. break;
  859. #endif
  860. #ifdef INET6
  861. case AF_INET6: /* Internet Protocol */
  862. if (sp->pp_mode == IFF_CISCO)
  863. h->protocol = htons (ETHERTYPE_IPV6);
  864. else {
  865. /*
  866. * Don't choke with an ENETDOWN early. It's
  867. * possible that we just started dialing out,
  868. * so don't drop the packet immediately. If
  869. * we notice that we run out of buffer space
  870. * below, we will however remember that we are
  871. * not ready to carry IP packets, and return
  872. * ENETDOWN, as opposed to ENOBUFS.
  873. */
  874. h->protocol = htons(PPP_IPV6);
  875. if (sp->state[IDX_IPV6CP] != STATE_OPENED)
  876. rv = ENETDOWN;
  877. }
  878. break;
  879. #endif
  880. default:
  881. m_freem (m);
  882. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  883. SPPP_UNLOCK(sp);
  884. return (EAFNOSUPPORT);
  885. }
  886. /*
  887. * Queue message on interface, and start output if interface
  888. * not yet active.
  889. */
  890. out:
  891. if (ifq != NULL)
  892. error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
  893. else
  894. IFQ_HANDOFF_ADJ(ifp, m, 3, error);
  895. if (error) {
  896. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  897. SPPP_UNLOCK(sp);
  898. return (rv? rv: ENOBUFS);
  899. }
  900. SPPP_UNLOCK(sp);
  901. /*
  902. * Unlike in sppp_input(), we can always bump the timestamp
  903. * here since sppp_output() is only called on behalf of
  904. * network-layer traffic; control-layer traffic is handled
  905. * by sppp_cp_send().
  906. */
  907. sp->pp_last_sent = time_uptime;
  908. return (0);
  909. }
  910. void
  911. sppp_attach(struct ifnet *ifp)
  912. {
  913. struct sppp *sp = IFP2SP(ifp);
  914. /* Initialize mtx lock */
  915. mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
  916. /* Initialize keepalive handler. */
  917. callout_init(&sp->keepalive_callout, 1);
  918. callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
  919. (void *)sp);
  920. ifp->if_mtu = PP_MTU;
  921. ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
  922. ifp->if_output = sppp_output;
  923. #if 0
  924. sp->pp_flags = PP_KEEPALIVE;
  925. #endif
  926. ifp->if_snd.ifq_maxlen = 32;
  927. sp->pp_fastq.ifq_maxlen = 32;
  928. sp->pp_cpq.ifq_maxlen = 20;
  929. sp->pp_loopcnt = 0;
  930. sp->pp_alivecnt = 0;
  931. bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
  932. bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
  933. sp->pp_phase = PHASE_DEAD;
  934. sp->pp_up = sppp_pp_up;
  935. sp->pp_down = sppp_pp_down;
  936. if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
  937. mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
  938. if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
  939. mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
  940. sp->pp_last_recv = sp->pp_last_sent = time_uptime;
  941. sp->confflags = 0;
  942. #ifdef INET
  943. sp->confflags |= CONF_ENABLE_VJ;
  944. #endif
  945. #ifdef INET6
  946. sp->confflags |= CONF_ENABLE_IPV6;
  947. #endif
  948. callout_init(&sp->ifstart_callout, 1);
  949. sp->if_start = ifp->if_start;
  950. ifp->if_start = sppp_ifstart;
  951. sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
  952. sl_compress_init(sp->pp_comp, -1);
  953. sppp_lcp_init(sp);
  954. sppp_ipcp_init(sp);
  955. sppp_ipv6cp_init(sp);
  956. sppp_pap_init(sp);
  957. sppp_chap_init(sp);
  958. }
  959. void
  960. sppp_detach(struct ifnet *ifp)
  961. {
  962. struct sppp *sp = IFP2SP(ifp);
  963. int i;
  964. KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
  965. /* Stop keepalive handler. */
  966. callout_drain(&sp->keepalive_callout);
  967. for (i = 0; i < IDX_COUNT; i++) {
  968. callout_drain(&sp->ch[i]);
  969. }
  970. callout_drain(&sp->pap_my_to_ch);
  971. mtx_destroy(&sp->pp_cpq.ifq_mtx);
  972. mtx_destroy(&sp->pp_fastq.ifq_mtx);
  973. mtx_destroy(&sp->mtx);
  974. }
  975. /*
  976. * Flush the interface output queue.
  977. */
  978. static void
  979. sppp_flush_unlocked(struct ifnet *ifp)
  980. {
  981. struct sppp *sp = IFP2SP(ifp);
  982. sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
  983. sppp_qflush (&sp->pp_fastq);
  984. sppp_qflush (&sp->pp_cpq);
  985. }
  986. void
  987. sppp_flush(struct ifnet *ifp)
  988. {
  989. struct sppp *sp = IFP2SP(ifp);
  990. SPPP_LOCK(sp);
  991. sppp_flush_unlocked (ifp);
  992. SPPP_UNLOCK(sp);
  993. }
  994. /*
  995. * Check if the output queue is empty.
  996. */
  997. int
  998. sppp_isempty(struct ifnet *ifp)
  999. {
  1000. struct sppp *sp = IFP2SP(ifp);
  1001. int empty;
  1002. SPPP_LOCK(sp);
  1003. empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
  1004. !SP2IFP(sp)->if_snd.ifq_head;
  1005. SPPP_UNLOCK(sp);
  1006. return (empty);
  1007. }
  1008. /*
  1009. * Get next packet to send.
  1010. */
  1011. struct mbuf *
  1012. sppp_dequeue(struct ifnet *ifp)
  1013. {
  1014. struct sppp *sp = IFP2SP(ifp);
  1015. struct mbuf *m;
  1016. SPPP_LOCK(sp);
  1017. /*
  1018. * Process only the control protocol queue until we have at
  1019. * least one NCP open.
  1020. *
  1021. * Do always serve all three queues in Cisco mode.
  1022. */
  1023. IF_DEQUEUE(&sp->pp_cpq, m);
  1024. if (m == NULL &&
  1025. (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
  1026. sp->pp_mode == PP_FR)) {
  1027. IF_DEQUEUE(&sp->pp_fastq, m);
  1028. if (m == NULL)
  1029. IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
  1030. }
  1031. SPPP_UNLOCK(sp);
  1032. return m;
  1033. }
  1034. /*
  1035. * Pick the next packet, do not remove it from the queue.
  1036. */
  1037. struct mbuf *
  1038. sppp_pick(struct ifnet *ifp)
  1039. {
  1040. struct sppp *sp = IFP2SP(ifp);
  1041. struct mbuf *m;
  1042. SPPP_LOCK(sp);
  1043. m = sp->pp_cpq.ifq_head;
  1044. if (m == NULL &&
  1045. (sp->pp_phase == PHASE_NETWORK ||
  1046. sp->pp_mode == IFF_CISCO ||
  1047. sp->pp_mode == PP_FR))
  1048. if ((m = sp->pp_fastq.ifq_head) == NULL)
  1049. m = SP2IFP(sp)->if_snd.ifq_head;
  1050. SPPP_UNLOCK(sp);
  1051. return (m);
  1052. }
  1053. /*
  1054. * Process an ioctl request. Called on low priority level.
  1055. */
  1056. int
  1057. sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
  1058. {
  1059. struct ifreq *ifr = (struct ifreq*) data;
  1060. struct sppp *sp = IFP2SP(ifp);
  1061. int rv, going_up, going_down, newmode;
  1062. SPPP_LOCK(sp);
  1063. rv = 0;
  1064. switch (cmd) {
  1065. case SIOCAIFADDR:
  1066. break;
  1067. case SIOCSIFADDR:
  1068. /* set the interface "up" when assigning an IP address */
  1069. ifp->if_flags |= IFF_UP;
  1070. /* FALLTHROUGH */
  1071. case SIOCSIFFLAGS:
  1072. going_up = ifp->if_flags & IFF_UP &&
  1073. (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
  1074. going_down = (ifp->if_flags & IFF_UP) == 0 &&
  1075. ifp->if_drv_flags & IFF_DRV_RUNNING;
  1076. newmode = ifp->if_flags & IFF_PASSIVE;
  1077. if (!newmode)
  1078. newmode = ifp->if_flags & IFF_AUTO;
  1079. if (!newmode)
  1080. newmode = ifp->if_flags & IFF_CISCO;
  1081. ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
  1082. ifp->if_flags |= newmode;
  1083. if (!newmode)
  1084. newmode = sp->pp_flags & PP_FR;
  1085. if (newmode != sp->pp_mode) {
  1086. going_down = 1;
  1087. if (!going_up)
  1088. going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
  1089. }
  1090. if (going_down) {
  1091. if (sp->pp_mode != IFF_CISCO &&
  1092. sp->pp_mode != PP_FR)
  1093. lcp.Close(sp);
  1094. else if (sp->pp_tlf)
  1095. (sp->pp_tlf)(sp);
  1096. sppp_flush_unlocked(ifp);
  1097. ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  1098. sp->pp_mode = newmode;
  1099. }
  1100. if (going_up) {
  1101. if (sp->pp_mode != IFF_CISCO &&
  1102. sp->pp_mode != PP_FR)
  1103. lcp.Close(sp);
  1104. sp->pp_mode = newmode;
  1105. if (sp->pp_mode == 0) {
  1106. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  1107. lcp.Open(sp);
  1108. }
  1109. if ((sp->pp_mode == IFF_CISCO) ||
  1110. (sp->pp_mode == PP_FR)) {
  1111. if (sp->pp_tls)
  1112. (sp->pp_tls)(sp);
  1113. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  1114. }
  1115. }
  1116. break;
  1117. #ifdef SIOCSIFMTU
  1118. #ifndef ifr_mtu
  1119. #define ifr_mtu ifr_metric
  1120. #endif
  1121. case SIOCSIFMTU:
  1122. if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
  1123. return (EINVAL);
  1124. ifp->if_mtu = ifr->ifr_mtu;
  1125. break;
  1126. #endif
  1127. #ifdef SLIOCSETMTU
  1128. case SLIOCSETMTU:
  1129. if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
  1130. return (EINVAL);
  1131. ifp->if_mtu = *(short*)data;
  1132. break;
  1133. #endif
  1134. #ifdef SIOCGIFMTU
  1135. case SIOCGIFMTU:
  1136. ifr->ifr_mtu = ifp->if_mtu;
  1137. break;
  1138. #endif
  1139. #ifdef SLIOCGETMTU
  1140. case SLIOCGETMTU:
  1141. *(short*)data = ifp->if_mtu;
  1142. break;
  1143. #endif
  1144. case SIOCADDMULTI:
  1145. case SIOCDELMULTI:
  1146. break;
  1147. case SIOCGIFGENERIC:
  1148. case SIOCSIFGENERIC:
  1149. rv = sppp_params(sp, cmd, data);
  1150. break;
  1151. default:
  1152. rv = ENOTTY;
  1153. }
  1154. SPPP_UNLOCK(sp);
  1155. return rv;
  1156. }
  1157. /*
  1158. * Cisco framing implementation.
  1159. */
  1160. /*
  1161. * Handle incoming Cisco keepalive protocol packets.
  1162. */
  1163. static void
  1164. sppp_cisco_input(struct sppp *sp, struct mbuf *m)
  1165. {
  1166. STDDCL;
  1167. struct cisco_packet *h;
  1168. u_long me, mymask;
  1169. if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
  1170. if (debug)
  1171. log(LOG_DEBUG,
  1172. SPP_FMT "cisco invalid packet length: %d bytes\n",
  1173. SPP_ARGS(ifp), m->m_pkthdr.len);
  1174. return;
  1175. }
  1176. h = mtod (m, struct cisco_packet*);
  1177. if (debug)
  1178. log(LOG_DEBUG,
  1179. SPP_FMT "cisco input: %d bytes "
  1180. "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
  1181. SPP_ARGS(ifp), m->m_pkthdr.len,
  1182. (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
  1183. (u_int)h->time0, (u_int)h->time1);
  1184. switch (ntohl (h->type)) {
  1185. default:
  1186. if (debug)
  1187. log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
  1188. SPP_ARGS(ifp), (u_long)ntohl (h->type));
  1189. break;
  1190. case CISCO_ADDR_REPLY:
  1191. /* Reply on address request, ignore */
  1192. break;
  1193. case CISCO_KEEPALIVE_REQ:
  1194. sp->pp_alivecnt = 0;
  1195. sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
  1196. if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
  1197. /* Local and remote sequence numbers are equal.
  1198. * Probably, the line is in loopback mode. */
  1199. if (sp->pp_loopcnt >= MAXALIVECNT) {
  1200. printf (SPP_FMT "loopback\n",
  1201. SPP_ARGS(ifp));
  1202. sp->pp_loopcnt = 0;
  1203. if (ifp->if_flags & IFF_UP) {
  1204. if_down (ifp);
  1205. sppp_qflush (&sp->pp_cpq);
  1206. }
  1207. }
  1208. ++sp->pp_loopcnt;
  1209. /* Generate new local sequence number */
  1210. sp->pp_seq[IDX_LCP] = random();
  1211. break;
  1212. }
  1213. sp->pp_loopcnt = 0;
  1214. if (! (ifp->if_flags & IFF_UP) &&
  1215. (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  1216. if_up(ifp);
  1217. printf (SPP_FMT "up\n", SPP_ARGS(ifp));
  1218. }
  1219. break;
  1220. case CISCO_ADDR_REQ:
  1221. sppp_get_ip_addrs(sp, &me, 0, &mymask);
  1222. if (me != 0L)
  1223. sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
  1224. break;
  1225. }
  1226. }
  1227. /*
  1228. * Send Cisco keepalive packet.
  1229. */
  1230. static void
  1231. sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
  1232. {
  1233. STDDCL;
  1234. struct ppp_header *h;
  1235. struct cisco_packet *ch;
  1236. struct mbuf *m;
  1237. struct timeval tv;
  1238. getmicrouptime(&tv);
  1239. MGETHDR (m, M_NOWAIT, MT_DATA);
  1240. if (! m)
  1241. return;
  1242. m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
  1243. m->m_pkthdr.rcvif = 0;
  1244. h = mtod (m, struct ppp_header*);
  1245. h->address = CISCO_MULTICAST;
  1246. h->control = 0;
  1247. h->protocol = htons (CISCO_KEEPALIVE);
  1248. ch = (struct cisco_packet*) (h + 1);
  1249. ch->type = htonl (type);
  1250. ch->par1 = htonl (par1);
  1251. ch->par2 = htonl (par2);
  1252. ch->rel = -1;
  1253. ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
  1254. ch->time1 = htons ((u_short) tv.tv_sec);
  1255. if (debug)
  1256. log(LOG_DEBUG,
  1257. SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
  1258. SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
  1259. (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
  1260. if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
  1261. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  1262. }
  1263. /*
  1264. * PPP protocol implementation.
  1265. */
  1266. /*
  1267. * Send PPP control protocol packet.
  1268. */
  1269. static void
  1270. sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
  1271. u_char ident, u_short len, void *data)
  1272. {
  1273. STDDCL;
  1274. struct ppp_header *h;
  1275. struct lcp_header *lh;
  1276. struct mbuf *m;
  1277. if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
  1278. len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
  1279. MGETHDR (m, M_NOWAIT, MT_DATA);
  1280. if (! m)
  1281. return;
  1282. m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
  1283. m->m_pkthdr.rcvif = 0;
  1284. h = mtod (m, struct ppp_header*);
  1285. h->address = PPP_ALLSTATIONS; /* broadcast address */
  1286. h->control = PPP_UI; /* Unnumbered Info */
  1287. h->protocol = htons (proto); /* Link Control Protocol */
  1288. lh = (struct lcp_header*) (h + 1);
  1289. lh->type = type;
  1290. lh->ident = ident;
  1291. lh->len = htons (LCP_HEADER_LEN + len);
  1292. if (len)
  1293. bcopy (data, lh+1, len);
  1294. if (debug) {
  1295. log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
  1296. SPP_ARGS(ifp),
  1297. sppp_proto_name(proto),
  1298. sppp_cp_type_name (lh->type), lh->ident,
  1299. ntohs (lh->len));
  1300. sppp_print_bytes ((u_char*) (lh+1), len);
  1301. log(-1, ">\n");
  1302. }
  1303. if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
  1304. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  1305. }
  1306. /*
  1307. * Handle incoming PPP control protocol packets.
  1308. */
  1309. static void
  1310. sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
  1311. {
  1312. STDDCL;
  1313. struct lcp_header *h;
  1314. int len = m->m_pkthdr.len;
  1315. int rv;
  1316. u_char *p;
  1317. if (len < 4) {
  1318. if (debug)
  1319. log(LOG_DEBUG,
  1320. SPP_FMT "%s invalid packet length: %d bytes\n",
  1321. SPP_ARGS(ifp), cp->name, len);
  1322. return;
  1323. }
  1324. h = mtod (m, struct lcp_header*);
  1325. if (debug) {
  1326. log(LOG_DEBUG,
  1327. SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
  1328. SPP_ARGS(ifp), cp->name,
  1329. sppp_state_name(sp->state[cp->protoidx]),
  1330. sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
  1331. sppp_print_bytes ((u_char*) (h+1), len-4);
  1332. log(-1, ">\n");
  1333. }
  1334. if (len > ntohs (h->len))
  1335. len = ntohs (h->len);
  1336. p = (u_char *)(h + 1);
  1337. switch (h->type) {
  1338. case CONF_REQ:
  1339. if (len < 4) {
  1340. if (debug)
  1341. log(-1, SPP_FMT "%s invalid conf-req length %d\n",
  1342. SPP_ARGS(ifp), cp->name,
  1343. len);
  1344. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1345. break;
  1346. }
  1347. /* handle states where RCR doesn't get a SCA/SCN */
  1348. switch (sp->state[cp->protoidx]) {
  1349. case STATE_CLOSING:
  1350. case STATE_STOPPING:
  1351. return;
  1352. case STATE_CLOSED:
  1353. sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
  1354. 0, 0);
  1355. return;
  1356. }
  1357. rv = (cp->RCR)(sp, h, len);
  1358. switch (sp->state[cp->protoidx]) {
  1359. case STATE_OPENED:
  1360. (cp->tld)(sp);
  1361. (cp->scr)(sp);
  1362. /* FALLTHROUGH */
  1363. case STATE_ACK_SENT:
  1364. case STATE_REQ_SENT:
  1365. /*
  1366. * sppp_cp_change_state() have the side effect of
  1367. * restarting the timeouts. We want to avoid that
  1368. * if the state don't change, otherwise we won't
  1369. * ever timeout and resend a configuration request
  1370. * that got lost.
  1371. */
  1372. if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
  1373. STATE_REQ_SENT))
  1374. break;
  1375. sppp_cp_change_state(cp, sp, rv?
  1376. STATE_ACK_SENT: STATE_REQ_SENT);
  1377. break;
  1378. case STATE_STOPPED:
  1379. sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
  1380. (cp->scr)(sp);
  1381. sppp_cp_change_state(cp, sp, rv?
  1382. STATE_ACK_SENT: STATE_REQ_SENT);
  1383. break;
  1384. case STATE_ACK_RCVD:
  1385. if (rv) {
  1386. sppp_cp_change_state(cp, sp, STATE_OPENED);
  1387. if (debug)
  1388. log(LOG_DEBUG, SPP_FMT "%s tlu\n",
  1389. SPP_ARGS(ifp),
  1390. cp->name);
  1391. (cp->tlu)(sp);
  1392. } else
  1393. sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
  1394. break;
  1395. default:
  1396. printf(SPP_FMT "%s illegal %s in state %s\n",
  1397. SPP_ARGS(ifp), cp->name,
  1398. sppp_cp_type_name(h->type),
  1399. sppp_state_name(sp->state[cp->protoidx]));
  1400. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1401. }
  1402. break;
  1403. case CONF_ACK:
  1404. if (h->ident != sp->confid[cp->protoidx]) {
  1405. if (debug)
  1406. log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
  1407. SPP_ARGS(ifp), cp->name,
  1408. h->ident, sp->confid[cp->protoidx]);
  1409. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1410. break;
  1411. }
  1412. switch (sp->state[cp->protoidx]) {
  1413. case STATE_CLOSED:
  1414. case STATE_STOPPED:
  1415. sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
  1416. break;
  1417. case STATE_CLOSING:
  1418. case STATE_STOPPING:
  1419. break;
  1420. case STATE_REQ_SENT:
  1421. sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
  1422. sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
  1423. break;
  1424. case STATE_OPENED:
  1425. (cp->tld)(sp);
  1426. /* FALLTHROUGH */
  1427. case STATE_ACK_RCVD:
  1428. (cp->scr)(sp);
  1429. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1430. break;
  1431. case STATE_ACK_SENT:
  1432. sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
  1433. sppp_cp_change_state(cp, sp, STATE_OPENED);
  1434. if (debug)
  1435. log(LOG_DEBUG, SPP_FMT "%s tlu\n",
  1436. SPP_ARGS(ifp), cp->name);
  1437. (cp->tlu)(sp);
  1438. break;
  1439. default:
  1440. printf(SPP_FMT "%s illegal %s in state %s\n",
  1441. SPP_ARGS(ifp), cp->name,
  1442. sppp_cp_type_name(h->type),
  1443. sppp_state_name(sp->state[cp->protoidx]));
  1444. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1445. }
  1446. break;
  1447. case CONF_NAK:
  1448. case CONF_REJ:
  1449. if (h->ident != sp->confid[cp->protoidx]) {
  1450. if (debug)
  1451. log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
  1452. SPP_ARGS(ifp), cp->name,
  1453. h->ident, sp->confid[cp->protoidx]);
  1454. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1455. break;
  1456. }
  1457. if (h->type == CONF_NAK)
  1458. (cp->RCN_nak)(sp, h, len);
  1459. else /* CONF_REJ */
  1460. (cp->RCN_rej)(sp, h, len);
  1461. switch (sp->state[cp->protoidx]) {
  1462. case STATE_CLOSED:
  1463. case STATE_STOPPED:
  1464. sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
  1465. break;
  1466. case STATE_REQ_SENT:
  1467. case STATE_ACK_SENT:
  1468. sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
  1469. /*
  1470. * Slow things down a bit if we think we might be
  1471. * in loopback. Depend on the timeout to send the
  1472. * next configuration request.
  1473. */
  1474. if (sp->pp_loopcnt)
  1475. break;
  1476. (cp->scr)(sp);
  1477. break;
  1478. case STATE_OPENED:
  1479. (cp->tld)(sp);
  1480. /* FALLTHROUGH */
  1481. case STATE_ACK_RCVD:
  1482. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1483. (cp->scr)(sp);
  1484. break;
  1485. case STATE_CLOSING:
  1486. case STATE_STOPPING:
  1487. break;
  1488. default:
  1489. printf(SPP_FMT "%s illegal %s in state %s\n",
  1490. SPP_ARGS(ifp), cp->name,
  1491. sppp_cp_type_name(h->type),
  1492. sppp_state_name(sp->state[cp->protoidx]));
  1493. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1494. }
  1495. break;
  1496. case TERM_REQ:
  1497. switch (sp->state[cp->protoidx]) {
  1498. case STATE_ACK_RCVD:
  1499. case STATE_ACK_SENT:
  1500. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1501. /* FALLTHROUGH */
  1502. case STATE_CLOSED:
  1503. case STATE_STOPPED:
  1504. case STATE_CLOSING:
  1505. case STATE_STOPPING:
  1506. case STATE_REQ_SENT:
  1507. sta:
  1508. /* Send Terminate-Ack packet. */
  1509. if (debug)
  1510. log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
  1511. SPP_ARGS(ifp), cp->name);
  1512. sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
  1513. break;
  1514. case STATE_OPENED:
  1515. (cp->tld)(sp);
  1516. sp->rst_counter[cp->protoidx] = 0;
  1517. sppp_cp_change_state(cp, sp, STATE_STOPPING);
  1518. goto sta;
  1519. break;
  1520. default:
  1521. printf(SPP_FMT "%s illegal %s in state %s\n",
  1522. SPP_ARGS(ifp), cp->name,
  1523. sppp_cp_type_name(h->type),
  1524. sppp_state_name(sp->state[cp->protoidx]));
  1525. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1526. }
  1527. break;
  1528. case TERM_ACK:
  1529. switch (sp->state[cp->protoidx]) {
  1530. case STATE_CLOSED:
  1531. case STATE_STOPPED:
  1532. case STATE_REQ_SENT:
  1533. case STATE_ACK_SENT:
  1534. break;
  1535. case STATE_CLOSING:
  1536. sppp_cp_change_state(cp, sp, STATE_CLOSED);
  1537. (cp->tlf)(sp);
  1538. break;
  1539. case STATE_STOPPING:
  1540. sppp_cp_change_state(cp, sp, STATE_STOPPED);
  1541. (cp->tlf)(sp);
  1542. break;
  1543. case STATE_ACK_RCVD:
  1544. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1545. break;
  1546. case STATE_OPENED:
  1547. (cp->tld)(sp);
  1548. (cp->scr)(sp);
  1549. sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
  1550. break;
  1551. default:
  1552. printf(SPP_FMT "%s illegal %s in state %s\n",
  1553. SPP_ARGS(ifp), cp->name,
  1554. sppp_cp_type_name(h->type),
  1555. sppp_state_name(sp->state[cp->protoidx]));
  1556. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1557. }
  1558. break;
  1559. case CODE_REJ:
  1560. /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
  1561. log(LOG_INFO,
  1562. SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
  1563. "danger will robinson\n",
  1564. SPP_ARGS(ifp), cp->name,
  1565. sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
  1566. switch (sp->state[cp->protoidx]) {
  1567. case STATE_CLOSED:
  1568. case STATE_STOPPED:
  1569. case STATE_REQ_SENT:
  1570. case STATE_ACK_SENT:
  1571. case STATE_CLOSING:
  1572. case STATE_STOPPING:
  1573. case STATE_OPENED:
  1574. break;
  1575. case STATE_ACK_RCVD:
  1576. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1577. break;
  1578. default:
  1579. printf(SPP_FMT "%s illegal %s in state %s\n",
  1580. SPP_ARGS(ifp), cp->name,
  1581. sppp_cp_type_name(h->type),
  1582. sppp_state_name(sp->state[cp->protoidx]));
  1583. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1584. }
  1585. break;
  1586. case PROTO_REJ:
  1587. {
  1588. int catastrophic;
  1589. const struct cp *upper;
  1590. int i;
  1591. u_int16_t proto;
  1592. catastrophic = 0;
  1593. upper = NULL;
  1594. proto = ntohs(*((u_int16_t *)p));
  1595. for (i = 0; i < IDX_COUNT; i++) {
  1596. if (cps[i]->proto == proto) {
  1597. upper = cps[i];
  1598. break;
  1599. }
  1600. }
  1601. if (upper == NULL)
  1602. catastrophic++;
  1603. if (catastrophic || debug)
  1604. log(catastrophic? LOG_INFO: LOG_DEBUG,
  1605. SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
  1606. SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
  1607. sppp_cp_type_name(h->type), proto,
  1608. upper ? upper->name : "unknown",
  1609. upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
  1610. /*
  1611. * if we got RXJ+ against conf-req, the peer does not implement
  1612. * this particular protocol type. terminate the protocol.
  1613. */
  1614. if (upper && !catastrophic) {
  1615. if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
  1616. upper->Close(sp);
  1617. break;
  1618. }
  1619. }
  1620. /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
  1621. switch (sp->state[cp->protoidx]) {
  1622. case STATE_CLOSED:
  1623. case STATE_STOPPED:
  1624. case STATE_REQ_SENT:
  1625. case STATE_ACK_SENT:
  1626. case STATE_CLOSING:
  1627. case STATE_STOPPING:
  1628. case STATE_OPENED:
  1629. break;
  1630. case STATE_ACK_RCVD:
  1631. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1632. break;
  1633. default:
  1634. printf(SPP_FMT "%s illegal %s in state %s\n",
  1635. SPP_ARGS(ifp), cp->name,
  1636. sppp_cp_type_name(h->type),
  1637. sppp_state_name(sp->state[cp->protoidx]));
  1638. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1639. }
  1640. break;
  1641. }
  1642. case DISC_REQ:
  1643. if (cp->proto != PPP_LCP)
  1644. goto illegal;
  1645. /* Discard the packet. */
  1646. break;
  1647. case ECHO_REQ:
  1648. if (cp->proto != PPP_LCP)
  1649. goto illegal;
  1650. if (sp->state[cp->protoidx] != STATE_OPENED) {
  1651. if (debug)
  1652. log(-1, SPP_FMT "lcp echo req but lcp closed\n",
  1653. SPP_ARGS(ifp));
  1654. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1655. break;
  1656. }
  1657. if (len < 8) {
  1658. if (debug)
  1659. log(-1, SPP_FMT "invalid lcp echo request "
  1660. "packet length: %d bytes\n",
  1661. SPP_ARGS(ifp), len);
  1662. break;
  1663. }
  1664. if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
  1665. ntohl (*(long*)(h+1)) == sp->lcp.magic) {
  1666. /* Line loopback mode detected. */
  1667. printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
  1668. sp->pp_loopcnt = MAXALIVECNT * 5;
  1669. if_down (ifp);
  1670. sppp_qflush (&sp->pp_cpq);
  1671. /* Shut down the PPP link. */
  1672. /* XXX */
  1673. lcp.Down(sp);
  1674. lcp.Up(sp);
  1675. break;
  1676. }
  1677. *(long*)(h+1) = htonl (sp->lcp.magic);
  1678. if (debug)
  1679. log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
  1680. SPP_ARGS(ifp));
  1681. sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
  1682. break;
  1683. case ECHO_REPLY:
  1684. if (cp->proto != PPP_LCP)
  1685. goto illegal;
  1686. if (h->ident != sp->lcp.echoid) {
  1687. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1688. break;
  1689. }
  1690. if (len < 8) {
  1691. if (debug)
  1692. log(-1, SPP_FMT "lcp invalid echo reply "
  1693. "packet length: %d bytes\n",
  1694. SPP_ARGS(ifp), len);
  1695. break;
  1696. }
  1697. if (debug)
  1698. log(-1, SPP_FMT "lcp got echo rep\n",
  1699. SPP_ARGS(ifp));
  1700. if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
  1701. ntohl (*(long*)(h+1)) != sp->lcp.magic)
  1702. sp->pp_alivecnt = 0;
  1703. break;
  1704. default:
  1705. /* Unknown packet type -- send Code-Reject packet. */
  1706. illegal:
  1707. if (debug)
  1708. log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
  1709. SPP_ARGS(ifp), cp->name, h->type);
  1710. sppp_cp_send(sp, cp->proto, CODE_REJ,
  1711. ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
  1712. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  1713. }
  1714. }
  1715. /*
  1716. * The generic part of all Up/Down/Open/Close/TO event handlers.
  1717. * Basically, the state transition handling in the automaton.
  1718. */
  1719. static void
  1720. sppp_up_event(const struct cp *cp, struct sppp *sp)
  1721. {
  1722. STDDCL;
  1723. if (debug)
  1724. log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
  1725. SPP_ARGS(ifp), cp->name,
  1726. sppp_state_name(sp->state[cp->protoidx]));
  1727. switch (sp->state[cp->protoidx]) {
  1728. case STATE_INITIAL:
  1729. sppp_cp_change_state(cp, sp, STATE_CLOSED);
  1730. break;
  1731. case STATE_STARTING:
  1732. sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
  1733. (cp->scr)(sp);
  1734. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1735. break;
  1736. default:
  1737. printf(SPP_FMT "%s illegal up in state %s\n",
  1738. SPP_ARGS(ifp), cp->name,
  1739. sppp_state_name(sp->state[cp->protoidx]));
  1740. }
  1741. }
  1742. static void
  1743. sppp_down_event(const struct cp *cp, struct sppp *sp)
  1744. {
  1745. STDDCL;
  1746. if (debug)
  1747. log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
  1748. SPP_ARGS(ifp), cp->name,
  1749. sppp_state_name(sp->state[cp->protoidx]));
  1750. switch (sp->state[cp->protoidx]) {
  1751. case STATE_CLOSED:
  1752. case STATE_CLOSING:
  1753. sppp_cp_change_state(cp, sp, STATE_INITIAL);
  1754. break;
  1755. case STATE_STOPPED:
  1756. sppp_cp_change_state(cp, sp, STATE_STARTING);
  1757. (cp->tls)(sp);
  1758. break;
  1759. case STATE_STOPPING:
  1760. case STATE_REQ_SENT:
  1761. case STATE_ACK_RCVD:
  1762. case STATE_ACK_SENT:
  1763. sppp_cp_change_state(cp, sp, STATE_STARTING);
  1764. break;
  1765. case STATE_OPENED:
  1766. (cp->tld)(sp);
  1767. sppp_cp_change_state(cp, sp, STATE_STARTING);
  1768. break;
  1769. default:
  1770. printf(SPP_FMT "%s illegal down in state %s\n",
  1771. SPP_ARGS(ifp), cp->name,
  1772. sppp_state_name(sp->state[cp->protoidx]));
  1773. }
  1774. }
  1775. static void
  1776. sppp_open_event(const struct cp *cp, struct sppp *sp)
  1777. {
  1778. STDDCL;
  1779. if (debug)
  1780. log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
  1781. SPP_ARGS(ifp), cp->name,
  1782. sppp_state_name(sp->state[cp->protoidx]));
  1783. switch (sp->state[cp->protoidx]) {
  1784. case STATE_INITIAL:
  1785. sppp_cp_change_state(cp, sp, STATE_STARTING);
  1786. (cp->tls)(sp);
  1787. break;
  1788. case STATE_STARTING:
  1789. break;
  1790. case STATE_CLOSED:
  1791. sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
  1792. (cp->scr)(sp);
  1793. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1794. break;
  1795. case STATE_STOPPED:
  1796. /*
  1797. * Try escaping stopped state. This seems to bite
  1798. * people occasionally, in particular for IPCP,
  1799. * presumably following previous IPCP negotiation
  1800. * aborts. Somehow, we must have missed a Down event
  1801. * which would have caused a transition into starting
  1802. * state, so as a bandaid we force the Down event now.
  1803. * This effectively implements (something like the)
  1804. * `restart' option mentioned in the state transition
  1805. * table of RFC 1661.
  1806. */
  1807. sppp_cp_change_state(cp, sp, STATE_STARTING);
  1808. (cp->tls)(sp);
  1809. break;
  1810. case STATE_STOPPING:
  1811. case STATE_REQ_SENT:
  1812. case STATE_ACK_RCVD:
  1813. case STATE_ACK_SENT:
  1814. case STATE_OPENED:
  1815. break;
  1816. case STATE_CLOSING:
  1817. sppp_cp_change_state(cp, sp, STATE_STOPPING);
  1818. break;
  1819. }
  1820. }
  1821. static void
  1822. sppp_close_event(const struct cp *cp, struct sppp *sp)
  1823. {
  1824. STDDCL;
  1825. if (debug)
  1826. log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
  1827. SPP_ARGS(ifp), cp->name,
  1828. sppp_state_name(sp->state[cp->protoidx]));
  1829. switch (sp->state[cp->protoidx]) {
  1830. case STATE_INITIAL:
  1831. case STATE_CLOSED:
  1832. case STATE_CLOSING:
  1833. break;
  1834. case STATE_STARTING:
  1835. sppp_cp_change_state(cp, sp, STATE_INITIAL);
  1836. (cp->tlf)(sp);
  1837. break;
  1838. case STATE_STOPPED:
  1839. sppp_cp_change_state(cp, sp, STATE_CLOSED);
  1840. break;
  1841. case STATE_STOPPING:
  1842. sppp_cp_change_state(cp, sp, STATE_CLOSING);
  1843. break;
  1844. case STATE_OPENED:
  1845. (cp->tld)(sp);
  1846. /* FALLTHROUGH */
  1847. case STATE_REQ_SENT:
  1848. case STATE_ACK_RCVD:
  1849. case STATE_ACK_SENT:
  1850. sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
  1851. sppp_cp_send(sp, cp->proto, TERM_REQ,
  1852. ++sp->pp_seq[cp->protoidx], 0, 0);
  1853. sppp_cp_change_state(cp, sp, STATE_CLOSING);
  1854. break;
  1855. }
  1856. }
  1857. static void
  1858. sppp_to_event(const struct cp *cp, struct sppp *sp)
  1859. {
  1860. STDDCL;
  1861. SPPP_LOCK(sp);
  1862. if (debug)
  1863. log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
  1864. SPP_ARGS(ifp), cp->name,
  1865. sppp_state_name(sp->state[cp->protoidx]),
  1866. sp->rst_counter[cp->protoidx]);
  1867. if (--sp->rst_counter[cp->protoidx] < 0)
  1868. /* TO- event */
  1869. switch (sp->state[cp->protoidx]) {
  1870. case STATE_CLOSING:
  1871. sppp_cp_change_state(cp, sp, STATE_CLOSED);
  1872. (cp->tlf)(sp);
  1873. break;
  1874. case STATE_STOPPING:
  1875. sppp_cp_change_state(cp, sp, STATE_STOPPED);
  1876. (cp->tlf)(sp);
  1877. break;
  1878. case STATE_REQ_SENT:
  1879. case STATE_ACK_RCVD:
  1880. case STATE_ACK_SENT:
  1881. sppp_cp_change_state(cp, sp, STATE_STOPPED);
  1882. (cp->tlf)(sp);
  1883. break;
  1884. }
  1885. else
  1886. /* TO+ event */
  1887. switch (sp->state[cp->protoidx]) {
  1888. case STATE_CLOSING:
  1889. case STATE_STOPPING:
  1890. sppp_cp_send(sp, cp->proto, TERM_REQ,
  1891. ++sp->pp_seq[cp->protoidx], 0, 0);
  1892. callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
  1893. cp->TO, (void *)sp);
  1894. break;
  1895. case STATE_REQ_SENT:
  1896. case STATE_ACK_RCVD:
  1897. (cp->scr)(sp);
  1898. /* sppp_cp_change_state() will restart the timer */
  1899. sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
  1900. break;
  1901. case STATE_ACK_SENT:
  1902. (cp->scr)(sp);
  1903. callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
  1904. cp->TO, (void *)sp);
  1905. break;
  1906. }
  1907. SPPP_UNLOCK(sp);
  1908. }
  1909. /*
  1910. * Change the state of a control protocol in the state automaton.
  1911. * Takes care of starting/stopping the restart timer.
  1912. */
  1913. static void
  1914. sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
  1915. {
  1916. sp->state[cp->protoidx] = newstate;
  1917. callout_stop (&sp->ch[cp->protoidx]);
  1918. switch (newstate) {
  1919. case STATE_INITIAL:
  1920. case STATE_STARTING:
  1921. case STATE_CLOSED:
  1922. case STATE_STOPPED:
  1923. case STATE_OPENED:
  1924. break;
  1925. case STATE_CLOSING:
  1926. case STATE_STOPPING:
  1927. case STATE_REQ_SENT:
  1928. case STATE_ACK_RCVD:
  1929. case STATE_ACK_SENT:
  1930. callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
  1931. cp->TO, (void *)sp);
  1932. break;
  1933. }
  1934. }
  1935. /*
  1936. *--------------------------------------------------------------------------*
  1937. * *
  1938. * The LCP implementation. *
  1939. * *
  1940. *--------------------------------------------------------------------------*
  1941. */
  1942. static void
  1943. sppp_pp_up(struct sppp *sp)
  1944. {
  1945. SPPP_LOCK(sp);
  1946. lcp.Up(sp);
  1947. SPPP_UNLOCK(sp);
  1948. }
  1949. static void
  1950. sppp_pp_down(struct sppp *sp)
  1951. {
  1952. SPPP_LOCK(sp);
  1953. lcp.Down(sp);
  1954. SPPP_UNLOCK(sp);
  1955. }
  1956. static void
  1957. sppp_lcp_init(struct sppp *sp)
  1958. {
  1959. sp->lcp.opts = (1 << LCP_OPT_MAGIC);
  1960. sp->lcp.magic = 0;
  1961. sp->state[IDX_LCP] = STATE_INITIAL;
  1962. sp->fail_counter[IDX_LCP] = 0;
  1963. sp->pp_seq[IDX_LCP] = 0;
  1964. sp->pp_rseq[IDX_LCP] = 0;
  1965. sp->lcp.protos = 0;
  1966. sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
  1967. /* Note that these values are relevant for all control protocols */
  1968. sp->lcp.timeout = 3 * hz;
  1969. sp->lcp.max_terminate = 2;
  1970. sp->lcp.max_configure = 10;
  1971. sp->lcp.max_failure = 10;
  1972. callout_init(&sp->ch[IDX_LCP], 1);
  1973. }
  1974. static void
  1975. sppp_lcp_up(struct sppp *sp)
  1976. {
  1977. STDDCL;
  1978. sp->pp_alivecnt = 0;
  1979. sp->lcp.opts = (1 << LCP_OPT_MAGIC);
  1980. sp->lcp.magic = 0;
  1981. sp->lcp.protos = 0;
  1982. sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
  1983. /*
  1984. * If we are authenticator, negotiate LCP_AUTH
  1985. */
  1986. if (sp->hisauth.proto != 0)
  1987. sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
  1988. else
  1989. sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
  1990. sp->pp_flags &= ~PP_NEEDAUTH;
  1991. /*
  1992. * If this interface is passive or dial-on-demand, and we are
  1993. * still in Initial state, it means we've got an incoming
  1994. * call. Activate the interface.
  1995. */
  1996. if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
  1997. if (debug)
  1998. log(LOG_DEBUG,
  1999. SPP_FMT "Up event", SPP_ARGS(ifp));
  2000. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  2001. if (sp->state[IDX_LCP] == STATE_INITIAL) {
  2002. if (debug)
  2003. log(-1, "(incoming call)\n");
  2004. sp->pp_flags |= PP_CALLIN;
  2005. lcp.Open(sp);
  2006. } else if (debug)
  2007. log(-1, "\n");
  2008. } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
  2009. (sp->state[IDX_LCP] == STATE_INITIAL)) {
  2010. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  2011. lcp.Open(sp);
  2012. }
  2013. sppp_up_event(&lcp, sp);
  2014. }
  2015. static void
  2016. sppp_lcp_down(struct sppp *sp)
  2017. {
  2018. STDDCL;
  2019. sppp_down_event(&lcp, sp);
  2020. /*
  2021. * If this is neither a dial-on-demand nor a passive
  2022. * interface, simulate an ``ifconfig down'' action, so the
  2023. * administrator can force a redial by another ``ifconfig
  2024. * up''. XXX For leased line operation, should we immediately
  2025. * try to reopen the connection here?
  2026. */
  2027. if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
  2028. log(LOG_INFO,
  2029. SPP_FMT "Down event, taking interface down.\n",
  2030. SPP_ARGS(ifp));
  2031. if_down(ifp);
  2032. } else {
  2033. if (debug)
  2034. log(LOG_DEBUG,
  2035. SPP_FMT "Down event (carrier loss)\n",
  2036. SPP_ARGS(ifp));
  2037. sp->pp_flags &= ~PP_CALLIN;
  2038. if (sp->state[IDX_LCP] != STATE_INITIAL)
  2039. lcp.Close(sp);
  2040. ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  2041. }
  2042. }
  2043. static void
  2044. sppp_lcp_open(struct sppp *sp)
  2045. {
  2046. sppp_open_event(&lcp, sp);
  2047. }
  2048. static void
  2049. sppp_lcp_close(struct sppp *sp)
  2050. {
  2051. sppp_close_event(&lcp, sp);
  2052. }
  2053. static void
  2054. sppp_lcp_TO(void *cookie)
  2055. {
  2056. sppp_to_event(&lcp, (struct sppp *)cookie);
  2057. }
  2058. /*
  2059. * Analyze a configure request. Return true if it was agreeable, and
  2060. * caused action sca, false if it has been rejected or nak'ed, and
  2061. * caused action scn. (The return value is used to make the state
  2062. * transition decision in the state automaton.)
  2063. */
  2064. static int
  2065. sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
  2066. {
  2067. STDDCL;
  2068. u_char *buf, *r, *p;
  2069. int origlen, rlen;
  2070. u_long nmagic;
  2071. u_short authproto;
  2072. len -= 4;
  2073. origlen = len;
  2074. buf = r = malloc (len, M_TEMP, M_NOWAIT);
  2075. if (! buf)
  2076. return (0);
  2077. if (debug)
  2078. log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
  2079. SPP_ARGS(ifp));
  2080. /* pass 1: check for things that need to be rejected */
  2081. p = (void*) (h+1);
  2082. for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
  2083. len-=p[1], p+=p[1]) {
  2084. if (debug)
  2085. log(-1, " %s ", sppp_lcp_opt_name(*p));
  2086. switch (*p) {
  2087. case LCP_OPT_MAGIC:
  2088. /* Magic number. */
  2089. if (len >= 6 && p[1] == 6)
  2090. continue;
  2091. if (debug)
  2092. log(-1, "[invalid] ");
  2093. break;
  2094. case LCP_OPT_ASYNC_MAP:
  2095. /* Async control character map. */
  2096. if (len >= 6 && p[1] == 6)
  2097. continue;
  2098. if (debug)
  2099. log(-1, "[invalid] ");
  2100. break;
  2101. case LCP_OPT_MRU:
  2102. /* Maximum receive unit. */
  2103. if (len >= 4 && p[1] == 4)
  2104. continue;
  2105. if (debug)
  2106. log(-1, "[invalid] ");
  2107. break;
  2108. case LCP_OPT_AUTH_PROTO:
  2109. if (len < 4) {
  2110. if (debug)
  2111. log(-1, "[invalid] ");
  2112. break;
  2113. }
  2114. authproto = (p[2] << 8) + p[3];
  2115. if (authproto == PPP_CHAP && p[1] != 5) {
  2116. if (debug)
  2117. log(-1, "[invalid chap len] ");
  2118. break;
  2119. }
  2120. if (sp->myauth.proto == 0) {
  2121. /* we are not configured to do auth */
  2122. if (debug)
  2123. log(-1, "[not configured] ");
  2124. break;
  2125. }
  2126. /*
  2127. * Remote want us to authenticate, remember this,
  2128. * so we stay in PHASE_AUTHENTICATE after LCP got
  2129. * up.
  2130. */
  2131. sp->pp_flags |= PP_NEEDAUTH;
  2132. continue;
  2133. default:
  2134. /* Others not supported. */
  2135. if (debug)
  2136. log(-1, "[rej] ");
  2137. break;
  2138. }
  2139. /* Add the option to rejected list. */
  2140. bcopy (p, r, p[1]);
  2141. r += p[1];
  2142. rlen += p[1];
  2143. }
  2144. if (rlen) {
  2145. if (debug)
  2146. log(-1, " send conf-rej\n");
  2147. sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
  2148. return 0;
  2149. } else if (debug)
  2150. log(-1, "\n");
  2151. /*
  2152. * pass 2: check for option values that are unacceptable and
  2153. * thus require to be nak'ed.
  2154. */
  2155. if (debug)
  2156. log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
  2157. SPP_ARGS(ifp));
  2158. p = (void*) (h+1);
  2159. len = origlen;
  2160. for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
  2161. len-=p[1], p+=p[1]) {
  2162. if (debug)
  2163. log(-1, " %s ", sppp_lcp_opt_name(*p));
  2164. switch (*p) {
  2165. case LCP_OPT_MAGIC:
  2166. /* Magic number -- extract. */
  2167. nmagic = (u_long)p[2] << 24 |
  2168. (u_long)p[3] << 16 | p[4] << 8 | p[5];
  2169. if (nmagic != sp->lcp.magic) {
  2170. sp->pp_loopcnt = 0;
  2171. if (debug)
  2172. log(-1, "0x%lx ", nmagic);
  2173. continue;
  2174. }
  2175. if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
  2176. log(-1, "[glitch] ");
  2177. ++sp->pp_loopcnt;
  2178. /*
  2179. * We negate our magic here, and NAK it. If
  2180. * we see it later in an NAK packet, we
  2181. * suggest a new one.
  2182. */
  2183. nmagic = ~sp->lcp.magic;
  2184. /* Gonna NAK it. */
  2185. p[2] = nmagic >> 24;
  2186. p[3] = nmagic >> 16;
  2187. p[4] = nmagic >> 8;
  2188. p[5] = nmagic;
  2189. break;
  2190. case LCP_OPT_ASYNC_MAP:
  2191. /*
  2192. * Async control character map -- just ignore it.
  2193. *
  2194. * Quote from RFC 1662, chapter 6:
  2195. * To enable this functionality, synchronous PPP
  2196. * implementations MUST always respond to the
  2197. * Async-Control-Character-Map Configuration
  2198. * Option with the LCP Configure-Ack. However,
  2199. * acceptance of the Configuration Option does
  2200. * not imply that the synchronous implementation
  2201. * will do any ACCM mapping. Instead, all such
  2202. * octet mapping will be performed by the
  2203. * asynchronous-to-synchronous converter.
  2204. */
  2205. continue;
  2206. case LCP_OPT_MRU:
  2207. /*
  2208. * Maximum receive unit. Always agreeable,
  2209. * but ignored by now.
  2210. */
  2211. sp->lcp.their_mru = p[2] * 256 + p[3];
  2212. if (debug)
  2213. log(-1, "%lu ", sp->lcp.their_mru);
  2214. continue;
  2215. case LCP_OPT_AUTH_PROTO:
  2216. authproto = (p[2] << 8) + p[3];
  2217. if (sp->myauth.proto != authproto) {
  2218. /* not agreed, nak */
  2219. if (debug)
  2220. log(-1, "[mine %s != his %s] ",
  2221. sppp_proto_name(sp->hisauth.proto),
  2222. sppp_proto_name(authproto));
  2223. p[2] = sp->myauth.proto >> 8;
  2224. p[3] = sp->myauth.proto;
  2225. break;
  2226. }
  2227. if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
  2228. if (debug)
  2229. log(-1, "[chap not MD5] ");
  2230. p[4] = CHAP_MD5;
  2231. break;
  2232. }
  2233. continue;
  2234. }
  2235. /* Add the option to nak'ed list. */
  2236. bcopy (p, r, p[1]);
  2237. r += p[1];
  2238. rlen += p[1];
  2239. }
  2240. if (rlen) {
  2241. /*
  2242. * Local and remote magics equal -- loopback?
  2243. */
  2244. if (sp->pp_loopcnt >= MAXALIVECNT*5) {
  2245. if (sp->pp_loopcnt == MAXALIVECNT*5)
  2246. printf (SPP_FMT "loopback\n",
  2247. SPP_ARGS(ifp));
  2248. if (ifp->if_flags & IFF_UP) {
  2249. if_down(ifp);
  2250. sppp_qflush(&sp->pp_cpq);
  2251. /* XXX ? */
  2252. lcp.Down(sp);
  2253. lcp.Up(sp);
  2254. }
  2255. } else if (!sp->pp_loopcnt &&
  2256. ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
  2257. if (debug)
  2258. log(-1, " max_failure (%d) exceeded, "
  2259. "send conf-rej\n",
  2260. sp->lcp.max_failure);
  2261. sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
  2262. } else {
  2263. if (debug)
  2264. log(-1, " send conf-nak\n");
  2265. sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
  2266. }
  2267. } else {
  2268. if (debug)
  2269. log(-1, " send conf-ack\n");
  2270. sp->fail_counter[IDX_LCP] = 0;
  2271. sp->pp_loopcnt = 0;
  2272. sppp_cp_send (sp, PPP_LCP, CONF_ACK,
  2273. h->ident, origlen, h+1);
  2274. }
  2275. free (buf, M_TEMP);
  2276. return (rlen == 0);
  2277. }
  2278. /*
  2279. * Analyze the LCP Configure-Reject option list, and adjust our
  2280. * negotiation.
  2281. */
  2282. static void
  2283. sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
  2284. {
  2285. STDDCL;
  2286. u_char *buf, *p;
  2287. len -= 4;
  2288. buf = malloc (len, M_TEMP, M_NOWAIT);
  2289. if (!buf)
  2290. return;
  2291. if (debug)
  2292. log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
  2293. SPP_ARGS(ifp));
  2294. p = (void*) (h+1);
  2295. for (; len >= 2 && p[1] >= 2 && len >= p[1];
  2296. len -= p[1], p += p[1]) {
  2297. if (debug)
  2298. log(-1, " %s ", sppp_lcp_opt_name(*p));
  2299. switch (*p) {
  2300. case LCP_OPT_MAGIC:
  2301. /* Magic number -- can't use it, use 0 */
  2302. sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
  2303. sp->lcp.magic = 0;
  2304. break;
  2305. case LCP_OPT_MRU:
  2306. /*
  2307. * Should not be rejected anyway, since we only
  2308. * negotiate a MRU if explicitly requested by
  2309. * peer.
  2310. */
  2311. sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
  2312. break;
  2313. case LCP_OPT_AUTH_PROTO:
  2314. /*
  2315. * Peer doesn't want to authenticate himself,
  2316. * deny unless this is a dialout call, and
  2317. * AUTHFLAG_NOCALLOUT is set.
  2318. */
  2319. if ((sp->pp_flags & PP_CALLIN) == 0 &&
  2320. (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
  2321. if (debug)
  2322. log(-1, "[don't insist on auth "
  2323. "for callout]");
  2324. sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
  2325. break;
  2326. }
  2327. if (debug)
  2328. log(-1, "[access denied]\n");
  2329. lcp.Close(sp);
  2330. break;
  2331. }
  2332. }
  2333. if (debug)
  2334. log(-1, "\n");
  2335. free (buf, M_TEMP);
  2336. return;
  2337. }
  2338. /*
  2339. * Analyze the LCP Configure-NAK option list, and adjust our
  2340. * negotiation.
  2341. */
  2342. static void
  2343. sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
  2344. {
  2345. STDDCL;
  2346. u_char *buf, *p;
  2347. u_long magic;
  2348. len -= 4;
  2349. buf = malloc (len, M_TEMP, M_NOWAIT);
  2350. if (!buf)
  2351. return;
  2352. if (debug)
  2353. log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
  2354. SPP_ARGS(ifp));
  2355. p = (void*) (h+1);
  2356. for (; len >= 2 && p[1] >= 2 && len >= p[1];
  2357. len -= p[1], p += p[1]) {
  2358. if (debug)
  2359. log(-1, " %s ", sppp_lcp_opt_name(*p));
  2360. switch (*p) {
  2361. case LCP_OPT_MAGIC:
  2362. /* Magic number -- renegotiate */
  2363. if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
  2364. len >= 6 && p[1] == 6) {
  2365. magic = (u_long)p[2] << 24 |
  2366. (u_long)p[3] << 16 | p[4] << 8 | p[5];
  2367. /*
  2368. * If the remote magic is our negated one,
  2369. * this looks like a loopback problem.
  2370. * Suggest a new magic to make sure.
  2371. */
  2372. if (magic == ~sp->lcp.magic) {
  2373. if (debug)
  2374. log(-1, "magic glitch ");
  2375. sp->lcp.magic = random();
  2376. } else {
  2377. sp->lcp.magic = magic;
  2378. if (debug)
  2379. log(-1, "%lu ", magic);
  2380. }
  2381. }
  2382. break;
  2383. case LCP_OPT_MRU:
  2384. /*
  2385. * Peer wants to advise us to negotiate an MRU.
  2386. * Agree on it if it's reasonable, or use
  2387. * default otherwise.
  2388. */
  2389. if (len >= 4 && p[1] == 4) {
  2390. u_int mru = p[2] * 256 + p[3];
  2391. if (debug)
  2392. log(-1, "%d ", mru);
  2393. if (mru < PP_MTU || mru > PP_MAX_MRU)
  2394. mru = PP_MTU;
  2395. sp->lcp.mru = mru;
  2396. sp->lcp.opts |= (1 << LCP_OPT_MRU);
  2397. }
  2398. break;
  2399. case LCP_OPT_AUTH_PROTO:
  2400. /*
  2401. * Peer doesn't like our authentication method,
  2402. * deny.
  2403. */
  2404. if (debug)
  2405. log(-1, "[access denied]\n");
  2406. lcp.Close(sp);
  2407. break;
  2408. }
  2409. }
  2410. if (debug)
  2411. log(-1, "\n");
  2412. free (buf, M_TEMP);
  2413. return;
  2414. }
  2415. static void
  2416. sppp_lcp_tlu(struct sppp *sp)
  2417. {
  2418. STDDCL;
  2419. int i;
  2420. u_long mask;
  2421. /* XXX ? */
  2422. if (! (ifp->if_flags & IFF_UP) &&
  2423. (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  2424. /* Coming out of loopback mode. */
  2425. if_up(ifp);
  2426. printf (SPP_FMT "up\n", SPP_ARGS(ifp));
  2427. }
  2428. for (i = 0; i < IDX_COUNT; i++)
  2429. if ((cps[i])->flags & CP_QUAL)
  2430. (cps[i])->Open(sp);
  2431. if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
  2432. (sp->pp_flags & PP_NEEDAUTH) != 0)
  2433. sp->pp_phase = PHASE_AUTHENTICATE;
  2434. else
  2435. sp->pp_phase = PHASE_NETWORK;
  2436. if (debug)
  2437. log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
  2438. sppp_phase_name(sp->pp_phase));
  2439. /*
  2440. * Open all authentication protocols. This is even required
  2441. * if we already proceeded to network phase, since it might be
  2442. * that remote wants us to authenticate, so we might have to
  2443. * send a PAP request. Undesired authentication protocols
  2444. * don't do anything when they get an Open event.
  2445. */
  2446. for (i = 0; i < IDX_COUNT; i++)
  2447. if ((cps[i])->flags & CP_AUTH)
  2448. (cps[i])->Open(sp);
  2449. if (sp->pp_phase == PHASE_NETWORK) {
  2450. /* Notify all NCPs. */
  2451. for (i = 0; i < IDX_COUNT; i++)
  2452. if (((cps[i])->flags & CP_NCP) &&
  2453. /*
  2454. * XXX
  2455. * Hack to administratively disable IPv6 if
  2456. * not desired. Perhaps we should have another
  2457. * flag for this, but right now, we can make
  2458. * all struct cp's read/only.
  2459. */
  2460. (cps[i] != &ipv6cp ||
  2461. (sp->confflags & CONF_ENABLE_IPV6)))
  2462. (cps[i])->Open(sp);
  2463. }
  2464. /* Send Up events to all started protos. */
  2465. for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
  2466. if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
  2467. (cps[i])->Up(sp);
  2468. /* notify low-level driver of state change */
  2469. if (sp->pp_chg)
  2470. sp->pp_chg(sp, (int)sp->pp_phase);
  2471. if (sp->pp_phase == PHASE_NETWORK)
  2472. /* if no NCP is starting, close down */
  2473. sppp_lcp_check_and_close(sp);
  2474. }
  2475. static void
  2476. sppp_lcp_tld(struct sppp *sp)
  2477. {
  2478. STDDCL;
  2479. int i;
  2480. u_long mask;
  2481. sp->pp_phase = PHASE_TERMINATE;
  2482. if (debug)
  2483. log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
  2484. sppp_phase_name(sp->pp_phase));
  2485. /*
  2486. * Take upper layers down. We send the Down event first and
  2487. * the Close second to prevent the upper layers from sending
  2488. * ``a flurry of terminate-request packets'', as the RFC
  2489. * describes it.
  2490. */
  2491. for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
  2492. if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
  2493. (cps[i])->Down(sp);
  2494. (cps[i])->Close(sp);
  2495. }
  2496. }
  2497. static void
  2498. sppp_lcp_tls(struct sppp *sp)
  2499. {
  2500. STDDCL;
  2501. sp->pp_phase = PHASE_ESTABLISH;
  2502. if (debug)
  2503. log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
  2504. sppp_phase_name(sp->pp_phase));
  2505. /* Notify lower layer if desired. */
  2506. if (sp->pp_tls)
  2507. (sp->pp_tls)(sp);
  2508. else
  2509. (sp->pp_up)(sp);
  2510. }
  2511. static void
  2512. sppp_lcp_tlf(struct sppp *sp)
  2513. {
  2514. STDDCL;
  2515. sp->pp_phase = PHASE_DEAD;
  2516. if (debug)
  2517. log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
  2518. sppp_phase_name(sp->pp_phase));
  2519. /* Notify lower layer if desired. */
  2520. if (sp->pp_tlf)
  2521. (sp->pp_tlf)(sp);
  2522. else
  2523. (sp->pp_down)(sp);
  2524. }
  2525. static void
  2526. sppp_lcp_scr(struct sppp *sp)
  2527. {
  2528. char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
  2529. int i = 0;
  2530. u_short authproto;
  2531. if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
  2532. if (! sp->lcp.magic)
  2533. sp->lcp.magic = random();
  2534. opt[i++] = LCP_OPT_MAGIC;
  2535. opt[i++] = 6;
  2536. opt[i++] = sp->lcp.magic >> 24;
  2537. opt[i++] = sp->lcp.magic >> 16;
  2538. opt[i++] = sp->lcp.magic >> 8;
  2539. opt[i++] = sp->lcp.magic;
  2540. }
  2541. if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
  2542. opt[i++] = LCP_OPT_MRU;
  2543. opt[i++] = 4;
  2544. opt[i++] = sp->lcp.mru >> 8;
  2545. opt[i++] = sp->lcp.mru;
  2546. }
  2547. if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
  2548. authproto = sp->hisauth.proto;
  2549. opt[i++] = LCP_OPT_AUTH_PROTO;
  2550. opt[i++] = authproto == PPP_CHAP? 5: 4;
  2551. opt[i++] = authproto >> 8;
  2552. opt[i++] = authproto;
  2553. if (authproto == PPP_CHAP)
  2554. opt[i++] = CHAP_MD5;
  2555. }
  2556. sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
  2557. sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
  2558. }
  2559. /*
  2560. * Check the open NCPs, return true if at least one NCP is open.
  2561. */
  2562. static int
  2563. sppp_ncp_check(struct sppp *sp)
  2564. {
  2565. int i, mask;
  2566. for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
  2567. if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
  2568. return 1;
  2569. return 0;
  2570. }
  2571. /*
  2572. * Re-check the open NCPs and see if we should terminate the link.
  2573. * Called by the NCPs during their tlf action handling.
  2574. */
  2575. static void
  2576. sppp_lcp_check_and_close(struct sppp *sp)
  2577. {
  2578. if (sp->pp_phase < PHASE_NETWORK)
  2579. /* don't bother, we are already going down */
  2580. return;
  2581. if (sppp_ncp_check(sp))
  2582. return;
  2583. lcp.Close(sp);
  2584. }
  2585. /*
  2586. *--------------------------------------------------------------------------*
  2587. * *
  2588. * The IPCP implementation. *
  2589. * *
  2590. *--------------------------------------------------------------------------*
  2591. */
  2592. #ifdef INET
  2593. static void
  2594. sppp_ipcp_init(struct sppp *sp)
  2595. {
  2596. sp->ipcp.opts = 0;
  2597. sp->ipcp.flags = 0;
  2598. sp->state[IDX_IPCP] = STATE_INITIAL;
  2599. sp->fail_counter[IDX_IPCP] = 0;
  2600. sp->pp_seq[IDX_IPCP] = 0;
  2601. sp->pp_rseq[IDX_IPCP] = 0;
  2602. callout_init(&sp->ch[IDX_IPCP], 1);
  2603. }
  2604. static void
  2605. sppp_ipcp_up(struct sppp *sp)
  2606. {
  2607. sppp_up_event(&ipcp, sp);
  2608. }
  2609. static void
  2610. sppp_ipcp_down(struct sppp *sp)
  2611. {
  2612. sppp_down_event(&ipcp, sp);
  2613. }
  2614. static void
  2615. sppp_ipcp_open(struct sppp *sp)
  2616. {
  2617. STDDCL;
  2618. u_long myaddr, hisaddr;
  2619. sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
  2620. IPCP_MYADDR_DYN | IPCP_VJ);
  2621. sp->ipcp.opts = 0;
  2622. sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
  2623. /*
  2624. * If we don't have his address, this probably means our
  2625. * interface doesn't want to talk IP at all. (This could
  2626. * be the case if somebody wants to speak only IPX, for
  2627. * example.) Don't open IPCP in this case.
  2628. */
  2629. if (hisaddr == 0L) {
  2630. /* XXX this message should go away */
  2631. if (debug)
  2632. log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
  2633. SPP_ARGS(ifp));
  2634. return;
  2635. }
  2636. if (myaddr == 0L) {
  2637. /*
  2638. * I don't have an assigned address, so i need to
  2639. * negotiate my address.
  2640. */
  2641. sp->ipcp.flags |= IPCP_MYADDR_DYN;
  2642. sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
  2643. } else
  2644. sp->ipcp.flags |= IPCP_MYADDR_SEEN;
  2645. if (sp->confflags & CONF_ENABLE_VJ) {
  2646. sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
  2647. sp->ipcp.max_state = MAX_STATES - 1;
  2648. sp->ipcp.compress_cid = 1;
  2649. }
  2650. sppp_open_event(&ipcp, sp);
  2651. }
  2652. static void
  2653. sppp_ipcp_close(struct sppp *sp)
  2654. {
  2655. sppp_close_event(&ipcp, sp);
  2656. if (sp->ipcp.flags & IPCP_MYADDR_DYN)
  2657. /*
  2658. * My address was dynamic, clear it again.
  2659. */
  2660. sppp_set_ip_addr(sp, 0L);
  2661. }
  2662. static void
  2663. sppp_ipcp_TO(void *cookie)
  2664. {
  2665. sppp_to_event(&ipcp, (struct sppp *)cookie);
  2666. }
  2667. /*
  2668. * Analyze a configure request. Return true if it was agreeable, and
  2669. * caused action sca, false if it has been rejected or nak'ed, and
  2670. * caused action scn. (The return value is used to make the state
  2671. * transition decision in the state automaton.)
  2672. */
  2673. static int
  2674. sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
  2675. {
  2676. u_char *buf, *r, *p;
  2677. struct ifnet *ifp = SP2IFP(sp);
  2678. int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
  2679. u_long hisaddr, desiredaddr;
  2680. int gotmyaddr = 0;
  2681. int desiredcomp;
  2682. len -= 4;
  2683. origlen = len;
  2684. /*
  2685. * Make sure to allocate a buf that can at least hold a
  2686. * conf-nak with an `address' option. We might need it below.
  2687. */
  2688. buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
  2689. if (! buf)
  2690. return (0);
  2691. /* pass 1: see if we can recognize them */
  2692. if (debug)
  2693. log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
  2694. SPP_ARGS(ifp));
  2695. p = (void*) (h+1);
  2696. for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
  2697. len-=p[1], p+=p[1]) {
  2698. if (debug)
  2699. log(-1, " %s ", sppp_ipcp_opt_name(*p));
  2700. switch (*p) {
  2701. case IPCP_OPT_COMPRESSION:
  2702. if (!(sp->confflags & CONF_ENABLE_VJ)) {
  2703. /* VJ compression administratively disabled */
  2704. if (debug)
  2705. log(-1, "[locally disabled] ");
  2706. break;
  2707. }
  2708. /*
  2709. * In theory, we should only conf-rej an
  2710. * option that is shorter than RFC 1618
  2711. * requires (i.e. < 4), and should conf-nak
  2712. * anything else that is not VJ. However,
  2713. * since our algorithm always uses the
  2714. * original option to NAK it with new values,
  2715. * things would become more complicated. In
  2716. * practice, the only commonly implemented IP
  2717. * compression option is VJ anyway, so the
  2718. * difference is negligible.
  2719. */
  2720. if (len >= 6 && p[1] == 6) {
  2721. /*
  2722. * correctly formed compression option
  2723. * that could be VJ compression
  2724. */
  2725. continue;
  2726. }
  2727. if (debug)
  2728. log(-1,
  2729. "optlen %d [invalid/unsupported] ",
  2730. p[1]);
  2731. break;
  2732. case IPCP_OPT_ADDRESS:
  2733. if (len >= 6 && p[1] == 6) {
  2734. /* correctly formed address option */
  2735. continue;
  2736. }
  2737. if (debug)
  2738. log(-1, "[invalid] ");
  2739. break;
  2740. default:
  2741. /* Others not supported. */
  2742. if (debug)
  2743. log(-1, "[rej] ");
  2744. break;
  2745. }
  2746. /* Add the option to rejected list. */
  2747. bcopy (p, r, p[1]);
  2748. r += p[1];
  2749. rlen += p[1];
  2750. }
  2751. if (rlen) {
  2752. if (debug)
  2753. log(-1, " send conf-rej\n");
  2754. sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
  2755. return 0;
  2756. } else if (debug)
  2757. log(-1, "\n");
  2758. /* pass 2: parse option values */
  2759. sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
  2760. if (debug)
  2761. log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
  2762. SPP_ARGS(ifp));
  2763. p = (void*) (h+1);
  2764. len = origlen;
  2765. for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
  2766. len-=p[1], p+=p[1]) {
  2767. if (debug)
  2768. log(-1, " %s ", sppp_ipcp_opt_name(*p));
  2769. switch (*p) {
  2770. case IPCP_OPT_COMPRESSION:
  2771. desiredcomp = p[2] << 8 | p[3];
  2772. /* We only support VJ */
  2773. if (desiredcomp == IPCP_COMP_VJ) {
  2774. if (debug)
  2775. log(-1, "VJ [ack] ");
  2776. sp->ipcp.flags |= IPCP_VJ;
  2777. sl_compress_init(sp->pp_comp, p[4]);
  2778. sp->ipcp.max_state = p[4];
  2779. sp->ipcp.compress_cid = p[5];
  2780. continue;
  2781. }
  2782. if (debug)
  2783. log(-1,
  2784. "compproto %#04x [not supported] ",
  2785. desiredcomp);
  2786. p[2] = IPCP_COMP_VJ >> 8;
  2787. p[3] = IPCP_COMP_VJ;
  2788. p[4] = sp->ipcp.max_state;
  2789. p[5] = sp->ipcp.compress_cid;
  2790. break;
  2791. case IPCP_OPT_ADDRESS:
  2792. /* This is the address he wants in his end */
  2793. desiredaddr = p[2] << 24 | p[3] << 16 |
  2794. p[4] << 8 | p[5];
  2795. if (desiredaddr == hisaddr ||
  2796. (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
  2797. /*
  2798. * Peer's address is same as our value,
  2799. * or we have set it to 0.0.0.* to
  2800. * indicate that we do not really care,
  2801. * this is agreeable. Gonna conf-ack
  2802. * it.
  2803. */
  2804. if (debug)
  2805. log(-1, "%s [ack] ",
  2806. sppp_dotted_quad(hisaddr));
  2807. /* record that we've seen it already */
  2808. sp->ipcp.flags |= IPCP_HISADDR_SEEN;
  2809. continue;
  2810. }
  2811. /*
  2812. * The address wasn't agreeable. This is either
  2813. * he sent us 0.0.0.0, asking to assign him an
  2814. * address, or he send us another address not
  2815. * matching our value. Either case, we gonna
  2816. * conf-nak it with our value.
  2817. * XXX: we should "rej" if hisaddr == 0
  2818. */
  2819. if (debug) {
  2820. if (desiredaddr == 0)
  2821. log(-1, "[addr requested] ");
  2822. else
  2823. log(-1, "%s [not agreed] ",
  2824. sppp_dotted_quad(desiredaddr));
  2825. }
  2826. p[2] = hisaddr >> 24;
  2827. p[3] = hisaddr >> 16;
  2828. p[4] = hisaddr >> 8;
  2829. p[5] = hisaddr;
  2830. break;
  2831. }
  2832. /* Add the option to nak'ed list. */
  2833. bcopy (p, r, p[1]);
  2834. r += p[1];
  2835. rlen += p[1];
  2836. }
  2837. /*
  2838. * If we are about to conf-ack the request, but haven't seen
  2839. * his address so far, gonna conf-nak it instead, with the
  2840. * `address' option present and our idea of his address being
  2841. * filled in there, to request negotiation of both addresses.
  2842. *
  2843. * XXX This can result in an endless req - nak loop if peer
  2844. * doesn't want to send us his address. Q: What should we do
  2845. * about it? XXX A: implement the max-failure counter.
  2846. */
  2847. if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
  2848. buf[0] = IPCP_OPT_ADDRESS;
  2849. buf[1] = 6;
  2850. buf[2] = hisaddr >> 24;
  2851. buf[3] = hisaddr >> 16;
  2852. buf[4] = hisaddr >> 8;
  2853. buf[5] = hisaddr;
  2854. rlen = 6;
  2855. if (debug)
  2856. log(-1, "still need hisaddr ");
  2857. }
  2858. if (rlen) {
  2859. if (debug)
  2860. log(-1, " send conf-nak\n");
  2861. sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
  2862. } else {
  2863. if (debug)
  2864. log(-1, " send conf-ack\n");
  2865. sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
  2866. h->ident, origlen, h+1);
  2867. }
  2868. free (buf, M_TEMP);
  2869. return (rlen == 0);
  2870. }
  2871. /*
  2872. * Analyze the IPCP Configure-Reject option list, and adjust our
  2873. * negotiation.
  2874. */
  2875. static void
  2876. sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
  2877. {
  2878. u_char *buf, *p;
  2879. struct ifnet *ifp = SP2IFP(sp);
  2880. int debug = ifp->if_flags & IFF_DEBUG;
  2881. len -= 4;
  2882. buf = malloc (len, M_TEMP, M_NOWAIT);
  2883. if (!buf)
  2884. return;
  2885. if (debug)
  2886. log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
  2887. SPP_ARGS(ifp));
  2888. p = (void*) (h+1);
  2889. for (; len >= 2 && p[1] >= 2 && len >= p[1];
  2890. len -= p[1], p += p[1]) {
  2891. if (debug)
  2892. log(-1, " %s ", sppp_ipcp_opt_name(*p));
  2893. switch (*p) {
  2894. case IPCP_OPT_COMPRESSION:
  2895. sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
  2896. break;
  2897. case IPCP_OPT_ADDRESS:
  2898. /*
  2899. * Peer doesn't grok address option. This is
  2900. * bad. XXX Should we better give up here?
  2901. * XXX We could try old "addresses" option...
  2902. */
  2903. sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
  2904. break;
  2905. }
  2906. }
  2907. if (debug)
  2908. log(-1, "\n");
  2909. free (buf, M_TEMP);
  2910. return;
  2911. }
  2912. /*
  2913. * Analyze the IPCP Configure-NAK option list, and adjust our
  2914. * negotiation.
  2915. */
  2916. static void
  2917. sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
  2918. {
  2919. u_char *buf, *p;
  2920. struct ifnet *ifp = SP2IFP(sp);
  2921. int debug = ifp->if_flags & IFF_DEBUG;
  2922. int desiredcomp;
  2923. u_long wantaddr;
  2924. len -= 4;
  2925. buf = malloc (len, M_TEMP, M_NOWAIT);
  2926. if (!buf)
  2927. return;
  2928. if (debug)
  2929. log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
  2930. SPP_ARGS(ifp));
  2931. p = (void*) (h+1);
  2932. for (; len >= 2 && p[1] >= 2 && len >= p[1];
  2933. len -= p[1], p += p[1]) {
  2934. if (debug)
  2935. log(-1, " %s ", sppp_ipcp_opt_name(*p));
  2936. switch (*p) {
  2937. case IPCP_OPT_COMPRESSION:
  2938. if (len >= 6 && p[1] == 6) {
  2939. desiredcomp = p[2] << 8 | p[3];
  2940. if (debug)
  2941. log(-1, "[wantcomp %#04x] ",
  2942. desiredcomp);
  2943. if (desiredcomp == IPCP_COMP_VJ) {
  2944. sl_compress_init(sp->pp_comp, p[4]);
  2945. sp->ipcp.max_state = p[4];
  2946. sp->ipcp.compress_cid = p[5];
  2947. if (debug)
  2948. log(-1, "[agree] ");
  2949. } else
  2950. sp->ipcp.opts &=
  2951. ~(1 << IPCP_OPT_COMPRESSION);
  2952. }
  2953. break;
  2954. case IPCP_OPT_ADDRESS:
  2955. /*
  2956. * Peer doesn't like our local IP address. See
  2957. * if we can do something for him. We'll drop
  2958. * him our address then.
  2959. */
  2960. if (len >= 6 && p[1] == 6) {
  2961. wantaddr = p[2] << 24 | p[3] << 16 |
  2962. p[4] << 8 | p[5];
  2963. sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
  2964. if (debug)
  2965. log(-1, "[wantaddr %s] ",
  2966. sppp_dotted_quad(wantaddr));
  2967. /*
  2968. * When doing dynamic address assignment,
  2969. * we accept his offer. Otherwise, we
  2970. * ignore it and thus continue to negotiate
  2971. * our already existing value.
  2972. * XXX: Bogus, if he said no once, he'll
  2973. * just say no again, might as well die.
  2974. */
  2975. if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
  2976. sppp_set_ip_addr(sp, wantaddr);
  2977. if (debug)
  2978. log(-1, "[agree] ");
  2979. sp->ipcp.flags |= IPCP_MYADDR_SEEN;
  2980. }
  2981. }
  2982. break;
  2983. }
  2984. }
  2985. if (debug)
  2986. log(-1, "\n");
  2987. free (buf, M_TEMP);
  2988. return;
  2989. }
  2990. static void
  2991. sppp_ipcp_tlu(struct sppp *sp)
  2992. {
  2993. /* we are up - notify isdn daemon */
  2994. if (sp->pp_con)
  2995. sp->pp_con(sp);
  2996. }
  2997. static void
  2998. sppp_ipcp_tld(struct sppp *sp)
  2999. {
  3000. }
  3001. static void
  3002. sppp_ipcp_tls(struct sppp *sp)
  3003. {
  3004. /* indicate to LCP that it must stay alive */
  3005. sp->lcp.protos |= (1 << IDX_IPCP);
  3006. }
  3007. static void
  3008. sppp_ipcp_tlf(struct sppp *sp)
  3009. {
  3010. /* we no longer need LCP */
  3011. sp->lcp.protos &= ~(1 << IDX_IPCP);
  3012. sppp_lcp_check_and_close(sp);
  3013. }
  3014. static void
  3015. sppp_ipcp_scr(struct sppp *sp)
  3016. {
  3017. char opt[6 /* compression */ + 6 /* address */];
  3018. u_long ouraddr;
  3019. int i = 0;
  3020. if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
  3021. opt[i++] = IPCP_OPT_COMPRESSION;
  3022. opt[i++] = 6;
  3023. opt[i++] = IPCP_COMP_VJ >> 8;
  3024. opt[i++] = IPCP_COMP_VJ;
  3025. opt[i++] = sp->ipcp.max_state;
  3026. opt[i++] = sp->ipcp.compress_cid;
  3027. }
  3028. if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
  3029. sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
  3030. opt[i++] = IPCP_OPT_ADDRESS;
  3031. opt[i++] = 6;
  3032. opt[i++] = ouraddr >> 24;
  3033. opt[i++] = ouraddr >> 16;
  3034. opt[i++] = ouraddr >> 8;
  3035. opt[i++] = ouraddr;
  3036. }
  3037. sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
  3038. sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
  3039. }
  3040. #else /* !INET */
  3041. static void
  3042. sppp_ipcp_init(struct sppp *sp)
  3043. {
  3044. }
  3045. static void
  3046. sppp_ipcp_up(struct sppp *sp)
  3047. {
  3048. }
  3049. static void
  3050. sppp_ipcp_down(struct sppp *sp)
  3051. {
  3052. }
  3053. static void
  3054. sppp_ipcp_open(struct sppp *sp)
  3055. {
  3056. }
  3057. static void
  3058. sppp_ipcp_close(struct sppp *sp)
  3059. {
  3060. }
  3061. static void
  3062. sppp_ipcp_TO(void *cookie)
  3063. {
  3064. }
  3065. static int
  3066. sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
  3067. {
  3068. return (0);
  3069. }
  3070. static void
  3071. sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
  3072. {
  3073. }
  3074. static void
  3075. sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
  3076. {
  3077. }
  3078. static void
  3079. sppp_ipcp_tlu(struct sppp *sp)
  3080. {
  3081. }
  3082. static void
  3083. sppp_ipcp_tld(struct sppp *sp)
  3084. {
  3085. }
  3086. static void
  3087. sppp_ipcp_tls(struct sppp *sp)
  3088. {
  3089. }
  3090. static void
  3091. sppp_ipcp_tlf(struct sppp *sp)
  3092. {
  3093. }
  3094. static void
  3095. sppp_ipcp_scr(struct sppp *sp)
  3096. {
  3097. }
  3098. #endif
  3099. /*
  3100. *--------------------------------------------------------------------------*
  3101. * *
  3102. * The IPv6CP implementation. *
  3103. * *
  3104. *--------------------------------------------------------------------------*
  3105. */
  3106. #ifdef INET6
  3107. static void
  3108. sppp_ipv6cp_init(struct sppp *sp)
  3109. {
  3110. sp->ipv6cp.opts = 0;
  3111. sp->ipv6cp.flags = 0;
  3112. sp->state[IDX_IPV6CP] = STATE_INITIAL;
  3113. sp->fail_counter[IDX_IPV6CP] = 0;
  3114. sp->pp_seq[IDX_IPV6CP] = 0;
  3115. sp->pp_rseq[IDX_IPV6CP] = 0;
  3116. callout_init(&sp->ch[IDX_IPV6CP], 1);
  3117. }
  3118. static void
  3119. sppp_ipv6cp_up(struct sppp *sp)
  3120. {
  3121. sppp_up_event(&ipv6cp, sp);
  3122. }
  3123. static void
  3124. sppp_ipv6cp_down(struct sppp *sp)
  3125. {
  3126. sppp_down_event(&ipv6cp, sp);
  3127. }
  3128. static void
  3129. sppp_ipv6cp_open(struct sppp *sp)
  3130. {
  3131. STDDCL;
  3132. struct in6_addr myaddr, hisaddr;
  3133. #ifdef IPV6CP_MYIFID_DYN
  3134. sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
  3135. #else
  3136. sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
  3137. #endif
  3138. sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
  3139. /*
  3140. * If we don't have our address, this probably means our
  3141. * interface doesn't want to talk IPv6 at all. (This could
  3142. * be the case if somebody wants to speak only IPX, for
  3143. * example.) Don't open IPv6CP in this case.
  3144. */
  3145. if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
  3146. /* XXX this message should go away */
  3147. if (debug)
  3148. log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
  3149. SPP_ARGS(ifp));
  3150. return;
  3151. }
  3152. sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
  3153. sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
  3154. sppp_open_event(&ipv6cp, sp);
  3155. }
  3156. static void
  3157. sppp_ipv6cp_close(struct sppp *sp)
  3158. {
  3159. sppp_close_event(&ipv6cp, sp);
  3160. }
  3161. static void
  3162. sppp_ipv6cp_TO(void *cookie)
  3163. {
  3164. sppp_to_event(&ipv6cp, (struct sppp *)cookie);
  3165. }
  3166. /*
  3167. * Analyze a configure request. Return true if it was agreeable, and
  3168. * caused action sca, false if it has been rejected or nak'ed, and
  3169. * caused action scn. (The return value is used to make the state
  3170. * transition decision in the state automaton.)
  3171. */
  3172. static int
  3173. sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
  3174. {
  3175. u_char *buf, *r, *p;
  3176. struct ifnet *ifp = SP2IFP(sp);
  3177. int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
  3178. struct in6_addr myaddr, desiredaddr, suggestaddr;
  3179. int ifidcount;
  3180. int type;
  3181. int collision, nohisaddr;
  3182. char ip6buf[INET6_ADDRSTRLEN];
  3183. len -= 4;
  3184. origlen = len;
  3185. /*
  3186. * Make sure to allocate a buf that can at least hold a
  3187. * conf-nak with an `address' option. We might need it below.
  3188. */
  3189. buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
  3190. if (! buf)
  3191. return (0);
  3192. /* pass 1: see if we can recognize them */
  3193. if (debug)
  3194. log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
  3195. SPP_ARGS(ifp));
  3196. p = (void*) (h+1);
  3197. ifidcount = 0;
  3198. for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
  3199. len-=p[1], p+=p[1]) {
  3200. if (debug)
  3201. log(-1, " %s", sppp_ipv6cp_opt_name(*p));
  3202. switch (*p) {
  3203. case IPV6CP_OPT_IFID:
  3204. if (len >= 10 && p[1] == 10 && ifidcount == 0) {
  3205. /* correctly formed address option */
  3206. ifidcount++;
  3207. continue;
  3208. }
  3209. if (debug)
  3210. log(-1, " [invalid]");
  3211. break;
  3212. #ifdef notyet
  3213. case IPV6CP_OPT_COMPRESSION:
  3214. if (len >= 4 && p[1] >= 4) {
  3215. /* correctly formed compress option */
  3216. continue;
  3217. }
  3218. if (debug)
  3219. log(-1, " [invalid]");
  3220. break;
  3221. #endif
  3222. default:
  3223. /* Others not supported. */
  3224. if (debug)
  3225. log(-1, " [rej]");
  3226. break;
  3227. }
  3228. /* Add the option to rejected list. */
  3229. bcopy (p, r, p[1]);
  3230. r += p[1];
  3231. rlen += p[1];
  3232. }
  3233. if (rlen) {
  3234. if (debug)
  3235. log(-1, " send conf-rej\n");
  3236. sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
  3237. goto end;
  3238. } else if (debug)
  3239. log(-1, "\n");
  3240. /* pass 2: parse option values */
  3241. sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
  3242. if (debug)
  3243. log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
  3244. SPP_ARGS(ifp));
  3245. p = (void*) (h+1);
  3246. len = origlen;
  3247. type = CONF_ACK;
  3248. for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
  3249. len-=p[1], p+=p[1]) {
  3250. if (debug)
  3251. log(-1, " %s", sppp_ipv6cp_opt_name(*p));
  3252. switch (*p) {
  3253. #ifdef notyet
  3254. case IPV6CP_OPT_COMPRESSION:
  3255. continue;
  3256. #endif
  3257. case IPV6CP_OPT_IFID:
  3258. bzero(&desiredaddr, sizeof(desiredaddr));
  3259. bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
  3260. collision = (bcmp(&desiredaddr.s6_addr[8],
  3261. &myaddr.s6_addr[8], 8) == 0);
  3262. nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
  3263. desiredaddr.s6_addr16[0] = htons(0xfe80);
  3264. (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
  3265. if (!collision && !nohisaddr) {
  3266. /* no collision, hisaddr known - Conf-Ack */
  3267. type = CONF_ACK;
  3268. if (debug) {
  3269. log(-1, " %s [%s]",
  3270. ip6_sprintf(ip6buf, &desiredaddr),
  3271. sppp_cp_type_name(type));
  3272. }
  3273. continue;
  3274. }
  3275. bzero(&suggestaddr, sizeof(suggestaddr));
  3276. if (collision && nohisaddr) {
  3277. /* collision, hisaddr unknown - Conf-Rej */
  3278. type = CONF_REJ;
  3279. bzero(&p[2], 8);
  3280. } else {
  3281. /*
  3282. * - no collision, hisaddr unknown, or
  3283. * - collision, hisaddr known
  3284. * Conf-Nak, suggest hisaddr
  3285. */
  3286. type = CONF_NAK;
  3287. sppp_suggest_ip6_addr(sp, &suggestaddr);
  3288. bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
  3289. }
  3290. if (debug)
  3291. log(-1, " %s [%s]",
  3292. ip6_sprintf(ip6buf, &desiredaddr),
  3293. sppp_cp_type_name(type));
  3294. break;
  3295. }
  3296. /* Add the option to nak'ed list. */
  3297. bcopy (p, r, p[1]);
  3298. r += p[1];
  3299. rlen += p[1];
  3300. }
  3301. if (rlen == 0 && type == CONF_ACK) {
  3302. if (debug)
  3303. log(-1, " send %s\n", sppp_cp_type_name(type));
  3304. sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
  3305. } else {
  3306. #ifdef DIAGNOSTIC
  3307. if (type == CONF_ACK)
  3308. panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
  3309. #endif
  3310. if (debug) {
  3311. log(-1, " send %s suggest %s\n",
  3312. sppp_cp_type_name(type),
  3313. ip6_sprintf(ip6buf, &suggestaddr));
  3314. }
  3315. sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
  3316. }
  3317. end:
  3318. free (buf, M_TEMP);
  3319. return (rlen == 0);
  3320. }
  3321. /*
  3322. * Analyze the IPv6CP Configure-Reject option list, and adjust our
  3323. * negotiation.
  3324. */
  3325. static void
  3326. sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
  3327. {
  3328. u_char *buf, *p;
  3329. struct ifnet *ifp = SP2IFP(sp);
  3330. int debug = ifp->if_flags & IFF_DEBUG;
  3331. len -= 4;
  3332. buf = malloc (len, M_TEMP, M_NOWAIT);
  3333. if (!buf)
  3334. return;
  3335. if (debug)
  3336. log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
  3337. SPP_ARGS(ifp));
  3338. p = (void*) (h+1);
  3339. for (; len >= 2 && p[1] >= 2 && len >= p[1];
  3340. len -= p[1], p += p[1]) {
  3341. if (debug)
  3342. log(-1, " %s", sppp_ipv6cp_opt_name(*p));
  3343. switch (*p) {
  3344. case IPV6CP_OPT_IFID:
  3345. /*
  3346. * Peer doesn't grok address option. This is
  3347. * bad. XXX Should we better give up here?
  3348. */
  3349. sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
  3350. break;
  3351. #ifdef notyet
  3352. case IPV6CP_OPT_COMPRESS:
  3353. sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
  3354. break;
  3355. #endif
  3356. }
  3357. }
  3358. if (debug)
  3359. log(-1, "\n");
  3360. free (buf, M_TEMP);
  3361. return;
  3362. }
  3363. /*
  3364. * Analyze the IPv6CP Configure-NAK option list, and adjust our
  3365. * negotiation.
  3366. */
  3367. static void
  3368. sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
  3369. {
  3370. u_char *buf, *p;
  3371. struct ifnet *ifp = SP2IFP(sp);
  3372. int debug = ifp->if_flags & IFF_DEBUG;
  3373. struct in6_addr suggestaddr;
  3374. char ip6buf[INET6_ADDRSTRLEN];
  3375. len -= 4;
  3376. buf = malloc (len, M_TEMP, M_NOWAIT);
  3377. if (!buf)
  3378. return;
  3379. if (debug)
  3380. log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
  3381. SPP_ARGS(ifp));
  3382. p = (void*) (h+1);
  3383. for (; len >= 2 && p[1] >= 2 && len >= p[1];
  3384. len -= p[1], p += p[1]) {
  3385. if (debug)
  3386. log(-1, " %s", sppp_ipv6cp_opt_name(*p));
  3387. switch (*p) {
  3388. case IPV6CP_OPT_IFID:
  3389. /*
  3390. * Peer doesn't like our local ifid. See
  3391. * if we can do something for him. We'll drop
  3392. * him our address then.
  3393. */
  3394. if (len < 10 || p[1] != 10)
  3395. break;
  3396. bzero(&suggestaddr, sizeof(suggestaddr));
  3397. suggestaddr.s6_addr16[0] = htons(0xfe80);
  3398. (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
  3399. bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
  3400. sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
  3401. if (debug)
  3402. log(-1, " [suggestaddr %s]",
  3403. ip6_sprintf(ip6buf, &suggestaddr));
  3404. #ifdef IPV6CP_MYIFID_DYN
  3405. /*
  3406. * When doing dynamic address assignment,
  3407. * we accept his offer.
  3408. */
  3409. if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
  3410. struct in6_addr lastsuggest;
  3411. /*
  3412. * If <suggested myaddr from peer> equals to
  3413. * <hisaddr we have suggested last time>,
  3414. * we have a collision. generate new random
  3415. * ifid.
  3416. */
  3417. sppp_suggest_ip6_addr(&lastsuggest);
  3418. if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
  3419. lastsuggest)) {
  3420. if (debug)
  3421. log(-1, " [random]");
  3422. sppp_gen_ip6_addr(sp, &suggestaddr);
  3423. }
  3424. sppp_set_ip6_addr(sp, &suggestaddr, 0);
  3425. if (debug)
  3426. log(-1, " [agree]");
  3427. sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
  3428. }
  3429. #else
  3430. /*
  3431. * Since we do not do dynamic address assignment,
  3432. * we ignore it and thus continue to negotiate
  3433. * our already existing value. This can possibly
  3434. * go into infinite request-reject loop.
  3435. *
  3436. * This is not likely because we normally use
  3437. * ifid based on MAC-address.
  3438. * If you have no ethernet card on the node, too bad.
  3439. * XXX should we use fail_counter?
  3440. */
  3441. #endif
  3442. break;
  3443. #ifdef notyet
  3444. case IPV6CP_OPT_COMPRESS:
  3445. /*
  3446. * Peer wants different compression parameters.
  3447. */
  3448. break;
  3449. #endif
  3450. }
  3451. }
  3452. if (debug)
  3453. log(-1, "\n");
  3454. free (buf, M_TEMP);
  3455. return;
  3456. }
  3457. static void
  3458. sppp_ipv6cp_tlu(struct sppp *sp)
  3459. {
  3460. /* we are up - notify isdn daemon */
  3461. if (sp->pp_con)
  3462. sp->pp_con(sp);
  3463. }
  3464. static void
  3465. sppp_ipv6cp_tld(struct sppp *sp)
  3466. {
  3467. }
  3468. static void
  3469. sppp_ipv6cp_tls(struct sppp *sp)
  3470. {
  3471. /* indicate to LCP that it must stay alive */
  3472. sp->lcp.protos |= (1 << IDX_IPV6CP);
  3473. }
  3474. static void
  3475. sppp_ipv6cp_tlf(struct sppp *sp)
  3476. {
  3477. #if 0 /* need #if 0 to close IPv6CP properly */
  3478. /* we no longer need LCP */
  3479. sp->lcp.protos &= ~(1 << IDX_IPV6CP);
  3480. sppp_lcp_check_and_close(sp);
  3481. #endif
  3482. }
  3483. static void
  3484. sppp_ipv6cp_scr(struct sppp *sp)
  3485. {
  3486. char opt[10 /* ifid */ + 4 /* compression, minimum */];
  3487. struct in6_addr ouraddr;
  3488. int i = 0;
  3489. if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
  3490. sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
  3491. opt[i++] = IPV6CP_OPT_IFID;
  3492. opt[i++] = 10;
  3493. bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
  3494. i += 8;
  3495. }
  3496. #ifdef notyet
  3497. if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
  3498. opt[i++] = IPV6CP_OPT_COMPRESSION;
  3499. opt[i++] = 4;
  3500. opt[i++] = 0; /* TBD */
  3501. opt[i++] = 0; /* TBD */
  3502. /* variable length data may follow */
  3503. }
  3504. #endif
  3505. sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
  3506. sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
  3507. }
  3508. #else /*INET6*/
  3509. static void sppp_ipv6cp_init(struct sppp *sp)
  3510. {
  3511. }
  3512. static void sppp_ipv6cp_up(struct sppp *sp)
  3513. {
  3514. }
  3515. static void sppp_ipv6cp_down(struct sppp *sp)
  3516. {
  3517. }
  3518. static void sppp_ipv6cp_open(struct sppp *sp)
  3519. {
  3520. }
  3521. static void sppp_ipv6cp_close(struct sppp *sp)
  3522. {
  3523. }
  3524. static void sppp_ipv6cp_TO(void *sp)
  3525. {
  3526. }
  3527. static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
  3528. {
  3529. return 0;
  3530. }
  3531. static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
  3532. {
  3533. }
  3534. static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
  3535. {
  3536. }
  3537. static void sppp_ipv6cp_tlu(struct sppp *sp)
  3538. {
  3539. }
  3540. static void sppp_ipv6cp_tld(struct sppp *sp)
  3541. {
  3542. }
  3543. static void sppp_ipv6cp_tls(struct sppp *sp)
  3544. {
  3545. }
  3546. static void sppp_ipv6cp_tlf(struct sppp *sp)
  3547. {
  3548. }
  3549. static void sppp_ipv6cp_scr(struct sppp *sp)
  3550. {
  3551. }
  3552. #endif /*INET6*/
  3553. /*
  3554. *--------------------------------------------------------------------------*
  3555. * *
  3556. * The CHAP implementation. *
  3557. * *
  3558. *--------------------------------------------------------------------------*
  3559. */
  3560. /*
  3561. * The authentication protocols don't employ a full-fledged state machine as
  3562. * the control protocols do, since they do have Open and Close events, but
  3563. * not Up and Down, nor are they explicitly terminated. Also, use of the
  3564. * authentication protocols may be different in both directions (this makes
  3565. * sense, think of a machine that never accepts incoming calls but only
  3566. * calls out, it doesn't require the called party to authenticate itself).
  3567. *
  3568. * Our state machine for the local authentication protocol (we are requesting
  3569. * the peer to authenticate) looks like:
  3570. *
  3571. * RCA-
  3572. * +--------------------------------------------+
  3573. * V scn,tld|
  3574. * +--------+ Close +---------+ RCA+
  3575. * | |<----------------------------------| |------+
  3576. * +--->| Closed | TO* | Opened | sca |
  3577. * | | |-----+ +-------| |<-----+
  3578. * | +--------+ irc | | +---------+
  3579. * | ^ | | ^
  3580. * | | | | |
  3581. * | | | | |
  3582. * | TO-| | | |
  3583. * | |tld TO+ V | |
  3584. * | | +------->+ | |
  3585. * | | | | | |
  3586. * | +--------+ V | |
  3587. * | | |<----+<--------------------+ |
  3588. * | | Req- | scr |
  3589. * | | Sent | |
  3590. * | | | |
  3591. * | +--------+ |
  3592. * | RCA- | | RCA+ |
  3593. * +------+ +------------------------------------------+
  3594. * scn,tld sca,irc,ict,tlu
  3595. *
  3596. *
  3597. * with:
  3598. *
  3599. * Open: LCP reached authentication phase
  3600. * Close: LCP reached terminate phase
  3601. *
  3602. * RCA+: received reply (pap-req, chap-response), acceptable
  3603. * RCN: received reply (pap-req, chap-response), not acceptable
  3604. * TO+: timeout with restart counter >= 0
  3605. * TO-: timeout with restart counter < 0
  3606. * TO*: reschedule timeout for CHAP
  3607. *
  3608. * scr: send request packet (none for PAP, chap-challenge)
  3609. * sca: send ack packet (pap-ack, chap-success)
  3610. * scn: send nak packet (pap-nak, chap-failure)
  3611. * ict: initialize re-challenge timer (CHAP only)
  3612. *
  3613. * tlu: this-layer-up, LCP reaches network phase
  3614. * tld: this-layer-down, LCP enters terminate phase
  3615. *
  3616. * Note that in CHAP mode, after sending a new challenge, while the state
  3617. * automaton falls back into Req-Sent state, it doesn't signal a tld
  3618. * event to LCP, so LCP remains in network phase. Only after not getting
  3619. * any response (or after getting an unacceptable response), CHAP closes,
  3620. * causing LCP to enter terminate phase.
  3621. *
  3622. * With PAP, there is no initial request that can be sent. The peer is
  3623. * expected to send one based on the successful negotiation of PAP as
  3624. * the authentication protocol during the LCP option negotiation.
  3625. *
  3626. * Incoming authentication protocol requests (remote requests
  3627. * authentication, we are peer) don't employ a state machine at all,
  3628. * they are simply answered. Some peers [Ascend P50 firmware rev
  3629. * 4.50] react allergically when sending IPCP requests while they are
  3630. * still in authentication phase (thereby violating the standard that
  3631. * demands that these NCP packets are to be discarded), so we keep
  3632. * track of the peer demanding us to authenticate, and only proceed to
  3633. * phase network once we've seen a positive acknowledge for the
  3634. * authentication.
  3635. */
  3636. /*
  3637. * Handle incoming CHAP packets.
  3638. */
  3639. static void
  3640. sppp_chap_input(struct sppp *sp, struct mbuf *m)
  3641. {
  3642. STDDCL;
  3643. struct lcp_header *h;
  3644. int len;
  3645. u_char *value, *name, digest[AUTHKEYLEN], dsize;
  3646. int value_len, name_len;
  3647. MD5_CTX ctx;
  3648. len = m->m_pkthdr.len;
  3649. if (len < 4) {
  3650. if (debug)
  3651. log(LOG_DEBUG,
  3652. SPP_FMT "chap invalid packet length: %d bytes\n",
  3653. SPP_ARGS(ifp), len);
  3654. return;
  3655. }
  3656. h = mtod (m, struct lcp_header*);
  3657. if (len > ntohs (h->len))
  3658. len = ntohs (h->len);
  3659. switch (h->type) {
  3660. /* challenge, failure and success are his authproto */
  3661. case CHAP_CHALLENGE:
  3662. value = 1 + (u_char*)(h+1);
  3663. value_len = value[-1];
  3664. name = value + value_len;
  3665. name_len = len - value_len - 5;
  3666. if (name_len < 0) {
  3667. if (debug) {
  3668. log(LOG_DEBUG,
  3669. SPP_FMT "chap corrupted challenge "
  3670. "<%s id=0x%x len=%d",
  3671. SPP_ARGS(ifp),
  3672. sppp_auth_type_name(PPP_CHAP, h->type),
  3673. h->ident, ntohs(h->len));
  3674. sppp_print_bytes((u_char*) (h+1), len-4);
  3675. log(-1, ">\n");
  3676. }
  3677. break;
  3678. }
  3679. if (debug) {
  3680. log(LOG_DEBUG,
  3681. SPP_FMT "chap input <%s id=0x%x len=%d name=",
  3682. SPP_ARGS(ifp),
  3683. sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
  3684. ntohs(h->len));
  3685. sppp_print_string((char*) name, name_len);
  3686. log(-1, " value-size=%d value=", value_len);
  3687. sppp_print_bytes(value, value_len);
  3688. log(-1, ">\n");
  3689. }
  3690. /* Compute reply value. */
  3691. MD5Init(&ctx);
  3692. MD5Update(&ctx, &h->ident, 1);
  3693. MD5Update(&ctx, sp->myauth.secret,
  3694. sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
  3695. MD5Update(&ctx, value, value_len);
  3696. MD5Final(digest, &ctx);
  3697. dsize = sizeof digest;
  3698. sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
  3699. sizeof dsize, (const char *)&dsize,
  3700. sizeof digest, digest,
  3701. (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
  3702. sp->myauth.name,
  3703. 0);
  3704. break;
  3705. case CHAP_SUCCESS:
  3706. if (debug) {
  3707. log(LOG_DEBUG, SPP_FMT "chap success",
  3708. SPP_ARGS(ifp));
  3709. if (len > 4) {
  3710. log(-1, ": ");
  3711. sppp_print_string((char*)(h + 1), len - 4);
  3712. }
  3713. log(-1, "\n");
  3714. }
  3715. SPPP_LOCK(sp);
  3716. sp->pp_flags &= ~PP_NEEDAUTH;
  3717. if (sp->myauth.proto == PPP_CHAP &&
  3718. (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
  3719. (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
  3720. /*
  3721. * We are authenticator for CHAP but didn't
  3722. * complete yet. Leave it to tlu to proceed
  3723. * to network phase.
  3724. */
  3725. SPPP_UNLOCK(sp);
  3726. break;
  3727. }
  3728. SPPP_UNLOCK(sp);
  3729. sppp_phase_network(sp);
  3730. break;
  3731. case CHAP_FAILURE:
  3732. if (debug) {
  3733. log(LOG_INFO, SPP_FMT "chap failure",
  3734. SPP_ARGS(ifp));
  3735. if (len > 4) {
  3736. log(-1, ": ");
  3737. sppp_print_string((char*)(h + 1), len - 4);
  3738. }
  3739. log(-1, "\n");
  3740. } else
  3741. log(LOG_INFO, SPP_FMT "chap failure\n",
  3742. SPP_ARGS(ifp));
  3743. /* await LCP shutdown by authenticator */
  3744. break;
  3745. /* response is my authproto */
  3746. case CHAP_RESPONSE:
  3747. value = 1 + (u_char*)(h+1);
  3748. value_len = value[-1];
  3749. name = value + value_len;
  3750. name_len = len - value_len - 5;
  3751. if (name_len < 0) {
  3752. if (debug) {
  3753. log(LOG_DEBUG,
  3754. SPP_FMT "chap corrupted response "
  3755. "<%s id=0x%x len=%d",
  3756. SPP_ARGS(ifp),
  3757. sppp_auth_type_name(PPP_CHAP, h->type),
  3758. h->ident, ntohs(h->len));
  3759. sppp_print_bytes((u_char*)(h+1), len-4);
  3760. log(-1, ">\n");
  3761. }
  3762. break;
  3763. }
  3764. if (h->ident != sp->confid[IDX_CHAP]) {
  3765. if (debug)
  3766. log(LOG_DEBUG,
  3767. SPP_FMT "chap dropping response for old ID "
  3768. "(got %d, expected %d)\n",
  3769. SPP_ARGS(ifp),
  3770. h->ident, sp->confid[IDX_CHAP]);
  3771. break;
  3772. }
  3773. if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
  3774. || bcmp(name, sp->hisauth.name, name_len) != 0) {
  3775. log(LOG_INFO, SPP_FMT "chap response, his name ",
  3776. SPP_ARGS(ifp));
  3777. sppp_print_string(name, name_len);
  3778. log(-1, " != expected ");
  3779. sppp_print_string(sp->hisauth.name,
  3780. sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
  3781. log(-1, "\n");
  3782. }
  3783. if (debug) {
  3784. log(LOG_DEBUG, SPP_FMT "chap input(%s) "
  3785. "<%s id=0x%x len=%d name=",
  3786. SPP_ARGS(ifp),
  3787. sppp_state_name(sp->state[IDX_CHAP]),
  3788. sppp_auth_type_name(PPP_CHAP, h->type),
  3789. h->ident, ntohs (h->len));
  3790. sppp_print_string((char*)name, name_len);
  3791. log(-1, " value-size=%d value=", value_len);
  3792. sppp_print_bytes(value, value_len);
  3793. log(-1, ">\n");
  3794. }
  3795. if (value_len != AUTHKEYLEN) {
  3796. if (debug)
  3797. log(LOG_DEBUG,
  3798. SPP_FMT "chap bad hash value length: "
  3799. "%d bytes, should be %d\n",
  3800. SPP_ARGS(ifp), value_len,
  3801. AUTHKEYLEN);
  3802. break;
  3803. }
  3804. MD5Init(&ctx);
  3805. MD5Update(&ctx, &h->ident, 1);
  3806. MD5Update(&ctx, sp->hisauth.secret,
  3807. sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
  3808. MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
  3809. MD5Final(digest, &ctx);
  3810. #define FAILMSG "Failed..."
  3811. #define SUCCMSG "Welcome!"
  3812. if (value_len != sizeof digest ||
  3813. bcmp(digest, value, value_len) != 0) {
  3814. /* action scn, tld */
  3815. sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
  3816. sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
  3817. 0);
  3818. chap.tld(sp);
  3819. break;
  3820. }
  3821. /* action sca, perhaps tlu */
  3822. if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
  3823. sp->state[IDX_CHAP] == STATE_OPENED)
  3824. sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
  3825. sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
  3826. 0);
  3827. if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
  3828. sppp_cp_change_state(&chap, sp, STATE_OPENED);
  3829. chap.tlu(sp);
  3830. }
  3831. break;
  3832. default:
  3833. /* Unknown CHAP packet type -- ignore. */
  3834. if (debug) {
  3835. log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
  3836. "<0x%x id=0x%xh len=%d",
  3837. SPP_ARGS(ifp),
  3838. sppp_state_name(sp->state[IDX_CHAP]),
  3839. h->type, h->ident, ntohs(h->len));
  3840. sppp_print_bytes((u_char*)(h+1), len-4);
  3841. log(-1, ">\n");
  3842. }
  3843. break;
  3844. }
  3845. }
  3846. static void
  3847. sppp_chap_init(struct sppp *sp)
  3848. {
  3849. /* Chap doesn't have STATE_INITIAL at all. */
  3850. sp->state[IDX_CHAP] = STATE_CLOSED;
  3851. sp->fail_counter[IDX_CHAP] = 0;
  3852. sp->pp_seq[IDX_CHAP] = 0;
  3853. sp->pp_rseq[IDX_CHAP] = 0;
  3854. callout_init(&sp->ch[IDX_CHAP], 1);
  3855. }
  3856. static void
  3857. sppp_chap_open(struct sppp *sp)
  3858. {
  3859. if (sp->myauth.proto == PPP_CHAP &&
  3860. (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
  3861. /* we are authenticator for CHAP, start it */
  3862. chap.scr(sp);
  3863. sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
  3864. sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
  3865. }
  3866. /* nothing to be done if we are peer, await a challenge */
  3867. }
  3868. static void
  3869. sppp_chap_close(struct sppp *sp)
  3870. {
  3871. if (sp->state[IDX_CHAP] != STATE_CLOSED)
  3872. sppp_cp_change_state(&chap, sp, STATE_CLOSED);
  3873. }
  3874. static void
  3875. sppp_chap_TO(void *cookie)
  3876. {
  3877. struct sppp *sp = (struct sppp *)cookie;
  3878. STDDCL;
  3879. SPPP_LOCK(sp);
  3880. if (debug)
  3881. log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
  3882. SPP_ARGS(ifp),
  3883. sppp_state_name(sp->state[IDX_CHAP]),
  3884. sp->rst_counter[IDX_CHAP]);
  3885. if (--sp->rst_counter[IDX_CHAP] < 0)
  3886. /* TO- event */
  3887. switch (sp->state[IDX_CHAP]) {
  3888. case STATE_REQ_SENT:
  3889. chap.tld(sp);
  3890. sppp_cp_change_state(&chap, sp, STATE_CLOSED);
  3891. break;
  3892. }
  3893. else
  3894. /* TO+ (or TO*) event */
  3895. switch (sp->state[IDX_CHAP]) {
  3896. case STATE_OPENED:
  3897. /* TO* event */
  3898. sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
  3899. /* FALLTHROUGH */
  3900. case STATE_REQ_SENT:
  3901. chap.scr(sp);
  3902. /* sppp_cp_change_state() will restart the timer */
  3903. sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
  3904. break;
  3905. }
  3906. SPPP_UNLOCK(sp);
  3907. }
  3908. static void
  3909. sppp_chap_tlu(struct sppp *sp)
  3910. {
  3911. STDDCL;
  3912. int i;
  3913. i = 0;
  3914. sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
  3915. /*
  3916. * Some broken CHAP implementations (Conware CoNet, firmware
  3917. * 4.0.?) don't want to re-authenticate their CHAP once the
  3918. * initial challenge-response exchange has taken place.
  3919. * Provide for an option to avoid rechallenges.
  3920. */
  3921. if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
  3922. /*
  3923. * Compute the re-challenge timeout. This will yield
  3924. * a number between 300 and 810 seconds.
  3925. */
  3926. i = 300 + ((unsigned)(random() & 0xff00) >> 7);
  3927. callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
  3928. }
  3929. if (debug) {
  3930. log(LOG_DEBUG,
  3931. SPP_FMT "chap %s, ",
  3932. SPP_ARGS(ifp),
  3933. sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
  3934. if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
  3935. log(-1, "next re-challenge in %d seconds\n", i);
  3936. else
  3937. log(-1, "re-challenging suppressed\n");
  3938. }
  3939. SPPP_LOCK(sp);
  3940. /* indicate to LCP that we need to be closed down */
  3941. sp->lcp.protos |= (1 << IDX_CHAP);
  3942. if (sp->pp_flags & PP_NEEDAUTH) {
  3943. /*
  3944. * Remote is authenticator, but his auth proto didn't
  3945. * complete yet. Defer the transition to network
  3946. * phase.
  3947. */
  3948. SPPP_UNLOCK(sp);
  3949. return;
  3950. }
  3951. SPPP_UNLOCK(sp);
  3952. /*
  3953. * If we are already in phase network, we are done here. This
  3954. * is the case if this is a dummy tlu event after a re-challenge.
  3955. */
  3956. if (sp->pp_phase != PHASE_NETWORK)
  3957. sppp_phase_network(sp);
  3958. }
  3959. static void
  3960. sppp_chap_tld(struct sppp *sp)
  3961. {
  3962. STDDCL;
  3963. if (debug)
  3964. log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
  3965. callout_stop(&sp->ch[IDX_CHAP]);
  3966. sp->lcp.protos &= ~(1 << IDX_CHAP);
  3967. lcp.Close(sp);
  3968. }
  3969. static void
  3970. sppp_chap_scr(struct sppp *sp)
  3971. {
  3972. u_long *ch;
  3973. u_char clen;
  3974. /* Compute random challenge. */
  3975. ch = (u_long *)sp->myauth.challenge;
  3976. arc4random_buf(ch, 4 * sizeof(*ch));
  3977. clen = AUTHKEYLEN;
  3978. sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
  3979. sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
  3980. sizeof clen, (const char *)&clen,
  3981. (size_t)AUTHKEYLEN, sp->myauth.challenge,
  3982. (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
  3983. sp->myauth.name,
  3984. 0);
  3985. }
  3986. /*
  3987. *--------------------------------------------------------------------------*
  3988. * *
  3989. * The PAP implementation. *
  3990. * *
  3991. *--------------------------------------------------------------------------*
  3992. */
  3993. /*
  3994. * For PAP, we need to keep a little state also if we are the peer, not the
  3995. * authenticator. This is since we don't get a request to authenticate, but
  3996. * have to repeatedly authenticate ourself until we got a response (or the
  3997. * retry counter is expired).
  3998. */
  3999. /*
  4000. * Handle incoming PAP packets. */
  4001. static void
  4002. sppp_pap_input(struct sppp *sp, struct mbuf *m)
  4003. {
  4004. STDDCL;
  4005. struct lcp_header *h;
  4006. int len;
  4007. u_char *name, *passwd, mlen;
  4008. int name_len, passwd_len;
  4009. len = m->m_pkthdr.len;
  4010. if (len < 5) {
  4011. if (debug)
  4012. log(LOG_DEBUG,
  4013. SPP_FMT "pap invalid packet length: %d bytes\n",
  4014. SPP_ARGS(ifp), len);
  4015. return;
  4016. }
  4017. h = mtod (m, struct lcp_header*);
  4018. if (len > ntohs (h->len))
  4019. len = ntohs (h->len);
  4020. switch (h->type) {
  4021. /* PAP request is my authproto */
  4022. case PAP_REQ:
  4023. name = 1 + (u_char*)(h+1);
  4024. name_len = name[-1];
  4025. passwd = name + name_len + 1;
  4026. if (name_len > len - 6 ||
  4027. (passwd_len = passwd[-1]) > len - 6 - name_len) {
  4028. if (debug) {
  4029. log(LOG_DEBUG, SPP_FMT "pap corrupted input "
  4030. "<%s id=0x%x len=%d",
  4031. SPP_ARGS(ifp),
  4032. sppp_auth_type_name(PPP_PAP, h->type),
  4033. h->ident, ntohs(h->len));
  4034. sppp_print_bytes((u_char*)(h+1), len-4);
  4035. log(-1, ">\n");
  4036. }
  4037. break;
  4038. }
  4039. if (debug) {
  4040. log(LOG_DEBUG, SPP_FMT "pap input(%s) "
  4041. "<%s id=0x%x len=%d name=",
  4042. SPP_ARGS(ifp),
  4043. sppp_state_name(sp->state[IDX_PAP]),
  4044. sppp_auth_type_name(PPP_PAP, h->type),
  4045. h->ident, ntohs(h->len));
  4046. sppp_print_string((char*)name, name_len);
  4047. log(-1, " passwd=");
  4048. sppp_print_string((char*)passwd, passwd_len);
  4049. log(-1, ">\n");
  4050. }
  4051. if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
  4052. passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
  4053. bcmp(name, sp->hisauth.name, name_len) != 0 ||
  4054. bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
  4055. /* action scn, tld */
  4056. mlen = sizeof(FAILMSG) - 1;
  4057. sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
  4058. sizeof mlen, (const char *)&mlen,
  4059. sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
  4060. 0);
  4061. pap.tld(sp);
  4062. break;
  4063. }
  4064. /* action sca, perhaps tlu */
  4065. if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
  4066. sp->state[IDX_PAP] == STATE_OPENED) {
  4067. mlen = sizeof(SUCCMSG) - 1;
  4068. sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
  4069. sizeof mlen, (const char *)&mlen,
  4070. sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
  4071. 0);
  4072. }
  4073. if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
  4074. sppp_cp_change_state(&pap, sp, STATE_OPENED);
  4075. pap.tlu(sp);
  4076. }
  4077. break;
  4078. /* ack and nak are his authproto */
  4079. case PAP_ACK:
  4080. callout_stop(&sp->pap_my_to_ch);
  4081. if (debug) {
  4082. log(LOG_DEBUG, SPP_FMT "pap success",
  4083. SPP_ARGS(ifp));
  4084. name_len = *((char *)h);
  4085. if (len > 5 && name_len) {
  4086. log(-1, ": ");
  4087. sppp_print_string((char*)(h+1), name_len);
  4088. }
  4089. log(-1, "\n");
  4090. }
  4091. SPPP_LOCK(sp);
  4092. sp->pp_flags &= ~PP_NEEDAUTH;
  4093. if (sp->myauth.proto == PPP_PAP &&
  4094. (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
  4095. (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
  4096. /*
  4097. * We are authenticator for PAP but didn't
  4098. * complete yet. Leave it to tlu to proceed
  4099. * to network phase.
  4100. */
  4101. SPPP_UNLOCK(sp);
  4102. break;
  4103. }
  4104. SPPP_UNLOCK(sp);
  4105. sppp_phase_network(sp);
  4106. break;
  4107. case PAP_NAK:
  4108. callout_stop (&sp->pap_my_to_ch);
  4109. if (debug) {
  4110. log(LOG_INFO, SPP_FMT "pap failure",
  4111. SPP_ARGS(ifp));
  4112. name_len = *((char *)h);
  4113. if (len > 5 && name_len) {
  4114. log(-1, ": ");
  4115. sppp_print_string((char*)(h+1), name_len);
  4116. }
  4117. log(-1, "\n");
  4118. } else
  4119. log(LOG_INFO, SPP_FMT "pap failure\n",
  4120. SPP_ARGS(ifp));
  4121. /* await LCP shutdown by authenticator */
  4122. break;
  4123. default:
  4124. /* Unknown PAP packet type -- ignore. */
  4125. if (debug) {
  4126. log(LOG_DEBUG, SPP_FMT "pap corrupted input "
  4127. "<0x%x id=0x%x len=%d",
  4128. SPP_ARGS(ifp),
  4129. h->type, h->ident, ntohs(h->len));
  4130. sppp_print_bytes((u_char*)(h+1), len-4);
  4131. log(-1, ">\n");
  4132. }
  4133. break;
  4134. }
  4135. }
  4136. static void
  4137. sppp_pap_init(struct sppp *sp)
  4138. {
  4139. /* PAP doesn't have STATE_INITIAL at all. */
  4140. sp->state[IDX_PAP] = STATE_CLOSED;
  4141. sp->fail_counter[IDX_PAP] = 0;
  4142. sp->pp_seq[IDX_PAP] = 0;
  4143. sp->pp_rseq[IDX_PAP] = 0;
  4144. callout_init(&sp->ch[IDX_PAP], 1);
  4145. callout_init(&sp->pap_my_to_ch, 1);
  4146. }
  4147. static void
  4148. sppp_pap_open(struct sppp *sp)
  4149. {
  4150. if (sp->hisauth.proto == PPP_PAP &&
  4151. (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
  4152. /* we are authenticator for PAP, start our timer */
  4153. sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
  4154. sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
  4155. }
  4156. if (sp->myauth.proto == PPP_PAP) {
  4157. /* we are peer, send a request, and start a timer */
  4158. pap.scr(sp);
  4159. callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
  4160. sppp_pap_my_TO, (void *)sp);
  4161. }
  4162. }
  4163. static void
  4164. sppp_pap_close(struct sppp *sp)
  4165. {
  4166. if (sp->state[IDX_PAP] != STATE_CLOSED)
  4167. sppp_cp_change_state(&pap, sp, STATE_CLOSED);
  4168. }
  4169. /*
  4170. * That's the timeout routine if we are authenticator. Since the
  4171. * authenticator is basically passive in PAP, we can't do much here.
  4172. */
  4173. static void
  4174. sppp_pap_TO(void *cookie)
  4175. {
  4176. struct sppp *sp = (struct sppp *)cookie;
  4177. STDDCL;
  4178. SPPP_LOCK(sp);
  4179. if (debug)
  4180. log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
  4181. SPP_ARGS(ifp),
  4182. sppp_state_name(sp->state[IDX_PAP]),
  4183. sp->rst_counter[IDX_PAP]);
  4184. if (--sp->rst_counter[IDX_PAP] < 0)
  4185. /* TO- event */
  4186. switch (sp->state[IDX_PAP]) {
  4187. case STATE_REQ_SENT:
  4188. pap.tld(sp);
  4189. sppp_cp_change_state(&pap, sp, STATE_CLOSED);
  4190. break;
  4191. }
  4192. else
  4193. /* TO+ event, not very much we could do */
  4194. switch (sp->state[IDX_PAP]) {
  4195. case STATE_REQ_SENT:
  4196. /* sppp_cp_change_state() will restart the timer */
  4197. sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
  4198. break;
  4199. }
  4200. SPPP_UNLOCK(sp);
  4201. }
  4202. /*
  4203. * That's the timeout handler if we are peer. Since the peer is active,
  4204. * we need to retransmit our PAP request since it is apparently lost.
  4205. * XXX We should impose a max counter.
  4206. */
  4207. static void
  4208. sppp_pap_my_TO(void *cookie)
  4209. {
  4210. struct sppp *sp = (struct sppp *)cookie;
  4211. STDDCL;
  4212. if (debug)
  4213. log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
  4214. SPP_ARGS(ifp));
  4215. SPPP_LOCK(sp);
  4216. pap.scr(sp);
  4217. SPPP_UNLOCK(sp);
  4218. }
  4219. static void
  4220. sppp_pap_tlu(struct sppp *sp)
  4221. {
  4222. STDDCL;
  4223. sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
  4224. if (debug)
  4225. log(LOG_DEBUG, SPP_FMT "%s tlu\n",
  4226. SPP_ARGS(ifp), pap.name);
  4227. SPPP_LOCK(sp);
  4228. /* indicate to LCP that we need to be closed down */
  4229. sp->lcp.protos |= (1 << IDX_PAP);
  4230. if (sp->pp_flags & PP_NEEDAUTH) {
  4231. /*
  4232. * Remote is authenticator, but his auth proto didn't
  4233. * complete yet. Defer the transition to network
  4234. * phase.
  4235. */
  4236. SPPP_UNLOCK(sp);
  4237. return;
  4238. }
  4239. SPPP_UNLOCK(sp);
  4240. sppp_phase_network(sp);
  4241. }
  4242. static void
  4243. sppp_pap_tld(struct sppp *sp)
  4244. {
  4245. STDDCL;
  4246. if (debug)
  4247. log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
  4248. callout_stop (&sp->ch[IDX_PAP]);
  4249. callout_stop (&sp->pap_my_to_ch);
  4250. sp->lcp.protos &= ~(1 << IDX_PAP);
  4251. lcp.Close(sp);
  4252. }
  4253. static void
  4254. sppp_pap_scr(struct sppp *sp)
  4255. {
  4256. u_char idlen, pwdlen;
  4257. sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
  4258. pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
  4259. idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
  4260. sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
  4261. sizeof idlen, (const char *)&idlen,
  4262. (size_t)idlen, sp->myauth.name,
  4263. sizeof pwdlen, (const char *)&pwdlen,
  4264. (size_t)pwdlen, sp->myauth.secret,
  4265. 0);
  4266. }
  4267. /*
  4268. * Random miscellaneous functions.
  4269. */
  4270. /*
  4271. * Send a PAP or CHAP proto packet.
  4272. *
  4273. * Varadic function, each of the elements for the ellipsis is of type
  4274. * ``size_t mlen, const u_char *msg''. Processing will stop iff
  4275. * mlen == 0.
  4276. * NOTE: never declare variadic functions with types subject to type
  4277. * promotion (i.e. u_char). This is asking for big trouble depending
  4278. * on the architecture you are on...
  4279. */
  4280. static void
  4281. sppp_auth_send(const struct cp *cp, struct sppp *sp,
  4282. unsigned int type, unsigned int id,
  4283. ...)
  4284. {
  4285. STDDCL;
  4286. struct ppp_header *h;
  4287. struct lcp_header *lh;
  4288. struct mbuf *m;
  4289. u_char *p;
  4290. int len;
  4291. unsigned int mlen;
  4292. const char *msg;
  4293. va_list ap;
  4294. MGETHDR (m, M_NOWAIT, MT_DATA);
  4295. if (! m)
  4296. return;
  4297. m->m_pkthdr.rcvif = 0;
  4298. h = mtod (m, struct ppp_header*);
  4299. h->address = PPP_ALLSTATIONS; /* broadcast address */
  4300. h->control = PPP_UI; /* Unnumbered Info */
  4301. h->protocol = htons(cp->proto);
  4302. lh = (struct lcp_header*)(h + 1);
  4303. lh->type = type;
  4304. lh->ident = id;
  4305. p = (u_char*) (lh+1);
  4306. va_start(ap, id);
  4307. len = 0;
  4308. while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
  4309. msg = va_arg(ap, const char *);
  4310. len += mlen;
  4311. if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
  4312. va_end(ap);
  4313. m_freem(m);
  4314. return;
  4315. }
  4316. bcopy(msg, p, mlen);
  4317. p += mlen;
  4318. }
  4319. va_end(ap);
  4320. m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
  4321. lh->len = htons (LCP_HEADER_LEN + len);
  4322. if (debug) {
  4323. log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
  4324. SPP_ARGS(ifp), cp->name,
  4325. sppp_auth_type_name(cp->proto, lh->type),
  4326. lh->ident, ntohs(lh->len));
  4327. sppp_print_bytes((u_char*) (lh+1), len);
  4328. log(-1, ">\n");
  4329. }
  4330. if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
  4331. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  4332. }
  4333. /*
  4334. * Flush interface queue.
  4335. */
  4336. static void
  4337. sppp_qflush(struct ifqueue *ifq)
  4338. {
  4339. struct mbuf *m, *n;
  4340. n = ifq->ifq_head;
  4341. while ((m = n)) {
  4342. n = m->m_nextpkt;
  4343. m_freem (m);
  4344. }
  4345. ifq->ifq_head = 0;
  4346. ifq->ifq_tail = 0;
  4347. ifq->ifq_len = 0;
  4348. }
  4349. /*
  4350. * Send keepalive packets, every 10 seconds.
  4351. */
  4352. static void
  4353. sppp_keepalive(void *dummy)
  4354. {
  4355. struct sppp *sp = (struct sppp*)dummy;
  4356. struct ifnet *ifp = SP2IFP(sp);
  4357. SPPP_LOCK(sp);
  4358. /* Keepalive mode disabled or channel down? */
  4359. if (! (sp->pp_flags & PP_KEEPALIVE) ||
  4360. ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
  4361. goto out;
  4362. if (sp->pp_mode == PP_FR) {
  4363. sppp_fr_keepalive (sp);
  4364. goto out;
  4365. }
  4366. /* No keepalive in PPP mode if LCP not opened yet. */
  4367. if (sp->pp_mode != IFF_CISCO &&
  4368. sp->pp_phase < PHASE_AUTHENTICATE)
  4369. goto out;
  4370. if (sp->pp_alivecnt == MAXALIVECNT) {
  4371. /* No keepalive packets got. Stop the interface. */
  4372. printf (SPP_FMT "down\n", SPP_ARGS(ifp));
  4373. if_down (ifp);
  4374. sppp_qflush (&sp->pp_cpq);
  4375. if (sp->pp_mode != IFF_CISCO) {
  4376. /* XXX */
  4377. /* Shut down the PPP link. */
  4378. lcp.Down(sp);
  4379. /* Initiate negotiation. XXX */
  4380. lcp.Up(sp);
  4381. }
  4382. }
  4383. if (sp->pp_alivecnt <= MAXALIVECNT)
  4384. ++sp->pp_alivecnt;
  4385. if (sp->pp_mode == IFF_CISCO)
  4386. sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
  4387. ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
  4388. else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
  4389. uint32_t nmagic = htonl(sp->lcp.magic);
  4390. sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
  4391. sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
  4392. sp->lcp.echoid, 4, &nmagic);
  4393. }
  4394. out:
  4395. SPPP_UNLOCK(sp);
  4396. callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
  4397. (void *)sp);
  4398. }
  4399. /*
  4400. * Get both IP addresses.
  4401. */
  4402. void
  4403. sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
  4404. {
  4405. struct epoch_tracker et;
  4406. struct ifnet *ifp = SP2IFP(sp);
  4407. struct ifaddr *ifa;
  4408. struct sockaddr_in *si, *sm;
  4409. u_long ssrc, ddst;
  4410. sm = NULL;
  4411. ssrc = ddst = 0L;
  4412. /*
  4413. * Pick the first AF_INET address from the list,
  4414. * aliases don't make any sense on a p2p link anyway.
  4415. */
  4416. si = NULL;
  4417. NET_EPOCH_ENTER(et);
  4418. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  4419. if (ifa->ifa_addr->sa_family == AF_INET) {
  4420. si = (struct sockaddr_in *)ifa->ifa_addr;
  4421. sm = (struct sockaddr_in *)ifa->ifa_netmask;
  4422. if (si)
  4423. break;
  4424. }
  4425. if (ifa) {
  4426. if (si && si->sin_addr.s_addr) {
  4427. ssrc = si->sin_addr.s_addr;
  4428. if (srcmask)
  4429. *srcmask = ntohl(sm->sin_addr.s_addr);
  4430. }
  4431. si = (struct sockaddr_in *)ifa->ifa_dstaddr;
  4432. if (si && si->sin_addr.s_addr)
  4433. ddst = si->sin_addr.s_addr;
  4434. }
  4435. NET_EPOCH_EXIT(et);
  4436. if (dst) *dst = ntohl(ddst);
  4437. if (src) *src = ntohl(ssrc);
  4438. }
  4439. #ifdef INET
  4440. /*
  4441. * Set my IP address.
  4442. */
  4443. static void
  4444. sppp_set_ip_addr(struct sppp *sp, u_long src)
  4445. {
  4446. STDDCL;
  4447. struct epoch_tracker et;
  4448. struct ifaddr *ifa;
  4449. struct sockaddr_in *si;
  4450. struct in_ifaddr *ia;
  4451. /*
  4452. * Pick the first AF_INET address from the list,
  4453. * aliases don't make any sense on a p2p link anyway.
  4454. */
  4455. si = NULL;
  4456. NET_EPOCH_ENTER(et);
  4457. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  4458. if (ifa->ifa_addr->sa_family == AF_INET) {
  4459. si = (struct sockaddr_in *)ifa->ifa_addr;
  4460. if (si != NULL) {
  4461. ifa_ref(ifa);
  4462. break;
  4463. }
  4464. }
  4465. }
  4466. NET_EPOCH_EXIT(et);
  4467. if (ifa != NULL) {
  4468. int error;
  4469. int fibnum = ifp->if_fib;
  4470. rt_addrmsg(RTM_DELETE, ifa, fibnum);
  4471. /* delete old route */
  4472. ia = ifatoia(ifa);
  4473. error = in_handle_ifaddr_route(RTM_DELETE, ia);
  4474. if (debug && error) {
  4475. log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
  4476. SPP_ARGS(ifp), error);
  4477. }
  4478. /* set new address */
  4479. si->sin_addr.s_addr = htonl(src);
  4480. IN_IFADDR_WLOCK();
  4481. LIST_REMOVE(ia, ia_hash);
  4482. LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
  4483. IN_IFADDR_WUNLOCK();
  4484. rt_addrmsg(RTM_ADD, ifa, fibnum);
  4485. /* add new route */
  4486. error = in_handle_ifaddr_route(RTM_ADD, ia);
  4487. if (debug && error) {
  4488. log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
  4489. SPP_ARGS(ifp), error);
  4490. }
  4491. ifa_free(ifa);
  4492. }
  4493. }
  4494. #endif
  4495. #ifdef INET6
  4496. /*
  4497. * Get both IPv6 addresses.
  4498. */
  4499. static void
  4500. sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
  4501. struct in6_addr *srcmask)
  4502. {
  4503. struct epoch_tracker et;
  4504. struct ifnet *ifp = SP2IFP(sp);
  4505. struct ifaddr *ifa;
  4506. struct sockaddr_in6 *si, *sm;
  4507. struct in6_addr ssrc, ddst;
  4508. sm = NULL;
  4509. bzero(&ssrc, sizeof(ssrc));
  4510. bzero(&ddst, sizeof(ddst));
  4511. /*
  4512. * Pick the first link-local AF_INET6 address from the list,
  4513. * aliases don't make any sense on a p2p link anyway.
  4514. */
  4515. si = NULL;
  4516. NET_EPOCH_ENTER(et);
  4517. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  4518. if (ifa->ifa_addr->sa_family == AF_INET6) {
  4519. si = (struct sockaddr_in6 *)ifa->ifa_addr;
  4520. sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
  4521. if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
  4522. break;
  4523. }
  4524. if (ifa) {
  4525. if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
  4526. bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
  4527. if (srcmask) {
  4528. bcopy(&sm->sin6_addr, srcmask,
  4529. sizeof(*srcmask));
  4530. }
  4531. }
  4532. si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
  4533. if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
  4534. bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
  4535. }
  4536. if (dst)
  4537. bcopy(&ddst, dst, sizeof(*dst));
  4538. if (src)
  4539. bcopy(&ssrc, src, sizeof(*src));
  4540. NET_EPOCH_EXIT(et);
  4541. }
  4542. #ifdef IPV6CP_MYIFID_DYN
  4543. /*
  4544. * Generate random ifid.
  4545. */
  4546. static void
  4547. sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
  4548. {
  4549. /* TBD */
  4550. }
  4551. /*
  4552. * Set my IPv6 address.
  4553. */
  4554. static void
  4555. sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
  4556. {
  4557. STDDCL;
  4558. struct epoch_tracker et;
  4559. struct ifaddr *ifa;
  4560. struct sockaddr_in6 *sin6;
  4561. /*
  4562. * Pick the first link-local AF_INET6 address from the list,
  4563. * aliases don't make any sense on a p2p link anyway.
  4564. */
  4565. sin6 = NULL;
  4566. NET_EPOCH_ENTER(et);
  4567. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  4568. if (ifa->ifa_addr->sa_family == AF_INET6) {
  4569. sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
  4570. if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  4571. ifa_ref(ifa);
  4572. break;
  4573. }
  4574. }
  4575. }
  4576. NET_EPOCH_EXIT(et);
  4577. if (ifa != NULL) {
  4578. int error;
  4579. struct sockaddr_in6 new_sin6 = *sin6;
  4580. bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
  4581. error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
  4582. if (debug && error) {
  4583. log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
  4584. " failed, error=%d\n", SPP_ARGS(ifp), error);
  4585. }
  4586. ifa_free(ifa);
  4587. }
  4588. }
  4589. #endif
  4590. /*
  4591. * Suggest a candidate address to be used by peer.
  4592. */
  4593. static void
  4594. sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
  4595. {
  4596. struct in6_addr myaddr;
  4597. struct timeval tv;
  4598. sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
  4599. myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
  4600. microtime(&tv);
  4601. if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
  4602. myaddr.s6_addr[14] ^= 0xff;
  4603. myaddr.s6_addr[15] ^= 0xff;
  4604. } else {
  4605. myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
  4606. myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
  4607. }
  4608. if (suggest)
  4609. bcopy(&myaddr, suggest, sizeof(myaddr));
  4610. }
  4611. #endif /*INET6*/
  4612. static int
  4613. sppp_params(struct sppp *sp, u_long cmd, void *data)
  4614. {
  4615. u_long subcmd;
  4616. struct ifreq *ifr = (struct ifreq *)data;
  4617. struct spppreq *spr;
  4618. int rv = 0;
  4619. if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == NULL)
  4620. return (EAGAIN);
  4621. /*
  4622. * ifr_data_get_ptr(ifr) is supposed to point to a struct spppreq.
  4623. * Check the cmd word first before attempting to fetch all the
  4624. * data.
  4625. */
  4626. rv = fueword(ifr_data_get_ptr(ifr), &subcmd);
  4627. if (rv == -1) {
  4628. rv = EFAULT;
  4629. goto quit;
  4630. }
  4631. if (copyin(ifr_data_get_ptr(ifr), spr, sizeof(struct spppreq)) != 0) {
  4632. rv = EFAULT;
  4633. goto quit;
  4634. }
  4635. switch (subcmd) {
  4636. case (u_long)SPPPIOGDEFS:
  4637. if (cmd != SIOCGIFGENERIC) {
  4638. rv = EINVAL;
  4639. break;
  4640. }
  4641. /*
  4642. * We copy over the entire current state, but clean
  4643. * out some of the stuff we don't wanna pass up.
  4644. * Remember, SIOCGIFGENERIC is unprotected, and can be
  4645. * called by any user. No need to ever get PAP or
  4646. * CHAP secrets back to userland anyway.
  4647. */
  4648. spr->defs.pp_phase = sp->pp_phase;
  4649. spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
  4650. spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
  4651. spr->defs.lcp = sp->lcp;
  4652. spr->defs.ipcp = sp->ipcp;
  4653. spr->defs.ipv6cp = sp->ipv6cp;
  4654. spr->defs.myauth = sp->myauth;
  4655. spr->defs.hisauth = sp->hisauth;
  4656. bzero(spr->defs.myauth.secret, AUTHKEYLEN);
  4657. bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
  4658. bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
  4659. bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
  4660. /*
  4661. * Fixup the LCP timeout value to milliseconds so
  4662. * spppcontrol doesn't need to bother about the value
  4663. * of "hz". We do the reverse calculation below when
  4664. * setting it.
  4665. */
  4666. spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
  4667. rv = copyout(spr, ifr_data_get_ptr(ifr),
  4668. sizeof(struct spppreq));
  4669. break;
  4670. case (u_long)SPPPIOSDEFS:
  4671. if (cmd != SIOCSIFGENERIC) {
  4672. rv = EINVAL;
  4673. break;
  4674. }
  4675. /*
  4676. * We have a very specific idea of which fields we
  4677. * allow being passed back from userland, so to not
  4678. * clobber our current state. For one, we only allow
  4679. * setting anything if LCP is in dead or establish
  4680. * phase. Once the authentication negotiations
  4681. * started, the authentication settings must not be
  4682. * changed again. (The administrator can force an
  4683. * ifconfig down in order to get LCP back into dead
  4684. * phase.)
  4685. *
  4686. * Also, we only allow for authentication parameters to be
  4687. * specified.
  4688. *
  4689. * XXX Should allow to set or clear pp_flags.
  4690. *
  4691. * Finally, if the respective authentication protocol to
  4692. * be used is set differently than 0, but the secret is
  4693. * passed as all zeros, we don't trash the existing secret.
  4694. * This allows an administrator to change the system name
  4695. * only without clobbering the secret (which he didn't get
  4696. * back in a previous SPPPIOGDEFS call). However, the
  4697. * secrets are cleared if the authentication protocol is
  4698. * reset to 0. */
  4699. if (sp->pp_phase != PHASE_DEAD &&
  4700. sp->pp_phase != PHASE_ESTABLISH) {
  4701. rv = EBUSY;
  4702. break;
  4703. }
  4704. if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
  4705. spr->defs.myauth.proto != PPP_CHAP) ||
  4706. (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
  4707. spr->defs.hisauth.proto != PPP_CHAP)) {
  4708. rv = EINVAL;
  4709. break;
  4710. }
  4711. if (spr->defs.myauth.proto == 0)
  4712. /* resetting myauth */
  4713. bzero(&sp->myauth, sizeof sp->myauth);
  4714. else {
  4715. /* setting/changing myauth */
  4716. sp->myauth.proto = spr->defs.myauth.proto;
  4717. bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
  4718. if (spr->defs.myauth.secret[0] != '\0')
  4719. bcopy(spr->defs.myauth.secret, sp->myauth.secret,
  4720. AUTHKEYLEN);
  4721. }
  4722. if (spr->defs.hisauth.proto == 0)
  4723. /* resetting hisauth */
  4724. bzero(&sp->hisauth, sizeof sp->hisauth);
  4725. else {
  4726. /* setting/changing hisauth */
  4727. sp->hisauth.proto = spr->defs.hisauth.proto;
  4728. sp->hisauth.flags = spr->defs.hisauth.flags;
  4729. bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
  4730. if (spr->defs.hisauth.secret[0] != '\0')
  4731. bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
  4732. AUTHKEYLEN);
  4733. }
  4734. /* set LCP restart timer timeout */
  4735. if (spr->defs.lcp.timeout != 0)
  4736. sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
  4737. /* set VJ enable and IPv6 disable flags */
  4738. #ifdef INET
  4739. if (spr->defs.enable_vj)
  4740. sp->confflags |= CONF_ENABLE_VJ;
  4741. else
  4742. sp->confflags &= ~CONF_ENABLE_VJ;
  4743. #endif
  4744. #ifdef INET6
  4745. if (spr->defs.enable_ipv6)
  4746. sp->confflags |= CONF_ENABLE_IPV6;
  4747. else
  4748. sp->confflags &= ~CONF_ENABLE_IPV6;
  4749. #endif
  4750. break;
  4751. default:
  4752. rv = EINVAL;
  4753. }
  4754. quit:
  4755. free(spr, M_TEMP);
  4756. return (rv);
  4757. }
  4758. static void
  4759. sppp_phase_network(struct sppp *sp)
  4760. {
  4761. STDDCL;
  4762. int i;
  4763. u_long mask;
  4764. sp->pp_phase = PHASE_NETWORK;
  4765. if (debug)
  4766. log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
  4767. sppp_phase_name(sp->pp_phase));
  4768. /* Notify NCPs now. */
  4769. for (i = 0; i < IDX_COUNT; i++)
  4770. if ((cps[i])->flags & CP_NCP)
  4771. (cps[i])->Open(sp);
  4772. /* Send Up events to all NCPs. */
  4773. for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
  4774. if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
  4775. (cps[i])->Up(sp);
  4776. /* if no NCP is starting, all this was in vain, close down */
  4777. sppp_lcp_check_and_close(sp);
  4778. }
  4779. static const char *
  4780. sppp_cp_type_name(u_char type)
  4781. {
  4782. static char buf[12];
  4783. switch (type) {
  4784. case CONF_REQ: return "conf-req";
  4785. case CONF_ACK: return "conf-ack";
  4786. case CONF_NAK: return "conf-nak";
  4787. case CONF_REJ: return "conf-rej";
  4788. case TERM_REQ: return "term-req";
  4789. case TERM_ACK: return "term-ack";
  4790. case CODE_REJ: return "code-rej";
  4791. case PROTO_REJ: return "proto-rej";
  4792. case ECHO_REQ: return "echo-req";
  4793. case ECHO_REPLY: return "echo-reply";
  4794. case DISC_REQ: return "discard-req";
  4795. }
  4796. snprintf (buf, sizeof(buf), "cp/0x%x", type);
  4797. return buf;
  4798. }
  4799. static const char *
  4800. sppp_auth_type_name(u_short proto, u_char type)
  4801. {
  4802. static char buf[12];
  4803. switch (proto) {
  4804. case PPP_CHAP:
  4805. switch (type) {
  4806. case CHAP_CHALLENGE: return "challenge";
  4807. case CHAP_RESPONSE: return "response";
  4808. case CHAP_SUCCESS: return "success";
  4809. case CHAP_FAILURE: return "failure";
  4810. }
  4811. case PPP_PAP:
  4812. switch (type) {
  4813. case PAP_REQ: return "req";
  4814. case PAP_ACK: return "ack";
  4815. case PAP_NAK: return "nak";
  4816. }
  4817. }
  4818. snprintf (buf, sizeof(buf), "auth/0x%x", type);
  4819. return buf;
  4820. }
  4821. static const char *
  4822. sppp_lcp_opt_name(u_char opt)
  4823. {
  4824. static char buf[12];
  4825. switch (opt) {
  4826. case LCP_OPT_MRU: return "mru";
  4827. case LCP_OPT_ASYNC_MAP: return "async-map";
  4828. case LCP_OPT_AUTH_PROTO: return "auth-proto";
  4829. case LCP_OPT_QUAL_PROTO: return "qual-proto";
  4830. case LCP_OPT_MAGIC: return "magic";
  4831. case LCP_OPT_PROTO_COMP: return "proto-comp";
  4832. case LCP_OPT_ADDR_COMP: return "addr-comp";
  4833. }
  4834. snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
  4835. return buf;
  4836. }
  4837. #ifdef INET
  4838. static const char *
  4839. sppp_ipcp_opt_name(u_char opt)
  4840. {
  4841. static char buf[12];
  4842. switch (opt) {
  4843. case IPCP_OPT_ADDRESSES: return "addresses";
  4844. case IPCP_OPT_COMPRESSION: return "compression";
  4845. case IPCP_OPT_ADDRESS: return "address";
  4846. }
  4847. snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
  4848. return buf;
  4849. }
  4850. #endif
  4851. #ifdef INET6
  4852. static const char *
  4853. sppp_ipv6cp_opt_name(u_char opt)
  4854. {
  4855. static char buf[12];
  4856. switch (opt) {
  4857. case IPV6CP_OPT_IFID: return "ifid";
  4858. case IPV6CP_OPT_COMPRESSION: return "compression";
  4859. }
  4860. sprintf (buf, "0x%x", opt);
  4861. return buf;
  4862. }
  4863. #endif
  4864. static const char *
  4865. sppp_state_name(int state)
  4866. {
  4867. switch (state) {
  4868. case STATE_INITIAL: return "initial";
  4869. case STATE_STARTING: return "starting";
  4870. case STATE_CLOSED: return "closed";
  4871. case STATE_STOPPED: return "stopped";
  4872. case STATE_CLOSING: return "closing";
  4873. case STATE_STOPPING: return "stopping";
  4874. case STATE_REQ_SENT: return "req-sent";
  4875. case STATE_ACK_RCVD: return "ack-rcvd";
  4876. case STATE_ACK_SENT: return "ack-sent";
  4877. case STATE_OPENED: return "opened";
  4878. }
  4879. return "illegal";
  4880. }
  4881. static const char *
  4882. sppp_phase_name(enum ppp_phase phase)
  4883. {
  4884. switch (phase) {
  4885. case PHASE_DEAD: return "dead";
  4886. case PHASE_ESTABLISH: return "establish";
  4887. case PHASE_TERMINATE: return "terminate";
  4888. case PHASE_AUTHENTICATE: return "authenticate";
  4889. case PHASE_NETWORK: return "network";
  4890. }
  4891. return "illegal";
  4892. }
  4893. static const char *
  4894. sppp_proto_name(u_short proto)
  4895. {
  4896. static char buf[12];
  4897. switch (proto) {
  4898. case PPP_LCP: return "lcp";
  4899. case PPP_IPCP: return "ipcp";
  4900. case PPP_PAP: return "pap";
  4901. case PPP_CHAP: return "chap";
  4902. case PPP_IPV6CP: return "ipv6cp";
  4903. }
  4904. snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
  4905. return buf;
  4906. }
  4907. static void
  4908. sppp_print_bytes(const u_char *p, u_short len)
  4909. {
  4910. if (len)
  4911. log(-1, " %*D", len, p, "-");
  4912. }
  4913. static void
  4914. sppp_print_string(const char *p, u_short len)
  4915. {
  4916. u_char c;
  4917. while (len-- > 0) {
  4918. c = *p++;
  4919. /*
  4920. * Print only ASCII chars directly. RFC 1994 recommends
  4921. * using only them, but we don't rely on it. */
  4922. if (c < ' ' || c > '~')
  4923. log(-1, "\\x%x", c);
  4924. else
  4925. log(-1, "%c", c);
  4926. }
  4927. }
  4928. #ifdef INET
  4929. static const char *
  4930. sppp_dotted_quad(u_long addr)
  4931. {
  4932. static char s[16];
  4933. sprintf(s, "%d.%d.%d.%d",
  4934. (int)((addr >> 24) & 0xff),
  4935. (int)((addr >> 16) & 0xff),
  4936. (int)((addr >> 8) & 0xff),
  4937. (int)(addr & 0xff));
  4938. return s;
  4939. }
  4940. #endif
  4941. static int
  4942. sppp_strnlen(u_char *p, int max)
  4943. {
  4944. int len;
  4945. for (len = 0; len < max && *p; ++p)
  4946. ++len;
  4947. return len;
  4948. }
  4949. /* a dummy, used to drop uninteresting events */
  4950. static void
  4951. sppp_null(struct sppp *unused)
  4952. {
  4953. /* do just nothing */
  4954. }