rtnetlink.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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/pci.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #include <linux/inet.h>
  41. #include <linux/netdevice.h>
  42. #include <net/ip.h>
  43. #include <net/protocol.h>
  44. #include <net/arp.h>
  45. #include <net/route.h>
  46. #include <net/udp.h>
  47. #include <net/sock.h>
  48. #include <net/pkt_sched.h>
  49. #include <net/fib_rules.h>
  50. #include <net/rtnetlink.h>
  51. #include <net/net_namespace.h>
  52. struct rtnl_link {
  53. rtnl_doit_func doit;
  54. rtnl_dumpit_func dumpit;
  55. };
  56. static DEFINE_MUTEX(rtnl_mutex);
  57. void rtnl_lock(void)
  58. {
  59. mutex_lock(&rtnl_mutex);
  60. }
  61. EXPORT_SYMBOL(rtnl_lock);
  62. void __rtnl_unlock(void)
  63. {
  64. mutex_unlock(&rtnl_mutex);
  65. }
  66. void rtnl_unlock(void)
  67. {
  68. /* This fellow will unlock it for us. */
  69. netdev_run_todo();
  70. }
  71. EXPORT_SYMBOL(rtnl_unlock);
  72. int rtnl_trylock(void)
  73. {
  74. return mutex_trylock(&rtnl_mutex);
  75. }
  76. EXPORT_SYMBOL(rtnl_trylock);
  77. int rtnl_is_locked(void)
  78. {
  79. return mutex_is_locked(&rtnl_mutex);
  80. }
  81. EXPORT_SYMBOL(rtnl_is_locked);
  82. #ifdef CONFIG_PROVE_LOCKING
  83. int lockdep_rtnl_is_held(void)
  84. {
  85. return lockdep_is_held(&rtnl_mutex);
  86. }
  87. EXPORT_SYMBOL(lockdep_rtnl_is_held);
  88. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  89. static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
  90. static inline int rtm_msgindex(int msgtype)
  91. {
  92. int msgindex = msgtype - RTM_BASE;
  93. /*
  94. * msgindex < 0 implies someone tried to register a netlink
  95. * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
  96. * the message type has not been added to linux/rtnetlink.h
  97. */
  98. BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
  99. return msgindex;
  100. }
  101. static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
  102. {
  103. struct rtnl_link *tab;
  104. if (protocol <= RTNL_FAMILY_MAX)
  105. tab = rtnl_msg_handlers[protocol];
  106. else
  107. tab = NULL;
  108. if (tab == NULL || tab[msgindex].doit == NULL)
  109. tab = rtnl_msg_handlers[PF_UNSPEC];
  110. return tab ? tab[msgindex].doit : NULL;
  111. }
  112. static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
  113. {
  114. struct rtnl_link *tab;
  115. if (protocol <= RTNL_FAMILY_MAX)
  116. tab = rtnl_msg_handlers[protocol];
  117. else
  118. tab = NULL;
  119. if (tab == NULL || tab[msgindex].dumpit == NULL)
  120. tab = rtnl_msg_handlers[PF_UNSPEC];
  121. return tab ? tab[msgindex].dumpit : NULL;
  122. }
  123. /**
  124. * __rtnl_register - Register a rtnetlink message type
  125. * @protocol: Protocol family or PF_UNSPEC
  126. * @msgtype: rtnetlink message type
  127. * @doit: Function pointer called for each request message
  128. * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
  129. *
  130. * Registers the specified function pointers (at least one of them has
  131. * to be non-NULL) to be called whenever a request message for the
  132. * specified protocol family and message type is received.
  133. *
  134. * The special protocol family PF_UNSPEC may be used to define fallback
  135. * function pointers for the case when no entry for the specific protocol
  136. * family exists.
  137. *
  138. * Returns 0 on success or a negative error code.
  139. */
  140. int __rtnl_register(int protocol, int msgtype,
  141. rtnl_doit_func doit, rtnl_dumpit_func dumpit)
  142. {
  143. struct rtnl_link *tab;
  144. int msgindex;
  145. BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
  146. msgindex = rtm_msgindex(msgtype);
  147. tab = rtnl_msg_handlers[protocol];
  148. if (tab == NULL) {
  149. tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
  150. if (tab == NULL)
  151. return -ENOBUFS;
  152. rtnl_msg_handlers[protocol] = tab;
  153. }
  154. if (doit)
  155. tab[msgindex].doit = doit;
  156. if (dumpit)
  157. tab[msgindex].dumpit = dumpit;
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_GPL(__rtnl_register);
  161. /**
  162. * rtnl_register - Register a rtnetlink message type
  163. *
  164. * Identical to __rtnl_register() but panics on failure. This is useful
  165. * as failure of this function is very unlikely, it can only happen due
  166. * to lack of memory when allocating the chain to store all message
  167. * handlers for a protocol. Meant for use in init functions where lack
  168. * of memory implies no sense in continuing.
  169. */
  170. void rtnl_register(int protocol, int msgtype,
  171. rtnl_doit_func doit, rtnl_dumpit_func dumpit)
  172. {
  173. if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
  174. panic("Unable to register rtnetlink message handler, "
  175. "protocol = %d, message type = %d\n",
  176. protocol, msgtype);
  177. }
  178. EXPORT_SYMBOL_GPL(rtnl_register);
  179. /**
  180. * rtnl_unregister - Unregister a rtnetlink message type
  181. * @protocol: Protocol family or PF_UNSPEC
  182. * @msgtype: rtnetlink message type
  183. *
  184. * Returns 0 on success or a negative error code.
  185. */
  186. int rtnl_unregister(int protocol, int msgtype)
  187. {
  188. int msgindex;
  189. BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
  190. msgindex = rtm_msgindex(msgtype);
  191. if (rtnl_msg_handlers[protocol] == NULL)
  192. return -ENOENT;
  193. rtnl_msg_handlers[protocol][msgindex].doit = NULL;
  194. rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(rtnl_unregister);
  198. /**
  199. * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
  200. * @protocol : Protocol family or PF_UNSPEC
  201. *
  202. * Identical to calling rtnl_unregster() for all registered message types
  203. * of a certain protocol family.
  204. */
  205. void rtnl_unregister_all(int protocol)
  206. {
  207. BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
  208. kfree(rtnl_msg_handlers[protocol]);
  209. rtnl_msg_handlers[protocol] = NULL;
  210. }
  211. EXPORT_SYMBOL_GPL(rtnl_unregister_all);
  212. static LIST_HEAD(link_ops);
  213. /**
  214. * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
  215. * @ops: struct rtnl_link_ops * to register
  216. *
  217. * The caller must hold the rtnl_mutex. This function should be used
  218. * by drivers that create devices during module initialization. It
  219. * must be called before registering the devices.
  220. *
  221. * Returns 0 on success or a negative error code.
  222. */
  223. int __rtnl_link_register(struct rtnl_link_ops *ops)
  224. {
  225. if (!ops->dellink)
  226. ops->dellink = unregister_netdevice_queue;
  227. list_add_tail(&ops->list, &link_ops);
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(__rtnl_link_register);
  231. /**
  232. * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
  233. * @ops: struct rtnl_link_ops * to register
  234. *
  235. * Returns 0 on success or a negative error code.
  236. */
  237. int rtnl_link_register(struct rtnl_link_ops *ops)
  238. {
  239. int err;
  240. rtnl_lock();
  241. err = __rtnl_link_register(ops);
  242. rtnl_unlock();
  243. return err;
  244. }
  245. EXPORT_SYMBOL_GPL(rtnl_link_register);
  246. static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
  247. {
  248. struct net_device *dev;
  249. LIST_HEAD(list_kill);
  250. for_each_netdev(net, dev) {
  251. if (dev->rtnl_link_ops == ops)
  252. ops->dellink(dev, &list_kill);
  253. }
  254. unregister_netdevice_many(&list_kill);
  255. }
  256. /**
  257. * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
  258. * @ops: struct rtnl_link_ops * to unregister
  259. *
  260. * The caller must hold the rtnl_mutex.
  261. */
  262. void __rtnl_link_unregister(struct rtnl_link_ops *ops)
  263. {
  264. struct net *net;
  265. for_each_net(net) {
  266. __rtnl_kill_links(net, ops);
  267. }
  268. list_del(&ops->list);
  269. }
  270. EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
  271. /**
  272. * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
  273. * @ops: struct rtnl_link_ops * to unregister
  274. */
  275. void rtnl_link_unregister(struct rtnl_link_ops *ops)
  276. {
  277. rtnl_lock();
  278. __rtnl_link_unregister(ops);
  279. rtnl_unlock();
  280. }
  281. EXPORT_SYMBOL_GPL(rtnl_link_unregister);
  282. static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
  283. {
  284. const struct rtnl_link_ops *ops;
  285. list_for_each_entry(ops, &link_ops, list) {
  286. if (!strcmp(ops->kind, kind))
  287. return ops;
  288. }
  289. return NULL;
  290. }
  291. static size_t rtnl_link_get_size(const struct net_device *dev)
  292. {
  293. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  294. size_t size;
  295. if (!ops)
  296. return 0;
  297. size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
  298. nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
  299. if (ops->get_size)
  300. /* IFLA_INFO_DATA + nested data */
  301. size += nla_total_size(sizeof(struct nlattr)) +
  302. ops->get_size(dev);
  303. if (ops->get_xstats_size)
  304. /* IFLA_INFO_XSTATS */
  305. size += nla_total_size(ops->get_xstats_size(dev));
  306. return size;
  307. }
  308. static LIST_HEAD(rtnl_af_ops);
  309. static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
  310. {
  311. const struct rtnl_af_ops *ops;
  312. list_for_each_entry(ops, &rtnl_af_ops, list) {
  313. if (ops->family == family)
  314. return ops;
  315. }
  316. return NULL;
  317. }
  318. /**
  319. * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
  320. * @ops: struct rtnl_af_ops * to register
  321. *
  322. * The caller must hold the rtnl_mutex.
  323. *
  324. * Returns 0 on success or a negative error code.
  325. */
  326. int __rtnl_af_register(struct rtnl_af_ops *ops)
  327. {
  328. list_add_tail(&ops->list, &rtnl_af_ops);
  329. return 0;
  330. }
  331. EXPORT_SYMBOL_GPL(__rtnl_af_register);
  332. /**
  333. * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
  334. * @ops: struct rtnl_af_ops * to register
  335. *
  336. * Returns 0 on success or a negative error code.
  337. */
  338. int rtnl_af_register(struct rtnl_af_ops *ops)
  339. {
  340. int err;
  341. rtnl_lock();
  342. err = __rtnl_af_register(ops);
  343. rtnl_unlock();
  344. return err;
  345. }
  346. EXPORT_SYMBOL_GPL(rtnl_af_register);
  347. /**
  348. * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
  349. * @ops: struct rtnl_af_ops * to unregister
  350. *
  351. * The caller must hold the rtnl_mutex.
  352. */
  353. void __rtnl_af_unregister(struct rtnl_af_ops *ops)
  354. {
  355. list_del(&ops->list);
  356. }
  357. EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
  358. /**
  359. * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
  360. * @ops: struct rtnl_af_ops * to unregister
  361. */
  362. void rtnl_af_unregister(struct rtnl_af_ops *ops)
  363. {
  364. rtnl_lock();
  365. __rtnl_af_unregister(ops);
  366. rtnl_unlock();
  367. }
  368. EXPORT_SYMBOL_GPL(rtnl_af_unregister);
  369. static size_t rtnl_link_get_af_size(const struct net_device *dev)
  370. {
  371. struct rtnl_af_ops *af_ops;
  372. size_t size;
  373. /* IFLA_AF_SPEC */
  374. size = nla_total_size(sizeof(struct nlattr));
  375. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  376. if (af_ops->get_link_af_size) {
  377. /* AF_* + nested data */
  378. size += nla_total_size(sizeof(struct nlattr)) +
  379. af_ops->get_link_af_size(dev);
  380. }
  381. }
  382. return size;
  383. }
  384. static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
  385. {
  386. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  387. struct nlattr *linkinfo, *data;
  388. int err = -EMSGSIZE;
  389. linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
  390. if (linkinfo == NULL)
  391. goto out;
  392. if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
  393. goto err_cancel_link;
  394. if (ops->fill_xstats) {
  395. err = ops->fill_xstats(skb, dev);
  396. if (err < 0)
  397. goto err_cancel_link;
  398. }
  399. if (ops->fill_info) {
  400. data = nla_nest_start(skb, IFLA_INFO_DATA);
  401. if (data == NULL)
  402. goto err_cancel_link;
  403. err = ops->fill_info(skb, dev);
  404. if (err < 0)
  405. goto err_cancel_data;
  406. nla_nest_end(skb, data);
  407. }
  408. nla_nest_end(skb, linkinfo);
  409. return 0;
  410. err_cancel_data:
  411. nla_nest_cancel(skb, data);
  412. err_cancel_link:
  413. nla_nest_cancel(skb, linkinfo);
  414. out:
  415. return err;
  416. }
  417. static const int rtm_min[RTM_NR_FAMILIES] =
  418. {
  419. [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  420. [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
  421. [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  422. [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
  423. [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  424. [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  425. [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  426. [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
  427. [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  428. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  429. };
  430. static const int rta_max[RTM_NR_FAMILIES] =
  431. {
  432. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  433. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  434. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  435. [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
  436. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  437. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  438. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  439. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  440. };
  441. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  442. {
  443. struct rtattr *rta;
  444. int size = RTA_LENGTH(attrlen);
  445. rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
  446. rta->rta_type = attrtype;
  447. rta->rta_len = size;
  448. memcpy(RTA_DATA(rta), data, attrlen);
  449. memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
  450. }
  451. EXPORT_SYMBOL(__rta_fill);
  452. int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
  453. {
  454. struct sock *rtnl = net->rtnl;
  455. int err = 0;
  456. NETLINK_CB(skb).dst_group = group;
  457. if (echo)
  458. atomic_inc(&skb->users);
  459. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  460. if (echo)
  461. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  462. return err;
  463. }
  464. int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
  465. {
  466. struct sock *rtnl = net->rtnl;
  467. return nlmsg_unicast(rtnl, skb, pid);
  468. }
  469. EXPORT_SYMBOL(rtnl_unicast);
  470. void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
  471. struct nlmsghdr *nlh, gfp_t flags)
  472. {
  473. struct sock *rtnl = net->rtnl;
  474. int report = 0;
  475. if (nlh)
  476. report = nlmsg_report(nlh);
  477. nlmsg_notify(rtnl, skb, pid, group, report, flags);
  478. }
  479. EXPORT_SYMBOL(rtnl_notify);
  480. void rtnl_set_sk_err(struct net *net, u32 group, int error)
  481. {
  482. struct sock *rtnl = net->rtnl;
  483. netlink_set_err(rtnl, 0, group, error);
  484. }
  485. EXPORT_SYMBOL(rtnl_set_sk_err);
  486. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  487. {
  488. struct nlattr *mx;
  489. int i, valid = 0;
  490. mx = nla_nest_start(skb, RTA_METRICS);
  491. if (mx == NULL)
  492. return -ENOBUFS;
  493. for (i = 0; i < RTAX_MAX; i++) {
  494. if (metrics[i]) {
  495. valid++;
  496. NLA_PUT_U32(skb, i+1, metrics[i]);
  497. }
  498. }
  499. if (!valid) {
  500. nla_nest_cancel(skb, mx);
  501. return 0;
  502. }
  503. return nla_nest_end(skb, mx);
  504. nla_put_failure:
  505. nla_nest_cancel(skb, mx);
  506. return -EMSGSIZE;
  507. }
  508. EXPORT_SYMBOL(rtnetlink_put_metrics);
  509. int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
  510. u32 ts, u32 tsage, long expires, u32 error)
  511. {
  512. struct rta_cacheinfo ci = {
  513. .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
  514. .rta_used = dst->__use,
  515. .rta_clntref = atomic_read(&(dst->__refcnt)),
  516. .rta_error = error,
  517. .rta_id = id,
  518. .rta_ts = ts,
  519. .rta_tsage = tsage,
  520. };
  521. if (expires)
  522. ci.rta_expires = jiffies_to_clock_t(expires);
  523. return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  524. }
  525. EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
  526. static void set_operstate(struct net_device *dev, unsigned char transition)
  527. {
  528. unsigned char operstate = dev->operstate;
  529. switch (transition) {
  530. case IF_OPER_UP:
  531. if ((operstate == IF_OPER_DORMANT ||
  532. operstate == IF_OPER_UNKNOWN) &&
  533. !netif_dormant(dev))
  534. operstate = IF_OPER_UP;
  535. break;
  536. case IF_OPER_DORMANT:
  537. if (operstate == IF_OPER_UP ||
  538. operstate == IF_OPER_UNKNOWN)
  539. operstate = IF_OPER_DORMANT;
  540. break;
  541. }
  542. if (dev->operstate != operstate) {
  543. write_lock_bh(&dev_base_lock);
  544. dev->operstate = operstate;
  545. write_unlock_bh(&dev_base_lock);
  546. netdev_state_change(dev);
  547. }
  548. }
  549. static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
  550. const struct ifinfomsg *ifm)
  551. {
  552. unsigned int flags = ifm->ifi_flags;
  553. /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
  554. if (ifm->ifi_change)
  555. flags = (flags & ifm->ifi_change) |
  556. (dev->flags & ~ifm->ifi_change);
  557. return flags;
  558. }
  559. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  560. const struct rtnl_link_stats64 *b)
  561. {
  562. a->rx_packets = b->rx_packets;
  563. a->tx_packets = b->tx_packets;
  564. a->rx_bytes = b->rx_bytes;
  565. a->tx_bytes = b->tx_bytes;
  566. a->rx_errors = b->rx_errors;
  567. a->tx_errors = b->tx_errors;
  568. a->rx_dropped = b->rx_dropped;
  569. a->tx_dropped = b->tx_dropped;
  570. a->multicast = b->multicast;
  571. a->collisions = b->collisions;
  572. a->rx_length_errors = b->rx_length_errors;
  573. a->rx_over_errors = b->rx_over_errors;
  574. a->rx_crc_errors = b->rx_crc_errors;
  575. a->rx_frame_errors = b->rx_frame_errors;
  576. a->rx_fifo_errors = b->rx_fifo_errors;
  577. a->rx_missed_errors = b->rx_missed_errors;
  578. a->tx_aborted_errors = b->tx_aborted_errors;
  579. a->tx_carrier_errors = b->tx_carrier_errors;
  580. a->tx_fifo_errors = b->tx_fifo_errors;
  581. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  582. a->tx_window_errors = b->tx_window_errors;
  583. a->rx_compressed = b->rx_compressed;
  584. a->tx_compressed = b->tx_compressed;
  585. }
  586. static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
  587. {
  588. memcpy(v, b, sizeof(*b));
  589. }
  590. /* All VF info */
  591. static inline int rtnl_vfinfo_size(const struct net_device *dev)
  592. {
  593. if (dev->dev.parent && dev_is_pci(dev->dev.parent)) {
  594. int num_vfs = dev_num_vf(dev->dev.parent);
  595. size_t size = nla_total_size(sizeof(struct nlattr));
  596. size += nla_total_size(num_vfs * sizeof(struct nlattr));
  597. size += num_vfs *
  598. (nla_total_size(sizeof(struct ifla_vf_mac)) +
  599. nla_total_size(sizeof(struct ifla_vf_vlan)) +
  600. nla_total_size(sizeof(struct ifla_vf_tx_rate)));
  601. return size;
  602. } else
  603. return 0;
  604. }
  605. static size_t rtnl_port_size(const struct net_device *dev)
  606. {
  607. size_t port_size = nla_total_size(4) /* PORT_VF */
  608. + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
  609. + nla_total_size(sizeof(struct ifla_port_vsi))
  610. /* PORT_VSI_TYPE */
  611. + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
  612. + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
  613. + nla_total_size(1) /* PROT_VDP_REQUEST */
  614. + nla_total_size(2); /* PORT_VDP_RESPONSE */
  615. size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
  616. size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
  617. + port_size;
  618. size_t port_self_size = nla_total_size(sizeof(struct nlattr))
  619. + port_size;
  620. if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
  621. return 0;
  622. if (dev_num_vf(dev->dev.parent))
  623. return port_self_size + vf_ports_size +
  624. vf_port_size * dev_num_vf(dev->dev.parent);
  625. else
  626. return port_self_size;
  627. }
  628. static noinline size_t if_nlmsg_size(const struct net_device *dev)
  629. {
  630. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  631. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  632. + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
  633. + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
  634. + nla_total_size(sizeof(struct rtnl_link_ifmap))
  635. + nla_total_size(sizeof(struct rtnl_link_stats))
  636. + nla_total_size(sizeof(struct rtnl_link_stats64))
  637. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  638. + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
  639. + nla_total_size(4) /* IFLA_TXQLEN */
  640. + nla_total_size(4) /* IFLA_WEIGHT */
  641. + nla_total_size(4) /* IFLA_MTU */
  642. + nla_total_size(4) /* IFLA_LINK */
  643. + nla_total_size(4) /* IFLA_MASTER */
  644. + nla_total_size(1) /* IFLA_OPERSTATE */
  645. + nla_total_size(1) /* IFLA_LINKMODE */
  646. + nla_total_size(4) /* IFLA_NUM_VF */
  647. + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */
  648. + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
  649. + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
  650. + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
  651. }
  652. static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
  653. {
  654. struct nlattr *vf_ports;
  655. struct nlattr *vf_port;
  656. int vf;
  657. int err;
  658. vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
  659. if (!vf_ports)
  660. return -EMSGSIZE;
  661. for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
  662. vf_port = nla_nest_start(skb, IFLA_VF_PORT);
  663. if (!vf_port)
  664. goto nla_put_failure;
  665. NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
  666. err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
  667. if (err == -EMSGSIZE)
  668. goto nla_put_failure;
  669. if (err) {
  670. nla_nest_cancel(skb, vf_port);
  671. continue;
  672. }
  673. nla_nest_end(skb, vf_port);
  674. }
  675. nla_nest_end(skb, vf_ports);
  676. return 0;
  677. nla_put_failure:
  678. nla_nest_cancel(skb, vf_ports);
  679. return -EMSGSIZE;
  680. }
  681. static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
  682. {
  683. struct nlattr *port_self;
  684. int err;
  685. port_self = nla_nest_start(skb, IFLA_PORT_SELF);
  686. if (!port_self)
  687. return -EMSGSIZE;
  688. err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
  689. if (err) {
  690. nla_nest_cancel(skb, port_self);
  691. return (err == -EMSGSIZE) ? err : 0;
  692. }
  693. nla_nest_end(skb, port_self);
  694. return 0;
  695. }
  696. static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
  697. {
  698. int err;
  699. if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
  700. return 0;
  701. err = rtnl_port_self_fill(skb, dev);
  702. if (err)
  703. return err;
  704. if (dev_num_vf(dev->dev.parent)) {
  705. err = rtnl_vf_ports_fill(skb, dev);
  706. if (err)
  707. return err;
  708. }
  709. return 0;
  710. }
  711. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  712. int type, u32 pid, u32 seq, u32 change,
  713. unsigned int flags)
  714. {
  715. struct ifinfomsg *ifm;
  716. struct nlmsghdr *nlh;
  717. struct rtnl_link_stats64 temp;
  718. const struct rtnl_link_stats64 *stats;
  719. struct nlattr *attr, *af_spec;
  720. struct rtnl_af_ops *af_ops;
  721. ASSERT_RTNL();
  722. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  723. if (nlh == NULL)
  724. return -EMSGSIZE;
  725. ifm = nlmsg_data(nlh);
  726. ifm->ifi_family = AF_UNSPEC;
  727. ifm->__ifi_pad = 0;
  728. ifm->ifi_type = dev->type;
  729. ifm->ifi_index = dev->ifindex;
  730. ifm->ifi_flags = dev_get_flags(dev);
  731. ifm->ifi_change = change;
  732. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  733. NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
  734. NLA_PUT_U8(skb, IFLA_OPERSTATE,
  735. netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
  736. NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
  737. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  738. NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
  739. if (dev->ifindex != dev->iflink)
  740. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  741. if (dev->master)
  742. NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
  743. if (dev->qdisc)
  744. NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
  745. if (dev->ifalias)
  746. NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
  747. if (1) {
  748. struct rtnl_link_ifmap map = {
  749. .mem_start = dev->mem_start,
  750. .mem_end = dev->mem_end,
  751. .base_addr = dev->base_addr,
  752. .irq = dev->irq,
  753. .dma = dev->dma,
  754. .port = dev->if_port,
  755. };
  756. NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  757. }
  758. if (dev->addr_len) {
  759. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  760. NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  761. }
  762. attr = nla_reserve(skb, IFLA_STATS,
  763. sizeof(struct rtnl_link_stats));
  764. if (attr == NULL)
  765. goto nla_put_failure;
  766. stats = dev_get_stats(dev, &temp);
  767. copy_rtnl_link_stats(nla_data(attr), stats);
  768. attr = nla_reserve(skb, IFLA_STATS64,
  769. sizeof(struct rtnl_link_stats64));
  770. if (attr == NULL)
  771. goto nla_put_failure;
  772. copy_rtnl_link_stats64(nla_data(attr), stats);
  773. if (dev->dev.parent)
  774. NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
  775. if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) {
  776. int i;
  777. struct nlattr *vfinfo, *vf;
  778. int num_vfs = dev_num_vf(dev->dev.parent);
  779. vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
  780. if (!vfinfo)
  781. goto nla_put_failure;
  782. for (i = 0; i < num_vfs; i++) {
  783. struct ifla_vf_info ivi;
  784. struct ifla_vf_mac vf_mac;
  785. struct ifla_vf_vlan vf_vlan;
  786. struct ifla_vf_tx_rate vf_tx_rate;
  787. if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
  788. break;
  789. vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf;
  790. memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
  791. vf_vlan.vlan = ivi.vlan;
  792. vf_vlan.qos = ivi.qos;
  793. vf_tx_rate.rate = ivi.tx_rate;
  794. vf = nla_nest_start(skb, IFLA_VF_INFO);
  795. if (!vf) {
  796. nla_nest_cancel(skb, vfinfo);
  797. goto nla_put_failure;
  798. }
  799. NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
  800. NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
  801. NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate);
  802. nla_nest_end(skb, vf);
  803. }
  804. nla_nest_end(skb, vfinfo);
  805. }
  806. if (rtnl_port_fill(skb, dev))
  807. goto nla_put_failure;
  808. if (dev->rtnl_link_ops) {
  809. if (rtnl_link_fill(skb, dev) < 0)
  810. goto nla_put_failure;
  811. }
  812. if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
  813. goto nla_put_failure;
  814. list_for_each_entry(af_ops, &rtnl_af_ops, list) {
  815. if (af_ops->fill_link_af) {
  816. struct nlattr *af;
  817. int err;
  818. if (!(af = nla_nest_start(skb, af_ops->family)))
  819. goto nla_put_failure;
  820. err = af_ops->fill_link_af(skb, dev);
  821. /*
  822. * Caller may return ENODATA to indicate that there
  823. * was no data to be dumped. This is not an error, it
  824. * means we should trim the attribute header and
  825. * continue.
  826. */
  827. if (err == -ENODATA)
  828. nla_nest_cancel(skb, af);
  829. else if (err < 0)
  830. goto nla_put_failure;
  831. nla_nest_end(skb, af);
  832. }
  833. }
  834. nla_nest_end(skb, af_spec);
  835. return nlmsg_end(skb, nlh);
  836. nla_put_failure:
  837. nlmsg_cancel(skb, nlh);
  838. return -EMSGSIZE;
  839. }
  840. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  841. {
  842. struct net *net = sock_net(skb->sk);
  843. int h, s_h;
  844. int idx = 0, s_idx;
  845. struct net_device *dev;
  846. struct hlist_head *head;
  847. struct hlist_node *node;
  848. s_h = cb->args[0];
  849. s_idx = cb->args[1];
  850. rcu_read_lock();
  851. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  852. idx = 0;
  853. head = &net->dev_index_head[h];
  854. hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
  855. if (idx < s_idx)
  856. goto cont;
  857. if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
  858. NETLINK_CB(cb->skb).pid,
  859. cb->nlh->nlmsg_seq, 0,
  860. NLM_F_MULTI) <= 0)
  861. goto out;
  862. cont:
  863. idx++;
  864. }
  865. }
  866. out:
  867. rcu_read_unlock();
  868. cb->args[1] = idx;
  869. cb->args[0] = h;
  870. return skb->len;
  871. }
  872. const struct nla_policy ifla_policy[IFLA_MAX+1] = {
  873. [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
  874. [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  875. [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  876. [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
  877. [IFLA_MTU] = { .type = NLA_U32 },
  878. [IFLA_LINK] = { .type = NLA_U32 },
  879. [IFLA_MASTER] = { .type = NLA_U32 },
  880. [IFLA_TXQLEN] = { .type = NLA_U32 },
  881. [IFLA_WEIGHT] = { .type = NLA_U32 },
  882. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  883. [IFLA_LINKMODE] = { .type = NLA_U8 },
  884. [IFLA_LINKINFO] = { .type = NLA_NESTED },
  885. [IFLA_NET_NS_PID] = { .type = NLA_U32 },
  886. [IFLA_NET_NS_FD] = { .type = NLA_U32 },
  887. [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
  888. [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
  889. [IFLA_VF_PORTS] = { .type = NLA_NESTED },
  890. [IFLA_PORT_SELF] = { .type = NLA_NESTED },
  891. [IFLA_AF_SPEC] = { .type = NLA_NESTED },
  892. };
  893. EXPORT_SYMBOL(ifla_policy);
  894. static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
  895. [IFLA_INFO_KIND] = { .type = NLA_STRING },
  896. [IFLA_INFO_DATA] = { .type = NLA_NESTED },
  897. };
  898. static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
  899. [IFLA_VF_INFO] = { .type = NLA_NESTED },
  900. };
  901. static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
  902. [IFLA_VF_MAC] = { .type = NLA_BINARY,
  903. .len = sizeof(struct ifla_vf_mac) },
  904. [IFLA_VF_VLAN] = { .type = NLA_BINARY,
  905. .len = sizeof(struct ifla_vf_vlan) },
  906. [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
  907. .len = sizeof(struct ifla_vf_tx_rate) },
  908. };
  909. static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
  910. [IFLA_PORT_VF] = { .type = NLA_U32 },
  911. [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
  912. .len = PORT_PROFILE_MAX },
  913. [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
  914. .len = sizeof(struct ifla_port_vsi)},
  915. [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
  916. .len = PORT_UUID_MAX },
  917. [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
  918. .len = PORT_UUID_MAX },
  919. [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
  920. [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
  921. };
  922. struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
  923. {
  924. struct net *net;
  925. /* Examine the link attributes and figure out which
  926. * network namespace we are talking about.
  927. */
  928. if (tb[IFLA_NET_NS_PID])
  929. net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
  930. else if (tb[IFLA_NET_NS_FD])
  931. net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
  932. else
  933. net = get_net(src_net);
  934. return net;
  935. }
  936. EXPORT_SYMBOL(rtnl_link_get_net);
  937. static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
  938. {
  939. if (dev) {
  940. if (tb[IFLA_ADDRESS] &&
  941. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  942. return -EINVAL;
  943. if (tb[IFLA_BROADCAST] &&
  944. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  945. return -EINVAL;
  946. }
  947. if (tb[IFLA_AF_SPEC]) {
  948. struct nlattr *af;
  949. int rem, err;
  950. nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
  951. const struct rtnl_af_ops *af_ops;
  952. if (!(af_ops = rtnl_af_lookup(nla_type(af))))
  953. return -EAFNOSUPPORT;
  954. if (!af_ops->set_link_af)
  955. return -EOPNOTSUPP;
  956. if (af_ops->validate_link_af) {
  957. err = af_ops->validate_link_af(dev, af);
  958. if (err < 0)
  959. return err;
  960. }
  961. }
  962. }
  963. return 0;
  964. }
  965. static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
  966. {
  967. int rem, err = -EINVAL;
  968. struct nlattr *vf;
  969. const struct net_device_ops *ops = dev->netdev_ops;
  970. nla_for_each_nested(vf, attr, rem) {
  971. switch (nla_type(vf)) {
  972. case IFLA_VF_MAC: {
  973. struct ifla_vf_mac *ivm;
  974. ivm = nla_data(vf);
  975. err = -EOPNOTSUPP;
  976. if (ops->ndo_set_vf_mac)
  977. err = ops->ndo_set_vf_mac(dev, ivm->vf,
  978. ivm->mac);
  979. break;
  980. }
  981. case IFLA_VF_VLAN: {
  982. struct ifla_vf_vlan *ivv;
  983. ivv = nla_data(vf);
  984. err = -EOPNOTSUPP;
  985. if (ops->ndo_set_vf_vlan)
  986. err = ops->ndo_set_vf_vlan(dev, ivv->vf,
  987. ivv->vlan,
  988. ivv->qos);
  989. break;
  990. }
  991. case IFLA_VF_TX_RATE: {
  992. struct ifla_vf_tx_rate *ivt;
  993. ivt = nla_data(vf);
  994. err = -EOPNOTSUPP;
  995. if (ops->ndo_set_vf_tx_rate)
  996. err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
  997. ivt->rate);
  998. break;
  999. }
  1000. default:
  1001. err = -EINVAL;
  1002. break;
  1003. }
  1004. if (err)
  1005. break;
  1006. }
  1007. return err;
  1008. }
  1009. static int do_set_master(struct net_device *dev, int ifindex)
  1010. {
  1011. struct net_device *master_dev;
  1012. const struct net_device_ops *ops;
  1013. int err;
  1014. if (dev->master) {
  1015. if (dev->master->ifindex == ifindex)
  1016. return 0;
  1017. ops = dev->master->netdev_ops;
  1018. if (ops->ndo_del_slave) {
  1019. err = ops->ndo_del_slave(dev->master, dev);
  1020. if (err)
  1021. return err;
  1022. } else {
  1023. return -EOPNOTSUPP;
  1024. }
  1025. }
  1026. if (ifindex) {
  1027. master_dev = __dev_get_by_index(dev_net(dev), ifindex);
  1028. if (!master_dev)
  1029. return -EINVAL;
  1030. ops = master_dev->netdev_ops;
  1031. if (ops->ndo_add_slave) {
  1032. err = ops->ndo_add_slave(master_dev, dev);
  1033. if (err)
  1034. return err;
  1035. } else {
  1036. return -EOPNOTSUPP;
  1037. }
  1038. }
  1039. return 0;
  1040. }
  1041. static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
  1042. struct nlattr **tb, char *ifname, int modified)
  1043. {
  1044. const struct net_device_ops *ops = dev->netdev_ops;
  1045. int send_addr_notify = 0;
  1046. int err;
  1047. if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
  1048. struct net *net = rtnl_link_get_net(dev_net(dev), tb);
  1049. if (IS_ERR(net)) {
  1050. err = PTR_ERR(net);
  1051. goto errout;
  1052. }
  1053. err = dev_change_net_namespace(dev, net, ifname);
  1054. put_net(net);
  1055. if (err)
  1056. goto errout;
  1057. modified = 1;
  1058. }
  1059. if (tb[IFLA_MAP]) {
  1060. struct rtnl_link_ifmap *u_map;
  1061. struct ifmap k_map;
  1062. if (!ops->ndo_set_config) {
  1063. err = -EOPNOTSUPP;
  1064. goto errout;
  1065. }
  1066. if (!netif_device_present(dev)) {
  1067. err = -ENODEV;
  1068. goto errout;
  1069. }
  1070. u_map = nla_data(tb[IFLA_MAP]);
  1071. k_map.mem_start = (unsigned long) u_map->mem_start;
  1072. k_map.mem_end = (unsigned long) u_map->mem_end;
  1073. k_map.base_addr = (unsigned short) u_map->base_addr;
  1074. k_map.irq = (unsigned char) u_map->irq;
  1075. k_map.dma = (unsigned char) u_map->dma;
  1076. k_map.port = (unsigned char) u_map->port;
  1077. err = ops->ndo_set_config(dev, &k_map);
  1078. if (err < 0)
  1079. goto errout;
  1080. modified = 1;
  1081. }
  1082. if (tb[IFLA_ADDRESS]) {
  1083. struct sockaddr *sa;
  1084. int len;
  1085. if (!ops->ndo_set_mac_address) {
  1086. err = -EOPNOTSUPP;
  1087. goto errout;
  1088. }
  1089. if (!netif_device_present(dev)) {
  1090. err = -ENODEV;
  1091. goto errout;
  1092. }
  1093. len = sizeof(sa_family_t) + dev->addr_len;
  1094. sa = kmalloc(len, GFP_KERNEL);
  1095. if (!sa) {
  1096. err = -ENOMEM;
  1097. goto errout;
  1098. }
  1099. sa->sa_family = dev->type;
  1100. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  1101. dev->addr_len);
  1102. err = ops->ndo_set_mac_address(dev, sa);
  1103. kfree(sa);
  1104. if (err)
  1105. goto errout;
  1106. send_addr_notify = 1;
  1107. modified = 1;
  1108. }
  1109. if (tb[IFLA_MTU]) {
  1110. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1111. if (err < 0)
  1112. goto errout;
  1113. modified = 1;
  1114. }
  1115. if (tb[IFLA_GROUP]) {
  1116. dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
  1117. modified = 1;
  1118. }
  1119. /*
  1120. * Interface selected by interface index but interface
  1121. * name provided implies that a name change has been
  1122. * requested.
  1123. */
  1124. if (ifm->ifi_index > 0 && ifname[0]) {
  1125. err = dev_change_name(dev, ifname);
  1126. if (err < 0)
  1127. goto errout;
  1128. modified = 1;
  1129. }
  1130. if (tb[IFLA_IFALIAS]) {
  1131. err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
  1132. nla_len(tb[IFLA_IFALIAS]));
  1133. if (err < 0)
  1134. goto errout;
  1135. modified = 1;
  1136. }
  1137. if (tb[IFLA_BROADCAST]) {
  1138. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  1139. send_addr_notify = 1;
  1140. }
  1141. if (ifm->ifi_flags || ifm->ifi_change) {
  1142. err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
  1143. if (err < 0)
  1144. goto errout;
  1145. }
  1146. if (tb[IFLA_MASTER]) {
  1147. err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
  1148. if (err)
  1149. goto errout;
  1150. modified = 1;
  1151. }
  1152. if (tb[IFLA_TXQLEN])
  1153. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  1154. if (tb[IFLA_OPERSTATE])
  1155. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  1156. if (tb[IFLA_LINKMODE]) {
  1157. write_lock_bh(&dev_base_lock);
  1158. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  1159. write_unlock_bh(&dev_base_lock);
  1160. }
  1161. if (tb[IFLA_VFINFO_LIST]) {
  1162. struct nlattr *attr;
  1163. int rem;
  1164. nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
  1165. if (nla_type(attr) != IFLA_VF_INFO) {
  1166. err = -EINVAL;
  1167. goto errout;
  1168. }
  1169. err = do_setvfinfo(dev, attr);
  1170. if (err < 0)
  1171. goto errout;
  1172. modified = 1;
  1173. }
  1174. }
  1175. err = 0;
  1176. if (tb[IFLA_VF_PORTS]) {
  1177. struct nlattr *port[IFLA_PORT_MAX+1];
  1178. struct nlattr *attr;
  1179. int vf;
  1180. int rem;
  1181. err = -EOPNOTSUPP;
  1182. if (!ops->ndo_set_vf_port)
  1183. goto errout;
  1184. nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
  1185. if (nla_type(attr) != IFLA_VF_PORT)
  1186. continue;
  1187. err = nla_parse_nested(port, IFLA_PORT_MAX,
  1188. attr, ifla_port_policy);
  1189. if (err < 0)
  1190. goto errout;
  1191. if (!port[IFLA_PORT_VF]) {
  1192. err = -EOPNOTSUPP;
  1193. goto errout;
  1194. }
  1195. vf = nla_get_u32(port[IFLA_PORT_VF]);
  1196. err = ops->ndo_set_vf_port(dev, vf, port);
  1197. if (err < 0)
  1198. goto errout;
  1199. modified = 1;
  1200. }
  1201. }
  1202. err = 0;
  1203. if (tb[IFLA_PORT_SELF]) {
  1204. struct nlattr *port[IFLA_PORT_MAX+1];
  1205. err = nla_parse_nested(port, IFLA_PORT_MAX,
  1206. tb[IFLA_PORT_SELF], ifla_port_policy);
  1207. if (err < 0)
  1208. goto errout;
  1209. err = -EOPNOTSUPP;
  1210. if (ops->ndo_set_vf_port)
  1211. err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
  1212. if (err < 0)
  1213. goto errout;
  1214. modified = 1;
  1215. }
  1216. if (tb[IFLA_AF_SPEC]) {
  1217. struct nlattr *af;
  1218. int rem;
  1219. nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
  1220. const struct rtnl_af_ops *af_ops;
  1221. if (!(af_ops = rtnl_af_lookup(nla_type(af))))
  1222. BUG();
  1223. err = af_ops->set_link_af(dev, af);
  1224. if (err < 0)
  1225. goto errout;
  1226. modified = 1;
  1227. }
  1228. }
  1229. err = 0;
  1230. errout:
  1231. if (err < 0 && modified && net_ratelimit())
  1232. printk(KERN_WARNING "A link change request failed with "
  1233. "some changes committed already. Interface %s may "
  1234. "have been left with an inconsistent configuration, "
  1235. "please check.\n", dev->name);
  1236. if (send_addr_notify)
  1237. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  1238. return err;
  1239. }
  1240. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1241. {
  1242. struct net *net = sock_net(skb->sk);
  1243. struct ifinfomsg *ifm;
  1244. struct net_device *dev;
  1245. int err;
  1246. struct nlattr *tb[IFLA_MAX+1];
  1247. char ifname[IFNAMSIZ];
  1248. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1249. if (err < 0)
  1250. goto errout;
  1251. if (tb[IFLA_IFNAME])
  1252. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1253. else
  1254. ifname[0] = '\0';
  1255. err = -EINVAL;
  1256. ifm = nlmsg_data(nlh);
  1257. if (ifm->ifi_index > 0)
  1258. dev = __dev_get_by_index(net, ifm->ifi_index);
  1259. else if (tb[IFLA_IFNAME])
  1260. dev = __dev_get_by_name(net, ifname);
  1261. else
  1262. goto errout;
  1263. if (dev == NULL) {
  1264. err = -ENODEV;
  1265. goto errout;
  1266. }
  1267. err = validate_linkmsg(dev, tb);
  1268. if (err < 0)
  1269. goto errout;
  1270. err = do_setlink(dev, ifm, tb, ifname, 0);
  1271. errout:
  1272. return err;
  1273. }
  1274. static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1275. {
  1276. struct net *net = sock_net(skb->sk);
  1277. const struct rtnl_link_ops *ops;
  1278. struct net_device *dev;
  1279. struct ifinfomsg *ifm;
  1280. char ifname[IFNAMSIZ];
  1281. struct nlattr *tb[IFLA_MAX+1];
  1282. int err;
  1283. LIST_HEAD(list_kill);
  1284. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1285. if (err < 0)
  1286. return err;
  1287. if (tb[IFLA_IFNAME])
  1288. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1289. ifm = nlmsg_data(nlh);
  1290. if (ifm->ifi_index > 0)
  1291. dev = __dev_get_by_index(net, ifm->ifi_index);
  1292. else if (tb[IFLA_IFNAME])
  1293. dev = __dev_get_by_name(net, ifname);
  1294. else
  1295. return -EINVAL;
  1296. if (!dev)
  1297. return -ENODEV;
  1298. ops = dev->rtnl_link_ops;
  1299. if (!ops)
  1300. return -EOPNOTSUPP;
  1301. ops->dellink(dev, &list_kill);
  1302. unregister_netdevice_many(&list_kill);
  1303. list_del(&list_kill);
  1304. return 0;
  1305. }
  1306. int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
  1307. {
  1308. unsigned int old_flags;
  1309. int err;
  1310. old_flags = dev->flags;
  1311. if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
  1312. err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
  1313. if (err < 0)
  1314. return err;
  1315. }
  1316. dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
  1317. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  1318. __dev_notify_flags(dev, old_flags);
  1319. return 0;
  1320. }
  1321. EXPORT_SYMBOL(rtnl_configure_link);
  1322. struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
  1323. char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
  1324. {
  1325. int err;
  1326. struct net_device *dev;
  1327. unsigned int num_queues = 1;
  1328. unsigned int real_num_queues = 1;
  1329. if (ops->get_tx_queues) {
  1330. err = ops->get_tx_queues(src_net, tb, &num_queues,
  1331. &real_num_queues);
  1332. if (err)
  1333. goto err;
  1334. }
  1335. err = -ENOMEM;
  1336. dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
  1337. if (!dev)
  1338. goto err;
  1339. dev_net_set(dev, net);
  1340. dev->rtnl_link_ops = ops;
  1341. dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
  1342. dev->real_num_tx_queues = real_num_queues;
  1343. if (tb[IFLA_MTU])
  1344. dev->mtu = nla_get_u32(tb[IFLA_MTU]);
  1345. if (tb[IFLA_ADDRESS])
  1346. memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
  1347. nla_len(tb[IFLA_ADDRESS]));
  1348. if (tb[IFLA_BROADCAST])
  1349. memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
  1350. nla_len(tb[IFLA_BROADCAST]));
  1351. if (tb[IFLA_TXQLEN])
  1352. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  1353. if (tb[IFLA_OPERSTATE])
  1354. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  1355. if (tb[IFLA_LINKMODE])
  1356. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  1357. if (tb[IFLA_GROUP])
  1358. dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
  1359. return dev;
  1360. err:
  1361. return ERR_PTR(err);
  1362. }
  1363. EXPORT_SYMBOL(rtnl_create_link);
  1364. static int rtnl_group_changelink(struct net *net, int group,
  1365. struct ifinfomsg *ifm,
  1366. struct nlattr **tb)
  1367. {
  1368. struct net_device *dev;
  1369. int err;
  1370. for_each_netdev(net, dev) {
  1371. if (dev->group == group) {
  1372. err = do_setlink(dev, ifm, tb, NULL, 0);
  1373. if (err < 0)
  1374. return err;
  1375. }
  1376. }
  1377. return 0;
  1378. }
  1379. static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1380. {
  1381. struct net *net = sock_net(skb->sk);
  1382. const struct rtnl_link_ops *ops;
  1383. struct net_device *dev;
  1384. struct ifinfomsg *ifm;
  1385. char kind[MODULE_NAME_LEN];
  1386. char ifname[IFNAMSIZ];
  1387. struct nlattr *tb[IFLA_MAX+1];
  1388. struct nlattr *linkinfo[IFLA_INFO_MAX+1];
  1389. int err;
  1390. #ifdef CONFIG_MODULES
  1391. replay:
  1392. #endif
  1393. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1394. if (err < 0)
  1395. return err;
  1396. if (tb[IFLA_IFNAME])
  1397. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1398. else
  1399. ifname[0] = '\0';
  1400. ifm = nlmsg_data(nlh);
  1401. if (ifm->ifi_index > 0)
  1402. dev = __dev_get_by_index(net, ifm->ifi_index);
  1403. else {
  1404. if (ifname[0])
  1405. dev = __dev_get_by_name(net, ifname);
  1406. else
  1407. dev = NULL;
  1408. }
  1409. err = validate_linkmsg(dev, tb);
  1410. if (err < 0)
  1411. return err;
  1412. if (tb[IFLA_LINKINFO]) {
  1413. err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
  1414. tb[IFLA_LINKINFO], ifla_info_policy);
  1415. if (err < 0)
  1416. return err;
  1417. } else
  1418. memset(linkinfo, 0, sizeof(linkinfo));
  1419. if (linkinfo[IFLA_INFO_KIND]) {
  1420. nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
  1421. ops = rtnl_link_ops_get(kind);
  1422. } else {
  1423. kind[0] = '\0';
  1424. ops = NULL;
  1425. }
  1426. if (1) {
  1427. struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
  1428. struct net *dest_net;
  1429. if (ops) {
  1430. if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
  1431. err = nla_parse_nested(attr, ops->maxtype,
  1432. linkinfo[IFLA_INFO_DATA],
  1433. ops->policy);
  1434. if (err < 0)
  1435. return err;
  1436. data = attr;
  1437. }
  1438. if (ops->validate) {
  1439. err = ops->validate(tb, data);
  1440. if (err < 0)
  1441. return err;
  1442. }
  1443. }
  1444. if (dev) {
  1445. int modified = 0;
  1446. if (nlh->nlmsg_flags & NLM_F_EXCL)
  1447. return -EEXIST;
  1448. if (nlh->nlmsg_flags & NLM_F_REPLACE)
  1449. return -EOPNOTSUPP;
  1450. if (linkinfo[IFLA_INFO_DATA]) {
  1451. if (!ops || ops != dev->rtnl_link_ops ||
  1452. !ops->changelink)
  1453. return -EOPNOTSUPP;
  1454. err = ops->changelink(dev, tb, data);
  1455. if (err < 0)
  1456. return err;
  1457. modified = 1;
  1458. }
  1459. return do_setlink(dev, ifm, tb, ifname, modified);
  1460. }
  1461. if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
  1462. if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
  1463. return rtnl_group_changelink(net,
  1464. nla_get_u32(tb[IFLA_GROUP]),
  1465. ifm, tb);
  1466. return -ENODEV;
  1467. }
  1468. if (ifm->ifi_index)
  1469. return -EOPNOTSUPP;
  1470. if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
  1471. return -EOPNOTSUPP;
  1472. if (!ops) {
  1473. #ifdef CONFIG_MODULES
  1474. if (kind[0]) {
  1475. __rtnl_unlock();
  1476. request_module("rtnl-link-%s", kind);
  1477. rtnl_lock();
  1478. ops = rtnl_link_ops_get(kind);
  1479. if (ops)
  1480. goto replay;
  1481. }
  1482. #endif
  1483. return -EOPNOTSUPP;
  1484. }
  1485. if (!ifname[0])
  1486. snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
  1487. dest_net = rtnl_link_get_net(net, tb);
  1488. if (IS_ERR(dest_net))
  1489. return PTR_ERR(dest_net);
  1490. dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
  1491. if (IS_ERR(dev))
  1492. err = PTR_ERR(dev);
  1493. else if (ops->newlink)
  1494. err = ops->newlink(net, dev, tb, data);
  1495. else
  1496. err = register_netdevice(dev);
  1497. if (err < 0 && !IS_ERR(dev))
  1498. free_netdev(dev);
  1499. if (err < 0)
  1500. goto out;
  1501. err = rtnl_configure_link(dev, ifm);
  1502. if (err < 0)
  1503. unregister_netdevice(dev);
  1504. out:
  1505. put_net(dest_net);
  1506. return err;
  1507. }
  1508. }
  1509. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1510. {
  1511. struct net *net = sock_net(skb->sk);
  1512. struct ifinfomsg *ifm;
  1513. char ifname[IFNAMSIZ];
  1514. struct nlattr *tb[IFLA_MAX+1];
  1515. struct net_device *dev = NULL;
  1516. struct sk_buff *nskb;
  1517. int err;
  1518. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  1519. if (err < 0)
  1520. return err;
  1521. if (tb[IFLA_IFNAME])
  1522. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  1523. ifm = nlmsg_data(nlh);
  1524. if (ifm->ifi_index > 0)
  1525. dev = __dev_get_by_index(net, ifm->ifi_index);
  1526. else if (tb[IFLA_IFNAME])
  1527. dev = __dev_get_by_name(net, ifname);
  1528. else
  1529. return -EINVAL;
  1530. if (dev == NULL)
  1531. return -ENODEV;
  1532. nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
  1533. if (nskb == NULL)
  1534. return -ENOBUFS;
  1535. err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
  1536. nlh->nlmsg_seq, 0, 0);
  1537. if (err < 0) {
  1538. /* -EMSGSIZE implies BUG in if_nlmsg_size */
  1539. WARN_ON(err == -EMSGSIZE);
  1540. kfree_skb(nskb);
  1541. } else
  1542. err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
  1543. return err;
  1544. }
  1545. static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  1546. {
  1547. int idx;
  1548. int s_idx = cb->family;
  1549. if (s_idx == 0)
  1550. s_idx = 1;
  1551. for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
  1552. int type = cb->nlh->nlmsg_type-RTM_BASE;
  1553. if (idx < s_idx || idx == PF_PACKET)
  1554. continue;
  1555. if (rtnl_msg_handlers[idx] == NULL ||
  1556. rtnl_msg_handlers[idx][type].dumpit == NULL)
  1557. continue;
  1558. if (idx > s_idx)
  1559. memset(&cb->args[0], 0, sizeof(cb->args));
  1560. if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
  1561. break;
  1562. }
  1563. cb->family = idx;
  1564. return skb->len;
  1565. }
  1566. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  1567. {
  1568. struct net *net = dev_net(dev);
  1569. struct sk_buff *skb;
  1570. int err = -ENOBUFS;
  1571. skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
  1572. if (skb == NULL)
  1573. goto errout;
  1574. err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
  1575. if (err < 0) {
  1576. /* -EMSGSIZE implies BUG in if_nlmsg_size() */
  1577. WARN_ON(err == -EMSGSIZE);
  1578. kfree_skb(skb);
  1579. goto errout;
  1580. }
  1581. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
  1582. return;
  1583. errout:
  1584. if (err < 0)
  1585. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  1586. }
  1587. /* Protected by RTNL sempahore. */
  1588. static struct rtattr **rta_buf;
  1589. static int rtattr_max;
  1590. /* Process one rtnetlink message. */
  1591. static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1592. {
  1593. struct net *net = sock_net(skb->sk);
  1594. rtnl_doit_func doit;
  1595. int sz_idx, kind;
  1596. int min_len;
  1597. int family;
  1598. int type;
  1599. int err;
  1600. type = nlh->nlmsg_type;
  1601. if (type > RTM_MAX)
  1602. return -EOPNOTSUPP;
  1603. type -= RTM_BASE;
  1604. /* All the messages must have at least 1 byte length */
  1605. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  1606. return 0;
  1607. family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
  1608. sz_idx = type>>2;
  1609. kind = type&3;
  1610. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
  1611. return -EPERM;
  1612. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  1613. struct sock *rtnl;
  1614. rtnl_dumpit_func dumpit;
  1615. dumpit = rtnl_get_dumpit(family, type);
  1616. if (dumpit == NULL)
  1617. return -EOPNOTSUPP;
  1618. __rtnl_unlock();
  1619. rtnl = net->rtnl;
  1620. err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
  1621. rtnl_lock();
  1622. return err;
  1623. }
  1624. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  1625. min_len = rtm_min[sz_idx];
  1626. if (nlh->nlmsg_len < min_len)
  1627. return -EINVAL;
  1628. if (nlh->nlmsg_len > min_len) {
  1629. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1630. struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
  1631. while (RTA_OK(attr, attrlen)) {
  1632. unsigned flavor = attr->rta_type;
  1633. if (flavor) {
  1634. if (flavor > rta_max[sz_idx])
  1635. return -EINVAL;
  1636. rta_buf[flavor-1] = attr;
  1637. }
  1638. attr = RTA_NEXT(attr, attrlen);
  1639. }
  1640. }
  1641. doit = rtnl_get_doit(family, type);
  1642. if (doit == NULL)
  1643. return -EOPNOTSUPP;
  1644. return doit(skb, nlh, (void *)&rta_buf[0]);
  1645. }
  1646. static void rtnetlink_rcv(struct sk_buff *skb)
  1647. {
  1648. rtnl_lock();
  1649. netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
  1650. rtnl_unlock();
  1651. }
  1652. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  1653. {
  1654. struct net_device *dev = ptr;
  1655. switch (event) {
  1656. case NETDEV_UP:
  1657. case NETDEV_DOWN:
  1658. case NETDEV_PRE_UP:
  1659. case NETDEV_POST_INIT:
  1660. case NETDEV_REGISTER:
  1661. case NETDEV_CHANGE:
  1662. case NETDEV_PRE_TYPE_CHANGE:
  1663. case NETDEV_GOING_DOWN:
  1664. case NETDEV_UNREGISTER:
  1665. case NETDEV_UNREGISTER_BATCH:
  1666. case NETDEV_RELEASE:
  1667. case NETDEV_JOIN:
  1668. break;
  1669. default:
  1670. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  1671. break;
  1672. }
  1673. return NOTIFY_DONE;
  1674. }
  1675. static struct notifier_block rtnetlink_dev_notifier = {
  1676. .notifier_call = rtnetlink_event,
  1677. };
  1678. static int __net_init rtnetlink_net_init(struct net *net)
  1679. {
  1680. struct sock *sk;
  1681. sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
  1682. rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
  1683. if (!sk)
  1684. return -ENOMEM;
  1685. net->rtnl = sk;
  1686. return 0;
  1687. }
  1688. static void __net_exit rtnetlink_net_exit(struct net *net)
  1689. {
  1690. netlink_kernel_release(net->rtnl);
  1691. net->rtnl = NULL;
  1692. }
  1693. static struct pernet_operations rtnetlink_net_ops = {
  1694. .init = rtnetlink_net_init,
  1695. .exit = rtnetlink_net_exit,
  1696. };
  1697. void __init rtnetlink_init(void)
  1698. {
  1699. int i;
  1700. rtattr_max = 0;
  1701. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  1702. if (rta_max[i] > rtattr_max)
  1703. rtattr_max = rta_max[i];
  1704. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  1705. if (!rta_buf)
  1706. panic("rtnetlink_init: cannot allocate rta_buf\n");
  1707. if (register_pernet_subsys(&rtnetlink_net_ops))
  1708. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  1709. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  1710. register_netdevice_notifier(&rtnetlink_dev_notifier);
  1711. rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
  1712. rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
  1713. rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
  1714. rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
  1715. rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
  1716. rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
  1717. }