rtnetlink.c 20 KB

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