rtnetlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/socket.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/timer.h>
  25. #include <linux/string.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mm.h>
  30. #include <linux/slab.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/capability.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/init.h>
  35. #include <linux/security.h>
  36. #include <linux/mutex.h>
  37. #include <linux/if_addr.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/fib_rules.h>
  51. #include <net/netlink.h>
  52. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  53. #include <linux/wireless.h>
  54. #include <net/iw_handler.h>
  55. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  56. static DEFINE_MUTEX(rtnl_mutex);
  57. void rtnl_lock(void)
  58. {
  59. mutex_lock(&rtnl_mutex);
  60. }
  61. void __rtnl_unlock(void)
  62. {
  63. mutex_unlock(&rtnl_mutex);
  64. }
  65. void rtnl_unlock(void)
  66. {
  67. mutex_unlock(&rtnl_mutex);
  68. if (rtnl && rtnl->sk_receive_queue.qlen)
  69. rtnl->sk_data_ready(rtnl, 0);
  70. netdev_run_todo();
  71. }
  72. int rtnl_trylock(void)
  73. {
  74. return mutex_trylock(&rtnl_mutex);
  75. }
  76. int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
  77. {
  78. memset(tb, 0, sizeof(struct rtattr*)*maxattr);
  79. while (RTA_OK(rta, len)) {
  80. unsigned flavor = rta->rta_type;
  81. if (flavor && flavor <= maxattr)
  82. tb[flavor-1] = rta;
  83. rta = RTA_NEXT(rta, len);
  84. }
  85. return 0;
  86. }
  87. struct sock *rtnl;
  88. struct rtnetlink_link * rtnetlink_links[NPROTO];
  89. static const int rtm_min[RTM_NR_FAMILIES] =
  90. {
  91. [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  92. [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
  93. [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  94. [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
  95. [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  96. [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  97. [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  98. [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
  99. [RTM_FAM(RTM_NEWPREFIX)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  100. [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  101. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  102. [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)),
  103. };
  104. static const int rta_max[RTM_NR_FAMILIES] =
  105. {
  106. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  107. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  108. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  109. [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
  110. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  111. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  112. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  113. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  114. [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX,
  115. };
  116. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  117. {
  118. struct rtattr *rta;
  119. int size = RTA_LENGTH(attrlen);
  120. rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
  121. rta->rta_type = attrtype;
  122. rta->rta_len = size;
  123. memcpy(RTA_DATA(rta), data, attrlen);
  124. memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
  125. }
  126. size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
  127. {
  128. size_t ret = RTA_PAYLOAD(rta);
  129. char *src = RTA_DATA(rta);
  130. if (ret > 0 && src[ret - 1] == '\0')
  131. ret--;
  132. if (size > 0) {
  133. size_t len = (ret >= size) ? size - 1 : ret;
  134. memset(dest, 0, size);
  135. memcpy(dest, src, len);
  136. }
  137. return ret;
  138. }
  139. int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  140. {
  141. int err = 0;
  142. NETLINK_CB(skb).dst_group = group;
  143. if (echo)
  144. atomic_inc(&skb->users);
  145. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  146. if (echo)
  147. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  148. return err;
  149. }
  150. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  151. {
  152. struct rtattr *mx = (struct rtattr*)skb->tail;
  153. int i;
  154. RTA_PUT(skb, RTA_METRICS, 0, NULL);
  155. for (i=0; i<RTAX_MAX; i++) {
  156. if (metrics[i])
  157. RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
  158. }
  159. mx->rta_len = skb->tail - (u8*)mx;
  160. if (mx->rta_len == RTA_LENGTH(0))
  161. skb_trim(skb, (u8*)mx - skb->data);
  162. return 0;
  163. rtattr_failure:
  164. skb_trim(skb, (u8*)mx - skb->data);
  165. return -1;
  166. }
  167. static void set_operstate(struct net_device *dev, unsigned char transition)
  168. {
  169. unsigned char operstate = dev->operstate;
  170. switch(transition) {
  171. case IF_OPER_UP:
  172. if ((operstate == IF_OPER_DORMANT ||
  173. operstate == IF_OPER_UNKNOWN) &&
  174. !netif_dormant(dev))
  175. operstate = IF_OPER_UP;
  176. break;
  177. case IF_OPER_DORMANT:
  178. if (operstate == IF_OPER_UP ||
  179. operstate == IF_OPER_UNKNOWN)
  180. operstate = IF_OPER_DORMANT;
  181. break;
  182. };
  183. if (dev->operstate != operstate) {
  184. write_lock_bh(&dev_base_lock);
  185. dev->operstate = operstate;
  186. write_unlock_bh(&dev_base_lock);
  187. netdev_state_change(dev);
  188. }
  189. }
  190. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  191. struct net_device_stats *b)
  192. {
  193. a->rx_packets = b->rx_packets;
  194. a->tx_packets = b->tx_packets;
  195. a->rx_bytes = b->rx_bytes;
  196. a->tx_bytes = b->tx_bytes;
  197. a->rx_errors = b->rx_errors;
  198. a->tx_errors = b->tx_errors;
  199. a->rx_dropped = b->rx_dropped;
  200. a->tx_dropped = b->tx_dropped;
  201. a->multicast = b->multicast;
  202. a->collisions = b->collisions;
  203. a->rx_length_errors = b->rx_length_errors;
  204. a->rx_over_errors = b->rx_over_errors;
  205. a->rx_crc_errors = b->rx_crc_errors;
  206. a->rx_frame_errors = b->rx_frame_errors;
  207. a->rx_fifo_errors = b->rx_fifo_errors;
  208. a->rx_missed_errors = b->rx_missed_errors;
  209. a->tx_aborted_errors = b->tx_aborted_errors;
  210. a->tx_carrier_errors = b->tx_carrier_errors;
  211. a->tx_fifo_errors = b->tx_fifo_errors;
  212. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  213. a->tx_window_errors = b->tx_window_errors;
  214. a->rx_compressed = b->rx_compressed;
  215. a->tx_compressed = b->tx_compressed;
  216. };
  217. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  218. void *iwbuf, int iwbuflen, int type, u32 pid,
  219. u32 seq, u32 change, unsigned int flags)
  220. {
  221. struct ifinfomsg *ifm;
  222. struct nlmsghdr *nlh;
  223. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  224. if (nlh == NULL)
  225. return -ENOBUFS;
  226. ifm = nlmsg_data(nlh);
  227. ifm->ifi_family = AF_UNSPEC;
  228. ifm->__ifi_pad = 0;
  229. ifm->ifi_type = dev->type;
  230. ifm->ifi_index = dev->ifindex;
  231. ifm->ifi_flags = dev_get_flags(dev);
  232. ifm->ifi_change = change;
  233. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  234. NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
  235. NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
  236. NLA_PUT_U8(skb, IFLA_OPERSTATE,
  237. netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
  238. NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
  239. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  240. if (dev->ifindex != dev->iflink)
  241. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  242. if (dev->master)
  243. NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
  244. if (dev->qdisc_sleeping)
  245. NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
  246. if (1) {
  247. struct rtnl_link_ifmap map = {
  248. .mem_start = dev->mem_start,
  249. .mem_end = dev->mem_end,
  250. .base_addr = dev->base_addr,
  251. .irq = dev->irq,
  252. .dma = dev->dma,
  253. .port = dev->if_port,
  254. };
  255. NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  256. }
  257. if (dev->addr_len) {
  258. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  259. NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  260. }
  261. if (dev->get_stats) {
  262. struct net_device_stats *stats = dev->get_stats(dev);
  263. if (stats) {
  264. struct nlattr *attr;
  265. attr = nla_reserve(skb, IFLA_STATS,
  266. sizeof(struct rtnl_link_stats));
  267. if (attr == NULL)
  268. goto nla_put_failure;
  269. copy_rtnl_link_stats(nla_data(attr), stats);
  270. }
  271. }
  272. if (iwbuf)
  273. NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
  274. return nlmsg_end(skb, nlh);
  275. nla_put_failure:
  276. return nlmsg_cancel(skb, nlh);
  277. }
  278. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  279. {
  280. int idx;
  281. int s_idx = cb->args[0];
  282. struct net_device *dev;
  283. read_lock(&dev_base_lock);
  284. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  285. if (idx < s_idx)
  286. continue;
  287. if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
  288. NETLINK_CB(cb->skb).pid,
  289. cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
  290. break;
  291. }
  292. read_unlock(&dev_base_lock);
  293. cb->args[0] = idx;
  294. return skb->len;
  295. }
  296. static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
  297. [IFLA_IFNAME] = { .type = NLA_STRING },
  298. [IFLA_MAP] = { .minlen = sizeof(struct rtnl_link_ifmap) },
  299. [IFLA_MTU] = { .type = NLA_U32 },
  300. [IFLA_TXQLEN] = { .type = NLA_U32 },
  301. [IFLA_WEIGHT] = { .type = NLA_U32 },
  302. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  303. [IFLA_LINKMODE] = { .type = NLA_U8 },
  304. };
  305. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  306. {
  307. struct ifinfomsg *ifm;
  308. struct net_device *dev;
  309. int err, send_addr_notify = 0, modified = 0;
  310. struct nlattr *tb[IFLA_MAX+1];
  311. char ifname[IFNAMSIZ];
  312. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  313. if (err < 0)
  314. goto errout;
  315. if (tb[IFLA_IFNAME] &&
  316. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ) >= IFNAMSIZ)
  317. return -EINVAL;
  318. err = -EINVAL;
  319. ifm = nlmsg_data(nlh);
  320. if (ifm->ifi_index >= 0)
  321. dev = dev_get_by_index(ifm->ifi_index);
  322. else if (tb[IFLA_IFNAME])
  323. dev = dev_get_by_name(ifname);
  324. else
  325. goto errout;
  326. if (dev == NULL) {
  327. err = -ENODEV;
  328. goto errout;
  329. }
  330. if (tb[IFLA_ADDRESS] &&
  331. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  332. goto errout_dev;
  333. if (tb[IFLA_BROADCAST] &&
  334. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  335. goto errout_dev;
  336. if (tb[IFLA_MAP]) {
  337. struct rtnl_link_ifmap *u_map;
  338. struct ifmap k_map;
  339. if (!dev->set_config) {
  340. err = -EOPNOTSUPP;
  341. goto errout_dev;
  342. }
  343. if (!netif_device_present(dev)) {
  344. err = -ENODEV;
  345. goto errout_dev;
  346. }
  347. u_map = nla_data(tb[IFLA_MAP]);
  348. k_map.mem_start = (unsigned long) u_map->mem_start;
  349. k_map.mem_end = (unsigned long) u_map->mem_end;
  350. k_map.base_addr = (unsigned short) u_map->base_addr;
  351. k_map.irq = (unsigned char) u_map->irq;
  352. k_map.dma = (unsigned char) u_map->dma;
  353. k_map.port = (unsigned char) u_map->port;
  354. err = dev->set_config(dev, &k_map);
  355. if (err < 0)
  356. goto errout_dev;
  357. modified = 1;
  358. }
  359. if (tb[IFLA_ADDRESS]) {
  360. struct sockaddr *sa;
  361. int len;
  362. if (!dev->set_mac_address) {
  363. err = -EOPNOTSUPP;
  364. goto errout_dev;
  365. }
  366. if (!netif_device_present(dev)) {
  367. err = -ENODEV;
  368. goto errout_dev;
  369. }
  370. len = sizeof(sa_family_t) + dev->addr_len;
  371. sa = kmalloc(len, GFP_KERNEL);
  372. if (!sa) {
  373. err = -ENOMEM;
  374. goto errout_dev;
  375. }
  376. sa->sa_family = dev->type;
  377. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  378. dev->addr_len);
  379. err = dev->set_mac_address(dev, sa);
  380. kfree(sa);
  381. if (err)
  382. goto errout_dev;
  383. send_addr_notify = 1;
  384. modified = 1;
  385. }
  386. if (tb[IFLA_MTU]) {
  387. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  388. if (err < 0)
  389. goto errout_dev;
  390. modified = 1;
  391. }
  392. /*
  393. * Interface selected by interface index but interface
  394. * name provided implies that a name change has been
  395. * requested.
  396. */
  397. if (ifm->ifi_index >= 0 && ifname[0]) {
  398. err = dev_change_name(dev, ifname);
  399. if (err < 0)
  400. goto errout_dev;
  401. modified = 1;
  402. }
  403. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  404. if (tb[IFLA_WIRELESS]) {
  405. /* Call Wireless Extensions.
  406. * Various stuff checked in there... */
  407. err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
  408. nla_len(tb[IFLA_WIRELESS]));
  409. if (err < 0)
  410. goto errout_dev;
  411. }
  412. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  413. if (tb[IFLA_BROADCAST]) {
  414. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  415. send_addr_notify = 1;
  416. }
  417. if (ifm->ifi_flags)
  418. dev_change_flags(dev, ifm->ifi_flags);
  419. if (tb[IFLA_TXQLEN])
  420. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  421. if (tb[IFLA_WEIGHT])
  422. dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
  423. if (tb[IFLA_OPERSTATE])
  424. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  425. if (tb[IFLA_LINKMODE]) {
  426. write_lock_bh(&dev_base_lock);
  427. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  428. write_unlock_bh(&dev_base_lock);
  429. }
  430. err = 0;
  431. errout_dev:
  432. if (err < 0 && modified && net_ratelimit())
  433. printk(KERN_WARNING "A link change request failed with "
  434. "some changes comitted already. Interface %s may "
  435. "have been left with an inconsistent configuration, "
  436. "please check.\n", dev->name);
  437. if (send_addr_notify)
  438. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  439. dev_put(dev);
  440. errout:
  441. return err;
  442. }
  443. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  444. {
  445. struct ifinfomsg *ifm;
  446. struct nlattr *tb[IFLA_MAX+1];
  447. struct net_device *dev = NULL;
  448. struct sk_buff *nskb;
  449. char *iw_buf = NULL, *iw = NULL;
  450. int iw_buf_len = 0;
  451. int err, payload;
  452. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  453. if (err < 0)
  454. goto errout;
  455. ifm = nlmsg_data(nlh);
  456. if (ifm->ifi_index >= 0) {
  457. dev = dev_get_by_index(ifm->ifi_index);
  458. if (dev == NULL)
  459. return -ENODEV;
  460. } else
  461. return -EINVAL;
  462. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  463. if (tb[IFLA_WIRELESS]) {
  464. /* Call Wireless Extensions. We need to know the size before
  465. * we can alloc. Various stuff checked in there... */
  466. err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
  467. nla_len(tb[IFLA_WIRELESS]),
  468. &iw_buf, &iw_buf_len);
  469. if (err < 0)
  470. goto errout;
  471. iw += IW_EV_POINT_OFF;
  472. }
  473. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  474. payload = NLMSG_ALIGN(sizeof(struct ifinfomsg) +
  475. nla_total_size(iw_buf_len));
  476. nskb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL);
  477. if (nskb == NULL) {
  478. err = -ENOBUFS;
  479. goto errout;
  480. }
  481. err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
  482. NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
  483. if (err <= 0) {
  484. kfree_skb(skb);
  485. goto errout;
  486. }
  487. err = netlink_unicast(rtnl, skb, NETLINK_CB(skb).pid, MSG_DONTWAIT);
  488. if (err > 0)
  489. err = 0;
  490. errout:
  491. kfree(iw_buf);
  492. dev_put(dev);
  493. return err;
  494. }
  495. static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  496. {
  497. int idx;
  498. int s_idx = cb->family;
  499. if (s_idx == 0)
  500. s_idx = 1;
  501. for (idx=1; idx<NPROTO; idx++) {
  502. int type = cb->nlh->nlmsg_type-RTM_BASE;
  503. if (idx < s_idx || idx == PF_PACKET)
  504. continue;
  505. if (rtnetlink_links[idx] == NULL ||
  506. rtnetlink_links[idx][type].dumpit == NULL)
  507. continue;
  508. if (idx > s_idx)
  509. memset(&cb->args[0], 0, sizeof(cb->args));
  510. if (rtnetlink_links[idx][type].dumpit(skb, cb))
  511. break;
  512. }
  513. cb->family = idx;
  514. return skb->len;
  515. }
  516. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  517. {
  518. struct sk_buff *skb;
  519. int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
  520. sizeof(struct rtnl_link_ifmap) +
  521. sizeof(struct rtnl_link_stats) + 128);
  522. skb = nlmsg_new(size, GFP_KERNEL);
  523. if (!skb)
  524. return;
  525. if (rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0) < 0) {
  526. kfree_skb(skb);
  527. return;
  528. }
  529. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  530. netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
  531. }
  532. /* Protected by RTNL sempahore. */
  533. static struct rtattr **rta_buf;
  534. static int rtattr_max;
  535. /* Process one rtnetlink message. */
  536. static __inline__ int
  537. rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  538. {
  539. struct rtnetlink_link *link;
  540. struct rtnetlink_link *link_tab;
  541. int sz_idx, kind;
  542. int min_len;
  543. int family;
  544. int type;
  545. int err;
  546. /* Only requests are handled by kernel now */
  547. if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
  548. return 0;
  549. type = nlh->nlmsg_type;
  550. /* A control message: ignore them */
  551. if (type < RTM_BASE)
  552. return 0;
  553. /* Unknown message: reply with EINVAL */
  554. if (type > RTM_MAX)
  555. goto err_inval;
  556. type -= RTM_BASE;
  557. /* All the messages must have at least 1 byte length */
  558. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  559. return 0;
  560. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  561. if (family >= NPROTO) {
  562. *errp = -EAFNOSUPPORT;
  563. return -1;
  564. }
  565. link_tab = rtnetlink_links[family];
  566. if (link_tab == NULL)
  567. link_tab = rtnetlink_links[PF_UNSPEC];
  568. link = &link_tab[type];
  569. sz_idx = type>>2;
  570. kind = type&3;
  571. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
  572. *errp = -EPERM;
  573. return -1;
  574. }
  575. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  576. if (link->dumpit == NULL)
  577. link = &(rtnetlink_links[PF_UNSPEC][type]);
  578. if (link->dumpit == NULL)
  579. goto err_inval;
  580. if ((*errp = netlink_dump_start(rtnl, skb, nlh,
  581. link->dumpit, NULL)) != 0) {
  582. return -1;
  583. }
  584. netlink_queue_skip(nlh, skb);
  585. return -1;
  586. }
  587. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  588. min_len = rtm_min[sz_idx];
  589. if (nlh->nlmsg_len < min_len)
  590. goto err_inval;
  591. if (nlh->nlmsg_len > min_len) {
  592. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  593. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  594. while (RTA_OK(attr, attrlen)) {
  595. unsigned flavor = attr->rta_type;
  596. if (flavor) {
  597. if (flavor > rta_max[sz_idx])
  598. goto err_inval;
  599. rta_buf[flavor-1] = attr;
  600. }
  601. attr = RTA_NEXT(attr, attrlen);
  602. }
  603. }
  604. if (link->doit == NULL)
  605. link = &(rtnetlink_links[PF_UNSPEC][type]);
  606. if (link->doit == NULL)
  607. goto err_inval;
  608. err = link->doit(skb, nlh, (void *)&rta_buf[0]);
  609. *errp = err;
  610. return err;
  611. err_inval:
  612. *errp = -EINVAL;
  613. return -1;
  614. }
  615. static void rtnetlink_rcv(struct sock *sk, int len)
  616. {
  617. unsigned int qlen = 0;
  618. do {
  619. mutex_lock(&rtnl_mutex);
  620. netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
  621. mutex_unlock(&rtnl_mutex);
  622. netdev_run_todo();
  623. } while (qlen);
  624. }
  625. static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
  626. {
  627. [RTM_GETLINK - RTM_BASE] = { .doit = rtnl_getlink,
  628. .dumpit = rtnl_dump_ifinfo },
  629. [RTM_SETLINK - RTM_BASE] = { .doit = rtnl_setlink },
  630. [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnl_dump_all },
  631. [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnl_dump_all },
  632. [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
  633. [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
  634. [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
  635. #ifdef CONFIG_FIB_RULES
  636. [RTM_NEWRULE - RTM_BASE] = { .doit = fib_nl_newrule },
  637. [RTM_DELRULE - RTM_BASE] = { .doit = fib_nl_delrule },
  638. #endif
  639. [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnl_dump_all },
  640. [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
  641. [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
  642. };
  643. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  644. {
  645. struct net_device *dev = ptr;
  646. switch (event) {
  647. case NETDEV_UNREGISTER:
  648. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  649. break;
  650. case NETDEV_REGISTER:
  651. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  652. break;
  653. case NETDEV_UP:
  654. case NETDEV_DOWN:
  655. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  656. break;
  657. case NETDEV_CHANGE:
  658. case NETDEV_GOING_DOWN:
  659. break;
  660. default:
  661. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  662. break;
  663. }
  664. return NOTIFY_DONE;
  665. }
  666. static struct notifier_block rtnetlink_dev_notifier = {
  667. .notifier_call = rtnetlink_event,
  668. };
  669. void __init rtnetlink_init(void)
  670. {
  671. int i;
  672. rtattr_max = 0;
  673. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  674. if (rta_max[i] > rtattr_max)
  675. rtattr_max = rta_max[i];
  676. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  677. if (!rta_buf)
  678. panic("rtnetlink_init: cannot allocate rta_buf\n");
  679. rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
  680. THIS_MODULE);
  681. if (rtnl == NULL)
  682. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  683. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  684. register_netdevice_notifier(&rtnetlink_dev_notifier);
  685. rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
  686. rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
  687. }
  688. EXPORT_SYMBOL(__rta_fill);
  689. EXPORT_SYMBOL(rtattr_strlcpy);
  690. EXPORT_SYMBOL(rtattr_parse);
  691. EXPORT_SYMBOL(rtnetlink_links);
  692. EXPORT_SYMBOL(rtnetlink_put_metrics);
  693. EXPORT_SYMBOL(rtnl);
  694. EXPORT_SYMBOL(rtnl_lock);
  695. EXPORT_SYMBOL(rtnl_trylock);
  696. EXPORT_SYMBOL(rtnl_unlock);