rtnetlink.c 86 KB

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