rtnetlink.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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 <asm/uaccess.h>
  38. #include <asm/system.h>
  39. #include <asm/string.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. struct rtnl_link
  52. {
  53. rtnl_doit_func doit;
  54. rtnl_dumpit_func dumpit;
  55. };
  56. static DEFINE_MUTEX(rtnl_mutex);
  57. static struct sock *rtnl;
  58. void rtnl_lock(void)
  59. {
  60. mutex_lock(&rtnl_mutex);
  61. }
  62. void __rtnl_unlock(void)
  63. {
  64. mutex_unlock(&rtnl_mutex);
  65. }
  66. void rtnl_unlock(void)
  67. {
  68. mutex_unlock(&rtnl_mutex);
  69. if (rtnl && rtnl->sk_receive_queue.qlen)
  70. rtnl->sk_data_ready(rtnl, 0);
  71. netdev_run_todo();
  72. }
  73. int rtnl_trylock(void)
  74. {
  75. return mutex_trylock(&rtnl_mutex);
  76. }
  77. int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
  78. {
  79. memset(tb, 0, sizeof(struct rtattr*)*maxattr);
  80. while (RTA_OK(rta, len)) {
  81. unsigned flavor = rta->rta_type;
  82. if (flavor && flavor <= maxattr)
  83. tb[flavor-1] = rta;
  84. rta = RTA_NEXT(rta, len);
  85. }
  86. return 0;
  87. }
  88. int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
  89. struct rtattr *rta, int len)
  90. {
  91. if (RTA_PAYLOAD(rta) < len)
  92. return -1;
  93. if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
  94. rta = RTA_DATA(rta) + RTA_ALIGN(len);
  95. return rtattr_parse_nested(tb, maxattr, rta);
  96. }
  97. memset(tb, 0, sizeof(struct rtattr *) * maxattr);
  98. return 0;
  99. }
  100. static struct rtnl_link *rtnl_msg_handlers[NPROTO];
  101. static inline int rtm_msgindex(int msgtype)
  102. {
  103. int msgindex = msgtype - RTM_BASE;
  104. /*
  105. * msgindex < 0 implies someone tried to register a netlink
  106. * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
  107. * the message type has not been added to linux/rtnetlink.h
  108. */
  109. BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
  110. return msgindex;
  111. }
  112. static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
  113. {
  114. struct rtnl_link *tab;
  115. tab = rtnl_msg_handlers[protocol];
  116. if (tab == NULL || tab[msgindex].doit == NULL)
  117. tab = rtnl_msg_handlers[PF_UNSPEC];
  118. return tab ? tab[msgindex].doit : NULL;
  119. }
  120. static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
  121. {
  122. struct rtnl_link *tab;
  123. tab = rtnl_msg_handlers[protocol];
  124. if (tab == NULL || tab[msgindex].dumpit == NULL)
  125. tab = rtnl_msg_handlers[PF_UNSPEC];
  126. return tab ? tab[msgindex].dumpit : NULL;
  127. }
  128. /**
  129. * __rtnl_register - Register a rtnetlink message type
  130. * @protocol: Protocol family or PF_UNSPEC
  131. * @msgtype: rtnetlink message type
  132. * @doit: Function pointer called for each request message
  133. * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
  134. *
  135. * Registers the specified function pointers (at least one of them has
  136. * to be non-NULL) to be called whenever a request message for the
  137. * specified protocol family and message type is received.
  138. *
  139. * The special protocol family PF_UNSPEC may be used to define fallback
  140. * function pointers for the case when no entry for the specific protocol
  141. * family exists.
  142. *
  143. * Returns 0 on success or a negative error code.
  144. */
  145. int __rtnl_register(int protocol, int msgtype,
  146. rtnl_doit_func doit, rtnl_dumpit_func dumpit)
  147. {
  148. struct rtnl_link *tab;
  149. int msgindex;
  150. BUG_ON(protocol < 0 || protocol >= NPROTO);
  151. msgindex = rtm_msgindex(msgtype);
  152. tab = rtnl_msg_handlers[protocol];
  153. if (tab == NULL) {
  154. tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
  155. if (tab == NULL)
  156. return -ENOBUFS;
  157. rtnl_msg_handlers[protocol] = tab;
  158. }
  159. if (doit)
  160. tab[msgindex].doit = doit;
  161. if (dumpit)
  162. tab[msgindex].dumpit = dumpit;
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_GPL(__rtnl_register);
  166. /**
  167. * rtnl_register - Register a rtnetlink message type
  168. *
  169. * Identical to __rtnl_register() but panics on failure. This is useful
  170. * as failure of this function is very unlikely, it can only happen due
  171. * to lack of memory when allocating the chain to store all message
  172. * handlers for a protocol. Meant for use in init functions where lack
  173. * of memory implies no sense in continueing.
  174. */
  175. void rtnl_register(int protocol, int msgtype,
  176. rtnl_doit_func doit, rtnl_dumpit_func dumpit)
  177. {
  178. if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
  179. panic("Unable to register rtnetlink message handler, "
  180. "protocol = %d, message type = %d\n",
  181. protocol, msgtype);
  182. }
  183. EXPORT_SYMBOL_GPL(rtnl_register);
  184. /**
  185. * rtnl_unregister - Unregister a rtnetlink message type
  186. * @protocol: Protocol family or PF_UNSPEC
  187. * @msgtype: rtnetlink message type
  188. *
  189. * Returns 0 on success or a negative error code.
  190. */
  191. int rtnl_unregister(int protocol, int msgtype)
  192. {
  193. int msgindex;
  194. BUG_ON(protocol < 0 || protocol >= NPROTO);
  195. msgindex = rtm_msgindex(msgtype);
  196. if (rtnl_msg_handlers[protocol] == NULL)
  197. return -ENOENT;
  198. rtnl_msg_handlers[protocol][msgindex].doit = NULL;
  199. rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
  200. return 0;
  201. }
  202. EXPORT_SYMBOL_GPL(rtnl_unregister);
  203. /**
  204. * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
  205. * @protocol : Protocol family or PF_UNSPEC
  206. *
  207. * Identical to calling rtnl_unregster() for all registered message types
  208. * of a certain protocol family.
  209. */
  210. void rtnl_unregister_all(int protocol)
  211. {
  212. BUG_ON(protocol < 0 || protocol >= NPROTO);
  213. kfree(rtnl_msg_handlers[protocol]);
  214. rtnl_msg_handlers[protocol] = NULL;
  215. }
  216. EXPORT_SYMBOL_GPL(rtnl_unregister_all);
  217. static LIST_HEAD(link_ops);
  218. /**
  219. * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
  220. * @ops: struct rtnl_link_ops * to register
  221. *
  222. * The caller must hold the rtnl_mutex. This function should be used
  223. * by drivers that create devices during module initialization. It
  224. * must be called before registering the devices.
  225. *
  226. * Returns 0 on success or a negative error code.
  227. */
  228. int __rtnl_link_register(struct rtnl_link_ops *ops)
  229. {
  230. list_add_tail(&ops->list, &link_ops);
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(__rtnl_link_register);
  234. /**
  235. * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
  236. * @ops: struct rtnl_link_ops * to register
  237. *
  238. * Returns 0 on success or a negative error code.
  239. */
  240. int rtnl_link_register(struct rtnl_link_ops *ops)
  241. {
  242. int err;
  243. rtnl_lock();
  244. err = __rtnl_link_register(ops);
  245. rtnl_unlock();
  246. return err;
  247. }
  248. EXPORT_SYMBOL_GPL(rtnl_link_register);
  249. /**
  250. * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
  251. * @ops: struct rtnl_link_ops * to unregister
  252. *
  253. * The caller must hold the rtnl_mutex. This function should be used
  254. * by drivers that unregister devices during module unloading. It must
  255. * be called after unregistering the devices.
  256. */
  257. void __rtnl_link_unregister(struct rtnl_link_ops *ops)
  258. {
  259. list_del(&ops->list);
  260. }
  261. EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
  262. /**
  263. * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
  264. * @ops: struct rtnl_link_ops * to unregister
  265. */
  266. void rtnl_link_unregister(struct rtnl_link_ops *ops)
  267. {
  268. rtnl_lock();
  269. __rtnl_link_unregister(ops);
  270. rtnl_unlock();
  271. }
  272. EXPORT_SYMBOL_GPL(rtnl_link_unregister);
  273. static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
  274. {
  275. const struct rtnl_link_ops *ops;
  276. list_for_each_entry(ops, &link_ops, list) {
  277. if (!strcmp(ops->kind, kind))
  278. return ops;
  279. }
  280. return NULL;
  281. }
  282. static size_t rtnl_link_get_size(const struct net_device *dev)
  283. {
  284. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  285. size_t size;
  286. if (!ops)
  287. return 0;
  288. size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
  289. nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
  290. if (ops->get_size)
  291. /* IFLA_INFO_DATA + nested data */
  292. size += nlmsg_total_size(sizeof(struct nlattr)) +
  293. ops->get_size(dev);
  294. if (ops->get_xstats_size)
  295. size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
  296. return size;
  297. }
  298. static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
  299. {
  300. const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
  301. struct nlattr *linkinfo, *data;
  302. int err = -EMSGSIZE;
  303. linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
  304. if (linkinfo == NULL)
  305. goto out;
  306. if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
  307. goto err_cancel_link;
  308. if (ops->fill_xstats) {
  309. err = ops->fill_xstats(skb, dev);
  310. if (err < 0)
  311. goto err_cancel_link;
  312. }
  313. if (ops->fill_info) {
  314. data = nla_nest_start(skb, IFLA_INFO_DATA);
  315. if (data == NULL)
  316. goto err_cancel_link;
  317. err = ops->fill_info(skb, dev);
  318. if (err < 0)
  319. goto err_cancel_data;
  320. nla_nest_end(skb, data);
  321. }
  322. nla_nest_end(skb, linkinfo);
  323. return 0;
  324. err_cancel_data:
  325. nla_nest_cancel(skb, data);
  326. err_cancel_link:
  327. nla_nest_cancel(skb, linkinfo);
  328. out:
  329. return err;
  330. }
  331. static const int rtm_min[RTM_NR_FAMILIES] =
  332. {
  333. [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  334. [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
  335. [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  336. [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
  337. [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  338. [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  339. [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  340. [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
  341. [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  342. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  343. };
  344. static const int rta_max[RTM_NR_FAMILIES] =
  345. {
  346. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  347. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  348. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  349. [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
  350. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  351. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  352. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  353. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  354. };
  355. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  356. {
  357. struct rtattr *rta;
  358. int size = RTA_LENGTH(attrlen);
  359. rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
  360. rta->rta_type = attrtype;
  361. rta->rta_len = size;
  362. memcpy(RTA_DATA(rta), data, attrlen);
  363. memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
  364. }
  365. size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
  366. {
  367. size_t ret = RTA_PAYLOAD(rta);
  368. char *src = RTA_DATA(rta);
  369. if (ret > 0 && src[ret - 1] == '\0')
  370. ret--;
  371. if (size > 0) {
  372. size_t len = (ret >= size) ? size - 1 : ret;
  373. memset(dest, 0, size);
  374. memcpy(dest, src, len);
  375. }
  376. return ret;
  377. }
  378. int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  379. {
  380. int err = 0;
  381. NETLINK_CB(skb).dst_group = group;
  382. if (echo)
  383. atomic_inc(&skb->users);
  384. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  385. if (echo)
  386. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  387. return err;
  388. }
  389. int rtnl_unicast(struct sk_buff *skb, u32 pid)
  390. {
  391. return nlmsg_unicast(rtnl, skb, pid);
  392. }
  393. int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
  394. struct nlmsghdr *nlh, gfp_t flags)
  395. {
  396. int report = 0;
  397. if (nlh)
  398. report = nlmsg_report(nlh);
  399. return nlmsg_notify(rtnl, skb, pid, group, report, flags);
  400. }
  401. void rtnl_set_sk_err(u32 group, int error)
  402. {
  403. netlink_set_err(rtnl, 0, group, error);
  404. }
  405. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  406. {
  407. struct nlattr *mx;
  408. int i, valid = 0;
  409. mx = nla_nest_start(skb, RTA_METRICS);
  410. if (mx == NULL)
  411. return -ENOBUFS;
  412. for (i = 0; i < RTAX_MAX; i++) {
  413. if (metrics[i]) {
  414. valid++;
  415. NLA_PUT_U32(skb, i+1, metrics[i]);
  416. }
  417. }
  418. if (!valid) {
  419. nla_nest_cancel(skb, mx);
  420. return 0;
  421. }
  422. return nla_nest_end(skb, mx);
  423. nla_put_failure:
  424. return nla_nest_cancel(skb, mx);
  425. }
  426. int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
  427. u32 ts, u32 tsage, long expires, u32 error)
  428. {
  429. struct rta_cacheinfo ci = {
  430. .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
  431. .rta_used = dst->__use,
  432. .rta_clntref = atomic_read(&(dst->__refcnt)),
  433. .rta_error = error,
  434. .rta_id = id,
  435. .rta_ts = ts,
  436. .rta_tsage = tsage,
  437. };
  438. if (expires)
  439. ci.rta_expires = jiffies_to_clock_t(expires);
  440. return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  441. }
  442. EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
  443. static void set_operstate(struct net_device *dev, unsigned char transition)
  444. {
  445. unsigned char operstate = dev->operstate;
  446. switch(transition) {
  447. case IF_OPER_UP:
  448. if ((operstate == IF_OPER_DORMANT ||
  449. operstate == IF_OPER_UNKNOWN) &&
  450. !netif_dormant(dev))
  451. operstate = IF_OPER_UP;
  452. break;
  453. case IF_OPER_DORMANT:
  454. if (operstate == IF_OPER_UP ||
  455. operstate == IF_OPER_UNKNOWN)
  456. operstate = IF_OPER_DORMANT;
  457. break;
  458. }
  459. if (dev->operstate != operstate) {
  460. write_lock_bh(&dev_base_lock);
  461. dev->operstate = operstate;
  462. write_unlock_bh(&dev_base_lock);
  463. netdev_state_change(dev);
  464. }
  465. }
  466. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  467. struct net_device_stats *b)
  468. {
  469. a->rx_packets = b->rx_packets;
  470. a->tx_packets = b->tx_packets;
  471. a->rx_bytes = b->rx_bytes;
  472. a->tx_bytes = b->tx_bytes;
  473. a->rx_errors = b->rx_errors;
  474. a->tx_errors = b->tx_errors;
  475. a->rx_dropped = b->rx_dropped;
  476. a->tx_dropped = b->tx_dropped;
  477. a->multicast = b->multicast;
  478. a->collisions = b->collisions;
  479. a->rx_length_errors = b->rx_length_errors;
  480. a->rx_over_errors = b->rx_over_errors;
  481. a->rx_crc_errors = b->rx_crc_errors;
  482. a->rx_frame_errors = b->rx_frame_errors;
  483. a->rx_fifo_errors = b->rx_fifo_errors;
  484. a->rx_missed_errors = b->rx_missed_errors;
  485. a->tx_aborted_errors = b->tx_aborted_errors;
  486. a->tx_carrier_errors = b->tx_carrier_errors;
  487. a->tx_fifo_errors = b->tx_fifo_errors;
  488. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  489. a->tx_window_errors = b->tx_window_errors;
  490. a->rx_compressed = b->rx_compressed;
  491. a->tx_compressed = b->tx_compressed;
  492. };
  493. static inline size_t if_nlmsg_size(const struct net_device *dev)
  494. {
  495. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  496. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  497. + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
  498. + nla_total_size(sizeof(struct rtnl_link_ifmap))
  499. + nla_total_size(sizeof(struct rtnl_link_stats))
  500. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  501. + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
  502. + nla_total_size(4) /* IFLA_TXQLEN */
  503. + nla_total_size(4) /* IFLA_WEIGHT */
  504. + nla_total_size(4) /* IFLA_MTU */
  505. + nla_total_size(4) /* IFLA_LINK */
  506. + nla_total_size(4) /* IFLA_MASTER */
  507. + nla_total_size(1) /* IFLA_OPERSTATE */
  508. + nla_total_size(1) /* IFLA_LINKMODE */
  509. + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
  510. }
  511. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  512. int type, u32 pid, u32 seq, u32 change,
  513. unsigned int flags)
  514. {
  515. struct ifinfomsg *ifm;
  516. struct nlmsghdr *nlh;
  517. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  518. if (nlh == NULL)
  519. return -EMSGSIZE;
  520. ifm = nlmsg_data(nlh);
  521. ifm->ifi_family = AF_UNSPEC;
  522. ifm->__ifi_pad = 0;
  523. ifm->ifi_type = dev->type;
  524. ifm->ifi_index = dev->ifindex;
  525. ifm->ifi_flags = dev_get_flags(dev);
  526. ifm->ifi_change = change;
  527. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  528. NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
  529. NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
  530. NLA_PUT_U8(skb, IFLA_OPERSTATE,
  531. netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
  532. NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
  533. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  534. if (dev->ifindex != dev->iflink)
  535. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  536. if (dev->master)
  537. NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
  538. if (dev->qdisc_sleeping)
  539. NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
  540. if (1) {
  541. struct rtnl_link_ifmap map = {
  542. .mem_start = dev->mem_start,
  543. .mem_end = dev->mem_end,
  544. .base_addr = dev->base_addr,
  545. .irq = dev->irq,
  546. .dma = dev->dma,
  547. .port = dev->if_port,
  548. };
  549. NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  550. }
  551. if (dev->addr_len) {
  552. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  553. NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  554. }
  555. if (dev->get_stats) {
  556. struct net_device_stats *stats = dev->get_stats(dev);
  557. if (stats) {
  558. struct nlattr *attr;
  559. attr = nla_reserve(skb, IFLA_STATS,
  560. sizeof(struct rtnl_link_stats));
  561. if (attr == NULL)
  562. goto nla_put_failure;
  563. copy_rtnl_link_stats(nla_data(attr), stats);
  564. }
  565. }
  566. if (dev->rtnl_link_ops) {
  567. if (rtnl_link_fill(skb, dev) < 0)
  568. goto nla_put_failure;
  569. }
  570. return nlmsg_end(skb, nlh);
  571. nla_put_failure:
  572. nlmsg_cancel(skb, nlh);
  573. return -EMSGSIZE;
  574. }
  575. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  576. {
  577. int idx;
  578. int s_idx = cb->args[0];
  579. struct net_device *dev;
  580. idx = 0;
  581. for_each_netdev(dev) {
  582. if (idx < s_idx)
  583. goto cont;
  584. if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
  585. NETLINK_CB(cb->skb).pid,
  586. cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
  587. break;
  588. cont:
  589. idx++;
  590. }
  591. cb->args[0] = idx;
  592. return skb->len;
  593. }
  594. static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
  595. [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
  596. [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  597. [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  598. [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
  599. [IFLA_MTU] = { .type = NLA_U32 },
  600. [IFLA_TXQLEN] = { .type = NLA_U32 },
  601. [IFLA_WEIGHT] = { .type = NLA_U32 },
  602. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  603. [IFLA_LINKMODE] = { .type = NLA_U8 },
  604. };
  605. static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
  606. [IFLA_INFO_KIND] = { .type = NLA_STRING },
  607. [IFLA_INFO_DATA] = { .type = NLA_NESTED },
  608. };
  609. static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
  610. struct nlattr **tb, char *ifname, int modified)
  611. {
  612. int send_addr_notify = 0;
  613. int err;
  614. if (tb[IFLA_MAP]) {
  615. struct rtnl_link_ifmap *u_map;
  616. struct ifmap k_map;
  617. if (!dev->set_config) {
  618. err = -EOPNOTSUPP;
  619. goto errout;
  620. }
  621. if (!netif_device_present(dev)) {
  622. err = -ENODEV;
  623. goto errout;
  624. }
  625. u_map = nla_data(tb[IFLA_MAP]);
  626. k_map.mem_start = (unsigned long) u_map->mem_start;
  627. k_map.mem_end = (unsigned long) u_map->mem_end;
  628. k_map.base_addr = (unsigned short) u_map->base_addr;
  629. k_map.irq = (unsigned char) u_map->irq;
  630. k_map.dma = (unsigned char) u_map->dma;
  631. k_map.port = (unsigned char) u_map->port;
  632. err = dev->set_config(dev, &k_map);
  633. if (err < 0)
  634. goto errout;
  635. modified = 1;
  636. }
  637. if (tb[IFLA_ADDRESS]) {
  638. struct sockaddr *sa;
  639. int len;
  640. if (!dev->set_mac_address) {
  641. err = -EOPNOTSUPP;
  642. goto errout;
  643. }
  644. if (!netif_device_present(dev)) {
  645. err = -ENODEV;
  646. goto errout;
  647. }
  648. len = sizeof(sa_family_t) + dev->addr_len;
  649. sa = kmalloc(len, GFP_KERNEL);
  650. if (!sa) {
  651. err = -ENOMEM;
  652. goto errout;
  653. }
  654. sa->sa_family = dev->type;
  655. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  656. dev->addr_len);
  657. err = dev->set_mac_address(dev, sa);
  658. kfree(sa);
  659. if (err)
  660. goto errout;
  661. send_addr_notify = 1;
  662. modified = 1;
  663. }
  664. if (tb[IFLA_MTU]) {
  665. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  666. if (err < 0)
  667. goto errout;
  668. modified = 1;
  669. }
  670. /*
  671. * Interface selected by interface index but interface
  672. * name provided implies that a name change has been
  673. * requested.
  674. */
  675. if (ifm->ifi_index > 0 && ifname[0]) {
  676. err = dev_change_name(dev, ifname);
  677. if (err < 0)
  678. goto errout;
  679. modified = 1;
  680. }
  681. if (tb[IFLA_BROADCAST]) {
  682. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  683. send_addr_notify = 1;
  684. }
  685. if (ifm->ifi_flags || ifm->ifi_change) {
  686. unsigned int flags = ifm->ifi_flags;
  687. /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
  688. if (ifm->ifi_change)
  689. flags = (flags & ifm->ifi_change) |
  690. (dev->flags & ~ifm->ifi_change);
  691. dev_change_flags(dev, flags);
  692. }
  693. if (tb[IFLA_TXQLEN])
  694. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  695. if (tb[IFLA_WEIGHT])
  696. dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
  697. if (tb[IFLA_OPERSTATE])
  698. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  699. if (tb[IFLA_LINKMODE]) {
  700. write_lock_bh(&dev_base_lock);
  701. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  702. write_unlock_bh(&dev_base_lock);
  703. }
  704. err = 0;
  705. errout:
  706. if (err < 0 && modified && net_ratelimit())
  707. printk(KERN_WARNING "A link change request failed with "
  708. "some changes comitted already. Interface %s may "
  709. "have been left with an inconsistent configuration, "
  710. "please check.\n", dev->name);
  711. if (send_addr_notify)
  712. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  713. return err;
  714. }
  715. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  716. {
  717. struct ifinfomsg *ifm;
  718. struct net_device *dev;
  719. int err;
  720. struct nlattr *tb[IFLA_MAX+1];
  721. char ifname[IFNAMSIZ];
  722. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  723. if (err < 0)
  724. goto errout;
  725. if (tb[IFLA_IFNAME])
  726. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  727. else
  728. ifname[0] = '\0';
  729. err = -EINVAL;
  730. ifm = nlmsg_data(nlh);
  731. if (ifm->ifi_index > 0)
  732. dev = dev_get_by_index(ifm->ifi_index);
  733. else if (tb[IFLA_IFNAME])
  734. dev = dev_get_by_name(ifname);
  735. else
  736. goto errout;
  737. if (dev == NULL) {
  738. err = -ENODEV;
  739. goto errout;
  740. }
  741. if (tb[IFLA_ADDRESS] &&
  742. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  743. goto errout_dev;
  744. if (tb[IFLA_BROADCAST] &&
  745. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  746. goto errout_dev;
  747. err = do_setlink(dev, ifm, tb, ifname, 0);
  748. errout_dev:
  749. dev_put(dev);
  750. errout:
  751. return err;
  752. }
  753. static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  754. {
  755. const struct rtnl_link_ops *ops;
  756. struct net_device *dev;
  757. struct ifinfomsg *ifm;
  758. char ifname[IFNAMSIZ];
  759. struct nlattr *tb[IFLA_MAX+1];
  760. int err;
  761. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  762. if (err < 0)
  763. return err;
  764. if (tb[IFLA_IFNAME])
  765. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  766. ifm = nlmsg_data(nlh);
  767. if (ifm->ifi_index > 0)
  768. dev = __dev_get_by_index(ifm->ifi_index);
  769. else if (tb[IFLA_IFNAME])
  770. dev = __dev_get_by_name(ifname);
  771. else
  772. return -EINVAL;
  773. if (!dev)
  774. return -ENODEV;
  775. ops = dev->rtnl_link_ops;
  776. if (!ops)
  777. return -EOPNOTSUPP;
  778. ops->dellink(dev);
  779. return 0;
  780. }
  781. static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  782. {
  783. const struct rtnl_link_ops *ops;
  784. struct net_device *dev;
  785. struct ifinfomsg *ifm;
  786. char kind[MODULE_NAME_LEN];
  787. char ifname[IFNAMSIZ];
  788. struct nlattr *tb[IFLA_MAX+1];
  789. struct nlattr *linkinfo[IFLA_INFO_MAX+1];
  790. int err;
  791. replay:
  792. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  793. if (err < 0)
  794. return err;
  795. if (tb[IFLA_IFNAME])
  796. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  797. else
  798. ifname[0] = '\0';
  799. ifm = nlmsg_data(nlh);
  800. if (ifm->ifi_index > 0)
  801. dev = __dev_get_by_index(ifm->ifi_index);
  802. else if (ifname[0])
  803. dev = __dev_get_by_name(ifname);
  804. else
  805. dev = NULL;
  806. if (tb[IFLA_LINKINFO]) {
  807. err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
  808. tb[IFLA_LINKINFO], ifla_info_policy);
  809. if (err < 0)
  810. return err;
  811. } else
  812. memset(linkinfo, 0, sizeof(linkinfo));
  813. if (linkinfo[IFLA_INFO_KIND]) {
  814. nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
  815. ops = rtnl_link_ops_get(kind);
  816. } else {
  817. kind[0] = '\0';
  818. ops = NULL;
  819. }
  820. if (1) {
  821. struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
  822. if (ops) {
  823. if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
  824. err = nla_parse_nested(attr, ops->maxtype,
  825. linkinfo[IFLA_INFO_DATA],
  826. ops->policy);
  827. if (err < 0)
  828. return err;
  829. data = attr;
  830. }
  831. if (ops->validate) {
  832. err = ops->validate(tb, data);
  833. if (err < 0)
  834. return err;
  835. }
  836. }
  837. if (dev) {
  838. int modified = 0;
  839. if (nlh->nlmsg_flags & NLM_F_EXCL)
  840. return -EEXIST;
  841. if (nlh->nlmsg_flags & NLM_F_REPLACE)
  842. return -EOPNOTSUPP;
  843. if (linkinfo[IFLA_INFO_DATA]) {
  844. if (!ops || ops != dev->rtnl_link_ops ||
  845. !ops->changelink)
  846. return -EOPNOTSUPP;
  847. err = ops->changelink(dev, tb, data);
  848. if (err < 0)
  849. return err;
  850. modified = 1;
  851. }
  852. return do_setlink(dev, ifm, tb, ifname, modified);
  853. }
  854. if (!(nlh->nlmsg_flags & NLM_F_CREATE))
  855. return -ENODEV;
  856. if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
  857. return -EOPNOTSUPP;
  858. if (tb[IFLA_ADDRESS] || tb[IFLA_BROADCAST] || tb[IFLA_MAP] ||
  859. tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
  860. return -EOPNOTSUPP;
  861. if (!ops) {
  862. #ifdef CONFIG_KMOD
  863. if (kind[0]) {
  864. __rtnl_unlock();
  865. request_module("rtnl-link-%s", kind);
  866. rtnl_lock();
  867. ops = rtnl_link_ops_get(kind);
  868. if (ops)
  869. goto replay;
  870. }
  871. #endif
  872. return -EOPNOTSUPP;
  873. }
  874. if (!ifname[0])
  875. snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
  876. dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
  877. if (!dev)
  878. return -ENOMEM;
  879. if (strchr(dev->name, '%')) {
  880. err = dev_alloc_name(dev, dev->name);
  881. if (err < 0)
  882. goto err_free;
  883. }
  884. dev->rtnl_link_ops = ops;
  885. if (tb[IFLA_MTU])
  886. dev->mtu = nla_get_u32(tb[IFLA_MTU]);
  887. if (tb[IFLA_TXQLEN])
  888. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  889. if (tb[IFLA_WEIGHT])
  890. dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
  891. if (tb[IFLA_OPERSTATE])
  892. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  893. if (tb[IFLA_LINKMODE])
  894. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  895. err = ops->newlink(dev, tb, data);
  896. err_free:
  897. if (err < 0)
  898. free_netdev(dev);
  899. return err;
  900. }
  901. }
  902. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  903. {
  904. struct ifinfomsg *ifm;
  905. struct nlattr *tb[IFLA_MAX+1];
  906. struct net_device *dev = NULL;
  907. struct sk_buff *nskb;
  908. int err;
  909. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  910. if (err < 0)
  911. return err;
  912. ifm = nlmsg_data(nlh);
  913. if (ifm->ifi_index > 0) {
  914. dev = dev_get_by_index(ifm->ifi_index);
  915. if (dev == NULL)
  916. return -ENODEV;
  917. } else
  918. return -EINVAL;
  919. nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
  920. if (nskb == NULL) {
  921. err = -ENOBUFS;
  922. goto errout;
  923. }
  924. err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
  925. nlh->nlmsg_seq, 0, 0);
  926. if (err < 0) {
  927. /* -EMSGSIZE implies BUG in if_nlmsg_size */
  928. WARN_ON(err == -EMSGSIZE);
  929. kfree_skb(nskb);
  930. goto errout;
  931. }
  932. err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
  933. errout:
  934. dev_put(dev);
  935. return err;
  936. }
  937. static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  938. {
  939. int idx;
  940. int s_idx = cb->family;
  941. if (s_idx == 0)
  942. s_idx = 1;
  943. for (idx=1; idx<NPROTO; idx++) {
  944. int type = cb->nlh->nlmsg_type-RTM_BASE;
  945. if (idx < s_idx || idx == PF_PACKET)
  946. continue;
  947. if (rtnl_msg_handlers[idx] == NULL ||
  948. rtnl_msg_handlers[idx][type].dumpit == NULL)
  949. continue;
  950. if (idx > s_idx)
  951. memset(&cb->args[0], 0, sizeof(cb->args));
  952. if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
  953. break;
  954. }
  955. cb->family = idx;
  956. return skb->len;
  957. }
  958. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  959. {
  960. struct sk_buff *skb;
  961. int err = -ENOBUFS;
  962. skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
  963. if (skb == NULL)
  964. goto errout;
  965. err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
  966. if (err < 0) {
  967. /* -EMSGSIZE implies BUG in if_nlmsg_size() */
  968. WARN_ON(err == -EMSGSIZE);
  969. kfree_skb(skb);
  970. goto errout;
  971. }
  972. err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
  973. errout:
  974. if (err < 0)
  975. rtnl_set_sk_err(RTNLGRP_LINK, err);
  976. }
  977. /* Protected by RTNL sempahore. */
  978. static struct rtattr **rta_buf;
  979. static int rtattr_max;
  980. /* Process one rtnetlink message. */
  981. static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  982. {
  983. rtnl_doit_func doit;
  984. int sz_idx, kind;
  985. int min_len;
  986. int family;
  987. int type;
  988. int err;
  989. type = nlh->nlmsg_type;
  990. if (type > RTM_MAX)
  991. return -EOPNOTSUPP;
  992. type -= RTM_BASE;
  993. /* All the messages must have at least 1 byte length */
  994. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  995. return 0;
  996. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  997. if (family >= NPROTO)
  998. return -EAFNOSUPPORT;
  999. sz_idx = type>>2;
  1000. kind = type&3;
  1001. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
  1002. return -EPERM;
  1003. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  1004. rtnl_dumpit_func dumpit;
  1005. dumpit = rtnl_get_dumpit(family, type);
  1006. if (dumpit == NULL)
  1007. return -EOPNOTSUPP;
  1008. __rtnl_unlock();
  1009. err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
  1010. rtnl_lock();
  1011. return err;
  1012. }
  1013. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  1014. min_len = rtm_min[sz_idx];
  1015. if (nlh->nlmsg_len < min_len)
  1016. return -EINVAL;
  1017. if (nlh->nlmsg_len > min_len) {
  1018. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1019. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  1020. while (RTA_OK(attr, attrlen)) {
  1021. unsigned flavor = attr->rta_type;
  1022. if (flavor) {
  1023. if (flavor > rta_max[sz_idx])
  1024. return -EINVAL;
  1025. rta_buf[flavor-1] = attr;
  1026. }
  1027. attr = RTA_NEXT(attr, attrlen);
  1028. }
  1029. }
  1030. doit = rtnl_get_doit(family, type);
  1031. if (doit == NULL)
  1032. return -EOPNOTSUPP;
  1033. return doit(skb, nlh, (void *)&rta_buf[0]);
  1034. }
  1035. static void rtnetlink_rcv(struct sock *sk, int len)
  1036. {
  1037. unsigned int qlen = 0;
  1038. do {
  1039. mutex_lock(&rtnl_mutex);
  1040. netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
  1041. mutex_unlock(&rtnl_mutex);
  1042. netdev_run_todo();
  1043. } while (qlen);
  1044. }
  1045. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  1046. {
  1047. struct net_device *dev = ptr;
  1048. switch (event) {
  1049. case NETDEV_UNREGISTER:
  1050. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  1051. break;
  1052. case NETDEV_REGISTER:
  1053. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  1054. break;
  1055. case NETDEV_UP:
  1056. case NETDEV_DOWN:
  1057. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  1058. break;
  1059. case NETDEV_CHANGE:
  1060. case NETDEV_GOING_DOWN:
  1061. break;
  1062. default:
  1063. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  1064. break;
  1065. }
  1066. return NOTIFY_DONE;
  1067. }
  1068. static struct notifier_block rtnetlink_dev_notifier = {
  1069. .notifier_call = rtnetlink_event,
  1070. };
  1071. void __init rtnetlink_init(void)
  1072. {
  1073. int i;
  1074. rtattr_max = 0;
  1075. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  1076. if (rta_max[i] > rtattr_max)
  1077. rtattr_max = rta_max[i];
  1078. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  1079. if (!rta_buf)
  1080. panic("rtnetlink_init: cannot allocate rta_buf\n");
  1081. rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
  1082. &rtnl_mutex, THIS_MODULE);
  1083. if (rtnl == NULL)
  1084. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  1085. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  1086. register_netdevice_notifier(&rtnetlink_dev_notifier);
  1087. rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
  1088. rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
  1089. rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
  1090. rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
  1091. rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
  1092. rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
  1093. }
  1094. EXPORT_SYMBOL(__rta_fill);
  1095. EXPORT_SYMBOL(rtattr_strlcpy);
  1096. EXPORT_SYMBOL(rtattr_parse);
  1097. EXPORT_SYMBOL(__rtattr_parse_nested_compat);
  1098. EXPORT_SYMBOL(rtnetlink_put_metrics);
  1099. EXPORT_SYMBOL(rtnl_lock);
  1100. EXPORT_SYMBOL(rtnl_trylock);
  1101. EXPORT_SYMBOL(rtnl_unlock);
  1102. EXPORT_SYMBOL(rtnl_unicast);
  1103. EXPORT_SYMBOL(rtnl_notify);
  1104. EXPORT_SYMBOL(rtnl_set_sk_err);