/net/wireless/wext-core.c

http://github.com/mirrors/linux · C · 1188 lines · 861 code · 136 blank · 191 comment · 124 complexity · b04b0bc34d925ac02513de9ca7ddb2c0 MD5 · raw file

  1. /*
  2. * This file implement the Wireless Extensions core API.
  3. *
  4. * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
  5. * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
  6. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * (As all part of the Linux kernel, this file is GPL)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/slab.h>
  14. #include <linux/wireless.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/export.h>
  17. #include <net/cfg80211.h>
  18. #include <net/iw_handler.h>
  19. #include <net/netlink.h>
  20. #include <net/wext.h>
  21. #include <net/net_namespace.h>
  22. typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  23. unsigned int, struct iw_request_info *,
  24. iw_handler);
  25. /*
  26. * Meta-data about all the standard Wireless Extension request we
  27. * know about.
  28. */
  29. static const struct iw_ioctl_description standard_ioctl[] = {
  30. [IW_IOCTL_IDX(SIOCSIWCOMMIT)] = {
  31. .header_type = IW_HEADER_TYPE_NULL,
  32. },
  33. [IW_IOCTL_IDX(SIOCGIWNAME)] = {
  34. .header_type = IW_HEADER_TYPE_CHAR,
  35. .flags = IW_DESCR_FLAG_DUMP,
  36. },
  37. [IW_IOCTL_IDX(SIOCSIWNWID)] = {
  38. .header_type = IW_HEADER_TYPE_PARAM,
  39. .flags = IW_DESCR_FLAG_EVENT,
  40. },
  41. [IW_IOCTL_IDX(SIOCGIWNWID)] = {
  42. .header_type = IW_HEADER_TYPE_PARAM,
  43. .flags = IW_DESCR_FLAG_DUMP,
  44. },
  45. [IW_IOCTL_IDX(SIOCSIWFREQ)] = {
  46. .header_type = IW_HEADER_TYPE_FREQ,
  47. .flags = IW_DESCR_FLAG_EVENT,
  48. },
  49. [IW_IOCTL_IDX(SIOCGIWFREQ)] = {
  50. .header_type = IW_HEADER_TYPE_FREQ,
  51. .flags = IW_DESCR_FLAG_DUMP,
  52. },
  53. [IW_IOCTL_IDX(SIOCSIWMODE)] = {
  54. .header_type = IW_HEADER_TYPE_UINT,
  55. .flags = IW_DESCR_FLAG_EVENT,
  56. },
  57. [IW_IOCTL_IDX(SIOCGIWMODE)] = {
  58. .header_type = IW_HEADER_TYPE_UINT,
  59. .flags = IW_DESCR_FLAG_DUMP,
  60. },
  61. [IW_IOCTL_IDX(SIOCSIWSENS)] = {
  62. .header_type = IW_HEADER_TYPE_PARAM,
  63. },
  64. [IW_IOCTL_IDX(SIOCGIWSENS)] = {
  65. .header_type = IW_HEADER_TYPE_PARAM,
  66. },
  67. [IW_IOCTL_IDX(SIOCSIWRANGE)] = {
  68. .header_type = IW_HEADER_TYPE_NULL,
  69. },
  70. [IW_IOCTL_IDX(SIOCGIWRANGE)] = {
  71. .header_type = IW_HEADER_TYPE_POINT,
  72. .token_size = 1,
  73. .max_tokens = sizeof(struct iw_range),
  74. .flags = IW_DESCR_FLAG_DUMP,
  75. },
  76. [IW_IOCTL_IDX(SIOCSIWPRIV)] = {
  77. .header_type = IW_HEADER_TYPE_NULL,
  78. },
  79. [IW_IOCTL_IDX(SIOCGIWPRIV)] = { /* (handled directly by us) */
  80. .header_type = IW_HEADER_TYPE_POINT,
  81. .token_size = sizeof(struct iw_priv_args),
  82. .max_tokens = 16,
  83. .flags = IW_DESCR_FLAG_NOMAX,
  84. },
  85. [IW_IOCTL_IDX(SIOCSIWSTATS)] = {
  86. .header_type = IW_HEADER_TYPE_NULL,
  87. },
  88. [IW_IOCTL_IDX(SIOCGIWSTATS)] = { /* (handled directly by us) */
  89. .header_type = IW_HEADER_TYPE_POINT,
  90. .token_size = 1,
  91. .max_tokens = sizeof(struct iw_statistics),
  92. .flags = IW_DESCR_FLAG_DUMP,
  93. },
  94. [IW_IOCTL_IDX(SIOCSIWSPY)] = {
  95. .header_type = IW_HEADER_TYPE_POINT,
  96. .token_size = sizeof(struct sockaddr),
  97. .max_tokens = IW_MAX_SPY,
  98. },
  99. [IW_IOCTL_IDX(SIOCGIWSPY)] = {
  100. .header_type = IW_HEADER_TYPE_POINT,
  101. .token_size = sizeof(struct sockaddr) +
  102. sizeof(struct iw_quality),
  103. .max_tokens = IW_MAX_SPY,
  104. },
  105. [IW_IOCTL_IDX(SIOCSIWTHRSPY)] = {
  106. .header_type = IW_HEADER_TYPE_POINT,
  107. .token_size = sizeof(struct iw_thrspy),
  108. .min_tokens = 1,
  109. .max_tokens = 1,
  110. },
  111. [IW_IOCTL_IDX(SIOCGIWTHRSPY)] = {
  112. .header_type = IW_HEADER_TYPE_POINT,
  113. .token_size = sizeof(struct iw_thrspy),
  114. .min_tokens = 1,
  115. .max_tokens = 1,
  116. },
  117. [IW_IOCTL_IDX(SIOCSIWAP)] = {
  118. .header_type = IW_HEADER_TYPE_ADDR,
  119. },
  120. [IW_IOCTL_IDX(SIOCGIWAP)] = {
  121. .header_type = IW_HEADER_TYPE_ADDR,
  122. .flags = IW_DESCR_FLAG_DUMP,
  123. },
  124. [IW_IOCTL_IDX(SIOCSIWMLME)] = {
  125. .header_type = IW_HEADER_TYPE_POINT,
  126. .token_size = 1,
  127. .min_tokens = sizeof(struct iw_mlme),
  128. .max_tokens = sizeof(struct iw_mlme),
  129. },
  130. [IW_IOCTL_IDX(SIOCGIWAPLIST)] = {
  131. .header_type = IW_HEADER_TYPE_POINT,
  132. .token_size = sizeof(struct sockaddr) +
  133. sizeof(struct iw_quality),
  134. .max_tokens = IW_MAX_AP,
  135. .flags = IW_DESCR_FLAG_NOMAX,
  136. },
  137. [IW_IOCTL_IDX(SIOCSIWSCAN)] = {
  138. .header_type = IW_HEADER_TYPE_POINT,
  139. .token_size = 1,
  140. .min_tokens = 0,
  141. .max_tokens = sizeof(struct iw_scan_req),
  142. },
  143. [IW_IOCTL_IDX(SIOCGIWSCAN)] = {
  144. .header_type = IW_HEADER_TYPE_POINT,
  145. .token_size = 1,
  146. .max_tokens = IW_SCAN_MAX_DATA,
  147. .flags = IW_DESCR_FLAG_NOMAX,
  148. },
  149. [IW_IOCTL_IDX(SIOCSIWESSID)] = {
  150. .header_type = IW_HEADER_TYPE_POINT,
  151. .token_size = 1,
  152. .max_tokens = IW_ESSID_MAX_SIZE,
  153. .flags = IW_DESCR_FLAG_EVENT,
  154. },
  155. [IW_IOCTL_IDX(SIOCGIWESSID)] = {
  156. .header_type = IW_HEADER_TYPE_POINT,
  157. .token_size = 1,
  158. .max_tokens = IW_ESSID_MAX_SIZE,
  159. .flags = IW_DESCR_FLAG_DUMP,
  160. },
  161. [IW_IOCTL_IDX(SIOCSIWNICKN)] = {
  162. .header_type = IW_HEADER_TYPE_POINT,
  163. .token_size = 1,
  164. .max_tokens = IW_ESSID_MAX_SIZE,
  165. },
  166. [IW_IOCTL_IDX(SIOCGIWNICKN)] = {
  167. .header_type = IW_HEADER_TYPE_POINT,
  168. .token_size = 1,
  169. .max_tokens = IW_ESSID_MAX_SIZE,
  170. },
  171. [IW_IOCTL_IDX(SIOCSIWRATE)] = {
  172. .header_type = IW_HEADER_TYPE_PARAM,
  173. },
  174. [IW_IOCTL_IDX(SIOCGIWRATE)] = {
  175. .header_type = IW_HEADER_TYPE_PARAM,
  176. },
  177. [IW_IOCTL_IDX(SIOCSIWRTS)] = {
  178. .header_type = IW_HEADER_TYPE_PARAM,
  179. },
  180. [IW_IOCTL_IDX(SIOCGIWRTS)] = {
  181. .header_type = IW_HEADER_TYPE_PARAM,
  182. },
  183. [IW_IOCTL_IDX(SIOCSIWFRAG)] = {
  184. .header_type = IW_HEADER_TYPE_PARAM,
  185. },
  186. [IW_IOCTL_IDX(SIOCGIWFRAG)] = {
  187. .header_type = IW_HEADER_TYPE_PARAM,
  188. },
  189. [IW_IOCTL_IDX(SIOCSIWTXPOW)] = {
  190. .header_type = IW_HEADER_TYPE_PARAM,
  191. },
  192. [IW_IOCTL_IDX(SIOCGIWTXPOW)] = {
  193. .header_type = IW_HEADER_TYPE_PARAM,
  194. },
  195. [IW_IOCTL_IDX(SIOCSIWRETRY)] = {
  196. .header_type = IW_HEADER_TYPE_PARAM,
  197. },
  198. [IW_IOCTL_IDX(SIOCGIWRETRY)] = {
  199. .header_type = IW_HEADER_TYPE_PARAM,
  200. },
  201. [IW_IOCTL_IDX(SIOCSIWENCODE)] = {
  202. .header_type = IW_HEADER_TYPE_POINT,
  203. .token_size = 1,
  204. .max_tokens = IW_ENCODING_TOKEN_MAX,
  205. .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
  206. },
  207. [IW_IOCTL_IDX(SIOCGIWENCODE)] = {
  208. .header_type = IW_HEADER_TYPE_POINT,
  209. .token_size = 1,
  210. .max_tokens = IW_ENCODING_TOKEN_MAX,
  211. .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
  212. },
  213. [IW_IOCTL_IDX(SIOCSIWPOWER)] = {
  214. .header_type = IW_HEADER_TYPE_PARAM,
  215. },
  216. [IW_IOCTL_IDX(SIOCGIWPOWER)] = {
  217. .header_type = IW_HEADER_TYPE_PARAM,
  218. },
  219. [IW_IOCTL_IDX(SIOCSIWGENIE)] = {
  220. .header_type = IW_HEADER_TYPE_POINT,
  221. .token_size = 1,
  222. .max_tokens = IW_GENERIC_IE_MAX,
  223. },
  224. [IW_IOCTL_IDX(SIOCGIWGENIE)] = {
  225. .header_type = IW_HEADER_TYPE_POINT,
  226. .token_size = 1,
  227. .max_tokens = IW_GENERIC_IE_MAX,
  228. },
  229. [IW_IOCTL_IDX(SIOCSIWAUTH)] = {
  230. .header_type = IW_HEADER_TYPE_PARAM,
  231. },
  232. [IW_IOCTL_IDX(SIOCGIWAUTH)] = {
  233. .header_type = IW_HEADER_TYPE_PARAM,
  234. },
  235. [IW_IOCTL_IDX(SIOCSIWENCODEEXT)] = {
  236. .header_type = IW_HEADER_TYPE_POINT,
  237. .token_size = 1,
  238. .min_tokens = sizeof(struct iw_encode_ext),
  239. .max_tokens = sizeof(struct iw_encode_ext) +
  240. IW_ENCODING_TOKEN_MAX,
  241. },
  242. [IW_IOCTL_IDX(SIOCGIWENCODEEXT)] = {
  243. .header_type = IW_HEADER_TYPE_POINT,
  244. .token_size = 1,
  245. .min_tokens = sizeof(struct iw_encode_ext),
  246. .max_tokens = sizeof(struct iw_encode_ext) +
  247. IW_ENCODING_TOKEN_MAX,
  248. },
  249. [IW_IOCTL_IDX(SIOCSIWPMKSA)] = {
  250. .header_type = IW_HEADER_TYPE_POINT,
  251. .token_size = 1,
  252. .min_tokens = sizeof(struct iw_pmksa),
  253. .max_tokens = sizeof(struct iw_pmksa),
  254. },
  255. };
  256. static const unsigned int standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
  257. /*
  258. * Meta-data about all the additional standard Wireless Extension events
  259. * we know about.
  260. */
  261. static const struct iw_ioctl_description standard_event[] = {
  262. [IW_EVENT_IDX(IWEVTXDROP)] = {
  263. .header_type = IW_HEADER_TYPE_ADDR,
  264. },
  265. [IW_EVENT_IDX(IWEVQUAL)] = {
  266. .header_type = IW_HEADER_TYPE_QUAL,
  267. },
  268. [IW_EVENT_IDX(IWEVCUSTOM)] = {
  269. .header_type = IW_HEADER_TYPE_POINT,
  270. .token_size = 1,
  271. .max_tokens = IW_CUSTOM_MAX,
  272. },
  273. [IW_EVENT_IDX(IWEVREGISTERED)] = {
  274. .header_type = IW_HEADER_TYPE_ADDR,
  275. },
  276. [IW_EVENT_IDX(IWEVEXPIRED)] = {
  277. .header_type = IW_HEADER_TYPE_ADDR,
  278. },
  279. [IW_EVENT_IDX(IWEVGENIE)] = {
  280. .header_type = IW_HEADER_TYPE_POINT,
  281. .token_size = 1,
  282. .max_tokens = IW_GENERIC_IE_MAX,
  283. },
  284. [IW_EVENT_IDX(IWEVMICHAELMICFAILURE)] = {
  285. .header_type = IW_HEADER_TYPE_POINT,
  286. .token_size = 1,
  287. .max_tokens = sizeof(struct iw_michaelmicfailure),
  288. },
  289. [IW_EVENT_IDX(IWEVASSOCREQIE)] = {
  290. .header_type = IW_HEADER_TYPE_POINT,
  291. .token_size = 1,
  292. .max_tokens = IW_GENERIC_IE_MAX,
  293. },
  294. [IW_EVENT_IDX(IWEVASSOCRESPIE)] = {
  295. .header_type = IW_HEADER_TYPE_POINT,
  296. .token_size = 1,
  297. .max_tokens = IW_GENERIC_IE_MAX,
  298. },
  299. [IW_EVENT_IDX(IWEVPMKIDCAND)] = {
  300. .header_type = IW_HEADER_TYPE_POINT,
  301. .token_size = 1,
  302. .max_tokens = sizeof(struct iw_pmkid_cand),
  303. },
  304. };
  305. static const unsigned int standard_event_num = ARRAY_SIZE(standard_event);
  306. /* Size (in bytes) of various events */
  307. static const int event_type_size[] = {
  308. IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  309. 0,
  310. IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  311. 0,
  312. IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  313. IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  314. IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  315. 0,
  316. IW_EV_POINT_LEN, /* Without variable payload */
  317. IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  318. IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  319. };
  320. #ifdef CONFIG_COMPAT
  321. static const int compat_event_type_size[] = {
  322. IW_EV_COMPAT_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  323. 0,
  324. IW_EV_COMPAT_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  325. 0,
  326. IW_EV_COMPAT_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  327. IW_EV_COMPAT_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  328. IW_EV_COMPAT_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  329. 0,
  330. IW_EV_COMPAT_POINT_LEN, /* Without variable payload */
  331. IW_EV_COMPAT_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  332. IW_EV_COMPAT_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  333. };
  334. #endif
  335. /* IW event code */
  336. void wireless_nlevent_flush(void)
  337. {
  338. struct sk_buff *skb;
  339. struct net *net;
  340. down_read(&net_rwsem);
  341. for_each_net(net) {
  342. while ((skb = skb_dequeue(&net->wext_nlevents)))
  343. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL,
  344. GFP_KERNEL);
  345. }
  346. up_read(&net_rwsem);
  347. }
  348. EXPORT_SYMBOL_GPL(wireless_nlevent_flush);
  349. static int wext_netdev_notifier_call(struct notifier_block *nb,
  350. unsigned long state, void *ptr)
  351. {
  352. /*
  353. * When a netdev changes state in any way, flush all pending messages
  354. * to avoid them going out in a strange order, e.g. RTM_NEWLINK after
  355. * RTM_DELLINK, or with IFF_UP after without IFF_UP during dev_close()
  356. * or similar - all of which could otherwise happen due to delays from
  357. * schedule_work().
  358. */
  359. wireless_nlevent_flush();
  360. return NOTIFY_OK;
  361. }
  362. static struct notifier_block wext_netdev_notifier = {
  363. .notifier_call = wext_netdev_notifier_call,
  364. };
  365. static int __net_init wext_pernet_init(struct net *net)
  366. {
  367. skb_queue_head_init(&net->wext_nlevents);
  368. return 0;
  369. }
  370. static void __net_exit wext_pernet_exit(struct net *net)
  371. {
  372. skb_queue_purge(&net->wext_nlevents);
  373. }
  374. static struct pernet_operations wext_pernet_ops = {
  375. .init = wext_pernet_init,
  376. .exit = wext_pernet_exit,
  377. };
  378. static int __init wireless_nlevent_init(void)
  379. {
  380. int err = register_pernet_subsys(&wext_pernet_ops);
  381. if (err)
  382. return err;
  383. err = register_netdevice_notifier(&wext_netdev_notifier);
  384. if (err)
  385. unregister_pernet_subsys(&wext_pernet_ops);
  386. return err;
  387. }
  388. subsys_initcall(wireless_nlevent_init);
  389. /* Process events generated by the wireless layer or the driver. */
  390. static void wireless_nlevent_process(struct work_struct *work)
  391. {
  392. wireless_nlevent_flush();
  393. }
  394. static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
  395. static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
  396. struct sk_buff *skb)
  397. {
  398. struct ifinfomsg *r;
  399. struct nlmsghdr *nlh;
  400. nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
  401. if (!nlh)
  402. return NULL;
  403. r = nlmsg_data(nlh);
  404. r->ifi_family = AF_UNSPEC;
  405. r->__ifi_pad = 0;
  406. r->ifi_type = dev->type;
  407. r->ifi_index = dev->ifindex;
  408. r->ifi_flags = dev_get_flags(dev);
  409. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  410. if (nla_put_string(skb, IFLA_IFNAME, dev->name))
  411. goto nla_put_failure;
  412. return nlh;
  413. nla_put_failure:
  414. nlmsg_cancel(skb, nlh);
  415. return NULL;
  416. }
  417. /*
  418. * Main event dispatcher. Called from other parts and drivers.
  419. * Send the event on the appropriate channels.
  420. * May be called from interrupt context.
  421. */
  422. void wireless_send_event(struct net_device * dev,
  423. unsigned int cmd,
  424. union iwreq_data * wrqu,
  425. const char * extra)
  426. {
  427. const struct iw_ioctl_description * descr = NULL;
  428. int extra_len = 0;
  429. struct iw_event *event; /* Mallocated whole event */
  430. int event_len; /* Its size */
  431. int hdr_len; /* Size of the event header */
  432. int wrqu_off = 0; /* Offset in wrqu */
  433. /* Don't "optimise" the following variable, it will crash */
  434. unsigned int cmd_index; /* *MUST* be unsigned */
  435. struct sk_buff *skb;
  436. struct nlmsghdr *nlh;
  437. struct nlattr *nla;
  438. #ifdef CONFIG_COMPAT
  439. struct __compat_iw_event *compat_event;
  440. struct compat_iw_point compat_wrqu;
  441. struct sk_buff *compskb;
  442. #endif
  443. /*
  444. * Nothing in the kernel sends scan events with data, be safe.
  445. * This is necessary because we cannot fix up scan event data
  446. * for compat, due to being contained in 'extra', but normally
  447. * applications are required to retrieve the scan data anyway
  448. * and no data is included in the event, this codifies that
  449. * practice.
  450. */
  451. if (WARN_ON(cmd == SIOCGIWSCAN && extra))
  452. extra = NULL;
  453. /* Get the description of the Event */
  454. if (cmd <= SIOCIWLAST) {
  455. cmd_index = IW_IOCTL_IDX(cmd);
  456. if (cmd_index < standard_ioctl_num)
  457. descr = &(standard_ioctl[cmd_index]);
  458. } else {
  459. cmd_index = IW_EVENT_IDX(cmd);
  460. if (cmd_index < standard_event_num)
  461. descr = &(standard_event[cmd_index]);
  462. }
  463. /* Don't accept unknown events */
  464. if (descr == NULL) {
  465. /* Note : we don't return an error to the driver, because
  466. * the driver would not know what to do about it. It can't
  467. * return an error to the user, because the event is not
  468. * initiated by a user request.
  469. * The best the driver could do is to log an error message.
  470. * We will do it ourselves instead...
  471. */
  472. netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  473. cmd);
  474. return;
  475. }
  476. /* Check extra parameters and set extra_len */
  477. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  478. /* Check if number of token fits within bounds */
  479. if (wrqu->data.length > descr->max_tokens) {
  480. netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too big (%d)\n",
  481. cmd, wrqu->data.length);
  482. return;
  483. }
  484. if (wrqu->data.length < descr->min_tokens) {
  485. netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too small (%d)\n",
  486. cmd, wrqu->data.length);
  487. return;
  488. }
  489. /* Calculate extra_len - extra is NULL for restricted events */
  490. if (extra != NULL)
  491. extra_len = wrqu->data.length * descr->token_size;
  492. /* Always at an offset in wrqu */
  493. wrqu_off = IW_EV_POINT_OFF;
  494. }
  495. /* Total length of the event */
  496. hdr_len = event_type_size[descr->header_type];
  497. event_len = hdr_len + extra_len;
  498. /*
  499. * The problem for 64/32 bit.
  500. *
  501. * On 64-bit, a regular event is laid out as follows:
  502. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  503. * | event.len | event.cmd | p a d d i n g |
  504. * | wrqu data ... (with the correct size) |
  505. *
  506. * This padding exists because we manipulate event->u,
  507. * and 'event' is not packed.
  508. *
  509. * An iw_point event is laid out like this instead:
  510. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  511. * | event.len | event.cmd | p a d d i n g |
  512. * | iwpnt.len | iwpnt.flg | p a d d i n g |
  513. * | extra data ...
  514. *
  515. * The second padding exists because struct iw_point is extended,
  516. * but this depends on the platform...
  517. *
  518. * On 32-bit, all the padding shouldn't be there.
  519. */
  520. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  521. if (!skb)
  522. return;
  523. /* Send via the RtNetlink event channel */
  524. nlh = rtnetlink_ifinfo_prep(dev, skb);
  525. if (WARN_ON(!nlh)) {
  526. kfree_skb(skb);
  527. return;
  528. }
  529. /* Add the wireless events in the netlink packet */
  530. nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
  531. if (!nla) {
  532. kfree_skb(skb);
  533. return;
  534. }
  535. event = nla_data(nla);
  536. /* Fill event - first clear to avoid data leaking */
  537. memset(event, 0, hdr_len);
  538. event->len = event_len;
  539. event->cmd = cmd;
  540. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  541. if (extra_len)
  542. memcpy(((char *) event) + hdr_len, extra, extra_len);
  543. nlmsg_end(skb, nlh);
  544. #ifdef CONFIG_COMPAT
  545. hdr_len = compat_event_type_size[descr->header_type];
  546. event_len = hdr_len + extra_len;
  547. compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  548. if (!compskb) {
  549. kfree_skb(skb);
  550. return;
  551. }
  552. /* Send via the RtNetlink event channel */
  553. nlh = rtnetlink_ifinfo_prep(dev, compskb);
  554. if (WARN_ON(!nlh)) {
  555. kfree_skb(skb);
  556. kfree_skb(compskb);
  557. return;
  558. }
  559. /* Add the wireless events in the netlink packet */
  560. nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
  561. if (!nla) {
  562. kfree_skb(skb);
  563. kfree_skb(compskb);
  564. return;
  565. }
  566. compat_event = nla_data(nla);
  567. compat_event->len = event_len;
  568. compat_event->cmd = cmd;
  569. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  570. compat_wrqu.length = wrqu->data.length;
  571. compat_wrqu.flags = wrqu->data.flags;
  572. memcpy(&compat_event->pointer,
  573. ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
  574. hdr_len - IW_EV_COMPAT_LCP_LEN);
  575. if (extra_len)
  576. memcpy(((char *) compat_event) + hdr_len,
  577. extra, extra_len);
  578. } else {
  579. /* extra_len must be zero, so no if (extra) needed */
  580. memcpy(&compat_event->pointer, wrqu,
  581. hdr_len - IW_EV_COMPAT_LCP_LEN);
  582. }
  583. nlmsg_end(compskb, nlh);
  584. skb_shinfo(skb)->frag_list = compskb;
  585. #endif
  586. skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
  587. schedule_work(&wireless_nlevent_work);
  588. }
  589. EXPORT_SYMBOL(wireless_send_event);
  590. /* IW handlers */
  591. struct iw_statistics *get_wireless_stats(struct net_device *dev)
  592. {
  593. #ifdef CONFIG_WIRELESS_EXT
  594. if ((dev->wireless_handlers != NULL) &&
  595. (dev->wireless_handlers->get_wireless_stats != NULL))
  596. return dev->wireless_handlers->get_wireless_stats(dev);
  597. #endif
  598. #ifdef CONFIG_CFG80211_WEXT
  599. if (dev->ieee80211_ptr &&
  600. dev->ieee80211_ptr->wiphy &&
  601. dev->ieee80211_ptr->wiphy->wext &&
  602. dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
  603. return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
  604. #endif
  605. /* not found */
  606. return NULL;
  607. }
  608. /* noinline to avoid a bogus warning with -O3 */
  609. static noinline int iw_handler_get_iwstats(struct net_device * dev,
  610. struct iw_request_info * info,
  611. union iwreq_data * wrqu,
  612. char * extra)
  613. {
  614. /* Get stats from the driver */
  615. struct iw_statistics *stats;
  616. stats = get_wireless_stats(dev);
  617. if (stats) {
  618. /* Copy statistics to extra */
  619. memcpy(extra, stats, sizeof(struct iw_statistics));
  620. wrqu->data.length = sizeof(struct iw_statistics);
  621. /* Check if we need to clear the updated flag */
  622. if (wrqu->data.flags != 0)
  623. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  624. return 0;
  625. } else
  626. return -EOPNOTSUPP;
  627. }
  628. static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
  629. {
  630. /* Don't "optimise" the following variable, it will crash */
  631. unsigned int index; /* *MUST* be unsigned */
  632. const struct iw_handler_def *handlers = NULL;
  633. #ifdef CONFIG_CFG80211_WEXT
  634. if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
  635. handlers = dev->ieee80211_ptr->wiphy->wext;
  636. #endif
  637. #ifdef CONFIG_WIRELESS_EXT
  638. if (dev->wireless_handlers)
  639. handlers = dev->wireless_handlers;
  640. #endif
  641. if (!handlers)
  642. return NULL;
  643. /* Try as a standard command */
  644. index = IW_IOCTL_IDX(cmd);
  645. if (index < handlers->num_standard)
  646. return handlers->standard[index];
  647. #ifdef CONFIG_WEXT_PRIV
  648. /* Try as a private command */
  649. index = cmd - SIOCIWFIRSTPRIV;
  650. if (index < handlers->num_private)
  651. return handlers->private[index];
  652. #endif
  653. /* Not found */
  654. return NULL;
  655. }
  656. static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
  657. const struct iw_ioctl_description *descr,
  658. iw_handler handler, struct net_device *dev,
  659. struct iw_request_info *info)
  660. {
  661. int err, extra_size, user_length = 0, essid_compat = 0;
  662. char *extra;
  663. /* Calculate space needed by arguments. Always allocate
  664. * for max space.
  665. */
  666. extra_size = descr->max_tokens * descr->token_size;
  667. /* Check need for ESSID compatibility for WE < 21 */
  668. switch (cmd) {
  669. case SIOCSIWESSID:
  670. case SIOCGIWESSID:
  671. case SIOCSIWNICKN:
  672. case SIOCGIWNICKN:
  673. if (iwp->length == descr->max_tokens + 1)
  674. essid_compat = 1;
  675. else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  676. char essid[IW_ESSID_MAX_SIZE + 1];
  677. unsigned int len;
  678. len = iwp->length * descr->token_size;
  679. if (len > IW_ESSID_MAX_SIZE)
  680. return -EFAULT;
  681. err = copy_from_user(essid, iwp->pointer, len);
  682. if (err)
  683. return -EFAULT;
  684. if (essid[iwp->length - 1] == '\0')
  685. essid_compat = 1;
  686. }
  687. break;
  688. default:
  689. break;
  690. }
  691. iwp->length -= essid_compat;
  692. /* Check what user space is giving us */
  693. if (IW_IS_SET(cmd)) {
  694. /* Check NULL pointer */
  695. if (!iwp->pointer && iwp->length != 0)
  696. return -EFAULT;
  697. /* Check if number of token fits within bounds */
  698. if (iwp->length > descr->max_tokens)
  699. return -E2BIG;
  700. if (iwp->length < descr->min_tokens)
  701. return -EINVAL;
  702. } else {
  703. /* Check NULL pointer */
  704. if (!iwp->pointer)
  705. return -EFAULT;
  706. /* Save user space buffer size for checking */
  707. user_length = iwp->length;
  708. /* Don't check if user_length > max to allow forward
  709. * compatibility. The test user_length < min is
  710. * implied by the test at the end.
  711. */
  712. /* Support for very large requests */
  713. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  714. (user_length > descr->max_tokens)) {
  715. /* Allow userspace to GET more than max so
  716. * we can support any size GET requests.
  717. * There is still a limit : -ENOMEM.
  718. */
  719. extra_size = user_length * descr->token_size;
  720. /* Note : user_length is originally a __u16,
  721. * and token_size is controlled by us,
  722. * so extra_size won't get negative and
  723. * won't overflow...
  724. */
  725. }
  726. }
  727. /* kzalloc() ensures NULL-termination for essid_compat. */
  728. extra = kzalloc(extra_size, GFP_KERNEL);
  729. if (!extra)
  730. return -ENOMEM;
  731. /* If it is a SET, get all the extra data in here */
  732. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  733. if (copy_from_user(extra, iwp->pointer,
  734. iwp->length *
  735. descr->token_size)) {
  736. err = -EFAULT;
  737. goto out;
  738. }
  739. if (cmd == SIOCSIWENCODEEXT) {
  740. struct iw_encode_ext *ee = (void *) extra;
  741. if (iwp->length < sizeof(*ee) + ee->key_len) {
  742. err = -EFAULT;
  743. goto out;
  744. }
  745. }
  746. }
  747. if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
  748. /*
  749. * If this is a GET, but not NOMAX, it means that the extra
  750. * data is not bounded by userspace, but by max_tokens. Thus
  751. * set the length to max_tokens. This matches the extra data
  752. * allocation.
  753. * The driver should fill it with the number of tokens it
  754. * provided, and it may check iwp->length rather than having
  755. * knowledge of max_tokens. If the driver doesn't change the
  756. * iwp->length, this ioctl just copies back max_token tokens
  757. * filled with zeroes. Hopefully the driver isn't claiming
  758. * them to be valid data.
  759. */
  760. iwp->length = descr->max_tokens;
  761. }
  762. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  763. iwp->length += essid_compat;
  764. /* If we have something to return to the user */
  765. if (!err && IW_IS_GET(cmd)) {
  766. /* Check if there is enough buffer up there */
  767. if (user_length < iwp->length) {
  768. err = -E2BIG;
  769. goto out;
  770. }
  771. if (copy_to_user(iwp->pointer, extra,
  772. iwp->length *
  773. descr->token_size)) {
  774. err = -EFAULT;
  775. goto out;
  776. }
  777. }
  778. /* Generate an event to notify listeners of the change */
  779. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  780. ((err == 0) || (err == -EIWCOMMIT))) {
  781. union iwreq_data *data = (union iwreq_data *) iwp;
  782. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  783. /* If the event is restricted, don't
  784. * export the payload.
  785. */
  786. wireless_send_event(dev, cmd, data, NULL);
  787. else
  788. wireless_send_event(dev, cmd, data, extra);
  789. }
  790. out:
  791. kfree(extra);
  792. return err;
  793. }
  794. /*
  795. * Call the commit handler in the driver
  796. * (if exist and if conditions are right)
  797. *
  798. * Note : our current commit strategy is currently pretty dumb,
  799. * but we will be able to improve on that...
  800. * The goal is to try to agreagate as many changes as possible
  801. * before doing the commit. Drivers that will define a commit handler
  802. * are usually those that need a reset after changing parameters, so
  803. * we want to minimise the number of reset.
  804. * A cool idea is to use a timer : at each "set" command, we re-set the
  805. * timer, when the timer eventually fires, we call the driver.
  806. * Hopefully, more on that later.
  807. *
  808. * Also, I'm waiting to see how many people will complain about the
  809. * netif_running(dev) test. I'm open on that one...
  810. * Hopefully, the driver will remember to do a commit in "open()" ;-)
  811. */
  812. int call_commit_handler(struct net_device *dev)
  813. {
  814. #ifdef CONFIG_WIRELESS_EXT
  815. if ((netif_running(dev)) &&
  816. (dev->wireless_handlers->standard[0] != NULL))
  817. /* Call the commit handler on the driver */
  818. return dev->wireless_handlers->standard[0](dev, NULL,
  819. NULL, NULL);
  820. else
  821. return 0; /* Command completed successfully */
  822. #else
  823. /* cfg80211 has no commit */
  824. return 0;
  825. #endif
  826. }
  827. /*
  828. * Main IOCTl dispatcher.
  829. * Check the type of IOCTL and call the appropriate wrapper...
  830. */
  831. static int wireless_process_ioctl(struct net *net, struct iwreq *iwr,
  832. unsigned int cmd,
  833. struct iw_request_info *info,
  834. wext_ioctl_func standard,
  835. wext_ioctl_func private)
  836. {
  837. struct net_device *dev;
  838. iw_handler handler;
  839. /* Permissions are already checked in dev_ioctl() before calling us.
  840. * The copy_to/from_user() of ifr is also dealt with in there */
  841. /* Make sure the device exist */
  842. if ((dev = __dev_get_by_name(net, iwr->ifr_name)) == NULL)
  843. return -ENODEV;
  844. /* A bunch of special cases, then the generic case...
  845. * Note that 'cmd' is already filtered in dev_ioctl() with
  846. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  847. if (cmd == SIOCGIWSTATS)
  848. return standard(dev, iwr, cmd, info,
  849. &iw_handler_get_iwstats);
  850. #ifdef CONFIG_WEXT_PRIV
  851. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  852. return standard(dev, iwr, cmd, info,
  853. iw_handler_get_private);
  854. #endif
  855. /* Basic check */
  856. if (!netif_device_present(dev))
  857. return -ENODEV;
  858. /* New driver API : try to find the handler */
  859. handler = get_handler(dev, cmd);
  860. if (handler) {
  861. /* Standard and private are not the same */
  862. if (cmd < SIOCIWFIRSTPRIV)
  863. return standard(dev, iwr, cmd, info, handler);
  864. else if (private)
  865. return private(dev, iwr, cmd, info, handler);
  866. }
  867. return -EOPNOTSUPP;
  868. }
  869. /* If command is `set a parameter', or `get the encoding parameters',
  870. * check if the user has the right to do it.
  871. */
  872. static int wext_permission_check(unsigned int cmd)
  873. {
  874. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
  875. cmd == SIOCGIWENCODEEXT) &&
  876. !capable(CAP_NET_ADMIN))
  877. return -EPERM;
  878. return 0;
  879. }
  880. /* entry point from dev ioctl */
  881. static int wext_ioctl_dispatch(struct net *net, struct iwreq *iwr,
  882. unsigned int cmd, struct iw_request_info *info,
  883. wext_ioctl_func standard,
  884. wext_ioctl_func private)
  885. {
  886. int ret = wext_permission_check(cmd);
  887. if (ret)
  888. return ret;
  889. dev_load(net, iwr->ifr_name);
  890. rtnl_lock();
  891. ret = wireless_process_ioctl(net, iwr, cmd, info, standard, private);
  892. rtnl_unlock();
  893. return ret;
  894. }
  895. /*
  896. * Wrapper to call a standard Wireless Extension handler.
  897. * We do various checks and also take care of moving data between
  898. * user space and kernel space.
  899. */
  900. static int ioctl_standard_call(struct net_device * dev,
  901. struct iwreq *iwr,
  902. unsigned int cmd,
  903. struct iw_request_info *info,
  904. iw_handler handler)
  905. {
  906. const struct iw_ioctl_description * descr;
  907. int ret = -EINVAL;
  908. /* Get the description of the IOCTL */
  909. if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
  910. return -EOPNOTSUPP;
  911. descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
  912. /* Check if we have a pointer to user space data or not */
  913. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  914. /* No extra arguments. Trivial to handle */
  915. ret = handler(dev, info, &(iwr->u), NULL);
  916. /* Generate an event to notify listeners of the change */
  917. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  918. ((ret == 0) || (ret == -EIWCOMMIT)))
  919. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  920. } else {
  921. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  922. handler, dev, info);
  923. }
  924. /* Call commit handler if needed and defined */
  925. if (ret == -EIWCOMMIT)
  926. ret = call_commit_handler(dev);
  927. /* Here, we will generate the appropriate event if needed */
  928. return ret;
  929. }
  930. int wext_handle_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  931. {
  932. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  933. struct iwreq iwr;
  934. int ret;
  935. if (copy_from_user(&iwr, arg, sizeof(iwr)))
  936. return -EFAULT;
  937. iwr.ifr_name[sizeof(iwr.ifr_name) - 1] = 0;
  938. ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
  939. ioctl_standard_call,
  940. ioctl_private_call);
  941. if (ret >= 0 &&
  942. IW_IS_GET(cmd) &&
  943. copy_to_user(arg, &iwr, sizeof(struct iwreq)))
  944. return -EFAULT;
  945. return ret;
  946. }
  947. #ifdef CONFIG_COMPAT
  948. static int compat_standard_call(struct net_device *dev,
  949. struct iwreq *iwr,
  950. unsigned int cmd,
  951. struct iw_request_info *info,
  952. iw_handler handler)
  953. {
  954. const struct iw_ioctl_description *descr;
  955. struct compat_iw_point *iwp_compat;
  956. struct iw_point iwp;
  957. int err;
  958. descr = standard_ioctl + IW_IOCTL_IDX(cmd);
  959. if (descr->header_type != IW_HEADER_TYPE_POINT)
  960. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  961. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  962. iwp.pointer = compat_ptr(iwp_compat->pointer);
  963. iwp.length = iwp_compat->length;
  964. iwp.flags = iwp_compat->flags;
  965. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  966. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  967. iwp_compat->length = iwp.length;
  968. iwp_compat->flags = iwp.flags;
  969. return err;
  970. }
  971. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  972. unsigned long arg)
  973. {
  974. void __user *argp = (void __user *)arg;
  975. struct iw_request_info info;
  976. struct iwreq iwr;
  977. char *colon;
  978. int ret;
  979. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  980. return -EFAULT;
  981. iwr.ifr_name[IFNAMSIZ-1] = 0;
  982. colon = strchr(iwr.ifr_name, ':');
  983. if (colon)
  984. *colon = 0;
  985. info.cmd = cmd;
  986. info.flags = IW_REQUEST_FLAG_COMPAT;
  987. ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
  988. compat_standard_call,
  989. compat_private_call);
  990. if (ret >= 0 &&
  991. IW_IS_GET(cmd) &&
  992. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  993. return -EFAULT;
  994. return ret;
  995. }
  996. #endif
  997. char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
  998. char *ends, struct iw_event *iwe, int event_len)
  999. {
  1000. int lcp_len = iwe_stream_lcp_len(info);
  1001. event_len = iwe_stream_event_len_adjust(info, event_len);
  1002. /* Check if it's possible */
  1003. if (likely((stream + event_len) < ends)) {
  1004. iwe->len = event_len;
  1005. /* Beware of alignement issues on 64 bits */
  1006. memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
  1007. memcpy(stream + lcp_len, &iwe->u,
  1008. event_len - lcp_len);
  1009. stream += event_len;
  1010. }
  1011. return stream;
  1012. }
  1013. EXPORT_SYMBOL(iwe_stream_add_event);
  1014. char *iwe_stream_add_point(struct iw_request_info *info, char *stream,
  1015. char *ends, struct iw_event *iwe, char *extra)
  1016. {
  1017. int event_len = iwe_stream_point_len(info) + iwe->u.data.length;
  1018. int point_len = iwe_stream_point_len(info);
  1019. int lcp_len = iwe_stream_lcp_len(info);
  1020. /* Check if it's possible */
  1021. if (likely((stream + event_len) < ends)) {
  1022. iwe->len = event_len;
  1023. memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
  1024. memcpy(stream + lcp_len,
  1025. ((char *) &iwe->u) + IW_EV_POINT_OFF,
  1026. IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
  1027. if (iwe->u.data.length && extra)
  1028. memcpy(stream + point_len, extra, iwe->u.data.length);
  1029. stream += event_len;
  1030. }
  1031. return stream;
  1032. }
  1033. EXPORT_SYMBOL(iwe_stream_add_point);
  1034. char *iwe_stream_add_value(struct iw_request_info *info, char *event,
  1035. char *value, char *ends, struct iw_event *iwe,
  1036. int event_len)
  1037. {
  1038. int lcp_len = iwe_stream_lcp_len(info);
  1039. /* Don't duplicate LCP */
  1040. event_len -= IW_EV_LCP_LEN;
  1041. /* Check if it's possible */
  1042. if (likely((value + event_len) < ends)) {
  1043. /* Add new value */
  1044. memcpy(value, &iwe->u, event_len);
  1045. value += event_len;
  1046. /* Patch LCP */
  1047. iwe->len = value - event;
  1048. memcpy(event, (char *) iwe, lcp_len);
  1049. }
  1050. return value;
  1051. }
  1052. EXPORT_SYMBOL(iwe_stream_add_value);