rtnetlink.c 16 KB

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