rtnetlink.c 16 KB

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