rtnetlink.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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/config.h>
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/socket.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/timer.h>
  26. #include <linux/string.h>
  27. #include <linux/sockios.h>
  28. #include <linux/net.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/capability.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/init.h>
  36. #include <linux/security.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. DECLARE_MUTEX(rtnl_sem);
  50. void rtnl_lock(void)
  51. {
  52. rtnl_shlock();
  53. }
  54. int rtnl_lock_interruptible(void)
  55. {
  56. return down_interruptible(&rtnl_sem);
  57. }
  58. void rtnl_unlock(void)
  59. {
  60. rtnl_shunlock();
  61. netdev_run_todo();
  62. }
  63. int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
  64. {
  65. memset(tb, 0, sizeof(struct rtattr*)*maxattr);
  66. while (RTA_OK(rta, len)) {
  67. unsigned flavor = rta->rta_type;
  68. if (flavor && flavor <= maxattr)
  69. tb[flavor-1] = rta;
  70. rta = RTA_NEXT(rta, len);
  71. }
  72. return 0;
  73. }
  74. struct sock *rtnl;
  75. struct rtnetlink_link * rtnetlink_links[NPROTO];
  76. static const int rtm_min[RTM_NR_FAMILIES] =
  77. {
  78. [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  79. [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
  80. [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  81. [RTM_FAM(RTM_NEWNEIGH)] = NLMSG_LENGTH(sizeof(struct ndmsg)),
  82. [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  83. [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  84. [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  85. [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  86. [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
  87. [RTM_FAM(RTM_NEWPREFIX)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  88. [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  89. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  90. };
  91. static const int rta_max[RTM_NR_FAMILIES] =
  92. {
  93. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  94. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  95. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  96. [RTM_FAM(RTM_NEWNEIGH)] = NDA_MAX,
  97. [RTM_FAM(RTM_NEWRULE)] = RTA_MAX,
  98. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  99. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  100. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  101. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  102. };
  103. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  104. {
  105. struct rtattr *rta;
  106. int size = RTA_LENGTH(attrlen);
  107. rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
  108. rta->rta_type = attrtype;
  109. rta->rta_len = size;
  110. memcpy(RTA_DATA(rta), data, attrlen);
  111. }
  112. size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
  113. {
  114. size_t ret = RTA_PAYLOAD(rta);
  115. char *src = RTA_DATA(rta);
  116. if (ret > 0 && src[ret - 1] == '\0')
  117. ret--;
  118. if (size > 0) {
  119. size_t len = (ret >= size) ? size - 1 : ret;
  120. memset(dest, 0, size);
  121. memcpy(dest, src, len);
  122. }
  123. return ret;
  124. }
  125. int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  126. {
  127. int err = 0;
  128. NETLINK_CB(skb).dst_groups = group;
  129. if (echo)
  130. atomic_inc(&skb->users);
  131. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  132. if (echo)
  133. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  134. return err;
  135. }
  136. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  137. {
  138. struct rtattr *mx = (struct rtattr*)skb->tail;
  139. int i;
  140. RTA_PUT(skb, RTA_METRICS, 0, NULL);
  141. for (i=0; i<RTAX_MAX; i++) {
  142. if (metrics[i])
  143. RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
  144. }
  145. mx->rta_len = skb->tail - (u8*)mx;
  146. if (mx->rta_len == RTA_LENGTH(0))
  147. skb_trim(skb, (u8*)mx - skb->data);
  148. return 0;
  149. rtattr_failure:
  150. skb_trim(skb, (u8*)mx - skb->data);
  151. return -1;
  152. }
  153. static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  154. int type, u32 pid, u32 seq, u32 change)
  155. {
  156. struct ifinfomsg *r;
  157. struct nlmsghdr *nlh;
  158. unsigned char *b = skb->tail;
  159. nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
  160. if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
  161. r = NLMSG_DATA(nlh);
  162. r->ifi_family = AF_UNSPEC;
  163. r->ifi_type = dev->type;
  164. r->ifi_index = dev->ifindex;
  165. r->ifi_flags = dev_get_flags(dev);
  166. r->ifi_change = change;
  167. RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
  168. if (1) {
  169. u32 txqlen = dev->tx_queue_len;
  170. RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
  171. }
  172. if (1) {
  173. u32 weight = dev->weight;
  174. RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
  175. }
  176. if (1) {
  177. struct rtnl_link_ifmap map = {
  178. .mem_start = dev->mem_start,
  179. .mem_end = dev->mem_end,
  180. .base_addr = dev->base_addr,
  181. .irq = dev->irq,
  182. .dma = dev->dma,
  183. .port = dev->if_port,
  184. };
  185. RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  186. }
  187. if (dev->addr_len) {
  188. RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  189. RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  190. }
  191. if (1) {
  192. u32 mtu = dev->mtu;
  193. RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
  194. }
  195. if (dev->ifindex != dev->iflink) {
  196. u32 iflink = dev->iflink;
  197. RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
  198. }
  199. if (dev->qdisc_sleeping)
  200. RTA_PUT(skb, IFLA_QDISC,
  201. strlen(dev->qdisc_sleeping->ops->id) + 1,
  202. dev->qdisc_sleeping->ops->id);
  203. if (dev->master) {
  204. u32 master = dev->master->ifindex;
  205. RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
  206. }
  207. if (dev->get_stats) {
  208. unsigned long *stats = (unsigned long*)dev->get_stats(dev);
  209. if (stats) {
  210. struct rtattr *a;
  211. __u32 *s;
  212. int i;
  213. int n = sizeof(struct rtnl_link_stats)/4;
  214. a = __RTA_PUT(skb, IFLA_STATS, n*4);
  215. s = RTA_DATA(a);
  216. for (i=0; i<n; i++)
  217. s[i] = stats[i];
  218. }
  219. }
  220. nlh->nlmsg_len = skb->tail - b;
  221. return skb->len;
  222. nlmsg_failure:
  223. rtattr_failure:
  224. skb_trim(skb, b - skb->data);
  225. return -1;
  226. }
  227. static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  228. {
  229. int idx;
  230. int s_idx = cb->args[0];
  231. struct net_device *dev;
  232. read_lock(&dev_base_lock);
  233. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  234. if (idx < s_idx)
  235. continue;
  236. if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
  237. break;
  238. }
  239. read_unlock(&dev_base_lock);
  240. cb->args[0] = idx;
  241. return skb->len;
  242. }
  243. static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  244. {
  245. struct ifinfomsg *ifm = NLMSG_DATA(nlh);
  246. struct rtattr **ida = arg;
  247. struct net_device *dev;
  248. int err, send_addr_notify = 0;
  249. if (ifm->ifi_index >= 0)
  250. dev = dev_get_by_index(ifm->ifi_index);
  251. else if (ida[IFLA_IFNAME - 1]) {
  252. char ifname[IFNAMSIZ];
  253. if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
  254. IFNAMSIZ) >= IFNAMSIZ)
  255. return -EINVAL;
  256. dev = dev_get_by_name(ifname);
  257. } else
  258. return -EINVAL;
  259. if (!dev)
  260. return -ENODEV;
  261. err = -EINVAL;
  262. if (ifm->ifi_flags)
  263. dev_change_flags(dev, ifm->ifi_flags);
  264. if (ida[IFLA_MAP - 1]) {
  265. struct rtnl_link_ifmap *u_map;
  266. struct ifmap k_map;
  267. if (!dev->set_config) {
  268. err = -EOPNOTSUPP;
  269. goto out;
  270. }
  271. if (!netif_device_present(dev)) {
  272. err = -ENODEV;
  273. goto out;
  274. }
  275. if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
  276. goto out;
  277. u_map = RTA_DATA(ida[IFLA_MAP - 1]);
  278. k_map.mem_start = (unsigned long) u_map->mem_start;
  279. k_map.mem_end = (unsigned long) u_map->mem_end;
  280. k_map.base_addr = (unsigned short) u_map->base_addr;
  281. k_map.irq = (unsigned char) u_map->irq;
  282. k_map.dma = (unsigned char) u_map->dma;
  283. k_map.port = (unsigned char) u_map->port;
  284. err = dev->set_config(dev, &k_map);
  285. if (err)
  286. goto out;
  287. }
  288. if (ida[IFLA_ADDRESS - 1]) {
  289. if (!dev->set_mac_address) {
  290. err = -EOPNOTSUPP;
  291. goto out;
  292. }
  293. if (!netif_device_present(dev)) {
  294. err = -ENODEV;
  295. goto out;
  296. }
  297. if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
  298. goto out;
  299. err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
  300. if (err)
  301. goto out;
  302. send_addr_notify = 1;
  303. }
  304. if (ida[IFLA_BROADCAST - 1]) {
  305. if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
  306. goto out;
  307. memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
  308. dev->addr_len);
  309. send_addr_notify = 1;
  310. }
  311. if (ida[IFLA_MTU - 1]) {
  312. if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  313. goto out;
  314. err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
  315. if (err)
  316. goto out;
  317. }
  318. if (ida[IFLA_TXQLEN - 1]) {
  319. if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  320. goto out;
  321. dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
  322. }
  323. if (ida[IFLA_WEIGHT - 1]) {
  324. if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  325. goto out;
  326. dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
  327. }
  328. if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
  329. char ifname[IFNAMSIZ];
  330. if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
  331. IFNAMSIZ) >= IFNAMSIZ)
  332. goto out;
  333. err = dev_change_name(dev, ifname);
  334. if (err)
  335. goto out;
  336. }
  337. err = 0;
  338. out:
  339. if (send_addr_notify)
  340. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  341. dev_put(dev);
  342. return err;
  343. }
  344. static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  345. {
  346. int idx;
  347. int s_idx = cb->family;
  348. if (s_idx == 0)
  349. s_idx = 1;
  350. for (idx=1; idx<NPROTO; idx++) {
  351. int type = cb->nlh->nlmsg_type-RTM_BASE;
  352. if (idx < s_idx || idx == PF_PACKET)
  353. continue;
  354. if (rtnetlink_links[idx] == NULL ||
  355. rtnetlink_links[idx][type].dumpit == NULL)
  356. continue;
  357. if (idx > s_idx)
  358. memset(&cb->args[0], 0, sizeof(cb->args));
  359. if (rtnetlink_links[idx][type].dumpit(skb, cb))
  360. break;
  361. }
  362. cb->family = idx;
  363. return skb->len;
  364. }
  365. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  366. {
  367. struct sk_buff *skb;
  368. int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
  369. sizeof(struct rtnl_link_ifmap) +
  370. sizeof(struct rtnl_link_stats) + 128);
  371. skb = alloc_skb(size, GFP_KERNEL);
  372. if (!skb)
  373. return;
  374. if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
  375. kfree_skb(skb);
  376. return;
  377. }
  378. NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
  379. netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
  380. }
  381. static int rtnetlink_done(struct netlink_callback *cb)
  382. {
  383. return 0;
  384. }
  385. /* Protected by RTNL sempahore. */
  386. static struct rtattr **rta_buf;
  387. static int rtattr_max;
  388. /* Process one rtnetlink message. */
  389. static __inline__ int
  390. rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  391. {
  392. struct rtnetlink_link *link;
  393. struct rtnetlink_link *link_tab;
  394. int sz_idx, kind;
  395. int min_len;
  396. int family;
  397. int type;
  398. int err;
  399. /* Only requests are handled by kernel now */
  400. if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
  401. return 0;
  402. type = nlh->nlmsg_type;
  403. /* A control message: ignore them */
  404. if (type < RTM_BASE)
  405. return 0;
  406. /* Unknown message: reply with EINVAL */
  407. if (type > RTM_MAX)
  408. goto err_inval;
  409. type -= RTM_BASE;
  410. /* All the messages must have at least 1 byte length */
  411. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  412. return 0;
  413. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  414. if (family >= NPROTO) {
  415. *errp = -EAFNOSUPPORT;
  416. return -1;
  417. }
  418. link_tab = rtnetlink_links[family];
  419. if (link_tab == NULL)
  420. link_tab = rtnetlink_links[PF_UNSPEC];
  421. link = &link_tab[type];
  422. sz_idx = type>>2;
  423. kind = type&3;
  424. if (kind != 2 && security_netlink_recv(skb)) {
  425. *errp = -EPERM;
  426. return -1;
  427. }
  428. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  429. u32 rlen;
  430. if (link->dumpit == NULL)
  431. link = &(rtnetlink_links[PF_UNSPEC][type]);
  432. if (link->dumpit == NULL)
  433. goto err_inval;
  434. if ((*errp = netlink_dump_start(rtnl, skb, nlh,
  435. link->dumpit,
  436. rtnetlink_done)) != 0) {
  437. return -1;
  438. }
  439. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  440. if (rlen > skb->len)
  441. rlen = skb->len;
  442. skb_pull(skb, rlen);
  443. return -1;
  444. }
  445. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  446. min_len = rtm_min[sz_idx];
  447. if (nlh->nlmsg_len < min_len)
  448. goto err_inval;
  449. if (nlh->nlmsg_len > min_len) {
  450. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  451. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  452. while (RTA_OK(attr, attrlen)) {
  453. unsigned flavor = attr->rta_type;
  454. if (flavor) {
  455. if (flavor > rta_max[sz_idx])
  456. goto err_inval;
  457. rta_buf[flavor-1] = attr;
  458. }
  459. attr = RTA_NEXT(attr, attrlen);
  460. }
  461. }
  462. if (link->doit == NULL)
  463. link = &(rtnetlink_links[PF_UNSPEC][type]);
  464. if (link->doit == NULL)
  465. goto err_inval;
  466. err = link->doit(skb, nlh, (void *)&rta_buf[0]);
  467. *errp = err;
  468. return err;
  469. err_inval:
  470. *errp = -EINVAL;
  471. return -1;
  472. }
  473. /*
  474. * Process one packet of messages.
  475. * Malformed skbs with wrong lengths of messages are discarded silently.
  476. */
  477. static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
  478. {
  479. int err;
  480. struct nlmsghdr * nlh;
  481. while (skb->len >= NLMSG_SPACE(0)) {
  482. u32 rlen;
  483. nlh = (struct nlmsghdr *)skb->data;
  484. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  485. return 0;
  486. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  487. if (rlen > skb->len)
  488. rlen = skb->len;
  489. if (rtnetlink_rcv_msg(skb, nlh, &err)) {
  490. /* Not error, but we must interrupt processing here:
  491. * Note, that in this case we do not pull message
  492. * from skb, it will be processed later.
  493. */
  494. if (err == 0)
  495. return -1;
  496. netlink_ack(skb, nlh, err);
  497. } else if (nlh->nlmsg_flags&NLM_F_ACK)
  498. netlink_ack(skb, nlh, 0);
  499. skb_pull(skb, rlen);
  500. }
  501. return 0;
  502. }
  503. /*
  504. * rtnetlink input queue processing routine:
  505. * - process as much as there was in the queue upon entry.
  506. * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
  507. * that will occur, when a dump started.
  508. */
  509. static void rtnetlink_rcv(struct sock *sk, int len)
  510. {
  511. unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
  512. do {
  513. struct sk_buff *skb;
  514. rtnl_lock();
  515. if (qlen > skb_queue_len(&sk->sk_receive_queue))
  516. qlen = skb_queue_len(&sk->sk_receive_queue);
  517. for (; qlen; qlen--) {
  518. skb = skb_dequeue(&sk->sk_receive_queue);
  519. if (rtnetlink_rcv_skb(skb)) {
  520. if (skb->len)
  521. skb_queue_head(&sk->sk_receive_queue,
  522. skb);
  523. else {
  524. kfree_skb(skb);
  525. qlen--;
  526. }
  527. break;
  528. }
  529. kfree_skb(skb);
  530. }
  531. up(&rtnl_sem);
  532. netdev_run_todo();
  533. } while (qlen);
  534. }
  535. static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
  536. {
  537. [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
  538. [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
  539. [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  540. [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  541. [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
  542. [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
  543. [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
  544. [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  545. };
  546. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  547. {
  548. struct net_device *dev = ptr;
  549. switch (event) {
  550. case NETDEV_UNREGISTER:
  551. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  552. break;
  553. case NETDEV_REGISTER:
  554. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  555. break;
  556. case NETDEV_UP:
  557. case NETDEV_DOWN:
  558. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  559. break;
  560. case NETDEV_CHANGE:
  561. case NETDEV_GOING_DOWN:
  562. break;
  563. default:
  564. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  565. break;
  566. }
  567. return NOTIFY_DONE;
  568. }
  569. static struct notifier_block rtnetlink_dev_notifier = {
  570. .notifier_call = rtnetlink_event,
  571. };
  572. void __init rtnetlink_init(void)
  573. {
  574. int i;
  575. rtattr_max = 0;
  576. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  577. if (rta_max[i] > rtattr_max)
  578. rtattr_max = rta_max[i];
  579. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  580. if (!rta_buf)
  581. panic("rtnetlink_init: cannot allocate rta_buf\n");
  582. rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
  583. if (rtnl == NULL)
  584. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  585. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  586. register_netdevice_notifier(&rtnetlink_dev_notifier);
  587. rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
  588. rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
  589. }
  590. EXPORT_SYMBOL(__rta_fill);
  591. EXPORT_SYMBOL(rtattr_strlcpy);
  592. EXPORT_SYMBOL(rtattr_parse);
  593. EXPORT_SYMBOL(rtnetlink_links);
  594. EXPORT_SYMBOL(rtnetlink_put_metrics);
  595. EXPORT_SYMBOL(rtnl);
  596. EXPORT_SYMBOL(rtnl_lock);
  597. EXPORT_SYMBOL(rtnl_lock_interruptible);
  598. EXPORT_SYMBOL(rtnl_sem);
  599. EXPORT_SYMBOL(rtnl_unlock);