rtnetlink.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Routing netlink socket interface: protocol independent part.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Fixes:
  16. * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
  17. */
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/socket.h>
  22. #include <linux/kernel.h>
  23. #include <linux/timer.h>
  24. #include <linux/string.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/capability.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/security.h>
  35. #include <linux/mutex.h>
  36. #include <linux/if_addr.h>
  37. #include <linux/if_bridge.h>
  38. #include <linux/if_vlan.h>
  39. #include <linux/pci.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/inet.h>
  43. #include <linux/netdevice.h>
  44. #include <net/switchdev.h>
  45. #include <net/ip.h>
  46. #include <net/protocol.h>
  47. #include <net/arp.h>
  48. #include <net/route.h>
  49. #include <net/udp.h>
  50. #include <net/tcp.h>
  51. #include <net/sock.h>
  52. #include <net/pkt_sched.h>
  53. #include <net/fib_rules.h>
  54. #include <net/rtnetlink.h>
  55. #include <net/net_namespace.h>
  56. struct rtnl_link {
  57. rtnl_doit_func doit;
  58. rtnl_dumpit_func dumpit;
  59. rtnl_calcit_func calcit;
  60. };
  61. static DEFINE_MUTEX(rtnl_mutex);
  62. void rtnl_lock(void)
  63. {
  64. mutex_lock(&rtnl_mutex);
  65. }
  66. EXPORT_SYMBOL(rtnl_lock);
  67. static struct sk_buff *defer_kfree_skb_list;
  68. void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
  69. {
  70. if (head && tail) {
  71. tail->next = defer_kfree_skb_list;
  72. defer_kfree_skb_list = head;
  73. }
  74. }
  75. EXPORT_SYMBOL(rtnl_kfree_skbs);
  76. void __rtnl_unlock(void)
  77. {
  78. struct sk_buff *head = defer_kfree_skb_list;
  79. defer_kfree_skb_list = NULL;
  80. mutex_unlock(&rtnl_mutex);
  81. while (head) {
  82. struct sk_buff *next = head->next;
  83. kfree_skb(head);
  84. cond_resched();
  85. head = next;
  86. }
  87. }
  88. void rtnl_unlock(void)
  89. {
  90. /* This fellow will unlock it for us. */
  91. netdev_run_todo();
  92. }
  93. EXPORT_SYMBOL(rtnl_unlock);
  94. int rtnl_trylock(void)
  95. {
  96. return mutex_trylock(&rtnl_mutex);
  97. }
  98. EXPORT_SYMBOL(rtnl_trylock);
  99. int rtnl_is_locked(void)
  100. {
  101. return mutex_is_locked(&rtnl_mutex);
  102. }
  103. EXPORT_SYMBOL(rtnl_is_locked);
  104. #ifdef CONFIG_PROVE_LOCKING
  105. bool lockdep_rtnl_is_held(void)
  106. {
  107. return lockdep_is_held(&rtnl_mutex);
  108. }
  109. EXPORT_SYMBOL(lockdep_rtnl_is_held);
  110. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  111. static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
  112. static inline int rtm_msgindex(int msgtype)
  113. {
  114. int msgindex = msgtype - RTM_BASE;
  115. /*
  116. * msgindex < 0 implies someone tried to register a netlink
  117. * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
  118. * the message type has not been added to linux/rtnetlink.h
  119. */
  120. BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
  121. return msgindex;
  122. }
  123. static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
  124. {
  125. struct rtnl_link *tab;
  126. if (protocol <= RTNL_FAMILY_MAX)
  127. tab = rtnl_msg_handlers[protocol];
  128. else
  129. tab = NULL;
  130. if (tab == NULL || tab[msgindex].doit == NULL)
  131. tab = rtnl_msg_handlers[PF_UNSPEC];
  132. return tab[msgindex].doit;
  133. }
  134. static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
  135. {
  136. struct rtnl_link *tab;
  137. if (protocol <= RTNL_FAMILY_MAX)
  138. tab = rtnl_msg_handlers[protocol];
  139. else
  140. tab = NULL;
  141. if (tab == NULL || tab[msgindex].dumpit == NULL)
  142. tab = rtnl_msg_handlers[PF_UNSPEC];
  143. return tab[msgindex].dumpit;
  144. }
  145. static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
  146. {
  147. struct rtnl_link *tab;
  148. if (protocol <= RTNL_FAMILY_MAX)
  149. tab = rtnl_msg_handlers[protocol];
  150. else
  151. tab = NULL;
  152. if (tab == NULL || tab[msgindex].calcit == NULL)
  153. tab = rtnl_msg_handlers[PF_UNSPEC];
  154. return tab[msgindex].calcit;
  155. }
  156. /**
  157. * __rtnl_register - Register a rtnetlink message type
  158. * @protocol: Protocol family or PF_UNSPEC
  159. * @msgtype: rtnetlink message type
  160. * @doit: Function pointer called for each request message
  161. * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
  162. * @calcit: Function pointer to calc size of dump message
  163. *
  164. * Registers the specified function pointers (at least one of them has
  165. * to be non-NULL) to be called whenever a request message for the
  166. * specified protocol family and message type is received.
  167. *
  168. * The special protocol family PF_UNSPEC may be used to define fallback
  169. * function pointers for the case when no entry for the specific protocol
  170. * family exists.
  171. *
  172. * Returns 0 on success or a negative error code.
  173. */
  174. int __rtnl_register(int protocol, int msgtype,
  175. rtnl_doit_func doit, rtnl_dumpit_func dumpit,
  176. rtnl_calcit_func calcit)
  177. {
  178. struct rtnl_link *tab;
  179. int msgindex;
  180. BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
  181. msgindex = rtm_msgindex(msgtype);
  182. tab = rtnl_msg_handlers[protocol];
  183. if (tab == NULL) {
  184. tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
  185. if (tab == NULL)
  186. return -ENOBUFS;
  187. rtnl_msg_handlers[protocol] = tab;
  188. }
  189. if (doit)
  190. tab[msgindex].doit = doit;
  191. if (dumpit)
  192. tab[msgindex].dumpit = dumpit;
  193. if (calcit)
  194. tab[msgindex].calcit = calcit;
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(__rtnl_register);
  198. /**
  199. * rtnl_register - Register a rtnetlink message type
  200. *
  201. * Identical to __rtnl_register() but panics on failure. This is useful
  202. * as failure of this function is very unlikely, it can only happen due
  203. * to lack of memory when allocating the chain to store all message
  204. * handlers for a protocol. Meant for use in init functions where lack
  205. * of memory implies no sense in continuing.
  206. */
  207. void rtnl_register(int protocol, int msgtype,
  208. rtnl_doit_func doit, rtnl_dumpit_func dumpit,
  209. rtnl_calcit_func calcit)
  210. {
  211. if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
  212. panic("Unable to register rtnetlink message handler, "
  213. "protocol = %d, message type = %d\n",
  214. protocol, msgtype);
  215. }
  216. EXPORT_SYMBOL_GPL(rtnl_register);
  217. /**
  218. * rtnl_unregister - Unregister a rtnetlink message type
  219. * @protocol: Protocol family or PF_UNSPEC
  220. * @msgtype: rtnetlink message type
  221. *
  222. * Returns 0 on success or a negative error code.
  223. */
  224. int rtnl_unregister(int protocol, int msgtype)
  225. {
  226. int msgindex;
  227. BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
  228. msgindex = rtm_msgindex(msgtype);
  229. if (rtnl_msg_handlers[protocol] == NULL)
  230. return -ENOENT;
  231. rtnl_msg_handlers[protocol][msgindex].doit = NULL;
  232. rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
  233. rtnl_msg_handlers[protocol][msgindex].calcit = NULL;
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(rtnl_unregister);
  237. /**
  238. * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
  239. * @protocol : Protocol family or PF_UNSPEC
  240. *
  241. * Identical to calling rtnl_unregster() for all registered message types
  242. * of a certain protocol family.
  243. */
  244. void rtnl_unregister_all(int protocol)
  245. {
  246. BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
  247. kfree(rtnl_msg_handlers[protocol]);
  248. rtnl_msg_handlers[protocol] = NULL;
  249. }
  250. EXPORT_SYMBOL_GPL(rtnl_unregister_all);
  251. static LIST_HEAD(link_ops);
  252. static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
  253. {
  254. const struct rtnl_link_ops *ops;
  255. list_for_each_entry(ops, &link_ops, list) {
  256. if (!strcmp(ops->kind, kind))
  257. return ops;
  258. }
  259. return NULL;
  260. }
  261. /**
  262. * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
  263. * @ops: struct rtnl_link_ops * to register
  264. *
  265. * The caller must hold the rtnl_mutex. This function should be used
  266. * by drivers that create devices during module initialization. It
  267. * must be called before registering the devices.
  268. *
  269. * Returns 0 on success or a negative error code.
  270. */
  271. int __rtnl_link_register(struct rtnl_link_ops *ops)
  272. {
  273. if (rtnl_link_ops_get(ops->kind))
  274. return -EEXIST;
  275. /* The check for setup is here because if ops
  276. * does not have that filled up, it is not possible
  277. * to use the ops for creating device. So do not
  278. * fill up dellink as well. That disables rtnl_dellink.
  279. */
  280. if (ops->setup && !ops->dellink)
  281. ops->dellink = unregister_netdevice_queue;
  282. list_add_tail(&ops->list, &link_ops);
  283. return 0;
  284. }
  285. EXPORT_SYMBOL_GPL(__rtnl_link_register);
  286. /**
  287. * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
  288. * @ops: struct rtnl_link_ops * to register
  289. *
  290. * Returns 0 on success or a negative error code.
  291. */
  292. int rtnl_link_register(struct rtnl_link_ops *ops)
  293. {
  294. int err;
  295. rtnl_lock();
  296. err = __rtnl_link_register(ops);
  297. rtnl_unlock();
  298. return err;
  299. }
  300. EXPORT_SYMBOL_GPL(rtnl_link_register);
  301. static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
  302. {
  303. struct net_device *dev;
  304. LIST_HEAD(list_kill);
  305. for_each_netdev(net, dev) {
  306. if (dev->rtnl_link_ops == ops)
  307. ops->dellink(dev, &list_kill);
  308. }
  309. unregister_netdevice_many(&list_kill);
  310. }
  311. /**
  312. * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
  313. * @ops: struct rtnl_link_ops * to unregister
  314. *
  315. * The caller must hold the rtnl_mutex.
  316. */
  317. void __rtnl_link_unregister(struct rtnl_link_ops *ops)
  318. {
  319. struct net *net;
  320. for_each_net(net) {
  321. __rtnl_kill_links(net, ops);
  322. }
  323. list_del(&ops->list);
  324. }
  325. EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
  326. /* Return with the rtnl_lock held when there are no network
  327. * devices unregistering in any network namespace.
  328. */
  329. static void rtnl_lock_unregistering_all(void)
  330. {
  331. struct net *net;
  332. bool unregistering;
  333. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  334. add_wait_queue(&netdev_unregistering_wq, &wait);
  335. for (;;) {
  336. unregistering = false;
  337. rtnl_lock();
  338. for_each_net(net) {
  339. if (net->dev_unreg_count > 0) {
  340. unregistering = true;
  341. break;
  342. }
  343. }
  344. if (!unregistering)
  345. break;
  346. __rtnl_unlock();
  347. wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  348. }
  349. remove_wait_queue(&netdev_unregistering_wq, &wait);
  350. }
  351. /**
  352. * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
  353. * @ops: struct rtnl_link_ops * to unregister
  354. */
  355. void rtnl_link_unregister(struct rtnl_link_ops *ops)
  356. {
  357. /* Close the race with cleanup_net() */
  358. mutex_lock(&net_mutex);
  359. rtnl_lock_unregistering_all();
  360. __rtnl_link_unregister(ops);
  361. rtnl_unlock();
  362. mutex_unlock(&net_mutex);
  363. }
  364. EXPORT_SYMBOL_GPL(rtnl_link_unregister);
  365. static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
  366. {
  367. struct net_device *master_dev;
  368. const struct rtnl_link_ops *ops;
  369. master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
  370. if (!master_dev)
  371. return 0;
  372. ops = master_dev->rtnl_link_ops;
  373. if (!ops || !ops->get_slave_size)
  374. return 0;
  375. /* IFLA_INFO_SLAVE_DATA + nested data */
  376. return nla_total_size(sizeof(struct nlattr)) +
  377. ops->get_slave_size(master_dev, dev);
  378. }
  379. static size_t rtnl_link_get_size(const struct net_device *dev)
  380. {
  381. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  382. size_t size;
  383. if (!ops)
  384. return 0;
  385. size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
  386. nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
  387. if (ops->get_size)
  388. /* IFLA_INFO_DATA + nested data */
  389. size += nla_total_size(sizeof(struct nlattr)) +
  390. ops->get_size(dev);
  391. if (ops->get_xstats_size)
  392. /* IFLA_INFO_XSTATS */
  393. size += nla_total_size(ops->get_xstats_size(dev));
  394. size += rtnl_link_get_slave_info_data_size(dev);
  395. return size;
  396. }
  397. static LIST_HEAD(rtnl_af_ops);
  398. static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
  399. {
  400. const struct rtnl_af_ops *ops;
  401. list_for_each_entry(ops, &rtnl_af_ops, list) {
  402. if (ops->family == family)
  403. return ops;
  404. }
  405. return NULL;
  406. }
  407. /**
  408. * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
  409. * @ops: struct rtnl_af_ops * to register
  410. *
  411. * Returns 0 on success or a negative error code.
  412. */
  413. void rtnl_af_register(struct rtnl_af_ops *ops)
  414. {
  415. rtnl_lock();
  416. list_add_tail(&ops->list, &rtnl_af_ops);
  417. rtnl_unlock();
  418. }
  419. EXPORT_SYMBOL_GPL(rtnl_af_register);
  420. /**
  421. * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
  422. * @ops: struct rtnl_af_ops * to unregister
  423. *
  424. * The caller must hold the rtnl_mutex.
  425. */
  426. void __rtnl_af_unregister(struct rtnl_af_ops *ops)
  427. {
  428. list_del(&ops->list);
  429. }
  430. EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
  431. /**
  432. * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
  433. * @ops: struct rtnl_af_ops * to unregister
  434. */
  435. void rtnl_af_unregister(struct rtnl_af_ops *ops)
  436. {
  437. rtnl_lock();
  438. __rtnl_af_unregister(ops);
  439. rtnl_unlock();
  440. }
  441. EXPORT_SYMBOL_GPL(rtnl_af_unregister);
  442. static size_t rtnl_link_get_af_size(const struct net_device *dev,
  443. u32 ext_filter_mask)
  444. {
  445. struct rtnl_af_ops *af_ops;
  446. size_t size;
  447. /* IFLA_AF_SPEC */
  448. size = nla_total_size(sizeof(struct nlattr));
  449. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  450. if (af_ops->get_link_af_size) {
  451. /* AF_* + nested data */
  452. size += nla_total_size(sizeof(struct nlattr)) +
  453. af_ops->get_link_af_size(dev, ext_filter_mask);
  454. }
  455. }
  456. return size;
  457. }
  458. static bool rtnl_have_link_slave_info(const struct net_device *dev)
  459. {
  460. struct net_device *master_dev;
  461. master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
  462. if (master_dev && master_dev->rtnl_link_ops)
  463. return true;
  464. return false;
  465. }
  466. static int rtnl_link_slave_info_fill(struct sk_buff *skb,
  467. const struct net_device *dev)
  468. {
  469. struct net_device *master_dev;
  470. const struct rtnl_link_ops *ops;
  471. struct nlattr *slave_data;
  472. int err;
  473. master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
  474. if (!master_dev)
  475. return 0;
  476. ops = master_dev->rtnl_link_ops;
  477. if (!ops)
  478. return 0;
  479. if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
  480. return -EMSGSIZE;
  481. if (ops->fill_slave_info) {
  482. slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
  483. if (!slave_data)
  484. return -EMSGSIZE;
  485. err = ops->fill_slave_info(skb, master_dev, dev);
  486. if (err < 0)
  487. goto err_cancel_slave_data;
  488. nla_nest_end(skb, slave_data);
  489. }
  490. return 0;
  491. err_cancel_slave_data:
  492. nla_nest_cancel(skb, slave_data);
  493. return err;
  494. }
  495. static int rtnl_link_info_fill(struct sk_buff *skb,
  496. const struct net_device *dev)
  497. {
  498. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  499. struct nlattr *data;
  500. int err;
  501. if (!ops)
  502. return 0;
  503. if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
  504. return -EMSGSIZE;
  505. if (ops->fill_xstats) {
  506. err = ops->fill_xstats(skb, dev);
  507. if (err < 0)
  508. return err;
  509. }
  510. if (ops->fill_info) {
  511. data = nla_nest_start(skb, IFLA_INFO_DATA);
  512. if (data == NULL)
  513. return -EMSGSIZE;
  514. err = ops->fill_info(skb, dev);
  515. if (err < 0)
  516. goto err_cancel_data;
  517. nla_nest_end(skb, data);
  518. }
  519. return 0;
  520. err_cancel_data:
  521. nla_nest_cancel(skb, data);
  522. return err;
  523. }
  524. static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
  525. {
  526. struct nlattr *linkinfo;
  527. int err = -EMSGSIZE;
  528. linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
  529. if (linkinfo == NULL)
  530. goto out;
  531. err = rtnl_link_info_fill(skb, dev);
  532. if (err < 0)
  533. goto err_cancel_link;
  534. err = rtnl_link_slave_info_fill(skb, dev);
  535. if (err < 0)
  536. goto err_cancel_link;
  537. nla_nest_end(skb, linkinfo);
  538. return 0;
  539. err_cancel_link:
  540. nla_nest_cancel(skb, linkinfo);
  541. out:
  542. return err;
  543. }
  544. int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
  545. {
  546. struct sock *rtnl = net->rtnl;
  547. int err = 0;
  548. NETLINK_CB(skb).dst_group = group;
  549. if (echo)
  550. atomic_inc(&skb->users);
  551. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  552. if (echo)
  553. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  554. return err;
  555. }
  556. int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
  557. {
  558. struct sock *rtnl = net->rtnl;
  559. return nlmsg_unicast(rtnl, skb, pid);
  560. }
  561. EXPORT_SYMBOL(rtnl_unicast);
  562. void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
  563. struct nlmsghdr *nlh, gfp_t flags)
  564. {
  565. struct sock *rtnl = net->rtnl;
  566. int report = 0;
  567. if (nlh)
  568. report = nlmsg_report(nlh);
  569. nlmsg_notify(rtnl, skb, pid, group, report, flags);
  570. }
  571. EXPORT_SYMBOL(rtnl_notify);
  572. void rtnl_set_sk_err(struct net *net, u32 group, int error)
  573. {
  574. struct sock *rtnl = net->rtnl;
  575. netlink_set_err(rtnl, 0, group, error);
  576. }
  577. EXPORT_SYMBOL(rtnl_set_sk_err);
  578. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  579. {
  580. struct nlattr *mx;
  581. int i, valid = 0;
  582. mx = nla_nest_start(skb, RTA_METRICS);
  583. if (mx == NULL)
  584. return -ENOBUFS;
  585. for (i = 0; i < RTAX_MAX; i++) {
  586. if (metrics[i]) {
  587. if (i == RTAX_CC_ALGO - 1) {
  588. char tmp[TCP_CA_NAME_MAX], *name;
  589. name = tcp_ca_get_name_by_key(metrics[i], tmp);
  590. if (!name)
  591. continue;
  592. if (nla_put_string(skb, i + 1, name))
  593. goto nla_put_failure;
  594. } else if (i == RTAX_FEATURES - 1) {
  595. u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
  596. if (!user_features)
  597. continue;
  598. BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
  599. if (nla_put_u32(skb, i + 1, user_features))
  600. goto nla_put_failure;
  601. } else {
  602. if (nla_put_u32(skb, i + 1, metrics[i]))
  603. goto nla_put_failure;
  604. }
  605. valid++;
  606. }
  607. }
  608. if (!valid) {
  609. nla_nest_cancel(skb, mx);
  610. return 0;
  611. }
  612. return nla_nest_end(skb, mx);
  613. nla_put_failure:
  614. nla_nest_cancel(skb, mx);
  615. return -EMSGSIZE;
  616. }
  617. EXPORT_SYMBOL(rtnetlink_put_metrics);
  618. int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
  619. long expires, u32 error)
  620. {
  621. struct rta_cacheinfo ci = {
  622. .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
  623. .rta_used = dst->__use,
  624. .rta_clntref = atomic_read(&(dst->__refcnt)),
  625. .rta_error = error,
  626. .rta_id = id,
  627. };
  628. if (expires) {
  629. unsigned long clock;
  630. clock = jiffies_to_clock_t(abs(expires));
  631. clock = min_t(unsigned long, clock, INT_MAX);
  632. ci.rta_expires = (expires > 0) ? clock : -clock;
  633. }
  634. return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  635. }
  636. EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
  637. static void set_operstate(struct net_device *dev, unsigned char transition)
  638. {
  639. unsigned char operstate = dev->operstate;
  640. switch (transition) {
  641. case IF_OPER_UP:
  642. if ((operstate == IF_OPER_DORMANT ||
  643. operstate == IF_OPER_UNKNOWN) &&
  644. !netif_dormant(dev))
  645. operstate = IF_OPER_UP;
  646. break;
  647. case IF_OPER_DORMANT:
  648. if (operstate == IF_OPER_UP ||
  649. operstate == IF_OPER_UNKNOWN)
  650. operstate = IF_OPER_DORMANT;
  651. break;
  652. }
  653. if (dev->operstate != operstate) {
  654. write_lock_bh(&dev_base_lock);
  655. dev->operstate = operstate;
  656. write_unlock_bh(&dev_base_lock);
  657. netdev_state_change(dev);
  658. }
  659. }
  660. static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
  661. {
  662. return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
  663. (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
  664. }
  665. static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
  666. const struct ifinfomsg *ifm)
  667. {
  668. unsigned int flags = ifm->ifi_flags;
  669. /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
  670. if (ifm->ifi_change)
  671. flags = (flags & ifm->ifi_change) |
  672. (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
  673. return flags;
  674. }
  675. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  676. const struct rtnl_link_stats64 *b)
  677. {
  678. a->rx_packets = b->rx_packets;
  679. a->tx_packets = b->tx_packets;
  680. a->rx_bytes = b->rx_bytes;
  681. a->tx_bytes = b->tx_bytes;
  682. a->rx_errors = b->rx_errors;
  683. a->tx_errors = b->tx_errors;
  684. a->rx_dropped = b->rx_dropped;
  685. a->tx_dropped = b->tx_dropped;
  686. a->multicast = b->multicast;
  687. a->collisions = b->collisions;
  688. a->rx_length_errors = b->rx_length_errors;
  689. a->rx_over_errors = b->rx_over_errors;
  690. a->rx_crc_errors = b->rx_crc_errors;
  691. a->rx_frame_errors = b->rx_frame_errors;
  692. a->rx_fifo_errors = b->rx_fifo_errors;
  693. a->rx_missed_errors = b->rx_missed_errors;
  694. a->tx_aborted_errors = b->tx_aborted_errors;
  695. a->tx_carrier_errors = b->tx_carrier_errors;
  696. a->tx_fifo_errors = b->tx_fifo_errors;
  697. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  698. a->tx_window_errors = b->tx_window_errors;
  699. a->rx_compressed = b->rx_compressed;
  700. a->tx_compressed = b->tx_compressed;
  701. a->rx_nohandler = b->rx_nohandler;
  702. }
  703. /* All VF info */
  704. static inline int rtnl_vfinfo_size(const struct net_device *dev,
  705. u32 ext_filter_mask)
  706. {
  707. if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
  708. int num_vfs = dev_num_vf(dev->dev.parent);
  709. size_t size = nla_total_size(0);
  710. size += num_vfs *
  711. (nla_total_size(0) +
  712. nla_total_size(sizeof(struct ifla_vf_mac)) +
  713. nla_total_size(sizeof(struct ifla_vf_vlan)) +
  714. nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
  715. nla_total_size(MAX_VLAN_LIST_LEN *
  716. sizeof(struct ifla_vf_vlan_info)) +
  717. nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
  718. nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
  719. nla_total_size(sizeof(struct ifla_vf_rate)) +
  720. nla_total_size(sizeof(struct ifla_vf_link_state)) +
  721. nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
  722. nla_total_size(0) + /* nest IFLA_VF_STATS */
  723. /* IFLA_VF_STATS_RX_PACKETS */
  724. nla_total_size_64bit(sizeof(__u64)) +
  725. /* IFLA_VF_STATS_TX_PACKETS */
  726. nla_total_size_64bit(sizeof(__u64)) +
  727. /* IFLA_VF_STATS_RX_BYTES */
  728. nla_total_size_64bit(sizeof(__u64)) +
  729. /* IFLA_VF_STATS_TX_BYTES */
  730. nla_total_size_64bit(sizeof(__u64)) +
  731. /* IFLA_VF_STATS_BROADCAST */
  732. nla_total_size_64bit(sizeof(__u64)) +
  733. /* IFLA_VF_STATS_MULTICAST */
  734. nla_total_size_64bit(sizeof(__u64)) +
  735. nla_total_size(sizeof(struct ifla_vf_trust)));
  736. return size;
  737. } else
  738. return 0;
  739. }
  740. static size_t rtnl_port_size(const struct net_device *dev,
  741. u32 ext_filter_mask)
  742. {
  743. size_t port_size = nla_total_size(4) /* PORT_VF */
  744. + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
  745. + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
  746. + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
  747. + nla_total_size(1) /* PROT_VDP_REQUEST */
  748. + nla_total_size(2); /* PORT_VDP_RESPONSE */
  749. size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
  750. size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
  751. + port_size;
  752. size_t port_self_size = nla_total_size(sizeof(struct nlattr))
  753. + port_size;
  754. if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
  755. !(ext_filter_mask & RTEXT_FILTER_VF))
  756. return 0;
  757. if (dev_num_vf(dev->dev.parent))
  758. return port_self_size + vf_ports_size +
  759. vf_port_size * dev_num_vf(dev->dev.parent);
  760. else
  761. return port_self_size;
  762. }
  763. static size_t rtnl_xdp_size(const struct net_device *dev)
  764. {
  765. size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
  766. nla_total_size(1); /* XDP_ATTACHED */
  767. if (!dev->netdev_ops->ndo_xdp)
  768. return 0;
  769. else
  770. return xdp_size;
  771. }
  772. static noinline size_t if_nlmsg_size(const struct net_device *dev,
  773. u32 ext_filter_mask)
  774. {
  775. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  776. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  777. + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
  778. + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
  779. + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
  780. + nla_total_size(sizeof(struct rtnl_link_stats))
  781. + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
  782. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  783. + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
  784. + nla_total_size(4) /* IFLA_TXQLEN */
  785. + nla_total_size(4) /* IFLA_WEIGHT */
  786. + nla_total_size(4) /* IFLA_MTU */
  787. + nla_total_size(4) /* IFLA_LINK */
  788. + nla_total_size(4) /* IFLA_MASTER */
  789. + nla_total_size(1) /* IFLA_CARRIER */
  790. + nla_total_size(4) /* IFLA_PROMISCUITY */
  791. + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
  792. + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
  793. + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
  794. + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
  795. + nla_total_size(1) /* IFLA_OPERSTATE */
  796. + nla_total_size(1) /* IFLA_LINKMODE */
  797. + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
  798. + nla_total_size(4) /* IFLA_LINK_NETNSID */
  799. + nla_total_size(ext_filter_mask
  800. & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
  801. + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
  802. + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
  803. + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
  804. + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
  805. + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
  806. + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
  807. + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
  808. + rtnl_xdp_size(dev) /* IFLA_XDP */
  809. + nla_total_size(1); /* IFLA_PROTO_DOWN */
  810. }
  811. static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
  812. {
  813. struct nlattr *vf_ports;
  814. struct nlattr *vf_port;
  815. int vf;
  816. int err;
  817. vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
  818. if (!vf_ports)
  819. return -EMSGSIZE;
  820. for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
  821. vf_port = nla_nest_start(skb, IFLA_VF_PORT);
  822. if (!vf_port)
  823. goto nla_put_failure;
  824. if (nla_put_u32(skb, IFLA_PORT_VF, vf))
  825. goto nla_put_failure;
  826. err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
  827. if (err == -EMSGSIZE)
  828. goto nla_put_failure;
  829. if (err) {
  830. nla_nest_cancel(skb, vf_port);
  831. continue;
  832. }
  833. nla_nest_end(skb, vf_port);
  834. }
  835. nla_nest_end(skb, vf_ports);
  836. return 0;
  837. nla_put_failure:
  838. nla_nest_cancel(skb, vf_ports);
  839. return -EMSGSIZE;
  840. }
  841. static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
  842. {
  843. struct nlattr *port_self;
  844. int err;
  845. port_self = nla_nest_start(skb, IFLA_PORT_SELF);
  846. if (!port_self)
  847. return -EMSGSIZE;
  848. err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
  849. if (err) {
  850. nla_nest_cancel(skb, port_self);
  851. return (err == -EMSGSIZE) ? err : 0;
  852. }
  853. nla_nest_end(skb, port_self);
  854. return 0;
  855. }
  856. static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
  857. u32 ext_filter_mask)
  858. {
  859. int err;
  860. if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
  861. !(ext_filter_mask & RTEXT_FILTER_VF))
  862. return 0;
  863. err = rtnl_port_self_fill(skb, dev);
  864. if (err)
  865. return err;
  866. if (dev_num_vf(dev->dev.parent)) {
  867. err = rtnl_vf_ports_fill(skb, dev);
  868. if (err)
  869. return err;
  870. }
  871. return 0;
  872. }
  873. static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
  874. {
  875. int err;
  876. struct netdev_phys_item_id ppid;
  877. err = dev_get_phys_port_id(dev, &ppid);
  878. if (err) {
  879. if (err == -EOPNOTSUPP)
  880. return 0;
  881. return err;
  882. }
  883. if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
  884. return -EMSGSIZE;
  885. return 0;
  886. }
  887. static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
  888. {
  889. char name[IFNAMSIZ];
  890. int err;
  891. err = dev_get_phys_port_name(dev, name, sizeof(name));
  892. if (err) {
  893. if (err == -EOPNOTSUPP)
  894. return 0;
  895. return err;
  896. }
  897. if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
  898. return -EMSGSIZE;
  899. return 0;
  900. }
  901. static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
  902. {
  903. int err;
  904. struct switchdev_attr attr = {
  905. .orig_dev = dev,
  906. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  907. .flags = SWITCHDEV_F_NO_RECURSE,
  908. };
  909. err = switchdev_port_attr_get(dev, &attr);
  910. if (err) {
  911. if (err == -EOPNOTSUPP)
  912. return 0;
  913. return err;
  914. }
  915. if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
  916. attr.u.ppid.id))
  917. return -EMSGSIZE;
  918. return 0;
  919. }
  920. static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
  921. struct net_device *dev)
  922. {
  923. struct rtnl_link_stats64 *sp;
  924. struct nlattr *attr;
  925. attr = nla_reserve_64bit(skb, IFLA_STATS64,
  926. sizeof(struct rtnl_link_stats64), IFLA_PAD);
  927. if (!attr)
  928. return -EMSGSIZE;
  929. sp = nla_data(attr);
  930. dev_get_stats(dev, sp);
  931. attr = nla_reserve(skb, IFLA_STATS,
  932. sizeof(struct rtnl_link_stats));
  933. if (!attr)
  934. return -EMSGSIZE;
  935. copy_rtnl_link_stats(nla_data(attr), sp);
  936. return 0;
  937. }
  938. static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
  939. struct net_device *dev,
  940. int vfs_num,
  941. struct nlattr *vfinfo)
  942. {
  943. struct ifla_vf_rss_query_en vf_rss_query_en;
  944. struct nlattr *vf, *vfstats, *vfvlanlist;
  945. struct ifla_vf_link_state vf_linkstate;
  946. struct ifla_vf_vlan_info vf_vlan_info;
  947. struct ifla_vf_spoofchk vf_spoofchk;
  948. struct ifla_vf_tx_rate vf_tx_rate;
  949. struct ifla_vf_stats vf_stats;
  950. struct ifla_vf_trust vf_trust;
  951. struct ifla_vf_vlan vf_vlan;
  952. struct ifla_vf_rate vf_rate;
  953. struct ifla_vf_mac vf_mac;
  954. struct ifla_vf_info ivi;
  955. /* Not all SR-IOV capable drivers support the
  956. * spoofcheck and "RSS query enable" query. Preset to
  957. * -1 so the user space tool can detect that the driver
  958. * didn't report anything.
  959. */
  960. ivi.spoofchk = -1;
  961. ivi.rss_query_en = -1;
  962. ivi.trusted = -1;
  963. memset(ivi.mac, 0, sizeof(ivi.mac));
  964. /* The default value for VF link state is "auto"
  965. * IFLA_VF_LINK_STATE_AUTO which equals zero
  966. */
  967. ivi.linkstate = 0;
  968. /* VLAN Protocol by default is 802.1Q */
  969. ivi.vlan_proto = htons(ETH_P_8021Q);
  970. if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
  971. return 0;
  972. memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
  973. vf_mac.vf =
  974. vf_vlan.vf =
  975. vf_vlan_info.vf =
  976. vf_rate.vf =
  977. vf_tx_rate.vf =
  978. vf_spoofchk.vf =
  979. vf_linkstate.vf =
  980. vf_rss_query_en.vf =
  981. vf_trust.vf = ivi.vf;
  982. memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
  983. vf_vlan.vlan = ivi.vlan;
  984. vf_vlan.qos = ivi.qos;
  985. vf_vlan_info.vlan = ivi.vlan;
  986. vf_vlan_info.qos = ivi.qos;
  987. vf_vlan_info.vlan_proto = ivi.vlan_proto;
  988. vf_tx_rate.rate = ivi.max_tx_rate;
  989. vf_rate.min_tx_rate = ivi.min_tx_rate;
  990. vf_rate.max_tx_rate = ivi.max_tx_rate;
  991. vf_spoofchk.setting = ivi.spoofchk;
  992. vf_linkstate.link_state = ivi.linkstate;
  993. vf_rss_query_en.setting = ivi.rss_query_en;
  994. vf_trust.setting = ivi.trusted;
  995. vf = nla_nest_start(skb, IFLA_VF_INFO);
  996. if (!vf)
  997. goto nla_put_vfinfo_failure;
  998. if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
  999. nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
  1000. nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
  1001. &vf_rate) ||
  1002. nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
  1003. &vf_tx_rate) ||
  1004. nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
  1005. &vf_spoofchk) ||
  1006. nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
  1007. &vf_linkstate) ||
  1008. nla_put(skb, IFLA_VF_RSS_QUERY_EN,
  1009. sizeof(vf_rss_query_en),
  1010. &vf_rss_query_en) ||
  1011. nla_put(skb, IFLA_VF_TRUST,
  1012. sizeof(vf_trust), &vf_trust))
  1013. goto nla_put_vf_failure;
  1014. vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
  1015. if (!vfvlanlist)
  1016. goto nla_put_vf_failure;
  1017. if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
  1018. &vf_vlan_info)) {
  1019. nla_nest_cancel(skb, vfvlanlist);
  1020. goto nla_put_vf_failure;
  1021. }
  1022. nla_nest_end(skb, vfvlanlist);
  1023. memset(&vf_stats, 0, sizeof(vf_stats));
  1024. if (dev->netdev_ops->ndo_get_vf_stats)
  1025. dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
  1026. &vf_stats);
  1027. vfstats = nla_nest_start(skb, IFLA_VF_STATS);
  1028. if (!vfstats)
  1029. goto nla_put_vf_failure;
  1030. if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
  1031. vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
  1032. nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
  1033. vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
  1034. nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
  1035. vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
  1036. nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
  1037. vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
  1038. nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
  1039. vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
  1040. nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
  1041. vf_stats.multicast, IFLA_VF_STATS_PAD)) {
  1042. nla_nest_cancel(skb, vfstats);
  1043. goto nla_put_vf_failure;
  1044. }
  1045. nla_nest_end(skb, vfstats);
  1046. nla_nest_end(skb, vf);
  1047. return 0;
  1048. nla_put_vf_failure:
  1049. nla_nest_cancel(skb, vf);
  1050. nla_put_vfinfo_failure:
  1051. nla_nest_cancel(skb, vfinfo);
  1052. return -EMSGSIZE;
  1053. }
  1054. static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
  1055. {
  1056. struct rtnl_link_ifmap map;
  1057. memset(&map, 0, sizeof(map));
  1058. map.mem_start = dev->mem_start;
  1059. map.mem_end = dev->mem_end;
  1060. map.base_addr = dev->base_addr;
  1061. map.irq = dev->irq;
  1062. map.dma = dev->dma;
  1063. map.port = dev->if_port;
  1064. if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
  1065. return -EMSGSIZE;
  1066. return 0;
  1067. }
  1068. static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
  1069. {
  1070. struct netdev_xdp xdp_op = {};
  1071. struct nlattr *xdp;
  1072. int err;
  1073. if (!dev->netdev_ops->ndo_xdp)
  1074. return 0;
  1075. xdp = nla_nest_start(skb, IFLA_XDP);
  1076. if (!xdp)
  1077. return -EMSGSIZE;
  1078. xdp_op.command = XDP_QUERY_PROG;
  1079. err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
  1080. if (err)
  1081. goto err_cancel;
  1082. err = nla_put_u8(skb, IFLA_XDP_ATTACHED, xdp_op.prog_attached);
  1083. if (err)
  1084. goto err_cancel;
  1085. nla_nest_end(skb, xdp);
  1086. return 0;
  1087. err_cancel:
  1088. nla_nest_cancel(skb, xdp);
  1089. return err;
  1090. }
  1091. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  1092. int type, u32 pid, u32 seq, u32 change,
  1093. unsigned int flags, u32 ext_filter_mask)
  1094. {
  1095. struct ifinfomsg *ifm;
  1096. struct nlmsghdr *nlh;
  1097. struct nlattr *af_spec;
  1098. struct rtnl_af_ops *af_ops;
  1099. struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
  1100. ASSERT_RTNL();
  1101. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  1102. if (nlh == NULL)
  1103. return -EMSGSIZE;
  1104. ifm = nlmsg_data(nlh);
  1105. ifm->ifi_family = AF_UNSPEC;
  1106. ifm->__ifi_pad = 0;
  1107. ifm->ifi_type = dev->type;
  1108. ifm->ifi_index = dev->ifindex;
  1109. ifm->ifi_flags = dev_get_flags(dev);
  1110. ifm->ifi_change = change;
  1111. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  1112. nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
  1113. nla_put_u8(skb, IFLA_OPERSTATE,
  1114. netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
  1115. nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
  1116. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  1117. nla_put_u32(skb, IFLA_GROUP, dev->group) ||
  1118. nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
  1119. nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
  1120. nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
  1121. nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
  1122. #ifdef CONFIG_RPS
  1123. nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
  1124. #endif
  1125. (dev->ifindex != dev_get_iflink(dev) &&
  1126. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
  1127. (upper_dev &&
  1128. nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
  1129. nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
  1130. (dev->qdisc &&
  1131. nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
  1132. (dev->ifalias &&
  1133. nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
  1134. nla_put_u32(skb, IFLA_CARRIER_CHANGES,
  1135. atomic_read(&dev->carrier_changes)) ||
  1136. nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
  1137. goto nla_put_failure;
  1138. if (rtnl_fill_link_ifmap(skb, dev))
  1139. goto nla_put_failure;
  1140. if (dev->addr_len) {
  1141. if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
  1142. nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
  1143. goto nla_put_failure;
  1144. }
  1145. if (rtnl_phys_port_id_fill(skb, dev))
  1146. goto nla_put_failure;
  1147. if (rtnl_phys_port_name_fill(skb, dev))
  1148. goto nla_put_failure;
  1149. if (rtnl_phys_switch_id_fill(skb, dev))
  1150. goto nla_put_failure;
  1151. if (rtnl_fill_stats(skb, dev))
  1152. goto nla_put_failure;
  1153. if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
  1154. nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
  1155. goto nla_put_failure;
  1156. if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
  1157. ext_filter_mask & RTEXT_FILTER_VF) {
  1158. int i;
  1159. struct nlattr *vfinfo;
  1160. int num_vfs = dev_num_vf(dev->dev.parent);
  1161. vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
  1162. if (!vfinfo)
  1163. goto nla_put_failure;
  1164. for (i = 0; i < num_vfs; i++) {
  1165. if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
  1166. goto nla_put_failure;
  1167. }
  1168. nla_nest_end(skb, vfinfo);
  1169. }
  1170. if (rtnl_port_fill(skb, dev, ext_filter_mask))
  1171. goto nla_put_failure;
  1172. if (rtnl_xdp_fill(skb, dev))
  1173. goto nla_put_failure;
  1174. if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
  1175. if (rtnl_link_fill(skb, dev) < 0)
  1176. goto nla_put_failure;
  1177. }
  1178. if (dev->rtnl_link_ops &&
  1179. dev->rtnl_link_ops->get_link_net) {
  1180. struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
  1181. if (!net_eq(dev_net(dev), link_net)) {
  1182. int id = peernet2id_alloc(dev_net(dev), link_net);
  1183. if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
  1184. goto nla_put_failure;
  1185. }
  1186. }
  1187. if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
  1188. goto nla_put_failure;
  1189. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  1190. if (af_ops->fill_link_af) {
  1191. struct nlattr *af;
  1192. int err;
  1193. if (!(af = nla_nest_start(skb, af_ops->family)))
  1194. goto nla_put_failure;
  1195. err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
  1196. /*
  1197. * Caller may return ENODATA to indicate that there
  1198. * was no data to be dumped. This is not an error, it
  1199. * means we should trim the attribute header and
  1200. * continue.
  1201. */
  1202. if (err == -ENODATA)
  1203. nla_nest_cancel(skb, af);
  1204. else if (err < 0)
  1205. goto nla_put_failure;
  1206. nla_nest_end(skb, af);
  1207. }
  1208. }
  1209. nla_nest_end(skb, af_spec);
  1210. nlmsg_end(skb, nlh);
  1211. return 0;
  1212. nla_put_failure:
  1213. nlmsg_cancel(skb, nlh);
  1214. return -EMSGSIZE;
  1215. }
  1216. static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
  1217. [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
  1218. [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1219. [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1220. [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
  1221. [IFLA_MTU] = { .type = NLA_U32 },
  1222. [IFLA_LINK] = { .type = NLA_U32 },
  1223. [IFLA_MASTER] = { .type = NLA_U32 },
  1224. [IFLA_CARRIER] = { .type = NLA_U8 },
  1225. [IFLA_TXQLEN] = { .type = NLA_U32 },
  1226. [IFLA_WEIGHT] = { .type = NLA_U32 },
  1227. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  1228. [IFLA_LINKMODE] = { .type = NLA_U8 },
  1229. [IFLA_LINKINFO] = { .type = NLA_NESTED },
  1230. [IFLA_NET_NS_PID] = { .type = NLA_U32 },
  1231. [IFLA_NET_NS_FD] = { .type = NLA_U32 },
  1232. [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
  1233. [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
  1234. [IFLA_VF_PORTS] = { .type = NLA_NESTED },
  1235. [IFLA_PORT_SELF] = { .type = NLA_NESTED },
  1236. [IFLA_AF_SPEC] = { .type = NLA_NESTED },
  1237. [IFLA_EXT_MASK] = { .type = NLA_U32 },
  1238. [IFLA_PROMISCUITY] = { .type = NLA_U32 },
  1239. [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
  1240. [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
  1241. [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
  1242. [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
  1243. [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
  1244. [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
  1245. [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
  1246. [IFLA_XDP] = { .type = NLA_NESTED },
  1247. };
  1248. static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
  1249. [IFLA_INFO_KIND] = { .type = NLA_STRING },
  1250. [IFLA_INFO_DATA] = { .type = NLA_NESTED },
  1251. [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
  1252. [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
  1253. };
  1254. static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
  1255. [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
  1256. [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
  1257. [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
  1258. [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
  1259. [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
  1260. [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
  1261. [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
  1262. [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
  1263. [IFLA_VF_STATS] = { .type = NLA_NESTED },
  1264. [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
  1265. [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
  1266. [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
  1267. };
  1268. static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
  1269. [IFLA_PORT_VF] = { .type = NLA_U32 },
  1270. [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
  1271. .len = PORT_PROFILE_MAX },
  1272. [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
  1273. .len = PORT_UUID_MAX },
  1274. [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
  1275. .len = PORT_UUID_MAX },
  1276. [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
  1277. [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
  1278. /* Unused, but we need to keep it here since user space could
  1279. * fill it. It's also broken with regard to NLA_BINARY use in
  1280. * combination with structs.
  1281. */
  1282. [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
  1283. .len = sizeof(struct ifla_port_vsi) },
  1284. };
  1285. static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
  1286. [IFLA_XDP_FD] = { .type = NLA_S32 },
  1287. [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
  1288. [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
  1289. };
  1290. static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
  1291. {
  1292. const struct rtnl_link_ops *ops = NULL;
  1293. struct nlattr *linfo[IFLA_INFO_MAX + 1];
  1294. if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
  1295. return NULL;
  1296. if (linfo[IFLA_INFO_KIND]) {
  1297. char kind[MODULE_NAME_LEN];
  1298. nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
  1299. ops = rtnl_link_ops_get(kind);
  1300. }
  1301. return ops;
  1302. }
  1303. static bool link_master_filtered(struct net_device *dev, int master_idx)
  1304. {
  1305. struct net_device *master;
  1306. if (!master_idx)
  1307. return false;
  1308. master = netdev_master_upper_dev_get(dev);
  1309. if (!master || master->ifindex != master_idx)
  1310. return true;
  1311. return false;
  1312. }
  1313. static bool link_kind_filtered(const struct net_device *dev,
  1314. const struct rtnl_link_ops *kind_ops)
  1315. {
  1316. if (kind_ops && dev->rtnl_link_ops != kind_ops)
  1317. return true;
  1318. return false;
  1319. }
  1320. static bool link_dump_filtered(struct net_device *dev,
  1321. int master_idx,
  1322. const struct rtnl_link_ops *kind_ops)
  1323. {
  1324. if (link_master_filtered(dev, master_idx) ||
  1325. link_kind_filtered(dev, kind_ops))
  1326. return true;
  1327. return false;
  1328. }
  1329. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  1330. {
  1331. struct net *net = sock_net(skb->sk);
  1332. int h, s_h;
  1333. int idx = 0, s_idx;
  1334. struct net_device *dev;
  1335. struct hlist_head *head;
  1336. struct nlattr *tb[IFLA_MAX+1];
  1337. u32 ext_filter_mask = 0;
  1338. const struct rtnl_link_ops *kind_ops = NULL;
  1339. unsigned int flags = NLM_F_MULTI;
  1340. int master_idx = 0;
  1341. int err;
  1342. int hdrlen;
  1343. s_h = cb->args[0];
  1344. s_idx = cb->args[1];
  1345. cb->seq = net->dev_base_seq;
  1346. /* A hack to preserve kernel<->userspace interface.
  1347. * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
  1348. * However, before Linux v3.9 the code here assumed rtgenmsg and that's
  1349. * what iproute2 < v3.9.0 used.
  1350. * We can detect the old iproute2. Even including the IFLA_EXT_MASK
  1351. * attribute, its netlink message is shorter than struct ifinfomsg.
  1352. */
  1353. hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
  1354. sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
  1355. if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
  1356. if (tb[IFLA_EXT_MASK])
  1357. ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
  1358. if (tb[IFLA_MASTER])
  1359. master_idx = nla_get_u32(tb[IFLA_MASTER]);
  1360. if (tb[IFLA_LINKINFO])
  1361. kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
  1362. if (master_idx || kind_ops)
  1363. flags |= NLM_F_DUMP_FILTERED;
  1364. }
  1365. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1366. idx = 0;
  1367. head = &net->dev_index_head[h];
  1368. hlist_for_each_entry(dev, head, index_hlist) {
  1369. if (link_dump_filtered(dev, master_idx, kind_ops))
  1370. goto cont;
  1371. if (idx < s_idx)
  1372. goto cont;
  1373. err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
  1374. NETLINK_CB(cb->skb).portid,
  1375. cb->nlh->nlmsg_seq, 0,
  1376. flags,
  1377. ext_filter_mask);
  1378. /* If we ran out of room on the first message,
  1379. * we're in trouble
  1380. */
  1381. WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
  1382. if (err < 0)
  1383. goto out;
  1384. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1385. cont:
  1386. idx++;
  1387. }
  1388. }
  1389. out:
  1390. cb->args[1] = idx;
  1391. cb->args[0] = h;
  1392. return skb->len;
  1393. }
  1394. int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
  1395. {
  1396. return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
  1397. }
  1398. EXPORT_SYMBOL(rtnl_nla_parse_ifla);
  1399. struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
  1400. {
  1401. struct net *net;
  1402. /* Examine the link attributes and figure out which
  1403. * network namespace we are talking about.
  1404. */
  1405. if (tb[IFLA_NET_NS_PID])
  1406. net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
  1407. else if (tb[IFLA_NET_NS_FD])
  1408. net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
  1409. else
  1410. net = get_net(src_net);
  1411. return net;
  1412. }
  1413. EXPORT_SYMBOL(rtnl_link_get_net);
  1414. static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
  1415. {
  1416. if (dev) {
  1417. if (tb[IFLA_ADDRESS] &&
  1418. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  1419. return -EINVAL;
  1420. if (tb[IFLA_BROADCAST] &&
  1421. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  1422. return -EINVAL;
  1423. }
  1424. if (tb[IFLA_AF_SPEC]) {
  1425. struct nlattr *af;
  1426. int rem, err;
  1427. nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
  1428. const struct rtnl_af_ops *af_ops;
  1429. if (!(af_ops = rtnl_af_lookup(nla_type(af))))
  1430. return -EAFNOSUPPORT;
  1431. if (!af_ops->set_link_af)
  1432. return -EOPNOTSUPP;
  1433. if (af_ops->validate_link_af) {
  1434. err = af_ops->validate_link_af(dev, af);
  1435. if (err < 0)
  1436. return err;
  1437. }
  1438. }
  1439. }
  1440. return 0;
  1441. }
  1442. static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
  1443. int guid_type)
  1444. {
  1445. const struct net_device_ops *ops = dev->netdev_ops;
  1446. return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
  1447. }
  1448. static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
  1449. {
  1450. if (dev->type != ARPHRD_INFINIBAND)
  1451. return -EOPNOTSUPP;
  1452. return handle_infiniband_guid(dev, ivt, guid_type);
  1453. }
  1454. static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
  1455. {
  1456. const struct net_device_ops *ops = dev->netdev_ops;
  1457. int err = -EINVAL;
  1458. if (tb[IFLA_VF_MAC]) {
  1459. struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
  1460. err = -EOPNOTSUPP;
  1461. if (ops->ndo_set_vf_mac)
  1462. err = ops->ndo_set_vf_mac(dev, ivm->vf,
  1463. ivm->mac);
  1464. if (err < 0)
  1465. return err;
  1466. }
  1467. if (tb[IFLA_VF_VLAN]) {
  1468. struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
  1469. err = -EOPNOTSUPP;
  1470. if (ops->ndo_set_vf_vlan)
  1471. err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
  1472. ivv->qos,
  1473. htons(ETH_P_8021Q));
  1474. if (err < 0)
  1475. return err;
  1476. }
  1477. if (tb[IFLA_VF_VLAN_LIST]) {
  1478. struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
  1479. struct nlattr *attr;
  1480. int rem, len = 0;
  1481. err = -EOPNOTSUPP;
  1482. if (!ops->ndo_set_vf_vlan)
  1483. return err;
  1484. nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
  1485. if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
  1486. nla_len(attr) < NLA_HDRLEN) {
  1487. return -EINVAL;
  1488. }
  1489. if (len >= MAX_VLAN_LIST_LEN)
  1490. return -EOPNOTSUPP;
  1491. ivvl[len] = nla_data(attr);
  1492. len++;
  1493. }
  1494. if (len == 0)
  1495. return -EINVAL;
  1496. err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
  1497. ivvl[0]->qos, ivvl[0]->vlan_proto);
  1498. if (err < 0)
  1499. return err;
  1500. }
  1501. if (tb[IFLA_VF_TX_RATE]) {
  1502. struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
  1503. struct ifla_vf_info ivf;
  1504. err = -EOPNOTSUPP;
  1505. if (ops->ndo_get_vf_config)
  1506. err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
  1507. if (err < 0)
  1508. return err;
  1509. err = -EOPNOTSUPP;
  1510. if (ops->ndo_set_vf_rate)
  1511. err = ops->ndo_set_vf_rate(dev, ivt->vf,
  1512. ivf.min_tx_rate,
  1513. ivt->rate);
  1514. if (err < 0)
  1515. return err;
  1516. }
  1517. if (tb[IFLA_VF_RATE]) {
  1518. struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
  1519. err = -EOPNOTSUPP;
  1520. if (ops->ndo_set_vf_rate)
  1521. err = ops->ndo_set_vf_rate(dev, ivt->vf,
  1522. ivt->min_tx_rate,
  1523. ivt->max_tx_rate);
  1524. if (err < 0)
  1525. return err;
  1526. }
  1527. if (tb[IFLA_VF_SPOOFCHK]) {
  1528. struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
  1529. err = -EOPNOTSUPP;
  1530. if (ops->ndo_set_vf_spoofchk)
  1531. err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
  1532. ivs->setting);
  1533. if (err < 0)
  1534. return err;
  1535. }
  1536. if (tb[IFLA_VF_LINK_STATE]) {
  1537. struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
  1538. err = -EOPNOTSUPP;
  1539. if (ops->ndo_set_vf_link_state)
  1540. err = ops->ndo_set_vf_link_state(dev, ivl->vf,
  1541. ivl->link_state);
  1542. if (err < 0)
  1543. return err;
  1544. }
  1545. if (tb[IFLA_VF_RSS_QUERY_EN]) {
  1546. struct ifla_vf_rss_query_en *ivrssq_en;
  1547. err = -EOPNOTSUPP;
  1548. ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
  1549. if (ops->ndo_set_vf_rss_query_en)
  1550. err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
  1551. ivrssq_en->setting);
  1552. if (err < 0)
  1553. return err;
  1554. }
  1555. if (tb[IFLA_VF_TRUST]) {
  1556. struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
  1557. err = -EOPNOTSUPP;
  1558. if (ops->ndo_set_vf_trust)
  1559. err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
  1560. if (err < 0)
  1561. return err;
  1562. }
  1563. if (tb[IFLA_VF_IB_NODE_GUID]) {
  1564. struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
  1565. if (!ops->ndo_set_vf_guid)
  1566. return -EOPNOTSUPP;
  1567. return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
  1568. }
  1569. if (tb[IFLA_VF_IB_PORT_GUID]) {
  1570. struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
  1571. if (!ops->ndo_set_vf_guid)
  1572. return -EOPNOTSUPP;
  1573. return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
  1574. }
  1575. return err;
  1576. }
  1577. static int do_set_master(struct net_device *dev, int ifindex)
  1578. {
  1579. struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
  1580. const struct net_device_ops *ops;
  1581. int err;
  1582. if (upper_dev) {
  1583. if (upper_dev->ifindex == ifindex)
  1584. return 0;
  1585. ops = upper_dev->netdev_ops;
  1586. if (ops->ndo_del_slave) {
  1587. err = ops->ndo_del_slave(upper_dev, dev);
  1588. if (err)
  1589. return err;
  1590. } else {
  1591. return -EOPNOTSUPP;
  1592. }
  1593. }
  1594. if (ifindex) {
  1595. upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
  1596. if (!upper_dev)
  1597. return -EINVAL;
  1598. ops = upper_dev->netdev_ops;
  1599. if (ops->ndo_add_slave) {
  1600. err = ops->ndo_add_slave(upper_dev, dev);
  1601. if (err)
  1602. return err;
  1603. } else {
  1604. return -EOPNOTSUPP;
  1605. }
  1606. }
  1607. return 0;
  1608. }
  1609. #define DO_SETLINK_MODIFIED 0x01
  1610. /* notify flag means notify + modified. */
  1611. #define DO_SETLINK_NOTIFY 0x03
  1612. static int do_setlink(const struct sk_buff *skb,
  1613. struct net_device *dev, struct ifinfomsg *ifm,
  1614. struct nlattr **tb, char *ifname, int status)
  1615. {
  1616. const struct net_device_ops *ops = dev->netdev_ops;
  1617. int err;
  1618. if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
  1619. struct net *net = rtnl_link_get_net(dev_net(dev), tb);
  1620. if (IS_ERR(net)) {
  1621. err = PTR_ERR(net);
  1622. goto errout;
  1623. }
  1624. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
  1625. put_net(net);
  1626. err = -EPERM;
  1627. goto errout;
  1628. }
  1629. err = dev_change_net_namespace(dev, net, ifname);
  1630. put_net(net);
  1631. if (err)
  1632. goto errout;
  1633. status |= DO_SETLINK_MODIFIED;
  1634. }
  1635. if (tb[IFLA_MAP]) {
  1636. struct rtnl_link_ifmap *u_map;
  1637. struct ifmap k_map;
  1638. if (!ops->ndo_set_config) {
  1639. err = -EOPNOTSUPP;
  1640. goto errout;
  1641. }
  1642. if (!netif_device_present(dev)) {
  1643. err = -ENODEV;
  1644. goto errout;
  1645. }
  1646. u_map = nla_data(tb[IFLA_MAP]);
  1647. k_map.mem_start = (unsigned long) u_map->mem_start;
  1648. k_map.mem_end = (unsigned long) u_map->mem_end;
  1649. k_map.base_addr = (unsigned short) u_map->base_addr;
  1650. k_map.irq = (unsigned char) u_map->irq;
  1651. k_map.dma = (unsigned char) u_map->dma;
  1652. k_map.port = (unsigned char) u_map->port;
  1653. err = ops->ndo_set_config(dev, &k_map);
  1654. if (err < 0)
  1655. goto errout;
  1656. status |= DO_SETLINK_NOTIFY;
  1657. }
  1658. if (tb[IFLA_ADDRESS]) {
  1659. struct sockaddr *sa;
  1660. int len;
  1661. len = sizeof(sa_family_t) + dev->addr_len;
  1662. sa = kmalloc(len, GFP_KERNEL);
  1663. if (!sa) {
  1664. err = -ENOMEM;
  1665. goto errout;
  1666. }
  1667. sa->sa_family = dev->type;
  1668. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  1669. dev->addr_len);
  1670. err = dev_set_mac_address(dev, sa);
  1671. kfree(sa);
  1672. if (err)
  1673. goto errout;
  1674. status |= DO_SETLINK_MODIFIED;
  1675. }
  1676. if (tb[IFLA_MTU]) {
  1677. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1678. if (err < 0)
  1679. goto errout;
  1680. status |= DO_SETLINK_MODIFIED;
  1681. }
  1682. if (tb[IFLA_GROUP]) {
  1683. dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
  1684. status |= DO_SETLINK_NOTIFY;
  1685. }
  1686. /*
  1687. * Interface selected by interface index but interface
  1688. * name provided implies that a name change has been
  1689. * requested.
  1690. */
  1691. if (ifm->ifi_index > 0 && ifname[0]) {
  1692. err = dev_change_name(dev, ifname);
  1693. if (err < 0)
  1694. goto errout;
  1695. status |= DO_SETLINK_MODIFIED;
  1696. }
  1697. if (tb[IFLA_IFALIAS]) {
  1698. err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
  1699. nla_len(tb[IFLA_IFALIAS]));
  1700. if (err < 0)
  1701. goto errout;
  1702. status |= DO_SETLINK_NOTIFY;
  1703. }
  1704. if (tb[IFLA_BROADCAST]) {
  1705. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  1706. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  1707. }
  1708. if (ifm->ifi_flags || ifm->ifi_change) {
  1709. err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
  1710. if (err < 0)
  1711. goto errout;
  1712. }
  1713. if (tb[IFLA_MASTER]) {
  1714. err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
  1715. if (err)
  1716. goto errout;
  1717. status |= DO_SETLINK_MODIFIED;
  1718. }
  1719. if (tb[IFLA_CARRIER]) {
  1720. err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
  1721. if (err)
  1722. goto errout;
  1723. status |= DO_SETLINK_MODIFIED;
  1724. }
  1725. if (tb[IFLA_TXQLEN]) {
  1726. unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
  1727. unsigned long orig_len = dev->tx_queue_len;
  1728. if (dev->tx_queue_len ^ value) {
  1729. dev->tx_queue_len = value;
  1730. err = call_netdevice_notifiers(
  1731. NETDEV_CHANGE_TX_QUEUE_LEN, dev);
  1732. err = notifier_to_errno(err);
  1733. if (err) {
  1734. dev->tx_queue_len = orig_len;
  1735. goto errout;
  1736. }
  1737. status |= DO_SETLINK_NOTIFY;
  1738. }
  1739. }
  1740. if (tb[IFLA_OPERSTATE])
  1741. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  1742. if (tb[IFLA_LINKMODE]) {
  1743. unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
  1744. write_lock_bh(&dev_base_lock);
  1745. if (dev->link_mode ^ value)
  1746. status |= DO_SETLINK_NOTIFY;
  1747. dev->link_mode = value;
  1748. write_unlock_bh(&dev_base_lock);
  1749. }
  1750. if (tb[IFLA_VFINFO_LIST]) {
  1751. struct nlattr *vfinfo[IFLA_VF_MAX + 1];
  1752. struct nlattr *attr;
  1753. int rem;
  1754. nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
  1755. if (nla_type(attr) != IFLA_VF_INFO ||
  1756. nla_len(attr) < NLA_HDRLEN) {
  1757. err = -EINVAL;
  1758. goto errout;
  1759. }
  1760. err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
  1761. ifla_vf_policy);
  1762. if (err < 0)
  1763. goto errout;
  1764. err = do_setvfinfo(dev, vfinfo);
  1765. if (err < 0)
  1766. goto errout;
  1767. status |= DO_SETLINK_NOTIFY;
  1768. }
  1769. }
  1770. err = 0;
  1771. if (tb[IFLA_VF_PORTS]) {
  1772. struct nlattr *port[IFLA_PORT_MAX+1];
  1773. struct nlattr *attr;
  1774. int vf;
  1775. int rem;
  1776. err = -EOPNOTSUPP;
  1777. if (!ops->ndo_set_vf_port)
  1778. goto errout;
  1779. nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
  1780. if (nla_type(attr) != IFLA_VF_PORT ||
  1781. nla_len(attr) < NLA_HDRLEN) {
  1782. err = -EINVAL;
  1783. goto errout;
  1784. }
  1785. err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
  1786. ifla_port_policy);
  1787. if (err < 0)
  1788. goto errout;
  1789. if (!port[IFLA_PORT_VF]) {
  1790. err = -EOPNOTSUPP;
  1791. goto errout;
  1792. }
  1793. vf = nla_get_u32(port[IFLA_PORT_VF]);
  1794. err = ops->ndo_set_vf_port(dev, vf, port);
  1795. if (err < 0)
  1796. goto errout;
  1797. status |= DO_SETLINK_NOTIFY;
  1798. }
  1799. }
  1800. err = 0;
  1801. if (tb[IFLA_PORT_SELF]) {
  1802. struct nlattr *port[IFLA_PORT_MAX+1];
  1803. err = nla_parse_nested(port, IFLA_PORT_MAX,
  1804. tb[IFLA_PORT_SELF], ifla_port_policy);
  1805. if (err < 0)
  1806. goto errout;
  1807. err = -EOPNOTSUPP;
  1808. if (ops->ndo_set_vf_port)
  1809. err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
  1810. if (err < 0)
  1811. goto errout;
  1812. status |= DO_SETLINK_NOTIFY;
  1813. }
  1814. if (tb[IFLA_AF_SPEC]) {
  1815. struct nlattr *af;
  1816. int rem;
  1817. nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
  1818. const struct rtnl_af_ops *af_ops;
  1819. if (!(af_ops = rtnl_af_lookup(nla_type(af))))
  1820. BUG();
  1821. err = af_ops->set_link_af(dev, af);
  1822. if (err < 0)
  1823. goto errout;
  1824. status |= DO_SETLINK_NOTIFY;
  1825. }
  1826. }
  1827. err = 0;
  1828. if (tb[IFLA_PROTO_DOWN]) {
  1829. err = dev_change_proto_down(dev,
  1830. nla_get_u8(tb[IFLA_PROTO_DOWN]));
  1831. if (err)
  1832. goto errout;
  1833. status |= DO_SETLINK_NOTIFY;
  1834. }
  1835. if (tb[IFLA_XDP]) {
  1836. struct nlattr *xdp[IFLA_XDP_MAX + 1];
  1837. u32 xdp_flags = 0;
  1838. err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
  1839. ifla_xdp_policy);
  1840. if (err < 0)
  1841. goto errout;
  1842. if (xdp[IFLA_XDP_ATTACHED]) {
  1843. err = -EINVAL;
  1844. goto errout;
  1845. }
  1846. if (xdp[IFLA_XDP_FLAGS]) {
  1847. xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
  1848. if (xdp_flags & ~XDP_FLAGS_MASK) {
  1849. err = -EINVAL;
  1850. goto errout;
  1851. }
  1852. }
  1853. if (xdp[IFLA_XDP_FD]) {
  1854. err = dev_change_xdp_fd(dev,
  1855. nla_get_s32(xdp[IFLA_XDP_FD]),
  1856. xdp_flags);
  1857. if (err)
  1858. goto errout;
  1859. status |= DO_SETLINK_NOTIFY;
  1860. }
  1861. }
  1862. errout:
  1863. if (status & DO_SETLINK_MODIFIED) {
  1864. if (status & DO_SETLINK_NOTIFY)
  1865. netdev_state_change(dev);
  1866. if (err < 0)
  1867. net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
  1868. dev->name);
  1869. }
  1870. return err;
  1871. }
  1872. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
  1873. {
  1874. struct net *net = sock_net(skb->sk);
  1875. struct ifinfomsg *ifm;
  1876. struct net_device *dev;
  1877. int err;
  1878. struct nlattr *tb[IFLA_MAX+1];
  1879. char ifname[IFNAMSIZ];
  1880. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1881. if (err < 0)
  1882. goto errout;
  1883. if (tb[IFLA_IFNAME])
  1884. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1885. else
  1886. ifname[0] = '\0';
  1887. err = -EINVAL;
  1888. ifm = nlmsg_data(nlh);
  1889. if (ifm->ifi_index > 0)
  1890. dev = __dev_get_by_index(net, ifm->ifi_index);
  1891. else if (tb[IFLA_IFNAME])
  1892. dev = __dev_get_by_name(net, ifname);
  1893. else
  1894. goto errout;
  1895. if (dev == NULL) {
  1896. err = -ENODEV;
  1897. goto errout;
  1898. }
  1899. err = validate_linkmsg(dev, tb);
  1900. if (err < 0)
  1901. goto errout;
  1902. err = do_setlink(skb, dev, ifm, tb, ifname, 0);
  1903. errout:
  1904. return err;
  1905. }
  1906. static int rtnl_group_dellink(const struct net *net, int group)
  1907. {
  1908. struct net_device *dev, *aux;
  1909. LIST_HEAD(list_kill);
  1910. bool found = false;
  1911. if (!group)
  1912. return -EPERM;
  1913. for_each_netdev(net, dev) {
  1914. if (dev->group == group) {
  1915. const struct rtnl_link_ops *ops;
  1916. found = true;
  1917. ops = dev->rtnl_link_ops;
  1918. if (!ops || !ops->dellink)
  1919. return -EOPNOTSUPP;
  1920. }
  1921. }
  1922. if (!found)
  1923. return -ENODEV;
  1924. for_each_netdev_safe(net, dev, aux) {
  1925. if (dev->group == group) {
  1926. const struct rtnl_link_ops *ops;
  1927. ops = dev->rtnl_link_ops;
  1928. ops->dellink(dev, &list_kill);
  1929. }
  1930. }
  1931. unregister_netdevice_many(&list_kill);
  1932. return 0;
  1933. }
  1934. int rtnl_delete_link(struct net_device *dev)
  1935. {
  1936. const struct rtnl_link_ops *ops;
  1937. LIST_HEAD(list_kill);
  1938. ops = dev->rtnl_link_ops;
  1939. if (!ops || !ops->dellink)
  1940. return -EOPNOTSUPP;
  1941. ops->dellink(dev, &list_kill);
  1942. unregister_netdevice_many(&list_kill);
  1943. return 0;
  1944. }
  1945. EXPORT_SYMBOL_GPL(rtnl_delete_link);
  1946. static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
  1947. {
  1948. struct net *net = sock_net(skb->sk);
  1949. struct net_device *dev;
  1950. struct ifinfomsg *ifm;
  1951. char ifname[IFNAMSIZ];
  1952. struct nlattr *tb[IFLA_MAX+1];
  1953. int err;
  1954. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1955. if (err < 0)
  1956. return err;
  1957. if (tb[IFLA_IFNAME])
  1958. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1959. ifm = nlmsg_data(nlh);
  1960. if (ifm->ifi_index > 0)
  1961. dev = __dev_get_by_index(net, ifm->ifi_index);
  1962. else if (tb[IFLA_IFNAME])
  1963. dev = __dev_get_by_name(net, ifname);
  1964. else if (tb[IFLA_GROUP])
  1965. return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
  1966. else
  1967. return -EINVAL;
  1968. if (!dev)
  1969. return -ENODEV;
  1970. return rtnl_delete_link(dev);
  1971. }
  1972. int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
  1973. {
  1974. unsigned int old_flags;
  1975. int err;
  1976. old_flags = dev->flags;
  1977. if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
  1978. err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
  1979. if (err < 0)
  1980. return err;
  1981. }
  1982. dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
  1983. __dev_notify_flags(dev, old_flags, ~0U);
  1984. return 0;
  1985. }
  1986. EXPORT_SYMBOL(rtnl_configure_link);
  1987. struct net_device *rtnl_create_link(struct net *net,
  1988. const char *ifname, unsigned char name_assign_type,
  1989. const struct rtnl_link_ops *ops, struct nlattr *tb[])
  1990. {
  1991. struct net_device *dev;
  1992. unsigned int num_tx_queues = 1;
  1993. unsigned int num_rx_queues = 1;
  1994. if (tb[IFLA_NUM_TX_QUEUES])
  1995. num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
  1996. else if (ops->get_num_tx_queues)
  1997. num_tx_queues = ops->get_num_tx_queues();
  1998. if (tb[IFLA_NUM_RX_QUEUES])
  1999. num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
  2000. else if (ops->get_num_rx_queues)
  2001. num_rx_queues = ops->get_num_rx_queues();
  2002. dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
  2003. ops->setup, num_tx_queues, num_rx_queues);
  2004. if (!dev)
  2005. return ERR_PTR(-ENOMEM);
  2006. dev_net_set(dev, net);
  2007. dev->rtnl_link_ops = ops;
  2008. dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
  2009. if (tb[IFLA_MTU])
  2010. dev->mtu = nla_get_u32(tb[IFLA_MTU]);
  2011. if (tb[IFLA_ADDRESS]) {
  2012. memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
  2013. nla_len(tb[IFLA_ADDRESS]));
  2014. dev->addr_assign_type = NET_ADDR_SET;
  2015. }
  2016. if (tb[IFLA_BROADCAST])
  2017. memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
  2018. nla_len(tb[IFLA_BROADCAST]));
  2019. if (tb[IFLA_TXQLEN])
  2020. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  2021. if (tb[IFLA_OPERSTATE])
  2022. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  2023. if (tb[IFLA_LINKMODE])
  2024. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  2025. if (tb[IFLA_GROUP])
  2026. dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
  2027. return dev;
  2028. }
  2029. EXPORT_SYMBOL(rtnl_create_link);
  2030. static int rtnl_group_changelink(const struct sk_buff *skb,
  2031. struct net *net, int group,
  2032. struct ifinfomsg *ifm,
  2033. struct nlattr **tb)
  2034. {
  2035. struct net_device *dev, *aux;
  2036. int err;
  2037. for_each_netdev_safe(net, dev, aux) {
  2038. if (dev->group == group) {
  2039. err = do_setlink(skb, dev, ifm, tb, NULL, 0);
  2040. if (err < 0)
  2041. return err;
  2042. }
  2043. }
  2044. return 0;
  2045. }
  2046. static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
  2047. {
  2048. struct net *net = sock_net(skb->sk);
  2049. const struct rtnl_link_ops *ops;
  2050. const struct rtnl_link_ops *m_ops = NULL;
  2051. struct net_device *dev;
  2052. struct net_device *master_dev = NULL;
  2053. struct ifinfomsg *ifm;
  2054. char kind[MODULE_NAME_LEN];
  2055. char ifname[IFNAMSIZ];
  2056. struct nlattr *tb[IFLA_MAX+1];
  2057. struct nlattr *linkinfo[IFLA_INFO_MAX+1];
  2058. unsigned char name_assign_type = NET_NAME_USER;
  2059. int err;
  2060. #ifdef CONFIG_MODULES
  2061. replay:
  2062. #endif
  2063. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  2064. if (err < 0)
  2065. return err;
  2066. if (tb[IFLA_IFNAME])
  2067. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  2068. else
  2069. ifname[0] = '\0';
  2070. ifm = nlmsg_data(nlh);
  2071. if (ifm->ifi_index > 0)
  2072. dev = __dev_get_by_index(net, ifm->ifi_index);
  2073. else {
  2074. if (ifname[0])
  2075. dev = __dev_get_by_name(net, ifname);
  2076. else
  2077. dev = NULL;
  2078. }
  2079. if (dev) {
  2080. master_dev = netdev_master_upper_dev_get(dev);
  2081. if (master_dev)
  2082. m_ops = master_dev->rtnl_link_ops;
  2083. }
  2084. err = validate_linkmsg(dev, tb);
  2085. if (err < 0)
  2086. return err;
  2087. if (tb[IFLA_LINKINFO]) {
  2088. err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
  2089. tb[IFLA_LINKINFO], ifla_info_policy);
  2090. if (err < 0)
  2091. return err;
  2092. } else
  2093. memset(linkinfo, 0, sizeof(linkinfo));
  2094. if (linkinfo[IFLA_INFO_KIND]) {
  2095. nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
  2096. ops = rtnl_link_ops_get(kind);
  2097. } else {
  2098. kind[0] = '\0';
  2099. ops = NULL;
  2100. }
  2101. if (1) {
  2102. struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
  2103. struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
  2104. struct nlattr **data = NULL;
  2105. struct nlattr **slave_data = NULL;
  2106. struct net *dest_net, *link_net = NULL;
  2107. if (ops) {
  2108. if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
  2109. err = nla_parse_nested(attr, ops->maxtype,
  2110. linkinfo[IFLA_INFO_DATA],
  2111. ops->policy);
  2112. if (err < 0)
  2113. return err;
  2114. data = attr;
  2115. }
  2116. if (ops->validate) {
  2117. err = ops->validate(tb, data);
  2118. if (err < 0)
  2119. return err;
  2120. }
  2121. }
  2122. if (m_ops) {
  2123. if (m_ops->slave_maxtype &&
  2124. linkinfo[IFLA_INFO_SLAVE_DATA]) {
  2125. err = nla_parse_nested(slave_attr,
  2126. m_ops->slave_maxtype,
  2127. linkinfo[IFLA_INFO_SLAVE_DATA],
  2128. m_ops->slave_policy);
  2129. if (err < 0)
  2130. return err;
  2131. slave_data = slave_attr;
  2132. }
  2133. if (m_ops->slave_validate) {
  2134. err = m_ops->slave_validate(tb, slave_data);
  2135. if (err < 0)
  2136. return err;
  2137. }
  2138. }
  2139. if (dev) {
  2140. int status = 0;
  2141. if (nlh->nlmsg_flags & NLM_F_EXCL)
  2142. return -EEXIST;
  2143. if (nlh->nlmsg_flags & NLM_F_REPLACE)
  2144. return -EOPNOTSUPP;
  2145. if (linkinfo[IFLA_INFO_DATA]) {
  2146. if (!ops || ops != dev->rtnl_link_ops ||
  2147. !ops->changelink)
  2148. return -EOPNOTSUPP;
  2149. err = ops->changelink(dev, tb, data);
  2150. if (err < 0)
  2151. return err;
  2152. status |= DO_SETLINK_NOTIFY;
  2153. }
  2154. if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
  2155. if (!m_ops || !m_ops->slave_changelink)
  2156. return -EOPNOTSUPP;
  2157. err = m_ops->slave_changelink(master_dev, dev,
  2158. tb, slave_data);
  2159. if (err < 0)
  2160. return err;
  2161. status |= DO_SETLINK_NOTIFY;
  2162. }
  2163. return do_setlink(skb, dev, ifm, tb, ifname, status);
  2164. }
  2165. if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
  2166. if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
  2167. return rtnl_group_changelink(skb, net,
  2168. nla_get_u32(tb[IFLA_GROUP]),
  2169. ifm, tb);
  2170. return -ENODEV;
  2171. }
  2172. if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
  2173. return -EOPNOTSUPP;
  2174. if (!ops) {
  2175. #ifdef CONFIG_MODULES
  2176. if (kind[0]) {
  2177. __rtnl_unlock();
  2178. request_module("rtnl-link-%s", kind);
  2179. rtnl_lock();
  2180. ops = rtnl_link_ops_get(kind);
  2181. if (ops)
  2182. goto replay;
  2183. }
  2184. #endif
  2185. return -EOPNOTSUPP;
  2186. }
  2187. if (!ops->setup)
  2188. return -EOPNOTSUPP;
  2189. if (!ifname[0]) {
  2190. snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
  2191. name_assign_type = NET_NAME_ENUM;
  2192. }
  2193. dest_net = rtnl_link_get_net(net, tb);
  2194. if (IS_ERR(dest_net))
  2195. return PTR_ERR(dest_net);
  2196. err = -EPERM;
  2197. if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
  2198. goto out;
  2199. if (tb[IFLA_LINK_NETNSID]) {
  2200. int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
  2201. link_net = get_net_ns_by_id(dest_net, id);
  2202. if (!link_net) {
  2203. err = -EINVAL;
  2204. goto out;
  2205. }
  2206. err = -EPERM;
  2207. if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
  2208. goto out;
  2209. }
  2210. dev = rtnl_create_link(link_net ? : dest_net, ifname,
  2211. name_assign_type, ops, tb);
  2212. if (IS_ERR(dev)) {
  2213. err = PTR_ERR(dev);
  2214. goto out;
  2215. }
  2216. dev->ifindex = ifm->ifi_index;
  2217. if (ops->newlink) {
  2218. err = ops->newlink(link_net ? : net, dev, tb, data);
  2219. /* Drivers should call free_netdev() in ->destructor
  2220. * and unregister it on failure after registration
  2221. * so that device could be finally freed in rtnl_unlock.
  2222. */
  2223. if (err < 0) {
  2224. /* If device is not registered at all, free it now */
  2225. if (dev->reg_state == NETREG_UNINITIALIZED)
  2226. free_netdev(dev);
  2227. goto out;
  2228. }
  2229. } else {
  2230. err = register_netdevice(dev);
  2231. if (err < 0) {
  2232. free_netdev(dev);
  2233. goto out;
  2234. }
  2235. }
  2236. err = rtnl_configure_link(dev, ifm);
  2237. if (err < 0)
  2238. goto out_unregister;
  2239. if (link_net) {
  2240. err = dev_change_net_namespace(dev, dest_net, ifname);
  2241. if (err < 0)
  2242. goto out_unregister;
  2243. }
  2244. if (tb[IFLA_MASTER]) {
  2245. err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
  2246. if (err)
  2247. goto out_unregister;
  2248. }
  2249. out:
  2250. if (link_net)
  2251. put_net(link_net);
  2252. put_net(dest_net);
  2253. return err;
  2254. out_unregister:
  2255. if (ops->newlink) {
  2256. LIST_HEAD(list_kill);
  2257. ops->dellink(dev, &list_kill);
  2258. unregister_netdevice_many(&list_kill);
  2259. } else {
  2260. unregister_netdevice(dev);
  2261. }
  2262. goto out;
  2263. }
  2264. }
  2265. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
  2266. {
  2267. struct net *net = sock_net(skb->sk);
  2268. struct ifinfomsg *ifm;
  2269. char ifname[IFNAMSIZ];
  2270. struct nlattr *tb[IFLA_MAX+1];
  2271. struct net_device *dev = NULL;
  2272. struct sk_buff *nskb;
  2273. int err;
  2274. u32 ext_filter_mask = 0;
  2275. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  2276. if (err < 0)
  2277. return err;
  2278. if (tb[IFLA_IFNAME])
  2279. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  2280. if (tb[IFLA_EXT_MASK])
  2281. ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
  2282. ifm = nlmsg_data(nlh);
  2283. if (ifm->ifi_index > 0)
  2284. dev = __dev_get_by_index(net, ifm->ifi_index);
  2285. else if (tb[IFLA_IFNAME])
  2286. dev = __dev_get_by_name(net, ifname);
  2287. else
  2288. return -EINVAL;
  2289. if (dev == NULL)
  2290. return -ENODEV;
  2291. nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
  2292. if (nskb == NULL)
  2293. return -ENOBUFS;
  2294. err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
  2295. nlh->nlmsg_seq, 0, 0, ext_filter_mask);
  2296. if (err < 0) {
  2297. /* -EMSGSIZE implies BUG in if_nlmsg_size */
  2298. WARN_ON(err == -EMSGSIZE);
  2299. kfree_skb(nskb);
  2300. } else
  2301. err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
  2302. return err;
  2303. }
  2304. static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
  2305. {
  2306. struct net *net = sock_net(skb->sk);
  2307. struct net_device *dev;
  2308. struct nlattr *tb[IFLA_MAX+1];
  2309. u32 ext_filter_mask = 0;
  2310. u16 min_ifinfo_dump_size = 0;
  2311. int hdrlen;
  2312. /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
  2313. hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
  2314. sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
  2315. if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
  2316. if (tb[IFLA_EXT_MASK])
  2317. ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
  2318. }
  2319. if (!ext_filter_mask)
  2320. return NLMSG_GOODSIZE;
  2321. /*
  2322. * traverse the list of net devices and compute the minimum
  2323. * buffer size based upon the filter mask.
  2324. */
  2325. list_for_each_entry(dev, &net->dev_base_head, dev_list) {
  2326. min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
  2327. if_nlmsg_size(dev,
  2328. ext_filter_mask));
  2329. }
  2330. return nlmsg_total_size(min_ifinfo_dump_size);
  2331. }
  2332. static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  2333. {
  2334. int idx;
  2335. int s_idx = cb->family;
  2336. if (s_idx == 0)
  2337. s_idx = 1;
  2338. for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
  2339. int type = cb->nlh->nlmsg_type-RTM_BASE;
  2340. if (idx < s_idx || idx == PF_PACKET)
  2341. continue;
  2342. if (rtnl_msg_handlers[idx] == NULL ||
  2343. rtnl_msg_handlers[idx][type].dumpit == NULL)
  2344. continue;
  2345. if (idx > s_idx) {
  2346. memset(&cb->args[0], 0, sizeof(cb->args));
  2347. cb->prev_seq = 0;
  2348. cb->seq = 0;
  2349. }
  2350. if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
  2351. break;
  2352. }
  2353. cb->family = idx;
  2354. return skb->len;
  2355. }
  2356. struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
  2357. unsigned int change, gfp_t flags)
  2358. {
  2359. struct net *net = dev_net(dev);
  2360. struct sk_buff *skb;
  2361. int err = -ENOBUFS;
  2362. size_t if_info_size;
  2363. skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
  2364. if (skb == NULL)
  2365. goto errout;
  2366. err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
  2367. if (err < 0) {
  2368. /* -EMSGSIZE implies BUG in if_nlmsg_size() */
  2369. WARN_ON(err == -EMSGSIZE);
  2370. kfree_skb(skb);
  2371. goto errout;
  2372. }
  2373. return skb;
  2374. errout:
  2375. if (err < 0)
  2376. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  2377. return NULL;
  2378. }
  2379. void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
  2380. {
  2381. struct net *net = dev_net(dev);
  2382. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
  2383. }
  2384. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
  2385. gfp_t flags)
  2386. {
  2387. struct sk_buff *skb;
  2388. if (dev->reg_state != NETREG_REGISTERED)
  2389. return;
  2390. skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
  2391. if (skb)
  2392. rtmsg_ifinfo_send(skb, dev, flags);
  2393. }
  2394. EXPORT_SYMBOL(rtmsg_ifinfo);
  2395. static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
  2396. struct net_device *dev,
  2397. u8 *addr, u16 vid, u32 pid, u32 seq,
  2398. int type, unsigned int flags,
  2399. int nlflags, u16 ndm_state)
  2400. {
  2401. struct nlmsghdr *nlh;
  2402. struct ndmsg *ndm;
  2403. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
  2404. if (!nlh)
  2405. return -EMSGSIZE;
  2406. ndm = nlmsg_data(nlh);
  2407. ndm->ndm_family = AF_BRIDGE;
  2408. ndm->ndm_pad1 = 0;
  2409. ndm->ndm_pad2 = 0;
  2410. ndm->ndm_flags = flags;
  2411. ndm->ndm_type = 0;
  2412. ndm->ndm_ifindex = dev->ifindex;
  2413. ndm->ndm_state = ndm_state;
  2414. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
  2415. goto nla_put_failure;
  2416. if (vid)
  2417. if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
  2418. goto nla_put_failure;
  2419. nlmsg_end(skb, nlh);
  2420. return 0;
  2421. nla_put_failure:
  2422. nlmsg_cancel(skb, nlh);
  2423. return -EMSGSIZE;
  2424. }
  2425. static inline size_t rtnl_fdb_nlmsg_size(void)
  2426. {
  2427. return NLMSG_ALIGN(sizeof(struct ndmsg)) +
  2428. nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
  2429. nla_total_size(sizeof(u16)) + /* NDA_VLAN */
  2430. 0;
  2431. }
  2432. static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
  2433. u16 ndm_state)
  2434. {
  2435. struct net *net = dev_net(dev);
  2436. struct sk_buff *skb;
  2437. int err = -ENOBUFS;
  2438. skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
  2439. if (!skb)
  2440. goto errout;
  2441. err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
  2442. 0, 0, type, NTF_SELF, 0, ndm_state);
  2443. if (err < 0) {
  2444. kfree_skb(skb);
  2445. goto errout;
  2446. }
  2447. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  2448. return;
  2449. errout:
  2450. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  2451. }
  2452. /**
  2453. * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
  2454. */
  2455. int ndo_dflt_fdb_add(struct ndmsg *ndm,
  2456. struct nlattr *tb[],
  2457. struct net_device *dev,
  2458. const unsigned char *addr, u16 vid,
  2459. u16 flags)
  2460. {
  2461. int err = -EINVAL;
  2462. /* If aging addresses are supported device will need to
  2463. * implement its own handler for this.
  2464. */
  2465. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  2466. pr_info("%s: FDB only supports static addresses\n", dev->name);
  2467. return err;
  2468. }
  2469. if (vid) {
  2470. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  2471. return err;
  2472. }
  2473. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  2474. err = dev_uc_add_excl(dev, addr);
  2475. else if (is_multicast_ether_addr(addr))
  2476. err = dev_mc_add_excl(dev, addr);
  2477. /* Only return duplicate errors if NLM_F_EXCL is set */
  2478. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  2479. err = 0;
  2480. return err;
  2481. }
  2482. EXPORT_SYMBOL(ndo_dflt_fdb_add);
  2483. static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
  2484. {
  2485. u16 vid = 0;
  2486. if (vlan_attr) {
  2487. if (nla_len(vlan_attr) != sizeof(u16)) {
  2488. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
  2489. return -EINVAL;
  2490. }
  2491. vid = nla_get_u16(vlan_attr);
  2492. if (!vid || vid >= VLAN_VID_MASK) {
  2493. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
  2494. vid);
  2495. return -EINVAL;
  2496. }
  2497. }
  2498. *p_vid = vid;
  2499. return 0;
  2500. }
  2501. static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
  2502. {
  2503. struct net *net = sock_net(skb->sk);
  2504. struct ndmsg *ndm;
  2505. struct nlattr *tb[NDA_MAX+1];
  2506. struct net_device *dev;
  2507. u8 *addr;
  2508. u16 vid;
  2509. int err;
  2510. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  2511. if (err < 0)
  2512. return err;
  2513. ndm = nlmsg_data(nlh);
  2514. if (ndm->ndm_ifindex == 0) {
  2515. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
  2516. return -EINVAL;
  2517. }
  2518. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  2519. if (dev == NULL) {
  2520. pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
  2521. return -ENODEV;
  2522. }
  2523. if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
  2524. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
  2525. return -EINVAL;
  2526. }
  2527. addr = nla_data(tb[NDA_LLADDR]);
  2528. err = fdb_vid_parse(tb[NDA_VLAN], &vid);
  2529. if (err)
  2530. return err;
  2531. err = -EOPNOTSUPP;
  2532. /* Support fdb on master device the net/bridge default case */
  2533. if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
  2534. (dev->priv_flags & IFF_BRIDGE_PORT)) {
  2535. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2536. const struct net_device_ops *ops = br_dev->netdev_ops;
  2537. err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
  2538. nlh->nlmsg_flags);
  2539. if (err)
  2540. goto out;
  2541. else
  2542. ndm->ndm_flags &= ~NTF_MASTER;
  2543. }
  2544. /* Embedded bridge, macvlan, and any other device support */
  2545. if ((ndm->ndm_flags & NTF_SELF)) {
  2546. if (dev->netdev_ops->ndo_fdb_add)
  2547. err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
  2548. vid,
  2549. nlh->nlmsg_flags);
  2550. else
  2551. err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
  2552. nlh->nlmsg_flags);
  2553. if (!err) {
  2554. rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
  2555. ndm->ndm_state);
  2556. ndm->ndm_flags &= ~NTF_SELF;
  2557. }
  2558. }
  2559. out:
  2560. return err;
  2561. }
  2562. /**
  2563. * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
  2564. */
  2565. int ndo_dflt_fdb_del(struct ndmsg *ndm,
  2566. struct nlattr *tb[],
  2567. struct net_device *dev,
  2568. const unsigned char *addr, u16 vid)
  2569. {
  2570. int err = -EINVAL;
  2571. /* If aging addresses are supported device will need to
  2572. * implement its own handler for this.
  2573. */
  2574. if (!(ndm->ndm_state & NUD_PERMANENT)) {
  2575. pr_info("%s: FDB only supports static addresses\n", dev->name);
  2576. return err;
  2577. }
  2578. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  2579. err = dev_uc_del(dev, addr);
  2580. else if (is_multicast_ether_addr(addr))
  2581. err = dev_mc_del(dev, addr);
  2582. return err;
  2583. }
  2584. EXPORT_SYMBOL(ndo_dflt_fdb_del);
  2585. static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
  2586. {
  2587. struct net *net = sock_net(skb->sk);
  2588. struct ndmsg *ndm;
  2589. struct nlattr *tb[NDA_MAX+1];
  2590. struct net_device *dev;
  2591. int err = -EINVAL;
  2592. __u8 *addr;
  2593. u16 vid;
  2594. if (!netlink_capable(skb, CAP_NET_ADMIN))
  2595. return -EPERM;
  2596. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  2597. if (err < 0)
  2598. return err;
  2599. ndm = nlmsg_data(nlh);
  2600. if (ndm->ndm_ifindex == 0) {
  2601. pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
  2602. return -EINVAL;
  2603. }
  2604. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  2605. if (dev == NULL) {
  2606. pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
  2607. return -ENODEV;
  2608. }
  2609. if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
  2610. pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
  2611. return -EINVAL;
  2612. }
  2613. addr = nla_data(tb[NDA_LLADDR]);
  2614. err = fdb_vid_parse(tb[NDA_VLAN], &vid);
  2615. if (err)
  2616. return err;
  2617. err = -EOPNOTSUPP;
  2618. /* Support fdb on master device the net/bridge default case */
  2619. if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
  2620. (dev->priv_flags & IFF_BRIDGE_PORT)) {
  2621. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2622. const struct net_device_ops *ops = br_dev->netdev_ops;
  2623. if (ops->ndo_fdb_del)
  2624. err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
  2625. if (err)
  2626. goto out;
  2627. else
  2628. ndm->ndm_flags &= ~NTF_MASTER;
  2629. }
  2630. /* Embedded bridge, macvlan, and any other device support */
  2631. if (ndm->ndm_flags & NTF_SELF) {
  2632. if (dev->netdev_ops->ndo_fdb_del)
  2633. err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
  2634. vid);
  2635. else
  2636. err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
  2637. if (!err) {
  2638. rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
  2639. ndm->ndm_state);
  2640. ndm->ndm_flags &= ~NTF_SELF;
  2641. }
  2642. }
  2643. out:
  2644. return err;
  2645. }
  2646. static int nlmsg_populate_fdb(struct sk_buff *skb,
  2647. struct netlink_callback *cb,
  2648. struct net_device *dev,
  2649. int *idx,
  2650. struct netdev_hw_addr_list *list)
  2651. {
  2652. struct netdev_hw_addr *ha;
  2653. int err;
  2654. u32 portid, seq;
  2655. portid = NETLINK_CB(cb->skb).portid;
  2656. seq = cb->nlh->nlmsg_seq;
  2657. list_for_each_entry(ha, &list->list, list) {
  2658. if (*idx < cb->args[2])
  2659. goto skip;
  2660. err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
  2661. portid, seq,
  2662. RTM_NEWNEIGH, NTF_SELF,
  2663. NLM_F_MULTI, NUD_PERMANENT);
  2664. if (err < 0)
  2665. return err;
  2666. skip:
  2667. *idx += 1;
  2668. }
  2669. return 0;
  2670. }
  2671. /**
  2672. * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
  2673. * @nlh: netlink message header
  2674. * @dev: netdevice
  2675. *
  2676. * Default netdevice operation to dump the existing unicast address list.
  2677. * Returns number of addresses from list put in skb.
  2678. */
  2679. int ndo_dflt_fdb_dump(struct sk_buff *skb,
  2680. struct netlink_callback *cb,
  2681. struct net_device *dev,
  2682. struct net_device *filter_dev,
  2683. int *idx)
  2684. {
  2685. int err;
  2686. netif_addr_lock_bh(dev);
  2687. err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
  2688. if (err)
  2689. goto out;
  2690. err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
  2691. out:
  2692. netif_addr_unlock_bh(dev);
  2693. return err;
  2694. }
  2695. EXPORT_SYMBOL(ndo_dflt_fdb_dump);
  2696. static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2697. {
  2698. struct net_device *dev;
  2699. struct nlattr *tb[IFLA_MAX+1];
  2700. struct net_device *br_dev = NULL;
  2701. const struct net_device_ops *ops = NULL;
  2702. const struct net_device_ops *cops = NULL;
  2703. struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
  2704. struct net *net = sock_net(skb->sk);
  2705. struct hlist_head *head;
  2706. int brport_idx = 0;
  2707. int br_idx = 0;
  2708. int h, s_h;
  2709. int idx = 0, s_idx;
  2710. int err = 0;
  2711. int fidx = 0;
  2712. if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
  2713. ifla_policy) == 0) {
  2714. if (tb[IFLA_MASTER])
  2715. br_idx = nla_get_u32(tb[IFLA_MASTER]);
  2716. }
  2717. brport_idx = ifm->ifi_index;
  2718. if (br_idx) {
  2719. br_dev = __dev_get_by_index(net, br_idx);
  2720. if (!br_dev)
  2721. return -ENODEV;
  2722. ops = br_dev->netdev_ops;
  2723. }
  2724. s_h = cb->args[0];
  2725. s_idx = cb->args[1];
  2726. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  2727. idx = 0;
  2728. head = &net->dev_index_head[h];
  2729. hlist_for_each_entry(dev, head, index_hlist) {
  2730. if (brport_idx && (dev->ifindex != brport_idx))
  2731. continue;
  2732. if (!br_idx) { /* user did not specify a specific bridge */
  2733. if (dev->priv_flags & IFF_BRIDGE_PORT) {
  2734. br_dev = netdev_master_upper_dev_get(dev);
  2735. cops = br_dev->netdev_ops;
  2736. }
  2737. } else {
  2738. if (dev != br_dev &&
  2739. !(dev->priv_flags & IFF_BRIDGE_PORT))
  2740. continue;
  2741. if (br_dev != netdev_master_upper_dev_get(dev) &&
  2742. !(dev->priv_flags & IFF_EBRIDGE))
  2743. continue;
  2744. cops = ops;
  2745. }
  2746. if (idx < s_idx)
  2747. goto cont;
  2748. if (dev->priv_flags & IFF_BRIDGE_PORT) {
  2749. if (cops && cops->ndo_fdb_dump) {
  2750. err = cops->ndo_fdb_dump(skb, cb,
  2751. br_dev, dev,
  2752. &fidx);
  2753. if (err == -EMSGSIZE)
  2754. goto out;
  2755. }
  2756. }
  2757. if (dev->netdev_ops->ndo_fdb_dump)
  2758. err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
  2759. dev, NULL,
  2760. &fidx);
  2761. else
  2762. err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
  2763. &fidx);
  2764. if (err == -EMSGSIZE)
  2765. goto out;
  2766. cops = NULL;
  2767. /* reset fdb offset to 0 for rest of the interfaces */
  2768. cb->args[2] = 0;
  2769. fidx = 0;
  2770. cont:
  2771. idx++;
  2772. }
  2773. }
  2774. out:
  2775. cb->args[0] = h;
  2776. cb->args[1] = idx;
  2777. cb->args[2] = fidx;
  2778. return skb->len;
  2779. }
  2780. static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
  2781. unsigned int attrnum, unsigned int flag)
  2782. {
  2783. if (mask & flag)
  2784. return nla_put_u8(skb, attrnum, !!(flags & flag));
  2785. return 0;
  2786. }
  2787. int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  2788. struct net_device *dev, u16 mode,
  2789. u32 flags, u32 mask, int nlflags,
  2790. u32 filter_mask,
  2791. int (*vlan_fill)(struct sk_buff *skb,
  2792. struct net_device *dev,
  2793. u32 filter_mask))
  2794. {
  2795. struct nlmsghdr *nlh;
  2796. struct ifinfomsg *ifm;
  2797. struct nlattr *br_afspec;
  2798. struct nlattr *protinfo;
  2799. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  2800. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2801. int err = 0;
  2802. nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
  2803. if (nlh == NULL)
  2804. return -EMSGSIZE;
  2805. ifm = nlmsg_data(nlh);
  2806. ifm->ifi_family = AF_BRIDGE;
  2807. ifm->__ifi_pad = 0;
  2808. ifm->ifi_type = dev->type;
  2809. ifm->ifi_index = dev->ifindex;
  2810. ifm->ifi_flags = dev_get_flags(dev);
  2811. ifm->ifi_change = 0;
  2812. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  2813. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  2814. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  2815. (br_dev &&
  2816. nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
  2817. (dev->addr_len &&
  2818. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  2819. (dev->ifindex != dev_get_iflink(dev) &&
  2820. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
  2821. goto nla_put_failure;
  2822. br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
  2823. if (!br_afspec)
  2824. goto nla_put_failure;
  2825. if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
  2826. nla_nest_cancel(skb, br_afspec);
  2827. goto nla_put_failure;
  2828. }
  2829. if (mode != BRIDGE_MODE_UNDEF) {
  2830. if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
  2831. nla_nest_cancel(skb, br_afspec);
  2832. goto nla_put_failure;
  2833. }
  2834. }
  2835. if (vlan_fill) {
  2836. err = vlan_fill(skb, dev, filter_mask);
  2837. if (err) {
  2838. nla_nest_cancel(skb, br_afspec);
  2839. goto nla_put_failure;
  2840. }
  2841. }
  2842. nla_nest_end(skb, br_afspec);
  2843. protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  2844. if (!protinfo)
  2845. goto nla_put_failure;
  2846. if (brport_nla_put_flag(skb, flags, mask,
  2847. IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
  2848. brport_nla_put_flag(skb, flags, mask,
  2849. IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
  2850. brport_nla_put_flag(skb, flags, mask,
  2851. IFLA_BRPORT_FAST_LEAVE,
  2852. BR_MULTICAST_FAST_LEAVE) ||
  2853. brport_nla_put_flag(skb, flags, mask,
  2854. IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
  2855. brport_nla_put_flag(skb, flags, mask,
  2856. IFLA_BRPORT_LEARNING, BR_LEARNING) ||
  2857. brport_nla_put_flag(skb, flags, mask,
  2858. IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
  2859. brport_nla_put_flag(skb, flags, mask,
  2860. IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
  2861. brport_nla_put_flag(skb, flags, mask,
  2862. IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
  2863. nla_nest_cancel(skb, protinfo);
  2864. goto nla_put_failure;
  2865. }
  2866. nla_nest_end(skb, protinfo);
  2867. nlmsg_end(skb, nlh);
  2868. return 0;
  2869. nla_put_failure:
  2870. nlmsg_cancel(skb, nlh);
  2871. return err ? err : -EMSGSIZE;
  2872. }
  2873. EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
  2874. static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
  2875. {
  2876. struct net *net = sock_net(skb->sk);
  2877. struct net_device *dev;
  2878. int idx = 0;
  2879. u32 portid = NETLINK_CB(cb->skb).portid;
  2880. u32 seq = cb->nlh->nlmsg_seq;
  2881. u32 filter_mask = 0;
  2882. int err;
  2883. if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
  2884. struct nlattr *extfilt;
  2885. extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
  2886. IFLA_EXT_MASK);
  2887. if (extfilt) {
  2888. if (nla_len(extfilt) < sizeof(filter_mask))
  2889. return -EINVAL;
  2890. filter_mask = nla_get_u32(extfilt);
  2891. }
  2892. }
  2893. rcu_read_lock();
  2894. for_each_netdev_rcu(net, dev) {
  2895. const struct net_device_ops *ops = dev->netdev_ops;
  2896. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2897. if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
  2898. if (idx >= cb->args[0]) {
  2899. err = br_dev->netdev_ops->ndo_bridge_getlink(
  2900. skb, portid, seq, dev,
  2901. filter_mask, NLM_F_MULTI);
  2902. if (err < 0 && err != -EOPNOTSUPP)
  2903. break;
  2904. }
  2905. idx++;
  2906. }
  2907. if (ops->ndo_bridge_getlink) {
  2908. if (idx >= cb->args[0]) {
  2909. err = ops->ndo_bridge_getlink(skb, portid,
  2910. seq, dev,
  2911. filter_mask,
  2912. NLM_F_MULTI);
  2913. if (err < 0 && err != -EOPNOTSUPP)
  2914. break;
  2915. }
  2916. idx++;
  2917. }
  2918. }
  2919. rcu_read_unlock();
  2920. cb->args[0] = idx;
  2921. return skb->len;
  2922. }
  2923. static inline size_t bridge_nlmsg_size(void)
  2924. {
  2925. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  2926. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  2927. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  2928. + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
  2929. + nla_total_size(sizeof(u32)) /* IFLA_MTU */
  2930. + nla_total_size(sizeof(u32)) /* IFLA_LINK */
  2931. + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
  2932. + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
  2933. + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
  2934. + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
  2935. + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
  2936. }
  2937. static int rtnl_bridge_notify(struct net_device *dev)
  2938. {
  2939. struct net *net = dev_net(dev);
  2940. struct sk_buff *skb;
  2941. int err = -EOPNOTSUPP;
  2942. if (!dev->netdev_ops->ndo_bridge_getlink)
  2943. return 0;
  2944. skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
  2945. if (!skb) {
  2946. err = -ENOMEM;
  2947. goto errout;
  2948. }
  2949. err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
  2950. if (err < 0)
  2951. goto errout;
  2952. if (!skb->len)
  2953. goto errout;
  2954. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  2955. return 0;
  2956. errout:
  2957. WARN_ON(err == -EMSGSIZE);
  2958. kfree_skb(skb);
  2959. if (err)
  2960. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  2961. return err;
  2962. }
  2963. static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
  2964. {
  2965. struct net *net = sock_net(skb->sk);
  2966. struct ifinfomsg *ifm;
  2967. struct net_device *dev;
  2968. struct nlattr *br_spec, *attr = NULL;
  2969. int rem, err = -EOPNOTSUPP;
  2970. u16 flags = 0;
  2971. bool have_flags = false;
  2972. if (nlmsg_len(nlh) < sizeof(*ifm))
  2973. return -EINVAL;
  2974. ifm = nlmsg_data(nlh);
  2975. if (ifm->ifi_family != AF_BRIDGE)
  2976. return -EPFNOSUPPORT;
  2977. dev = __dev_get_by_index(net, ifm->ifi_index);
  2978. if (!dev) {
  2979. pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
  2980. return -ENODEV;
  2981. }
  2982. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  2983. if (br_spec) {
  2984. nla_for_each_nested(attr, br_spec, rem) {
  2985. if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
  2986. if (nla_len(attr) < sizeof(flags))
  2987. return -EINVAL;
  2988. have_flags = true;
  2989. flags = nla_get_u16(attr);
  2990. break;
  2991. }
  2992. }
  2993. }
  2994. if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
  2995. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2996. if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
  2997. err = -EOPNOTSUPP;
  2998. goto out;
  2999. }
  3000. err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
  3001. if (err)
  3002. goto out;
  3003. flags &= ~BRIDGE_FLAGS_MASTER;
  3004. }
  3005. if ((flags & BRIDGE_FLAGS_SELF)) {
  3006. if (!dev->netdev_ops->ndo_bridge_setlink)
  3007. err = -EOPNOTSUPP;
  3008. else
  3009. err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
  3010. flags);
  3011. if (!err) {
  3012. flags &= ~BRIDGE_FLAGS_SELF;
  3013. /* Generate event to notify upper layer of bridge
  3014. * change
  3015. */
  3016. err = rtnl_bridge_notify(dev);
  3017. }
  3018. }
  3019. if (have_flags)
  3020. memcpy(nla_data(attr), &flags, sizeof(flags));
  3021. out:
  3022. return err;
  3023. }
  3024. static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
  3025. {
  3026. struct net *net = sock_net(skb->sk);
  3027. struct ifinfomsg *ifm;
  3028. struct net_device *dev;
  3029. struct nlattr *br_spec, *attr = NULL;
  3030. int rem, err = -EOPNOTSUPP;
  3031. u16 flags = 0;
  3032. bool have_flags = false;
  3033. if (nlmsg_len(nlh) < sizeof(*ifm))
  3034. return -EINVAL;
  3035. ifm = nlmsg_data(nlh);
  3036. if (ifm->ifi_family != AF_BRIDGE)
  3037. return -EPFNOSUPPORT;
  3038. dev = __dev_get_by_index(net, ifm->ifi_index);
  3039. if (!dev) {
  3040. pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
  3041. return -ENODEV;
  3042. }
  3043. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  3044. if (br_spec) {
  3045. nla_for_each_nested(attr, br_spec, rem) {
  3046. if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
  3047. if (nla_len(attr) < sizeof(flags))
  3048. return -EINVAL;
  3049. have_flags = true;
  3050. flags = nla_get_u16(attr);
  3051. break;
  3052. }
  3053. }
  3054. }
  3055. if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
  3056. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  3057. if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
  3058. err = -EOPNOTSUPP;
  3059. goto out;
  3060. }
  3061. err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
  3062. if (err)
  3063. goto out;
  3064. flags &= ~BRIDGE_FLAGS_MASTER;
  3065. }
  3066. if ((flags & BRIDGE_FLAGS_SELF)) {
  3067. if (!dev->netdev_ops->ndo_bridge_dellink)
  3068. err = -EOPNOTSUPP;
  3069. else
  3070. err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
  3071. flags);
  3072. if (!err) {
  3073. flags &= ~BRIDGE_FLAGS_SELF;
  3074. /* Generate event to notify upper layer of bridge
  3075. * change
  3076. */
  3077. err = rtnl_bridge_notify(dev);
  3078. }
  3079. }
  3080. if (have_flags)
  3081. memcpy(nla_data(attr), &flags, sizeof(flags));
  3082. out:
  3083. return err;
  3084. }
  3085. static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
  3086. {
  3087. return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
  3088. (!idxattr || idxattr == attrid);
  3089. }
  3090. #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
  3091. static int rtnl_get_offload_stats_attr_size(int attr_id)
  3092. {
  3093. switch (attr_id) {
  3094. case IFLA_OFFLOAD_XSTATS_CPU_HIT:
  3095. return sizeof(struct rtnl_link_stats64);
  3096. }
  3097. return 0;
  3098. }
  3099. static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
  3100. int *prividx)
  3101. {
  3102. struct nlattr *attr = NULL;
  3103. int attr_id, size;
  3104. void *attr_data;
  3105. int err;
  3106. if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
  3107. dev->netdev_ops->ndo_get_offload_stats))
  3108. return -ENODATA;
  3109. for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
  3110. attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
  3111. if (attr_id < *prividx)
  3112. continue;
  3113. size = rtnl_get_offload_stats_attr_size(attr_id);
  3114. if (!size)
  3115. continue;
  3116. if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
  3117. continue;
  3118. attr = nla_reserve_64bit(skb, attr_id, size,
  3119. IFLA_OFFLOAD_XSTATS_UNSPEC);
  3120. if (!attr)
  3121. goto nla_put_failure;
  3122. attr_data = nla_data(attr);
  3123. memset(attr_data, 0, size);
  3124. err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
  3125. attr_data);
  3126. if (err)
  3127. goto get_offload_stats_failure;
  3128. }
  3129. if (!attr)
  3130. return -ENODATA;
  3131. *prividx = 0;
  3132. return 0;
  3133. nla_put_failure:
  3134. err = -EMSGSIZE;
  3135. get_offload_stats_failure:
  3136. *prividx = attr_id;
  3137. return err;
  3138. }
  3139. static int rtnl_get_offload_stats_size(const struct net_device *dev)
  3140. {
  3141. int nla_size = 0;
  3142. int attr_id;
  3143. int size;
  3144. if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
  3145. dev->netdev_ops->ndo_get_offload_stats))
  3146. return 0;
  3147. for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
  3148. attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
  3149. if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
  3150. continue;
  3151. size = rtnl_get_offload_stats_attr_size(attr_id);
  3152. nla_size += nla_total_size_64bit(size);
  3153. }
  3154. if (nla_size != 0)
  3155. nla_size += nla_total_size(0);
  3156. return nla_size;
  3157. }
  3158. static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
  3159. int type, u32 pid, u32 seq, u32 change,
  3160. unsigned int flags, unsigned int filter_mask,
  3161. int *idxattr, int *prividx)
  3162. {
  3163. struct if_stats_msg *ifsm;
  3164. struct nlmsghdr *nlh;
  3165. struct nlattr *attr;
  3166. int s_prividx = *prividx;
  3167. int err;
  3168. ASSERT_RTNL();
  3169. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
  3170. if (!nlh)
  3171. return -EMSGSIZE;
  3172. ifsm = nlmsg_data(nlh);
  3173. ifsm->ifindex = dev->ifindex;
  3174. ifsm->filter_mask = filter_mask;
  3175. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
  3176. struct rtnl_link_stats64 *sp;
  3177. attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
  3178. sizeof(struct rtnl_link_stats64),
  3179. IFLA_STATS_UNSPEC);
  3180. if (!attr)
  3181. goto nla_put_failure;
  3182. sp = nla_data(attr);
  3183. dev_get_stats(dev, sp);
  3184. }
  3185. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
  3186. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  3187. if (ops && ops->fill_linkxstats) {
  3188. *idxattr = IFLA_STATS_LINK_XSTATS;
  3189. attr = nla_nest_start(skb,
  3190. IFLA_STATS_LINK_XSTATS);
  3191. if (!attr)
  3192. goto nla_put_failure;
  3193. err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
  3194. nla_nest_end(skb, attr);
  3195. if (err)
  3196. goto nla_put_failure;
  3197. *idxattr = 0;
  3198. }
  3199. }
  3200. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
  3201. *idxattr)) {
  3202. const struct rtnl_link_ops *ops = NULL;
  3203. const struct net_device *master;
  3204. master = netdev_master_upper_dev_get(dev);
  3205. if (master)
  3206. ops = master->rtnl_link_ops;
  3207. if (ops && ops->fill_linkxstats) {
  3208. *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
  3209. attr = nla_nest_start(skb,
  3210. IFLA_STATS_LINK_XSTATS_SLAVE);
  3211. if (!attr)
  3212. goto nla_put_failure;
  3213. err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
  3214. nla_nest_end(skb, attr);
  3215. if (err)
  3216. goto nla_put_failure;
  3217. *idxattr = 0;
  3218. }
  3219. }
  3220. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
  3221. *idxattr)) {
  3222. *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
  3223. attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
  3224. if (!attr)
  3225. goto nla_put_failure;
  3226. err = rtnl_get_offload_stats(skb, dev, prividx);
  3227. if (err == -ENODATA)
  3228. nla_nest_cancel(skb, attr);
  3229. else
  3230. nla_nest_end(skb, attr);
  3231. if (err && err != -ENODATA)
  3232. goto nla_put_failure;
  3233. *idxattr = 0;
  3234. }
  3235. if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
  3236. struct rtnl_af_ops *af_ops;
  3237. *idxattr = IFLA_STATS_AF_SPEC;
  3238. attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
  3239. if (!attr)
  3240. goto nla_put_failure;
  3241. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  3242. if (af_ops->fill_stats_af) {
  3243. struct nlattr *af;
  3244. int err;
  3245. af = nla_nest_start(skb, af_ops->family);
  3246. if (!af)
  3247. goto nla_put_failure;
  3248. err = af_ops->fill_stats_af(skb, dev);
  3249. if (err == -ENODATA)
  3250. nla_nest_cancel(skb, af);
  3251. else if (err < 0)
  3252. goto nla_put_failure;
  3253. nla_nest_end(skb, af);
  3254. }
  3255. }
  3256. nla_nest_end(skb, attr);
  3257. *idxattr = 0;
  3258. }
  3259. nlmsg_end(skb, nlh);
  3260. return 0;
  3261. nla_put_failure:
  3262. /* not a multi message or no progress mean a real error */
  3263. if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
  3264. nlmsg_cancel(skb, nlh);
  3265. else
  3266. nlmsg_end(skb, nlh);
  3267. return -EMSGSIZE;
  3268. }
  3269. static size_t if_nlmsg_stats_size(const struct net_device *dev,
  3270. u32 filter_mask)
  3271. {
  3272. size_t size = 0;
  3273. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
  3274. size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
  3275. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
  3276. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  3277. int attr = IFLA_STATS_LINK_XSTATS;
  3278. if (ops && ops->get_linkxstats_size) {
  3279. size += nla_total_size(ops->get_linkxstats_size(dev,
  3280. attr));
  3281. /* for IFLA_STATS_LINK_XSTATS */
  3282. size += nla_total_size(0);
  3283. }
  3284. }
  3285. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
  3286. struct net_device *_dev = (struct net_device *)dev;
  3287. const struct rtnl_link_ops *ops = NULL;
  3288. const struct net_device *master;
  3289. /* netdev_master_upper_dev_get can't take const */
  3290. master = netdev_master_upper_dev_get(_dev);
  3291. if (master)
  3292. ops = master->rtnl_link_ops;
  3293. if (ops && ops->get_linkxstats_size) {
  3294. int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
  3295. size += nla_total_size(ops->get_linkxstats_size(dev,
  3296. attr));
  3297. /* for IFLA_STATS_LINK_XSTATS_SLAVE */
  3298. size += nla_total_size(0);
  3299. }
  3300. }
  3301. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
  3302. size += rtnl_get_offload_stats_size(dev);
  3303. if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
  3304. struct rtnl_af_ops *af_ops;
  3305. /* for IFLA_STATS_AF_SPEC */
  3306. size += nla_total_size(0);
  3307. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  3308. if (af_ops->get_stats_af_size) {
  3309. size += nla_total_size(
  3310. af_ops->get_stats_af_size(dev));
  3311. /* for AF_* */
  3312. size += nla_total_size(0);
  3313. }
  3314. }
  3315. }
  3316. return size;
  3317. }
  3318. static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
  3319. {
  3320. struct net *net = sock_net(skb->sk);
  3321. struct net_device *dev = NULL;
  3322. int idxattr = 0, prividx = 0;
  3323. struct if_stats_msg *ifsm;
  3324. struct sk_buff *nskb;
  3325. u32 filter_mask;
  3326. int err;
  3327. if (nlmsg_len(nlh) < sizeof(*ifsm))
  3328. return -EINVAL;
  3329. ifsm = nlmsg_data(nlh);
  3330. if (ifsm->ifindex > 0)
  3331. dev = __dev_get_by_index(net, ifsm->ifindex);
  3332. else
  3333. return -EINVAL;
  3334. if (!dev)
  3335. return -ENODEV;
  3336. filter_mask = ifsm->filter_mask;
  3337. if (!filter_mask)
  3338. return -EINVAL;
  3339. nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
  3340. if (!nskb)
  3341. return -ENOBUFS;
  3342. err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
  3343. NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  3344. 0, filter_mask, &idxattr, &prividx);
  3345. if (err < 0) {
  3346. /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
  3347. WARN_ON(err == -EMSGSIZE);
  3348. kfree_skb(nskb);
  3349. } else {
  3350. err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
  3351. }
  3352. return err;
  3353. }
  3354. static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3355. {
  3356. int h, s_h, err, s_idx, s_idxattr, s_prividx;
  3357. struct net *net = sock_net(skb->sk);
  3358. unsigned int flags = NLM_F_MULTI;
  3359. struct if_stats_msg *ifsm;
  3360. struct hlist_head *head;
  3361. struct net_device *dev;
  3362. u32 filter_mask = 0;
  3363. int idx = 0;
  3364. s_h = cb->args[0];
  3365. s_idx = cb->args[1];
  3366. s_idxattr = cb->args[2];
  3367. s_prividx = cb->args[3];
  3368. cb->seq = net->dev_base_seq;
  3369. if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
  3370. return -EINVAL;
  3371. ifsm = nlmsg_data(cb->nlh);
  3372. filter_mask = ifsm->filter_mask;
  3373. if (!filter_mask)
  3374. return -EINVAL;
  3375. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  3376. idx = 0;
  3377. head = &net->dev_index_head[h];
  3378. hlist_for_each_entry(dev, head, index_hlist) {
  3379. if (idx < s_idx)
  3380. goto cont;
  3381. err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
  3382. NETLINK_CB(cb->skb).portid,
  3383. cb->nlh->nlmsg_seq, 0,
  3384. flags, filter_mask,
  3385. &s_idxattr, &s_prividx);
  3386. /* If we ran out of room on the first message,
  3387. * we're in trouble
  3388. */
  3389. WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
  3390. if (err < 0)
  3391. goto out;
  3392. s_prividx = 0;
  3393. s_idxattr = 0;
  3394. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  3395. cont:
  3396. idx++;
  3397. }
  3398. }
  3399. out:
  3400. cb->args[3] = s_prividx;
  3401. cb->args[2] = s_idxattr;
  3402. cb->args[1] = idx;
  3403. cb->args[0] = h;
  3404. return skb->len;
  3405. }
  3406. /* Process one rtnetlink message. */
  3407. static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  3408. {
  3409. struct net *net = sock_net(skb->sk);
  3410. rtnl_doit_func doit;
  3411. int kind;
  3412. int family;
  3413. int type;
  3414. int err;
  3415. type = nlh->nlmsg_type;
  3416. if (type > RTM_MAX)
  3417. return -EOPNOTSUPP;
  3418. type -= RTM_BASE;
  3419. /* All the messages must have at least 1 byte length */
  3420. if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
  3421. return 0;
  3422. family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
  3423. kind = type&3;
  3424. if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
  3425. return -EPERM;
  3426. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  3427. struct sock *rtnl;
  3428. rtnl_dumpit_func dumpit;
  3429. rtnl_calcit_func calcit;
  3430. u16 min_dump_alloc = 0;
  3431. dumpit = rtnl_get_dumpit(family, type);
  3432. if (dumpit == NULL)
  3433. return -EOPNOTSUPP;
  3434. calcit = rtnl_get_calcit(family, type);
  3435. if (calcit)
  3436. min_dump_alloc = calcit(skb, nlh);
  3437. __rtnl_unlock();
  3438. rtnl = net->rtnl;
  3439. {
  3440. struct netlink_dump_control c = {
  3441. .dump = dumpit,
  3442. .min_dump_alloc = min_dump_alloc,
  3443. };
  3444. err = netlink_dump_start(rtnl, skb, nlh, &c);
  3445. }
  3446. rtnl_lock();
  3447. return err;
  3448. }
  3449. doit = rtnl_get_doit(family, type);
  3450. if (doit == NULL)
  3451. return -EOPNOTSUPP;
  3452. return doit(skb, nlh);
  3453. }
  3454. static void rtnetlink_rcv(struct sk_buff *skb)
  3455. {
  3456. rtnl_lock();
  3457. netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
  3458. rtnl_unlock();
  3459. }
  3460. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  3461. {
  3462. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3463. switch (event) {
  3464. case NETDEV_REBOOT:
  3465. case NETDEV_CHANGEMTU:
  3466. case NETDEV_CHANGEADDR:
  3467. case NETDEV_CHANGENAME:
  3468. case NETDEV_FEAT_CHANGE:
  3469. case NETDEV_BONDING_FAILOVER:
  3470. case NETDEV_POST_TYPE_CHANGE:
  3471. case NETDEV_NOTIFY_PEERS:
  3472. case NETDEV_CHANGEUPPER:
  3473. case NETDEV_RESEND_IGMP:
  3474. case NETDEV_PRECHANGEMTU:
  3475. case NETDEV_CHANGEINFODATA:
  3476. case NETDEV_PRECHANGEUPPER:
  3477. case NETDEV_CHANGELOWERSTATE:
  3478. case NETDEV_UDP_TUNNEL_PUSH_INFO:
  3479. case NETDEV_CHANGE_TX_QUEUE_LEN:
  3480. rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
  3481. break;
  3482. default:
  3483. break;
  3484. }
  3485. return NOTIFY_DONE;
  3486. }
  3487. static struct notifier_block rtnetlink_dev_notifier = {
  3488. .notifier_call = rtnetlink_event,
  3489. };
  3490. static int __net_init rtnetlink_net_init(struct net *net)
  3491. {
  3492. struct sock *sk;
  3493. struct netlink_kernel_cfg cfg = {
  3494. .groups = RTNLGRP_MAX,
  3495. .input = rtnetlink_rcv,
  3496. .cb_mutex = &rtnl_mutex,
  3497. .flags = NL_CFG_F_NONROOT_RECV,
  3498. };
  3499. sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
  3500. if (!sk)
  3501. return -ENOMEM;
  3502. net->rtnl = sk;
  3503. return 0;
  3504. }
  3505. static void __net_exit rtnetlink_net_exit(struct net *net)
  3506. {
  3507. netlink_kernel_release(net->rtnl);
  3508. net->rtnl = NULL;
  3509. }
  3510. static struct pernet_operations rtnetlink_net_ops = {
  3511. .init = rtnetlink_net_init,
  3512. .exit = rtnetlink_net_exit,
  3513. };
  3514. void __init rtnetlink_init(void)
  3515. {
  3516. if (register_pernet_subsys(&rtnetlink_net_ops))
  3517. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  3518. register_netdevice_notifier(&rtnetlink_dev_notifier);
  3519. rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
  3520. rtnl_dump_ifinfo, rtnl_calcit);
  3521. rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
  3522. rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
  3523. rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
  3524. rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
  3525. rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
  3526. rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, NULL);
  3527. rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
  3528. rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
  3529. rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
  3530. rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
  3531. rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
  3532. rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
  3533. rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
  3534. NULL);
  3535. }