rtnetlink.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158
  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 <asm/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 && dev_is_pci(dev->dev.parent) &&
  708. (ext_filter_mask & RTEXT_FILTER_VF)) {
  709. int num_vfs = dev_num_vf(dev->dev.parent);
  710. size_t size = nla_total_size(0);
  711. size += num_vfs *
  712. (nla_total_size(0) +
  713. nla_total_size(sizeof(struct ifla_vf_mac)) +
  714. nla_total_size(sizeof(struct ifla_vf_vlan)) +
  715. nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
  716. nla_total_size(MAX_VLAN_LIST_LEN *
  717. sizeof(struct ifla_vf_vlan_info)) +
  718. nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
  719. nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
  720. nla_total_size(sizeof(struct ifla_vf_rate)) +
  721. nla_total_size(sizeof(struct ifla_vf_link_state)) +
  722. nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
  723. nla_total_size(0) + /* nest IFLA_VF_STATS */
  724. /* IFLA_VF_STATS_RX_PACKETS */
  725. nla_total_size_64bit(sizeof(__u64)) +
  726. /* IFLA_VF_STATS_TX_PACKETS */
  727. nla_total_size_64bit(sizeof(__u64)) +
  728. /* IFLA_VF_STATS_RX_BYTES */
  729. nla_total_size_64bit(sizeof(__u64)) +
  730. /* IFLA_VF_STATS_TX_BYTES */
  731. nla_total_size_64bit(sizeof(__u64)) +
  732. /* IFLA_VF_STATS_BROADCAST */
  733. nla_total_size_64bit(sizeof(__u64)) +
  734. /* IFLA_VF_STATS_MULTICAST */
  735. nla_total_size_64bit(sizeof(__u64)) +
  736. nla_total_size(sizeof(struct ifla_vf_trust)));
  737. return size;
  738. } else
  739. return 0;
  740. }
  741. static size_t rtnl_port_size(const struct net_device *dev,
  742. u32 ext_filter_mask)
  743. {
  744. size_t port_size = nla_total_size(4) /* PORT_VF */
  745. + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
  746. + nla_total_size(sizeof(struct ifla_port_vsi))
  747. /* PORT_VSI_TYPE */
  748. + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
  749. + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
  750. + nla_total_size(1) /* PROT_VDP_REQUEST */
  751. + nla_total_size(2); /* PORT_VDP_RESPONSE */
  752. size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
  753. size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
  754. + port_size;
  755. size_t port_self_size = nla_total_size(sizeof(struct nlattr))
  756. + port_size;
  757. if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
  758. !(ext_filter_mask & RTEXT_FILTER_VF))
  759. return 0;
  760. if (dev_num_vf(dev->dev.parent))
  761. return port_self_size + vf_ports_size +
  762. vf_port_size * dev_num_vf(dev->dev.parent);
  763. else
  764. return port_self_size;
  765. }
  766. static size_t rtnl_xdp_size(const struct net_device *dev)
  767. {
  768. size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
  769. nla_total_size(1); /* XDP_ATTACHED */
  770. if (!dev->netdev_ops->ndo_xdp)
  771. return 0;
  772. else
  773. return xdp_size;
  774. }
  775. static noinline size_t if_nlmsg_size(const struct net_device *dev,
  776. u32 ext_filter_mask)
  777. {
  778. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  779. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  780. + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
  781. + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
  782. + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
  783. + nla_total_size(sizeof(struct rtnl_link_stats))
  784. + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
  785. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  786. + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
  787. + nla_total_size(4) /* IFLA_TXQLEN */
  788. + nla_total_size(4) /* IFLA_WEIGHT */
  789. + nla_total_size(4) /* IFLA_MTU */
  790. + nla_total_size(4) /* IFLA_LINK */
  791. + nla_total_size(4) /* IFLA_MASTER */
  792. + nla_total_size(1) /* IFLA_CARRIER */
  793. + nla_total_size(4) /* IFLA_PROMISCUITY */
  794. + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
  795. + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
  796. + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
  797. + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
  798. + nla_total_size(1) /* IFLA_OPERSTATE */
  799. + nla_total_size(1) /* IFLA_LINKMODE */
  800. + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
  801. + nla_total_size(4) /* IFLA_LINK_NETNSID */
  802. + nla_total_size(4) /* IFLA_GROUP */
  803. + nla_total_size(ext_filter_mask
  804. & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
  805. + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
  806. + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
  807. + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
  808. + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
  809. + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
  810. + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
  811. + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
  812. + rtnl_xdp_size(dev) /* IFLA_XDP */
  813. + nla_total_size(1); /* IFLA_PROTO_DOWN */
  814. }
  815. static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
  816. {
  817. struct nlattr *vf_ports;
  818. struct nlattr *vf_port;
  819. int vf;
  820. int err;
  821. vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
  822. if (!vf_ports)
  823. return -EMSGSIZE;
  824. for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
  825. vf_port = nla_nest_start(skb, IFLA_VF_PORT);
  826. if (!vf_port)
  827. goto nla_put_failure;
  828. if (nla_put_u32(skb, IFLA_PORT_VF, vf))
  829. goto nla_put_failure;
  830. err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
  831. if (err == -EMSGSIZE)
  832. goto nla_put_failure;
  833. if (err) {
  834. nla_nest_cancel(skb, vf_port);
  835. continue;
  836. }
  837. nla_nest_end(skb, vf_port);
  838. }
  839. nla_nest_end(skb, vf_ports);
  840. return 0;
  841. nla_put_failure:
  842. nla_nest_cancel(skb, vf_ports);
  843. return -EMSGSIZE;
  844. }
  845. static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
  846. {
  847. struct nlattr *port_self;
  848. int err;
  849. port_self = nla_nest_start(skb, IFLA_PORT_SELF);
  850. if (!port_self)
  851. return -EMSGSIZE;
  852. err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
  853. if (err) {
  854. nla_nest_cancel(skb, port_self);
  855. return (err == -EMSGSIZE) ? err : 0;
  856. }
  857. nla_nest_end(skb, port_self);
  858. return 0;
  859. }
  860. static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
  861. u32 ext_filter_mask)
  862. {
  863. int err;
  864. if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
  865. !(ext_filter_mask & RTEXT_FILTER_VF))
  866. return 0;
  867. err = rtnl_port_self_fill(skb, dev);
  868. if (err)
  869. return err;
  870. if (dev_num_vf(dev->dev.parent)) {
  871. err = rtnl_vf_ports_fill(skb, dev);
  872. if (err)
  873. return err;
  874. }
  875. return 0;
  876. }
  877. static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
  878. {
  879. int err;
  880. struct netdev_phys_item_id ppid;
  881. err = dev_get_phys_port_id(dev, &ppid);
  882. if (err) {
  883. if (err == -EOPNOTSUPP)
  884. return 0;
  885. return err;
  886. }
  887. if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
  888. return -EMSGSIZE;
  889. return 0;
  890. }
  891. static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
  892. {
  893. char name[IFNAMSIZ];
  894. int err;
  895. err = dev_get_phys_port_name(dev, name, sizeof(name));
  896. if (err) {
  897. if (err == -EOPNOTSUPP)
  898. return 0;
  899. return err;
  900. }
  901. if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
  902. return -EMSGSIZE;
  903. return 0;
  904. }
  905. static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
  906. {
  907. int err;
  908. struct switchdev_attr attr = {
  909. .orig_dev = dev,
  910. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  911. .flags = SWITCHDEV_F_NO_RECURSE,
  912. };
  913. err = switchdev_port_attr_get(dev, &attr);
  914. if (err) {
  915. if (err == -EOPNOTSUPP)
  916. return 0;
  917. return err;
  918. }
  919. if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
  920. attr.u.ppid.id))
  921. return -EMSGSIZE;
  922. return 0;
  923. }
  924. static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
  925. struct net_device *dev)
  926. {
  927. struct rtnl_link_stats64 *sp;
  928. struct nlattr *attr;
  929. attr = nla_reserve_64bit(skb, IFLA_STATS64,
  930. sizeof(struct rtnl_link_stats64), IFLA_PAD);
  931. if (!attr)
  932. return -EMSGSIZE;
  933. sp = nla_data(attr);
  934. dev_get_stats(dev, sp);
  935. attr = nla_reserve(skb, IFLA_STATS,
  936. sizeof(struct rtnl_link_stats));
  937. if (!attr)
  938. return -EMSGSIZE;
  939. copy_rtnl_link_stats(nla_data(attr), sp);
  940. return 0;
  941. }
  942. static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
  943. struct net_device *dev,
  944. int vfs_num,
  945. struct nlattr *vfinfo)
  946. {
  947. struct ifla_vf_rss_query_en vf_rss_query_en;
  948. struct nlattr *vf, *vfstats, *vfvlanlist;
  949. struct ifla_vf_link_state vf_linkstate;
  950. struct ifla_vf_vlan_info vf_vlan_info;
  951. struct ifla_vf_spoofchk vf_spoofchk;
  952. struct ifla_vf_tx_rate vf_tx_rate;
  953. struct ifla_vf_stats vf_stats;
  954. struct ifla_vf_trust vf_trust;
  955. struct ifla_vf_vlan vf_vlan;
  956. struct ifla_vf_rate vf_rate;
  957. struct ifla_vf_mac vf_mac;
  958. struct ifla_vf_info ivi;
  959. memset(&ivi, 0, sizeof(ivi));
  960. /* Not all SR-IOV capable drivers support the
  961. * spoofcheck and "RSS query enable" query. Preset to
  962. * -1 so the user space tool can detect that the driver
  963. * didn't report anything.
  964. */
  965. ivi.spoofchk = -1;
  966. ivi.rss_query_en = -1;
  967. ivi.trusted = -1;
  968. /* The default value for VF link state is "auto"
  969. * IFLA_VF_LINK_STATE_AUTO which equals zero
  970. */
  971. ivi.linkstate = 0;
  972. /* VLAN Protocol by default is 802.1Q */
  973. ivi.vlan_proto = htons(ETH_P_8021Q);
  974. if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
  975. return 0;
  976. memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
  977. vf_mac.vf =
  978. vf_vlan.vf =
  979. vf_vlan_info.vf =
  980. vf_rate.vf =
  981. vf_tx_rate.vf =
  982. vf_spoofchk.vf =
  983. vf_linkstate.vf =
  984. vf_rss_query_en.vf =
  985. vf_trust.vf = ivi.vf;
  986. memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
  987. vf_vlan.vlan = ivi.vlan;
  988. vf_vlan.qos = ivi.qos;
  989. vf_vlan_info.vlan = ivi.vlan;
  990. vf_vlan_info.qos = ivi.qos;
  991. vf_vlan_info.vlan_proto = ivi.vlan_proto;
  992. vf_tx_rate.rate = ivi.max_tx_rate;
  993. vf_rate.min_tx_rate = ivi.min_tx_rate;
  994. vf_rate.max_tx_rate = ivi.max_tx_rate;
  995. vf_spoofchk.setting = ivi.spoofchk;
  996. vf_linkstate.link_state = ivi.linkstate;
  997. vf_rss_query_en.setting = ivi.rss_query_en;
  998. vf_trust.setting = ivi.trusted;
  999. vf = nla_nest_start(skb, IFLA_VF_INFO);
  1000. if (!vf)
  1001. goto nla_put_vfinfo_failure;
  1002. if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
  1003. nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
  1004. nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
  1005. &vf_rate) ||
  1006. nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
  1007. &vf_tx_rate) ||
  1008. nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
  1009. &vf_spoofchk) ||
  1010. nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
  1011. &vf_linkstate) ||
  1012. nla_put(skb, IFLA_VF_RSS_QUERY_EN,
  1013. sizeof(vf_rss_query_en),
  1014. &vf_rss_query_en) ||
  1015. nla_put(skb, IFLA_VF_TRUST,
  1016. sizeof(vf_trust), &vf_trust))
  1017. goto nla_put_vf_failure;
  1018. vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
  1019. if (!vfvlanlist)
  1020. goto nla_put_vf_failure;
  1021. if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
  1022. &vf_vlan_info)) {
  1023. nla_nest_cancel(skb, vfvlanlist);
  1024. goto nla_put_vf_failure;
  1025. }
  1026. nla_nest_end(skb, vfvlanlist);
  1027. memset(&vf_stats, 0, sizeof(vf_stats));
  1028. if (dev->netdev_ops->ndo_get_vf_stats)
  1029. dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
  1030. &vf_stats);
  1031. vfstats = nla_nest_start(skb, IFLA_VF_STATS);
  1032. if (!vfstats)
  1033. goto nla_put_vf_failure;
  1034. if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
  1035. vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
  1036. nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
  1037. vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
  1038. nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
  1039. vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
  1040. nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
  1041. vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
  1042. nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
  1043. vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
  1044. nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
  1045. vf_stats.multicast, IFLA_VF_STATS_PAD)) {
  1046. nla_nest_cancel(skb, vfstats);
  1047. goto nla_put_vf_failure;
  1048. }
  1049. nla_nest_end(skb, vfstats);
  1050. nla_nest_end(skb, vf);
  1051. return 0;
  1052. nla_put_vf_failure:
  1053. nla_nest_cancel(skb, vf);
  1054. nla_put_vfinfo_failure:
  1055. nla_nest_cancel(skb, vfinfo);
  1056. return -EMSGSIZE;
  1057. }
  1058. static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
  1059. {
  1060. struct rtnl_link_ifmap map;
  1061. memset(&map, 0, sizeof(map));
  1062. map.mem_start = dev->mem_start;
  1063. map.mem_end = dev->mem_end;
  1064. map.base_addr = dev->base_addr;
  1065. map.irq = dev->irq;
  1066. map.dma = dev->dma;
  1067. map.port = dev->if_port;
  1068. if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
  1069. return -EMSGSIZE;
  1070. return 0;
  1071. }
  1072. static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
  1073. {
  1074. struct netdev_xdp xdp_op = {};
  1075. struct nlattr *xdp;
  1076. int err;
  1077. if (!dev->netdev_ops->ndo_xdp)
  1078. return 0;
  1079. xdp = nla_nest_start(skb, IFLA_XDP);
  1080. if (!xdp)
  1081. return -EMSGSIZE;
  1082. xdp_op.command = XDP_QUERY_PROG;
  1083. err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
  1084. if (err)
  1085. goto err_cancel;
  1086. err = nla_put_u8(skb, IFLA_XDP_ATTACHED, xdp_op.prog_attached);
  1087. if (err)
  1088. goto err_cancel;
  1089. nla_nest_end(skb, xdp);
  1090. return 0;
  1091. err_cancel:
  1092. nla_nest_cancel(skb, xdp);
  1093. return err;
  1094. }
  1095. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  1096. int type, u32 pid, u32 seq, u32 change,
  1097. unsigned int flags, u32 ext_filter_mask)
  1098. {
  1099. struct ifinfomsg *ifm;
  1100. struct nlmsghdr *nlh;
  1101. struct nlattr *af_spec;
  1102. struct rtnl_af_ops *af_ops;
  1103. struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
  1104. ASSERT_RTNL();
  1105. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  1106. if (nlh == NULL)
  1107. return -EMSGSIZE;
  1108. ifm = nlmsg_data(nlh);
  1109. ifm->ifi_family = AF_UNSPEC;
  1110. ifm->__ifi_pad = 0;
  1111. ifm->ifi_type = dev->type;
  1112. ifm->ifi_index = dev->ifindex;
  1113. ifm->ifi_flags = dev_get_flags(dev);
  1114. ifm->ifi_change = change;
  1115. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  1116. nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
  1117. nla_put_u8(skb, IFLA_OPERSTATE,
  1118. netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
  1119. nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
  1120. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  1121. nla_put_u32(skb, IFLA_GROUP, dev->group) ||
  1122. nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
  1123. nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
  1124. nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
  1125. nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
  1126. #ifdef CONFIG_RPS
  1127. nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
  1128. #endif
  1129. (dev->ifindex != dev_get_iflink(dev) &&
  1130. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
  1131. (upper_dev &&
  1132. nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
  1133. nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
  1134. (dev->qdisc &&
  1135. nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
  1136. (dev->ifalias &&
  1137. nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
  1138. nla_put_u32(skb, IFLA_CARRIER_CHANGES,
  1139. atomic_read(&dev->carrier_changes)) ||
  1140. nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
  1141. goto nla_put_failure;
  1142. if (rtnl_fill_link_ifmap(skb, dev))
  1143. goto nla_put_failure;
  1144. if (dev->addr_len) {
  1145. if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
  1146. nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
  1147. goto nla_put_failure;
  1148. }
  1149. if (rtnl_phys_port_id_fill(skb, dev))
  1150. goto nla_put_failure;
  1151. if (rtnl_phys_port_name_fill(skb, dev))
  1152. goto nla_put_failure;
  1153. if (rtnl_phys_switch_id_fill(skb, dev))
  1154. goto nla_put_failure;
  1155. if (rtnl_fill_stats(skb, dev))
  1156. goto nla_put_failure;
  1157. if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
  1158. nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
  1159. goto nla_put_failure;
  1160. if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
  1161. ext_filter_mask & RTEXT_FILTER_VF) {
  1162. int i;
  1163. struct nlattr *vfinfo;
  1164. int num_vfs = dev_num_vf(dev->dev.parent);
  1165. vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
  1166. if (!vfinfo)
  1167. goto nla_put_failure;
  1168. for (i = 0; i < num_vfs; i++) {
  1169. if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
  1170. goto nla_put_failure;
  1171. }
  1172. nla_nest_end(skb, vfinfo);
  1173. }
  1174. if (rtnl_port_fill(skb, dev, ext_filter_mask))
  1175. goto nla_put_failure;
  1176. if (rtnl_xdp_fill(skb, dev))
  1177. goto nla_put_failure;
  1178. if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
  1179. if (rtnl_link_fill(skb, dev) < 0)
  1180. goto nla_put_failure;
  1181. }
  1182. if (dev->rtnl_link_ops &&
  1183. dev->rtnl_link_ops->get_link_net) {
  1184. struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
  1185. if (!net_eq(dev_net(dev), link_net)) {
  1186. int id = peernet2id_alloc(dev_net(dev), link_net);
  1187. if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
  1188. goto nla_put_failure;
  1189. }
  1190. }
  1191. if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
  1192. goto nla_put_failure;
  1193. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  1194. if (af_ops->fill_link_af) {
  1195. struct nlattr *af;
  1196. int err;
  1197. if (!(af = nla_nest_start(skb, af_ops->family)))
  1198. goto nla_put_failure;
  1199. err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
  1200. /*
  1201. * Caller may return ENODATA to indicate that there
  1202. * was no data to be dumped. This is not an error, it
  1203. * means we should trim the attribute header and
  1204. * continue.
  1205. */
  1206. if (err == -ENODATA)
  1207. nla_nest_cancel(skb, af);
  1208. else if (err < 0)
  1209. goto nla_put_failure;
  1210. nla_nest_end(skb, af);
  1211. }
  1212. }
  1213. nla_nest_end(skb, af_spec);
  1214. nlmsg_end(skb, nlh);
  1215. return 0;
  1216. nla_put_failure:
  1217. nlmsg_cancel(skb, nlh);
  1218. return -EMSGSIZE;
  1219. }
  1220. static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
  1221. [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
  1222. [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1223. [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1224. [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
  1225. [IFLA_MTU] = { .type = NLA_U32 },
  1226. [IFLA_LINK] = { .type = NLA_U32 },
  1227. [IFLA_MASTER] = { .type = NLA_U32 },
  1228. [IFLA_CARRIER] = { .type = NLA_U8 },
  1229. [IFLA_TXQLEN] = { .type = NLA_U32 },
  1230. [IFLA_WEIGHT] = { .type = NLA_U32 },
  1231. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  1232. [IFLA_LINKMODE] = { .type = NLA_U8 },
  1233. [IFLA_LINKINFO] = { .type = NLA_NESTED },
  1234. [IFLA_NET_NS_PID] = { .type = NLA_U32 },
  1235. [IFLA_NET_NS_FD] = { .type = NLA_U32 },
  1236. [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
  1237. [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
  1238. [IFLA_VF_PORTS] = { .type = NLA_NESTED },
  1239. [IFLA_PORT_SELF] = { .type = NLA_NESTED },
  1240. [IFLA_AF_SPEC] = { .type = NLA_NESTED },
  1241. [IFLA_EXT_MASK] = { .type = NLA_U32 },
  1242. [IFLA_PROMISCUITY] = { .type = NLA_U32 },
  1243. [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
  1244. [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
  1245. [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
  1246. [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
  1247. [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
  1248. [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
  1249. [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
  1250. [IFLA_XDP] = { .type = NLA_NESTED },
  1251. [IFLA_GROUP] = { .type = NLA_U32 },
  1252. };
  1253. static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
  1254. [IFLA_INFO_KIND] = { .type = NLA_STRING },
  1255. [IFLA_INFO_DATA] = { .type = NLA_NESTED },
  1256. [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
  1257. [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
  1258. };
  1259. static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
  1260. [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
  1261. [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
  1262. [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
  1263. [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
  1264. [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
  1265. [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
  1266. [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
  1267. [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
  1268. [IFLA_VF_STATS] = { .type = NLA_NESTED },
  1269. [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
  1270. [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
  1271. [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
  1272. };
  1273. static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
  1274. [IFLA_PORT_VF] = { .type = NLA_U32 },
  1275. [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
  1276. .len = PORT_PROFILE_MAX },
  1277. [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
  1278. .len = sizeof(struct ifla_port_vsi)},
  1279. [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
  1280. .len = PORT_UUID_MAX },
  1281. [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
  1282. .len = PORT_UUID_MAX },
  1283. [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
  1284. [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
  1285. };
  1286. static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
  1287. [IFLA_XDP_FD] = { .type = NLA_S32 },
  1288. [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
  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 (err < 0) {
  1379. if (likely(skb->len))
  1380. goto out;
  1381. goto out_err;
  1382. }
  1383. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1384. cont:
  1385. idx++;
  1386. }
  1387. }
  1388. out:
  1389. err = skb->len;
  1390. out_err:
  1391. cb->args[1] = idx;
  1392. cb->args[0] = h;
  1393. return err;
  1394. }
  1395. int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
  1396. {
  1397. return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
  1398. }
  1399. EXPORT_SYMBOL(rtnl_nla_parse_ifla);
  1400. struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
  1401. {
  1402. struct net *net;
  1403. /* Examine the link attributes and figure out which
  1404. * network namespace we are talking about.
  1405. */
  1406. if (tb[IFLA_NET_NS_PID])
  1407. net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
  1408. else if (tb[IFLA_NET_NS_FD])
  1409. net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
  1410. else
  1411. net = get_net(src_net);
  1412. return net;
  1413. }
  1414. EXPORT_SYMBOL(rtnl_link_get_net);
  1415. static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
  1416. {
  1417. if (dev) {
  1418. if (tb[IFLA_ADDRESS] &&
  1419. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  1420. return -EINVAL;
  1421. if (tb[IFLA_BROADCAST] &&
  1422. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  1423. return -EINVAL;
  1424. }
  1425. if (tb[IFLA_AF_SPEC]) {
  1426. struct nlattr *af;
  1427. int rem, err;
  1428. nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
  1429. const struct rtnl_af_ops *af_ops;
  1430. if (!(af_ops = rtnl_af_lookup(nla_type(af))))
  1431. return -EAFNOSUPPORT;
  1432. if (!af_ops->set_link_af)
  1433. return -EOPNOTSUPP;
  1434. if (af_ops->validate_link_af) {
  1435. err = af_ops->validate_link_af(dev, af);
  1436. if (err < 0)
  1437. return err;
  1438. }
  1439. }
  1440. }
  1441. return 0;
  1442. }
  1443. static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
  1444. int guid_type)
  1445. {
  1446. const struct net_device_ops *ops = dev->netdev_ops;
  1447. return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
  1448. }
  1449. static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
  1450. {
  1451. if (dev->type != ARPHRD_INFINIBAND)
  1452. return -EOPNOTSUPP;
  1453. return handle_infiniband_guid(dev, ivt, guid_type);
  1454. }
  1455. static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
  1456. {
  1457. const struct net_device_ops *ops = dev->netdev_ops;
  1458. int err = -EINVAL;
  1459. if (tb[IFLA_VF_MAC]) {
  1460. struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
  1461. err = -EOPNOTSUPP;
  1462. if (ops->ndo_set_vf_mac)
  1463. err = ops->ndo_set_vf_mac(dev, ivm->vf,
  1464. ivm->mac);
  1465. if (err < 0)
  1466. return err;
  1467. }
  1468. if (tb[IFLA_VF_VLAN]) {
  1469. struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
  1470. err = -EOPNOTSUPP;
  1471. if (ops->ndo_set_vf_vlan)
  1472. err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
  1473. ivv->qos,
  1474. htons(ETH_P_8021Q));
  1475. if (err < 0)
  1476. return err;
  1477. }
  1478. if (tb[IFLA_VF_VLAN_LIST]) {
  1479. struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
  1480. struct nlattr *attr;
  1481. int rem, len = 0;
  1482. err = -EOPNOTSUPP;
  1483. if (!ops->ndo_set_vf_vlan)
  1484. return err;
  1485. nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
  1486. if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
  1487. nla_len(attr) < NLA_HDRLEN) {
  1488. return -EINVAL;
  1489. }
  1490. if (len >= MAX_VLAN_LIST_LEN)
  1491. return -EOPNOTSUPP;
  1492. ivvl[len] = nla_data(attr);
  1493. len++;
  1494. }
  1495. if (len == 0)
  1496. return -EINVAL;
  1497. err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
  1498. ivvl[0]->qos, ivvl[0]->vlan_proto);
  1499. if (err < 0)
  1500. return err;
  1501. }
  1502. if (tb[IFLA_VF_TX_RATE]) {
  1503. struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
  1504. struct ifla_vf_info ivf;
  1505. err = -EOPNOTSUPP;
  1506. if (ops->ndo_get_vf_config)
  1507. err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
  1508. if (err < 0)
  1509. return err;
  1510. err = -EOPNOTSUPP;
  1511. if (ops->ndo_set_vf_rate)
  1512. err = ops->ndo_set_vf_rate(dev, ivt->vf,
  1513. ivf.min_tx_rate,
  1514. ivt->rate);
  1515. if (err < 0)
  1516. return err;
  1517. }
  1518. if (tb[IFLA_VF_RATE]) {
  1519. struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
  1520. err = -EOPNOTSUPP;
  1521. if (ops->ndo_set_vf_rate)
  1522. err = ops->ndo_set_vf_rate(dev, ivt->vf,
  1523. ivt->min_tx_rate,
  1524. ivt->max_tx_rate);
  1525. if (err < 0)
  1526. return err;
  1527. }
  1528. if (tb[IFLA_VF_SPOOFCHK]) {
  1529. struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
  1530. err = -EOPNOTSUPP;
  1531. if (ops->ndo_set_vf_spoofchk)
  1532. err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
  1533. ivs->setting);
  1534. if (err < 0)
  1535. return err;
  1536. }
  1537. if (tb[IFLA_VF_LINK_STATE]) {
  1538. struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
  1539. err = -EOPNOTSUPP;
  1540. if (ops->ndo_set_vf_link_state)
  1541. err = ops->ndo_set_vf_link_state(dev, ivl->vf,
  1542. ivl->link_state);
  1543. if (err < 0)
  1544. return err;
  1545. }
  1546. if (tb[IFLA_VF_RSS_QUERY_EN]) {
  1547. struct ifla_vf_rss_query_en *ivrssq_en;
  1548. err = -EOPNOTSUPP;
  1549. ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
  1550. if (ops->ndo_set_vf_rss_query_en)
  1551. err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
  1552. ivrssq_en->setting);
  1553. if (err < 0)
  1554. return err;
  1555. }
  1556. if (tb[IFLA_VF_TRUST]) {
  1557. struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
  1558. err = -EOPNOTSUPP;
  1559. if (ops->ndo_set_vf_trust)
  1560. err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
  1561. if (err < 0)
  1562. return err;
  1563. }
  1564. if (tb[IFLA_VF_IB_NODE_GUID]) {
  1565. struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
  1566. if (!ops->ndo_set_vf_guid)
  1567. return -EOPNOTSUPP;
  1568. return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
  1569. }
  1570. if (tb[IFLA_VF_IB_PORT_GUID]) {
  1571. struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
  1572. if (!ops->ndo_set_vf_guid)
  1573. return -EOPNOTSUPP;
  1574. return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
  1575. }
  1576. return err;
  1577. }
  1578. static int do_set_master(struct net_device *dev, int ifindex)
  1579. {
  1580. struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
  1581. const struct net_device_ops *ops;
  1582. int err;
  1583. if (upper_dev) {
  1584. if (upper_dev->ifindex == ifindex)
  1585. return 0;
  1586. ops = upper_dev->netdev_ops;
  1587. if (ops->ndo_del_slave) {
  1588. err = ops->ndo_del_slave(upper_dev, dev);
  1589. if (err)
  1590. return err;
  1591. } else {
  1592. return -EOPNOTSUPP;
  1593. }
  1594. }
  1595. if (ifindex) {
  1596. upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
  1597. if (!upper_dev)
  1598. return -EINVAL;
  1599. ops = upper_dev->netdev_ops;
  1600. if (ops->ndo_add_slave) {
  1601. err = ops->ndo_add_slave(upper_dev, dev);
  1602. if (err)
  1603. return err;
  1604. } else {
  1605. return -EOPNOTSUPP;
  1606. }
  1607. }
  1608. return 0;
  1609. }
  1610. #define DO_SETLINK_MODIFIED 0x01
  1611. /* notify flag means notify + modified. */
  1612. #define DO_SETLINK_NOTIFY 0x03
  1613. static int do_setlink(const struct sk_buff *skb,
  1614. struct net_device *dev, struct ifinfomsg *ifm,
  1615. struct nlattr **tb, char *ifname, int status)
  1616. {
  1617. const struct net_device_ops *ops = dev->netdev_ops;
  1618. int err;
  1619. err = validate_linkmsg(dev, tb);
  1620. if (err < 0)
  1621. return err;
  1622. if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
  1623. struct net *net = rtnl_link_get_net(dev_net(dev), tb);
  1624. if (IS_ERR(net)) {
  1625. err = PTR_ERR(net);
  1626. goto errout;
  1627. }
  1628. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
  1629. put_net(net);
  1630. err = -EPERM;
  1631. goto errout;
  1632. }
  1633. err = dev_change_net_namespace(dev, net, ifname);
  1634. put_net(net);
  1635. if (err)
  1636. goto errout;
  1637. status |= DO_SETLINK_MODIFIED;
  1638. }
  1639. if (tb[IFLA_MAP]) {
  1640. struct rtnl_link_ifmap *u_map;
  1641. struct ifmap k_map;
  1642. if (!ops->ndo_set_config) {
  1643. err = -EOPNOTSUPP;
  1644. goto errout;
  1645. }
  1646. if (!netif_device_present(dev)) {
  1647. err = -ENODEV;
  1648. goto errout;
  1649. }
  1650. u_map = nla_data(tb[IFLA_MAP]);
  1651. k_map.mem_start = (unsigned long) u_map->mem_start;
  1652. k_map.mem_end = (unsigned long) u_map->mem_end;
  1653. k_map.base_addr = (unsigned short) u_map->base_addr;
  1654. k_map.irq = (unsigned char) u_map->irq;
  1655. k_map.dma = (unsigned char) u_map->dma;
  1656. k_map.port = (unsigned char) u_map->port;
  1657. err = ops->ndo_set_config(dev, &k_map);
  1658. if (err < 0)
  1659. goto errout;
  1660. status |= DO_SETLINK_NOTIFY;
  1661. }
  1662. if (tb[IFLA_ADDRESS]) {
  1663. struct sockaddr *sa;
  1664. int len;
  1665. len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
  1666. sizeof(*sa));
  1667. sa = kmalloc(len, GFP_KERNEL);
  1668. if (!sa) {
  1669. err = -ENOMEM;
  1670. goto errout;
  1671. }
  1672. sa->sa_family = dev->type;
  1673. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  1674. dev->addr_len);
  1675. err = dev_set_mac_address(dev, sa);
  1676. kfree(sa);
  1677. if (err)
  1678. goto errout;
  1679. status |= DO_SETLINK_MODIFIED;
  1680. }
  1681. if (tb[IFLA_MTU]) {
  1682. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1683. if (err < 0)
  1684. goto errout;
  1685. status |= DO_SETLINK_MODIFIED;
  1686. }
  1687. if (tb[IFLA_GROUP]) {
  1688. dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
  1689. status |= DO_SETLINK_NOTIFY;
  1690. }
  1691. /*
  1692. * Interface selected by interface index but interface
  1693. * name provided implies that a name change has been
  1694. * requested.
  1695. */
  1696. if (ifm->ifi_index > 0 && ifname[0]) {
  1697. err = dev_change_name(dev, ifname);
  1698. if (err < 0)
  1699. goto errout;
  1700. status |= DO_SETLINK_MODIFIED;
  1701. }
  1702. if (tb[IFLA_IFALIAS]) {
  1703. err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
  1704. nla_len(tb[IFLA_IFALIAS]));
  1705. if (err < 0)
  1706. goto errout;
  1707. status |= DO_SETLINK_NOTIFY;
  1708. }
  1709. if (tb[IFLA_BROADCAST]) {
  1710. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  1711. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  1712. }
  1713. if (ifm->ifi_flags || ifm->ifi_change) {
  1714. err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
  1715. if (err < 0)
  1716. goto errout;
  1717. }
  1718. if (tb[IFLA_MASTER]) {
  1719. err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
  1720. if (err)
  1721. goto errout;
  1722. status |= DO_SETLINK_MODIFIED;
  1723. }
  1724. if (tb[IFLA_CARRIER]) {
  1725. err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
  1726. if (err)
  1727. goto errout;
  1728. status |= DO_SETLINK_MODIFIED;
  1729. }
  1730. if (tb[IFLA_TXQLEN]) {
  1731. unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
  1732. unsigned long orig_len = dev->tx_queue_len;
  1733. if (dev->tx_queue_len ^ value) {
  1734. dev->tx_queue_len = value;
  1735. err = call_netdevice_notifiers(
  1736. NETDEV_CHANGE_TX_QUEUE_LEN, dev);
  1737. err = notifier_to_errno(err);
  1738. if (err) {
  1739. dev->tx_queue_len = orig_len;
  1740. goto errout;
  1741. }
  1742. status |= DO_SETLINK_NOTIFY;
  1743. }
  1744. }
  1745. if (tb[IFLA_OPERSTATE])
  1746. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  1747. if (tb[IFLA_LINKMODE]) {
  1748. unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
  1749. write_lock_bh(&dev_base_lock);
  1750. if (dev->link_mode ^ value)
  1751. status |= DO_SETLINK_NOTIFY;
  1752. dev->link_mode = value;
  1753. write_unlock_bh(&dev_base_lock);
  1754. }
  1755. if (tb[IFLA_VFINFO_LIST]) {
  1756. struct nlattr *vfinfo[IFLA_VF_MAX + 1];
  1757. struct nlattr *attr;
  1758. int rem;
  1759. nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
  1760. if (nla_type(attr) != IFLA_VF_INFO ||
  1761. nla_len(attr) < NLA_HDRLEN) {
  1762. err = -EINVAL;
  1763. goto errout;
  1764. }
  1765. err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
  1766. ifla_vf_policy);
  1767. if (err < 0)
  1768. goto errout;
  1769. err = do_setvfinfo(dev, vfinfo);
  1770. if (err < 0)
  1771. goto errout;
  1772. status |= DO_SETLINK_NOTIFY;
  1773. }
  1774. }
  1775. err = 0;
  1776. if (tb[IFLA_VF_PORTS]) {
  1777. struct nlattr *port[IFLA_PORT_MAX+1];
  1778. struct nlattr *attr;
  1779. int vf;
  1780. int rem;
  1781. err = -EOPNOTSUPP;
  1782. if (!ops->ndo_set_vf_port)
  1783. goto errout;
  1784. nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
  1785. if (nla_type(attr) != IFLA_VF_PORT ||
  1786. nla_len(attr) < NLA_HDRLEN) {
  1787. err = -EINVAL;
  1788. goto errout;
  1789. }
  1790. err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
  1791. ifla_port_policy);
  1792. if (err < 0)
  1793. goto errout;
  1794. if (!port[IFLA_PORT_VF]) {
  1795. err = -EOPNOTSUPP;
  1796. goto errout;
  1797. }
  1798. vf = nla_get_u32(port[IFLA_PORT_VF]);
  1799. err = ops->ndo_set_vf_port(dev, vf, port);
  1800. if (err < 0)
  1801. goto errout;
  1802. status |= DO_SETLINK_NOTIFY;
  1803. }
  1804. }
  1805. err = 0;
  1806. if (tb[IFLA_PORT_SELF]) {
  1807. struct nlattr *port[IFLA_PORT_MAX+1];
  1808. err = nla_parse_nested(port, IFLA_PORT_MAX,
  1809. tb[IFLA_PORT_SELF], ifla_port_policy);
  1810. if (err < 0)
  1811. goto errout;
  1812. err = -EOPNOTSUPP;
  1813. if (ops->ndo_set_vf_port)
  1814. err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
  1815. if (err < 0)
  1816. goto errout;
  1817. status |= DO_SETLINK_NOTIFY;
  1818. }
  1819. if (tb[IFLA_AF_SPEC]) {
  1820. struct nlattr *af;
  1821. int rem;
  1822. nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
  1823. const struct rtnl_af_ops *af_ops;
  1824. if (!(af_ops = rtnl_af_lookup(nla_type(af))))
  1825. BUG();
  1826. err = af_ops->set_link_af(dev, af);
  1827. if (err < 0)
  1828. goto errout;
  1829. status |= DO_SETLINK_NOTIFY;
  1830. }
  1831. }
  1832. err = 0;
  1833. if (tb[IFLA_PROTO_DOWN]) {
  1834. err = dev_change_proto_down(dev,
  1835. nla_get_u8(tb[IFLA_PROTO_DOWN]));
  1836. if (err)
  1837. goto errout;
  1838. status |= DO_SETLINK_NOTIFY;
  1839. }
  1840. if (tb[IFLA_XDP]) {
  1841. struct nlattr *xdp[IFLA_XDP_MAX + 1];
  1842. err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
  1843. ifla_xdp_policy);
  1844. if (err < 0)
  1845. goto errout;
  1846. if (xdp[IFLA_XDP_ATTACHED]) {
  1847. err = -EINVAL;
  1848. goto errout;
  1849. }
  1850. if (xdp[IFLA_XDP_FD]) {
  1851. err = dev_change_xdp_fd(dev,
  1852. nla_get_s32(xdp[IFLA_XDP_FD]));
  1853. if (err)
  1854. goto errout;
  1855. status |= DO_SETLINK_NOTIFY;
  1856. }
  1857. }
  1858. errout:
  1859. if (status & DO_SETLINK_MODIFIED) {
  1860. if (status & DO_SETLINK_NOTIFY)
  1861. netdev_state_change(dev);
  1862. if (err < 0)
  1863. 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",
  1864. dev->name);
  1865. }
  1866. return err;
  1867. }
  1868. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
  1869. {
  1870. struct net *net = sock_net(skb->sk);
  1871. struct ifinfomsg *ifm;
  1872. struct net_device *dev;
  1873. int err;
  1874. struct nlattr *tb[IFLA_MAX+1];
  1875. char ifname[IFNAMSIZ];
  1876. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1877. if (err < 0)
  1878. goto errout;
  1879. if (tb[IFLA_IFNAME])
  1880. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1881. else
  1882. ifname[0] = '\0';
  1883. err = -EINVAL;
  1884. ifm = nlmsg_data(nlh);
  1885. if (ifm->ifi_index > 0)
  1886. dev = __dev_get_by_index(net, ifm->ifi_index);
  1887. else if (tb[IFLA_IFNAME])
  1888. dev = __dev_get_by_name(net, ifname);
  1889. else
  1890. goto errout;
  1891. if (dev == NULL) {
  1892. err = -ENODEV;
  1893. goto errout;
  1894. }
  1895. err = do_setlink(skb, dev, ifm, tb, ifname, 0);
  1896. errout:
  1897. return err;
  1898. }
  1899. static int rtnl_group_dellink(const struct net *net, int group)
  1900. {
  1901. struct net_device *dev, *aux;
  1902. LIST_HEAD(list_kill);
  1903. bool found = false;
  1904. if (!group)
  1905. return -EPERM;
  1906. for_each_netdev(net, dev) {
  1907. if (dev->group == group) {
  1908. const struct rtnl_link_ops *ops;
  1909. found = true;
  1910. ops = dev->rtnl_link_ops;
  1911. if (!ops || !ops->dellink)
  1912. return -EOPNOTSUPP;
  1913. }
  1914. }
  1915. if (!found)
  1916. return -ENODEV;
  1917. for_each_netdev_safe(net, dev, aux) {
  1918. if (dev->group == group) {
  1919. const struct rtnl_link_ops *ops;
  1920. ops = dev->rtnl_link_ops;
  1921. ops->dellink(dev, &list_kill);
  1922. }
  1923. }
  1924. unregister_netdevice_many(&list_kill);
  1925. return 0;
  1926. }
  1927. int rtnl_delete_link(struct net_device *dev)
  1928. {
  1929. const struct rtnl_link_ops *ops;
  1930. LIST_HEAD(list_kill);
  1931. ops = dev->rtnl_link_ops;
  1932. if (!ops || !ops->dellink)
  1933. return -EOPNOTSUPP;
  1934. ops->dellink(dev, &list_kill);
  1935. unregister_netdevice_many(&list_kill);
  1936. return 0;
  1937. }
  1938. EXPORT_SYMBOL_GPL(rtnl_delete_link);
  1939. static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
  1940. {
  1941. struct net *net = sock_net(skb->sk);
  1942. struct net_device *dev;
  1943. struct ifinfomsg *ifm;
  1944. char ifname[IFNAMSIZ];
  1945. struct nlattr *tb[IFLA_MAX+1];
  1946. int err;
  1947. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1948. if (err < 0)
  1949. return err;
  1950. if (tb[IFLA_IFNAME])
  1951. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1952. ifm = nlmsg_data(nlh);
  1953. if (ifm->ifi_index > 0)
  1954. dev = __dev_get_by_index(net, ifm->ifi_index);
  1955. else if (tb[IFLA_IFNAME])
  1956. dev = __dev_get_by_name(net, ifname);
  1957. else if (tb[IFLA_GROUP])
  1958. return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
  1959. else
  1960. return -EINVAL;
  1961. if (!dev)
  1962. return -ENODEV;
  1963. return rtnl_delete_link(dev);
  1964. }
  1965. int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
  1966. {
  1967. unsigned int old_flags;
  1968. int err;
  1969. old_flags = dev->flags;
  1970. if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
  1971. err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
  1972. if (err < 0)
  1973. return err;
  1974. }
  1975. if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
  1976. __dev_notify_flags(dev, old_flags, 0U);
  1977. } else {
  1978. dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
  1979. __dev_notify_flags(dev, old_flags, ~0U);
  1980. }
  1981. return 0;
  1982. }
  1983. EXPORT_SYMBOL(rtnl_configure_link);
  1984. struct net_device *rtnl_create_link(struct net *net,
  1985. const char *ifname, unsigned char name_assign_type,
  1986. const struct rtnl_link_ops *ops, struct nlattr *tb[])
  1987. {
  1988. int err;
  1989. struct net_device *dev;
  1990. unsigned int num_tx_queues = 1;
  1991. unsigned int num_rx_queues = 1;
  1992. if (tb[IFLA_NUM_TX_QUEUES])
  1993. num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
  1994. else if (ops->get_num_tx_queues)
  1995. num_tx_queues = ops->get_num_tx_queues();
  1996. if (tb[IFLA_NUM_RX_QUEUES])
  1997. num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
  1998. else if (ops->get_num_rx_queues)
  1999. num_rx_queues = ops->get_num_rx_queues();
  2000. err = -ENOMEM;
  2001. dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
  2002. ops->setup, num_tx_queues, num_rx_queues);
  2003. if (!dev)
  2004. goto err;
  2005. dev_net_set(dev, net);
  2006. dev->rtnl_link_ops = ops;
  2007. dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
  2008. if (tb[IFLA_MTU])
  2009. dev->mtu = nla_get_u32(tb[IFLA_MTU]);
  2010. if (tb[IFLA_ADDRESS]) {
  2011. memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
  2012. nla_len(tb[IFLA_ADDRESS]));
  2013. dev->addr_assign_type = NET_ADDR_SET;
  2014. }
  2015. if (tb[IFLA_BROADCAST])
  2016. memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
  2017. nla_len(tb[IFLA_BROADCAST]));
  2018. if (tb[IFLA_TXQLEN])
  2019. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  2020. if (tb[IFLA_OPERSTATE])
  2021. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  2022. if (tb[IFLA_LINKMODE])
  2023. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  2024. if (tb[IFLA_GROUP])
  2025. dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
  2026. return dev;
  2027. err:
  2028. return ERR_PTR(err);
  2029. }
  2030. EXPORT_SYMBOL(rtnl_create_link);
  2031. static int rtnl_group_changelink(const struct sk_buff *skb,
  2032. struct net *net, int group,
  2033. struct ifinfomsg *ifm,
  2034. struct nlattr **tb)
  2035. {
  2036. struct net_device *dev, *aux;
  2037. int err;
  2038. for_each_netdev_safe(net, dev, aux) {
  2039. if (dev->group == group) {
  2040. err = do_setlink(skb, dev, ifm, tb, NULL, 0);
  2041. if (err < 0)
  2042. return err;
  2043. }
  2044. }
  2045. return 0;
  2046. }
  2047. static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
  2048. {
  2049. struct net *net = sock_net(skb->sk);
  2050. const struct rtnl_link_ops *ops;
  2051. const struct rtnl_link_ops *m_ops = NULL;
  2052. struct net_device *dev;
  2053. struct net_device *master_dev = NULL;
  2054. struct ifinfomsg *ifm;
  2055. char kind[MODULE_NAME_LEN];
  2056. char ifname[IFNAMSIZ];
  2057. struct nlattr *tb[IFLA_MAX+1];
  2058. struct nlattr *linkinfo[IFLA_INFO_MAX+1];
  2059. unsigned char name_assign_type = NET_NAME_USER;
  2060. int err;
  2061. #ifdef CONFIG_MODULES
  2062. replay:
  2063. #endif
  2064. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  2065. if (err < 0)
  2066. return err;
  2067. if (tb[IFLA_IFNAME])
  2068. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  2069. else
  2070. ifname[0] = '\0';
  2071. ifm = nlmsg_data(nlh);
  2072. if (ifm->ifi_index > 0)
  2073. dev = __dev_get_by_index(net, ifm->ifi_index);
  2074. else {
  2075. if (ifname[0])
  2076. dev = __dev_get_by_name(net, ifname);
  2077. else
  2078. dev = NULL;
  2079. }
  2080. if (dev) {
  2081. master_dev = netdev_master_upper_dev_get(dev);
  2082. if (master_dev)
  2083. m_ops = master_dev->rtnl_link_ops;
  2084. }
  2085. err = validate_linkmsg(dev, tb);
  2086. if (err < 0)
  2087. return err;
  2088. if (tb[IFLA_LINKINFO]) {
  2089. err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
  2090. tb[IFLA_LINKINFO], ifla_info_policy);
  2091. if (err < 0)
  2092. return err;
  2093. } else
  2094. memset(linkinfo, 0, sizeof(linkinfo));
  2095. if (linkinfo[IFLA_INFO_KIND]) {
  2096. nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
  2097. ops = rtnl_link_ops_get(kind);
  2098. } else {
  2099. kind[0] = '\0';
  2100. ops = NULL;
  2101. }
  2102. if (1) {
  2103. struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
  2104. struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
  2105. struct nlattr **data = NULL;
  2106. struct nlattr **slave_data = NULL;
  2107. struct net *dest_net, *link_net = NULL;
  2108. if (ops) {
  2109. if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
  2110. err = nla_parse_nested(attr, ops->maxtype,
  2111. linkinfo[IFLA_INFO_DATA],
  2112. ops->policy);
  2113. if (err < 0)
  2114. return err;
  2115. data = attr;
  2116. }
  2117. if (ops->validate) {
  2118. err = ops->validate(tb, data);
  2119. if (err < 0)
  2120. return err;
  2121. }
  2122. }
  2123. if (m_ops) {
  2124. if (m_ops->slave_maxtype &&
  2125. linkinfo[IFLA_INFO_SLAVE_DATA]) {
  2126. err = nla_parse_nested(slave_attr,
  2127. m_ops->slave_maxtype,
  2128. linkinfo[IFLA_INFO_SLAVE_DATA],
  2129. m_ops->slave_policy);
  2130. if (err < 0)
  2131. return err;
  2132. slave_data = slave_attr;
  2133. }
  2134. if (m_ops->slave_validate) {
  2135. err = m_ops->slave_validate(tb, slave_data);
  2136. if (err < 0)
  2137. return err;
  2138. }
  2139. }
  2140. if (dev) {
  2141. int status = 0;
  2142. if (nlh->nlmsg_flags & NLM_F_EXCL)
  2143. return -EEXIST;
  2144. if (nlh->nlmsg_flags & NLM_F_REPLACE)
  2145. return -EOPNOTSUPP;
  2146. if (linkinfo[IFLA_INFO_DATA]) {
  2147. if (!ops || ops != dev->rtnl_link_ops ||
  2148. !ops->changelink)
  2149. return -EOPNOTSUPP;
  2150. err = ops->changelink(dev, tb, data);
  2151. if (err < 0)
  2152. return err;
  2153. status |= DO_SETLINK_NOTIFY;
  2154. }
  2155. if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
  2156. if (!m_ops || !m_ops->slave_changelink)
  2157. return -EOPNOTSUPP;
  2158. err = m_ops->slave_changelink(master_dev, dev,
  2159. tb, slave_data);
  2160. if (err < 0)
  2161. return err;
  2162. status |= DO_SETLINK_NOTIFY;
  2163. }
  2164. return do_setlink(skb, dev, ifm, tb, ifname, status);
  2165. }
  2166. if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
  2167. if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
  2168. return rtnl_group_changelink(skb, net,
  2169. nla_get_u32(tb[IFLA_GROUP]),
  2170. ifm, tb);
  2171. return -ENODEV;
  2172. }
  2173. if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
  2174. return -EOPNOTSUPP;
  2175. if (!ops) {
  2176. #ifdef CONFIG_MODULES
  2177. if (kind[0]) {
  2178. __rtnl_unlock();
  2179. request_module("rtnl-link-%s", kind);
  2180. rtnl_lock();
  2181. ops = rtnl_link_ops_get(kind);
  2182. if (ops)
  2183. goto replay;
  2184. }
  2185. #endif
  2186. return -EOPNOTSUPP;
  2187. }
  2188. if (!ops->setup)
  2189. return -EOPNOTSUPP;
  2190. if (!ifname[0]) {
  2191. snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
  2192. name_assign_type = NET_NAME_ENUM;
  2193. }
  2194. dest_net = rtnl_link_get_net(net, tb);
  2195. if (IS_ERR(dest_net))
  2196. return PTR_ERR(dest_net);
  2197. err = -EPERM;
  2198. if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
  2199. goto out;
  2200. if (tb[IFLA_LINK_NETNSID]) {
  2201. int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
  2202. link_net = get_net_ns_by_id(dest_net, id);
  2203. if (!link_net) {
  2204. err = -EINVAL;
  2205. goto out;
  2206. }
  2207. err = -EPERM;
  2208. if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
  2209. goto out;
  2210. }
  2211. dev = rtnl_create_link(link_net ? : dest_net, ifname,
  2212. name_assign_type, ops, tb);
  2213. if (IS_ERR(dev)) {
  2214. err = PTR_ERR(dev);
  2215. goto out;
  2216. }
  2217. dev->ifindex = ifm->ifi_index;
  2218. if (ops->newlink) {
  2219. err = ops->newlink(link_net ? : net, dev, tb, data);
  2220. /* Drivers should call free_netdev() in ->destructor
  2221. * and unregister it on failure after registration
  2222. * so that device could be finally freed in rtnl_unlock.
  2223. */
  2224. if (err < 0) {
  2225. /* If device is not registered at all, free it now */
  2226. if (dev->reg_state == NETREG_UNINITIALIZED)
  2227. free_netdev(dev);
  2228. goto out;
  2229. }
  2230. } else {
  2231. err = register_netdevice(dev);
  2232. if (err < 0) {
  2233. free_netdev(dev);
  2234. goto out;
  2235. }
  2236. }
  2237. err = rtnl_configure_link(dev, ifm);
  2238. if (err < 0)
  2239. goto out_unregister;
  2240. if (link_net) {
  2241. err = dev_change_net_namespace(dev, dest_net, ifname);
  2242. if (err < 0)
  2243. goto out_unregister;
  2244. }
  2245. out:
  2246. if (link_net)
  2247. put_net(link_net);
  2248. put_net(dest_net);
  2249. return err;
  2250. out_unregister:
  2251. if (ops->newlink) {
  2252. LIST_HEAD(list_kill);
  2253. ops->dellink(dev, &list_kill);
  2254. unregister_netdevice_many(&list_kill);
  2255. } else {
  2256. unregister_netdevice(dev);
  2257. }
  2258. goto out;
  2259. }
  2260. }
  2261. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
  2262. {
  2263. struct net *net = sock_net(skb->sk);
  2264. struct ifinfomsg *ifm;
  2265. char ifname[IFNAMSIZ];
  2266. struct nlattr *tb[IFLA_MAX+1];
  2267. struct net_device *dev = NULL;
  2268. struct sk_buff *nskb;
  2269. int err;
  2270. u32 ext_filter_mask = 0;
  2271. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  2272. if (err < 0)
  2273. return err;
  2274. if (tb[IFLA_IFNAME])
  2275. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  2276. if (tb[IFLA_EXT_MASK])
  2277. ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
  2278. ifm = nlmsg_data(nlh);
  2279. if (ifm->ifi_index > 0)
  2280. dev = __dev_get_by_index(net, ifm->ifi_index);
  2281. else if (tb[IFLA_IFNAME])
  2282. dev = __dev_get_by_name(net, ifname);
  2283. else
  2284. return -EINVAL;
  2285. if (dev == NULL)
  2286. return -ENODEV;
  2287. nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
  2288. if (nskb == NULL)
  2289. return -ENOBUFS;
  2290. err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
  2291. nlh->nlmsg_seq, 0, 0, ext_filter_mask);
  2292. if (err < 0) {
  2293. /* -EMSGSIZE implies BUG in if_nlmsg_size */
  2294. WARN_ON(err == -EMSGSIZE);
  2295. kfree_skb(nskb);
  2296. } else
  2297. err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
  2298. return err;
  2299. }
  2300. static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
  2301. {
  2302. struct net *net = sock_net(skb->sk);
  2303. struct net_device *dev;
  2304. struct nlattr *tb[IFLA_MAX+1];
  2305. u32 ext_filter_mask = 0;
  2306. u16 min_ifinfo_dump_size = 0;
  2307. int hdrlen;
  2308. /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
  2309. hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
  2310. sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
  2311. if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
  2312. if (tb[IFLA_EXT_MASK])
  2313. ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
  2314. }
  2315. if (!ext_filter_mask)
  2316. return NLMSG_GOODSIZE;
  2317. /*
  2318. * traverse the list of net devices and compute the minimum
  2319. * buffer size based upon the filter mask.
  2320. */
  2321. list_for_each_entry(dev, &net->dev_base_head, dev_list) {
  2322. min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
  2323. if_nlmsg_size(dev,
  2324. ext_filter_mask));
  2325. }
  2326. return nlmsg_total_size(min_ifinfo_dump_size);
  2327. }
  2328. static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  2329. {
  2330. int idx;
  2331. int s_idx = cb->family;
  2332. if (s_idx == 0)
  2333. s_idx = 1;
  2334. for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
  2335. int type = cb->nlh->nlmsg_type-RTM_BASE;
  2336. if (idx < s_idx || idx == PF_PACKET)
  2337. continue;
  2338. if (rtnl_msg_handlers[idx] == NULL ||
  2339. rtnl_msg_handlers[idx][type].dumpit == NULL)
  2340. continue;
  2341. if (idx > s_idx) {
  2342. memset(&cb->args[0], 0, sizeof(cb->args));
  2343. cb->prev_seq = 0;
  2344. cb->seq = 0;
  2345. }
  2346. if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
  2347. break;
  2348. }
  2349. cb->family = idx;
  2350. return skb->len;
  2351. }
  2352. struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
  2353. unsigned int change, gfp_t flags)
  2354. {
  2355. struct net *net = dev_net(dev);
  2356. struct sk_buff *skb;
  2357. int err = -ENOBUFS;
  2358. size_t if_info_size;
  2359. skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
  2360. if (skb == NULL)
  2361. goto errout;
  2362. err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
  2363. if (err < 0) {
  2364. /* -EMSGSIZE implies BUG in if_nlmsg_size() */
  2365. WARN_ON(err == -EMSGSIZE);
  2366. kfree_skb(skb);
  2367. goto errout;
  2368. }
  2369. return skb;
  2370. errout:
  2371. if (err < 0)
  2372. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  2373. return NULL;
  2374. }
  2375. void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
  2376. {
  2377. struct net *net = dev_net(dev);
  2378. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
  2379. }
  2380. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
  2381. gfp_t flags)
  2382. {
  2383. struct sk_buff *skb;
  2384. if (dev->reg_state != NETREG_REGISTERED)
  2385. return;
  2386. skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
  2387. if (skb)
  2388. rtmsg_ifinfo_send(skb, dev, flags);
  2389. }
  2390. EXPORT_SYMBOL(rtmsg_ifinfo);
  2391. static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
  2392. struct net_device *dev,
  2393. u8 *addr, u16 vid, u32 pid, u32 seq,
  2394. int type, unsigned int flags,
  2395. int nlflags, u16 ndm_state)
  2396. {
  2397. struct nlmsghdr *nlh;
  2398. struct ndmsg *ndm;
  2399. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
  2400. if (!nlh)
  2401. return -EMSGSIZE;
  2402. ndm = nlmsg_data(nlh);
  2403. ndm->ndm_family = AF_BRIDGE;
  2404. ndm->ndm_pad1 = 0;
  2405. ndm->ndm_pad2 = 0;
  2406. ndm->ndm_flags = flags;
  2407. ndm->ndm_type = 0;
  2408. ndm->ndm_ifindex = dev->ifindex;
  2409. ndm->ndm_state = ndm_state;
  2410. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
  2411. goto nla_put_failure;
  2412. if (vid)
  2413. if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
  2414. goto nla_put_failure;
  2415. nlmsg_end(skb, nlh);
  2416. return 0;
  2417. nla_put_failure:
  2418. nlmsg_cancel(skb, nlh);
  2419. return -EMSGSIZE;
  2420. }
  2421. static inline size_t rtnl_fdb_nlmsg_size(void)
  2422. {
  2423. return NLMSG_ALIGN(sizeof(struct ndmsg)) +
  2424. nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
  2425. nla_total_size(sizeof(u16)) + /* NDA_VLAN */
  2426. 0;
  2427. }
  2428. static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
  2429. u16 ndm_state)
  2430. {
  2431. struct net *net = dev_net(dev);
  2432. struct sk_buff *skb;
  2433. int err = -ENOBUFS;
  2434. skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
  2435. if (!skb)
  2436. goto errout;
  2437. err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
  2438. 0, 0, type, NTF_SELF, 0, ndm_state);
  2439. if (err < 0) {
  2440. kfree_skb(skb);
  2441. goto errout;
  2442. }
  2443. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  2444. return;
  2445. errout:
  2446. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  2447. }
  2448. /**
  2449. * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
  2450. */
  2451. int ndo_dflt_fdb_add(struct ndmsg *ndm,
  2452. struct nlattr *tb[],
  2453. struct net_device *dev,
  2454. const unsigned char *addr, u16 vid,
  2455. u16 flags)
  2456. {
  2457. int err = -EINVAL;
  2458. /* If aging addresses are supported device will need to
  2459. * implement its own handler for this.
  2460. */
  2461. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  2462. pr_info("%s: FDB only supports static addresses\n", dev->name);
  2463. return err;
  2464. }
  2465. if (vid) {
  2466. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  2467. return err;
  2468. }
  2469. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  2470. err = dev_uc_add_excl(dev, addr);
  2471. else if (is_multicast_ether_addr(addr))
  2472. err = dev_mc_add_excl(dev, addr);
  2473. /* Only return duplicate errors if NLM_F_EXCL is set */
  2474. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  2475. err = 0;
  2476. return err;
  2477. }
  2478. EXPORT_SYMBOL(ndo_dflt_fdb_add);
  2479. static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
  2480. {
  2481. u16 vid = 0;
  2482. if (vlan_attr) {
  2483. if (nla_len(vlan_attr) != sizeof(u16)) {
  2484. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
  2485. return -EINVAL;
  2486. }
  2487. vid = nla_get_u16(vlan_attr);
  2488. if (!vid || vid >= VLAN_VID_MASK) {
  2489. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
  2490. vid);
  2491. return -EINVAL;
  2492. }
  2493. }
  2494. *p_vid = vid;
  2495. return 0;
  2496. }
  2497. static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
  2498. {
  2499. struct net *net = sock_net(skb->sk);
  2500. struct ndmsg *ndm;
  2501. struct nlattr *tb[NDA_MAX+1];
  2502. struct net_device *dev;
  2503. u8 *addr;
  2504. u16 vid;
  2505. int err;
  2506. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  2507. if (err < 0)
  2508. return err;
  2509. ndm = nlmsg_data(nlh);
  2510. if (ndm->ndm_ifindex == 0) {
  2511. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
  2512. return -EINVAL;
  2513. }
  2514. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  2515. if (dev == NULL) {
  2516. pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
  2517. return -ENODEV;
  2518. }
  2519. if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
  2520. pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
  2521. return -EINVAL;
  2522. }
  2523. addr = nla_data(tb[NDA_LLADDR]);
  2524. err = fdb_vid_parse(tb[NDA_VLAN], &vid);
  2525. if (err)
  2526. return err;
  2527. err = -EOPNOTSUPP;
  2528. /* Support fdb on master device the net/bridge default case */
  2529. if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
  2530. (dev->priv_flags & IFF_BRIDGE_PORT)) {
  2531. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2532. const struct net_device_ops *ops = br_dev->netdev_ops;
  2533. err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
  2534. nlh->nlmsg_flags);
  2535. if (err)
  2536. goto out;
  2537. else
  2538. ndm->ndm_flags &= ~NTF_MASTER;
  2539. }
  2540. /* Embedded bridge, macvlan, and any other device support */
  2541. if ((ndm->ndm_flags & NTF_SELF)) {
  2542. if (dev->netdev_ops->ndo_fdb_add)
  2543. err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
  2544. vid,
  2545. nlh->nlmsg_flags);
  2546. else
  2547. err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
  2548. nlh->nlmsg_flags);
  2549. if (!err) {
  2550. rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
  2551. ndm->ndm_state);
  2552. ndm->ndm_flags &= ~NTF_SELF;
  2553. }
  2554. }
  2555. out:
  2556. return err;
  2557. }
  2558. /**
  2559. * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
  2560. */
  2561. int ndo_dflt_fdb_del(struct ndmsg *ndm,
  2562. struct nlattr *tb[],
  2563. struct net_device *dev,
  2564. const unsigned char *addr, u16 vid)
  2565. {
  2566. int err = -EINVAL;
  2567. /* If aging addresses are supported device will need to
  2568. * implement its own handler for this.
  2569. */
  2570. if (!(ndm->ndm_state & NUD_PERMANENT)) {
  2571. pr_info("%s: FDB only supports static addresses\n", dev->name);
  2572. return err;
  2573. }
  2574. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  2575. err = dev_uc_del(dev, addr);
  2576. else if (is_multicast_ether_addr(addr))
  2577. err = dev_mc_del(dev, addr);
  2578. return err;
  2579. }
  2580. EXPORT_SYMBOL(ndo_dflt_fdb_del);
  2581. static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
  2582. {
  2583. struct net *net = sock_net(skb->sk);
  2584. struct ndmsg *ndm;
  2585. struct nlattr *tb[NDA_MAX+1];
  2586. struct net_device *dev;
  2587. int err = -EINVAL;
  2588. __u8 *addr;
  2589. u16 vid;
  2590. if (!netlink_capable(skb, CAP_NET_ADMIN))
  2591. return -EPERM;
  2592. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  2593. if (err < 0)
  2594. return err;
  2595. ndm = nlmsg_data(nlh);
  2596. if (ndm->ndm_ifindex == 0) {
  2597. pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
  2598. return -EINVAL;
  2599. }
  2600. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  2601. if (dev == NULL) {
  2602. pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
  2603. return -ENODEV;
  2604. }
  2605. if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
  2606. pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
  2607. return -EINVAL;
  2608. }
  2609. addr = nla_data(tb[NDA_LLADDR]);
  2610. err = fdb_vid_parse(tb[NDA_VLAN], &vid);
  2611. if (err)
  2612. return err;
  2613. err = -EOPNOTSUPP;
  2614. /* Support fdb on master device the net/bridge default case */
  2615. if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
  2616. (dev->priv_flags & IFF_BRIDGE_PORT)) {
  2617. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2618. const struct net_device_ops *ops = br_dev->netdev_ops;
  2619. if (ops->ndo_fdb_del)
  2620. err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
  2621. if (err)
  2622. goto out;
  2623. else
  2624. ndm->ndm_flags &= ~NTF_MASTER;
  2625. }
  2626. /* Embedded bridge, macvlan, and any other device support */
  2627. if (ndm->ndm_flags & NTF_SELF) {
  2628. if (dev->netdev_ops->ndo_fdb_del)
  2629. err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
  2630. vid);
  2631. else
  2632. err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
  2633. if (!err) {
  2634. rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
  2635. ndm->ndm_state);
  2636. ndm->ndm_flags &= ~NTF_SELF;
  2637. }
  2638. }
  2639. out:
  2640. return err;
  2641. }
  2642. static int nlmsg_populate_fdb(struct sk_buff *skb,
  2643. struct netlink_callback *cb,
  2644. struct net_device *dev,
  2645. int *idx,
  2646. struct netdev_hw_addr_list *list)
  2647. {
  2648. struct netdev_hw_addr *ha;
  2649. int err;
  2650. u32 portid, seq;
  2651. portid = NETLINK_CB(cb->skb).portid;
  2652. seq = cb->nlh->nlmsg_seq;
  2653. list_for_each_entry(ha, &list->list, list) {
  2654. if (*idx < cb->args[2])
  2655. goto skip;
  2656. err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
  2657. portid, seq,
  2658. RTM_NEWNEIGH, NTF_SELF,
  2659. NLM_F_MULTI, NUD_PERMANENT);
  2660. if (err < 0)
  2661. return err;
  2662. skip:
  2663. *idx += 1;
  2664. }
  2665. return 0;
  2666. }
  2667. /**
  2668. * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
  2669. * @nlh: netlink message header
  2670. * @dev: netdevice
  2671. *
  2672. * Default netdevice operation to dump the existing unicast address list.
  2673. * Returns number of addresses from list put in skb.
  2674. */
  2675. int ndo_dflt_fdb_dump(struct sk_buff *skb,
  2676. struct netlink_callback *cb,
  2677. struct net_device *dev,
  2678. struct net_device *filter_dev,
  2679. int *idx)
  2680. {
  2681. int err;
  2682. netif_addr_lock_bh(dev);
  2683. err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
  2684. if (err)
  2685. goto out;
  2686. nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
  2687. out:
  2688. netif_addr_unlock_bh(dev);
  2689. return err;
  2690. }
  2691. EXPORT_SYMBOL(ndo_dflt_fdb_dump);
  2692. static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2693. {
  2694. struct net_device *dev;
  2695. struct nlattr *tb[IFLA_MAX+1];
  2696. struct net_device *br_dev = NULL;
  2697. const struct net_device_ops *ops = NULL;
  2698. const struct net_device_ops *cops = NULL;
  2699. struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
  2700. struct net *net = sock_net(skb->sk);
  2701. struct hlist_head *head;
  2702. int brport_idx = 0;
  2703. int br_idx = 0;
  2704. int h, s_h;
  2705. int idx = 0, s_idx;
  2706. int err = 0;
  2707. int fidx = 0;
  2708. if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
  2709. ifla_policy) == 0) {
  2710. if (tb[IFLA_MASTER])
  2711. br_idx = nla_get_u32(tb[IFLA_MASTER]);
  2712. }
  2713. brport_idx = ifm->ifi_index;
  2714. if (br_idx) {
  2715. br_dev = __dev_get_by_index(net, br_idx);
  2716. if (!br_dev)
  2717. return -ENODEV;
  2718. ops = br_dev->netdev_ops;
  2719. }
  2720. s_h = cb->args[0];
  2721. s_idx = cb->args[1];
  2722. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  2723. idx = 0;
  2724. head = &net->dev_index_head[h];
  2725. hlist_for_each_entry(dev, head, index_hlist) {
  2726. if (brport_idx && (dev->ifindex != brport_idx))
  2727. continue;
  2728. if (!br_idx) { /* user did not specify a specific bridge */
  2729. if (dev->priv_flags & IFF_BRIDGE_PORT) {
  2730. br_dev = netdev_master_upper_dev_get(dev);
  2731. cops = br_dev->netdev_ops;
  2732. }
  2733. } else {
  2734. if (dev != br_dev &&
  2735. !(dev->priv_flags & IFF_BRIDGE_PORT))
  2736. continue;
  2737. if (br_dev != netdev_master_upper_dev_get(dev) &&
  2738. !(dev->priv_flags & IFF_EBRIDGE))
  2739. continue;
  2740. cops = ops;
  2741. }
  2742. if (idx < s_idx)
  2743. goto cont;
  2744. if (dev->priv_flags & IFF_BRIDGE_PORT) {
  2745. if (cops && cops->ndo_fdb_dump) {
  2746. err = cops->ndo_fdb_dump(skb, cb,
  2747. br_dev, dev,
  2748. &fidx);
  2749. if (err == -EMSGSIZE)
  2750. goto out;
  2751. }
  2752. }
  2753. if (dev->netdev_ops->ndo_fdb_dump)
  2754. err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
  2755. dev, NULL,
  2756. &fidx);
  2757. else
  2758. err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
  2759. &fidx);
  2760. if (err == -EMSGSIZE)
  2761. goto out;
  2762. cops = NULL;
  2763. /* reset fdb offset to 0 for rest of the interfaces */
  2764. cb->args[2] = 0;
  2765. fidx = 0;
  2766. cont:
  2767. idx++;
  2768. }
  2769. }
  2770. out:
  2771. cb->args[0] = h;
  2772. cb->args[1] = idx;
  2773. cb->args[2] = fidx;
  2774. return skb->len;
  2775. }
  2776. static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
  2777. unsigned int attrnum, unsigned int flag)
  2778. {
  2779. if (mask & flag)
  2780. return nla_put_u8(skb, attrnum, !!(flags & flag));
  2781. return 0;
  2782. }
  2783. int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  2784. struct net_device *dev, u16 mode,
  2785. u32 flags, u32 mask, int nlflags,
  2786. u32 filter_mask,
  2787. int (*vlan_fill)(struct sk_buff *skb,
  2788. struct net_device *dev,
  2789. u32 filter_mask))
  2790. {
  2791. struct nlmsghdr *nlh;
  2792. struct ifinfomsg *ifm;
  2793. struct nlattr *br_afspec;
  2794. struct nlattr *protinfo;
  2795. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  2796. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2797. int err = 0;
  2798. nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
  2799. if (nlh == NULL)
  2800. return -EMSGSIZE;
  2801. ifm = nlmsg_data(nlh);
  2802. ifm->ifi_family = AF_BRIDGE;
  2803. ifm->__ifi_pad = 0;
  2804. ifm->ifi_type = dev->type;
  2805. ifm->ifi_index = dev->ifindex;
  2806. ifm->ifi_flags = dev_get_flags(dev);
  2807. ifm->ifi_change = 0;
  2808. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  2809. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  2810. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  2811. (br_dev &&
  2812. nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
  2813. (dev->addr_len &&
  2814. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  2815. (dev->ifindex != dev_get_iflink(dev) &&
  2816. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
  2817. goto nla_put_failure;
  2818. br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
  2819. if (!br_afspec)
  2820. goto nla_put_failure;
  2821. if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
  2822. nla_nest_cancel(skb, br_afspec);
  2823. goto nla_put_failure;
  2824. }
  2825. if (mode != BRIDGE_MODE_UNDEF) {
  2826. if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
  2827. nla_nest_cancel(skb, br_afspec);
  2828. goto nla_put_failure;
  2829. }
  2830. }
  2831. if (vlan_fill) {
  2832. err = vlan_fill(skb, dev, filter_mask);
  2833. if (err) {
  2834. nla_nest_cancel(skb, br_afspec);
  2835. goto nla_put_failure;
  2836. }
  2837. }
  2838. nla_nest_end(skb, br_afspec);
  2839. protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  2840. if (!protinfo)
  2841. goto nla_put_failure;
  2842. if (brport_nla_put_flag(skb, flags, mask,
  2843. IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
  2844. brport_nla_put_flag(skb, flags, mask,
  2845. IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
  2846. brport_nla_put_flag(skb, flags, mask,
  2847. IFLA_BRPORT_FAST_LEAVE,
  2848. BR_MULTICAST_FAST_LEAVE) ||
  2849. brport_nla_put_flag(skb, flags, mask,
  2850. IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
  2851. brport_nla_put_flag(skb, flags, mask,
  2852. IFLA_BRPORT_LEARNING, BR_LEARNING) ||
  2853. brport_nla_put_flag(skb, flags, mask,
  2854. IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
  2855. brport_nla_put_flag(skb, flags, mask,
  2856. IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
  2857. brport_nla_put_flag(skb, flags, mask,
  2858. IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
  2859. nla_nest_cancel(skb, protinfo);
  2860. goto nla_put_failure;
  2861. }
  2862. nla_nest_end(skb, protinfo);
  2863. nlmsg_end(skb, nlh);
  2864. return 0;
  2865. nla_put_failure:
  2866. nlmsg_cancel(skb, nlh);
  2867. return err ? err : -EMSGSIZE;
  2868. }
  2869. EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
  2870. static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
  2871. {
  2872. struct net *net = sock_net(skb->sk);
  2873. struct net_device *dev;
  2874. int idx = 0;
  2875. u32 portid = NETLINK_CB(cb->skb).portid;
  2876. u32 seq = cb->nlh->nlmsg_seq;
  2877. u32 filter_mask = 0;
  2878. int err;
  2879. if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
  2880. struct nlattr *extfilt;
  2881. extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
  2882. IFLA_EXT_MASK);
  2883. if (extfilt) {
  2884. if (nla_len(extfilt) < sizeof(filter_mask))
  2885. return -EINVAL;
  2886. filter_mask = nla_get_u32(extfilt);
  2887. }
  2888. }
  2889. rcu_read_lock();
  2890. for_each_netdev_rcu(net, dev) {
  2891. const struct net_device_ops *ops = dev->netdev_ops;
  2892. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  2893. if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
  2894. if (idx >= cb->args[0]) {
  2895. err = br_dev->netdev_ops->ndo_bridge_getlink(
  2896. skb, portid, seq, dev,
  2897. filter_mask, NLM_F_MULTI);
  2898. if (err < 0 && err != -EOPNOTSUPP) {
  2899. if (likely(skb->len))
  2900. break;
  2901. goto out_err;
  2902. }
  2903. }
  2904. idx++;
  2905. }
  2906. if (ops->ndo_bridge_getlink) {
  2907. if (idx >= cb->args[0]) {
  2908. err = ops->ndo_bridge_getlink(skb, portid,
  2909. seq, dev,
  2910. filter_mask,
  2911. NLM_F_MULTI);
  2912. if (err < 0 && err != -EOPNOTSUPP) {
  2913. if (likely(skb->len))
  2914. break;
  2915. goto out_err;
  2916. }
  2917. }
  2918. idx++;
  2919. }
  2920. }
  2921. err = skb->len;
  2922. out_err:
  2923. rcu_read_unlock();
  2924. cb->args[0] = idx;
  2925. return err;
  2926. }
  2927. static inline size_t bridge_nlmsg_size(void)
  2928. {
  2929. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  2930. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  2931. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  2932. + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
  2933. + nla_total_size(sizeof(u32)) /* IFLA_MTU */
  2934. + nla_total_size(sizeof(u32)) /* IFLA_LINK */
  2935. + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
  2936. + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
  2937. + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
  2938. + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
  2939. + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
  2940. }
  2941. static int rtnl_bridge_notify(struct net_device *dev)
  2942. {
  2943. struct net *net = dev_net(dev);
  2944. struct sk_buff *skb;
  2945. int err = -EOPNOTSUPP;
  2946. if (!dev->netdev_ops->ndo_bridge_getlink)
  2947. return 0;
  2948. skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
  2949. if (!skb) {
  2950. err = -ENOMEM;
  2951. goto errout;
  2952. }
  2953. err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
  2954. if (err < 0)
  2955. goto errout;
  2956. if (!skb->len)
  2957. goto errout;
  2958. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  2959. return 0;
  2960. errout:
  2961. WARN_ON(err == -EMSGSIZE);
  2962. kfree_skb(skb);
  2963. if (err)
  2964. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  2965. return err;
  2966. }
  2967. static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
  2968. {
  2969. struct net *net = sock_net(skb->sk);
  2970. struct ifinfomsg *ifm;
  2971. struct net_device *dev;
  2972. struct nlattr *br_spec, *attr = NULL;
  2973. int rem, err = -EOPNOTSUPP;
  2974. u16 flags = 0;
  2975. bool have_flags = false;
  2976. if (nlmsg_len(nlh) < sizeof(*ifm))
  2977. return -EINVAL;
  2978. ifm = nlmsg_data(nlh);
  2979. if (ifm->ifi_family != AF_BRIDGE)
  2980. return -EPFNOSUPPORT;
  2981. dev = __dev_get_by_index(net, ifm->ifi_index);
  2982. if (!dev) {
  2983. pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
  2984. return -ENODEV;
  2985. }
  2986. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  2987. if (br_spec) {
  2988. nla_for_each_nested(attr, br_spec, rem) {
  2989. if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
  2990. if (nla_len(attr) < sizeof(flags))
  2991. return -EINVAL;
  2992. have_flags = true;
  2993. flags = nla_get_u16(attr);
  2994. break;
  2995. }
  2996. }
  2997. }
  2998. if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
  2999. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  3000. if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
  3001. err = -EOPNOTSUPP;
  3002. goto out;
  3003. }
  3004. err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
  3005. if (err)
  3006. goto out;
  3007. flags &= ~BRIDGE_FLAGS_MASTER;
  3008. }
  3009. if ((flags & BRIDGE_FLAGS_SELF)) {
  3010. if (!dev->netdev_ops->ndo_bridge_setlink)
  3011. err = -EOPNOTSUPP;
  3012. else
  3013. err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
  3014. flags);
  3015. if (!err) {
  3016. flags &= ~BRIDGE_FLAGS_SELF;
  3017. /* Generate event to notify upper layer of bridge
  3018. * change
  3019. */
  3020. err = rtnl_bridge_notify(dev);
  3021. }
  3022. }
  3023. if (have_flags)
  3024. memcpy(nla_data(attr), &flags, sizeof(flags));
  3025. out:
  3026. return err;
  3027. }
  3028. static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
  3029. {
  3030. struct net *net = sock_net(skb->sk);
  3031. struct ifinfomsg *ifm;
  3032. struct net_device *dev;
  3033. struct nlattr *br_spec, *attr = NULL;
  3034. int rem, err = -EOPNOTSUPP;
  3035. u16 flags = 0;
  3036. bool have_flags = false;
  3037. if (nlmsg_len(nlh) < sizeof(*ifm))
  3038. return -EINVAL;
  3039. ifm = nlmsg_data(nlh);
  3040. if (ifm->ifi_family != AF_BRIDGE)
  3041. return -EPFNOSUPPORT;
  3042. dev = __dev_get_by_index(net, ifm->ifi_index);
  3043. if (!dev) {
  3044. pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
  3045. return -ENODEV;
  3046. }
  3047. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  3048. if (br_spec) {
  3049. nla_for_each_nested(attr, br_spec, rem) {
  3050. if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
  3051. if (nla_len(attr) < sizeof(flags))
  3052. return -EINVAL;
  3053. have_flags = true;
  3054. flags = nla_get_u16(attr);
  3055. break;
  3056. }
  3057. }
  3058. }
  3059. if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
  3060. struct net_device *br_dev = netdev_master_upper_dev_get(dev);
  3061. if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
  3062. err = -EOPNOTSUPP;
  3063. goto out;
  3064. }
  3065. err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
  3066. if (err)
  3067. goto out;
  3068. flags &= ~BRIDGE_FLAGS_MASTER;
  3069. }
  3070. if ((flags & BRIDGE_FLAGS_SELF)) {
  3071. if (!dev->netdev_ops->ndo_bridge_dellink)
  3072. err = -EOPNOTSUPP;
  3073. else
  3074. err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
  3075. flags);
  3076. if (!err) {
  3077. flags &= ~BRIDGE_FLAGS_SELF;
  3078. /* Generate event to notify upper layer of bridge
  3079. * change
  3080. */
  3081. err = rtnl_bridge_notify(dev);
  3082. }
  3083. }
  3084. if (have_flags)
  3085. memcpy(nla_data(attr), &flags, sizeof(flags));
  3086. out:
  3087. return err;
  3088. }
  3089. static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
  3090. {
  3091. return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
  3092. (!idxattr || idxattr == attrid);
  3093. }
  3094. #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
  3095. static int rtnl_get_offload_stats_attr_size(int attr_id)
  3096. {
  3097. switch (attr_id) {
  3098. case IFLA_OFFLOAD_XSTATS_CPU_HIT:
  3099. return sizeof(struct rtnl_link_stats64);
  3100. }
  3101. return 0;
  3102. }
  3103. static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
  3104. int *prividx)
  3105. {
  3106. struct nlattr *attr = NULL;
  3107. int attr_id, size;
  3108. void *attr_data;
  3109. int err;
  3110. if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
  3111. dev->netdev_ops->ndo_get_offload_stats))
  3112. return -ENODATA;
  3113. for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
  3114. attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
  3115. if (attr_id < *prividx)
  3116. continue;
  3117. size = rtnl_get_offload_stats_attr_size(attr_id);
  3118. if (!size)
  3119. continue;
  3120. if (!dev->netdev_ops->ndo_has_offload_stats(attr_id))
  3121. continue;
  3122. attr = nla_reserve_64bit(skb, attr_id, size,
  3123. IFLA_OFFLOAD_XSTATS_UNSPEC);
  3124. if (!attr)
  3125. goto nla_put_failure;
  3126. attr_data = nla_data(attr);
  3127. memset(attr_data, 0, size);
  3128. err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
  3129. attr_data);
  3130. if (err)
  3131. goto get_offload_stats_failure;
  3132. }
  3133. if (!attr)
  3134. return -ENODATA;
  3135. *prividx = 0;
  3136. return 0;
  3137. nla_put_failure:
  3138. err = -EMSGSIZE;
  3139. get_offload_stats_failure:
  3140. *prividx = attr_id;
  3141. return err;
  3142. }
  3143. static int rtnl_get_offload_stats_size(const struct net_device *dev)
  3144. {
  3145. int nla_size = 0;
  3146. int attr_id;
  3147. int size;
  3148. if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
  3149. dev->netdev_ops->ndo_get_offload_stats))
  3150. return 0;
  3151. for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
  3152. attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
  3153. if (!dev->netdev_ops->ndo_has_offload_stats(attr_id))
  3154. continue;
  3155. size = rtnl_get_offload_stats_attr_size(attr_id);
  3156. nla_size += nla_total_size_64bit(size);
  3157. }
  3158. if (nla_size != 0)
  3159. nla_size += nla_total_size(0);
  3160. return nla_size;
  3161. }
  3162. static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
  3163. int type, u32 pid, u32 seq, u32 change,
  3164. unsigned int flags, unsigned int filter_mask,
  3165. int *idxattr, int *prividx)
  3166. {
  3167. struct if_stats_msg *ifsm;
  3168. struct nlmsghdr *nlh;
  3169. struct nlattr *attr;
  3170. int s_prividx = *prividx;
  3171. int err;
  3172. ASSERT_RTNL();
  3173. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
  3174. if (!nlh)
  3175. return -EMSGSIZE;
  3176. ifsm = nlmsg_data(nlh);
  3177. ifsm->family = PF_UNSPEC;
  3178. ifsm->pad1 = 0;
  3179. ifsm->pad2 = 0;
  3180. ifsm->ifindex = dev->ifindex;
  3181. ifsm->filter_mask = filter_mask;
  3182. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
  3183. struct rtnl_link_stats64 *sp;
  3184. attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
  3185. sizeof(struct rtnl_link_stats64),
  3186. IFLA_STATS_UNSPEC);
  3187. if (!attr)
  3188. goto nla_put_failure;
  3189. sp = nla_data(attr);
  3190. dev_get_stats(dev, sp);
  3191. }
  3192. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
  3193. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  3194. if (ops && ops->fill_linkxstats) {
  3195. *idxattr = IFLA_STATS_LINK_XSTATS;
  3196. attr = nla_nest_start(skb,
  3197. IFLA_STATS_LINK_XSTATS);
  3198. if (!attr)
  3199. goto nla_put_failure;
  3200. err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
  3201. nla_nest_end(skb, attr);
  3202. if (err)
  3203. goto nla_put_failure;
  3204. *idxattr = 0;
  3205. }
  3206. }
  3207. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
  3208. *idxattr)) {
  3209. const struct rtnl_link_ops *ops = NULL;
  3210. const struct net_device *master;
  3211. master = netdev_master_upper_dev_get(dev);
  3212. if (master)
  3213. ops = master->rtnl_link_ops;
  3214. if (ops && ops->fill_linkxstats) {
  3215. *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
  3216. attr = nla_nest_start(skb,
  3217. IFLA_STATS_LINK_XSTATS_SLAVE);
  3218. if (!attr)
  3219. goto nla_put_failure;
  3220. err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
  3221. nla_nest_end(skb, attr);
  3222. if (err)
  3223. goto nla_put_failure;
  3224. *idxattr = 0;
  3225. }
  3226. }
  3227. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
  3228. *idxattr)) {
  3229. *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
  3230. attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
  3231. if (!attr)
  3232. goto nla_put_failure;
  3233. err = rtnl_get_offload_stats(skb, dev, prividx);
  3234. if (err == -ENODATA)
  3235. nla_nest_cancel(skb, attr);
  3236. else
  3237. nla_nest_end(skb, attr);
  3238. if (err && err != -ENODATA)
  3239. goto nla_put_failure;
  3240. *idxattr = 0;
  3241. }
  3242. nlmsg_end(skb, nlh);
  3243. return 0;
  3244. nla_put_failure:
  3245. /* not a multi message or no progress mean a real error */
  3246. if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
  3247. nlmsg_cancel(skb, nlh);
  3248. else
  3249. nlmsg_end(skb, nlh);
  3250. return -EMSGSIZE;
  3251. }
  3252. static size_t if_nlmsg_stats_size(const struct net_device *dev,
  3253. u32 filter_mask)
  3254. {
  3255. size_t size = 0;
  3256. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
  3257. size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
  3258. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
  3259. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  3260. int attr = IFLA_STATS_LINK_XSTATS;
  3261. if (ops && ops->get_linkxstats_size) {
  3262. size += nla_total_size(ops->get_linkxstats_size(dev,
  3263. attr));
  3264. /* for IFLA_STATS_LINK_XSTATS */
  3265. size += nla_total_size(0);
  3266. }
  3267. }
  3268. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
  3269. struct net_device *_dev = (struct net_device *)dev;
  3270. const struct rtnl_link_ops *ops = NULL;
  3271. const struct net_device *master;
  3272. /* netdev_master_upper_dev_get can't take const */
  3273. master = netdev_master_upper_dev_get(_dev);
  3274. if (master)
  3275. ops = master->rtnl_link_ops;
  3276. if (ops && ops->get_linkxstats_size) {
  3277. int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
  3278. size += nla_total_size(ops->get_linkxstats_size(dev,
  3279. attr));
  3280. /* for IFLA_STATS_LINK_XSTATS_SLAVE */
  3281. size += nla_total_size(0);
  3282. }
  3283. }
  3284. if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
  3285. size += rtnl_get_offload_stats_size(dev);
  3286. return size;
  3287. }
  3288. static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
  3289. {
  3290. struct net *net = sock_net(skb->sk);
  3291. struct net_device *dev = NULL;
  3292. int idxattr = 0, prividx = 0;
  3293. struct if_stats_msg *ifsm;
  3294. struct sk_buff *nskb;
  3295. u32 filter_mask;
  3296. int err;
  3297. if (nlmsg_len(nlh) < sizeof(*ifsm))
  3298. return -EINVAL;
  3299. ifsm = nlmsg_data(nlh);
  3300. if (ifsm->ifindex > 0)
  3301. dev = __dev_get_by_index(net, ifsm->ifindex);
  3302. else
  3303. return -EINVAL;
  3304. if (!dev)
  3305. return -ENODEV;
  3306. filter_mask = ifsm->filter_mask;
  3307. if (!filter_mask)
  3308. return -EINVAL;
  3309. nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
  3310. if (!nskb)
  3311. return -ENOBUFS;
  3312. err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
  3313. NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  3314. 0, filter_mask, &idxattr, &prividx);
  3315. if (err < 0) {
  3316. /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
  3317. WARN_ON(err == -EMSGSIZE);
  3318. kfree_skb(nskb);
  3319. } else {
  3320. err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
  3321. }
  3322. return err;
  3323. }
  3324. static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3325. {
  3326. int h, s_h, err, s_idx, s_idxattr, s_prividx;
  3327. struct net *net = sock_net(skb->sk);
  3328. unsigned int flags = NLM_F_MULTI;
  3329. struct if_stats_msg *ifsm;
  3330. struct hlist_head *head;
  3331. struct net_device *dev;
  3332. u32 filter_mask = 0;
  3333. int idx = 0;
  3334. s_h = cb->args[0];
  3335. s_idx = cb->args[1];
  3336. s_idxattr = cb->args[2];
  3337. s_prividx = cb->args[3];
  3338. cb->seq = net->dev_base_seq;
  3339. if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
  3340. return -EINVAL;
  3341. ifsm = nlmsg_data(cb->nlh);
  3342. filter_mask = ifsm->filter_mask;
  3343. if (!filter_mask)
  3344. return -EINVAL;
  3345. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  3346. idx = 0;
  3347. head = &net->dev_index_head[h];
  3348. hlist_for_each_entry(dev, head, index_hlist) {
  3349. if (idx < s_idx)
  3350. goto cont;
  3351. err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
  3352. NETLINK_CB(cb->skb).portid,
  3353. cb->nlh->nlmsg_seq, 0,
  3354. flags, filter_mask,
  3355. &s_idxattr, &s_prividx);
  3356. /* If we ran out of room on the first message,
  3357. * we're in trouble
  3358. */
  3359. WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
  3360. if (err < 0)
  3361. goto out;
  3362. s_prividx = 0;
  3363. s_idxattr = 0;
  3364. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  3365. cont:
  3366. idx++;
  3367. }
  3368. }
  3369. out:
  3370. cb->args[3] = s_prividx;
  3371. cb->args[2] = s_idxattr;
  3372. cb->args[1] = idx;
  3373. cb->args[0] = h;
  3374. return skb->len;
  3375. }
  3376. /* Process one rtnetlink message. */
  3377. static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  3378. {
  3379. struct net *net = sock_net(skb->sk);
  3380. rtnl_doit_func doit;
  3381. int kind;
  3382. int family;
  3383. int type;
  3384. int err;
  3385. type = nlh->nlmsg_type;
  3386. if (type > RTM_MAX)
  3387. return -EOPNOTSUPP;
  3388. type -= RTM_BASE;
  3389. /* All the messages must have at least 1 byte length */
  3390. if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
  3391. return 0;
  3392. family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
  3393. kind = type&3;
  3394. if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
  3395. return -EPERM;
  3396. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  3397. struct sock *rtnl;
  3398. rtnl_dumpit_func dumpit;
  3399. rtnl_calcit_func calcit;
  3400. u16 min_dump_alloc = 0;
  3401. dumpit = rtnl_get_dumpit(family, type);
  3402. if (dumpit == NULL)
  3403. return -EOPNOTSUPP;
  3404. calcit = rtnl_get_calcit(family, type);
  3405. if (calcit)
  3406. min_dump_alloc = calcit(skb, nlh);
  3407. __rtnl_unlock();
  3408. rtnl = net->rtnl;
  3409. {
  3410. struct netlink_dump_control c = {
  3411. .dump = dumpit,
  3412. .min_dump_alloc = min_dump_alloc,
  3413. };
  3414. err = netlink_dump_start(rtnl, skb, nlh, &c);
  3415. }
  3416. rtnl_lock();
  3417. return err;
  3418. }
  3419. doit = rtnl_get_doit(family, type);
  3420. if (doit == NULL)
  3421. return -EOPNOTSUPP;
  3422. return doit(skb, nlh);
  3423. }
  3424. static void rtnetlink_rcv(struct sk_buff *skb)
  3425. {
  3426. rtnl_lock();
  3427. netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
  3428. rtnl_unlock();
  3429. }
  3430. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  3431. {
  3432. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3433. switch (event) {
  3434. case NETDEV_UP:
  3435. case NETDEV_DOWN:
  3436. case NETDEV_PRE_UP:
  3437. case NETDEV_POST_INIT:
  3438. case NETDEV_REGISTER:
  3439. case NETDEV_CHANGE:
  3440. case NETDEV_PRE_TYPE_CHANGE:
  3441. case NETDEV_GOING_DOWN:
  3442. case NETDEV_UNREGISTER:
  3443. case NETDEV_UNREGISTER_FINAL:
  3444. case NETDEV_RELEASE:
  3445. case NETDEV_JOIN:
  3446. case NETDEV_BONDING_INFO:
  3447. break;
  3448. default:
  3449. rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
  3450. break;
  3451. }
  3452. return NOTIFY_DONE;
  3453. }
  3454. static struct notifier_block rtnetlink_dev_notifier = {
  3455. .notifier_call = rtnetlink_event,
  3456. };
  3457. static int __net_init rtnetlink_net_init(struct net *net)
  3458. {
  3459. struct sock *sk;
  3460. struct netlink_kernel_cfg cfg = {
  3461. .groups = RTNLGRP_MAX,
  3462. .input = rtnetlink_rcv,
  3463. .cb_mutex = &rtnl_mutex,
  3464. .flags = NL_CFG_F_NONROOT_RECV,
  3465. };
  3466. sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
  3467. if (!sk)
  3468. return -ENOMEM;
  3469. net->rtnl = sk;
  3470. return 0;
  3471. }
  3472. static void __net_exit rtnetlink_net_exit(struct net *net)
  3473. {
  3474. netlink_kernel_release(net->rtnl);
  3475. net->rtnl = NULL;
  3476. }
  3477. static struct pernet_operations rtnetlink_net_ops = {
  3478. .init = rtnetlink_net_init,
  3479. .exit = rtnetlink_net_exit,
  3480. };
  3481. void __init rtnetlink_init(void)
  3482. {
  3483. if (register_pernet_subsys(&rtnetlink_net_ops))
  3484. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  3485. register_netdevice_notifier(&rtnetlink_dev_notifier);
  3486. rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
  3487. rtnl_dump_ifinfo, rtnl_calcit);
  3488. rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
  3489. rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
  3490. rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
  3491. rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
  3492. rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
  3493. rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
  3494. rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
  3495. rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
  3496. rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
  3497. rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
  3498. rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
  3499. rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
  3500. NULL);
  3501. }