rtnetlink.c 20 KB

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