rtnetlink.c 21 KB

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