if.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
  5. * Copyright (c) 1980, 1986, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include "opt_bpf.h"
  33. #include "opt_inet6.h"
  34. #include "opt_inet.h"
  35. #include "opt_ddb.h"
  36. #include <sys/param.h>
  37. #include <sys/capsicum.h>
  38. #include <sys/conf.h>
  39. #include <sys/eventhandler.h>
  40. #include <sys/malloc.h>
  41. #include <sys/domainset.h>
  42. #include <sys/sbuf.h>
  43. #include <sys/bus.h>
  44. #include <sys/epoch.h>
  45. #include <sys/mbuf.h>
  46. #include <sys/systm.h>
  47. #include <sys/priv.h>
  48. #include <sys/proc.h>
  49. #include <sys/socket.h>
  50. #include <sys/socketvar.h>
  51. #include <sys/protosw.h>
  52. #include <sys/kernel.h>
  53. #include <sys/lock.h>
  54. #include <sys/refcount.h>
  55. #include <sys/module.h>
  56. #include <sys/nv.h>
  57. #include <sys/rwlock.h>
  58. #include <sys/sockio.h>
  59. #include <sys/syslog.h>
  60. #include <sys/sysctl.h>
  61. #include <sys/sysent.h>
  62. #include <sys/taskqueue.h>
  63. #include <sys/domain.h>
  64. #include <sys/jail.h>
  65. #include <sys/priv.h>
  66. #ifdef DDB
  67. #include <ddb/ddb.h>
  68. #endif
  69. #include <machine/stdarg.h>
  70. #include <vm/uma.h>
  71. #include <net/bpf.h>
  72. #include <net/ethernet.h>
  73. #include <net/if.h>
  74. #include <net/if_arp.h>
  75. #include <net/if_clone.h>
  76. #include <net/if_dl.h>
  77. #include <net/if_strings.h>
  78. #include <net/if_types.h>
  79. #include <net/if_var.h>
  80. #include <net/if_media.h>
  81. #include <net/if_mib.h>
  82. #include <net/if_private.h>
  83. #include <net/if_vlan_var.h>
  84. #include <net/radix.h>
  85. #include <net/route.h>
  86. #include <net/route/route_ctl.h>
  87. #include <net/vnet.h>
  88. #if defined(INET) || defined(INET6)
  89. #include <net/ethernet.h>
  90. #include <netinet/in.h>
  91. #include <netinet/in_var.h>
  92. #include <netinet/ip.h>
  93. #include <netinet/ip_carp.h>
  94. #ifdef INET
  95. #include <net/debugnet.h>
  96. #include <netinet/if_ether.h>
  97. #endif /* INET */
  98. #ifdef INET6
  99. #include <netinet6/in6_var.h>
  100. #include <netinet6/in6_ifattach.h>
  101. #endif /* INET6 */
  102. #endif /* INET || INET6 */
  103. #include <security/mac/mac_framework.h>
  104. /*
  105. * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
  106. * and ifr_ifru when it is used in SIOCGIFCONF.
  107. */
  108. _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
  109. offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
  110. __read_mostly epoch_t net_epoch_preempt;
  111. #ifdef COMPAT_FREEBSD32
  112. #include <sys/mount.h>
  113. #include <compat/freebsd32/freebsd32.h>
  114. struct ifreq_buffer32 {
  115. uint32_t length; /* (size_t) */
  116. uint32_t buffer; /* (void *) */
  117. };
  118. /*
  119. * Interface request structure used for socket
  120. * ioctl's. All interface ioctl's must have parameter
  121. * definitions which begin with ifr_name. The
  122. * remainder may be interface specific.
  123. */
  124. struct ifreq32 {
  125. char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
  126. union {
  127. struct sockaddr ifru_addr;
  128. struct sockaddr ifru_dstaddr;
  129. struct sockaddr ifru_broadaddr;
  130. struct ifreq_buffer32 ifru_buffer;
  131. short ifru_flags[2];
  132. short ifru_index;
  133. int ifru_jid;
  134. int ifru_metric;
  135. int ifru_mtu;
  136. int ifru_phys;
  137. int ifru_media;
  138. uint32_t ifru_data;
  139. int ifru_cap[2];
  140. u_int ifru_fib;
  141. u_char ifru_vlan_pcp;
  142. } ifr_ifru;
  143. };
  144. CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
  145. CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
  146. __offsetof(struct ifreq32, ifr_ifru));
  147. struct ifconf32 {
  148. int32_t ifc_len;
  149. union {
  150. uint32_t ifcu_buf;
  151. uint32_t ifcu_req;
  152. } ifc_ifcu;
  153. };
  154. #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
  155. struct ifdrv32 {
  156. char ifd_name[IFNAMSIZ];
  157. uint32_t ifd_cmd;
  158. uint32_t ifd_len;
  159. uint32_t ifd_data;
  160. };
  161. #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
  162. #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
  163. struct ifgroupreq32 {
  164. char ifgr_name[IFNAMSIZ];
  165. u_int ifgr_len;
  166. union {
  167. char ifgru_group[IFNAMSIZ];
  168. uint32_t ifgru_groups;
  169. } ifgr_ifgru;
  170. };
  171. #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
  172. #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
  173. #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
  174. #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
  175. struct ifmediareq32 {
  176. char ifm_name[IFNAMSIZ];
  177. int ifm_current;
  178. int ifm_mask;
  179. int ifm_status;
  180. int ifm_active;
  181. int ifm_count;
  182. uint32_t ifm_ulist; /* (int *) */
  183. };
  184. #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
  185. #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
  186. #endif /* COMPAT_FREEBSD32 */
  187. union ifreq_union {
  188. struct ifreq ifr;
  189. #ifdef COMPAT_FREEBSD32
  190. struct ifreq32 ifr32;
  191. #endif
  192. };
  193. SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  194. "Link layers");
  195. SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  196. "Generic link-management");
  197. SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
  198. &ifqmaxlen, 0, "max send queue size");
  199. /* Log link state change events */
  200. static int log_link_state_change = 1;
  201. SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
  202. &log_link_state_change, 0,
  203. "log interface link state change events");
  204. /* Log promiscuous mode change events */
  205. static int log_promisc_mode_change = 1;
  206. SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
  207. &log_promisc_mode_change, 1,
  208. "log promiscuous mode change events");
  209. /* Interface description */
  210. static unsigned int ifdescr_maxlen = 1024;
  211. SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
  212. &ifdescr_maxlen, 0,
  213. "administrative maximum length for interface description");
  214. static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
  215. /* global sx for non-critical path ifdescr */
  216. static struct sx ifdescr_sx;
  217. SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
  218. void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
  219. void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
  220. /* These are external hooks for CARP. */
  221. void (*carp_linkstate_p)(struct ifnet *ifp);
  222. void (*carp_demote_adj_p)(int, char *);
  223. int (*carp_master_p)(struct ifaddr *);
  224. #if defined(INET) || defined(INET6)
  225. int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
  226. int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
  227. const struct sockaddr *sa);
  228. int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
  229. int (*carp_attach_p)(struct ifaddr *, int);
  230. void (*carp_detach_p)(struct ifaddr *, bool);
  231. #endif
  232. #ifdef INET
  233. int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
  234. #endif
  235. #ifdef INET6
  236. struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
  237. caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
  238. const struct in6_addr *taddr);
  239. #endif
  240. struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
  241. /*
  242. * XXX: Style; these should be sorted alphabetically, and unprototyped
  243. * static functions should be prototyped. Currently they are sorted by
  244. * declaration order.
  245. */
  246. static void if_attachdomain(void *);
  247. static void if_attachdomain1(struct ifnet *);
  248. static int ifconf(u_long, caddr_t);
  249. static void if_input_default(struct ifnet *, struct mbuf *);
  250. static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
  251. static int if_setflag(struct ifnet *, int, int, int *, int);
  252. static int if_transmit_default(struct ifnet *ifp, struct mbuf *m);
  253. static void if_unroute(struct ifnet *, int flag, int fam);
  254. static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
  255. static void do_link_state_change(void *, int);
  256. static int if_getgroup(struct ifgroupreq *, struct ifnet *);
  257. static int if_getgroupmembers(struct ifgroupreq *);
  258. static void if_delgroups(struct ifnet *);
  259. static void if_attach_internal(struct ifnet *, bool);
  260. static int if_detach_internal(struct ifnet *, bool);
  261. static void if_siocaddmulti(void *, int);
  262. static void if_link_ifnet(struct ifnet *);
  263. static bool if_unlink_ifnet(struct ifnet *, bool);
  264. #ifdef VIMAGE
  265. static int if_vmove(struct ifnet *, struct vnet *);
  266. #endif
  267. #ifdef INET6
  268. /*
  269. * XXX: declare here to avoid to include many inet6 related files..
  270. * should be more generalized?
  271. */
  272. extern void nd6_setmtu(struct ifnet *);
  273. #endif
  274. /* ipsec helper hooks */
  275. VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
  276. VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
  277. int ifqmaxlen = IFQ_MAXLEN;
  278. VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
  279. VNET_DEFINE(struct ifgrouphead, ifg_head);
  280. /* Table of ifnet by index. */
  281. static int if_index;
  282. static int if_indexlim = 8;
  283. static struct ifindex_entry {
  284. struct ifnet *ife_ifnet;
  285. uint16_t ife_gencnt;
  286. } *ifindex_table;
  287. SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
  288. CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  289. "Variables global to all interfaces");
  290. static int
  291. sysctl_ifcount(SYSCTL_HANDLER_ARGS)
  292. {
  293. int rv = 0;
  294. IFNET_RLOCK();
  295. for (int i = 1; i <= if_index; i++)
  296. if (ifindex_table[i].ife_ifnet != NULL &&
  297. ifindex_table[i].ife_ifnet->if_vnet == curvnet)
  298. rv = i;
  299. IFNET_RUNLOCK();
  300. return (sysctl_handle_int(oidp, &rv, 0, req));
  301. }
  302. SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
  303. CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
  304. "Maximum known interface index");
  305. /*
  306. * The global network interface list (V_ifnet) and related state (such as
  307. * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
  308. * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
  309. */
  310. struct sx ifnet_sxlock;
  311. SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
  312. struct sx ifnet_detach_sxlock;
  313. SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
  314. SX_RECURSE);
  315. #ifdef VIMAGE
  316. #define VNET_IS_SHUTTING_DOWN(_vnet) \
  317. ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
  318. #endif
  319. static if_com_alloc_t *if_com_alloc[256];
  320. static if_com_free_t *if_com_free[256];
  321. static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
  322. MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
  323. MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
  324. struct ifnet *
  325. ifnet_byindex(u_int idx)
  326. {
  327. struct ifnet *ifp;
  328. NET_EPOCH_ASSERT();
  329. if (__predict_false(idx > if_index))
  330. return (NULL);
  331. ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
  332. if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
  333. ifp = NULL;
  334. return (ifp);
  335. }
  336. struct ifnet *
  337. ifnet_byindex_ref(u_int idx)
  338. {
  339. struct ifnet *ifp;
  340. ifp = ifnet_byindex(idx);
  341. if (ifp == NULL || (ifp->if_flags & IFF_DYING))
  342. return (NULL);
  343. if (!if_try_ref(ifp))
  344. return (NULL);
  345. return (ifp);
  346. }
  347. struct ifnet *
  348. ifnet_byindexgen(uint16_t idx, uint16_t gen)
  349. {
  350. struct ifnet *ifp;
  351. NET_EPOCH_ASSERT();
  352. if (__predict_false(idx > if_index))
  353. return (NULL);
  354. ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
  355. if (ifindex_table[idx].ife_gencnt == gen)
  356. return (ifp);
  357. else
  358. return (NULL);
  359. }
  360. /*
  361. * Network interface utility routines.
  362. *
  363. * Routines with ifa_ifwith* names take sockaddr *'s as
  364. * parameters.
  365. */
  366. static void
  367. if_init_idxtable(void *arg __unused)
  368. {
  369. ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
  370. M_IFNET, M_WAITOK | M_ZERO);
  371. }
  372. SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
  373. static void
  374. vnet_if_init(const void *unused __unused)
  375. {
  376. CK_STAILQ_INIT(&V_ifnet);
  377. CK_STAILQ_INIT(&V_ifg_head);
  378. vnet_if_clone_init();
  379. }
  380. VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
  381. NULL);
  382. static void
  383. if_link_ifnet(struct ifnet *ifp)
  384. {
  385. IFNET_WLOCK();
  386. CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
  387. #ifdef VIMAGE
  388. curvnet->vnet_ifcnt++;
  389. #endif
  390. IFNET_WUNLOCK();
  391. }
  392. static bool
  393. if_unlink_ifnet(struct ifnet *ifp, bool vmove)
  394. {
  395. struct ifnet *iter;
  396. int found = 0;
  397. IFNET_WLOCK();
  398. CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
  399. if (iter == ifp) {
  400. CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
  401. if (!vmove)
  402. ifp->if_flags |= IFF_DYING;
  403. found = 1;
  404. break;
  405. }
  406. #ifdef VIMAGE
  407. curvnet->vnet_ifcnt--;
  408. #endif
  409. IFNET_WUNLOCK();
  410. return (found);
  411. }
  412. #ifdef VIMAGE
  413. static void
  414. vnet_if_return(const void *unused __unused)
  415. {
  416. struct ifnet *ifp, *nifp;
  417. struct ifnet **pending;
  418. int found __diagused;
  419. int i;
  420. i = 0;
  421. /*
  422. * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
  423. * enter NET_EPOCH, but that's not possible, because if_vmove() calls
  424. * if_detach_internal(), which waits for NET_EPOCH callbacks to
  425. * complete. We can't do that from within NET_EPOCH.
  426. *
  427. * However, we can also use the IFNET_xLOCK, which is the V_ifnet
  428. * read/write lock. We cannot hold the lock as we call if_vmove()
  429. * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
  430. * ctx lock.
  431. */
  432. IFNET_WLOCK();
  433. pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
  434. M_IFNET, M_WAITOK | M_ZERO);
  435. /* Return all inherited interfaces to their parent vnets. */
  436. CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
  437. if (ifp->if_home_vnet != ifp->if_vnet) {
  438. found = if_unlink_ifnet(ifp, true);
  439. MPASS(found);
  440. pending[i++] = ifp;
  441. }
  442. }
  443. IFNET_WUNLOCK();
  444. for (int j = 0; j < i; j++) {
  445. sx_xlock(&ifnet_detach_sxlock);
  446. if_vmove(pending[j], pending[j]->if_home_vnet);
  447. sx_xunlock(&ifnet_detach_sxlock);
  448. }
  449. free(pending, M_IFNET);
  450. }
  451. VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
  452. vnet_if_return, NULL);
  453. #endif
  454. /*
  455. * Allocate a struct ifnet and an index for an interface. A layer 2
  456. * common structure will also be allocated if an allocation routine is
  457. * registered for the passed type.
  458. */
  459. static struct ifnet *
  460. if_alloc_domain(u_char type, int numa_domain)
  461. {
  462. struct ifnet *ifp;
  463. u_short idx;
  464. KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
  465. if (numa_domain == IF_NODOM)
  466. ifp = malloc(sizeof(struct ifnet), M_IFNET,
  467. M_WAITOK | M_ZERO);
  468. else
  469. ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
  470. DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
  471. ifp->if_type = type;
  472. ifp->if_alloctype = type;
  473. ifp->if_numa_domain = numa_domain;
  474. #ifdef VIMAGE
  475. ifp->if_vnet = curvnet;
  476. #endif
  477. if (if_com_alloc[type] != NULL) {
  478. ifp->if_l2com = if_com_alloc[type](type, ifp);
  479. KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
  480. type));
  481. }
  482. IF_ADDR_LOCK_INIT(ifp);
  483. TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
  484. TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
  485. ifp->if_afdata_initialized = 0;
  486. IF_AFDATA_LOCK_INIT(ifp);
  487. CK_STAILQ_INIT(&ifp->if_addrhead);
  488. CK_STAILQ_INIT(&ifp->if_multiaddrs);
  489. CK_STAILQ_INIT(&ifp->if_groups);
  490. #ifdef MAC
  491. mac_ifnet_init(ifp);
  492. #endif
  493. ifq_init(&ifp->if_snd, ifp);
  494. refcount_init(&ifp->if_refcount, 1); /* Index reference. */
  495. for (int i = 0; i < IFCOUNTERS; i++)
  496. ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
  497. ifp->if_get_counter = if_get_counter_default;
  498. ifp->if_pcp = IFNET_PCP_NONE;
  499. /* Allocate an ifindex array entry. */
  500. IFNET_WLOCK();
  501. /*
  502. * Try to find an empty slot below if_index. If we fail, take the
  503. * next slot.
  504. */
  505. for (idx = 1; idx <= if_index; idx++) {
  506. if (ifindex_table[idx].ife_ifnet == NULL)
  507. break;
  508. }
  509. /* Catch if_index overflow. */
  510. if (idx >= if_indexlim) {
  511. struct ifindex_entry *new, *old;
  512. int newlim;
  513. newlim = if_indexlim * 2;
  514. new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
  515. memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
  516. old = ifindex_table;
  517. ck_pr_store_ptr(&ifindex_table, new);
  518. if_indexlim = newlim;
  519. NET_EPOCH_WAIT();
  520. free(old, M_IFNET);
  521. }
  522. if (idx > if_index)
  523. if_index = idx;
  524. ifp->if_index = idx;
  525. ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
  526. ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
  527. IFNET_WUNLOCK();
  528. return (ifp);
  529. }
  530. struct ifnet *
  531. if_alloc_dev(u_char type, device_t dev)
  532. {
  533. int numa_domain;
  534. if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
  535. return (if_alloc_domain(type, IF_NODOM));
  536. return (if_alloc_domain(type, numa_domain));
  537. }
  538. struct ifnet *
  539. if_alloc(u_char type)
  540. {
  541. return (if_alloc_domain(type, IF_NODOM));
  542. }
  543. /*
  544. * Do the actual work of freeing a struct ifnet, and layer 2 common
  545. * structure. This call is made when the network epoch guarantees
  546. * us that nobody holds a pointer to the interface.
  547. */
  548. static void
  549. if_free_deferred(epoch_context_t ctx)
  550. {
  551. struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
  552. KASSERT((ifp->if_flags & IFF_DYING),
  553. ("%s: interface not dying", __func__));
  554. if (if_com_free[ifp->if_alloctype] != NULL)
  555. if_com_free[ifp->if_alloctype](ifp->if_l2com,
  556. ifp->if_alloctype);
  557. #ifdef MAC
  558. mac_ifnet_destroy(ifp);
  559. #endif /* MAC */
  560. IF_AFDATA_DESTROY(ifp);
  561. IF_ADDR_LOCK_DESTROY(ifp);
  562. ifq_delete(&ifp->if_snd);
  563. for (int i = 0; i < IFCOUNTERS; i++)
  564. counter_u64_free(ifp->if_counters[i]);
  565. if_freedescr(ifp->if_description);
  566. free(ifp->if_hw_addr, M_IFADDR);
  567. free(ifp, M_IFNET);
  568. }
  569. /*
  570. * Deregister an interface and free the associated storage.
  571. */
  572. void
  573. if_free(struct ifnet *ifp)
  574. {
  575. ifp->if_flags |= IFF_DYING; /* XXX: Locking */
  576. /*
  577. * XXXGL: An interface index is really an alias to ifp pointer.
  578. * Why would we clear the alias now, and not in the deferred
  579. * context? Indeed there is nothing wrong with some network
  580. * thread obtaining ifp via ifnet_byindex() inside the network
  581. * epoch and then dereferencing ifp while we perform if_free(),
  582. * and after if_free() finished, too.
  583. *
  584. * This early index freeing was important back when ifindex was
  585. * virtualized and interface would outlive the vnet.
  586. */
  587. IFNET_WLOCK();
  588. MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
  589. ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
  590. ifindex_table[ifp->if_index].ife_gencnt++;
  591. while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
  592. if_index--;
  593. IFNET_WUNLOCK();
  594. if (refcount_release(&ifp->if_refcount))
  595. NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
  596. }
  597. /*
  598. * Interfaces to keep an ifnet type-stable despite the possibility of the
  599. * driver calling if_free(). If there are additional references, we defer
  600. * freeing the underlying data structure.
  601. */
  602. void
  603. if_ref(struct ifnet *ifp)
  604. {
  605. u_int old __diagused;
  606. /* We don't assert the ifnet list lock here, but arguably should. */
  607. old = refcount_acquire(&ifp->if_refcount);
  608. KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
  609. }
  610. bool
  611. if_try_ref(struct ifnet *ifp)
  612. {
  613. NET_EPOCH_ASSERT();
  614. return (refcount_acquire_if_not_zero(&ifp->if_refcount));
  615. }
  616. void
  617. if_rele(struct ifnet *ifp)
  618. {
  619. if (!refcount_release(&ifp->if_refcount))
  620. return;
  621. NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
  622. }
  623. void
  624. ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
  625. {
  626. mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
  627. if (ifq->ifq_maxlen == 0)
  628. ifq->ifq_maxlen = ifqmaxlen;
  629. ifq->altq_type = 0;
  630. ifq->altq_disc = NULL;
  631. ifq->altq_flags &= ALTQF_CANTCHANGE;
  632. ifq->altq_tbr = NULL;
  633. ifq->altq_ifp = ifp;
  634. }
  635. void
  636. ifq_delete(struct ifaltq *ifq)
  637. {
  638. mtx_destroy(&ifq->ifq_mtx);
  639. }
  640. /*
  641. * Perform generic interface initialization tasks and attach the interface
  642. * to the list of "active" interfaces. If vmove flag is set on entry
  643. * to if_attach_internal(), perform only a limited subset of initialization
  644. * tasks, given that we are moving from one vnet to another an ifnet which
  645. * has already been fully initialized.
  646. *
  647. * Note that if_detach_internal() removes group membership unconditionally
  648. * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
  649. * Thus, when if_vmove() is applied to a cloned interface, group membership
  650. * is lost while a cloned one always joins a group whose name is
  651. * ifc->ifc_name. To recover this after if_detach_internal() and
  652. * if_attach_internal(), the cloner should be specified to
  653. * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
  654. * attempts to join a group whose name is ifc->ifc_name.
  655. *
  656. * XXX:
  657. * - The decision to return void and thus require this function to
  658. * succeed is questionable.
  659. * - We should probably do more sanity checking. For instance we don't
  660. * do anything to insure if_xname is unique or non-empty.
  661. */
  662. void
  663. if_attach(struct ifnet *ifp)
  664. {
  665. if_attach_internal(ifp, false);
  666. }
  667. /*
  668. * Compute the least common TSO limit.
  669. */
  670. void
  671. if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
  672. {
  673. /*
  674. * 1) If there is no limit currently, take the limit from
  675. * the network adapter.
  676. *
  677. * 2) If the network adapter has a limit below the current
  678. * limit, apply it.
  679. */
  680. if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
  681. ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
  682. pmax->tsomaxbytes = ifp->if_hw_tsomax;
  683. }
  684. if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
  685. ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
  686. pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
  687. }
  688. if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
  689. ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
  690. pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
  691. }
  692. }
  693. /*
  694. * Update TSO limit of a network adapter.
  695. *
  696. * Returns zero if no change. Else non-zero.
  697. */
  698. int
  699. if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
  700. {
  701. int retval = 0;
  702. if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
  703. ifp->if_hw_tsomax = pmax->tsomaxbytes;
  704. retval++;
  705. }
  706. if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
  707. ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
  708. retval++;
  709. }
  710. if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
  711. ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
  712. retval++;
  713. }
  714. return (retval);
  715. }
  716. static void
  717. if_attach_internal(struct ifnet *ifp, bool vmove)
  718. {
  719. unsigned socksize, ifasize;
  720. int namelen, masklen;
  721. struct sockaddr_dl *sdl;
  722. struct ifaddr *ifa;
  723. MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
  724. #ifdef VIMAGE
  725. ifp->if_vnet = curvnet;
  726. if (ifp->if_home_vnet == NULL)
  727. ifp->if_home_vnet = curvnet;
  728. #endif
  729. if_addgroup(ifp, IFG_ALL);
  730. #ifdef VIMAGE
  731. /* Restore group membership for cloned interface. */
  732. if (vmove)
  733. if_clone_restoregroup(ifp);
  734. #endif
  735. getmicrotime(&ifp->if_lastchange);
  736. ifp->if_epoch = time_uptime;
  737. KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
  738. (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
  739. ("transmit and qflush must both either be set or both be NULL"));
  740. if (ifp->if_transmit == NULL) {
  741. ifp->if_transmit = if_transmit_default;
  742. ifp->if_qflush = if_qflush;
  743. }
  744. if (ifp->if_input == NULL)
  745. ifp->if_input = if_input_default;
  746. if (ifp->if_requestencap == NULL)
  747. ifp->if_requestencap = if_requestencap_default;
  748. if (!vmove) {
  749. #ifdef MAC
  750. mac_ifnet_create(ifp);
  751. #endif
  752. /*
  753. * Create a Link Level name for this device.
  754. */
  755. namelen = strlen(ifp->if_xname);
  756. /*
  757. * Always save enough space for any possiable name so we
  758. * can do a rename in place later.
  759. */
  760. masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
  761. socksize = masklen + ifp->if_addrlen;
  762. if (socksize < sizeof(*sdl))
  763. socksize = sizeof(*sdl);
  764. socksize = roundup2(socksize, sizeof(long));
  765. ifasize = sizeof(*ifa) + 2 * socksize;
  766. ifa = ifa_alloc(ifasize, M_WAITOK);
  767. sdl = (struct sockaddr_dl *)(ifa + 1);
  768. sdl->sdl_len = socksize;
  769. sdl->sdl_family = AF_LINK;
  770. bcopy(ifp->if_xname, sdl->sdl_data, namelen);
  771. sdl->sdl_nlen = namelen;
  772. sdl->sdl_index = ifp->if_index;
  773. sdl->sdl_type = ifp->if_type;
  774. ifp->if_addr = ifa;
  775. ifa->ifa_ifp = ifp;
  776. ifa->ifa_addr = (struct sockaddr *)sdl;
  777. sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
  778. ifa->ifa_netmask = (struct sockaddr *)sdl;
  779. sdl->sdl_len = masklen;
  780. while (namelen != 0)
  781. sdl->sdl_data[--namelen] = 0xff;
  782. CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
  783. /* Reliably crash if used uninitialized. */
  784. ifp->if_broadcastaddr = NULL;
  785. if (ifp->if_type == IFT_ETHER) {
  786. ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
  787. M_WAITOK | M_ZERO);
  788. }
  789. #if defined(INET) || defined(INET6)
  790. /* Use defaults for TSO, if nothing is set */
  791. if (ifp->if_hw_tsomax == 0 &&
  792. ifp->if_hw_tsomaxsegcount == 0 &&
  793. ifp->if_hw_tsomaxsegsize == 0) {
  794. /*
  795. * The TSO defaults needs to be such that an
  796. * NFS mbuf list of 35 mbufs totalling just
  797. * below 64K works and that a chain of mbufs
  798. * can be defragged into at most 32 segments:
  799. */
  800. ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
  801. (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
  802. ifp->if_hw_tsomaxsegcount = 35;
  803. ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
  804. /* XXX some drivers set IFCAP_TSO after ethernet attach */
  805. if (ifp->if_capabilities & IFCAP_TSO) {
  806. if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
  807. ifp->if_hw_tsomax,
  808. ifp->if_hw_tsomaxsegcount,
  809. ifp->if_hw_tsomaxsegsize);
  810. }
  811. }
  812. #endif
  813. }
  814. #ifdef VIMAGE
  815. else {
  816. /*
  817. * Update the interface index in the link layer address
  818. * of the interface.
  819. */
  820. for (ifa = ifp->if_addr; ifa != NULL;
  821. ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
  822. if (ifa->ifa_addr->sa_family == AF_LINK) {
  823. sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  824. sdl->sdl_index = ifp->if_index;
  825. }
  826. }
  827. }
  828. #endif
  829. if_link_ifnet(ifp);
  830. if (domain_init_status >= 2)
  831. if_attachdomain1(ifp);
  832. EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
  833. if (IS_DEFAULT_VNET(curvnet))
  834. devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
  835. }
  836. static void
  837. if_epochalloc(void *dummy __unused)
  838. {
  839. net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
  840. }
  841. SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
  842. static void
  843. if_attachdomain(void *dummy)
  844. {
  845. struct ifnet *ifp;
  846. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
  847. if_attachdomain1(ifp);
  848. }
  849. SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
  850. if_attachdomain, NULL);
  851. static void
  852. if_attachdomain1(struct ifnet *ifp)
  853. {
  854. struct domain *dp;
  855. /*
  856. * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
  857. * cannot lock ifp->if_afdata initialization, entirely.
  858. */
  859. IF_AFDATA_LOCK(ifp);
  860. if (ifp->if_afdata_initialized >= domain_init_status) {
  861. IF_AFDATA_UNLOCK(ifp);
  862. log(LOG_WARNING, "%s called more than once on %s\n",
  863. __func__, ifp->if_xname);
  864. return;
  865. }
  866. ifp->if_afdata_initialized = domain_init_status;
  867. IF_AFDATA_UNLOCK(ifp);
  868. /* address family dependent data region */
  869. bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
  870. SLIST_FOREACH(dp, &domains, dom_next) {
  871. if (dp->dom_ifattach)
  872. ifp->if_afdata[dp->dom_family] =
  873. (*dp->dom_ifattach)(ifp);
  874. }
  875. }
  876. /*
  877. * Remove any unicast or broadcast network addresses from an interface.
  878. */
  879. void
  880. if_purgeaddrs(struct ifnet *ifp)
  881. {
  882. struct ifaddr *ifa;
  883. #ifdef INET6
  884. /*
  885. * Need to leave multicast addresses of proxy NDP llentries
  886. * before in6_purgeifaddr() because the llentries are keys
  887. * for in6_multi objects of proxy NDP entries.
  888. * in6_purgeifaddr()s clean up llentries including proxy NDPs
  889. * then we would lose the keys if they are called earlier.
  890. */
  891. in6_purge_proxy_ndp(ifp);
  892. #endif
  893. while (1) {
  894. struct epoch_tracker et;
  895. NET_EPOCH_ENTER(et);
  896. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  897. if (ifa->ifa_addr->sa_family != AF_LINK)
  898. break;
  899. }
  900. NET_EPOCH_EXIT(et);
  901. if (ifa == NULL)
  902. break;
  903. #ifdef INET
  904. /* XXX: Ugly!! ad hoc just for INET */
  905. if (ifa->ifa_addr->sa_family == AF_INET) {
  906. struct ifreq ifr;
  907. bzero(&ifr, sizeof(ifr));
  908. ifr.ifr_addr = *ifa->ifa_addr;
  909. if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
  910. NULL) == 0)
  911. continue;
  912. }
  913. #endif /* INET */
  914. #ifdef INET6
  915. if (ifa->ifa_addr->sa_family == AF_INET6) {
  916. in6_purgeifaddr((struct in6_ifaddr *)ifa);
  917. /* ifp_addrhead is already updated */
  918. continue;
  919. }
  920. #endif /* INET6 */
  921. IF_ADDR_WLOCK(ifp);
  922. CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
  923. IF_ADDR_WUNLOCK(ifp);
  924. ifa_free(ifa);
  925. }
  926. }
  927. /*
  928. * Remove any multicast network addresses from an interface when an ifnet
  929. * is going away.
  930. */
  931. static void
  932. if_purgemaddrs(struct ifnet *ifp)
  933. {
  934. struct ifmultiaddr *ifma;
  935. IF_ADDR_WLOCK(ifp);
  936. while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
  937. ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
  938. CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
  939. if_delmulti_locked(ifp, ifma, 1);
  940. }
  941. IF_ADDR_WUNLOCK(ifp);
  942. }
  943. /*
  944. * Detach an interface, removing it from the list of "active" interfaces.
  945. * If vmove flag is set on entry to if_detach_internal(), perform only a
  946. * limited subset of cleanup tasks, given that we are moving an ifnet from
  947. * one vnet to another, where it must be fully operational.
  948. *
  949. * XXXRW: There are some significant questions about event ordering, and
  950. * how to prevent things from starting to use the interface during detach.
  951. */
  952. void
  953. if_detach(struct ifnet *ifp)
  954. {
  955. bool found;
  956. CURVNET_SET_QUIET(ifp->if_vnet);
  957. found = if_unlink_ifnet(ifp, false);
  958. if (found) {
  959. sx_xlock(&ifnet_detach_sxlock);
  960. if_detach_internal(ifp, false);
  961. sx_xunlock(&ifnet_detach_sxlock);
  962. }
  963. CURVNET_RESTORE();
  964. }
  965. /*
  966. * The vmove flag, if set, indicates that we are called from a callpath
  967. * that is moving an interface to a different vnet instance.
  968. *
  969. * The shutdown flag, if set, indicates that we are called in the
  970. * process of shutting down a vnet instance. Currently only the
  971. * vnet_if_return SYSUNINIT function sets it. Note: we can be called
  972. * on a vnet instance shutdown without this flag being set, e.g., when
  973. * the cloned interfaces are destoyed as first thing of teardown.
  974. */
  975. static int
  976. if_detach_internal(struct ifnet *ifp, bool vmove)
  977. {
  978. struct ifaddr *ifa;
  979. int i;
  980. struct domain *dp;
  981. #ifdef VIMAGE
  982. bool shutdown;
  983. shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
  984. #endif
  985. sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
  986. /*
  987. * At this point we know the interface still was on the ifnet list
  988. * and we removed it so we are in a stable state.
  989. */
  990. NET_EPOCH_WAIT();
  991. /*
  992. * Ensure all pending EPOCH(9) callbacks have been executed. This
  993. * fixes issues about late destruction of multicast options
  994. * which lead to leave group calls, which in turn access the
  995. * belonging ifnet structure:
  996. */
  997. NET_EPOCH_DRAIN_CALLBACKS();
  998. /*
  999. * In any case (destroy or vmove) detach us from the groups
  1000. * and remove/wait for pending events on the taskq.
  1001. * XXX-BZ in theory an interface could still enqueue a taskq change?
  1002. */
  1003. if_delgroups(ifp);
  1004. taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
  1005. taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
  1006. if_down(ifp);
  1007. #ifdef VIMAGE
  1008. /*
  1009. * On VNET shutdown abort here as the stack teardown will do all
  1010. * the work top-down for us.
  1011. */
  1012. if (shutdown) {
  1013. /* Give interface users the chance to clean up. */
  1014. EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
  1015. /*
  1016. * In case of a vmove we are done here without error.
  1017. * If we would signal an error it would lead to the same
  1018. * abort as if we did not find the ifnet anymore.
  1019. * if_detach() calls us in void context and does not care
  1020. * about an early abort notification, so life is splendid :)
  1021. */
  1022. goto finish_vnet_shutdown;
  1023. }
  1024. #endif
  1025. /*
  1026. * At this point we are not tearing down a VNET and are either
  1027. * going to destroy or vmove the interface and have to cleanup
  1028. * accordingly.
  1029. */
  1030. /*
  1031. * Remove routes and flush queues.
  1032. */
  1033. #ifdef ALTQ
  1034. if (ALTQ_IS_ENABLED(&ifp->if_snd))
  1035. altq_disable(&ifp->if_snd);
  1036. if (ALTQ_IS_ATTACHED(&ifp->if_snd))
  1037. altq_detach(&ifp->if_snd);
  1038. #endif
  1039. if_purgeaddrs(ifp);
  1040. #ifdef INET
  1041. in_ifdetach(ifp);
  1042. #endif
  1043. #ifdef INET6
  1044. /*
  1045. * Remove all IPv6 kernel structs related to ifp. This should be done
  1046. * before removing routing entries below, since IPv6 interface direct
  1047. * routes are expected to be removed by the IPv6-specific kernel API.
  1048. * Otherwise, the kernel will detect some inconsistency and bark it.
  1049. */
  1050. in6_ifdetach(ifp);
  1051. #endif
  1052. if_purgemaddrs(ifp);
  1053. EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
  1054. if (IS_DEFAULT_VNET(curvnet))
  1055. devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
  1056. if (!vmove) {
  1057. /*
  1058. * Prevent further calls into the device driver via ifnet.
  1059. */
  1060. if_dead(ifp);
  1061. /*
  1062. * Clean up all addresses.
  1063. */
  1064. IF_ADDR_WLOCK(ifp);
  1065. if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
  1066. ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
  1067. CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
  1068. IF_ADDR_WUNLOCK(ifp);
  1069. ifa_free(ifa);
  1070. } else
  1071. IF_ADDR_WUNLOCK(ifp);
  1072. }
  1073. rt_flushifroutes(ifp);
  1074. #ifdef VIMAGE
  1075. finish_vnet_shutdown:
  1076. #endif
  1077. /*
  1078. * We cannot hold the lock over dom_ifdetach calls as they might
  1079. * sleep, for example trying to drain a callout, thus open up the
  1080. * theoretical race with re-attaching.
  1081. */
  1082. IF_AFDATA_LOCK(ifp);
  1083. i = ifp->if_afdata_initialized;
  1084. ifp->if_afdata_initialized = 0;
  1085. IF_AFDATA_UNLOCK(ifp);
  1086. if (i == 0)
  1087. return (0);
  1088. SLIST_FOREACH(dp, &domains, dom_next) {
  1089. if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
  1090. (*dp->dom_ifdetach)(ifp,
  1091. ifp->if_afdata[dp->dom_family]);
  1092. ifp->if_afdata[dp->dom_family] = NULL;
  1093. }
  1094. }
  1095. return (0);
  1096. }
  1097. #ifdef VIMAGE
  1098. /*
  1099. * if_vmove() performs a limited version of if_detach() in current
  1100. * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
  1101. */
  1102. static int
  1103. if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
  1104. {
  1105. int rc;
  1106. /*
  1107. * Detach from current vnet, but preserve LLADDR info, do not
  1108. * mark as dead etc. so that the ifnet can be reattached later.
  1109. * If we cannot find it, we lost the race to someone else.
  1110. */
  1111. rc = if_detach_internal(ifp, true);
  1112. if (rc != 0)
  1113. return (rc);
  1114. /*
  1115. * Perform interface-specific reassignment tasks, if provided by
  1116. * the driver.
  1117. */
  1118. if (ifp->if_reassign != NULL)
  1119. ifp->if_reassign(ifp, new_vnet, NULL);
  1120. /*
  1121. * Switch to the context of the target vnet.
  1122. */
  1123. CURVNET_SET_QUIET(new_vnet);
  1124. if_attach_internal(ifp, true);
  1125. CURVNET_RESTORE();
  1126. return (0);
  1127. }
  1128. /*
  1129. * Move an ifnet to or from another child prison/vnet, specified by the jail id.
  1130. */
  1131. static int
  1132. if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
  1133. {
  1134. struct prison *pr;
  1135. struct ifnet *difp;
  1136. int error;
  1137. bool found __diagused;
  1138. bool shutdown;
  1139. MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
  1140. /* Try to find the prison within our visibility. */
  1141. sx_slock(&allprison_lock);
  1142. pr = prison_find_child(td->td_ucred->cr_prison, jid);
  1143. sx_sunlock(&allprison_lock);
  1144. if (pr == NULL)
  1145. return (ENXIO);
  1146. prison_hold_locked(pr);
  1147. mtx_unlock(&pr->pr_mtx);
  1148. /* Do not try to move the iface from and to the same prison. */
  1149. if (pr->pr_vnet == ifp->if_vnet) {
  1150. prison_free(pr);
  1151. return (EEXIST);
  1152. }
  1153. /* Make sure the named iface does not exists in the dst. prison/vnet. */
  1154. /* XXX Lock interfaces to avoid races. */
  1155. CURVNET_SET_QUIET(pr->pr_vnet);
  1156. difp = ifunit(ifname);
  1157. CURVNET_RESTORE();
  1158. if (difp != NULL) {
  1159. prison_free(pr);
  1160. return (EEXIST);
  1161. }
  1162. sx_xlock(&ifnet_detach_sxlock);
  1163. /* Make sure the VNET is stable. */
  1164. shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
  1165. if (shutdown) {
  1166. sx_xunlock(&ifnet_detach_sxlock);
  1167. prison_free(pr);
  1168. return (EBUSY);
  1169. }
  1170. found = if_unlink_ifnet(ifp, true);
  1171. if (! found) {
  1172. sx_xunlock(&ifnet_detach_sxlock);
  1173. prison_free(pr);
  1174. return (ENODEV);
  1175. }
  1176. /* Move the interface into the child jail/vnet. */
  1177. error = if_vmove(ifp, pr->pr_vnet);
  1178. /* Report the new if_xname back to the userland on success. */
  1179. if (error == 0)
  1180. sprintf(ifname, "%s", ifp->if_xname);
  1181. sx_xunlock(&ifnet_detach_sxlock);
  1182. prison_free(pr);
  1183. return (error);
  1184. }
  1185. static int
  1186. if_vmove_reclaim(struct thread *td, char *ifname, int jid)
  1187. {
  1188. struct prison *pr;
  1189. struct vnet *vnet_dst;
  1190. struct ifnet *ifp;
  1191. int error, found __diagused;
  1192. bool shutdown;
  1193. /* Try to find the prison within our visibility. */
  1194. sx_slock(&allprison_lock);
  1195. pr = prison_find_child(td->td_ucred->cr_prison, jid);
  1196. sx_sunlock(&allprison_lock);
  1197. if (pr == NULL)
  1198. return (ENXIO);
  1199. prison_hold_locked(pr);
  1200. mtx_unlock(&pr->pr_mtx);
  1201. /* Make sure the named iface exists in the source prison/vnet. */
  1202. CURVNET_SET(pr->pr_vnet);
  1203. ifp = ifunit(ifname); /* XXX Lock to avoid races. */
  1204. if (ifp == NULL) {
  1205. CURVNET_RESTORE();
  1206. prison_free(pr);
  1207. return (ENXIO);
  1208. }
  1209. /* Do not try to move the iface from and to the same prison. */
  1210. vnet_dst = TD_TO_VNET(td);
  1211. if (vnet_dst == ifp->if_vnet) {
  1212. CURVNET_RESTORE();
  1213. prison_free(pr);
  1214. return (EEXIST);
  1215. }
  1216. /* Make sure the VNET is stable. */
  1217. shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
  1218. if (shutdown) {
  1219. CURVNET_RESTORE();
  1220. prison_free(pr);
  1221. return (EBUSY);
  1222. }
  1223. /* Get interface back from child jail/vnet. */
  1224. found = if_unlink_ifnet(ifp, true);
  1225. MPASS(found);
  1226. sx_xlock(&ifnet_detach_sxlock);
  1227. error = if_vmove(ifp, vnet_dst);
  1228. sx_xunlock(&ifnet_detach_sxlock);
  1229. CURVNET_RESTORE();
  1230. /* Report the new if_xname back to the userland on success. */
  1231. if (error == 0)
  1232. sprintf(ifname, "%s", ifp->if_xname);
  1233. prison_free(pr);
  1234. return (error);
  1235. }
  1236. #endif /* VIMAGE */
  1237. /*
  1238. * Add a group to an interface
  1239. */
  1240. int
  1241. if_addgroup(struct ifnet *ifp, const char *groupname)
  1242. {
  1243. struct ifg_list *ifgl;
  1244. struct ifg_group *ifg = NULL;
  1245. struct ifg_member *ifgm;
  1246. int new = 0;
  1247. if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
  1248. groupname[strlen(groupname) - 1] <= '9')
  1249. return (EINVAL);
  1250. IFNET_WLOCK();
  1251. CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
  1252. if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
  1253. IFNET_WUNLOCK();
  1254. return (EEXIST);
  1255. }
  1256. if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
  1257. IFNET_WUNLOCK();
  1258. return (ENOMEM);
  1259. }
  1260. if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
  1261. free(ifgl, M_TEMP);
  1262. IFNET_WUNLOCK();
  1263. return (ENOMEM);
  1264. }
  1265. CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
  1266. if (!strcmp(ifg->ifg_group, groupname))
  1267. break;
  1268. if (ifg == NULL) {
  1269. if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
  1270. free(ifgl, M_TEMP);
  1271. free(ifgm, M_TEMP);
  1272. IFNET_WUNLOCK();
  1273. return (ENOMEM);
  1274. }
  1275. strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
  1276. ifg->ifg_refcnt = 0;
  1277. CK_STAILQ_INIT(&ifg->ifg_members);
  1278. CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
  1279. new = 1;
  1280. }
  1281. ifg->ifg_refcnt++;
  1282. ifgl->ifgl_group = ifg;
  1283. ifgm->ifgm_ifp = ifp;
  1284. IF_ADDR_WLOCK(ifp);
  1285. CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
  1286. CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
  1287. IF_ADDR_WUNLOCK(ifp);
  1288. IFNET_WUNLOCK();
  1289. if (new)
  1290. EVENTHANDLER_INVOKE(group_attach_event, ifg);
  1291. EVENTHANDLER_INVOKE(group_change_event, groupname);
  1292. return (0);
  1293. }
  1294. /*
  1295. * Helper function to remove a group out of an interface. Expects the global
  1296. * ifnet lock to be write-locked, and drops it before returning.
  1297. */
  1298. static void
  1299. _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
  1300. const char *groupname)
  1301. {
  1302. struct ifg_member *ifgm;
  1303. bool freeifgl;
  1304. IFNET_WLOCK_ASSERT();
  1305. IF_ADDR_WLOCK(ifp);
  1306. CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
  1307. IF_ADDR_WUNLOCK(ifp);
  1308. CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
  1309. if (ifgm->ifgm_ifp == ifp) {
  1310. CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
  1311. ifg_member, ifgm_next);
  1312. break;
  1313. }
  1314. }
  1315. if (--ifgl->ifgl_group->ifg_refcnt == 0) {
  1316. CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
  1317. ifg_next);
  1318. freeifgl = true;
  1319. } else {
  1320. freeifgl = false;
  1321. }
  1322. IFNET_WUNLOCK();
  1323. NET_EPOCH_WAIT();
  1324. EVENTHANDLER_INVOKE(group_change_event, groupname);
  1325. if (freeifgl) {
  1326. EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
  1327. free(ifgl->ifgl_group, M_TEMP);
  1328. }
  1329. free(ifgm, M_TEMP);
  1330. free(ifgl, M_TEMP);
  1331. }
  1332. /*
  1333. * Remove a group from an interface
  1334. */
  1335. int
  1336. if_delgroup(struct ifnet *ifp, const char *groupname)
  1337. {
  1338. struct ifg_list *ifgl;
  1339. IFNET_WLOCK();
  1340. CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
  1341. if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
  1342. break;
  1343. if (ifgl == NULL) {
  1344. IFNET_WUNLOCK();
  1345. return (ENOENT);
  1346. }
  1347. _if_delgroup_locked(ifp, ifgl, groupname);
  1348. return (0);
  1349. }
  1350. /*
  1351. * Remove an interface from all groups
  1352. */
  1353. static void
  1354. if_delgroups(struct ifnet *ifp)
  1355. {
  1356. struct ifg_list *ifgl;
  1357. char groupname[IFNAMSIZ];
  1358. IFNET_WLOCK();
  1359. while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
  1360. strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
  1361. _if_delgroup_locked(ifp, ifgl, groupname);
  1362. IFNET_WLOCK();
  1363. }
  1364. IFNET_WUNLOCK();
  1365. }
  1366. /*
  1367. * Stores all groups from an interface in memory pointed to by ifgr.
  1368. */
  1369. static int
  1370. if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
  1371. {
  1372. int len, error;
  1373. struct ifg_list *ifgl;
  1374. struct ifg_req ifgrq, *ifgp;
  1375. NET_EPOCH_ASSERT();
  1376. if (ifgr->ifgr_len == 0) {
  1377. CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
  1378. ifgr->ifgr_len += sizeof(struct ifg_req);
  1379. return (0);
  1380. }
  1381. len = ifgr->ifgr_len;
  1382. ifgp = ifgr->ifgr_groups;
  1383. /* XXX: wire */
  1384. CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
  1385. if (len < sizeof(ifgrq))
  1386. return (EINVAL);
  1387. bzero(&ifgrq, sizeof ifgrq);
  1388. strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
  1389. sizeof(ifgrq.ifgrq_group));
  1390. if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
  1391. return (error);
  1392. len -= sizeof(ifgrq);
  1393. ifgp++;
  1394. }
  1395. return (0);
  1396. }
  1397. /*
  1398. * Stores all members of a group in memory pointed to by igfr
  1399. */
  1400. static int
  1401. if_getgroupmembers(struct ifgroupreq *ifgr)
  1402. {
  1403. struct ifg_group *ifg;
  1404. struct ifg_member *ifgm;
  1405. struct ifg_req ifgrq, *ifgp;
  1406. int len, error;
  1407. IFNET_RLOCK();
  1408. CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
  1409. if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
  1410. break;
  1411. if (ifg == NULL) {
  1412. IFNET_RUNLOCK();
  1413. return (ENOENT);
  1414. }
  1415. if (ifgr->ifgr_len == 0) {
  1416. CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
  1417. ifgr->ifgr_len += sizeof(ifgrq);
  1418. IFNET_RUNLOCK();
  1419. return (0);
  1420. }
  1421. len = ifgr->ifgr_len;
  1422. ifgp = ifgr->ifgr_groups;
  1423. CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
  1424. if (len < sizeof(ifgrq)) {
  1425. IFNET_RUNLOCK();
  1426. return (EINVAL);
  1427. }
  1428. bzero(&ifgrq, sizeof ifgrq);
  1429. strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
  1430. sizeof(ifgrq.ifgrq_member));
  1431. if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
  1432. IFNET_RUNLOCK();
  1433. return (error);
  1434. }
  1435. len -= sizeof(ifgrq);
  1436. ifgp++;
  1437. }
  1438. IFNET_RUNLOCK();
  1439. return (0);
  1440. }
  1441. /*
  1442. * Return counter values from counter(9)s stored in ifnet.
  1443. */
  1444. uint64_t
  1445. if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
  1446. {
  1447. KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
  1448. return (counter_u64_fetch(ifp->if_counters[cnt]));
  1449. }
  1450. /*
  1451. * Increase an ifnet counter. Usually used for counters shared
  1452. * between the stack and a driver, but function supports them all.
  1453. */
  1454. void
  1455. if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
  1456. {
  1457. KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
  1458. counter_u64_add(ifp->if_counters[cnt], inc);
  1459. }
  1460. /*
  1461. * Copy data from ifnet to userland API structure if_data.
  1462. */
  1463. void
  1464. if_data_copy(struct ifnet *ifp, struct if_data *ifd)
  1465. {
  1466. ifd->ifi_type = ifp->if_type;
  1467. ifd->ifi_physical = 0;
  1468. ifd->ifi_addrlen = ifp->if_addrlen;
  1469. ifd->ifi_hdrlen = ifp->if_hdrlen;
  1470. ifd->ifi_link_state = ifp->if_link_state;
  1471. ifd->ifi_vhid = 0;
  1472. ifd->ifi_datalen = sizeof(struct if_data);
  1473. ifd->ifi_mtu = ifp->if_mtu;
  1474. ifd->ifi_metric = ifp->if_metric;
  1475. ifd->ifi_baudrate = ifp->if_baudrate;
  1476. ifd->ifi_hwassist = ifp->if_hwassist;
  1477. ifd->ifi_epoch = ifp->if_epoch;
  1478. ifd->ifi_lastchange = ifp->if_lastchange;
  1479. ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
  1480. ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
  1481. ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
  1482. ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
  1483. ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
  1484. ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
  1485. ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
  1486. ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
  1487. ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
  1488. ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
  1489. ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
  1490. ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
  1491. }
  1492. /*
  1493. * Initialization, destruction and refcounting functions for ifaddrs.
  1494. */
  1495. struct ifaddr *
  1496. ifa_alloc(size_t size, int flags)
  1497. {
  1498. struct ifaddr *ifa;
  1499. KASSERT(size >= sizeof(struct ifaddr),
  1500. ("%s: invalid size %zu", __func__, size));
  1501. ifa = malloc(size, M_IFADDR, M_ZERO | flags);
  1502. if (ifa == NULL)
  1503. return (NULL);
  1504. if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
  1505. goto fail;
  1506. if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
  1507. goto fail;
  1508. if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
  1509. goto fail;
  1510. if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
  1511. goto fail;
  1512. refcount_init(&ifa->ifa_refcnt, 1);
  1513. return (ifa);
  1514. fail:
  1515. /* free(NULL) is okay */
  1516. counter_u64_free(ifa->ifa_opackets);
  1517. counter_u64_free(ifa->ifa_ipackets);
  1518. counter_u64_free(ifa->ifa_obytes);
  1519. counter_u64_free(ifa->ifa_ibytes);
  1520. free(ifa, M_IFADDR);
  1521. return (NULL);
  1522. }
  1523. void
  1524. ifa_ref(struct ifaddr *ifa)
  1525. {
  1526. u_int old __diagused;
  1527. old = refcount_acquire(&ifa->ifa_refcnt);
  1528. KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
  1529. }
  1530. int
  1531. ifa_try_ref(struct ifaddr *ifa)
  1532. {
  1533. NET_EPOCH_ASSERT();
  1534. return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
  1535. }
  1536. static void
  1537. ifa_destroy(epoch_context_t ctx)
  1538. {
  1539. struct ifaddr *ifa;
  1540. ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
  1541. counter_u64_free(ifa->ifa_opackets);
  1542. counter_u64_free(ifa->ifa_ipackets);
  1543. counter_u64_free(ifa->ifa_obytes);
  1544. counter_u64_free(ifa->ifa_ibytes);
  1545. free(ifa, M_IFADDR);
  1546. }
  1547. void
  1548. ifa_free(struct ifaddr *ifa)
  1549. {
  1550. if (refcount_release(&ifa->ifa_refcnt))
  1551. NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
  1552. }
  1553. /*
  1554. * XXX: Because sockaddr_dl has deeper structure than the sockaddr
  1555. * structs used to represent other address families, it is necessary
  1556. * to perform a different comparison.
  1557. */
  1558. #define sa_dl_equal(a1, a2) \
  1559. ((((const struct sockaddr_dl *)(a1))->sdl_len == \
  1560. ((const struct sockaddr_dl *)(a2))->sdl_len) && \
  1561. (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
  1562. CLLADDR((const struct sockaddr_dl *)(a2)), \
  1563. ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
  1564. /*
  1565. * Locate an interface based on a complete address.
  1566. */
  1567. /*ARGSUSED*/
  1568. struct ifaddr *
  1569. ifa_ifwithaddr(const struct sockaddr *addr)
  1570. {
  1571. struct ifnet *ifp;
  1572. struct ifaddr *ifa;
  1573. NET_EPOCH_ASSERT();
  1574. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  1575. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1576. if (ifa->ifa_addr->sa_family != addr->sa_family)
  1577. continue;
  1578. if (sa_equal(addr, ifa->ifa_addr)) {
  1579. goto done;
  1580. }
  1581. /* IP6 doesn't have broadcast */
  1582. if ((ifp->if_flags & IFF_BROADCAST) &&
  1583. ifa->ifa_broadaddr &&
  1584. ifa->ifa_broadaddr->sa_len != 0 &&
  1585. sa_equal(ifa->ifa_broadaddr, addr)) {
  1586. goto done;
  1587. }
  1588. }
  1589. }
  1590. ifa = NULL;
  1591. done:
  1592. return (ifa);
  1593. }
  1594. int
  1595. ifa_ifwithaddr_check(const struct sockaddr *addr)
  1596. {
  1597. struct epoch_tracker et;
  1598. int rc;
  1599. NET_EPOCH_ENTER(et);
  1600. rc = (ifa_ifwithaddr(addr) != NULL);
  1601. NET_EPOCH_EXIT(et);
  1602. return (rc);
  1603. }
  1604. /*
  1605. * Locate an interface based on the broadcast address.
  1606. */
  1607. /* ARGSUSED */
  1608. struct ifaddr *
  1609. ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
  1610. {
  1611. struct ifnet *ifp;
  1612. struct ifaddr *ifa;
  1613. NET_EPOCH_ASSERT();
  1614. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  1615. if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
  1616. continue;
  1617. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1618. if (ifa->ifa_addr->sa_family != addr->sa_family)
  1619. continue;
  1620. if ((ifp->if_flags & IFF_BROADCAST) &&
  1621. ifa->ifa_broadaddr &&
  1622. ifa->ifa_broadaddr->sa_len != 0 &&
  1623. sa_equal(ifa->ifa_broadaddr, addr)) {
  1624. goto done;
  1625. }
  1626. }
  1627. }
  1628. ifa = NULL;
  1629. done:
  1630. return (ifa);
  1631. }
  1632. /*
  1633. * Locate the point to point interface with a given destination address.
  1634. */
  1635. /*ARGSUSED*/
  1636. struct ifaddr *
  1637. ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
  1638. {
  1639. struct ifnet *ifp;
  1640. struct ifaddr *ifa;
  1641. NET_EPOCH_ASSERT();
  1642. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  1643. if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  1644. continue;
  1645. if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
  1646. continue;
  1647. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1648. if (ifa->ifa_addr->sa_family != addr->sa_family)
  1649. continue;
  1650. if (ifa->ifa_dstaddr != NULL &&
  1651. sa_equal(addr, ifa->ifa_dstaddr)) {
  1652. goto done;
  1653. }
  1654. }
  1655. }
  1656. ifa = NULL;
  1657. done:
  1658. return (ifa);
  1659. }
  1660. /*
  1661. * Find an interface on a specific network. If many, choice
  1662. * is most specific found.
  1663. */
  1664. struct ifaddr *
  1665. ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
  1666. {
  1667. struct ifnet *ifp;
  1668. struct ifaddr *ifa;
  1669. struct ifaddr *ifa_maybe = NULL;
  1670. u_int af = addr->sa_family;
  1671. const char *addr_data = addr->sa_data, *cplim;
  1672. NET_EPOCH_ASSERT();
  1673. /*
  1674. * AF_LINK addresses can be looked up directly by their index number,
  1675. * so do that if we can.
  1676. */
  1677. if (af == AF_LINK) {
  1678. ifp = ifnet_byindex(
  1679. ((const struct sockaddr_dl *)addr)->sdl_index);
  1680. return (ifp ? ifp->if_addr : NULL);
  1681. }
  1682. /*
  1683. * Scan though each interface, looking for ones that have addresses
  1684. * in this address family and the requested fib.
  1685. */
  1686. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  1687. if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
  1688. continue;
  1689. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1690. const char *cp, *cp2, *cp3;
  1691. if (ifa->ifa_addr->sa_family != af)
  1692. next: continue;
  1693. if (af == AF_INET &&
  1694. ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
  1695. /*
  1696. * This is a bit broken as it doesn't
  1697. * take into account that the remote end may
  1698. * be a single node in the network we are
  1699. * looking for.
  1700. * The trouble is that we don't know the
  1701. * netmask for the remote end.
  1702. */
  1703. if (ifa->ifa_dstaddr != NULL &&
  1704. sa_equal(addr, ifa->ifa_dstaddr)) {
  1705. goto done;
  1706. }
  1707. } else {
  1708. /*
  1709. * Scan all the bits in the ifa's address.
  1710. * If a bit dissagrees with what we are
  1711. * looking for, mask it with the netmask
  1712. * to see if it really matters.
  1713. * (A byte at a time)
  1714. */
  1715. if (ifa->ifa_netmask == 0)
  1716. continue;
  1717. cp = addr_data;
  1718. cp2 = ifa->ifa_addr->sa_data;
  1719. cp3 = ifa->ifa_netmask->sa_data;
  1720. cplim = ifa->ifa_netmask->sa_len
  1721. + (char *)ifa->ifa_netmask;
  1722. while (cp3 < cplim)
  1723. if ((*cp++ ^ *cp2++) & *cp3++)
  1724. goto next; /* next address! */
  1725. /*
  1726. * If the netmask of what we just found
  1727. * is more specific than what we had before
  1728. * (if we had one), or if the virtual status
  1729. * of new prefix is better than of the old one,
  1730. * then remember the new one before continuing
  1731. * to search for an even better one.
  1732. */
  1733. if (ifa_maybe == NULL ||
  1734. ifa_preferred(ifa_maybe, ifa) ||
  1735. rn_refines((caddr_t)ifa->ifa_netmask,
  1736. (caddr_t)ifa_maybe->ifa_netmask)) {
  1737. ifa_maybe = ifa;
  1738. }
  1739. }
  1740. }
  1741. }
  1742. ifa = ifa_maybe;
  1743. ifa_maybe = NULL;
  1744. done:
  1745. return (ifa);
  1746. }
  1747. /*
  1748. * Find an interface address specific to an interface best matching
  1749. * a given address.
  1750. */
  1751. struct ifaddr *
  1752. ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
  1753. {
  1754. struct ifaddr *ifa;
  1755. const char *cp, *cp2, *cp3;
  1756. char *cplim;
  1757. struct ifaddr *ifa_maybe = NULL;
  1758. u_int af = addr->sa_family;
  1759. if (af >= AF_MAX)
  1760. return (NULL);
  1761. NET_EPOCH_ASSERT();
  1762. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1763. if (ifa->ifa_addr->sa_family != af)
  1764. continue;
  1765. if (ifa_maybe == NULL)
  1766. ifa_maybe = ifa;
  1767. if (ifa->ifa_netmask == 0) {
  1768. if (sa_equal(addr, ifa->ifa_addr) ||
  1769. (ifa->ifa_dstaddr &&
  1770. sa_equal(addr, ifa->ifa_dstaddr)))
  1771. goto done;
  1772. continue;
  1773. }
  1774. if (ifp->if_flags & IFF_POINTOPOINT) {
  1775. if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
  1776. goto done;
  1777. } else {
  1778. cp = addr->sa_data;
  1779. cp2 = ifa->ifa_addr->sa_data;
  1780. cp3 = ifa->ifa_netmask->sa_data;
  1781. cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
  1782. for (; cp3 < cplim; cp3++)
  1783. if ((*cp++ ^ *cp2++) & *cp3)
  1784. break;
  1785. if (cp3 == cplim)
  1786. goto done;
  1787. }
  1788. }
  1789. ifa = ifa_maybe;
  1790. done:
  1791. return (ifa);
  1792. }
  1793. /*
  1794. * See whether new ifa is better than current one:
  1795. * 1) A non-virtual one is preferred over virtual.
  1796. * 2) A virtual in master state preferred over any other state.
  1797. *
  1798. * Used in several address selecting functions.
  1799. */
  1800. int
  1801. ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
  1802. {
  1803. return (cur->ifa_carp && (!next->ifa_carp ||
  1804. ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
  1805. }
  1806. struct sockaddr_dl *
  1807. link_alloc_sdl(size_t size, int flags)
  1808. {
  1809. return (malloc(size, M_TEMP, flags));
  1810. }
  1811. void
  1812. link_free_sdl(struct sockaddr *sa)
  1813. {
  1814. free(sa, M_TEMP);
  1815. }
  1816. /*
  1817. * Fills in given sdl with interface basic info.
  1818. * Returns pointer to filled sdl.
  1819. */
  1820. struct sockaddr_dl *
  1821. link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
  1822. {
  1823. struct sockaddr_dl *sdl;
  1824. sdl = (struct sockaddr_dl *)paddr;
  1825. memset(sdl, 0, sizeof(struct sockaddr_dl));
  1826. sdl->sdl_len = sizeof(struct sockaddr_dl);
  1827. sdl->sdl_family = AF_LINK;
  1828. sdl->sdl_index = ifp->if_index;
  1829. sdl->sdl_type = iftype;
  1830. return (sdl);
  1831. }
  1832. /*
  1833. * Mark an interface down and notify protocols of
  1834. * the transition.
  1835. */
  1836. static void
  1837. if_unroute(struct ifnet *ifp, int flag, int fam)
  1838. {
  1839. KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
  1840. ifp->if_flags &= ~flag;
  1841. getmicrotime(&ifp->if_lastchange);
  1842. ifp->if_qflush(ifp);
  1843. if (ifp->if_carp)
  1844. (*carp_linkstate_p)(ifp);
  1845. rt_ifmsg(ifp, IFF_UP);
  1846. }
  1847. void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
  1848. void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
  1849. struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
  1850. struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
  1851. int (*vlan_tag_p)(struct ifnet *, uint16_t *);
  1852. int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
  1853. int (*vlan_setcookie_p)(struct ifnet *, void *);
  1854. void *(*vlan_cookie_p)(struct ifnet *);
  1855. /*
  1856. * Handle a change in the interface link state. To avoid LORs
  1857. * between driver lock and upper layer locks, as well as possible
  1858. * recursions, we post event to taskqueue, and all job
  1859. * is done in static do_link_state_change().
  1860. */
  1861. void
  1862. if_link_state_change(struct ifnet *ifp, int link_state)
  1863. {
  1864. /* Return if state hasn't changed. */
  1865. if (ifp->if_link_state == link_state)
  1866. return;
  1867. ifp->if_link_state = link_state;
  1868. /* XXXGL: reference ifp? */
  1869. taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
  1870. }
  1871. static void
  1872. do_link_state_change(void *arg, int pending)
  1873. {
  1874. struct ifnet *ifp;
  1875. int link_state;
  1876. ifp = arg;
  1877. link_state = ifp->if_link_state;
  1878. CURVNET_SET(ifp->if_vnet);
  1879. rt_ifmsg(ifp, 0);
  1880. if (ifp->if_vlantrunk != NULL)
  1881. (*vlan_link_state_p)(ifp);
  1882. if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
  1883. ifp->if_l2com != NULL)
  1884. (*ng_ether_link_state_p)(ifp, link_state);
  1885. if (ifp->if_carp)
  1886. (*carp_linkstate_p)(ifp);
  1887. if (ifp->if_bridge)
  1888. ifp->if_bridge_linkstate(ifp);
  1889. if (ifp->if_lagg)
  1890. (*lagg_linkstate_p)(ifp, link_state);
  1891. if (IS_DEFAULT_VNET(curvnet))
  1892. devctl_notify("IFNET", ifp->if_xname,
  1893. (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
  1894. NULL);
  1895. if (pending > 1)
  1896. if_printf(ifp, "%d link states coalesced\n", pending);
  1897. if (log_link_state_change)
  1898. if_printf(ifp, "link state changed to %s\n",
  1899. (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
  1900. EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
  1901. CURVNET_RESTORE();
  1902. }
  1903. /*
  1904. * Mark an interface down and notify protocols of
  1905. * the transition.
  1906. */
  1907. void
  1908. if_down(struct ifnet *ifp)
  1909. {
  1910. EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
  1911. if_unroute(ifp, IFF_UP, AF_UNSPEC);
  1912. }
  1913. /*
  1914. * Mark an interface up and notify protocols of
  1915. * the transition.
  1916. */
  1917. void
  1918. if_up(struct ifnet *ifp)
  1919. {
  1920. ifp->if_flags |= IFF_UP;
  1921. getmicrotime(&ifp->if_lastchange);
  1922. if (ifp->if_carp)
  1923. (*carp_linkstate_p)(ifp);
  1924. rt_ifmsg(ifp, IFF_UP);
  1925. EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
  1926. }
  1927. /*
  1928. * Flush an interface queue.
  1929. */
  1930. void
  1931. if_qflush(struct ifnet *ifp)
  1932. {
  1933. struct mbuf *m, *n;
  1934. struct ifaltq *ifq;
  1935. ifq = &ifp->if_snd;
  1936. IFQ_LOCK(ifq);
  1937. #ifdef ALTQ
  1938. if (ALTQ_IS_ENABLED(ifq))
  1939. ALTQ_PURGE(ifq);
  1940. #endif
  1941. n = ifq->ifq_head;
  1942. while ((m = n) != NULL) {
  1943. n = m->m_nextpkt;
  1944. m_freem(m);
  1945. }
  1946. ifq->ifq_head = 0;
  1947. ifq->ifq_tail = 0;
  1948. ifq->ifq_len = 0;
  1949. IFQ_UNLOCK(ifq);
  1950. }
  1951. /*
  1952. * Map interface name to interface structure pointer, with or without
  1953. * returning a reference.
  1954. */
  1955. struct ifnet *
  1956. ifunit_ref(const char *name)
  1957. {
  1958. struct epoch_tracker et;
  1959. struct ifnet *ifp;
  1960. NET_EPOCH_ENTER(et);
  1961. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  1962. if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
  1963. !(ifp->if_flags & IFF_DYING))
  1964. break;
  1965. }
  1966. if (ifp != NULL) {
  1967. if_ref(ifp);
  1968. MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
  1969. }
  1970. NET_EPOCH_EXIT(et);
  1971. return (ifp);
  1972. }
  1973. struct ifnet *
  1974. ifunit(const char *name)
  1975. {
  1976. struct epoch_tracker et;
  1977. struct ifnet *ifp;
  1978. NET_EPOCH_ENTER(et);
  1979. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  1980. if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
  1981. break;
  1982. }
  1983. NET_EPOCH_EXIT(et);
  1984. return (ifp);
  1985. }
  1986. void *
  1987. ifr_buffer_get_buffer(void *data)
  1988. {
  1989. union ifreq_union *ifrup;
  1990. ifrup = data;
  1991. #ifdef COMPAT_FREEBSD32
  1992. if (SV_CURPROC_FLAG(SV_ILP32))
  1993. return ((void *)(uintptr_t)
  1994. ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
  1995. #endif
  1996. return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
  1997. }
  1998. static void
  1999. ifr_buffer_set_buffer_null(void *data)
  2000. {
  2001. union ifreq_union *ifrup;
  2002. ifrup = data;
  2003. #ifdef COMPAT_FREEBSD32
  2004. if (SV_CURPROC_FLAG(SV_ILP32))
  2005. ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
  2006. else
  2007. #endif
  2008. ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
  2009. }
  2010. size_t
  2011. ifr_buffer_get_length(void *data)
  2012. {
  2013. union ifreq_union *ifrup;
  2014. ifrup = data;
  2015. #ifdef COMPAT_FREEBSD32
  2016. if (SV_CURPROC_FLAG(SV_ILP32))
  2017. return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
  2018. #endif
  2019. return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
  2020. }
  2021. static void
  2022. ifr_buffer_set_length(void *data, size_t len)
  2023. {
  2024. union ifreq_union *ifrup;
  2025. ifrup = data;
  2026. #ifdef COMPAT_FREEBSD32
  2027. if (SV_CURPROC_FLAG(SV_ILP32))
  2028. ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
  2029. else
  2030. #endif
  2031. ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
  2032. }
  2033. void *
  2034. ifr_data_get_ptr(void *ifrp)
  2035. {
  2036. union ifreq_union *ifrup;
  2037. ifrup = ifrp;
  2038. #ifdef COMPAT_FREEBSD32
  2039. if (SV_CURPROC_FLAG(SV_ILP32))
  2040. return ((void *)(uintptr_t)
  2041. ifrup->ifr32.ifr_ifru.ifru_data);
  2042. #endif
  2043. return (ifrup->ifr.ifr_ifru.ifru_data);
  2044. }
  2045. struct ifcap_nv_bit_name {
  2046. uint64_t cap_bit;
  2047. const char *cap_name;
  2048. };
  2049. #define CAPNV(x) {.cap_bit = IFCAP_##x, \
  2050. .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
  2051. const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
  2052. CAPNV(RXCSUM),
  2053. CAPNV(TXCSUM),
  2054. CAPNV(NETCONS),
  2055. CAPNV(VLAN_MTU),
  2056. CAPNV(VLAN_HWTAGGING),
  2057. CAPNV(JUMBO_MTU),
  2058. CAPNV(POLLING),
  2059. CAPNV(VLAN_HWCSUM),
  2060. CAPNV(TSO4),
  2061. CAPNV(TSO6),
  2062. CAPNV(LRO),
  2063. CAPNV(WOL_UCAST),
  2064. CAPNV(WOL_MCAST),
  2065. CAPNV(WOL_MAGIC),
  2066. CAPNV(TOE4),
  2067. CAPNV(TOE6),
  2068. CAPNV(VLAN_HWFILTER),
  2069. CAPNV(VLAN_HWTSO),
  2070. CAPNV(LINKSTATE),
  2071. CAPNV(NETMAP),
  2072. CAPNV(RXCSUM_IPV6),
  2073. CAPNV(TXCSUM_IPV6),
  2074. CAPNV(HWSTATS),
  2075. CAPNV(TXRTLMT),
  2076. CAPNV(HWRXTSTMP),
  2077. CAPNV(MEXTPG),
  2078. CAPNV(TXTLS4),
  2079. CAPNV(TXTLS6),
  2080. CAPNV(VXLAN_HWCSUM),
  2081. CAPNV(VXLAN_HWTSO),
  2082. CAPNV(TXTLS_RTLMT),
  2083. {0, NULL}
  2084. };
  2085. #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
  2086. .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
  2087. const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
  2088. CAP2NV(RXTLS4),
  2089. CAP2NV(RXTLS6),
  2090. CAP2NV(IPSEC_OFFLOAD),
  2091. {0, NULL}
  2092. };
  2093. #undef CAPNV
  2094. #undef CAP2NV
  2095. int
  2096. if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
  2097. const struct ifcap_nv_bit_name *nn, bool all)
  2098. {
  2099. int i, res;
  2100. res = 0;
  2101. for (i = 0; nn[i].cap_name != NULL; i++) {
  2102. if (nvlist_exists_bool(nv, nn[i].cap_name)) {
  2103. if (all || nvlist_get_bool(nv, nn[i].cap_name))
  2104. res |= nn[i].cap_bit;
  2105. } else {
  2106. res |= *old_cap & nn[i].cap_bit;
  2107. }
  2108. }
  2109. return (res);
  2110. }
  2111. void
  2112. if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
  2113. int ifr_cap, int ifr_req)
  2114. {
  2115. int i;
  2116. for (i = 0; nn[i].cap_name != NULL; i++) {
  2117. if ((nn[i].cap_bit & ifr_cap) != 0) {
  2118. nvlist_add_bool(nv, nn[i].cap_name,
  2119. (nn[i].cap_bit & ifr_req) != 0);
  2120. }
  2121. }
  2122. }
  2123. /*
  2124. * Hardware specific interface ioctls.
  2125. */
  2126. int
  2127. ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
  2128. {
  2129. struct ifreq *ifr;
  2130. int error = 0, do_ifup = 0;
  2131. int new_flags, temp_flags;
  2132. size_t descrlen, nvbuflen;
  2133. char *descrbuf;
  2134. char new_name[IFNAMSIZ];
  2135. void *buf;
  2136. nvlist_t *nvcap;
  2137. struct siocsifcapnv_driver_data drv_ioctl_data;
  2138. ifr = (struct ifreq *)data;
  2139. switch (cmd) {
  2140. case SIOCGIFINDEX:
  2141. ifr->ifr_index = ifp->if_index;
  2142. break;
  2143. case SIOCGIFFLAGS:
  2144. temp_flags = ifp->if_flags | ifp->if_drv_flags;
  2145. ifr->ifr_flags = temp_flags & 0xffff;
  2146. ifr->ifr_flagshigh = temp_flags >> 16;
  2147. break;
  2148. case SIOCGIFCAP:
  2149. ifr->ifr_reqcap = ifp->if_capabilities;
  2150. ifr->ifr_curcap = ifp->if_capenable;
  2151. break;
  2152. case SIOCGIFCAPNV:
  2153. if ((ifp->if_capabilities & IFCAP_NV) == 0) {
  2154. error = EINVAL;
  2155. break;
  2156. }
  2157. buf = NULL;
  2158. nvcap = nvlist_create(0);
  2159. for (;;) {
  2160. if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
  2161. ifp->if_capabilities, ifp->if_capenable);
  2162. if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
  2163. ifp->if_capabilities2, ifp->if_capenable2);
  2164. error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
  2165. __DECONST(caddr_t, nvcap));
  2166. if (error != 0) {
  2167. if_printf(ifp,
  2168. "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
  2169. error);
  2170. break;
  2171. }
  2172. buf = nvlist_pack(nvcap, &nvbuflen);
  2173. if (buf == NULL) {
  2174. error = nvlist_error(nvcap);
  2175. if (error == 0)
  2176. error = EDOOFUS;
  2177. break;
  2178. }
  2179. if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
  2180. ifr->ifr_cap_nv.length = nvbuflen;
  2181. ifr->ifr_cap_nv.buffer = NULL;
  2182. error = EFBIG;
  2183. break;
  2184. }
  2185. ifr->ifr_cap_nv.length = nvbuflen;
  2186. error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
  2187. break;
  2188. }
  2189. free(buf, M_NVLIST);
  2190. nvlist_destroy(nvcap);
  2191. break;
  2192. case SIOCGIFDATA:
  2193. {
  2194. struct if_data ifd;
  2195. /* Ensure uninitialised padding is not leaked. */
  2196. memset(&ifd, 0, sizeof(ifd));
  2197. if_data_copy(ifp, &ifd);
  2198. error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
  2199. break;
  2200. }
  2201. #ifdef MAC
  2202. case SIOCGIFMAC:
  2203. error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
  2204. break;
  2205. #endif
  2206. case SIOCGIFMETRIC:
  2207. ifr->ifr_metric = ifp->if_metric;
  2208. break;
  2209. case SIOCGIFMTU:
  2210. ifr->ifr_mtu = ifp->if_mtu;
  2211. break;
  2212. case SIOCGIFPHYS:
  2213. /* XXXGL: did this ever worked? */
  2214. ifr->ifr_phys = 0;
  2215. break;
  2216. case SIOCGIFDESCR:
  2217. error = 0;
  2218. sx_slock(&ifdescr_sx);
  2219. if (ifp->if_description == NULL)
  2220. error = ENOMSG;
  2221. else {
  2222. /* space for terminating nul */
  2223. descrlen = strlen(ifp->if_description) + 1;
  2224. if (ifr_buffer_get_length(ifr) < descrlen)
  2225. ifr_buffer_set_buffer_null(ifr);
  2226. else
  2227. error = copyout(ifp->if_description,
  2228. ifr_buffer_get_buffer(ifr), descrlen);
  2229. ifr_buffer_set_length(ifr, descrlen);
  2230. }
  2231. sx_sunlock(&ifdescr_sx);
  2232. break;
  2233. case SIOCSIFDESCR:
  2234. error = priv_check(td, PRIV_NET_SETIFDESCR);
  2235. if (error)
  2236. return (error);
  2237. /*
  2238. * Copy only (length-1) bytes to make sure that
  2239. * if_description is always nul terminated. The
  2240. * length parameter is supposed to count the
  2241. * terminating nul in.
  2242. */
  2243. if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
  2244. return (ENAMETOOLONG);
  2245. else if (ifr_buffer_get_length(ifr) == 0)
  2246. descrbuf = NULL;
  2247. else {
  2248. descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
  2249. error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
  2250. ifr_buffer_get_length(ifr) - 1);
  2251. if (error) {
  2252. if_freedescr(descrbuf);
  2253. break;
  2254. }
  2255. }
  2256. if_setdescr(ifp, descrbuf);
  2257. getmicrotime(&ifp->if_lastchange);
  2258. break;
  2259. case SIOCGIFFIB:
  2260. ifr->ifr_fib = ifp->if_fib;
  2261. break;
  2262. case SIOCSIFFIB:
  2263. error = priv_check(td, PRIV_NET_SETIFFIB);
  2264. if (error)
  2265. return (error);
  2266. if (ifr->ifr_fib >= rt_numfibs)
  2267. return (EINVAL);
  2268. ifp->if_fib = ifr->ifr_fib;
  2269. break;
  2270. case SIOCSIFFLAGS:
  2271. error = priv_check(td, PRIV_NET_SETIFFLAGS);
  2272. if (error)
  2273. return (error);
  2274. /*
  2275. * Currently, no driver owned flags pass the IFF_CANTCHANGE
  2276. * check, so we don't need special handling here yet.
  2277. */
  2278. new_flags = (ifr->ifr_flags & 0xffff) |
  2279. (ifr->ifr_flagshigh << 16);
  2280. if (ifp->if_flags & IFF_UP &&
  2281. (new_flags & IFF_UP) == 0) {
  2282. if_down(ifp);
  2283. } else if (new_flags & IFF_UP &&
  2284. (ifp->if_flags & IFF_UP) == 0) {
  2285. do_ifup = 1;
  2286. }
  2287. /* See if permanently promiscuous mode bit is about to flip */
  2288. if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
  2289. if (new_flags & IFF_PPROMISC)
  2290. ifp->if_flags |= IFF_PROMISC;
  2291. else if (ifp->if_pcount == 0)
  2292. ifp->if_flags &= ~IFF_PROMISC;
  2293. if (log_promisc_mode_change)
  2294. if_printf(ifp, "permanently promiscuous mode %s\n",
  2295. ((new_flags & IFF_PPROMISC) ?
  2296. "enabled" : "disabled"));
  2297. }
  2298. ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
  2299. (new_flags &~ IFF_CANTCHANGE);
  2300. if (ifp->if_ioctl) {
  2301. (void) (*ifp->if_ioctl)(ifp, cmd, data);
  2302. }
  2303. if (do_ifup)
  2304. if_up(ifp);
  2305. getmicrotime(&ifp->if_lastchange);
  2306. break;
  2307. case SIOCSIFCAP:
  2308. error = priv_check(td, PRIV_NET_SETIFCAP);
  2309. if (error != 0)
  2310. return (error);
  2311. if (ifp->if_ioctl == NULL)
  2312. return (EOPNOTSUPP);
  2313. if (ifr->ifr_reqcap & ~ifp->if_capabilities)
  2314. return (EINVAL);
  2315. error = (*ifp->if_ioctl)(ifp, cmd, data);
  2316. if (error == 0)
  2317. getmicrotime(&ifp->if_lastchange);
  2318. break;
  2319. case SIOCSIFCAPNV:
  2320. error = priv_check(td, PRIV_NET_SETIFCAP);
  2321. if (error != 0)
  2322. return (error);
  2323. if (ifp->if_ioctl == NULL)
  2324. return (EOPNOTSUPP);
  2325. if ((ifp->if_capabilities & IFCAP_NV) == 0)
  2326. return (EINVAL);
  2327. if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
  2328. return (EINVAL);
  2329. nvcap = NULL;
  2330. buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
  2331. for (;;) {
  2332. error = copyin(ifr->ifr_cap_nv.buffer, buf,
  2333. ifr->ifr_cap_nv.length);
  2334. if (error != 0)
  2335. break;
  2336. nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
  2337. if (nvcap == NULL) {
  2338. error = EINVAL;
  2339. break;
  2340. }
  2341. drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
  2342. &ifp->if_capenable, ifcap_nv_bit_names, false);
  2343. if ((drv_ioctl_data.reqcap &
  2344. ~ifp->if_capabilities) != 0) {
  2345. error = EINVAL;
  2346. break;
  2347. }
  2348. drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
  2349. &ifp->if_capenable2, ifcap2_nv_bit_names, false);
  2350. if ((drv_ioctl_data.reqcap2 &
  2351. ~ifp->if_capabilities2) != 0) {
  2352. error = EINVAL;
  2353. break;
  2354. }
  2355. drv_ioctl_data.nvcap = nvcap;
  2356. error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
  2357. (caddr_t)&drv_ioctl_data);
  2358. break;
  2359. }
  2360. nvlist_destroy(nvcap);
  2361. free(buf, M_TEMP);
  2362. if (error == 0)
  2363. getmicrotime(&ifp->if_lastchange);
  2364. break;
  2365. #ifdef MAC
  2366. case SIOCSIFMAC:
  2367. error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
  2368. break;
  2369. #endif
  2370. case SIOCSIFNAME:
  2371. error = priv_check(td, PRIV_NET_SETIFNAME);
  2372. if (error)
  2373. return (error);
  2374. error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
  2375. NULL);
  2376. if (error != 0)
  2377. return (error);
  2378. error = if_rename(ifp, new_name);
  2379. break;
  2380. #ifdef VIMAGE
  2381. case SIOCSIFVNET:
  2382. error = priv_check(td, PRIV_NET_SETIFVNET);
  2383. if (error)
  2384. return (error);
  2385. error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
  2386. break;
  2387. #endif
  2388. case SIOCSIFMETRIC:
  2389. error = priv_check(td, PRIV_NET_SETIFMETRIC);
  2390. if (error)
  2391. return (error);
  2392. ifp->if_metric = ifr->ifr_metric;
  2393. getmicrotime(&ifp->if_lastchange);
  2394. break;
  2395. case SIOCSIFPHYS:
  2396. error = priv_check(td, PRIV_NET_SETIFPHYS);
  2397. if (error)
  2398. return (error);
  2399. if (ifp->if_ioctl == NULL)
  2400. return (EOPNOTSUPP);
  2401. error = (*ifp->if_ioctl)(ifp, cmd, data);
  2402. if (error == 0)
  2403. getmicrotime(&ifp->if_lastchange);
  2404. break;
  2405. case SIOCSIFMTU:
  2406. {
  2407. u_long oldmtu = ifp->if_mtu;
  2408. error = priv_check(td, PRIV_NET_SETIFMTU);
  2409. if (error)
  2410. return (error);
  2411. if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
  2412. return (EINVAL);
  2413. if (ifp->if_ioctl == NULL)
  2414. return (EOPNOTSUPP);
  2415. /* Disallow MTU changes on bridge member interfaces. */
  2416. if (ifp->if_bridge)
  2417. return (EOPNOTSUPP);
  2418. error = (*ifp->if_ioctl)(ifp, cmd, data);
  2419. if (error == 0) {
  2420. getmicrotime(&ifp->if_lastchange);
  2421. rt_ifmsg(ifp, 0);
  2422. #ifdef INET
  2423. DEBUGNET_NOTIFY_MTU(ifp);
  2424. #endif
  2425. }
  2426. /*
  2427. * If the link MTU changed, do network layer specific procedure.
  2428. */
  2429. if (ifp->if_mtu != oldmtu)
  2430. if_notifymtu(ifp);
  2431. break;
  2432. }
  2433. case SIOCADDMULTI:
  2434. case SIOCDELMULTI:
  2435. if (cmd == SIOCADDMULTI)
  2436. error = priv_check(td, PRIV_NET_ADDMULTI);
  2437. else
  2438. error = priv_check(td, PRIV_NET_DELMULTI);
  2439. if (error)
  2440. return (error);
  2441. /* Don't allow group membership on non-multicast interfaces. */
  2442. if ((ifp->if_flags & IFF_MULTICAST) == 0)
  2443. return (EOPNOTSUPP);
  2444. /* Don't let users screw up protocols' entries. */
  2445. if (ifr->ifr_addr.sa_family != AF_LINK)
  2446. return (EINVAL);
  2447. if (cmd == SIOCADDMULTI) {
  2448. struct epoch_tracker et;
  2449. struct ifmultiaddr *ifma;
  2450. /*
  2451. * Userland is only permitted to join groups once
  2452. * via the if_addmulti() KPI, because it cannot hold
  2453. * struct ifmultiaddr * between calls. It may also
  2454. * lose a race while we check if the membership
  2455. * already exists.
  2456. */
  2457. NET_EPOCH_ENTER(et);
  2458. ifma = if_findmulti(ifp, &ifr->ifr_addr);
  2459. NET_EPOCH_EXIT(et);
  2460. if (ifma != NULL)
  2461. error = EADDRINUSE;
  2462. else
  2463. error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
  2464. } else {
  2465. error = if_delmulti(ifp, &ifr->ifr_addr);
  2466. }
  2467. if (error == 0)
  2468. getmicrotime(&ifp->if_lastchange);
  2469. break;
  2470. case SIOCSIFPHYADDR:
  2471. case SIOCDIFPHYADDR:
  2472. #ifdef INET6
  2473. case SIOCSIFPHYADDR_IN6:
  2474. #endif
  2475. case SIOCSIFMEDIA:
  2476. case SIOCSIFGENERIC:
  2477. error = priv_check(td, PRIV_NET_HWIOCTL);
  2478. if (error)
  2479. return (error);
  2480. if (ifp->if_ioctl == NULL)
  2481. return (EOPNOTSUPP);
  2482. error = (*ifp->if_ioctl)(ifp, cmd, data);
  2483. if (error == 0)
  2484. getmicrotime(&ifp->if_lastchange);
  2485. break;
  2486. case SIOCGIFSTATUS:
  2487. case SIOCGIFPSRCADDR:
  2488. case SIOCGIFPDSTADDR:
  2489. case SIOCGIFMEDIA:
  2490. case SIOCGIFXMEDIA:
  2491. case SIOCGIFGENERIC:
  2492. case SIOCGIFRSSKEY:
  2493. case SIOCGIFRSSHASH:
  2494. case SIOCGIFDOWNREASON:
  2495. if (ifp->if_ioctl == NULL)
  2496. return (EOPNOTSUPP);
  2497. error = (*ifp->if_ioctl)(ifp, cmd, data);
  2498. break;
  2499. case SIOCSIFLLADDR:
  2500. error = priv_check(td, PRIV_NET_SETLLADDR);
  2501. if (error)
  2502. return (error);
  2503. error = if_setlladdr(ifp,
  2504. ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
  2505. break;
  2506. case SIOCGHWADDR:
  2507. error = if_gethwaddr(ifp, ifr);
  2508. break;
  2509. case SIOCAIFGROUP:
  2510. error = priv_check(td, PRIV_NET_ADDIFGROUP);
  2511. if (error)
  2512. return (error);
  2513. error = if_addgroup(ifp,
  2514. ((struct ifgroupreq *)data)->ifgr_group);
  2515. if (error != 0)
  2516. return (error);
  2517. break;
  2518. case SIOCGIFGROUP:
  2519. {
  2520. struct epoch_tracker et;
  2521. NET_EPOCH_ENTER(et);
  2522. error = if_getgroup((struct ifgroupreq *)data, ifp);
  2523. NET_EPOCH_EXIT(et);
  2524. break;
  2525. }
  2526. case SIOCDIFGROUP:
  2527. error = priv_check(td, PRIV_NET_DELIFGROUP);
  2528. if (error)
  2529. return (error);
  2530. error = if_delgroup(ifp,
  2531. ((struct ifgroupreq *)data)->ifgr_group);
  2532. if (error != 0)
  2533. return (error);
  2534. break;
  2535. default:
  2536. error = ENOIOCTL;
  2537. break;
  2538. }
  2539. return (error);
  2540. }
  2541. /*
  2542. * Interface ioctls.
  2543. */
  2544. int
  2545. ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
  2546. {
  2547. #ifdef COMPAT_FREEBSD32
  2548. union {
  2549. struct ifconf ifc;
  2550. struct ifdrv ifd;
  2551. struct ifgroupreq ifgr;
  2552. struct ifmediareq ifmr;
  2553. } thunk;
  2554. u_long saved_cmd;
  2555. struct ifconf32 *ifc32;
  2556. struct ifdrv32 *ifd32;
  2557. struct ifgroupreq32 *ifgr32;
  2558. struct ifmediareq32 *ifmr32;
  2559. #endif
  2560. struct ifnet *ifp;
  2561. struct ifreq *ifr;
  2562. int error;
  2563. int oif_flags;
  2564. #ifdef VIMAGE
  2565. bool shutdown;
  2566. #endif
  2567. CURVNET_SET(so->so_vnet);
  2568. #ifdef VIMAGE
  2569. /* Make sure the VNET is stable. */
  2570. shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
  2571. if (shutdown) {
  2572. CURVNET_RESTORE();
  2573. return (EBUSY);
  2574. }
  2575. #endif
  2576. #ifdef COMPAT_FREEBSD32
  2577. saved_cmd = cmd;
  2578. switch (cmd) {
  2579. case SIOCGIFCONF32:
  2580. ifc32 = (struct ifconf32 *)data;
  2581. thunk.ifc.ifc_len = ifc32->ifc_len;
  2582. thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
  2583. data = (caddr_t)&thunk.ifc;
  2584. cmd = SIOCGIFCONF;
  2585. break;
  2586. case SIOCGDRVSPEC32:
  2587. case SIOCSDRVSPEC32:
  2588. ifd32 = (struct ifdrv32 *)data;
  2589. memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
  2590. sizeof(thunk.ifd.ifd_name));
  2591. thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
  2592. thunk.ifd.ifd_len = ifd32->ifd_len;
  2593. thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
  2594. data = (caddr_t)&thunk.ifd;
  2595. cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
  2596. break;
  2597. case SIOCAIFGROUP32:
  2598. case SIOCGIFGROUP32:
  2599. case SIOCDIFGROUP32:
  2600. case SIOCGIFGMEMB32:
  2601. ifgr32 = (struct ifgroupreq32 *)data;
  2602. memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
  2603. sizeof(thunk.ifgr.ifgr_name));
  2604. thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
  2605. switch (cmd) {
  2606. case SIOCAIFGROUP32:
  2607. case SIOCDIFGROUP32:
  2608. memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
  2609. sizeof(thunk.ifgr.ifgr_group));
  2610. break;
  2611. case SIOCGIFGROUP32:
  2612. case SIOCGIFGMEMB32:
  2613. thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
  2614. break;
  2615. }
  2616. data = (caddr_t)&thunk.ifgr;
  2617. cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
  2618. break;
  2619. case SIOCGIFMEDIA32:
  2620. case SIOCGIFXMEDIA32:
  2621. ifmr32 = (struct ifmediareq32 *)data;
  2622. memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
  2623. sizeof(thunk.ifmr.ifm_name));
  2624. thunk.ifmr.ifm_current = ifmr32->ifm_current;
  2625. thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
  2626. thunk.ifmr.ifm_status = ifmr32->ifm_status;
  2627. thunk.ifmr.ifm_active = ifmr32->ifm_active;
  2628. thunk.ifmr.ifm_count = ifmr32->ifm_count;
  2629. thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
  2630. data = (caddr_t)&thunk.ifmr;
  2631. cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
  2632. break;
  2633. }
  2634. #endif
  2635. switch (cmd) {
  2636. case SIOCGIFCONF:
  2637. error = ifconf(cmd, data);
  2638. goto out_noref;
  2639. }
  2640. ifr = (struct ifreq *)data;
  2641. switch (cmd) {
  2642. #ifdef VIMAGE
  2643. case SIOCSIFRVNET:
  2644. error = priv_check(td, PRIV_NET_SETIFVNET);
  2645. if (error == 0)
  2646. error = if_vmove_reclaim(td, ifr->ifr_name,
  2647. ifr->ifr_jid);
  2648. goto out_noref;
  2649. #endif
  2650. case SIOCIFCREATE:
  2651. case SIOCIFCREATE2:
  2652. error = priv_check(td, PRIV_NET_IFCREATE);
  2653. if (error == 0)
  2654. error = if_clone_create(ifr->ifr_name,
  2655. sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
  2656. ifr_data_get_ptr(ifr) : NULL);
  2657. goto out_noref;
  2658. case SIOCIFDESTROY:
  2659. error = priv_check(td, PRIV_NET_IFDESTROY);
  2660. if (error == 0) {
  2661. sx_xlock(&ifnet_detach_sxlock);
  2662. error = if_clone_destroy(ifr->ifr_name);
  2663. sx_xunlock(&ifnet_detach_sxlock);
  2664. }
  2665. goto out_noref;
  2666. case SIOCIFGCLONERS:
  2667. error = if_clone_list((struct if_clonereq *)data);
  2668. goto out_noref;
  2669. case SIOCGIFGMEMB:
  2670. error = if_getgroupmembers((struct ifgroupreq *)data);
  2671. goto out_noref;
  2672. #if defined(INET) || defined(INET6)
  2673. case SIOCSVH:
  2674. case SIOCGVH:
  2675. if (carp_ioctl_p == NULL)
  2676. error = EPROTONOSUPPORT;
  2677. else
  2678. error = (*carp_ioctl_p)(ifr, cmd, td);
  2679. goto out_noref;
  2680. #endif
  2681. }
  2682. ifp = ifunit_ref(ifr->ifr_name);
  2683. if (ifp == NULL) {
  2684. error = ENXIO;
  2685. goto out_noref;
  2686. }
  2687. error = ifhwioctl(cmd, ifp, data, td);
  2688. if (error != ENOIOCTL)
  2689. goto out_ref;
  2690. oif_flags = ifp->if_flags;
  2691. if (so->so_proto == NULL) {
  2692. error = EOPNOTSUPP;
  2693. goto out_ref;
  2694. }
  2695. /*
  2696. * Pass the request on to the socket control method, and if the
  2697. * latter returns EOPNOTSUPP, directly to the interface.
  2698. *
  2699. * Make an exception for the legacy SIOCSIF* requests. Drivers
  2700. * trust SIOCSIFADDR et al to come from an already privileged
  2701. * layer, and do not perform any credentials checks or input
  2702. * validation.
  2703. */
  2704. error = so->so_proto->pr_control(so, cmd, data, ifp, td);
  2705. if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
  2706. cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
  2707. cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
  2708. error = (*ifp->if_ioctl)(ifp, cmd, data);
  2709. if (!(oif_flags & IFF_UP) && (ifp->if_flags & IFF_UP))
  2710. if_up(ifp);
  2711. out_ref:
  2712. if_rele(ifp);
  2713. out_noref:
  2714. CURVNET_RESTORE();
  2715. #ifdef COMPAT_FREEBSD32
  2716. if (error != 0)
  2717. return (error);
  2718. switch (saved_cmd) {
  2719. case SIOCGIFCONF32:
  2720. ifc32->ifc_len = thunk.ifc.ifc_len;
  2721. break;
  2722. case SIOCGDRVSPEC32:
  2723. /*
  2724. * SIOCGDRVSPEC is IOWR, but nothing actually touches
  2725. * the struct so just assert that ifd_len (the only
  2726. * field it might make sense to update) hasn't
  2727. * changed.
  2728. */
  2729. KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
  2730. ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
  2731. thunk.ifd.ifd_len));
  2732. break;
  2733. case SIOCGIFGROUP32:
  2734. case SIOCGIFGMEMB32:
  2735. ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
  2736. break;
  2737. case SIOCGIFMEDIA32:
  2738. case SIOCGIFXMEDIA32:
  2739. ifmr32->ifm_current = thunk.ifmr.ifm_current;
  2740. ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
  2741. ifmr32->ifm_status = thunk.ifmr.ifm_status;
  2742. ifmr32->ifm_active = thunk.ifmr.ifm_active;
  2743. ifmr32->ifm_count = thunk.ifmr.ifm_count;
  2744. break;
  2745. }
  2746. #endif
  2747. return (error);
  2748. }
  2749. int
  2750. if_rename(struct ifnet *ifp, char *new_name)
  2751. {
  2752. struct ifaddr *ifa;
  2753. struct sockaddr_dl *sdl;
  2754. size_t namelen, onamelen;
  2755. char old_name[IFNAMSIZ];
  2756. char strbuf[IFNAMSIZ + 8];
  2757. if (new_name[0] == '\0')
  2758. return (EINVAL);
  2759. if (strcmp(new_name, ifp->if_xname) == 0)
  2760. return (0);
  2761. if (ifunit(new_name) != NULL)
  2762. return (EEXIST);
  2763. /*
  2764. * XXX: Locking. Nothing else seems to lock if_flags,
  2765. * and there are numerous other races with the
  2766. * ifunit() checks not being atomic with namespace
  2767. * changes (renames, vmoves, if_attach, etc).
  2768. */
  2769. ifp->if_flags |= IFF_RENAMING;
  2770. EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
  2771. if_printf(ifp, "changing name to '%s'\n", new_name);
  2772. IF_ADDR_WLOCK(ifp);
  2773. strlcpy(old_name, ifp->if_xname, sizeof(old_name));
  2774. strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
  2775. ifa = ifp->if_addr;
  2776. sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  2777. namelen = strlen(new_name);
  2778. onamelen = sdl->sdl_nlen;
  2779. /*
  2780. * Move the address if needed. This is safe because we
  2781. * allocate space for a name of length IFNAMSIZ when we
  2782. * create this in if_attach().
  2783. */
  2784. if (namelen != onamelen) {
  2785. bcopy(sdl->sdl_data + onamelen,
  2786. sdl->sdl_data + namelen, sdl->sdl_alen);
  2787. }
  2788. bcopy(new_name, sdl->sdl_data, namelen);
  2789. sdl->sdl_nlen = namelen;
  2790. sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
  2791. bzero(sdl->sdl_data, onamelen);
  2792. while (namelen != 0)
  2793. sdl->sdl_data[--namelen] = 0xff;
  2794. IF_ADDR_WUNLOCK(ifp);
  2795. EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
  2796. ifp->if_flags &= ~IFF_RENAMING;
  2797. snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
  2798. devctl_notify("IFNET", old_name, "RENAME", strbuf);
  2799. return (0);
  2800. }
  2801. /*
  2802. * The code common to handling reference counted flags,
  2803. * e.g., in ifpromisc() and if_allmulti().
  2804. * The "pflag" argument can specify a permanent mode flag to check,
  2805. * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
  2806. *
  2807. * Only to be used on stack-owned flags, not driver-owned flags.
  2808. */
  2809. static int
  2810. if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
  2811. {
  2812. struct ifreq ifr;
  2813. int error;
  2814. int oldflags, oldcount;
  2815. /* Sanity checks to catch programming errors */
  2816. KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
  2817. ("%s: setting driver-owned flag %d", __func__, flag));
  2818. if (onswitch)
  2819. KASSERT(*refcount >= 0,
  2820. ("%s: increment negative refcount %d for flag %d",
  2821. __func__, *refcount, flag));
  2822. else
  2823. KASSERT(*refcount > 0,
  2824. ("%s: decrement non-positive refcount %d for flag %d",
  2825. __func__, *refcount, flag));
  2826. /* In case this mode is permanent, just touch refcount */
  2827. if (ifp->if_flags & pflag) {
  2828. *refcount += onswitch ? 1 : -1;
  2829. return (0);
  2830. }
  2831. /* Save ifnet parameters for if_ioctl() may fail */
  2832. oldcount = *refcount;
  2833. oldflags = ifp->if_flags;
  2834. /*
  2835. * See if we aren't the only and touching refcount is enough.
  2836. * Actually toggle interface flag if we are the first or last.
  2837. */
  2838. if (onswitch) {
  2839. if ((*refcount)++)
  2840. return (0);
  2841. ifp->if_flags |= flag;
  2842. } else {
  2843. if (--(*refcount))
  2844. return (0);
  2845. ifp->if_flags &= ~flag;
  2846. }
  2847. /* Call down the driver since we've changed interface flags */
  2848. if (ifp->if_ioctl == NULL) {
  2849. error = EOPNOTSUPP;
  2850. goto recover;
  2851. }
  2852. ifr.ifr_flags = ifp->if_flags & 0xffff;
  2853. ifr.ifr_flagshigh = ifp->if_flags >> 16;
  2854. error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
  2855. if (error)
  2856. goto recover;
  2857. /* Notify userland that interface flags have changed */
  2858. rt_ifmsg(ifp, flag);
  2859. return (0);
  2860. recover:
  2861. /* Recover after driver error */
  2862. *refcount = oldcount;
  2863. ifp->if_flags = oldflags;
  2864. return (error);
  2865. }
  2866. /*
  2867. * Set/clear promiscuous mode on interface ifp based on the truth value
  2868. * of pswitch. The calls are reference counted so that only the first
  2869. * "on" request actually has an effect, as does the final "off" request.
  2870. * Results are undefined if the "off" and "on" requests are not matched.
  2871. */
  2872. int
  2873. ifpromisc(struct ifnet *ifp, int pswitch)
  2874. {
  2875. int error;
  2876. int oldflags = ifp->if_flags;
  2877. error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
  2878. &ifp->if_pcount, pswitch);
  2879. /* If promiscuous mode status has changed, log a message */
  2880. if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
  2881. log_promisc_mode_change)
  2882. if_printf(ifp, "promiscuous mode %s\n",
  2883. (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
  2884. return (error);
  2885. }
  2886. /*
  2887. * Return interface configuration
  2888. * of system. List may be used
  2889. * in later ioctl's (above) to get
  2890. * other information.
  2891. */
  2892. /*ARGSUSED*/
  2893. static int
  2894. ifconf(u_long cmd, caddr_t data)
  2895. {
  2896. struct ifconf *ifc = (struct ifconf *)data;
  2897. struct ifnet *ifp;
  2898. struct ifaddr *ifa;
  2899. struct ifreq ifr;
  2900. struct sbuf *sb;
  2901. int error, full = 0, valid_len, max_len;
  2902. /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
  2903. max_len = maxphys - 1;
  2904. /* Prevent hostile input from being able to crash the system */
  2905. if (ifc->ifc_len <= 0)
  2906. return (EINVAL);
  2907. again:
  2908. if (ifc->ifc_len <= max_len) {
  2909. max_len = ifc->ifc_len;
  2910. full = 1;
  2911. }
  2912. sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
  2913. max_len = 0;
  2914. valid_len = 0;
  2915. IFNET_RLOCK();
  2916. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  2917. struct epoch_tracker et;
  2918. int addrs;
  2919. /*
  2920. * Zero the ifr to make sure we don't disclose the contents
  2921. * of the stack.
  2922. */
  2923. memset(&ifr, 0, sizeof(ifr));
  2924. if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
  2925. >= sizeof(ifr.ifr_name)) {
  2926. sbuf_delete(sb);
  2927. IFNET_RUNLOCK();
  2928. return (ENAMETOOLONG);
  2929. }
  2930. addrs = 0;
  2931. NET_EPOCH_ENTER(et);
  2932. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  2933. struct sockaddr *sa = ifa->ifa_addr;
  2934. if (prison_if(curthread->td_ucred, sa) != 0)
  2935. continue;
  2936. addrs++;
  2937. if (sa->sa_len <= sizeof(*sa)) {
  2938. if (sa->sa_len < sizeof(*sa)) {
  2939. memset(&ifr.ifr_ifru.ifru_addr, 0,
  2940. sizeof(ifr.ifr_ifru.ifru_addr));
  2941. memcpy(&ifr.ifr_ifru.ifru_addr, sa,
  2942. sa->sa_len);
  2943. } else
  2944. ifr.ifr_ifru.ifru_addr = *sa;
  2945. sbuf_bcat(sb, &ifr, sizeof(ifr));
  2946. max_len += sizeof(ifr);
  2947. } else {
  2948. sbuf_bcat(sb, &ifr,
  2949. offsetof(struct ifreq, ifr_addr));
  2950. max_len += offsetof(struct ifreq, ifr_addr);
  2951. sbuf_bcat(sb, sa, sa->sa_len);
  2952. max_len += sa->sa_len;
  2953. }
  2954. if (sbuf_error(sb) == 0)
  2955. valid_len = sbuf_len(sb);
  2956. }
  2957. NET_EPOCH_EXIT(et);
  2958. if (addrs == 0) {
  2959. sbuf_bcat(sb, &ifr, sizeof(ifr));
  2960. max_len += sizeof(ifr);
  2961. if (sbuf_error(sb) == 0)
  2962. valid_len = sbuf_len(sb);
  2963. }
  2964. }
  2965. IFNET_RUNLOCK();
  2966. /*
  2967. * If we didn't allocate enough space (uncommon), try again. If
  2968. * we have already allocated as much space as we are allowed,
  2969. * return what we've got.
  2970. */
  2971. if (valid_len != max_len && !full) {
  2972. sbuf_delete(sb);
  2973. goto again;
  2974. }
  2975. ifc->ifc_len = valid_len;
  2976. sbuf_finish(sb);
  2977. error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
  2978. sbuf_delete(sb);
  2979. return (error);
  2980. }
  2981. /*
  2982. * Just like ifpromisc(), but for all-multicast-reception mode.
  2983. */
  2984. int
  2985. if_allmulti(struct ifnet *ifp, int onswitch)
  2986. {
  2987. return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
  2988. }
  2989. struct ifmultiaddr *
  2990. if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
  2991. {
  2992. struct ifmultiaddr *ifma;
  2993. IF_ADDR_LOCK_ASSERT(ifp);
  2994. CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
  2995. if (sa->sa_family == AF_LINK) {
  2996. if (sa_dl_equal(ifma->ifma_addr, sa))
  2997. break;
  2998. } else {
  2999. if (sa_equal(ifma->ifma_addr, sa))
  3000. break;
  3001. }
  3002. }
  3003. return ifma;
  3004. }
  3005. /*
  3006. * Allocate a new ifmultiaddr and initialize based on passed arguments. We
  3007. * make copies of passed sockaddrs. The ifmultiaddr will not be added to
  3008. * the ifnet multicast address list here, so the caller must do that and
  3009. * other setup work (such as notifying the device driver). The reference
  3010. * count is initialized to 1.
  3011. */
  3012. static struct ifmultiaddr *
  3013. if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
  3014. int mflags)
  3015. {
  3016. struct ifmultiaddr *ifma;
  3017. struct sockaddr *dupsa;
  3018. ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
  3019. M_ZERO);
  3020. if (ifma == NULL)
  3021. return (NULL);
  3022. dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
  3023. if (dupsa == NULL) {
  3024. free(ifma, M_IFMADDR);
  3025. return (NULL);
  3026. }
  3027. bcopy(sa, dupsa, sa->sa_len);
  3028. ifma->ifma_addr = dupsa;
  3029. ifma->ifma_ifp = ifp;
  3030. ifma->ifma_refcount = 1;
  3031. ifma->ifma_protospec = NULL;
  3032. if (llsa == NULL) {
  3033. ifma->ifma_lladdr = NULL;
  3034. return (ifma);
  3035. }
  3036. dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
  3037. if (dupsa == NULL) {
  3038. free(ifma->ifma_addr, M_IFMADDR);
  3039. free(ifma, M_IFMADDR);
  3040. return (NULL);
  3041. }
  3042. bcopy(llsa, dupsa, llsa->sa_len);
  3043. ifma->ifma_lladdr = dupsa;
  3044. return (ifma);
  3045. }
  3046. /*
  3047. * if_freemulti: free ifmultiaddr structure and possibly attached related
  3048. * addresses. The caller is responsible for implementing reference
  3049. * counting, notifying the driver, handling routing messages, and releasing
  3050. * any dependent link layer state.
  3051. */
  3052. #ifdef MCAST_VERBOSE
  3053. extern void kdb_backtrace(void);
  3054. #endif
  3055. static void
  3056. if_freemulti_internal(struct ifmultiaddr *ifma)
  3057. {
  3058. KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
  3059. ifma->ifma_refcount));
  3060. if (ifma->ifma_lladdr != NULL)
  3061. free(ifma->ifma_lladdr, M_IFMADDR);
  3062. #ifdef MCAST_VERBOSE
  3063. kdb_backtrace();
  3064. printf("%s freeing ifma: %p\n", __func__, ifma);
  3065. #endif
  3066. free(ifma->ifma_addr, M_IFMADDR);
  3067. free(ifma, M_IFMADDR);
  3068. }
  3069. static void
  3070. if_destroymulti(epoch_context_t ctx)
  3071. {
  3072. struct ifmultiaddr *ifma;
  3073. ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
  3074. if_freemulti_internal(ifma);
  3075. }
  3076. void
  3077. if_freemulti(struct ifmultiaddr *ifma)
  3078. {
  3079. KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
  3080. ifma->ifma_refcount));
  3081. NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
  3082. }
  3083. /*
  3084. * Register an additional multicast address with a network interface.
  3085. *
  3086. * - If the address is already present, bump the reference count on the
  3087. * address and return.
  3088. * - If the address is not link-layer, look up a link layer address.
  3089. * - Allocate address structures for one or both addresses, and attach to the
  3090. * multicast address list on the interface. If automatically adding a link
  3091. * layer address, the protocol address will own a reference to the link
  3092. * layer address, to be freed when it is freed.
  3093. * - Notify the network device driver of an addition to the multicast address
  3094. * list.
  3095. *
  3096. * 'sa' points to caller-owned memory with the desired multicast address.
  3097. *
  3098. * 'retifma' will be used to return a pointer to the resulting multicast
  3099. * address reference, if desired.
  3100. */
  3101. int
  3102. if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
  3103. struct ifmultiaddr **retifma)
  3104. {
  3105. struct ifmultiaddr *ifma, *ll_ifma;
  3106. struct sockaddr *llsa;
  3107. struct sockaddr_dl sdl;
  3108. int error;
  3109. #ifdef INET
  3110. IN_MULTI_LIST_UNLOCK_ASSERT();
  3111. #endif
  3112. #ifdef INET6
  3113. IN6_MULTI_LIST_UNLOCK_ASSERT();
  3114. #endif
  3115. /*
  3116. * If the address is already present, return a new reference to it;
  3117. * otherwise, allocate storage and set up a new address.
  3118. */
  3119. IF_ADDR_WLOCK(ifp);
  3120. ifma = if_findmulti(ifp, sa);
  3121. if (ifma != NULL) {
  3122. ifma->ifma_refcount++;
  3123. if (retifma != NULL)
  3124. *retifma = ifma;
  3125. IF_ADDR_WUNLOCK(ifp);
  3126. return (0);
  3127. }
  3128. /*
  3129. * The address isn't already present; resolve the protocol address
  3130. * into a link layer address, and then look that up, bump its
  3131. * refcount or allocate an ifma for that also.
  3132. * Most link layer resolving functions returns address data which
  3133. * fits inside default sockaddr_dl structure. However callback
  3134. * can allocate another sockaddr structure, in that case we need to
  3135. * free it later.
  3136. */
  3137. llsa = NULL;
  3138. ll_ifma = NULL;
  3139. if (ifp->if_resolvemulti != NULL) {
  3140. /* Provide called function with buffer size information */
  3141. sdl.sdl_len = sizeof(sdl);
  3142. llsa = (struct sockaddr *)&sdl;
  3143. error = ifp->if_resolvemulti(ifp, &llsa, sa);
  3144. if (error)
  3145. goto unlock_out;
  3146. }
  3147. /*
  3148. * Allocate the new address. Don't hook it up yet, as we may also
  3149. * need to allocate a link layer multicast address.
  3150. */
  3151. ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
  3152. if (ifma == NULL) {
  3153. error = ENOMEM;
  3154. goto free_llsa_out;
  3155. }
  3156. /*
  3157. * If a link layer address is found, we'll need to see if it's
  3158. * already present in the address list, or allocate is as well.
  3159. * When this block finishes, the link layer address will be on the
  3160. * list.
  3161. */
  3162. if (llsa != NULL) {
  3163. ll_ifma = if_findmulti(ifp, llsa);
  3164. if (ll_ifma == NULL) {
  3165. ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
  3166. if (ll_ifma == NULL) {
  3167. --ifma->ifma_refcount;
  3168. if_freemulti(ifma);
  3169. error = ENOMEM;
  3170. goto free_llsa_out;
  3171. }
  3172. ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
  3173. CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
  3174. ifma_link);
  3175. } else
  3176. ll_ifma->ifma_refcount++;
  3177. ifma->ifma_llifma = ll_ifma;
  3178. }
  3179. /*
  3180. * We now have a new multicast address, ifma, and possibly a new or
  3181. * referenced link layer address. Add the primary address to the
  3182. * ifnet address list.
  3183. */
  3184. ifma->ifma_flags |= IFMA_F_ENQUEUED;
  3185. CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
  3186. if (retifma != NULL)
  3187. *retifma = ifma;
  3188. /*
  3189. * Must generate the message while holding the lock so that 'ifma'
  3190. * pointer is still valid.
  3191. */
  3192. rt_newmaddrmsg(RTM_NEWMADDR, ifma);
  3193. IF_ADDR_WUNLOCK(ifp);
  3194. /*
  3195. * We are certain we have added something, so call down to the
  3196. * interface to let them know about it.
  3197. */
  3198. if (ifp->if_ioctl != NULL) {
  3199. if (THREAD_CAN_SLEEP())
  3200. (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
  3201. else
  3202. taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
  3203. }
  3204. if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
  3205. link_free_sdl(llsa);
  3206. return (0);
  3207. free_llsa_out:
  3208. if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
  3209. link_free_sdl(llsa);
  3210. unlock_out:
  3211. IF_ADDR_WUNLOCK(ifp);
  3212. return (error);
  3213. }
  3214. static void
  3215. if_siocaddmulti(void *arg, int pending)
  3216. {
  3217. struct ifnet *ifp;
  3218. ifp = arg;
  3219. #ifdef DIAGNOSTIC
  3220. if (pending > 1)
  3221. if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
  3222. #endif
  3223. CURVNET_SET(ifp->if_vnet);
  3224. (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
  3225. CURVNET_RESTORE();
  3226. }
  3227. /*
  3228. * Delete a multicast group membership by network-layer group address.
  3229. *
  3230. * Returns ENOENT if the entry could not be found. If ifp no longer
  3231. * exists, results are undefined. This entry point should only be used
  3232. * from subsystems which do appropriate locking to hold ifp for the
  3233. * duration of the call.
  3234. * Network-layer protocol domains must use if_delmulti_ifma().
  3235. */
  3236. int
  3237. if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
  3238. {
  3239. struct ifmultiaddr *ifma;
  3240. int lastref;
  3241. KASSERT(ifp, ("%s: NULL ifp", __func__));
  3242. IF_ADDR_WLOCK(ifp);
  3243. lastref = 0;
  3244. ifma = if_findmulti(ifp, sa);
  3245. if (ifma != NULL)
  3246. lastref = if_delmulti_locked(ifp, ifma, 0);
  3247. IF_ADDR_WUNLOCK(ifp);
  3248. if (ifma == NULL)
  3249. return (ENOENT);
  3250. if (lastref && ifp->if_ioctl != NULL) {
  3251. (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
  3252. }
  3253. return (0);
  3254. }
  3255. /*
  3256. * Delete all multicast group membership for an interface.
  3257. * Should be used to quickly flush all multicast filters.
  3258. */
  3259. void
  3260. if_delallmulti(struct ifnet *ifp)
  3261. {
  3262. struct ifmultiaddr *ifma;
  3263. struct ifmultiaddr *next;
  3264. IF_ADDR_WLOCK(ifp);
  3265. CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
  3266. if_delmulti_locked(ifp, ifma, 0);
  3267. IF_ADDR_WUNLOCK(ifp);
  3268. }
  3269. void
  3270. if_delmulti_ifma(struct ifmultiaddr *ifma)
  3271. {
  3272. if_delmulti_ifma_flags(ifma, 0);
  3273. }
  3274. /*
  3275. * Delete a multicast group membership by group membership pointer.
  3276. * Network-layer protocol domains must use this routine.
  3277. *
  3278. * It is safe to call this routine if the ifp disappeared.
  3279. */
  3280. void
  3281. if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
  3282. {
  3283. struct ifnet *ifp;
  3284. int lastref;
  3285. MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
  3286. #ifdef INET
  3287. IN_MULTI_LIST_UNLOCK_ASSERT();
  3288. #endif
  3289. ifp = ifma->ifma_ifp;
  3290. #ifdef DIAGNOSTIC
  3291. if (ifp == NULL) {
  3292. printf("%s: ifma_ifp seems to be detached\n", __func__);
  3293. } else {
  3294. struct epoch_tracker et;
  3295. struct ifnet *oifp;
  3296. NET_EPOCH_ENTER(et);
  3297. CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
  3298. if (ifp == oifp)
  3299. break;
  3300. NET_EPOCH_EXIT(et);
  3301. if (ifp != oifp)
  3302. ifp = NULL;
  3303. }
  3304. #endif
  3305. /*
  3306. * If and only if the ifnet instance exists: Acquire the address lock.
  3307. */
  3308. if (ifp != NULL)
  3309. IF_ADDR_WLOCK(ifp);
  3310. lastref = if_delmulti_locked(ifp, ifma, flags);
  3311. if (ifp != NULL) {
  3312. /*
  3313. * If and only if the ifnet instance exists:
  3314. * Release the address lock.
  3315. * If the group was left: update the hardware hash filter.
  3316. */
  3317. IF_ADDR_WUNLOCK(ifp);
  3318. if (lastref && ifp->if_ioctl != NULL) {
  3319. (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
  3320. }
  3321. }
  3322. }
  3323. /*
  3324. * Perform deletion of network-layer and/or link-layer multicast address.
  3325. *
  3326. * Return 0 if the reference count was decremented.
  3327. * Return 1 if the final reference was released, indicating that the
  3328. * hardware hash filter should be reprogrammed.
  3329. */
  3330. static int
  3331. if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
  3332. {
  3333. struct ifmultiaddr *ll_ifma;
  3334. if (ifp != NULL && ifma->ifma_ifp != NULL) {
  3335. KASSERT(ifma->ifma_ifp == ifp,
  3336. ("%s: inconsistent ifp %p", __func__, ifp));
  3337. IF_ADDR_WLOCK_ASSERT(ifp);
  3338. }
  3339. ifp = ifma->ifma_ifp;
  3340. MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
  3341. /*
  3342. * If the ifnet is detaching, null out references to ifnet,
  3343. * so that upper protocol layers will notice, and not attempt
  3344. * to obtain locks for an ifnet which no longer exists. The
  3345. * routing socket announcement must happen before the ifnet
  3346. * instance is detached from the system.
  3347. */
  3348. if (detaching) {
  3349. #ifdef DIAGNOSTIC
  3350. printf("%s: detaching ifnet instance %p\n", __func__, ifp);
  3351. #endif
  3352. /*
  3353. * ifp may already be nulled out if we are being reentered
  3354. * to delete the ll_ifma.
  3355. */
  3356. if (ifp != NULL) {
  3357. rt_newmaddrmsg(RTM_DELMADDR, ifma);
  3358. ifma->ifma_ifp = NULL;
  3359. }
  3360. }
  3361. if (--ifma->ifma_refcount > 0)
  3362. return 0;
  3363. if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
  3364. CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
  3365. ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
  3366. }
  3367. /*
  3368. * If this ifma is a network-layer ifma, a link-layer ifma may
  3369. * have been associated with it. Release it first if so.
  3370. */
  3371. ll_ifma = ifma->ifma_llifma;
  3372. if (ll_ifma != NULL) {
  3373. KASSERT(ifma->ifma_lladdr != NULL,
  3374. ("%s: llifma w/o lladdr", __func__));
  3375. if (detaching)
  3376. ll_ifma->ifma_ifp = NULL; /* XXX */
  3377. if (--ll_ifma->ifma_refcount == 0) {
  3378. if (ifp != NULL) {
  3379. if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
  3380. CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
  3381. ifma_link);
  3382. ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
  3383. }
  3384. }
  3385. if_freemulti(ll_ifma);
  3386. }
  3387. }
  3388. #ifdef INVARIANTS
  3389. if (ifp) {
  3390. struct ifmultiaddr *ifmatmp;
  3391. CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
  3392. MPASS(ifma != ifmatmp);
  3393. }
  3394. #endif
  3395. if_freemulti(ifma);
  3396. /*
  3397. * The last reference to this instance of struct ifmultiaddr
  3398. * was released; the hardware should be notified of this change.
  3399. */
  3400. return 1;
  3401. }
  3402. /*
  3403. * Set the link layer address on an interface.
  3404. *
  3405. * At this time we only support certain types of interfaces,
  3406. * and we don't allow the length of the address to change.
  3407. *
  3408. * Set noinline to be dtrace-friendly
  3409. */
  3410. __noinline int
  3411. if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
  3412. {
  3413. struct sockaddr_dl *sdl;
  3414. struct ifaddr *ifa;
  3415. struct ifreq ifr;
  3416. ifa = ifp->if_addr;
  3417. if (ifa == NULL)
  3418. return (EINVAL);
  3419. sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  3420. if (sdl == NULL)
  3421. return (EINVAL);
  3422. if (len != sdl->sdl_alen) /* don't allow length to change */
  3423. return (EINVAL);
  3424. switch (ifp->if_type) {
  3425. case IFT_ETHER:
  3426. case IFT_XETHER:
  3427. case IFT_L2VLAN:
  3428. case IFT_BRIDGE:
  3429. case IFT_IEEE8023ADLAG:
  3430. bcopy(lladdr, LLADDR(sdl), len);
  3431. break;
  3432. default:
  3433. return (ENODEV);
  3434. }
  3435. /*
  3436. * If the interface is already up, we need
  3437. * to re-init it in order to reprogram its
  3438. * address filter.
  3439. */
  3440. if ((ifp->if_flags & IFF_UP) != 0) {
  3441. if (ifp->if_ioctl) {
  3442. ifp->if_flags &= ~IFF_UP;
  3443. ifr.ifr_flags = ifp->if_flags & 0xffff;
  3444. ifr.ifr_flagshigh = ifp->if_flags >> 16;
  3445. (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
  3446. ifp->if_flags |= IFF_UP;
  3447. ifr.ifr_flags = ifp->if_flags & 0xffff;
  3448. ifr.ifr_flagshigh = ifp->if_flags >> 16;
  3449. (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
  3450. }
  3451. }
  3452. EVENTHANDLER_INVOKE(iflladdr_event, ifp);
  3453. return (0);
  3454. }
  3455. /*
  3456. * Compat function for handling basic encapsulation requests.
  3457. * Not converted stacks (FDDI, IB, ..) supports traditional
  3458. * output model: ARP (and other similar L2 protocols) are handled
  3459. * inside output routine, arpresolve/nd6_resolve() returns MAC
  3460. * address instead of full prepend.
  3461. *
  3462. * This function creates calculated header==MAC for IPv4/IPv6 and
  3463. * returns EAFNOSUPPORT (which is then handled in ARP code) for other
  3464. * address families.
  3465. */
  3466. static int
  3467. if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
  3468. {
  3469. if (req->rtype != IFENCAP_LL)
  3470. return (EOPNOTSUPP);
  3471. if (req->bufsize < req->lladdr_len)
  3472. return (ENOMEM);
  3473. switch (req->family) {
  3474. case AF_INET:
  3475. case AF_INET6:
  3476. break;
  3477. default:
  3478. return (EAFNOSUPPORT);
  3479. }
  3480. /* Copy lladdr to storage as is */
  3481. memmove(req->buf, req->lladdr, req->lladdr_len);
  3482. req->bufsize = req->lladdr_len;
  3483. req->lladdr_off = 0;
  3484. return (0);
  3485. }
  3486. /*
  3487. * Tunnel interfaces can nest, also they may cause infinite recursion
  3488. * calls when misconfigured. We'll prevent this by detecting loops.
  3489. * High nesting level may cause stack exhaustion. We'll prevent this
  3490. * by introducing upper limit.
  3491. *
  3492. * Return 0, if tunnel nesting count is equal or less than limit.
  3493. */
  3494. int
  3495. if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
  3496. int limit)
  3497. {
  3498. struct m_tag *mtag;
  3499. int count;
  3500. count = 1;
  3501. mtag = NULL;
  3502. while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
  3503. if (*(struct ifnet **)(mtag + 1) == ifp) {
  3504. log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
  3505. return (EIO);
  3506. }
  3507. count++;
  3508. }
  3509. if (count > limit) {
  3510. log(LOG_NOTICE,
  3511. "%s: if_output recursively called too many times(%d)\n",
  3512. if_name(ifp), count);
  3513. return (EIO);
  3514. }
  3515. mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
  3516. if (mtag == NULL)
  3517. return (ENOMEM);
  3518. *(struct ifnet **)(mtag + 1) = ifp;
  3519. m_tag_prepend(m, mtag);
  3520. return (0);
  3521. }
  3522. /*
  3523. * Get the link layer address that was read from the hardware at attach.
  3524. *
  3525. * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
  3526. * their component interfaces as IFT_IEEE8023ADLAG.
  3527. */
  3528. int
  3529. if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
  3530. {
  3531. if (ifp->if_hw_addr == NULL)
  3532. return (ENODEV);
  3533. switch (ifp->if_type) {
  3534. case IFT_ETHER:
  3535. case IFT_IEEE8023ADLAG:
  3536. bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
  3537. return (0);
  3538. default:
  3539. return (ENODEV);
  3540. }
  3541. }
  3542. /*
  3543. * The name argument must be a pointer to storage which will last as
  3544. * long as the interface does. For physical devices, the result of
  3545. * device_get_name(dev) is a good choice and for pseudo-devices a
  3546. * static string works well.
  3547. */
  3548. void
  3549. if_initname(struct ifnet *ifp, const char *name, int unit)
  3550. {
  3551. ifp->if_dname = name;
  3552. ifp->if_dunit = unit;
  3553. if (unit != IF_DUNIT_NONE)
  3554. snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
  3555. else
  3556. strlcpy(ifp->if_xname, name, IFNAMSIZ);
  3557. }
  3558. static int
  3559. if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
  3560. {
  3561. char if_fmt[256];
  3562. snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
  3563. vlog(pri, if_fmt, ap);
  3564. return (0);
  3565. }
  3566. int
  3567. if_printf(struct ifnet *ifp, const char *fmt, ...)
  3568. {
  3569. va_list ap;
  3570. va_start(ap, fmt);
  3571. if_vlog(ifp, LOG_INFO, fmt, ap);
  3572. va_end(ap);
  3573. return (0);
  3574. }
  3575. int
  3576. if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
  3577. {
  3578. va_list ap;
  3579. va_start(ap, fmt);
  3580. if_vlog(ifp, pri, fmt, ap);
  3581. va_end(ap);
  3582. return (0);
  3583. }
  3584. void
  3585. if_start(struct ifnet *ifp)
  3586. {
  3587. (*(ifp)->if_start)(ifp);
  3588. }
  3589. /*
  3590. * Backwards compatibility interface for drivers
  3591. * that have not implemented it
  3592. */
  3593. static int
  3594. if_transmit_default(struct ifnet *ifp, struct mbuf *m)
  3595. {
  3596. int error;
  3597. IFQ_HANDOFF(ifp, m, error);
  3598. return (error);
  3599. }
  3600. static void
  3601. if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
  3602. {
  3603. m_freem(m);
  3604. }
  3605. int
  3606. if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
  3607. {
  3608. int active = 0;
  3609. IF_LOCK(ifq);
  3610. if (_IF_QFULL(ifq)) {
  3611. IF_UNLOCK(ifq);
  3612. if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
  3613. m_freem(m);
  3614. return (0);
  3615. }
  3616. if (ifp != NULL) {
  3617. if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
  3618. if (m->m_flags & (M_BCAST|M_MCAST))
  3619. if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
  3620. active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
  3621. }
  3622. _IF_ENQUEUE(ifq, m);
  3623. IF_UNLOCK(ifq);
  3624. if (ifp != NULL && !active)
  3625. (*(ifp)->if_start)(ifp);
  3626. return (1);
  3627. }
  3628. void
  3629. if_register_com_alloc(u_char type,
  3630. if_com_alloc_t *a, if_com_free_t *f)
  3631. {
  3632. KASSERT(if_com_alloc[type] == NULL,
  3633. ("if_register_com_alloc: %d already registered", type));
  3634. KASSERT(if_com_free[type] == NULL,
  3635. ("if_register_com_alloc: %d free already registered", type));
  3636. if_com_alloc[type] = a;
  3637. if_com_free[type] = f;
  3638. }
  3639. void
  3640. if_deregister_com_alloc(u_char type)
  3641. {
  3642. KASSERT(if_com_alloc[type] != NULL,
  3643. ("if_deregister_com_alloc: %d not registered", type));
  3644. KASSERT(if_com_free[type] != NULL,
  3645. ("if_deregister_com_alloc: %d free not registered", type));
  3646. /*
  3647. * Ensure all pending EPOCH(9) callbacks have been executed. This
  3648. * fixes issues about late invocation of if_destroy(), which leads
  3649. * to memory leak from if_com_alloc[type] allocated if_l2com.
  3650. */
  3651. NET_EPOCH_DRAIN_CALLBACKS();
  3652. if_com_alloc[type] = NULL;
  3653. if_com_free[type] = NULL;
  3654. }
  3655. /* API for driver access to network stack owned ifnet.*/
  3656. uint64_t
  3657. if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
  3658. {
  3659. uint64_t oldbrate;
  3660. oldbrate = ifp->if_baudrate;
  3661. ifp->if_baudrate = baudrate;
  3662. return (oldbrate);
  3663. }
  3664. uint64_t
  3665. if_getbaudrate(const if_t ifp)
  3666. {
  3667. return (ifp->if_baudrate);
  3668. }
  3669. int
  3670. if_setcapabilities(if_t ifp, int capabilities)
  3671. {
  3672. ifp->if_capabilities = capabilities;
  3673. return (0);
  3674. }
  3675. int
  3676. if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
  3677. {
  3678. ifp->if_capabilities &= ~clearbit;
  3679. ifp->if_capabilities |= setbit;
  3680. return (0);
  3681. }
  3682. int
  3683. if_getcapabilities(const if_t ifp)
  3684. {
  3685. return (ifp->if_capabilities);
  3686. }
  3687. int
  3688. if_setcapenable(if_t ifp, int capabilities)
  3689. {
  3690. ifp->if_capenable = capabilities;
  3691. return (0);
  3692. }
  3693. int
  3694. if_setcapenablebit(if_t ifp, int setcap, int clearcap)
  3695. {
  3696. ifp->if_capenable &= ~clearcap;
  3697. ifp->if_capenable |= setcap;
  3698. return (0);
  3699. }
  3700. int
  3701. if_setcapabilities2(if_t ifp, int capabilities)
  3702. {
  3703. ifp->if_capabilities2 = capabilities;
  3704. return (0);
  3705. }
  3706. int
  3707. if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
  3708. {
  3709. ifp->if_capabilities2 &= ~clearbit;
  3710. ifp->if_capabilities2 |= setbit;
  3711. return (0);
  3712. }
  3713. int
  3714. if_getcapabilities2(const if_t ifp)
  3715. {
  3716. return (ifp->if_capabilities2);
  3717. }
  3718. int
  3719. if_setcapenable2(if_t ifp, int capabilities2)
  3720. {
  3721. ifp->if_capenable2 = capabilities2;
  3722. return (0);
  3723. }
  3724. int
  3725. if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
  3726. {
  3727. ifp->if_capenable2 &= ~clearcap;
  3728. ifp->if_capenable2 |= setcap;
  3729. return (0);
  3730. }
  3731. const char *
  3732. if_getdname(const if_t ifp)
  3733. {
  3734. return (ifp->if_dname);
  3735. }
  3736. void
  3737. if_setdname(if_t ifp, const char *dname)
  3738. {
  3739. ifp->if_dname = dname;
  3740. }
  3741. const char *
  3742. if_name(if_t ifp)
  3743. {
  3744. return (ifp->if_xname);
  3745. }
  3746. int
  3747. if_setname(if_t ifp, const char *name)
  3748. {
  3749. if (strlen(name) > sizeof(ifp->if_xname) - 1)
  3750. return (ENAMETOOLONG);
  3751. strcpy(ifp->if_xname, name);
  3752. return (0);
  3753. }
  3754. int
  3755. if_togglecapenable(if_t ifp, int togglecap)
  3756. {
  3757. ifp->if_capenable ^= togglecap;
  3758. return (0);
  3759. }
  3760. int
  3761. if_getcapenable(const if_t ifp)
  3762. {
  3763. return (ifp->if_capenable);
  3764. }
  3765. int
  3766. if_togglecapenable2(if_t ifp, int togglecap)
  3767. {
  3768. ifp->if_capenable2 ^= togglecap;
  3769. return (0);
  3770. }
  3771. int
  3772. if_getcapenable2(const if_t ifp)
  3773. {
  3774. return (ifp->if_capenable2);
  3775. }
  3776. int
  3777. if_getdunit(const if_t ifp)
  3778. {
  3779. return (ifp->if_dunit);
  3780. }
  3781. int
  3782. if_getindex(const if_t ifp)
  3783. {
  3784. return (ifp->if_index);
  3785. }
  3786. int
  3787. if_getidxgen(const if_t ifp)
  3788. {
  3789. return (ifp->if_idxgen);
  3790. }
  3791. const char *
  3792. if_getdescr(if_t ifp)
  3793. {
  3794. return (ifp->if_description);
  3795. }
  3796. void
  3797. if_setdescr(if_t ifp, char *descrbuf)
  3798. {
  3799. sx_xlock(&ifdescr_sx);
  3800. char *odescrbuf = ifp->if_description;
  3801. ifp->if_description = descrbuf;
  3802. sx_xunlock(&ifdescr_sx);
  3803. if_freedescr(odescrbuf);
  3804. }
  3805. char *
  3806. if_allocdescr(size_t sz, int malloc_flag)
  3807. {
  3808. malloc_flag &= (M_WAITOK | M_NOWAIT);
  3809. return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
  3810. }
  3811. void
  3812. if_freedescr(char *descrbuf)
  3813. {
  3814. free(descrbuf, M_IFDESCR);
  3815. }
  3816. int
  3817. if_getalloctype(const if_t ifp)
  3818. {
  3819. return (ifp->if_alloctype);
  3820. }
  3821. void
  3822. if_setlastchange(if_t ifp)
  3823. {
  3824. getmicrotime(&ifp->if_lastchange);
  3825. }
  3826. /*
  3827. * This is largely undesirable because it ties ifnet to a device, but does
  3828. * provide flexiblity for an embedded product vendor. Should be used with
  3829. * the understanding that it violates the interface boundaries, and should be
  3830. * a last resort only.
  3831. */
  3832. int
  3833. if_setdev(if_t ifp, void *dev)
  3834. {
  3835. return (0);
  3836. }
  3837. int
  3838. if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
  3839. {
  3840. ifp->if_drv_flags &= ~clear_flags;
  3841. ifp->if_drv_flags |= set_flags;
  3842. return (0);
  3843. }
  3844. int
  3845. if_getdrvflags(const if_t ifp)
  3846. {
  3847. return (ifp->if_drv_flags);
  3848. }
  3849. int
  3850. if_setdrvflags(if_t ifp, int flags)
  3851. {
  3852. ifp->if_drv_flags = flags;
  3853. return (0);
  3854. }
  3855. int
  3856. if_setflags(if_t ifp, int flags)
  3857. {
  3858. ifp->if_flags = flags;
  3859. return (0);
  3860. }
  3861. int
  3862. if_setflagbits(if_t ifp, int set, int clear)
  3863. {
  3864. ifp->if_flags &= ~clear;
  3865. ifp->if_flags |= set;
  3866. return (0);
  3867. }
  3868. int
  3869. if_getflags(const if_t ifp)
  3870. {
  3871. return (ifp->if_flags);
  3872. }
  3873. int
  3874. if_clearhwassist(if_t ifp)
  3875. {
  3876. ifp->if_hwassist = 0;
  3877. return (0);
  3878. }
  3879. int
  3880. if_sethwassistbits(if_t ifp, int toset, int toclear)
  3881. {
  3882. ifp->if_hwassist &= ~toclear;
  3883. ifp->if_hwassist |= toset;
  3884. return (0);
  3885. }
  3886. int
  3887. if_sethwassist(if_t ifp, int hwassist_bit)
  3888. {
  3889. ifp->if_hwassist = hwassist_bit;
  3890. return (0);
  3891. }
  3892. int
  3893. if_gethwassist(const if_t ifp)
  3894. {
  3895. return (ifp->if_hwassist);
  3896. }
  3897. int
  3898. if_togglehwassist(if_t ifp, int toggle_bits)
  3899. {
  3900. ifp->if_hwassist ^= toggle_bits;
  3901. return (0);
  3902. }
  3903. int
  3904. if_setmtu(if_t ifp, int mtu)
  3905. {
  3906. ifp->if_mtu = mtu;
  3907. return (0);
  3908. }
  3909. void
  3910. if_notifymtu(if_t ifp)
  3911. {
  3912. #ifdef INET6
  3913. nd6_setmtu(ifp);
  3914. #endif
  3915. rt_updatemtu(ifp);
  3916. }
  3917. int
  3918. if_getmtu(const if_t ifp)
  3919. {
  3920. return (ifp->if_mtu);
  3921. }
  3922. int
  3923. if_getmtu_family(const if_t ifp, int family)
  3924. {
  3925. struct domain *dp;
  3926. SLIST_FOREACH(dp, &domains, dom_next) {
  3927. if (dp->dom_family == family && dp->dom_ifmtu != NULL)
  3928. return (dp->dom_ifmtu(ifp));
  3929. }
  3930. return (ifp->if_mtu);
  3931. }
  3932. /*
  3933. * Methods for drivers to access interface unicast and multicast
  3934. * link level addresses. Driver shall not know 'struct ifaddr' neither
  3935. * 'struct ifmultiaddr'.
  3936. */
  3937. u_int
  3938. if_lladdr_count(if_t ifp)
  3939. {
  3940. struct epoch_tracker et;
  3941. struct ifaddr *ifa;
  3942. u_int count;
  3943. count = 0;
  3944. NET_EPOCH_ENTER(et);
  3945. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  3946. if (ifa->ifa_addr->sa_family == AF_LINK)
  3947. count++;
  3948. NET_EPOCH_EXIT(et);
  3949. return (count);
  3950. }
  3951. int
  3952. if_foreach(if_foreach_cb_t cb, void *cb_arg)
  3953. {
  3954. if_t ifp;
  3955. int error;
  3956. NET_EPOCH_ASSERT();
  3957. MPASS(cb);
  3958. error = 0;
  3959. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  3960. error = cb(ifp, cb_arg);
  3961. if (error != 0)
  3962. break;
  3963. }
  3964. return (error);
  3965. }
  3966. /*
  3967. * Iterates over the list of interfaces, permitting callback function @cb to sleep.
  3968. * Stops iteration if @cb returns non-zero error code.
  3969. * Returns the last error code from @cb.
  3970. * @match_cb: optional match callback limiting the iteration to only matched interfaces
  3971. * @match_arg: argument to pass to @match_cb
  3972. * @cb: iteration callback
  3973. * @cb_arg: argument to pass to @cb
  3974. */
  3975. int
  3976. if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
  3977. void *cb_arg)
  3978. {
  3979. int match_count = 0, array_size = 16; /* 128 bytes for malloc */
  3980. struct ifnet **match_array = NULL;
  3981. int error = 0;
  3982. MPASS(cb);
  3983. while (true) {
  3984. struct ifnet **new_array;
  3985. int new_size = array_size;
  3986. struct epoch_tracker et;
  3987. struct ifnet *ifp;
  3988. while (new_size < match_count)
  3989. new_size *= 2;
  3990. new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
  3991. if (match_array != NULL)
  3992. memcpy(new_array, match_array, array_size * sizeof(void *));
  3993. free(match_array, M_TEMP);
  3994. match_array = new_array;
  3995. array_size = new_size;
  3996. match_count = 0;
  3997. NET_EPOCH_ENTER(et);
  3998. CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  3999. if (match_cb != NULL && !match_cb(ifp, match_arg))
  4000. continue;
  4001. if (match_count < array_size) {
  4002. if (if_try_ref(ifp))
  4003. match_array[match_count++] = ifp;
  4004. } else
  4005. match_count++;
  4006. }
  4007. NET_EPOCH_EXIT(et);
  4008. if (match_count > array_size) {
  4009. for (int i = 0; i < array_size; i++)
  4010. if_rele(match_array[i]);
  4011. continue;
  4012. } else {
  4013. for (int i = 0; i < match_count; i++) {
  4014. if (error == 0)
  4015. error = cb(match_array[i], cb_arg);
  4016. if_rele(match_array[i]);
  4017. }
  4018. free(match_array, M_TEMP);
  4019. break;
  4020. }
  4021. }
  4022. return (error);
  4023. }
  4024. /*
  4025. * Uses just 1 pointer of the 4 available in the public struct.
  4026. */
  4027. if_t
  4028. if_iter_start(struct if_iter *iter)
  4029. {
  4030. if_t ifp;
  4031. NET_EPOCH_ASSERT();
  4032. bzero(iter, sizeof(*iter));
  4033. ifp = CK_STAILQ_FIRST(&V_ifnet);
  4034. if (ifp != NULL)
  4035. iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
  4036. else
  4037. iter->context[0] = NULL;
  4038. return (ifp);
  4039. }
  4040. if_t
  4041. if_iter_next(struct if_iter *iter)
  4042. {
  4043. if_t cur_ifp = iter->context[0];
  4044. if (cur_ifp != NULL)
  4045. iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
  4046. return (cur_ifp);
  4047. }
  4048. void
  4049. if_iter_finish(struct if_iter *iter)
  4050. {
  4051. /* Nothing to do here for now. */
  4052. }
  4053. u_int
  4054. if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
  4055. {
  4056. struct epoch_tracker et;
  4057. struct ifaddr *ifa;
  4058. u_int count;
  4059. MPASS(cb);
  4060. count = 0;
  4061. NET_EPOCH_ENTER(et);
  4062. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  4063. if (ifa->ifa_addr->sa_family != AF_LINK)
  4064. continue;
  4065. count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
  4066. count);
  4067. }
  4068. NET_EPOCH_EXIT(et);
  4069. return (count);
  4070. }
  4071. u_int
  4072. if_llmaddr_count(if_t ifp)
  4073. {
  4074. struct epoch_tracker et;
  4075. struct ifmultiaddr *ifma;
  4076. int count;
  4077. count = 0;
  4078. NET_EPOCH_ENTER(et);
  4079. CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
  4080. if (ifma->ifma_addr->sa_family == AF_LINK)
  4081. count++;
  4082. NET_EPOCH_EXIT(et);
  4083. return (count);
  4084. }
  4085. bool
  4086. if_maddr_empty(if_t ifp)
  4087. {
  4088. return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
  4089. }
  4090. u_int
  4091. if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
  4092. {
  4093. struct epoch_tracker et;
  4094. struct ifmultiaddr *ifma;
  4095. u_int count;
  4096. MPASS(cb);
  4097. count = 0;
  4098. NET_EPOCH_ENTER(et);
  4099. CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
  4100. if (ifma->ifma_addr->sa_family != AF_LINK)
  4101. continue;
  4102. count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
  4103. count);
  4104. }
  4105. NET_EPOCH_EXIT(et);
  4106. return (count);
  4107. }
  4108. u_int
  4109. if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
  4110. {
  4111. struct epoch_tracker et;
  4112. struct ifaddr *ifa;
  4113. u_int count;
  4114. MPASS(cb);
  4115. count = 0;
  4116. NET_EPOCH_ENTER(et);
  4117. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  4118. if (ifa->ifa_addr->sa_family != type)
  4119. continue;
  4120. count += (*cb)(cb_arg, ifa, count);
  4121. }
  4122. NET_EPOCH_EXIT(et);
  4123. return (count);
  4124. }
  4125. struct ifaddr *
  4126. ifa_iter_start(if_t ifp, struct ifa_iter *iter)
  4127. {
  4128. struct ifaddr *ifa;
  4129. NET_EPOCH_ASSERT();
  4130. bzero(iter, sizeof(*iter));
  4131. ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
  4132. if (ifa != NULL)
  4133. iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
  4134. else
  4135. iter->context[0] = NULL;
  4136. return (ifa);
  4137. }
  4138. struct ifaddr *
  4139. ifa_iter_next(struct ifa_iter *iter)
  4140. {
  4141. struct ifaddr *ifa = iter->context[0];
  4142. if (ifa != NULL)
  4143. iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
  4144. return (ifa);
  4145. }
  4146. void
  4147. ifa_iter_finish(struct ifa_iter *iter)
  4148. {
  4149. /* Nothing to do here for now. */
  4150. }
  4151. int
  4152. if_setsoftc(if_t ifp, void *softc)
  4153. {
  4154. ifp->if_softc = softc;
  4155. return (0);
  4156. }
  4157. void *
  4158. if_getsoftc(const if_t ifp)
  4159. {
  4160. return (ifp->if_softc);
  4161. }
  4162. void
  4163. if_setrcvif(struct mbuf *m, if_t ifp)
  4164. {
  4165. MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
  4166. m->m_pkthdr.rcvif = (struct ifnet *)ifp;
  4167. }
  4168. void
  4169. if_setvtag(struct mbuf *m, uint16_t tag)
  4170. {
  4171. m->m_pkthdr.ether_vtag = tag;
  4172. }
  4173. uint16_t
  4174. if_getvtag(struct mbuf *m)
  4175. {
  4176. return (m->m_pkthdr.ether_vtag);
  4177. }
  4178. int
  4179. if_sendq_empty(if_t ifp)
  4180. {
  4181. return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
  4182. }
  4183. struct ifaddr *
  4184. if_getifaddr(const if_t ifp)
  4185. {
  4186. return (ifp->if_addr);
  4187. }
  4188. int
  4189. if_getamcount(const if_t ifp)
  4190. {
  4191. return (ifp->if_amcount);
  4192. }
  4193. int
  4194. if_setsendqready(if_t ifp)
  4195. {
  4196. IFQ_SET_READY(&ifp->if_snd);
  4197. return (0);
  4198. }
  4199. int
  4200. if_setsendqlen(if_t ifp, int tx_desc_count)
  4201. {
  4202. IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
  4203. ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
  4204. return (0);
  4205. }
  4206. void
  4207. if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
  4208. {
  4209. ifp->if_netmap = na;
  4210. }
  4211. struct netmap_adapter *
  4212. if_getnetmapadapter(if_t ifp)
  4213. {
  4214. return (ifp->if_netmap);
  4215. }
  4216. int
  4217. if_vlantrunkinuse(if_t ifp)
  4218. {
  4219. return (ifp->if_vlantrunk != NULL);
  4220. }
  4221. void
  4222. if_init(if_t ifp, void *ctx)
  4223. {
  4224. (*ifp->if_init)(ctx);
  4225. }
  4226. void
  4227. if_input(if_t ifp, struct mbuf* sendmp)
  4228. {
  4229. (*ifp->if_input)(ifp, sendmp);
  4230. }
  4231. int
  4232. if_transmit(if_t ifp, struct mbuf *m)
  4233. {
  4234. return ((*ifp->if_transmit)(ifp, m));
  4235. }
  4236. int
  4237. if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
  4238. {
  4239. if (ifp->if_resolvemulti == NULL)
  4240. return (EOPNOTSUPP);
  4241. return (ifp->if_resolvemulti(ifp, srcs, dst));
  4242. }
  4243. int
  4244. if_ioctl(if_t ifp, u_long cmd, void *data)
  4245. {
  4246. if (ifp->if_ioctl == NULL)
  4247. return (EOPNOTSUPP);
  4248. return (ifp->if_ioctl(ifp, cmd, data));
  4249. }
  4250. struct mbuf *
  4251. if_dequeue(if_t ifp)
  4252. {
  4253. struct mbuf *m;
  4254. IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
  4255. return (m);
  4256. }
  4257. int
  4258. if_sendq_prepend(if_t ifp, struct mbuf *m)
  4259. {
  4260. IFQ_DRV_PREPEND(&ifp->if_snd, m);
  4261. return (0);
  4262. }
  4263. int
  4264. if_setifheaderlen(if_t ifp, int len)
  4265. {
  4266. ifp->if_hdrlen = len;
  4267. return (0);
  4268. }
  4269. caddr_t
  4270. if_getlladdr(const if_t ifp)
  4271. {
  4272. return (IF_LLADDR(ifp));
  4273. }
  4274. void *
  4275. if_gethandle(u_char type)
  4276. {
  4277. return (if_alloc(type));
  4278. }
  4279. void
  4280. if_vlancap(if_t ifp)
  4281. {
  4282. VLAN_CAPABILITIES(ifp);
  4283. }
  4284. int
  4285. if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
  4286. {
  4287. ifp->if_hw_tsomax = if_hw_tsomax;
  4288. return (0);
  4289. }
  4290. int
  4291. if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
  4292. {
  4293. ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
  4294. return (0);
  4295. }
  4296. int
  4297. if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
  4298. {
  4299. ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
  4300. return (0);
  4301. }
  4302. u_int
  4303. if_gethwtsomax(const if_t ifp)
  4304. {
  4305. return (ifp->if_hw_tsomax);
  4306. }
  4307. u_int
  4308. if_gethwtsomaxsegcount(const if_t ifp)
  4309. {
  4310. return (ifp->if_hw_tsomaxsegcount);
  4311. }
  4312. u_int
  4313. if_gethwtsomaxsegsize(const if_t ifp)
  4314. {
  4315. return (ifp->if_hw_tsomaxsegsize);
  4316. }
  4317. void
  4318. if_setinitfn(if_t ifp, if_init_fn_t init_fn)
  4319. {
  4320. ifp->if_init = init_fn;
  4321. }
  4322. void
  4323. if_setinputfn(if_t ifp, if_input_fn_t input_fn)
  4324. {
  4325. ifp->if_input = input_fn;
  4326. }
  4327. if_input_fn_t
  4328. if_getinputfn(if_t ifp)
  4329. {
  4330. return (ifp->if_input);
  4331. }
  4332. void
  4333. if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
  4334. {
  4335. ifp->if_ioctl = ioctl_fn;
  4336. }
  4337. void
  4338. if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
  4339. {
  4340. ifp->if_output = output_fn;
  4341. }
  4342. void
  4343. if_setstartfn(if_t ifp, if_start_fn_t start_fn)
  4344. {
  4345. ifp->if_start = start_fn;
  4346. }
  4347. if_start_fn_t
  4348. if_getstartfn(if_t ifp)
  4349. {
  4350. return (ifp->if_start);
  4351. }
  4352. void
  4353. if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
  4354. {
  4355. ifp->if_transmit = start_fn;
  4356. }
  4357. if_transmit_fn_t
  4358. if_gettransmitfn(if_t ifp)
  4359. {
  4360. return (ifp->if_transmit);
  4361. }
  4362. void
  4363. if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
  4364. {
  4365. ifp->if_qflush = flush_fn;
  4366. }
  4367. void
  4368. if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
  4369. {
  4370. ifp->if_snd_tag_alloc = alloc_fn;
  4371. }
  4372. int
  4373. if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
  4374. struct m_snd_tag **mstp)
  4375. {
  4376. if (ifp->if_snd_tag_alloc == NULL)
  4377. return (EOPNOTSUPP);
  4378. return (ifp->if_snd_tag_alloc(ifp, params, mstp));
  4379. }
  4380. void
  4381. if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
  4382. {
  4383. ifp->if_get_counter = fn;
  4384. }
  4385. void
  4386. if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
  4387. {
  4388. ifp->if_reassign = fn;
  4389. }
  4390. void
  4391. if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
  4392. {
  4393. ifp->if_ratelimit_query = fn;
  4394. }
  4395. void
  4396. if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
  4397. {
  4398. ifp->if_debugnet_methods = m;
  4399. }
  4400. struct label *
  4401. if_getmaclabel(if_t ifp)
  4402. {
  4403. return (ifp->if_label);
  4404. }
  4405. void
  4406. if_setmaclabel(if_t ifp, struct label *label)
  4407. {
  4408. ifp->if_label = label;
  4409. }
  4410. int
  4411. if_gettype(if_t ifp)
  4412. {
  4413. return (ifp->if_type);
  4414. }
  4415. void *
  4416. if_getllsoftc(if_t ifp)
  4417. {
  4418. return (ifp->if_llsoftc);
  4419. }
  4420. void
  4421. if_setllsoftc(if_t ifp, void *llsoftc)
  4422. {
  4423. ifp->if_llsoftc = llsoftc;
  4424. };
  4425. int
  4426. if_getlinkstate(if_t ifp)
  4427. {
  4428. return (ifp->if_link_state);
  4429. }
  4430. const uint8_t *
  4431. if_getbroadcastaddr(if_t ifp)
  4432. {
  4433. return (ifp->if_broadcastaddr);
  4434. }
  4435. void
  4436. if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
  4437. {
  4438. ifp->if_broadcastaddr = addr;
  4439. }
  4440. int
  4441. if_getnumadomain(if_t ifp)
  4442. {
  4443. return (ifp->if_numa_domain);
  4444. }
  4445. uint64_t
  4446. if_getcounter(if_t ifp, ift_counter counter)
  4447. {
  4448. return (ifp->if_get_counter(ifp, counter));
  4449. }
  4450. bool
  4451. if_altq_is_enabled(if_t ifp)
  4452. {
  4453. return (ALTQ_IS_ENABLED(&ifp->if_snd));
  4454. }
  4455. struct vnet *
  4456. if_getvnet(if_t ifp)
  4457. {
  4458. return (ifp->if_vnet);
  4459. }
  4460. void *
  4461. if_getafdata(if_t ifp, int af)
  4462. {
  4463. return (ifp->if_afdata[af]);
  4464. }
  4465. u_int
  4466. if_getfib(if_t ifp)
  4467. {
  4468. return (ifp->if_fib);
  4469. }
  4470. uint8_t
  4471. if_getaddrlen(if_t ifp)
  4472. {
  4473. return (ifp->if_addrlen);
  4474. }
  4475. struct bpf_if *
  4476. if_getbpf(if_t ifp)
  4477. {
  4478. return (ifp->if_bpf);
  4479. }
  4480. struct ifvlantrunk *
  4481. if_getvlantrunk(if_t ifp)
  4482. {
  4483. return (ifp->if_vlantrunk);
  4484. }
  4485. uint8_t
  4486. if_getpcp(if_t ifp)
  4487. {
  4488. return (ifp->if_pcp);
  4489. }
  4490. void *
  4491. if_getl2com(if_t ifp)
  4492. {
  4493. return (ifp->if_l2com);
  4494. }
  4495. void
  4496. if_setipsec_accel_methods(if_t ifp, const struct if_ipsec_accel_methods *m)
  4497. {
  4498. ifp->if_ipsec_accel_m = m;
  4499. }
  4500. #ifdef DDB
  4501. static void
  4502. if_show_ifnet(struct ifnet *ifp)
  4503. {
  4504. if (ifp == NULL)
  4505. return;
  4506. db_printf("%s:\n", ifp->if_xname);
  4507. #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
  4508. IF_DB_PRINTF("%s", if_dname);
  4509. IF_DB_PRINTF("%d", if_dunit);
  4510. IF_DB_PRINTF("%s", if_description);
  4511. IF_DB_PRINTF("%u", if_index);
  4512. IF_DB_PRINTF("%d", if_idxgen);
  4513. IF_DB_PRINTF("%u", if_refcount);
  4514. IF_DB_PRINTF("%p", if_softc);
  4515. IF_DB_PRINTF("%p", if_l2com);
  4516. IF_DB_PRINTF("%p", if_llsoftc);
  4517. IF_DB_PRINTF("%d", if_amcount);
  4518. IF_DB_PRINTF("%p", if_addr);
  4519. IF_DB_PRINTF("%p", if_broadcastaddr);
  4520. IF_DB_PRINTF("%p", if_afdata);
  4521. IF_DB_PRINTF("%d", if_afdata_initialized);
  4522. IF_DB_PRINTF("%u", if_fib);
  4523. IF_DB_PRINTF("%p", if_vnet);
  4524. IF_DB_PRINTF("%p", if_home_vnet);
  4525. IF_DB_PRINTF("%p", if_vlantrunk);
  4526. IF_DB_PRINTF("%p", if_bpf);
  4527. IF_DB_PRINTF("%u", if_pcount);
  4528. IF_DB_PRINTF("%p", if_bridge);
  4529. IF_DB_PRINTF("%p", if_lagg);
  4530. IF_DB_PRINTF("%p", if_pf_kif);
  4531. IF_DB_PRINTF("%p", if_carp);
  4532. IF_DB_PRINTF("%p", if_label);
  4533. IF_DB_PRINTF("%p", if_netmap);
  4534. IF_DB_PRINTF("0x%08x", if_flags);
  4535. IF_DB_PRINTF("0x%08x", if_drv_flags);
  4536. IF_DB_PRINTF("0x%08x", if_capabilities);
  4537. IF_DB_PRINTF("0x%08x", if_capenable);
  4538. IF_DB_PRINTF("%p", if_snd.ifq_head);
  4539. IF_DB_PRINTF("%p", if_snd.ifq_tail);
  4540. IF_DB_PRINTF("%d", if_snd.ifq_len);
  4541. IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
  4542. IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
  4543. IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
  4544. IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
  4545. IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
  4546. IF_DB_PRINTF("%d", if_snd.altq_type);
  4547. IF_DB_PRINTF("%x", if_snd.altq_flags);
  4548. #undef IF_DB_PRINTF
  4549. }
  4550. DB_SHOW_COMMAND(ifnet, db_show_ifnet)
  4551. {
  4552. if (!have_addr) {
  4553. db_printf("usage: show ifnet <struct ifnet *>\n");
  4554. return;
  4555. }
  4556. if_show_ifnet((struct ifnet *)addr);
  4557. }
  4558. DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
  4559. {
  4560. struct ifnet *ifp;
  4561. u_short idx;
  4562. for (idx = 1; idx <= if_index; idx++) {
  4563. ifp = ifindex_table[idx].ife_ifnet;
  4564. if (ifp == NULL)
  4565. continue;
  4566. db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);
  4567. if (db_pager_quit)
  4568. break;
  4569. }
  4570. }
  4571. #endif /* DDB */