rtnetlink.c 16 KB

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