rtnetlink.c 17 KB

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