rtnetlink.c 20 KB

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