rtnetlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  153. {
  154. struct rtattr *mx = (struct rtattr*)skb->tail;
  155. int i;
  156. RTA_PUT(skb, RTA_METRICS, 0, NULL);
  157. for (i=0; i<RTAX_MAX; i++) {
  158. if (metrics[i])
  159. RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
  160. }
  161. mx->rta_len = skb->tail - (u8*)mx;
  162. if (mx->rta_len == RTA_LENGTH(0))
  163. skb_trim(skb, (u8*)mx - skb->data);
  164. return 0;
  165. rtattr_failure:
  166. skb_trim(skb, (u8*)mx - skb->data);
  167. return -1;
  168. }
  169. static void set_operstate(struct net_device *dev, unsigned char transition)
  170. {
  171. unsigned char operstate = dev->operstate;
  172. switch(transition) {
  173. case IF_OPER_UP:
  174. if ((operstate == IF_OPER_DORMANT ||
  175. operstate == IF_OPER_UNKNOWN) &&
  176. !netif_dormant(dev))
  177. operstate = IF_OPER_UP;
  178. break;
  179. case IF_OPER_DORMANT:
  180. if (operstate == IF_OPER_UP ||
  181. operstate == IF_OPER_UNKNOWN)
  182. operstate = IF_OPER_DORMANT;
  183. break;
  184. };
  185. if (dev->operstate != operstate) {
  186. write_lock_bh(&dev_base_lock);
  187. dev->operstate = operstate;
  188. write_unlock_bh(&dev_base_lock);
  189. netdev_state_change(dev);
  190. }
  191. }
  192. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  193. struct net_device_stats *b)
  194. {
  195. a->rx_packets = b->rx_packets;
  196. a->tx_packets = b->tx_packets;
  197. a->rx_bytes = b->rx_bytes;
  198. a->tx_bytes = b->tx_bytes;
  199. a->rx_errors = b->rx_errors;
  200. a->tx_errors = b->tx_errors;
  201. a->rx_dropped = b->rx_dropped;
  202. a->tx_dropped = b->tx_dropped;
  203. a->multicast = b->multicast;
  204. a->collisions = b->collisions;
  205. a->rx_length_errors = b->rx_length_errors;
  206. a->rx_over_errors = b->rx_over_errors;
  207. a->rx_crc_errors = b->rx_crc_errors;
  208. a->rx_frame_errors = b->rx_frame_errors;
  209. a->rx_fifo_errors = b->rx_fifo_errors;
  210. a->rx_missed_errors = b->rx_missed_errors;
  211. a->tx_aborted_errors = b->tx_aborted_errors;
  212. a->tx_carrier_errors = b->tx_carrier_errors;
  213. a->tx_fifo_errors = b->tx_fifo_errors;
  214. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  215. a->tx_window_errors = b->tx_window_errors;
  216. a->rx_compressed = b->rx_compressed;
  217. a->tx_compressed = b->tx_compressed;
  218. };
  219. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  220. void *iwbuf, int iwbuflen, int type, u32 pid,
  221. u32 seq, u32 change, unsigned int flags)
  222. {
  223. struct ifinfomsg *ifm;
  224. struct nlmsghdr *nlh;
  225. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  226. if (nlh == NULL)
  227. return -ENOBUFS;
  228. ifm = nlmsg_data(nlh);
  229. ifm->ifi_family = AF_UNSPEC;
  230. ifm->__ifi_pad = 0;
  231. ifm->ifi_type = dev->type;
  232. ifm->ifi_index = dev->ifindex;
  233. ifm->ifi_flags = dev_get_flags(dev);
  234. ifm->ifi_change = change;
  235. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  236. NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
  237. NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
  238. NLA_PUT_U8(skb, IFLA_OPERSTATE,
  239. netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
  240. NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
  241. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  242. if (dev->ifindex != dev->iflink)
  243. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  244. if (dev->master)
  245. NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
  246. if (dev->qdisc_sleeping)
  247. NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
  248. if (1) {
  249. struct rtnl_link_ifmap map = {
  250. .mem_start = dev->mem_start,
  251. .mem_end = dev->mem_end,
  252. .base_addr = dev->base_addr,
  253. .irq = dev->irq,
  254. .dma = dev->dma,
  255. .port = dev->if_port,
  256. };
  257. NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  258. }
  259. if (dev->addr_len) {
  260. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  261. NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  262. }
  263. if (dev->get_stats) {
  264. struct net_device_stats *stats = dev->get_stats(dev);
  265. if (stats) {
  266. struct nlattr *attr;
  267. attr = nla_reserve(skb, IFLA_STATS,
  268. sizeof(struct rtnl_link_stats));
  269. if (attr == NULL)
  270. goto nla_put_failure;
  271. copy_rtnl_link_stats(nla_data(attr), stats);
  272. }
  273. }
  274. if (iwbuf)
  275. NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
  276. return nlmsg_end(skb, nlh);
  277. nla_put_failure:
  278. return nlmsg_cancel(skb, nlh);
  279. }
  280. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  281. {
  282. int idx;
  283. int s_idx = cb->args[0];
  284. struct net_device *dev;
  285. read_lock(&dev_base_lock);
  286. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  287. if (idx < s_idx)
  288. continue;
  289. if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
  290. NETLINK_CB(cb->skb).pid,
  291. cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
  292. break;
  293. }
  294. read_unlock(&dev_base_lock);
  295. cb->args[0] = idx;
  296. return skb->len;
  297. }
  298. static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
  299. [IFLA_IFNAME] = { .type = NLA_STRING },
  300. [IFLA_MAP] = { .minlen = sizeof(struct rtnl_link_ifmap) },
  301. [IFLA_MTU] = { .type = NLA_U32 },
  302. [IFLA_TXQLEN] = { .type = NLA_U32 },
  303. [IFLA_WEIGHT] = { .type = NLA_U32 },
  304. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  305. [IFLA_LINKMODE] = { .type = NLA_U8 },
  306. };
  307. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  308. {
  309. struct ifinfomsg *ifm;
  310. struct net_device *dev;
  311. int err, send_addr_notify = 0, modified = 0;
  312. struct nlattr *tb[IFLA_MAX+1];
  313. char ifname[IFNAMSIZ];
  314. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  315. if (err < 0)
  316. goto errout;
  317. if (tb[IFLA_IFNAME] &&
  318. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ) >= IFNAMSIZ)
  319. return -EINVAL;
  320. err = -EINVAL;
  321. ifm = nlmsg_data(nlh);
  322. if (ifm->ifi_index >= 0)
  323. dev = dev_get_by_index(ifm->ifi_index);
  324. else if (tb[IFLA_IFNAME])
  325. dev = dev_get_by_name(ifname);
  326. else
  327. goto errout;
  328. if (dev == NULL) {
  329. err = -ENODEV;
  330. goto errout;
  331. }
  332. if (tb[IFLA_ADDRESS] &&
  333. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  334. goto errout_dev;
  335. if (tb[IFLA_BROADCAST] &&
  336. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  337. goto errout_dev;
  338. if (tb[IFLA_MAP]) {
  339. struct rtnl_link_ifmap *u_map;
  340. struct ifmap k_map;
  341. if (!dev->set_config) {
  342. err = -EOPNOTSUPP;
  343. goto errout_dev;
  344. }
  345. if (!netif_device_present(dev)) {
  346. err = -ENODEV;
  347. goto errout_dev;
  348. }
  349. u_map = nla_data(tb[IFLA_MAP]);
  350. k_map.mem_start = (unsigned long) u_map->mem_start;
  351. k_map.mem_end = (unsigned long) u_map->mem_end;
  352. k_map.base_addr = (unsigned short) u_map->base_addr;
  353. k_map.irq = (unsigned char) u_map->irq;
  354. k_map.dma = (unsigned char) u_map->dma;
  355. k_map.port = (unsigned char) u_map->port;
  356. err = dev->set_config(dev, &k_map);
  357. if (err < 0)
  358. goto errout_dev;
  359. modified = 1;
  360. }
  361. if (tb[IFLA_ADDRESS]) {
  362. struct sockaddr *sa;
  363. int len;
  364. if (!dev->set_mac_address) {
  365. err = -EOPNOTSUPP;
  366. goto errout_dev;
  367. }
  368. if (!netif_device_present(dev)) {
  369. err = -ENODEV;
  370. goto errout_dev;
  371. }
  372. len = sizeof(sa_family_t) + dev->addr_len;
  373. sa = kmalloc(len, GFP_KERNEL);
  374. if (!sa) {
  375. err = -ENOMEM;
  376. goto errout_dev;
  377. }
  378. sa->sa_family = dev->type;
  379. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  380. dev->addr_len);
  381. err = dev->set_mac_address(dev, sa);
  382. kfree(sa);
  383. if (err)
  384. goto errout_dev;
  385. send_addr_notify = 1;
  386. modified = 1;
  387. }
  388. if (tb[IFLA_MTU]) {
  389. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  390. if (err < 0)
  391. goto errout_dev;
  392. modified = 1;
  393. }
  394. /*
  395. * Interface selected by interface index but interface
  396. * name provided implies that a name change has been
  397. * requested.
  398. */
  399. if (ifm->ifi_index >= 0 && ifname[0]) {
  400. err = dev_change_name(dev, ifname);
  401. if (err < 0)
  402. goto errout_dev;
  403. modified = 1;
  404. }
  405. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  406. if (tb[IFLA_WIRELESS]) {
  407. /* Call Wireless Extensions.
  408. * Various stuff checked in there... */
  409. err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
  410. nla_len(tb[IFLA_WIRELESS]));
  411. if (err < 0)
  412. goto errout_dev;
  413. }
  414. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  415. if (tb[IFLA_BROADCAST]) {
  416. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  417. send_addr_notify = 1;
  418. }
  419. if (ifm->ifi_flags)
  420. dev_change_flags(dev, ifm->ifi_flags);
  421. if (tb[IFLA_TXQLEN])
  422. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  423. if (tb[IFLA_WEIGHT])
  424. dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
  425. if (tb[IFLA_OPERSTATE])
  426. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  427. if (tb[IFLA_LINKMODE]) {
  428. write_lock_bh(&dev_base_lock);
  429. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  430. write_unlock_bh(&dev_base_lock);
  431. }
  432. err = 0;
  433. errout_dev:
  434. if (err < 0 && modified && net_ratelimit())
  435. printk(KERN_WARNING "A link change request failed with "
  436. "some changes comitted already. Interface %s may "
  437. "have been left with an inconsistent configuration, "
  438. "please check.\n", dev->name);
  439. if (send_addr_notify)
  440. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  441. dev_put(dev);
  442. errout:
  443. return err;
  444. }
  445. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  446. {
  447. struct ifinfomsg *ifm;
  448. struct nlattr *tb[IFLA_MAX+1];
  449. struct net_device *dev = NULL;
  450. struct sk_buff *nskb;
  451. char *iw_buf = NULL, *iw = NULL;
  452. int iw_buf_len = 0;
  453. int err, payload;
  454. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  455. if (err < 0)
  456. goto errout;
  457. ifm = nlmsg_data(nlh);
  458. if (ifm->ifi_index >= 0) {
  459. dev = dev_get_by_index(ifm->ifi_index);
  460. if (dev == NULL)
  461. return -ENODEV;
  462. } else
  463. return -EINVAL;
  464. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  465. if (tb[IFLA_WIRELESS]) {
  466. /* Call Wireless Extensions. We need to know the size before
  467. * we can alloc. Various stuff checked in there... */
  468. err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
  469. nla_len(tb[IFLA_WIRELESS]),
  470. &iw_buf, &iw_buf_len);
  471. if (err < 0)
  472. goto errout;
  473. iw += IW_EV_POINT_OFF;
  474. }
  475. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  476. payload = NLMSG_ALIGN(sizeof(struct ifinfomsg) +
  477. nla_total_size(iw_buf_len));
  478. nskb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL);
  479. if (nskb == NULL) {
  480. err = -ENOBUFS;
  481. goto errout;
  482. }
  483. err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
  484. NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
  485. if (err <= 0) {
  486. kfree_skb(skb);
  487. goto errout;
  488. }
  489. err = rtnl_unicast(skb, NETLINK_CB(skb).pid);
  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);
  697. EXPORT_SYMBOL(rtnl_unicast);