wext-core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. * This file implement the Wireless Extensions core API.
  3. *
  4. * Authors : Jean Tourrilhes - HPL - <[email protected]>
  5. * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
  6. * Copyright 2009 Johannes Berg <[email protected]>
  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. ASSERT_RTNL();
  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. }
  347. EXPORT_SYMBOL_GPL(wireless_nlevent_flush);
  348. static int wext_netdev_notifier_call(struct notifier_block *nb,
  349. unsigned long state, void *ptr)
  350. {
  351. /*
  352. * When a netdev changes state in any way, flush all pending messages
  353. * to avoid them going out in a strange order, e.g. RTM_NEWLINK after
  354. * RTM_DELLINK, or with IFF_UP after without IFF_UP during dev_close()
  355. * or similar - all of which could otherwise happen due to delays from
  356. * schedule_work().
  357. */
  358. wireless_nlevent_flush();
  359. return NOTIFY_OK;
  360. }
  361. static struct notifier_block wext_netdev_notifier = {
  362. .notifier_call = wext_netdev_notifier_call,
  363. };
  364. static int __net_init wext_pernet_init(struct net *net)
  365. {
  366. skb_queue_head_init(&net->wext_nlevents);
  367. return 0;
  368. }
  369. static void __net_exit wext_pernet_exit(struct net *net)
  370. {
  371. skb_queue_purge(&net->wext_nlevents);
  372. }
  373. static struct pernet_operations wext_pernet_ops = {
  374. .init = wext_pernet_init,
  375. .exit = wext_pernet_exit,
  376. };
  377. static int __init wireless_nlevent_init(void)
  378. {
  379. int err = register_pernet_subsys(&wext_pernet_ops);
  380. if (err)
  381. return err;
  382. err = register_netdevice_notifier(&wext_netdev_notifier);
  383. if (err)
  384. unregister_pernet_subsys(&wext_pernet_ops);
  385. return err;
  386. }
  387. subsys_initcall(wireless_nlevent_init);
  388. /* Process events generated by the wireless layer or the driver. */
  389. static void wireless_nlevent_process(struct work_struct *work)
  390. {
  391. rtnl_lock();
  392. wireless_nlevent_flush();
  393. rtnl_unlock();
  394. }
  395. static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
  396. static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
  397. struct sk_buff *skb)
  398. {
  399. struct ifinfomsg *r;
  400. struct nlmsghdr *nlh;
  401. nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
  402. if (!nlh)
  403. return NULL;
  404. r = nlmsg_data(nlh);
  405. r->ifi_family = AF_UNSPEC;
  406. r->__ifi_pad = 0;
  407. r->ifi_type = dev->type;
  408. r->ifi_index = dev->ifindex;
  409. r->ifi_flags = dev_get_flags(dev);
  410. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  411. if (nla_put_string(skb, IFLA_IFNAME, dev->name))
  412. goto nla_put_failure;
  413. return nlh;
  414. nla_put_failure:
  415. nlmsg_cancel(skb, nlh);
  416. return NULL;
  417. }
  418. /*
  419. * Main event dispatcher. Called from other parts and drivers.
  420. * Send the event on the appropriate channels.
  421. * May be called from interrupt context.
  422. */
  423. void wireless_send_event(struct net_device * dev,
  424. unsigned int cmd,
  425. union iwreq_data * wrqu,
  426. const char * extra)
  427. {
  428. const struct iw_ioctl_description * descr = NULL;
  429. int extra_len = 0;
  430. struct iw_event *event; /* Mallocated whole event */
  431. int event_len; /* Its size */
  432. int hdr_len; /* Size of the event header */
  433. int wrqu_off = 0; /* Offset in wrqu */
  434. /* Don't "optimise" the following variable, it will crash */
  435. unsigned int cmd_index; /* *MUST* be unsigned */
  436. struct sk_buff *skb;
  437. struct nlmsghdr *nlh;
  438. struct nlattr *nla;
  439. #ifdef CONFIG_COMPAT
  440. struct __compat_iw_event *compat_event;
  441. struct compat_iw_point compat_wrqu;
  442. struct sk_buff *compskb;
  443. #endif
  444. /*
  445. * Nothing in the kernel sends scan events with data, be safe.
  446. * This is necessary because we cannot fix up scan event data
  447. * for compat, due to being contained in 'extra', but normally
  448. * applications are required to retrieve the scan data anyway
  449. * and no data is included in the event, this codifies that
  450. * practice.
  451. */
  452. if (WARN_ON(cmd == SIOCGIWSCAN && extra))
  453. extra = NULL;
  454. /* Get the description of the Event */
  455. if (cmd <= SIOCIWLAST) {
  456. cmd_index = IW_IOCTL_IDX(cmd);
  457. if (cmd_index < standard_ioctl_num)
  458. descr = &(standard_ioctl[cmd_index]);
  459. } else {
  460. cmd_index = IW_EVENT_IDX(cmd);
  461. if (cmd_index < standard_event_num)
  462. descr = &(standard_event[cmd_index]);
  463. }
  464. /* Don't accept unknown events */
  465. if (descr == NULL) {
  466. /* Note : we don't return an error to the driver, because
  467. * the driver would not know what to do about it. It can't
  468. * return an error to the user, because the event is not
  469. * initiated by a user request.
  470. * The best the driver could do is to log an error message.
  471. * We will do it ourselves instead...
  472. */
  473. netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  474. cmd);
  475. return;
  476. }
  477. /* Check extra parameters and set extra_len */
  478. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  479. /* Check if number of token fits within bounds */
  480. if (wrqu->data.length > descr->max_tokens) {
  481. netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too big (%d)\n",
  482. cmd, wrqu->data.length);
  483. return;
  484. }
  485. if (wrqu->data.length < descr->min_tokens) {
  486. netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too small (%d)\n",
  487. cmd, wrqu->data.length);
  488. return;
  489. }
  490. /* Calculate extra_len - extra is NULL for restricted events */
  491. if (extra != NULL)
  492. extra_len = wrqu->data.length * descr->token_size;
  493. /* Always at an offset in wrqu */
  494. wrqu_off = IW_EV_POINT_OFF;
  495. }
  496. /* Total length of the event */
  497. hdr_len = event_type_size[descr->header_type];
  498. event_len = hdr_len + extra_len;
  499. /*
  500. * The problem for 64/32 bit.
  501. *
  502. * On 64-bit, a regular event is laid out as follows:
  503. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  504. * | event.len | event.cmd | p a d d i n g |
  505. * | wrqu data ... (with the correct size) |
  506. *
  507. * This padding exists because we manipulate event->u,
  508. * and 'event' is not packed.
  509. *
  510. * An iw_point event is laid out like this instead:
  511. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  512. * | event.len | event.cmd | p a d d i n g |
  513. * | iwpnt.len | iwpnt.flg | p a d d i n g |
  514. * | extra data ...
  515. *
  516. * The second padding exists because struct iw_point is extended,
  517. * but this depends on the platform...
  518. *
  519. * On 32-bit, all the padding shouldn't be there.
  520. */
  521. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  522. if (!skb)
  523. return;
  524. /* Send via the RtNetlink event channel */
  525. nlh = rtnetlink_ifinfo_prep(dev, skb);
  526. if (WARN_ON(!nlh)) {
  527. kfree_skb(skb);
  528. return;
  529. }
  530. /* Add the wireless events in the netlink packet */
  531. nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
  532. if (!nla) {
  533. kfree_skb(skb);
  534. return;
  535. }
  536. event = nla_data(nla);
  537. /* Fill event - first clear to avoid data leaking */
  538. memset(event, 0, hdr_len);
  539. event->len = event_len;
  540. event->cmd = cmd;
  541. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  542. if (extra_len)
  543. memcpy(((char *) event) + hdr_len, extra, extra_len);
  544. nlmsg_end(skb, nlh);
  545. #ifdef CONFIG_COMPAT
  546. hdr_len = compat_event_type_size[descr->header_type];
  547. event_len = hdr_len + extra_len;
  548. compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  549. if (!compskb) {
  550. kfree_skb(skb);
  551. return;
  552. }
  553. /* Send via the RtNetlink event channel */
  554. nlh = rtnetlink_ifinfo_prep(dev, compskb);
  555. if (WARN_ON(!nlh)) {
  556. kfree_skb(skb);
  557. kfree_skb(compskb);
  558. return;
  559. }
  560. /* Add the wireless events in the netlink packet */
  561. nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
  562. if (!nla) {
  563. kfree_skb(skb);
  564. kfree_skb(compskb);
  565. return;
  566. }
  567. compat_event = nla_data(nla);
  568. compat_event->len = event_len;
  569. compat_event->cmd = cmd;
  570. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  571. compat_wrqu.length = wrqu->data.length;
  572. compat_wrqu.flags = wrqu->data.flags;
  573. memcpy(&compat_event->pointer,
  574. ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
  575. hdr_len - IW_EV_COMPAT_LCP_LEN);
  576. if (extra_len)
  577. memcpy(((char *) compat_event) + hdr_len,
  578. extra, extra_len);
  579. } else {
  580. /* extra_len must be zero, so no if (extra) needed */
  581. memcpy(&compat_event->pointer, wrqu,
  582. hdr_len - IW_EV_COMPAT_LCP_LEN);
  583. }
  584. nlmsg_end(compskb, nlh);
  585. skb_shinfo(skb)->frag_list = compskb;
  586. #endif
  587. skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
  588. schedule_work(&wireless_nlevent_work);
  589. }
  590. EXPORT_SYMBOL(wireless_send_event);
  591. /* IW handlers */
  592. struct iw_statistics *get_wireless_stats(struct net_device *dev)
  593. {
  594. #ifdef CONFIG_WIRELESS_EXT
  595. if ((dev->wireless_handlers != NULL) &&
  596. (dev->wireless_handlers->get_wireless_stats != NULL))
  597. return dev->wireless_handlers->get_wireless_stats(dev);
  598. #endif
  599. #ifdef CONFIG_CFG80211_WEXT
  600. if (dev->ieee80211_ptr &&
  601. dev->ieee80211_ptr->wiphy &&
  602. dev->ieee80211_ptr->wiphy->wext &&
  603. dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
  604. return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
  605. #endif
  606. /* not found */
  607. return NULL;
  608. }
  609. static 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 ifreq *ifr,
  832. unsigned int cmd,
  833. struct iw_request_info *info,
  834. wext_ioctl_func standard,
  835. wext_ioctl_func private)
  836. {
  837. struct iwreq *iwr = (struct iwreq *) ifr;
  838. struct net_device *dev;
  839. iw_handler handler;
  840. /* Permissions are already checked in dev_ioctl() before calling us.
  841. * The copy_to/from_user() of ifr is also dealt with in there */
  842. /* Make sure the device exist */
  843. if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
  844. return -ENODEV;
  845. /* A bunch of special cases, then the generic case...
  846. * Note that 'cmd' is already filtered in dev_ioctl() with
  847. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  848. if (cmd == SIOCGIWSTATS)
  849. return standard(dev, iwr, cmd, info,
  850. &iw_handler_get_iwstats);
  851. #ifdef CONFIG_WEXT_PRIV
  852. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  853. return standard(dev, iwr, cmd, info,
  854. iw_handler_get_private);
  855. #endif
  856. /* Basic check */
  857. if (!netif_device_present(dev))
  858. return -ENODEV;
  859. /* New driver API : try to find the handler */
  860. handler = get_handler(dev, cmd);
  861. if (handler) {
  862. /* Standard and private are not the same */
  863. if (cmd < SIOCIWFIRSTPRIV)
  864. return standard(dev, iwr, cmd, info, handler);
  865. else if (private)
  866. return private(dev, iwr, cmd, info, handler);
  867. }
  868. /* Old driver API : call driver ioctl handler */
  869. if (dev->netdev_ops->ndo_do_ioctl)
  870. return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
  871. return -EOPNOTSUPP;
  872. }
  873. /* If command is `set a parameter', or `get the encoding parameters',
  874. * check if the user has the right to do it.
  875. */
  876. static int wext_permission_check(unsigned int cmd)
  877. {
  878. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
  879. cmd == SIOCGIWENCODEEXT) &&
  880. !capable(CAP_NET_ADMIN))
  881. return -EPERM;
  882. return 0;
  883. }
  884. /* entry point from dev ioctl */
  885. static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
  886. unsigned int cmd, struct iw_request_info *info,
  887. wext_ioctl_func standard,
  888. wext_ioctl_func private)
  889. {
  890. int ret = wext_permission_check(cmd);
  891. if (ret)
  892. return ret;
  893. dev_load(net, ifr->ifr_name);
  894. rtnl_lock();
  895. ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
  896. rtnl_unlock();
  897. return ret;
  898. }
  899. /*
  900. * Wrapper to call a standard Wireless Extension handler.
  901. * We do various checks and also take care of moving data between
  902. * user space and kernel space.
  903. */
  904. static int ioctl_standard_call(struct net_device * dev,
  905. struct iwreq *iwr,
  906. unsigned int cmd,
  907. struct iw_request_info *info,
  908. iw_handler handler)
  909. {
  910. const struct iw_ioctl_description * descr;
  911. int ret = -EINVAL;
  912. /* Get the description of the IOCTL */
  913. if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
  914. return -EOPNOTSUPP;
  915. descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
  916. /* Check if we have a pointer to user space data or not */
  917. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  918. /* No extra arguments. Trivial to handle */
  919. ret = handler(dev, info, &(iwr->u), NULL);
  920. /* Generate an event to notify listeners of the change */
  921. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  922. ((ret == 0) || (ret == -EIWCOMMIT)))
  923. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  924. } else {
  925. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  926. handler, dev, info);
  927. }
  928. /* Call commit handler if needed and defined */
  929. if (ret == -EIWCOMMIT)
  930. ret = call_commit_handler(dev);
  931. /* Here, we will generate the appropriate event if needed */
  932. return ret;
  933. }
  934. int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
  935. void __user *arg)
  936. {
  937. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  938. int ret;
  939. ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
  940. ioctl_standard_call,
  941. ioctl_private_call);
  942. if (ret >= 0 &&
  943. IW_IS_GET(cmd) &&
  944. copy_to_user(arg, ifr, sizeof(struct iwreq)))
  945. return -EFAULT;
  946. return ret;
  947. }
  948. #ifdef CONFIG_COMPAT
  949. static int compat_standard_call(struct net_device *dev,
  950. struct iwreq *iwr,
  951. unsigned int cmd,
  952. struct iw_request_info *info,
  953. iw_handler handler)
  954. {
  955. const struct iw_ioctl_description *descr;
  956. struct compat_iw_point *iwp_compat;
  957. struct iw_point iwp;
  958. int err;
  959. descr = standard_ioctl + IW_IOCTL_IDX(cmd);
  960. if (descr->header_type != IW_HEADER_TYPE_POINT)
  961. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  962. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  963. iwp.pointer = compat_ptr(iwp_compat->pointer);
  964. iwp.length = iwp_compat->length;
  965. iwp.flags = iwp_compat->flags;
  966. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  967. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  968. iwp_compat->length = iwp.length;
  969. iwp_compat->flags = iwp.flags;
  970. return err;
  971. }
  972. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  973. unsigned long arg)
  974. {
  975. void __user *argp = (void __user *)arg;
  976. struct iw_request_info info;
  977. struct iwreq iwr;
  978. char *colon;
  979. int ret;
  980. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  981. return -EFAULT;
  982. iwr.ifr_name[IFNAMSIZ-1] = 0;
  983. colon = strchr(iwr.ifr_name, ':');
  984. if (colon)
  985. *colon = 0;
  986. info.cmd = cmd;
  987. info.flags = IW_REQUEST_FLAG_COMPAT;
  988. ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
  989. compat_standard_call,
  990. compat_private_call);
  991. if (ret >= 0 &&
  992. IW_IS_GET(cmd) &&
  993. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  994. return -EFAULT;
  995. return ret;
  996. }
  997. #endif