rtnetlink.c 17 KB

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