12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187 |
- #define _GNU_SOURCE
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <limits.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <sys/utsname.h>
- #include <sys/mman.h>
- #include <linux/if.h>
- #include <linux/if_packet.h>
- #include <linux/sockios.h>
- #include <netinet/in.h>
- #include <linux/if_ether.h>
- #include <net/if_arp.h>
- #include <poll.h>
- #include <dirent.h>
- #include "pcap-int.h"
- #include "pcap/sll.h"
- #include "pcap/vlan.h"
- #ifdef PF_PACKET
- # include <linux/if_packet.h>
-
- # ifdef PACKET_HOST
- # define HAVE_PF_PACKET_SOCKETS
- # ifdef PACKET_AUXDATA
- # define HAVE_PACKET_AUXDATA
- # endif
- # endif
-
- # ifdef PCAP_SUPPORT_PACKET_RING
- # ifdef TPACKET_HDRLEN
- # define HAVE_PACKET_RING
- # ifdef TPACKET3_HDRLEN
- # define HAVE_TPACKET3
- # endif
- # ifdef TPACKET2_HDRLEN
- # define HAVE_TPACKET2
- # else
- # define TPACKET_V1 0
- # endif
- # endif
- # endif
- #endif
- #ifdef SO_ATTACH_FILTER
- #include <linux/types.h>
- #include <linux/filter.h>
- #endif
- #ifdef HAVE_LINUX_NET_TSTAMP_H
- #include <linux/net_tstamp.h>
- #endif
- #ifdef HAVE_LINUX_SOCKIOS_H
- #include <linux/sockios.h>
- #endif
- #ifdef HAVE_LINUX_IF_BONDING_H
- #include <linux/if_bonding.h>
- #if defined(SIOCBONDINFOQUERY)
- #define BOND_INFO_QUERY_IOCTL SIOCBONDINFOQUERY
- #elif defined(BOND_INFO_QUERY_OLD)
- #define BOND_INFO_QUERY_IOCTL BOND_INFO_QUERY_OLD
- #endif
- #endif
- #ifdef HAVE_LINUX_WIRELESS_H
- #include <linux/wireless.h>
- #endif
- #ifdef HAVE_LIBNL
- #include <linux/nl80211.h>
- #include <netlink/genl/genl.h>
- #include <netlink/genl/family.h>
- #include <netlink/genl/ctrl.h>
- #include <netlink/msg.h>
- #include <netlink/attr.h>
- #endif
- #ifdef HAVE_LINUX_ETHTOOL_H
- #include <linux/ethtool.h>
- #endif
- #ifndef HAVE_SOCKLEN_T
- typedef int socklen_t;
- #endif
- #ifndef MSG_TRUNC
- #define MSG_TRUNC 0x20
- #endif
- #ifndef SOL_PACKET
- #define SOL_PACKET 263
- #endif
- #define MAX_LINKHEADER_SIZE 256
- #define BIGGER_THAN_ALL_MTUS (64*1024)
- struct pcap_linux {
- u_int packets_read;
- long proc_dropped;
- struct pcap_stat stat;
- char *device;
- int filter_in_userland;
- int blocks_to_filter_in_userland;
- int must_do_on_close;
- int timeout;
- int sock_packet;
- int cooked;
- int ifindex;
- int lo_ifindex;
- bpf_u_int32 oldmode;
- char *mondevice;
- u_char *mmapbuf;
- size_t mmapbuflen;
- int vlan_offset;
- u_int tp_version;
- u_int tp_hdrlen;
- u_char *oneshot_buffer;
- int poll_timeout;
- #ifdef HAVE_TPACKET3
- unsigned char *current_packet;
- int packets_left;
- #endif
- };
- #define MUST_CLEAR_PROMISC 0x00000001
- #define MUST_CLEAR_RFMON 0x00000002
- #define MUST_DELETE_MONIF 0x00000004
- static int get_if_flags(const char *, bpf_u_int32 *, char *);
- static int is_wifi(int, const char *);
- static void map_arphrd_to_dlt(pcap_t *, int, int, const char *, int);
- #ifdef HAVE_PF_PACKET_SOCKETS
- static short int map_packet_type_to_sll_type(short int);
- #endif
- static int pcap_activate_linux(pcap_t *);
- static int activate_old(pcap_t *);
- static int activate_new(pcap_t *);
- static int activate_mmap(pcap_t *, int *);
- static int pcap_can_set_rfmon_linux(pcap_t *);
- static int pcap_read_linux(pcap_t *, int, pcap_handler, u_char *);
- static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
- static int pcap_inject_linux(pcap_t *, const void *, size_t);
- static int pcap_stats_linux(pcap_t *, struct pcap_stat *);
- static int pcap_setfilter_linux(pcap_t *, struct bpf_program *);
- static int pcap_setdirection_linux(pcap_t *, pcap_direction_t);
- static int pcap_set_datalink_linux(pcap_t *, int);
- static void pcap_cleanup_linux(pcap_t *);
- struct tpacket_hdr_64 {
- uint64_t tp_status;
- unsigned int tp_len;
- unsigned int tp_snaplen;
- unsigned short tp_mac;
- unsigned short tp_net;
- unsigned int tp_sec;
- unsigned int tp_usec;
- };
- #define TPACKET_V1_64 99
- union thdr {
- struct tpacket_hdr *h1;
- struct tpacket_hdr_64 *h1_64;
- #ifdef HAVE_TPACKET2
- struct tpacket2_hdr *h2;
- #endif
- #ifdef HAVE_TPACKET3
- struct tpacket_block_desc *h3;
- #endif
- void *raw;
- };
- #ifdef HAVE_PACKET_RING
- #define RING_GET_FRAME_AT(h, offset) (((union thdr **)h->buffer)[(offset)])
- #define RING_GET_CURRENT_FRAME(h) RING_GET_FRAME_AT(h, h->offset)
- static void destroy_ring(pcap_t *handle);
- static int create_ring(pcap_t *handle, int *status);
- static int prepare_tpacket_socket(pcap_t *handle);
- static void pcap_cleanup_linux_mmap(pcap_t *);
- static int pcap_read_linux_mmap_v1(pcap_t *, int, pcap_handler , u_char *);
- static int pcap_read_linux_mmap_v1_64(pcap_t *, int, pcap_handler , u_char *);
- #ifdef HAVE_TPACKET2
- static int pcap_read_linux_mmap_v2(pcap_t *, int, pcap_handler , u_char *);
- #endif
- #ifdef HAVE_TPACKET3
- static int pcap_read_linux_mmap_v3(pcap_t *, int, pcap_handler , u_char *);
- #endif
- static int pcap_setfilter_linux_mmap(pcap_t *, struct bpf_program *);
- static int pcap_setnonblock_mmap(pcap_t *p, int nonblock);
- static int pcap_getnonblock_mmap(pcap_t *p);
- static void pcap_oneshot_mmap(u_char *user, const struct pcap_pkthdr *h,
- const u_char *bytes);
- #endif
- #ifdef TP_STATUS_VLAN_VALID
- #define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & TP_STATUS_VLAN_VALID))
- #else
-
- #define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & 0x10))
- #endif
- #ifdef TP_STATUS_VLAN_TPID_VALID
- # define VLAN_TPID(hdr, hv) (((hv)->tp_vlan_tpid || ((hdr)->tp_status & TP_STATUS_VLAN_TPID_VALID)) ? (hv)->tp_vlan_tpid : ETH_P_8021Q)
- #else
- # define VLAN_TPID(hdr, hv) ETH_P_8021Q
- #endif
- #ifdef HAVE_PF_PACKET_SOCKETS
- static int iface_get_id(int fd, const char *device, char *ebuf);
- #endif
- static int iface_get_mtu(int fd, const char *device, char *ebuf);
- static int iface_get_arptype(int fd, const char *device, char *ebuf);
- #ifdef HAVE_PF_PACKET_SOCKETS
- static int iface_bind(int fd, int ifindex, char *ebuf, int protocol);
- #ifdef IW_MODE_MONITOR
- static int has_wext(int sock_fd, const char *device, char *ebuf);
- #endif
- static int enter_rfmon_mode(pcap_t *handle, int sock_fd,
- const char *device);
- #endif
- #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP)
- static int iface_ethtool_get_ts_info(const char *device, pcap_t *handle,
- char *ebuf);
- #endif
- #ifdef HAVE_PACKET_RING
- static int iface_get_offload(pcap_t *handle);
- #endif
- static int iface_bind_old(int fd, const char *device, char *ebuf);
- #ifdef SO_ATTACH_FILTER
- static int fix_program(pcap_t *handle, struct sock_fprog *fcode,
- int is_mapped);
- static int fix_offset(struct bpf_insn *p);
- static int set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode);
- static int reset_kernel_filter(pcap_t *handle);
- static struct sock_filter total_insn
- = BPF_STMT(BPF_RET | BPF_K, 0);
- static struct sock_fprog total_fcode
- = { 1, &total_insn };
- #endif
- pcap_t *
- pcap_create_interface(const char *device, char *ebuf)
- {
- pcap_t *handle;
- handle = pcap_create_common(ebuf, sizeof (struct pcap_linux));
- if (handle == NULL)
- return NULL;
- handle->activate_op = pcap_activate_linux;
- handle->can_set_rfmon_op = pcap_can_set_rfmon_linux;
- #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP)
-
- if (iface_ethtool_get_ts_info(device, handle, ebuf) == -1) {
- pcap_close(handle);
- return NULL;
- }
- #endif
- #if defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS)
-
- handle->tstamp_precision_count = 2;
- handle->tstamp_precision_list = malloc(2 * sizeof(u_int));
- if (handle->tstamp_precision_list == NULL) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "malloc");
- pcap_close(handle);
- return NULL;
- }
- handle->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
- handle->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
- #endif
- return handle;
- }
- #ifdef HAVE_LIBNL
- static int
- get_mac80211_phydev(pcap_t *handle, const char *device, char *phydev_path,
- size_t phydev_max_pathlen)
- {
- char *pathstr;
- ssize_t bytes_read;
-
- if (asprintf(&pathstr, "/sys/class/net/%s/phy80211", device) == -1) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: Can't generate path name string for /sys/class/net device",
- device);
- return PCAP_ERROR;
- }
- bytes_read = readlink(pathstr, phydev_path, phydev_max_pathlen);
- if (bytes_read == -1) {
- if (errno == ENOENT || errno == EINVAL) {
-
- free(pathstr);
- return 0;
- }
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "%s: Can't readlink %s", device, pathstr);
- free(pathstr);
- return PCAP_ERROR;
- }
- free(pathstr);
- phydev_path[bytes_read] = '\0';
- return 1;
- }
- #ifdef HAVE_LIBNL_SOCKETS
- #define get_nl_errmsg nl_geterror
- #else
- #define nl_sock nl_handle
- static inline struct nl_handle *
- nl_socket_alloc(void)
- {
- return nl_handle_alloc();
- }
- static inline void
- nl_socket_free(struct nl_handle *h)
- {
- nl_handle_destroy(h);
- }
- #define get_nl_errmsg strerror
- static inline int
- __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
- {
- struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
- if (!tmp)
- return -ENOMEM;
- *cache = tmp;
- return 0;
- }
- #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
- #endif
- struct nl80211_state {
- struct nl_sock *nl_sock;
- struct nl_cache *nl_cache;
- struct genl_family *nl80211;
- };
- static int
- nl80211_init(pcap_t *handle, struct nl80211_state *state, const char *device)
- {
- int err;
- state->nl_sock = nl_socket_alloc();
- if (!state->nl_sock) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: failed to allocate netlink handle", device);
- return PCAP_ERROR;
- }
- if (genl_connect(state->nl_sock)) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: failed to connect to generic netlink", device);
- goto out_handle_destroy;
- }
- err = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache);
- if (err < 0) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: failed to allocate generic netlink cache: %s",
- device, get_nl_errmsg(-err));
- goto out_handle_destroy;
- }
- state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
- if (!state->nl80211) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl80211 not found", device);
- goto out_cache_free;
- }
- return 0;
- out_cache_free:
- nl_cache_free(state->nl_cache);
- out_handle_destroy:
- nl_socket_free(state->nl_sock);
- return PCAP_ERROR;
- }
- static void
- nl80211_cleanup(struct nl80211_state *state)
- {
- genl_family_put(state->nl80211);
- nl_cache_free(state->nl_cache);
- nl_socket_free(state->nl_sock);
- }
- static int
- del_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state,
- const char *device, const char *mondevice);
- static int
- add_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state,
- const char *device, const char *mondevice)
- {
- struct pcap_linux *handlep = handle->priv;
- int ifindex;
- struct nl_msg *msg;
- int err;
- ifindex = iface_get_id(sock_fd, device, handle->errbuf);
- if (ifindex == -1)
- return PCAP_ERROR;
- msg = nlmsg_alloc();
- if (!msg) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: failed to allocate netlink msg", device);
- return PCAP_ERROR;
- }
- genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
- 0, NL80211_CMD_NEW_INTERFACE, 0);
- NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
- NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, mondevice);
- NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
- err = nl_send_auto_complete(state->nl_sock, msg);
- if (err < 0) {
- #if defined HAVE_LIBNL_NLE
- if (err == -NLE_FAILURE) {
- #else
- if (err == -ENFILE) {
- #endif
-
- nlmsg_free(msg);
- return 0;
- } else {
-
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl_send_auto_complete failed adding %s interface: %s",
- device, mondevice, get_nl_errmsg(-err));
- nlmsg_free(msg);
- return PCAP_ERROR;
- }
- }
- err = nl_wait_for_ack(state->nl_sock);
- if (err < 0) {
- #if defined HAVE_LIBNL_NLE
- if (err == -NLE_FAILURE) {
- #else
- if (err == -ENFILE) {
- #endif
-
- nlmsg_free(msg);
- return 0;
- } else {
-
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl_wait_for_ack failed adding %s interface: %s",
- device, mondevice, get_nl_errmsg(-err));
- nlmsg_free(msg);
- return PCAP_ERROR;
- }
- }
-
- nlmsg_free(msg);
-
- handlep->mondevice = strdup(mondevice);
- if (handlep->mondevice == NULL) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "strdup");
-
- del_mon_if(handle, sock_fd, state, device, mondevice);
- return PCAP_ERROR;
- }
- return 1;
- nla_put_failure:
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl_put failed adding %s interface",
- device, mondevice);
- nlmsg_free(msg);
- return PCAP_ERROR;
- }
- static int
- del_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state,
- const char *device, const char *mondevice)
- {
- int ifindex;
- struct nl_msg *msg;
- int err;
- ifindex = iface_get_id(sock_fd, mondevice, handle->errbuf);
- if (ifindex == -1)
- return PCAP_ERROR;
- msg = nlmsg_alloc();
- if (!msg) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: failed to allocate netlink msg", device);
- return PCAP_ERROR;
- }
- genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
- 0, NL80211_CMD_DEL_INTERFACE, 0);
- NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
- err = nl_send_auto_complete(state->nl_sock, msg);
- if (err < 0) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl_send_auto_complete failed deleting %s interface: %s",
- device, mondevice, get_nl_errmsg(-err));
- nlmsg_free(msg);
- return PCAP_ERROR;
- }
- err = nl_wait_for_ack(state->nl_sock);
- if (err < 0) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl_wait_for_ack failed adding %s interface: %s",
- device, mondevice, get_nl_errmsg(-err));
- nlmsg_free(msg);
- return PCAP_ERROR;
- }
-
- nlmsg_free(msg);
- return 1;
- nla_put_failure:
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: nl_put failed deleting %s interface",
- device, mondevice);
- nlmsg_free(msg);
- return PCAP_ERROR;
- }
- static int
- enter_rfmon_mode_mac80211(pcap_t *handle, int sock_fd, const char *device)
- {
- struct pcap_linux *handlep = handle->priv;
- int ret;
- char phydev_path[PATH_MAX+1];
- struct nl80211_state nlstate;
- struct ifreq ifr;
- u_int n;
-
- ret = get_mac80211_phydev(handle, device, phydev_path, PATH_MAX);
- if (ret < 0)
- return ret;
- if (ret == 0)
- return 0;
-
-
- ret = nl80211_init(handle, &nlstate, device);
- if (ret != 0)
- return ret;
- for (n = 0; n < UINT_MAX; n++) {
-
- char mondevice[3+10+1];
- pcap_snprintf(mondevice, sizeof mondevice, "mon%u", n);
- ret = add_mon_if(handle, sock_fd, &nlstate, device, mondevice);
- if (ret == 1) {
-
- goto added;
- }
- if (ret < 0) {
-
- nl80211_cleanup(&nlstate);
- return ret;
- }
- }
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: No free monN interfaces", device);
- nl80211_cleanup(&nlstate);
- return PCAP_ERROR;
- added:
- #if 0
-
- delay.tv_sec = 0;
- delay.tv_nsec = 500000000;
- nanosleep(&delay, NULL);
- #endif
-
- if (!pcap_do_addexit(handle)) {
-
- del_mon_if(handle, sock_fd, &nlstate, device,
- handlep->mondevice);
- nl80211_cleanup(&nlstate);
- return PCAP_ERROR;
- }
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, handlep->mondevice, sizeof(ifr.ifr_name));
- if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "%s: Can't get flags for %s", device,
- handlep->mondevice);
- del_mon_if(handle, sock_fd, &nlstate, device,
- handlep->mondevice);
- nl80211_cleanup(&nlstate);
- return PCAP_ERROR;
- }
- ifr.ifr_flags |= IFF_UP|IFF_RUNNING;
- if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "%s: Can't set flags for %s", device,
- handlep->mondevice);
- del_mon_if(handle, sock_fd, &nlstate, device,
- handlep->mondevice);
- nl80211_cleanup(&nlstate);
- return PCAP_ERROR;
- }
-
- nl80211_cleanup(&nlstate);
-
- handlep->must_do_on_close |= MUST_DELETE_MONIF;
-
- pcap_add_to_pcaps_to_close(handle);
- return 1;
- }
- #endif
- #ifdef IW_MODE_MONITOR
- static int
- is_bonding_device(int fd, const char *device)
- {
- #ifdef BOND_INFO_QUERY_IOCTL
- struct ifreq ifr;
- ifbond ifb;
- memset(&ifr, 0, sizeof ifr);
- strlcpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
- memset(&ifb, 0, sizeof ifb);
- ifr.ifr_data = (caddr_t)&ifb;
- if (ioctl(fd, BOND_INFO_QUERY_IOCTL, &ifr) == 0)
- return 1;
- #endif
- return 0;
- }
- #endif
- static int pcap_protocol(pcap_t *handle)
- {
- int protocol;
- protocol = handle->opt.protocol;
- if (protocol == 0)
- protocol = ETH_P_ALL;
- return htons(protocol);
- }
- static int
- pcap_can_set_rfmon_linux(pcap_t *handle)
- {
- #ifdef HAVE_LIBNL
- char phydev_path[PATH_MAX+1];
- int ret;
- #endif
- #ifdef IW_MODE_MONITOR
- int sock_fd;
- struct iwreq ireq;
- #endif
- if (strcmp(handle->opt.device, "any") == 0) {
-
- return 0;
- }
- #ifdef HAVE_LIBNL
-
- ret = get_mac80211_phydev(handle, handle->opt.device, phydev_path,
- PATH_MAX);
- if (ret < 0)
- return ret;
- if (ret == 1)
- return 1;
- #endif
- #ifdef IW_MODE_MONITOR
-
- sock_fd = socket(PF_PACKET, SOCK_RAW, pcap_protocol(handle));
- if (sock_fd == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "socket");
- return PCAP_ERROR;
- }
- if (is_bonding_device(sock_fd, handle->opt.device)) {
-
- close(sock_fd);
- return 0;
- }
-
- strlcpy(ireq.ifr_ifrn.ifrn_name, handle->opt.device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- if (ioctl(sock_fd, SIOCGIWMODE, &ireq) != -1) {
-
- close(sock_fd);
- return 1;
- }
- if (errno == ENODEV) {
-
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "SIOCGIWMODE failed");
- close(sock_fd);
- return PCAP_ERROR_NO_SUCH_DEVICE;
- }
- close(sock_fd);
- #endif
- return 0;
- }
- static long int
- linux_if_drops(const char * if_name)
- {
- char buffer[512];
- char * bufptr;
- FILE * file;
- int field_to_convert = 3, if_name_sz = strlen(if_name);
- long int dropped_pkts = 0;
- file = fopen("/proc/net/dev", "r");
- if (!file)
- return 0;
- while (!dropped_pkts && fgets( buffer, sizeof(buffer), file ))
- {
-
- if (field_to_convert != 4 && strstr(buffer, "bytes"))
- {
- field_to_convert = 4;
- continue;
- }
-
- if ((bufptr = strstr(buffer, if_name)) &&
- (bufptr == buffer || *(bufptr-1) == ' ') &&
- *(bufptr + if_name_sz) == ':')
- {
- bufptr = bufptr + if_name_sz + 1;
-
- while( --field_to_convert && *bufptr != '\0')
- {
- while (*bufptr != '\0' && *(bufptr++) == ' ');
- while (*bufptr != '\0' && *(bufptr++) != ' ');
- }
-
- while (*bufptr != '\0' && *bufptr == ' ') bufptr++;
- if (*bufptr != '\0')
- dropped_pkts = strtol(bufptr, NULL, 10);
- break;
- }
- }
- fclose(file);
- return dropped_pkts;
- }
- static void pcap_cleanup_linux( pcap_t *handle )
- {
- struct pcap_linux *handlep = handle->priv;
- struct ifreq ifr;
- #ifdef HAVE_LIBNL
- struct nl80211_state nlstate;
- int ret;
- #endif
- #ifdef IW_MODE_MONITOR
- int oldflags;
- struct iwreq ireq;
- #endif
- if (handlep->must_do_on_close != 0) {
-
- if (handlep->must_do_on_close & MUST_CLEAR_PROMISC) {
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, handlep->device,
- sizeof(ifr.ifr_name));
- if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
- fprintf(stderr,
- "Can't restore interface %s flags (SIOCGIFFLAGS failed: %s).\n"
- "Please adjust manually.\n"
- "Hint: This can't happen with Linux >= 2.2.0.\n",
- handlep->device, strerror(errno));
- } else {
- if (ifr.ifr_flags & IFF_PROMISC) {
-
- ifr.ifr_flags &= ~IFF_PROMISC;
- if (ioctl(handle->fd, SIOCSIFFLAGS,
- &ifr) == -1) {
- fprintf(stderr,
- "Can't restore interface %s flags (SIOCSIFFLAGS failed: %s).\n"
- "Please adjust manually.\n"
- "Hint: This can't happen with Linux >= 2.2.0.\n",
- handlep->device,
- strerror(errno));
- }
- }
- }
- }
- #ifdef HAVE_LIBNL
- if (handlep->must_do_on_close & MUST_DELETE_MONIF) {
- ret = nl80211_init(handle, &nlstate, handlep->device);
- if (ret >= 0) {
- ret = del_mon_if(handle, handle->fd, &nlstate,
- handlep->device, handlep->mondevice);
- nl80211_cleanup(&nlstate);
- }
- if (ret < 0) {
- fprintf(stderr,
- "Can't delete monitor interface %s (%s).\n"
- "Please delete manually.\n",
- handlep->mondevice, handle->errbuf);
- }
- }
- #endif
- #ifdef IW_MODE_MONITOR
- if (handlep->must_do_on_close & MUST_CLEAR_RFMON) {
-
-
- oldflags = 0;
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, handlep->device,
- sizeof(ifr.ifr_name));
- if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) != -1) {
- if (ifr.ifr_flags & IFF_UP) {
- oldflags = ifr.ifr_flags;
- ifr.ifr_flags &= ~IFF_UP;
- if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1)
- oldflags = 0;
- }
- }
-
- strlcpy(ireq.ifr_ifrn.ifrn_name, handlep->device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- ireq.u.mode = handlep->oldmode;
- if (ioctl(handle->fd, SIOCSIWMODE, &ireq) == -1) {
-
- fprintf(stderr,
- "Can't restore interface %s wireless mode (SIOCSIWMODE failed: %s).\n"
- "Please adjust manually.\n",
- handlep->device, strerror(errno));
- }
-
- if (oldflags != 0) {
- ifr.ifr_flags = oldflags;
- if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
- fprintf(stderr,
- "Can't bring interface %s back up (SIOCSIFFLAGS failed: %s).\n"
- "Please adjust manually.\n",
- handlep->device, strerror(errno));
- }
- }
- }
- #endif
-
- pcap_remove_from_pcaps_to_close(handle);
- }
- if (handlep->mondevice != NULL) {
- free(handlep->mondevice);
- handlep->mondevice = NULL;
- }
- if (handlep->device != NULL) {
- free(handlep->device);
- handlep->device = NULL;
- }
- pcap_cleanup_live_common(handle);
- }
- static void
- set_poll_timeout(struct pcap_linux *handlep)
- {
- #ifdef HAVE_TPACKET3
- struct utsname utsname;
- char *version_component, *endp;
- int major, minor;
- int broken_tpacket_v3 = 1;
-
- if (uname(&utsname) == 0) {
-
- version_component = utsname.release;
- major = strtol(version_component, &endp, 10);
- if (endp != version_component && *endp == '.') {
-
- version_component = endp + 1;
- minor = strtol(version_component, &endp, 10);
- if (endp != version_component &&
- (*endp == '.' || *endp == '\0')) {
-
- if (major >= 4 || (major == 3 && minor >= 19)) {
-
- broken_tpacket_v3 = 0;
- }
- }
- }
- }
- #endif
- if (handlep->timeout == 0) {
- #ifdef HAVE_TPACKET3
-
- if (handlep->tp_version == TPACKET_V3 && broken_tpacket_v3)
- handlep->poll_timeout = 1;
- else
- #endif
- handlep->poll_timeout = -1;
- } else if (handlep->timeout > 0) {
- #ifdef HAVE_TPACKET3
-
- if (handlep->tp_version == TPACKET_V3 && !broken_tpacket_v3)
- handlep->poll_timeout = -1;
- else
- #endif
- handlep->poll_timeout = handlep->timeout;
- } else {
-
- handlep->poll_timeout = 0;
- }
- }
- static int
- pcap_activate_linux(pcap_t *handle)
- {
- struct pcap_linux *handlep = handle->priv;
- const char *device;
- struct ifreq ifr;
- int status = 0;
- int ret;
- device = handle->opt.device;
-
- if (strlen(device) >= sizeof(ifr.ifr_name)) {
- status = PCAP_ERROR_NO_SUCH_DEVICE;
- goto fail;
- }
-
- if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
- handle->snapshot = MAXIMUM_SNAPLEN;
- handle->inject_op = pcap_inject_linux;
- handle->setfilter_op = pcap_setfilter_linux;
- handle->setdirection_op = pcap_setdirection_linux;
- handle->set_datalink_op = pcap_set_datalink_linux;
- handle->getnonblock_op = pcap_getnonblock_fd;
- handle->setnonblock_op = pcap_setnonblock_fd;
- handle->cleanup_op = pcap_cleanup_linux;
- handle->read_op = pcap_read_linux;
- handle->stats_op = pcap_stats_linux;
-
- if (strcmp(device, "any") == 0) {
- if (handle->opt.promisc) {
- handle->opt.promisc = 0;
-
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "Promiscuous mode not supported on the \"any\" device");
- status = PCAP_WARNING_PROMISC_NOTSUP;
- }
- }
- handlep->device = strdup(device);
- if (handlep->device == NULL) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "strdup");
- return PCAP_ERROR;
- }
-
- handlep->timeout = handle->opt.timeout;
-
- if (handle->opt.promisc)
- handlep->proc_dropped = linux_if_drops(handlep->device);
-
- ret = activate_new(handle);
- if (ret < 0) {
-
- status = ret;
- goto fail;
- }
- if (ret == 1) {
-
- switch (activate_mmap(handle, &status)) {
- case 1:
-
- set_poll_timeout(handlep);
- return status;
- case 0:
-
- break;
- case -1:
-
- status = ret;
- goto fail;
- }
- }
- else if (ret == 0) {
-
- if ((ret = activate_old(handle)) != 1) {
-
- status = ret;
- goto fail;
- }
- }
-
- if (handle->opt.buffer_size != 0) {
-
- if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
- &handle->opt.buffer_size,
- sizeof(handle->opt.buffer_size)) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "SO_RCVBUF");
- status = PCAP_ERROR;
- goto fail;
- }
- }
-
- handle->buffer = malloc(handle->bufsize + handle->offset);
- if (!handle->buffer) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "malloc");
- status = PCAP_ERROR;
- goto fail;
- }
-
- handle->selectable_fd = handle->fd;
- return status;
- fail:
- pcap_cleanup_linux(handle);
- return status;
- }
- static int
- pcap_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
- {
-
- return pcap_read_packet(handle, callback, user);
- }
- static int
- pcap_set_datalink_linux(pcap_t *handle, int dlt)
- {
- handle->linktype = dlt;
- return 0;
- }
- static inline int
- linux_check_direction(const pcap_t *handle, const struct sockaddr_ll *sll)
- {
- struct pcap_linux *handlep = handle->priv;
- if (sll->sll_pkttype == PACKET_OUTGOING) {
-
- if (sll->sll_ifindex == handlep->lo_ifindex)
- return 0;
-
- if ((sll->sll_protocol == LINUX_SLL_P_CAN ||
- sll->sll_protocol == LINUX_SLL_P_CANFD) &&
- handle->direction != PCAP_D_OUT)
- return 0;
-
- if (handle->direction == PCAP_D_IN)
- return 0;
- } else {
-
- if (handle->direction == PCAP_D_OUT)
- return 0;
- }
- return 1;
- }
- static int
- pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
- {
- struct pcap_linux *handlep = handle->priv;
- u_char *bp;
- int offset;
- #ifdef HAVE_PF_PACKET_SOCKETS
- struct sockaddr_ll from;
- struct sll_header *hdrp;
- #else
- struct sockaddr from;
- #endif
- #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_STRUCT_TPACKET_AUXDATA_TP_VLAN_TCI)
- struct iovec iov;
- struct msghdr msg;
- struct cmsghdr *cmsg;
- union {
- struct cmsghdr cmsg;
- char buf[CMSG_SPACE(sizeof(struct tpacket_auxdata))];
- } cmsg_buf;
- #else
- socklen_t fromlen;
- #endif
- int packet_len, caplen;
- struct pcap_pkthdr pcap_header;
- struct bpf_aux_data aux_data;
- #ifdef HAVE_PF_PACKET_SOCKETS
-
- if (handlep->cooked)
- offset = SLL_HDR_LEN;
- else
- offset = 0;
- #else
-
- offset = 0;
- #endif
-
- bp = (u_char *)handle->buffer + handle->offset;
- #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_STRUCT_TPACKET_AUXDATA_TP_VLAN_TCI)
- msg.msg_name = &from;
- msg.msg_namelen = sizeof(from);
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1;
- msg.msg_control = &cmsg_buf;
- msg.msg_controllen = sizeof(cmsg_buf);
- msg.msg_flags = 0;
- iov.iov_len = handle->bufsize - offset;
- iov.iov_base = bp + offset;
- #endif
- do {
-
- if (handle->break_loop) {
-
- handle->break_loop = 0;
- return PCAP_ERROR_BREAK;
- }
- #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_STRUCT_TPACKET_AUXDATA_TP_VLAN_TCI)
- packet_len = recvmsg(handle->fd, &msg, MSG_TRUNC);
- #else
- fromlen = sizeof(from);
- packet_len = recvfrom(
- handle->fd, bp + offset,
- handle->bufsize - offset, MSG_TRUNC,
- (struct sockaddr *) &from, &fromlen);
- #endif
- } while (packet_len == -1 && errno == EINTR);
-
- if (packet_len == -1) {
- switch (errno) {
- case EAGAIN:
- return 0;
- case ENETDOWN:
-
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "The interface went down");
- return PCAP_ERROR;
- default:
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "recvfrom");
- return PCAP_ERROR;
- }
- }
- #ifdef HAVE_PF_PACKET_SOCKETS
- if (!handlep->sock_packet) {
-
- if (handlep->ifindex != -1 &&
- from.sll_ifindex != handlep->ifindex)
- return 0;
-
- if (!linux_check_direction(handle, &from))
- return 0;
- }
- #endif
- #ifdef HAVE_PF_PACKET_SOCKETS
-
- if (handlep->cooked) {
-
- packet_len += SLL_HDR_LEN;
- hdrp = (struct sll_header *)bp;
- hdrp->sll_pkttype = map_packet_type_to_sll_type(from.sll_pkttype);
- hdrp->sll_hatype = htons(from.sll_hatype);
- hdrp->sll_halen = htons(from.sll_halen);
- memcpy(hdrp->sll_addr, from.sll_addr,
- (from.sll_halen > SLL_ADDRLEN) ?
- SLL_ADDRLEN :
- from.sll_halen);
- hdrp->sll_protocol = from.sll_protocol;
- }
-
- aux_data.vlan_tag_present = 0;
- aux_data.vlan_tag = 0;
- #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_STRUCT_TPACKET_AUXDATA_TP_VLAN_TCI)
- if (handlep->vlan_offset != -1) {
- for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
- struct tpacket_auxdata *aux;
- unsigned int len;
- struct vlan_tag *tag;
- if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct tpacket_auxdata)) ||
- cmsg->cmsg_level != SOL_PACKET ||
- cmsg->cmsg_type != PACKET_AUXDATA) {
-
- continue;
- }
- aux = (struct tpacket_auxdata *)CMSG_DATA(cmsg);
- if (!VLAN_VALID(aux, aux)) {
-
- continue;
- }
- len = (u_int)packet_len > iov.iov_len ? iov.iov_len : (u_int)packet_len;
- if (len < (u_int)handlep->vlan_offset)
- break;
-
- bp -= VLAN_TAG_LEN;
- memmove(bp, bp + VLAN_TAG_LEN, handlep->vlan_offset);
-
- tag = (struct vlan_tag *)(bp + handlep->vlan_offset);
- tag->vlan_tpid = htons(VLAN_TPID(aux, aux));
- tag->vlan_tci = htons(aux->tp_vlan_tci);
-
- aux_data.vlan_tag_present = 1;
- aux_data.vlan_tag = htons(aux->tp_vlan_tci) & 0x0fff;
-
- packet_len += VLAN_TAG_LEN;
- }
- }
- #endif
- #endif
-
- caplen = packet_len;
- if (caplen > handle->snapshot)
- caplen = handle->snapshot;
-
- if (handlep->filter_in_userland && handle->fcode.bf_insns) {
- if (bpf_filter_with_aux_data(handle->fcode.bf_insns, bp,
- packet_len, caplen, &aux_data) == 0) {
-
- return 0;
- }
- }
-
-
- #if defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS)
- if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
- if (ioctl(handle->fd, SIOCGSTAMPNS, &pcap_header.ts) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "SIOCGSTAMPNS");
- return PCAP_ERROR;
- }
- } else
- #endif
- {
- if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "SIOCGSTAMP");
- return PCAP_ERROR;
- }
- }
- pcap_header.caplen = caplen;
- pcap_header.len = packet_len;
-
- handlep->packets_read++;
-
- callback(userdata, &pcap_header, bp);
- return 1;
- }
- static int
- pcap_inject_linux(pcap_t *handle, const void *buf, size_t size)
- {
- struct pcap_linux *handlep = handle->priv;
- int ret;
- #ifdef HAVE_PF_PACKET_SOCKETS
- if (!handlep->sock_packet) {
-
- if (handlep->ifindex == -1) {
-
- strlcpy(handle->errbuf,
- "Sending packets isn't supported on the \"any\" device",
- PCAP_ERRBUF_SIZE);
- return (-1);
- }
- if (handlep->cooked) {
-
- strlcpy(handle->errbuf,
- "Sending packets isn't supported in cooked mode",
- PCAP_ERRBUF_SIZE);
- return (-1);
- }
- }
- #endif
- ret = send(handle->fd, buf, size, 0);
- if (ret == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "send");
- return (-1);
- }
- return (ret);
- }
- static int
- pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats)
- {
- struct pcap_linux *handlep = handle->priv;
- #ifdef HAVE_STRUCT_TPACKET_STATS
- #ifdef HAVE_TPACKET3
-
- struct tpacket_stats_v3 kstats;
- #else
- struct tpacket_stats kstats;
- #endif
- socklen_t len = sizeof (struct tpacket_stats);
- #endif
- long if_dropped = 0;
-
- if (handle->opt.promisc)
- {
- if_dropped = handlep->proc_dropped;
- handlep->proc_dropped = linux_if_drops(handlep->device);
- handlep->stat.ps_ifdrop += (handlep->proc_dropped - if_dropped);
- }
- #ifdef HAVE_STRUCT_TPACKET_STATS
-
- if (getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS,
- &kstats, &len) > -1) {
-
- handlep->stat.ps_recv += kstats.tp_packets;
- handlep->stat.ps_drop += kstats.tp_drops;
- *stats = handlep->stat;
- return 0;
- }
- else
- {
-
- if (errno != EOPNOTSUPP) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "pcap_stats");
- return -1;
- }
- }
- #endif
-
- stats->ps_recv = handlep->packets_read;
- stats->ps_drop = 0;
- stats->ps_ifdrop = handlep->stat.ps_ifdrop;
- return 0;
- }
- static int
- add_linux_if(pcap_if_list_t *devlistp, const char *ifname, int fd, char *errbuf)
- {
- const char *p;
- char name[512];
- char *q, *saveq;
- struct ifreq ifrflags;
-
- p = ifname;
- q = &name[0];
- while (*p != '\0' && isascii(*p) && !isspace(*p)) {
- if (*p == ':') {
-
- saveq = q;
- while (isascii(*p) && isdigit(*p))
- *q++ = *p++;
- if (*p != ':') {
-
- q = saveq;
- }
- break;
- } else
- *q++ = *p++;
- }
- *q = '\0';
-
- strlcpy(ifrflags.ifr_name, name, sizeof(ifrflags.ifr_name));
- if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
- if (errno == ENXIO || errno == ENODEV)
- return (0);
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "SIOCGIFFLAGS: %.*s",
- (int)sizeof(ifrflags.ifr_name),
- ifrflags.ifr_name);
- return (-1);
- }
-
- if (find_or_add_if(devlistp, name, ifrflags.ifr_flags,
- get_if_flags, errbuf) == NULL) {
-
- return (-1);
- }
- return (0);
- }
- static int
- scan_sys_class_net(pcap_if_list_t *devlistp, char *errbuf)
- {
- DIR *sys_class_net_d;
- int fd;
- struct dirent *ent;
- char subsystem_path[PATH_MAX+1];
- struct stat statb;
- int ret = 1;
- sys_class_net_d = opendir("/sys/class/net");
- if (sys_class_net_d == NULL) {
-
- if (errno == ENOENT)
- return (0);
-
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "Can't open /sys/class/net");
- return (-1);
- }
-
- fd = socket(PF_UNIX, SOCK_RAW, 0);
- if (fd < 0) {
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "socket");
- (void)closedir(sys_class_net_d);
- return (-1);
- }
- for (;;) {
- errno = 0;
- ent = readdir(sys_class_net_d);
- if (ent == NULL) {
-
- break;
- }
-
- if (strcmp(ent->d_name, ".") == 0 ||
- strcmp(ent->d_name, "..") == 0)
- continue;
-
- if (ent->d_type == DT_REG)
- continue;
-
- pcap_snprintf(subsystem_path, sizeof subsystem_path,
- "/sys/class/net/%s/ifindex", ent->d_name);
- if (lstat(subsystem_path, &statb) != 0) {
-
- continue;
- }
-
- if (add_linux_if(devlistp, &ent->d_name[0], fd, errbuf) == -1) {
-
- ret = -1;
- break;
- }
- }
- if (ret != -1) {
-
- if (errno != 0) {
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "Error reading /sys/class/net");
- ret = -1;
- }
- }
- (void)close(fd);
- (void)closedir(sys_class_net_d);
- return (ret);
- }
- static int
- scan_proc_net_dev(pcap_if_list_t *devlistp, char *errbuf)
- {
- FILE *proc_net_f;
- int fd;
- char linebuf[512];
- int linenum;
- char *p;
- int ret = 0;
- proc_net_f = fopen("/proc/net/dev", "r");
- if (proc_net_f == NULL) {
-
- if (errno == ENOENT)
- return (0);
-
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "Can't open /proc/net/dev");
- return (-1);
- }
-
- fd = socket(PF_UNIX, SOCK_RAW, 0);
- if (fd < 0) {
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "socket");
- (void)fclose(proc_net_f);
- return (-1);
- }
- for (linenum = 1;
- fgets(linebuf, sizeof linebuf, proc_net_f) != NULL; linenum++) {
-
- if (linenum <= 2)
- continue;
- p = &linebuf[0];
-
- while (*p != '\0' && isascii(*p) && isspace(*p))
- p++;
- if (*p == '\0' || *p == '\n')
- continue;
-
- if (add_linux_if(devlistp, p, fd, errbuf) == -1) {
-
- ret = -1;
- break;
- }
- }
- if (ret != -1) {
-
- if (ferror(proc_net_f)) {
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- errno, "Error reading /proc/net/dev");
- ret = -1;
- }
- }
- (void)close(fd);
- (void)fclose(proc_net_f);
- return (ret);
- }
- static const char any_descr[] = "Pseudo-device that captures on all interfaces";
- static int
- can_be_bound(const char *name _U_)
- {
- return (1);
- }
- static int
- get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
- {
- int sock;
- FILE *fh;
- unsigned int arptype;
- struct ifreq ifr;
- struct ethtool_value info;
- if (*flags & PCAP_IF_LOOPBACK) {
-
- *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
- return 0;
- }
- sock = socket(AF_INET, SOCK_DGRAM, 0);
- if (sock == -1) {
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
- "Can't create socket to get ethtool information for %s",
- name);
- return -1;
- }
-
- if (is_wifi(sock, name)) {
-
- *flags |= PCAP_IF_WIRELESS;
- } else {
-
- char *pathstr;
- if (asprintf(&pathstr, "/sys/class/net/%s/type", name) == -1) {
- pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
- "%s: Can't generate path name string for /sys/class/net device",
- name);
- close(sock);
- return -1;
- }
- fh = fopen(pathstr, "r");
- if (fh != NULL) {
- if (fscanf(fh, "%u", &arptype) == 1) {
-
- switch (arptype) {
- #ifdef ARPHRD_LOOPBACK
- case ARPHRD_LOOPBACK:
-
- close(sock);
- fclose(fh);
- free(pathstr);
- return 0;
- #endif
- case ARPHRD_IRDA:
- case ARPHRD_IEEE80211:
- case ARPHRD_IEEE80211_PRISM:
- case ARPHRD_IEEE80211_RADIOTAP:
- #ifdef ARPHRD_IEEE802154
- case ARPHRD_IEEE802154:
- #endif
- #ifdef ARPHRD_IEEE802154_MONITOR
- case ARPHRD_IEEE802154_MONITOR:
- #endif
- #ifdef ARPHRD_6LOWPAN
- case ARPHRD_6LOWPAN:
- #endif
-
- *flags |= PCAP_IF_WIRELESS;
- break;
- }
- }
- fclose(fh);
- free(pathstr);
- }
- }
- #ifdef ETHTOOL_GLINK
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
- info.cmd = ETHTOOL_GLINK;
- ifr.ifr_data = (caddr_t)&info;
- if (ioctl(sock, SIOCETHTOOL, &ifr) == -1) {
- int save_errno = errno;
- switch (save_errno) {
- case EOPNOTSUPP:
- case EINVAL:
-
- *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
- close(sock);
- return 0;
- case ENODEV:
-
- close(sock);
- return 0;
- default:
-
- pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
- save_errno,
- "%s: SIOCETHTOOL(ETHTOOL_GLINK) ioctl failed",
- name);
- close(sock);
- return -1;
- }
- }
-
- if (info.data) {
-
- *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
- } else {
-
- *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
- }
- #endif
- close(sock);
- return 0;
- }
- int
- pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
- {
- int ret;
-
- if (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
- get_if_flags) == -1)
- return (-1);
-
- ret = scan_sys_class_net(devlistp, errbuf);
- if (ret == -1)
- return (-1);
- if (ret == 0) {
-
- if (scan_proc_net_dev(devlistp, errbuf) == -1)
- return (-1);
- }
-
- if (add_dev(devlistp, "any",
- PCAP_IF_UP|PCAP_IF_RUNNING|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
- any_descr, errbuf) == NULL)
- return (-1);
- return (0);
- }
- static int
- pcap_setfilter_linux_common(pcap_t *handle, struct bpf_program *filter,
- int is_mmapped)
- {
- struct pcap_linux *handlep;
- #ifdef SO_ATTACH_FILTER
- struct sock_fprog fcode;
- int can_filter_in_kernel;
- int err = 0;
- #endif
- if (!handle)
- return -1;
- if (!filter) {
- strlcpy(handle->errbuf, "setfilter: No filter specified",
- PCAP_ERRBUF_SIZE);
- return -1;
- }
- handlep = handle->priv;
-
- if (install_bpf_program(handle, filter) < 0)
-
- return -1;
-
- handlep->filter_in_userland = 1;
-
- #ifdef SO_ATTACH_FILTER
- #ifdef USHRT_MAX
- if (handle->fcode.bf_len > USHRT_MAX) {
-
- fprintf(stderr, "Warning: Filter too complex for kernel\n");
- fcode.len = 0;
- fcode.filter = NULL;
- can_filter_in_kernel = 0;
- } else
- #endif
- {
-
- switch (fix_program(handle, &fcode, is_mmapped)) {
- case -1:
- default:
-
- return -1;
- case 0:
-
- can_filter_in_kernel = 0;
- break;
- case 1:
-
- can_filter_in_kernel = 1;
- break;
- }
- }
-
- if (can_filter_in_kernel) {
- if ((err = set_kernel_filter(handle, &fcode)) == 0)
- {
-
- handlep->filter_in_userland = 0;
- }
- else if (err == -1)
- {
-
- if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) {
- fprintf(stderr,
- "Warning: Kernel filter failed: %s\n",
- pcap_strerror(errno));
- }
- }
- }
-
- if (handlep->filter_in_userland) {
- if (reset_kernel_filter(handle) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't remove kernel filter");
- err = -2;
- }
- }
-
- if (fcode.filter != NULL)
- free(fcode.filter);
- if (err == -2)
-
- return -1;
- #endif
- return 0;
- }
- static int
- pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter)
- {
- return pcap_setfilter_linux_common(handle, filter, 0);
- }
- static int
- pcap_setdirection_linux(pcap_t *handle, pcap_direction_t d)
- {
- #ifdef HAVE_PF_PACKET_SOCKETS
- struct pcap_linux *handlep = handle->priv;
- if (!handlep->sock_packet) {
- handle->direction = d;
- return 0;
- }
- #endif
-
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "Setting direction is not supported on SOCK_PACKET sockets");
- return -1;
- }
- #ifdef HAVE_PF_PACKET_SOCKETS
- static short int
- map_packet_type_to_sll_type(short int sll_pkttype)
- {
- switch (sll_pkttype) {
- case PACKET_HOST:
- return htons(LINUX_SLL_HOST);
- case PACKET_BROADCAST:
- return htons(LINUX_SLL_BROADCAST);
- case PACKET_MULTICAST:
- return htons(LINUX_SLL_MULTICAST);
- case PACKET_OTHERHOST:
- return htons(LINUX_SLL_OTHERHOST);
- case PACKET_OUTGOING:
- return htons(LINUX_SLL_OUTGOING);
- default:
- return -1;
- }
- }
- #endif
- static int
- is_wifi(int sock_fd
- #ifndef IW_MODE_MONITOR
- _U_
- #endif
- , const char *device)
- {
- char *pathstr;
- struct stat statb;
- #ifdef IW_MODE_MONITOR
- char errbuf[PCAP_ERRBUF_SIZE];
- #endif
-
- if (asprintf(&pathstr, "/sys/class/net/%s/wireless", device) == -1) {
-
- return 0;
- }
- if (stat(pathstr, &statb) == 0) {
- free(pathstr);
- return 1;
- }
- free(pathstr);
- #ifdef IW_MODE_MONITOR
-
- if (has_wext(sock_fd, device, errbuf) == 1) {
-
- return 1;
- }
- #endif
- return 0;
- }
- static void map_arphrd_to_dlt(pcap_t *handle, int sock_fd, int arptype,
- const char *device, int cooked_ok)
- {
- static const char cdma_rmnet[] = "cdma_rmnet";
- switch (arptype) {
- case ARPHRD_ETHER:
-
- if (strncmp(device, cdma_rmnet, sizeof cdma_rmnet - 1) == 0) {
- handle->linktype = DLT_RAW;
- return;
- }
-
- if (!is_wifi(sock_fd, device)) {
-
- handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
-
- if (handle->dlt_list != NULL) {
- handle->dlt_list[0] = DLT_EN10MB;
- handle->dlt_list[1] = DLT_DOCSIS;
- handle->dlt_count = 2;
- }
- }
-
- case ARPHRD_METRICOM:
- case ARPHRD_LOOPBACK:
- handle->linktype = DLT_EN10MB;
- handle->offset = 2;
- break;
- case ARPHRD_EETHER:
- handle->linktype = DLT_EN3MB;
- break;
- case ARPHRD_AX25:
- handle->linktype = DLT_AX25_KISS;
- break;
- case ARPHRD_PRONET:
- handle->linktype = DLT_PRONET;
- break;
- case ARPHRD_CHAOS:
- handle->linktype = DLT_CHAOS;
- break;
- #ifndef ARPHRD_CAN
- #define ARPHRD_CAN 280
- #endif
- case ARPHRD_CAN:
-
- handle->linktype = DLT_LINUX_SLL;
- break;
- #ifndef ARPHRD_IEEE802_TR
- #define ARPHRD_IEEE802_TR 800
- #endif
- case ARPHRD_IEEE802_TR:
- case ARPHRD_IEEE802:
- handle->linktype = DLT_IEEE802;
- handle->offset = 2;
- break;
- case ARPHRD_ARCNET:
- handle->linktype = DLT_ARCNET_LINUX;
- break;
- #ifndef ARPHRD_FDDI
- #define ARPHRD_FDDI 774
- #endif
- case ARPHRD_FDDI:
- handle->linktype = DLT_FDDI;
- handle->offset = 3;
- break;
- #ifndef ARPHRD_ATM
- #define ARPHRD_ATM 19
- #endif
- case ARPHRD_ATM:
-
- if (cooked_ok)
- handle->linktype = DLT_LINUX_SLL;
- else
- handle->linktype = -1;
- break;
- #ifndef ARPHRD_IEEE80211
- #define ARPHRD_IEEE80211 801
- #endif
- case ARPHRD_IEEE80211:
- handle->linktype = DLT_IEEE802_11;
- break;
- #ifndef ARPHRD_IEEE80211_PRISM
- #define ARPHRD_IEEE80211_PRISM 802
- #endif
- case ARPHRD_IEEE80211_PRISM:
- handle->linktype = DLT_PRISM_HEADER;
- break;
- #ifndef ARPHRD_IEEE80211_RADIOTAP
- #define ARPHRD_IEEE80211_RADIOTAP 803
- #endif
- case ARPHRD_IEEE80211_RADIOTAP:
- handle->linktype = DLT_IEEE802_11_RADIO;
- break;
- case ARPHRD_PPP:
-
- if (cooked_ok)
- handle->linktype = DLT_LINUX_SLL;
- else {
-
- handle->linktype = DLT_RAW;
- }
- break;
- #ifndef ARPHRD_CISCO
- #define ARPHRD_CISCO 513
- #endif
- case ARPHRD_CISCO:
- handle->linktype = DLT_C_HDLC;
- break;
-
- case ARPHRD_TUNNEL:
- #ifndef ARPHRD_SIT
- #define ARPHRD_SIT 776
- #endif
- case ARPHRD_SIT:
- case ARPHRD_CSLIP:
- case ARPHRD_SLIP6:
- case ARPHRD_CSLIP6:
- case ARPHRD_ADAPT:
- case ARPHRD_SLIP:
- #ifndef ARPHRD_RAWHDLC
- #define ARPHRD_RAWHDLC 518
- #endif
- case ARPHRD_RAWHDLC:
- #ifndef ARPHRD_DLCI
- #define ARPHRD_DLCI 15
- #endif
- case ARPHRD_DLCI:
-
- handle->linktype = DLT_RAW;
- break;
- #ifndef ARPHRD_FRAD
- #define ARPHRD_FRAD 770
- #endif
- case ARPHRD_FRAD:
- handle->linktype = DLT_FRELAY;
- break;
- case ARPHRD_LOCALTLK:
- handle->linktype = DLT_LTALK;
- break;
- case 18:
-
- handle->linktype = DLT_IP_OVER_FC;
- break;
- #ifndef ARPHRD_FCPP
- #define ARPHRD_FCPP 784
- #endif
- case ARPHRD_FCPP:
- #ifndef ARPHRD_FCAL
- #define ARPHRD_FCAL 785
- #endif
- case ARPHRD_FCAL:
- #ifndef ARPHRD_FCPL
- #define ARPHRD_FCPL 786
- #endif
- case ARPHRD_FCPL:
- #ifndef ARPHRD_FCFABRIC
- #define ARPHRD_FCFABRIC 787
- #endif
- case ARPHRD_FCFABRIC:
-
- handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 3);
-
- if (handle->dlt_list != NULL) {
- handle->dlt_list[0] = DLT_FC_2;
- handle->dlt_list[1] = DLT_FC_2_WITH_FRAME_DELIMS;
- handle->dlt_list[2] = DLT_IP_OVER_FC;
- handle->dlt_count = 3;
- }
- handle->linktype = DLT_FC_2;
- break;
- #ifndef ARPHRD_IRDA
- #define ARPHRD_IRDA 783
- #endif
- case ARPHRD_IRDA:
-
- handle->linktype = DLT_LINUX_IRDA;
-
-
- break;
-
- #ifndef ARPHRD_LAPD
- #define ARPHRD_LAPD 8445
- #endif
- case ARPHRD_LAPD:
-
- handle->linktype = DLT_LINUX_LAPD;
- break;
- #ifndef ARPHRD_NONE
- #define ARPHRD_NONE 0xFFFE
- #endif
- case ARPHRD_NONE:
-
- handle->linktype = DLT_RAW;
- break;
- #ifndef ARPHRD_IEEE802154
- #define ARPHRD_IEEE802154 804
- #endif
- case ARPHRD_IEEE802154:
- handle->linktype = DLT_IEEE802_15_4_NOFCS;
- break;
- #ifndef ARPHRD_NETLINK
- #define ARPHRD_NETLINK 824
- #endif
- case ARPHRD_NETLINK:
- handle->linktype = DLT_NETLINK;
-
-
- break;
- #ifndef ARPHRD_VSOCKMON
- #define ARPHRD_VSOCKMON 826
- #endif
- case ARPHRD_VSOCKMON:
- handle->linktype = DLT_VSOCK;
- break;
- default:
- handle->linktype = -1;
- break;
- }
- }
- static int
- activate_new(pcap_t *handle)
- {
- #ifdef HAVE_PF_PACKET_SOCKETS
- struct pcap_linux *handlep = handle->priv;
- const char *device = handle->opt.device;
- int is_any_device = (strcmp(device, "any") == 0);
- int protocol = pcap_protocol(handle);
- int sock_fd = -1, arptype;
- #ifdef HAVE_PACKET_AUXDATA
- int val;
- #endif
- int err = 0;
- struct packet_mreq mr;
- #if defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT)
- int bpf_extensions;
- socklen_t len = sizeof(bpf_extensions);
- #endif
-
- sock_fd = is_any_device ?
- socket(PF_PACKET, SOCK_DGRAM, protocol) :
- socket(PF_PACKET, SOCK_RAW, protocol);
- if (sock_fd == -1) {
- if (errno == EINVAL || errno == EAFNOSUPPORT) {
-
- return 0;
- }
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "socket");
- if (errno == EPERM || errno == EACCES) {
-
- return PCAP_ERROR_PERM_DENIED;
- } else {
-
- return PCAP_ERROR;
- }
- }
-
- handlep->sock_packet = 0;
-
- handlep->lo_ifindex = iface_get_id(sock_fd, "lo", handle->errbuf);
-
- handle->offset = 0;
-
- if (!is_any_device) {
-
- handlep->cooked = 0;
- if (handle->opt.rfmon) {
-
- err = enter_rfmon_mode(handle, sock_fd, device);
- if (err < 0) {
-
- close(sock_fd);
- return err;
- }
- if (err == 0) {
-
- close(sock_fd);
- return PCAP_ERROR_RFMON_NOTSUP;
- }
-
- if (handlep->mondevice != NULL)
- device = handlep->mondevice;
- }
- arptype = iface_get_arptype(sock_fd, device, handle->errbuf);
- if (arptype < 0) {
- close(sock_fd);
- return arptype;
- }
- map_arphrd_to_dlt(handle, sock_fd, arptype, device, 1);
- if (handle->linktype == -1 ||
- handle->linktype == DLT_LINUX_SLL ||
- handle->linktype == DLT_LINUX_IRDA ||
- handle->linktype == DLT_LINUX_LAPD ||
- handle->linktype == DLT_NETLINK ||
- (handle->linktype == DLT_EN10MB &&
- (strncmp("isdn", device, 4) == 0 ||
- strncmp("isdY", device, 4) == 0))) {
-
- if (close(sock_fd) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "close");
- return PCAP_ERROR;
- }
- sock_fd = socket(PF_PACKET, SOCK_DGRAM, protocol);
- if (sock_fd == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "socket");
- if (errno == EPERM || errno == EACCES) {
-
- return PCAP_ERROR_PERM_DENIED;
- } else {
-
- return PCAP_ERROR;
- }
- }
- handlep->cooked = 1;
-
- if (handle->dlt_list != NULL) {
- free(handle->dlt_list);
- handle->dlt_list = NULL;
- handle->dlt_count = 0;
- }
- if (handle->linktype == -1) {
-
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "arptype %d not "
- "supported by libpcap - "
- "falling back to cooked "
- "socket",
- arptype);
- }
-
- if (handle->linktype != DLT_LINUX_IRDA &&
- handle->linktype != DLT_LINUX_LAPD &&
- handle->linktype != DLT_NETLINK)
- handle->linktype = DLT_LINUX_SLL;
- }
- handlep->ifindex = iface_get_id(sock_fd, device,
- handle->errbuf);
- if (handlep->ifindex == -1) {
- close(sock_fd);
- return PCAP_ERROR;
- }
- if ((err = iface_bind(sock_fd, handlep->ifindex,
- handle->errbuf, protocol)) != 1) {
- close(sock_fd);
- if (err < 0)
- return err;
- else
- return 0;
- }
- } else {
-
- if (handle->opt.rfmon) {
-
- close(sock_fd);
- return PCAP_ERROR_RFMON_NOTSUP;
- }
-
- handlep->cooked = 1;
- handle->linktype = DLT_LINUX_SLL;
-
- handlep->ifindex = -1;
- }
-
-
- if (!is_any_device && handle->opt.promisc) {
- memset(&mr, 0, sizeof(mr));
- mr.mr_ifindex = handlep->ifindex;
- mr.mr_type = PACKET_MR_PROMISC;
- if (setsockopt(sock_fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
- &mr, sizeof(mr)) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "setsockopt");
- close(sock_fd);
- return PCAP_ERROR;
- }
- }
-
- #ifdef HAVE_PACKET_AUXDATA
- val = 1;
- if (setsockopt(sock_fd, SOL_PACKET, PACKET_AUXDATA, &val,
- sizeof(val)) == -1 && errno != ENOPROTOOPT) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "setsockopt");
- close(sock_fd);
- return PCAP_ERROR;
- }
- handle->offset += VLAN_TAG_LEN;
- #endif
-
- if (handlep->cooked) {
- if (handle->snapshot < SLL_HDR_LEN + 1)
- handle->snapshot = SLL_HDR_LEN + 1;
- }
- handle->bufsize = handle->snapshot;
-
- switch (handle->linktype) {
- case DLT_EN10MB:
-
- handlep->vlan_offset = 2 * ETH_ALEN;
- break;
- case DLT_LINUX_SLL:
-
- handlep->vlan_offset = SLL_HDR_LEN - 2;
- break;
- default:
- handlep->vlan_offset = -1;
- break;
- }
- #if defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS)
- if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
- int nsec_tstamps = 1;
- if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPNS, &nsec_tstamps, sizeof(nsec_tstamps)) < 0) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "setsockopt: unable to set SO_TIMESTAMPNS");
- close(sock_fd);
- return PCAP_ERROR;
- }
- }
- #endif
-
- handle->fd = sock_fd;
- #if defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT)
-
- if (getsockopt(sock_fd, SOL_SOCKET, SO_BPF_EXTENSIONS,
- &bpf_extensions, &len) == 0) {
- if (bpf_extensions >= SKF_AD_VLAN_TAG_PRESENT) {
-
- handle->bpf_codegen_flags |= BPF_SPECIAL_VLAN_HANDLING;
- }
- }
- #endif
- return 1;
- #else
- strlcpy(ebuf,
- "New packet capturing interface not supported by build "
- "environment", PCAP_ERRBUF_SIZE);
- return 0;
- #endif
- }
- #ifdef HAVE_PACKET_RING
- static int
- activate_mmap(pcap_t *handle, int *status)
- {
- struct pcap_linux *handlep = handle->priv;
- int ret;
-
- handlep->oneshot_buffer = malloc(handle->snapshot);
- if (handlep->oneshot_buffer == NULL) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't allocate oneshot buffer");
- *status = PCAP_ERROR;
- return -1;
- }
- if (handle->opt.buffer_size == 0) {
-
- handle->opt.buffer_size = 2*1024*1024;
- }
- ret = prepare_tpacket_socket(handle);
- if (ret == -1) {
- free(handlep->oneshot_buffer);
- *status = PCAP_ERROR;
- return ret;
- }
- ret = create_ring(handle, status);
- if (ret == 0) {
-
- free(handlep->oneshot_buffer);
- return 0;
- }
- if (ret == -1) {
-
- free(handlep->oneshot_buffer);
- return -1;
- }
-
- switch (handlep->tp_version) {
- case TPACKET_V1:
- handle->read_op = pcap_read_linux_mmap_v1;
- break;
- case TPACKET_V1_64:
- handle->read_op = pcap_read_linux_mmap_v1_64;
- break;
- #ifdef HAVE_TPACKET2
- case TPACKET_V2:
- handle->read_op = pcap_read_linux_mmap_v2;
- break;
- #endif
- #ifdef HAVE_TPACKET3
- case TPACKET_V3:
- handle->read_op = pcap_read_linux_mmap_v3;
- break;
- #endif
- }
- handle->cleanup_op = pcap_cleanup_linux_mmap;
- handle->setfilter_op = pcap_setfilter_linux_mmap;
- handle->setnonblock_op = pcap_setnonblock_mmap;
- handle->getnonblock_op = pcap_getnonblock_mmap;
- handle->oneshot_callback = pcap_oneshot_mmap;
- handle->selectable_fd = handle->fd;
- return 1;
- }
- #else
- static int
- activate_mmap(pcap_t *handle _U_, int *status _U_)
- {
- return 0;
- }
- #endif
- #ifdef HAVE_PACKET_RING
- #if defined(HAVE_TPACKET2) || defined(HAVE_TPACKET3)
- static int
- init_tpacket(pcap_t *handle, int version, const char *version_str)
- {
- struct pcap_linux *handlep = handle->priv;
- int val = version;
- socklen_t len = sizeof(val);
-
- if (getsockopt(handle->fd, SOL_PACKET, PACKET_HDRLEN, &val, &len) < 0) {
- if (errno == ENOPROTOOPT || errno == EINVAL)
- return 1;
-
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't get %s header len on packet socket",
- version_str);
- return -1;
- }
- handlep->tp_hdrlen = val;
- val = version;
- if (setsockopt(handle->fd, SOL_PACKET, PACKET_VERSION, &val,
- sizeof(val)) < 0) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't activate %s on packet socket", version_str);
- return -1;
- }
- handlep->tp_version = version;
-
- val = VLAN_TAG_LEN;
- if (setsockopt(handle->fd, SOL_PACKET, PACKET_RESERVE, &val,
- sizeof(val)) < 0) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't set up reserve on packet socket");
- return -1;
- }
- return 0;
- }
- #endif
- #if defined(__i386__)
- #define ISA_64_BIT "x86_64"
- #elif defined(__ppc__)
- #define ISA_64_BIT "ppc64"
- #elif defined(__sparc__)
- #define ISA_64_BIT "sparc64"
- #elif defined(__s390__)
- #define ISA_64_BIT "s390x"
- #elif defined(__mips__)
- #define ISA_64_BIT "mips64"
- #elif defined(__hppa__)
- #define ISA_64_BIT "parisc64"
- #endif
- static int
- prepare_tpacket_socket(pcap_t *handle)
- {
- struct pcap_linux *handlep = handle->priv;
- #if defined(HAVE_TPACKET2) || defined(HAVE_TPACKET3)
- int ret;
- #endif
- #ifdef HAVE_TPACKET3
-
- if (!handle->opt.immediate) {
- ret = init_tpacket(handle, TPACKET_V3, "TPACKET_V3");
- if (ret == 0) {
-
- return 1;
- }
- if (ret == -1) {
-
- return -1;
- }
- }
- #endif
- #ifdef HAVE_TPACKET2
-
- ret = init_tpacket(handle, TPACKET_V2, "TPACKET_V2");
- if (ret == 0) {
-
- return 1;
- }
- if (ret == -1) {
-
- return -1;
- }
- #endif
-
- handlep->tp_version = TPACKET_V1;
- handlep->tp_hdrlen = sizeof(struct tpacket_hdr);
- #ifdef ISA_64_BIT
-
- if (sizeof(long) == 4) {
-
- struct utsname utsname;
- if (uname(&utsname) == -1) {
-
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "uname failed");
- return -1;
- }
- if (strcmp(utsname.machine, ISA_64_BIT) == 0) {
-
- handlep->tp_version = TPACKET_V1_64;
- handlep->tp_hdrlen = sizeof(struct tpacket_hdr_64);
- }
- }
- #endif
- return 1;
- }
- #define MAX(a,b) ((a)>(b)?(a):(b))
- static int
- create_ring(pcap_t *handle, int *status)
- {
- struct pcap_linux *handlep = handle->priv;
- unsigned i, j, frames_per_block;
- #ifdef HAVE_TPACKET3
-
- struct tpacket_req3 req;
- #else
- struct tpacket_req req;
- #endif
- socklen_t len;
- unsigned int sk_type, tp_reserve, maclen, tp_hdrlen, netoff, macoff;
- unsigned int frame_size;
-
- *status = 0;
- switch (handlep->tp_version) {
- case TPACKET_V1:
- case TPACKET_V1_64:
- #ifdef HAVE_TPACKET2
- case TPACKET_V2:
- #endif
-
- frame_size = handle->snapshot;
- if (handle->linktype == DLT_EN10MB) {
- unsigned int max_frame_len;
- int mtu;
- int offload;
- mtu = iface_get_mtu(handle->fd, handle->opt.device,
- handle->errbuf);
- if (mtu == -1) {
- *status = PCAP_ERROR;
- return -1;
- }
- offload = iface_get_offload(handle);
- if (offload == -1) {
- *status = PCAP_ERROR;
- return -1;
- }
- if (offload)
- max_frame_len = MAX(mtu, 65535);
- else
- max_frame_len = mtu;
- max_frame_len += 18;
- if (frame_size > max_frame_len)
- frame_size = max_frame_len;
- }
-
- len = sizeof(sk_type);
- if (getsockopt(handle->fd, SOL_SOCKET, SO_TYPE, &sk_type,
- &len) < 0) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "getsockopt");
- *status = PCAP_ERROR;
- return -1;
- }
- #ifdef PACKET_RESERVE
- len = sizeof(tp_reserve);
- if (getsockopt(handle->fd, SOL_PACKET, PACKET_RESERVE,
- &tp_reserve, &len) < 0) {
- if (errno != ENOPROTOOPT) {
-
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "getsockopt");
- *status = PCAP_ERROR;
- return -1;
- }
- tp_reserve = 0;
- }
- #else
- tp_reserve = 0;
- #endif
- maclen = (sk_type == SOCK_DGRAM) ? 0 : MAX_LINKHEADER_SIZE;
-
- tp_hdrlen = TPACKET_ALIGN(handlep->tp_hdrlen) + sizeof(struct sockaddr_ll) ;
- netoff = TPACKET_ALIGN(tp_hdrlen + (maclen < 16 ? 16 : maclen)) + tp_reserve;
-
-
- macoff = netoff - maclen;
- req.tp_frame_size = TPACKET_ALIGN(macoff + frame_size);
-
- req.tp_frame_nr = (handle->opt.buffer_size + req.tp_frame_size - 1)/req.tp_frame_size;
- break;
- #ifdef HAVE_TPACKET3
- case TPACKET_V3:
-
- req.tp_frame_size = MAXIMUM_SNAPLEN;
-
- req.tp_frame_nr = (handle->opt.buffer_size + req.tp_frame_size - 1)/req.tp_frame_size;
- break;
- #endif
- default:
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "Internal error: unknown TPACKET_ value %u",
- handlep->tp_version);
- *status = PCAP_ERROR;
- return -1;
- }
-
- req.tp_block_size = getpagesize();
- while (req.tp_block_size < req.tp_frame_size)
- req.tp_block_size <<= 1;
- frames_per_block = req.tp_block_size/req.tp_frame_size;
-
- #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP)
-
- if (handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER ||
- handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER_UNSYNCED) {
- struct hwtstamp_config hwconfig;
- struct ifreq ifr;
- int timesource;
-
- memset(&hwconfig, 0, sizeof(hwconfig));
- hwconfig.tx_type = HWTSTAMP_TX_ON;
- hwconfig.rx_filter = HWTSTAMP_FILTER_ALL;
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, handle->opt.device, sizeof(ifr.ifr_name));
- ifr.ifr_data = (void *)&hwconfig;
- if (ioctl(handle->fd, SIOCSHWTSTAMP, &ifr) < 0) {
- switch (errno) {
- case EPERM:
-
- *status = PCAP_ERROR_PERM_DENIED;
- return -1;
- case EOPNOTSUPP:
- case ERANGE:
-
- *status = PCAP_WARNING_TSTAMP_TYPE_NOTSUP;
- break;
- default:
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "SIOCSHWTSTAMP failed");
- *status = PCAP_ERROR;
- return -1;
- }
- } else {
-
- if (handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER) {
-
- timesource = SOF_TIMESTAMPING_SYS_HARDWARE;
- } else {
-
- timesource = SOF_TIMESTAMPING_RAW_HARDWARE;
- }
- if (setsockopt(handle->fd, SOL_PACKET, PACKET_TIMESTAMP,
- (void *)×ource, sizeof(timesource))) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't set PACKET_TIMESTAMP");
- *status = PCAP_ERROR;
- return -1;
- }
- }
- }
- #endif
-
- retry:
- req.tp_block_nr = req.tp_frame_nr / frames_per_block;
-
- req.tp_frame_nr = req.tp_block_nr * frames_per_block;
- #ifdef HAVE_TPACKET3
-
- req.tp_retire_blk_tov = (handlep->timeout>=0)?handlep->timeout:0;
-
- req.tp_sizeof_priv = 0;
-
- req.tp_feature_req_word = 0;
- #endif
- if (setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING,
- (void *) &req, sizeof(req))) {
- if ((errno == ENOMEM) && (req.tp_block_nr > 1)) {
-
- if (req.tp_frame_nr < 20)
- req.tp_frame_nr -= 1;
- else
- req.tp_frame_nr -= req.tp_frame_nr/20;
- goto retry;
- }
- if (errno == ENOPROTOOPT) {
-
- return 0;
- }
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't create rx ring on packet socket");
- *status = PCAP_ERROR;
- return -1;
- }
-
- handlep->mmapbuflen = req.tp_block_nr * req.tp_block_size;
- handlep->mmapbuf = mmap(0, handlep->mmapbuflen,
- PROT_READ|PROT_WRITE, MAP_SHARED, handle->fd, 0);
- if (handlep->mmapbuf == MAP_FAILED) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't mmap rx ring");
-
- destroy_ring(handle);
- *status = PCAP_ERROR;
- return -1;
- }
-
- handle->cc = req.tp_frame_nr;
- handle->buffer = malloc(handle->cc * sizeof(union thdr *));
- if (!handle->buffer) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "can't allocate ring of frame headers");
- destroy_ring(handle);
- *status = PCAP_ERROR;
- return -1;
- }
-
- handle->offset = 0;
- for (i=0; i<req.tp_block_nr; ++i) {
- void *base = &handlep->mmapbuf[i*req.tp_block_size];
- for (j=0; j<frames_per_block; ++j, ++handle->offset) {
- RING_GET_CURRENT_FRAME(handle) = base;
- base += req.tp_frame_size;
- }
- }
- handle->bufsize = req.tp_frame_size;
- handle->offset = 0;
- return 1;
- }
- static void
- destroy_ring(pcap_t *handle)
- {
- struct pcap_linux *handlep = handle->priv;
-
- struct tpacket_req req;
- memset(&req, 0, sizeof(req));
-
- (void)setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING,
- (void *) &req, sizeof(req));
-
- if (handlep->mmapbuf) {
-
- (void)munmap(handlep->mmapbuf, handlep->mmapbuflen);
- handlep->mmapbuf = NULL;
- }
- }
- static void
- pcap_oneshot_mmap(u_char *user, const struct pcap_pkthdr *h,
- const u_char *bytes)
- {
- struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
- pcap_t *handle = sp->pd;
- struct pcap_linux *handlep = handle->priv;
- *sp->hdr = *h;
- memcpy(handlep->oneshot_buffer, bytes, h->caplen);
- *sp->pkt = handlep->oneshot_buffer;
- }
- static void
- pcap_cleanup_linux_mmap( pcap_t *handle )
- {
- struct pcap_linux *handlep = handle->priv;
- destroy_ring(handle);
- if (handlep->oneshot_buffer != NULL) {
- free(handlep->oneshot_buffer);
- handlep->oneshot_buffer = NULL;
- }
- pcap_cleanup_linux(handle);
- }
- static int
- pcap_getnonblock_mmap(pcap_t *handle)
- {
- struct pcap_linux *handlep = handle->priv;
-
- return (handlep->timeout<0);
- }
- static int
- pcap_setnonblock_mmap(pcap_t *handle, int nonblock)
- {
- struct pcap_linux *handlep = handle->priv;
-
- if (pcap_setnonblock_fd(handle, nonblock) == -1)
- return -1;
-
- if (nonblock) {
- if (handlep->timeout >= 0) {
-
- handlep->timeout = ~handlep->timeout;
- }
- } else {
- if (handlep->timeout < 0) {
- handlep->timeout = ~handlep->timeout;
- }
- }
-
- set_poll_timeout(handlep);
- return 0;
- }
- static inline int
- pcap_get_ring_frame_status(pcap_t *handle, int offset)
- {
- struct pcap_linux *handlep = handle->priv;
- union thdr h;
- h.raw = RING_GET_FRAME_AT(handle, offset);
- switch (handlep->tp_version) {
- case TPACKET_V1:
- return (h.h1->tp_status);
- break;
- case TPACKET_V1_64:
- return (h.h1_64->tp_status);
- break;
- #ifdef HAVE_TPACKET2
- case TPACKET_V2:
- return (h.h2->tp_status);
- break;
- #endif
- #ifdef HAVE_TPACKET3
- case TPACKET_V3:
- return (h.h3->hdr.bh1.block_status);
- break;
- #endif
- }
-
- return 0;
- }
- #ifndef POLLRDHUP
- #define POLLRDHUP 0
- #endif
- static int pcap_wait_for_frames_mmap(pcap_t *handle)
- {
- struct pcap_linux *handlep = handle->priv;
- char c;
- struct pollfd pollinfo;
- int ret;
- pollinfo.fd = handle->fd;
- pollinfo.events = POLLIN;
- do {
-
- ret = poll(&pollinfo, 1, handlep->poll_timeout);
- if (ret < 0 && errno != EINTR) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't poll on packet socket");
- return PCAP_ERROR;
- } else if (ret > 0 &&
- (pollinfo.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
-
- if (pollinfo.revents & (POLLHUP | POLLRDHUP)) {
- pcap_snprintf(handle->errbuf,
- PCAP_ERRBUF_SIZE,
- "Hangup on packet socket");
- return PCAP_ERROR;
- }
- if (pollinfo.revents & POLLERR) {
-
- if (recv(handle->fd, &c, sizeof c,
- MSG_PEEK) != -1)
- continue;
- if (errno == ENETDOWN) {
-
- pcap_snprintf(handle->errbuf,
- PCAP_ERRBUF_SIZE,
- "The interface went down");
- } else {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "Error condition on packet socket");
- }
- return PCAP_ERROR;
- }
- if (pollinfo.revents & POLLNVAL) {
- pcap_snprintf(handle->errbuf,
- PCAP_ERRBUF_SIZE,
- "Invalid polling request on packet socket");
- return PCAP_ERROR;
- }
- }
-
- if (handle->break_loop) {
- handle->break_loop = 0;
- return PCAP_ERROR_BREAK;
- }
- } while (ret < 0);
- return 0;
- }
- static int pcap_handle_packet_mmap(
- pcap_t *handle,
- pcap_handler callback,
- u_char *user,
- unsigned char *frame,
- unsigned int tp_len,
- unsigned int tp_mac,
- unsigned int tp_snaplen,
- unsigned int tp_sec,
- unsigned int tp_usec,
- int tp_vlan_tci_valid,
- __u16 tp_vlan_tci,
- __u16 tp_vlan_tpid)
- {
- struct pcap_linux *handlep = handle->priv;
- unsigned char *bp;
- struct sockaddr_ll *sll;
- struct pcap_pkthdr pcaphdr;
- unsigned int snaplen = tp_snaplen;
-
- if (tp_mac + tp_snaplen > handle->bufsize) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "corrupted frame on kernel ring mac "
- "offset %u + caplen %u > frame len %d",
- tp_mac, tp_snaplen, handle->bufsize);
- return -1;
- }
-
- bp = frame + tp_mac;
-
- sll = (void *)frame + TPACKET_ALIGN(handlep->tp_hdrlen);
- if (handlep->cooked) {
- struct sll_header *hdrp;
-
- bp -= SLL_HDR_LEN;
-
- if (bp < (u_char *)frame +
- TPACKET_ALIGN(handlep->tp_hdrlen) +
- sizeof(struct sockaddr_ll)) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "cooked-mode frame doesn't have room for sll header");
- return -1;
- }
-
- hdrp = (struct sll_header *)bp;
- hdrp->sll_pkttype = map_packet_type_to_sll_type(
- sll->sll_pkttype);
- hdrp->sll_hatype = htons(sll->sll_hatype);
- hdrp->sll_halen = htons(sll->sll_halen);
- memcpy(hdrp->sll_addr, sll->sll_addr, SLL_ADDRLEN);
- hdrp->sll_protocol = sll->sll_protocol;
- snaplen += sizeof(struct sll_header);
- }
- if (handlep->filter_in_userland && handle->fcode.bf_insns) {
- struct bpf_aux_data aux_data;
- aux_data.vlan_tag_present = tp_vlan_tci_valid;
- aux_data.vlan_tag = tp_vlan_tci & 0x0fff;
- if (bpf_filter_with_aux_data(handle->fcode.bf_insns,
- bp,
- tp_len,
- snaplen,
- &aux_data) == 0)
- return 0;
- }
- if (!linux_check_direction(handle, sll))
- return 0;
-
- pcaphdr.ts.tv_sec = tp_sec;
- pcaphdr.ts.tv_usec = tp_usec;
- pcaphdr.caplen = tp_snaplen;
- pcaphdr.len = tp_len;
-
- if (handlep->cooked) {
-
- pcaphdr.caplen += SLL_HDR_LEN;
- pcaphdr.len += SLL_HDR_LEN;
- }
- #if defined(HAVE_TPACKET2) || defined(HAVE_TPACKET3)
- if (tp_vlan_tci_valid &&
- handlep->vlan_offset != -1 &&
- tp_snaplen >= (unsigned int) handlep->vlan_offset)
- {
- struct vlan_tag *tag;
-
- bp -= VLAN_TAG_LEN;
- memmove(bp, bp + VLAN_TAG_LEN, handlep->vlan_offset);
-
- tag = (struct vlan_tag *)(bp + handlep->vlan_offset);
- tag->vlan_tpid = htons(tp_vlan_tpid);
- tag->vlan_tci = htons(tp_vlan_tci);
-
- pcaphdr.caplen += VLAN_TAG_LEN;
- pcaphdr.len += VLAN_TAG_LEN;
- }
- #endif
-
- if (pcaphdr.caplen > (bpf_u_int32)handle->snapshot)
- pcaphdr.caplen = handle->snapshot;
-
- callback(user, &pcaphdr, bp);
- return 1;
- }
- static int
- pcap_read_linux_mmap_v1(pcap_t *handle, int max_packets, pcap_handler callback,
- u_char *user)
- {
- struct pcap_linux *handlep = handle->priv;
- union thdr h;
- int pkts = 0;
- int ret;
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h1->tp_status == TP_STATUS_KERNEL) {
-
- ret = pcap_wait_for_frames_mmap(handle);
- if (ret) {
- return ret;
- }
- }
-
- while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) {
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h1->tp_status == TP_STATUS_KERNEL)
- break;
- ret = pcap_handle_packet_mmap(
- handle,
- callback,
- user,
- h.raw,
- h.h1->tp_len,
- h.h1->tp_mac,
- h.h1->tp_snaplen,
- h.h1->tp_sec,
- h.h1->tp_usec,
- 0,
- 0,
- 0);
- if (ret == 1) {
- pkts++;
- handlep->packets_read++;
- } else if (ret < 0) {
- return ret;
- }
-
- h.h1->tp_status = TP_STATUS_KERNEL;
- if (handlep->blocks_to_filter_in_userland > 0) {
- handlep->blocks_to_filter_in_userland--;
- if (handlep->blocks_to_filter_in_userland == 0) {
-
- handlep->filter_in_userland = 0;
- }
- }
-
- if (++handle->offset >= handle->cc)
- handle->offset = 0;
-
- if (handle->break_loop) {
- handle->break_loop = 0;
- return PCAP_ERROR_BREAK;
- }
- }
- return pkts;
- }
- static int
- pcap_read_linux_mmap_v1_64(pcap_t *handle, int max_packets, pcap_handler callback,
- u_char *user)
- {
- struct pcap_linux *handlep = handle->priv;
- union thdr h;
- int pkts = 0;
- int ret;
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h1_64->tp_status == TP_STATUS_KERNEL) {
-
- ret = pcap_wait_for_frames_mmap(handle);
- if (ret) {
- return ret;
- }
- }
-
- while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) {
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h1_64->tp_status == TP_STATUS_KERNEL)
- break;
- ret = pcap_handle_packet_mmap(
- handle,
- callback,
- user,
- h.raw,
- h.h1_64->tp_len,
- h.h1_64->tp_mac,
- h.h1_64->tp_snaplen,
- h.h1_64->tp_sec,
- h.h1_64->tp_usec,
- 0,
- 0,
- 0);
- if (ret == 1) {
- pkts++;
- handlep->packets_read++;
- } else if (ret < 0) {
- return ret;
- }
-
- h.h1_64->tp_status = TP_STATUS_KERNEL;
- if (handlep->blocks_to_filter_in_userland > 0) {
- handlep->blocks_to_filter_in_userland--;
- if (handlep->blocks_to_filter_in_userland == 0) {
-
- handlep->filter_in_userland = 0;
- }
- }
-
- if (++handle->offset >= handle->cc)
- handle->offset = 0;
-
- if (handle->break_loop) {
- handle->break_loop = 0;
- return PCAP_ERROR_BREAK;
- }
- }
- return pkts;
- }
- #ifdef HAVE_TPACKET2
- static int
- pcap_read_linux_mmap_v2(pcap_t *handle, int max_packets, pcap_handler callback,
- u_char *user)
- {
- struct pcap_linux *handlep = handle->priv;
- union thdr h;
- int pkts = 0;
- int ret;
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h2->tp_status == TP_STATUS_KERNEL) {
-
- ret = pcap_wait_for_frames_mmap(handle);
- if (ret) {
- return ret;
- }
- }
-
- while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) {
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h2->tp_status == TP_STATUS_KERNEL)
- break;
- ret = pcap_handle_packet_mmap(
- handle,
- callback,
- user,
- h.raw,
- h.h2->tp_len,
- h.h2->tp_mac,
- h.h2->tp_snaplen,
- h.h2->tp_sec,
- handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? h.h2->tp_nsec : h.h2->tp_nsec / 1000,
- VLAN_VALID(h.h2, h.h2),
- h.h2->tp_vlan_tci,
- VLAN_TPID(h.h2, h.h2));
- if (ret == 1) {
- pkts++;
- handlep->packets_read++;
- } else if (ret < 0) {
- return ret;
- }
-
- h.h2->tp_status = TP_STATUS_KERNEL;
- if (handlep->blocks_to_filter_in_userland > 0) {
- handlep->blocks_to_filter_in_userland--;
- if (handlep->blocks_to_filter_in_userland == 0) {
-
- handlep->filter_in_userland = 0;
- }
- }
-
- if (++handle->offset >= handle->cc)
- handle->offset = 0;
-
- if (handle->break_loop) {
- handle->break_loop = 0;
- return PCAP_ERROR_BREAK;
- }
- }
- return pkts;
- }
- #endif
- #ifdef HAVE_TPACKET3
- static int
- pcap_read_linux_mmap_v3(pcap_t *handle, int max_packets, pcap_handler callback,
- u_char *user)
- {
- struct pcap_linux *handlep = handle->priv;
- union thdr h;
- int pkts = 0;
- int ret;
- again:
- if (handlep->current_packet == NULL) {
-
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h3->hdr.bh1.block_status == TP_STATUS_KERNEL) {
-
- ret = pcap_wait_for_frames_mmap(handle);
- if (ret) {
- return ret;
- }
- }
- }
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h3->hdr.bh1.block_status == TP_STATUS_KERNEL) {
- if (pkts == 0 && handlep->timeout == 0) {
-
- goto again;
- }
- return pkts;
- }
-
- while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) {
- int packets_to_read;
- if (handlep->current_packet == NULL) {
- h.raw = RING_GET_CURRENT_FRAME(handle);
- if (h.h3->hdr.bh1.block_status == TP_STATUS_KERNEL)
- break;
- handlep->current_packet = h.raw + h.h3->hdr.bh1.offset_to_first_pkt;
- handlep->packets_left = h.h3->hdr.bh1.num_pkts;
- }
- packets_to_read = handlep->packets_left;
- if (!PACKET_COUNT_IS_UNLIMITED(max_packets) &&
- packets_to_read > (max_packets - pkts)) {
-
- packets_to_read = max_packets - pkts;
- }
- while (packets_to_read-- && !handle->break_loop) {
- struct tpacket3_hdr* tp3_hdr = (struct tpacket3_hdr*) handlep->current_packet;
- ret = pcap_handle_packet_mmap(
- handle,
- callback,
- user,
- handlep->current_packet,
- tp3_hdr->tp_len,
- tp3_hdr->tp_mac,
- tp3_hdr->tp_snaplen,
- tp3_hdr->tp_sec,
- handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? tp3_hdr->tp_nsec : tp3_hdr->tp_nsec / 1000,
- VLAN_VALID(tp3_hdr, &tp3_hdr->hv1),
- tp3_hdr->hv1.tp_vlan_tci,
- VLAN_TPID(tp3_hdr, &tp3_hdr->hv1));
- if (ret == 1) {
- pkts++;
- handlep->packets_read++;
- } else if (ret < 0) {
- handlep->current_packet = NULL;
- return ret;
- }
- handlep->current_packet += tp3_hdr->tp_next_offset;
- handlep->packets_left--;
- }
- if (handlep->packets_left <= 0) {
-
- h.h3->hdr.bh1.block_status = TP_STATUS_KERNEL;
- if (handlep->blocks_to_filter_in_userland > 0) {
- handlep->blocks_to_filter_in_userland--;
- if (handlep->blocks_to_filter_in_userland == 0) {
-
- handlep->filter_in_userland = 0;
- }
- }
-
- if (++handle->offset >= handle->cc)
- handle->offset = 0;
- handlep->current_packet = NULL;
- }
-
- if (handle->break_loop) {
- handle->break_loop = 0;
- return PCAP_ERROR_BREAK;
- }
- }
- if (pkts == 0 && handlep->timeout == 0) {
-
- goto again;
- }
- return pkts;
- }
- #endif
- static int
- pcap_setfilter_linux_mmap(pcap_t *handle, struct bpf_program *filter)
- {
- struct pcap_linux *handlep = handle->priv;
- int n, offset;
- int ret;
-
- ret = pcap_setfilter_linux_common(handle, filter, 1);
- if (ret < 0)
- return ret;
-
- if (handlep->filter_in_userland)
- return ret;
-
- offset = handle->offset;
- if (--offset < 0)
- offset = handle->cc - 1;
- for (n=0; n < handle->cc; ++n) {
- if (--offset < 0)
- offset = handle->cc - 1;
- if (pcap_get_ring_frame_status(handle, offset) != TP_STATUS_KERNEL)
- break;
- }
-
- if (n != 0)
- n--;
-
- handlep->blocks_to_filter_in_userland = handle->cc - n;
- handlep->filter_in_userland = 1;
- return ret;
- }
- #endif
- #ifdef HAVE_PF_PACKET_SOCKETS
- static int
- iface_get_id(int fd, const char *device, char *ebuf)
- {
- struct ifreq ifr;
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "SIOCGIFINDEX");
- return -1;
- }
- return ifr.ifr_ifindex;
- }
- static int
- iface_bind(int fd, int ifindex, char *ebuf, int protocol)
- {
- struct sockaddr_ll sll;
- int err;
- socklen_t errlen = sizeof(err);
- memset(&sll, 0, sizeof(sll));
- sll.sll_family = AF_PACKET;
- sll.sll_ifindex = ifindex;
- sll.sll_protocol = protocol;
- if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
- if (errno == ENETDOWN) {
-
- return PCAP_ERROR_IFACE_NOT_UP;
- } else {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "bind");
- return PCAP_ERROR;
- }
- }
-
- if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "getsockopt");
- return 0;
- }
- if (err == ENETDOWN) {
-
- return PCAP_ERROR_IFACE_NOT_UP;
- } else if (err > 0) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- err, "bind");
- return 0;
- }
- return 1;
- }
- #ifdef IW_MODE_MONITOR
- static int
- has_wext(int sock_fd, const char *device, char *ebuf)
- {
- struct iwreq ireq;
- if (is_bonding_device(sock_fd, device))
- return 0;
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- if (ioctl(sock_fd, SIOCGIWNAME, &ireq) >= 0)
- return 1;
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
- "%s: SIOCGIWNAME", device);
- if (errno == ENODEV)
- return PCAP_ERROR_NO_SUCH_DEVICE;
- return 0;
- }
- typedef enum {
- MONITOR_WEXT,
- MONITOR_HOSTAP,
- MONITOR_PRISM,
- MONITOR_PRISM54,
- MONITOR_ACX100,
- MONITOR_RT2500,
- MONITOR_RT2570,
- MONITOR_RT73,
- MONITOR_RTL8XXX
- } monitor_type;
- static int
- enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device)
- {
-
- struct pcap_linux *handlep = handle->priv;
- int err;
- struct iwreq ireq;
- struct iw_priv_args *priv;
- monitor_type montype;
- int i;
- __u32 cmd;
- struct ifreq ifr;
- int oldflags;
- int args[2];
- int channel;
-
- err = has_wext(sock_fd, device, handle->errbuf);
- if (err <= 0)
- return err;
-
- montype = MONITOR_WEXT;
- cmd = 0;
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- ireq.u.data.pointer = (void *)args;
- ireq.u.data.length = 0;
- ireq.u.data.flags = 0;
- if (ioctl(sock_fd, SIOCGIWPRIV, &ireq) != -1) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "%s: SIOCGIWPRIV with a zero-length buffer didn't fail!",
- device);
- return PCAP_ERROR;
- }
- if (errno != EOPNOTSUPP) {
-
- if (errno != E2BIG) {
-
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "%s: SIOCGIWPRIV", device);
- return PCAP_ERROR;
- }
-
- priv = malloc(ireq.u.data.length * sizeof (struct iw_priv_args));
- if (priv == NULL) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "malloc");
- return PCAP_ERROR;
- }
- ireq.u.data.pointer = (void *)priv;
- if (ioctl(sock_fd, SIOCGIWPRIV, &ireq) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "%s: SIOCGIWPRIV", device);
- free(priv);
- return PCAP_ERROR;
- }
-
- for (i = 0; i < ireq.u.data.length; i++) {
- if (strcmp(priv[i].name, "monitor_type") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_INT)
- break;
- if (!(priv[i].set_args & IW_PRIV_SIZE_FIXED))
- break;
- if ((priv[i].set_args & IW_PRIV_SIZE_MASK) != 1)
- break;
- montype = MONITOR_HOSTAP;
- cmd = priv[i].cmd;
- break;
- }
- if (strcmp(priv[i].name, "set_prismhdr") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_INT)
- break;
- if (!(priv[i].set_args & IW_PRIV_SIZE_FIXED))
- break;
- if ((priv[i].set_args & IW_PRIV_SIZE_MASK) != 1)
- break;
- montype = MONITOR_PRISM54;
- cmd = priv[i].cmd;
- break;
- }
- if (strcmp(priv[i].name, "forceprismheader") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_INT)
- break;
- if (!(priv[i].set_args & IW_PRIV_SIZE_FIXED))
- break;
- if ((priv[i].set_args & IW_PRIV_SIZE_MASK) != 1)
- break;
- montype = MONITOR_RT2570;
- cmd = priv[i].cmd;
- break;
- }
- if (strcmp(priv[i].name, "forceprism") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_CHAR)
- break;
- if (priv[i].set_args & IW_PRIV_SIZE_FIXED)
- break;
- montype = MONITOR_RT73;
- cmd = priv[i].cmd;
- break;
- }
- if (strcmp(priv[i].name, "prismhdr") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_INT)
- break;
- if (!(priv[i].set_args & IW_PRIV_SIZE_FIXED))
- break;
- if ((priv[i].set_args & IW_PRIV_SIZE_MASK) != 1)
- break;
- montype = MONITOR_RTL8XXX;
- cmd = priv[i].cmd;
- break;
- }
- if (strcmp(priv[i].name, "rfmontx") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_INT)
- break;
- if ((priv[i].set_args & IW_PRIV_SIZE_MASK) != 2)
- break;
- montype = MONITOR_RT2500;
- cmd = priv[i].cmd;
- break;
- }
- if (strcmp(priv[i].name, "monitor") == 0) {
-
- if ((priv[i].set_args & IW_PRIV_TYPE_MASK) != IW_PRIV_TYPE_INT)
- break;
- if (!(priv[i].set_args & IW_PRIV_SIZE_FIXED))
- break;
- switch (priv[i].set_args & IW_PRIV_SIZE_MASK) {
- case 1:
- montype = MONITOR_PRISM;
- cmd = priv[i].cmd;
- break;
- case 2:
- montype = MONITOR_ACX100;
- cmd = priv[i].cmd;
- break;
- default:
- break;
- }
- }
- }
- free(priv);
- }
-
-
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- if (ioctl(sock_fd, SIOCGIWMODE, &ireq) == -1) {
-
- return PCAP_ERROR_RFMON_NOTSUP;
- }
-
- if (ireq.u.mode == IW_MODE_MONITOR) {
-
- return 1;
- }
-
-
- if (!pcap_do_addexit(handle)) {
-
- return PCAP_ERROR_RFMON_NOTSUP;
- }
-
- handlep->oldmode = ireq.u.mode;
-
- if (montype == MONITOR_PRISM) {
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- ireq.u.data.length = 1;
- args[0] = 3;
- memcpy(ireq.u.name, args, sizeof (int));
- if (ioctl(sock_fd, cmd, &ireq) != -1) {
-
- handlep->must_do_on_close |= MUST_CLEAR_RFMON;
-
- pcap_add_to_pcaps_to_close(handle);
- return 1;
- }
-
- }
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "%s: Can't get flags", device);
- return PCAP_ERROR;
- }
- oldflags = 0;
- if (ifr.ifr_flags & IFF_UP) {
- oldflags = ifr.ifr_flags;
- ifr.ifr_flags &= ~IFF_UP;
- if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "%s: Can't set flags",
- device);
- return PCAP_ERROR;
- }
- }
-
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- ireq.u.mode = IW_MODE_MONITOR;
- if (ioctl(sock_fd, SIOCSIWMODE, &ireq) == -1) {
-
- ifr.ifr_flags = oldflags;
- if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "%s: Can't set flags",
- device);
- return PCAP_ERROR;
- }
- return PCAP_ERROR_RFMON_NOTSUP;
- }
-
-
- switch (montype) {
- case MONITOR_WEXT:
-
- break;
- case MONITOR_HOSTAP:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 3;
- memcpy(ireq.u.name, args, sizeof (int));
- if (ioctl(sock_fd, cmd, &ireq) != -1)
- break;
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 2;
- memcpy(ireq.u.name, args, sizeof (int));
- if (ioctl(sock_fd, cmd, &ireq) != -1)
- break;
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 1;
- memcpy(ireq.u.name, args, sizeof (int));
- ioctl(sock_fd, cmd, &ireq);
- break;
- case MONITOR_PRISM:
-
- break;
- case MONITOR_PRISM54:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 3;
- memcpy(ireq.u.name, args, sizeof (int));
- ioctl(sock_fd, cmd, &ireq);
- break;
- case MONITOR_ACX100:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- if (ioctl(sock_fd, SIOCGIWFREQ, &ireq) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "%s: SIOCGIWFREQ", device);
- return PCAP_ERROR;
- }
- channel = ireq.u.freq.m;
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 1;
- args[1] = channel;
- memcpy(ireq.u.name, args, 2*sizeof (int));
- ioctl(sock_fd, cmd, &ireq);
- break;
- case MONITOR_RT2500:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 0;
- memcpy(ireq.u.name, args, sizeof (int));
- ioctl(sock_fd, cmd, &ireq);
- break;
- case MONITOR_RT2570:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 1;
- memcpy(ireq.u.name, args, sizeof (int));
- ioctl(sock_fd, cmd, &ireq);
- break;
- case MONITOR_RT73:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- ireq.u.data.length = 1;
- ireq.u.data.pointer = "1";
- ireq.u.data.flags = 0;
- ioctl(sock_fd, cmd, &ireq);
- break;
- case MONITOR_RTL8XXX:
-
- memset(&ireq, 0, sizeof ireq);
- strlcpy(ireq.ifr_ifrn.ifrn_name, device,
- sizeof ireq.ifr_ifrn.ifrn_name);
- args[0] = 1;
- memcpy(ireq.u.name, args, sizeof (int));
- ioctl(sock_fd, cmd, &ireq);
- break;
- }
-
- if (oldflags != 0) {
- ifr.ifr_flags = oldflags;
- if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "%s: Can't set flags",
- device);
-
- if (ioctl(handle->fd, SIOCSIWMODE, &ireq) == -1) {
-
- fprintf(stderr,
- "Can't restore interface wireless mode (SIOCSIWMODE failed: %s).\n"
- "Please adjust manually.\n",
- strerror(errno));
- }
- return PCAP_ERROR;
- }
- }
-
- handlep->must_do_on_close |= MUST_CLEAR_RFMON;
-
- pcap_add_to_pcaps_to_close(handle);
- return 1;
- }
- #endif
- static int
- enter_rfmon_mode(pcap_t *handle, int sock_fd, const char *device)
- {
- #if defined(HAVE_LIBNL) || defined(IW_MODE_MONITOR)
- int ret;
- #endif
- #ifdef HAVE_LIBNL
- ret = enter_rfmon_mode_mac80211(handle, sock_fd, device);
- if (ret < 0)
- return ret;
- if (ret == 1)
- return 1;
- #endif
- #ifdef IW_MODE_MONITOR
- ret = enter_rfmon_mode_wext(handle, sock_fd, device);
- if (ret < 0)
- return ret;
- if (ret == 1)
- return 1;
- #endif
-
- return 0;
- }
- #if defined(HAVE_LINUX_NET_TSTAMP_H) && defined(PACKET_TIMESTAMP)
- static const struct {
- int soft_timestamping_val;
- int pcap_tstamp_val;
- } sof_ts_type_map[3] = {
- { SOF_TIMESTAMPING_SOFTWARE, PCAP_TSTAMP_HOST },
- { SOF_TIMESTAMPING_SYS_HARDWARE, PCAP_TSTAMP_ADAPTER },
- { SOF_TIMESTAMPING_RAW_HARDWARE, PCAP_TSTAMP_ADAPTER_UNSYNCED }
- };
- #define NUM_SOF_TIMESTAMPING_TYPES (sizeof sof_ts_type_map / sizeof sof_ts_type_map[0])
- static void
- iface_set_all_ts_types(pcap_t *handle)
- {
- u_int i;
- handle->tstamp_type_count = NUM_SOF_TIMESTAMPING_TYPES;
- handle->tstamp_type_list = malloc(NUM_SOF_TIMESTAMPING_TYPES * sizeof(u_int));
- for (i = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++)
- handle->tstamp_type_list[i] = sof_ts_type_map[i].pcap_tstamp_val;
- }
- #ifdef ETHTOOL_GET_TS_INFO
- static int
- iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf)
- {
- int fd;
- struct ifreq ifr;
- struct ethtool_ts_info info;
- int num_ts_types;
- u_int i, j;
-
- if (strcmp(device, "any") == 0) {
- handle->tstamp_type_list = NULL;
- return 0;
- }
-
- fd = socket(PF_UNIX, SOCK_RAW, 0);
- if (fd < 0) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "socket for SIOCETHTOOL(ETHTOOL_GET_TS_INFO)");
- return -1;
- }
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- memset(&info, 0, sizeof(info));
- info.cmd = ETHTOOL_GET_TS_INFO;
- ifr.ifr_data = (caddr_t)&info;
- if (ioctl(fd, SIOCETHTOOL, &ifr) == -1) {
- int save_errno = errno;
- close(fd);
- switch (save_errno) {
- case EOPNOTSUPP:
- case EINVAL:
-
- iface_set_all_ts_types(handle);
- return 0;
- case ENODEV:
-
- handle->tstamp_type_list = NULL;
- return 0;
- default:
-
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- save_errno,
- "%s: SIOCETHTOOL(ETHTOOL_GET_TS_INFO) ioctl failed",
- device);
- return -1;
- }
- }
- close(fd);
-
- if (!(info.rx_filters & (1 << HWTSTAMP_FILTER_ALL))) {
-
- handle->tstamp_type_list = NULL;
- return 0;
- }
- num_ts_types = 0;
- for (i = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) {
- if (info.so_timestamping & sof_ts_type_map[i].soft_timestamping_val)
- num_ts_types++;
- }
- handle->tstamp_type_count = num_ts_types;
- if (num_ts_types != 0) {
- handle->tstamp_type_list = malloc(num_ts_types * sizeof(u_int));
- for (i = 0, j = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) {
- if (info.so_timestamping & sof_ts_type_map[i].soft_timestamping_val) {
- handle->tstamp_type_list[j] = sof_ts_type_map[i].pcap_tstamp_val;
- j++;
- }
- }
- } else
- handle->tstamp_type_list = NULL;
- return 0;
- }
- #else
- static int
- iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf _U_)
- {
-
- if (strcmp(device, "any") == 0) {
- handle->tstamp_type_list = NULL;
- return 0;
- }
-
- iface_set_all_ts_types(handle);
- return 0;
- }
- #endif
- #endif
- #ifdef HAVE_PACKET_RING
- #if defined(SIOCETHTOOL) && (defined(ETHTOOL_GTSO) || defined(ETHTOOL_GUFO) || defined(ETHTOOL_GGSO) || defined(ETHTOOL_GFLAGS) || defined(ETHTOOL_GGRO))
- static int
- iface_ethtool_flag_ioctl(pcap_t *handle, int cmd, const char *cmdname)
- {
- struct ifreq ifr;
- struct ethtool_value eval;
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, handle->opt.device, sizeof(ifr.ifr_name));
- eval.cmd = cmd;
- eval.data = 0;
- ifr.ifr_data = (caddr_t)&eval;
- if (ioctl(handle->fd, SIOCETHTOOL, &ifr) == -1) {
- if (errno == EOPNOTSUPP || errno == EINVAL) {
-
- return 0;
- }
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "%s: SIOCETHTOOL(%s) ioctl failed",
- handle->opt.device, cmdname);
- return -1;
- }
- return eval.data;
- }
- static int
- iface_get_offload(pcap_t *handle)
- {
- int ret;
- #ifdef ETHTOOL_GTSO
- ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GTSO, "ETHTOOL_GTSO");
- if (ret == -1)
- return -1;
- if (ret)
- return 1;
- #endif
- #ifdef ETHTOOL_GUFO
- ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GUFO, "ETHTOOL_GUFO");
- if (ret == -1)
- return -1;
- if (ret)
- return 1;
- #endif
- #ifdef ETHTOOL_GGSO
-
- ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GGSO, "ETHTOOL_GGSO");
- if (ret == -1)
- return -1;
- if (ret)
- return 1;
- #endif
- #ifdef ETHTOOL_GFLAGS
- ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
- if (ret == -1)
- return -1;
- if (ret & ETH_FLAG_LRO)
- return 1;
- #endif
- #ifdef ETHTOOL_GGRO
-
- ret = iface_ethtool_flag_ioctl(handle, ETHTOOL_GGRO, "ETHTOOL_GGRO");
- if (ret == -1)
- return -1;
- if (ret)
- return 1;
- #endif
- return 0;
- }
- #else
- static int
- iface_get_offload(pcap_t *handle _U_)
- {
-
- return 0;
- }
- #endif
- #endif
- #endif
- static int
- activate_old(pcap_t *handle)
- {
- struct pcap_linux *handlep = handle->priv;
- int err;
- int arptype;
- struct ifreq ifr;
- const char *device = handle->opt.device;
- struct utsname utsname;
- int mtu;
-
- handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
- if (handle->fd == -1) {
- err = errno;
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- err, "socket");
- if (err == EPERM || err == EACCES) {
-
- return PCAP_ERROR_PERM_DENIED;
- } else {
-
- return PCAP_ERROR;
- }
- }
-
- handlep->sock_packet = 1;
-
- handlep->cooked = 0;
-
- if (strcmp(device, "any") == 0) {
- strlcpy(handle->errbuf, "pcap_activate: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
- PCAP_ERRBUF_SIZE);
- return PCAP_ERROR;
- }
- if (iface_bind_old(handle->fd, device, handle->errbuf) == -1)
- return PCAP_ERROR;
-
- arptype = iface_get_arptype(handle->fd, device, handle->errbuf);
- if (arptype < 0)
- return PCAP_ERROR;
-
- map_arphrd_to_dlt(handle, handle->fd, arptype, device, 0);
- if (handle->linktype == -1) {
- pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
- "unknown arptype %d", arptype);
- return PCAP_ERROR;
- }
-
- if (handle->opt.promisc) {
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "SIOCGIFFLAGS");
- return PCAP_ERROR;
- }
- if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
-
-
- if (!pcap_do_addexit(handle)) {
-
- return PCAP_ERROR;
- }
- ifr.ifr_flags |= IFF_PROMISC;
- if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno, "SIOCSIFFLAGS");
- return PCAP_ERROR;
- }
- handlep->must_do_on_close |= MUST_CLEAR_PROMISC;
-
- pcap_add_to_pcaps_to_close(handle);
- }
- }
-
- if (uname(&utsname) < 0 ||
- strncmp(utsname.release, "2.0", 3) == 0) {
-
- mtu = iface_get_mtu(handle->fd, device, handle->errbuf);
- if (mtu == -1)
- return PCAP_ERROR;
- handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
- if (handle->bufsize < (u_int)handle->snapshot)
- handle->bufsize = (u_int)handle->snapshot;
- } else {
-
- handle->bufsize = (u_int)handle->snapshot;
- }
-
- handle->offset = 0;
-
- handlep->vlan_offset = -1;
- return 1;
- }
- static int
- iface_bind_old(int fd, const char *device, char *ebuf)
- {
- struct sockaddr saddr;
- int err;
- socklen_t errlen = sizeof(err);
- memset(&saddr, 0, sizeof(saddr));
- strlcpy(saddr.sa_data, device, sizeof(saddr.sa_data));
- if (bind(fd, &saddr, sizeof(saddr)) == -1) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "bind");
- return -1;
- }
-
- if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "getsockopt");
- return -1;
- }
- if (err > 0) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- err, "bind");
- return -1;
- }
- return 0;
- }
- static int
- iface_get_mtu(int fd, const char *device, char *ebuf)
- {
- struct ifreq ifr;
- if (!device)
- return BIGGER_THAN_ALL_MTUS;
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "SIOCGIFMTU");
- return -1;
- }
- return ifr.ifr_mtu;
- }
- static int
- iface_get_arptype(int fd, const char *device, char *ebuf)
- {
- struct ifreq ifr;
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
- pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
- errno, "SIOCGIFHWADDR");
- if (errno == ENODEV) {
-
- return PCAP_ERROR_NO_SUCH_DEVICE;
- }
- return PCAP_ERROR;
- }
- return ifr.ifr_hwaddr.sa_family;
- }
- #ifdef SO_ATTACH_FILTER
- static int
- fix_program(pcap_t *handle, struct sock_fprog *fcode, int is_mmapped)
- {
- struct pcap_linux *handlep = handle->priv;
- size_t prog_size;
- register int i;
- register struct bpf_insn *p;
- struct bpf_insn *f;
- int len;
-
- prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len;
- len = handle->fcode.bf_len;
- f = (struct bpf_insn *)malloc(prog_size);
- if (f == NULL) {
- pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
- errno, "malloc");
- return -1;
- }
- memcpy(f, handle->fcode.bf_insns, prog_size);
- fcode->len = len;
- fcode->filter = (struct sock_filter *) f;
- for (i = 0; i < len; ++i) {
- p = &f[i];
-
- switch (BPF_CLASS(p->code)) {
- case BPF_RET:
-
- if (!is_mmapped) {
-
- if (BPF_MODE(p->code) == BPF_K) {
-
- if (p->k != 0)
- p->k = MAXIMUM_SNAPLEN;
- }
- }
- break;
- case BPF_LD:
- case BPF_LDX:
-
- switch (BPF_MODE(p->code)) {
- case BPF_ABS:
- case BPF_IND:
- case BPF_MSH:
-
- if (handlep->cooked) {
-
- if (fix_offset(p) < 0) {
-
- return 0;
- }
- }
- break;
- }
- break;
- }
- }
- return 1;
- }
- static int
- fix_offset(struct bpf_insn *p)
- {
-
- if (p->k >= SLL_HDR_LEN) {
-
- p->k -= SLL_HDR_LEN;
- } else if (p->k == 0) {
-
- p->k = SKF_AD_OFF + SKF_AD_PKTTYPE;
- } else if (p->k == 14) {
-
- p->k = SKF_AD_OFF + SKF_AD_PROTOCOL;
- } else if ((bpf_int32)(p->k) > 0) {
-
- return -1;
- }
- return 0;
- }
- static int
- set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode)
- {
- int total_filter_on = 0;
- int save_mode;
- int ret;
- int save_errno;
-
- if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
- &total_fcode, sizeof(total_fcode)) == 0) {
- char drain[1];
-
- total_filter_on = 1;
-
- save_mode = fcntl(handle->fd, F_GETFL, 0);
- if (save_mode == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't get FD flags when changing filter");
- return -2;
- }
- if (fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) < 0) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't set nonblocking mode when changing filter");
- return -2;
- }
- while (recv(handle->fd, &drain, sizeof drain, MSG_TRUNC) >= 0)
- ;
- save_errno = errno;
- if (save_errno != EAGAIN) {
-
- (void)fcntl(handle->fd, F_SETFL, save_mode);
- (void)reset_kernel_filter(handle);
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, save_errno,
- "recv failed when changing filter");
- return -2;
- }
- if (fcntl(handle->fd, F_SETFL, save_mode) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't restore FD flags when changing filter");
- return -2;
- }
- }
-
- ret = setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
- fcode, sizeof(*fcode));
- if (ret == -1 && total_filter_on) {
-
- save_errno = errno;
-
- if (reset_kernel_filter(handle) == -1) {
- pcap_fmt_errmsg_for_errno(handle->errbuf,
- PCAP_ERRBUF_SIZE, errno,
- "can't remove kernel total filter");
- return -2;
- }
- errno = save_errno;
- }
- return ret;
- }
- static int
- reset_kernel_filter(pcap_t *handle)
- {
- int ret;
-
- int dummy = 0;
- ret = setsockopt(handle->fd, SOL_SOCKET, SO_DETACH_FILTER,
- &dummy, sizeof(dummy));
-
- if (ret == -1 && errno != ENOENT && errno != ENONET)
- return -1;
- return 0;
- }
- #endif
- int
- pcap_set_protocol_linux(pcap_t *p, int protocol)
- {
- if (pcap_check_activated(p))
- return (PCAP_ERROR_ACTIVATED);
- p->opt.protocol = protocol;
- return (0);
- }
- const char *
- pcap_lib_version(void)
- {
- #ifdef HAVE_PACKET_RING
- #if defined(HAVE_TPACKET3)
- return (PCAP_VERSION_STRING " (with TPACKET_V3)");
- #elif defined(HAVE_TPACKET2)
- return (PCAP_VERSION_STRING " (with TPACKET_V2)");
- #else
- return (PCAP_VERSION_STRING " (with TPACKET_V1)");
- #endif
- #else
- return (PCAP_VERSION_STRING " (without TPACKET)");
- #endif
- }
|