wext-core.c 29 KB

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