wext-core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. 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. return register_netdevice_notifier(&wext_netdev_notifier);
  383. }
  384. subsys_initcall(wireless_nlevent_init);
  385. /* Process events generated by the wireless layer or the driver. */
  386. static void wireless_nlevent_process(struct work_struct *work)
  387. {
  388. rtnl_lock();
  389. wireless_nlevent_flush();
  390. rtnl_unlock();
  391. }
  392. static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
  393. static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
  394. struct sk_buff *skb)
  395. {
  396. struct ifinfomsg *r;
  397. struct nlmsghdr *nlh;
  398. nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
  399. if (!nlh)
  400. return NULL;
  401. r = nlmsg_data(nlh);
  402. r->ifi_family = AF_UNSPEC;
  403. r->__ifi_pad = 0;
  404. r->ifi_type = dev->type;
  405. r->ifi_index = dev->ifindex;
  406. r->ifi_flags = dev_get_flags(dev);
  407. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  408. if (nla_put_string(skb, IFLA_IFNAME, dev->name))
  409. goto nla_put_failure;
  410. return nlh;
  411. nla_put_failure:
  412. nlmsg_cancel(skb, nlh);
  413. return NULL;
  414. }
  415. /*
  416. * Main event dispatcher. Called from other parts and drivers.
  417. * Send the event on the appropriate channels.
  418. * May be called from interrupt context.
  419. */
  420. void wireless_send_event(struct net_device * dev,
  421. unsigned int cmd,
  422. union iwreq_data * wrqu,
  423. const char * extra)
  424. {
  425. const struct iw_ioctl_description * descr = NULL;
  426. int extra_len = 0;
  427. struct iw_event *event; /* Mallocated whole event */
  428. int event_len; /* Its size */
  429. int hdr_len; /* Size of the event header */
  430. int wrqu_off = 0; /* Offset in wrqu */
  431. /* Don't "optimise" the following variable, it will crash */
  432. unsigned int cmd_index; /* *MUST* be unsigned */
  433. struct sk_buff *skb;
  434. struct nlmsghdr *nlh;
  435. struct nlattr *nla;
  436. #ifdef CONFIG_COMPAT
  437. struct __compat_iw_event *compat_event;
  438. struct compat_iw_point compat_wrqu;
  439. struct sk_buff *compskb;
  440. #endif
  441. /*
  442. * Nothing in the kernel sends scan events with data, be safe.
  443. * This is necessary because we cannot fix up scan event data
  444. * for compat, due to being contained in 'extra', but normally
  445. * applications are required to retrieve the scan data anyway
  446. * and no data is included in the event, this codifies that
  447. * practice.
  448. */
  449. if (WARN_ON(cmd == SIOCGIWSCAN && extra))
  450. extra = NULL;
  451. /* Get the description of the Event */
  452. if (cmd <= SIOCIWLAST) {
  453. cmd_index = IW_IOCTL_IDX(cmd);
  454. if (cmd_index < standard_ioctl_num)
  455. descr = &(standard_ioctl[cmd_index]);
  456. } else {
  457. cmd_index = IW_EVENT_IDX(cmd);
  458. if (cmd_index < standard_event_num)
  459. descr = &(standard_event[cmd_index]);
  460. }
  461. /* Don't accept unknown events */
  462. if (descr == NULL) {
  463. /* Note : we don't return an error to the driver, because
  464. * the driver would not know what to do about it. It can't
  465. * return an error to the user, because the event is not
  466. * initiated by a user request.
  467. * The best the driver could do is to log an error message.
  468. * We will do it ourselves instead...
  469. */
  470. netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  471. cmd);
  472. return;
  473. }
  474. /* Check extra parameters and set extra_len */
  475. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  476. /* Check if number of token fits within bounds */
  477. if (wrqu->data.length > descr->max_tokens) {
  478. netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too big (%d)\n",
  479. cmd, wrqu->data.length);
  480. return;
  481. }
  482. if (wrqu->data.length < descr->min_tokens) {
  483. netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too small (%d)\n",
  484. cmd, wrqu->data.length);
  485. return;
  486. }
  487. /* Calculate extra_len - extra is NULL for restricted events */
  488. if (extra != NULL)
  489. extra_len = wrqu->data.length * descr->token_size;
  490. /* Always at an offset in wrqu */
  491. wrqu_off = IW_EV_POINT_OFF;
  492. }
  493. /* Total length of the event */
  494. hdr_len = event_type_size[descr->header_type];
  495. event_len = hdr_len + extra_len;
  496. /*
  497. * The problem for 64/32 bit.
  498. *
  499. * On 64-bit, a regular event is laid out as follows:
  500. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  501. * | event.len | event.cmd | p a d d i n g |
  502. * | wrqu data ... (with the correct size) |
  503. *
  504. * This padding exists because we manipulate event->u,
  505. * and 'event' is not packed.
  506. *
  507. * An iw_point event is laid out like this instead:
  508. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  509. * | event.len | event.cmd | p a d d i n g |
  510. * | iwpnt.len | iwpnt.flg | p a d d i n g |
  511. * | extra data ...
  512. *
  513. * The second padding exists because struct iw_point is extended,
  514. * but this depends on the platform...
  515. *
  516. * On 32-bit, all the padding shouldn't be there.
  517. */
  518. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  519. if (!skb)
  520. return;
  521. /* Send via the RtNetlink event channel */
  522. nlh = rtnetlink_ifinfo_prep(dev, skb);
  523. if (WARN_ON(!nlh)) {
  524. kfree_skb(skb);
  525. return;
  526. }
  527. /* Add the wireless events in the netlink packet */
  528. nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
  529. if (!nla) {
  530. kfree_skb(skb);
  531. return;
  532. }
  533. event = nla_data(nla);
  534. /* Fill event - first clear to avoid data leaking */
  535. memset(event, 0, hdr_len);
  536. event->len = event_len;
  537. event->cmd = cmd;
  538. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  539. if (extra_len)
  540. memcpy(((char *) event) + hdr_len, extra, extra_len);
  541. nlmsg_end(skb, nlh);
  542. #ifdef CONFIG_COMPAT
  543. hdr_len = compat_event_type_size[descr->header_type];
  544. event_len = hdr_len + extra_len;
  545. compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  546. if (!compskb) {
  547. kfree_skb(skb);
  548. return;
  549. }
  550. /* Send via the RtNetlink event channel */
  551. nlh = rtnetlink_ifinfo_prep(dev, compskb);
  552. if (WARN_ON(!nlh)) {
  553. kfree_skb(skb);
  554. kfree_skb(compskb);
  555. return;
  556. }
  557. /* Add the wireless events in the netlink packet */
  558. nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
  559. if (!nla) {
  560. kfree_skb(skb);
  561. kfree_skb(compskb);
  562. return;
  563. }
  564. compat_event = nla_data(nla);
  565. compat_event->len = event_len;
  566. compat_event->cmd = cmd;
  567. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  568. compat_wrqu.length = wrqu->data.length;
  569. compat_wrqu.flags = wrqu->data.flags;
  570. memcpy(&compat_event->pointer,
  571. ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
  572. hdr_len - IW_EV_COMPAT_LCP_LEN);
  573. if (extra_len)
  574. memcpy(((char *) compat_event) + hdr_len,
  575. extra, extra_len);
  576. } else {
  577. /* extra_len must be zero, so no if (extra) needed */
  578. memcpy(&compat_event->pointer, wrqu,
  579. hdr_len - IW_EV_COMPAT_LCP_LEN);
  580. }
  581. nlmsg_end(compskb, nlh);
  582. skb_shinfo(skb)->frag_list = compskb;
  583. #endif
  584. skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
  585. schedule_work(&wireless_nlevent_work);
  586. }
  587. EXPORT_SYMBOL(wireless_send_event);
  588. /* IW handlers */
  589. struct iw_statistics *get_wireless_stats(struct net_device *dev)
  590. {
  591. #ifdef CONFIG_WIRELESS_EXT
  592. if ((dev->wireless_handlers != NULL) &&
  593. (dev->wireless_handlers->get_wireless_stats != NULL))
  594. return dev->wireless_handlers->get_wireless_stats(dev);
  595. #endif
  596. #ifdef CONFIG_CFG80211_WEXT
  597. if (dev->ieee80211_ptr &&
  598. dev->ieee80211_ptr->wiphy &&
  599. dev->ieee80211_ptr->wiphy->wext &&
  600. dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
  601. return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
  602. #endif
  603. /* not found */
  604. return NULL;
  605. }
  606. static int iw_handler_get_iwstats(struct net_device * dev,
  607. struct iw_request_info * info,
  608. union iwreq_data * wrqu,
  609. char * extra)
  610. {
  611. /* Get stats from the driver */
  612. struct iw_statistics *stats;
  613. stats = get_wireless_stats(dev);
  614. if (stats) {
  615. /* Copy statistics to extra */
  616. memcpy(extra, stats, sizeof(struct iw_statistics));
  617. wrqu->data.length = sizeof(struct iw_statistics);
  618. /* Check if we need to clear the updated flag */
  619. if (wrqu->data.flags != 0)
  620. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  621. return 0;
  622. } else
  623. return -EOPNOTSUPP;
  624. }
  625. static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
  626. {
  627. /* Don't "optimise" the following variable, it will crash */
  628. unsigned int index; /* *MUST* be unsigned */
  629. const struct iw_handler_def *handlers = NULL;
  630. #ifdef CONFIG_CFG80211_WEXT
  631. if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
  632. handlers = dev->ieee80211_ptr->wiphy->wext;
  633. #endif
  634. #ifdef CONFIG_WIRELESS_EXT
  635. if (dev->wireless_handlers)
  636. handlers = dev->wireless_handlers;
  637. #endif
  638. if (!handlers)
  639. return NULL;
  640. /* Try as a standard command */
  641. index = IW_IOCTL_IDX(cmd);
  642. if (index < handlers->num_standard)
  643. return handlers->standard[index];
  644. #ifdef CONFIG_WEXT_PRIV
  645. /* Try as a private command */
  646. index = cmd - SIOCIWFIRSTPRIV;
  647. if (index < handlers->num_private)
  648. return handlers->private[index];
  649. #endif
  650. /* Not found */
  651. return NULL;
  652. }
  653. static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
  654. const struct iw_ioctl_description *descr,
  655. iw_handler handler, struct net_device *dev,
  656. struct iw_request_info *info)
  657. {
  658. int err, extra_size, user_length = 0, essid_compat = 0;
  659. char *extra;
  660. /* Calculate space needed by arguments. Always allocate
  661. * for max space.
  662. */
  663. extra_size = descr->max_tokens * descr->token_size;
  664. /* Check need for ESSID compatibility for WE < 21 */
  665. switch (cmd) {
  666. case SIOCSIWESSID:
  667. case SIOCGIWESSID:
  668. case SIOCSIWNICKN:
  669. case SIOCGIWNICKN:
  670. if (iwp->length == descr->max_tokens + 1)
  671. essid_compat = 1;
  672. else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  673. char essid[IW_ESSID_MAX_SIZE + 1];
  674. unsigned int len;
  675. len = iwp->length * descr->token_size;
  676. if (len > IW_ESSID_MAX_SIZE)
  677. return -EFAULT;
  678. err = copy_from_user(essid, iwp->pointer, len);
  679. if (err)
  680. return -EFAULT;
  681. if (essid[iwp->length - 1] == '\0')
  682. essid_compat = 1;
  683. }
  684. break;
  685. default:
  686. break;
  687. }
  688. iwp->length -= essid_compat;
  689. /* Check what user space is giving us */
  690. if (IW_IS_SET(cmd)) {
  691. /* Check NULL pointer */
  692. if (!iwp->pointer && iwp->length != 0)
  693. return -EFAULT;
  694. /* Check if number of token fits within bounds */
  695. if (iwp->length > descr->max_tokens)
  696. return -E2BIG;
  697. if (iwp->length < descr->min_tokens)
  698. return -EINVAL;
  699. } else {
  700. /* Check NULL pointer */
  701. if (!iwp->pointer)
  702. return -EFAULT;
  703. /* Save user space buffer size for checking */
  704. user_length = iwp->length;
  705. /* Don't check if user_length > max to allow forward
  706. * compatibility. The test user_length < min is
  707. * implied by the test at the end.
  708. */
  709. /* Support for very large requests */
  710. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  711. (user_length > descr->max_tokens)) {
  712. /* Allow userspace to GET more than max so
  713. * we can support any size GET requests.
  714. * There is still a limit : -ENOMEM.
  715. */
  716. extra_size = user_length * descr->token_size;
  717. /* Note : user_length is originally a __u16,
  718. * and token_size is controlled by us,
  719. * so extra_size won't get negative and
  720. * won't overflow...
  721. */
  722. }
  723. }
  724. /* kzalloc() ensures NULL-termination for essid_compat. */
  725. extra = kzalloc(extra_size, GFP_KERNEL);
  726. if (!extra)
  727. return -ENOMEM;
  728. /* If it is a SET, get all the extra data in here */
  729. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  730. if (copy_from_user(extra, iwp->pointer,
  731. iwp->length *
  732. descr->token_size)) {
  733. err = -EFAULT;
  734. goto out;
  735. }
  736. if (cmd == SIOCSIWENCODEEXT) {
  737. struct iw_encode_ext *ee = (void *) extra;
  738. if (iwp->length < sizeof(*ee) + ee->key_len) {
  739. err = -EFAULT;
  740. goto out;
  741. }
  742. }
  743. }
  744. if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
  745. /*
  746. * If this is a GET, but not NOMAX, it means that the extra
  747. * data is not bounded by userspace, but by max_tokens. Thus
  748. * set the length to max_tokens. This matches the extra data
  749. * allocation.
  750. * The driver should fill it with the number of tokens it
  751. * provided, and it may check iwp->length rather than having
  752. * knowledge of max_tokens. If the driver doesn't change the
  753. * iwp->length, this ioctl just copies back max_token tokens
  754. * filled with zeroes. Hopefully the driver isn't claiming
  755. * them to be valid data.
  756. */
  757. iwp->length = descr->max_tokens;
  758. }
  759. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  760. iwp->length += essid_compat;
  761. /* If we have something to return to the user */
  762. if (!err && IW_IS_GET(cmd)) {
  763. /* Check if there is enough buffer up there */
  764. if (user_length < iwp->length) {
  765. err = -E2BIG;
  766. goto out;
  767. }
  768. if (copy_to_user(iwp->pointer, extra,
  769. iwp->length *
  770. descr->token_size)) {
  771. err = -EFAULT;
  772. goto out;
  773. }
  774. }
  775. /* Generate an event to notify listeners of the change */
  776. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  777. ((err == 0) || (err == -EIWCOMMIT))) {
  778. union iwreq_data *data = (union iwreq_data *) iwp;
  779. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  780. /* If the event is restricted, don't
  781. * export the payload.
  782. */
  783. wireless_send_event(dev, cmd, data, NULL);
  784. else
  785. wireless_send_event(dev, cmd, data, extra);
  786. }
  787. out:
  788. kfree(extra);
  789. return err;
  790. }
  791. /*
  792. * Call the commit handler in the driver
  793. * (if exist and if conditions are right)
  794. *
  795. * Note : our current commit strategy is currently pretty dumb,
  796. * but we will be able to improve on that...
  797. * The goal is to try to agreagate as many changes as possible
  798. * before doing the commit. Drivers that will define a commit handler
  799. * are usually those that need a reset after changing parameters, so
  800. * we want to minimise the number of reset.
  801. * A cool idea is to use a timer : at each "set" command, we re-set the
  802. * timer, when the timer eventually fires, we call the driver.
  803. * Hopefully, more on that later.
  804. *
  805. * Also, I'm waiting to see how many people will complain about the
  806. * netif_running(dev) test. I'm open on that one...
  807. * Hopefully, the driver will remember to do a commit in "open()" ;-)
  808. */
  809. int call_commit_handler(struct net_device *dev)
  810. {
  811. #ifdef CONFIG_WIRELESS_EXT
  812. if ((netif_running(dev)) &&
  813. (dev->wireless_handlers->standard[0] != NULL))
  814. /* Call the commit handler on the driver */
  815. return dev->wireless_handlers->standard[0](dev, NULL,
  816. NULL, NULL);
  817. else
  818. return 0; /* Command completed successfully */
  819. #else
  820. /* cfg80211 has no commit */
  821. return 0;
  822. #endif
  823. }
  824. /*
  825. * Main IOCTl dispatcher.
  826. * Check the type of IOCTL and call the appropriate wrapper...
  827. */
  828. static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
  829. unsigned int cmd,
  830. struct iw_request_info *info,
  831. wext_ioctl_func standard,
  832. wext_ioctl_func private)
  833. {
  834. struct iwreq *iwr = (struct iwreq *) ifr;
  835. struct net_device *dev;
  836. iw_handler handler;
  837. /* Permissions are already checked in dev_ioctl() before calling us.
  838. * The copy_to/from_user() of ifr is also dealt with in there */
  839. /* Make sure the device exist */
  840. if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
  841. return -ENODEV;
  842. /* A bunch of special cases, then the generic case...
  843. * Note that 'cmd' is already filtered in dev_ioctl() with
  844. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  845. if (cmd == SIOCGIWSTATS)
  846. return standard(dev, iwr, cmd, info,
  847. &iw_handler_get_iwstats);
  848. #ifdef CONFIG_WEXT_PRIV
  849. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  850. return standard(dev, iwr, cmd, info,
  851. iw_handler_get_private);
  852. #endif
  853. /* Basic check */
  854. if (!netif_device_present(dev))
  855. return -ENODEV;
  856. /* New driver API : try to find the handler */
  857. handler = get_handler(dev, cmd);
  858. if (handler) {
  859. /* Standard and private are not the same */
  860. if (cmd < SIOCIWFIRSTPRIV)
  861. return standard(dev, iwr, cmd, info, handler);
  862. else if (private)
  863. return private(dev, iwr, cmd, info, handler);
  864. }
  865. /* Old driver API : call driver ioctl handler */
  866. if (dev->netdev_ops->ndo_do_ioctl)
  867. return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
  868. return -EOPNOTSUPP;
  869. }
  870. /* If command is `set a parameter', or `get the encoding parameters',
  871. * check if the user has the right to do it.
  872. */
  873. static int wext_permission_check(unsigned int cmd)
  874. {
  875. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
  876. cmd == SIOCGIWENCODEEXT) &&
  877. !capable(CAP_NET_ADMIN))
  878. return -EPERM;
  879. return 0;
  880. }
  881. /* entry point from dev ioctl */
  882. static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
  883. unsigned int cmd, struct iw_request_info *info,
  884. wext_ioctl_func standard,
  885. wext_ioctl_func private)
  886. {
  887. int ret = wext_permission_check(cmd);
  888. if (ret)
  889. return ret;
  890. dev_load(net, ifr->ifr_name);
  891. rtnl_lock();
  892. ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
  893. rtnl_unlock();
  894. return ret;
  895. }
  896. /*
  897. * Wrapper to call a standard Wireless Extension handler.
  898. * We do various checks and also take care of moving data between
  899. * user space and kernel space.
  900. */
  901. static int ioctl_standard_call(struct net_device * dev,
  902. struct iwreq *iwr,
  903. unsigned int cmd,
  904. struct iw_request_info *info,
  905. iw_handler handler)
  906. {
  907. const struct iw_ioctl_description * descr;
  908. int ret = -EINVAL;
  909. /* Get the description of the IOCTL */
  910. if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
  911. return -EOPNOTSUPP;
  912. descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
  913. /* Check if we have a pointer to user space data or not */
  914. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  915. /* No extra arguments. Trivial to handle */
  916. ret = handler(dev, info, &(iwr->u), NULL);
  917. /* Generate an event to notify listeners of the change */
  918. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  919. ((ret == 0) || (ret == -EIWCOMMIT)))
  920. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  921. } else {
  922. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  923. handler, dev, info);
  924. }
  925. /* Call commit handler if needed and defined */
  926. if (ret == -EIWCOMMIT)
  927. ret = call_commit_handler(dev);
  928. /* Here, we will generate the appropriate event if needed */
  929. return ret;
  930. }
  931. int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
  932. void __user *arg)
  933. {
  934. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  935. int ret;
  936. ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
  937. ioctl_standard_call,
  938. ioctl_private_call);
  939. if (ret >= 0 &&
  940. IW_IS_GET(cmd) &&
  941. copy_to_user(arg, ifr, sizeof(struct iwreq)))
  942. return -EFAULT;
  943. return ret;
  944. }
  945. #ifdef CONFIG_COMPAT
  946. static int compat_standard_call(struct net_device *dev,
  947. struct iwreq *iwr,
  948. unsigned int cmd,
  949. struct iw_request_info *info,
  950. iw_handler handler)
  951. {
  952. const struct iw_ioctl_description *descr;
  953. struct compat_iw_point *iwp_compat;
  954. struct iw_point iwp;
  955. int err;
  956. descr = standard_ioctl + IW_IOCTL_IDX(cmd);
  957. if (descr->header_type != IW_HEADER_TYPE_POINT)
  958. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  959. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  960. iwp.pointer = compat_ptr(iwp_compat->pointer);
  961. iwp.length = iwp_compat->length;
  962. iwp.flags = iwp_compat->flags;
  963. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  964. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  965. iwp_compat->length = iwp.length;
  966. iwp_compat->flags = iwp.flags;
  967. return err;
  968. }
  969. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  970. unsigned long arg)
  971. {
  972. void __user *argp = (void __user *)arg;
  973. struct iw_request_info info;
  974. struct iwreq iwr;
  975. char *colon;
  976. int ret;
  977. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  978. return -EFAULT;
  979. iwr.ifr_name[IFNAMSIZ-1] = 0;
  980. colon = strchr(iwr.ifr_name, ':');
  981. if (colon)
  982. *colon = 0;
  983. info.cmd = cmd;
  984. info.flags = IW_REQUEST_FLAG_COMPAT;
  985. ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
  986. compat_standard_call,
  987. compat_private_call);
  988. if (ret >= 0 &&
  989. IW_IS_GET(cmd) &&
  990. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  991. return -EFAULT;
  992. return ret;
  993. }
  994. #endif