gencode.c 239 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242
  1. /*#define CHASE_CHAIN*/
  2. /*
  3. * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that: (1) source code distributions
  8. * retain the above copyright notice and this paragraph in its entirety, (2)
  9. * distributions including binary code include the above copyright notice and
  10. * this paragraph in its entirety in the documentation or other materials
  11. * provided with the distribution, and (3) all advertising materials mentioning
  12. * features or use of this software display the following acknowledgement:
  13. * ``This product includes software developed by the University of California,
  14. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15. * the University nor the names of its contributors may be used to endorse
  16. * or promote products derived from this software without specific prior
  17. * written permission.
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <pcap-types.h>
  26. #ifdef _WIN32
  27. #include <ws2tcpip.h>
  28. #else
  29. #include <sys/socket.h>
  30. #ifdef __NetBSD__
  31. #include <sys/param.h>
  32. #endif
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #endif /* _WIN32 */
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <memory.h>
  39. #include <setjmp.h>
  40. #include <stdarg.h>
  41. #ifdef MSDOS
  42. #include "pcap-dos.h"
  43. #endif
  44. #include "pcap-int.h"
  45. #include "ethertype.h"
  46. #include "nlpid.h"
  47. #include "llc.h"
  48. #include "gencode.h"
  49. #include "ieee80211.h"
  50. #include "atmuni31.h"
  51. #include "sunatmpos.h"
  52. #include "ppp.h"
  53. #include "pcap/sll.h"
  54. #include "pcap/ipnet.h"
  55. #include "arcnet.h"
  56. #include "grammar.h"
  57. #include "scanner.h"
  58. #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
  59. #include <linux/types.h>
  60. #include <linux/if_packet.h>
  61. #include <linux/filter.h>
  62. #endif
  63. #ifdef HAVE_NET_PFVAR_H
  64. #include <sys/socket.h>
  65. #include <net/if.h>
  66. #include <net/pfvar.h>
  67. #include <net/if_pflog.h>
  68. #endif
  69. #ifndef offsetof
  70. #define offsetof(s, e) ((size_t)&((s *)0)->e)
  71. #endif
  72. #ifdef _WIN32
  73. #ifdef INET6
  74. #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
  75. /* IPv6 address */
  76. struct in6_addr
  77. {
  78. union
  79. {
  80. uint8_t u6_addr8[16];
  81. uint16_t u6_addr16[8];
  82. uint32_t u6_addr32[4];
  83. } in6_u;
  84. #define s6_addr in6_u.u6_addr8
  85. #define s6_addr16 in6_u.u6_addr16
  86. #define s6_addr32 in6_u.u6_addr32
  87. #define s6_addr64 in6_u.u6_addr64
  88. };
  89. typedef unsigned short sa_family_t;
  90. #define __SOCKADDR_COMMON(sa_prefix) \
  91. sa_family_t sa_prefix##family
  92. /* Ditto, for IPv6. */
  93. struct sockaddr_in6
  94. {
  95. __SOCKADDR_COMMON (sin6_);
  96. uint16_t sin6_port; /* Transport layer port # */
  97. uint32_t sin6_flowinfo; /* IPv6 flow information */
  98. struct in6_addr sin6_addr; /* IPv6 address */
  99. };
  100. #ifndef EAI_ADDRFAMILY
  101. struct addrinfo {
  102. int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
  103. int ai_family; /* PF_xxx */
  104. int ai_socktype; /* SOCK_xxx */
  105. int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
  106. size_t ai_addrlen; /* length of ai_addr */
  107. char *ai_canonname; /* canonical name for hostname */
  108. struct sockaddr *ai_addr; /* binary address */
  109. struct addrinfo *ai_next; /* next structure in linked list */
  110. };
  111. #endif /* EAI_ADDRFAMILY */
  112. #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
  113. #endif /* INET6 */
  114. #else /* _WIN32 */
  115. #include <netdb.h> /* for "struct addrinfo" */
  116. #endif /* _WIN32 */
  117. #include <pcap/namedb.h>
  118. #include "nametoaddr.h"
  119. #define ETHERMTU 1500
  120. #ifndef ETHERTYPE_TEB
  121. #define ETHERTYPE_TEB 0x6558
  122. #endif
  123. #ifndef IPPROTO_HOPOPTS
  124. #define IPPROTO_HOPOPTS 0
  125. #endif
  126. #ifndef IPPROTO_ROUTING
  127. #define IPPROTO_ROUTING 43
  128. #endif
  129. #ifndef IPPROTO_FRAGMENT
  130. #define IPPROTO_FRAGMENT 44
  131. #endif
  132. #ifndef IPPROTO_DSTOPTS
  133. #define IPPROTO_DSTOPTS 60
  134. #endif
  135. #ifndef IPPROTO_SCTP
  136. #define IPPROTO_SCTP 132
  137. #endif
  138. #define GENEVE_PORT 6081
  139. #ifdef HAVE_OS_PROTO_H
  140. #include "os-proto.h"
  141. #endif
  142. #define JMP(c) ((c)|BPF_JMP|BPF_K)
  143. /*
  144. * "Push" the current value of the link-layer header type and link-layer
  145. * header offset onto a "stack", and set a new value. (It's not a
  146. * full-blown stack; we keep only the top two items.)
  147. */
  148. #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
  149. { \
  150. (cs)->prevlinktype = (cs)->linktype; \
  151. (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
  152. (cs)->linktype = (new_linktype); \
  153. (cs)->off_linkhdr.is_variable = (new_is_variable); \
  154. (cs)->off_linkhdr.constant_part = (new_constant_part); \
  155. (cs)->off_linkhdr.reg = (new_reg); \
  156. (cs)->is_geneve = 0; \
  157. }
  158. /*
  159. * Offset "not set" value.
  160. */
  161. #define OFFSET_NOT_SET 0xffffffffU
  162. /*
  163. * Absolute offsets, which are offsets from the beginning of the raw
  164. * packet data, are, in the general case, the sum of a variable value
  165. * and a constant value; the variable value may be absent, in which
  166. * case the offset is only the constant value, and the constant value
  167. * may be zero, in which case the offset is only the variable value.
  168. *
  169. * bpf_abs_offset is a structure containing all that information:
  170. *
  171. * is_variable is 1 if there's a variable part.
  172. *
  173. * constant_part is the constant part of the value, possibly zero;
  174. *
  175. * if is_variable is 1, reg is the register number for a register
  176. * containing the variable value if the register has been assigned,
  177. * and -1 otherwise.
  178. */
  179. typedef struct {
  180. int is_variable;
  181. u_int constant_part;
  182. int reg;
  183. } bpf_abs_offset;
  184. /*
  185. * Value passed to gen_load_a() to indicate what the offset argument
  186. * is relative to the beginning of.
  187. */
  188. enum e_offrel {
  189. OR_PACKET, /* full packet data */
  190. OR_LINKHDR, /* link-layer header */
  191. OR_PREVLINKHDR, /* previous link-layer header */
  192. OR_LLC, /* 802.2 LLC header */
  193. OR_PREVMPLSHDR, /* previous MPLS header */
  194. OR_LINKTYPE, /* link-layer type */
  195. OR_LINKPL, /* link-layer payload */
  196. OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */
  197. OR_TRAN_IPV4, /* transport-layer header, with IPv4 network layer */
  198. OR_TRAN_IPV6 /* transport-layer header, with IPv6 network layer */
  199. };
  200. /*
  201. * We divy out chunks of memory rather than call malloc each time so
  202. * we don't have to worry about leaking memory. It's probably
  203. * not a big deal if all this memory was wasted but if this ever
  204. * goes into a library that would probably not be a good idea.
  205. *
  206. * XXX - this *is* in a library....
  207. */
  208. #define NCHUNKS 16
  209. #define CHUNK0SIZE 1024
  210. struct chunk {
  211. size_t n_left;
  212. void *m;
  213. };
  214. /* Code generator state */
  215. struct _compiler_state {
  216. jmp_buf top_ctx;
  217. pcap_t *bpf_pcap;
  218. struct icode ic;
  219. int snaplen;
  220. int linktype;
  221. int prevlinktype;
  222. int outermostlinktype;
  223. bpf_u_int32 netmask;
  224. int no_optimize;
  225. /* Hack for handling VLAN and MPLS stacks. */
  226. u_int label_stack_depth;
  227. u_int vlan_stack_depth;
  228. /* XXX */
  229. u_int pcap_fddipad;
  230. /*
  231. * As errors are handled by a longjmp, anything allocated must
  232. * be freed in the longjmp handler, so it must be reachable
  233. * from that handler.
  234. *
  235. * One thing that's allocated is the result of pcap_nametoaddrinfo();
  236. * it must be freed with freeaddrinfo(). This variable points to
  237. * any addrinfo structure that would need to be freed.
  238. */
  239. struct addrinfo *ai;
  240. /*
  241. * Various code constructs need to know the layout of the packet.
  242. * These values give the necessary offsets from the beginning
  243. * of the packet data.
  244. */
  245. /*
  246. * Absolute offset of the beginning of the link-layer header.
  247. */
  248. bpf_abs_offset off_linkhdr;
  249. /*
  250. * If we're checking a link-layer header for a packet encapsulated
  251. * in another protocol layer, this is the equivalent information
  252. * for the previous layers' link-layer header from the beginning
  253. * of the raw packet data.
  254. */
  255. bpf_abs_offset off_prevlinkhdr;
  256. /*
  257. * This is the equivalent information for the outermost layers'
  258. * link-layer header.
  259. */
  260. bpf_abs_offset off_outermostlinkhdr;
  261. /*
  262. * Absolute offset of the beginning of the link-layer payload.
  263. */
  264. bpf_abs_offset off_linkpl;
  265. /*
  266. * "off_linktype" is the offset to information in the link-layer
  267. * header giving the packet type. This is an absolute offset
  268. * from the beginning of the packet.
  269. *
  270. * For Ethernet, it's the offset of the Ethernet type field; this
  271. * means that it must have a value that skips VLAN tags.
  272. *
  273. * For link-layer types that always use 802.2 headers, it's the
  274. * offset of the LLC header; this means that it must have a value
  275. * that skips VLAN tags.
  276. *
  277. * For PPP, it's the offset of the PPP type field.
  278. *
  279. * For Cisco HDLC, it's the offset of the CHDLC type field.
  280. *
  281. * For BSD loopback, it's the offset of the AF_ value.
  282. *
  283. * For Linux cooked sockets, it's the offset of the type field.
  284. *
  285. * off_linktype.constant_part is set to OFFSET_NOT_SET for no
  286. * encapsulation, in which case, IP is assumed.
  287. */
  288. bpf_abs_offset off_linktype;
  289. /*
  290. * TRUE if the link layer includes an ATM pseudo-header.
  291. */
  292. int is_atm;
  293. /*
  294. * TRUE if "geneve" appeared in the filter; it causes us to
  295. * generate code that checks for a Geneve header and assume
  296. * that later filters apply to the encapsulated payload.
  297. */
  298. int is_geneve;
  299. /*
  300. * TRUE if we need variable length part of VLAN offset
  301. */
  302. int is_vlan_vloffset;
  303. /*
  304. * These are offsets for the ATM pseudo-header.
  305. */
  306. u_int off_vpi;
  307. u_int off_vci;
  308. u_int off_proto;
  309. /*
  310. * These are offsets for the MTP2 fields.
  311. */
  312. u_int off_li;
  313. u_int off_li_hsl;
  314. /*
  315. * These are offsets for the MTP3 fields.
  316. */
  317. u_int off_sio;
  318. u_int off_opc;
  319. u_int off_dpc;
  320. u_int off_sls;
  321. /*
  322. * This is the offset of the first byte after the ATM pseudo_header,
  323. * or -1 if there is no ATM pseudo-header.
  324. */
  325. u_int off_payload;
  326. /*
  327. * These are offsets to the beginning of the network-layer header.
  328. * They are relative to the beginning of the link-layer payload
  329. * (i.e., they don't include off_linkhdr.constant_part or
  330. * off_linkpl.constant_part).
  331. *
  332. * If the link layer never uses 802.2 LLC:
  333. *
  334. * "off_nl" and "off_nl_nosnap" are the same.
  335. *
  336. * If the link layer always uses 802.2 LLC:
  337. *
  338. * "off_nl" is the offset if there's a SNAP header following
  339. * the 802.2 header;
  340. *
  341. * "off_nl_nosnap" is the offset if there's no SNAP header.
  342. *
  343. * If the link layer is Ethernet:
  344. *
  345. * "off_nl" is the offset if the packet is an Ethernet II packet
  346. * (we assume no 802.3+802.2+SNAP);
  347. *
  348. * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
  349. * with an 802.2 header following it.
  350. */
  351. u_int off_nl;
  352. u_int off_nl_nosnap;
  353. /*
  354. * Here we handle simple allocation of the scratch registers.
  355. * If too many registers are alloc'd, the allocator punts.
  356. */
  357. int regused[BPF_MEMWORDS];
  358. int curreg;
  359. /*
  360. * Memory chunks.
  361. */
  362. struct chunk chunks[NCHUNKS];
  363. int cur_chunk;
  364. };
  365. void PCAP_NORETURN
  366. bpf_syntax_error(compiler_state_t *cstate, const char *msg)
  367. {
  368. bpf_error(cstate, "syntax error in filter expression: %s", msg);
  369. /* NOTREACHED */
  370. }
  371. /* VARARGS */
  372. void PCAP_NORETURN
  373. bpf_error(compiler_state_t *cstate, const char *fmt, ...)
  374. {
  375. va_list ap;
  376. va_start(ap, fmt);
  377. if (cstate->bpf_pcap != NULL)
  378. (void)pcap_vsnprintf(pcap_geterr(cstate->bpf_pcap),
  379. PCAP_ERRBUF_SIZE, fmt, ap);
  380. va_end(ap);
  381. longjmp(cstate->top_ctx, 1);
  382. /* NOTREACHED */
  383. }
  384. static void init_linktype(compiler_state_t *, pcap_t *);
  385. static void init_regs(compiler_state_t *);
  386. static int alloc_reg(compiler_state_t *);
  387. static void free_reg(compiler_state_t *, int);
  388. static void initchunks(compiler_state_t *cstate);
  389. static void *newchunk(compiler_state_t *cstate, size_t);
  390. static void freechunks(compiler_state_t *cstate);
  391. static inline struct block *new_block(compiler_state_t *cstate, int);
  392. static inline struct slist *new_stmt(compiler_state_t *cstate, int);
  393. static struct block *gen_retblk(compiler_state_t *cstate, int);
  394. static inline void syntax(compiler_state_t *cstate);
  395. static void backpatch(struct block *, struct block *);
  396. static void merge(struct block *, struct block *);
  397. static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int,
  398. u_int, bpf_int32);
  399. static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int,
  400. u_int, bpf_int32);
  401. static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int,
  402. u_int, bpf_int32);
  403. static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int,
  404. u_int, bpf_int32);
  405. static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int,
  406. u_int, bpf_int32);
  407. static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int,
  408. u_int, bpf_int32, bpf_u_int32);
  409. static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int,
  410. u_int, const u_char *);
  411. static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, bpf_u_int32,
  412. bpf_u_int32, bpf_u_int32, bpf_u_int32, int, bpf_int32);
  413. static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *,
  414. u_int, u_int);
  415. static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int,
  416. u_int);
  417. static struct slist *gen_loadx_iphdrlen(compiler_state_t *);
  418. static struct block *gen_uncond(compiler_state_t *, int);
  419. static inline struct block *gen_true(compiler_state_t *);
  420. static inline struct block *gen_false(compiler_state_t *);
  421. static struct block *gen_ether_linktype(compiler_state_t *, int);
  422. static struct block *gen_ipnet_linktype(compiler_state_t *, int);
  423. static struct block *gen_linux_sll_linktype(compiler_state_t *, int);
  424. static struct slist *gen_load_prism_llprefixlen(compiler_state_t *);
  425. static struct slist *gen_load_avs_llprefixlen(compiler_state_t *);
  426. static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *);
  427. static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *);
  428. static void insert_compute_vloffsets(compiler_state_t *, struct block *);
  429. static struct slist *gen_abs_offset_varpart(compiler_state_t *,
  430. bpf_abs_offset *);
  431. static int ethertype_to_ppptype(int);
  432. static struct block *gen_linktype(compiler_state_t *, int);
  433. static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32);
  434. static struct block *gen_llc_linktype(compiler_state_t *, int);
  435. static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32,
  436. int, int, u_int, u_int);
  437. #ifdef INET6
  438. static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *,
  439. struct in6_addr *, int, int, u_int, u_int);
  440. #endif
  441. static struct block *gen_ahostop(compiler_state_t *, const u_char *, int);
  442. static struct block *gen_ehostop(compiler_state_t *, const u_char *, int);
  443. static struct block *gen_fhostop(compiler_state_t *, const u_char *, int);
  444. static struct block *gen_thostop(compiler_state_t *, const u_char *, int);
  445. static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int);
  446. static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int);
  447. static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int);
  448. static struct block *gen_mpls_linktype(compiler_state_t *, int);
  449. static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32,
  450. int, int, int);
  451. #ifdef INET6
  452. static struct block *gen_host6(compiler_state_t *, struct in6_addr *,
  453. struct in6_addr *, int, int, int);
  454. #endif
  455. #ifndef INET6
  456. static struct block *gen_gateway(compiler_state_t *, const u_char *,
  457. struct addrinfo *, int, int);
  458. #endif
  459. static struct block *gen_ipfrag(compiler_state_t *);
  460. static struct block *gen_portatom(compiler_state_t *, int, bpf_int32);
  461. static struct block *gen_portrangeatom(compiler_state_t *, int, bpf_int32,
  462. bpf_int32);
  463. static struct block *gen_portatom6(compiler_state_t *, int, bpf_int32);
  464. static struct block *gen_portrangeatom6(compiler_state_t *, int, bpf_int32,
  465. bpf_int32);
  466. struct block *gen_portop(compiler_state_t *, int, int, int);
  467. static struct block *gen_port(compiler_state_t *, int, int, int);
  468. struct block *gen_portrangeop(compiler_state_t *, int, int, int, int);
  469. static struct block *gen_portrange(compiler_state_t *, int, int, int, int);
  470. struct block *gen_portop6(compiler_state_t *, int, int, int);
  471. static struct block *gen_port6(compiler_state_t *, int, int, int);
  472. struct block *gen_portrangeop6(compiler_state_t *, int, int, int, int);
  473. static struct block *gen_portrange6(compiler_state_t *, int, int, int, int);
  474. static int lookup_proto(compiler_state_t *, const char *, int);
  475. static struct block *gen_protochain(compiler_state_t *, int, int, int);
  476. static struct block *gen_proto(compiler_state_t *, int, int, int);
  477. static struct slist *xfer_to_x(compiler_state_t *, struct arth *);
  478. static struct slist *xfer_to_a(compiler_state_t *, struct arth *);
  479. static struct block *gen_mac_multicast(compiler_state_t *, int);
  480. static struct block *gen_len(compiler_state_t *, int, int);
  481. static struct block *gen_check_802_11_data_frame(compiler_state_t *);
  482. static struct block *gen_geneve_ll_check(compiler_state_t *cstate);
  483. static struct block *gen_ppi_dlt_check(compiler_state_t *);
  484. static struct block *gen_msg_abbrev(compiler_state_t *, int type);
  485. static void
  486. initchunks(compiler_state_t *cstate)
  487. {
  488. int i;
  489. for (i = 0; i < NCHUNKS; i++) {
  490. cstate->chunks[i].n_left = 0;
  491. cstate->chunks[i].m = NULL;
  492. }
  493. cstate->cur_chunk = 0;
  494. }
  495. static void *
  496. newchunk(compiler_state_t *cstate, size_t n)
  497. {
  498. struct chunk *cp;
  499. int k;
  500. size_t size;
  501. #ifndef __NetBSD__
  502. /* XXX Round up to nearest long. */
  503. n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
  504. #else
  505. /* XXX Round up to structure boundary. */
  506. n = ALIGN(n);
  507. #endif
  508. cp = &cstate->chunks[cstate->cur_chunk];
  509. if (n > cp->n_left) {
  510. ++cp;
  511. k = ++cstate->cur_chunk;
  512. if (k >= NCHUNKS)
  513. bpf_error(cstate, "out of memory");
  514. size = CHUNK0SIZE << k;
  515. cp->m = (void *)malloc(size);
  516. if (cp->m == NULL)
  517. bpf_error(cstate, "out of memory");
  518. memset((char *)cp->m, 0, size);
  519. cp->n_left = size;
  520. if (n > size)
  521. bpf_error(cstate, "out of memory");
  522. }
  523. cp->n_left -= n;
  524. return (void *)((char *)cp->m + cp->n_left);
  525. }
  526. static void
  527. freechunks(compiler_state_t *cstate)
  528. {
  529. int i;
  530. for (i = 0; i < NCHUNKS; ++i)
  531. if (cstate->chunks[i].m != NULL)
  532. free(cstate->chunks[i].m);
  533. }
  534. /*
  535. * A strdup whose allocations are freed after code generation is over.
  536. */
  537. char *
  538. sdup(compiler_state_t *cstate, const char *s)
  539. {
  540. size_t n = strlen(s) + 1;
  541. char *cp = newchunk(cstate, n);
  542. strlcpy(cp, s, n);
  543. return (cp);
  544. }
  545. static inline struct block *
  546. new_block(compiler_state_t *cstate, int code)
  547. {
  548. struct block *p;
  549. p = (struct block *)newchunk(cstate, sizeof(*p));
  550. p->s.code = code;
  551. p->head = p;
  552. return p;
  553. }
  554. static inline struct slist *
  555. new_stmt(compiler_state_t *cstate, int code)
  556. {
  557. struct slist *p;
  558. p = (struct slist *)newchunk(cstate, sizeof(*p));
  559. p->s.code = code;
  560. return p;
  561. }
  562. static struct block *
  563. gen_retblk(compiler_state_t *cstate, int v)
  564. {
  565. struct block *b = new_block(cstate, BPF_RET|BPF_K);
  566. b->s.k = v;
  567. return b;
  568. }
  569. static inline PCAP_NORETURN_DEF void
  570. syntax(compiler_state_t *cstate)
  571. {
  572. bpf_error(cstate, "syntax error in filter expression");
  573. }
  574. int
  575. pcap_compile(pcap_t *p, struct bpf_program *program,
  576. const char *buf, int optimize, bpf_u_int32 mask)
  577. {
  578. #ifdef _WIN32
  579. static int done = 0;
  580. #endif
  581. compiler_state_t cstate;
  582. const char * volatile xbuf = buf;
  583. yyscan_t scanner = NULL;
  584. YY_BUFFER_STATE in_buffer = NULL;
  585. u_int len;
  586. int rc;
  587. /*
  588. * If this pcap_t hasn't been activated, it doesn't have a
  589. * link-layer type, so we can't use it.
  590. */
  591. if (!p->activated) {
  592. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  593. "not-yet-activated pcap_t passed to pcap_compile");
  594. return (-1);
  595. }
  596. #ifdef _WIN32
  597. if (!done)
  598. pcap_wsockinit();
  599. done = 1;
  600. #endif
  601. #ifdef ENABLE_REMOTE
  602. /*
  603. * If the device on which we're capturing need to be notified
  604. * that a new filter is being compiled, do so.
  605. *
  606. * This allows them to save a copy of it, in case, for example,
  607. * they're implementing a form of remote packet capture, and
  608. * want the remote machine to filter out the packets in which
  609. * it's sending the packets it's captured.
  610. *
  611. * XXX - the fact that we happen to be compiling a filter
  612. * doesn't necessarily mean we'll be installing it as the
  613. * filter for this pcap_t; we might be running it from userland
  614. * on captured packets to do packet classification. We really
  615. * need a better way of handling this, but this is all that
  616. * the WinPcap code did.
  617. */
  618. if (p->save_current_filter_op != NULL)
  619. (p->save_current_filter_op)(p, buf);
  620. #endif
  621. initchunks(&cstate);
  622. cstate.no_optimize = 0;
  623. #ifdef INET6
  624. cstate.ai = NULL;
  625. #endif
  626. cstate.ic.root = NULL;
  627. cstate.ic.cur_mark = 0;
  628. cstate.bpf_pcap = p;
  629. init_regs(&cstate);
  630. if (setjmp(cstate.top_ctx)) {
  631. #ifdef INET6
  632. if (cstate.ai != NULL)
  633. freeaddrinfo(cstate.ai);
  634. #endif
  635. rc = -1;
  636. goto quit;
  637. }
  638. cstate.netmask = mask;
  639. cstate.snaplen = pcap_snapshot(p);
  640. if (cstate.snaplen == 0) {
  641. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  642. "snaplen of 0 rejects all packets");
  643. rc = -1;
  644. goto quit;
  645. }
  646. if (pcap_lex_init(&scanner) != 0)
  647. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  648. errno, "can't initialize scanner");
  649. in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner);
  650. /*
  651. * Associate the compiler state with the lexical analyzer
  652. * state.
  653. */
  654. pcap_set_extra(&cstate, scanner);
  655. init_linktype(&cstate, p);
  656. (void)pcap_parse(scanner, &cstate);
  657. if (cstate.ic.root == NULL)
  658. cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);
  659. if (optimize && !cstate.no_optimize) {
  660. bpf_optimize(&cstate, &cstate.ic);
  661. if (cstate.ic.root == NULL ||
  662. (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0))
  663. bpf_error(&cstate, "expression rejects all packets");
  664. }
  665. program->bf_insns = icode_to_fcode(&cstate, &cstate.ic, cstate.ic.root, &len);
  666. program->bf_len = len;
  667. rc = 0; /* We're all okay */
  668. quit:
  669. /*
  670. * Clean up everything for the lexical analyzer.
  671. */
  672. if (in_buffer != NULL)
  673. pcap__delete_buffer(in_buffer, scanner);
  674. if (scanner != NULL)
  675. pcap_lex_destroy(scanner);
  676. /*
  677. * Clean up our own allocated memory.
  678. */
  679. freechunks(&cstate);
  680. return (rc);
  681. }
  682. /*
  683. * entry point for using the compiler with no pcap open
  684. * pass in all the stuff that is needed explicitly instead.
  685. */
  686. int
  687. pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
  688. struct bpf_program *program,
  689. const char *buf, int optimize, bpf_u_int32 mask)
  690. {
  691. pcap_t *p;
  692. int ret;
  693. p = pcap_open_dead(linktype_arg, snaplen_arg);
  694. if (p == NULL)
  695. return (-1);
  696. ret = pcap_compile(p, program, buf, optimize, mask);
  697. pcap_close(p);
  698. return (ret);
  699. }
  700. /*
  701. * Clean up a "struct bpf_program" by freeing all the memory allocated
  702. * in it.
  703. */
  704. void
  705. pcap_freecode(struct bpf_program *program)
  706. {
  707. program->bf_len = 0;
  708. if (program->bf_insns != NULL) {
  709. free((char *)program->bf_insns);
  710. program->bf_insns = NULL;
  711. }
  712. }
  713. /*
  714. * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
  715. * which of the jt and jf fields has been resolved and which is a pointer
  716. * back to another unresolved block (or nil). At least one of the fields
  717. * in each block is already resolved.
  718. */
  719. static void
  720. backpatch(struct block *list, struct block *target)
  721. {
  722. struct block *next;
  723. while (list) {
  724. if (!list->sense) {
  725. next = JT(list);
  726. JT(list) = target;
  727. } else {
  728. next = JF(list);
  729. JF(list) = target;
  730. }
  731. list = next;
  732. }
  733. }
  734. /*
  735. * Merge the lists in b0 and b1, using the 'sense' field to indicate
  736. * which of jt and jf is the link.
  737. */
  738. static void
  739. merge(struct block *b0, struct block *b1)
  740. {
  741. register struct block **p = &b0;
  742. /* Find end of list. */
  743. while (*p)
  744. p = !((*p)->sense) ? &JT(*p) : &JF(*p);
  745. /* Concatenate the lists. */
  746. *p = b1;
  747. }
  748. void
  749. finish_parse(compiler_state_t *cstate, struct block *p)
  750. {
  751. struct block *ppi_dlt_check;
  752. /*
  753. * Insert before the statements of the first (root) block any
  754. * statements needed to load the lengths of any variable-length
  755. * headers into registers.
  756. *
  757. * XXX - a fancier strategy would be to insert those before the
  758. * statements of all blocks that use those lengths and that
  759. * have no predecessors that use them, so that we only compute
  760. * the lengths if we need them. There might be even better
  761. * approaches than that.
  762. *
  763. * However, those strategies would be more complicated, and
  764. * as we don't generate code to compute a length if the
  765. * program has no tests that use the length, and as most
  766. * tests will probably use those lengths, we would just
  767. * postpone computing the lengths so that it's not done
  768. * for tests that fail early, and it's not clear that's
  769. * worth the effort.
  770. */
  771. insert_compute_vloffsets(cstate, p->head);
  772. /*
  773. * For DLT_PPI captures, generate a check of the per-packet
  774. * DLT value to make sure it's DLT_IEEE802_11.
  775. *
  776. * XXX - TurboCap cards use DLT_PPI for Ethernet.
  777. * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
  778. * with appropriate Ethernet information and use that rather
  779. * than using something such as DLT_PPI where you don't know
  780. * the link-layer header type until runtime, which, in the
  781. * general case, would force us to generate both Ethernet *and*
  782. * 802.11 code (*and* anything else for which PPI is used)
  783. * and choose between them early in the BPF program?
  784. */
  785. ppi_dlt_check = gen_ppi_dlt_check(cstate);
  786. if (ppi_dlt_check != NULL)
  787. gen_and(ppi_dlt_check, p);
  788. backpatch(p, gen_retblk(cstate, cstate->snaplen));
  789. p->sense = !p->sense;
  790. backpatch(p, gen_retblk(cstate, 0));
  791. cstate->ic.root = p->head;
  792. }
  793. void
  794. gen_and(struct block *b0, struct block *b1)
  795. {
  796. backpatch(b0, b1->head);
  797. b0->sense = !b0->sense;
  798. b1->sense = !b1->sense;
  799. merge(b1, b0);
  800. b1->sense = !b1->sense;
  801. b1->head = b0->head;
  802. }
  803. void
  804. gen_or(struct block *b0, struct block *b1)
  805. {
  806. b0->sense = !b0->sense;
  807. backpatch(b0, b1->head);
  808. b0->sense = !b0->sense;
  809. merge(b1, b0);
  810. b1->head = b0->head;
  811. }
  812. void
  813. gen_not(struct block *b)
  814. {
  815. b->sense = !b->sense;
  816. }
  817. static struct block *
  818. gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  819. u_int size, bpf_int32 v)
  820. {
  821. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
  822. }
  823. static struct block *
  824. gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  825. u_int size, bpf_int32 v)
  826. {
  827. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
  828. }
  829. static struct block *
  830. gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  831. u_int size, bpf_int32 v)
  832. {
  833. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
  834. }
  835. static struct block *
  836. gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  837. u_int size, bpf_int32 v)
  838. {
  839. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
  840. }
  841. static struct block *
  842. gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  843. u_int size, bpf_int32 v)
  844. {
  845. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
  846. }
  847. static struct block *
  848. gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  849. u_int size, bpf_int32 v, bpf_u_int32 mask)
  850. {
  851. return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
  852. }
  853. static struct block *
  854. gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  855. u_int size, const u_char *v)
  856. {
  857. register struct block *b, *tmp;
  858. b = NULL;
  859. while (size >= 4) {
  860. register const u_char *p = &v[size - 4];
  861. bpf_int32 w = ((bpf_int32)p[0] << 24) |
  862. ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
  863. tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, w);
  864. if (b != NULL)
  865. gen_and(b, tmp);
  866. b = tmp;
  867. size -= 4;
  868. }
  869. while (size >= 2) {
  870. register const u_char *p = &v[size - 2];
  871. bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
  872. tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, w);
  873. if (b != NULL)
  874. gen_and(b, tmp);
  875. b = tmp;
  876. size -= 2;
  877. }
  878. if (size > 0) {
  879. tmp = gen_cmp(cstate, offrel, offset, BPF_B, (bpf_int32)v[0]);
  880. if (b != NULL)
  881. gen_and(b, tmp);
  882. b = tmp;
  883. }
  884. return b;
  885. }
  886. /*
  887. * AND the field of size "size" at offset "offset" relative to the header
  888. * specified by "offrel" with "mask", and compare it with the value "v"
  889. * with the test specified by "jtype"; if "reverse" is true, the test
  890. * should test the opposite of "jtype".
  891. */
  892. static struct block *
  893. gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, bpf_u_int32 offset,
  894. bpf_u_int32 size, bpf_u_int32 mask, bpf_u_int32 jtype, int reverse,
  895. bpf_int32 v)
  896. {
  897. struct slist *s, *s2;
  898. struct block *b;
  899. s = gen_load_a(cstate, offrel, offset, size);
  900. if (mask != 0xffffffff) {
  901. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  902. s2->s.k = mask;
  903. sappend(s, s2);
  904. }
  905. b = new_block(cstate, JMP(jtype));
  906. b->stmts = s;
  907. b->s.k = v;
  908. if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
  909. gen_not(b);
  910. return b;
  911. }
  912. static void
  913. init_linktype(compiler_state_t *cstate, pcap_t *p)
  914. {
  915. cstate->pcap_fddipad = p->fddipad;
  916. /*
  917. * We start out with only one link-layer header.
  918. */
  919. cstate->outermostlinktype = pcap_datalink(p);
  920. cstate->off_outermostlinkhdr.constant_part = 0;
  921. cstate->off_outermostlinkhdr.is_variable = 0;
  922. cstate->off_outermostlinkhdr.reg = -1;
  923. cstate->prevlinktype = cstate->outermostlinktype;
  924. cstate->off_prevlinkhdr.constant_part = 0;
  925. cstate->off_prevlinkhdr.is_variable = 0;
  926. cstate->off_prevlinkhdr.reg = -1;
  927. cstate->linktype = cstate->outermostlinktype;
  928. cstate->off_linkhdr.constant_part = 0;
  929. cstate->off_linkhdr.is_variable = 0;
  930. cstate->off_linkhdr.reg = -1;
  931. /*
  932. * XXX
  933. */
  934. cstate->off_linkpl.constant_part = 0;
  935. cstate->off_linkpl.is_variable = 0;
  936. cstate->off_linkpl.reg = -1;
  937. cstate->off_linktype.constant_part = 0;
  938. cstate->off_linktype.is_variable = 0;
  939. cstate->off_linktype.reg = -1;
  940. /*
  941. * Assume it's not raw ATM with a pseudo-header, for now.
  942. */
  943. cstate->is_atm = 0;
  944. cstate->off_vpi = OFFSET_NOT_SET;
  945. cstate->off_vci = OFFSET_NOT_SET;
  946. cstate->off_proto = OFFSET_NOT_SET;
  947. cstate->off_payload = OFFSET_NOT_SET;
  948. /*
  949. * And not Geneve.
  950. */
  951. cstate->is_geneve = 0;
  952. /*
  953. * No variable length VLAN offset by default
  954. */
  955. cstate->is_vlan_vloffset = 0;
  956. /*
  957. * And assume we're not doing SS7.
  958. */
  959. cstate->off_li = OFFSET_NOT_SET;
  960. cstate->off_li_hsl = OFFSET_NOT_SET;
  961. cstate->off_sio = OFFSET_NOT_SET;
  962. cstate->off_opc = OFFSET_NOT_SET;
  963. cstate->off_dpc = OFFSET_NOT_SET;
  964. cstate->off_sls = OFFSET_NOT_SET;
  965. cstate->label_stack_depth = 0;
  966. cstate->vlan_stack_depth = 0;
  967. switch (cstate->linktype) {
  968. case DLT_ARCNET:
  969. cstate->off_linktype.constant_part = 2;
  970. cstate->off_linkpl.constant_part = 6;
  971. cstate->off_nl = 0; /* XXX in reality, variable! */
  972. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  973. break;
  974. case DLT_ARCNET_LINUX:
  975. cstate->off_linktype.constant_part = 4;
  976. cstate->off_linkpl.constant_part = 8;
  977. cstate->off_nl = 0; /* XXX in reality, variable! */
  978. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  979. break;
  980. case DLT_EN10MB:
  981. cstate->off_linktype.constant_part = 12;
  982. cstate->off_linkpl.constant_part = 14; /* Ethernet header length */
  983. cstate->off_nl = 0; /* Ethernet II */
  984. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  985. break;
  986. case DLT_SLIP:
  987. /*
  988. * SLIP doesn't have a link level type. The 16 byte
  989. * header is hacked into our SLIP driver.
  990. */
  991. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  992. cstate->off_linkpl.constant_part = 16;
  993. cstate->off_nl = 0;
  994. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  995. break;
  996. case DLT_SLIP_BSDOS:
  997. /* XXX this may be the same as the DLT_PPP_BSDOS case */
  998. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  999. /* XXX end */
  1000. cstate->off_linkpl.constant_part = 24;
  1001. cstate->off_nl = 0;
  1002. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1003. break;
  1004. case DLT_NULL:
  1005. case DLT_LOOP:
  1006. cstate->off_linktype.constant_part = 0;
  1007. cstate->off_linkpl.constant_part = 4;
  1008. cstate->off_nl = 0;
  1009. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1010. break;
  1011. case DLT_ENC:
  1012. cstate->off_linktype.constant_part = 0;
  1013. cstate->off_linkpl.constant_part = 12;
  1014. cstate->off_nl = 0;
  1015. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1016. break;
  1017. case DLT_PPP:
  1018. case DLT_PPP_PPPD:
  1019. case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
  1020. case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
  1021. cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */
  1022. cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */
  1023. cstate->off_nl = 0;
  1024. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1025. break;
  1026. case DLT_PPP_ETHER:
  1027. /*
  1028. * This does no include the Ethernet header, and
  1029. * only covers session state.
  1030. */
  1031. cstate->off_linktype.constant_part = 6;
  1032. cstate->off_linkpl.constant_part = 8;
  1033. cstate->off_nl = 0;
  1034. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1035. break;
  1036. case DLT_PPP_BSDOS:
  1037. cstate->off_linktype.constant_part = 5;
  1038. cstate->off_linkpl.constant_part = 24;
  1039. cstate->off_nl = 0;
  1040. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1041. break;
  1042. case DLT_FDDI:
  1043. /*
  1044. * FDDI doesn't really have a link-level type field.
  1045. * We set "off_linktype" to the offset of the LLC header.
  1046. *
  1047. * To check for Ethernet types, we assume that SSAP = SNAP
  1048. * is being used and pick out the encapsulated Ethernet type.
  1049. * XXX - should we generate code to check for SNAP?
  1050. */
  1051. cstate->off_linktype.constant_part = 13;
  1052. cstate->off_linktype.constant_part += cstate->pcap_fddipad;
  1053. cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */
  1054. cstate->off_linkpl.constant_part += cstate->pcap_fddipad;
  1055. cstate->off_nl = 8; /* 802.2+SNAP */
  1056. cstate->off_nl_nosnap = 3; /* 802.2 */
  1057. break;
  1058. case DLT_IEEE802:
  1059. /*
  1060. * Token Ring doesn't really have a link-level type field.
  1061. * We set "off_linktype" to the offset of the LLC header.
  1062. *
  1063. * To check for Ethernet types, we assume that SSAP = SNAP
  1064. * is being used and pick out the encapsulated Ethernet type.
  1065. * XXX - should we generate code to check for SNAP?
  1066. *
  1067. * XXX - the header is actually variable-length.
  1068. * Some various Linux patched versions gave 38
  1069. * as "off_linktype" and 40 as "off_nl"; however,
  1070. * if a token ring packet has *no* routing
  1071. * information, i.e. is not source-routed, the correct
  1072. * values are 20 and 22, as they are in the vanilla code.
  1073. *
  1074. * A packet is source-routed iff the uppermost bit
  1075. * of the first byte of the source address, at an
  1076. * offset of 8, has the uppermost bit set. If the
  1077. * packet is source-routed, the total number of bytes
  1078. * of routing information is 2 plus bits 0x1F00 of
  1079. * the 16-bit value at an offset of 14 (shifted right
  1080. * 8 - figure out which byte that is).
  1081. */
  1082. cstate->off_linktype.constant_part = 14;
  1083. cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */
  1084. cstate->off_nl = 8; /* 802.2+SNAP */
  1085. cstate->off_nl_nosnap = 3; /* 802.2 */
  1086. break;
  1087. case DLT_PRISM_HEADER:
  1088. case DLT_IEEE802_11_RADIO_AVS:
  1089. case DLT_IEEE802_11_RADIO:
  1090. cstate->off_linkhdr.is_variable = 1;
  1091. /* Fall through, 802.11 doesn't have a variable link
  1092. * prefix but is otherwise the same. */
  1093. case DLT_IEEE802_11:
  1094. /*
  1095. * 802.11 doesn't really have a link-level type field.
  1096. * We set "off_linktype.constant_part" to the offset of
  1097. * the LLC header.
  1098. *
  1099. * To check for Ethernet types, we assume that SSAP = SNAP
  1100. * is being used and pick out the encapsulated Ethernet type.
  1101. * XXX - should we generate code to check for SNAP?
  1102. *
  1103. * We also handle variable-length radio headers here.
  1104. * The Prism header is in theory variable-length, but in
  1105. * practice it's always 144 bytes long. However, some
  1106. * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
  1107. * sometimes or always supply an AVS header, so we
  1108. * have to check whether the radio header is a Prism
  1109. * header or an AVS header, so, in practice, it's
  1110. * variable-length.
  1111. */
  1112. cstate->off_linktype.constant_part = 24;
  1113. cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
  1114. cstate->off_linkpl.is_variable = 1;
  1115. cstate->off_nl = 8; /* 802.2+SNAP */
  1116. cstate->off_nl_nosnap = 3; /* 802.2 */
  1117. break;
  1118. case DLT_PPI:
  1119. /*
  1120. * At the moment we treat PPI the same way that we treat
  1121. * normal Radiotap encoded packets. The difference is in
  1122. * the function that generates the code at the beginning
  1123. * to compute the header length. Since this code generator
  1124. * of PPI supports bare 802.11 encapsulation only (i.e.
  1125. * the encapsulated DLT should be DLT_IEEE802_11) we
  1126. * generate code to check for this too.
  1127. */
  1128. cstate->off_linktype.constant_part = 24;
  1129. cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
  1130. cstate->off_linkpl.is_variable = 1;
  1131. cstate->off_linkhdr.is_variable = 1;
  1132. cstate->off_nl = 8; /* 802.2+SNAP */
  1133. cstate->off_nl_nosnap = 3; /* 802.2 */
  1134. break;
  1135. case DLT_ATM_RFC1483:
  1136. case DLT_ATM_CLIP: /* Linux ATM defines this */
  1137. /*
  1138. * assume routed, non-ISO PDUs
  1139. * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
  1140. *
  1141. * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
  1142. * or PPP with the PPP NLPID (e.g., PPPoA)? The
  1143. * latter would presumably be treated the way PPPoE
  1144. * should be, so you can do "pppoe and udp port 2049"
  1145. * or "pppoa and tcp port 80" and have it check for
  1146. * PPPo{A,E} and a PPP protocol of IP and....
  1147. */
  1148. cstate->off_linktype.constant_part = 0;
  1149. cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */
  1150. cstate->off_nl = 8; /* 802.2+SNAP */
  1151. cstate->off_nl_nosnap = 3; /* 802.2 */
  1152. break;
  1153. case DLT_SUNATM:
  1154. /*
  1155. * Full Frontal ATM; you get AALn PDUs with an ATM
  1156. * pseudo-header.
  1157. */
  1158. cstate->is_atm = 1;
  1159. cstate->off_vpi = SUNATM_VPI_POS;
  1160. cstate->off_vci = SUNATM_VCI_POS;
  1161. cstate->off_proto = PROTO_POS;
  1162. cstate->off_payload = SUNATM_PKT_BEGIN_POS;
  1163. cstate->off_linktype.constant_part = cstate->off_payload;
  1164. cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */
  1165. cstate->off_nl = 8; /* 802.2+SNAP */
  1166. cstate->off_nl_nosnap = 3; /* 802.2 */
  1167. break;
  1168. case DLT_RAW:
  1169. case DLT_IPV4:
  1170. case DLT_IPV6:
  1171. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1172. cstate->off_linkpl.constant_part = 0;
  1173. cstate->off_nl = 0;
  1174. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1175. break;
  1176. case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
  1177. cstate->off_linktype.constant_part = 14;
  1178. cstate->off_linkpl.constant_part = 16;
  1179. cstate->off_nl = 0;
  1180. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1181. break;
  1182. case DLT_LTALK:
  1183. /*
  1184. * LocalTalk does have a 1-byte type field in the LLAP header,
  1185. * but really it just indicates whether there is a "short" or
  1186. * "long" DDP packet following.
  1187. */
  1188. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1189. cstate->off_linkpl.constant_part = 0;
  1190. cstate->off_nl = 0;
  1191. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1192. break;
  1193. case DLT_IP_OVER_FC:
  1194. /*
  1195. * RFC 2625 IP-over-Fibre-Channel doesn't really have a
  1196. * link-level type field. We set "off_linktype" to the
  1197. * offset of the LLC header.
  1198. *
  1199. * To check for Ethernet types, we assume that SSAP = SNAP
  1200. * is being used and pick out the encapsulated Ethernet type.
  1201. * XXX - should we generate code to check for SNAP? RFC
  1202. * 2625 says SNAP should be used.
  1203. */
  1204. cstate->off_linktype.constant_part = 16;
  1205. cstate->off_linkpl.constant_part = 16;
  1206. cstate->off_nl = 8; /* 802.2+SNAP */
  1207. cstate->off_nl_nosnap = 3; /* 802.2 */
  1208. break;
  1209. case DLT_FRELAY:
  1210. /*
  1211. * XXX - we should set this to handle SNAP-encapsulated
  1212. * frames (NLPID of 0x80).
  1213. */
  1214. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1215. cstate->off_linkpl.constant_part = 0;
  1216. cstate->off_nl = 0;
  1217. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1218. break;
  1219. /*
  1220. * the only BPF-interesting FRF.16 frames are non-control frames;
  1221. * Frame Relay has a variable length link-layer
  1222. * so lets start with offset 4 for now and increments later on (FIXME);
  1223. */
  1224. case DLT_MFR:
  1225. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1226. cstate->off_linkpl.constant_part = 0;
  1227. cstate->off_nl = 4;
  1228. cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
  1229. break;
  1230. case DLT_APPLE_IP_OVER_IEEE1394:
  1231. cstate->off_linktype.constant_part = 16;
  1232. cstate->off_linkpl.constant_part = 18;
  1233. cstate->off_nl = 0;
  1234. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1235. break;
  1236. case DLT_SYMANTEC_FIREWALL:
  1237. cstate->off_linktype.constant_part = 6;
  1238. cstate->off_linkpl.constant_part = 44;
  1239. cstate->off_nl = 0; /* Ethernet II */
  1240. cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
  1241. break;
  1242. #ifdef HAVE_NET_PFVAR_H
  1243. case DLT_PFLOG:
  1244. cstate->off_linktype.constant_part = 0;
  1245. cstate->off_linkpl.constant_part = PFLOG_HDRLEN;
  1246. cstate->off_nl = 0;
  1247. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1248. break;
  1249. #endif
  1250. case DLT_JUNIPER_MFR:
  1251. case DLT_JUNIPER_MLFR:
  1252. case DLT_JUNIPER_MLPPP:
  1253. case DLT_JUNIPER_PPP:
  1254. case DLT_JUNIPER_CHDLC:
  1255. case DLT_JUNIPER_FRELAY:
  1256. cstate->off_linktype.constant_part = 4;
  1257. cstate->off_linkpl.constant_part = 4;
  1258. cstate->off_nl = 0;
  1259. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1260. break;
  1261. case DLT_JUNIPER_ATM1:
  1262. cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */
  1263. cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */
  1264. cstate->off_nl = 0;
  1265. cstate->off_nl_nosnap = 10;
  1266. break;
  1267. case DLT_JUNIPER_ATM2:
  1268. cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */
  1269. cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */
  1270. cstate->off_nl = 0;
  1271. cstate->off_nl_nosnap = 10;
  1272. break;
  1273. /* frames captured on a Juniper PPPoE service PIC
  1274. * contain raw ethernet frames */
  1275. case DLT_JUNIPER_PPPOE:
  1276. case DLT_JUNIPER_ETHER:
  1277. cstate->off_linkpl.constant_part = 14;
  1278. cstate->off_linktype.constant_part = 16;
  1279. cstate->off_nl = 18; /* Ethernet II */
  1280. cstate->off_nl_nosnap = 21; /* 802.3+802.2 */
  1281. break;
  1282. case DLT_JUNIPER_PPPOE_ATM:
  1283. cstate->off_linktype.constant_part = 4;
  1284. cstate->off_linkpl.constant_part = 6;
  1285. cstate->off_nl = 0;
  1286. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1287. break;
  1288. case DLT_JUNIPER_GGSN:
  1289. cstate->off_linktype.constant_part = 6;
  1290. cstate->off_linkpl.constant_part = 12;
  1291. cstate->off_nl = 0;
  1292. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1293. break;
  1294. case DLT_JUNIPER_ES:
  1295. cstate->off_linktype.constant_part = 6;
  1296. cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
  1297. cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
  1298. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1299. break;
  1300. case DLT_JUNIPER_MONITOR:
  1301. cstate->off_linktype.constant_part = 12;
  1302. cstate->off_linkpl.constant_part = 12;
  1303. cstate->off_nl = 0; /* raw IP/IP6 header */
  1304. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1305. break;
  1306. case DLT_BACNET_MS_TP:
  1307. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1308. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1309. cstate->off_nl = OFFSET_NOT_SET;
  1310. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1311. break;
  1312. case DLT_JUNIPER_SERVICES:
  1313. cstate->off_linktype.constant_part = 12;
  1314. cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
  1315. cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
  1316. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1317. break;
  1318. case DLT_JUNIPER_VP:
  1319. cstate->off_linktype.constant_part = 18;
  1320. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1321. cstate->off_nl = OFFSET_NOT_SET;
  1322. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1323. break;
  1324. case DLT_JUNIPER_ST:
  1325. cstate->off_linktype.constant_part = 18;
  1326. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1327. cstate->off_nl = OFFSET_NOT_SET;
  1328. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1329. break;
  1330. case DLT_JUNIPER_ISM:
  1331. cstate->off_linktype.constant_part = 8;
  1332. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1333. cstate->off_nl = OFFSET_NOT_SET;
  1334. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1335. break;
  1336. case DLT_JUNIPER_VS:
  1337. case DLT_JUNIPER_SRX_E2E:
  1338. case DLT_JUNIPER_FIBRECHANNEL:
  1339. case DLT_JUNIPER_ATM_CEMIC:
  1340. cstate->off_linktype.constant_part = 8;
  1341. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1342. cstate->off_nl = OFFSET_NOT_SET;
  1343. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1344. break;
  1345. case DLT_MTP2:
  1346. cstate->off_li = 2;
  1347. cstate->off_li_hsl = 4;
  1348. cstate->off_sio = 3;
  1349. cstate->off_opc = 4;
  1350. cstate->off_dpc = 4;
  1351. cstate->off_sls = 7;
  1352. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1353. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1354. cstate->off_nl = OFFSET_NOT_SET;
  1355. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1356. break;
  1357. case DLT_MTP2_WITH_PHDR:
  1358. cstate->off_li = 6;
  1359. cstate->off_li_hsl = 8;
  1360. cstate->off_sio = 7;
  1361. cstate->off_opc = 8;
  1362. cstate->off_dpc = 8;
  1363. cstate->off_sls = 11;
  1364. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1365. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1366. cstate->off_nl = OFFSET_NOT_SET;
  1367. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1368. break;
  1369. case DLT_ERF:
  1370. cstate->off_li = 22;
  1371. cstate->off_li_hsl = 24;
  1372. cstate->off_sio = 23;
  1373. cstate->off_opc = 24;
  1374. cstate->off_dpc = 24;
  1375. cstate->off_sls = 27;
  1376. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1377. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1378. cstate->off_nl = OFFSET_NOT_SET;
  1379. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1380. break;
  1381. case DLT_PFSYNC:
  1382. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1383. cstate->off_linkpl.constant_part = 4;
  1384. cstate->off_nl = 0;
  1385. cstate->off_nl_nosnap = 0;
  1386. break;
  1387. case DLT_AX25_KISS:
  1388. /*
  1389. * Currently, only raw "link[N:M]" filtering is supported.
  1390. */
  1391. cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */
  1392. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1393. cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */
  1394. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1395. break;
  1396. case DLT_IPNET:
  1397. cstate->off_linktype.constant_part = 1;
  1398. cstate->off_linkpl.constant_part = 24; /* ipnet header length */
  1399. cstate->off_nl = 0;
  1400. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1401. break;
  1402. case DLT_NETANALYZER:
  1403. cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */
  1404. cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
  1405. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */
  1406. cstate->off_nl = 0; /* Ethernet II */
  1407. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  1408. break;
  1409. case DLT_NETANALYZER_TRANSPARENT:
  1410. cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
  1411. cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
  1412. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */
  1413. cstate->off_nl = 0; /* Ethernet II */
  1414. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  1415. break;
  1416. default:
  1417. /*
  1418. * For values in the range in which we've assigned new
  1419. * DLT_ values, only raw "link[N:M]" filtering is supported.
  1420. */
  1421. if (cstate->linktype >= DLT_MATCHING_MIN &&
  1422. cstate->linktype <= DLT_MATCHING_MAX) {
  1423. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1424. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1425. cstate->off_nl = OFFSET_NOT_SET;
  1426. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1427. } else {
  1428. bpf_error(cstate, "unknown data link type %d", cstate->linktype);
  1429. }
  1430. break;
  1431. }
  1432. cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;
  1433. }
  1434. /*
  1435. * Load a value relative to the specified absolute offset.
  1436. */
  1437. static struct slist *
  1438. gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,
  1439. u_int offset, u_int size)
  1440. {
  1441. struct slist *s, *s2;
  1442. s = gen_abs_offset_varpart(cstate, abs_offset);
  1443. /*
  1444. * If "s" is non-null, it has code to arrange that the X register
  1445. * contains the variable part of the absolute offset, so we
  1446. * generate a load relative to that, with an offset of
  1447. * abs_offset->constant_part + offset.
  1448. *
  1449. * Otherwise, we can do an absolute load with an offset of
  1450. * abs_offset->constant_part + offset.
  1451. */
  1452. if (s != NULL) {
  1453. /*
  1454. * "s" points to a list of statements that puts the
  1455. * variable part of the absolute offset into the X register.
  1456. * Do an indirect load, to use the X register as an offset.
  1457. */
  1458. s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
  1459. s2->s.k = abs_offset->constant_part + offset;
  1460. sappend(s, s2);
  1461. } else {
  1462. /*
  1463. * There is no variable part of the absolute offset, so
  1464. * just do an absolute load.
  1465. */
  1466. s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
  1467. s->s.k = abs_offset->constant_part + offset;
  1468. }
  1469. return s;
  1470. }
  1471. /*
  1472. * Load a value relative to the beginning of the specified header.
  1473. */
  1474. static struct slist *
  1475. gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  1476. u_int size)
  1477. {
  1478. struct slist *s, *s2;
  1479. switch (offrel) {
  1480. case OR_PACKET:
  1481. s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
  1482. s->s.k = offset;
  1483. break;
  1484. case OR_LINKHDR:
  1485. s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);
  1486. break;
  1487. case OR_PREVLINKHDR:
  1488. s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);
  1489. break;
  1490. case OR_LLC:
  1491. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);
  1492. break;
  1493. case OR_PREVMPLSHDR:
  1494. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);
  1495. break;
  1496. case OR_LINKPL:
  1497. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);
  1498. break;
  1499. case OR_LINKPL_NOSNAP:
  1500. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);
  1501. break;
  1502. case OR_LINKTYPE:
  1503. s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);
  1504. break;
  1505. case OR_TRAN_IPV4:
  1506. /*
  1507. * Load the X register with the length of the IPv4 header
  1508. * (plus the offset of the link-layer header, if it's
  1509. * preceded by a variable-length header such as a radio
  1510. * header), in bytes.
  1511. */
  1512. s = gen_loadx_iphdrlen(cstate);
  1513. /*
  1514. * Load the item at {offset of the link-layer payload} +
  1515. * {offset, relative to the start of the link-layer
  1516. * paylod, of the IPv4 header} + {length of the IPv4 header} +
  1517. * {specified offset}.
  1518. *
  1519. * If the offset of the link-layer payload is variable,
  1520. * the variable part of that offset is included in the
  1521. * value in the X register, and we include the constant
  1522. * part in the offset of the load.
  1523. */
  1524. s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
  1525. s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;
  1526. sappend(s, s2);
  1527. break;
  1528. case OR_TRAN_IPV6:
  1529. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);
  1530. break;
  1531. default:
  1532. abort();
  1533. /* NOTREACHED */
  1534. }
  1535. return s;
  1536. }
  1537. /*
  1538. * Generate code to load into the X register the sum of the length of
  1539. * the IPv4 header and the variable part of the offset of the link-layer
  1540. * payload.
  1541. */
  1542. static struct slist *
  1543. gen_loadx_iphdrlen(compiler_state_t *cstate)
  1544. {
  1545. struct slist *s, *s2;
  1546. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  1547. if (s != NULL) {
  1548. /*
  1549. * The offset of the link-layer payload has a variable
  1550. * part. "s" points to a list of statements that put
  1551. * the variable part of that offset into the X register.
  1552. *
  1553. * The 4*([k]&0xf) addressing mode can't be used, as we
  1554. * don't have a constant offset, so we have to load the
  1555. * value in question into the A register and add to it
  1556. * the value from the X register.
  1557. */
  1558. s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  1559. s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  1560. sappend(s, s2);
  1561. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  1562. s2->s.k = 0xf;
  1563. sappend(s, s2);
  1564. s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
  1565. s2->s.k = 2;
  1566. sappend(s, s2);
  1567. /*
  1568. * The A register now contains the length of the IP header.
  1569. * We need to add to it the variable part of the offset of
  1570. * the link-layer payload, which is still in the X
  1571. * register, and move the result into the X register.
  1572. */
  1573. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  1574. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  1575. } else {
  1576. /*
  1577. * The offset of the link-layer payload is a constant,
  1578. * so no code was generated to load the (non-existent)
  1579. * variable part of that offset.
  1580. *
  1581. * This means we can use the 4*([k]&0xf) addressing
  1582. * mode. Load the length of the IPv4 header, which
  1583. * is at an offset of cstate->off_nl from the beginning of
  1584. * the link-layer payload, and thus at an offset of
  1585. * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
  1586. * of the raw packet data, using that addressing mode.
  1587. */
  1588. s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
  1589. s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  1590. }
  1591. return s;
  1592. }
  1593. static struct block *
  1594. gen_uncond(compiler_state_t *cstate, int rsense)
  1595. {
  1596. struct block *b;
  1597. struct slist *s;
  1598. s = new_stmt(cstate, BPF_LD|BPF_IMM);
  1599. s->s.k = !rsense;
  1600. b = new_block(cstate, JMP(BPF_JEQ));
  1601. b->stmts = s;
  1602. return b;
  1603. }
  1604. static inline struct block *
  1605. gen_true(compiler_state_t *cstate)
  1606. {
  1607. return gen_uncond(cstate, 1);
  1608. }
  1609. static inline struct block *
  1610. gen_false(compiler_state_t *cstate)
  1611. {
  1612. return gen_uncond(cstate, 0);
  1613. }
  1614. /*
  1615. * Byte-swap a 32-bit number.
  1616. * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
  1617. * big-endian platforms.)
  1618. */
  1619. #define SWAPLONG(y) \
  1620. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  1621. /*
  1622. * Generate code to match a particular packet type.
  1623. *
  1624. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1625. * value, if <= ETHERMTU. We use that to determine whether to
  1626. * match the type/length field or to check the type/length field for
  1627. * a value <= ETHERMTU to see whether it's a type field and then do
  1628. * the appropriate test.
  1629. */
  1630. static struct block *
  1631. gen_ether_linktype(compiler_state_t *cstate, int proto)
  1632. {
  1633. struct block *b0, *b1;
  1634. switch (proto) {
  1635. case LLCSAP_ISONS:
  1636. case LLCSAP_IP:
  1637. case LLCSAP_NETBEUI:
  1638. /*
  1639. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1640. * so we check the DSAP and SSAP.
  1641. *
  1642. * LLCSAP_IP checks for IP-over-802.2, rather
  1643. * than IP-over-Ethernet or IP-over-SNAP.
  1644. *
  1645. * XXX - should we check both the DSAP and the
  1646. * SSAP, like this, or should we check just the
  1647. * DSAP, as we do for other types <= ETHERMTU
  1648. * (i.e., other SAP values)?
  1649. */
  1650. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1651. gen_not(b0);
  1652. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)
  1653. ((proto << 8) | proto));
  1654. gen_and(b0, b1);
  1655. return b1;
  1656. case LLCSAP_IPX:
  1657. /*
  1658. * Check for;
  1659. *
  1660. * Ethernet_II frames, which are Ethernet
  1661. * frames with a frame type of ETHERTYPE_IPX;
  1662. *
  1663. * Ethernet_802.3 frames, which are 802.3
  1664. * frames (i.e., the type/length field is
  1665. * a length field, <= ETHERMTU, rather than
  1666. * a type field) with the first two bytes
  1667. * after the Ethernet/802.3 header being
  1668. * 0xFFFF;
  1669. *
  1670. * Ethernet_802.2 frames, which are 802.3
  1671. * frames with an 802.2 LLC header and
  1672. * with the IPX LSAP as the DSAP in the LLC
  1673. * header;
  1674. *
  1675. * Ethernet_SNAP frames, which are 802.3
  1676. * frames with an LLC header and a SNAP
  1677. * header and with an OUI of 0x000000
  1678. * (encapsulated Ethernet) and a protocol
  1679. * ID of ETHERTYPE_IPX in the SNAP header.
  1680. *
  1681. * XXX - should we generate the same code both
  1682. * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
  1683. */
  1684. /*
  1685. * This generates code to check both for the
  1686. * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
  1687. */
  1688. b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
  1689. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF);
  1690. gen_or(b0, b1);
  1691. /*
  1692. * Now we add code to check for SNAP frames with
  1693. * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
  1694. */
  1695. b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
  1696. gen_or(b0, b1);
  1697. /*
  1698. * Now we generate code to check for 802.3
  1699. * frames in general.
  1700. */
  1701. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1702. gen_not(b0);
  1703. /*
  1704. * Now add the check for 802.3 frames before the
  1705. * check for Ethernet_802.2 and Ethernet_802.3,
  1706. * as those checks should only be done on 802.3
  1707. * frames, not on Ethernet frames.
  1708. */
  1709. gen_and(b0, b1);
  1710. /*
  1711. * Now add the check for Ethernet_II frames, and
  1712. * do that before checking for the other frame
  1713. * types.
  1714. */
  1715. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX);
  1716. gen_or(b0, b1);
  1717. return b1;
  1718. case ETHERTYPE_ATALK:
  1719. case ETHERTYPE_AARP:
  1720. /*
  1721. * EtherTalk (AppleTalk protocols on Ethernet link
  1722. * layer) may use 802.2 encapsulation.
  1723. */
  1724. /*
  1725. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  1726. * we check for an Ethernet type field less than
  1727. * 1500, which means it's an 802.3 length field.
  1728. */
  1729. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1730. gen_not(b0);
  1731. /*
  1732. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  1733. * SNAP packets with an organization code of
  1734. * 0x080007 (Apple, for Appletalk) and a protocol
  1735. * type of ETHERTYPE_ATALK (Appletalk).
  1736. *
  1737. * 802.2-encapsulated ETHERTYPE_AARP packets are
  1738. * SNAP packets with an organization code of
  1739. * 0x000000 (encapsulated Ethernet) and a protocol
  1740. * type of ETHERTYPE_AARP (Appletalk ARP).
  1741. */
  1742. if (proto == ETHERTYPE_ATALK)
  1743. b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
  1744. else /* proto == ETHERTYPE_AARP */
  1745. b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
  1746. gen_and(b0, b1);
  1747. /*
  1748. * Check for Ethernet encapsulation (Ethertalk
  1749. * phase 1?); we just check for the Ethernet
  1750. * protocol type.
  1751. */
  1752. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
  1753. gen_or(b0, b1);
  1754. return b1;
  1755. default:
  1756. if (proto <= ETHERMTU) {
  1757. /*
  1758. * This is an LLC SAP value, so the frames
  1759. * that match would be 802.2 frames.
  1760. * Check that the frame is an 802.2 frame
  1761. * (i.e., that the length/type field is
  1762. * a length field, <= ETHERMTU) and
  1763. * then check the DSAP.
  1764. */
  1765. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1766. gen_not(b0);
  1767. b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, (bpf_int32)proto);
  1768. gen_and(b0, b1);
  1769. return b1;
  1770. } else {
  1771. /*
  1772. * This is an Ethernet type, so compare
  1773. * the length/type field with it (if
  1774. * the frame is an 802.2 frame, the length
  1775. * field will be <= ETHERMTU, and, as
  1776. * "proto" is > ETHERMTU, this test
  1777. * will fail and the frame won't match,
  1778. * which is what we want).
  1779. */
  1780. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
  1781. (bpf_int32)proto);
  1782. }
  1783. }
  1784. }
  1785. static struct block *
  1786. gen_loopback_linktype(compiler_state_t *cstate, int proto)
  1787. {
  1788. /*
  1789. * For DLT_NULL, the link-layer header is a 32-bit word
  1790. * containing an AF_ value in *host* byte order, and for
  1791. * DLT_ENC, the link-layer header begins with a 32-bit
  1792. * word containing an AF_ value in host byte order.
  1793. *
  1794. * In addition, if we're reading a saved capture file,
  1795. * the host byte order in the capture may not be the
  1796. * same as the host byte order on this machine.
  1797. *
  1798. * For DLT_LOOP, the link-layer header is a 32-bit
  1799. * word containing an AF_ value in *network* byte order.
  1800. */
  1801. if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) {
  1802. /*
  1803. * The AF_ value is in host byte order, but the BPF
  1804. * interpreter will convert it to network byte order.
  1805. *
  1806. * If this is a save file, and it's from a machine
  1807. * with the opposite byte order to ours, we byte-swap
  1808. * the AF_ value.
  1809. *
  1810. * Then we run it through "htonl()", and generate
  1811. * code to compare against the result.
  1812. */
  1813. if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)
  1814. proto = SWAPLONG(proto);
  1815. proto = htonl(proto);
  1816. }
  1817. return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, (bpf_int32)proto));
  1818. }
  1819. /*
  1820. * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
  1821. * or IPv6 then we have an error.
  1822. */
  1823. static struct block *
  1824. gen_ipnet_linktype(compiler_state_t *cstate, int proto)
  1825. {
  1826. switch (proto) {
  1827. case ETHERTYPE_IP:
  1828. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, (bpf_int32)IPH_AF_INET);
  1829. /* NOTREACHED */
  1830. case ETHERTYPE_IPV6:
  1831. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  1832. (bpf_int32)IPH_AF_INET6);
  1833. /* NOTREACHED */
  1834. default:
  1835. break;
  1836. }
  1837. return gen_false(cstate);
  1838. }
  1839. /*
  1840. * Generate code to match a particular packet type.
  1841. *
  1842. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1843. * value, if <= ETHERMTU. We use that to determine whether to
  1844. * match the type field or to check the type field for the special
  1845. * LINUX_SLL_P_802_2 value and then do the appropriate test.
  1846. */
  1847. static struct block *
  1848. gen_linux_sll_linktype(compiler_state_t *cstate, int proto)
  1849. {
  1850. struct block *b0, *b1;
  1851. switch (proto) {
  1852. case LLCSAP_ISONS:
  1853. case LLCSAP_IP:
  1854. case LLCSAP_NETBEUI:
  1855. /*
  1856. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1857. * so we check the DSAP and SSAP.
  1858. *
  1859. * LLCSAP_IP checks for IP-over-802.2, rather
  1860. * than IP-over-Ethernet or IP-over-SNAP.
  1861. *
  1862. * XXX - should we check both the DSAP and the
  1863. * SSAP, like this, or should we check just the
  1864. * DSAP, as we do for other types <= ETHERMTU
  1865. * (i.e., other SAP values)?
  1866. */
  1867. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  1868. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)
  1869. ((proto << 8) | proto));
  1870. gen_and(b0, b1);
  1871. return b1;
  1872. case LLCSAP_IPX:
  1873. /*
  1874. * Ethernet_II frames, which are Ethernet
  1875. * frames with a frame type of ETHERTYPE_IPX;
  1876. *
  1877. * Ethernet_802.3 frames, which have a frame
  1878. * type of LINUX_SLL_P_802_3;
  1879. *
  1880. * Ethernet_802.2 frames, which are 802.3
  1881. * frames with an 802.2 LLC header (i.e, have
  1882. * a frame type of LINUX_SLL_P_802_2) and
  1883. * with the IPX LSAP as the DSAP in the LLC
  1884. * header;
  1885. *
  1886. * Ethernet_SNAP frames, which are 802.3
  1887. * frames with an LLC header and a SNAP
  1888. * header and with an OUI of 0x000000
  1889. * (encapsulated Ethernet) and a protocol
  1890. * ID of ETHERTYPE_IPX in the SNAP header.
  1891. *
  1892. * First, do the checks on LINUX_SLL_P_802_2
  1893. * frames; generate the check for either
  1894. * Ethernet_802.2 or Ethernet_SNAP frames, and
  1895. * then put a check for LINUX_SLL_P_802_2 frames
  1896. * before it.
  1897. */
  1898. b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
  1899. b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
  1900. gen_or(b0, b1);
  1901. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  1902. gen_and(b0, b1);
  1903. /*
  1904. * Now check for 802.3 frames and OR that with
  1905. * the previous test.
  1906. */
  1907. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
  1908. gen_or(b0, b1);
  1909. /*
  1910. * Now add the check for Ethernet_II frames, and
  1911. * do that before checking for the other frame
  1912. * types.
  1913. */
  1914. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX);
  1915. gen_or(b0, b1);
  1916. return b1;
  1917. case ETHERTYPE_ATALK:
  1918. case ETHERTYPE_AARP:
  1919. /*
  1920. * EtherTalk (AppleTalk protocols on Ethernet link
  1921. * layer) may use 802.2 encapsulation.
  1922. */
  1923. /*
  1924. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  1925. * we check for the 802.2 protocol type in the
  1926. * "Ethernet type" field.
  1927. */
  1928. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  1929. /*
  1930. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  1931. * SNAP packets with an organization code of
  1932. * 0x080007 (Apple, for Appletalk) and a protocol
  1933. * type of ETHERTYPE_ATALK (Appletalk).
  1934. *
  1935. * 802.2-encapsulated ETHERTYPE_AARP packets are
  1936. * SNAP packets with an organization code of
  1937. * 0x000000 (encapsulated Ethernet) and a protocol
  1938. * type of ETHERTYPE_AARP (Appletalk ARP).
  1939. */
  1940. if (proto == ETHERTYPE_ATALK)
  1941. b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
  1942. else /* proto == ETHERTYPE_AARP */
  1943. b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
  1944. gen_and(b0, b1);
  1945. /*
  1946. * Check for Ethernet encapsulation (Ethertalk
  1947. * phase 1?); we just check for the Ethernet
  1948. * protocol type.
  1949. */
  1950. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
  1951. gen_or(b0, b1);
  1952. return b1;
  1953. default:
  1954. if (proto <= ETHERMTU) {
  1955. /*
  1956. * This is an LLC SAP value, so the frames
  1957. * that match would be 802.2 frames.
  1958. * Check for the 802.2 protocol type
  1959. * in the "Ethernet type" field, and
  1960. * then check the DSAP.
  1961. */
  1962. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  1963. b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,
  1964. (bpf_int32)proto);
  1965. gen_and(b0, b1);
  1966. return b1;
  1967. } else {
  1968. /*
  1969. * This is an Ethernet type, so compare
  1970. * the length/type field with it (if
  1971. * the frame is an 802.2 frame, the length
  1972. * field will be <= ETHERMTU, and, as
  1973. * "proto" is > ETHERMTU, this test
  1974. * will fail and the frame won't match,
  1975. * which is what we want).
  1976. */
  1977. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
  1978. }
  1979. }
  1980. }
  1981. static struct slist *
  1982. gen_load_prism_llprefixlen(compiler_state_t *cstate)
  1983. {
  1984. struct slist *s1, *s2;
  1985. struct slist *sjeq_avs_cookie;
  1986. struct slist *sjcommon;
  1987. /*
  1988. * This code is not compatible with the optimizer, as
  1989. * we are generating jmp instructions within a normal
  1990. * slist of instructions
  1991. */
  1992. cstate->no_optimize = 1;
  1993. /*
  1994. * Generate code to load the length of the radio header into
  1995. * the register assigned to hold that length, if one has been
  1996. * assigned. (If one hasn't been assigned, no code we've
  1997. * generated uses that prefix, so we don't need to generate any
  1998. * code to load it.)
  1999. *
  2000. * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
  2001. * or always use the AVS header rather than the Prism header.
  2002. * We load a 4-byte big-endian value at the beginning of the
  2003. * raw packet data, and see whether, when masked with 0xFFFFF000,
  2004. * it's equal to 0x80211000. If so, that indicates that it's
  2005. * an AVS header (the masked-out bits are the version number).
  2006. * Otherwise, it's a Prism header.
  2007. *
  2008. * XXX - the Prism header is also, in theory, variable-length,
  2009. * but no known software generates headers that aren't 144
  2010. * bytes long.
  2011. */
  2012. if (cstate->off_linkhdr.reg != -1) {
  2013. /*
  2014. * Load the cookie.
  2015. */
  2016. s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2017. s1->s.k = 0;
  2018. /*
  2019. * AND it with 0xFFFFF000.
  2020. */
  2021. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  2022. s2->s.k = 0xFFFFF000;
  2023. sappend(s1, s2);
  2024. /*
  2025. * Compare with 0x80211000.
  2026. */
  2027. sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));
  2028. sjeq_avs_cookie->s.k = 0x80211000;
  2029. sappend(s1, sjeq_avs_cookie);
  2030. /*
  2031. * If it's AVS:
  2032. *
  2033. * The 4 bytes at an offset of 4 from the beginning of
  2034. * the AVS header are the length of the AVS header.
  2035. * That field is big-endian.
  2036. */
  2037. s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2038. s2->s.k = 4;
  2039. sappend(s1, s2);
  2040. sjeq_avs_cookie->s.jt = s2;
  2041. /*
  2042. * Now jump to the code to allocate a register
  2043. * into which to save the header length and
  2044. * store the length there. (The "jump always"
  2045. * instruction needs to have the k field set;
  2046. * it's added to the PC, so, as we're jumping
  2047. * over a single instruction, it should be 1.)
  2048. */
  2049. sjcommon = new_stmt(cstate, JMP(BPF_JA));
  2050. sjcommon->s.k = 1;
  2051. sappend(s1, sjcommon);
  2052. /*
  2053. * Now for the code that handles the Prism header.
  2054. * Just load the length of the Prism header (144)
  2055. * into the A register. Have the test for an AVS
  2056. * header branch here if we don't have an AVS header.
  2057. */
  2058. s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
  2059. s2->s.k = 144;
  2060. sappend(s1, s2);
  2061. sjeq_avs_cookie->s.jf = s2;
  2062. /*
  2063. * Now allocate a register to hold that value and store
  2064. * it. The code for the AVS header will jump here after
  2065. * loading the length of the AVS header.
  2066. */
  2067. s2 = new_stmt(cstate, BPF_ST);
  2068. s2->s.k = cstate->off_linkhdr.reg;
  2069. sappend(s1, s2);
  2070. sjcommon->s.jf = s2;
  2071. /*
  2072. * Now move it into the X register.
  2073. */
  2074. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2075. sappend(s1, s2);
  2076. return (s1);
  2077. } else
  2078. return (NULL);
  2079. }
  2080. static struct slist *
  2081. gen_load_avs_llprefixlen(compiler_state_t *cstate)
  2082. {
  2083. struct slist *s1, *s2;
  2084. /*
  2085. * Generate code to load the length of the AVS header into
  2086. * the register assigned to hold that length, if one has been
  2087. * assigned. (If one hasn't been assigned, no code we've
  2088. * generated uses that prefix, so we don't need to generate any
  2089. * code to load it.)
  2090. */
  2091. if (cstate->off_linkhdr.reg != -1) {
  2092. /*
  2093. * The 4 bytes at an offset of 4 from the beginning of
  2094. * the AVS header are the length of the AVS header.
  2095. * That field is big-endian.
  2096. */
  2097. s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2098. s1->s.k = 4;
  2099. /*
  2100. * Now allocate a register to hold that value and store
  2101. * it.
  2102. */
  2103. s2 = new_stmt(cstate, BPF_ST);
  2104. s2->s.k = cstate->off_linkhdr.reg;
  2105. sappend(s1, s2);
  2106. /*
  2107. * Now move it into the X register.
  2108. */
  2109. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2110. sappend(s1, s2);
  2111. return (s1);
  2112. } else
  2113. return (NULL);
  2114. }
  2115. static struct slist *
  2116. gen_load_radiotap_llprefixlen(compiler_state_t *cstate)
  2117. {
  2118. struct slist *s1, *s2;
  2119. /*
  2120. * Generate code to load the length of the radiotap header into
  2121. * the register assigned to hold that length, if one has been
  2122. * assigned. (If one hasn't been assigned, no code we've
  2123. * generated uses that prefix, so we don't need to generate any
  2124. * code to load it.)
  2125. */
  2126. if (cstate->off_linkhdr.reg != -1) {
  2127. /*
  2128. * The 2 bytes at offsets of 2 and 3 from the beginning
  2129. * of the radiotap header are the length of the radiotap
  2130. * header; unfortunately, it's little-endian, so we have
  2131. * to load it a byte at a time and construct the value.
  2132. */
  2133. /*
  2134. * Load the high-order byte, at an offset of 3, shift it
  2135. * left a byte, and put the result in the X register.
  2136. */
  2137. s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2138. s1->s.k = 3;
  2139. s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
  2140. sappend(s1, s2);
  2141. s2->s.k = 8;
  2142. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2143. sappend(s1, s2);
  2144. /*
  2145. * Load the next byte, at an offset of 2, and OR the
  2146. * value from the X register into it.
  2147. */
  2148. s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2149. sappend(s1, s2);
  2150. s2->s.k = 2;
  2151. s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
  2152. sappend(s1, s2);
  2153. /*
  2154. * Now allocate a register to hold that value and store
  2155. * it.
  2156. */
  2157. s2 = new_stmt(cstate, BPF_ST);
  2158. s2->s.k = cstate->off_linkhdr.reg;
  2159. sappend(s1, s2);
  2160. /*
  2161. * Now move it into the X register.
  2162. */
  2163. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2164. sappend(s1, s2);
  2165. return (s1);
  2166. } else
  2167. return (NULL);
  2168. }
  2169. /*
  2170. * At the moment we treat PPI as normal Radiotap encoded
  2171. * packets. The difference is in the function that generates
  2172. * the code at the beginning to compute the header length.
  2173. * Since this code generator of PPI supports bare 802.11
  2174. * encapsulation only (i.e. the encapsulated DLT should be
  2175. * DLT_IEEE802_11) we generate code to check for this too;
  2176. * that's done in finish_parse().
  2177. */
  2178. static struct slist *
  2179. gen_load_ppi_llprefixlen(compiler_state_t *cstate)
  2180. {
  2181. struct slist *s1, *s2;
  2182. /*
  2183. * Generate code to load the length of the radiotap header
  2184. * into the register assigned to hold that length, if one has
  2185. * been assigned.
  2186. */
  2187. if (cstate->off_linkhdr.reg != -1) {
  2188. /*
  2189. * The 2 bytes at offsets of 2 and 3 from the beginning
  2190. * of the radiotap header are the length of the radiotap
  2191. * header; unfortunately, it's little-endian, so we have
  2192. * to load it a byte at a time and construct the value.
  2193. */
  2194. /*
  2195. * Load the high-order byte, at an offset of 3, shift it
  2196. * left a byte, and put the result in the X register.
  2197. */
  2198. s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2199. s1->s.k = 3;
  2200. s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
  2201. sappend(s1, s2);
  2202. s2->s.k = 8;
  2203. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2204. sappend(s1, s2);
  2205. /*
  2206. * Load the next byte, at an offset of 2, and OR the
  2207. * value from the X register into it.
  2208. */
  2209. s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2210. sappend(s1, s2);
  2211. s2->s.k = 2;
  2212. s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
  2213. sappend(s1, s2);
  2214. /*
  2215. * Now allocate a register to hold that value and store
  2216. * it.
  2217. */
  2218. s2 = new_stmt(cstate, BPF_ST);
  2219. s2->s.k = cstate->off_linkhdr.reg;
  2220. sappend(s1, s2);
  2221. /*
  2222. * Now move it into the X register.
  2223. */
  2224. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2225. sappend(s1, s2);
  2226. return (s1);
  2227. } else
  2228. return (NULL);
  2229. }
  2230. /*
  2231. * Load a value relative to the beginning of the link-layer header after the 802.11
  2232. * header, i.e. LLC_SNAP.
  2233. * The link-layer header doesn't necessarily begin at the beginning
  2234. * of the packet data; there might be a variable-length prefix containing
  2235. * radio information.
  2236. */
  2237. static struct slist *
  2238. gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)
  2239. {
  2240. struct slist *s2;
  2241. struct slist *sjset_data_frame_1;
  2242. struct slist *sjset_data_frame_2;
  2243. struct slist *sjset_qos;
  2244. struct slist *sjset_radiotap_flags_present;
  2245. struct slist *sjset_radiotap_ext_present;
  2246. struct slist *sjset_radiotap_tsft_present;
  2247. struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
  2248. struct slist *s_roundup;
  2249. if (cstate->off_linkpl.reg == -1) {
  2250. /*
  2251. * No register has been assigned to the offset of
  2252. * the link-layer payload, which means nobody needs
  2253. * it; don't bother computing it - just return
  2254. * what we already have.
  2255. */
  2256. return (s);
  2257. }
  2258. /*
  2259. * This code is not compatible with the optimizer, as
  2260. * we are generating jmp instructions within a normal
  2261. * slist of instructions
  2262. */
  2263. cstate->no_optimize = 1;
  2264. /*
  2265. * If "s" is non-null, it has code to arrange that the X register
  2266. * contains the length of the prefix preceding the link-layer
  2267. * header.
  2268. *
  2269. * Otherwise, the length of the prefix preceding the link-layer
  2270. * header is "off_outermostlinkhdr.constant_part".
  2271. */
  2272. if (s == NULL) {
  2273. /*
  2274. * There is no variable-length header preceding the
  2275. * link-layer header.
  2276. *
  2277. * Load the length of the fixed-length prefix preceding
  2278. * the link-layer header (if any) into the X register,
  2279. * and store it in the cstate->off_linkpl.reg register.
  2280. * That length is off_outermostlinkhdr.constant_part.
  2281. */
  2282. s = new_stmt(cstate, BPF_LDX|BPF_IMM);
  2283. s->s.k = cstate->off_outermostlinkhdr.constant_part;
  2284. }
  2285. /*
  2286. * The X register contains the offset of the beginning of the
  2287. * link-layer header; add 24, which is the minimum length
  2288. * of the MAC header for a data frame, to that, and store it
  2289. * in cstate->off_linkpl.reg, and then load the Frame Control field,
  2290. * which is at the offset in the X register, with an indexed load.
  2291. */
  2292. s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);
  2293. sappend(s, s2);
  2294. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  2295. s2->s.k = 24;
  2296. sappend(s, s2);
  2297. s2 = new_stmt(cstate, BPF_ST);
  2298. s2->s.k = cstate->off_linkpl.reg;
  2299. sappend(s, s2);
  2300. s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  2301. s2->s.k = 0;
  2302. sappend(s, s2);
  2303. /*
  2304. * Check the Frame Control field to see if this is a data frame;
  2305. * a data frame has the 0x08 bit (b3) in that field set and the
  2306. * 0x04 bit (b2) clear.
  2307. */
  2308. sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));
  2309. sjset_data_frame_1->s.k = 0x08;
  2310. sappend(s, sjset_data_frame_1);
  2311. /*
  2312. * If b3 is set, test b2, otherwise go to the first statement of
  2313. * the rest of the program.
  2314. */
  2315. sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));
  2316. sjset_data_frame_2->s.k = 0x04;
  2317. sappend(s, sjset_data_frame_2);
  2318. sjset_data_frame_1->s.jf = snext;
  2319. /*
  2320. * If b2 is not set, this is a data frame; test the QoS bit.
  2321. * Otherwise, go to the first statement of the rest of the
  2322. * program.
  2323. */
  2324. sjset_data_frame_2->s.jt = snext;
  2325. sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));
  2326. sjset_qos->s.k = 0x80; /* QoS bit */
  2327. sappend(s, sjset_qos);
  2328. /*
  2329. * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
  2330. * field.
  2331. * Otherwise, go to the first statement of the rest of the
  2332. * program.
  2333. */
  2334. sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
  2335. s2->s.k = cstate->off_linkpl.reg;
  2336. sappend(s, s2);
  2337. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
  2338. s2->s.k = 2;
  2339. sappend(s, s2);
  2340. s2 = new_stmt(cstate, BPF_ST);
  2341. s2->s.k = cstate->off_linkpl.reg;
  2342. sappend(s, s2);
  2343. /*
  2344. * If we have a radiotap header, look at it to see whether
  2345. * there's Atheros padding between the MAC-layer header
  2346. * and the payload.
  2347. *
  2348. * Note: all of the fields in the radiotap header are
  2349. * little-endian, so we byte-swap all of the values
  2350. * we test against, as they will be loaded as big-endian
  2351. * values.
  2352. *
  2353. * XXX - in the general case, we would have to scan through
  2354. * *all* the presence bits, if there's more than one word of
  2355. * presence bits. That would require a loop, meaning that
  2356. * we wouldn't be able to run the filter in the kernel.
  2357. *
  2358. * We assume here that the Atheros adapters that insert the
  2359. * annoying padding don't have multiple antennae and therefore
  2360. * do not generate radiotap headers with multiple presence words.
  2361. */
  2362. if (cstate->linktype == DLT_IEEE802_11_RADIO) {
  2363. /*
  2364. * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
  2365. * in the first presence flag word?
  2366. */
  2367. sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);
  2368. s2->s.k = 4;
  2369. sappend(s, s2);
  2370. sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));
  2371. sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002);
  2372. sappend(s, sjset_radiotap_flags_present);
  2373. /*
  2374. * If not, skip all of this.
  2375. */
  2376. sjset_radiotap_flags_present->s.jf = snext;
  2377. /*
  2378. * Otherwise, is the "extension" bit set in that word?
  2379. */
  2380. sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));
  2381. sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000);
  2382. sappend(s, sjset_radiotap_ext_present);
  2383. sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present;
  2384. /*
  2385. * If so, skip all of this.
  2386. */
  2387. sjset_radiotap_ext_present->s.jt = snext;
  2388. /*
  2389. * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
  2390. */
  2391. sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));
  2392. sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001);
  2393. sappend(s, sjset_radiotap_tsft_present);
  2394. sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present;
  2395. /*
  2396. * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
  2397. * at an offset of 16 from the beginning of the raw packet
  2398. * data (8 bytes for the radiotap header and 8 bytes for
  2399. * the TSFT field).
  2400. *
  2401. * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
  2402. * is set.
  2403. */
  2404. s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  2405. s2->s.k = 16;
  2406. sappend(s, s2);
  2407. sjset_radiotap_tsft_present->s.jt = s2;
  2408. sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
  2409. sjset_tsft_datapad->s.k = 0x20;
  2410. sappend(s, sjset_tsft_datapad);
  2411. /*
  2412. * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
  2413. * at an offset of 8 from the beginning of the raw packet
  2414. * data (8 bytes for the radiotap header).
  2415. *
  2416. * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
  2417. * is set.
  2418. */
  2419. s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  2420. s2->s.k = 8;
  2421. sappend(s, s2);
  2422. sjset_radiotap_tsft_present->s.jf = s2;
  2423. sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
  2424. sjset_notsft_datapad->s.k = 0x20;
  2425. sappend(s, sjset_notsft_datapad);
  2426. /*
  2427. * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
  2428. * set, round the length of the 802.11 header to
  2429. * a multiple of 4. Do that by adding 3 and then
  2430. * dividing by and multiplying by 4, which we do by
  2431. * ANDing with ~3.
  2432. */
  2433. s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);
  2434. s_roundup->s.k = cstate->off_linkpl.reg;
  2435. sappend(s, s_roundup);
  2436. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
  2437. s2->s.k = 3;
  2438. sappend(s, s2);
  2439. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);
  2440. s2->s.k = ~3;
  2441. sappend(s, s2);
  2442. s2 = new_stmt(cstate, BPF_ST);
  2443. s2->s.k = cstate->off_linkpl.reg;
  2444. sappend(s, s2);
  2445. sjset_tsft_datapad->s.jt = s_roundup;
  2446. sjset_tsft_datapad->s.jf = snext;
  2447. sjset_notsft_datapad->s.jt = s_roundup;
  2448. sjset_notsft_datapad->s.jf = snext;
  2449. } else
  2450. sjset_qos->s.jf = snext;
  2451. return s;
  2452. }
  2453. static void
  2454. insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)
  2455. {
  2456. struct slist *s;
  2457. /* There is an implicit dependency between the link
  2458. * payload and link header since the payload computation
  2459. * includes the variable part of the header. Therefore,
  2460. * if nobody else has allocated a register for the link
  2461. * header and we need it, do it now. */
  2462. if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&
  2463. cstate->off_linkhdr.reg == -1)
  2464. cstate->off_linkhdr.reg = alloc_reg(cstate);
  2465. /*
  2466. * For link-layer types that have a variable-length header
  2467. * preceding the link-layer header, generate code to load
  2468. * the offset of the link-layer header into the register
  2469. * assigned to that offset, if any.
  2470. *
  2471. * XXX - this, and the next switch statement, won't handle
  2472. * encapsulation of 802.11 or 802.11+radio information in
  2473. * some other protocol stack. That's significantly more
  2474. * complicated.
  2475. */
  2476. switch (cstate->outermostlinktype) {
  2477. case DLT_PRISM_HEADER:
  2478. s = gen_load_prism_llprefixlen(cstate);
  2479. break;
  2480. case DLT_IEEE802_11_RADIO_AVS:
  2481. s = gen_load_avs_llprefixlen(cstate);
  2482. break;
  2483. case DLT_IEEE802_11_RADIO:
  2484. s = gen_load_radiotap_llprefixlen(cstate);
  2485. break;
  2486. case DLT_PPI:
  2487. s = gen_load_ppi_llprefixlen(cstate);
  2488. break;
  2489. default:
  2490. s = NULL;
  2491. break;
  2492. }
  2493. /*
  2494. * For link-layer types that have a variable-length link-layer
  2495. * header, generate code to load the offset of the link-layer
  2496. * payload into the register assigned to that offset, if any.
  2497. */
  2498. switch (cstate->outermostlinktype) {
  2499. case DLT_IEEE802_11:
  2500. case DLT_PRISM_HEADER:
  2501. case DLT_IEEE802_11_RADIO_AVS:
  2502. case DLT_IEEE802_11_RADIO:
  2503. case DLT_PPI:
  2504. s = gen_load_802_11_header_len(cstate, s, b->stmts);
  2505. break;
  2506. }
  2507. /*
  2508. * If there there is no initialization yet and we need variable
  2509. * length offsets for VLAN, initialize them to zero
  2510. */
  2511. if (s == NULL && cstate->is_vlan_vloffset) {
  2512. struct slist *s2;
  2513. if (cstate->off_linkpl.reg == -1)
  2514. cstate->off_linkpl.reg = alloc_reg(cstate);
  2515. if (cstate->off_linktype.reg == -1)
  2516. cstate->off_linktype.reg = alloc_reg(cstate);
  2517. s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
  2518. s->s.k = 0;
  2519. s2 = new_stmt(cstate, BPF_ST);
  2520. s2->s.k = cstate->off_linkpl.reg;
  2521. sappend(s, s2);
  2522. s2 = new_stmt(cstate, BPF_ST);
  2523. s2->s.k = cstate->off_linktype.reg;
  2524. sappend(s, s2);
  2525. }
  2526. /*
  2527. * If we have any offset-loading code, append all the
  2528. * existing statements in the block to those statements,
  2529. * and make the resulting list the list of statements
  2530. * for the block.
  2531. */
  2532. if (s != NULL) {
  2533. sappend(s, b->stmts);
  2534. b->stmts = s;
  2535. }
  2536. }
  2537. static struct block *
  2538. gen_ppi_dlt_check(compiler_state_t *cstate)
  2539. {
  2540. struct slist *s_load_dlt;
  2541. struct block *b;
  2542. if (cstate->linktype == DLT_PPI)
  2543. {
  2544. /* Create the statements that check for the DLT
  2545. */
  2546. s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2547. s_load_dlt->s.k = 4;
  2548. b = new_block(cstate, JMP(BPF_JEQ));
  2549. b->stmts = s_load_dlt;
  2550. b->s.k = SWAPLONG(DLT_IEEE802_11);
  2551. }
  2552. else
  2553. {
  2554. b = NULL;
  2555. }
  2556. return b;
  2557. }
  2558. /*
  2559. * Take an absolute offset, and:
  2560. *
  2561. * if it has no variable part, return NULL;
  2562. *
  2563. * if it has a variable part, generate code to load the register
  2564. * containing that variable part into the X register, returning
  2565. * a pointer to that code - if no register for that offset has
  2566. * been allocated, allocate it first.
  2567. *
  2568. * (The code to set that register will be generated later, but will
  2569. * be placed earlier in the code sequence.)
  2570. */
  2571. static struct slist *
  2572. gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)
  2573. {
  2574. struct slist *s;
  2575. if (off->is_variable) {
  2576. if (off->reg == -1) {
  2577. /*
  2578. * We haven't yet assigned a register for the
  2579. * variable part of the offset of the link-layer
  2580. * header; allocate one.
  2581. */
  2582. off->reg = alloc_reg(cstate);
  2583. }
  2584. /*
  2585. * Load the register containing the variable part of the
  2586. * offset of the link-layer header into the X register.
  2587. */
  2588. s = new_stmt(cstate, BPF_LDX|BPF_MEM);
  2589. s->s.k = off->reg;
  2590. return s;
  2591. } else {
  2592. /*
  2593. * That offset isn't variable, there's no variable part,
  2594. * so we don't need to generate any code.
  2595. */
  2596. return NULL;
  2597. }
  2598. }
  2599. /*
  2600. * Map an Ethernet type to the equivalent PPP type.
  2601. */
  2602. static int
  2603. ethertype_to_ppptype(int proto)
  2604. {
  2605. switch (proto) {
  2606. case ETHERTYPE_IP:
  2607. proto = PPP_IP;
  2608. break;
  2609. case ETHERTYPE_IPV6:
  2610. proto = PPP_IPV6;
  2611. break;
  2612. case ETHERTYPE_DN:
  2613. proto = PPP_DECNET;
  2614. break;
  2615. case ETHERTYPE_ATALK:
  2616. proto = PPP_APPLE;
  2617. break;
  2618. case ETHERTYPE_NS:
  2619. proto = PPP_NS;
  2620. break;
  2621. case LLCSAP_ISONS:
  2622. proto = PPP_OSI;
  2623. break;
  2624. case LLCSAP_8021D:
  2625. /*
  2626. * I'm assuming the "Bridging PDU"s that go
  2627. * over PPP are Spanning Tree Protocol
  2628. * Bridging PDUs.
  2629. */
  2630. proto = PPP_BRPDU;
  2631. break;
  2632. case LLCSAP_IPX:
  2633. proto = PPP_IPX;
  2634. break;
  2635. }
  2636. return (proto);
  2637. }
  2638. /*
  2639. * Generate any tests that, for encapsulation of a link-layer packet
  2640. * inside another protocol stack, need to be done to check for those
  2641. * link-layer packets (and that haven't already been done by a check
  2642. * for that encapsulation).
  2643. */
  2644. static struct block *
  2645. gen_prevlinkhdr_check(compiler_state_t *cstate)
  2646. {
  2647. struct block *b0;
  2648. if (cstate->is_geneve)
  2649. return gen_geneve_ll_check(cstate);
  2650. switch (cstate->prevlinktype) {
  2651. case DLT_SUNATM:
  2652. /*
  2653. * This is LANE-encapsulated Ethernet; check that the LANE
  2654. * packet doesn't begin with an LE Control marker, i.e.
  2655. * that it's data, not a control message.
  2656. *
  2657. * (We've already generated a test for LANE.)
  2658. */
  2659. b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
  2660. gen_not(b0);
  2661. return b0;
  2662. default:
  2663. /*
  2664. * No such tests are necessary.
  2665. */
  2666. return NULL;
  2667. }
  2668. /*NOTREACHED*/
  2669. }
  2670. /*
  2671. * The three different values we should check for when checking for an
  2672. * IPv6 packet with DLT_NULL.
  2673. */
  2674. #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
  2675. #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
  2676. #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
  2677. /*
  2678. * Generate code to match a particular packet type by matching the
  2679. * link-layer type field or fields in the 802.2 LLC header.
  2680. *
  2681. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  2682. * value, if <= ETHERMTU.
  2683. */
  2684. static struct block *
  2685. gen_linktype(compiler_state_t *cstate, int proto)
  2686. {
  2687. struct block *b0, *b1, *b2;
  2688. const char *description;
  2689. /* are we checking MPLS-encapsulated packets? */
  2690. if (cstate->label_stack_depth > 0) {
  2691. switch (proto) {
  2692. case ETHERTYPE_IP:
  2693. case PPP_IP:
  2694. /* FIXME add other L3 proto IDs */
  2695. return gen_mpls_linktype(cstate, Q_IP);
  2696. case ETHERTYPE_IPV6:
  2697. case PPP_IPV6:
  2698. /* FIXME add other L3 proto IDs */
  2699. return gen_mpls_linktype(cstate, Q_IPV6);
  2700. default:
  2701. bpf_error(cstate, "unsupported protocol over mpls");
  2702. /* NOTREACHED */
  2703. }
  2704. }
  2705. switch (cstate->linktype) {
  2706. case DLT_EN10MB:
  2707. case DLT_NETANALYZER:
  2708. case DLT_NETANALYZER_TRANSPARENT:
  2709. /* Geneve has an EtherType regardless of whether there is an
  2710. * L2 header. */
  2711. if (!cstate->is_geneve)
  2712. b0 = gen_prevlinkhdr_check(cstate);
  2713. else
  2714. b0 = NULL;
  2715. b1 = gen_ether_linktype(cstate, proto);
  2716. if (b0 != NULL)
  2717. gen_and(b0, b1);
  2718. return b1;
  2719. /*NOTREACHED*/
  2720. break;
  2721. case DLT_C_HDLC:
  2722. switch (proto) {
  2723. case LLCSAP_ISONS:
  2724. proto = (proto << 8 | LLCSAP_ISONS);
  2725. /* fall through */
  2726. default:
  2727. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
  2728. /*NOTREACHED*/
  2729. break;
  2730. }
  2731. break;
  2732. case DLT_IEEE802_11:
  2733. case DLT_PRISM_HEADER:
  2734. case DLT_IEEE802_11_RADIO_AVS:
  2735. case DLT_IEEE802_11_RADIO:
  2736. case DLT_PPI:
  2737. /*
  2738. * Check that we have a data frame.
  2739. */
  2740. b0 = gen_check_802_11_data_frame(cstate);
  2741. /*
  2742. * Now check for the specified link-layer type.
  2743. */
  2744. b1 = gen_llc_linktype(cstate, proto);
  2745. gen_and(b0, b1);
  2746. return b1;
  2747. /*NOTREACHED*/
  2748. break;
  2749. case DLT_FDDI:
  2750. /*
  2751. * XXX - check for LLC frames.
  2752. */
  2753. return gen_llc_linktype(cstate, proto);
  2754. /*NOTREACHED*/
  2755. break;
  2756. case DLT_IEEE802:
  2757. /*
  2758. * XXX - check for LLC PDUs, as per IEEE 802.5.
  2759. */
  2760. return gen_llc_linktype(cstate, proto);
  2761. /*NOTREACHED*/
  2762. break;
  2763. case DLT_ATM_RFC1483:
  2764. case DLT_ATM_CLIP:
  2765. case DLT_IP_OVER_FC:
  2766. return gen_llc_linktype(cstate, proto);
  2767. /*NOTREACHED*/
  2768. break;
  2769. case DLT_SUNATM:
  2770. /*
  2771. * Check for an LLC-encapsulated version of this protocol;
  2772. * if we were checking for LANE, linktype would no longer
  2773. * be DLT_SUNATM.
  2774. *
  2775. * Check for LLC encapsulation and then check the protocol.
  2776. */
  2777. b0 = gen_atmfield_code(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
  2778. b1 = gen_llc_linktype(cstate, proto);
  2779. gen_and(b0, b1);
  2780. return b1;
  2781. /*NOTREACHED*/
  2782. break;
  2783. case DLT_LINUX_SLL:
  2784. return gen_linux_sll_linktype(cstate, proto);
  2785. /*NOTREACHED*/
  2786. break;
  2787. case DLT_SLIP:
  2788. case DLT_SLIP_BSDOS:
  2789. case DLT_RAW:
  2790. /*
  2791. * These types don't provide any type field; packets
  2792. * are always IPv4 or IPv6.
  2793. *
  2794. * XXX - for IPv4, check for a version number of 4, and,
  2795. * for IPv6, check for a version number of 6?
  2796. */
  2797. switch (proto) {
  2798. case ETHERTYPE_IP:
  2799. /* Check for a version number of 4. */
  2800. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
  2801. case ETHERTYPE_IPV6:
  2802. /* Check for a version number of 6. */
  2803. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
  2804. default:
  2805. return gen_false(cstate); /* always false */
  2806. }
  2807. /*NOTREACHED*/
  2808. break;
  2809. case DLT_IPV4:
  2810. /*
  2811. * Raw IPv4, so no type field.
  2812. */
  2813. if (proto == ETHERTYPE_IP)
  2814. return gen_true(cstate); /* always true */
  2815. /* Checking for something other than IPv4; always false */
  2816. return gen_false(cstate);
  2817. /*NOTREACHED*/
  2818. break;
  2819. case DLT_IPV6:
  2820. /*
  2821. * Raw IPv6, so no type field.
  2822. */
  2823. if (proto == ETHERTYPE_IPV6)
  2824. return gen_true(cstate); /* always true */
  2825. /* Checking for something other than IPv6; always false */
  2826. return gen_false(cstate);
  2827. /*NOTREACHED*/
  2828. break;
  2829. case DLT_PPP:
  2830. case DLT_PPP_PPPD:
  2831. case DLT_PPP_SERIAL:
  2832. case DLT_PPP_ETHER:
  2833. /*
  2834. * We use Ethernet protocol types inside libpcap;
  2835. * map them to the corresponding PPP protocol types.
  2836. */
  2837. proto = ethertype_to_ppptype(proto);
  2838. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
  2839. /*NOTREACHED*/
  2840. break;
  2841. case DLT_PPP_BSDOS:
  2842. /*
  2843. * We use Ethernet protocol types inside libpcap;
  2844. * map them to the corresponding PPP protocol types.
  2845. */
  2846. switch (proto) {
  2847. case ETHERTYPE_IP:
  2848. /*
  2849. * Also check for Van Jacobson-compressed IP.
  2850. * XXX - do this for other forms of PPP?
  2851. */
  2852. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);
  2853. b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);
  2854. gen_or(b0, b1);
  2855. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
  2856. gen_or(b1, b0);
  2857. return b0;
  2858. default:
  2859. proto = ethertype_to_ppptype(proto);
  2860. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
  2861. (bpf_int32)proto);
  2862. }
  2863. /*NOTREACHED*/
  2864. break;
  2865. case DLT_NULL:
  2866. case DLT_LOOP:
  2867. case DLT_ENC:
  2868. switch (proto) {
  2869. case ETHERTYPE_IP:
  2870. return (gen_loopback_linktype(cstate, AF_INET));
  2871. case ETHERTYPE_IPV6:
  2872. /*
  2873. * AF_ values may, unfortunately, be platform-
  2874. * dependent; AF_INET isn't, because everybody
  2875. * used 4.2BSD's value, but AF_INET6 is, because
  2876. * 4.2BSD didn't have a value for it (given that
  2877. * IPv6 didn't exist back in the early 1980's),
  2878. * and they all picked their own values.
  2879. *
  2880. * This means that, if we're reading from a
  2881. * savefile, we need to check for all the
  2882. * possible values.
  2883. *
  2884. * If we're doing a live capture, we only need
  2885. * to check for this platform's value; however,
  2886. * Npcap uses 24, which isn't Windows's AF_INET6
  2887. * value. (Given the multiple different values,
  2888. * programs that read pcap files shouldn't be
  2889. * checking for their platform's AF_INET6 value
  2890. * anyway, they should check for all of the
  2891. * possible values. and they might as well do
  2892. * that even for live captures.)
  2893. */
  2894. if (cstate->bpf_pcap->rfile != NULL) {
  2895. /*
  2896. * Savefile - check for all three
  2897. * possible IPv6 values.
  2898. */
  2899. b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);
  2900. b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);
  2901. gen_or(b0, b1);
  2902. b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);
  2903. gen_or(b0, b1);
  2904. return (b1);
  2905. } else {
  2906. /*
  2907. * Live capture, so we only need to
  2908. * check for the value used on this
  2909. * platform.
  2910. */
  2911. #ifdef _WIN32
  2912. /*
  2913. * Npcap doesn't use Windows's AF_INET6,
  2914. * as that collides with AF_IPX on
  2915. * some BSDs (both have the value 23).
  2916. * Instead, it uses 24.
  2917. */
  2918. return (gen_loopback_linktype(cstate, 24));
  2919. #else /* _WIN32 */
  2920. #ifdef AF_INET6
  2921. return (gen_loopback_linktype(cstate, AF_INET6));
  2922. #else /* AF_INET6 */
  2923. /*
  2924. * I guess this platform doesn't support
  2925. * IPv6, so we just reject all packets.
  2926. */
  2927. return gen_false(cstate);
  2928. #endif /* AF_INET6 */
  2929. #endif /* _WIN32 */
  2930. }
  2931. default:
  2932. /*
  2933. * Not a type on which we support filtering.
  2934. * XXX - support those that have AF_ values
  2935. * #defined on this platform, at least?
  2936. */
  2937. return gen_false(cstate);
  2938. }
  2939. #ifdef HAVE_NET_PFVAR_H
  2940. case DLT_PFLOG:
  2941. /*
  2942. * af field is host byte order in contrast to the rest of
  2943. * the packet.
  2944. */
  2945. if (proto == ETHERTYPE_IP)
  2946. return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
  2947. BPF_B, (bpf_int32)AF_INET));
  2948. else if (proto == ETHERTYPE_IPV6)
  2949. return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
  2950. BPF_B, (bpf_int32)AF_INET6));
  2951. else
  2952. return gen_false(cstate);
  2953. /*NOTREACHED*/
  2954. break;
  2955. #endif /* HAVE_NET_PFVAR_H */
  2956. case DLT_ARCNET:
  2957. case DLT_ARCNET_LINUX:
  2958. /*
  2959. * XXX should we check for first fragment if the protocol
  2960. * uses PHDS?
  2961. */
  2962. switch (proto) {
  2963. default:
  2964. return gen_false(cstate);
  2965. case ETHERTYPE_IPV6:
  2966. return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2967. (bpf_int32)ARCTYPE_INET6));
  2968. case ETHERTYPE_IP:
  2969. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2970. (bpf_int32)ARCTYPE_IP);
  2971. b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2972. (bpf_int32)ARCTYPE_IP_OLD);
  2973. gen_or(b0, b1);
  2974. return (b1);
  2975. case ETHERTYPE_ARP:
  2976. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2977. (bpf_int32)ARCTYPE_ARP);
  2978. b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2979. (bpf_int32)ARCTYPE_ARP_OLD);
  2980. gen_or(b0, b1);
  2981. return (b1);
  2982. case ETHERTYPE_REVARP:
  2983. return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2984. (bpf_int32)ARCTYPE_REVARP));
  2985. case ETHERTYPE_ATALK:
  2986. return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  2987. (bpf_int32)ARCTYPE_ATALK));
  2988. }
  2989. /*NOTREACHED*/
  2990. break;
  2991. case DLT_LTALK:
  2992. switch (proto) {
  2993. case ETHERTYPE_ATALK:
  2994. return gen_true(cstate);
  2995. default:
  2996. return gen_false(cstate);
  2997. }
  2998. /*NOTREACHED*/
  2999. break;
  3000. case DLT_FRELAY:
  3001. /*
  3002. * XXX - assumes a 2-byte Frame Relay header with
  3003. * DLCI and flags. What if the address is longer?
  3004. */
  3005. switch (proto) {
  3006. case ETHERTYPE_IP:
  3007. /*
  3008. * Check for the special NLPID for IP.
  3009. */
  3010. return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
  3011. case ETHERTYPE_IPV6:
  3012. /*
  3013. * Check for the special NLPID for IPv6.
  3014. */
  3015. return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
  3016. case LLCSAP_ISONS:
  3017. /*
  3018. * Check for several OSI protocols.
  3019. *
  3020. * Frame Relay packets typically have an OSI
  3021. * NLPID at the beginning; we check for each
  3022. * of them.
  3023. *
  3024. * What we check for is the NLPID and a frame
  3025. * control field of UI, i.e. 0x03 followed
  3026. * by the NLPID.
  3027. */
  3028. b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
  3029. b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
  3030. b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
  3031. gen_or(b1, b2);
  3032. gen_or(b0, b2);
  3033. return b2;
  3034. default:
  3035. return gen_false(cstate);
  3036. }
  3037. /*NOTREACHED*/
  3038. break;
  3039. case DLT_MFR:
  3040. bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");
  3041. case DLT_JUNIPER_MFR:
  3042. case DLT_JUNIPER_MLFR:
  3043. case DLT_JUNIPER_MLPPP:
  3044. case DLT_JUNIPER_ATM1:
  3045. case DLT_JUNIPER_ATM2:
  3046. case DLT_JUNIPER_PPPOE:
  3047. case DLT_JUNIPER_PPPOE_ATM:
  3048. case DLT_JUNIPER_GGSN:
  3049. case DLT_JUNIPER_ES:
  3050. case DLT_JUNIPER_MONITOR:
  3051. case DLT_JUNIPER_SERVICES:
  3052. case DLT_JUNIPER_ETHER:
  3053. case DLT_JUNIPER_PPP:
  3054. case DLT_JUNIPER_FRELAY:
  3055. case DLT_JUNIPER_CHDLC:
  3056. case DLT_JUNIPER_VP:
  3057. case DLT_JUNIPER_ST:
  3058. case DLT_JUNIPER_ISM:
  3059. case DLT_JUNIPER_VS:
  3060. case DLT_JUNIPER_SRX_E2E:
  3061. case DLT_JUNIPER_FIBRECHANNEL:
  3062. case DLT_JUNIPER_ATM_CEMIC:
  3063. /* just lets verify the magic number for now -
  3064. * on ATM we may have up to 6 different encapsulations on the wire
  3065. * and need a lot of heuristics to figure out that the payload
  3066. * might be;
  3067. *
  3068. * FIXME encapsulation specific BPF_ filters
  3069. */
  3070. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
  3071. case DLT_BACNET_MS_TP:
  3072. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
  3073. case DLT_IPNET:
  3074. return gen_ipnet_linktype(cstate, proto);
  3075. case DLT_LINUX_IRDA:
  3076. bpf_error(cstate, "IrDA link-layer type filtering not implemented");
  3077. case DLT_DOCSIS:
  3078. bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");
  3079. case DLT_MTP2:
  3080. case DLT_MTP2_WITH_PHDR:
  3081. bpf_error(cstate, "MTP2 link-layer type filtering not implemented");
  3082. case DLT_ERF:
  3083. bpf_error(cstate, "ERF link-layer type filtering not implemented");
  3084. case DLT_PFSYNC:
  3085. bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");
  3086. case DLT_LINUX_LAPD:
  3087. bpf_error(cstate, "LAPD link-layer type filtering not implemented");
  3088. case DLT_USB_FREEBSD:
  3089. case DLT_USB_LINUX:
  3090. case DLT_USB_LINUX_MMAPPED:
  3091. case DLT_USBPCAP:
  3092. bpf_error(cstate, "USB link-layer type filtering not implemented");
  3093. case DLT_BLUETOOTH_HCI_H4:
  3094. case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
  3095. bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
  3096. case DLT_CAN20B:
  3097. case DLT_CAN_SOCKETCAN:
  3098. bpf_error(cstate, "CAN link-layer type filtering not implemented");
  3099. case DLT_IEEE802_15_4:
  3100. case DLT_IEEE802_15_4_LINUX:
  3101. case DLT_IEEE802_15_4_NONASK_PHY:
  3102. case DLT_IEEE802_15_4_NOFCS:
  3103. bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");
  3104. case DLT_IEEE802_16_MAC_CPS_RADIO:
  3105. bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");
  3106. case DLT_SITA:
  3107. bpf_error(cstate, "SITA link-layer type filtering not implemented");
  3108. case DLT_RAIF1:
  3109. bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");
  3110. case DLT_IPMB:
  3111. bpf_error(cstate, "IPMB link-layer type filtering not implemented");
  3112. case DLT_AX25_KISS:
  3113. bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
  3114. case DLT_NFLOG:
  3115. /* Using the fixed-size NFLOG header it is possible to tell only
  3116. * the address family of the packet, other meaningful data is
  3117. * either missing or behind TLVs.
  3118. */
  3119. bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
  3120. default:
  3121. /*
  3122. * Does this link-layer header type have a field
  3123. * indicating the type of the next protocol? If
  3124. * so, off_linktype.constant_part will be the offset of that
  3125. * field in the packet; if not, it will be OFFSET_NOT_SET.
  3126. */
  3127. if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
  3128. /*
  3129. * Yes; assume it's an Ethernet type. (If
  3130. * it's not, it needs to be handled specially
  3131. * above.)
  3132. */
  3133. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
  3134. } else {
  3135. /*
  3136. * No; report an error.
  3137. */
  3138. description = pcap_datalink_val_to_description(cstate->linktype);
  3139. if (description != NULL) {
  3140. bpf_error(cstate, "%s link-layer type filtering not implemented",
  3141. description);
  3142. } else {
  3143. bpf_error(cstate, "DLT %u link-layer type filtering not implemented",
  3144. cstate->linktype);
  3145. }
  3146. }
  3147. break;
  3148. }
  3149. }
  3150. /*
  3151. * Check for an LLC SNAP packet with a given organization code and
  3152. * protocol type; we check the entire contents of the 802.2 LLC and
  3153. * snap headers, checking for DSAP and SSAP of SNAP and a control
  3154. * field of 0x03 in the LLC header, and for the specified organization
  3155. * code and protocol type in the SNAP header.
  3156. */
  3157. static struct block *
  3158. gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
  3159. {
  3160. u_char snapblock[8];
  3161. snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
  3162. snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
  3163. snapblock[2] = 0x03; /* control = UI */
  3164. snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */
  3165. snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */
  3166. snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */
  3167. snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */
  3168. snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */
  3169. return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
  3170. }
  3171. /*
  3172. * Generate code to match frames with an LLC header.
  3173. */
  3174. struct block *
  3175. gen_llc(compiler_state_t *cstate)
  3176. {
  3177. struct block *b0, *b1;
  3178. switch (cstate->linktype) {
  3179. case DLT_EN10MB:
  3180. /*
  3181. * We check for an Ethernet type field less than
  3182. * 1500, which means it's an 802.3 length field.
  3183. */
  3184. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  3185. gen_not(b0);
  3186. /*
  3187. * Now check for the purported DSAP and SSAP not being
  3188. * 0xFF, to rule out NetWare-over-802.3.
  3189. */
  3190. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF);
  3191. gen_not(b1);
  3192. gen_and(b0, b1);
  3193. return b1;
  3194. case DLT_SUNATM:
  3195. /*
  3196. * We check for LLC traffic.
  3197. */
  3198. b0 = gen_atmtype_abbrev(cstate, A_LLC);
  3199. return b0;
  3200. case DLT_IEEE802: /* Token Ring */
  3201. /*
  3202. * XXX - check for LLC frames.
  3203. */
  3204. return gen_true(cstate);
  3205. case DLT_FDDI:
  3206. /*
  3207. * XXX - check for LLC frames.
  3208. */
  3209. return gen_true(cstate);
  3210. case DLT_ATM_RFC1483:
  3211. /*
  3212. * For LLC encapsulation, these are defined to have an
  3213. * 802.2 LLC header.
  3214. *
  3215. * For VC encapsulation, they don't, but there's no
  3216. * way to check for that; the protocol used on the VC
  3217. * is negotiated out of band.
  3218. */
  3219. return gen_true(cstate);
  3220. case DLT_IEEE802_11:
  3221. case DLT_PRISM_HEADER:
  3222. case DLT_IEEE802_11_RADIO:
  3223. case DLT_IEEE802_11_RADIO_AVS:
  3224. case DLT_PPI:
  3225. /*
  3226. * Check that we have a data frame.
  3227. */
  3228. b0 = gen_check_802_11_data_frame(cstate);
  3229. return b0;
  3230. default:
  3231. bpf_error(cstate, "'llc' not supported for linktype %d", cstate->linktype);
  3232. /* NOTREACHED */
  3233. }
  3234. }
  3235. struct block *
  3236. gen_llc_i(compiler_state_t *cstate)
  3237. {
  3238. struct block *b0, *b1;
  3239. struct slist *s;
  3240. /*
  3241. * Check whether this is an LLC frame.
  3242. */
  3243. b0 = gen_llc(cstate);
  3244. /*
  3245. * Load the control byte and test the low-order bit; it must
  3246. * be clear for I frames.
  3247. */
  3248. s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
  3249. b1 = new_block(cstate, JMP(BPF_JSET));
  3250. b1->s.k = 0x01;
  3251. b1->stmts = s;
  3252. gen_not(b1);
  3253. gen_and(b0, b1);
  3254. return b1;
  3255. }
  3256. struct block *
  3257. gen_llc_s(compiler_state_t *cstate)
  3258. {
  3259. struct block *b0, *b1;
  3260. /*
  3261. * Check whether this is an LLC frame.
  3262. */
  3263. b0 = gen_llc(cstate);
  3264. /*
  3265. * Now compare the low-order 2 bit of the control byte against
  3266. * the appropriate value for S frames.
  3267. */
  3268. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
  3269. gen_and(b0, b1);
  3270. return b1;
  3271. }
  3272. struct block *
  3273. gen_llc_u(compiler_state_t *cstate)
  3274. {
  3275. struct block *b0, *b1;
  3276. /*
  3277. * Check whether this is an LLC frame.
  3278. */
  3279. b0 = gen_llc(cstate);
  3280. /*
  3281. * Now compare the low-order 2 bit of the control byte against
  3282. * the appropriate value for U frames.
  3283. */
  3284. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
  3285. gen_and(b0, b1);
  3286. return b1;
  3287. }
  3288. struct block *
  3289. gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
  3290. {
  3291. struct block *b0, *b1;
  3292. /*
  3293. * Check whether this is an LLC frame.
  3294. */
  3295. b0 = gen_llc(cstate);
  3296. /*
  3297. * Now check for an S frame with the appropriate type.
  3298. */
  3299. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
  3300. gen_and(b0, b1);
  3301. return b1;
  3302. }
  3303. struct block *
  3304. gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
  3305. {
  3306. struct block *b0, *b1;
  3307. /*
  3308. * Check whether this is an LLC frame.
  3309. */
  3310. b0 = gen_llc(cstate);
  3311. /*
  3312. * Now check for a U frame with the appropriate type.
  3313. */
  3314. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
  3315. gen_and(b0, b1);
  3316. return b1;
  3317. }
  3318. /*
  3319. * Generate code to match a particular packet type, for link-layer types
  3320. * using 802.2 LLC headers.
  3321. *
  3322. * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
  3323. * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
  3324. *
  3325. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  3326. * value, if <= ETHERMTU. We use that to determine whether to
  3327. * match the DSAP or both DSAP and LSAP or to check the OUI and
  3328. * protocol ID in a SNAP header.
  3329. */
  3330. static struct block *
  3331. gen_llc_linktype(compiler_state_t *cstate, int proto)
  3332. {
  3333. /*
  3334. * XXX - handle token-ring variable-length header.
  3335. */
  3336. switch (proto) {
  3337. case LLCSAP_IP:
  3338. case LLCSAP_ISONS:
  3339. case LLCSAP_NETBEUI:
  3340. /*
  3341. * XXX - should we check both the DSAP and the
  3342. * SSAP, like this, or should we check just the
  3343. * DSAP, as we do for other SAP values?
  3344. */
  3345. return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
  3346. ((proto << 8) | proto));
  3347. case LLCSAP_IPX:
  3348. /*
  3349. * XXX - are there ever SNAP frames for IPX on
  3350. * non-Ethernet 802.x networks?
  3351. */
  3352. return gen_cmp(cstate, OR_LLC, 0, BPF_B,
  3353. (bpf_int32)LLCSAP_IPX);
  3354. case ETHERTYPE_ATALK:
  3355. /*
  3356. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  3357. * SNAP packets with an organization code of
  3358. * 0x080007 (Apple, for Appletalk) and a protocol
  3359. * type of ETHERTYPE_ATALK (Appletalk).
  3360. *
  3361. * XXX - check for an organization code of
  3362. * encapsulated Ethernet as well?
  3363. */
  3364. return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
  3365. default:
  3366. /*
  3367. * XXX - we don't have to check for IPX 802.3
  3368. * here, but should we check for the IPX Ethertype?
  3369. */
  3370. if (proto <= ETHERMTU) {
  3371. /*
  3372. * This is an LLC SAP value, so check
  3373. * the DSAP.
  3374. */
  3375. return gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)proto);
  3376. } else {
  3377. /*
  3378. * This is an Ethernet type; we assume that it's
  3379. * unlikely that it'll appear in the right place
  3380. * at random, and therefore check only the
  3381. * location that would hold the Ethernet type
  3382. * in a SNAP frame with an organization code of
  3383. * 0x000000 (encapsulated Ethernet).
  3384. *
  3385. * XXX - if we were to check for the SNAP DSAP and
  3386. * LSAP, as per XXX, and were also to check for an
  3387. * organization code of 0x000000 (encapsulated
  3388. * Ethernet), we'd do
  3389. *
  3390. * return gen_snap(cstate, 0x000000, proto);
  3391. *
  3392. * here; for now, we don't, as per the above.
  3393. * I don't know whether it's worth the extra CPU
  3394. * time to do the right check or not.
  3395. */
  3396. return gen_cmp(cstate, OR_LLC, 6, BPF_H, (bpf_int32)proto);
  3397. }
  3398. }
  3399. }
  3400. static struct block *
  3401. gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
  3402. int dir, int proto, u_int src_off, u_int dst_off)
  3403. {
  3404. struct block *b0, *b1;
  3405. u_int offset;
  3406. switch (dir) {
  3407. case Q_SRC:
  3408. offset = src_off;
  3409. break;
  3410. case Q_DST:
  3411. offset = dst_off;
  3412. break;
  3413. case Q_AND:
  3414. b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
  3415. b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
  3416. gen_and(b0, b1);
  3417. return b1;
  3418. case Q_OR:
  3419. case Q_DEFAULT:
  3420. b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
  3421. b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
  3422. gen_or(b0, b1);
  3423. return b1;
  3424. case Q_ADDR1:
  3425. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3426. break;
  3427. case Q_ADDR2:
  3428. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3429. break;
  3430. case Q_ADDR3:
  3431. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3432. break;
  3433. case Q_ADDR4:
  3434. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3435. break;
  3436. case Q_RA:
  3437. bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3438. break;
  3439. case Q_TA:
  3440. bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3441. break;
  3442. default:
  3443. abort();
  3444. }
  3445. b0 = gen_linktype(cstate, proto);
  3446. b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, (bpf_int32)addr, mask);
  3447. gen_and(b0, b1);
  3448. return b1;
  3449. }
  3450. #ifdef INET6
  3451. static struct block *
  3452. gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
  3453. struct in6_addr *mask, int dir, int proto, u_int src_off, u_int dst_off)
  3454. {
  3455. struct block *b0, *b1;
  3456. u_int offset;
  3457. uint32_t *a, *m;
  3458. switch (dir) {
  3459. case Q_SRC:
  3460. offset = src_off;
  3461. break;
  3462. case Q_DST:
  3463. offset = dst_off;
  3464. break;
  3465. case Q_AND:
  3466. b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
  3467. b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
  3468. gen_and(b0, b1);
  3469. return b1;
  3470. case Q_OR:
  3471. case Q_DEFAULT:
  3472. b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
  3473. b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
  3474. gen_or(b0, b1);
  3475. return b1;
  3476. case Q_ADDR1:
  3477. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3478. break;
  3479. case Q_ADDR2:
  3480. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3481. break;
  3482. case Q_ADDR3:
  3483. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3484. break;
  3485. case Q_ADDR4:
  3486. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3487. break;
  3488. case Q_RA:
  3489. bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3490. break;
  3491. case Q_TA:
  3492. bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3493. break;
  3494. default:
  3495. abort();
  3496. }
  3497. /* this order is important */
  3498. a = (uint32_t *)addr;
  3499. m = (uint32_t *)mask;
  3500. b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
  3501. b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
  3502. gen_and(b0, b1);
  3503. b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
  3504. gen_and(b0, b1);
  3505. b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
  3506. gen_and(b0, b1);
  3507. b0 = gen_linktype(cstate, proto);
  3508. gen_and(b0, b1);
  3509. return b1;
  3510. }
  3511. #endif
  3512. static struct block *
  3513. gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3514. {
  3515. register struct block *b0, *b1;
  3516. switch (dir) {
  3517. case Q_SRC:
  3518. return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
  3519. case Q_DST:
  3520. return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
  3521. case Q_AND:
  3522. b0 = gen_ehostop(cstate, eaddr, Q_SRC);
  3523. b1 = gen_ehostop(cstate, eaddr, Q_DST);
  3524. gen_and(b0, b1);
  3525. return b1;
  3526. case Q_DEFAULT:
  3527. case Q_OR:
  3528. b0 = gen_ehostop(cstate, eaddr, Q_SRC);
  3529. b1 = gen_ehostop(cstate, eaddr, Q_DST);
  3530. gen_or(b0, b1);
  3531. return b1;
  3532. case Q_ADDR1:
  3533. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
  3534. break;
  3535. case Q_ADDR2:
  3536. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
  3537. break;
  3538. case Q_ADDR3:
  3539. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
  3540. break;
  3541. case Q_ADDR4:
  3542. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
  3543. break;
  3544. case Q_RA:
  3545. bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
  3546. break;
  3547. case Q_TA:
  3548. bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
  3549. break;
  3550. }
  3551. abort();
  3552. /* NOTREACHED */
  3553. }
  3554. /*
  3555. * Like gen_ehostop, but for DLT_FDDI
  3556. */
  3557. static struct block *
  3558. gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3559. {
  3560. struct block *b0, *b1;
  3561. switch (dir) {
  3562. case Q_SRC:
  3563. return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
  3564. case Q_DST:
  3565. return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
  3566. case Q_AND:
  3567. b0 = gen_fhostop(cstate, eaddr, Q_SRC);
  3568. b1 = gen_fhostop(cstate, eaddr, Q_DST);
  3569. gen_and(b0, b1);
  3570. return b1;
  3571. case Q_DEFAULT:
  3572. case Q_OR:
  3573. b0 = gen_fhostop(cstate, eaddr, Q_SRC);
  3574. b1 = gen_fhostop(cstate, eaddr, Q_DST);
  3575. gen_or(b0, b1);
  3576. return b1;
  3577. case Q_ADDR1:
  3578. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  3579. break;
  3580. case Q_ADDR2:
  3581. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  3582. break;
  3583. case Q_ADDR3:
  3584. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  3585. break;
  3586. case Q_ADDR4:
  3587. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  3588. break;
  3589. case Q_RA:
  3590. bpf_error(cstate, "'ra' is only supported on 802.11");
  3591. break;
  3592. case Q_TA:
  3593. bpf_error(cstate, "'ta' is only supported on 802.11");
  3594. break;
  3595. }
  3596. abort();
  3597. /* NOTREACHED */
  3598. }
  3599. /*
  3600. * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
  3601. */
  3602. static struct block *
  3603. gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3604. {
  3605. register struct block *b0, *b1;
  3606. switch (dir) {
  3607. case Q_SRC:
  3608. return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
  3609. case Q_DST:
  3610. return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
  3611. case Q_AND:
  3612. b0 = gen_thostop(cstate, eaddr, Q_SRC);
  3613. b1 = gen_thostop(cstate, eaddr, Q_DST);
  3614. gen_and(b0, b1);
  3615. return b1;
  3616. case Q_DEFAULT:
  3617. case Q_OR:
  3618. b0 = gen_thostop(cstate, eaddr, Q_SRC);
  3619. b1 = gen_thostop(cstate, eaddr, Q_DST);
  3620. gen_or(b0, b1);
  3621. return b1;
  3622. case Q_ADDR1:
  3623. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  3624. break;
  3625. case Q_ADDR2:
  3626. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  3627. break;
  3628. case Q_ADDR3:
  3629. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  3630. break;
  3631. case Q_ADDR4:
  3632. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  3633. break;
  3634. case Q_RA:
  3635. bpf_error(cstate, "'ra' is only supported on 802.11");
  3636. break;
  3637. case Q_TA:
  3638. bpf_error(cstate, "'ta' is only supported on 802.11");
  3639. break;
  3640. }
  3641. abort();
  3642. /* NOTREACHED */
  3643. }
  3644. /*
  3645. * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
  3646. * various 802.11 + radio headers.
  3647. */
  3648. static struct block *
  3649. gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3650. {
  3651. register struct block *b0, *b1, *b2;
  3652. register struct slist *s;
  3653. #ifdef ENABLE_WLAN_FILTERING_PATCH
  3654. /*
  3655. * TODO GV 20070613
  3656. * We need to disable the optimizer because the optimizer is buggy
  3657. * and wipes out some LD instructions generated by the below
  3658. * code to validate the Frame Control bits
  3659. */
  3660. cstate->no_optimize = 1;
  3661. #endif /* ENABLE_WLAN_FILTERING_PATCH */
  3662. switch (dir) {
  3663. case Q_SRC:
  3664. /*
  3665. * Oh, yuk.
  3666. *
  3667. * For control frames, there is no SA.
  3668. *
  3669. * For management frames, SA is at an
  3670. * offset of 10 from the beginning of
  3671. * the packet.
  3672. *
  3673. * For data frames, SA is at an offset
  3674. * of 10 from the beginning of the packet
  3675. * if From DS is clear, at an offset of
  3676. * 16 from the beginning of the packet
  3677. * if From DS is set and To DS is clear,
  3678. * and an offset of 24 from the beginning
  3679. * of the packet if From DS is set and To DS
  3680. * is set.
  3681. */
  3682. /*
  3683. * Generate the tests to be done for data frames
  3684. * with From DS set.
  3685. *
  3686. * First, check for To DS set, i.e. check "link[1] & 0x01".
  3687. */
  3688. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3689. b1 = new_block(cstate, JMP(BPF_JSET));
  3690. b1->s.k = 0x01; /* To DS */
  3691. b1->stmts = s;
  3692. /*
  3693. * If To DS is set, the SA is at 24.
  3694. */
  3695. b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
  3696. gen_and(b1, b0);
  3697. /*
  3698. * Now, check for To DS not set, i.e. check
  3699. * "!(link[1] & 0x01)".
  3700. */
  3701. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3702. b2 = new_block(cstate, JMP(BPF_JSET));
  3703. b2->s.k = 0x01; /* To DS */
  3704. b2->stmts = s;
  3705. gen_not(b2);
  3706. /*
  3707. * If To DS is not set, the SA is at 16.
  3708. */
  3709. b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
  3710. gen_and(b2, b1);
  3711. /*
  3712. * Now OR together the last two checks. That gives
  3713. * the complete set of checks for data frames with
  3714. * From DS set.
  3715. */
  3716. gen_or(b1, b0);
  3717. /*
  3718. * Now check for From DS being set, and AND that with
  3719. * the ORed-together checks.
  3720. */
  3721. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3722. b1 = new_block(cstate, JMP(BPF_JSET));
  3723. b1->s.k = 0x02; /* From DS */
  3724. b1->stmts = s;
  3725. gen_and(b1, b0);
  3726. /*
  3727. * Now check for data frames with From DS not set.
  3728. */
  3729. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3730. b2 = new_block(cstate, JMP(BPF_JSET));
  3731. b2->s.k = 0x02; /* From DS */
  3732. b2->stmts = s;
  3733. gen_not(b2);
  3734. /*
  3735. * If From DS isn't set, the SA is at 10.
  3736. */
  3737. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  3738. gen_and(b2, b1);
  3739. /*
  3740. * Now OR together the checks for data frames with
  3741. * From DS not set and for data frames with From DS
  3742. * set; that gives the checks done for data frames.
  3743. */
  3744. gen_or(b1, b0);
  3745. /*
  3746. * Now check for a data frame.
  3747. * I.e, check "link[0] & 0x08".
  3748. */
  3749. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3750. b1 = new_block(cstate, JMP(BPF_JSET));
  3751. b1->s.k = 0x08;
  3752. b1->stmts = s;
  3753. /*
  3754. * AND that with the checks done for data frames.
  3755. */
  3756. gen_and(b1, b0);
  3757. /*
  3758. * If the high-order bit of the type value is 0, this
  3759. * is a management frame.
  3760. * I.e, check "!(link[0] & 0x08)".
  3761. */
  3762. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3763. b2 = new_block(cstate, JMP(BPF_JSET));
  3764. b2->s.k = 0x08;
  3765. b2->stmts = s;
  3766. gen_not(b2);
  3767. /*
  3768. * For management frames, the SA is at 10.
  3769. */
  3770. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  3771. gen_and(b2, b1);
  3772. /*
  3773. * OR that with the checks done for data frames.
  3774. * That gives the checks done for management and
  3775. * data frames.
  3776. */
  3777. gen_or(b1, b0);
  3778. /*
  3779. * If the low-order bit of the type value is 1,
  3780. * this is either a control frame or a frame
  3781. * with a reserved type, and thus not a
  3782. * frame with an SA.
  3783. *
  3784. * I.e., check "!(link[0] & 0x04)".
  3785. */
  3786. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3787. b1 = new_block(cstate, JMP(BPF_JSET));
  3788. b1->s.k = 0x04;
  3789. b1->stmts = s;
  3790. gen_not(b1);
  3791. /*
  3792. * AND that with the checks for data and management
  3793. * frames.
  3794. */
  3795. gen_and(b1, b0);
  3796. return b0;
  3797. case Q_DST:
  3798. /*
  3799. * Oh, yuk.
  3800. *
  3801. * For control frames, there is no DA.
  3802. *
  3803. * For management frames, DA is at an
  3804. * offset of 4 from the beginning of
  3805. * the packet.
  3806. *
  3807. * For data frames, DA is at an offset
  3808. * of 4 from the beginning of the packet
  3809. * if To DS is clear and at an offset of
  3810. * 16 from the beginning of the packet
  3811. * if To DS is set.
  3812. */
  3813. /*
  3814. * Generate the tests to be done for data frames.
  3815. *
  3816. * First, check for To DS set, i.e. "link[1] & 0x01".
  3817. */
  3818. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3819. b1 = new_block(cstate, JMP(BPF_JSET));
  3820. b1->s.k = 0x01; /* To DS */
  3821. b1->stmts = s;
  3822. /*
  3823. * If To DS is set, the DA is at 16.
  3824. */
  3825. b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
  3826. gen_and(b1, b0);
  3827. /*
  3828. * Now, check for To DS not set, i.e. check
  3829. * "!(link[1] & 0x01)".
  3830. */
  3831. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3832. b2 = new_block(cstate, JMP(BPF_JSET));
  3833. b2->s.k = 0x01; /* To DS */
  3834. b2->stmts = s;
  3835. gen_not(b2);
  3836. /*
  3837. * If To DS is not set, the DA is at 4.
  3838. */
  3839. b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
  3840. gen_and(b2, b1);
  3841. /*
  3842. * Now OR together the last two checks. That gives
  3843. * the complete set of checks for data frames.
  3844. */
  3845. gen_or(b1, b0);
  3846. /*
  3847. * Now check for a data frame.
  3848. * I.e, check "link[0] & 0x08".
  3849. */
  3850. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3851. b1 = new_block(cstate, JMP(BPF_JSET));
  3852. b1->s.k = 0x08;
  3853. b1->stmts = s;
  3854. /*
  3855. * AND that with the checks done for data frames.
  3856. */
  3857. gen_and(b1, b0);
  3858. /*
  3859. * If the high-order bit of the type value is 0, this
  3860. * is a management frame.
  3861. * I.e, check "!(link[0] & 0x08)".
  3862. */
  3863. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3864. b2 = new_block(cstate, JMP(BPF_JSET));
  3865. b2->s.k = 0x08;
  3866. b2->stmts = s;
  3867. gen_not(b2);
  3868. /*
  3869. * For management frames, the DA is at 4.
  3870. */
  3871. b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
  3872. gen_and(b2, b1);
  3873. /*
  3874. * OR that with the checks done for data frames.
  3875. * That gives the checks done for management and
  3876. * data frames.
  3877. */
  3878. gen_or(b1, b0);
  3879. /*
  3880. * If the low-order bit of the type value is 1,
  3881. * this is either a control frame or a frame
  3882. * with a reserved type, and thus not a
  3883. * frame with an SA.
  3884. *
  3885. * I.e., check "!(link[0] & 0x04)".
  3886. */
  3887. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3888. b1 = new_block(cstate, JMP(BPF_JSET));
  3889. b1->s.k = 0x04;
  3890. b1->stmts = s;
  3891. gen_not(b1);
  3892. /*
  3893. * AND that with the checks for data and management
  3894. * frames.
  3895. */
  3896. gen_and(b1, b0);
  3897. return b0;
  3898. case Q_RA:
  3899. /*
  3900. * Not present in management frames; addr1 in other
  3901. * frames.
  3902. */
  3903. /*
  3904. * If the high-order bit of the type value is 0, this
  3905. * is a management frame.
  3906. * I.e, check "(link[0] & 0x08)".
  3907. */
  3908. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3909. b1 = new_block(cstate, JMP(BPF_JSET));
  3910. b1->s.k = 0x08;
  3911. b1->stmts = s;
  3912. /*
  3913. * Check addr1.
  3914. */
  3915. b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
  3916. /*
  3917. * AND that with the check of addr1.
  3918. */
  3919. gen_and(b1, b0);
  3920. return (b0);
  3921. case Q_TA:
  3922. /*
  3923. * Not present in management frames; addr2, if present,
  3924. * in other frames.
  3925. */
  3926. /*
  3927. * Not present in CTS or ACK control frames.
  3928. */
  3929. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  3930. IEEE80211_FC0_TYPE_MASK);
  3931. gen_not(b0);
  3932. b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
  3933. IEEE80211_FC0_SUBTYPE_MASK);
  3934. gen_not(b1);
  3935. b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
  3936. IEEE80211_FC0_SUBTYPE_MASK);
  3937. gen_not(b2);
  3938. gen_and(b1, b2);
  3939. gen_or(b0, b2);
  3940. /*
  3941. * If the high-order bit of the type value is 0, this
  3942. * is a management frame.
  3943. * I.e, check "(link[0] & 0x08)".
  3944. */
  3945. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3946. b1 = new_block(cstate, JMP(BPF_JSET));
  3947. b1->s.k = 0x08;
  3948. b1->stmts = s;
  3949. /*
  3950. * AND that with the check for frames other than
  3951. * CTS and ACK frames.
  3952. */
  3953. gen_and(b1, b2);
  3954. /*
  3955. * Check addr2.
  3956. */
  3957. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  3958. gen_and(b2, b1);
  3959. return b1;
  3960. /*
  3961. * XXX - add BSSID keyword?
  3962. */
  3963. case Q_ADDR1:
  3964. return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
  3965. case Q_ADDR2:
  3966. /*
  3967. * Not present in CTS or ACK control frames.
  3968. */
  3969. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  3970. IEEE80211_FC0_TYPE_MASK);
  3971. gen_not(b0);
  3972. b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
  3973. IEEE80211_FC0_SUBTYPE_MASK);
  3974. gen_not(b1);
  3975. b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
  3976. IEEE80211_FC0_SUBTYPE_MASK);
  3977. gen_not(b2);
  3978. gen_and(b1, b2);
  3979. gen_or(b0, b2);
  3980. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  3981. gen_and(b2, b1);
  3982. return b1;
  3983. case Q_ADDR3:
  3984. /*
  3985. * Not present in control frames.
  3986. */
  3987. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  3988. IEEE80211_FC0_TYPE_MASK);
  3989. gen_not(b0);
  3990. b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
  3991. gen_and(b0, b1);
  3992. return b1;
  3993. case Q_ADDR4:
  3994. /*
  3995. * Present only if the direction mask has both "From DS"
  3996. * and "To DS" set. Neither control frames nor management
  3997. * frames should have both of those set, so we don't
  3998. * check the frame type.
  3999. */
  4000. b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
  4001. IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
  4002. b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
  4003. gen_and(b0, b1);
  4004. return b1;
  4005. case Q_AND:
  4006. b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
  4007. b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
  4008. gen_and(b0, b1);
  4009. return b1;
  4010. case Q_DEFAULT:
  4011. case Q_OR:
  4012. b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
  4013. b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
  4014. gen_or(b0, b1);
  4015. return b1;
  4016. }
  4017. abort();
  4018. /* NOTREACHED */
  4019. }
  4020. /*
  4021. * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
  4022. * (We assume that the addresses are IEEE 48-bit MAC addresses,
  4023. * as the RFC states.)
  4024. */
  4025. static struct block *
  4026. gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  4027. {
  4028. register struct block *b0, *b1;
  4029. switch (dir) {
  4030. case Q_SRC:
  4031. return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  4032. case Q_DST:
  4033. return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
  4034. case Q_AND:
  4035. b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
  4036. b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
  4037. gen_and(b0, b1);
  4038. return b1;
  4039. case Q_DEFAULT:
  4040. case Q_OR:
  4041. b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
  4042. b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
  4043. gen_or(b0, b1);
  4044. return b1;
  4045. case Q_ADDR1:
  4046. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  4047. break;
  4048. case Q_ADDR2:
  4049. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  4050. break;
  4051. case Q_ADDR3:
  4052. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  4053. break;
  4054. case Q_ADDR4:
  4055. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  4056. break;
  4057. case Q_RA:
  4058. bpf_error(cstate, "'ra' is only supported on 802.11");
  4059. break;
  4060. case Q_TA:
  4061. bpf_error(cstate, "'ta' is only supported on 802.11");
  4062. break;
  4063. }
  4064. abort();
  4065. /* NOTREACHED */
  4066. }
  4067. /*
  4068. * This is quite tricky because there may be pad bytes in front of the
  4069. * DECNET header, and then there are two possible data packet formats that
  4070. * carry both src and dst addresses, plus 5 packet types in a format that
  4071. * carries only the src node, plus 2 types that use a different format and
  4072. * also carry just the src node.
  4073. *
  4074. * Yuck.
  4075. *
  4076. * Instead of doing those all right, we just look for data packets with
  4077. * 0 or 1 bytes of padding. If you want to look at other packets, that
  4078. * will require a lot more hacking.
  4079. *
  4080. * To add support for filtering on DECNET "areas" (network numbers)
  4081. * one would want to add a "mask" argument to this routine. That would
  4082. * make the filter even more inefficient, although one could be clever
  4083. * and not generate masking instructions if the mask is 0xFFFF.
  4084. */
  4085. static struct block *
  4086. gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
  4087. {
  4088. struct block *b0, *b1, *b2, *tmp;
  4089. u_int offset_lh; /* offset if long header is received */
  4090. u_int offset_sh; /* offset if short header is received */
  4091. switch (dir) {
  4092. case Q_DST:
  4093. offset_sh = 1; /* follows flags */
  4094. offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
  4095. break;
  4096. case Q_SRC:
  4097. offset_sh = 3; /* follows flags, dstnode */
  4098. offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
  4099. break;
  4100. case Q_AND:
  4101. /* Inefficient because we do our Calvinball dance twice */
  4102. b0 = gen_dnhostop(cstate, addr, Q_SRC);
  4103. b1 = gen_dnhostop(cstate, addr, Q_DST);
  4104. gen_and(b0, b1);
  4105. return b1;
  4106. case Q_OR:
  4107. case Q_DEFAULT:
  4108. /* Inefficient because we do our Calvinball dance twice */
  4109. b0 = gen_dnhostop(cstate, addr, Q_SRC);
  4110. b1 = gen_dnhostop(cstate, addr, Q_DST);
  4111. gen_or(b0, b1);
  4112. return b1;
  4113. case Q_ISO:
  4114. bpf_error(cstate, "ISO host filtering not implemented");
  4115. default:
  4116. abort();
  4117. }
  4118. b0 = gen_linktype(cstate, ETHERTYPE_DN);
  4119. /* Check for pad = 1, long header case */
  4120. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
  4121. (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
  4122. b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
  4123. BPF_H, (bpf_int32)ntohs((u_short)addr));
  4124. gen_and(tmp, b1);
  4125. /* Check for pad = 0, long header case */
  4126. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
  4127. b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
  4128. gen_and(tmp, b2);
  4129. gen_or(b2, b1);
  4130. /* Check for pad = 1, short header case */
  4131. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
  4132. (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
  4133. b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
  4134. gen_and(tmp, b2);
  4135. gen_or(b2, b1);
  4136. /* Check for pad = 0, short header case */
  4137. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
  4138. b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
  4139. gen_and(tmp, b2);
  4140. gen_or(b2, b1);
  4141. /* Combine with test for cstate->linktype */
  4142. gen_and(b0, b1);
  4143. return b1;
  4144. }
  4145. /*
  4146. * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
  4147. * test the bottom-of-stack bit, and then check the version number
  4148. * field in the IP header.
  4149. */
  4150. static struct block *
  4151. gen_mpls_linktype(compiler_state_t *cstate, int proto)
  4152. {
  4153. struct block *b0, *b1;
  4154. switch (proto) {
  4155. case Q_IP:
  4156. /* match the bottom-of-stack bit */
  4157. b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
  4158. /* match the IPv4 version number */
  4159. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
  4160. gen_and(b0, b1);
  4161. return b1;
  4162. case Q_IPV6:
  4163. /* match the bottom-of-stack bit */
  4164. b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
  4165. /* match the IPv4 version number */
  4166. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
  4167. gen_and(b0, b1);
  4168. return b1;
  4169. default:
  4170. abort();
  4171. }
  4172. }
  4173. static struct block *
  4174. gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
  4175. int proto, int dir, int type)
  4176. {
  4177. struct block *b0, *b1;
  4178. const char *typestr;
  4179. if (type == Q_NET)
  4180. typestr = "net";
  4181. else
  4182. typestr = "host";
  4183. switch (proto) {
  4184. case Q_DEFAULT:
  4185. b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
  4186. /*
  4187. * Only check for non-IPv4 addresses if we're not
  4188. * checking MPLS-encapsulated packets.
  4189. */
  4190. if (cstate->label_stack_depth == 0) {
  4191. b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
  4192. gen_or(b0, b1);
  4193. b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
  4194. gen_or(b1, b0);
  4195. }
  4196. return b0;
  4197. case Q_IP:
  4198. return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
  4199. case Q_RARP:
  4200. return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
  4201. case Q_ARP:
  4202. return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
  4203. case Q_TCP:
  4204. bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
  4205. case Q_SCTP:
  4206. bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
  4207. case Q_UDP:
  4208. bpf_error(cstate, "'udp' modifier applied to %s", typestr);
  4209. case Q_ICMP:
  4210. bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
  4211. case Q_IGMP:
  4212. bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
  4213. case Q_IGRP:
  4214. bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
  4215. case Q_PIM:
  4216. bpf_error(cstate, "'pim' modifier applied to %s", typestr);
  4217. case Q_VRRP:
  4218. bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
  4219. case Q_CARP:
  4220. bpf_error(cstate, "'carp' modifier applied to %s", typestr);
  4221. case Q_ATALK:
  4222. bpf_error(cstate, "ATALK host filtering not implemented");
  4223. case Q_AARP:
  4224. bpf_error(cstate, "AARP host filtering not implemented");
  4225. case Q_DECNET:
  4226. return gen_dnhostop(cstate, addr, dir);
  4227. case Q_SCA:
  4228. bpf_error(cstate, "SCA host filtering not implemented");
  4229. case Q_LAT:
  4230. bpf_error(cstate, "LAT host filtering not implemented");
  4231. case Q_MOPDL:
  4232. bpf_error(cstate, "MOPDL host filtering not implemented");
  4233. case Q_MOPRC:
  4234. bpf_error(cstate, "MOPRC host filtering not implemented");
  4235. case Q_IPV6:
  4236. bpf_error(cstate, "'ip6' modifier applied to ip host");
  4237. case Q_ICMPV6:
  4238. bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
  4239. case Q_AH:
  4240. bpf_error(cstate, "'ah' modifier applied to %s", typestr);
  4241. case Q_ESP:
  4242. bpf_error(cstate, "'esp' modifier applied to %s", typestr);
  4243. case Q_ISO:
  4244. bpf_error(cstate, "ISO host filtering not implemented");
  4245. case Q_ESIS:
  4246. bpf_error(cstate, "'esis' modifier applied to %s", typestr);
  4247. case Q_ISIS:
  4248. bpf_error(cstate, "'isis' modifier applied to %s", typestr);
  4249. case Q_CLNP:
  4250. bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
  4251. case Q_STP:
  4252. bpf_error(cstate, "'stp' modifier applied to %s", typestr);
  4253. case Q_IPX:
  4254. bpf_error(cstate, "IPX host filtering not implemented");
  4255. case Q_NETBEUI:
  4256. bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
  4257. case Q_RADIO:
  4258. bpf_error(cstate, "'radio' modifier applied to %s", typestr);
  4259. default:
  4260. abort();
  4261. }
  4262. /* NOTREACHED */
  4263. }
  4264. #ifdef INET6
  4265. static struct block *
  4266. gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
  4267. struct in6_addr *mask, int proto, int dir, int type)
  4268. {
  4269. const char *typestr;
  4270. if (type == Q_NET)
  4271. typestr = "net";
  4272. else
  4273. typestr = "host";
  4274. switch (proto) {
  4275. case Q_DEFAULT:
  4276. return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
  4277. case Q_LINK:
  4278. bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
  4279. case Q_IP:
  4280. bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
  4281. case Q_RARP:
  4282. bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
  4283. case Q_ARP:
  4284. bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
  4285. case Q_SCTP:
  4286. bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
  4287. case Q_TCP:
  4288. bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
  4289. case Q_UDP:
  4290. bpf_error(cstate, "'udp' modifier applied to %s", typestr);
  4291. case Q_ICMP:
  4292. bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
  4293. case Q_IGMP:
  4294. bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
  4295. case Q_IGRP:
  4296. bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
  4297. case Q_PIM:
  4298. bpf_error(cstate, "'pim' modifier applied to %s", typestr);
  4299. case Q_VRRP:
  4300. bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
  4301. case Q_CARP:
  4302. bpf_error(cstate, "'carp' modifier applied to %s", typestr);
  4303. case Q_ATALK:
  4304. bpf_error(cstate, "ATALK host filtering not implemented");
  4305. case Q_AARP:
  4306. bpf_error(cstate, "AARP host filtering not implemented");
  4307. case Q_DECNET:
  4308. bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
  4309. case Q_SCA:
  4310. bpf_error(cstate, "SCA host filtering not implemented");
  4311. case Q_LAT:
  4312. bpf_error(cstate, "LAT host filtering not implemented");
  4313. case Q_MOPDL:
  4314. bpf_error(cstate, "MOPDL host filtering not implemented");
  4315. case Q_MOPRC:
  4316. bpf_error(cstate, "MOPRC host filtering not implemented");
  4317. case Q_IPV6:
  4318. return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
  4319. case Q_ICMPV6:
  4320. bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
  4321. case Q_AH:
  4322. bpf_error(cstate, "'ah' modifier applied to %s", typestr);
  4323. case Q_ESP:
  4324. bpf_error(cstate, "'esp' modifier applied to %s", typestr);
  4325. case Q_ISO:
  4326. bpf_error(cstate, "ISO host filtering not implemented");
  4327. case Q_ESIS:
  4328. bpf_error(cstate, "'esis' modifier applied to %s", typestr);
  4329. case Q_ISIS:
  4330. bpf_error(cstate, "'isis' modifier applied to %s", typestr);
  4331. case Q_CLNP:
  4332. bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
  4333. case Q_STP:
  4334. bpf_error(cstate, "'stp' modifier applied to %s", typestr);
  4335. case Q_IPX:
  4336. bpf_error(cstate, "IPX host filtering not implemented");
  4337. case Q_NETBEUI:
  4338. bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
  4339. case Q_RADIO:
  4340. bpf_error(cstate, "'radio' modifier applied to %s", typestr);
  4341. default:
  4342. abort();
  4343. }
  4344. /* NOTREACHED */
  4345. }
  4346. #endif
  4347. #ifndef INET6
  4348. static struct block *
  4349. gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
  4350. struct addrinfo *alist, int proto, int dir)
  4351. {
  4352. struct block *b0, *b1, *tmp;
  4353. struct addrinfo *ai;
  4354. struct sockaddr_in *sin;
  4355. if (dir != 0)
  4356. bpf_error(cstate, "direction applied to 'gateway'");
  4357. switch (proto) {
  4358. case Q_DEFAULT:
  4359. case Q_IP:
  4360. case Q_ARP:
  4361. case Q_RARP:
  4362. switch (cstate->linktype) {
  4363. case DLT_EN10MB:
  4364. case DLT_NETANALYZER:
  4365. case DLT_NETANALYZER_TRANSPARENT:
  4366. b1 = gen_prevlinkhdr_check(cstate);
  4367. b0 = gen_ehostop(cstate, eaddr, Q_OR);
  4368. if (b1 != NULL)
  4369. gen_and(b1, b0);
  4370. break;
  4371. case DLT_FDDI:
  4372. b0 = gen_fhostop(cstate, eaddr, Q_OR);
  4373. break;
  4374. case DLT_IEEE802:
  4375. b0 = gen_thostop(cstate, eaddr, Q_OR);
  4376. break;
  4377. case DLT_IEEE802_11:
  4378. case DLT_PRISM_HEADER:
  4379. case DLT_IEEE802_11_RADIO_AVS:
  4380. case DLT_IEEE802_11_RADIO:
  4381. case DLT_PPI:
  4382. b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
  4383. break;
  4384. case DLT_SUNATM:
  4385. /*
  4386. * This is LLC-multiplexed traffic; if it were
  4387. * LANE, cstate->linktype would have been set to
  4388. * DLT_EN10MB.
  4389. */
  4390. bpf_error(cstate,
  4391. "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  4392. break;
  4393. case DLT_IP_OVER_FC:
  4394. b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
  4395. break;
  4396. default:
  4397. bpf_error(cstate,
  4398. "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  4399. }
  4400. b1 = NULL;
  4401. for (ai = alist; ai != NULL; ai = ai->ai_next) {
  4402. /*
  4403. * Does it have an address?
  4404. */
  4405. if (ai->ai_addr != NULL) {
  4406. /*
  4407. * Yes. Is it an IPv4 address?
  4408. */
  4409. if (ai->ai_addr->sa_family == AF_INET) {
  4410. /*
  4411. * Generate an entry for it.
  4412. */
  4413. sin = (struct sockaddr_in *)ai->ai_addr;
  4414. tmp = gen_host(cstate,
  4415. ntohl(sin->sin_addr.s_addr),
  4416. 0xffffffff, proto, Q_OR, Q_HOST);
  4417. /*
  4418. * Is it the *first* IPv4 address?
  4419. */
  4420. if (b1 == NULL) {
  4421. /*
  4422. * Yes, so start with it.
  4423. */
  4424. b1 = tmp;
  4425. } else {
  4426. /*
  4427. * No, so OR it into the
  4428. * existing set of
  4429. * addresses.
  4430. */
  4431. gen_or(b1, tmp);
  4432. b1 = tmp;
  4433. }
  4434. }
  4435. }
  4436. }
  4437. if (b1 == NULL) {
  4438. /*
  4439. * No IPv4 addresses found.
  4440. */
  4441. return (NULL);
  4442. }
  4443. gen_not(b1);
  4444. gen_and(b0, b1);
  4445. return b1;
  4446. }
  4447. bpf_error(cstate, "illegal modifier of 'gateway'");
  4448. /* NOTREACHED */
  4449. }
  4450. #endif
  4451. struct block *
  4452. gen_proto_abbrev(compiler_state_t *cstate, int proto)
  4453. {
  4454. struct block *b0;
  4455. struct block *b1;
  4456. switch (proto) {
  4457. case Q_SCTP:
  4458. b1 = gen_proto(cstate, IPPROTO_SCTP, Q_IP, Q_DEFAULT);
  4459. b0 = gen_proto(cstate, IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
  4460. gen_or(b0, b1);
  4461. break;
  4462. case Q_TCP:
  4463. b1 = gen_proto(cstate, IPPROTO_TCP, Q_IP, Q_DEFAULT);
  4464. b0 = gen_proto(cstate, IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
  4465. gen_or(b0, b1);
  4466. break;
  4467. case Q_UDP:
  4468. b1 = gen_proto(cstate, IPPROTO_UDP, Q_IP, Q_DEFAULT);
  4469. b0 = gen_proto(cstate, IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
  4470. gen_or(b0, b1);
  4471. break;
  4472. case Q_ICMP:
  4473. b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
  4474. break;
  4475. #ifndef IPPROTO_IGMP
  4476. #define IPPROTO_IGMP 2
  4477. #endif
  4478. case Q_IGMP:
  4479. b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
  4480. break;
  4481. #ifndef IPPROTO_IGRP
  4482. #define IPPROTO_IGRP 9
  4483. #endif
  4484. case Q_IGRP:
  4485. b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
  4486. break;
  4487. #ifndef IPPROTO_PIM
  4488. #define IPPROTO_PIM 103
  4489. #endif
  4490. case Q_PIM:
  4491. b1 = gen_proto(cstate, IPPROTO_PIM, Q_IP, Q_DEFAULT);
  4492. b0 = gen_proto(cstate, IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
  4493. gen_or(b0, b1);
  4494. break;
  4495. #ifndef IPPROTO_VRRP
  4496. #define IPPROTO_VRRP 112
  4497. #endif
  4498. case Q_VRRP:
  4499. b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
  4500. break;
  4501. #ifndef IPPROTO_CARP
  4502. #define IPPROTO_CARP 112
  4503. #endif
  4504. case Q_CARP:
  4505. b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
  4506. break;
  4507. case Q_IP:
  4508. b1 = gen_linktype(cstate, ETHERTYPE_IP);
  4509. break;
  4510. case Q_ARP:
  4511. b1 = gen_linktype(cstate, ETHERTYPE_ARP);
  4512. break;
  4513. case Q_RARP:
  4514. b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
  4515. break;
  4516. case Q_LINK:
  4517. bpf_error(cstate, "link layer applied in wrong context");
  4518. case Q_ATALK:
  4519. b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
  4520. break;
  4521. case Q_AARP:
  4522. b1 = gen_linktype(cstate, ETHERTYPE_AARP);
  4523. break;
  4524. case Q_DECNET:
  4525. b1 = gen_linktype(cstate, ETHERTYPE_DN);
  4526. break;
  4527. case Q_SCA:
  4528. b1 = gen_linktype(cstate, ETHERTYPE_SCA);
  4529. break;
  4530. case Q_LAT:
  4531. b1 = gen_linktype(cstate, ETHERTYPE_LAT);
  4532. break;
  4533. case Q_MOPDL:
  4534. b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
  4535. break;
  4536. case Q_MOPRC:
  4537. b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
  4538. break;
  4539. case Q_IPV6:
  4540. b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
  4541. break;
  4542. #ifndef IPPROTO_ICMPV6
  4543. #define IPPROTO_ICMPV6 58
  4544. #endif
  4545. case Q_ICMPV6:
  4546. b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
  4547. break;
  4548. #ifndef IPPROTO_AH
  4549. #define IPPROTO_AH 51
  4550. #endif
  4551. case Q_AH:
  4552. b1 = gen_proto(cstate, IPPROTO_AH, Q_IP, Q_DEFAULT);
  4553. b0 = gen_proto(cstate, IPPROTO_AH, Q_IPV6, Q_DEFAULT);
  4554. gen_or(b0, b1);
  4555. break;
  4556. #ifndef IPPROTO_ESP
  4557. #define IPPROTO_ESP 50
  4558. #endif
  4559. case Q_ESP:
  4560. b1 = gen_proto(cstate, IPPROTO_ESP, Q_IP, Q_DEFAULT);
  4561. b0 = gen_proto(cstate, IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
  4562. gen_or(b0, b1);
  4563. break;
  4564. case Q_ISO:
  4565. b1 = gen_linktype(cstate, LLCSAP_ISONS);
  4566. break;
  4567. case Q_ESIS:
  4568. b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
  4569. break;
  4570. case Q_ISIS:
  4571. b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
  4572. break;
  4573. case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
  4574. b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4575. b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
  4576. gen_or(b0, b1);
  4577. b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
  4578. gen_or(b0, b1);
  4579. b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4580. gen_or(b0, b1);
  4581. b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4582. gen_or(b0, b1);
  4583. break;
  4584. case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
  4585. b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4586. b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
  4587. gen_or(b0, b1);
  4588. b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
  4589. gen_or(b0, b1);
  4590. b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4591. gen_or(b0, b1);
  4592. b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4593. gen_or(b0, b1);
  4594. break;
  4595. case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
  4596. b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4597. b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4598. gen_or(b0, b1);
  4599. b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
  4600. gen_or(b0, b1);
  4601. break;
  4602. case Q_ISIS_LSP:
  4603. b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
  4604. b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
  4605. gen_or(b0, b1);
  4606. break;
  4607. case Q_ISIS_SNP:
  4608. b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4609. b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4610. gen_or(b0, b1);
  4611. b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4612. gen_or(b0, b1);
  4613. b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4614. gen_or(b0, b1);
  4615. break;
  4616. case Q_ISIS_CSNP:
  4617. b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4618. b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4619. gen_or(b0, b1);
  4620. break;
  4621. case Q_ISIS_PSNP:
  4622. b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4623. b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4624. gen_or(b0, b1);
  4625. break;
  4626. case Q_CLNP:
  4627. b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
  4628. break;
  4629. case Q_STP:
  4630. b1 = gen_linktype(cstate, LLCSAP_8021D);
  4631. break;
  4632. case Q_IPX:
  4633. b1 = gen_linktype(cstate, LLCSAP_IPX);
  4634. break;
  4635. case Q_NETBEUI:
  4636. b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
  4637. break;
  4638. case Q_RADIO:
  4639. bpf_error(cstate, "'radio' is not a valid protocol type");
  4640. default:
  4641. abort();
  4642. }
  4643. return b1;
  4644. }
  4645. static struct block *
  4646. gen_ipfrag(compiler_state_t *cstate)
  4647. {
  4648. struct slist *s;
  4649. struct block *b;
  4650. /* not IPv4 frag other than the first frag */
  4651. s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
  4652. b = new_block(cstate, JMP(BPF_JSET));
  4653. b->s.k = 0x1fff;
  4654. b->stmts = s;
  4655. gen_not(b);
  4656. return b;
  4657. }
  4658. /*
  4659. * Generate a comparison to a port value in the transport-layer header
  4660. * at the specified offset from the beginning of that header.
  4661. *
  4662. * XXX - this handles a variable-length prefix preceding the link-layer
  4663. * header, such as the radiotap or AVS radio prefix, but doesn't handle
  4664. * variable-length link-layer headers (such as Token Ring or 802.11
  4665. * headers).
  4666. */
  4667. static struct block *
  4668. gen_portatom(compiler_state_t *cstate, int off, bpf_int32 v)
  4669. {
  4670. return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
  4671. }
  4672. static struct block *
  4673. gen_portatom6(compiler_state_t *cstate, int off, bpf_int32 v)
  4674. {
  4675. return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
  4676. }
  4677. struct block *
  4678. gen_portop(compiler_state_t *cstate, int port, int proto, int dir)
  4679. {
  4680. struct block *b0, *b1, *tmp;
  4681. /* ip proto 'proto' and not a fragment other than the first fragment */
  4682. tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)proto);
  4683. b0 = gen_ipfrag(cstate);
  4684. gen_and(tmp, b0);
  4685. switch (dir) {
  4686. case Q_SRC:
  4687. b1 = gen_portatom(cstate, 0, (bpf_int32)port);
  4688. break;
  4689. case Q_DST:
  4690. b1 = gen_portatom(cstate, 2, (bpf_int32)port);
  4691. break;
  4692. case Q_OR:
  4693. case Q_DEFAULT:
  4694. tmp = gen_portatom(cstate, 0, (bpf_int32)port);
  4695. b1 = gen_portatom(cstate, 2, (bpf_int32)port);
  4696. gen_or(tmp, b1);
  4697. break;
  4698. case Q_AND:
  4699. tmp = gen_portatom(cstate, 0, (bpf_int32)port);
  4700. b1 = gen_portatom(cstate, 2, (bpf_int32)port);
  4701. gen_and(tmp, b1);
  4702. break;
  4703. default:
  4704. abort();
  4705. }
  4706. gen_and(b0, b1);
  4707. return b1;
  4708. }
  4709. static struct block *
  4710. gen_port(compiler_state_t *cstate, int port, int ip_proto, int dir)
  4711. {
  4712. struct block *b0, *b1, *tmp;
  4713. /*
  4714. * ether proto ip
  4715. *
  4716. * For FDDI, RFC 1188 says that SNAP encapsulation is used,
  4717. * not LLC encapsulation with LLCSAP_IP.
  4718. *
  4719. * For IEEE 802 networks - which includes 802.5 token ring
  4720. * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
  4721. * says that SNAP encapsulation is used, not LLC encapsulation
  4722. * with LLCSAP_IP.
  4723. *
  4724. * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
  4725. * RFC 2225 say that SNAP encapsulation is used, not LLC
  4726. * encapsulation with LLCSAP_IP.
  4727. *
  4728. * So we always check for ETHERTYPE_IP.
  4729. */
  4730. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  4731. switch (ip_proto) {
  4732. case IPPROTO_UDP:
  4733. case IPPROTO_TCP:
  4734. case IPPROTO_SCTP:
  4735. b1 = gen_portop(cstate, port, ip_proto, dir);
  4736. break;
  4737. case PROTO_UNDEF:
  4738. tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
  4739. b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
  4740. gen_or(tmp, b1);
  4741. tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
  4742. gen_or(tmp, b1);
  4743. break;
  4744. default:
  4745. abort();
  4746. }
  4747. gen_and(b0, b1);
  4748. return b1;
  4749. }
  4750. struct block *
  4751. gen_portop6(compiler_state_t *cstate, int port, int proto, int dir)
  4752. {
  4753. struct block *b0, *b1, *tmp;
  4754. /* ip6 proto 'proto' */
  4755. /* XXX - catch the first fragment of a fragmented packet? */
  4756. b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)proto);
  4757. switch (dir) {
  4758. case Q_SRC:
  4759. b1 = gen_portatom6(cstate, 0, (bpf_int32)port);
  4760. break;
  4761. case Q_DST:
  4762. b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
  4763. break;
  4764. case Q_OR:
  4765. case Q_DEFAULT:
  4766. tmp = gen_portatom6(cstate, 0, (bpf_int32)port);
  4767. b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
  4768. gen_or(tmp, b1);
  4769. break;
  4770. case Q_AND:
  4771. tmp = gen_portatom6(cstate, 0, (bpf_int32)port);
  4772. b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
  4773. gen_and(tmp, b1);
  4774. break;
  4775. default:
  4776. abort();
  4777. }
  4778. gen_and(b0, b1);
  4779. return b1;
  4780. }
  4781. static struct block *
  4782. gen_port6(compiler_state_t *cstate, int port, int ip_proto, int dir)
  4783. {
  4784. struct block *b0, *b1, *tmp;
  4785. /* link proto ip6 */
  4786. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  4787. switch (ip_proto) {
  4788. case IPPROTO_UDP:
  4789. case IPPROTO_TCP:
  4790. case IPPROTO_SCTP:
  4791. b1 = gen_portop6(cstate, port, ip_proto, dir);
  4792. break;
  4793. case PROTO_UNDEF:
  4794. tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
  4795. b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
  4796. gen_or(tmp, b1);
  4797. tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
  4798. gen_or(tmp, b1);
  4799. break;
  4800. default:
  4801. abort();
  4802. }
  4803. gen_and(b0, b1);
  4804. return b1;
  4805. }
  4806. /* gen_portrange code */
  4807. static struct block *
  4808. gen_portrangeatom(compiler_state_t *cstate, int off, bpf_int32 v1,
  4809. bpf_int32 v2)
  4810. {
  4811. struct block *b1, *b2;
  4812. if (v1 > v2) {
  4813. /*
  4814. * Reverse the order of the ports, so v1 is the lower one.
  4815. */
  4816. bpf_int32 vtemp;
  4817. vtemp = v1;
  4818. v1 = v2;
  4819. v2 = vtemp;
  4820. }
  4821. b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
  4822. b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
  4823. gen_and(b1, b2);
  4824. return b2;
  4825. }
  4826. struct block *
  4827. gen_portrangeop(compiler_state_t *cstate, int port1, int port2, int proto,
  4828. int dir)
  4829. {
  4830. struct block *b0, *b1, *tmp;
  4831. /* ip proto 'proto' and not a fragment other than the first fragment */
  4832. tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)proto);
  4833. b0 = gen_ipfrag(cstate);
  4834. gen_and(tmp, b0);
  4835. switch (dir) {
  4836. case Q_SRC:
  4837. b1 = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
  4838. break;
  4839. case Q_DST:
  4840. b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
  4841. break;
  4842. case Q_OR:
  4843. case Q_DEFAULT:
  4844. tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
  4845. b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
  4846. gen_or(tmp, b1);
  4847. break;
  4848. case Q_AND:
  4849. tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
  4850. b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
  4851. gen_and(tmp, b1);
  4852. break;
  4853. default:
  4854. abort();
  4855. }
  4856. gen_and(b0, b1);
  4857. return b1;
  4858. }
  4859. static struct block *
  4860. gen_portrange(compiler_state_t *cstate, int port1, int port2, int ip_proto,
  4861. int dir)
  4862. {
  4863. struct block *b0, *b1, *tmp;
  4864. /* link proto ip */
  4865. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  4866. switch (ip_proto) {
  4867. case IPPROTO_UDP:
  4868. case IPPROTO_TCP:
  4869. case IPPROTO_SCTP:
  4870. b1 = gen_portrangeop(cstate, port1, port2, ip_proto, dir);
  4871. break;
  4872. case PROTO_UNDEF:
  4873. tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
  4874. b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
  4875. gen_or(tmp, b1);
  4876. tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
  4877. gen_or(tmp, b1);
  4878. break;
  4879. default:
  4880. abort();
  4881. }
  4882. gen_and(b0, b1);
  4883. return b1;
  4884. }
  4885. static struct block *
  4886. gen_portrangeatom6(compiler_state_t *cstate, int off, bpf_int32 v1,
  4887. bpf_int32 v2)
  4888. {
  4889. struct block *b1, *b2;
  4890. if (v1 > v2) {
  4891. /*
  4892. * Reverse the order of the ports, so v1 is the lower one.
  4893. */
  4894. bpf_int32 vtemp;
  4895. vtemp = v1;
  4896. v1 = v2;
  4897. v2 = vtemp;
  4898. }
  4899. b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
  4900. b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
  4901. gen_and(b1, b2);
  4902. return b2;
  4903. }
  4904. struct block *
  4905. gen_portrangeop6(compiler_state_t *cstate, int port1, int port2, int proto,
  4906. int dir)
  4907. {
  4908. struct block *b0, *b1, *tmp;
  4909. /* ip6 proto 'proto' */
  4910. /* XXX - catch the first fragment of a fragmented packet? */
  4911. b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)proto);
  4912. switch (dir) {
  4913. case Q_SRC:
  4914. b1 = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
  4915. break;
  4916. case Q_DST:
  4917. b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
  4918. break;
  4919. case Q_OR:
  4920. case Q_DEFAULT:
  4921. tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
  4922. b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
  4923. gen_or(tmp, b1);
  4924. break;
  4925. case Q_AND:
  4926. tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
  4927. b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
  4928. gen_and(tmp, b1);
  4929. break;
  4930. default:
  4931. abort();
  4932. }
  4933. gen_and(b0, b1);
  4934. return b1;
  4935. }
  4936. static struct block *
  4937. gen_portrange6(compiler_state_t *cstate, int port1, int port2, int ip_proto,
  4938. int dir)
  4939. {
  4940. struct block *b0, *b1, *tmp;
  4941. /* link proto ip6 */
  4942. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  4943. switch (ip_proto) {
  4944. case IPPROTO_UDP:
  4945. case IPPROTO_TCP:
  4946. case IPPROTO_SCTP:
  4947. b1 = gen_portrangeop6(cstate, port1, port2, ip_proto, dir);
  4948. break;
  4949. case PROTO_UNDEF:
  4950. tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
  4951. b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
  4952. gen_or(tmp, b1);
  4953. tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
  4954. gen_or(tmp, b1);
  4955. break;
  4956. default:
  4957. abort();
  4958. }
  4959. gen_and(b0, b1);
  4960. return b1;
  4961. }
  4962. static int
  4963. lookup_proto(compiler_state_t *cstate, const char *name, int proto)
  4964. {
  4965. register int v;
  4966. switch (proto) {
  4967. case Q_DEFAULT:
  4968. case Q_IP:
  4969. case Q_IPV6:
  4970. v = pcap_nametoproto(name);
  4971. if (v == PROTO_UNDEF)
  4972. bpf_error(cstate, "unknown ip proto '%s'", name);
  4973. break;
  4974. case Q_LINK:
  4975. /* XXX should look up h/w protocol type based on cstate->linktype */
  4976. v = pcap_nametoeproto(name);
  4977. if (v == PROTO_UNDEF) {
  4978. v = pcap_nametollc(name);
  4979. if (v == PROTO_UNDEF)
  4980. bpf_error(cstate, "unknown ether proto '%s'", name);
  4981. }
  4982. break;
  4983. case Q_ISO:
  4984. if (strcmp(name, "esis") == 0)
  4985. v = ISO9542_ESIS;
  4986. else if (strcmp(name, "isis") == 0)
  4987. v = ISO10589_ISIS;
  4988. else if (strcmp(name, "clnp") == 0)
  4989. v = ISO8473_CLNP;
  4990. else
  4991. bpf_error(cstate, "unknown osi proto '%s'", name);
  4992. break;
  4993. default:
  4994. v = PROTO_UNDEF;
  4995. break;
  4996. }
  4997. return v;
  4998. }
  4999. #if 0
  5000. struct stmt *
  5001. gen_joinsp(struct stmt **s, int n)
  5002. {
  5003. return NULL;
  5004. }
  5005. #endif
  5006. static struct block *
  5007. gen_protochain(compiler_state_t *cstate, int v, int proto, int dir)
  5008. {
  5009. #ifdef NO_PROTOCHAIN
  5010. return gen_proto(cstate, v, proto, dir);
  5011. #else
  5012. struct block *b0, *b;
  5013. struct slist *s[100];
  5014. int fix2, fix3, fix4, fix5;
  5015. int ahcheck, again, end;
  5016. int i, max;
  5017. int reg2 = alloc_reg(cstate);
  5018. memset(s, 0, sizeof(s));
  5019. fix3 = fix4 = fix5 = 0;
  5020. switch (proto) {
  5021. case Q_IP:
  5022. case Q_IPV6:
  5023. break;
  5024. case Q_DEFAULT:
  5025. b0 = gen_protochain(cstate, v, Q_IP, dir);
  5026. b = gen_protochain(cstate, v, Q_IPV6, dir);
  5027. gen_or(b0, b);
  5028. return b;
  5029. default:
  5030. bpf_error(cstate, "bad protocol applied for 'protochain'");
  5031. /*NOTREACHED*/
  5032. }
  5033. /*
  5034. * We don't handle variable-length prefixes before the link-layer
  5035. * header, or variable-length link-layer headers, here yet.
  5036. * We might want to add BPF instructions to do the protochain
  5037. * work, to simplify that and, on platforms that have a BPF
  5038. * interpreter with the new instructions, let the filtering
  5039. * be done in the kernel. (We already require a modified BPF
  5040. * engine to do the protochain stuff, to support backward
  5041. * branches, and backward branch support is unlikely to appear
  5042. * in kernel BPF engines.)
  5043. */
  5044. if (cstate->off_linkpl.is_variable)
  5045. bpf_error(cstate, "'protochain' not supported with variable length headers");
  5046. cstate->no_optimize = 1; /* this code is not compatible with optimizer yet */
  5047. /*
  5048. * s[0] is a dummy entry to protect other BPF insn from damage
  5049. * by s[fix] = foo with uninitialized variable "fix". It is somewhat
  5050. * hard to find interdependency made by jump table fixup.
  5051. */
  5052. i = 0;
  5053. s[i] = new_stmt(cstate, 0); /*dummy*/
  5054. i++;
  5055. switch (proto) {
  5056. case Q_IP:
  5057. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  5058. /* A = ip->ip_p */
  5059. s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  5060. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
  5061. i++;
  5062. /* X = ip->ip_hl << 2 */
  5063. s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
  5064. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5065. i++;
  5066. break;
  5067. case Q_IPV6:
  5068. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  5069. /* A = ip6->ip_nxt */
  5070. s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  5071. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
  5072. i++;
  5073. /* X = sizeof(struct ip6_hdr) */
  5074. s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
  5075. s[i]->s.k = 40;
  5076. i++;
  5077. break;
  5078. default:
  5079. bpf_error(cstate, "unsupported proto to gen_protochain");
  5080. /*NOTREACHED*/
  5081. }
  5082. /* again: if (A == v) goto end; else fall through; */
  5083. again = i;
  5084. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5085. s[i]->s.k = v;
  5086. s[i]->s.jt = NULL; /*later*/
  5087. s[i]->s.jf = NULL; /*update in next stmt*/
  5088. fix5 = i;
  5089. i++;
  5090. #ifndef IPPROTO_NONE
  5091. #define IPPROTO_NONE 59
  5092. #endif
  5093. /* if (A == IPPROTO_NONE) goto end */
  5094. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5095. s[i]->s.jt = NULL; /*later*/
  5096. s[i]->s.jf = NULL; /*update in next stmt*/
  5097. s[i]->s.k = IPPROTO_NONE;
  5098. s[fix5]->s.jf = s[i];
  5099. fix2 = i;
  5100. i++;
  5101. if (proto == Q_IPV6) {
  5102. int v6start, v6end, v6advance, j;
  5103. v6start = i;
  5104. /* if (A == IPPROTO_HOPOPTS) goto v6advance */
  5105. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5106. s[i]->s.jt = NULL; /*later*/
  5107. s[i]->s.jf = NULL; /*update in next stmt*/
  5108. s[i]->s.k = IPPROTO_HOPOPTS;
  5109. s[fix2]->s.jf = s[i];
  5110. i++;
  5111. /* if (A == IPPROTO_DSTOPTS) goto v6advance */
  5112. s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5113. s[i]->s.jt = NULL; /*later*/
  5114. s[i]->s.jf = NULL; /*update in next stmt*/
  5115. s[i]->s.k = IPPROTO_DSTOPTS;
  5116. i++;
  5117. /* if (A == IPPROTO_ROUTING) goto v6advance */
  5118. s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5119. s[i]->s.jt = NULL; /*later*/
  5120. s[i]->s.jf = NULL; /*update in next stmt*/
  5121. s[i]->s.k = IPPROTO_ROUTING;
  5122. i++;
  5123. /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
  5124. s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5125. s[i]->s.jt = NULL; /*later*/
  5126. s[i]->s.jf = NULL; /*later*/
  5127. s[i]->s.k = IPPROTO_FRAGMENT;
  5128. fix3 = i;
  5129. v6end = i;
  5130. i++;
  5131. /* v6advance: */
  5132. v6advance = i;
  5133. /*
  5134. * in short,
  5135. * A = P[X + packet head];
  5136. * X = X + (P[X + packet head + 1] + 1) * 8;
  5137. */
  5138. /* A = P[X + packet head] */
  5139. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5140. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5141. i++;
  5142. /* MEM[reg2] = A */
  5143. s[i] = new_stmt(cstate, BPF_ST);
  5144. s[i]->s.k = reg2;
  5145. i++;
  5146. /* A = P[X + packet head + 1]; */
  5147. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5148. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
  5149. i++;
  5150. /* A += 1 */
  5151. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5152. s[i]->s.k = 1;
  5153. i++;
  5154. /* A *= 8 */
  5155. s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
  5156. s[i]->s.k = 8;
  5157. i++;
  5158. /* A += X */
  5159. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
  5160. s[i]->s.k = 0;
  5161. i++;
  5162. /* X = A; */
  5163. s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
  5164. i++;
  5165. /* A = MEM[reg2] */
  5166. s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
  5167. s[i]->s.k = reg2;
  5168. i++;
  5169. /* goto again; (must use BPF_JA for backward jump) */
  5170. s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
  5171. s[i]->s.k = again - i - 1;
  5172. s[i - 1]->s.jf = s[i];
  5173. i++;
  5174. /* fixup */
  5175. for (j = v6start; j <= v6end; j++)
  5176. s[j]->s.jt = s[v6advance];
  5177. } else {
  5178. /* nop */
  5179. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5180. s[i]->s.k = 0;
  5181. s[fix2]->s.jf = s[i];
  5182. i++;
  5183. }
  5184. /* ahcheck: */
  5185. ahcheck = i;
  5186. /* if (A == IPPROTO_AH) then fall through; else goto end; */
  5187. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5188. s[i]->s.jt = NULL; /*later*/
  5189. s[i]->s.jf = NULL; /*later*/
  5190. s[i]->s.k = IPPROTO_AH;
  5191. if (fix3)
  5192. s[fix3]->s.jf = s[ahcheck];
  5193. fix4 = i;
  5194. i++;
  5195. /*
  5196. * in short,
  5197. * A = P[X];
  5198. * X = X + (P[X + 1] + 2) * 4;
  5199. */
  5200. /* A = X */
  5201. s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
  5202. i++;
  5203. /* A = P[X + packet head]; */
  5204. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5205. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5206. i++;
  5207. /* MEM[reg2] = A */
  5208. s[i] = new_stmt(cstate, BPF_ST);
  5209. s[i]->s.k = reg2;
  5210. i++;
  5211. /* A = X */
  5212. s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
  5213. i++;
  5214. /* A += 1 */
  5215. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5216. s[i]->s.k = 1;
  5217. i++;
  5218. /* X = A */
  5219. s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
  5220. i++;
  5221. /* A = P[X + packet head] */
  5222. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5223. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5224. i++;
  5225. /* A += 2 */
  5226. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5227. s[i]->s.k = 2;
  5228. i++;
  5229. /* A *= 4 */
  5230. s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
  5231. s[i]->s.k = 4;
  5232. i++;
  5233. /* X = A; */
  5234. s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
  5235. i++;
  5236. /* A = MEM[reg2] */
  5237. s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
  5238. s[i]->s.k = reg2;
  5239. i++;
  5240. /* goto again; (must use BPF_JA for backward jump) */
  5241. s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
  5242. s[i]->s.k = again - i - 1;
  5243. i++;
  5244. /* end: nop */
  5245. end = i;
  5246. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5247. s[i]->s.k = 0;
  5248. s[fix2]->s.jt = s[end];
  5249. s[fix4]->s.jf = s[end];
  5250. s[fix5]->s.jt = s[end];
  5251. i++;
  5252. /*
  5253. * make slist chain
  5254. */
  5255. max = i;
  5256. for (i = 0; i < max - 1; i++)
  5257. s[i]->next = s[i + 1];
  5258. s[max - 1]->next = NULL;
  5259. /*
  5260. * emit final check
  5261. */
  5262. b = new_block(cstate, JMP(BPF_JEQ));
  5263. b->stmts = s[1]; /*remember, s[0] is dummy*/
  5264. b->s.k = v;
  5265. free_reg(cstate, reg2);
  5266. gen_and(b0, b);
  5267. return b;
  5268. #endif
  5269. }
  5270. static struct block *
  5271. gen_check_802_11_data_frame(compiler_state_t *cstate)
  5272. {
  5273. struct slist *s;
  5274. struct block *b0, *b1;
  5275. /*
  5276. * A data frame has the 0x08 bit (b3) in the frame control field set
  5277. * and the 0x04 bit (b2) clear.
  5278. */
  5279. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  5280. b0 = new_block(cstate, JMP(BPF_JSET));
  5281. b0->s.k = 0x08;
  5282. b0->stmts = s;
  5283. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  5284. b1 = new_block(cstate, JMP(BPF_JSET));
  5285. b1->s.k = 0x04;
  5286. b1->stmts = s;
  5287. gen_not(b1);
  5288. gen_and(b1, b0);
  5289. return b0;
  5290. }
  5291. /*
  5292. * Generate code that checks whether the packet is a packet for protocol
  5293. * <proto> and whether the type field in that protocol's header has
  5294. * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
  5295. * IP packet and checks the protocol number in the IP header against <v>.
  5296. *
  5297. * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
  5298. * against Q_IP and Q_IPV6.
  5299. */
  5300. static struct block *
  5301. gen_proto(compiler_state_t *cstate, int v, int proto, int dir)
  5302. {
  5303. struct block *b0, *b1;
  5304. #ifndef CHASE_CHAIN
  5305. struct block *b2;
  5306. #endif
  5307. if (dir != Q_DEFAULT)
  5308. bpf_error(cstate, "direction applied to 'proto'");
  5309. switch (proto) {
  5310. case Q_DEFAULT:
  5311. b0 = gen_proto(cstate, v, Q_IP, dir);
  5312. b1 = gen_proto(cstate, v, Q_IPV6, dir);
  5313. gen_or(b0, b1);
  5314. return b1;
  5315. case Q_IP:
  5316. /*
  5317. * For FDDI, RFC 1188 says that SNAP encapsulation is used,
  5318. * not LLC encapsulation with LLCSAP_IP.
  5319. *
  5320. * For IEEE 802 networks - which includes 802.5 token ring
  5321. * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
  5322. * says that SNAP encapsulation is used, not LLC encapsulation
  5323. * with LLCSAP_IP.
  5324. *
  5325. * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
  5326. * RFC 2225 say that SNAP encapsulation is used, not LLC
  5327. * encapsulation with LLCSAP_IP.
  5328. *
  5329. * So we always check for ETHERTYPE_IP.
  5330. */
  5331. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  5332. #ifndef CHASE_CHAIN
  5333. b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)v);
  5334. #else
  5335. b1 = gen_protochain(cstate, v, Q_IP);
  5336. #endif
  5337. gen_and(b0, b1);
  5338. return b1;
  5339. case Q_ISO:
  5340. switch (cstate->linktype) {
  5341. case DLT_FRELAY:
  5342. /*
  5343. * Frame Relay packets typically have an OSI
  5344. * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
  5345. * generates code to check for all the OSI
  5346. * NLPIDs, so calling it and then adding a check
  5347. * for the particular NLPID for which we're
  5348. * looking is bogus, as we can just check for
  5349. * the NLPID.
  5350. *
  5351. * What we check for is the NLPID and a frame
  5352. * control field value of UI, i.e. 0x03 followed
  5353. * by the NLPID.
  5354. *
  5355. * XXX - assumes a 2-byte Frame Relay header with
  5356. * DLCI and flags. What if the address is longer?
  5357. *
  5358. * XXX - what about SNAP-encapsulated frames?
  5359. */
  5360. return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
  5361. /*NOTREACHED*/
  5362. break;
  5363. case DLT_C_HDLC:
  5364. /*
  5365. * Cisco uses an Ethertype lookalike - for OSI,
  5366. * it's 0xfefe.
  5367. */
  5368. b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
  5369. /* OSI in C-HDLC is stuffed with a fudge byte */
  5370. b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (long)v);
  5371. gen_and(b0, b1);
  5372. return b1;
  5373. default:
  5374. b0 = gen_linktype(cstate, LLCSAP_ISONS);
  5375. b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (long)v);
  5376. gen_and(b0, b1);
  5377. return b1;
  5378. }
  5379. case Q_ISIS:
  5380. b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
  5381. /*
  5382. * 4 is the offset of the PDU type relative to the IS-IS
  5383. * header.
  5384. */
  5385. b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (long)v);
  5386. gen_and(b0, b1);
  5387. return b1;
  5388. case Q_ARP:
  5389. bpf_error(cstate, "arp does not encapsulate another protocol");
  5390. /* NOTREACHED */
  5391. case Q_RARP:
  5392. bpf_error(cstate, "rarp does not encapsulate another protocol");
  5393. /* NOTREACHED */
  5394. case Q_ATALK:
  5395. bpf_error(cstate, "atalk encapsulation is not specifiable");
  5396. /* NOTREACHED */
  5397. case Q_DECNET:
  5398. bpf_error(cstate, "decnet encapsulation is not specifiable");
  5399. /* NOTREACHED */
  5400. case Q_SCA:
  5401. bpf_error(cstate, "sca does not encapsulate another protocol");
  5402. /* NOTREACHED */
  5403. case Q_LAT:
  5404. bpf_error(cstate, "lat does not encapsulate another protocol");
  5405. /* NOTREACHED */
  5406. case Q_MOPRC:
  5407. bpf_error(cstate, "moprc does not encapsulate another protocol");
  5408. /* NOTREACHED */
  5409. case Q_MOPDL:
  5410. bpf_error(cstate, "mopdl does not encapsulate another protocol");
  5411. /* NOTREACHED */
  5412. case Q_LINK:
  5413. return gen_linktype(cstate, v);
  5414. case Q_UDP:
  5415. bpf_error(cstate, "'udp proto' is bogus");
  5416. /* NOTREACHED */
  5417. case Q_TCP:
  5418. bpf_error(cstate, "'tcp proto' is bogus");
  5419. /* NOTREACHED */
  5420. case Q_SCTP:
  5421. bpf_error(cstate, "'sctp proto' is bogus");
  5422. /* NOTREACHED */
  5423. case Q_ICMP:
  5424. bpf_error(cstate, "'icmp proto' is bogus");
  5425. /* NOTREACHED */
  5426. case Q_IGMP:
  5427. bpf_error(cstate, "'igmp proto' is bogus");
  5428. /* NOTREACHED */
  5429. case Q_IGRP:
  5430. bpf_error(cstate, "'igrp proto' is bogus");
  5431. /* NOTREACHED */
  5432. case Q_PIM:
  5433. bpf_error(cstate, "'pim proto' is bogus");
  5434. /* NOTREACHED */
  5435. case Q_VRRP:
  5436. bpf_error(cstate, "'vrrp proto' is bogus");
  5437. /* NOTREACHED */
  5438. case Q_CARP:
  5439. bpf_error(cstate, "'carp proto' is bogus");
  5440. /* NOTREACHED */
  5441. case Q_IPV6:
  5442. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  5443. #ifndef CHASE_CHAIN
  5444. /*
  5445. * Also check for a fragment header before the final
  5446. * header.
  5447. */
  5448. b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
  5449. b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, (bpf_int32)v);
  5450. gen_and(b2, b1);
  5451. b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)v);
  5452. gen_or(b2, b1);
  5453. #else
  5454. b1 = gen_protochain(cstate, v, Q_IPV6);
  5455. #endif
  5456. gen_and(b0, b1);
  5457. return b1;
  5458. case Q_ICMPV6:
  5459. bpf_error(cstate, "'icmp6 proto' is bogus");
  5460. case Q_AH:
  5461. bpf_error(cstate, "'ah proto' is bogus");
  5462. case Q_ESP:
  5463. bpf_error(cstate, "'ah proto' is bogus");
  5464. case Q_STP:
  5465. bpf_error(cstate, "'stp proto' is bogus");
  5466. case Q_IPX:
  5467. bpf_error(cstate, "'ipx proto' is bogus");
  5468. case Q_NETBEUI:
  5469. bpf_error(cstate, "'netbeui proto' is bogus");
  5470. case Q_RADIO:
  5471. bpf_error(cstate, "'radio proto' is bogus");
  5472. default:
  5473. abort();
  5474. /* NOTREACHED */
  5475. }
  5476. /* NOTREACHED */
  5477. }
  5478. struct block *
  5479. gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
  5480. {
  5481. int proto = q.proto;
  5482. int dir = q.dir;
  5483. int tproto;
  5484. u_char *eaddr;
  5485. bpf_u_int32 mask, addr;
  5486. struct addrinfo *res, *res0;
  5487. struct sockaddr_in *sin4;
  5488. #ifdef INET6
  5489. int tproto6;
  5490. struct sockaddr_in6 *sin6;
  5491. struct in6_addr mask128;
  5492. #endif /*INET6*/
  5493. struct block *b, *tmp;
  5494. int port, real_proto;
  5495. int port1, port2;
  5496. switch (q.addr) {
  5497. case Q_NET:
  5498. addr = pcap_nametonetaddr(name);
  5499. if (addr == 0)
  5500. bpf_error(cstate, "unknown network '%s'", name);
  5501. /* Left justify network addr and calculate its network mask */
  5502. mask = 0xffffffff;
  5503. while (addr && (addr & 0xff000000) == 0) {
  5504. addr <<= 8;
  5505. mask <<= 8;
  5506. }
  5507. return gen_host(cstate, addr, mask, proto, dir, q.addr);
  5508. case Q_DEFAULT:
  5509. case Q_HOST:
  5510. if (proto == Q_LINK) {
  5511. switch (cstate->linktype) {
  5512. case DLT_EN10MB:
  5513. case DLT_NETANALYZER:
  5514. case DLT_NETANALYZER_TRANSPARENT:
  5515. eaddr = pcap_ether_hostton(name);
  5516. if (eaddr == NULL)
  5517. bpf_error(cstate,
  5518. "unknown ether host '%s'", name);
  5519. tmp = gen_prevlinkhdr_check(cstate);
  5520. b = gen_ehostop(cstate, eaddr, dir);
  5521. if (tmp != NULL)
  5522. gen_and(tmp, b);
  5523. free(eaddr);
  5524. return b;
  5525. case DLT_FDDI:
  5526. eaddr = pcap_ether_hostton(name);
  5527. if (eaddr == NULL)
  5528. bpf_error(cstate,
  5529. "unknown FDDI host '%s'", name);
  5530. b = gen_fhostop(cstate, eaddr, dir);
  5531. free(eaddr);
  5532. return b;
  5533. case DLT_IEEE802:
  5534. eaddr = pcap_ether_hostton(name);
  5535. if (eaddr == NULL)
  5536. bpf_error(cstate,
  5537. "unknown token ring host '%s'", name);
  5538. b = gen_thostop(cstate, eaddr, dir);
  5539. free(eaddr);
  5540. return b;
  5541. case DLT_IEEE802_11:
  5542. case DLT_PRISM_HEADER:
  5543. case DLT_IEEE802_11_RADIO_AVS:
  5544. case DLT_IEEE802_11_RADIO:
  5545. case DLT_PPI:
  5546. eaddr = pcap_ether_hostton(name);
  5547. if (eaddr == NULL)
  5548. bpf_error(cstate,
  5549. "unknown 802.11 host '%s'", name);
  5550. b = gen_wlanhostop(cstate, eaddr, dir);
  5551. free(eaddr);
  5552. return b;
  5553. case DLT_IP_OVER_FC:
  5554. eaddr = pcap_ether_hostton(name);
  5555. if (eaddr == NULL)
  5556. bpf_error(cstate,
  5557. "unknown Fibre Channel host '%s'", name);
  5558. b = gen_ipfchostop(cstate, eaddr, dir);
  5559. free(eaddr);
  5560. return b;
  5561. }
  5562. bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
  5563. } else if (proto == Q_DECNET) {
  5564. unsigned short dn_addr;
  5565. if (!__pcap_nametodnaddr(name, &dn_addr)) {
  5566. #ifdef DECNETLIB
  5567. bpf_error(cstate, "unknown decnet host name '%s'\n", name);
  5568. #else
  5569. bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
  5570. name);
  5571. #endif
  5572. }
  5573. /*
  5574. * I don't think DECNET hosts can be multihomed, so
  5575. * there is no need to build up a list of addresses
  5576. */
  5577. return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
  5578. } else {
  5579. #ifdef INET6
  5580. memset(&mask128, 0xff, sizeof(mask128));
  5581. #endif
  5582. res0 = res = pcap_nametoaddrinfo(name);
  5583. if (res == NULL)
  5584. bpf_error(cstate, "unknown host '%s'", name);
  5585. cstate->ai = res;
  5586. b = tmp = NULL;
  5587. tproto = proto;
  5588. #ifdef INET6
  5589. tproto6 = proto;
  5590. #endif
  5591. if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
  5592. tproto == Q_DEFAULT) {
  5593. tproto = Q_IP;
  5594. #ifdef INET6
  5595. tproto6 = Q_IPV6;
  5596. #endif
  5597. }
  5598. for (res = res0; res; res = res->ai_next) {
  5599. switch (res->ai_family) {
  5600. case AF_INET:
  5601. #ifdef INET6
  5602. if (tproto == Q_IPV6)
  5603. continue;
  5604. #endif
  5605. sin4 = (struct sockaddr_in *)
  5606. res->ai_addr;
  5607. tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
  5608. 0xffffffff, tproto, dir, q.addr);
  5609. break;
  5610. #ifdef INET6
  5611. case AF_INET6:
  5612. if (tproto6 == Q_IP)
  5613. continue;
  5614. sin6 = (struct sockaddr_in6 *)
  5615. res->ai_addr;
  5616. tmp = gen_host6(cstate, &sin6->sin6_addr,
  5617. &mask128, tproto6, dir, q.addr);
  5618. break;
  5619. #endif
  5620. default:
  5621. continue;
  5622. }
  5623. if (b)
  5624. gen_or(b, tmp);
  5625. b = tmp;
  5626. }
  5627. cstate->ai = NULL;
  5628. freeaddrinfo(res0);
  5629. if (b == NULL) {
  5630. bpf_error(cstate, "unknown host '%s'%s", name,
  5631. (proto == Q_DEFAULT)
  5632. ? ""
  5633. : " for specified address family");
  5634. }
  5635. return b;
  5636. }
  5637. case Q_PORT:
  5638. if (proto != Q_DEFAULT &&
  5639. proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
  5640. bpf_error(cstate, "illegal qualifier of 'port'");
  5641. if (pcap_nametoport(name, &port, &real_proto) == 0)
  5642. bpf_error(cstate, "unknown port '%s'", name);
  5643. if (proto == Q_UDP) {
  5644. if (real_proto == IPPROTO_TCP)
  5645. bpf_error(cstate, "port '%s' is tcp", name);
  5646. else if (real_proto == IPPROTO_SCTP)
  5647. bpf_error(cstate, "port '%s' is sctp", name);
  5648. else
  5649. /* override PROTO_UNDEF */
  5650. real_proto = IPPROTO_UDP;
  5651. }
  5652. if (proto == Q_TCP) {
  5653. if (real_proto == IPPROTO_UDP)
  5654. bpf_error(cstate, "port '%s' is udp", name);
  5655. else if (real_proto == IPPROTO_SCTP)
  5656. bpf_error(cstate, "port '%s' is sctp", name);
  5657. else
  5658. /* override PROTO_UNDEF */
  5659. real_proto = IPPROTO_TCP;
  5660. }
  5661. if (proto == Q_SCTP) {
  5662. if (real_proto == IPPROTO_UDP)
  5663. bpf_error(cstate, "port '%s' is udp", name);
  5664. else if (real_proto == IPPROTO_TCP)
  5665. bpf_error(cstate, "port '%s' is tcp", name);
  5666. else
  5667. /* override PROTO_UNDEF */
  5668. real_proto = IPPROTO_SCTP;
  5669. }
  5670. if (port < 0)
  5671. bpf_error(cstate, "illegal port number %d < 0", port);
  5672. if (port > 65535)
  5673. bpf_error(cstate, "illegal port number %d > 65535", port);
  5674. b = gen_port(cstate, port, real_proto, dir);
  5675. gen_or(gen_port6(cstate, port, real_proto, dir), b);
  5676. return b;
  5677. case Q_PORTRANGE:
  5678. if (proto != Q_DEFAULT &&
  5679. proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
  5680. bpf_error(cstate, "illegal qualifier of 'portrange'");
  5681. if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
  5682. bpf_error(cstate, "unknown port in range '%s'", name);
  5683. if (proto == Q_UDP) {
  5684. if (real_proto == IPPROTO_TCP)
  5685. bpf_error(cstate, "port in range '%s' is tcp", name);
  5686. else if (real_proto == IPPROTO_SCTP)
  5687. bpf_error(cstate, "port in range '%s' is sctp", name);
  5688. else
  5689. /* override PROTO_UNDEF */
  5690. real_proto = IPPROTO_UDP;
  5691. }
  5692. if (proto == Q_TCP) {
  5693. if (real_proto == IPPROTO_UDP)
  5694. bpf_error(cstate, "port in range '%s' is udp", name);
  5695. else if (real_proto == IPPROTO_SCTP)
  5696. bpf_error(cstate, "port in range '%s' is sctp", name);
  5697. else
  5698. /* override PROTO_UNDEF */
  5699. real_proto = IPPROTO_TCP;
  5700. }
  5701. if (proto == Q_SCTP) {
  5702. if (real_proto == IPPROTO_UDP)
  5703. bpf_error(cstate, "port in range '%s' is udp", name);
  5704. else if (real_proto == IPPROTO_TCP)
  5705. bpf_error(cstate, "port in range '%s' is tcp", name);
  5706. else
  5707. /* override PROTO_UNDEF */
  5708. real_proto = IPPROTO_SCTP;
  5709. }
  5710. if (port1 < 0)
  5711. bpf_error(cstate, "illegal port number %d < 0", port1);
  5712. if (port1 > 65535)
  5713. bpf_error(cstate, "illegal port number %d > 65535", port1);
  5714. if (port2 < 0)
  5715. bpf_error(cstate, "illegal port number %d < 0", port2);
  5716. if (port2 > 65535)
  5717. bpf_error(cstate, "illegal port number %d > 65535", port2);
  5718. b = gen_portrange(cstate, port1, port2, real_proto, dir);
  5719. gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
  5720. return b;
  5721. case Q_GATEWAY:
  5722. #ifndef INET6
  5723. eaddr = pcap_ether_hostton(name);
  5724. if (eaddr == NULL)
  5725. bpf_error(cstate, "unknown ether host: %s", name);
  5726. res = pcap_nametoaddrinfo(name);
  5727. cstate->ai = res;
  5728. if (res == NULL)
  5729. bpf_error(cstate, "unknown host '%s'", name);
  5730. b = gen_gateway(cstate, eaddr, res, proto, dir);
  5731. cstate->ai = NULL;
  5732. freeaddrinfo(res);
  5733. if (b == NULL)
  5734. bpf_error(cstate, "unknown host '%s'", name);
  5735. return b;
  5736. #else
  5737. bpf_error(cstate, "'gateway' not supported in this configuration");
  5738. #endif /*INET6*/
  5739. case Q_PROTO:
  5740. real_proto = lookup_proto(cstate, name, proto);
  5741. if (real_proto >= 0)
  5742. return gen_proto(cstate, real_proto, proto, dir);
  5743. else
  5744. bpf_error(cstate, "unknown protocol: %s", name);
  5745. case Q_PROTOCHAIN:
  5746. real_proto = lookup_proto(cstate, name, proto);
  5747. if (real_proto >= 0)
  5748. return gen_protochain(cstate, real_proto, proto, dir);
  5749. else
  5750. bpf_error(cstate, "unknown protocol: %s", name);
  5751. case Q_UNDEF:
  5752. syntax(cstate);
  5753. /* NOTREACHED */
  5754. }
  5755. abort();
  5756. /* NOTREACHED */
  5757. }
  5758. struct block *
  5759. gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
  5760. unsigned int masklen, struct qual q)
  5761. {
  5762. register int nlen, mlen;
  5763. bpf_u_int32 n, m;
  5764. nlen = __pcap_atoin(s1, &n);
  5765. /* Promote short ipaddr */
  5766. n <<= 32 - nlen;
  5767. if (s2 != NULL) {
  5768. mlen = __pcap_atoin(s2, &m);
  5769. /* Promote short ipaddr */
  5770. m <<= 32 - mlen;
  5771. if ((n & ~m) != 0)
  5772. bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
  5773. s1, s2);
  5774. } else {
  5775. /* Convert mask len to mask */
  5776. if (masklen > 32)
  5777. bpf_error(cstate, "mask length must be <= 32");
  5778. if (masklen == 0) {
  5779. /*
  5780. * X << 32 is not guaranteed by C to be 0; it's
  5781. * undefined.
  5782. */
  5783. m = 0;
  5784. } else
  5785. m = 0xffffffff << (32 - masklen);
  5786. if ((n & ~m) != 0)
  5787. bpf_error(cstate, "non-network bits set in \"%s/%d\"",
  5788. s1, masklen);
  5789. }
  5790. switch (q.addr) {
  5791. case Q_NET:
  5792. return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
  5793. default:
  5794. bpf_error(cstate, "Mask syntax for networks only");
  5795. /* NOTREACHED */
  5796. }
  5797. /* NOTREACHED */
  5798. }
  5799. struct block *
  5800. gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
  5801. {
  5802. bpf_u_int32 mask;
  5803. int proto = q.proto;
  5804. int dir = q.dir;
  5805. register int vlen;
  5806. if (s == NULL)
  5807. vlen = 32;
  5808. else if (q.proto == Q_DECNET) {
  5809. vlen = __pcap_atodn(s, &v);
  5810. if (vlen == 0)
  5811. bpf_error(cstate, "malformed decnet address '%s'", s);
  5812. } else
  5813. vlen = __pcap_atoin(s, &v);
  5814. switch (q.addr) {
  5815. case Q_DEFAULT:
  5816. case Q_HOST:
  5817. case Q_NET:
  5818. if (proto == Q_DECNET)
  5819. return gen_host(cstate, v, 0, proto, dir, q.addr);
  5820. else if (proto == Q_LINK) {
  5821. bpf_error(cstate, "illegal link layer address");
  5822. } else {
  5823. mask = 0xffffffff;
  5824. if (s == NULL && q.addr == Q_NET) {
  5825. /* Promote short net number */
  5826. while (v && (v & 0xff000000) == 0) {
  5827. v <<= 8;
  5828. mask <<= 8;
  5829. }
  5830. } else {
  5831. /* Promote short ipaddr */
  5832. v <<= 32 - vlen;
  5833. mask <<= 32 - vlen ;
  5834. }
  5835. return gen_host(cstate, v, mask, proto, dir, q.addr);
  5836. }
  5837. case Q_PORT:
  5838. if (proto == Q_UDP)
  5839. proto = IPPROTO_UDP;
  5840. else if (proto == Q_TCP)
  5841. proto = IPPROTO_TCP;
  5842. else if (proto == Q_SCTP)
  5843. proto = IPPROTO_SCTP;
  5844. else if (proto == Q_DEFAULT)
  5845. proto = PROTO_UNDEF;
  5846. else
  5847. bpf_error(cstate, "illegal qualifier of 'port'");
  5848. if (v > 65535)
  5849. bpf_error(cstate, "illegal port number %u > 65535", v);
  5850. {
  5851. struct block *b;
  5852. b = gen_port(cstate, (int)v, proto, dir);
  5853. gen_or(gen_port6(cstate, (int)v, proto, dir), b);
  5854. return b;
  5855. }
  5856. case Q_PORTRANGE:
  5857. if (proto == Q_UDP)
  5858. proto = IPPROTO_UDP;
  5859. else if (proto == Q_TCP)
  5860. proto = IPPROTO_TCP;
  5861. else if (proto == Q_SCTP)
  5862. proto = IPPROTO_SCTP;
  5863. else if (proto == Q_DEFAULT)
  5864. proto = PROTO_UNDEF;
  5865. else
  5866. bpf_error(cstate, "illegal qualifier of 'portrange'");
  5867. if (v > 65535)
  5868. bpf_error(cstate, "illegal port number %u > 65535", v);
  5869. {
  5870. struct block *b;
  5871. b = gen_portrange(cstate, (int)v, (int)v, proto, dir);
  5872. gen_or(gen_portrange6(cstate, (int)v, (int)v, proto, dir), b);
  5873. return b;
  5874. }
  5875. case Q_GATEWAY:
  5876. bpf_error(cstate, "'gateway' requires a name");
  5877. /* NOTREACHED */
  5878. case Q_PROTO:
  5879. return gen_proto(cstate, (int)v, proto, dir);
  5880. case Q_PROTOCHAIN:
  5881. return gen_protochain(cstate, (int)v, proto, dir);
  5882. case Q_UNDEF:
  5883. syntax(cstate);
  5884. /* NOTREACHED */
  5885. default:
  5886. abort();
  5887. /* NOTREACHED */
  5888. }
  5889. /* NOTREACHED */
  5890. }
  5891. #ifdef INET6
  5892. struct block *
  5893. gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
  5894. unsigned int masklen, struct qual q)
  5895. {
  5896. struct addrinfo *res;
  5897. struct in6_addr *addr;
  5898. struct in6_addr mask;
  5899. struct block *b;
  5900. uint32_t *a, *m;
  5901. if (s2)
  5902. bpf_error(cstate, "no mask %s supported", s2);
  5903. res = pcap_nametoaddrinfo(s1);
  5904. if (!res)
  5905. bpf_error(cstate, "invalid ip6 address %s", s1);
  5906. cstate->ai = res;
  5907. if (res->ai_next)
  5908. bpf_error(cstate, "%s resolved to multiple address", s1);
  5909. addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
  5910. if (sizeof(mask) * 8 < masklen)
  5911. bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
  5912. memset(&mask, 0, sizeof(mask));
  5913. memset(&mask, 0xff, masklen / 8);
  5914. if (masklen % 8) {
  5915. mask.s6_addr[masklen / 8] =
  5916. (0xff << (8 - masklen % 8)) & 0xff;
  5917. }
  5918. a = (uint32_t *)addr;
  5919. m = (uint32_t *)&mask;
  5920. if ((a[0] & ~m[0]) || (a[1] & ~m[1])
  5921. || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
  5922. bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
  5923. }
  5924. switch (q.addr) {
  5925. case Q_DEFAULT:
  5926. case Q_HOST:
  5927. if (masklen != 128)
  5928. bpf_error(cstate, "Mask syntax for networks only");
  5929. /* FALLTHROUGH */
  5930. case Q_NET:
  5931. b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
  5932. cstate->ai = NULL;
  5933. freeaddrinfo(res);
  5934. return b;
  5935. default:
  5936. bpf_error(cstate, "invalid qualifier against IPv6 address");
  5937. /* NOTREACHED */
  5938. }
  5939. }
  5940. #endif /*INET6*/
  5941. struct block *
  5942. gen_ecode(compiler_state_t *cstate, const u_char *eaddr, struct qual q)
  5943. {
  5944. struct block *b, *tmp;
  5945. if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
  5946. switch (cstate->linktype) {
  5947. case DLT_EN10MB:
  5948. case DLT_NETANALYZER:
  5949. case DLT_NETANALYZER_TRANSPARENT:
  5950. tmp = gen_prevlinkhdr_check(cstate);
  5951. b = gen_ehostop(cstate, eaddr, (int)q.dir);
  5952. if (tmp != NULL)
  5953. gen_and(tmp, b);
  5954. return b;
  5955. case DLT_FDDI:
  5956. return gen_fhostop(cstate, eaddr, (int)q.dir);
  5957. case DLT_IEEE802:
  5958. return gen_thostop(cstate, eaddr, (int)q.dir);
  5959. case DLT_IEEE802_11:
  5960. case DLT_PRISM_HEADER:
  5961. case DLT_IEEE802_11_RADIO_AVS:
  5962. case DLT_IEEE802_11_RADIO:
  5963. case DLT_PPI:
  5964. return gen_wlanhostop(cstate, eaddr, (int)q.dir);
  5965. case DLT_IP_OVER_FC:
  5966. return gen_ipfchostop(cstate, eaddr, (int)q.dir);
  5967. default:
  5968. bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  5969. break;
  5970. }
  5971. }
  5972. bpf_error(cstate, "ethernet address used in non-ether expression");
  5973. /* NOTREACHED */
  5974. }
  5975. void
  5976. sappend(struct slist *s0, struct slist *s1)
  5977. {
  5978. /*
  5979. * This is definitely not the best way to do this, but the
  5980. * lists will rarely get long.
  5981. */
  5982. while (s0->next)
  5983. s0 = s0->next;
  5984. s0->next = s1;
  5985. }
  5986. static struct slist *
  5987. xfer_to_x(compiler_state_t *cstate, struct arth *a)
  5988. {
  5989. struct slist *s;
  5990. s = new_stmt(cstate, BPF_LDX|BPF_MEM);
  5991. s->s.k = a->regno;
  5992. return s;
  5993. }
  5994. static struct slist *
  5995. xfer_to_a(compiler_state_t *cstate, struct arth *a)
  5996. {
  5997. struct slist *s;
  5998. s = new_stmt(cstate, BPF_LD|BPF_MEM);
  5999. s->s.k = a->regno;
  6000. return s;
  6001. }
  6002. /*
  6003. * Modify "index" to use the value stored into its register as an
  6004. * offset relative to the beginning of the header for the protocol
  6005. * "proto", and allocate a register and put an item "size" bytes long
  6006. * (1, 2, or 4) at that offset into that register, making it the register
  6007. * for "index".
  6008. */
  6009. struct arth *
  6010. gen_load(compiler_state_t *cstate, int proto, struct arth *inst, int size)
  6011. {
  6012. struct slist *s, *tmp;
  6013. struct block *b;
  6014. int regno = alloc_reg(cstate);
  6015. free_reg(cstate, inst->regno);
  6016. switch (size) {
  6017. default:
  6018. bpf_error(cstate, "data size must be 1, 2, or 4");
  6019. case 1:
  6020. size = BPF_B;
  6021. break;
  6022. case 2:
  6023. size = BPF_H;
  6024. break;
  6025. case 4:
  6026. size = BPF_W;
  6027. break;
  6028. }
  6029. switch (proto) {
  6030. default:
  6031. bpf_error(cstate, "unsupported index operation");
  6032. case Q_RADIO:
  6033. /*
  6034. * The offset is relative to the beginning of the packet
  6035. * data, if we have a radio header. (If we don't, this
  6036. * is an error.)
  6037. */
  6038. if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
  6039. cstate->linktype != DLT_IEEE802_11_RADIO &&
  6040. cstate->linktype != DLT_PRISM_HEADER)
  6041. bpf_error(cstate, "radio information not present in capture");
  6042. /*
  6043. * Load into the X register the offset computed into the
  6044. * register specified by "index".
  6045. */
  6046. s = xfer_to_x(cstate, inst);
  6047. /*
  6048. * Load the item at that offset.
  6049. */
  6050. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
  6051. sappend(s, tmp);
  6052. sappend(inst->s, s);
  6053. break;
  6054. case Q_LINK:
  6055. /*
  6056. * The offset is relative to the beginning of
  6057. * the link-layer header.
  6058. *
  6059. * XXX - what about ATM LANE? Should the index be
  6060. * relative to the beginning of the AAL5 frame, so
  6061. * that 0 refers to the beginning of the LE Control
  6062. * field, or relative to the beginning of the LAN
  6063. * frame, so that 0 refers, for Ethernet LANE, to
  6064. * the beginning of the destination address?
  6065. */
  6066. s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
  6067. /*
  6068. * If "s" is non-null, it has code to arrange that the
  6069. * X register contains the length of the prefix preceding
  6070. * the link-layer header. Add to it the offset computed
  6071. * into the register specified by "index", and move that
  6072. * into the X register. Otherwise, just load into the X
  6073. * register the offset computed into the register specified
  6074. * by "index".
  6075. */
  6076. if (s != NULL) {
  6077. sappend(s, xfer_to_a(cstate, inst));
  6078. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6079. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6080. } else
  6081. s = xfer_to_x(cstate, inst);
  6082. /*
  6083. * Load the item at the sum of the offset we've put in the
  6084. * X register and the offset of the start of the link
  6085. * layer header (which is 0 if the radio header is
  6086. * variable-length; that header length is what we put
  6087. * into the X register and then added to the index).
  6088. */
  6089. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
  6090. tmp->s.k = cstate->off_linkhdr.constant_part;
  6091. sappend(s, tmp);
  6092. sappend(inst->s, s);
  6093. break;
  6094. case Q_IP:
  6095. case Q_ARP:
  6096. case Q_RARP:
  6097. case Q_ATALK:
  6098. case Q_DECNET:
  6099. case Q_SCA:
  6100. case Q_LAT:
  6101. case Q_MOPRC:
  6102. case Q_MOPDL:
  6103. case Q_IPV6:
  6104. /*
  6105. * The offset is relative to the beginning of
  6106. * the network-layer header.
  6107. * XXX - are there any cases where we want
  6108. * cstate->off_nl_nosnap?
  6109. */
  6110. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  6111. /*
  6112. * If "s" is non-null, it has code to arrange that the
  6113. * X register contains the variable part of the offset
  6114. * of the link-layer payload. Add to it the offset
  6115. * computed into the register specified by "index",
  6116. * and move that into the X register. Otherwise, just
  6117. * load into the X register the offset computed into
  6118. * the register specified by "index".
  6119. */
  6120. if (s != NULL) {
  6121. sappend(s, xfer_to_a(cstate, inst));
  6122. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6123. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6124. } else
  6125. s = xfer_to_x(cstate, inst);
  6126. /*
  6127. * Load the item at the sum of the offset we've put in the
  6128. * X register, the offset of the start of the network
  6129. * layer header from the beginning of the link-layer
  6130. * payload, and the constant part of the offset of the
  6131. * start of the link-layer payload.
  6132. */
  6133. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
  6134. tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  6135. sappend(s, tmp);
  6136. sappend(inst->s, s);
  6137. /*
  6138. * Do the computation only if the packet contains
  6139. * the protocol in question.
  6140. */
  6141. b = gen_proto_abbrev(cstate, proto);
  6142. if (inst->b)
  6143. gen_and(inst->b, b);
  6144. inst->b = b;
  6145. break;
  6146. case Q_SCTP:
  6147. case Q_TCP:
  6148. case Q_UDP:
  6149. case Q_ICMP:
  6150. case Q_IGMP:
  6151. case Q_IGRP:
  6152. case Q_PIM:
  6153. case Q_VRRP:
  6154. case Q_CARP:
  6155. /*
  6156. * The offset is relative to the beginning of
  6157. * the transport-layer header.
  6158. *
  6159. * Load the X register with the length of the IPv4 header
  6160. * (plus the offset of the link-layer header, if it's
  6161. * a variable-length header), in bytes.
  6162. *
  6163. * XXX - are there any cases where we want
  6164. * cstate->off_nl_nosnap?
  6165. * XXX - we should, if we're built with
  6166. * IPv6 support, generate code to load either
  6167. * IPv4, IPv6, or both, as appropriate.
  6168. */
  6169. s = gen_loadx_iphdrlen(cstate);
  6170. /*
  6171. * The X register now contains the sum of the variable
  6172. * part of the offset of the link-layer payload and the
  6173. * length of the network-layer header.
  6174. *
  6175. * Load into the A register the offset relative to
  6176. * the beginning of the transport layer header,
  6177. * add the X register to that, move that to the
  6178. * X register, and load with an offset from the
  6179. * X register equal to the sum of the constant part of
  6180. * the offset of the link-layer payload and the offset,
  6181. * relative to the beginning of the link-layer payload,
  6182. * of the network-layer header.
  6183. */
  6184. sappend(s, xfer_to_a(cstate, inst));
  6185. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6186. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6187. sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size));
  6188. tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  6189. sappend(inst->s, s);
  6190. /*
  6191. * Do the computation only if the packet contains
  6192. * the protocol in question - which is true only
  6193. * if this is an IP datagram and is the first or
  6194. * only fragment of that datagram.
  6195. */
  6196. gen_and(gen_proto_abbrev(cstate, proto), b = gen_ipfrag(cstate));
  6197. if (inst->b)
  6198. gen_and(inst->b, b);
  6199. gen_and(gen_proto_abbrev(cstate, Q_IP), b);
  6200. inst->b = b;
  6201. break;
  6202. case Q_ICMPV6:
  6203. /*
  6204. * Do the computation only if the packet contains
  6205. * the protocol in question.
  6206. */
  6207. b = gen_proto_abbrev(cstate, Q_IPV6);
  6208. if (inst->b) {
  6209. gen_and(inst->b, b);
  6210. }
  6211. inst->b = b;
  6212. /*
  6213. * Check if we have an icmp6 next header
  6214. */
  6215. b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
  6216. if (inst->b) {
  6217. gen_and(inst->b, b);
  6218. }
  6219. inst->b = b;
  6220. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  6221. /*
  6222. * If "s" is non-null, it has code to arrange that the
  6223. * X register contains the variable part of the offset
  6224. * of the link-layer payload. Add to it the offset
  6225. * computed into the register specified by "index",
  6226. * and move that into the X register. Otherwise, just
  6227. * load into the X register the offset computed into
  6228. * the register specified by "index".
  6229. */
  6230. if (s != NULL) {
  6231. sappend(s, xfer_to_a(cstate, inst));
  6232. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6233. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6234. } else {
  6235. s = xfer_to_x(cstate, inst);
  6236. }
  6237. /*
  6238. * Load the item at the sum of the offset we've put in the
  6239. * X register, the offset of the start of the network
  6240. * layer header from the beginning of the link-layer
  6241. * payload, and the constant part of the offset of the
  6242. * start of the link-layer payload.
  6243. */
  6244. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
  6245. tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
  6246. sappend(s, tmp);
  6247. sappend(inst->s, s);
  6248. break;
  6249. }
  6250. inst->regno = regno;
  6251. s = new_stmt(cstate, BPF_ST);
  6252. s->s.k = regno;
  6253. sappend(inst->s, s);
  6254. return inst;
  6255. }
  6256. struct block *
  6257. gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
  6258. struct arth *a1, int reversed)
  6259. {
  6260. struct slist *s0, *s1, *s2;
  6261. struct block *b, *tmp;
  6262. s0 = xfer_to_x(cstate, a1);
  6263. s1 = xfer_to_a(cstate, a0);
  6264. if (code == BPF_JEQ) {
  6265. s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
  6266. b = new_block(cstate, JMP(code));
  6267. sappend(s1, s2);
  6268. }
  6269. else
  6270. b = new_block(cstate, BPF_JMP|code|BPF_X);
  6271. if (reversed)
  6272. gen_not(b);
  6273. sappend(s0, s1);
  6274. sappend(a1->s, s0);
  6275. sappend(a0->s, a1->s);
  6276. b->stmts = a0->s;
  6277. free_reg(cstate, a0->regno);
  6278. free_reg(cstate, a1->regno);
  6279. /* 'and' together protocol checks */
  6280. if (a0->b) {
  6281. if (a1->b) {
  6282. gen_and(a0->b, tmp = a1->b);
  6283. }
  6284. else
  6285. tmp = a0->b;
  6286. } else
  6287. tmp = a1->b;
  6288. if (tmp)
  6289. gen_and(tmp, b);
  6290. return b;
  6291. }
  6292. struct arth *
  6293. gen_loadlen(compiler_state_t *cstate)
  6294. {
  6295. int regno = alloc_reg(cstate);
  6296. struct arth *a = (struct arth *)newchunk(cstate, sizeof(*a));
  6297. struct slist *s;
  6298. s = new_stmt(cstate, BPF_LD|BPF_LEN);
  6299. s->next = new_stmt(cstate, BPF_ST);
  6300. s->next->s.k = regno;
  6301. a->s = s;
  6302. a->regno = regno;
  6303. return a;
  6304. }
  6305. struct arth *
  6306. gen_loadi(compiler_state_t *cstate, int val)
  6307. {
  6308. struct arth *a;
  6309. struct slist *s;
  6310. int reg;
  6311. a = (struct arth *)newchunk(cstate, sizeof(*a));
  6312. reg = alloc_reg(cstate);
  6313. s = new_stmt(cstate, BPF_LD|BPF_IMM);
  6314. s->s.k = val;
  6315. s->next = new_stmt(cstate, BPF_ST);
  6316. s->next->s.k = reg;
  6317. a->s = s;
  6318. a->regno = reg;
  6319. return a;
  6320. }
  6321. struct arth *
  6322. gen_neg(compiler_state_t *cstate, struct arth *a)
  6323. {
  6324. struct slist *s;
  6325. s = xfer_to_a(cstate, a);
  6326. sappend(a->s, s);
  6327. s = new_stmt(cstate, BPF_ALU|BPF_NEG);
  6328. s->s.k = 0;
  6329. sappend(a->s, s);
  6330. s = new_stmt(cstate, BPF_ST);
  6331. s->s.k = a->regno;
  6332. sappend(a->s, s);
  6333. return a;
  6334. }
  6335. struct arth *
  6336. gen_arth(compiler_state_t *cstate, int code, struct arth *a0,
  6337. struct arth *a1)
  6338. {
  6339. struct slist *s0, *s1, *s2;
  6340. /*
  6341. * Disallow division by, or modulus by, zero; we do this here
  6342. * so that it gets done even if the optimizer is disabled.
  6343. */
  6344. if (code == BPF_DIV) {
  6345. if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
  6346. bpf_error(cstate, "division by zero");
  6347. } else if (code == BPF_MOD) {
  6348. if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
  6349. bpf_error(cstate, "modulus by zero");
  6350. }
  6351. s0 = xfer_to_x(cstate, a1);
  6352. s1 = xfer_to_a(cstate, a0);
  6353. s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
  6354. sappend(s1, s2);
  6355. sappend(s0, s1);
  6356. sappend(a1->s, s0);
  6357. sappend(a0->s, a1->s);
  6358. free_reg(cstate, a0->regno);
  6359. free_reg(cstate, a1->regno);
  6360. s0 = new_stmt(cstate, BPF_ST);
  6361. a0->regno = s0->s.k = alloc_reg(cstate);
  6362. sappend(a0->s, s0);
  6363. return a0;
  6364. }
  6365. /*
  6366. * Initialize the table of used registers and the current register.
  6367. */
  6368. static void
  6369. init_regs(compiler_state_t *cstate)
  6370. {
  6371. cstate->curreg = 0;
  6372. memset(cstate->regused, 0, sizeof cstate->regused);
  6373. }
  6374. /*
  6375. * Return the next free register.
  6376. */
  6377. static int
  6378. alloc_reg(compiler_state_t *cstate)
  6379. {
  6380. int n = BPF_MEMWORDS;
  6381. while (--n >= 0) {
  6382. if (cstate->regused[cstate->curreg])
  6383. cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
  6384. else {
  6385. cstate->regused[cstate->curreg] = 1;
  6386. return cstate->curreg;
  6387. }
  6388. }
  6389. bpf_error(cstate, "too many registers needed to evaluate expression");
  6390. /* NOTREACHED */
  6391. }
  6392. /*
  6393. * Return a register to the table so it can
  6394. * be used later.
  6395. */
  6396. static void
  6397. free_reg(compiler_state_t *cstate, int n)
  6398. {
  6399. cstate->regused[n] = 0;
  6400. }
  6401. static struct block *
  6402. gen_len(compiler_state_t *cstate, int jmp, int n)
  6403. {
  6404. struct slist *s;
  6405. struct block *b;
  6406. s = new_stmt(cstate, BPF_LD|BPF_LEN);
  6407. b = new_block(cstate, JMP(jmp));
  6408. b->stmts = s;
  6409. b->s.k = n;
  6410. return b;
  6411. }
  6412. struct block *
  6413. gen_greater(compiler_state_t *cstate, int n)
  6414. {
  6415. return gen_len(cstate, BPF_JGE, n);
  6416. }
  6417. /*
  6418. * Actually, this is less than or equal.
  6419. */
  6420. struct block *
  6421. gen_less(compiler_state_t *cstate, int n)
  6422. {
  6423. struct block *b;
  6424. b = gen_len(cstate, BPF_JGT, n);
  6425. gen_not(b);
  6426. return b;
  6427. }
  6428. /*
  6429. * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
  6430. * the beginning of the link-layer header.
  6431. * XXX - that means you can't test values in the radiotap header, but
  6432. * as that header is difficult if not impossible to parse generally
  6433. * without a loop, that might not be a severe problem. A new keyword
  6434. * "radio" could be added for that, although what you'd really want
  6435. * would be a way of testing particular radio header values, which
  6436. * would generate code appropriate to the radio header in question.
  6437. */
  6438. struct block *
  6439. gen_byteop(compiler_state_t *cstate, int op, int idx, int val)
  6440. {
  6441. struct block *b;
  6442. struct slist *s;
  6443. switch (op) {
  6444. default:
  6445. abort();
  6446. case '=':
  6447. return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
  6448. case '<':
  6449. b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
  6450. return b;
  6451. case '>':
  6452. b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
  6453. return b;
  6454. case '|':
  6455. s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
  6456. break;
  6457. case '&':
  6458. s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  6459. break;
  6460. }
  6461. s->s.k = val;
  6462. b = new_block(cstate, JMP(BPF_JEQ));
  6463. b->stmts = s;
  6464. gen_not(b);
  6465. return b;
  6466. }
  6467. static const u_char abroadcast[] = { 0x0 };
  6468. struct block *
  6469. gen_broadcast(compiler_state_t *cstate, int proto)
  6470. {
  6471. bpf_u_int32 hostmask;
  6472. struct block *b0, *b1, *b2;
  6473. static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  6474. switch (proto) {
  6475. case Q_DEFAULT:
  6476. case Q_LINK:
  6477. switch (cstate->linktype) {
  6478. case DLT_ARCNET:
  6479. case DLT_ARCNET_LINUX:
  6480. return gen_ahostop(cstate, abroadcast, Q_DST);
  6481. case DLT_EN10MB:
  6482. case DLT_NETANALYZER:
  6483. case DLT_NETANALYZER_TRANSPARENT:
  6484. b1 = gen_prevlinkhdr_check(cstate);
  6485. b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
  6486. if (b1 != NULL)
  6487. gen_and(b1, b0);
  6488. return b0;
  6489. case DLT_FDDI:
  6490. return gen_fhostop(cstate, ebroadcast, Q_DST);
  6491. case DLT_IEEE802:
  6492. return gen_thostop(cstate, ebroadcast, Q_DST);
  6493. case DLT_IEEE802_11:
  6494. case DLT_PRISM_HEADER:
  6495. case DLT_IEEE802_11_RADIO_AVS:
  6496. case DLT_IEEE802_11_RADIO:
  6497. case DLT_PPI:
  6498. return gen_wlanhostop(cstate, ebroadcast, Q_DST);
  6499. case DLT_IP_OVER_FC:
  6500. return gen_ipfchostop(cstate, ebroadcast, Q_DST);
  6501. default:
  6502. bpf_error(cstate, "not a broadcast link");
  6503. }
  6504. break;
  6505. case Q_IP:
  6506. /*
  6507. * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
  6508. * as an indication that we don't know the netmask, and fail
  6509. * in that case.
  6510. */
  6511. if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
  6512. bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
  6513. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  6514. hostmask = ~cstate->netmask;
  6515. b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, (bpf_int32)0, hostmask);
  6516. b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
  6517. (bpf_int32)(~0 & hostmask), hostmask);
  6518. gen_or(b1, b2);
  6519. gen_and(b0, b2);
  6520. return b2;
  6521. }
  6522. bpf_error(cstate, "only link-layer/IP broadcast filters supported");
  6523. /* NOTREACHED */
  6524. }
  6525. /*
  6526. * Generate code to test the low-order bit of a MAC address (that's
  6527. * the bottom bit of the *first* byte).
  6528. */
  6529. static struct block *
  6530. gen_mac_multicast(compiler_state_t *cstate, int offset)
  6531. {
  6532. register struct block *b0;
  6533. register struct slist *s;
  6534. /* link[offset] & 1 != 0 */
  6535. s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
  6536. b0 = new_block(cstate, JMP(BPF_JSET));
  6537. b0->s.k = 1;
  6538. b0->stmts = s;
  6539. return b0;
  6540. }
  6541. struct block *
  6542. gen_multicast(compiler_state_t *cstate, int proto)
  6543. {
  6544. register struct block *b0, *b1, *b2;
  6545. register struct slist *s;
  6546. switch (proto) {
  6547. case Q_DEFAULT:
  6548. case Q_LINK:
  6549. switch (cstate->linktype) {
  6550. case DLT_ARCNET:
  6551. case DLT_ARCNET_LINUX:
  6552. /* all ARCnet multicasts use the same address */
  6553. return gen_ahostop(cstate, abroadcast, Q_DST);
  6554. case DLT_EN10MB:
  6555. case DLT_NETANALYZER:
  6556. case DLT_NETANALYZER_TRANSPARENT:
  6557. b1 = gen_prevlinkhdr_check(cstate);
  6558. /* ether[0] & 1 != 0 */
  6559. b0 = gen_mac_multicast(cstate, 0);
  6560. if (b1 != NULL)
  6561. gen_and(b1, b0);
  6562. return b0;
  6563. case DLT_FDDI:
  6564. /*
  6565. * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
  6566. *
  6567. * XXX - was that referring to bit-order issues?
  6568. */
  6569. /* fddi[1] & 1 != 0 */
  6570. return gen_mac_multicast(cstate, 1);
  6571. case DLT_IEEE802:
  6572. /* tr[2] & 1 != 0 */
  6573. return gen_mac_multicast(cstate, 2);
  6574. case DLT_IEEE802_11:
  6575. case DLT_PRISM_HEADER:
  6576. case DLT_IEEE802_11_RADIO_AVS:
  6577. case DLT_IEEE802_11_RADIO:
  6578. case DLT_PPI:
  6579. /*
  6580. * Oh, yuk.
  6581. *
  6582. * For control frames, there is no DA.
  6583. *
  6584. * For management frames, DA is at an
  6585. * offset of 4 from the beginning of
  6586. * the packet.
  6587. *
  6588. * For data frames, DA is at an offset
  6589. * of 4 from the beginning of the packet
  6590. * if To DS is clear and at an offset of
  6591. * 16 from the beginning of the packet
  6592. * if To DS is set.
  6593. */
  6594. /*
  6595. * Generate the tests to be done for data frames.
  6596. *
  6597. * First, check for To DS set, i.e. "link[1] & 0x01".
  6598. */
  6599. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  6600. b1 = new_block(cstate, JMP(BPF_JSET));
  6601. b1->s.k = 0x01; /* To DS */
  6602. b1->stmts = s;
  6603. /*
  6604. * If To DS is set, the DA is at 16.
  6605. */
  6606. b0 = gen_mac_multicast(cstate, 16);
  6607. gen_and(b1, b0);
  6608. /*
  6609. * Now, check for To DS not set, i.e. check
  6610. * "!(link[1] & 0x01)".
  6611. */
  6612. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  6613. b2 = new_block(cstate, JMP(BPF_JSET));
  6614. b2->s.k = 0x01; /* To DS */
  6615. b2->stmts = s;
  6616. gen_not(b2);
  6617. /*
  6618. * If To DS is not set, the DA is at 4.
  6619. */
  6620. b1 = gen_mac_multicast(cstate, 4);
  6621. gen_and(b2, b1);
  6622. /*
  6623. * Now OR together the last two checks. That gives
  6624. * the complete set of checks for data frames.
  6625. */
  6626. gen_or(b1, b0);
  6627. /*
  6628. * Now check for a data frame.
  6629. * I.e, check "link[0] & 0x08".
  6630. */
  6631. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  6632. b1 = new_block(cstate, JMP(BPF_JSET));
  6633. b1->s.k = 0x08;
  6634. b1->stmts = s;
  6635. /*
  6636. * AND that with the checks done for data frames.
  6637. */
  6638. gen_and(b1, b0);
  6639. /*
  6640. * If the high-order bit of the type value is 0, this
  6641. * is a management frame.
  6642. * I.e, check "!(link[0] & 0x08)".
  6643. */
  6644. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  6645. b2 = new_block(cstate, JMP(BPF_JSET));
  6646. b2->s.k = 0x08;
  6647. b2->stmts = s;
  6648. gen_not(b2);
  6649. /*
  6650. * For management frames, the DA is at 4.
  6651. */
  6652. b1 = gen_mac_multicast(cstate, 4);
  6653. gen_and(b2, b1);
  6654. /*
  6655. * OR that with the checks done for data frames.
  6656. * That gives the checks done for management and
  6657. * data frames.
  6658. */
  6659. gen_or(b1, b0);
  6660. /*
  6661. * If the low-order bit of the type value is 1,
  6662. * this is either a control frame or a frame
  6663. * with a reserved type, and thus not a
  6664. * frame with an SA.
  6665. *
  6666. * I.e., check "!(link[0] & 0x04)".
  6667. */
  6668. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  6669. b1 = new_block(cstate, JMP(BPF_JSET));
  6670. b1->s.k = 0x04;
  6671. b1->stmts = s;
  6672. gen_not(b1);
  6673. /*
  6674. * AND that with the checks for data and management
  6675. * frames.
  6676. */
  6677. gen_and(b1, b0);
  6678. return b0;
  6679. case DLT_IP_OVER_FC:
  6680. b0 = gen_mac_multicast(cstate, 2);
  6681. return b0;
  6682. default:
  6683. break;
  6684. }
  6685. /* Link not known to support multicasts */
  6686. break;
  6687. case Q_IP:
  6688. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  6689. b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, (bpf_int32)224);
  6690. gen_and(b0, b1);
  6691. return b1;
  6692. case Q_IPV6:
  6693. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  6694. b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, (bpf_int32)255);
  6695. gen_and(b0, b1);
  6696. return b1;
  6697. }
  6698. bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
  6699. /* NOTREACHED */
  6700. }
  6701. /*
  6702. * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
  6703. * Outbound traffic is sent by this machine, while inbound traffic is
  6704. * sent by a remote machine (and may include packets destined for a
  6705. * unicast or multicast link-layer address we are not subscribing to).
  6706. * These are the same definitions implemented by pcap_setdirection().
  6707. * Capturing only unicast traffic destined for this host is probably
  6708. * better accomplished using a higher-layer filter.
  6709. */
  6710. struct block *
  6711. gen_inbound(compiler_state_t *cstate, int dir)
  6712. {
  6713. register struct block *b0;
  6714. /*
  6715. * Only some data link types support inbound/outbound qualifiers.
  6716. */
  6717. switch (cstate->linktype) {
  6718. case DLT_SLIP:
  6719. b0 = gen_relation(cstate, BPF_JEQ,
  6720. gen_load(cstate, Q_LINK, gen_loadi(cstate, 0), 1),
  6721. gen_loadi(cstate, 0),
  6722. dir);
  6723. break;
  6724. case DLT_IPNET:
  6725. if (dir) {
  6726. /* match outgoing packets */
  6727. b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
  6728. } else {
  6729. /* match incoming packets */
  6730. b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
  6731. }
  6732. break;
  6733. case DLT_LINUX_SLL:
  6734. /* match outgoing packets */
  6735. b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
  6736. if (!dir) {
  6737. /* to filter on inbound traffic, invert the match */
  6738. gen_not(b0);
  6739. }
  6740. break;
  6741. #ifdef HAVE_NET_PFVAR_H
  6742. case DLT_PFLOG:
  6743. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
  6744. (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
  6745. break;
  6746. #endif
  6747. case DLT_PPP_PPPD:
  6748. if (dir) {
  6749. /* match outgoing packets */
  6750. b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
  6751. } else {
  6752. /* match incoming packets */
  6753. b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
  6754. }
  6755. break;
  6756. case DLT_JUNIPER_MFR:
  6757. case DLT_JUNIPER_MLFR:
  6758. case DLT_JUNIPER_MLPPP:
  6759. case DLT_JUNIPER_ATM1:
  6760. case DLT_JUNIPER_ATM2:
  6761. case DLT_JUNIPER_PPPOE:
  6762. case DLT_JUNIPER_PPPOE_ATM:
  6763. case DLT_JUNIPER_GGSN:
  6764. case DLT_JUNIPER_ES:
  6765. case DLT_JUNIPER_MONITOR:
  6766. case DLT_JUNIPER_SERVICES:
  6767. case DLT_JUNIPER_ETHER:
  6768. case DLT_JUNIPER_PPP:
  6769. case DLT_JUNIPER_FRELAY:
  6770. case DLT_JUNIPER_CHDLC:
  6771. case DLT_JUNIPER_VP:
  6772. case DLT_JUNIPER_ST:
  6773. case DLT_JUNIPER_ISM:
  6774. case DLT_JUNIPER_VS:
  6775. case DLT_JUNIPER_SRX_E2E:
  6776. case DLT_JUNIPER_FIBRECHANNEL:
  6777. case DLT_JUNIPER_ATM_CEMIC:
  6778. /* juniper flags (including direction) are stored
  6779. * the byte after the 3-byte magic number */
  6780. if (dir) {
  6781. /* match outgoing packets */
  6782. b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
  6783. } else {
  6784. /* match incoming packets */
  6785. b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
  6786. }
  6787. break;
  6788. default:
  6789. /*
  6790. * If we have packet meta-data indicating a direction,
  6791. * and that metadata can be checked by BPF code, check
  6792. * it. Otherwise, give up, as this link-layer type has
  6793. * nothing in the packet data.
  6794. *
  6795. * Currently, the only platform where a BPF filter can
  6796. * check that metadata is Linux with the in-kernel
  6797. * BPF interpreter. If other packet capture mechanisms
  6798. * and BPF filters also supported this, it would be
  6799. * nice. It would be even better if they made that
  6800. * metadata available so that we could provide it
  6801. * with newer capture APIs, allowing it to be saved
  6802. * in pcapng files.
  6803. */
  6804. #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
  6805. /*
  6806. * This is Linux with PF_PACKET support.
  6807. * If this is a *live* capture, we can look at
  6808. * special meta-data in the filter expression;
  6809. * if it's a savefile, we can't.
  6810. */
  6811. if (cstate->bpf_pcap->rfile != NULL) {
  6812. /* We have a FILE *, so this is a savefile */
  6813. bpf_error(cstate, "inbound/outbound not supported on linktype %d when reading savefiles",
  6814. cstate->linktype);
  6815. b0 = NULL;
  6816. /* NOTREACHED */
  6817. }
  6818. /* match outgoing packets */
  6819. b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
  6820. PACKET_OUTGOING);
  6821. if (!dir) {
  6822. /* to filter on inbound traffic, invert the match */
  6823. gen_not(b0);
  6824. }
  6825. #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
  6826. bpf_error(cstate, "inbound/outbound not supported on linktype %d",
  6827. cstate->linktype);
  6828. /* NOTREACHED */
  6829. #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
  6830. }
  6831. return (b0);
  6832. }
  6833. #ifdef HAVE_NET_PFVAR_H
  6834. /* PF firewall log matched interface */
  6835. struct block *
  6836. gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
  6837. {
  6838. struct block *b0;
  6839. u_int len, off;
  6840. if (cstate->linktype != DLT_PFLOG) {
  6841. bpf_error(cstate, "ifname supported only on PF linktype");
  6842. /* NOTREACHED */
  6843. }
  6844. len = sizeof(((struct pfloghdr *)0)->ifname);
  6845. off = offsetof(struct pfloghdr, ifname);
  6846. if (strlen(ifname) >= len) {
  6847. bpf_error(cstate, "ifname interface names can only be %d characters",
  6848. len-1);
  6849. /* NOTREACHED */
  6850. }
  6851. b0 = gen_bcmp(cstate, OR_LINKHDR, off, strlen(ifname), (const u_char *)ifname);
  6852. return (b0);
  6853. }
  6854. /* PF firewall log ruleset name */
  6855. struct block *
  6856. gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
  6857. {
  6858. struct block *b0;
  6859. if (cstate->linktype != DLT_PFLOG) {
  6860. bpf_error(cstate, "ruleset supported only on PF linktype");
  6861. /* NOTREACHED */
  6862. }
  6863. if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
  6864. bpf_error(cstate, "ruleset names can only be %ld characters",
  6865. (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
  6866. /* NOTREACHED */
  6867. }
  6868. b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
  6869. strlen(ruleset), (const u_char *)ruleset);
  6870. return (b0);
  6871. }
  6872. /* PF firewall log rule number */
  6873. struct block *
  6874. gen_pf_rnr(compiler_state_t *cstate, int rnr)
  6875. {
  6876. struct block *b0;
  6877. if (cstate->linktype != DLT_PFLOG) {
  6878. bpf_error(cstate, "rnr supported only on PF linktype");
  6879. /* NOTREACHED */
  6880. }
  6881. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
  6882. (bpf_int32)rnr);
  6883. return (b0);
  6884. }
  6885. /* PF firewall log sub-rule number */
  6886. struct block *
  6887. gen_pf_srnr(compiler_state_t *cstate, int srnr)
  6888. {
  6889. struct block *b0;
  6890. if (cstate->linktype != DLT_PFLOG) {
  6891. bpf_error(cstate, "srnr supported only on PF linktype");
  6892. /* NOTREACHED */
  6893. }
  6894. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
  6895. (bpf_int32)srnr);
  6896. return (b0);
  6897. }
  6898. /* PF firewall log reason code */
  6899. struct block *
  6900. gen_pf_reason(compiler_state_t *cstate, int reason)
  6901. {
  6902. struct block *b0;
  6903. if (cstate->linktype != DLT_PFLOG) {
  6904. bpf_error(cstate, "reason supported only on PF linktype");
  6905. /* NOTREACHED */
  6906. }
  6907. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
  6908. (bpf_int32)reason);
  6909. return (b0);
  6910. }
  6911. /* PF firewall log action */
  6912. struct block *
  6913. gen_pf_action(compiler_state_t *cstate, int action)
  6914. {
  6915. struct block *b0;
  6916. if (cstate->linktype != DLT_PFLOG) {
  6917. bpf_error(cstate, "action supported only on PF linktype");
  6918. /* NOTREACHED */
  6919. }
  6920. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
  6921. (bpf_int32)action);
  6922. return (b0);
  6923. }
  6924. #else /* !HAVE_NET_PFVAR_H */
  6925. struct block *
  6926. gen_pf_ifname(compiler_state_t *cstate, const char *ifname _U_)
  6927. {
  6928. bpf_error(cstate, "libpcap was compiled without pf support");
  6929. /* NOTREACHED */
  6930. }
  6931. struct block *
  6932. gen_pf_ruleset(compiler_state_t *cstate, char *ruleset _U_)
  6933. {
  6934. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  6935. /* NOTREACHED */
  6936. }
  6937. struct block *
  6938. gen_pf_rnr(compiler_state_t *cstate, int rnr _U_)
  6939. {
  6940. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  6941. /* NOTREACHED */
  6942. }
  6943. struct block *
  6944. gen_pf_srnr(compiler_state_t *cstate, int srnr _U_)
  6945. {
  6946. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  6947. /* NOTREACHED */
  6948. }
  6949. struct block *
  6950. gen_pf_reason(compiler_state_t *cstate, int reason _U_)
  6951. {
  6952. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  6953. /* NOTREACHED */
  6954. }
  6955. struct block *
  6956. gen_pf_action(compiler_state_t *cstate, int action _U_)
  6957. {
  6958. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  6959. /* NOTREACHED */
  6960. }
  6961. #endif /* HAVE_NET_PFVAR_H */
  6962. /* IEEE 802.11 wireless header */
  6963. struct block *
  6964. gen_p80211_type(compiler_state_t *cstate, int type, int mask)
  6965. {
  6966. struct block *b0;
  6967. switch (cstate->linktype) {
  6968. case DLT_IEEE802_11:
  6969. case DLT_PRISM_HEADER:
  6970. case DLT_IEEE802_11_RADIO_AVS:
  6971. case DLT_IEEE802_11_RADIO:
  6972. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, (bpf_int32)type,
  6973. (bpf_int32)mask);
  6974. break;
  6975. default:
  6976. bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
  6977. /* NOTREACHED */
  6978. }
  6979. return (b0);
  6980. }
  6981. struct block *
  6982. gen_p80211_fcdir(compiler_state_t *cstate, int fcdir)
  6983. {
  6984. struct block *b0;
  6985. switch (cstate->linktype) {
  6986. case DLT_IEEE802_11:
  6987. case DLT_PRISM_HEADER:
  6988. case DLT_IEEE802_11_RADIO_AVS:
  6989. case DLT_IEEE802_11_RADIO:
  6990. break;
  6991. default:
  6992. bpf_error(cstate, "frame direction supported only with 802.11 headers");
  6993. /* NOTREACHED */
  6994. }
  6995. b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, (bpf_int32)fcdir,
  6996. (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
  6997. return (b0);
  6998. }
  6999. struct block *
  7000. gen_acode(compiler_state_t *cstate, const u_char *eaddr, struct qual q)
  7001. {
  7002. switch (cstate->linktype) {
  7003. case DLT_ARCNET:
  7004. case DLT_ARCNET_LINUX:
  7005. if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
  7006. q.proto == Q_LINK)
  7007. return (gen_ahostop(cstate, eaddr, (int)q.dir));
  7008. else {
  7009. bpf_error(cstate, "ARCnet address used in non-arc expression");
  7010. /* NOTREACHED */
  7011. }
  7012. break;
  7013. default:
  7014. bpf_error(cstate, "aid supported only on ARCnet");
  7015. /* NOTREACHED */
  7016. }
  7017. }
  7018. static struct block *
  7019. gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  7020. {
  7021. register struct block *b0, *b1;
  7022. switch (dir) {
  7023. /* src comes first, different from Ethernet */
  7024. case Q_SRC:
  7025. return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
  7026. case Q_DST:
  7027. return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
  7028. case Q_AND:
  7029. b0 = gen_ahostop(cstate, eaddr, Q_SRC);
  7030. b1 = gen_ahostop(cstate, eaddr, Q_DST);
  7031. gen_and(b0, b1);
  7032. return b1;
  7033. case Q_DEFAULT:
  7034. case Q_OR:
  7035. b0 = gen_ahostop(cstate, eaddr, Q_SRC);
  7036. b1 = gen_ahostop(cstate, eaddr, Q_DST);
  7037. gen_or(b0, b1);
  7038. return b1;
  7039. case Q_ADDR1:
  7040. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  7041. break;
  7042. case Q_ADDR2:
  7043. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  7044. break;
  7045. case Q_ADDR3:
  7046. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  7047. break;
  7048. case Q_ADDR4:
  7049. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  7050. break;
  7051. case Q_RA:
  7052. bpf_error(cstate, "'ra' is only supported on 802.11");
  7053. break;
  7054. case Q_TA:
  7055. bpf_error(cstate, "'ta' is only supported on 802.11");
  7056. break;
  7057. }
  7058. abort();
  7059. /* NOTREACHED */
  7060. }
  7061. static struct block *
  7062. gen_vlan_tpid_test(compiler_state_t *cstate)
  7063. {
  7064. struct block *b0, *b1;
  7065. /* check for VLAN, including QinQ */
  7066. b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
  7067. b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
  7068. gen_or(b0,b1);
  7069. b0 = b1;
  7070. b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
  7071. gen_or(b0,b1);
  7072. return b1;
  7073. }
  7074. static struct block *
  7075. gen_vlan_vid_test(compiler_state_t *cstate, int vlan_num)
  7076. {
  7077. return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, (bpf_int32)vlan_num, 0x0fff);
  7078. }
  7079. static struct block *
  7080. gen_vlan_no_bpf_extensions(compiler_state_t *cstate, int vlan_num)
  7081. {
  7082. struct block *b0, *b1;
  7083. b0 = gen_vlan_tpid_test(cstate);
  7084. if (vlan_num >= 0) {
  7085. b1 = gen_vlan_vid_test(cstate, vlan_num);
  7086. gen_and(b0, b1);
  7087. b0 = b1;
  7088. }
  7089. /*
  7090. * Both payload and link header type follow the VLAN tags so that
  7091. * both need to be updated.
  7092. */
  7093. cstate->off_linkpl.constant_part += 4;
  7094. cstate->off_linktype.constant_part += 4;
  7095. return b0;
  7096. }
  7097. #if defined(SKF_AD_VLAN_TAG_PRESENT)
  7098. /* add v to variable part of off */
  7099. static void
  7100. gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, int v, struct slist *s)
  7101. {
  7102. struct slist *s2;
  7103. if (!off->is_variable)
  7104. off->is_variable = 1;
  7105. if (off->reg == -1)
  7106. off->reg = alloc_reg(cstate);
  7107. s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
  7108. s2->s.k = off->reg;
  7109. sappend(s, s2);
  7110. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
  7111. s2->s.k = v;
  7112. sappend(s, s2);
  7113. s2 = new_stmt(cstate, BPF_ST);
  7114. s2->s.k = off->reg;
  7115. sappend(s, s2);
  7116. }
  7117. /*
  7118. * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
  7119. * and link type offsets first
  7120. */
  7121. static void
  7122. gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
  7123. {
  7124. struct slist s;
  7125. /* offset determined at run time, shift variable part */
  7126. s.next = NULL;
  7127. cstate->is_vlan_vloffset = 1;
  7128. gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
  7129. gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
  7130. /* we get a pointer to a chain of or-ed blocks, patch first of them */
  7131. sappend(s.next, b_tpid->head->stmts);
  7132. b_tpid->head->stmts = s.next;
  7133. }
  7134. /*
  7135. * patch block b_vid (VLAN id test) to load VID value either from packet
  7136. * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
  7137. */
  7138. static void
  7139. gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
  7140. {
  7141. struct slist *s, *s2, *sjeq;
  7142. unsigned cnt;
  7143. s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  7144. s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
  7145. /* true -> next instructions, false -> beginning of b_vid */
  7146. sjeq = new_stmt(cstate, JMP(BPF_JEQ));
  7147. sjeq->s.k = 1;
  7148. sjeq->s.jf = b_vid->stmts;
  7149. sappend(s, sjeq);
  7150. s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  7151. s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
  7152. sappend(s, s2);
  7153. sjeq->s.jt = s2;
  7154. /* jump to the test in b_vid (bypass loading VID from packet data) */
  7155. cnt = 0;
  7156. for (s2 = b_vid->stmts; s2; s2 = s2->next)
  7157. cnt++;
  7158. s2 = new_stmt(cstate, JMP(BPF_JA));
  7159. s2->s.k = cnt;
  7160. sappend(s, s2);
  7161. /* insert our statements at the beginning of b_vid */
  7162. sappend(s, b_vid->stmts);
  7163. b_vid->stmts = s;
  7164. }
  7165. /*
  7166. * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
  7167. * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
  7168. * tag can be either in metadata or in packet data; therefore if the
  7169. * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
  7170. * header for VLAN tag. As the decision is done at run time, we need
  7171. * update variable part of the offsets
  7172. */
  7173. static struct block *
  7174. gen_vlan_bpf_extensions(compiler_state_t *cstate, int vlan_num)
  7175. {
  7176. struct block *b0, *b_tpid, *b_vid = NULL;
  7177. struct slist *s;
  7178. /* generate new filter code based on extracting packet
  7179. * metadata */
  7180. s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  7181. s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
  7182. b0 = new_block(cstate, JMP(BPF_JEQ));
  7183. b0->stmts = s;
  7184. b0->s.k = 1;
  7185. /*
  7186. * This is tricky. We need to insert the statements updating variable
  7187. * parts of offsets before the the traditional TPID and VID tests so
  7188. * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
  7189. * we do not want this update to affect those checks. That's why we
  7190. * generate both test blocks first and insert the statements updating
  7191. * variable parts of both offsets after that. This wouldn't work if
  7192. * there already were variable length link header when entering this
  7193. * function but gen_vlan_bpf_extensions() isn't called in that case.
  7194. */
  7195. b_tpid = gen_vlan_tpid_test(cstate);
  7196. if (vlan_num >= 0)
  7197. b_vid = gen_vlan_vid_test(cstate, vlan_num);
  7198. gen_vlan_patch_tpid_test(cstate, b_tpid);
  7199. gen_or(b0, b_tpid);
  7200. b0 = b_tpid;
  7201. if (vlan_num >= 0) {
  7202. gen_vlan_patch_vid_test(cstate, b_vid);
  7203. gen_and(b0, b_vid);
  7204. b0 = b_vid;
  7205. }
  7206. return b0;
  7207. }
  7208. #endif
  7209. /*
  7210. * support IEEE 802.1Q VLAN trunk over ethernet
  7211. */
  7212. struct block *
  7213. gen_vlan(compiler_state_t *cstate, int vlan_num)
  7214. {
  7215. struct block *b0;
  7216. /* can't check for VLAN-encapsulated packets inside MPLS */
  7217. if (cstate->label_stack_depth > 0)
  7218. bpf_error(cstate, "no VLAN match after MPLS");
  7219. /*
  7220. * Check for a VLAN packet, and then change the offsets to point
  7221. * to the type and data fields within the VLAN packet. Just
  7222. * increment the offsets, so that we can support a hierarchy, e.g.
  7223. * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
  7224. * VLAN 100.
  7225. *
  7226. * XXX - this is a bit of a kludge. If we were to split the
  7227. * compiler into a parser that parses an expression and
  7228. * generates an expression tree, and a code generator that
  7229. * takes an expression tree (which could come from our
  7230. * parser or from some other parser) and generates BPF code,
  7231. * we could perhaps make the offsets parameters of routines
  7232. * and, in the handler for an "AND" node, pass to subnodes
  7233. * other than the VLAN node the adjusted offsets.
  7234. *
  7235. * This would mean that "vlan" would, instead of changing the
  7236. * behavior of *all* tests after it, change only the behavior
  7237. * of tests ANDed with it. That would change the documented
  7238. * semantics of "vlan", which might break some expressions.
  7239. * However, it would mean that "(vlan and ip) or ip" would check
  7240. * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
  7241. * checking only for VLAN-encapsulated IP, so that could still
  7242. * be considered worth doing; it wouldn't break expressions
  7243. * that are of the form "vlan and ..." or "vlan N and ...",
  7244. * which I suspect are the most common expressions involving
  7245. * "vlan". "vlan or ..." doesn't necessarily do what the user
  7246. * would really want, now, as all the "or ..." tests would
  7247. * be done assuming a VLAN, even though the "or" could be viewed
  7248. * as meaning "or, if this isn't a VLAN packet...".
  7249. */
  7250. switch (cstate->linktype) {
  7251. case DLT_EN10MB:
  7252. case DLT_NETANALYZER:
  7253. case DLT_NETANALYZER_TRANSPARENT:
  7254. #if defined(SKF_AD_VLAN_TAG_PRESENT)
  7255. /* Verify that this is the outer part of the packet and
  7256. * not encapsulated somehow. */
  7257. if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
  7258. cstate->off_linkhdr.constant_part ==
  7259. cstate->off_outermostlinkhdr.constant_part) {
  7260. /*
  7261. * Do we need special VLAN handling?
  7262. */
  7263. if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
  7264. b0 = gen_vlan_bpf_extensions(cstate, vlan_num);
  7265. else
  7266. b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num);
  7267. } else
  7268. #endif
  7269. b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num);
  7270. break;
  7271. case DLT_IEEE802_11:
  7272. case DLT_PRISM_HEADER:
  7273. case DLT_IEEE802_11_RADIO_AVS:
  7274. case DLT_IEEE802_11_RADIO:
  7275. b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num);
  7276. break;
  7277. default:
  7278. bpf_error(cstate, "no VLAN support for data link type %d",
  7279. cstate->linktype);
  7280. /*NOTREACHED*/
  7281. }
  7282. cstate->vlan_stack_depth++;
  7283. return (b0);
  7284. }
  7285. /*
  7286. * support for MPLS
  7287. */
  7288. struct block *
  7289. gen_mpls(compiler_state_t *cstate, int label_num)
  7290. {
  7291. struct block *b0, *b1;
  7292. if (cstate->label_stack_depth > 0) {
  7293. /* just match the bottom-of-stack bit clear */
  7294. b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
  7295. } else {
  7296. /*
  7297. * We're not in an MPLS stack yet, so check the link-layer
  7298. * type against MPLS.
  7299. */
  7300. switch (cstate->linktype) {
  7301. case DLT_C_HDLC: /* fall through */
  7302. case DLT_EN10MB:
  7303. case DLT_NETANALYZER:
  7304. case DLT_NETANALYZER_TRANSPARENT:
  7305. b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
  7306. break;
  7307. case DLT_PPP:
  7308. b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
  7309. break;
  7310. /* FIXME add other DLT_s ...
  7311. * for Frame-Relay/and ATM this may get messy due to SNAP headers
  7312. * leave it for now */
  7313. default:
  7314. bpf_error(cstate, "no MPLS support for data link type %d",
  7315. cstate->linktype);
  7316. /*NOTREACHED*/
  7317. break;
  7318. }
  7319. }
  7320. /* If a specific MPLS label is requested, check it */
  7321. if (label_num >= 0) {
  7322. label_num = label_num << 12; /* label is shifted 12 bits on the wire */
  7323. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, (bpf_int32)label_num,
  7324. 0xfffff000); /* only compare the first 20 bits */
  7325. gen_and(b0, b1);
  7326. b0 = b1;
  7327. }
  7328. /*
  7329. * Change the offsets to point to the type and data fields within
  7330. * the MPLS packet. Just increment the offsets, so that we
  7331. * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
  7332. * capture packets with an outer label of 100000 and an inner
  7333. * label of 1024.
  7334. *
  7335. * Increment the MPLS stack depth as well; this indicates that
  7336. * we're checking MPLS-encapsulated headers, to make sure higher
  7337. * level code generators don't try to match against IP-related
  7338. * protocols such as Q_ARP, Q_RARP etc.
  7339. *
  7340. * XXX - this is a bit of a kludge. See comments in gen_vlan().
  7341. */
  7342. cstate->off_nl_nosnap += 4;
  7343. cstate->off_nl += 4;
  7344. cstate->label_stack_depth++;
  7345. return (b0);
  7346. }
  7347. /*
  7348. * Support PPPOE discovery and session.
  7349. */
  7350. struct block *
  7351. gen_pppoed(compiler_state_t *cstate)
  7352. {
  7353. /* check for PPPoE discovery */
  7354. return gen_linktype(cstate, (bpf_int32)ETHERTYPE_PPPOED);
  7355. }
  7356. struct block *
  7357. gen_pppoes(compiler_state_t *cstate, int sess_num)
  7358. {
  7359. struct block *b0, *b1;
  7360. /*
  7361. * Test against the PPPoE session link-layer type.
  7362. */
  7363. b0 = gen_linktype(cstate, (bpf_int32)ETHERTYPE_PPPOES);
  7364. /* If a specific session is requested, check PPPoE session id */
  7365. if (sess_num >= 0) {
  7366. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W,
  7367. (bpf_int32)sess_num, 0x0000ffff);
  7368. gen_and(b0, b1);
  7369. b0 = b1;
  7370. }
  7371. /*
  7372. * Change the offsets to point to the type and data fields within
  7373. * the PPP packet, and note that this is PPPoE rather than
  7374. * raw PPP.
  7375. *
  7376. * XXX - this is a bit of a kludge. If we were to split the
  7377. * compiler into a parser that parses an expression and
  7378. * generates an expression tree, and a code generator that
  7379. * takes an expression tree (which could come from our
  7380. * parser or from some other parser) and generates BPF code,
  7381. * we could perhaps make the offsets parameters of routines
  7382. * and, in the handler for an "AND" node, pass to subnodes
  7383. * other than the PPPoE node the adjusted offsets.
  7384. *
  7385. * This would mean that "pppoes" would, instead of changing the
  7386. * behavior of *all* tests after it, change only the behavior
  7387. * of tests ANDed with it. That would change the documented
  7388. * semantics of "pppoes", which might break some expressions.
  7389. * However, it would mean that "(pppoes and ip) or ip" would check
  7390. * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
  7391. * checking only for VLAN-encapsulated IP, so that could still
  7392. * be considered worth doing; it wouldn't break expressions
  7393. * that are of the form "pppoes and ..." which I suspect are the
  7394. * most common expressions involving "pppoes". "pppoes or ..."
  7395. * doesn't necessarily do what the user would really want, now,
  7396. * as all the "or ..." tests would be done assuming PPPoE, even
  7397. * though the "or" could be viewed as meaning "or, if this isn't
  7398. * a PPPoE packet...".
  7399. *
  7400. * The "network-layer" protocol is PPPoE, which has a 6-byte
  7401. * PPPoE header, followed by a PPP packet.
  7402. *
  7403. * There is no HDLC encapsulation for the PPP packet (it's
  7404. * encapsulated in PPPoES instead), so the link-layer type
  7405. * starts at the first byte of the PPP packet. For PPPoE,
  7406. * that offset is relative to the beginning of the total
  7407. * link-layer payload, including any 802.2 LLC header, so
  7408. * it's 6 bytes past cstate->off_nl.
  7409. */
  7410. PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
  7411. cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
  7412. cstate->off_linkpl.reg);
  7413. cstate->off_linktype = cstate->off_linkhdr;
  7414. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
  7415. cstate->off_nl = 0;
  7416. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  7417. return b0;
  7418. }
  7419. /* Check that this is Geneve and the VNI is correct if
  7420. * specified. Parameterized to handle both IPv4 and IPv6. */
  7421. static struct block *
  7422. gen_geneve_check(compiler_state_t *cstate,
  7423. struct block *(*gen_portfn)(compiler_state_t *, int, int, int),
  7424. enum e_offrel offrel, int vni)
  7425. {
  7426. struct block *b0, *b1;
  7427. b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
  7428. /* Check that we are operating on version 0. Otherwise, we
  7429. * can't decode the rest of the fields. The version is 2 bits
  7430. * in the first byte of the Geneve header. */
  7431. b1 = gen_mcmp(cstate, offrel, 8, BPF_B, (bpf_int32)0, 0xc0);
  7432. gen_and(b0, b1);
  7433. b0 = b1;
  7434. if (vni >= 0) {
  7435. vni <<= 8; /* VNI is in the upper 3 bytes */
  7436. b1 = gen_mcmp(cstate, offrel, 12, BPF_W, (bpf_int32)vni,
  7437. 0xffffff00);
  7438. gen_and(b0, b1);
  7439. b0 = b1;
  7440. }
  7441. return b0;
  7442. }
  7443. /* The IPv4 and IPv6 Geneve checks need to do two things:
  7444. * - Verify that this actually is Geneve with the right VNI.
  7445. * - Place the IP header length (plus variable link prefix if
  7446. * needed) into register A to be used later to compute
  7447. * the inner packet offsets. */
  7448. static struct block *
  7449. gen_geneve4(compiler_state_t *cstate, int vni)
  7450. {
  7451. struct block *b0, *b1;
  7452. struct slist *s, *s1;
  7453. b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni);
  7454. /* Load the IP header length into A. */
  7455. s = gen_loadx_iphdrlen(cstate);
  7456. s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
  7457. sappend(s, s1);
  7458. /* Forcibly append these statements to the true condition
  7459. * of the protocol check by creating a new block that is
  7460. * always true and ANDing them. */
  7461. b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
  7462. b1->stmts = s;
  7463. b1->s.k = 0;
  7464. gen_and(b0, b1);
  7465. return b1;
  7466. }
  7467. static struct block *
  7468. gen_geneve6(compiler_state_t *cstate, int vni)
  7469. {
  7470. struct block *b0, *b1;
  7471. struct slist *s, *s1;
  7472. b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni);
  7473. /* Load the IP header length. We need to account for a
  7474. * variable length link prefix if there is one. */
  7475. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  7476. if (s) {
  7477. s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
  7478. s1->s.k = 40;
  7479. sappend(s, s1);
  7480. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
  7481. s1->s.k = 0;
  7482. sappend(s, s1);
  7483. } else {
  7484. s = new_stmt(cstate, BPF_LD|BPF_IMM);
  7485. s->s.k = 40;
  7486. }
  7487. /* Forcibly append these statements to the true condition
  7488. * of the protocol check by creating a new block that is
  7489. * always true and ANDing them. */
  7490. s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  7491. sappend(s, s1);
  7492. b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
  7493. b1->stmts = s;
  7494. b1->s.k = 0;
  7495. gen_and(b0, b1);
  7496. return b1;
  7497. }
  7498. /* We need to store three values based on the Geneve header::
  7499. * - The offset of the linktype.
  7500. * - The offset of the end of the Geneve header.
  7501. * - The offset of the end of the encapsulated MAC header. */
  7502. static struct slist *
  7503. gen_geneve_offsets(compiler_state_t *cstate)
  7504. {
  7505. struct slist *s, *s1, *s_proto;
  7506. /* First we need to calculate the offset of the Geneve header
  7507. * itself. This is composed of the IP header previously calculated
  7508. * (include any variable link prefix) and stored in A plus the
  7509. * fixed sized headers (fixed link prefix, MAC length, and UDP
  7510. * header). */
  7511. s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  7512. s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
  7513. /* Stash this in X since we'll need it later. */
  7514. s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  7515. sappend(s, s1);
  7516. /* The EtherType in Geneve is 2 bytes in. Calculate this and
  7517. * store it. */
  7518. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  7519. s1->s.k = 2;
  7520. sappend(s, s1);
  7521. cstate->off_linktype.reg = alloc_reg(cstate);
  7522. cstate->off_linktype.is_variable = 1;
  7523. cstate->off_linktype.constant_part = 0;
  7524. s1 = new_stmt(cstate, BPF_ST);
  7525. s1->s.k = cstate->off_linktype.reg;
  7526. sappend(s, s1);
  7527. /* Load the Geneve option length and mask and shift to get the
  7528. * number of bytes. It is stored in the first byte of the Geneve
  7529. * header. */
  7530. s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  7531. s1->s.k = 0;
  7532. sappend(s, s1);
  7533. s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  7534. s1->s.k = 0x3f;
  7535. sappend(s, s1);
  7536. s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
  7537. s1->s.k = 4;
  7538. sappend(s, s1);
  7539. /* Add in the rest of the Geneve base header. */
  7540. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  7541. s1->s.k = 8;
  7542. sappend(s, s1);
  7543. /* Add the Geneve header length to its offset and store. */
  7544. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
  7545. s1->s.k = 0;
  7546. sappend(s, s1);
  7547. /* Set the encapsulated type as Ethernet. Even though we may
  7548. * not actually have Ethernet inside there are two reasons this
  7549. * is useful:
  7550. * - The linktype field is always in EtherType format regardless
  7551. * of whether it is in Geneve or an inner Ethernet frame.
  7552. * - The only link layer that we have specific support for is
  7553. * Ethernet. We will confirm that the packet actually is
  7554. * Ethernet at runtime before executing these checks. */
  7555. PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
  7556. s1 = new_stmt(cstate, BPF_ST);
  7557. s1->s.k = cstate->off_linkhdr.reg;
  7558. sappend(s, s1);
  7559. /* Calculate whether we have an Ethernet header or just raw IP/
  7560. * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
  7561. * and linktype by 14 bytes so that the network header can be found
  7562. * seamlessly. Otherwise, keep what we've calculated already. */
  7563. /* We have a bare jmp so we can't use the optimizer. */
  7564. cstate->no_optimize = 1;
  7565. /* Load the EtherType in the Geneve header, 2 bytes in. */
  7566. s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
  7567. s1->s.k = 2;
  7568. sappend(s, s1);
  7569. /* Load X with the end of the Geneve header. */
  7570. s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
  7571. s1->s.k = cstate->off_linkhdr.reg;
  7572. sappend(s, s1);
  7573. /* Check if the EtherType is Transparent Ethernet Bridging. At the
  7574. * end of this check, we should have the total length in X. In
  7575. * the non-Ethernet case, it's already there. */
  7576. s_proto = new_stmt(cstate, JMP(BPF_JEQ));
  7577. s_proto->s.k = ETHERTYPE_TEB;
  7578. sappend(s, s_proto);
  7579. s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
  7580. sappend(s, s1);
  7581. s_proto->s.jt = s1;
  7582. /* Since this is Ethernet, use the EtherType of the payload
  7583. * directly as the linktype. Overwrite what we already have. */
  7584. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  7585. s1->s.k = 12;
  7586. sappend(s, s1);
  7587. s1 = new_stmt(cstate, BPF_ST);
  7588. s1->s.k = cstate->off_linktype.reg;
  7589. sappend(s, s1);
  7590. /* Advance two bytes further to get the end of the Ethernet
  7591. * header. */
  7592. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  7593. s1->s.k = 2;
  7594. sappend(s, s1);
  7595. /* Move the result to X. */
  7596. s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  7597. sappend(s, s1);
  7598. /* Store the final result of our linkpl calculation. */
  7599. cstate->off_linkpl.reg = alloc_reg(cstate);
  7600. cstate->off_linkpl.is_variable = 1;
  7601. cstate->off_linkpl.constant_part = 0;
  7602. s1 = new_stmt(cstate, BPF_STX);
  7603. s1->s.k = cstate->off_linkpl.reg;
  7604. sappend(s, s1);
  7605. s_proto->s.jf = s1;
  7606. cstate->off_nl = 0;
  7607. return s;
  7608. }
  7609. /* Check to see if this is a Geneve packet. */
  7610. struct block *
  7611. gen_geneve(compiler_state_t *cstate, int vni)
  7612. {
  7613. struct block *b0, *b1;
  7614. struct slist *s;
  7615. b0 = gen_geneve4(cstate, vni);
  7616. b1 = gen_geneve6(cstate, vni);
  7617. gen_or(b0, b1);
  7618. b0 = b1;
  7619. /* Later filters should act on the payload of the Geneve frame,
  7620. * update all of the header pointers. Attach this code so that
  7621. * it gets executed in the event that the Geneve filter matches. */
  7622. s = gen_geneve_offsets(cstate);
  7623. b1 = gen_true(cstate);
  7624. sappend(s, b1->stmts);
  7625. b1->stmts = s;
  7626. gen_and(b0, b1);
  7627. cstate->is_geneve = 1;
  7628. return b1;
  7629. }
  7630. /* Check that the encapsulated frame has a link layer header
  7631. * for Ethernet filters. */
  7632. static struct block *
  7633. gen_geneve_ll_check(compiler_state_t *cstate)
  7634. {
  7635. struct block *b0;
  7636. struct slist *s, *s1;
  7637. /* The easiest way to see if there is a link layer present
  7638. * is to check if the link layer header and payload are not
  7639. * the same. */
  7640. /* Geneve always generates pure variable offsets so we can
  7641. * compare only the registers. */
  7642. s = new_stmt(cstate, BPF_LD|BPF_MEM);
  7643. s->s.k = cstate->off_linkhdr.reg;
  7644. s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
  7645. s1->s.k = cstate->off_linkpl.reg;
  7646. sappend(s, s1);
  7647. b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
  7648. b0->stmts = s;
  7649. b0->s.k = 0;
  7650. gen_not(b0);
  7651. return b0;
  7652. }
  7653. struct block *
  7654. gen_atmfield_code(compiler_state_t *cstate, int atmfield, bpf_int32 jvalue,
  7655. bpf_u_int32 jtype, int reverse)
  7656. {
  7657. struct block *b0;
  7658. switch (atmfield) {
  7659. case A_VPI:
  7660. if (!cstate->is_atm)
  7661. bpf_error(cstate, "'vpi' supported only on raw ATM");
  7662. if (cstate->off_vpi == OFFSET_NOT_SET)
  7663. abort();
  7664. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B, 0xffffffff, jtype,
  7665. reverse, jvalue);
  7666. break;
  7667. case A_VCI:
  7668. if (!cstate->is_atm)
  7669. bpf_error(cstate, "'vci' supported only on raw ATM");
  7670. if (cstate->off_vci == OFFSET_NOT_SET)
  7671. abort();
  7672. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H, 0xffffffff, jtype,
  7673. reverse, jvalue);
  7674. break;
  7675. case A_PROTOTYPE:
  7676. if (cstate->off_proto == OFFSET_NOT_SET)
  7677. abort(); /* XXX - this isn't on FreeBSD */
  7678. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 0x0f, jtype,
  7679. reverse, jvalue);
  7680. break;
  7681. case A_MSGTYPE:
  7682. if (cstate->off_payload == OFFSET_NOT_SET)
  7683. abort();
  7684. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
  7685. 0xffffffff, jtype, reverse, jvalue);
  7686. break;
  7687. case A_CALLREFTYPE:
  7688. if (!cstate->is_atm)
  7689. bpf_error(cstate, "'callref' supported only on raw ATM");
  7690. if (cstate->off_proto == OFFSET_NOT_SET)
  7691. abort();
  7692. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 0xffffffff,
  7693. jtype, reverse, jvalue);
  7694. break;
  7695. default:
  7696. abort();
  7697. }
  7698. return b0;
  7699. }
  7700. struct block *
  7701. gen_atmtype_abbrev(compiler_state_t *cstate, int type)
  7702. {
  7703. struct block *b0, *b1;
  7704. switch (type) {
  7705. case A_METAC:
  7706. /* Get all packets in Meta signalling Circuit */
  7707. if (!cstate->is_atm)
  7708. bpf_error(cstate, "'metac' supported only on raw ATM");
  7709. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7710. b1 = gen_atmfield_code(cstate, A_VCI, 1, BPF_JEQ, 0);
  7711. gen_and(b0, b1);
  7712. break;
  7713. case A_BCC:
  7714. /* Get all packets in Broadcast Circuit*/
  7715. if (!cstate->is_atm)
  7716. bpf_error(cstate, "'bcc' supported only on raw ATM");
  7717. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7718. b1 = gen_atmfield_code(cstate, A_VCI, 2, BPF_JEQ, 0);
  7719. gen_and(b0, b1);
  7720. break;
  7721. case A_OAMF4SC:
  7722. /* Get all cells in Segment OAM F4 circuit*/
  7723. if (!cstate->is_atm)
  7724. bpf_error(cstate, "'oam4sc' supported only on raw ATM");
  7725. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7726. b1 = gen_atmfield_code(cstate, A_VCI, 3, BPF_JEQ, 0);
  7727. gen_and(b0, b1);
  7728. break;
  7729. case A_OAMF4EC:
  7730. /* Get all cells in End-to-End OAM F4 Circuit*/
  7731. if (!cstate->is_atm)
  7732. bpf_error(cstate, "'oam4ec' supported only on raw ATM");
  7733. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7734. b1 = gen_atmfield_code(cstate, A_VCI, 4, BPF_JEQ, 0);
  7735. gen_and(b0, b1);
  7736. break;
  7737. case A_SC:
  7738. /* Get all packets in connection Signalling Circuit */
  7739. if (!cstate->is_atm)
  7740. bpf_error(cstate, "'sc' supported only on raw ATM");
  7741. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7742. b1 = gen_atmfield_code(cstate, A_VCI, 5, BPF_JEQ, 0);
  7743. gen_and(b0, b1);
  7744. break;
  7745. case A_ILMIC:
  7746. /* Get all packets in ILMI Circuit */
  7747. if (!cstate->is_atm)
  7748. bpf_error(cstate, "'ilmic' supported only on raw ATM");
  7749. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7750. b1 = gen_atmfield_code(cstate, A_VCI, 16, BPF_JEQ, 0);
  7751. gen_and(b0, b1);
  7752. break;
  7753. case A_LANE:
  7754. /* Get all LANE packets */
  7755. if (!cstate->is_atm)
  7756. bpf_error(cstate, "'lane' supported only on raw ATM");
  7757. b1 = gen_atmfield_code(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
  7758. /*
  7759. * Arrange that all subsequent tests assume LANE
  7760. * rather than LLC-encapsulated packets, and set
  7761. * the offsets appropriately for LANE-encapsulated
  7762. * Ethernet.
  7763. *
  7764. * We assume LANE means Ethernet, not Token Ring.
  7765. */
  7766. PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
  7767. cstate->off_payload + 2, /* Ethernet header */
  7768. -1);
  7769. cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
  7770. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */
  7771. cstate->off_nl = 0; /* Ethernet II */
  7772. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  7773. break;
  7774. case A_LLC:
  7775. /* Get all LLC-encapsulated packets */
  7776. if (!cstate->is_atm)
  7777. bpf_error(cstate, "'llc' supported only on raw ATM");
  7778. b1 = gen_atmfield_code(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
  7779. cstate->linktype = cstate->prevlinktype;
  7780. break;
  7781. default:
  7782. abort();
  7783. }
  7784. return b1;
  7785. }
  7786. /*
  7787. * Filtering for MTP2 messages based on li value
  7788. * FISU, length is null
  7789. * LSSU, length is 1 or 2
  7790. * MSU, length is 3 or more
  7791. * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
  7792. */
  7793. struct block *
  7794. gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
  7795. {
  7796. struct block *b0, *b1;
  7797. switch (type) {
  7798. case M_FISU:
  7799. if ( (cstate->linktype != DLT_MTP2) &&
  7800. (cstate->linktype != DLT_ERF) &&
  7801. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  7802. bpf_error(cstate, "'fisu' supported only on MTP2");
  7803. /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
  7804. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
  7805. break;
  7806. case M_LSSU:
  7807. if ( (cstate->linktype != DLT_MTP2) &&
  7808. (cstate->linktype != DLT_ERF) &&
  7809. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  7810. bpf_error(cstate, "'lssu' supported only on MTP2");
  7811. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
  7812. b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
  7813. gen_and(b1, b0);
  7814. break;
  7815. case M_MSU:
  7816. if ( (cstate->linktype != DLT_MTP2) &&
  7817. (cstate->linktype != DLT_ERF) &&
  7818. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  7819. bpf_error(cstate, "'msu' supported only on MTP2");
  7820. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
  7821. break;
  7822. case MH_FISU:
  7823. if ( (cstate->linktype != DLT_MTP2) &&
  7824. (cstate->linktype != DLT_ERF) &&
  7825. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  7826. bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
  7827. /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
  7828. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JEQ, 0, 0);
  7829. break;
  7830. case MH_LSSU:
  7831. if ( (cstate->linktype != DLT_MTP2) &&
  7832. (cstate->linktype != DLT_ERF) &&
  7833. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  7834. bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
  7835. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 1, 0x0100);
  7836. b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0);
  7837. gen_and(b1, b0);
  7838. break;
  7839. case MH_MSU:
  7840. if ( (cstate->linktype != DLT_MTP2) &&
  7841. (cstate->linktype != DLT_ERF) &&
  7842. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  7843. bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
  7844. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0x0100);
  7845. break;
  7846. default:
  7847. abort();
  7848. }
  7849. return b0;
  7850. }
  7851. struct block *
  7852. gen_mtp3field_code(compiler_state_t *cstate, int mtp3field, bpf_u_int32 jvalue,
  7853. bpf_u_int32 jtype, int reverse)
  7854. {
  7855. struct block *b0;
  7856. bpf_u_int32 val1 , val2 , val3;
  7857. u_int newoff_sio = cstate->off_sio;
  7858. u_int newoff_opc = cstate->off_opc;
  7859. u_int newoff_dpc = cstate->off_dpc;
  7860. u_int newoff_sls = cstate->off_sls;
  7861. switch (mtp3field) {
  7862. case MH_SIO:
  7863. newoff_sio += 3; /* offset for MTP2_HSL */
  7864. /* FALLTHROUGH */
  7865. case M_SIO:
  7866. if (cstate->off_sio == OFFSET_NOT_SET)
  7867. bpf_error(cstate, "'sio' supported only on SS7");
  7868. /* sio coded on 1 byte so max value 255 */
  7869. if(jvalue > 255)
  7870. bpf_error(cstate, "sio value %u too big; max value = 255",
  7871. jvalue);
  7872. b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffff,
  7873. (u_int)jtype, reverse, (u_int)jvalue);
  7874. break;
  7875. case MH_OPC:
  7876. newoff_opc+=3;
  7877. case M_OPC:
  7878. if (cstate->off_opc == OFFSET_NOT_SET)
  7879. bpf_error(cstate, "'opc' supported only on SS7");
  7880. /* opc coded on 14 bits so max value 16383 */
  7881. if (jvalue > 16383)
  7882. bpf_error(cstate, "opc value %u too big; max value = 16383",
  7883. jvalue);
  7884. /* the following instructions are made to convert jvalue
  7885. * to the form used to write opc in an ss7 message*/
  7886. val1 = jvalue & 0x00003c00;
  7887. val1 = val1 >>10;
  7888. val2 = jvalue & 0x000003fc;
  7889. val2 = val2 <<6;
  7890. val3 = jvalue & 0x00000003;
  7891. val3 = val3 <<22;
  7892. jvalue = val1 + val2 + val3;
  7893. b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0f,
  7894. (u_int)jtype, reverse, (u_int)jvalue);
  7895. break;
  7896. case MH_DPC:
  7897. newoff_dpc += 3;
  7898. /* FALLTHROUGH */
  7899. case M_DPC:
  7900. if (cstate->off_dpc == OFFSET_NOT_SET)
  7901. bpf_error(cstate, "'dpc' supported only on SS7");
  7902. /* dpc coded on 14 bits so max value 16383 */
  7903. if (jvalue > 16383)
  7904. bpf_error(cstate, "dpc value %u too big; max value = 16383",
  7905. jvalue);
  7906. /* the following instructions are made to convert jvalue
  7907. * to the forme used to write dpc in an ss7 message*/
  7908. val1 = jvalue & 0x000000ff;
  7909. val1 = val1 << 24;
  7910. val2 = jvalue & 0x00003f00;
  7911. val2 = val2 << 8;
  7912. jvalue = val1 + val2;
  7913. b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000,
  7914. (u_int)jtype, reverse, (u_int)jvalue);
  7915. break;
  7916. case MH_SLS:
  7917. newoff_sls+=3;
  7918. case M_SLS:
  7919. if (cstate->off_sls == OFFSET_NOT_SET)
  7920. bpf_error(cstate, "'sls' supported only on SS7");
  7921. /* sls coded on 4 bits so max value 15 */
  7922. if (jvalue > 15)
  7923. bpf_error(cstate, "sls value %u too big; max value = 15",
  7924. jvalue);
  7925. /* the following instruction is made to convert jvalue
  7926. * to the forme used to write sls in an ss7 message*/
  7927. jvalue = jvalue << 4;
  7928. b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0,
  7929. (u_int)jtype,reverse, (u_int)jvalue);
  7930. break;
  7931. default:
  7932. abort();
  7933. }
  7934. return b0;
  7935. }
  7936. static struct block *
  7937. gen_msg_abbrev(compiler_state_t *cstate, int type)
  7938. {
  7939. struct block *b1;
  7940. /*
  7941. * Q.2931 signalling protocol messages for handling virtual circuits
  7942. * establishment and teardown
  7943. */
  7944. switch (type) {
  7945. case A_SETUP:
  7946. b1 = gen_atmfield_code(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
  7947. break;
  7948. case A_CALLPROCEED:
  7949. b1 = gen_atmfield_code(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
  7950. break;
  7951. case A_CONNECT:
  7952. b1 = gen_atmfield_code(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
  7953. break;
  7954. case A_CONNECTACK:
  7955. b1 = gen_atmfield_code(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
  7956. break;
  7957. case A_RELEASE:
  7958. b1 = gen_atmfield_code(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
  7959. break;
  7960. case A_RELEASE_DONE:
  7961. b1 = gen_atmfield_code(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
  7962. break;
  7963. default:
  7964. abort();
  7965. }
  7966. return b1;
  7967. }
  7968. struct block *
  7969. gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
  7970. {
  7971. struct block *b0, *b1;
  7972. switch (type) {
  7973. case A_OAM:
  7974. if (!cstate->is_atm)
  7975. bpf_error(cstate, "'oam' supported only on raw ATM");
  7976. b1 = gen_atmmulti_abbrev(cstate, A_OAMF4);
  7977. break;
  7978. case A_OAMF4:
  7979. if (!cstate->is_atm)
  7980. bpf_error(cstate, "'oamf4' supported only on raw ATM");
  7981. /* OAM F4 type */
  7982. b0 = gen_atmfield_code(cstate, A_VCI, 3, BPF_JEQ, 0);
  7983. b1 = gen_atmfield_code(cstate, A_VCI, 4, BPF_JEQ, 0);
  7984. gen_or(b0, b1);
  7985. b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
  7986. gen_and(b0, b1);
  7987. break;
  7988. case A_CONNECTMSG:
  7989. /*
  7990. * Get Q.2931 signalling messages for switched
  7991. * virtual connection
  7992. */
  7993. if (!cstate->is_atm)
  7994. bpf_error(cstate, "'connectmsg' supported only on raw ATM");
  7995. b0 = gen_msg_abbrev(cstate, A_SETUP);
  7996. b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
  7997. gen_or(b0, b1);
  7998. b0 = gen_msg_abbrev(cstate, A_CONNECT);
  7999. gen_or(b0, b1);
  8000. b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
  8001. gen_or(b0, b1);
  8002. b0 = gen_msg_abbrev(cstate, A_RELEASE);
  8003. gen_or(b0, b1);
  8004. b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
  8005. gen_or(b0, b1);
  8006. b0 = gen_atmtype_abbrev(cstate, A_SC);
  8007. gen_and(b0, b1);
  8008. break;
  8009. case A_METACONNECT:
  8010. if (!cstate->is_atm)
  8011. bpf_error(cstate, "'metaconnect' supported only on raw ATM");
  8012. b0 = gen_msg_abbrev(cstate, A_SETUP);
  8013. b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
  8014. gen_or(b0, b1);
  8015. b0 = gen_msg_abbrev(cstate, A_CONNECT);
  8016. gen_or(b0, b1);
  8017. b0 = gen_msg_abbrev(cstate, A_RELEASE);
  8018. gen_or(b0, b1);
  8019. b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
  8020. gen_or(b0, b1);
  8021. b0 = gen_atmtype_abbrev(cstate, A_METAC);
  8022. gen_and(b0, b1);
  8023. break;
  8024. default:
  8025. abort();
  8026. }
  8027. return b1;
  8028. }